1
0

ceval.cpp 33 KB

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