ceval.cpp 31 KB

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