ceval.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. #include "pocketpy/ceval.h"
  2. namespace pkpy{
  3. #define PREDICT_INT_OP(op) \
  4. if(is_small_int(_0) && is_small_int(_1)){ \
  5. TOP() = VAR((PK_BITS(_0)>>2) op (PK_BITS(_1)>>2)); \
  6. DISPATCH() \
  7. }
  8. #define PREDICT_INT_DIV_OP(op) \
  9. if(is_small_int(_0) && is_small_int(_1)){ \
  10. if(_1 == PK_SMALL_INT(0)) ZeroDivisionError(); \
  11. TOP() = VAR((PK_BITS(_0)>>2) op (PK_BITS(_1)>>2)); \
  12. DISPATCH() \
  13. }
  14. #define BINARY_F_COMPARE(func, op, rfunc) \
  15. PyObject* ret; \
  16. const PyTypeInfo* _ti = _inst_type_info(_0); \
  17. if(_ti->m##func){ \
  18. ret = _ti->m##func(this, _0, _1); \
  19. }else{ \
  20. PyObject* self; \
  21. PyObject* _2 = get_unbound_method(_0, func, &self, false); \
  22. if(_2 != nullptr) ret = call_method(self, _2, _1); \
  23. else ret = NotImplemented; \
  24. } \
  25. if(ret == NotImplemented){ \
  26. PyObject* self; \
  27. PyObject* _2 = get_unbound_method(_1, rfunc, &self, false); \
  28. if(_2 != nullptr) ret = call_method(self, _2, _0); \
  29. else BinaryOptError(op, _0, _1); \
  30. if(ret == NotImplemented) BinaryOptError(op, _0, _1); \
  31. }
  32. bool VM::py_lt(PyObject* _0, PyObject* _1){
  33. BINARY_F_COMPARE(__lt__, "<", __gt__);
  34. return ret == True;
  35. }
  36. bool VM::py_le(PyObject* _0, PyObject* _1){
  37. BINARY_F_COMPARE(__le__, "<=", __ge__);
  38. return ret == True;
  39. }
  40. bool VM::py_gt(PyObject* _0, PyObject* _1){
  41. BINARY_F_COMPARE(__gt__, ">", __lt__);
  42. return ret == True;
  43. }
  44. bool VM::py_ge(PyObject* _0, PyObject* _1){
  45. BINARY_F_COMPARE(__ge__, ">=", __le__);
  46. return ret == True;
  47. }
  48. #undef BINARY_F_COMPARE
  49. PyObject* VM::_run_top_frame(){
  50. Frame* frame = top_frame();
  51. const Frame* base_frame = frame;
  52. bool need_raise = false;
  53. while(true){
  54. try{
  55. if(need_raise){ need_raise = false; _raise(); }
  56. /**********************************************************************/
  57. /* NOTE:
  58. * Be aware of accidental gc!
  59. * DO NOT leave any strong reference of PyObject* in the C stack
  60. */
  61. {
  62. #define CEVAL_STEP_CALLBACK() \
  63. if(_ceval_on_step) _ceval_on_step(this, frame, byte); \
  64. if(_profiler) _profiler->_step(callstack.size(), frame); \
  65. if(!_next_breakpoint.empty()) { _next_breakpoint._step(this); }
  66. #define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; }
  67. __NEXT_FRAME:
  68. // cache
  69. const CodeObject* co = frame->co;
  70. const Bytecode* co_codes = co->codes.data();
  71. Bytecode byte = co_codes[frame->next_bytecode()];
  72. CEVAL_STEP_CALLBACK();
  73. #define TARGET(op) case OP_##op:
  74. #define DISPATCH() { byte = co_codes[frame->next_bytecode()]; CEVAL_STEP_CALLBACK(); goto __NEXT_STEP;}
  75. __NEXT_STEP:;
  76. #if PK_DEBUG_CEVAL_STEP
  77. _log_s_data();
  78. #endif
  79. switch ((Opcode)byte.op)
  80. {
  81. TARGET(NO_OP) DISPATCH();
  82. /*****************************************/
  83. TARGET(POP_TOP) POP(); DISPATCH();
  84. TARGET(DUP_TOP) PUSH(TOP()); DISPATCH();
  85. TARGET(ROT_TWO) std::swap(TOP(), SECOND()); DISPATCH();
  86. TARGET(ROT_THREE){
  87. PyObject* _0 = TOP();
  88. TOP() = SECOND();
  89. SECOND() = THIRD();
  90. THIRD() = _0;
  91. } DISPATCH();
  92. TARGET(PRINT_EXPR){
  93. if(TOP() != None) stdout_write(CAST(Str&, py_repr(TOP())) + "\n");
  94. POP();
  95. } DISPATCH();
  96. /*****************************************/
  97. TARGET(LOAD_CONST)
  98. if(heap._should_auto_collect()) heap._auto_collect();
  99. PUSH(co->consts[byte.arg]);
  100. DISPATCH();
  101. TARGET(LOAD_NONE) PUSH(None); DISPATCH();
  102. TARGET(LOAD_TRUE) PUSH(True); DISPATCH();
  103. TARGET(LOAD_FALSE) PUSH(False); DISPATCH();
  104. /*****************************************/
  105. TARGET(LOAD_INT_0) PUSH(PK_SMALL_INT(0)); DISPATCH();
  106. TARGET(LOAD_INT_1) PUSH(PK_SMALL_INT(1)); DISPATCH();
  107. TARGET(LOAD_INT_2) PUSH(PK_SMALL_INT(2)); DISPATCH();
  108. TARGET(LOAD_INT_3) PUSH(PK_SMALL_INT(3)); DISPATCH();
  109. TARGET(LOAD_INT_4) PUSH(PK_SMALL_INT(4)); DISPATCH();
  110. TARGET(LOAD_INT_5) PUSH(PK_SMALL_INT(5)); DISPATCH();
  111. TARGET(LOAD_INT_6) PUSH(PK_SMALL_INT(6)); DISPATCH();
  112. TARGET(LOAD_INT_7) PUSH(PK_SMALL_INT(7)); DISPATCH();
  113. TARGET(LOAD_INT_8) PUSH(PK_SMALL_INT(8)); DISPATCH();
  114. TARGET(LOAD_INT_9) PUSH(PK_SMALL_INT(9)); DISPATCH();
  115. TARGET(LOAD_INT_10) PUSH(PK_SMALL_INT(10)); DISPATCH();
  116. TARGET(LOAD_INT_11) PUSH(PK_SMALL_INT(11)); DISPATCH();
  117. TARGET(LOAD_INT_12) PUSH(PK_SMALL_INT(12)); DISPATCH();
  118. TARGET(LOAD_INT_13) PUSH(PK_SMALL_INT(13)); DISPATCH();
  119. TARGET(LOAD_INT_14) PUSH(PK_SMALL_INT(14)); DISPATCH();
  120. TARGET(LOAD_INT_15) PUSH(PK_SMALL_INT(15)); DISPATCH();
  121. TARGET(LOAD_INT_16) PUSH(PK_SMALL_INT(16)); DISPATCH();
  122. /*****************************************/
  123. TARGET(LOAD_ELLIPSIS) PUSH(Ellipsis); DISPATCH();
  124. TARGET(LOAD_FUNCTION) {
  125. const FuncDecl_& decl = co->func_decls[byte.arg];
  126. PyObject* obj;
  127. if(decl->nested){
  128. NameDict_ captured = frame->_locals.to_namedict();
  129. obj = VAR(Function(decl, frame->_module, nullptr, captured));
  130. captured->set(decl->code->name, obj);
  131. }else{
  132. obj = VAR(Function(decl, frame->_module, nullptr, nullptr));
  133. }
  134. PUSH(obj);
  135. } DISPATCH();
  136. TARGET(LOAD_NULL) PUSH(PY_NULL); DISPATCH();
  137. /*****************************************/
  138. TARGET(LOAD_FAST) {
  139. if(heap._should_auto_collect()) heap._auto_collect();
  140. PyObject* _0 = frame->_locals[byte.arg];
  141. if(_0 == PY_NULL) vm->UnboundLocalError(co->varnames[byte.arg]);
  142. PUSH(_0);
  143. } DISPATCH();
  144. TARGET(LOAD_NAME) {
  145. StrName _name(byte.arg);
  146. PyObject** slot = frame->_locals.try_get_name(_name);
  147. if(slot != nullptr) {
  148. if(*slot == PY_NULL) vm->UnboundLocalError(_name);
  149. PUSH(*slot);
  150. DISPATCH();
  151. }
  152. PyObject* _0 = frame->f_closure_try_get(_name);
  153. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  154. _0 = frame->f_globals().try_get_likely_found(_name);
  155. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  156. _0 = vm->builtins->attr().try_get_likely_found(_name);
  157. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  158. vm->NameError(_name);
  159. } DISPATCH();
  160. TARGET(LOAD_NONLOCAL) {
  161. if(heap._should_auto_collect()) heap._auto_collect();
  162. StrName _name(byte.arg);
  163. PyObject* _0 = frame->f_closure_try_get(_name);
  164. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  165. _0 = frame->f_globals().try_get_likely_found(_name);
  166. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  167. _0 = vm->builtins->attr().try_get_likely_found(_name);
  168. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  169. vm->NameError(_name);
  170. } DISPATCH();
  171. TARGET(LOAD_GLOBAL){
  172. if(heap._should_auto_collect()) heap._auto_collect();
  173. StrName _name(byte.arg);
  174. PyObject* _0 = frame->f_globals().try_get_likely_found(_name);
  175. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  176. _0 = vm->builtins->attr().try_get_likely_found(_name);
  177. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  178. vm->NameError(_name);
  179. } DISPATCH();
  180. TARGET(LOAD_ATTR){
  181. TOP() = getattr(TOP(), StrName(byte.arg));
  182. } DISPATCH();
  183. TARGET(LOAD_CLASS_GLOBAL){
  184. PK_ASSERT(_curr_class != nullptr);
  185. StrName _name(byte.arg);
  186. PyObject* _0 = getattr(_curr_class, _name, false);
  187. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  188. // load global if attribute not found
  189. _0 = frame->f_globals().try_get_likely_found(_name);
  190. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  191. _0 = vm->builtins->attr().try_get_likely_found(_name);
  192. if(_0 != nullptr) { PUSH(_0); DISPATCH(); }
  193. vm->NameError(_name);
  194. } DISPATCH();
  195. TARGET(LOAD_METHOD){
  196. PyObject* _0;
  197. TOP() = get_unbound_method(TOP(), StrName(byte.arg), &_0, true, true);
  198. PUSH(_0);
  199. }DISPATCH();
  200. TARGET(LOAD_SUBSCR){
  201. PyObject* _1 = POPX(); // b
  202. PyObject* _0 = TOP(); // a
  203. auto _ti = _inst_type_info(_0);
  204. if(_ti->m__getitem__){
  205. TOP() = _ti->m__getitem__(this, _0, _1);
  206. }else{
  207. TOP() = call_method(_0, __getitem__, _1);
  208. }
  209. } DISPATCH();
  210. TARGET(STORE_FAST)
  211. frame->_locals[byte.arg] = POPX();
  212. DISPATCH();
  213. TARGET(STORE_NAME){
  214. StrName _name(byte.arg);
  215. PyObject* _0 = POPX();
  216. if(frame->_callable != nullptr){
  217. PyObject** slot = frame->_locals.try_get_name(_name);
  218. if(slot == nullptr) vm->UnboundLocalError(_name);
  219. *slot = _0;
  220. }else{
  221. frame->f_globals().set(_name, _0);
  222. }
  223. } DISPATCH();
  224. TARGET(STORE_GLOBAL)
  225. frame->f_globals().set(StrName(byte.arg), POPX());
  226. DISPATCH();
  227. TARGET(STORE_ATTR) {
  228. PyObject* _0 = TOP(); // a
  229. PyObject* _1 = SECOND(); // val
  230. setattr(_0, StrName(byte.arg), _1);
  231. STACK_SHRINK(2);
  232. } DISPATCH();
  233. TARGET(STORE_SUBSCR){
  234. PyObject* _2 = POPX(); // b
  235. PyObject* _1 = POPX(); // a
  236. PyObject* _0 = POPX(); // val
  237. auto _ti = _inst_type_info(_1);
  238. if(_ti->m__setitem__){
  239. _ti->m__setitem__(this, _1, _2, _0);
  240. }else{
  241. call_method(_1, __setitem__, _2, _0);
  242. }
  243. }DISPATCH();
  244. TARGET(DELETE_FAST){
  245. PyObject* _0 = frame->_locals[byte.arg];
  246. if(_0 == PY_NULL) vm->UnboundLocalError(co->varnames[byte.arg]);
  247. frame->_locals[byte.arg] = PY_NULL;
  248. }DISPATCH();
  249. TARGET(DELETE_NAME){
  250. StrName _name(byte.arg);
  251. if(frame->_callable != nullptr){
  252. PyObject** slot = frame->_locals.try_get_name(_name);
  253. if(slot == nullptr) vm->UnboundLocalError(_name);
  254. *slot = PY_NULL;
  255. }else{
  256. if(!frame->f_globals().del(_name)) vm->NameError(_name);
  257. }
  258. } DISPATCH();
  259. TARGET(DELETE_GLOBAL){
  260. StrName _name(byte.arg);
  261. if(!frame->f_globals().del(_name)) vm->NameError(_name);
  262. }DISPATCH();
  263. TARGET(DELETE_ATTR){
  264. PyObject* _0 = POPX();
  265. delattr(_0, StrName(byte.arg));
  266. } DISPATCH();
  267. TARGET(DELETE_SUBSCR){
  268. PyObject* _1 = POPX();
  269. PyObject* _0 = POPX();
  270. auto _ti = _inst_type_info(_0);
  271. if(_ti->m__delitem__){
  272. _ti->m__delitem__(this, _0, _1);
  273. }else{
  274. call_method(_0, __delitem__, _1);
  275. }
  276. }DISPATCH();
  277. /*****************************************/
  278. TARGET(BUILD_LONG) {
  279. PyObject* _0 = builtins->attr().try_get_likely_found(pk_id_long);
  280. if(_0 == nullptr) AttributeError(builtins, pk_id_long);
  281. TOP() = call(_0, TOP());
  282. } DISPATCH();
  283. TARGET(BUILD_IMAG) {
  284. PyObject* _0 = builtins->attr().try_get_likely_found(pk_id_complex);
  285. if(_0 == nullptr) AttributeError(builtins, pk_id_long);
  286. TOP() = call(_0, VAR(0), TOP());
  287. } DISPATCH();
  288. TARGET(BUILD_BYTES) {
  289. const Str& s = CAST(Str&, TOP());
  290. unsigned char* p = new unsigned char[s.size];
  291. memcpy(p, s.data, s.size);
  292. TOP() = VAR(Bytes(p, s.size));
  293. } DISPATCH();
  294. TARGET(BUILD_TUPLE){
  295. PyObject* _0 = VAR(STACK_VIEW(byte.arg).to_tuple());
  296. STACK_SHRINK(byte.arg);
  297. PUSH(_0);
  298. } DISPATCH();
  299. TARGET(BUILD_LIST){
  300. PyObject* _0 = VAR(STACK_VIEW(byte.arg).to_list());
  301. STACK_SHRINK(byte.arg);
  302. PUSH(_0);
  303. } DISPATCH();
  304. TARGET(BUILD_DICT){
  305. if(byte.arg == 0){
  306. PUSH(VAR(Dict(this)));
  307. DISPATCH();
  308. }
  309. PyObject* _0 = VAR(STACK_VIEW(byte.arg).to_list());
  310. _0 = call(_t(tp_dict), _0);
  311. STACK_SHRINK(byte.arg);
  312. PUSH(_0);
  313. } DISPATCH();
  314. TARGET(BUILD_SET){
  315. PyObject* _0 = VAR(STACK_VIEW(byte.arg).to_list());
  316. _0 = call(builtins->attr(pk_id_set), _0);
  317. STACK_SHRINK(byte.arg);
  318. PUSH(_0);
  319. } DISPATCH();
  320. TARGET(BUILD_SLICE){
  321. PyObject* _2 = POPX(); // step
  322. PyObject* _1 = POPX(); // stop
  323. PyObject* _0 = POPX(); // start
  324. PUSH(VAR(Slice(_0, _1, _2)));
  325. } DISPATCH();
  326. TARGET(BUILD_STRING) {
  327. SStream ss;
  328. ArgsView view = STACK_VIEW(byte.arg);
  329. for(PyObject* obj : view) ss << CAST(Str&, py_str(obj));
  330. STACK_SHRINK(byte.arg);
  331. PUSH(VAR(ss.str()));
  332. } DISPATCH();
  333. /*****************************************/
  334. TARGET(BUILD_TUPLE_UNPACK) {
  335. auto _lock = heap.gc_scope_lock();
  336. List list;
  337. _unpack_as_list(STACK_VIEW(byte.arg), list);
  338. STACK_SHRINK(byte.arg);
  339. PyObject* _0 = VAR(Tuple(std::move(list)));
  340. PUSH(_0);
  341. } DISPATCH();
  342. TARGET(BUILD_LIST_UNPACK) {
  343. auto _lock = heap.gc_scope_lock();
  344. List list;
  345. _unpack_as_list(STACK_VIEW(byte.arg), list);
  346. STACK_SHRINK(byte.arg);
  347. PyObject* _0 = VAR(std::move(list));
  348. PUSH(_0);
  349. } DISPATCH();
  350. TARGET(BUILD_DICT_UNPACK) {
  351. auto _lock = heap.gc_scope_lock();
  352. Dict dict(this);
  353. _unpack_as_dict(STACK_VIEW(byte.arg), dict);
  354. STACK_SHRINK(byte.arg);
  355. PyObject* _0 = VAR(std::move(dict));
  356. PUSH(_0);
  357. } DISPATCH();
  358. TARGET(BUILD_SET_UNPACK) {
  359. auto _lock = heap.gc_scope_lock();
  360. List list;
  361. _unpack_as_list(STACK_VIEW(byte.arg), list);
  362. STACK_SHRINK(byte.arg);
  363. PyObject* _0 = VAR(std::move(list));
  364. _0 = call(builtins->attr(pk_id_set), _0);
  365. PUSH(_0);
  366. } DISPATCH();
  367. /*****************************************/
  368. #define BINARY_OP_SPECIAL(func) \
  369. _ti = _inst_type_info(_0); \
  370. if(_ti->m##func){ \
  371. TOP() = _ti->m##func(this, _0, _1); \
  372. }else{ \
  373. PyObject* self; \
  374. PyObject* _2 = get_unbound_method(_0, func, &self, false); \
  375. if(_2 != nullptr) TOP() = call_method(self, _2, _1); \
  376. else TOP() = NotImplemented; \
  377. }
  378. #define BINARY_OP_RSPECIAL(op, func) \
  379. if(TOP() == NotImplemented){ \
  380. PyObject* self; \
  381. PyObject* _2 = get_unbound_method(_1, func, &self, false); \
  382. if(_2 != nullptr) TOP() = call_method(self, _2, _0); \
  383. else BinaryOptError(op, _0, _1); \
  384. if(TOP() == NotImplemented) BinaryOptError(op, _0, _1); \
  385. }
  386. TARGET(BINARY_TRUEDIV){
  387. PyObject* _1 = POPX();
  388. PyObject* _0 = TOP();
  389. const PyTypeInfo* _ti;
  390. BINARY_OP_SPECIAL(__truediv__);
  391. if(TOP() == NotImplemented) BinaryOptError("/", _0, _1);
  392. } DISPATCH();
  393. TARGET(BINARY_POW){
  394. PyObject* _1 = POPX();
  395. PyObject* _0 = TOP();
  396. const PyTypeInfo* _ti;
  397. BINARY_OP_SPECIAL(__pow__);
  398. if(TOP() == NotImplemented) BinaryOptError("**", _0, _1);
  399. } DISPATCH();
  400. TARGET(BINARY_ADD){
  401. PyObject* _1 = POPX();
  402. PyObject* _0 = TOP();
  403. PREDICT_INT_OP(+)
  404. const PyTypeInfo* _ti;
  405. BINARY_OP_SPECIAL(__add__);
  406. BINARY_OP_RSPECIAL("+", __radd__);
  407. } DISPATCH()
  408. TARGET(BINARY_SUB){
  409. PyObject* _1 = POPX();
  410. PyObject* _0 = TOP();
  411. PREDICT_INT_OP(-)
  412. const PyTypeInfo* _ti;
  413. BINARY_OP_SPECIAL(__sub__);
  414. BINARY_OP_RSPECIAL("-", __rsub__);
  415. } DISPATCH()
  416. TARGET(BINARY_MUL){
  417. PyObject* _1 = POPX();
  418. PyObject* _0 = TOP();
  419. PREDICT_INT_OP(*)
  420. const PyTypeInfo* _ti;
  421. BINARY_OP_SPECIAL(__mul__);
  422. BINARY_OP_RSPECIAL("*", __rmul__);
  423. } DISPATCH()
  424. TARGET(BINARY_FLOORDIV){
  425. PyObject* _1 = POPX();
  426. PyObject* _0 = TOP();
  427. PREDICT_INT_DIV_OP(/)
  428. const PyTypeInfo* _ti;
  429. BINARY_OP_SPECIAL(__floordiv__);
  430. if(TOP() == NotImplemented) BinaryOptError("//", _0, _1);
  431. } DISPATCH()
  432. TARGET(BINARY_MOD){
  433. PyObject* _1 = POPX();
  434. PyObject* _0 = TOP();
  435. PREDICT_INT_DIV_OP(%)
  436. const PyTypeInfo* _ti;
  437. BINARY_OP_SPECIAL(__mod__);
  438. if(TOP() == NotImplemented) BinaryOptError("%", _0, _1);
  439. } DISPATCH()
  440. TARGET(COMPARE_LT){
  441. PyObject* _1 = POPX();
  442. PyObject* _0 = TOP();
  443. PREDICT_INT_OP(<)
  444. TOP() = VAR(py_lt(_0, _1));
  445. } DISPATCH()
  446. TARGET(COMPARE_LE){
  447. PyObject* _1 = POPX();
  448. PyObject* _0 = TOP();
  449. PREDICT_INT_OP(<=)
  450. TOP() = VAR(py_le(_0, _1));
  451. } DISPATCH()
  452. TARGET(COMPARE_EQ){
  453. PyObject* _1 = POPX();
  454. PyObject* _0 = TOP();
  455. TOP() = VAR(py_eq(_0, _1));
  456. } DISPATCH()
  457. TARGET(COMPARE_NE){
  458. PyObject* _1 = POPX();
  459. PyObject* _0 = TOP();
  460. TOP() = VAR(py_ne(_0, _1));
  461. } DISPATCH()
  462. TARGET(COMPARE_GT){
  463. PyObject* _1 = POPX();
  464. PyObject* _0 = TOP();
  465. PREDICT_INT_OP(>)
  466. TOP() = VAR(py_gt(_0, _1));
  467. } DISPATCH()
  468. TARGET(COMPARE_GE){
  469. PyObject* _1 = POPX();
  470. PyObject* _0 = TOP();
  471. PREDICT_INT_OP(>=)
  472. TOP() = VAR(py_ge(_0, _1));
  473. } DISPATCH()
  474. TARGET(BITWISE_LSHIFT){
  475. PyObject* _1 = POPX();
  476. PyObject* _0 = TOP();
  477. PREDICT_INT_OP(<<)
  478. const PyTypeInfo* _ti;
  479. BINARY_OP_SPECIAL(__lshift__);
  480. if(TOP() == NotImplemented) BinaryOptError("<<", _0, _1);
  481. } DISPATCH()
  482. TARGET(BITWISE_RSHIFT){
  483. PyObject* _1 = POPX();
  484. PyObject* _0 = TOP();
  485. PREDICT_INT_OP(>>)
  486. const PyTypeInfo* _ti;
  487. BINARY_OP_SPECIAL(__rshift__);
  488. if(TOP() == NotImplemented) BinaryOptError(">>", _0, _1);
  489. } DISPATCH()
  490. TARGET(BITWISE_AND){
  491. PyObject* _1 = POPX();
  492. PyObject* _0 = TOP();
  493. PREDICT_INT_OP(&)
  494. const PyTypeInfo* _ti;
  495. BINARY_OP_SPECIAL(__and__);
  496. if(TOP() == NotImplemented) BinaryOptError("&", _0, _1);
  497. } DISPATCH()
  498. TARGET(BITWISE_OR){
  499. PyObject* _1 = POPX();
  500. PyObject* _0 = TOP();
  501. PREDICT_INT_OP(|)
  502. const PyTypeInfo* _ti;
  503. BINARY_OP_SPECIAL(__or__);
  504. if(TOP() == NotImplemented) BinaryOptError("|", _0, _1);
  505. } DISPATCH()
  506. TARGET(BITWISE_XOR){
  507. PyObject* _1 = POPX();
  508. PyObject* _0 = TOP();
  509. PREDICT_INT_OP(^)
  510. const PyTypeInfo* _ti;
  511. BINARY_OP_SPECIAL(__xor__);
  512. if(TOP() == NotImplemented) BinaryOptError("^", _0, _1);
  513. } DISPATCH()
  514. TARGET(BINARY_MATMUL){
  515. PyObject* _1 = POPX();
  516. PyObject* _0 = TOP();
  517. const PyTypeInfo* _ti;
  518. BINARY_OP_SPECIAL(__matmul__);
  519. if(TOP() == NotImplemented) BinaryOptError("@", _0, _1);
  520. } DISPATCH();
  521. #undef BINARY_OP_SPECIAL
  522. #undef BINARY_OP_RSPECIAL
  523. #undef PREDICT_INT_OP
  524. TARGET(IS_OP){
  525. PyObject* _1 = POPX(); // rhs
  526. PyObject* _0 = TOP(); // lhs
  527. TOP() = VAR(static_cast<bool>((_0==_1) ^ byte.arg));
  528. } DISPATCH();
  529. TARGET(CONTAINS_OP){
  530. // a in b -> b __contains__ a
  531. auto _ti = _inst_type_info(TOP());
  532. PyObject* _0;
  533. if(_ti->m__contains__){
  534. _0 = _ti->m__contains__(this, TOP(), SECOND());
  535. }else{
  536. _0 = call_method(TOP(), __contains__, SECOND());
  537. }
  538. POP();
  539. TOP() = VAR(static_cast<bool>((int)CAST(bool, _0) ^ byte.arg));
  540. } DISPATCH();
  541. /*****************************************/
  542. TARGET(JUMP_ABSOLUTE)
  543. frame->jump_abs(byte.arg);
  544. DISPATCH();
  545. TARGET(JUMP_ABSOLUTE_TOP)
  546. frame->jump_abs(_CAST(int, POPX()));
  547. DISPATCH();
  548. TARGET(POP_JUMP_IF_FALSE){
  549. if(!py_bool(TOP())) frame->jump_abs(byte.arg);
  550. POP();
  551. } DISPATCH();
  552. TARGET(POP_JUMP_IF_TRUE){
  553. if(py_bool(TOP())) frame->jump_abs(byte.arg);
  554. POP();
  555. } DISPATCH();
  556. TARGET(JUMP_IF_TRUE_OR_POP){
  557. if(py_bool(TOP())) frame->jump_abs(byte.arg);
  558. else POP();
  559. } DISPATCH();
  560. TARGET(JUMP_IF_FALSE_OR_POP){
  561. if(!py_bool(TOP())) frame->jump_abs(byte.arg);
  562. else POP();
  563. } DISPATCH();
  564. TARGET(SHORTCUT_IF_FALSE_OR_POP){
  565. if(!py_bool(TOP())){ // [b, False]
  566. STACK_SHRINK(2); // []
  567. PUSH(vm->False); // [False]
  568. frame->jump_abs(byte.arg);
  569. } else POP(); // [b]
  570. } DISPATCH();
  571. TARGET(LOOP_CONTINUE)
  572. frame->jump_abs(byte.arg);
  573. DISPATCH();
  574. TARGET(LOOP_BREAK)
  575. frame->jump_abs_break(&s_data, byte.arg);
  576. DISPATCH();
  577. TARGET(GOTO) {
  578. StrName _name(byte.arg);
  579. int index = co->labels.try_get_likely_found(_name);
  580. if(index < 0) RuntimeError(_S("label ", _name.escape(), " not found"));
  581. frame->jump_abs_break(&s_data, index);
  582. } DISPATCH();
  583. /*****************************************/
  584. TARGET(FSTRING_EVAL){
  585. PyObject* _0 = co->consts[byte.arg];
  586. std::string_view string = CAST(Str&, _0).sv();
  587. auto it = _cached_codes.find(string);
  588. CodeObject_ code;
  589. if(it == _cached_codes.end()){
  590. code = vm->compile(string, "<eval>", EVAL_MODE, true);
  591. _cached_codes[string] = code;
  592. }else{
  593. code = it->second;
  594. }
  595. _0 = vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
  596. PUSH(_0);
  597. } DISPATCH();
  598. TARGET(REPR)
  599. TOP() = py_repr(TOP());
  600. DISPATCH();
  601. TARGET(CALL){
  602. PyObject* _0 = vectorcall(
  603. byte.arg & 0xFF, // ARGC
  604. (byte.arg>>8) & 0xFF, // KWARGC
  605. true
  606. );
  607. if(_0 == PY_OP_CALL) DISPATCH_OP_CALL();
  608. PUSH(_0);
  609. } DISPATCH();
  610. TARGET(CALL_TP){
  611. PyObject* _0;
  612. PyObject* _1;
  613. PyObject* _2;
  614. // [callable, <self>, args: tuple, kwargs: dict | NULL]
  615. if(byte.arg){
  616. _2 = POPX();
  617. _1 = POPX();
  618. for(PyObject* obj: _CAST(Tuple&, _1)) PUSH(obj);
  619. _CAST(Dict&, _2).apply([this](PyObject* k, PyObject* v){
  620. PUSH(VAR(StrName(CAST(Str&, k)).index));
  621. PUSH(v);
  622. });
  623. _0 = vectorcall(
  624. _CAST(Tuple&, _1).size(), // ARGC
  625. _CAST(Dict&, _2).size(), // KWARGC
  626. true
  627. );
  628. }else{
  629. // no **kwargs
  630. _1 = POPX();
  631. for(PyObject* obj: _CAST(Tuple&, _1)) PUSH(obj);
  632. _0 = vectorcall(
  633. _CAST(Tuple&, _1).size(), // ARGC
  634. 0, // KWARGC
  635. true
  636. );
  637. }
  638. if(_0 == PY_OP_CALL) DISPATCH_OP_CALL();
  639. PUSH(_0);
  640. } DISPATCH();
  641. TARGET(RETURN_VALUE){
  642. PyObject* _0 = byte.arg == BC_NOARG ? POPX() : None;
  643. _pop_frame();
  644. if(frame == base_frame){ // [ frameBase<- ]
  645. return _0;
  646. }else{
  647. frame = top_frame();
  648. PUSH(_0);
  649. goto __NEXT_FRAME;
  650. }
  651. } DISPATCH();
  652. TARGET(YIELD_VALUE)
  653. return PY_OP_YIELD;
  654. /*****************************************/
  655. TARGET(LIST_APPEND){
  656. PyObject* _0 = POPX();
  657. CAST(List&, SECOND()).push_back(_0);
  658. } DISPATCH();
  659. TARGET(DICT_ADD) {
  660. PyObject* _0 = POPX();
  661. Tuple& t = CAST(Tuple&, _0);
  662. call_method(SECOND(), __setitem__, t[0], t[1]);
  663. } DISPATCH();
  664. TARGET(SET_ADD){
  665. PyObject* _0 = POPX();
  666. call_method(SECOND(), pk_id_add, _0);
  667. } DISPATCH();
  668. /*****************************************/
  669. TARGET(UNARY_NEGATIVE)
  670. TOP() = py_negate(TOP());
  671. DISPATCH();
  672. TARGET(UNARY_NOT){
  673. PyObject* _0 = TOP();
  674. if(_0==True) TOP()=False;
  675. else if(_0==False) TOP()=True;
  676. else TOP() = VAR(!py_bool(_0));
  677. } DISPATCH();
  678. TARGET(UNARY_STAR)
  679. TOP() = VAR(StarWrapper(byte.arg, TOP()));
  680. DISPATCH();
  681. TARGET(UNARY_INVERT){
  682. PyObject* _0;
  683. auto _ti = _inst_type_info(TOP());
  684. if(_ti->m__invert__) _0 = _ti->m__invert__(this, TOP());
  685. else _0 = call_method(TOP(), __invert__);
  686. TOP() = _0;
  687. } DISPATCH();
  688. /*****************************************/
  689. TARGET(GET_ITER)
  690. TOP() = py_iter(TOP());
  691. DISPATCH();
  692. TARGET(FOR_ITER){
  693. PyObject* _0 = py_next(TOP());
  694. if(_0 != StopIteration){
  695. PUSH(_0);
  696. }else{
  697. frame->jump_abs_break(&s_data, co->_get_block_codei(frame->_ip).end);
  698. }
  699. } DISPATCH();
  700. TARGET(FOR_ITER_STORE_FAST){
  701. PyObject* _0 = py_next(TOP());
  702. if(_0 != StopIteration){
  703. frame->_locals[byte.arg] = _0;
  704. }else{
  705. frame->jump_abs_break(&s_data, co->_get_block_codei(frame->_ip).end);
  706. }
  707. } DISPATCH()
  708. TARGET(FOR_ITER_STORE_GLOBAL){
  709. PyObject* _0 = py_next(TOP());
  710. if(_0 != StopIteration){
  711. frame->f_globals().set(StrName(byte.arg), _0);
  712. }else{
  713. frame->jump_abs_break(&s_data, co->_get_block_codei(frame->_ip).end);
  714. }
  715. } DISPATCH()
  716. TARGET(FOR_ITER_YIELD_VALUE){
  717. PyObject* _0 = py_next(TOP());
  718. if(_0 != StopIteration){
  719. PUSH(_0);
  720. return PY_OP_YIELD;
  721. }else{
  722. frame->jump_abs_break(&s_data, co->_get_block_codei(frame->_ip).end);
  723. }
  724. } DISPATCH()
  725. /*****************************************/
  726. TARGET(IMPORT_PATH){
  727. PyObject* _0 = co->consts[byte.arg];
  728. PUSH(py_import(CAST(Str&, _0)));
  729. } DISPATCH();
  730. TARGET(POP_IMPORT_STAR) {
  731. PyObject* _0 = POPX(); // pop the module
  732. PyObject* _1 = _0->attr().try_get(__all__);
  733. StrName _name;
  734. if(_1 != nullptr){
  735. for(PyObject* key: CAST(List&, _1)){
  736. _name = StrName::get(CAST(Str&, key).sv());
  737. PyObject* value = _0->attr().try_get_likely_found(_name);
  738. if(value == nullptr){
  739. ImportError(_S("cannot import name ", _name.escape()));
  740. }else{
  741. frame->f_globals().set(_name, value);
  742. }
  743. }
  744. }else{
  745. for(auto& [name, value]: _0->attr().items()){
  746. std::string_view s = name.sv();
  747. if(s.empty() || s[0] == '_') continue;
  748. frame->f_globals().set(name, value);
  749. }
  750. }
  751. } DISPATCH();
  752. /*****************************************/
  753. TARGET(UNPACK_SEQUENCE){
  754. auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
  755. PyObject* _0 = py_iter(POPX());
  756. for(int i=0; i<byte.arg; i++){
  757. PyObject* _1 = py_next(_0);
  758. if(_1 == StopIteration) ValueError("not enough values to unpack");
  759. PUSH(_1);
  760. }
  761. if(py_next(_0) != StopIteration) ValueError("too many values to unpack");
  762. } DISPATCH();
  763. TARGET(UNPACK_EX) {
  764. auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
  765. PyObject* _0 = py_iter(POPX());
  766. PyObject* _1;
  767. for(int i=0; i<byte.arg; i++){
  768. _1 = py_next(_0);
  769. if(_1 == StopIteration) ValueError("not enough values to unpack");
  770. PUSH(_1);
  771. }
  772. List extras;
  773. while(true){
  774. _1 = py_next(_0);
  775. if(_1 == StopIteration) break;
  776. extras.push_back(_1);
  777. }
  778. PUSH(VAR(extras));
  779. } DISPATCH();
  780. /*****************************************/
  781. TARGET(BEGIN_CLASS){
  782. StrName _name(byte.arg);
  783. PyObject* _0 = POPX(); // super
  784. if(_0 == None) _0 = _t(tp_object);
  785. check_type(_0, tp_type);
  786. _curr_class = new_type_object(frame->_module, _name, PK_OBJ_GET(Type, _0));
  787. } DISPATCH();
  788. TARGET(END_CLASS) {
  789. PK_ASSERT(_curr_class != nullptr);
  790. StrName _name(byte.arg);
  791. frame->_module->attr().set(_name, _curr_class);
  792. // call on_end_subclass
  793. PyTypeInfo* ti = &_all_types[PK_OBJ_GET(Type, _curr_class)];
  794. if(ti->base != tp_object){
  795. PyTypeInfo* base_ti = &_all_types[ti->base];
  796. if(base_ti->on_end_subclass) base_ti->on_end_subclass(this, ti);
  797. }
  798. _curr_class = nullptr;
  799. } DISPATCH();
  800. TARGET(STORE_CLASS_ATTR){
  801. PK_ASSERT(_curr_class != nullptr);
  802. StrName _name(byte.arg);
  803. PyObject* _0 = POPX();
  804. if(is_type(_0, tp_function)){
  805. PK_OBJ_GET(Function, _0)._class = _curr_class;
  806. }
  807. _curr_class->attr().set(_name, _0);
  808. } DISPATCH();
  809. TARGET(BEGIN_CLASS_DECORATION){
  810. PUSH(_curr_class);
  811. } DISPATCH();
  812. TARGET(END_CLASS_DECORATION){
  813. _curr_class = POPX();
  814. } DISPATCH();
  815. TARGET(ADD_CLASS_ANNOTATION) {
  816. PK_ASSERT(_curr_class != nullptr);
  817. StrName _name(byte.arg);
  818. Type type = PK_OBJ_GET(Type, _curr_class);
  819. _all_types[type].annotated_fields.push_back(_name);
  820. } DISPATCH();
  821. /*****************************************/
  822. TARGET(WITH_ENTER)
  823. PUSH(call_method(TOP(), __enter__));
  824. DISPATCH();
  825. TARGET(WITH_EXIT)
  826. call_method(TOP(), __exit__);
  827. POP();
  828. DISPATCH();
  829. /*****************************************/
  830. TARGET(EXCEPTION_MATCH) {
  831. PyObject* assumed_type = POPX();
  832. check_type(assumed_type, tp_type);
  833. PyObject* e_obj = TOP();
  834. bool ok = isinstance(e_obj, PK_OBJ_GET(Type, assumed_type));
  835. PUSH(VAR(ok));
  836. } DISPATCH();
  837. TARGET(RAISE) {
  838. if(is_type(TOP(), tp_type)){
  839. TOP() = call(TOP());
  840. }
  841. if(!isinstance(TOP(), tp_exception)){
  842. _builtin_error("TypeError", "exceptions must derive from Exception");
  843. }
  844. _error(POPX());
  845. } DISPATCH();
  846. TARGET(RAISE_ASSERT)
  847. if(byte.arg){
  848. PyObject* _0 = py_str(POPX());
  849. _builtin_error("AssertionError", CAST(Str, _0));
  850. }else{
  851. _builtin_error("AssertionError");
  852. }
  853. DISPATCH();
  854. TARGET(RE_RAISE) _raise(true); DISPATCH();
  855. TARGET(POP_EXCEPTION) _last_exception = POPX(); DISPATCH();
  856. /*****************************************/
  857. TARGET(FORMAT_STRING) {
  858. PyObject* _0 = POPX();
  859. const Str& spec = CAST(Str&, co->consts[byte.arg]);
  860. PUSH(_format_string(spec, _0));
  861. } DISPATCH();
  862. /*****************************************/
  863. TARGET(INC_FAST){
  864. PyObject** p = &frame->_locals[byte.arg];
  865. if(*p == PY_NULL) vm->NameError(co->varnames[byte.arg]);
  866. *p = VAR(CAST(i64, *p) + 1);
  867. } DISPATCH();
  868. TARGET(DEC_FAST){
  869. PyObject** p = &frame->_locals[byte.arg];
  870. if(*p == PY_NULL) vm->NameError(co->varnames[byte.arg]);
  871. *p = VAR(CAST(i64, *p) - 1);
  872. } DISPATCH();
  873. TARGET(INC_GLOBAL){
  874. StrName _name(byte.arg);
  875. PyObject** p = frame->f_globals().try_get_2_likely_found(_name);
  876. if(p == nullptr) vm->NameError(_name);
  877. *p = VAR(CAST(i64, *p) + 1);
  878. } DISPATCH();
  879. TARGET(DEC_GLOBAL){
  880. StrName _name(byte.arg);
  881. PyObject** p = frame->f_globals().try_get_2_likely_found(_name);
  882. if(p == nullptr) vm->NameError(_name);
  883. *p = VAR(CAST(i64, *p) - 1);
  884. } DISPATCH();
  885. /*****************************************/
  886. }
  887. }
  888. #undef DISPATCH
  889. #undef TARGET
  890. #undef DISPATCH_OP_CALL
  891. #undef CEVAL_STEP_CALLBACK
  892. /**********************************************************************/
  893. PK_UNREACHABLE()
  894. }catch(HandledException){
  895. continue;
  896. }catch(UnhandledException){
  897. PyObject* e_obj = POPX();
  898. Exception& _e = PK_OBJ_GET(Exception, e_obj);
  899. bool is_base_frame_to_be_popped = frame == base_frame;
  900. _pop_frame();
  901. if(callstack.empty()) throw _e; // propagate to the top level
  902. frame = top_frame();
  903. PUSH(e_obj);
  904. if(is_base_frame_to_be_popped) throw ToBeRaisedException();
  905. need_raise = true;
  906. }catch(ToBeRaisedException){
  907. need_raise = true;
  908. }
  909. }
  910. }
  911. #undef TOP
  912. #undef SECOND
  913. #undef THIRD
  914. #undef PEEK
  915. #undef STACK_SHRINK
  916. #undef PUSH
  917. #undef POP
  918. #undef POPX
  919. #undef STACK_VIEW
  920. #undef DISPATCH
  921. #undef TARGET
  922. #undef DISPATCH_OP_CALL
  923. } // namespace pkpy