ceval.cpp 24 KB

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