ceval.cpp 31 KB

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