ceval.cpp 26 KB

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