vm.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2014 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include "py/emitglue.h"
  31. #include "py/objtype.h"
  32. #include "py/runtime.h"
  33. #include "py/bc0.h"
  34. #include "py/bc.h"
  35. #if 0
  36. #define TRACE(ip) printf("sp=%d ", (int)(sp - &code_state->state[0] + 1)); mp_bytecode_print2(ip, 1, code_state->fun_bc->const_table);
  37. #else
  38. #define TRACE(ip)
  39. #endif
  40. // Value stack grows up (this makes it incompatible with native C stack, but
  41. // makes sure that arguments to functions are in natural order arg1..argN
  42. // (Python semantics mandates left-to-right evaluation order, including for
  43. // function arguments). Stack pointer is pre-incremented and points at the
  44. // top element.
  45. // Exception stack also grows up, top element is also pointed at.
  46. #define DECODE_UINT \
  47. mp_uint_t unum = 0; \
  48. do { \
  49. unum = (unum << 7) + (*ip & 0x7f); \
  50. } while ((*ip++ & 0x80) != 0)
  51. #define DECODE_ULABEL size_t ulab = (ip[0] | (ip[1] << 8)); ip += 2
  52. #define DECODE_SLABEL size_t slab = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2
  53. #if MICROPY_PERSISTENT_CODE
  54. #define DECODE_QSTR \
  55. qstr qst = ip[0] | ip[1] << 8; \
  56. ip += 2;
  57. #define DECODE_PTR \
  58. DECODE_UINT; \
  59. void *ptr = (void*)(uintptr_t)code_state->fun_bc->const_table[unum]
  60. #define DECODE_OBJ \
  61. DECODE_UINT; \
  62. mp_obj_t obj = (mp_obj_t)code_state->fun_bc->const_table[unum]
  63. #else
  64. #define DECODE_QSTR qstr qst = 0; \
  65. do { \
  66. qst = (qst << 7) + (*ip & 0x7f); \
  67. } while ((*ip++ & 0x80) != 0)
  68. #define DECODE_PTR \
  69. ip = (byte*)MP_ALIGN(ip, sizeof(void*)); \
  70. void *ptr = *(void**)ip; \
  71. ip += sizeof(void*)
  72. #define DECODE_OBJ \
  73. ip = (byte*)MP_ALIGN(ip, sizeof(mp_obj_t)); \
  74. mp_obj_t obj = *(mp_obj_t*)ip; \
  75. ip += sizeof(mp_obj_t)
  76. #endif
  77. #define PUSH(val) *++sp = (val)
  78. #define POP() (*sp--)
  79. #define TOP() (*sp)
  80. #define SET_TOP(val) *sp = (val)
  81. #if MICROPY_PY_SYS_EXC_INFO
  82. #define CLEAR_SYS_EXC_INFO() MP_STATE_VM(cur_exception) = NULL;
  83. #else
  84. #define CLEAR_SYS_EXC_INFO()
  85. #endif
  86. #define PUSH_EXC_BLOCK(with_or_finally) do { \
  87. DECODE_ULABEL; /* except labels are always forward */ \
  88. ++exc_sp; \
  89. exc_sp->handler = ip + ulab; \
  90. exc_sp->val_sp = MP_TAGPTR_MAKE(sp, ((with_or_finally) << 1) | currently_in_except_block); \
  91. exc_sp->prev_exc = NULL; \
  92. currently_in_except_block = 0; /* in a try block now */ \
  93. } while (0)
  94. #define POP_EXC_BLOCK() \
  95. currently_in_except_block = MP_TAGPTR_TAG0(exc_sp->val_sp); /* restore previous state */ \
  96. exc_sp--; /* pop back to previous exception handler */ \
  97. CLEAR_SYS_EXC_INFO() /* just clear sys.exc_info(), not compliant, but it shouldn't be used in 1st place */
  98. // fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
  99. // sp points to bottom of stack which grows up
  100. // returns:
  101. // MP_VM_RETURN_NORMAL, sp valid, return value in *sp
  102. // MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
  103. // MP_VM_RETURN_EXCEPTION, exception in fastn[0]
  104. mp_vm_return_kind_t mp_execute_bytecode(mp_code_state_t *code_state, volatile mp_obj_t inject_exc) {
  105. #define SELECTIVE_EXC_IP (0)
  106. #if SELECTIVE_EXC_IP
  107. #define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip; } /* stores ip 1 byte past last opcode */
  108. #define MARK_EXC_IP_GLOBAL()
  109. #else
  110. #define MARK_EXC_IP_SELECTIVE()
  111. #define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; } /* stores ip pointing to last opcode */
  112. #endif
  113. #if MICROPY_OPT_COMPUTED_GOTO
  114. #include "py/vmentrytable.h"
  115. #define DISPATCH() do { \
  116. TRACE(ip); \
  117. MARK_EXC_IP_GLOBAL(); \
  118. goto *entry_table[*ip++]; \
  119. } while (0)
  120. #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
  121. #define ENTRY(op) entry_##op
  122. #define ENTRY_DEFAULT entry_default
  123. #else
  124. #define DISPATCH() break
  125. #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
  126. #define ENTRY(op) case op
  127. #define ENTRY_DEFAULT default
  128. #endif
  129. // nlr_raise needs to be implemented as a goto, so that the C compiler's flow analyser
  130. // sees that it's possible for us to jump from the dispatch loop to the exception
  131. // handler. Without this, the code may have a different stack layout in the dispatch
  132. // loop and the exception handler, leading to very obscure bugs.
  133. #define RAISE(o) do { nlr_pop(); nlr.ret_val = MP_OBJ_TO_PTR(o); goto exception_handler; } while (0)
  134. #if MICROPY_STACKLESS
  135. run_code_state: ;
  136. #endif
  137. // Pointers which are constant for particular invocation of mp_execute_bytecode()
  138. mp_obj_t * /*const*/ fastn;
  139. mp_exc_stack_t * /*const*/ exc_stack;
  140. {
  141. size_t n_state = mp_decode_uint_value(code_state->fun_bc->bytecode);
  142. fastn = &code_state->state[n_state - 1];
  143. exc_stack = (mp_exc_stack_t*)(code_state->state + n_state);
  144. }
  145. // variables that are visible to the exception handler (declared volatile)
  146. volatile bool currently_in_except_block = MP_TAGPTR_TAG0(code_state->exc_sp); // 0 or 1, to detect nested exceptions
  147. mp_exc_stack_t *volatile exc_sp = MP_TAGPTR_PTR(code_state->exc_sp); // stack grows up, exc_sp points to top of stack
  148. #if MICROPY_PY_THREAD_GIL && MICROPY_PY_THREAD_GIL_VM_DIVISOR
  149. // This needs to be volatile and outside the VM loop so it persists across handling
  150. // of any exceptions. Otherwise it's possible that the VM never gives up the GIL.
  151. volatile int gil_divisor = MICROPY_PY_THREAD_GIL_VM_DIVISOR;
  152. #endif
  153. // outer exception handling loop
  154. for (;;) {
  155. nlr_buf_t nlr;
  156. outer_dispatch_loop:
  157. if (nlr_push(&nlr) == 0) {
  158. // local variables that are not visible to the exception handler
  159. const byte *ip = code_state->ip;
  160. mp_obj_t *sp = code_state->sp;
  161. mp_obj_t obj_shared;
  162. MICROPY_VM_HOOK_INIT
  163. // If we have exception to inject, now that we finish setting up
  164. // execution context, raise it. This works as if RAISE_VARARGS
  165. // bytecode was executed.
  166. // Injecting exc into yield from generator is a special case,
  167. // handled by MP_BC_YIELD_FROM itself
  168. if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) {
  169. mp_obj_t exc = inject_exc;
  170. inject_exc = MP_OBJ_NULL;
  171. exc = mp_make_raise_obj(exc);
  172. RAISE(exc);
  173. }
  174. // loop to execute byte code
  175. for (;;) {
  176. dispatch_loop:
  177. #if MICROPY_OPT_COMPUTED_GOTO
  178. DISPATCH();
  179. #else
  180. TRACE(ip);
  181. MARK_EXC_IP_GLOBAL();
  182. switch (*ip++) {
  183. #endif
  184. ENTRY(MP_BC_LOAD_CONST_FALSE):
  185. PUSH(mp_const_false);
  186. DISPATCH();
  187. ENTRY(MP_BC_LOAD_CONST_NONE):
  188. PUSH(mp_const_none);
  189. DISPATCH();
  190. ENTRY(MP_BC_LOAD_CONST_TRUE):
  191. PUSH(mp_const_true);
  192. DISPATCH();
  193. ENTRY(MP_BC_LOAD_CONST_SMALL_INT): {
  194. mp_int_t num = 0;
  195. if ((ip[0] & 0x40) != 0) {
  196. // Number is negative
  197. num--;
  198. }
  199. do {
  200. num = (num << 7) | (*ip & 0x7f);
  201. } while ((*ip++ & 0x80) != 0);
  202. PUSH(MP_OBJ_NEW_SMALL_INT(num));
  203. DISPATCH();
  204. }
  205. ENTRY(MP_BC_LOAD_CONST_STRING): {
  206. DECODE_QSTR;
  207. PUSH(MP_OBJ_NEW_QSTR(qst));
  208. DISPATCH();
  209. }
  210. ENTRY(MP_BC_LOAD_CONST_OBJ): {
  211. DECODE_OBJ;
  212. PUSH(obj);
  213. DISPATCH();
  214. }
  215. ENTRY(MP_BC_LOAD_NULL):
  216. PUSH(MP_OBJ_NULL);
  217. DISPATCH();
  218. ENTRY(MP_BC_LOAD_FAST_N): {
  219. DECODE_UINT;
  220. obj_shared = fastn[-unum];
  221. load_check:
  222. if (obj_shared == MP_OBJ_NULL) {
  223. local_name_error: {
  224. MARK_EXC_IP_SELECTIVE();
  225. mp_obj_t obj = mp_obj_new_exception_msg(&mp_type_NameError, "local variable referenced before assignment");
  226. RAISE(obj);
  227. }
  228. }
  229. PUSH(obj_shared);
  230. DISPATCH();
  231. }
  232. ENTRY(MP_BC_LOAD_DEREF): {
  233. DECODE_UINT;
  234. obj_shared = mp_obj_cell_get(fastn[-unum]);
  235. goto load_check;
  236. }
  237. #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
  238. ENTRY(MP_BC_LOAD_NAME): {
  239. MARK_EXC_IP_SELECTIVE();
  240. DECODE_QSTR;
  241. PUSH(mp_load_name(qst));
  242. DISPATCH();
  243. }
  244. #else
  245. ENTRY(MP_BC_LOAD_NAME): {
  246. MARK_EXC_IP_SELECTIVE();
  247. DECODE_QSTR;
  248. mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
  249. mp_uint_t x = *ip;
  250. if (x < mp_locals_get()->map.alloc && mp_locals_get()->map.table[x].key == key) {
  251. PUSH(mp_locals_get()->map.table[x].value);
  252. } else {
  253. mp_map_elem_t *elem = mp_map_lookup(&mp_locals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
  254. if (elem != NULL) {
  255. *(byte*)ip = (elem - &mp_locals_get()->map.table[0]) & 0xff;
  256. PUSH(elem->value);
  257. } else {
  258. PUSH(mp_load_name(MP_OBJ_QSTR_VALUE(key)));
  259. }
  260. }
  261. ip++;
  262. DISPATCH();
  263. }
  264. #endif
  265. #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
  266. ENTRY(MP_BC_LOAD_GLOBAL): {
  267. MARK_EXC_IP_SELECTIVE();
  268. DECODE_QSTR;
  269. PUSH(mp_load_global(qst));
  270. DISPATCH();
  271. }
  272. #else
  273. ENTRY(MP_BC_LOAD_GLOBAL): {
  274. MARK_EXC_IP_SELECTIVE();
  275. DECODE_QSTR;
  276. mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
  277. mp_uint_t x = *ip;
  278. if (x < mp_globals_get()->map.alloc && mp_globals_get()->map.table[x].key == key) {
  279. PUSH(mp_globals_get()->map.table[x].value);
  280. } else {
  281. mp_map_elem_t *elem = mp_map_lookup(&mp_globals_get()->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
  282. if (elem != NULL) {
  283. *(byte*)ip = (elem - &mp_globals_get()->map.table[0]) & 0xff;
  284. PUSH(elem->value);
  285. } else {
  286. PUSH(mp_load_global(MP_OBJ_QSTR_VALUE(key)));
  287. }
  288. }
  289. ip++;
  290. DISPATCH();
  291. }
  292. #endif
  293. #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
  294. ENTRY(MP_BC_LOAD_ATTR): {
  295. MARK_EXC_IP_SELECTIVE();
  296. DECODE_QSTR;
  297. SET_TOP(mp_load_attr(TOP(), qst));
  298. DISPATCH();
  299. }
  300. #else
  301. ENTRY(MP_BC_LOAD_ATTR): {
  302. MARK_EXC_IP_SELECTIVE();
  303. DECODE_QSTR;
  304. mp_obj_t top = TOP();
  305. if (mp_obj_get_type(top)->attr == mp_obj_instance_attr) {
  306. mp_obj_instance_t *self = MP_OBJ_TO_PTR(top);
  307. mp_uint_t x = *ip;
  308. mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
  309. mp_map_elem_t *elem;
  310. if (x < self->members.alloc && self->members.table[x].key == key) {
  311. elem = &self->members.table[x];
  312. } else {
  313. elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
  314. if (elem != NULL) {
  315. *(byte*)ip = elem - &self->members.table[0];
  316. } else {
  317. goto load_attr_cache_fail;
  318. }
  319. }
  320. SET_TOP(elem->value);
  321. ip++;
  322. DISPATCH();
  323. }
  324. load_attr_cache_fail:
  325. SET_TOP(mp_load_attr(top, qst));
  326. ip++;
  327. DISPATCH();
  328. }
  329. #endif
  330. ENTRY(MP_BC_LOAD_METHOD): {
  331. MARK_EXC_IP_SELECTIVE();
  332. DECODE_QSTR;
  333. mp_load_method(*sp, qst, sp);
  334. sp += 1;
  335. DISPATCH();
  336. }
  337. ENTRY(MP_BC_LOAD_SUPER_METHOD): {
  338. MARK_EXC_IP_SELECTIVE();
  339. DECODE_QSTR;
  340. sp -= 1;
  341. mp_load_super_method(qst, sp - 1);
  342. DISPATCH();
  343. }
  344. ENTRY(MP_BC_LOAD_BUILD_CLASS):
  345. MARK_EXC_IP_SELECTIVE();
  346. PUSH(mp_load_build_class());
  347. DISPATCH();
  348. ENTRY(MP_BC_LOAD_SUBSCR): {
  349. MARK_EXC_IP_SELECTIVE();
  350. mp_obj_t index = POP();
  351. SET_TOP(mp_obj_subscr(TOP(), index, MP_OBJ_SENTINEL));
  352. DISPATCH();
  353. }
  354. ENTRY(MP_BC_STORE_FAST_N): {
  355. DECODE_UINT;
  356. fastn[-unum] = POP();
  357. DISPATCH();
  358. }
  359. ENTRY(MP_BC_STORE_DEREF): {
  360. DECODE_UINT;
  361. mp_obj_cell_set(fastn[-unum], POP());
  362. DISPATCH();
  363. }
  364. ENTRY(MP_BC_STORE_NAME): {
  365. MARK_EXC_IP_SELECTIVE();
  366. DECODE_QSTR;
  367. mp_store_name(qst, POP());
  368. DISPATCH();
  369. }
  370. ENTRY(MP_BC_STORE_GLOBAL): {
  371. MARK_EXC_IP_SELECTIVE();
  372. DECODE_QSTR;
  373. mp_store_global(qst, POP());
  374. DISPATCH();
  375. }
  376. #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
  377. ENTRY(MP_BC_STORE_ATTR): {
  378. MARK_EXC_IP_SELECTIVE();
  379. DECODE_QSTR;
  380. mp_store_attr(sp[0], qst, sp[-1]);
  381. sp -= 2;
  382. DISPATCH();
  383. }
  384. #else
  385. // This caching code works with MICROPY_PY_BUILTINS_PROPERTY and/or
  386. // MICROPY_PY_DESCRIPTORS enabled because if the attr exists in
  387. // self->members then it can't be a property or have descriptors. A
  388. // consequence of this is that we can't use MP_MAP_LOOKUP_ADD_IF_NOT_FOUND
  389. // in the fast-path below, because that store could override a property.
  390. ENTRY(MP_BC_STORE_ATTR): {
  391. MARK_EXC_IP_SELECTIVE();
  392. DECODE_QSTR;
  393. mp_obj_t top = TOP();
  394. if (mp_obj_get_type(top)->attr == mp_obj_instance_attr && sp[-1] != MP_OBJ_NULL) {
  395. mp_obj_instance_t *self = MP_OBJ_TO_PTR(top);
  396. mp_uint_t x = *ip;
  397. mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
  398. mp_map_elem_t *elem;
  399. if (x < self->members.alloc && self->members.table[x].key == key) {
  400. elem = &self->members.table[x];
  401. } else {
  402. elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
  403. if (elem != NULL) {
  404. *(byte*)ip = elem - &self->members.table[0];
  405. } else {
  406. goto store_attr_cache_fail;
  407. }
  408. }
  409. elem->value = sp[-1];
  410. sp -= 2;
  411. ip++;
  412. DISPATCH();
  413. }
  414. store_attr_cache_fail:
  415. mp_store_attr(sp[0], qst, sp[-1]);
  416. sp -= 2;
  417. ip++;
  418. DISPATCH();
  419. }
  420. #endif
  421. ENTRY(MP_BC_STORE_SUBSCR):
  422. MARK_EXC_IP_SELECTIVE();
  423. mp_obj_subscr(sp[-1], sp[0], sp[-2]);
  424. sp -= 3;
  425. DISPATCH();
  426. ENTRY(MP_BC_DELETE_FAST): {
  427. MARK_EXC_IP_SELECTIVE();
  428. DECODE_UINT;
  429. if (fastn[-unum] == MP_OBJ_NULL) {
  430. goto local_name_error;
  431. }
  432. fastn[-unum] = MP_OBJ_NULL;
  433. DISPATCH();
  434. }
  435. ENTRY(MP_BC_DELETE_DEREF): {
  436. MARK_EXC_IP_SELECTIVE();
  437. DECODE_UINT;
  438. if (mp_obj_cell_get(fastn[-unum]) == MP_OBJ_NULL) {
  439. goto local_name_error;
  440. }
  441. mp_obj_cell_set(fastn[-unum], MP_OBJ_NULL);
  442. DISPATCH();
  443. }
  444. ENTRY(MP_BC_DELETE_NAME): {
  445. MARK_EXC_IP_SELECTIVE();
  446. DECODE_QSTR;
  447. mp_delete_name(qst);
  448. DISPATCH();
  449. }
  450. ENTRY(MP_BC_DELETE_GLOBAL): {
  451. MARK_EXC_IP_SELECTIVE();
  452. DECODE_QSTR;
  453. mp_delete_global(qst);
  454. DISPATCH();
  455. }
  456. ENTRY(MP_BC_DUP_TOP): {
  457. mp_obj_t top = TOP();
  458. PUSH(top);
  459. DISPATCH();
  460. }
  461. ENTRY(MP_BC_DUP_TOP_TWO):
  462. sp += 2;
  463. sp[0] = sp[-2];
  464. sp[-1] = sp[-3];
  465. DISPATCH();
  466. ENTRY(MP_BC_POP_TOP):
  467. sp -= 1;
  468. DISPATCH();
  469. ENTRY(MP_BC_ROT_TWO): {
  470. mp_obj_t top = sp[0];
  471. sp[0] = sp[-1];
  472. sp[-1] = top;
  473. DISPATCH();
  474. }
  475. ENTRY(MP_BC_ROT_THREE): {
  476. mp_obj_t top = sp[0];
  477. sp[0] = sp[-1];
  478. sp[-1] = sp[-2];
  479. sp[-2] = top;
  480. DISPATCH();
  481. }
  482. ENTRY(MP_BC_JUMP): {
  483. DECODE_SLABEL;
  484. ip += slab;
  485. DISPATCH_WITH_PEND_EXC_CHECK();
  486. }
  487. ENTRY(MP_BC_POP_JUMP_IF_TRUE): {
  488. DECODE_SLABEL;
  489. if (mp_obj_is_true(POP())) {
  490. ip += slab;
  491. }
  492. DISPATCH_WITH_PEND_EXC_CHECK();
  493. }
  494. ENTRY(MP_BC_POP_JUMP_IF_FALSE): {
  495. DECODE_SLABEL;
  496. if (!mp_obj_is_true(POP())) {
  497. ip += slab;
  498. }
  499. DISPATCH_WITH_PEND_EXC_CHECK();
  500. }
  501. ENTRY(MP_BC_JUMP_IF_TRUE_OR_POP): {
  502. DECODE_SLABEL;
  503. if (mp_obj_is_true(TOP())) {
  504. ip += slab;
  505. } else {
  506. sp--;
  507. }
  508. DISPATCH_WITH_PEND_EXC_CHECK();
  509. }
  510. ENTRY(MP_BC_JUMP_IF_FALSE_OR_POP): {
  511. DECODE_SLABEL;
  512. if (mp_obj_is_true(TOP())) {
  513. sp--;
  514. } else {
  515. ip += slab;
  516. }
  517. DISPATCH_WITH_PEND_EXC_CHECK();
  518. }
  519. ENTRY(MP_BC_SETUP_WITH): {
  520. MARK_EXC_IP_SELECTIVE();
  521. // stack: (..., ctx_mgr)
  522. mp_obj_t obj = TOP();
  523. mp_load_method(obj, MP_QSTR___exit__, sp);
  524. mp_load_method(obj, MP_QSTR___enter__, sp + 2);
  525. mp_obj_t ret = mp_call_method_n_kw(0, 0, sp + 2);
  526. sp += 1;
  527. PUSH_EXC_BLOCK(1);
  528. PUSH(ret);
  529. // stack: (..., __exit__, ctx_mgr, as_value)
  530. DISPATCH();
  531. }
  532. ENTRY(MP_BC_WITH_CLEANUP): {
  533. MARK_EXC_IP_SELECTIVE();
  534. // Arriving here, there's "exception control block" on top of stack,
  535. // and __exit__ method (with self) underneath it. Bytecode calls __exit__,
  536. // and "deletes" it off stack, shifting "exception control block"
  537. // to its place.
  538. // The bytecode emitter ensures that there is enough space on the Python
  539. // value stack to hold the __exit__ method plus an additional 4 entries.
  540. if (TOP() == mp_const_none) {
  541. // stack: (..., __exit__, ctx_mgr, None)
  542. sp[1] = mp_const_none;
  543. sp[2] = mp_const_none;
  544. sp -= 2;
  545. mp_call_method_n_kw(3, 0, sp);
  546. SET_TOP(mp_const_none);
  547. } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
  548. // Getting here there are two distinct cases:
  549. // - unwind return, stack: (..., __exit__, ctx_mgr, ret_val, SMALL_INT(-1))
  550. // - unwind jump, stack: (..., __exit__, ctx_mgr, dest_ip, SMALL_INT(num_exc))
  551. // For both cases we do exactly the same thing.
  552. mp_obj_t data = sp[-1];
  553. mp_obj_t cause = sp[0];
  554. sp[-1] = mp_const_none;
  555. sp[0] = mp_const_none;
  556. sp[1] = mp_const_none;
  557. mp_call_method_n_kw(3, 0, sp - 3);
  558. sp[-3] = data;
  559. sp[-2] = cause;
  560. sp -= 2; // we removed (__exit__, ctx_mgr)
  561. } else {
  562. assert(mp_obj_is_exception_instance(TOP()));
  563. // stack: (..., __exit__, ctx_mgr, exc_instance)
  564. // Need to pass (exc_type, exc_instance, None) as arguments to __exit__.
  565. sp[1] = sp[0];
  566. sp[0] = MP_OBJ_FROM_PTR(mp_obj_get_type(sp[0]));
  567. sp[2] = mp_const_none;
  568. sp -= 2;
  569. mp_obj_t ret_value = mp_call_method_n_kw(3, 0, sp);
  570. if (mp_obj_is_true(ret_value)) {
  571. // We need to silence/swallow the exception. This is done
  572. // by popping the exception and the __exit__ handler and
  573. // replacing it with None, which signals END_FINALLY to just
  574. // execute the finally handler normally.
  575. SET_TOP(mp_const_none);
  576. assert(exc_sp >= exc_stack);
  577. POP_EXC_BLOCK();
  578. } else {
  579. // We need to re-raise the exception. We pop __exit__ handler
  580. // by copying the exception instance down to the new top-of-stack.
  581. sp[0] = sp[3];
  582. }
  583. }
  584. DISPATCH();
  585. }
  586. ENTRY(MP_BC_UNWIND_JUMP): {
  587. MARK_EXC_IP_SELECTIVE();
  588. DECODE_SLABEL;
  589. PUSH((mp_obj_t)(mp_uint_t)(uintptr_t)(ip + slab)); // push destination ip for jump
  590. PUSH((mp_obj_t)(mp_uint_t)(*ip)); // push number of exception handlers to unwind (0x80 bit set if we also need to pop stack)
  591. unwind_jump:;
  592. mp_uint_t unum = (mp_uint_t)POP(); // get number of exception handlers to unwind
  593. while ((unum & 0x7f) > 0) {
  594. unum -= 1;
  595. assert(exc_sp >= exc_stack);
  596. if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
  597. // Getting here the stack looks like:
  598. // (..., X, dest_ip)
  599. // where X is pointed to by exc_sp->val_sp and in the case
  600. // of a "with" block contains the context manager info.
  601. // We're going to run "finally" code as a coroutine
  602. // (not calling it recursively). Set up a sentinel
  603. // on the stack so it can return back to us when it is
  604. // done (when WITH_CLEANUP or END_FINALLY reached).
  605. // The sentinel is the number of exception handlers left to
  606. // unwind, which is a non-negative integer.
  607. PUSH(MP_OBJ_NEW_SMALL_INT(unum));
  608. ip = exc_sp->handler; // get exception handler byte code address
  609. exc_sp--; // pop exception handler
  610. goto dispatch_loop; // run the exception handler
  611. }
  612. POP_EXC_BLOCK();
  613. }
  614. ip = (const byte*)MP_OBJ_TO_PTR(POP()); // pop destination ip for jump
  615. if (unum != 0) {
  616. // pop the exhausted iterator
  617. sp -= MP_OBJ_ITER_BUF_NSLOTS;
  618. }
  619. DISPATCH_WITH_PEND_EXC_CHECK();
  620. }
  621. // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
  622. ENTRY(MP_BC_SETUP_EXCEPT):
  623. ENTRY(MP_BC_SETUP_FINALLY): {
  624. MARK_EXC_IP_SELECTIVE();
  625. #if SELECTIVE_EXC_IP
  626. PUSH_EXC_BLOCK((code_state->ip[-1] == MP_BC_SETUP_FINALLY) ? 1 : 0);
  627. #else
  628. PUSH_EXC_BLOCK((code_state->ip[0] == MP_BC_SETUP_FINALLY) ? 1 : 0);
  629. #endif
  630. DISPATCH();
  631. }
  632. ENTRY(MP_BC_END_FINALLY):
  633. MARK_EXC_IP_SELECTIVE();
  634. // if TOS is None, just pops it and continues
  635. // if TOS is an integer, finishes coroutine and returns control to caller
  636. // if TOS is an exception, reraises the exception
  637. if (TOP() == mp_const_none) {
  638. sp--;
  639. } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
  640. // We finished "finally" coroutine and now dispatch back
  641. // to our caller, based on TOS value
  642. mp_int_t cause = MP_OBJ_SMALL_INT_VALUE(POP());
  643. if (cause < 0) {
  644. // A negative cause indicates unwind return
  645. goto unwind_return;
  646. } else {
  647. // Otherwise it's an unwind jump and we must push as a raw
  648. // number the number of exception handlers to unwind
  649. PUSH((mp_obj_t)cause);
  650. goto unwind_jump;
  651. }
  652. } else {
  653. assert(mp_obj_is_exception_instance(TOP()));
  654. RAISE(TOP());
  655. }
  656. DISPATCH();
  657. ENTRY(MP_BC_GET_ITER):
  658. MARK_EXC_IP_SELECTIVE();
  659. SET_TOP(mp_getiter(TOP(), NULL));
  660. DISPATCH();
  661. // An iterator for a for-loop takes MP_OBJ_ITER_BUF_NSLOTS slots on
  662. // the Python value stack. These slots are either used to store the
  663. // iterator object itself, or the first slot is MP_OBJ_NULL and
  664. // the second slot holds a reference to the iterator object.
  665. ENTRY(MP_BC_GET_ITER_STACK): {
  666. MARK_EXC_IP_SELECTIVE();
  667. mp_obj_t obj = TOP();
  668. mp_obj_iter_buf_t *iter_buf = (mp_obj_iter_buf_t*)sp;
  669. sp += MP_OBJ_ITER_BUF_NSLOTS - 1;
  670. obj = mp_getiter(obj, iter_buf);
  671. if (obj != MP_OBJ_FROM_PTR(iter_buf)) {
  672. // Iterator didn't use the stack so indicate that with MP_OBJ_NULL.
  673. sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] = MP_OBJ_NULL;
  674. sp[-MP_OBJ_ITER_BUF_NSLOTS + 2] = obj;
  675. }
  676. DISPATCH();
  677. }
  678. ENTRY(MP_BC_FOR_ITER): {
  679. MARK_EXC_IP_SELECTIVE();
  680. DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
  681. code_state->sp = sp;
  682. mp_obj_t obj;
  683. if (sp[-MP_OBJ_ITER_BUF_NSLOTS + 1] == MP_OBJ_NULL) {
  684. obj = sp[-MP_OBJ_ITER_BUF_NSLOTS + 2];
  685. } else {
  686. obj = MP_OBJ_FROM_PTR(&sp[-MP_OBJ_ITER_BUF_NSLOTS + 1]);
  687. }
  688. mp_obj_t value = mp_iternext_allow_raise(obj);
  689. if (value == MP_OBJ_STOP_ITERATION) {
  690. sp -= MP_OBJ_ITER_BUF_NSLOTS; // pop the exhausted iterator
  691. ip += ulab; // jump to after for-block
  692. } else {
  693. PUSH(value); // push the next iteration value
  694. }
  695. DISPATCH();
  696. }
  697. // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
  698. ENTRY(MP_BC_POP_BLOCK):
  699. // we are exiting an exception handler, so pop the last one of the exception-stack
  700. assert(exc_sp >= exc_stack);
  701. POP_EXC_BLOCK();
  702. DISPATCH();
  703. // matched against: SETUP_EXCEPT
  704. ENTRY(MP_BC_POP_EXCEPT):
  705. assert(exc_sp >= exc_stack);
  706. assert(currently_in_except_block);
  707. POP_EXC_BLOCK();
  708. DISPATCH();
  709. ENTRY(MP_BC_BUILD_TUPLE): {
  710. MARK_EXC_IP_SELECTIVE();
  711. DECODE_UINT;
  712. sp -= unum - 1;
  713. SET_TOP(mp_obj_new_tuple(unum, sp));
  714. DISPATCH();
  715. }
  716. ENTRY(MP_BC_BUILD_LIST): {
  717. MARK_EXC_IP_SELECTIVE();
  718. DECODE_UINT;
  719. sp -= unum - 1;
  720. SET_TOP(mp_obj_new_list(unum, sp));
  721. DISPATCH();
  722. }
  723. ENTRY(MP_BC_BUILD_MAP): {
  724. MARK_EXC_IP_SELECTIVE();
  725. DECODE_UINT;
  726. PUSH(mp_obj_new_dict(unum));
  727. DISPATCH();
  728. }
  729. ENTRY(MP_BC_STORE_MAP):
  730. MARK_EXC_IP_SELECTIVE();
  731. sp -= 2;
  732. mp_obj_dict_store(sp[0], sp[2], sp[1]);
  733. DISPATCH();
  734. #if MICROPY_PY_BUILTINS_SET
  735. ENTRY(MP_BC_BUILD_SET): {
  736. MARK_EXC_IP_SELECTIVE();
  737. DECODE_UINT;
  738. sp -= unum - 1;
  739. SET_TOP(mp_obj_new_set(unum, sp));
  740. DISPATCH();
  741. }
  742. #endif
  743. #if MICROPY_PY_BUILTINS_SLICE
  744. ENTRY(MP_BC_BUILD_SLICE): {
  745. MARK_EXC_IP_SELECTIVE();
  746. DECODE_UINT;
  747. if (unum == 2) {
  748. mp_obj_t stop = POP();
  749. mp_obj_t start = TOP();
  750. SET_TOP(mp_obj_new_slice(start, stop, mp_const_none));
  751. } else {
  752. mp_obj_t step = POP();
  753. mp_obj_t stop = POP();
  754. mp_obj_t start = TOP();
  755. SET_TOP(mp_obj_new_slice(start, stop, step));
  756. }
  757. DISPATCH();
  758. }
  759. #endif
  760. ENTRY(MP_BC_STORE_COMP): {
  761. MARK_EXC_IP_SELECTIVE();
  762. DECODE_UINT;
  763. mp_obj_t obj = sp[-(unum >> 2)];
  764. if ((unum & 3) == 0) {
  765. mp_obj_list_append(obj, sp[0]);
  766. sp--;
  767. } else if (!MICROPY_PY_BUILTINS_SET || (unum & 3) == 1) {
  768. mp_obj_dict_store(obj, sp[0], sp[-1]);
  769. sp -= 2;
  770. #if MICROPY_PY_BUILTINS_SET
  771. } else {
  772. mp_obj_set_store(obj, sp[0]);
  773. sp--;
  774. #endif
  775. }
  776. DISPATCH();
  777. }
  778. ENTRY(MP_BC_UNPACK_SEQUENCE): {
  779. MARK_EXC_IP_SELECTIVE();
  780. DECODE_UINT;
  781. mp_unpack_sequence(sp[0], unum, sp);
  782. sp += unum - 1;
  783. DISPATCH();
  784. }
  785. ENTRY(MP_BC_UNPACK_EX): {
  786. MARK_EXC_IP_SELECTIVE();
  787. DECODE_UINT;
  788. mp_unpack_ex(sp[0], unum, sp);
  789. sp += (unum & 0xff) + ((unum >> 8) & 0xff);
  790. DISPATCH();
  791. }
  792. ENTRY(MP_BC_MAKE_FUNCTION): {
  793. DECODE_PTR;
  794. PUSH(mp_make_function_from_raw_code(ptr, MP_OBJ_NULL, MP_OBJ_NULL));
  795. DISPATCH();
  796. }
  797. ENTRY(MP_BC_MAKE_FUNCTION_DEFARGS): {
  798. DECODE_PTR;
  799. // Stack layout: def_tuple def_dict <- TOS
  800. mp_obj_t def_dict = POP();
  801. SET_TOP(mp_make_function_from_raw_code(ptr, TOP(), def_dict));
  802. DISPATCH();
  803. }
  804. ENTRY(MP_BC_MAKE_CLOSURE): {
  805. DECODE_PTR;
  806. size_t n_closed_over = *ip++;
  807. // Stack layout: closed_overs <- TOS
  808. sp -= n_closed_over - 1;
  809. SET_TOP(mp_make_closure_from_raw_code(ptr, n_closed_over, sp));
  810. DISPATCH();
  811. }
  812. ENTRY(MP_BC_MAKE_CLOSURE_DEFARGS): {
  813. DECODE_PTR;
  814. size_t n_closed_over = *ip++;
  815. // Stack layout: def_tuple def_dict closed_overs <- TOS
  816. sp -= 2 + n_closed_over - 1;
  817. SET_TOP(mp_make_closure_from_raw_code(ptr, 0x100 | n_closed_over, sp));
  818. DISPATCH();
  819. }
  820. ENTRY(MP_BC_CALL_FUNCTION): {
  821. MARK_EXC_IP_SELECTIVE();
  822. DECODE_UINT;
  823. // unum & 0xff == n_positional
  824. // (unum >> 8) & 0xff == n_keyword
  825. sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
  826. #if MICROPY_STACKLESS
  827. if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
  828. code_state->ip = ip;
  829. code_state->sp = sp;
  830. code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
  831. mp_code_state_t *new_state = mp_obj_fun_bc_prepare_codestate(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1);
  832. if (new_state) {
  833. new_state->prev = code_state;
  834. code_state = new_state;
  835. nlr_pop();
  836. goto run_code_state;
  837. }
  838. #if MICROPY_STACKLESS_STRICT
  839. else {
  840. deep_recursion_error:
  841. mp_raise_recursion_depth();
  842. }
  843. #else
  844. // If we couldn't allocate codestate on heap, in
  845. // non non-strict case fall thru to stack allocation.
  846. #endif
  847. }
  848. #endif
  849. SET_TOP(mp_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
  850. DISPATCH();
  851. }
  852. ENTRY(MP_BC_CALL_FUNCTION_VAR_KW): {
  853. MARK_EXC_IP_SELECTIVE();
  854. DECODE_UINT;
  855. // unum & 0xff == n_positional
  856. // (unum >> 8) & 0xff == n_keyword
  857. // We have following stack layout here:
  858. // fun arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS
  859. sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 2;
  860. #if MICROPY_STACKLESS
  861. if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
  862. code_state->ip = ip;
  863. code_state->sp = sp;
  864. code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
  865. mp_call_args_t out_args;
  866. mp_call_prepare_args_n_kw_var(false, unum, sp, &out_args);
  867. mp_code_state_t *new_state = mp_obj_fun_bc_prepare_codestate(out_args.fun,
  868. out_args.n_args, out_args.n_kw, out_args.args);
  869. #if !MICROPY_ENABLE_PYSTACK
  870. // Freeing args at this point does not follow a LIFO order so only do it if
  871. // pystack is not enabled. For pystack, they are freed when code_state is.
  872. mp_nonlocal_free(out_args.args, out_args.n_alloc * sizeof(mp_obj_t));
  873. #endif
  874. if (new_state) {
  875. new_state->prev = code_state;
  876. code_state = new_state;
  877. nlr_pop();
  878. goto run_code_state;
  879. }
  880. #if MICROPY_STACKLESS_STRICT
  881. else {
  882. goto deep_recursion_error;
  883. }
  884. #else
  885. // If we couldn't allocate codestate on heap, in
  886. // non non-strict case fall thru to stack allocation.
  887. #endif
  888. }
  889. #endif
  890. SET_TOP(mp_call_method_n_kw_var(false, unum, sp));
  891. DISPATCH();
  892. }
  893. ENTRY(MP_BC_CALL_METHOD): {
  894. MARK_EXC_IP_SELECTIVE();
  895. DECODE_UINT;
  896. // unum & 0xff == n_positional
  897. // (unum >> 8) & 0xff == n_keyword
  898. sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
  899. #if MICROPY_STACKLESS
  900. if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
  901. code_state->ip = ip;
  902. code_state->sp = sp;
  903. code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
  904. size_t n_args = unum & 0xff;
  905. size_t n_kw = (unum >> 8) & 0xff;
  906. int adjust = (sp[1] == MP_OBJ_NULL) ? 0 : 1;
  907. mp_code_state_t *new_state = mp_obj_fun_bc_prepare_codestate(*sp, n_args + adjust, n_kw, sp + 2 - adjust);
  908. if (new_state) {
  909. new_state->prev = code_state;
  910. code_state = new_state;
  911. nlr_pop();
  912. goto run_code_state;
  913. }
  914. #if MICROPY_STACKLESS_STRICT
  915. else {
  916. goto deep_recursion_error;
  917. }
  918. #else
  919. // If we couldn't allocate codestate on heap, in
  920. // non non-strict case fall thru to stack allocation.
  921. #endif
  922. }
  923. #endif
  924. SET_TOP(mp_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
  925. DISPATCH();
  926. }
  927. ENTRY(MP_BC_CALL_METHOD_VAR_KW): {
  928. MARK_EXC_IP_SELECTIVE();
  929. DECODE_UINT;
  930. // unum & 0xff == n_positional
  931. // (unum >> 8) & 0xff == n_keyword
  932. // We have following stack layout here:
  933. // fun self arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS
  934. sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 3;
  935. #if MICROPY_STACKLESS
  936. if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
  937. code_state->ip = ip;
  938. code_state->sp = sp;
  939. code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
  940. mp_call_args_t out_args;
  941. mp_call_prepare_args_n_kw_var(true, unum, sp, &out_args);
  942. mp_code_state_t *new_state = mp_obj_fun_bc_prepare_codestate(out_args.fun,
  943. out_args.n_args, out_args.n_kw, out_args.args);
  944. #if !MICROPY_ENABLE_PYSTACK
  945. // Freeing args at this point does not follow a LIFO order so only do it if
  946. // pystack is not enabled. For pystack, they are freed when code_state is.
  947. mp_nonlocal_free(out_args.args, out_args.n_alloc * sizeof(mp_obj_t));
  948. #endif
  949. if (new_state) {
  950. new_state->prev = code_state;
  951. code_state = new_state;
  952. nlr_pop();
  953. goto run_code_state;
  954. }
  955. #if MICROPY_STACKLESS_STRICT
  956. else {
  957. goto deep_recursion_error;
  958. }
  959. #else
  960. // If we couldn't allocate codestate on heap, in
  961. // non non-strict case fall thru to stack allocation.
  962. #endif
  963. }
  964. #endif
  965. SET_TOP(mp_call_method_n_kw_var(true, unum, sp));
  966. DISPATCH();
  967. }
  968. ENTRY(MP_BC_RETURN_VALUE):
  969. MARK_EXC_IP_SELECTIVE();
  970. // These next 3 lines pop a try-finally exception handler, if one
  971. // is there on the exception stack. Without this the finally block
  972. // is executed a second time when the return is executed, because
  973. // the try-finally exception handler is still on the stack.
  974. // TODO Possibly find a better way to handle this case.
  975. if (currently_in_except_block) {
  976. POP_EXC_BLOCK();
  977. }
  978. unwind_return:
  979. while (exc_sp >= exc_stack) {
  980. if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
  981. // Getting here the stack looks like:
  982. // (..., X, [iter0, iter1, ...,] ret_val)
  983. // where X is pointed to by exc_sp->val_sp and in the case
  984. // of a "with" block contains the context manager info.
  985. // There may be 0 or more for-iterators between X and the
  986. // return value, and these must be removed before control can
  987. // pass to the finally code. We simply copy the ret_value down
  988. // over these iterators, if they exist. If they don't then the
  989. // following is a null operation.
  990. mp_obj_t *finally_sp = MP_TAGPTR_PTR(exc_sp->val_sp);
  991. finally_sp[1] = sp[0];
  992. sp = &finally_sp[1];
  993. // We're going to run "finally" code as a coroutine
  994. // (not calling it recursively). Set up a sentinel
  995. // on a stack so it can return back to us when it is
  996. // done (when WITH_CLEANUP or END_FINALLY reached).
  997. PUSH(MP_OBJ_NEW_SMALL_INT(-1));
  998. ip = exc_sp->handler;
  999. exc_sp--;
  1000. goto dispatch_loop;
  1001. }
  1002. exc_sp--;
  1003. }
  1004. nlr_pop();
  1005. code_state->sp = sp;
  1006. assert(exc_sp == exc_stack - 1);
  1007. MICROPY_VM_HOOK_RETURN
  1008. #if MICROPY_STACKLESS
  1009. if (code_state->prev != NULL) {
  1010. mp_obj_t res = *sp;
  1011. mp_globals_set(code_state->old_globals);
  1012. mp_code_state_t *new_code_state = code_state->prev;
  1013. #if MICROPY_ENABLE_PYSTACK
  1014. // Free code_state, and args allocated by mp_call_prepare_args_n_kw_var
  1015. // (The latter is implicitly freed when using pystack due to its LIFO nature.)
  1016. // The sizeof in the following statement does not include the size of the variable
  1017. // part of the struct. This arg is anyway not used if pystack is enabled.
  1018. mp_nonlocal_free(code_state, sizeof(mp_code_state_t));
  1019. #endif
  1020. code_state = new_code_state;
  1021. *code_state->sp = res;
  1022. goto run_code_state;
  1023. }
  1024. #endif
  1025. return MP_VM_RETURN_NORMAL;
  1026. ENTRY(MP_BC_RAISE_VARARGS): {
  1027. MARK_EXC_IP_SELECTIVE();
  1028. mp_uint_t unum = *ip++;
  1029. mp_obj_t obj;
  1030. if (unum == 2) {
  1031. mp_warning("exception chaining not supported");
  1032. // ignore (pop) "from" argument
  1033. sp--;
  1034. }
  1035. if (unum == 0) {
  1036. // search for the inner-most previous exception, to reraise it
  1037. obj = MP_OBJ_NULL;
  1038. for (mp_exc_stack_t *e = exc_sp; e >= exc_stack; e--) {
  1039. if (e->prev_exc != NULL) {
  1040. obj = MP_OBJ_FROM_PTR(e->prev_exc);
  1041. break;
  1042. }
  1043. }
  1044. if (obj == MP_OBJ_NULL) {
  1045. obj = mp_obj_new_exception_msg(&mp_type_RuntimeError, "no active exception to reraise");
  1046. RAISE(obj);
  1047. }
  1048. } else {
  1049. obj = POP();
  1050. }
  1051. obj = mp_make_raise_obj(obj);
  1052. RAISE(obj);
  1053. }
  1054. ENTRY(MP_BC_YIELD_VALUE):
  1055. yield:
  1056. nlr_pop();
  1057. code_state->ip = ip;
  1058. code_state->sp = sp;
  1059. code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
  1060. return MP_VM_RETURN_YIELD;
  1061. ENTRY(MP_BC_YIELD_FROM): {
  1062. MARK_EXC_IP_SELECTIVE();
  1063. //#define EXC_MATCH(exc, type) MP_OBJ_IS_TYPE(exc, type)
  1064. #define EXC_MATCH(exc, type) mp_obj_exception_match(exc, type)
  1065. #define GENERATOR_EXIT_IF_NEEDED(t) if (t != MP_OBJ_NULL && EXC_MATCH(t, MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) { RAISE(t); }
  1066. mp_vm_return_kind_t ret_kind;
  1067. mp_obj_t send_value = POP();
  1068. mp_obj_t t_exc = MP_OBJ_NULL;
  1069. mp_obj_t ret_value;
  1070. code_state->sp = sp; // Save sp because it's needed if mp_resume raises StopIteration
  1071. if (inject_exc != MP_OBJ_NULL) {
  1072. t_exc = inject_exc;
  1073. inject_exc = MP_OBJ_NULL;
  1074. ret_kind = mp_resume(TOP(), MP_OBJ_NULL, t_exc, &ret_value);
  1075. } else {
  1076. ret_kind = mp_resume(TOP(), send_value, MP_OBJ_NULL, &ret_value);
  1077. }
  1078. if (ret_kind == MP_VM_RETURN_YIELD) {
  1079. ip--;
  1080. PUSH(ret_value);
  1081. goto yield;
  1082. } else if (ret_kind == MP_VM_RETURN_NORMAL) {
  1083. // Pop exhausted gen
  1084. sp--;
  1085. if (ret_value == MP_OBJ_STOP_ITERATION) {
  1086. // Optimize StopIteration
  1087. // TODO: get StopIteration's value
  1088. PUSH(mp_const_none);
  1089. } else {
  1090. PUSH(ret_value);
  1091. }
  1092. // If we injected GeneratorExit downstream, then even
  1093. // if it was swallowed, we re-raise GeneratorExit
  1094. GENERATOR_EXIT_IF_NEEDED(t_exc);
  1095. DISPATCH();
  1096. } else {
  1097. assert(ret_kind == MP_VM_RETURN_EXCEPTION);
  1098. // Pop exhausted gen
  1099. sp--;
  1100. if (EXC_MATCH(ret_value, MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
  1101. PUSH(mp_obj_exception_get_value(ret_value));
  1102. // If we injected GeneratorExit downstream, then even
  1103. // if it was swallowed, we re-raise GeneratorExit
  1104. GENERATOR_EXIT_IF_NEEDED(t_exc);
  1105. DISPATCH();
  1106. } else {
  1107. RAISE(ret_value);
  1108. }
  1109. }
  1110. }
  1111. ENTRY(MP_BC_IMPORT_NAME): {
  1112. MARK_EXC_IP_SELECTIVE();
  1113. DECODE_QSTR;
  1114. mp_obj_t obj = POP();
  1115. SET_TOP(mp_import_name(qst, obj, TOP()));
  1116. DISPATCH();
  1117. }
  1118. ENTRY(MP_BC_IMPORT_FROM): {
  1119. MARK_EXC_IP_SELECTIVE();
  1120. DECODE_QSTR;
  1121. mp_obj_t obj = mp_import_from(TOP(), qst);
  1122. PUSH(obj);
  1123. DISPATCH();
  1124. }
  1125. ENTRY(MP_BC_IMPORT_STAR):
  1126. MARK_EXC_IP_SELECTIVE();
  1127. mp_import_all(POP());
  1128. DISPATCH();
  1129. #if MICROPY_OPT_COMPUTED_GOTO
  1130. ENTRY(MP_BC_LOAD_CONST_SMALL_INT_MULTI):
  1131. PUSH(MP_OBJ_NEW_SMALL_INT((mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16));
  1132. DISPATCH();
  1133. ENTRY(MP_BC_LOAD_FAST_MULTI):
  1134. obj_shared = fastn[MP_BC_LOAD_FAST_MULTI - (mp_int_t)ip[-1]];
  1135. goto load_check;
  1136. ENTRY(MP_BC_STORE_FAST_MULTI):
  1137. fastn[MP_BC_STORE_FAST_MULTI - (mp_int_t)ip[-1]] = POP();
  1138. DISPATCH();
  1139. ENTRY(MP_BC_UNARY_OP_MULTI):
  1140. MARK_EXC_IP_SELECTIVE();
  1141. SET_TOP(mp_unary_op(ip[-1] - MP_BC_UNARY_OP_MULTI, TOP()));
  1142. DISPATCH();
  1143. ENTRY(MP_BC_BINARY_OP_MULTI): {
  1144. MARK_EXC_IP_SELECTIVE();
  1145. mp_obj_t rhs = POP();
  1146. mp_obj_t lhs = TOP();
  1147. SET_TOP(mp_binary_op(ip[-1] - MP_BC_BINARY_OP_MULTI, lhs, rhs));
  1148. DISPATCH();
  1149. }
  1150. ENTRY_DEFAULT:
  1151. MARK_EXC_IP_SELECTIVE();
  1152. #else
  1153. ENTRY_DEFAULT:
  1154. if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
  1155. PUSH(MP_OBJ_NEW_SMALL_INT((mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16));
  1156. DISPATCH();
  1157. } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
  1158. obj_shared = fastn[MP_BC_LOAD_FAST_MULTI - (mp_int_t)ip[-1]];
  1159. goto load_check;
  1160. } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
  1161. fastn[MP_BC_STORE_FAST_MULTI - (mp_int_t)ip[-1]] = POP();
  1162. DISPATCH();
  1163. } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 7) {
  1164. SET_TOP(mp_unary_op(ip[-1] - MP_BC_UNARY_OP_MULTI, TOP()));
  1165. DISPATCH();
  1166. } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) {
  1167. mp_obj_t rhs = POP();
  1168. mp_obj_t lhs = TOP();
  1169. SET_TOP(mp_binary_op(ip[-1] - MP_BC_BINARY_OP_MULTI, lhs, rhs));
  1170. DISPATCH();
  1171. } else
  1172. #endif
  1173. {
  1174. mp_obj_t obj = mp_obj_new_exception_msg(&mp_type_NotImplementedError, "byte code not implemented");
  1175. nlr_pop();
  1176. fastn[0] = obj;
  1177. return MP_VM_RETURN_EXCEPTION;
  1178. }
  1179. #if !MICROPY_OPT_COMPUTED_GOTO
  1180. } // switch
  1181. #endif
  1182. pending_exception_check:
  1183. MICROPY_VM_HOOK_LOOP
  1184. #if MICROPY_ENABLE_SCHEDULER
  1185. // This is an inlined variant of mp_handle_pending
  1186. if (MP_STATE_VM(sched_state) == MP_SCHED_PENDING) {
  1187. MARK_EXC_IP_SELECTIVE();
  1188. mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
  1189. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  1190. if (obj != MP_OBJ_NULL) {
  1191. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  1192. if (!mp_sched_num_pending()) {
  1193. MP_STATE_VM(sched_state) = MP_SCHED_IDLE;
  1194. }
  1195. MICROPY_END_ATOMIC_SECTION(atomic_state);
  1196. RAISE(obj);
  1197. }
  1198. mp_handle_pending_tail(atomic_state);
  1199. }
  1200. #else
  1201. // This is an inlined variant of mp_handle_pending
  1202. if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
  1203. MARK_EXC_IP_SELECTIVE();
  1204. mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
  1205. MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
  1206. RAISE(obj);
  1207. }
  1208. #endif
  1209. #if MICROPY_PY_THREAD_GIL
  1210. #if MICROPY_PY_THREAD_GIL_VM_DIVISOR
  1211. if (--gil_divisor == 0) {
  1212. gil_divisor = MICROPY_PY_THREAD_GIL_VM_DIVISOR;
  1213. #else
  1214. {
  1215. #endif
  1216. #if MICROPY_ENABLE_SCHEDULER
  1217. // can only switch threads if the scheduler is unlocked
  1218. if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE)
  1219. #endif
  1220. {
  1221. MP_THREAD_GIL_EXIT();
  1222. MP_THREAD_GIL_ENTER();
  1223. }
  1224. }
  1225. #endif
  1226. } // for loop
  1227. } else {
  1228. exception_handler:
  1229. // exception occurred
  1230. #if MICROPY_PY_SYS_EXC_INFO
  1231. MP_STATE_VM(cur_exception) = nlr.ret_val;
  1232. #endif
  1233. #if SELECTIVE_EXC_IP
  1234. // with selective ip, we store the ip 1 byte past the opcode, so move ptr back
  1235. code_state->ip -= 1;
  1236. #endif
  1237. if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t*)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
  1238. if (code_state->ip) {
  1239. // check if it's a StopIteration within a for block
  1240. if (*code_state->ip == MP_BC_FOR_ITER) {
  1241. const byte *ip = code_state->ip + 1;
  1242. DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
  1243. code_state->ip = ip + ulab; // jump to after for-block
  1244. code_state->sp -= MP_OBJ_ITER_BUF_NSLOTS; // pop the exhausted iterator
  1245. goto outer_dispatch_loop; // continue with dispatch loop
  1246. } else if (*code_state->ip == MP_BC_YIELD_FROM) {
  1247. // StopIteration inside yield from call means return a value of
  1248. // yield from, so inject exception's value as yield from's result
  1249. // (Instead of stack pop then push we just replace exhausted gen with value)
  1250. *code_state->sp = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(nlr.ret_val));
  1251. code_state->ip++; // yield from is over, move to next instruction
  1252. goto outer_dispatch_loop; // continue with dispatch loop
  1253. }
  1254. }
  1255. }
  1256. #if MICROPY_STACKLESS
  1257. unwind_loop:
  1258. #endif
  1259. // set file and line number that the exception occurred at
  1260. // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
  1261. // But consider how to handle nested exceptions.
  1262. if (nlr.ret_val != &mp_const_GeneratorExit_obj) {
  1263. const byte *ip = code_state->fun_bc->bytecode;
  1264. ip = mp_decode_uint_skip(ip); // skip n_state
  1265. ip = mp_decode_uint_skip(ip); // skip n_exc_stack
  1266. ip++; // skip scope_params
  1267. ip++; // skip n_pos_args
  1268. ip++; // skip n_kwonly_args
  1269. ip++; // skip n_def_pos_args
  1270. size_t bc = code_state->ip - ip;
  1271. size_t code_info_size = mp_decode_uint_value(ip);
  1272. ip = mp_decode_uint_skip(ip); // skip code_info_size
  1273. bc -= code_info_size;
  1274. #if MICROPY_PERSISTENT_CODE
  1275. qstr block_name = ip[0] | (ip[1] << 8);
  1276. qstr source_file = ip[2] | (ip[3] << 8);
  1277. ip += 4;
  1278. #else
  1279. qstr block_name = mp_decode_uint_value(ip);
  1280. ip = mp_decode_uint_skip(ip);
  1281. qstr source_file = mp_decode_uint_value(ip);
  1282. ip = mp_decode_uint_skip(ip);
  1283. #endif
  1284. size_t source_line = 1;
  1285. size_t c;
  1286. while ((c = *ip)) {
  1287. size_t b, l;
  1288. if ((c & 0x80) == 0) {
  1289. // 0b0LLBBBBB encoding
  1290. b = c & 0x1f;
  1291. l = c >> 5;
  1292. ip += 1;
  1293. } else {
  1294. // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
  1295. b = c & 0xf;
  1296. l = ((c << 4) & 0x700) | ip[1];
  1297. ip += 2;
  1298. }
  1299. if (bc >= b) {
  1300. bc -= b;
  1301. source_line += l;
  1302. } else {
  1303. // found source line corresponding to bytecode offset
  1304. break;
  1305. }
  1306. }
  1307. mp_obj_exception_add_traceback(MP_OBJ_FROM_PTR(nlr.ret_val), source_file, source_line, block_name);
  1308. }
  1309. while (currently_in_except_block) {
  1310. // nested exception
  1311. assert(exc_sp >= exc_stack);
  1312. // TODO make a proper message for nested exception
  1313. // at the moment we are just raising the very last exception (the one that caused the nested exception)
  1314. // move up to previous exception handler
  1315. POP_EXC_BLOCK();
  1316. }
  1317. if (exc_sp >= exc_stack) {
  1318. // set flag to indicate that we are now handling an exception
  1319. currently_in_except_block = 1;
  1320. // catch exception and pass to byte code
  1321. code_state->ip = exc_sp->handler;
  1322. mp_obj_t *sp = MP_TAGPTR_PTR(exc_sp->val_sp);
  1323. // save this exception in the stack so it can be used in a reraise, if needed
  1324. exc_sp->prev_exc = nlr.ret_val;
  1325. // push exception object so it can be handled by bytecode
  1326. PUSH(MP_OBJ_FROM_PTR(nlr.ret_val));
  1327. code_state->sp = sp;
  1328. #if MICROPY_STACKLESS
  1329. } else if (code_state->prev != NULL) {
  1330. mp_globals_set(code_state->old_globals);
  1331. mp_code_state_t *new_code_state = code_state->prev;
  1332. #if MICROPY_ENABLE_PYSTACK
  1333. // Free code_state, and args allocated by mp_call_prepare_args_n_kw_var
  1334. // (The latter is implicitly freed when using pystack due to its LIFO nature.)
  1335. // The sizeof in the following statement does not include the size of the variable
  1336. // part of the struct. This arg is anyway not used if pystack is enabled.
  1337. mp_nonlocal_free(code_state, sizeof(mp_code_state_t));
  1338. #endif
  1339. code_state = new_code_state;
  1340. size_t n_state = mp_decode_uint_value(code_state->fun_bc->bytecode);
  1341. fastn = &code_state->state[n_state - 1];
  1342. exc_stack = (mp_exc_stack_t*)(code_state->state + n_state);
  1343. // variables that are visible to the exception handler (declared volatile)
  1344. currently_in_except_block = MP_TAGPTR_TAG0(code_state->exc_sp); // 0 or 1, to detect nested exceptions
  1345. exc_sp = MP_TAGPTR_PTR(code_state->exc_sp); // stack grows up, exc_sp points to top of stack
  1346. goto unwind_loop;
  1347. #endif
  1348. } else {
  1349. // propagate exception to higher level
  1350. // TODO what to do about ip and sp? they don't really make sense at this point
  1351. fastn[0] = MP_OBJ_FROM_PTR(nlr.ret_val); // must put exception here because sp is invalid
  1352. return MP_VM_RETURN_EXCEPTION;
  1353. }
  1354. }
  1355. }
  1356. }