ceval.h 24 KB

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