ceval.cpp 31 KB

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