codeobject.h 7.9 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; // the block id of this bytecode
  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. std::vector<int> id;
  35. int parent; // parent index in co_blocks
  36. int start; // start index of this block in co_code, inclusive
  37. int end; // end index of this block in co_code, exclusive
  38. std::string to_string() const {
  39. if(parent == -1) return "";
  40. std::string s = "[";
  41. for(int i = 0; i < id.size(); i++){
  42. s += std::to_string(id[i]);
  43. if(i != id.size()-1) s += "-";
  44. }
  45. s += ": type=";
  46. s += std::to_string(type);
  47. s += "]";
  48. return s;
  49. }
  50. bool operator==(const std::vector<int>& other) const{ return id == other; }
  51. bool operator!=(const std::vector<int>& other) const{ return id != other; }
  52. int depth() const{ return id.size(); }
  53. };
  54. struct CodeObject {
  55. _Source src;
  56. _Str name;
  57. CodeObject(_Source src, _Str name) {
  58. this->src = src;
  59. this->name = name;
  60. }
  61. std::vector<Bytecode> co_code;
  62. PyVarList co_consts;
  63. std::vector<std::pair<_Str, NameScope>> co_names;
  64. std::vector<_Str> co_global_names;
  65. std::vector<CodeBlock> co_blocks = { CodeBlock{NO_BLOCK, {}, -1} };
  66. // tmp variables
  67. int _currBlockIndex = 0;
  68. bool __isCurrBlockLoop() const {
  69. return co_blocks[_currBlockIndex].type == FOR_LOOP || co_blocks[_currBlockIndex].type == WHILE_LOOP;
  70. }
  71. void __enter_block(CodeBlockType type){
  72. const CodeBlock& currBlock = co_blocks[_currBlockIndex];
  73. std::vector<int> copy(currBlock.id);
  74. copy.push_back(-1);
  75. int t = 0;
  76. while(true){
  77. copy[copy.size()-1] = t;
  78. auto it = std::find(co_blocks.begin(), co_blocks.end(), copy);
  79. if(it == co_blocks.end()) break;
  80. t++;
  81. }
  82. co_blocks.push_back(CodeBlock{type, copy, _currBlockIndex, (int)co_code.size()});
  83. _currBlockIndex = co_blocks.size()-1;
  84. }
  85. void __exit_block(){
  86. co_blocks[_currBlockIndex].end = co_code.size();
  87. _currBlockIndex = co_blocks[_currBlockIndex].parent;
  88. if(_currBlockIndex < 0) UNREACHABLE();
  89. }
  90. // for goto use
  91. // goto/label should be put at toplevel statements
  92. emhash8::HashMap<_Str, int> co_labels;
  93. void add_label(const _Str& label){
  94. if(co_labels.find(label) != co_labels.end()){
  95. _Str msg = "label '" + label + "' already exists";
  96. throw std::runtime_error(msg.c_str());
  97. }
  98. co_labels[label] = co_code.size();
  99. }
  100. int add_name(_Str name, NameScope scope){
  101. if(scope == NAME_LOCAL && std::find(co_global_names.begin(), co_global_names.end(), name) != co_global_names.end()){
  102. scope = NAME_GLOBAL;
  103. }
  104. auto p = std::make_pair(name, scope);
  105. for(int i=0; i<co_names.size(); i++){
  106. if(co_names[i] == p) return i;
  107. }
  108. co_names.push_back(p);
  109. return co_names.size() - 1;
  110. }
  111. int add_const(PyVar v){
  112. co_consts.push_back(v);
  113. return co_consts.size() - 1;
  114. }
  115. void optimize_level_1(){
  116. for(int i=0; i<co_code.size(); i++){
  117. if(co_code[i].op >= OP_BINARY_OP && co_code[i].op <= OP_CONTAINS_OP){
  118. for(int j=0; j<2; j++){
  119. Bytecode& bc = co_code[i-j-1];
  120. if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
  121. if(bc.op == OP_LOAD_NAME_REF){
  122. bc.op = OP_LOAD_NAME;
  123. }
  124. }else{
  125. break;
  126. }
  127. }
  128. }else if(co_code[i].op == OP_CALL){
  129. int ARGC = co_code[i].arg & 0xFFFF;
  130. int KWARGC = (co_code[i].arg >> 16) & 0xFFFF;
  131. if(KWARGC != 0) continue;
  132. for(int j=0; j<ARGC+1; j++){
  133. Bytecode& bc = co_code[i-j-1];
  134. if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
  135. if(bc.op == OP_LOAD_NAME_REF){
  136. bc.op = OP_LOAD_NAME;
  137. }
  138. }else{
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. }
  145. void optimize(int level=1){
  146. optimize_level_1();
  147. }
  148. };
  149. class Frame {
  150. private:
  151. std::vector<PyVar> s_data;
  152. int ip = -1;
  153. int next_ip = 0;
  154. public:
  155. const _Code code;
  156. PyVar _module;
  157. pkpy::shared_ptr<PyVarDict> _locals;
  158. inline PyVarDict& f_locals() noexcept { return *_locals; }
  159. inline PyVarDict& f_globals() noexcept { return _module->attribs; }
  160. Frame(const _Code code, PyVar _module, pkpy::shared_ptr<PyVarDict> _locals)
  161. : code(code), _module(_module), _locals(_locals) {
  162. }
  163. inline const Bytecode& next_bytecode() {
  164. ip = next_ip;
  165. next_ip = ip + 1;
  166. return code->co_code[ip];
  167. }
  168. _Str curr_snapshot(){
  169. int line = code->co_code[ip].line;
  170. return code->src->snapshot(line);
  171. }
  172. inline int stack_size() const{ return s_data.size(); }
  173. inline bool has_next_bytecode() const{ return next_ip < code->co_code.size(); }
  174. inline PyVar pop(){
  175. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  176. PyVar v = std::move(s_data.back());
  177. s_data.pop_back();
  178. return v;
  179. }
  180. inline void __pop(){
  181. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  182. s_data.pop_back();
  183. }
  184. inline void try_deref(VM*, PyVar&);
  185. inline PyVar pop_value(VM* vm){
  186. PyVar value = pop();
  187. try_deref(vm, value);
  188. return value;
  189. }
  190. inline PyVar top_value(VM* vm){
  191. PyVar value = top();
  192. try_deref(vm, value);
  193. return value;
  194. }
  195. inline PyVar& top(){
  196. if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
  197. return s_data.back();
  198. }
  199. inline PyVar top_value_offset(VM* vm, int n){
  200. PyVar value = s_data[s_data.size() + n];
  201. try_deref(vm, value);
  202. return value;
  203. }
  204. template<typename T>
  205. inline void push(T&& obj){ s_data.push_back(std::forward<T>(obj)); }
  206. inline void jump_abs(int i){ next_ip = i; }
  207. inline void jump_rel(int i){ next_ip = ip + i; }
  208. void jump_abs_safe(int target){
  209. const Bytecode& prev = code->co_code[ip];
  210. int i = prev.block;
  211. next_ip = target;
  212. if(next_ip >= code->co_code.size()){
  213. while(i>=0){
  214. if(code->co_blocks[i].type == FOR_LOOP) pop();
  215. i = code->co_blocks[i].parent;
  216. }
  217. }else{
  218. const Bytecode& next = code->co_code[target];
  219. while(i>=0 && i!=next.block){
  220. if(code->co_blocks[i].type == FOR_LOOP) pop();
  221. i = code->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. int new_size = s_data.size() - n;
  228. if(new_size < 0) throw std::runtime_error("stack_size() < n");
  229. pkpy::Args v(n);
  230. for(int i=n-1; i>=0; i--){
  231. v[i] = std::move(s_data[new_size + i]);
  232. try_deref(vm, v[i]);
  233. }
  234. s_data.resize(new_size);
  235. return v;
  236. }
  237. pkpy::Args pop_n_reversed(int n){
  238. pkpy::Args v(n);
  239. for(int i=n-1; i>=0; i--) v[i] = pop();
  240. return v;
  241. }
  242. };