ceval.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. #include "pocketpy/interpreter/ceval.hpp"
  2. #include "pocketpy/objects/base.h"
  3. namespace pkpy {
  4. #define PREDICT_INT_OP(op) \
  5. if(is_int(_0) && is_int(_1)) { \
  6. TOP() = VAR(_0._i64 op _1._i64); \
  7. DISPATCH() \
  8. }
  9. #define PREDICT_INT_DIV_OP(op) \
  10. if(is_int(_0) && is_int(_1)) { \
  11. i64 divisor = _1._i64; \
  12. if(divisor == 0) ZeroDivisionError(); \
  13. TOP() = VAR(_0._i64 op divisor); \
  14. DISPATCH() \
  15. }
  16. #define BINARY_F_COMPARE(func, op, rfunc) \
  17. PyVar ret; \
  18. const PyTypeInfo* _ti = _tp_info(_0); \
  19. if(_ti->m##func) { \
  20. ret = _ti->m##func(this, _0, _1); \
  21. } else { \
  22. PyVar self; \
  23. PyVar _2 = get_unbound_method(_0, func, &self, false); \
  24. if(_2.type) \
  25. ret = call_method(self, _2, _1); \
  26. else \
  27. ret = NotImplemented; \
  28. } \
  29. if(is_not_implemented(ret)) { \
  30. PyVar self; \
  31. PyVar _2 = get_unbound_method(_1, rfunc, &self, false); \
  32. if(_2.type) \
  33. ret = call_method(self, _2, _0); \
  34. else \
  35. BinaryOptError(op, _0, _1); \
  36. if(is_not_implemented(ret)) BinaryOptError(op, _0, _1); \
  37. }
  38. void VM::__op_unpack_sequence(uint16_t arg) {
  39. PyVar _0 = POPX();
  40. if(is_type(_0, VM::tp_tuple)) {
  41. // fast path for tuple
  42. Tuple& tuple = PK_OBJ_GET(Tuple, _0);
  43. if(tuple.size() == arg) {
  44. for(PyVar obj: tuple)
  45. PUSH(obj);
  46. } else {
  47. ValueError(_S("expected ", (int)arg, " values to unpack, got ", (int)tuple.size()));
  48. }
  49. } else {
  50. auto _lock = gc_scope_lock(); // lock the gc via RAII!!
  51. _0 = py_iter(_0);
  52. const PyTypeInfo* ti = _tp_info(_0);
  53. for(int i = 0; i < arg; i++) {
  54. PyVar _1 = _py_next(ti, _0);
  55. if(_1 == StopIteration) ValueError("not enough values to unpack");
  56. PUSH(_1);
  57. }
  58. if(_py_next(ti, _0) != StopIteration) ValueError("too many values to unpack");
  59. }
  60. }
  61. bool VM::py_lt(PyVar _0, PyVar _1) {
  62. BINARY_F_COMPARE(__lt__, "<", __gt__);
  63. assert(ret.type == tp_bool);
  64. return ret.extra;
  65. }
  66. bool VM::py_le(PyVar _0, PyVar _1) {
  67. BINARY_F_COMPARE(__le__, "<=", __ge__);
  68. assert(ret.type == tp_bool);
  69. return ret.extra;
  70. }
  71. bool VM::py_gt(PyVar _0, PyVar _1) {
  72. BINARY_F_COMPARE(__gt__, ">", __lt__);
  73. assert(ret.type == tp_bool);
  74. return ret.extra;
  75. }
  76. bool VM::py_ge(PyVar _0, PyVar _1) {
  77. BINARY_F_COMPARE(__ge__, ">=", __le__);
  78. assert(ret.type == tp_bool);
  79. return ret.extra;
  80. }
  81. #undef BINARY_F_COMPARE
  82. #if PK_ENABLE_PROFILER
  83. #define CEVAL_STEP_CALLBACK() \
  84. if(_ceval_on_step) _ceval_on_step(this, frame, byte); \
  85. if(_profiler) _profiler->_step(callstack.size(), frame); \
  86. if(!_next_breakpoint.empty()) { _next_breakpoint._step(this); }
  87. #else
  88. #define CEVAL_STEP_CALLBACK() \
  89. if(_ceval_on_step && _ceval_on_step) { \
  90. if(_ceval_on_step) \
  91. if(_ceval_on_step) _ceval_on_step(this, frame, byte); \
  92. }
  93. #endif
  94. #define DISPATCH() \
  95. { \
  96. frame->_ip++; \
  97. goto __NEXT_STEP; \
  98. }
  99. #define DISPATCH_JUMP(__offset) \
  100. { \
  101. frame->_ip += __offset; \
  102. goto __NEXT_STEP; \
  103. }
  104. #define DISPATCH_JUMP_ABSOLUTE(__target) \
  105. { \
  106. frame->_ip = c11__at(Bytecode, &frame->co->codes, __target); \
  107. goto __NEXT_STEP; \
  108. }
  109. PyVar VM::__run_top_frame() {
  110. Frame* frame = &callstack.top();
  111. const Frame* base_frame = frame;
  112. InternalException __internal_exception;
  113. while(true) {
  114. try {
  115. /**********************************************************************/
  116. {
  117. __NEXT_FRAME:
  118. Bytecode byte;
  119. if(__internal_exception.type == InternalExceptionType::Null) {
  120. // None
  121. frame->_ip++;
  122. } else if(__internal_exception.type == InternalExceptionType::Handled) {
  123. // HandledException + continue
  124. frame->_ip = c11__at(Bytecode, &frame->co->codes, __internal_exception.arg);
  125. __internal_exception = {};
  126. } else {
  127. // UnhandledException + continue (need_raise = true)
  128. // ToBeRaisedException + continue (need_raise = true)
  129. __internal_exception = {};
  130. __raise_exc(); // no return
  131. }
  132. __NEXT_STEP:
  133. byte = *frame->_ip;
  134. CEVAL_STEP_CALLBACK()
  135. #if PK_DEBUG_CEVAL_STEP
  136. __log_s_data();
  137. #endif
  138. switch((Opcode)byte.op) {
  139. case OP_NO_OP: DISPATCH()
  140. /*****************************************/
  141. case OP_POP_TOP: POP(); DISPATCH()
  142. case OP_DUP_TOP: PUSH(TOP()); DISPATCH()
  143. case OP_DUP_TOP_TWO:
  144. // [a, b]
  145. PUSH(SECOND()); // [a, b, a]
  146. PUSH(SECOND()); // [a, b, a, b]
  147. DISPATCH()
  148. case OP_ROT_TWO: std::swap(TOP(), SECOND()); DISPATCH()
  149. case OP_ROT_THREE: {
  150. // [a, b, c] -> [c, a, b]
  151. PyVar _0 = TOP();
  152. TOP() = SECOND();
  153. SECOND() = THIRD();
  154. THIRD() = _0;
  155. }
  156. DISPATCH()
  157. case OP_PRINT_EXPR:
  158. if(!is_none(TOP())) stdout_write(py_repr(TOP()) + "\n");
  159. POP();
  160. DISPATCH()
  161. /*****************************************/
  162. case OP_LOAD_CONST:
  163. PUSH(c11__getitem(PyVar, &frame->co->consts, byte.arg));
  164. DISPATCH()
  165. case OP_LOAD_NONE: PUSH(None); DISPATCH()
  166. case OP_LOAD_TRUE: PUSH(True); DISPATCH()
  167. case OP_LOAD_FALSE: PUSH(False); DISPATCH()
  168. /*****************************************/
  169. case OP_LOAD_SMALL_INT: s_data.emplace(tp_int, (i64)(int16_t)byte.arg); DISPATCH()
  170. /*****************************************/
  171. case OP_LOAD_ELLIPSIS: PUSH(Ellipsis); DISPATCH()
  172. case OP_LOAD_FUNCTION: {
  173. FuncDecl_ decl = c11__getitem(FuncDecl_, &frame->co->func_decls, byte.arg);
  174. PyVar obj;
  175. if(decl->nested) {
  176. NameDict* captured = frame->_locals.to_namedict();
  177. obj = new_object<Function>(tp_function, decl, frame->_module, nullptr, captured);
  178. uint16_t name = pk_StrName__map2(pkpy_Str__sv(&decl->code->name));
  179. captured->set(name, obj);
  180. } else {
  181. obj = new_object<Function>(tp_function, decl, frame->_module, nullptr, nullptr);
  182. }
  183. PUSH(obj);
  184. }
  185. DISPATCH()
  186. case OP_LOAD_NULL: PUSH(PY_NULL); DISPATCH()
  187. /*****************************************/
  188. case OP_LOAD_FAST: {
  189. PyVar _0 = frame->_locals[byte.arg];
  190. if(_0 == PY_NULL) vm->UnboundLocalError(c11__getitem(uint16_t, &frame->co->varnames, byte.arg));
  191. PUSH(_0);
  192. }
  193. DISPATCH()
  194. case OP_LOAD_NAME: {
  195. StrName _name(byte.arg);
  196. PyVar* slot = frame->_locals.try_get_name(_name);
  197. if(slot != nullptr) {
  198. if(*slot == PY_NULL) vm->UnboundLocalError(_name);
  199. PUSH(*slot);
  200. DISPATCH()
  201. }
  202. PyVar* _0 = frame->f_closure_try_get(_name);
  203. if(_0 != nullptr) {
  204. PUSH(*_0);
  205. DISPATCH()
  206. }
  207. _0 = frame->f_globals().try_get_2_likely_found(_name);
  208. if(_0 != nullptr) {
  209. PUSH(*_0);
  210. DISPATCH()
  211. }
  212. _0 = vm->builtins->attr().try_get_2_likely_found(_name);
  213. if(_0 != nullptr) {
  214. PUSH(*_0);
  215. DISPATCH()
  216. }
  217. vm->NameError(_name);
  218. }
  219. DISPATCH()
  220. case OP_LOAD_NONLOCAL: {
  221. StrName _name(byte.arg);
  222. PyVar* _0 = frame->f_closure_try_get(_name);
  223. if(_0 != nullptr) {
  224. PUSH(*_0);
  225. DISPATCH()
  226. }
  227. _0 = frame->f_globals().try_get_2_likely_found(_name);
  228. if(_0 != nullptr) {
  229. PUSH(*_0);
  230. DISPATCH()
  231. }
  232. _0 = vm->builtins->attr().try_get_2_likely_found(_name);
  233. if(_0 != nullptr) {
  234. PUSH(*_0);
  235. DISPATCH()
  236. }
  237. vm->NameError(_name);
  238. }
  239. DISPATCH()
  240. case OP_LOAD_GLOBAL: {
  241. StrName _name(byte.arg);
  242. PyVar _0 = frame->f_globals().try_get_likely_found(_name);
  243. if(_0 != nullptr) {
  244. PUSH(_0);
  245. DISPATCH()
  246. }
  247. _0 = vm->builtins->attr().try_get_likely_found(_name);
  248. if(_0 != nullptr) {
  249. PUSH(_0);
  250. DISPATCH()
  251. }
  252. vm->NameError(_name);
  253. }
  254. DISPATCH()
  255. case OP_LOAD_ATTR: {
  256. TOP() = getattr(TOP(), StrName(byte.arg));
  257. }
  258. DISPATCH()
  259. case OP_LOAD_CLASS_GLOBAL: {
  260. assert(__curr_class != nullptr);
  261. StrName _name(byte.arg);
  262. PyVar _0 = getattr(__curr_class, _name, false);
  263. if(_0 != nullptr) {
  264. PUSH(_0);
  265. DISPATCH()
  266. }
  267. // load global if attribute not found
  268. _0 = frame->f_globals().try_get_likely_found(_name);
  269. if(_0 != nullptr) {
  270. PUSH(_0);
  271. DISPATCH()
  272. }
  273. _0 = vm->builtins->attr().try_get_likely_found(_name);
  274. if(_0 != nullptr) {
  275. PUSH(_0);
  276. DISPATCH()
  277. }
  278. vm->NameError(_name);
  279. }
  280. DISPATCH()
  281. case OP_LOAD_METHOD: {
  282. PyVar _0;
  283. TOP() = get_unbound_method(TOP(), StrName(byte.arg), &_0, true, true);
  284. PUSH(_0);
  285. }
  286. DISPATCH()
  287. case OP_LOAD_SUBSCR: {
  288. PyVar _1 = POPX(); // b
  289. PyVar _0 = TOP(); // a
  290. auto _ti = _tp_info(_0);
  291. if(_ti->m__getitem__) {
  292. TOP() = _ti->m__getitem__(this, _0, _1);
  293. } else {
  294. TOP() = call_method(_0, __getitem__, _1);
  295. }
  296. }
  297. DISPATCH()
  298. case OP_LOAD_SUBSCR_FAST: {
  299. PyVar _1 = frame->_locals[byte.arg];
  300. if(_1 == PY_NULL) vm->UnboundLocalError(c11__getitem(uint16_t, &frame->co->varnames, byte.arg));
  301. PyVar _0 = TOP(); // a
  302. auto _ti = _tp_info(_0);
  303. if(_ti->m__getitem__) {
  304. TOP() = _ti->m__getitem__(this, _0, _1);
  305. } else {
  306. TOP() = call_method(_0, __getitem__, _1);
  307. }
  308. }
  309. DISPATCH()
  310. case OP_LOAD_SUBSCR_SMALL_INT: {
  311. PyVar _1 = VAR((int16_t)byte.arg);
  312. PyVar _0 = TOP(); // a
  313. auto _ti = _tp_info(_0);
  314. if(_ti->m__getitem__) {
  315. TOP() = _ti->m__getitem__(this, _0, _1);
  316. } else {
  317. TOP() = call_method(_0, __getitem__, _1);
  318. }
  319. }
  320. DISPATCH()
  321. case OP_STORE_FAST: frame->_locals[byte.arg] = POPX(); DISPATCH()
  322. case OP_STORE_NAME: {
  323. StrName _name(byte.arg);
  324. PyVar _0 = POPX();
  325. if(frame->_callable != nullptr) {
  326. PyVar* slot = frame->_locals.try_get_name(_name);
  327. if(slot != nullptr) {
  328. *slot = _0; // store in locals if possible
  329. } else {
  330. Function& func = frame->_callable->as<Function>();
  331. if(func.decl == __dynamic_func_decl) {
  332. assert(func._closure != nullptr);
  333. func._closure->set(_name, _0);
  334. } else {
  335. vm->NameError(_name);
  336. }
  337. }
  338. } else {
  339. frame->f_globals().set(_name, _0);
  340. }
  341. }
  342. DISPATCH()
  343. case OP_STORE_GLOBAL: frame->f_globals().set(StrName(byte.arg), POPX()); DISPATCH()
  344. case OP_STORE_ATTR: {
  345. PyVar _0 = TOP(); // a
  346. PyVar _1 = SECOND(); // val
  347. setattr(_0, StrName(byte.arg), _1);
  348. STACK_SHRINK(2);
  349. }
  350. DISPATCH()
  351. case OP_STORE_SUBSCR: {
  352. PyVar _2 = POPX(); // b
  353. PyVar _1 = POPX(); // a
  354. PyVar _0 = POPX(); // val
  355. auto _ti = _tp_info(_1);
  356. if(_ti->m__setitem__) {
  357. _ti->m__setitem__(this, _1, _2, _0);
  358. } else {
  359. call_method(_1, __setitem__, _2, _0);
  360. }
  361. }
  362. DISPATCH()
  363. case OP_STORE_SUBSCR_FAST: {
  364. PyVar _2 = frame->_locals[byte.arg]; // b
  365. if(_2 == PY_NULL) vm->UnboundLocalError(c11__getitem(uint16_t, &frame->co->varnames, byte.arg));
  366. PyVar _1 = POPX(); // a
  367. PyVar _0 = POPX(); // val
  368. auto _ti = _tp_info(_1);
  369. if(_ti->m__setitem__) {
  370. _ti->m__setitem__(this, _1, _2, _0);
  371. } else {
  372. call_method(_1, __setitem__, _2, _0);
  373. }
  374. }
  375. DISPATCH()
  376. case OP_DELETE_FAST: {
  377. PyVar _0 = frame->_locals[byte.arg];
  378. if(_0 == PY_NULL) vm->UnboundLocalError(c11__getitem(uint16_t, &frame->co->varnames, byte.arg));
  379. frame->_locals[byte.arg].set_null();
  380. }
  381. DISPATCH()
  382. case OP_DELETE_NAME: {
  383. StrName _name(byte.arg);
  384. if(frame->_callable != nullptr) {
  385. PyVar* slot = frame->_locals.try_get_name(_name);
  386. if(slot != nullptr) {
  387. slot->set_null();
  388. } else {
  389. Function& func = frame->_callable->as<Function>();
  390. if(func.decl == __dynamic_func_decl) {
  391. assert(func._closure != nullptr);
  392. bool ok = func._closure->del(_name);
  393. if(!ok) vm->NameError(_name);
  394. } else {
  395. vm->NameError(_name);
  396. }
  397. }
  398. } else {
  399. if(!frame->f_globals().del(_name)) vm->NameError(_name);
  400. }
  401. }
  402. DISPATCH()
  403. case OP_DELETE_GLOBAL: {
  404. StrName _name(byte.arg);
  405. if(!frame->f_globals().del(_name)) vm->NameError(_name);
  406. }
  407. DISPATCH()
  408. case OP_DELETE_ATTR: {
  409. PyVar _0 = POPX();
  410. delattr(_0, StrName(byte.arg));
  411. }
  412. DISPATCH()
  413. case OP_DELETE_SUBSCR: {
  414. PyVar _1 = POPX();
  415. PyVar _0 = POPX();
  416. auto _ti = _tp_info(_0);
  417. if(_ti->m__delitem__) {
  418. _ti->m__delitem__(this, _0, _1);
  419. } else {
  420. call_method(_0, __delitem__, _1);
  421. }
  422. }
  423. DISPATCH()
  424. /*****************************************/
  425. case OP_BUILD_LONG: {
  426. PyVar _0 = builtins->attr().try_get_likely_found(pk_id_long);
  427. if(_0 == nullptr) AttributeError(builtins, pk_id_long);
  428. TOP() = call(_0, TOP());
  429. }
  430. DISPATCH()
  431. case OP_BUILD_IMAG: {
  432. PyVar _0 = builtins->attr().try_get_likely_found(pk_id_complex);
  433. if(_0 == nullptr) AttributeError(builtins, pk_id_long);
  434. TOP() = call(_0, VAR(0), TOP());
  435. }
  436. DISPATCH()
  437. case OP_BUILD_BYTES: {
  438. const Str& s = CAST(Str&, TOP());
  439. unsigned char* p = (unsigned char*)std::malloc(s.size);
  440. std::memcpy(p, s.c_str(), s.size);
  441. TOP() = VAR(Bytes(p, s.size));
  442. }
  443. DISPATCH()
  444. case OP_BUILD_TUPLE: {
  445. PyVar _0 = VAR(STACK_VIEW(byte.arg).to_tuple());
  446. STACK_SHRINK(byte.arg);
  447. PUSH(_0);
  448. }
  449. DISPATCH()
  450. case OP_BUILD_LIST: {
  451. PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list());
  452. STACK_SHRINK(byte.arg);
  453. PUSH(_0);
  454. }
  455. DISPATCH()
  456. case OP_BUILD_DICT: {
  457. if(byte.arg == 0) {
  458. PUSH(VAR(Dict()));
  459. DISPATCH()
  460. }
  461. PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list());
  462. _0 = call(_t(tp_dict), _0);
  463. STACK_SHRINK(byte.arg);
  464. PUSH(_0);
  465. }
  466. DISPATCH()
  467. case OP_BUILD_SET: {
  468. PyVar _0 = VAR(STACK_VIEW(byte.arg).to_list());
  469. _0 = call(builtins->attr()[pk_id_set], _0);
  470. STACK_SHRINK(byte.arg);
  471. PUSH(_0);
  472. }
  473. DISPATCH()
  474. case OP_BUILD_SLICE: {
  475. PyVar _2 = POPX(); // step
  476. PyVar _1 = POPX(); // stop
  477. PyVar _0 = POPX(); // start
  478. PUSH(VAR(Slice(_0, _1, _2)));
  479. }
  480. DISPATCH()
  481. case OP_BUILD_STRING: {
  482. SStream ss;
  483. ArgsView view = STACK_VIEW(byte.arg);
  484. for(PyVar obj: view)
  485. ss << py_str(obj);
  486. STACK_SHRINK(byte.arg);
  487. PUSH(VAR(ss.str()));
  488. }
  489. DISPATCH()
  490. /*****************************************/
  491. case OP_BUILD_TUPLE_UNPACK: {
  492. List list;
  493. __unpack_as_list(STACK_VIEW(byte.arg), list);
  494. STACK_SHRINK(byte.arg);
  495. PyVar _0 = VAR(list.to_tuple());
  496. PUSH(_0);
  497. }
  498. DISPATCH()
  499. case OP_BUILD_LIST_UNPACK: {
  500. List list;
  501. __unpack_as_list(STACK_VIEW(byte.arg), list);
  502. STACK_SHRINK(byte.arg);
  503. PyVar _0 = VAR(std::move(list));
  504. PUSH(_0);
  505. }
  506. DISPATCH()
  507. case OP_BUILD_DICT_UNPACK: {
  508. Dict dict;
  509. __unpack_as_dict(STACK_VIEW(byte.arg), dict);
  510. STACK_SHRINK(byte.arg);
  511. PyVar _0 = VAR(std::move(dict));
  512. PUSH(_0);
  513. }
  514. DISPATCH()
  515. case OP_BUILD_SET_UNPACK: {
  516. List list;
  517. __unpack_as_list(STACK_VIEW(byte.arg), list);
  518. STACK_SHRINK(byte.arg);
  519. PyVar _0 = VAR(std::move(list));
  520. _0 = call(builtins->attr()[pk_id_set], _0);
  521. PUSH(_0);
  522. }
  523. DISPATCH()
  524. /*****************************************/
  525. #define BINARY_OP_SPECIAL(func) \
  526. _ti = _tp_info(_0); \
  527. if(_ti->m##func) { \
  528. TOP() = _ti->m##func(this, _0, _1); \
  529. } else { \
  530. PyVar self; \
  531. PyVar _2 = get_unbound_method(_0, func, &self, false); \
  532. if(_2 != nullptr) \
  533. TOP() = call_method(self, _2, _1); \
  534. else \
  535. TOP() = NotImplemented; \
  536. }
  537. #define BINARY_OP_RSPECIAL(op, func) \
  538. if(is_not_implemented(TOP())) { \
  539. PyVar self; \
  540. PyVar _2 = get_unbound_method(_1, func, &self, false); \
  541. if(_2 != nullptr) \
  542. TOP() = call_method(self, _2, _0); \
  543. else \
  544. BinaryOptError(op, _0, _1); \
  545. if(is_not_implemented(TOP())) BinaryOptError(op, _0, _1); \
  546. }
  547. case OP_BINARY_TRUEDIV: {
  548. PyVar _1 = POPX();
  549. PyVar _0 = TOP();
  550. const PyTypeInfo* _ti;
  551. BINARY_OP_SPECIAL(__truediv__);
  552. if(is_not_implemented(TOP())) BinaryOptError("/", _0, _1);
  553. }
  554. DISPATCH()
  555. case OP_BINARY_POW: {
  556. PyVar _1 = POPX();
  557. PyVar _0 = TOP();
  558. const PyTypeInfo* _ti;
  559. BINARY_OP_SPECIAL(__pow__);
  560. if(is_not_implemented(TOP())) BinaryOptError("**", _0, _1);
  561. }
  562. DISPATCH()
  563. case OP_BINARY_ADD: {
  564. PyVar _1 = POPX();
  565. PyVar _0 = TOP();
  566. PREDICT_INT_OP(+)
  567. const PyTypeInfo* _ti;
  568. BINARY_OP_SPECIAL(__add__);
  569. BINARY_OP_RSPECIAL("+", __radd__);
  570. }
  571. DISPATCH()
  572. case OP_BINARY_SUB: {
  573. PyVar _1 = POPX();
  574. PyVar _0 = TOP();
  575. PREDICT_INT_OP(-)
  576. const PyTypeInfo* _ti;
  577. BINARY_OP_SPECIAL(__sub__);
  578. BINARY_OP_RSPECIAL("-", __rsub__);
  579. }
  580. DISPATCH()
  581. case OP_BINARY_MUL: {
  582. PyVar _1 = POPX();
  583. PyVar _0 = TOP();
  584. PREDICT_INT_OP(*)
  585. const PyTypeInfo* _ti;
  586. BINARY_OP_SPECIAL(__mul__);
  587. BINARY_OP_RSPECIAL("*", __rmul__);
  588. }
  589. DISPATCH()
  590. case OP_BINARY_FLOORDIV: {
  591. PyVar _1 = POPX();
  592. PyVar _0 = TOP();
  593. PREDICT_INT_DIV_OP(/)
  594. const PyTypeInfo* _ti;
  595. BINARY_OP_SPECIAL(__floordiv__);
  596. if(is_not_implemented(TOP())) BinaryOptError("//", _0, _1);
  597. }
  598. DISPATCH()
  599. case OP_BINARY_MOD: {
  600. PyVar _1 = POPX();
  601. PyVar _0 = TOP();
  602. PREDICT_INT_DIV_OP(%)
  603. const PyTypeInfo* _ti;
  604. BINARY_OP_SPECIAL(__mod__);
  605. if(is_not_implemented(TOP())) BinaryOptError("%", _0, _1);
  606. }
  607. DISPATCH()
  608. case OP_COMPARE_LT: {
  609. PyVar _1 = POPX();
  610. PyVar _0 = TOP();
  611. PREDICT_INT_OP(<)
  612. TOP() = VAR(py_lt(_0, _1));
  613. }
  614. DISPATCH()
  615. case OP_COMPARE_LE: {
  616. PyVar _1 = POPX();
  617. PyVar _0 = TOP();
  618. PREDICT_INT_OP(<=)
  619. TOP() = VAR(py_le(_0, _1));
  620. }
  621. DISPATCH()
  622. case OP_COMPARE_EQ: {
  623. PyVar _1 = POPX();
  624. PyVar _0 = TOP();
  625. TOP() = VAR(py_eq(_0, _1));
  626. }
  627. DISPATCH()
  628. case OP_COMPARE_NE: {
  629. PyVar _1 = POPX();
  630. PyVar _0 = TOP();
  631. TOP() = VAR(py_ne(_0, _1));
  632. }
  633. DISPATCH()
  634. case OP_COMPARE_GT: {
  635. PyVar _1 = POPX();
  636. PyVar _0 = TOP();
  637. PREDICT_INT_OP(>)
  638. TOP() = VAR(py_gt(_0, _1));
  639. }
  640. DISPATCH()
  641. case OP_COMPARE_GE: {
  642. PyVar _1 = POPX();
  643. PyVar _0 = TOP();
  644. PREDICT_INT_OP(>=)
  645. TOP() = VAR(py_ge(_0, _1));
  646. }
  647. DISPATCH()
  648. case OP_BITWISE_LSHIFT: {
  649. PyVar _1 = POPX();
  650. PyVar _0 = TOP();
  651. PREDICT_INT_OP(<<)
  652. const PyTypeInfo* _ti;
  653. BINARY_OP_SPECIAL(__lshift__);
  654. if(is_not_implemented(TOP())) BinaryOptError("<<", _0, _1);
  655. }
  656. DISPATCH()
  657. case OP_BITWISE_RSHIFT: {
  658. PyVar _1 = POPX();
  659. PyVar _0 = TOP();
  660. PREDICT_INT_OP(>>)
  661. const PyTypeInfo* _ti;
  662. BINARY_OP_SPECIAL(__rshift__);
  663. if(is_not_implemented(TOP())) BinaryOptError(">>", _0, _1);
  664. }
  665. DISPATCH()
  666. case OP_BITWISE_AND: {
  667. PyVar _1 = POPX();
  668. PyVar _0 = TOP();
  669. PREDICT_INT_OP(&)
  670. const PyTypeInfo* _ti;
  671. BINARY_OP_SPECIAL(__and__);
  672. if(is_not_implemented(TOP())) BinaryOptError("&", _0, _1);
  673. }
  674. DISPATCH()
  675. case OP_BITWISE_OR: {
  676. PyVar _1 = POPX();
  677. PyVar _0 = TOP();
  678. PREDICT_INT_OP(|)
  679. const PyTypeInfo* _ti;
  680. BINARY_OP_SPECIAL(__or__);
  681. if(is_not_implemented(TOP())) BinaryOptError("|", _0, _1);
  682. }
  683. DISPATCH()
  684. case OP_BITWISE_XOR: {
  685. PyVar _1 = POPX();
  686. PyVar _0 = TOP();
  687. PREDICT_INT_OP(^)
  688. const PyTypeInfo* _ti;
  689. BINARY_OP_SPECIAL(__xor__);
  690. if(is_not_implemented(TOP())) BinaryOptError("^", _0, _1);
  691. }
  692. DISPATCH()
  693. case OP_BINARY_MATMUL: {
  694. PyVar _1 = POPX();
  695. PyVar _0 = TOP();
  696. const PyTypeInfo* _ti;
  697. BINARY_OP_SPECIAL(__matmul__);
  698. if(is_not_implemented(TOP())) BinaryOptError("@", _0, _1);
  699. }
  700. DISPATCH()
  701. #undef BINARY_OP_SPECIAL
  702. #undef BINARY_OP_RSPECIAL
  703. #undef PREDICT_INT_OP
  704. case OP_IS_OP: {
  705. PyVar _1 = POPX(); // rhs
  706. PyVar _0 = TOP(); // lhs
  707. TOP() = PyVar__IS_OP(&_0, &_1) ? True : False;
  708. }
  709. DISPATCH()
  710. case OP_IS_NOT_OP: {
  711. PyVar _1 = POPX(); // rhs
  712. PyVar _0 = TOP(); // lhs
  713. TOP() = PyVar__IS_OP(&_0, &_1) ? False : True;
  714. }
  715. DISPATCH()
  716. case OP_CONTAINS_OP: {
  717. // a in b -> b __contains__ a
  718. auto _ti = _tp_info(TOP());
  719. PyVar _0;
  720. if(_ti->m__contains__) {
  721. _0 = _ti->m__contains__(this, TOP(), SECOND());
  722. } else {
  723. _0 = call_method(TOP(), __contains__, SECOND());
  724. }
  725. POP();
  726. TOP() = VAR(static_cast<bool>((int)CAST(bool, _0) ^ byte.arg));
  727. }
  728. DISPATCH()
  729. /*****************************************/
  730. case OP_JUMP_FORWARD: DISPATCH_JUMP((int16_t)byte.arg)
  731. case OP_POP_JUMP_IF_FALSE:
  732. if(!py_bool(POPX())) DISPATCH_JUMP((int16_t)byte.arg)
  733. DISPATCH()
  734. case OP_POP_JUMP_IF_TRUE:
  735. if(py_bool(POPX())) DISPATCH_JUMP((int16_t)byte.arg)
  736. DISPATCH()
  737. case OP_JUMP_IF_TRUE_OR_POP:
  738. if(py_bool(TOP())) {
  739. DISPATCH_JUMP((int16_t)byte.arg)
  740. } else {
  741. POP();
  742. DISPATCH()
  743. }
  744. case OP_JUMP_IF_FALSE_OR_POP:
  745. if(!py_bool(TOP())) {
  746. DISPATCH_JUMP((int16_t)byte.arg)
  747. } else {
  748. POP();
  749. DISPATCH()
  750. }
  751. case OP_SHORTCUT_IF_FALSE_OR_POP:
  752. if(!py_bool(TOP())) { // [b, False]
  753. STACK_SHRINK(2); // []
  754. PUSH(vm->False); // [False]
  755. DISPATCH_JUMP((int16_t)byte.arg)
  756. } else {
  757. POP(); // [b]
  758. DISPATCH()
  759. }
  760. case OP_LOOP_CONTINUE:
  761. // just an alias of OP_JUMP_FORWARD
  762. DISPATCH_JUMP((int16_t)byte.arg)
  763. case OP_LOOP_BREAK: {
  764. frame->prepare_jump_break(&s_data, frame->ip() + byte.arg);
  765. DISPATCH_JUMP((int16_t)byte.arg)
  766. }
  767. case OP_JUMP_ABSOLUTE_TOP: DISPATCH_JUMP_ABSOLUTE(_CAST(int, POPX()))
  768. case OP_GOTO: {
  769. StrName _name(byte.arg);
  770. int target = c11_smallmap_n2i__get(&frame->co->labels, byte.arg, -1);
  771. if(target < 0) RuntimeError(_S("label ", _name.escape(), " not found"));
  772. frame->prepare_jump_break(&s_data, target);
  773. DISPATCH_JUMP_ABSOLUTE(target)
  774. }
  775. /*****************************************/
  776. case OP_FSTRING_EVAL: {
  777. PyVar _0 = c11__getitem(PyVar, &frame->co->consts, byte.arg);
  778. std::string_view string = CAST(Str&, _0).sv();
  779. // TODO: optimize this
  780. CodeObject* code = vm->compile(string, "<eval>", EVAL_MODE, true);
  781. _0 = vm->_exec(code, frame->_module, frame->_callable, frame->_locals);
  782. CodeObject__delete(code);
  783. PUSH(_0);
  784. }
  785. DISPATCH()
  786. case OP_REPR: TOP() = VAR(py_repr(TOP())); DISPATCH()
  787. case OP_CALL: {
  788. pk_ManagedHeap__collect_if_needed(&heap);
  789. PyVar _0 = vectorcall(byte.arg & 0xFF, // ARGC
  790. (byte.arg >> 8) & 0xFF, // KWARGC
  791. true);
  792. if(_0.type == tp_op_call) {
  793. frame = &callstack.top();
  794. goto __NEXT_FRAME;
  795. }
  796. PUSH(_0);
  797. }
  798. DISPATCH()
  799. case OP_CALL_TP: {
  800. pk_ManagedHeap__collect_if_needed(&heap);
  801. PyVar _0;
  802. PyVar _1;
  803. PyVar _2;
  804. // [callable, <self>, args: tuple, kwargs: dict | NULL]
  805. if(byte.arg) {
  806. _2 = POPX();
  807. _1 = POPX();
  808. for(PyVar obj: _CAST(Tuple&, _1))
  809. PUSH(obj);
  810. _CAST(Dict&, _2).apply([this](PyVar k, PyVar v) {
  811. PUSH(VAR(StrName(CAST(Str&, k)).index));
  812. PUSH(v);
  813. });
  814. _0 = vectorcall(_CAST(Tuple&, _1).size(), // ARGC
  815. _CAST(Dict&, _2).size(), // KWARGC
  816. true);
  817. } else {
  818. // no **kwargs
  819. _1 = POPX();
  820. for(PyVar obj: _CAST(Tuple&, _1))
  821. PUSH(obj);
  822. _0 = vectorcall(_CAST(Tuple&, _1).size(), // ARGC
  823. 0, // KWARGC
  824. true);
  825. }
  826. if(_0.type == tp_op_call) {
  827. frame = &callstack.top();
  828. goto __NEXT_FRAME;
  829. }
  830. PUSH(_0);
  831. }
  832. DISPATCH()
  833. case OP_RETURN_VALUE: {
  834. PyVar _0 = byte.arg == BC_NOARG ? POPX() : None;
  835. __pop_frame();
  836. if(frame == base_frame) { // [ frameBase<- ]
  837. return _0;
  838. } else {
  839. frame = &callstack.top();
  840. PUSH(_0);
  841. goto __NEXT_FRAME;
  842. }
  843. }
  844. DISPATCH()
  845. case OP_YIELD_VALUE: return PY_OP_YIELD;
  846. /*****************************************/
  847. case OP_LIST_APPEND: {
  848. PyVar _0 = POPX();
  849. PK_OBJ_GET(List, SECOND()).push_back(_0);
  850. }
  851. DISPATCH()
  852. case OP_DICT_ADD: {
  853. PyVar _0 = POPX();
  854. const Tuple& t = PK_OBJ_GET(Tuple, _0);
  855. PK_OBJ_GET(Dict, SECOND()).set(this, t[0], t[1]);
  856. }
  857. DISPATCH()
  858. case OP_SET_ADD: {
  859. PyVar _0 = POPX();
  860. call_method(SECOND(), pk_id_add, _0);
  861. }
  862. DISPATCH()
  863. /*****************************************/
  864. case OP_UNARY_NEGATIVE: TOP() = py_negate(TOP()); DISPATCH()
  865. case OP_UNARY_NOT: TOP() = VAR(!py_bool(TOP())); DISPATCH()
  866. case OP_UNARY_STAR: TOP() = VAR(StarWrapper(byte.arg, TOP())); DISPATCH()
  867. case OP_UNARY_INVERT: {
  868. PyVar _0;
  869. auto _ti = _tp_info(TOP());
  870. if(_ti->m__invert__)
  871. _0 = _ti->m__invert__(this, TOP());
  872. else
  873. _0 = call_method(TOP(), __invert__);
  874. TOP() = _0;
  875. }
  876. DISPATCH()
  877. /*****************************************/
  878. case OP_GET_ITER: TOP() = py_iter(TOP()); DISPATCH()
  879. case OP_GET_ITER_NEW: TOP() = py_iter(TOP()); DISPATCH()
  880. case OP_FOR_ITER: {
  881. PyVar _0 = py_next(TOP());
  882. if(_0 == StopIteration) {
  883. int target = frame->prepare_loop_break(&s_data);
  884. DISPATCH_JUMP_ABSOLUTE(target)
  885. } else {
  886. PUSH(_0);
  887. DISPATCH()
  888. }
  889. }
  890. case OP_FOR_ITER_STORE_FAST: {
  891. PyVar _0 = py_next(TOP());
  892. if(_0 == StopIteration) {
  893. int target = frame->prepare_loop_break(&s_data);
  894. DISPATCH_JUMP_ABSOLUTE(target)
  895. } else {
  896. frame->_locals[byte.arg] = _0;
  897. DISPATCH()
  898. }
  899. }
  900. case OP_FOR_ITER_STORE_GLOBAL: {
  901. PyVar _0 = py_next(TOP());
  902. if(_0 == StopIteration) {
  903. int target = frame->prepare_loop_break(&s_data);
  904. DISPATCH_JUMP_ABSOLUTE(target)
  905. } else {
  906. frame->f_globals().set(StrName(byte.arg), _0);
  907. DISPATCH()
  908. }
  909. }
  910. case OP_FOR_ITER_YIELD_VALUE: {
  911. PyVar _0 = py_next(TOP());
  912. if(_0 == StopIteration) {
  913. int target = frame->prepare_loop_break(&s_data);
  914. DISPATCH_JUMP_ABSOLUTE(target)
  915. } else {
  916. PUSH(_0);
  917. return PY_OP_YIELD;
  918. }
  919. }
  920. case OP_FOR_ITER_UNPACK: {
  921. PyVar _0 = TOP();
  922. const PyTypeInfo* _ti = _tp_info(_0);
  923. if(_ti->op__next__) {
  924. unsigned n = _ti->op__next__(this, _0);
  925. if(n == 0) {
  926. // StopIteration
  927. int target = frame->prepare_loop_break(&s_data);
  928. DISPATCH_JUMP_ABSOLUTE(target)
  929. } else if(n == 1) {
  930. // UNPACK_SEQUENCE
  931. __op_unpack_sequence(byte.arg);
  932. } else {
  933. if(n != byte.arg) {
  934. ValueError(_S("expected ", (int)byte.arg, " values to unpack, got ", (int)n));
  935. }
  936. }
  937. } else {
  938. // FOR_ITER
  939. _0 = call_method(_0, __next__);
  940. if(_0 != StopIteration) {
  941. PUSH(_0);
  942. // UNPACK_SEQUENCE
  943. __op_unpack_sequence(byte.arg);
  944. } else {
  945. int target = frame->prepare_loop_break(&s_data);
  946. DISPATCH_JUMP_ABSOLUTE(target)
  947. }
  948. }
  949. }
  950. DISPATCH()
  951. /*****************************************/
  952. case OP_IMPORT_PATH: {
  953. PyVar _0 = c11__getitem(PyVar, &frame->co->consts, byte.arg);
  954. PUSH(py_import(CAST(Str&, _0)));
  955. }
  956. DISPATCH()
  957. case OP_POP_IMPORT_STAR: {
  958. PyVar _0 = POPX(); // pop the module
  959. PyVar _1 = _0->attr().try_get(__all__);
  960. StrName _name;
  961. if(_1 != nullptr) {
  962. for(PyVar key: CAST(List&, _1)) {
  963. _name = StrName::get(CAST(Str&, key).sv());
  964. PyVar value = _0->attr().try_get_likely_found(_name);
  965. if(value == nullptr) {
  966. ImportError(_S("cannot import name ", _name.escape()));
  967. } else {
  968. frame->f_globals().set(_name, value);
  969. }
  970. }
  971. } else {
  972. for(auto& [name, value]: _0->attr().items()) {
  973. std::string_view s = name.sv();
  974. if(s.empty() || s[0] == '_') continue;
  975. frame->f_globals().set(name, value);
  976. }
  977. }
  978. }
  979. DISPATCH()
  980. /*****************************************/
  981. case OP_UNPACK_SEQUENCE: {
  982. __op_unpack_sequence(byte.arg);
  983. }
  984. DISPATCH()
  985. case OP_UNPACK_EX: {
  986. auto _lock = gc_scope_lock(); // lock the gc via RAII!!
  987. PyVar _0 = py_iter(POPX());
  988. const PyTypeInfo* _ti = _tp_info(_0);
  989. PyVar _1;
  990. for(int i = 0; i < byte.arg; i++) {
  991. _1 = _py_next(_ti, _0);
  992. if(_1 == StopIteration) ValueError("not enough values to unpack");
  993. PUSH(_1);
  994. }
  995. List extras;
  996. while(true) {
  997. _1 = _py_next(_ti, _0);
  998. if(_1 == StopIteration) break;
  999. extras.push_back(_1);
  1000. }
  1001. PUSH(VAR(std::move(extras)));
  1002. }
  1003. DISPATCH()
  1004. /*****************************************/
  1005. case OP_BEGIN_CLASS: {
  1006. StrName _name(byte.arg);
  1007. PyVar _0 = POPX(); // super
  1008. if(is_none(_0)) _0 = _t(tp_object);
  1009. check_type(_0, tp_type);
  1010. __curr_class = new_type_object(frame->_module, _name, PK_OBJ_GET(Type, _0), true);
  1011. }
  1012. DISPATCH()
  1013. case OP_END_CLASS: {
  1014. assert(__curr_class != nullptr);
  1015. StrName _name(byte.arg);
  1016. frame->_module->attr().set(_name, __curr_class);
  1017. // call on_end_subclass
  1018. PyTypeInfo* ti = &_all_types[__curr_class->as<Type>()];
  1019. if(ti->base != tp_object) {
  1020. PyTypeInfo* base_ti = &_all_types[ti->base];
  1021. if(base_ti->on_end_subclass) base_ti->on_end_subclass(this, ti);
  1022. }
  1023. __curr_class = nullptr;
  1024. }
  1025. DISPATCH()
  1026. case OP_STORE_CLASS_ATTR: {
  1027. assert(__curr_class != nullptr);
  1028. StrName _name(byte.arg);
  1029. PyVar _0 = POPX();
  1030. if(is_type(_0, tp_function)) { PK_OBJ_GET(Function, _0)._class = __curr_class; }
  1031. __curr_class->attr().set(_name, _0);
  1032. }
  1033. DISPATCH()
  1034. case OP_BEGIN_CLASS_DECORATION: {
  1035. PUSH(__curr_class);
  1036. }
  1037. DISPATCH()
  1038. case OP_END_CLASS_DECORATION: {
  1039. __curr_class = POPX().get();
  1040. }
  1041. DISPATCH()
  1042. case OP_ADD_CLASS_ANNOTATION: {
  1043. assert(__curr_class != nullptr);
  1044. StrName _name(byte.arg);
  1045. Type type = __curr_class->as<Type>();
  1046. _all_types[type].annotated_fields.push_back(_name);
  1047. }
  1048. DISPATCH()
  1049. /*****************************************/
  1050. case OP_WITH_ENTER: PUSH(call_method(TOP(), __enter__)); DISPATCH()
  1051. case OP_WITH_EXIT:
  1052. call_method(TOP(), __exit__);
  1053. POP();
  1054. DISPATCH()
  1055. /*****************************************/
  1056. case OP_TRY_ENTER: {
  1057. frame->set_unwind_target(s_data.sp);
  1058. DISPATCH()
  1059. }
  1060. case OP_EXCEPTION_MATCH: {
  1061. PyVar assumed_type = POPX();
  1062. check_type(assumed_type, tp_type);
  1063. PyVar e_obj = TOP();
  1064. bool ok = isinstance(e_obj, PK_OBJ_GET(Type, assumed_type));
  1065. PUSH(VAR(ok));
  1066. }
  1067. DISPATCH()
  1068. case OP_RAISE: {
  1069. if(is_type(TOP(), tp_type)) { TOP() = call(TOP()); }
  1070. if(!isinstance(TOP(), tp_exception)) { TypeError("exceptions must derive from Exception"); }
  1071. _error(POPX());
  1072. }
  1073. DISPATCH()
  1074. case OP_RAISE_ASSERT:
  1075. if(byte.arg) {
  1076. Str msg = py_str(TOP());
  1077. POP();
  1078. AssertionError(msg);
  1079. } else {
  1080. AssertionError();
  1081. }
  1082. DISPATCH()
  1083. case OP_RE_RAISE: __raise_exc(true); DISPATCH()
  1084. case OP_POP_EXCEPTION: __last_exception = POPX().get(); DISPATCH()
  1085. /*****************************************/
  1086. case OP_FORMAT_STRING: {
  1087. PyVar _0 = POPX();
  1088. const Str& spec = CAST(Str&, c11__getitem(PyVar, &frame->co->consts, byte.arg));
  1089. PUSH(__format_object(_0, spec));
  1090. }
  1091. DISPATCH()
  1092. /*****************************************/
  1093. default: PK_UNREACHABLE()
  1094. }
  1095. }
  1096. /**********************************************************************/
  1097. PK_UNREACHABLE()
  1098. } catch(InternalException internal) {
  1099. __internal_exception = internal;
  1100. if(internal.type == InternalExceptionType::Unhandled) {
  1101. __last_exception = POPX().get();
  1102. Exception& _e = __last_exception->as<Exception>();
  1103. bool is_base_frame_to_be_popped = frame == base_frame;
  1104. __pop_frame();
  1105. if(callstack.empty()) {
  1106. // propagate to the top level
  1107. throw TopLevelException(this, &_e);
  1108. }
  1109. frame = &callstack.top();
  1110. PUSH(__last_exception);
  1111. if(is_base_frame_to_be_popped) { throw InternalException(InternalExceptionType::ToBeRaised); }
  1112. }
  1113. }
  1114. }
  1115. }
  1116. #undef TOP
  1117. #undef SECOND
  1118. #undef THIRD
  1119. #undef STACK_SHRINK
  1120. #undef PUSH
  1121. #undef POP
  1122. #undef POPX
  1123. #undef STACK_VIEW
  1124. #undef DISPATCH
  1125. #undef DISPATCH_JUMP
  1126. #undef CEVAL_STEP_CALLBACK
  1127. } // namespace pkpy