codeobject.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #pragma once
  2. #include "obj.h"
  3. #include "ref.h"
  4. #include "error.h"
  5. enum Opcode {
  6. #define OPCODE(name) OP_##name,
  7. #include "opcodes.h"
  8. #undef OPCODE
  9. };
  10. static const char* OP_NAMES[] = {
  11. #define OPCODE(name) #name,
  12. #include "opcodes.h"
  13. #undef OPCODE
  14. };
  15. struct Bytecode{
  16. uint8_t op;
  17. int arg;
  18. int line;
  19. uint16_t block;
  20. };
  21. _Str pad(const _Str& s, const int n){
  22. if(s.size() >= n) return s.substr(0, n);
  23. return s + std::string(n - s.size(), ' ');
  24. }
  25. enum CodeBlockType {
  26. NO_BLOCK,
  27. FOR_LOOP,
  28. WHILE_LOOP,
  29. CONTEXT_MANAGER,
  30. TRY_EXCEPT,
  31. };
  32. struct CodeBlock {
  33. CodeBlockType type;
  34. int parent; // parent index in blocks
  35. int start; // start index of this block in co_code, inclusive
  36. int end; // end index of this block in co_code, exclusive
  37. std::string to_string() const {
  38. if(parent == -1) return "";
  39. return "[B:" + std::to_string(type) + "]";
  40. }
  41. };
  42. struct CodeObject {
  43. pkpy::shared_ptr<SourceData> src;
  44. _Str name;
  45. CodeObject(pkpy::shared_ptr<SourceData> src, _Str name) {
  46. this->src = src;
  47. this->name = name;
  48. }
  49. std::vector<Bytecode> co_code;
  50. PyVarList consts;
  51. std::vector<std::pair<_Str, NameScope>> names;
  52. emhash8::HashMap<_Str, int> global_names;
  53. std::vector<CodeBlock> blocks = { CodeBlock{NO_BLOCK, -1} };
  54. emhash8::HashMap<_Str, int> labels;
  55. bool add_label(const _Str& label){
  56. if(labels.contains(label)) return false;
  57. labels[label] = co_code.size();
  58. return true;
  59. }
  60. int add_name(_Str name, NameScope scope){
  61. if(scope == NAME_LOCAL && global_names.contains(name)) scope = NAME_GLOBAL;
  62. auto p = std::make_pair(name, scope);
  63. for(int i=0; i<names.size(); i++){
  64. if(names[i] == p) return i;
  65. }
  66. names.push_back(p);
  67. return names.size() - 1;
  68. }
  69. int add_const(PyVar v){
  70. consts.push_back(v);
  71. return consts.size() - 1;
  72. }
  73. void optimize_level_1(){
  74. for(int i=0; i<co_code.size(); i++){
  75. if(co_code[i].op >= OP_BINARY_OP && co_code[i].op <= OP_CONTAINS_OP){
  76. for(int j=0; j<2; j++){
  77. Bytecode& bc = co_code[i-j-1];
  78. if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
  79. if(bc.op == OP_LOAD_NAME_REF){
  80. bc.op = OP_LOAD_NAME;
  81. }
  82. }else{
  83. break;
  84. }
  85. }
  86. }else if(co_code[i].op == OP_CALL){
  87. int ARGC = co_code[i].arg & 0xFFFF;
  88. int KWARGC = (co_code[i].arg >> 16) & 0xFFFF;
  89. if(KWARGC != 0) continue;
  90. for(int j=0; j<ARGC+1; j++){
  91. Bytecode& bc = co_code[i-j-1];
  92. if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
  93. if(bc.op == OP_LOAD_NAME_REF){
  94. bc.op = OP_LOAD_NAME;
  95. }
  96. }else{
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. void optimize(int level=1){
  104. optimize_level_1();
  105. }
  106. /************************************************/
  107. int _curr_block_i = 0;
  108. bool _is_curr_block_loop() const {
  109. return blocks[_curr_block_i].type == FOR_LOOP || blocks[_curr_block_i].type == WHILE_LOOP;
  110. }
  111. void _enter_block(CodeBlockType type){
  112. blocks.push_back(CodeBlock{type, _curr_block_i, (int)co_code.size()});
  113. _curr_block_i = blocks.size()-1;
  114. }
  115. void _exit_block(){
  116. blocks[_curr_block_i].end = co_code.size();
  117. _curr_block_i = blocks[_curr_block_i].parent;
  118. if(_curr_block_i < 0) UNREACHABLE();
  119. }
  120. /************************************************/
  121. };
  122. static thread_local i64 kFrameGlobalId = 0;
  123. struct Frame {
  124. std::vector<PyVar> _data;
  125. int _ip = -1;
  126. int _next_ip = 0;
  127. const _Code co;
  128. PyVar _module;
  129. pkpy::shared_ptr<PyVarDict> _locals;
  130. const i64 id;
  131. inline PyVarDict& f_locals() noexcept { return *_locals; }
  132. inline PyVarDict& f_globals() noexcept { return _module->attribs; }
  133. Frame(const _Code co, PyVar _module, pkpy::shared_ptr<PyVarDict> _locals)
  134. : co(co), _module(_module), _locals(_locals), id(kFrameGlobalId++) { }
  135. inline const Bytecode& next_bytecode() {
  136. _ip = _next_ip;
  137. _next_ip = _ip + 1;
  138. return co->co_code[_ip];
  139. }
  140. _Str snapshot(){
  141. int line = co->co_code[_ip].line;
  142. return co->src->snapshot(line);
  143. }
  144. _Str stack_info(){
  145. _StrStream ss;
  146. ss << "[";
  147. for(int i=0; i<_data.size(); i++){
  148. ss << OBJ_TP_NAME(_data[i]);
  149. if(i != _data.size()-1) ss << ", ";
  150. }
  151. ss << "]";
  152. return ss.str();
  153. }
  154. inline bool has_next_bytecode() const {
  155. return _next_ip < co->co_code.size();
  156. }
  157. inline PyVar pop(){
  158. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  159. PyVar v = std::move(_data.back());
  160. _data.pop_back();
  161. return v;
  162. }
  163. inline void __pop(){
  164. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  165. _data.pop_back();
  166. }
  167. inline void try_deref(VM*, PyVar&);
  168. inline PyVar pop_value(VM* vm){
  169. PyVar value = pop();
  170. try_deref(vm, value);
  171. return value;
  172. }
  173. inline PyVar top_value(VM* vm){
  174. PyVar value = top();
  175. try_deref(vm, value);
  176. return value;
  177. }
  178. inline PyVar& top(){
  179. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  180. return _data.back();
  181. }
  182. inline PyVar top_value_offset(VM* vm, int n){
  183. PyVar value = _data[_data.size() + n];
  184. try_deref(vm, value);
  185. return value;
  186. }
  187. template<typename T>
  188. inline void push(T&& obj){ _data.push_back(std::forward<T>(obj)); }
  189. inline void jump_abs(int i){ _next_ip = i; }
  190. inline void jump_rel(int i){ _next_ip += i; }
  191. std::stack<std::pair<int, std::vector<PyVar>>> s_try_block;
  192. inline void on_try_block_enter(){
  193. s_try_block.push(std::make_pair(co->co_code[_ip].block, _data));
  194. }
  195. inline void on_try_block_exit(){
  196. s_try_block.pop();
  197. }
  198. bool jump_to_exception_handler(){
  199. if(s_try_block.empty()) return false;
  200. PyVar obj = pop();
  201. auto& p = s_try_block.top();
  202. _data = std::move(p.second);
  203. _data.push_back(obj);
  204. _next_ip = co->blocks[p.first].end;
  205. on_try_block_exit();
  206. return true;
  207. }
  208. void jump_abs_safe(int target){
  209. const Bytecode& prev = co->co_code[_ip];
  210. int i = prev.block;
  211. _next_ip = target;
  212. if(_next_ip >= co->co_code.size()){
  213. while(i>=0){
  214. if(co->blocks[i].type == FOR_LOOP) pop();
  215. i = co->blocks[i].parent;
  216. }
  217. }else{
  218. const Bytecode& next = co->co_code[target];
  219. while(i>=0 && i!=next.block){
  220. if(co->blocks[i].type == FOR_LOOP) pop();
  221. i = co->blocks[i].parent;
  222. }
  223. if(i!=next.block) throw std::runtime_error("invalid jump");
  224. }
  225. }
  226. pkpy::Args pop_n_values_reversed(VM* vm, int n){
  227. pkpy::Args v(n);
  228. for(int i=n-1; i>=0; i--){
  229. v[i] = pop();
  230. try_deref(vm, v[i]);
  231. }
  232. return v;
  233. }
  234. pkpy::Args pop_n_reversed(int n){
  235. pkpy::Args v(n);
  236. for(int i=n-1; i>=0; i--) v[i] = pop();
  237. return v;
  238. }
  239. };