blob: f9aa0f9b3c72aa9017beca86296e5d56a5b7c4b4 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
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
Damien429d7192013-10-04 19:53:11 +010028#include <stdio.h>
Paul Sokolovsky55ca0752014-03-30 17:35:53 +030029#include <string.h>
Damien429d7192013-10-04 19:53:11 +010030#include <assert.h>
31
Damien Georgeb4b10fd2015-01-01 23:30:53 +000032#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000033#include "py/nlr.h"
34#include "py/emitglue.h"
Damien George7ee91cf2015-01-06 12:51:39 +000035#include "py/objtype.h"
Damien George51dfcb42015-01-01 20:27:54 +000036#include "py/runtime.h"
37#include "py/bc0.h"
38#include "py/bc.h"
Damien429d7192013-10-04 19:53:11 +010039
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +030040#if 0
Damien George7764f162014-12-12 17:18:56 +000041#define TRACE(ip) printf("sp=" INT_FMT " ", sp - code_state->sp); mp_bytecode_print2(ip, 1);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +030042#else
43#define TRACE(ip)
44#endif
Damien Georgee90be0d2014-04-10 16:21:34 +000045
Paul Sokolovsky85193422014-01-31 19:45:15 +020046// Value stack grows up (this makes it incompatible with native C stack, but
47// makes sure that arguments to functions are in natural order arg1..argN
48// (Python semantics mandates left-to-right evaluation order, including for
49// function arguments). Stack pointer is pre-incremented and points at the
50// top element.
51// Exception stack also grows up, top element is also pointed at.
52
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020053// Exception stack unwind reasons (WHY_* in CPython-speak)
Damien Georgecbddb272014-02-01 20:08:18 +000054// TODO perhaps compress this to RETURN=0, JUMP>0, with number of unwinds
55// left to do encoded in the JUMP number
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020056typedef enum {
57 UNWIND_RETURN = 1,
Damien Georgecbddb272014-02-01 20:08:18 +000058 UNWIND_JUMP,
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020059} mp_unwind_reason_t;
60
Damien Georgecd97a432014-12-02 19:25:10 +000061#define DECODE_UINT \
62 mp_uint_t unum = 0; \
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020063 do { \
64 unum = (unum << 7) + (*ip & 0x7f); \
Damien Georgecd97a432014-12-02 19:25:10 +000065 } while ((*ip++ & 0x80) != 0)
66#define DECODE_ULABEL mp_uint_t ulab = (ip[0] | (ip[1] << 8)); ip += 2
67#define DECODE_SLABEL mp_uint_t slab = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2
Damien Georgec8e9c0d2015-11-02 17:27:18 +000068
69#if MICROPY_PERSISTENT_CODE
70
71#define DECODE_QSTR \
72 qstr qst = ip[0] | ip[1] << 8; \
73 ip += 2;
74#define DECODE_PTR \
75 DECODE_UINT; \
76 void *ptr = (void*)code_state->const_table[unum]
77
78#else
79
Damien Georged8675542014-05-25 22:58:04 +010080#define DECODE_QSTR qstr qst = 0; \
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020081 do { \
82 qst = (qst << 7) + (*ip & 0x7f); \
Damien Georged8675542014-05-25 22:58:04 +010083 } while ((*ip++ & 0x80) != 0)
Damien Georgecd97a432014-12-02 19:25:10 +000084#define DECODE_PTR \
Damien George40f3c022014-07-03 13:25:24 +010085 ip = (byte*)(((mp_uint_t)ip + sizeof(mp_uint_t) - 1) & (~(sizeof(mp_uint_t) - 1))); /* align ip */ \
Damien Georgecd97a432014-12-02 19:25:10 +000086 void *ptr = (void*)*(mp_uint_t*)ip; \
87 ip += sizeof(mp_uint_t)
Damien Georgec8e9c0d2015-11-02 17:27:18 +000088
89#endif
90
Damien George20006db2014-01-18 14:10:48 +000091#define PUSH(val) *++sp = (val)
92#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000093#define TOP() (*sp)
94#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010095
Paul Sokolovskyae2c81f2015-04-26 01:20:49 +030096#if MICROPY_PY_SYS_EXC_INFO
97#define CLEAR_SYS_EXC_INFO() MP_STATE_VM(cur_exception) = MP_OBJ_NULL;
98#else
99#define CLEAR_SYS_EXC_INFO()
100#endif
101
Damien George74eb44c2014-12-22 12:49:57 +0000102#define PUSH_EXC_BLOCK(with_or_finally) do { \
Paul Sokolovsky4fff26a2014-03-29 02:49:07 +0200103 DECODE_ULABEL; /* except labels are always forward */ \
104 ++exc_sp; \
Damien Georgecd97a432014-12-02 19:25:10 +0000105 exc_sp->handler = ip + ulab; \
Damien George74eb44c2014-12-22 12:49:57 +0000106 exc_sp->val_sp = MP_TAGPTR_MAKE(sp, ((with_or_finally) << 1) | currently_in_except_block); \
Damien Georged7592a12014-03-30 00:54:48 +0000107 exc_sp->prev_exc = MP_OBJ_NULL; \
Damien Georgecd97a432014-12-02 19:25:10 +0000108 currently_in_except_block = 0; /* in a try block now */ \
109} while (0)
Paul Sokolovsky4fff26a2014-03-29 02:49:07 +0200110
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200111#define POP_EXC_BLOCK() \
Damien George74eb44c2014-12-22 12:49:57 +0000112 currently_in_except_block = MP_TAGPTR_TAG0(exc_sp->val_sp); /* restore previous state */ \
Paul Sokolovskyae2c81f2015-04-26 01:20:49 +0300113 exc_sp--; /* pop back to previous exception handler */ \
114 CLEAR_SYS_EXC_INFO() /* just clear sys.exc_info(), not compliant, but it shouldn't be used in 1st place */
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200115
Damien George20006db2014-01-18 14:10:48 +0000116// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
117// sp points to bottom of stack which grows up
Damien Georgec8f78bc2014-02-15 22:55:00 +0000118// returns:
119// MP_VM_RETURN_NORMAL, sp valid, return value in *sp
120// MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
121// MP_VM_RETURN_EXCEPTION, exception in fastn[0]
Damien Georgeaabd83e2014-06-07 14:16:08 +0100122mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_obj_t inject_exc) {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200123#define SELECTIVE_EXC_IP (0)
124#if SELECTIVE_EXC_IP
Damien Georgef89d6592014-12-29 00:29:59 +0000125#define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip; } /* stores ip 1 byte past last opcode */
Paul Sokolovsky74957502014-12-28 07:17:43 +0200126#define MARK_EXC_IP_GLOBAL()
127#else
128#define MARK_EXC_IP_SELECTIVE()
Damien Georgef89d6592014-12-29 00:29:59 +0000129#define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; } /* stores ip pointing to last opcode */
Paul Sokolovsky74957502014-12-28 07:17:43 +0200130#endif
Damien George58ebde42014-05-21 20:32:59 +0100131#if MICROPY_OPT_COMPUTED_GOTO
Damien George51dfcb42015-01-01 20:27:54 +0000132 #include "py/vmentrytable.h"
AZ Huang9309d992014-04-15 15:57:01 +0800133 #define DISPATCH() do { \
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300134 TRACE(ip); \
Paul Sokolovsky74957502014-12-28 07:17:43 +0200135 MARK_EXC_IP_GLOBAL(); \
Damien Georgedb128912014-04-27 18:19:06 +0100136 goto *entry_table[*ip++]; \
Damien George4dea9222015-04-09 15:29:54 +0000137 } while (0)
Damien George124df6f2014-10-25 18:19:55 +0100138 #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
AZ Huang9309d992014-04-15 15:57:01 +0800139 #define ENTRY(op) entry_##op
140 #define ENTRY_DEFAULT entry_default
AZ Huangb1f692e2014-04-14 23:22:44 +0800141#else
AZ Huang9309d992014-04-15 15:57:01 +0800142 #define DISPATCH() break
Damien George124df6f2014-10-25 18:19:55 +0100143 #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
AZ Huang9309d992014-04-15 15:57:01 +0800144 #define ENTRY(op) case op
145 #define ENTRY_DEFAULT default
AZ Huangb1f692e2014-04-14 23:22:44 +0800146#endif
147
Damien George66ae8c92014-04-17 16:50:23 +0100148 // nlr_raise needs to be implemented as a goto, so that the C compiler's flow analyser
149 // sees that it's possible for us to jump from the dispatch loop to the exception
150 // handler. Without this, the code may have a different stack layout in the dispatch
151 // loop and the exception handler, leading to very obscure bugs.
Damien George4dea9222015-04-09 15:29:54 +0000152 #define RAISE(o) do { nlr_pop(); nlr.ret_val = o; goto exception_handler; } while (0)
Damien429d7192013-10-04 19:53:11 +0100153
Paul Sokolovsky20397572015-03-28 01:14:44 +0200154#if MICROPY_STACKLESS
155run_code_state: ;
156#endif
Damien Georgeaabd83e2014-06-07 14:16:08 +0100157 // Pointers which are constant for particular invocation of mp_execute_bytecode()
stijn36cc84a2015-04-09 11:34:08 +0200158 mp_obj_t * /*const*/ fastn = &code_state->state[code_state->n_state - 1];
159 mp_exc_stack_t * /*const*/ exc_stack = (mp_exc_stack_t*)(code_state->state + code_state->n_state);
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300160
Damien George66ae8c92014-04-17 16:50:23 +0100161 // variables that are visible to the exception handler (declared volatile)
Damien George74eb44c2014-12-22 12:49:57 +0000162 volatile bool currently_in_except_block = MP_TAGPTR_TAG0(code_state->exc_sp); // 0 or 1, to detect nested exceptions
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300163 mp_exc_stack_t *volatile exc_sp = MP_TAGPTR_PTR(code_state->exc_sp); // stack grows up, exc_sp points to top of stack
Damienc9f91972013-10-15 23:46:01 +0100164
Damience89a212013-10-15 22:25:17 +0100165 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +0100166 for (;;) {
Damien George66ae8c92014-04-17 16:50:23 +0100167 nlr_buf_t nlr;
Damien George9e6e9352014-03-26 18:37:06 +0000168outer_dispatch_loop:
Damience89a212013-10-15 22:25:17 +0100169 if (nlr_push(&nlr) == 0) {
Damien George66ae8c92014-04-17 16:50:23 +0100170 // local variables that are not visible to the exception handler
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300171 const byte *ip = code_state->ip;
172 mp_obj_t *sp = code_state->sp;
Damien Georged8675542014-05-25 22:58:04 +0100173 mp_obj_t obj_shared;
Damien George66ae8c92014-04-17 16:50:23 +0100174
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200175 // If we have exception to inject, now that we finish setting up
176 // execution context, raise it. This works as if RAISE_VARARGS
177 // bytecode was executed.
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200178 // Injecting exc into yield from generator is a special case,
179 // handled by MP_BC_YIELD_FROM itself
180 if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) {
Damien Georged8675542014-05-25 22:58:04 +0100181 mp_obj_t exc = inject_exc;
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200182 inject_exc = MP_OBJ_NULL;
Damien Georged8675542014-05-25 22:58:04 +0100183 exc = mp_make_raise_obj(exc);
184 RAISE(exc);
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200185 }
Damien George66ae8c92014-04-17 16:50:23 +0100186
Damience89a212013-10-15 22:25:17 +0100187 // loop to execute byte code
188 for (;;) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200189dispatch_loop:
Damien George58ebde42014-05-21 20:32:59 +0100190#if MICROPY_OPT_COMPUTED_GOTO
AZ Huangb1f692e2014-04-14 23:22:44 +0800191 DISPATCH();
192#else
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300193 TRACE(ip);
Paul Sokolovsky74957502014-12-28 07:17:43 +0200194 MARK_EXC_IP_GLOBAL();
Damien Georgedb128912014-04-27 18:19:06 +0100195 switch (*ip++) {
AZ Huangb1f692e2014-04-14 23:22:44 +0800196#endif
Damien429d7192013-10-04 19:53:11 +0100197
AZ Huangb1f692e2014-04-14 23:22:44 +0800198 ENTRY(MP_BC_LOAD_CONST_FALSE):
199 PUSH(mp_const_false);
200 DISPATCH();
Damien429d7192013-10-04 19:53:11 +0100201
AZ Huangb1f692e2014-04-14 23:22:44 +0800202 ENTRY(MP_BC_LOAD_CONST_NONE):
203 PUSH(mp_const_none);
204 DISPATCH();
Damien429d7192013-10-04 19:53:11 +0100205
AZ Huangb1f692e2014-04-14 23:22:44 +0800206 ENTRY(MP_BC_LOAD_CONST_TRUE):
207 PUSH(mp_const_true);
208 DISPATCH();
Damien Georgee9906ac2014-01-04 18:44:46 +0000209
AZ Huangb1f692e2014-04-14 23:22:44 +0800210 ENTRY(MP_BC_LOAD_CONST_SMALL_INT): {
Damien George40f3c022014-07-03 13:25:24 +0100211 mp_int_t num = 0;
AZ Huangb1f692e2014-04-14 23:22:44 +0800212 if ((ip[0] & 0x40) != 0) {
213 // Number is negative
214 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200215 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800216 do {
217 num = (num << 7) | (*ip & 0x7f);
218 } while ((*ip++ & 0x80) != 0);
219 PUSH(MP_OBJ_NEW_SMALL_INT(num));
220 DISPATCH();
221 }
Damience89a212013-10-15 22:25:17 +0100222
Damien Georged8675542014-05-25 22:58:04 +0100223 ENTRY(MP_BC_LOAD_CONST_STRING): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800224 DECODE_QSTR;
Damien Georgeed570e42015-06-25 13:58:41 +0000225 PUSH(MP_OBJ_NEW_QSTR(qst));
AZ Huangb1f692e2014-04-14 23:22:44 +0800226 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100227 }
Damience89a212013-10-15 22:25:17 +0100228
Damien Georgedab13852015-01-13 15:55:54 +0000229 ENTRY(MP_BC_LOAD_CONST_OBJ): {
230 DECODE_PTR;
231 PUSH(ptr);
232 DISPATCH();
233 }
234
AZ Huangb1f692e2014-04-14 23:22:44 +0800235 ENTRY(MP_BC_LOAD_NULL):
236 PUSH(MP_OBJ_NULL);
237 DISPATCH();
Damien George523b5752014-03-31 11:59:23 +0100238
Damien Georgecd97a432014-12-02 19:25:10 +0000239 ENTRY(MP_BC_LOAD_FAST_N): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800240 DECODE_UINT;
Damien Georged8675542014-05-25 22:58:04 +0100241 obj_shared = fastn[-unum];
AZ Huangb1f692e2014-04-14 23:22:44 +0800242 load_check:
Damien Georged8675542014-05-25 22:58:04 +0100243 if (obj_shared == MP_OBJ_NULL) {
244 local_name_error: {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200245 MARK_EXC_IP_SELECTIVE();
Damien Georged8675542014-05-25 22:58:04 +0100246 mp_obj_t obj = mp_obj_new_exception_msg(&mp_type_NameError, "local variable referenced before assignment");
247 RAISE(obj);
248 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800249 }
Damien Georged8675542014-05-25 22:58:04 +0100250 PUSH(obj_shared);
AZ Huangb1f692e2014-04-14 23:22:44 +0800251 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000252 }
Damien George2bf7c092014-04-09 15:26:46 +0100253
Damien Georgecd97a432014-12-02 19:25:10 +0000254 ENTRY(MP_BC_LOAD_DEREF): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800255 DECODE_UINT;
Damien Georged8675542014-05-25 22:58:04 +0100256 obj_shared = mp_obj_cell_get(fastn[-unum]);
AZ Huangb1f692e2014-04-14 23:22:44 +0800257 goto load_check;
Damien Georgecd97a432014-12-02 19:25:10 +0000258 }
Damien9ecbcff2013-12-11 00:41:43 +0000259
Damien George7ee91cf2015-01-06 12:51:39 +0000260 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100261 ENTRY(MP_BC_LOAD_NAME): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200262 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800263 DECODE_QSTR;
264 PUSH(mp_load_name(qst));
265 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100266 }
Damien George7ee91cf2015-01-06 12:51:39 +0000267 #else
268 ENTRY(MP_BC_LOAD_NAME): {
269 MARK_EXC_IP_SELECTIVE();
270 DECODE_QSTR;
271 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
272 mp_uint_t x = *ip;
273 if (x < MP_STATE_CTX(dict_locals)->map.alloc && MP_STATE_CTX(dict_locals)->map.table[x].key == key) {
274 PUSH(MP_STATE_CTX(dict_locals)->map.table[x].value);
275 } else {
276 mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_locals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
277 if (elem != NULL) {
278 *(byte*)ip = (elem - &MP_STATE_CTX(dict_locals)->map.table[0]) & 0xff;
279 PUSH(elem->value);
280 } else {
281 PUSH(mp_load_name(MP_OBJ_QSTR_VALUE(key)));
282 }
283 }
284 ip++;
285 DISPATCH();
286 }
287 #endif
Damience89a212013-10-15 22:25:17 +0100288
Damien George7ee91cf2015-01-06 12:51:39 +0000289 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100290 ENTRY(MP_BC_LOAD_GLOBAL): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200291 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800292 DECODE_QSTR;
293 PUSH(mp_load_global(qst));
294 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100295 }
Damien George7ee91cf2015-01-06 12:51:39 +0000296 #else
297 ENTRY(MP_BC_LOAD_GLOBAL): {
298 MARK_EXC_IP_SELECTIVE();
299 DECODE_QSTR;
300 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
301 mp_uint_t x = *ip;
302 if (x < MP_STATE_CTX(dict_globals)->map.alloc && MP_STATE_CTX(dict_globals)->map.table[x].key == key) {
303 PUSH(MP_STATE_CTX(dict_globals)->map.table[x].value);
304 } else {
305 mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_globals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
306 if (elem != NULL) {
307 *(byte*)ip = (elem - &MP_STATE_CTX(dict_globals)->map.table[0]) & 0xff;
308 PUSH(elem->value);
309 } else {
310 PUSH(mp_load_global(MP_OBJ_QSTR_VALUE(key)));
311 }
312 }
313 ip++;
314 DISPATCH();
315 }
316 #endif
Damience89a212013-10-15 22:25:17 +0100317
Damien George7ee91cf2015-01-06 12:51:39 +0000318 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100319 ENTRY(MP_BC_LOAD_ATTR): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200320 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800321 DECODE_QSTR;
322 SET_TOP(mp_load_attr(TOP(), qst));
323 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100324 }
Damien George7ee91cf2015-01-06 12:51:39 +0000325 #else
326 ENTRY(MP_BC_LOAD_ATTR): {
327 MARK_EXC_IP_SELECTIVE();
328 DECODE_QSTR;
329 mp_obj_t top = TOP();
Damien Georgeb1bbe962015-04-01 14:10:50 +0000330 if (mp_obj_get_type(top)->attr == mp_obj_instance_attr) {
Damien George7ee91cf2015-01-06 12:51:39 +0000331 mp_obj_instance_t *self = top;
332 mp_uint_t x = *ip;
333 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
334 mp_map_elem_t *elem;
335 if (x < self->members.alloc && self->members.table[x].key == key) {
336 elem = &self->members.table[x];
337 } else {
338 elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
339 if (elem != NULL) {
340 *(byte*)ip = elem - &self->members.table[0];
341 } else {
342 goto load_attr_cache_fail;
343 }
344 }
345 SET_TOP(elem->value);
346 ip++;
347 DISPATCH();
348 }
349 load_attr_cache_fail:
350 SET_TOP(mp_load_attr(top, qst));
351 ip++;
352 DISPATCH();
353 }
354 #endif
Damience89a212013-10-15 22:25:17 +0100355
Damien Georged8675542014-05-25 22:58:04 +0100356 ENTRY(MP_BC_LOAD_METHOD): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200357 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800358 DECODE_QSTR;
359 mp_load_method(*sp, qst, sp);
360 sp += 1;
361 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100362 }
Damience89a212013-10-15 22:25:17 +0100363
AZ Huangb1f692e2014-04-14 23:22:44 +0800364 ENTRY(MP_BC_LOAD_BUILD_CLASS):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200365 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800366 PUSH(mp_load_build_class());
367 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100368
Damien Georged8675542014-05-25 22:58:04 +0100369 ENTRY(MP_BC_LOAD_SUBSCR): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200370 MARK_EXC_IP_SELECTIVE();
Damien Georged8675542014-05-25 22:58:04 +0100371 mp_obj_t index = POP();
372 SET_TOP(mp_obj_subscr(TOP(), index, MP_OBJ_SENTINEL));
Damien George729f7b42014-04-17 22:10:53 +0100373 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100374 }
Damien George729f7b42014-04-17 22:10:53 +0100375
Damien Georgecd97a432014-12-02 19:25:10 +0000376 ENTRY(MP_BC_STORE_FAST_N): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800377 DECODE_UINT;
378 fastn[-unum] = POP();
379 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000380 }
Damience89a212013-10-15 22:25:17 +0100381
Damien Georgecd97a432014-12-02 19:25:10 +0000382 ENTRY(MP_BC_STORE_DEREF): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800383 DECODE_UINT;
384 mp_obj_cell_set(fastn[-unum], POP());
385 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000386 }
Damien9ecbcff2013-12-11 00:41:43 +0000387
Damien Georged8675542014-05-25 22:58:04 +0100388 ENTRY(MP_BC_STORE_NAME): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200389 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800390 DECODE_QSTR;
391 mp_store_name(qst, POP());
392 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100393 }
Damience89a212013-10-15 22:25:17 +0100394
Damien Georged8675542014-05-25 22:58:04 +0100395 ENTRY(MP_BC_STORE_GLOBAL): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200396 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800397 DECODE_QSTR;
398 mp_store_global(qst, POP());
399 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100400 }
Damien6addc892013-11-04 23:04:50 +0000401
Damien George7ee91cf2015-01-06 12:51:39 +0000402 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100403 ENTRY(MP_BC_STORE_ATTR): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200404 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800405 DECODE_QSTR;
406 mp_store_attr(sp[0], qst, sp[-1]);
407 sp -= 2;
408 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100409 }
Damien George7ee91cf2015-01-06 12:51:39 +0000410 #else
stijn28fa84b2015-02-14 18:43:54 +0100411 // This caching code works with MICROPY_PY_BUILTINS_PROPERTY and/or
412 // MICROPY_PY_DESCRIPTORS enabled because if the attr exists in
413 // self->members then it can't be a property or have descriptors. A
Damien George7ee91cf2015-01-06 12:51:39 +0000414 // consequence of this is that we can't use MP_MAP_LOOKUP_ADD_IF_NOT_FOUND
415 // in the fast-path below, because that store could override a property.
416 ENTRY(MP_BC_STORE_ATTR): {
417 MARK_EXC_IP_SELECTIVE();
418 DECODE_QSTR;
419 mp_obj_t top = TOP();
Damien Georgeb1bbe962015-04-01 14:10:50 +0000420 if (mp_obj_get_type(top)->attr == mp_obj_instance_attr && sp[-1] != MP_OBJ_NULL) {
Damien George7ee91cf2015-01-06 12:51:39 +0000421 mp_obj_instance_t *self = top;
422 mp_uint_t x = *ip;
423 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
424 mp_map_elem_t *elem;
425 if (x < self->members.alloc && self->members.table[x].key == key) {
426 elem = &self->members.table[x];
427 } else {
428 elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
429 if (elem != NULL) {
430 *(byte*)ip = elem - &self->members.table[0];
431 } else {
432 goto store_attr_cache_fail;
433 }
434 }
435 elem->value = sp[-1];
436 sp -= 2;
437 ip++;
438 DISPATCH();
439 }
440 store_attr_cache_fail:
441 mp_store_attr(sp[0], qst, sp[-1]);
442 sp -= 2;
443 ip++;
444 DISPATCH();
445 }
446 #endif
Damience89a212013-10-15 22:25:17 +0100447
AZ Huangb1f692e2014-04-14 23:22:44 +0800448 ENTRY(MP_BC_STORE_SUBSCR):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200449 MARK_EXC_IP_SELECTIVE();
Damien George729f7b42014-04-17 22:10:53 +0100450 mp_obj_subscr(sp[-1], sp[0], sp[-2]);
AZ Huangb1f692e2014-04-14 23:22:44 +0800451 sp -= 3;
452 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100453
Damien Georgecd97a432014-12-02 19:25:10 +0000454 ENTRY(MP_BC_DELETE_FAST): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200455 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800456 DECODE_UINT;
457 if (fastn[-unum] == MP_OBJ_NULL) {
458 goto local_name_error;
459 }
460 fastn[-unum] = MP_OBJ_NULL;
461 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000462 }
Damien George2bf7c092014-04-09 15:26:46 +0100463
Damien Georgecd97a432014-12-02 19:25:10 +0000464 ENTRY(MP_BC_DELETE_DEREF): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200465 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800466 DECODE_UINT;
467 if (mp_obj_cell_get(fastn[-unum]) == MP_OBJ_NULL) {
468 goto local_name_error;
469 }
470 mp_obj_cell_set(fastn[-unum], MP_OBJ_NULL);
471 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000472 }
Damien George2bf7c092014-04-09 15:26:46 +0100473
Damien Georged8675542014-05-25 22:58:04 +0100474 ENTRY(MP_BC_DELETE_NAME): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200475 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800476 DECODE_QSTR;
477 mp_delete_name(qst);
478 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100479 }
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200480
Damien Georged8675542014-05-25 22:58:04 +0100481 ENTRY(MP_BC_DELETE_GLOBAL): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200482 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800483 DECODE_QSTR;
484 mp_delete_global(qst);
485 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100486 }
Damien George1d24ea52014-04-08 21:11:49 +0100487
Damien Georged8675542014-05-25 22:58:04 +0100488 ENTRY(MP_BC_DUP_TOP): {
489 mp_obj_t top = TOP();
490 PUSH(top);
AZ Huangb1f692e2014-04-14 23:22:44 +0800491 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100492 }
Damience89a212013-10-15 22:25:17 +0100493
AZ Huangb1f692e2014-04-14 23:22:44 +0800494 ENTRY(MP_BC_DUP_TOP_TWO):
495 sp += 2;
496 sp[0] = sp[-2];
497 sp[-1] = sp[-3];
498 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100499
AZ Huangb1f692e2014-04-14 23:22:44 +0800500 ENTRY(MP_BC_POP_TOP):
501 sp -= 1;
502 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100503
Damien Georged8675542014-05-25 22:58:04 +0100504 ENTRY(MP_BC_ROT_TWO): {
505 mp_obj_t top = sp[0];
AZ Huangb1f692e2014-04-14 23:22:44 +0800506 sp[0] = sp[-1];
Damien Georged8675542014-05-25 22:58:04 +0100507 sp[-1] = top;
AZ Huangb1f692e2014-04-14 23:22:44 +0800508 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100509 }
Damien4ebb32f2013-11-02 14:33:10 +0000510
Damien Georged8675542014-05-25 22:58:04 +0100511 ENTRY(MP_BC_ROT_THREE): {
512 mp_obj_t top = sp[0];
AZ Huangb1f692e2014-04-14 23:22:44 +0800513 sp[0] = sp[-1];
514 sp[-1] = sp[-2];
Damien Georged8675542014-05-25 22:58:04 +0100515 sp[-2] = top;
AZ Huangb1f692e2014-04-14 23:22:44 +0800516 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100517 }
Damience89a212013-10-15 22:25:17 +0100518
Damien Georgecd97a432014-12-02 19:25:10 +0000519 ENTRY(MP_BC_JUMP): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800520 DECODE_SLABEL;
Damien Georgecd97a432014-12-02 19:25:10 +0000521 ip += slab;
Damien George124df6f2014-10-25 18:19:55 +0100522 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000523 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800524
Damien Georgecd97a432014-12-02 19:25:10 +0000525 ENTRY(MP_BC_POP_JUMP_IF_TRUE): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800526 DECODE_SLABEL;
527 if (mp_obj_is_true(POP())) {
Damien Georgecd97a432014-12-02 19:25:10 +0000528 ip += slab;
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200529 }
Damien George124df6f2014-10-25 18:19:55 +0100530 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000531 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200532
Damien Georgecd97a432014-12-02 19:25:10 +0000533 ENTRY(MP_BC_POP_JUMP_IF_FALSE): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800534 DECODE_SLABEL;
535 if (!mp_obj_is_true(POP())) {
Damien Georgecd97a432014-12-02 19:25:10 +0000536 ip += slab;
AZ Huangb1f692e2014-04-14 23:22:44 +0800537 }
Damien George124df6f2014-10-25 18:19:55 +0100538 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000539 }
Damien George600ae732014-01-21 23:48:04 +0000540
Damien Georgecd97a432014-12-02 19:25:10 +0000541 ENTRY(MP_BC_JUMP_IF_TRUE_OR_POP): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800542 DECODE_SLABEL;
543 if (mp_obj_is_true(TOP())) {
Damien Georgecd97a432014-12-02 19:25:10 +0000544 ip += slab;
AZ Huangb1f692e2014-04-14 23:22:44 +0800545 } else {
546 sp--;
547 }
Damien George124df6f2014-10-25 18:19:55 +0100548 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000549 }
Damience89a212013-10-15 22:25:17 +0100550
Damien Georgecd97a432014-12-02 19:25:10 +0000551 ENTRY(MP_BC_JUMP_IF_FALSE_OR_POP): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800552 DECODE_SLABEL;
553 if (mp_obj_is_true(TOP())) {
554 sp--;
555 } else {
Damien Georgecd97a432014-12-02 19:25:10 +0000556 ip += slab;
AZ Huangb1f692e2014-04-14 23:22:44 +0800557 }
Damien George124df6f2014-10-25 18:19:55 +0100558 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000559 }
Damience89a212013-10-15 22:25:17 +0100560
Damien Georged8675542014-05-25 22:58:04 +0100561 ENTRY(MP_BC_SETUP_WITH): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200562 MARK_EXC_IP_SELECTIVE();
Damien George8c1d23a2015-04-24 01:52:28 +0100563 // stack: (..., ctx_mgr)
Damien Georged8675542014-05-25 22:58:04 +0100564 mp_obj_t obj = TOP();
Damien George8c1d23a2015-04-24 01:52:28 +0100565 mp_load_method(obj, MP_QSTR___exit__, sp);
566 mp_load_method(obj, MP_QSTR___enter__, sp + 2);
567 mp_obj_t ret = mp_call_method_n_kw(0, 0, sp + 2);
568 sp += 1;
Damien George74eb44c2014-12-22 12:49:57 +0000569 PUSH_EXC_BLOCK(1);
Damien Georged8675542014-05-25 22:58:04 +0100570 PUSH(ret);
Damien George8c1d23a2015-04-24 01:52:28 +0100571 // stack: (..., __exit__, ctx_mgr, as_value)
AZ Huangb1f692e2014-04-14 23:22:44 +0800572 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100573 }
Damience89a212013-10-15 22:25:17 +0100574
AZ Huangb1f692e2014-04-14 23:22:44 +0800575 ENTRY(MP_BC_WITH_CLEANUP): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200576 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800577 // Arriving here, there's "exception control block" on top of stack,
Damien George8c1d23a2015-04-24 01:52:28 +0100578 // and __exit__ method (with self) underneath it. Bytecode calls __exit__,
AZ Huangb1f692e2014-04-14 23:22:44 +0800579 // and "deletes" it off stack, shifting "exception control block"
580 // to its place.
AZ Huangb1f692e2014-04-14 23:22:44 +0800581 if (TOP() == mp_const_none) {
Damien George8c1d23a2015-04-24 01:52:28 +0100582 // stack: (..., __exit__, ctx_mgr, None)
583 sp[1] = mp_const_none;
584 sp[2] = mp_const_none;
585 sp -= 2;
586 mp_call_method_n_kw(3, 0, sp);
AZ Huangb1f692e2014-04-14 23:22:44 +0800587 SET_TOP(mp_const_none);
AZ Huangb1f692e2014-04-14 23:22:44 +0800588 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
Damien George5e1d9932015-03-25 22:20:37 +0000589 mp_int_t cause_val = MP_OBJ_SMALL_INT_VALUE(TOP());
590 if (cause_val == UNWIND_RETURN) {
Damien George8c1d23a2015-04-24 01:52:28 +0100591 // stack: (..., __exit__, ctx_mgr, ret_val, UNWIND_RETURN)
592 mp_obj_t ret_val = sp[-1];
593 sp[-1] = mp_const_none;
594 sp[0] = mp_const_none;
595 sp[1] = mp_const_none;
596 mp_call_method_n_kw(3, 0, sp - 3);
597 sp[-3] = ret_val;
598 sp[-2] = MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN);
Damien George5e1d9932015-03-25 22:20:37 +0000599 } else {
600 assert(cause_val == UNWIND_JUMP);
Damien George8c1d23a2015-04-24 01:52:28 +0100601 // stack: (..., __exit__, ctx_mgr, dest_ip, num_exc, UNWIND_JUMP)
602 mp_obj_t dest_ip = sp[-2];
603 mp_obj_t num_exc = sp[-1];
604 sp[-2] = mp_const_none;
605 sp[-1] = mp_const_none;
606 sp[0] = mp_const_none;
607 mp_call_method_n_kw(3, 0, sp - 4);
608 sp[-4] = dest_ip;
609 sp[-3] = num_exc;
610 sp[-2] = MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP);
AZ Huangb1f692e2014-04-14 23:22:44 +0800611 }
Damien George8c1d23a2015-04-24 01:52:28 +0100612 sp -= 2; // we removed (__exit__, ctx_mgr)
Damien George5e1d9932015-03-25 22:20:37 +0000613 } else {
614 assert(mp_obj_is_exception_type(TOP()));
Damien George8c1d23a2015-04-24 01:52:28 +0100615 // stack: (..., __exit__, ctx_mgr, traceback, exc_val, exc_type)
Damien George596f41d2015-02-10 13:21:42 +0000616 // Need to pass (sp[0], sp[-1], sp[-2]) as arguments so must reverse the
617 // order of these on the value stack (don't want to create a temporary
618 // array because it increases stack footprint of the VM).
619 mp_obj_t obj = sp[-2];
620 sp[-2] = sp[0];
621 sp[0] = obj;
Damien George8c1d23a2015-04-24 01:52:28 +0100622 mp_obj_t ret_value = mp_call_method_n_kw(3, 0, sp - 4);
Damien Georged8675542014-05-25 22:58:04 +0100623 if (mp_obj_is_true(ret_value)) {
Damien George8c1d23a2015-04-24 01:52:28 +0100624 // We need to silence/swallow the exception. This is done
625 // by popping the exception and the __exit__ handler and
626 // replacing it with None, which signals END_FINALLY to just
627 // execute the finally handler normally.
Damien George596f41d2015-02-10 13:21:42 +0000628 sp -= 4;
Damien George8c1d23a2015-04-24 01:52:28 +0100629 SET_TOP(mp_const_none);
AZ Huangb1f692e2014-04-14 23:22:44 +0800630 assert(exc_sp >= exc_stack);
AZ Huangb1f692e2014-04-14 23:22:44 +0800631 POP_EXC_BLOCK();
Damien George596f41d2015-02-10 13:21:42 +0000632 } else {
Damien George8c1d23a2015-04-24 01:52:28 +0100633 // We need to re-raise the exception. We pop __exit__ handler
634 // and copy the 3 exception values down (remembering that they
635 // are reversed due to above code).
636 sp[-4] = sp[0];
637 sp[-3] = sp[-1];
638 sp -= 2;
AZ Huangb1f692e2014-04-14 23:22:44 +0800639 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800640 }
641 DISPATCH();
642 }
Damienc226dca2013-10-16 16:12:52 +0100643
Damien Georgecd97a432014-12-02 19:25:10 +0000644 ENTRY(MP_BC_UNWIND_JUMP): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200645 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800646 DECODE_SLABEL;
Damien Georgecd97a432014-12-02 19:25:10 +0000647 PUSH((void*)(ip + slab)); // push destination ip for jump
Damien George40f3c022014-07-03 13:25:24 +0100648 PUSH((void*)(mp_uint_t)(*ip)); // push number of exception handlers to unwind (0x80 bit set if we also need to pop stack)
Damien Georgecd97a432014-12-02 19:25:10 +0000649unwind_jump:;
650 mp_uint_t unum = (mp_uint_t)POP(); // get number of exception handlers to unwind
Damien George25c84642014-05-30 15:20:41 +0100651 while ((unum & 0x7f) > 0) {
AZ Huangb1f692e2014-04-14 23:22:44 +0800652 unum -= 1;
653 assert(exc_sp >= exc_stack);
Damien George74eb44c2014-12-22 12:49:57 +0000654 if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
Damien George4bf3f2d2015-10-15 17:48:28 +0100655 // Getting here the stack looks like:
656 // (..., X, dest_ip)
657 // where X is pointed to by exc_sp->val_sp and in the case
658 // of a "with" block contains the context manager info.
AZ Huangb1f692e2014-04-14 23:22:44 +0800659 // We're going to run "finally" code as a coroutine
660 // (not calling it recursively). Set up a sentinel
661 // on a stack so it can return back to us when it is
Damien George4bf3f2d2015-10-15 17:48:28 +0100662 // done (when WITH_CLEANUP or END_FINALLY reached).
AZ Huangb1f692e2014-04-14 23:22:44 +0800663 PUSH((void*)unum); // push number of exception handlers left to unwind
664 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP)); // push sentinel
665 ip = exc_sp->handler; // get exception handler byte code address
666 exc_sp--; // pop exception handler
667 goto dispatch_loop; // run the exception handler
668 }
669 exc_sp--;
670 }
671 ip = (const byte*)POP(); // pop destination ip for jump
Damien George25c84642014-05-30 15:20:41 +0100672 if (unum != 0) {
673 sp--;
674 }
Damien George124df6f2014-10-25 18:19:55 +0100675 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000676 }
Damience89a212013-10-15 22:25:17 +0100677
AZ Huangb1f692e2014-04-14 23:22:44 +0800678 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
679 ENTRY(MP_BC_SETUP_EXCEPT):
Damien Georgecd97a432014-12-02 19:25:10 +0000680 ENTRY(MP_BC_SETUP_FINALLY): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200681 MARK_EXC_IP_SELECTIVE();
Damien Georgef89d6592014-12-29 00:29:59 +0000682 #if SELECTIVE_EXC_IP
683 PUSH_EXC_BLOCK((code_state->ip[-1] == MP_BC_SETUP_FINALLY) ? 1 : 0);
684 #else
685 PUSH_EXC_BLOCK((code_state->ip[0] == MP_BC_SETUP_FINALLY) ? 1 : 0);
686 #endif
AZ Huangb1f692e2014-04-14 23:22:44 +0800687 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000688 }
Damience89a212013-10-15 22:25:17 +0100689
AZ Huangb1f692e2014-04-14 23:22:44 +0800690 ENTRY(MP_BC_END_FINALLY):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200691 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800692 // not fully implemented
693 // if TOS is an exception, reraises the exception (3 values on TOS)
694 // if TOS is None, just pops it and continues
695 // if TOS is an integer, does something else
696 // else error
697 if (mp_obj_is_exception_type(TOP())) {
Damien George66ae8c92014-04-17 16:50:23 +0100698 RAISE(sp[-1]);
AZ Huangb1f692e2014-04-14 23:22:44 +0800699 }
700 if (TOP() == mp_const_none) {
Damien George20006db2014-01-18 14:10:48 +0000701 sp--;
Damien George5e1d9932015-03-25 22:20:37 +0000702 } else {
703 assert(MP_OBJ_IS_SMALL_INT(TOP()));
AZ Huangb1f692e2014-04-14 23:22:44 +0800704 // We finished "finally" coroutine and now dispatch back
705 // to our caller, based on TOS value
706 mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
Damien George5e1d9932015-03-25 22:20:37 +0000707 if (reason == UNWIND_RETURN) {
708 goto unwind_return;
709 } else {
710 assert(reason == UNWIND_JUMP);
711 goto unwind_jump;
AZ Huangb1f692e2014-04-14 23:22:44 +0800712 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800713 }
714 DISPATCH();
715
716 ENTRY(MP_BC_GET_ITER):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200717 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800718 SET_TOP(mp_getiter(TOP()));
719 DISPATCH();
720
Damien Georged8675542014-05-25 22:58:04 +0100721 ENTRY(MP_BC_FOR_ITER): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200722 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800723 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien Georgec60a2612014-06-01 12:32:28 +0100724 code_state->sp = sp;
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +0300725 assert(TOP());
Damien Georged8675542014-05-25 22:58:04 +0100726 mp_obj_t value = mp_iternext_allow_raise(TOP());
727 if (value == MP_OBJ_STOP_ITERATION) {
AZ Huangb1f692e2014-04-14 23:22:44 +0800728 --sp; // pop the exhausted iterator
Damien Georgecd97a432014-12-02 19:25:10 +0000729 ip += ulab; // jump to after for-block
AZ Huangb1f692e2014-04-14 23:22:44 +0800730 } else {
Damien Georged8675542014-05-25 22:58:04 +0100731 PUSH(value); // push the next iteration value
AZ Huangb1f692e2014-04-14 23:22:44 +0800732 }
733 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100734 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800735
736 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
737 ENTRY(MP_BC_POP_BLOCK):
738 // we are exiting an exception handler, so pop the last one of the exception-stack
739 assert(exc_sp >= exc_stack);
740 POP_EXC_BLOCK();
741 DISPATCH();
742
743 // matched against: SETUP_EXCEPT
744 ENTRY(MP_BC_POP_EXCEPT):
745 // TODO need to work out how blocks work etc
746 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
747 assert(exc_sp >= exc_stack);
748 assert(currently_in_except_block);
749 //sp = (mp_obj_t*)(*exc_sp--);
750 //exc_sp--; // discard ip
751 POP_EXC_BLOCK();
752 //sp -= 3; // pop 3 exception values
753 DISPATCH();
754
755 ENTRY(MP_BC_NOT):
756 if (TOP() == mp_const_true) {
757 SET_TOP(mp_const_false);
758 } else {
759 SET_TOP(mp_const_true);
760 }
761 DISPATCH();
762
Damien Georgecd97a432014-12-02 19:25:10 +0000763 ENTRY(MP_BC_BUILD_TUPLE): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200764 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800765 DECODE_UINT;
766 sp -= unum - 1;
767 SET_TOP(mp_obj_new_tuple(unum, sp));
768 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000769 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800770
Damien Georgecd97a432014-12-02 19:25:10 +0000771 ENTRY(MP_BC_BUILD_LIST): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200772 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800773 DECODE_UINT;
774 sp -= unum - 1;
775 SET_TOP(mp_obj_new_list(unum, sp));
776 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000777 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800778
Damien Georgecd97a432014-12-02 19:25:10 +0000779 ENTRY(MP_BC_LIST_APPEND): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200780 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800781 DECODE_UINT;
782 // I think it's guaranteed by the compiler that sp[unum] is a list
783 mp_obj_list_append(sp[-unum], sp[0]);
784 sp--;
785 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000786 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800787
Damien Georgecd97a432014-12-02 19:25:10 +0000788 ENTRY(MP_BC_BUILD_MAP): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200789 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800790 DECODE_UINT;
791 PUSH(mp_obj_new_dict(unum));
792 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000793 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800794
795 ENTRY(MP_BC_STORE_MAP):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200796 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800797 sp -= 2;
798 mp_obj_dict_store(sp[0], sp[2], sp[1]);
799 DISPATCH();
800
Damien Georgecd97a432014-12-02 19:25:10 +0000801 ENTRY(MP_BC_MAP_ADD): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200802 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800803 DECODE_UINT;
804 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
805 mp_obj_dict_store(sp[-unum - 1], sp[0], sp[-1]);
806 sp -= 2;
807 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000808 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800809
Damien George3ebd4d02014-06-01 13:46:47 +0100810#if MICROPY_PY_BUILTINS_SET
Damien Georgecd97a432014-12-02 19:25:10 +0000811 ENTRY(MP_BC_BUILD_SET): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200812 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800813 DECODE_UINT;
814 sp -= unum - 1;
815 SET_TOP(mp_obj_new_set(unum, sp));
816 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000817 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800818
Damien Georgecd97a432014-12-02 19:25:10 +0000819 ENTRY(MP_BC_SET_ADD): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200820 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800821 DECODE_UINT;
822 // I think it's guaranteed by the compiler that sp[-unum] is a set
823 mp_obj_set_store(sp[-unum], sp[0]);
824 sp--;
825 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000826 }
Damien George3ebd4d02014-06-01 13:46:47 +0100827#endif
Damienc12aa462013-10-16 20:57:49 +0100828
Damien Georgefb510b32014-06-01 13:32:54 +0100829#if MICROPY_PY_BUILTINS_SLICE
Damien Georgecd97a432014-12-02 19:25:10 +0000830 ENTRY(MP_BC_BUILD_SLICE): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200831 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800832 DECODE_UINT;
833 if (unum == 2) {
Damien Georged8675542014-05-25 22:58:04 +0100834 mp_obj_t stop = POP();
835 mp_obj_t start = TOP();
836 SET_TOP(mp_obj_new_slice(start, stop, mp_const_none));
AZ Huangb1f692e2014-04-14 23:22:44 +0800837 } else {
Damien Georged8675542014-05-25 22:58:04 +0100838 mp_obj_t step = POP();
839 mp_obj_t stop = POP();
840 mp_obj_t start = TOP();
841 SET_TOP(mp_obj_new_slice(start, stop, step));
AZ Huangb1f692e2014-04-14 23:22:44 +0800842 }
843 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000844 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800845#endif
846
Damien Georgecd97a432014-12-02 19:25:10 +0000847 ENTRY(MP_BC_UNPACK_SEQUENCE): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200848 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800849 DECODE_UINT;
850 mp_unpack_sequence(sp[0], unum, sp);
851 sp += unum - 1;
852 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000853 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800854
Damien Georgecd97a432014-12-02 19:25:10 +0000855 ENTRY(MP_BC_UNPACK_EX): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200856 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800857 DECODE_UINT;
858 mp_unpack_ex(sp[0], unum, sp);
859 sp += (unum & 0xff) + ((unum >> 8) & 0xff);
860 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000861 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800862
Damien Georgecd97a432014-12-02 19:25:10 +0000863 ENTRY(MP_BC_MAKE_FUNCTION): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800864 DECODE_PTR;
Damien Georgecd97a432014-12-02 19:25:10 +0000865 PUSH(mp_make_function_from_raw_code(ptr, MP_OBJ_NULL, MP_OBJ_NULL));
AZ Huangb1f692e2014-04-14 23:22:44 +0800866 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000867 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800868
Damien Georged8675542014-05-25 22:58:04 +0100869 ENTRY(MP_BC_MAKE_FUNCTION_DEFARGS): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800870 DECODE_PTR;
871 // Stack layout: def_tuple def_dict <- TOS
Damien Georged8675542014-05-25 22:58:04 +0100872 mp_obj_t def_dict = POP();
Damien Georgecd97a432014-12-02 19:25:10 +0000873 SET_TOP(mp_make_function_from_raw_code(ptr, TOP(), def_dict));
AZ Huangb1f692e2014-04-14 23:22:44 +0800874 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100875 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800876
Damien George3558f622014-04-20 17:50:40 +0100877 ENTRY(MP_BC_MAKE_CLOSURE): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800878 DECODE_PTR;
Damien George40f3c022014-07-03 13:25:24 +0100879 mp_uint_t n_closed_over = *ip++;
Damien George3558f622014-04-20 17:50:40 +0100880 // Stack layout: closed_overs <- TOS
881 sp -= n_closed_over - 1;
Damien Georgecd97a432014-12-02 19:25:10 +0000882 SET_TOP(mp_make_closure_from_raw_code(ptr, n_closed_over, sp));
AZ Huangb1f692e2014-04-14 23:22:44 +0800883 DISPATCH();
Damien George3558f622014-04-20 17:50:40 +0100884 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800885
Damien George3558f622014-04-20 17:50:40 +0100886 ENTRY(MP_BC_MAKE_CLOSURE_DEFARGS): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800887 DECODE_PTR;
Damien George40f3c022014-07-03 13:25:24 +0100888 mp_uint_t n_closed_over = *ip++;
Damien George3558f622014-04-20 17:50:40 +0100889 // Stack layout: def_tuple def_dict closed_overs <- TOS
890 sp -= 2 + n_closed_over - 1;
Damien Georgecd97a432014-12-02 19:25:10 +0000891 SET_TOP(mp_make_closure_from_raw_code(ptr, 0x100 | n_closed_over, sp));
AZ Huangb1f692e2014-04-14 23:22:44 +0800892 DISPATCH();
Damien George3558f622014-04-20 17:50:40 +0100893 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800894
Damien Georgecd97a432014-12-02 19:25:10 +0000895 ENTRY(MP_BC_CALL_FUNCTION): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200896 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800897 DECODE_UINT;
898 // unum & 0xff == n_positional
899 // (unum >> 8) & 0xff == n_keyword
900 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200901 #if MICROPY_STACKLESS
902 if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
903 code_state->ip = ip;
904 code_state->sp = sp;
905 code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
906 mp_code_state *new_state = mp_obj_fun_bc_prepare_codestate(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1);
Paul Sokolovsky332a9092015-03-28 01:14:44 +0200907 if (new_state) {
908 new_state->prev = code_state;
909 code_state = new_state;
910 nlr_pop();
911 goto run_code_state;
912 }
Paul Sokolovsky7f1c9812015-03-28 01:14:45 +0200913 #if MICROPY_STACKLESS_STRICT
914 else {
915 deep_recursion_error:
916 mp_exc_recursion_depth();
917 }
918 #endif
Paul Sokolovsky20397572015-03-28 01:14:44 +0200919 }
920 #endif
AZ Huangb1f692e2014-04-14 23:22:44 +0800921 SET_TOP(mp_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
922 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000923 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800924
Damien Georgecd97a432014-12-02 19:25:10 +0000925 ENTRY(MP_BC_CALL_FUNCTION_VAR_KW): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200926 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800927 DECODE_UINT;
928 // unum & 0xff == n_positional
929 // (unum >> 8) & 0xff == n_keyword
930 // We have folowing stack layout here:
931 // fun arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS
932 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 2;
Paul Sokolovskyf0a8f212015-03-28 01:14:45 +0200933 #if MICROPY_STACKLESS
934 if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
935 code_state->ip = ip;
936 code_state->sp = sp;
937 code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
938
Damien George12a5e172015-04-01 23:31:30 +0100939 mp_call_args_t out_args;
Paul Sokolovskyf0a8f212015-03-28 01:14:45 +0200940 mp_call_prepare_args_n_kw_var(false, unum, sp, &out_args);
941
942 mp_code_state *new_state = mp_obj_fun_bc_prepare_codestate(out_args.fun,
943 out_args.n_args, out_args.n_kw, out_args.args);
944 m_del(mp_obj_t, out_args.args, out_args.n_alloc);
945 if (new_state) {
946 new_state->prev = code_state;
947 code_state = new_state;
948 nlr_pop();
949 goto run_code_state;
950 }
Paul Sokolovsky7f1c9812015-03-28 01:14:45 +0200951 #if MICROPY_STACKLESS_STRICT
952 else {
953 goto deep_recursion_error;
954 }
955 #endif
Paul Sokolovskyf0a8f212015-03-28 01:14:45 +0200956 }
957 #endif
AZ Huangb1f692e2014-04-14 23:22:44 +0800958 SET_TOP(mp_call_method_n_kw_var(false, unum, sp));
959 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000960 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800961
Damien Georgecd97a432014-12-02 19:25:10 +0000962 ENTRY(MP_BC_CALL_METHOD): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200963 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800964 DECODE_UINT;
965 // unum & 0xff == n_positional
966 // (unum >> 8) & 0xff == n_keyword
967 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
Paul Sokolovsky390e9262015-03-28 01:14:44 +0200968 #if MICROPY_STACKLESS
969 if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
970 code_state->ip = ip;
971 code_state->sp = sp;
972 code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
973
974 mp_uint_t n_args = unum & 0xff;
975 mp_uint_t n_kw = (unum >> 8) & 0xff;
976 int adjust = (sp[1] == NULL) ? 0 : 1;
977
978 mp_code_state *new_state = mp_obj_fun_bc_prepare_codestate(*sp, n_args + adjust, n_kw, sp + 2 - adjust);
979 if (new_state) {
980 new_state->prev = code_state;
981 code_state = new_state;
982 nlr_pop();
983 goto run_code_state;
984 }
Paul Sokolovsky7f1c9812015-03-28 01:14:45 +0200985 #if MICROPY_STACKLESS_STRICT
986 else {
987 goto deep_recursion_error;
988 }
989 #endif
Paul Sokolovsky390e9262015-03-28 01:14:44 +0200990 }
991 #endif
AZ Huangb1f692e2014-04-14 23:22:44 +0800992 SET_TOP(mp_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
993 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000994 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800995
Damien Georgecd97a432014-12-02 19:25:10 +0000996 ENTRY(MP_BC_CALL_METHOD_VAR_KW): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200997 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800998 DECODE_UINT;
999 // unum & 0xff == n_positional
1000 // (unum >> 8) & 0xff == n_keyword
1001 // We have folowing stack layout here:
1002 // fun self arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS
1003 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 3;
Paul Sokolovskyf0a8f212015-03-28 01:14:45 +02001004 #if MICROPY_STACKLESS
1005 if (mp_obj_get_type(*sp) == &mp_type_fun_bc) {
1006 code_state->ip = ip;
1007 code_state->sp = sp;
1008 code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
1009
Damien George12a5e172015-04-01 23:31:30 +01001010 mp_call_args_t out_args;
Paul Sokolovskyf0a8f212015-03-28 01:14:45 +02001011 mp_call_prepare_args_n_kw_var(true, unum, sp, &out_args);
1012
1013 mp_code_state *new_state = mp_obj_fun_bc_prepare_codestate(out_args.fun,
1014 out_args.n_args, out_args.n_kw, out_args.args);
1015 m_del(mp_obj_t, out_args.args, out_args.n_alloc);
1016 if (new_state) {
1017 new_state->prev = code_state;
1018 code_state = new_state;
1019 nlr_pop();
1020 goto run_code_state;
1021 }
Paul Sokolovsky7f1c9812015-03-28 01:14:45 +02001022 #if MICROPY_STACKLESS_STRICT
1023 else {
1024 goto deep_recursion_error;
1025 }
1026 #endif
Paul Sokolovskyf0a8f212015-03-28 01:14:45 +02001027 }
1028 #endif
AZ Huangb1f692e2014-04-14 23:22:44 +08001029 SET_TOP(mp_call_method_n_kw_var(true, unum, sp));
1030 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +00001031 }
AZ Huangb1f692e2014-04-14 23:22:44 +08001032
1033 ENTRY(MP_BC_RETURN_VALUE):
Paul Sokolovsky74957502014-12-28 07:17:43 +02001034 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +08001035unwind_return:
1036 while (exc_sp >= exc_stack) {
Damien George74eb44c2014-12-22 12:49:57 +00001037 if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
Damien George4bf3f2d2015-10-15 17:48:28 +01001038 // Getting here the stack looks like:
1039 // (..., X, [iter0, iter1, ...,] ret_val)
1040 // where X is pointed to by exc_sp->val_sp and in the case
1041 // of a "with" block contains the context manager info.
1042 // There may be 0 or more for-iterators between X and the
1043 // return value, and these must be removed before control can
1044 // pass to the finally code. We simply copy the ret_value down
1045 // over these iterators, if they exist. If they don't then the
1046 // following is a null operation.
1047 mp_obj_t *finally_sp = MP_TAGPTR_PTR(exc_sp->val_sp);
1048 finally_sp[1] = sp[0];
1049 sp = &finally_sp[1];
AZ Huangb1f692e2014-04-14 23:22:44 +08001050 // We're going to run "finally" code as a coroutine
1051 // (not calling it recursively). Set up a sentinel
1052 // on a stack so it can return back to us when it is
Damien George4bf3f2d2015-10-15 17:48:28 +01001053 // done (when WITH_CLEANUP or END_FINALLY reached).
AZ Huangb1f692e2014-04-14 23:22:44 +08001054 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN));
1055 ip = exc_sp->handler;
AZ Huangb1f692e2014-04-14 23:22:44 +08001056 exc_sp--;
1057 goto dispatch_loop;
1058 }
1059 exc_sp--;
1060 }
1061 nlr_pop();
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +03001062 code_state->sp = sp;
AZ Huangb1f692e2014-04-14 23:22:44 +08001063 assert(exc_sp == exc_stack - 1);
Paul Sokolovsky20397572015-03-28 01:14:44 +02001064 #if MICROPY_STACKLESS
1065 if (code_state->prev != NULL) {
1066 mp_obj_t res = *sp;
1067 mp_globals_set(code_state->old_globals);
1068 code_state = code_state->prev;
1069 *code_state->sp = res;
1070 goto run_code_state;
1071 }
1072 #endif
AZ Huangb1f692e2014-04-14 23:22:44 +08001073 return MP_VM_RETURN_NORMAL;
1074
Damien Georged8675542014-05-25 22:58:04 +01001075 ENTRY(MP_BC_RAISE_VARARGS): {
Paul Sokolovsky74957502014-12-28 07:17:43 +02001076 MARK_EXC_IP_SELECTIVE();
Damien Georgecd97a432014-12-02 19:25:10 +00001077 mp_uint_t unum = *ip++;
Damien Georged8675542014-05-25 22:58:04 +01001078 mp_obj_t obj;
Paul Sokolovsky2ff2ea52015-09-01 10:35:58 +03001079 if (unum == 2) {
1080 mp_warning("exception chaining not supported");
1081 // ignore (pop) "from" argument
1082 sp--;
1083 }
AZ Huangb1f692e2014-04-14 23:22:44 +08001084 if (unum == 0) {
1085 // search for the inner-most previous exception, to reraise it
Damien Georged8675542014-05-25 22:58:04 +01001086 obj = MP_OBJ_NULL;
AZ Huangb1f692e2014-04-14 23:22:44 +08001087 for (mp_exc_stack_t *e = exc_sp; e >= exc_stack; e--) {
1088 if (e->prev_exc != MP_OBJ_NULL) {
Damien Georged8675542014-05-25 22:58:04 +01001089 obj = e->prev_exc;
AZ Huangb1f692e2014-04-14 23:22:44 +08001090 break;
1091 }
1092 }
Damien Georged8675542014-05-25 22:58:04 +01001093 if (obj == MP_OBJ_NULL) {
1094 obj = mp_obj_new_exception_msg(&mp_type_RuntimeError, "No active exception to reraise");
1095 RAISE(obj);
AZ Huangb1f692e2014-04-14 23:22:44 +08001096 }
1097 } else {
Damien Georged8675542014-05-25 22:58:04 +01001098 obj = POP();
AZ Huangb1f692e2014-04-14 23:22:44 +08001099 }
Damien Georged8675542014-05-25 22:58:04 +01001100 obj = mp_make_raise_obj(obj);
1101 RAISE(obj);
1102 }
AZ Huangb1f692e2014-04-14 23:22:44 +08001103
1104 ENTRY(MP_BC_YIELD_VALUE):
1105yield:
1106 nlr_pop();
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +03001107 code_state->ip = ip;
1108 code_state->sp = sp;
1109 code_state->exc_sp = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
AZ Huangb1f692e2014-04-14 23:22:44 +08001110 return MP_VM_RETURN_YIELD;
1111
1112 ENTRY(MP_BC_YIELD_FROM): {
Paul Sokolovsky74957502014-12-28 07:17:43 +02001113 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +08001114//#define EXC_MATCH(exc, type) MP_OBJ_IS_TYPE(exc, type)
1115#define EXC_MATCH(exc, type) mp_obj_exception_match(exc, type)
Damien George66ae8c92014-04-17 16:50:23 +01001116#define GENERATOR_EXIT_IF_NEEDED(t) if (t != MP_OBJ_NULL && EXC_MATCH(t, &mp_type_GeneratorExit)) { RAISE(t); }
AZ Huangb1f692e2014-04-14 23:22:44 +08001117 mp_vm_return_kind_t ret_kind;
Damien Georged8675542014-05-25 22:58:04 +01001118 mp_obj_t send_value = POP();
AZ Huangb1f692e2014-04-14 23:22:44 +08001119 mp_obj_t t_exc = MP_OBJ_NULL;
Damien Georged8675542014-05-25 22:58:04 +01001120 mp_obj_t ret_value;
AZ Huangb1f692e2014-04-14 23:22:44 +08001121 if (inject_exc != MP_OBJ_NULL) {
1122 t_exc = inject_exc;
1123 inject_exc = MP_OBJ_NULL;
Damien Georged8675542014-05-25 22:58:04 +01001124 ret_kind = mp_resume(TOP(), MP_OBJ_NULL, t_exc, &ret_value);
AZ Huangb1f692e2014-04-14 23:22:44 +08001125 } else {
Damien Georged8675542014-05-25 22:58:04 +01001126 ret_kind = mp_resume(TOP(), send_value, MP_OBJ_NULL, &ret_value);
AZ Huangb1f692e2014-04-14 23:22:44 +08001127 }
1128
1129 if (ret_kind == MP_VM_RETURN_YIELD) {
1130 ip--;
Damien Georged8675542014-05-25 22:58:04 +01001131 PUSH(ret_value);
AZ Huangb1f692e2014-04-14 23:22:44 +08001132 goto yield;
1133 }
1134 if (ret_kind == MP_VM_RETURN_NORMAL) {
1135 // Pop exhausted gen
1136 sp--;
Damien Georged8675542014-05-25 22:58:04 +01001137 if (ret_value == MP_OBJ_NULL) {
AZ Huangb1f692e2014-04-14 23:22:44 +08001138 // Optimize StopIteration
1139 // TODO: get StopIteration's value
1140 PUSH(mp_const_none);
1141 } else {
Damien Georged8675542014-05-25 22:58:04 +01001142 PUSH(ret_value);
AZ Huangb1f692e2014-04-14 23:22:44 +08001143 }
1144
1145 // If we injected GeneratorExit downstream, then even
1146 // if it was swallowed, we re-raise GeneratorExit
1147 GENERATOR_EXIT_IF_NEEDED(t_exc);
1148 DISPATCH();
1149 }
1150 if (ret_kind == MP_VM_RETURN_EXCEPTION) {
1151 // Pop exhausted gen
1152 sp--;
Damien Georged8675542014-05-25 22:58:04 +01001153 if (EXC_MATCH(ret_value, &mp_type_StopIteration)) {
1154 PUSH(mp_obj_exception_get_value(ret_value));
AZ Huangb1f692e2014-04-14 23:22:44 +08001155 // If we injected GeneratorExit downstream, then even
1156 // if it was swallowed, we re-raise GeneratorExit
1157 GENERATOR_EXIT_IF_NEEDED(t_exc);
1158 DISPATCH();
1159 } else {
Damien Georged8675542014-05-25 22:58:04 +01001160 RAISE(ret_value);
AZ Huangb1f692e2014-04-14 23:22:44 +08001161 }
1162 }
Damien429d7192013-10-04 19:53:11 +01001163 }
AZ Huangb1f692e2014-04-14 23:22:44 +08001164
Damien Georged8675542014-05-25 22:58:04 +01001165 ENTRY(MP_BC_IMPORT_NAME): {
Paul Sokolovsky74957502014-12-28 07:17:43 +02001166 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +08001167 DECODE_QSTR;
Damien Georged8675542014-05-25 22:58:04 +01001168 mp_obj_t obj = POP();
1169 SET_TOP(mp_import_name(qst, obj, TOP()));
AZ Huangb1f692e2014-04-14 23:22:44 +08001170 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +01001171 }
AZ Huangb1f692e2014-04-14 23:22:44 +08001172
Damien Georged8675542014-05-25 22:58:04 +01001173 ENTRY(MP_BC_IMPORT_FROM): {
Paul Sokolovsky74957502014-12-28 07:17:43 +02001174 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +08001175 DECODE_QSTR;
Damien Georged8675542014-05-25 22:58:04 +01001176 mp_obj_t obj = mp_import_from(TOP(), qst);
1177 PUSH(obj);
AZ Huangb1f692e2014-04-14 23:22:44 +08001178 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +01001179 }
AZ Huangb1f692e2014-04-14 23:22:44 +08001180
1181 ENTRY(MP_BC_IMPORT_STAR):
Paul Sokolovsky74957502014-12-28 07:17:43 +02001182 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +08001183 mp_import_all(POP());
1184 DISPATCH();
1185
Damien George8456cc02014-10-25 16:43:46 +01001186#if MICROPY_OPT_COMPUTED_GOTO
1187 ENTRY(MP_BC_LOAD_CONST_SMALL_INT_MULTI):
1188 PUSH(MP_OBJ_NEW_SMALL_INT((mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16));
1189 DISPATCH();
1190
1191 ENTRY(MP_BC_LOAD_FAST_MULTI):
1192 obj_shared = fastn[MP_BC_LOAD_FAST_MULTI - (mp_int_t)ip[-1]];
1193 goto load_check;
1194
1195 ENTRY(MP_BC_STORE_FAST_MULTI):
1196 fastn[MP_BC_STORE_FAST_MULTI - (mp_int_t)ip[-1]] = POP();
1197 DISPATCH();
1198
1199 ENTRY(MP_BC_UNARY_OP_MULTI):
Paul Sokolovsky74957502014-12-28 07:17:43 +02001200 MARK_EXC_IP_SELECTIVE();
Damien George8456cc02014-10-25 16:43:46 +01001201 SET_TOP(mp_unary_op(ip[-1] - MP_BC_UNARY_OP_MULTI, TOP()));
1202 DISPATCH();
1203
1204 ENTRY(MP_BC_BINARY_OP_MULTI): {
Paul Sokolovsky74957502014-12-28 07:17:43 +02001205 MARK_EXC_IP_SELECTIVE();
Damien George8456cc02014-10-25 16:43:46 +01001206 mp_obj_t rhs = POP();
1207 mp_obj_t lhs = TOP();
1208 SET_TOP(mp_binary_op(ip[-1] - MP_BC_BINARY_OP_MULTI, lhs, rhs));
1209 DISPATCH();
1210 }
1211
1212 ENTRY_DEFAULT:
Paul Sokolovsky74957502014-12-28 07:17:43 +02001213 MARK_EXC_IP_SELECTIVE();
Damien George8456cc02014-10-25 16:43:46 +01001214#else
1215 ENTRY_DEFAULT:
1216 if (ip[-1] < MP_BC_LOAD_CONST_SMALL_INT_MULTI + 64) {
1217 PUSH(MP_OBJ_NEW_SMALL_INT((mp_int_t)ip[-1] - MP_BC_LOAD_CONST_SMALL_INT_MULTI - 16));
1218 DISPATCH();
1219 } else if (ip[-1] < MP_BC_LOAD_FAST_MULTI + 16) {
1220 obj_shared = fastn[MP_BC_LOAD_FAST_MULTI - (mp_int_t)ip[-1]];
1221 goto load_check;
1222 } else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
1223 fastn[MP_BC_STORE_FAST_MULTI - (mp_int_t)ip[-1]] = POP();
1224 DISPATCH();
Damien Georgec2a4e4e2015-05-11 12:25:19 +00001225 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 6) {
Damien George8456cc02014-10-25 16:43:46 +01001226 SET_TOP(mp_unary_op(ip[-1] - MP_BC_UNARY_OP_MULTI, TOP()));
1227 DISPATCH();
Damien Georgec5029bc2015-06-13 22:00:10 +01001228 } else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) {
Damien George8456cc02014-10-25 16:43:46 +01001229 mp_obj_t rhs = POP();
1230 mp_obj_t lhs = TOP();
1231 SET_TOP(mp_binary_op(ip[-1] - MP_BC_BINARY_OP_MULTI, lhs, rhs));
1232 DISPATCH();
1233 } else
1234#endif
1235 {
Damien Georged8675542014-05-25 22:58:04 +01001236 mp_obj_t obj = mp_obj_new_exception_msg(&mp_type_NotImplementedError, "byte code not implemented");
AZ Huangb1f692e2014-04-14 23:22:44 +08001237 nlr_pop();
Damien Georged8675542014-05-25 22:58:04 +01001238 fastn[0] = obj;
AZ Huangb1f692e2014-04-14 23:22:44 +08001239 return MP_VM_RETURN_EXCEPTION;
Damien Georged8675542014-05-25 22:58:04 +01001240 }
Damien George66ae8c92014-04-17 16:50:23 +01001241
Damien George58ebde42014-05-21 20:32:59 +01001242#if !MICROPY_OPT_COMPUTED_GOTO
AZ Huang9309d992014-04-15 15:57:01 +08001243 } // switch
AZ Huangb1f692e2014-04-14 23:22:44 +08001244#endif
Damien George124df6f2014-10-25 18:19:55 +01001245
1246pending_exception_check:
Damien Georgeb4b10fd2015-01-01 23:30:53 +00001247 if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) {
Paul Sokolovsky74957502014-12-28 07:17:43 +02001248 MARK_EXC_IP_SELECTIVE();
Damien Georgeb4b10fd2015-01-01 23:30:53 +00001249 mp_obj_t obj = MP_STATE_VM(mp_pending_exception);
1250 MP_STATE_VM(mp_pending_exception) = MP_OBJ_NULL;
Damien George124df6f2014-10-25 18:19:55 +01001251 RAISE(obj);
1252 }
Damien George124df6f2014-10-25 18:19:55 +01001253
Damien George66ae8c92014-04-17 16:50:23 +01001254 } // for loop
Damien429d7192013-10-04 19:53:11 +01001255
Damience89a212013-10-15 22:25:17 +01001256 } else {
Damien George66ae8c92014-04-17 16:50:23 +01001257exception_handler:
Damience89a212013-10-15 22:25:17 +01001258 // exception occurred
Damien429d7192013-10-04 19:53:11 +01001259
Paul Sokolovsky8b85d142015-04-25 03:17:41 +03001260 #if MICROPY_PY_SYS_EXC_INFO
1261 MP_STATE_VM(cur_exception) = nlr.ret_val;
1262 #endif
1263
Damien Georgef89d6592014-12-29 00:29:59 +00001264 #if SELECTIVE_EXC_IP
1265 // with selective ip, we store the ip 1 byte past the opcode, so move ptr back
1266 code_state->ip -= 1;
1267 #endif
1268
Paul Sokolovskya7c02c42015-05-10 17:18:10 +03001269 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
1270 if (code_state->ip) {
1271 // check if it's a StopIteration within a for block
1272 if (*code_state->ip == MP_BC_FOR_ITER) {
1273 const byte *ip = code_state->ip + 1;
1274 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
1275 code_state->ip = ip + ulab; // jump to after for-block
1276 code_state->sp -= 1; // pop the exhausted iterator
1277 goto outer_dispatch_loop; // continue with dispatch loop
Paul Sokolovsky6738c1d2015-05-11 02:59:25 +03001278 } else if (*code_state->ip == MP_BC_YIELD_FROM) {
1279 // StopIteration inside yield from call means return a value of
1280 // yield from, so inject exception's value as yield from's result
1281 *++code_state->sp = mp_obj_exception_get_value(nlr.ret_val);
1282 code_state->ip++; // yield from is over, move to next instruction
1283 goto outer_dispatch_loop; // continue with dispatch loop
Paul Sokolovskya7c02c42015-05-10 17:18:10 +03001284 }
1285 }
Damien George9e6e9352014-03-26 18:37:06 +00001286 }
1287
Paul Sokolovsky20397572015-03-28 01:14:44 +02001288#if MICROPY_STACKLESS
1289unwind_loop:
1290#endif
Damien George08335002014-01-18 23:24:36 +00001291 // set file and line number that the exception occurred at
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +02001292 // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
1293 // But consider how to handle nested exceptions.
Damien George6902eed2014-04-04 10:52:59 +00001294 // TODO need a better way of not adding traceback to constant objects (right now, just GeneratorExit_obj and MemoryError_obj)
1295 if (mp_obj_is_exception_instance(nlr.ret_val) && nlr.ret_val != &mp_const_GeneratorExit_obj && nlr.ret_val != &mp_const_MemoryError_obj) {
Damien Georgeb534e1b2014-09-04 14:44:01 +01001296 const byte *ip = code_state->code_info;
1297 mp_uint_t code_info_size = mp_decode_uint(&ip);
Damien Georgec8e9c0d2015-11-02 17:27:18 +00001298 #if MICROPY_PERSISTENT_CODE
1299 qstr block_name = ip[0] | (ip[1] << 8);
1300 qstr source_file = ip[2] | (ip[3] << 8);
1301 ip += 4;
1302 #else
Damien Georgeb534e1b2014-09-04 14:44:01 +01001303 qstr block_name = mp_decode_uint(&ip);
1304 qstr source_file = mp_decode_uint(&ip);
Damien Georgec8e9c0d2015-11-02 17:27:18 +00001305 #endif
Damien Georgeb534e1b2014-09-04 14:44:01 +01001306 mp_uint_t bc = code_state->ip - code_state->code_info - code_info_size;
Damien Georgeb427d6a2014-08-26 23:35:57 +01001307 mp_uint_t source_line = 1;
1308 mp_uint_t c;
Damien Georgeb534e1b2014-09-04 14:44:01 +01001309 while ((c = *ip)) {
Damien Georgeb427d6a2014-08-26 23:35:57 +01001310 mp_uint_t b, l;
1311 if ((c & 0x80) == 0) {
1312 // 0b0LLBBBBB encoding
1313 b = c & 0x1f;
1314 l = c >> 5;
Damien Georgeb534e1b2014-09-04 14:44:01 +01001315 ip += 1;
Damien Georgeb427d6a2014-08-26 23:35:57 +01001316 } else {
1317 // 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
1318 b = c & 0xf;
Damien Georgeb534e1b2014-09-04 14:44:01 +01001319 l = ((c << 4) & 0x700) | ip[1];
1320 ip += 2;
Damien Georgeb427d6a2014-08-26 23:35:57 +01001321 }
1322 if (bc >= b) {
1323 bc -= b;
1324 source_line += l;
1325 } else {
1326 // found source line corresponding to bytecode offset
1327 break;
Paul Sokolovsky411732e2014-06-02 18:24:34 +03001328 }
Damien George08335002014-01-18 23:24:36 +00001329 }
Damien George136b1492014-01-19 12:38:49 +00001330 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +00001331 }
1332
Damien8f9e2ee2013-12-29 16:54:59 +00001333 while (currently_in_except_block) {
1334 // nested exception
1335
Paul Sokolovskyc0abc282014-03-22 13:49:31 +02001336 assert(exc_sp >= exc_stack);
Damien8f9e2ee2013-12-29 16:54:59 +00001337
1338 // TODO make a proper message for nested exception
1339 // at the moment we are just raising the very last exception (the one that caused the nested exception)
1340
1341 // move up to previous exception handler
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +02001342 POP_EXC_BLOCK();
Damien8f9e2ee2013-12-29 16:54:59 +00001343 }
1344
Paul Sokolovskyc0abc282014-03-22 13:49:31 +02001345 if (exc_sp >= exc_stack) {
Damien8f9e2ee2013-12-29 16:54:59 +00001346 // set flag to indicate that we are now handling an exception
1347 currently_in_except_block = 1;
1348
Damience89a212013-10-15 22:25:17 +01001349 // catch exception and pass to byte code
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +03001350 code_state->ip = exc_sp->handler;
Damien George66ae8c92014-04-17 16:50:23 +01001351 mp_obj_t *sp = MP_TAGPTR_PTR(exc_sp->val_sp);
Damien Georged7592a12014-03-30 00:54:48 +00001352 // save this exception in the stack so it can be used in a reraise, if needed
1353 exc_sp->prev_exc = nlr.ret_val;
Damienc9f91972013-10-15 23:46:01 +01001354 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +00001355 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +01001356 PUSH(nlr.ret_val);
Paul Sokolovsky682f9e62014-03-29 02:52:17 +02001357 PUSH(mp_obj_get_type(nlr.ret_val));
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +03001358 code_state->sp = sp;
Damien8f9e2ee2013-12-29 16:54:59 +00001359
Paul Sokolovsky20397572015-03-28 01:14:44 +02001360 #if MICROPY_STACKLESS
1361 } else if (code_state->prev != NULL) {
1362 mp_globals_set(code_state->old_globals);
1363 code_state = code_state->prev;
1364 fastn = &code_state->state[code_state->n_state - 1];
1365 exc_stack = (mp_exc_stack_t*)(code_state->state + code_state->n_state);
1366 // variables that are visible to the exception handler (declared volatile)
1367 currently_in_except_block = MP_TAGPTR_TAG0(code_state->exc_sp); // 0 or 1, to detect nested exceptions
1368 exc_sp = MP_TAGPTR_PTR(code_state->exc_sp); // stack grows up, exc_sp points to top of stack
1369 goto unwind_loop;
1370
1371 #endif
Damience89a212013-10-15 22:25:17 +01001372 } else {
Damien Georgec8f78bc2014-02-15 22:55:00 +00001373 // propagate exception to higher level
1374 // TODO what to do about ip and sp? they don't really make sense at this point
1375 fastn[0] = nlr.ret_val; // must put exception here because sp is invalid
1376 return MP_VM_RETURN_EXCEPTION;
Damience89a212013-10-15 22:25:17 +01001377 }
Damien429d7192013-10-04 19:53:11 +01001378 }
1379 }
1380}