codeobject.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. // tmp variables
  56. int _curr_block_i = 0;
  57. bool __is_curr_block_loop() const {
  58. return blocks[_curr_block_i].type == FOR_LOOP || blocks[_curr_block_i].type == WHILE_LOOP;
  59. }
  60. void __enter_block(CodeBlockType type){
  61. blocks.push_back(CodeBlock{type, _curr_block_i, (int)co_code.size()});
  62. _curr_block_i = blocks.size()-1;
  63. }
  64. void __exit_block(){
  65. blocks[_curr_block_i].end = co_code.size();
  66. _curr_block_i = blocks[_curr_block_i].parent;
  67. if(_curr_block_i < 0) UNREACHABLE();
  68. }
  69. bool add_label(const _Str& label){
  70. if(labels.contains(label)) return false;
  71. labels[label] = co_code.size();
  72. return true;
  73. }
  74. int add_name(_Str name, NameScope scope){
  75. if(scope == NAME_LOCAL && global_names.contains(name)) scope = NAME_GLOBAL;
  76. auto p = std::make_pair(name, scope);
  77. for(int i=0; i<names.size(); i++){
  78. if(names[i] == p) return i;
  79. }
  80. names.push_back(p);
  81. return names.size() - 1;
  82. }
  83. int add_const(PyVar v){
  84. consts.push_back(v);
  85. return consts.size() - 1;
  86. }
  87. void optimize_level_1(){
  88. for(int i=0; i<co_code.size(); i++){
  89. if(co_code[i].op >= OP_BINARY_OP && co_code[i].op <= OP_CONTAINS_OP){
  90. for(int j=0; j<2; 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. }else if(co_code[i].op == OP_CALL){
  101. int ARGC = co_code[i].arg & 0xFFFF;
  102. int KWARGC = (co_code[i].arg >> 16) & 0xFFFF;
  103. if(KWARGC != 0) continue;
  104. for(int j=0; j<ARGC+1; j++){
  105. Bytecode& bc = co_code[i-j-1];
  106. if(bc.op >= OP_LOAD_CONST && bc.op <= OP_LOAD_NAME_REF){
  107. if(bc.op == OP_LOAD_NAME_REF){
  108. bc.op = OP_LOAD_NAME;
  109. }
  110. }else{
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. void optimize(int level=1){
  118. optimize_level_1();
  119. }
  120. };
  121. static thread_local i64 kFrameGlobalId = 0;
  122. struct Frame {
  123. std::vector<PyVar> _data;
  124. int _ip = -1;
  125. int _next_ip = 0;
  126. const _Code co;
  127. PyVar _module;
  128. pkpy::shared_ptr<PyVarDict> _locals;
  129. const i64 id;
  130. inline PyVarDict& f_locals() noexcept { return *_locals; }
  131. inline PyVarDict& f_globals() noexcept { return _module->attribs; }
  132. Frame(const _Code co, PyVar _module, pkpy::shared_ptr<PyVarDict> _locals)
  133. : co(co), _module(_module), _locals(_locals), id(kFrameGlobalId++) { }
  134. inline const Bytecode& next_bytecode() {
  135. _ip = _next_ip;
  136. _next_ip = _ip + 1;
  137. return co->co_code[_ip];
  138. }
  139. _Str curr_snapshot(){
  140. int line = co->co_code[_ip].line;
  141. return co->src->snapshot(line);
  142. }
  143. _Str stack_info(){
  144. _StrStream ss;
  145. ss << "[";
  146. for(int i=0; i<_data.size(); i++){
  147. ss << OBJ_TP_NAME(_data[i]);
  148. if(i != _data.size()-1) ss << ", ";
  149. }
  150. ss << "]";
  151. return ss.str();
  152. }
  153. inline bool has_next_bytecode() const {
  154. return _next_ip < co->co_code.size();
  155. }
  156. inline PyVar pop(){
  157. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  158. PyVar v = std::move(_data.back());
  159. _data.pop_back();
  160. return v;
  161. }
  162. inline void __pop(){
  163. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  164. _data.pop_back();
  165. }
  166. inline void try_deref(VM*, PyVar&);
  167. inline PyVar pop_value(VM* vm){
  168. PyVar value = pop();
  169. try_deref(vm, value);
  170. return value;
  171. }
  172. inline PyVar top_value(VM* vm){
  173. PyVar value = top();
  174. try_deref(vm, value);
  175. return value;
  176. }
  177. inline PyVar& top(){
  178. if(_data.empty()) throw std::runtime_error("_data.empty() is true");
  179. return _data.back();
  180. }
  181. inline PyVar top_value_offset(VM* vm, int n){
  182. PyVar value = _data[_data.size() + n];
  183. try_deref(vm, value);
  184. return value;
  185. }
  186. template<typename T>
  187. inline void push(T&& obj){ _data.push_back(std::forward<T>(obj)); }
  188. inline void jump_abs(int i){ _next_ip = i; }
  189. inline void jump_rel(int i){ _next_ip += i; }
  190. std::stack<std::pair<int, std::vector<PyVar>>> s_try_block;
  191. inline void on_try_block_enter(){
  192. s_try_block.push(std::make_pair(co->co_code[_ip].block, _data));
  193. }
  194. inline void on_try_block_exit(){
  195. s_try_block.pop();
  196. }
  197. bool jump_to_exception_handler(){
  198. if(s_try_block.empty()) return false;
  199. PyVar obj = pop();
  200. auto& p = s_try_block.top();
  201. _data = std::move(p.second);
  202. _data.push_back(obj);
  203. _next_ip = co->blocks[p.first].end;
  204. on_try_block_exit();
  205. return true;
  206. }
  207. void jump_abs_safe(int target){
  208. const Bytecode& prev = co->co_code[_ip];
  209. int i = prev.block;
  210. _next_ip = target;
  211. if(_next_ip >= co->co_code.size()){
  212. while(i>=0){
  213. if(co->blocks[i].type == FOR_LOOP) pop();
  214. i = co->blocks[i].parent;
  215. }
  216. }else{
  217. const Bytecode& next = co->co_code[target];
  218. while(i>=0 && i!=next.block){
  219. if(co->blocks[i].type == FOR_LOOP) pop();
  220. i = co->blocks[i].parent;
  221. }
  222. if(i!=next.block) throw std::runtime_error("invalid jump");
  223. }
  224. }
  225. pkpy::Args pop_n_values_reversed(VM* vm, int n){
  226. pkpy::Args v(n);
  227. for(int i=n-1; i>=0; i--){
  228. v[i] = pop();
  229. try_deref(vm, v[i]);
  230. }
  231. return v;
  232. }
  233. pkpy::Args pop_n_reversed(int n){
  234. pkpy::Args v(n);
  235. for(int i=n-1; i>=0; i--) v[i] = pop();
  236. return v;
  237. }
  238. };