ceval.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #pragma once
  2. #include "common.h"
  3. #include "vm.h"
  4. namespace pkpy{
  5. #define DISPATCH() goto __NEXT_STEP
  6. inline PyObject* VM::run_frame(Frame* frame){
  7. __NEXT_STEP:;
  8. /* NOTE:
  9. * Be aware of accidental gc!
  10. * DO NOT leave any strong reference of PyObject* in the C stack
  11. * For example, frame->popx() returns a strong reference which may be dangerous
  12. * `Args` containing strong references is safe if it is passed to `call` or `fast_call`
  13. */
  14. //heap._auto_collect(this);
  15. const Bytecode& byte = frame->next_bytecode();
  16. switch (byte.op)
  17. {
  18. case OP_NO_OP: DISPATCH();
  19. /*****************************************/
  20. case OP_POP_TOP: frame->pop(); DISPATCH();
  21. case OP_DUP_TOP: frame->push(frame->top()); DISPATCH();
  22. case OP_ROT_TWO: std::swap(frame->top(), frame->top_1()); DISPATCH();
  23. case OP_PRINT_EXPR: {
  24. PyObject* obj = frame->top(); // use top() to avoid accidental gc
  25. if(obj != None) *_stdout << CAST(Str, asRepr(obj)) << '\n';
  26. frame->pop();
  27. } DISPATCH();
  28. /*****************************************/
  29. case OP_LOAD_CONST: frame->push(frame->co->consts[byte.arg]); DISPATCH();
  30. case OP_LOAD_NONE: frame->push(None); DISPATCH();
  31. case OP_LOAD_TRUE: frame->push(True); DISPATCH();
  32. case OP_LOAD_FALSE: frame->push(False); DISPATCH();
  33. case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); DISPATCH();
  34. case OP_LOAD_BUILTIN_EVAL: frame->push(builtins->attr(m_eval)); DISPATCH();
  35. case OP_LOAD_FUNCTION: {
  36. FuncDecl_ decl = frame->co->func_decls[byte.arg];
  37. PyObject* obj = VAR(Function({decl, frame->_module, frame->_locals}));
  38. frame->push(obj);
  39. } DISPATCH();
  40. /*****************************************/
  41. case OP_LOAD_NAME: {
  42. StrName name = frame->co->names[byte.arg];
  43. PyObject* val;
  44. int i = 0; // names[0] is ensured to be non-null
  45. do{
  46. val = frame->names[i++]->try_get(name);
  47. if(val != nullptr){
  48. frame->push(val);
  49. DISPATCH();
  50. }
  51. }while(frame->names[i] != nullptr);
  52. vm->NameError(name);
  53. } DISPATCH();
  54. case OP_LOAD_ATTR: {
  55. PyObject* a = frame->top();
  56. StrName name = frame->co->names[byte.arg];
  57. frame->top() = getattr(a, name);
  58. } DISPATCH();
  59. case OP_LOAD_SUBSCR: {
  60. Args args(2);
  61. args[1] = frame->popx(); // b
  62. args[0] = frame->top(); // a
  63. frame->top() = fast_call(__getitem__, std::move(args));
  64. } DISPATCH();
  65. case OP_STORE_LOCAL: {
  66. StrName name = frame->co->names[byte.arg];
  67. frame->f_locals().set(name, frame->popx());
  68. } DISPATCH();
  69. case OP_STORE_GLOBAL: {
  70. StrName name = frame->co->names[byte.arg];
  71. frame->f_globals().set(name, frame->popx());
  72. } DISPATCH();
  73. case OP_STORE_ATTR: {
  74. StrName name = frame->co->names[byte.arg];
  75. PyObject* a = frame->top();
  76. PyObject* val = frame->top_1();
  77. setattr(a, name, val);
  78. frame->pop_n(2);
  79. } DISPATCH();
  80. case OP_STORE_SUBSCR: {
  81. Args args(3);
  82. args[1] = frame->popx(); // b
  83. args[0] = frame->popx(); // a
  84. args[2] = frame->popx(); // val
  85. fast_call(__setitem__, std::move(args));
  86. } DISPATCH();
  87. case OP_DELETE_LOCAL: {
  88. StrName name = frame->co->names[byte.arg];
  89. if(frame->f_locals().contains(name)){
  90. frame->f_locals().erase(name);
  91. }else{
  92. NameError(name);
  93. }
  94. } DISPATCH();
  95. case OP_DELETE_GLOBAL: {
  96. StrName name = frame->co->names[byte.arg];
  97. if(frame->f_globals().contains(name)){
  98. frame->f_globals().erase(name);
  99. }else{
  100. NameError(name);
  101. }
  102. } DISPATCH();
  103. case OP_DELETE_ATTR: {
  104. PyObject* a = frame->popx();
  105. StrName name = frame->co->names[byte.arg];
  106. if(!a->is_attr_valid()) TypeError("cannot delete attribute");
  107. if(!a->attr().contains(name)) AttributeError(a, name);
  108. a->attr().erase(name);
  109. } DISPATCH();
  110. case OP_DELETE_SUBSCR: {
  111. PyObject* b = frame->popx();
  112. PyObject* a = frame->popx();
  113. fast_call(__delitem__, Args{a, b});
  114. } DISPATCH();
  115. /*****************************************/
  116. case OP_BUILD_LIST:
  117. frame->push(VAR(frame->popx_n_reversed(byte.arg).to_list()));
  118. DISPATCH();
  119. case OP_BUILD_DICT: {
  120. PyObject* t = VAR(frame->popx_n_reversed(byte.arg));
  121. PyObject* obj = call(builtins->attr(m_dict), Args{t});
  122. frame->push(obj);
  123. } DISPATCH();
  124. case OP_BUILD_SET: {
  125. PyObject* t = VAR(frame->popx_n_reversed(byte.arg));
  126. PyObject* obj = call(builtins->attr(m_set), Args{t});
  127. frame->push(obj);
  128. } DISPATCH();
  129. case OP_BUILD_SLICE: {
  130. PyObject* step = frame->popx();
  131. PyObject* stop = frame->popx();
  132. PyObject* start = frame->popx();
  133. Slice s;
  134. if(start != None) s.start = CAST(int, start);
  135. if(stop != None) s.stop = CAST(int, stop);
  136. if(step != None) s.step = CAST(int, step);
  137. frame->push(VAR(s));
  138. } DISPATCH();
  139. case OP_BUILD_TUPLE: {
  140. Tuple items = frame->popx_n_reversed(byte.arg);
  141. frame->push(VAR(std::move(items)));
  142. } DISPATCH();
  143. case OP_BUILD_STRING: {
  144. // asStr() may run extra bytecode
  145. // so we use top_n_reversed() in order to avoid accidental gc
  146. Args items = frame->top_n_reversed(byte.arg);
  147. StrStream ss;
  148. for(int i=0; i<items.size(); i++) ss << CAST(Str, asStr(items[i]));
  149. frame->pop_n(byte.arg);
  150. frame->push(VAR(ss.str()));
  151. } DISPATCH();
  152. /*****************************************/
  153. case OP_BINARY_OP: {
  154. Args args(2);
  155. args[1] = frame->popx(); // lhs
  156. args[0] = frame->top(); // rhs
  157. frame->top() = fast_call(BINARY_SPECIAL_METHODS[byte.arg], std::move(args));
  158. } DISPATCH();
  159. case OP_COMPARE_OP: {
  160. Args args(2);
  161. args[1] = frame->popx(); // lhs
  162. args[0] = frame->top(); // rhs
  163. frame->top() = fast_call(COMPARE_SPECIAL_METHODS[byte.arg], std::move(args));
  164. } DISPATCH();
  165. case OP_BITWISE_OP: {
  166. Args args(2);
  167. args[1] = frame->popx(); // lhs
  168. args[0] = frame->top(); // rhs
  169. frame->top() = fast_call(BITWISE_SPECIAL_METHODS[byte.arg], std::move(args));
  170. } DISPATCH();
  171. case OP_IS_OP: {
  172. PyObject* rhs = frame->popx();
  173. PyObject* lhs = frame->top();
  174. bool ret_c = lhs == rhs;
  175. if(byte.arg == 1) ret_c = !ret_c;
  176. frame->top() = VAR(ret_c);
  177. } DISPATCH();
  178. case OP_CONTAINS_OP: {
  179. Args args(2);
  180. args[0] = frame->popx();
  181. args[1] = frame->top();
  182. PyObject* ret = fast_call(__contains__, std::move(args));
  183. bool ret_c = CAST(bool, ret);
  184. if(byte.arg == 1) ret_c = !ret_c;
  185. frame->top() = VAR(ret_c);
  186. } DISPATCH();
  187. /*****************************************/
  188. case OP_JUMP_ABSOLUTE: frame->jump_abs(byte.arg); DISPATCH();
  189. case OP_POP_JUMP_IF_FALSE:
  190. if(!asBool(frame->popx())) frame->jump_abs(byte.arg);
  191. DISPATCH();
  192. case OP_JUMP_IF_TRUE_OR_POP:
  193. if(asBool(frame->top()) == true) frame->jump_abs(byte.arg);
  194. else frame->pop();
  195. DISPATCH();
  196. case OP_JUMP_IF_FALSE_OR_POP:
  197. if(asBool(frame->top()) == false) frame->jump_abs(byte.arg);
  198. else frame->pop();
  199. DISPATCH();
  200. case OP_LOOP_CONTINUE: {
  201. int target = frame->co->blocks[byte.block].start;
  202. frame->jump_abs(target);
  203. } DISPATCH();
  204. case OP_LOOP_BREAK: {
  205. int target = frame->co->blocks[byte.block].end;
  206. frame->jump_abs_break(target);
  207. } DISPATCH();
  208. case OP_GOTO: {
  209. StrName label = frame->co->names[byte.arg];
  210. auto it = frame->co->labels.find(label);
  211. if(it == frame->co->labels.end()) _error("KeyError", "label " + label.str().escape(true) + " not found");
  212. frame->jump_abs_break(it->second);
  213. } DISPATCH();
  214. /*****************************************/
  215. // TODO: examine this later
  216. case OP_CALL: case OP_CALL_UNPACK: {
  217. Args args = frame->popx_n_reversed(byte.arg);
  218. if(byte.op == OP_CALL_UNPACK) unpack_args(args);
  219. PyObject* callable = frame->popx();
  220. PyObject* ret = call(callable, std::move(args), no_arg(), true);
  221. if(ret == _py_op_call) return ret;
  222. frame->push(std::move(ret));
  223. } DISPATCH();
  224. case OP_CALL_KWARGS: case OP_CALL_KWARGS_UNPACK: {
  225. int ARGC = byte.arg & 0xFFFF;
  226. int KWARGC = (byte.arg >> 16) & 0xFFFF;
  227. Args kwargs = frame->popx_n_reversed(KWARGC*2);
  228. Args args = frame->popx_n_reversed(ARGC);
  229. if(byte.op == OP_CALL_KWARGS_UNPACK) unpack_args(args);
  230. PyObject* callable = frame->popx();
  231. PyObject* ret = call(callable, std::move(args), kwargs, true);
  232. if(ret == _py_op_call) return ret;
  233. frame->push(std::move(ret));
  234. } DISPATCH();
  235. case OP_RETURN_VALUE: return frame->popx();
  236. /*****************************************/
  237. case OP_LIST_APPEND: {
  238. PyObject* obj = frame->popx();
  239. List& list = CAST(List&, frame->top_1());
  240. list.push_back(obj);
  241. } DISPATCH();
  242. case OP_DICT_ADD: {
  243. PyObject* kv = frame->popx();
  244. // we do copy here to avoid accidental gc in `kv`
  245. // TODO: optimize to avoid copy
  246. call(frame->top_1(), __setitem__, CAST(Tuple, kv));
  247. } DISPATCH();
  248. case OP_SET_ADD: {
  249. PyObject* obj = frame->popx();
  250. call(frame->top_1(), m_add, Args{obj});
  251. } DISPATCH();
  252. /*****************************************/
  253. case OP_UNARY_NEGATIVE:
  254. frame->top() = num_negated(frame->top());
  255. DISPATCH();
  256. case OP_UNARY_NOT:
  257. frame->top() = VAR(!asBool(frame->top()));
  258. DISPATCH();
  259. case OP_UNARY_STAR:
  260. frame->top() = VAR(StarWrapper(frame->top()));
  261. DISPATCH();
  262. /*****************************************/
  263. case OP_GET_ITER:
  264. frame->top() = asIter(frame->top());
  265. DISPATCH();
  266. case OP_FOR_ITER: {
  267. BaseIter* it = PyIter_AS_C(frame->top());
  268. PyObject* obj = it->next();
  269. if(obj != nullptr){
  270. frame->push(obj);
  271. }else{
  272. int target = frame->co->blocks[byte.block].end;
  273. frame->jump_abs_break(target);
  274. }
  275. } DISPATCH();
  276. /*****************************************/
  277. case OP_IMPORT_NAME: {
  278. StrName name = frame->co->names[byte.arg];
  279. PyObject* ext_mod = _modules.try_get(name);
  280. if(ext_mod == nullptr){
  281. Str source;
  282. auto it = _lazy_modules.find(name);
  283. if(it == _lazy_modules.end()){
  284. bool ok = false;
  285. source = _read_file_cwd(name.str() + ".py", &ok);
  286. if(!ok) _error("ImportError", "module " + name.str().escape(true) + " not found");
  287. }else{
  288. source = it->second;
  289. _lazy_modules.erase(it);
  290. }
  291. CodeObject_ code = compile(source, name.str(), EXEC_MODE);
  292. PyObject* new_mod = new_module(name);
  293. _exec(code, new_mod);
  294. new_mod->attr()._try_perfect_rehash();
  295. }
  296. frame->push(ext_mod);
  297. } DISPATCH();
  298. case OP_IMPORT_STAR: {
  299. PyObject* obj = frame->popx();
  300. for(auto& [name, value]: obj->attr().items()){
  301. Str s = name.str();
  302. if(s.empty() || s[0] == '_') continue;
  303. frame->f_globals().set(name, value);
  304. }
  305. }; DISPATCH();
  306. /*****************************************/
  307. /*****************************************/
  308. // case OP_SETUP_DECORATOR: DISPATCH();
  309. // case OP_BEGIN_CLASS: {
  310. // StrName name = frame->co->names[byte.arg];
  311. // PyObject* clsBase = frame->popx();
  312. // if(clsBase == None) clsBase = _t(tp_object);
  313. // check_type(clsBase, tp_type);
  314. // PyObject* cls = new_type_object(frame->_module, name, OBJ_GET(Type, clsBase));
  315. // frame->push(cls);
  316. // } DISPATCH();
  317. // case OP_END_CLASS: {
  318. // PyObject* cls = frame->popx();
  319. // cls->attr()._try_perfect_rehash();
  320. // }; DISPATCH();
  321. // case OP_STORE_CLASS_ATTR: {
  322. // StrName name = frame->co->names[byte.arg];
  323. // PyObject* obj = frame->popx();
  324. // PyObject* cls = frame->top();
  325. // cls->attr().set(name, obj);
  326. // } DISPATCH();
  327. // case OP_ASSERT: {
  328. // PyObject* _msg = frame->pop_value(this);
  329. // Str msg = CAST(Str, asStr(_msg));
  330. // PyObject* expr = frame->pop_value(this);
  331. // if(asBool(expr) != True) _error("AssertionError", msg);
  332. // } DISPATCH();
  333. // case OP_EXCEPTION_MATCH: {
  334. // const auto& e = CAST(Exception&, frame->top());
  335. // StrName name = frame->co->names[byte.arg].first;
  336. // frame->push(VAR(e.match_type(name)));
  337. // } DISPATCH();
  338. // case OP_RAISE: {
  339. // PyObject* obj = frame->pop_value(this);
  340. // Str msg = obj == None ? "" : CAST(Str, asStr(obj));
  341. // StrName type = frame->co->names[byte.arg].first;
  342. // _error(type, msg);
  343. // } DISPATCH();
  344. // case OP_RE_RAISE: _raise(); DISPATCH();
  345. // case OP_YIELD_VALUE: return _py_op_yield;
  346. // // TODO: using "goto" inside with block may cause __exit__ not called
  347. // case OP_WITH_ENTER: call(frame->pop_value(this), __enter__, no_arg()); DISPATCH();
  348. // case OP_WITH_EXIT: call(frame->pop_value(this), __exit__, no_arg()); DISPATCH();
  349. // case OP_TRY_BLOCK_ENTER: frame->on_try_block_enter(); DISPATCH();
  350. // case OP_TRY_BLOCK_EXIT: frame->on_try_block_exit(); DISPATCH();
  351. default: throw std::runtime_error(Str("opcode ") + OP_NAMES[byte.op] + " is not implemented");
  352. }
  353. UNREACHABLE();
  354. }
  355. #undef DISPATCH
  356. } // namespace pkpy