ceval.cpp 34 KB

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