ceval.cpp 25 KB

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