blob: 8bf59f1e05039f3a728212371eafae632824fc62 [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 George999cedb2015-11-27 17:01:44 +000041//#define TRACE(ip) printf("sp=" INT_FMT " ", sp - code_state->sp); mp_bytecode_print2(ip, 1);
42#define TRACE(ip) printf("sp=%d ", sp - code_state->sp); mp_bytecode_print2(ip, 1);
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +030043#else
44#define TRACE(ip)
45#endif
Damien Georgee90be0d2014-04-10 16:21:34 +000046
Paul Sokolovsky85193422014-01-31 19:45:15 +020047// Value stack grows up (this makes it incompatible with native C stack, but
48// makes sure that arguments to functions are in natural order arg1..argN
49// (Python semantics mandates left-to-right evaluation order, including for
50// function arguments). Stack pointer is pre-incremented and points at the
51// top element.
52// Exception stack also grows up, top element is also pointed at.
53
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020054// Exception stack unwind reasons (WHY_* in CPython-speak)
Damien Georgecbddb272014-02-01 20:08:18 +000055// TODO perhaps compress this to RETURN=0, JUMP>0, with number of unwinds
56// left to do encoded in the JUMP number
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020057typedef enum {
58 UNWIND_RETURN = 1,
Damien Georgecbddb272014-02-01 20:08:18 +000059 UNWIND_JUMP,
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020060} mp_unwind_reason_t;
61
Damien Georgecd97a432014-12-02 19:25:10 +000062#define DECODE_UINT \
63 mp_uint_t unum = 0; \
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020064 do { \
65 unum = (unum << 7) + (*ip & 0x7f); \
Damien Georgecd97a432014-12-02 19:25:10 +000066 } while ((*ip++ & 0x80) != 0)
67#define DECODE_ULABEL mp_uint_t ulab = (ip[0] | (ip[1] << 8)); ip += 2
68#define DECODE_SLABEL mp_uint_t slab = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2
Damien Georgec8e9c0d2015-11-02 17:27:18 +000069
70#if MICROPY_PERSISTENT_CODE
71
72#define DECODE_QSTR \
73 qstr qst = ip[0] | ip[1] << 8; \
74 ip += 2;
75#define DECODE_PTR \
76 DECODE_UINT; \
Damien George999cedb2015-11-27 17:01:44 +000077 void *ptr = (void*)(uintptr_t)code_state->const_table[unum]
78#define DECODE_OBJ \
79 DECODE_UINT; \
80 mp_obj_t obj = (mp_obj_t)code_state->const_table[unum]
Damien Georgec8e9c0d2015-11-02 17:27:18 +000081
82#else
83
Damien Georged8675542014-05-25 22:58:04 +010084#define DECODE_QSTR qstr qst = 0; \
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020085 do { \
86 qst = (qst << 7) + (*ip & 0x7f); \
Damien Georged8675542014-05-25 22:58:04 +010087 } while ((*ip++ & 0x80) != 0)
Damien Georgecd97a432014-12-02 19:25:10 +000088#define DECODE_PTR \
Damien George999cedb2015-11-27 17:01:44 +000089 ip = (byte*)MP_ALIGN(ip, sizeof(void*)); \
90 void *ptr = *(void**)ip; \
91 ip += sizeof(void*)
92#define DECODE_OBJ \
93 ip = (byte*)MP_ALIGN(ip, sizeof(mp_obj_t)); \
94 mp_obj_t obj = *(mp_obj_t*)ip; \
95 ip += sizeof(mp_obj_t)
Damien Georgec8e9c0d2015-11-02 17:27:18 +000096
97#endif
98
Damien George20006db2014-01-18 14:10:48 +000099#define PUSH(val) *++sp = (val)
100#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +0000101#define TOP() (*sp)
102#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +0100103
Paul Sokolovskyae2c81f2015-04-26 01:20:49 +0300104#if MICROPY_PY_SYS_EXC_INFO
Damien George999cedb2015-11-27 17:01:44 +0000105#define CLEAR_SYS_EXC_INFO() MP_STATE_VM(cur_exception) = NULL;
Paul Sokolovskyae2c81f2015-04-26 01:20:49 +0300106#else
107#define CLEAR_SYS_EXC_INFO()
108#endif
109
Damien George74eb44c2014-12-22 12:49:57 +0000110#define PUSH_EXC_BLOCK(with_or_finally) do { \
Paul Sokolovsky4fff26a2014-03-29 02:49:07 +0200111 DECODE_ULABEL; /* except labels are always forward */ \
112 ++exc_sp; \
Damien Georgecd97a432014-12-02 19:25:10 +0000113 exc_sp->handler = ip + ulab; \
Damien George74eb44c2014-12-22 12:49:57 +0000114 exc_sp->val_sp = MP_TAGPTR_MAKE(sp, ((with_or_finally) << 1) | currently_in_except_block); \
Damien George999cedb2015-11-27 17:01:44 +0000115 exc_sp->prev_exc = NULL; \
Damien Georgecd97a432014-12-02 19:25:10 +0000116 currently_in_except_block = 0; /* in a try block now */ \
117} while (0)
Paul Sokolovsky4fff26a2014-03-29 02:49:07 +0200118
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200119#define POP_EXC_BLOCK() \
Damien George74eb44c2014-12-22 12:49:57 +0000120 currently_in_except_block = MP_TAGPTR_TAG0(exc_sp->val_sp); /* restore previous state */ \
Paul Sokolovskyae2c81f2015-04-26 01:20:49 +0300121 exc_sp--; /* pop back to previous exception handler */ \
122 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 +0200123
Damien George20006db2014-01-18 14:10:48 +0000124// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
125// sp points to bottom of stack which grows up
Damien Georgec8f78bc2014-02-15 22:55:00 +0000126// returns:
127// MP_VM_RETURN_NORMAL, sp valid, return value in *sp
128// MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
129// MP_VM_RETURN_EXCEPTION, exception in fastn[0]
Damien Georgeaabd83e2014-06-07 14:16:08 +0100130mp_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 +0200131#define SELECTIVE_EXC_IP (0)
132#if SELECTIVE_EXC_IP
Damien Georgef89d6592014-12-29 00:29:59 +0000133#define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip; } /* stores ip 1 byte past last opcode */
Paul Sokolovsky74957502014-12-28 07:17:43 +0200134#define MARK_EXC_IP_GLOBAL()
135#else
136#define MARK_EXC_IP_SELECTIVE()
Damien Georgef89d6592014-12-29 00:29:59 +0000137#define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; } /* stores ip pointing to last opcode */
Paul Sokolovsky74957502014-12-28 07:17:43 +0200138#endif
Damien George58ebde42014-05-21 20:32:59 +0100139#if MICROPY_OPT_COMPUTED_GOTO
Damien George51dfcb42015-01-01 20:27:54 +0000140 #include "py/vmentrytable.h"
AZ Huang9309d992014-04-15 15:57:01 +0800141 #define DISPATCH() do { \
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300142 TRACE(ip); \
Paul Sokolovsky74957502014-12-28 07:17:43 +0200143 MARK_EXC_IP_GLOBAL(); \
Damien Georgedb128912014-04-27 18:19:06 +0100144 goto *entry_table[*ip++]; \
Damien George4dea9222015-04-09 15:29:54 +0000145 } while (0)
Damien George124df6f2014-10-25 18:19:55 +0100146 #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
AZ Huang9309d992014-04-15 15:57:01 +0800147 #define ENTRY(op) entry_##op
148 #define ENTRY_DEFAULT entry_default
AZ Huangb1f692e2014-04-14 23:22:44 +0800149#else
AZ Huang9309d992014-04-15 15:57:01 +0800150 #define DISPATCH() break
Damien George124df6f2014-10-25 18:19:55 +0100151 #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check
AZ Huang9309d992014-04-15 15:57:01 +0800152 #define ENTRY(op) case op
153 #define ENTRY_DEFAULT default
AZ Huangb1f692e2014-04-14 23:22:44 +0800154#endif
155
Damien George66ae8c92014-04-17 16:50:23 +0100156 // nlr_raise needs to be implemented as a goto, so that the C compiler's flow analyser
157 // sees that it's possible for us to jump from the dispatch loop to the exception
158 // handler. Without this, the code may have a different stack layout in the dispatch
159 // loop and the exception handler, leading to very obscure bugs.
Damien George999cedb2015-11-27 17:01:44 +0000160 #define RAISE(o) do { nlr_pop(); nlr.ret_val = MP_OBJ_TO_PTR(o); goto exception_handler; } while (0)
Damien429d7192013-10-04 19:53:11 +0100161
Paul Sokolovsky20397572015-03-28 01:14:44 +0200162#if MICROPY_STACKLESS
163run_code_state: ;
164#endif
Damien Georgeaabd83e2014-06-07 14:16:08 +0100165 // Pointers which are constant for particular invocation of mp_execute_bytecode()
stijn36cc84a2015-04-09 11:34:08 +0200166 mp_obj_t * /*const*/ fastn = &code_state->state[code_state->n_state - 1];
167 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 +0300168
Damien George66ae8c92014-04-17 16:50:23 +0100169 // variables that are visible to the exception handler (declared volatile)
Damien George74eb44c2014-12-22 12:49:57 +0000170 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 +0300171 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 +0100172
Damience89a212013-10-15 22:25:17 +0100173 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +0100174 for (;;) {
Damien George66ae8c92014-04-17 16:50:23 +0100175 nlr_buf_t nlr;
Damien George9e6e9352014-03-26 18:37:06 +0000176outer_dispatch_loop:
Damience89a212013-10-15 22:25:17 +0100177 if (nlr_push(&nlr) == 0) {
Damien George66ae8c92014-04-17 16:50:23 +0100178 // local variables that are not visible to the exception handler
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300179 const byte *ip = code_state->ip;
180 mp_obj_t *sp = code_state->sp;
Damien Georged8675542014-05-25 22:58:04 +0100181 mp_obj_t obj_shared;
Damien George66ae8c92014-04-17 16:50:23 +0100182
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200183 // If we have exception to inject, now that we finish setting up
184 // execution context, raise it. This works as if RAISE_VARARGS
185 // bytecode was executed.
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200186 // Injecting exc into yield from generator is a special case,
187 // handled by MP_BC_YIELD_FROM itself
188 if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) {
Damien Georged8675542014-05-25 22:58:04 +0100189 mp_obj_t exc = inject_exc;
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200190 inject_exc = MP_OBJ_NULL;
Damien Georged8675542014-05-25 22:58:04 +0100191 exc = mp_make_raise_obj(exc);
192 RAISE(exc);
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200193 }
Damien George66ae8c92014-04-17 16:50:23 +0100194
Damience89a212013-10-15 22:25:17 +0100195 // loop to execute byte code
196 for (;;) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200197dispatch_loop:
Damien George58ebde42014-05-21 20:32:59 +0100198#if MICROPY_OPT_COMPUTED_GOTO
AZ Huangb1f692e2014-04-14 23:22:44 +0800199 DISPATCH();
200#else
Paul Sokolovskyc5e32c62014-04-23 03:40:24 +0300201 TRACE(ip);
Paul Sokolovsky74957502014-12-28 07:17:43 +0200202 MARK_EXC_IP_GLOBAL();
Damien Georgedb128912014-04-27 18:19:06 +0100203 switch (*ip++) {
AZ Huangb1f692e2014-04-14 23:22:44 +0800204#endif
Damien429d7192013-10-04 19:53:11 +0100205
AZ Huangb1f692e2014-04-14 23:22:44 +0800206 ENTRY(MP_BC_LOAD_CONST_FALSE):
207 PUSH(mp_const_false);
208 DISPATCH();
Damien429d7192013-10-04 19:53:11 +0100209
AZ Huangb1f692e2014-04-14 23:22:44 +0800210 ENTRY(MP_BC_LOAD_CONST_NONE):
211 PUSH(mp_const_none);
212 DISPATCH();
Damien429d7192013-10-04 19:53:11 +0100213
AZ Huangb1f692e2014-04-14 23:22:44 +0800214 ENTRY(MP_BC_LOAD_CONST_TRUE):
215 PUSH(mp_const_true);
216 DISPATCH();
Damien Georgee9906ac2014-01-04 18:44:46 +0000217
AZ Huangb1f692e2014-04-14 23:22:44 +0800218 ENTRY(MP_BC_LOAD_CONST_SMALL_INT): {
Damien George40f3c022014-07-03 13:25:24 +0100219 mp_int_t num = 0;
AZ Huangb1f692e2014-04-14 23:22:44 +0800220 if ((ip[0] & 0x40) != 0) {
221 // Number is negative
222 num--;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200223 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800224 do {
225 num = (num << 7) | (*ip & 0x7f);
226 } while ((*ip++ & 0x80) != 0);
227 PUSH(MP_OBJ_NEW_SMALL_INT(num));
228 DISPATCH();
229 }
Damience89a212013-10-15 22:25:17 +0100230
Damien Georged8675542014-05-25 22:58:04 +0100231 ENTRY(MP_BC_LOAD_CONST_STRING): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800232 DECODE_QSTR;
Damien Georgeed570e42015-06-25 13:58:41 +0000233 PUSH(MP_OBJ_NEW_QSTR(qst));
AZ Huangb1f692e2014-04-14 23:22:44 +0800234 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100235 }
Damience89a212013-10-15 22:25:17 +0100236
Damien Georgedab13852015-01-13 15:55:54 +0000237 ENTRY(MP_BC_LOAD_CONST_OBJ): {
Damien George999cedb2015-11-27 17:01:44 +0000238 DECODE_OBJ;
239 PUSH(obj);
Damien Georgedab13852015-01-13 15:55:54 +0000240 DISPATCH();
241 }
242
AZ Huangb1f692e2014-04-14 23:22:44 +0800243 ENTRY(MP_BC_LOAD_NULL):
244 PUSH(MP_OBJ_NULL);
245 DISPATCH();
Damien George523b5752014-03-31 11:59:23 +0100246
Damien Georgecd97a432014-12-02 19:25:10 +0000247 ENTRY(MP_BC_LOAD_FAST_N): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800248 DECODE_UINT;
Damien Georged8675542014-05-25 22:58:04 +0100249 obj_shared = fastn[-unum];
AZ Huangb1f692e2014-04-14 23:22:44 +0800250 load_check:
Damien Georged8675542014-05-25 22:58:04 +0100251 if (obj_shared == MP_OBJ_NULL) {
252 local_name_error: {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200253 MARK_EXC_IP_SELECTIVE();
Damien Georged8675542014-05-25 22:58:04 +0100254 mp_obj_t obj = mp_obj_new_exception_msg(&mp_type_NameError, "local variable referenced before assignment");
255 RAISE(obj);
256 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800257 }
Damien Georged8675542014-05-25 22:58:04 +0100258 PUSH(obj_shared);
AZ Huangb1f692e2014-04-14 23:22:44 +0800259 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000260 }
Damien George2bf7c092014-04-09 15:26:46 +0100261
Damien Georgecd97a432014-12-02 19:25:10 +0000262 ENTRY(MP_BC_LOAD_DEREF): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800263 DECODE_UINT;
Damien Georged8675542014-05-25 22:58:04 +0100264 obj_shared = mp_obj_cell_get(fastn[-unum]);
AZ Huangb1f692e2014-04-14 23:22:44 +0800265 goto load_check;
Damien Georgecd97a432014-12-02 19:25:10 +0000266 }
Damien9ecbcff2013-12-11 00:41:43 +0000267
Damien George7ee91cf2015-01-06 12:51:39 +0000268 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100269 ENTRY(MP_BC_LOAD_NAME): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200270 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800271 DECODE_QSTR;
272 PUSH(mp_load_name(qst));
273 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100274 }
Damien George7ee91cf2015-01-06 12:51:39 +0000275 #else
276 ENTRY(MP_BC_LOAD_NAME): {
277 MARK_EXC_IP_SELECTIVE();
278 DECODE_QSTR;
279 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
280 mp_uint_t x = *ip;
281 if (x < MP_STATE_CTX(dict_locals)->map.alloc && MP_STATE_CTX(dict_locals)->map.table[x].key == key) {
282 PUSH(MP_STATE_CTX(dict_locals)->map.table[x].value);
283 } else {
284 mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_locals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
285 if (elem != NULL) {
286 *(byte*)ip = (elem - &MP_STATE_CTX(dict_locals)->map.table[0]) & 0xff;
287 PUSH(elem->value);
288 } else {
289 PUSH(mp_load_name(MP_OBJ_QSTR_VALUE(key)));
290 }
291 }
292 ip++;
293 DISPATCH();
294 }
295 #endif
Damience89a212013-10-15 22:25:17 +0100296
Damien George7ee91cf2015-01-06 12:51:39 +0000297 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100298 ENTRY(MP_BC_LOAD_GLOBAL): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200299 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800300 DECODE_QSTR;
301 PUSH(mp_load_global(qst));
302 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100303 }
Damien George7ee91cf2015-01-06 12:51:39 +0000304 #else
305 ENTRY(MP_BC_LOAD_GLOBAL): {
306 MARK_EXC_IP_SELECTIVE();
307 DECODE_QSTR;
308 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
309 mp_uint_t x = *ip;
310 if (x < MP_STATE_CTX(dict_globals)->map.alloc && MP_STATE_CTX(dict_globals)->map.table[x].key == key) {
311 PUSH(MP_STATE_CTX(dict_globals)->map.table[x].value);
312 } else {
313 mp_map_elem_t *elem = mp_map_lookup(&MP_STATE_CTX(dict_globals)->map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
314 if (elem != NULL) {
315 *(byte*)ip = (elem - &MP_STATE_CTX(dict_globals)->map.table[0]) & 0xff;
316 PUSH(elem->value);
317 } else {
318 PUSH(mp_load_global(MP_OBJ_QSTR_VALUE(key)));
319 }
320 }
321 ip++;
322 DISPATCH();
323 }
324 #endif
Damience89a212013-10-15 22:25:17 +0100325
Damien George7ee91cf2015-01-06 12:51:39 +0000326 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100327 ENTRY(MP_BC_LOAD_ATTR): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200328 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800329 DECODE_QSTR;
330 SET_TOP(mp_load_attr(TOP(), qst));
331 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100332 }
Damien George7ee91cf2015-01-06 12:51:39 +0000333 #else
334 ENTRY(MP_BC_LOAD_ATTR): {
335 MARK_EXC_IP_SELECTIVE();
336 DECODE_QSTR;
337 mp_obj_t top = TOP();
Damien Georgeb1bbe962015-04-01 14:10:50 +0000338 if (mp_obj_get_type(top)->attr == mp_obj_instance_attr) {
Damien George999cedb2015-11-27 17:01:44 +0000339 mp_obj_instance_t *self = MP_OBJ_TO_PTR(top);
Damien George7ee91cf2015-01-06 12:51:39 +0000340 mp_uint_t x = *ip;
341 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
342 mp_map_elem_t *elem;
343 if (x < self->members.alloc && self->members.table[x].key == key) {
344 elem = &self->members.table[x];
345 } else {
346 elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
347 if (elem != NULL) {
348 *(byte*)ip = elem - &self->members.table[0];
349 } else {
350 goto load_attr_cache_fail;
351 }
352 }
353 SET_TOP(elem->value);
354 ip++;
355 DISPATCH();
356 }
357 load_attr_cache_fail:
358 SET_TOP(mp_load_attr(top, qst));
359 ip++;
360 DISPATCH();
361 }
362 #endif
Damience89a212013-10-15 22:25:17 +0100363
Damien Georged8675542014-05-25 22:58:04 +0100364 ENTRY(MP_BC_LOAD_METHOD): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200365 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800366 DECODE_QSTR;
367 mp_load_method(*sp, qst, sp);
368 sp += 1;
369 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100370 }
Damience89a212013-10-15 22:25:17 +0100371
AZ Huangb1f692e2014-04-14 23:22:44 +0800372 ENTRY(MP_BC_LOAD_BUILD_CLASS):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200373 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800374 PUSH(mp_load_build_class());
375 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100376
Damien Georged8675542014-05-25 22:58:04 +0100377 ENTRY(MP_BC_LOAD_SUBSCR): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200378 MARK_EXC_IP_SELECTIVE();
Damien Georged8675542014-05-25 22:58:04 +0100379 mp_obj_t index = POP();
380 SET_TOP(mp_obj_subscr(TOP(), index, MP_OBJ_SENTINEL));
Damien George729f7b42014-04-17 22:10:53 +0100381 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100382 }
Damien George729f7b42014-04-17 22:10:53 +0100383
Damien Georgecd97a432014-12-02 19:25:10 +0000384 ENTRY(MP_BC_STORE_FAST_N): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800385 DECODE_UINT;
386 fastn[-unum] = POP();
387 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000388 }
Damience89a212013-10-15 22:25:17 +0100389
Damien Georgecd97a432014-12-02 19:25:10 +0000390 ENTRY(MP_BC_STORE_DEREF): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800391 DECODE_UINT;
392 mp_obj_cell_set(fastn[-unum], POP());
393 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000394 }
Damien9ecbcff2013-12-11 00:41:43 +0000395
Damien Georged8675542014-05-25 22:58:04 +0100396 ENTRY(MP_BC_STORE_NAME): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200397 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800398 DECODE_QSTR;
399 mp_store_name(qst, POP());
400 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100401 }
Damience89a212013-10-15 22:25:17 +0100402
Damien Georged8675542014-05-25 22:58:04 +0100403 ENTRY(MP_BC_STORE_GLOBAL): {
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_global(qst, POP());
407 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100408 }
Damien6addc892013-11-04 23:04:50 +0000409
Damien George7ee91cf2015-01-06 12:51:39 +0000410 #if !MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
Damien Georged8675542014-05-25 22:58:04 +0100411 ENTRY(MP_BC_STORE_ATTR): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200412 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800413 DECODE_QSTR;
414 mp_store_attr(sp[0], qst, sp[-1]);
415 sp -= 2;
416 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100417 }
Damien George7ee91cf2015-01-06 12:51:39 +0000418 #else
stijn28fa84b2015-02-14 18:43:54 +0100419 // This caching code works with MICROPY_PY_BUILTINS_PROPERTY and/or
420 // MICROPY_PY_DESCRIPTORS enabled because if the attr exists in
421 // self->members then it can't be a property or have descriptors. A
Damien George7ee91cf2015-01-06 12:51:39 +0000422 // consequence of this is that we can't use MP_MAP_LOOKUP_ADD_IF_NOT_FOUND
423 // in the fast-path below, because that store could override a property.
424 ENTRY(MP_BC_STORE_ATTR): {
425 MARK_EXC_IP_SELECTIVE();
426 DECODE_QSTR;
427 mp_obj_t top = TOP();
Damien Georgeb1bbe962015-04-01 14:10:50 +0000428 if (mp_obj_get_type(top)->attr == mp_obj_instance_attr && sp[-1] != MP_OBJ_NULL) {
Damien George999cedb2015-11-27 17:01:44 +0000429 mp_obj_instance_t *self = MP_OBJ_TO_PTR(top);
Damien George7ee91cf2015-01-06 12:51:39 +0000430 mp_uint_t x = *ip;
431 mp_obj_t key = MP_OBJ_NEW_QSTR(qst);
432 mp_map_elem_t *elem;
433 if (x < self->members.alloc && self->members.table[x].key == key) {
434 elem = &self->members.table[x];
435 } else {
436 elem = mp_map_lookup(&self->members, key, MP_MAP_LOOKUP);
437 if (elem != NULL) {
438 *(byte*)ip = elem - &self->members.table[0];
439 } else {
440 goto store_attr_cache_fail;
441 }
442 }
443 elem->value = sp[-1];
444 sp -= 2;
445 ip++;
446 DISPATCH();
447 }
448 store_attr_cache_fail:
449 mp_store_attr(sp[0], qst, sp[-1]);
450 sp -= 2;
451 ip++;
452 DISPATCH();
453 }
454 #endif
Damience89a212013-10-15 22:25:17 +0100455
AZ Huangb1f692e2014-04-14 23:22:44 +0800456 ENTRY(MP_BC_STORE_SUBSCR):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200457 MARK_EXC_IP_SELECTIVE();
Damien George729f7b42014-04-17 22:10:53 +0100458 mp_obj_subscr(sp[-1], sp[0], sp[-2]);
AZ Huangb1f692e2014-04-14 23:22:44 +0800459 sp -= 3;
460 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100461
Damien Georgecd97a432014-12-02 19:25:10 +0000462 ENTRY(MP_BC_DELETE_FAST): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200463 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800464 DECODE_UINT;
465 if (fastn[-unum] == MP_OBJ_NULL) {
466 goto local_name_error;
467 }
468 fastn[-unum] = MP_OBJ_NULL;
469 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000470 }
Damien George2bf7c092014-04-09 15:26:46 +0100471
Damien Georgecd97a432014-12-02 19:25:10 +0000472 ENTRY(MP_BC_DELETE_DEREF): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200473 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800474 DECODE_UINT;
475 if (mp_obj_cell_get(fastn[-unum]) == MP_OBJ_NULL) {
476 goto local_name_error;
477 }
478 mp_obj_cell_set(fastn[-unum], MP_OBJ_NULL);
479 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000480 }
Damien George2bf7c092014-04-09 15:26:46 +0100481
Damien Georged8675542014-05-25 22:58:04 +0100482 ENTRY(MP_BC_DELETE_NAME): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200483 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800484 DECODE_QSTR;
485 mp_delete_name(qst);
486 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100487 }
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200488
Damien Georged8675542014-05-25 22:58:04 +0100489 ENTRY(MP_BC_DELETE_GLOBAL): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200490 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800491 DECODE_QSTR;
492 mp_delete_global(qst);
493 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100494 }
Damien George1d24ea52014-04-08 21:11:49 +0100495
Damien Georged8675542014-05-25 22:58:04 +0100496 ENTRY(MP_BC_DUP_TOP): {
497 mp_obj_t top = TOP();
498 PUSH(top);
AZ Huangb1f692e2014-04-14 23:22:44 +0800499 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100500 }
Damience89a212013-10-15 22:25:17 +0100501
AZ Huangb1f692e2014-04-14 23:22:44 +0800502 ENTRY(MP_BC_DUP_TOP_TWO):
503 sp += 2;
504 sp[0] = sp[-2];
505 sp[-1] = sp[-3];
506 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100507
AZ Huangb1f692e2014-04-14 23:22:44 +0800508 ENTRY(MP_BC_POP_TOP):
509 sp -= 1;
510 DISPATCH();
Damience89a212013-10-15 22:25:17 +0100511
Damien Georged8675542014-05-25 22:58:04 +0100512 ENTRY(MP_BC_ROT_TWO): {
513 mp_obj_t top = sp[0];
AZ Huangb1f692e2014-04-14 23:22:44 +0800514 sp[0] = sp[-1];
Damien Georged8675542014-05-25 22:58:04 +0100515 sp[-1] = top;
AZ Huangb1f692e2014-04-14 23:22:44 +0800516 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100517 }
Damien4ebb32f2013-11-02 14:33:10 +0000518
Damien Georged8675542014-05-25 22:58:04 +0100519 ENTRY(MP_BC_ROT_THREE): {
520 mp_obj_t top = sp[0];
AZ Huangb1f692e2014-04-14 23:22:44 +0800521 sp[0] = sp[-1];
522 sp[-1] = sp[-2];
Damien Georged8675542014-05-25 22:58:04 +0100523 sp[-2] = top;
AZ Huangb1f692e2014-04-14 23:22:44 +0800524 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100525 }
Damience89a212013-10-15 22:25:17 +0100526
Damien Georgecd97a432014-12-02 19:25:10 +0000527 ENTRY(MP_BC_JUMP): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800528 DECODE_SLABEL;
Damien Georgecd97a432014-12-02 19:25:10 +0000529 ip += slab;
Damien George124df6f2014-10-25 18:19:55 +0100530 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000531 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800532
Damien Georgecd97a432014-12-02 19:25:10 +0000533 ENTRY(MP_BC_POP_JUMP_IF_TRUE): {
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;
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200537 }
Damien George124df6f2014-10-25 18:19:55 +0100538 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000539 }
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200540
Damien Georgecd97a432014-12-02 19:25:10 +0000541 ENTRY(MP_BC_POP_JUMP_IF_FALSE): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800542 DECODE_SLABEL;
543 if (!mp_obj_is_true(POP())) {
Damien Georgecd97a432014-12-02 19:25:10 +0000544 ip += slab;
AZ Huangb1f692e2014-04-14 23:22:44 +0800545 }
Damien George124df6f2014-10-25 18:19:55 +0100546 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000547 }
Damien George600ae732014-01-21 23:48:04 +0000548
Damien Georgecd97a432014-12-02 19:25:10 +0000549 ENTRY(MP_BC_JUMP_IF_TRUE_OR_POP): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800550 DECODE_SLABEL;
551 if (mp_obj_is_true(TOP())) {
Damien Georgecd97a432014-12-02 19:25:10 +0000552 ip += slab;
AZ Huangb1f692e2014-04-14 23:22:44 +0800553 } else {
554 sp--;
555 }
Damien George124df6f2014-10-25 18:19:55 +0100556 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000557 }
Damience89a212013-10-15 22:25:17 +0100558
Damien Georgecd97a432014-12-02 19:25:10 +0000559 ENTRY(MP_BC_JUMP_IF_FALSE_OR_POP): {
AZ Huangb1f692e2014-04-14 23:22:44 +0800560 DECODE_SLABEL;
561 if (mp_obj_is_true(TOP())) {
562 sp--;
563 } else {
Damien Georgecd97a432014-12-02 19:25:10 +0000564 ip += slab;
AZ Huangb1f692e2014-04-14 23:22:44 +0800565 }
Damien George124df6f2014-10-25 18:19:55 +0100566 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000567 }
Damience89a212013-10-15 22:25:17 +0100568
Damien Georged8675542014-05-25 22:58:04 +0100569 ENTRY(MP_BC_SETUP_WITH): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200570 MARK_EXC_IP_SELECTIVE();
Damien George8c1d23a2015-04-24 01:52:28 +0100571 // stack: (..., ctx_mgr)
Damien Georged8675542014-05-25 22:58:04 +0100572 mp_obj_t obj = TOP();
Damien George8c1d23a2015-04-24 01:52:28 +0100573 mp_load_method(obj, MP_QSTR___exit__, sp);
574 mp_load_method(obj, MP_QSTR___enter__, sp + 2);
575 mp_obj_t ret = mp_call_method_n_kw(0, 0, sp + 2);
576 sp += 1;
Damien George74eb44c2014-12-22 12:49:57 +0000577 PUSH_EXC_BLOCK(1);
Damien Georged8675542014-05-25 22:58:04 +0100578 PUSH(ret);
Damien George8c1d23a2015-04-24 01:52:28 +0100579 // stack: (..., __exit__, ctx_mgr, as_value)
AZ Huangb1f692e2014-04-14 23:22:44 +0800580 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100581 }
Damience89a212013-10-15 22:25:17 +0100582
AZ Huangb1f692e2014-04-14 23:22:44 +0800583 ENTRY(MP_BC_WITH_CLEANUP): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200584 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800585 // Arriving here, there's "exception control block" on top of stack,
Damien George8c1d23a2015-04-24 01:52:28 +0100586 // and __exit__ method (with self) underneath it. Bytecode calls __exit__,
AZ Huangb1f692e2014-04-14 23:22:44 +0800587 // and "deletes" it off stack, shifting "exception control block"
588 // to its place.
AZ Huangb1f692e2014-04-14 23:22:44 +0800589 if (TOP() == mp_const_none) {
Damien George8c1d23a2015-04-24 01:52:28 +0100590 // stack: (..., __exit__, ctx_mgr, None)
591 sp[1] = mp_const_none;
592 sp[2] = mp_const_none;
593 sp -= 2;
594 mp_call_method_n_kw(3, 0, sp);
AZ Huangb1f692e2014-04-14 23:22:44 +0800595 SET_TOP(mp_const_none);
AZ Huangb1f692e2014-04-14 23:22:44 +0800596 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
Damien George5e1d9932015-03-25 22:20:37 +0000597 mp_int_t cause_val = MP_OBJ_SMALL_INT_VALUE(TOP());
598 if (cause_val == UNWIND_RETURN) {
Damien George8c1d23a2015-04-24 01:52:28 +0100599 // stack: (..., __exit__, ctx_mgr, ret_val, UNWIND_RETURN)
600 mp_obj_t ret_val = sp[-1];
601 sp[-1] = mp_const_none;
602 sp[0] = mp_const_none;
603 sp[1] = mp_const_none;
604 mp_call_method_n_kw(3, 0, sp - 3);
605 sp[-3] = ret_val;
606 sp[-2] = MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN);
Damien George5e1d9932015-03-25 22:20:37 +0000607 } else {
608 assert(cause_val == UNWIND_JUMP);
Damien George8c1d23a2015-04-24 01:52:28 +0100609 // stack: (..., __exit__, ctx_mgr, dest_ip, num_exc, UNWIND_JUMP)
610 mp_obj_t dest_ip = sp[-2];
611 mp_obj_t num_exc = sp[-1];
612 sp[-2] = mp_const_none;
613 sp[-1] = mp_const_none;
614 sp[0] = mp_const_none;
615 mp_call_method_n_kw(3, 0, sp - 4);
616 sp[-4] = dest_ip;
617 sp[-3] = num_exc;
618 sp[-2] = MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP);
AZ Huangb1f692e2014-04-14 23:22:44 +0800619 }
Damien George8c1d23a2015-04-24 01:52:28 +0100620 sp -= 2; // we removed (__exit__, ctx_mgr)
Damien George5e1d9932015-03-25 22:20:37 +0000621 } else {
622 assert(mp_obj_is_exception_type(TOP()));
Damien George8c1d23a2015-04-24 01:52:28 +0100623 // stack: (..., __exit__, ctx_mgr, traceback, exc_val, exc_type)
Damien George596f41d2015-02-10 13:21:42 +0000624 // Need to pass (sp[0], sp[-1], sp[-2]) as arguments so must reverse the
625 // order of these on the value stack (don't want to create a temporary
626 // array because it increases stack footprint of the VM).
627 mp_obj_t obj = sp[-2];
628 sp[-2] = sp[0];
629 sp[0] = obj;
Damien George8c1d23a2015-04-24 01:52:28 +0100630 mp_obj_t ret_value = mp_call_method_n_kw(3, 0, sp - 4);
Damien Georged8675542014-05-25 22:58:04 +0100631 if (mp_obj_is_true(ret_value)) {
Damien George8c1d23a2015-04-24 01:52:28 +0100632 // We need to silence/swallow the exception. This is done
633 // by popping the exception and the __exit__ handler and
634 // replacing it with None, which signals END_FINALLY to just
635 // execute the finally handler normally.
Damien George596f41d2015-02-10 13:21:42 +0000636 sp -= 4;
Damien George8c1d23a2015-04-24 01:52:28 +0100637 SET_TOP(mp_const_none);
AZ Huangb1f692e2014-04-14 23:22:44 +0800638 assert(exc_sp >= exc_stack);
AZ Huangb1f692e2014-04-14 23:22:44 +0800639 POP_EXC_BLOCK();
Damien George596f41d2015-02-10 13:21:42 +0000640 } else {
Damien George8c1d23a2015-04-24 01:52:28 +0100641 // We need to re-raise the exception. We pop __exit__ handler
642 // and copy the 3 exception values down (remembering that they
643 // are reversed due to above code).
644 sp[-4] = sp[0];
645 sp[-3] = sp[-1];
646 sp -= 2;
AZ Huangb1f692e2014-04-14 23:22:44 +0800647 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800648 }
649 DISPATCH();
650 }
Damienc226dca2013-10-16 16:12:52 +0100651
Damien Georgecd97a432014-12-02 19:25:10 +0000652 ENTRY(MP_BC_UNWIND_JUMP): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200653 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800654 DECODE_SLABEL;
Damien George999cedb2015-11-27 17:01:44 +0000655 PUSH((mp_obj_t)(mp_uint_t)(uintptr_t)(ip + slab)); // push destination ip for jump
656 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)
Damien Georgecd97a432014-12-02 19:25:10 +0000657unwind_jump:;
658 mp_uint_t unum = (mp_uint_t)POP(); // get number of exception handlers to unwind
Damien George25c84642014-05-30 15:20:41 +0100659 while ((unum & 0x7f) > 0) {
AZ Huangb1f692e2014-04-14 23:22:44 +0800660 unum -= 1;
661 assert(exc_sp >= exc_stack);
Damien George74eb44c2014-12-22 12:49:57 +0000662 if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
Damien George4bf3f2d2015-10-15 17:48:28 +0100663 // Getting here the stack looks like:
664 // (..., X, dest_ip)
665 // where X is pointed to by exc_sp->val_sp and in the case
666 // of a "with" block contains the context manager info.
AZ Huangb1f692e2014-04-14 23:22:44 +0800667 // We're going to run "finally" code as a coroutine
668 // (not calling it recursively). Set up a sentinel
669 // on a stack so it can return back to us when it is
Damien George4bf3f2d2015-10-15 17:48:28 +0100670 // done (when WITH_CLEANUP or END_FINALLY reached).
Damien George999cedb2015-11-27 17:01:44 +0000671 PUSH((mp_obj_t)unum); // push number of exception handlers left to unwind
AZ Huangb1f692e2014-04-14 23:22:44 +0800672 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP)); // push sentinel
673 ip = exc_sp->handler; // get exception handler byte code address
674 exc_sp--; // pop exception handler
675 goto dispatch_loop; // run the exception handler
676 }
677 exc_sp--;
678 }
Damien George999cedb2015-11-27 17:01:44 +0000679 ip = (const byte*)MP_OBJ_TO_PTR(POP()); // pop destination ip for jump
Damien George25c84642014-05-30 15:20:41 +0100680 if (unum != 0) {
681 sp--;
682 }
Damien George124df6f2014-10-25 18:19:55 +0100683 DISPATCH_WITH_PEND_EXC_CHECK();
Damien Georgecd97a432014-12-02 19:25:10 +0000684 }
Damience89a212013-10-15 22:25:17 +0100685
AZ Huangb1f692e2014-04-14 23:22:44 +0800686 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
687 ENTRY(MP_BC_SETUP_EXCEPT):
Damien Georgecd97a432014-12-02 19:25:10 +0000688 ENTRY(MP_BC_SETUP_FINALLY): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200689 MARK_EXC_IP_SELECTIVE();
Damien Georgef89d6592014-12-29 00:29:59 +0000690 #if SELECTIVE_EXC_IP
691 PUSH_EXC_BLOCK((code_state->ip[-1] == MP_BC_SETUP_FINALLY) ? 1 : 0);
692 #else
693 PUSH_EXC_BLOCK((code_state->ip[0] == MP_BC_SETUP_FINALLY) ? 1 : 0);
694 #endif
AZ Huangb1f692e2014-04-14 23:22:44 +0800695 DISPATCH();
Damien Georgecd97a432014-12-02 19:25:10 +0000696 }
Damience89a212013-10-15 22:25:17 +0100697
AZ Huangb1f692e2014-04-14 23:22:44 +0800698 ENTRY(MP_BC_END_FINALLY):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200699 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800700 // not fully implemented
701 // if TOS is an exception, reraises the exception (3 values on TOS)
702 // if TOS is None, just pops it and continues
703 // if TOS is an integer, does something else
704 // else error
705 if (mp_obj_is_exception_type(TOP())) {
Damien George66ae8c92014-04-17 16:50:23 +0100706 RAISE(sp[-1]);
AZ Huangb1f692e2014-04-14 23:22:44 +0800707 }
708 if (TOP() == mp_const_none) {
Damien George20006db2014-01-18 14:10:48 +0000709 sp--;
Damien George5e1d9932015-03-25 22:20:37 +0000710 } else {
711 assert(MP_OBJ_IS_SMALL_INT(TOP()));
AZ Huangb1f692e2014-04-14 23:22:44 +0800712 // We finished "finally" coroutine and now dispatch back
713 // to our caller, based on TOS value
714 mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
Damien George5e1d9932015-03-25 22:20:37 +0000715 if (reason == UNWIND_RETURN) {
716 goto unwind_return;
717 } else {
718 assert(reason == UNWIND_JUMP);
719 goto unwind_jump;
AZ Huangb1f692e2014-04-14 23:22:44 +0800720 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800721 }
722 DISPATCH();
723
724 ENTRY(MP_BC_GET_ITER):
Paul Sokolovsky74957502014-12-28 07:17:43 +0200725 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800726 SET_TOP(mp_getiter(TOP()));
727 DISPATCH();
728
Damien Georged8675542014-05-25 22:58:04 +0100729 ENTRY(MP_BC_FOR_ITER): {
Paul Sokolovsky74957502014-12-28 07:17:43 +0200730 MARK_EXC_IP_SELECTIVE();
AZ Huangb1f692e2014-04-14 23:22:44 +0800731 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien Georgec60a2612014-06-01 12:32:28 +0100732 code_state->sp = sp;
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +0300733 assert(TOP());
Damien Georged8675542014-05-25 22:58:04 +0100734 mp_obj_t value = mp_iternext_allow_raise(TOP());
735 if (value == MP_OBJ_STOP_ITERATION) {
AZ Huangb1f692e2014-04-14 23:22:44 +0800736 --sp; // pop the exhausted iterator
Damien Georgecd97a432014-12-02 19:25:10 +0000737 ip += ulab; // jump to after for-block
AZ Huangb1f692e2014-04-14 23:22:44 +0800738 } else {
Damien Georged8675542014-05-25 22:58:04 +0100739 PUSH(value); // push the next iteration value
AZ Huangb1f692e2014-04-14 23:22:44 +0800740 }
741 DISPATCH();
Damien Georged8675542014-05-25 22:58:04 +0100742 }
AZ Huangb1f692e2014-04-14 23:22:44 +0800743
744 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
745 ENTRY(MP_BC_POP_BLOCK):
746 // we are exiting an exception handler, so pop the last one of the exception-stack
747 assert(exc_sp >= exc_stack);
748 POP_EXC_BLOCK();
749 DISPATCH();
750
751 // matched against: SETUP_EXCEPT
752 ENTRY(MP_BC_POP_EXCEPT):
753 // TODO need to work out how blocks work etc
754 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
755 assert(exc_sp >= exc_stack);
756 assert(currently_in_except_block);
757 //sp = (mp_obj_t*)(*exc_sp--);
758 //exc_sp--; // discard ip
759 POP_EXC_BLOCK();
760 //sp -= 3; // pop 3 exception values
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;
Damien George7a30e872015-12-17 12:32:41 +0000976 int adjust = (sp[1] == MP_OBJ_NULL) ? 0 : 1;
Paul Sokolovsky390e9262015-03-28 01:14:44 +0200977
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--) {
Damien George999cedb2015-11-27 17:01:44 +00001088 if (e->prev_exc != NULL) {
1089 obj = MP_OBJ_FROM_PTR(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 George999cedb2015-11-27 17:01:44 +00001116#define GENERATOR_EXIT_IF_NEEDED(t) if (t != MP_OBJ_NULL && EXC_MATCH(t, MP_OBJ_FROM_PTR(&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 George999cedb2015-11-27 17:01:44 +00001153 if (EXC_MATCH(ret_value, MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
Damien Georged8675542014-05-25 22:58:04 +01001154 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 Georgebdbe8c92015-12-08 12:28:11 +00001225 } else if (ip[-1] < MP_BC_UNARY_OP_MULTI + 7) {
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
Damien George999cedb2015-11-27 17:01:44 +00001269 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))) {
Paul Sokolovskya7c02c42015-05-10 17:18:10 +03001270 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
Damien George999cedb2015-11-27 17:01:44 +00001281 *++code_state->sp = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(nlr.ret_val));
Paul Sokolovsky6738c1d2015-05-11 02:59:25 +03001282 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)
Damien George999cedb2015-11-27 17:01:44 +00001295 if (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 George999cedb2015-11-27 17:01:44 +00001330 mp_obj_exception_add_traceback(MP_OBJ_FROM_PTR(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);
Damien George999cedb2015-11-27 17:01:44 +00001356 PUSH(MP_OBJ_FROM_PTR(nlr.ret_val));
1357 PUSH(MP_OBJ_FROM_PTR(((mp_obj_base_t*)nlr.ret_val)->type));
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
Damien George999cedb2015-11-27 17:01:44 +00001375 fastn[0] = MP_OBJ_FROM_PTR(nlr.ret_val); // must put exception here because sp is invalid
Damien Georgec8f78bc2014-02-15 22:55:00 +00001376 return MP_VM_RETURN_EXCEPTION;
Damience89a212013-10-15 22:25:17 +01001377 }
Damien429d7192013-10-04 19:53:11 +01001378 }
1379 }
1380}