blob: 84bd4172dcbd741591cc221f3621aa86d9b0c917 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
Damien429d7192013-10-04 19:53:11 +01002#include <assert.h>
3
Damience89a212013-10-15 22:25:17 +01004#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01005#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00006#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "obj.h"
Damien429d7192013-10-04 19:53:11 +01009#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "bc0.h"
Damieneb19efb2013-10-10 22:06:54 +010011#include "bc.h"
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +020012#include "objgenerator.h"
Damien429d7192013-10-04 19:53:11 +010013
Paul Sokolovsky85193422014-01-31 19:45:15 +020014// Value stack grows up (this makes it incompatible with native C stack, but
15// makes sure that arguments to functions are in natural order arg1..argN
16// (Python semantics mandates left-to-right evaluation order, including for
17// function arguments). Stack pointer is pre-incremented and points at the
18// top element.
19// Exception stack also grows up, top element is also pointed at.
20
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020021// Exception stack unwind reasons (WHY_* in CPython-speak)
Damien Georgecbddb272014-02-01 20:08:18 +000022// TODO perhaps compress this to RETURN=0, JUMP>0, with number of unwinds
23// left to do encoded in the JUMP number
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020024typedef enum {
25 UNWIND_RETURN = 1,
Damien Georgecbddb272014-02-01 20:08:18 +000026 UNWIND_JUMP,
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020027} mp_unwind_reason_t;
28
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020029#define DECODE_UINT { \
30 unum = 0; \
31 do { \
32 unum = (unum << 7) + (*ip & 0x7f); \
33 } while ((*ip++ & 0x80) != 0); \
34}
Damien03c9cfb2013-11-05 22:06:08 +000035#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
36#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020037#define DECODE_QSTR { \
38 qst = 0; \
39 do { \
40 qst = (qst << 7) + (*ip & 0x7f); \
41 } while ((*ip++ & 0x80) != 0); \
42}
Damien George20006db2014-01-18 14:10:48 +000043#define PUSH(val) *++sp = (val)
44#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000045#define TOP() (*sp)
46#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010047
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +020048#define PUSH_EXC_BLOCK() \
Paul Sokolovsky4fff26a2014-03-29 02:49:07 +020049 DECODE_ULABEL; /* except labels are always forward */ \
50 ++exc_sp; \
51 exc_sp->opcode = op; \
52 exc_sp->handler = ip + unum; \
53 exc_sp->val_sp = MP_TAGPTR_MAKE(sp, currently_in_except_block); \
54 currently_in_except_block = 0; /* in a try block now */
55
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +020056#define POP_EXC_BLOCK() \
57 currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); /* restore previous state */ \
58 exc_sp--; /* pop back to previous exception handler */
59
Damien Georgebee17b02014-03-27 11:07:04 +000060mp_vm_return_kind_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, const mp_obj_t *args2, uint n_args2, mp_obj_t *ret) {
Damien George8dcc0c72014-03-27 10:55:21 +000061 const byte *ip = code;
62
63 // get code info size, and skip line number table
64 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
65 ip += code_info_size;
66
Damien Georgebee17b02014-03-27 11:07:04 +000067 // bytecode prelude: state size and exception stack size; 16 bit uints
68 machine_uint_t n_state = ip[0] | (ip[1] << 8);
69 machine_uint_t n_exc_stack = ip[2] | (ip[3] << 8);
70 ip += 4;
Damien George8dcc0c72014-03-27 10:55:21 +000071
Damien George20006db2014-01-18 14:10:48 +000072 // allocate state for locals and stack
73 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000074 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000075 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000076 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000077 }
Damien George20006db2014-01-18 14:10:48 +000078 mp_obj_t *sp = &state[0] - 1;
79
Damien George8dcc0c72014-03-27 10:55:21 +000080 // allocate state for exceptions
81 mp_exc_stack exc_state[4];
82 mp_exc_stack *exc_stack = &exc_state[0];
83 if (n_exc_stack > 4) {
84 exc_stack = m_new(mp_exc_stack, n_exc_stack);
85 }
86 mp_exc_stack *exc_sp = &exc_stack[0] - 1;
87
Damienbd254452013-10-16 20:39:12 +010088 // init args
Damien Georgefb083ea2014-02-01 18:29:40 +000089 for (uint i = 0; i < n_args; i++) {
Damien George20006db2014-01-18 14:10:48 +000090 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010091 }
Damien Georgefb083ea2014-02-01 18:29:40 +000092 for (uint i = 0; i < n_args2; i++) {
93 state[n_state - 1 - n_args - i] = args2[i];
94 }
Damien George08335002014-01-18 23:24:36 +000095
Damien George8dcc0c72014-03-27 10:55:21 +000096 // bytecode prelude: initialise closed over variables
97 for (uint n_local = *ip++; n_local > 0; n_local--) {
98 uint local_num = *ip++;
99 if (local_num < n_args + n_args2) {
100 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
101 } else {
102 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +0000103 }
104 }
105
106 // execute the byte code
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200107 mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp, exc_stack, &exc_sp, MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +0000108
Damien Georgec8f78bc2014-02-15 22:55:00 +0000109 switch (vm_return_kind) {
110 case MP_VM_RETURN_NORMAL:
111 *ret = *sp;
112 return MP_VM_RETURN_NORMAL;
113 case MP_VM_RETURN_EXCEPTION:
114 *ret = state[n_state - 1];
115 return MP_VM_RETURN_EXCEPTION;
116 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
117 default:
118 assert(0);
119 *ret = mp_const_none;
120 return MP_VM_RETURN_NORMAL;
121 }
Damienbd254452013-10-16 20:39:12 +0100122}
123
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]
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200130mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out,
131 mp_obj_t *fastn, mp_obj_t **sp_in_out,
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200132 mp_exc_stack *exc_stack, mp_exc_stack **exc_sp_in_out,
133 volatile mp_obj_t inject_exc) {
Damienc9f91972013-10-15 23:46:01 +0100134 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
135
Damienbd254452013-10-16 20:39:12 +0100136 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +0000137 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +0100138 machine_uint_t unum;
Damien Georgecbd2f742014-01-19 11:48:48 +0000139 qstr qst;
Damiend99b0522013-12-21 18:17:45 +0000140 mp_obj_t obj1, obj2;
Damience89a212013-10-15 22:25:17 +0100141 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +0100142
Paul Sokolovsky16734202014-03-22 23:20:07 +0200143 volatile bool currently_in_except_block = MP_TAGPTR_TAG(*exc_sp_in_out); // 0 or 1, to detect nested exceptions
144 mp_exc_stack *volatile exc_sp = MP_TAGPTR_PTR(*exc_sp_in_out); // stack grows up, exc_sp points to top of stack
Damien George08335002014-01-18 23:24:36 +0000145 const byte *volatile save_ip = ip; // this is so we can access ip in the exception handler without making ip volatile (which means the compiler can't keep it in a register in the main loop)
Damienc9f91972013-10-15 23:46:01 +0100146
Damience89a212013-10-15 22:25:17 +0100147 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +0100148 for (;;) {
Damien George9e6e9352014-03-26 18:37:06 +0000149outer_dispatch_loop:
Damience89a212013-10-15 22:25:17 +0100150 if (nlr_push(&nlr) == 0) {
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200151 // If we have exception to inject, now that we finish setting up
152 // execution context, raise it. This works as if RAISE_VARARGS
153 // bytecode was executed.
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200154 // Injecting exc into yield from generator is a special case,
155 // handled by MP_BC_YIELD_FROM itself
156 if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) {
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200157 mp_obj_t t = inject_exc;
158 inject_exc = MP_OBJ_NULL;
159 nlr_jump(rt_make_raise_obj(t));
160 }
Damience89a212013-10-15 22:25:17 +0100161 // loop to execute byte code
162 for (;;) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200163dispatch_loop:
Damien George08335002014-01-18 23:24:36 +0000164 save_ip = ip;
Damience89a212013-10-15 22:25:17 +0100165 int op = *ip++;
166 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000167 case MP_BC_LOAD_CONST_FALSE:
168 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +0100169 break;
Damien429d7192013-10-04 19:53:11 +0100170
Damiend99b0522013-12-21 18:17:45 +0000171 case MP_BC_LOAD_CONST_NONE:
172 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100173 break;
Damien429d7192013-10-04 19:53:11 +0100174
Damiend99b0522013-12-21 18:17:45 +0000175 case MP_BC_LOAD_CONST_TRUE:
176 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100177 break;
Damien429d7192013-10-04 19:53:11 +0100178
Damien Georgee9906ac2014-01-04 18:44:46 +0000179 case MP_BC_LOAD_CONST_ELLIPSIS:
Damien George07ddab52014-03-29 13:15:08 +0000180 PUSH((mp_obj_t)&mp_const_ellipsis_obj);
Damien Georgee9906ac2014-01-04 18:44:46 +0000181 break;
182
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200183 case MP_BC_LOAD_CONST_SMALL_INT: {
Damien George4d79d5d2014-02-20 00:00:04 +0000184 machine_int_t num = 0;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200185 if ((ip[0] & 0x40) != 0) {
186 // Number is negative
187 num--;
188 }
189 do {
190 num = (num << 7) | (*ip & 0x7f);
191 } while ((*ip++ & 0x80) != 0);
192 PUSH(MP_OBJ_NEW_SMALL_INT(num));
Damience89a212013-10-15 22:25:17 +0100193 break;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200194 }
Damience89a212013-10-15 22:25:17 +0100195
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200196 case MP_BC_LOAD_CONST_INT:
197 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000198 PUSH(mp_obj_new_int_from_long_str(qstr_str(qst)));
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200199 break;
200
Damiend99b0522013-12-21 18:17:45 +0000201 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000202 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000203 PUSH(rt_load_const_dec(qst));
Damien7410e442013-11-02 19:47:57 +0000204 break;
205
Damiend99b0522013-12-21 18:17:45 +0000206 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100207 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000208 PUSH(rt_load_const_str(qst)); // TODO
Damience89a212013-10-15 22:25:17 +0100209 break;
210
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200211 case MP_BC_LOAD_CONST_BYTES:
212 DECODE_QSTR;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200213 PUSH(rt_load_const_bytes(qst));
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200214 break;
215
Damiend99b0522013-12-21 18:17:45 +0000216 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100217 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000218 PUSH(rt_load_const_str(qst));
Damience89a212013-10-15 22:25:17 +0100219 break;
220
Damiend99b0522013-12-21 18:17:45 +0000221 case MP_BC_LOAD_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000222 PUSH(fastn[0]);
Damience89a212013-10-15 22:25:17 +0100223 break;
224
Damiend99b0522013-12-21 18:17:45 +0000225 case MP_BC_LOAD_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000226 PUSH(fastn[-1]);
Damience89a212013-10-15 22:25:17 +0100227 break;
228
Damiend99b0522013-12-21 18:17:45 +0000229 case MP_BC_LOAD_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000230 PUSH(fastn[-2]);
Damience89a212013-10-15 22:25:17 +0100231 break;
232
Damiend99b0522013-12-21 18:17:45 +0000233 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100234 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000235 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100236 break;
237
Damiend99b0522013-12-21 18:17:45 +0000238 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000239 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000240 PUSH(rt_get_cell(fastn[-unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000241 break;
242
Damiend99b0522013-12-21 18:17:45 +0000243 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100244 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000245 PUSH(rt_load_name(qst));
Damience89a212013-10-15 22:25:17 +0100246 break;
247
Damiend99b0522013-12-21 18:17:45 +0000248 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100249 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000250 PUSH(rt_load_global(qst));
Damience89a212013-10-15 22:25:17 +0100251 break;
252
Damiend99b0522013-12-21 18:17:45 +0000253 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100254 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000255 SET_TOP(rt_load_attr(TOP(), qst));
Damience89a212013-10-15 22:25:17 +0100256 break;
257
Damiend99b0522013-12-21 18:17:45 +0000258 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100259 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000260 rt_load_method(*sp, qst, sp);
Damien George20006db2014-01-18 14:10:48 +0000261 sp += 1;
Damience89a212013-10-15 22:25:17 +0100262 break;
263
Damiend99b0522013-12-21 18:17:45 +0000264 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100265 PUSH(rt_load_build_class());
266 break;
267
Damiend99b0522013-12-21 18:17:45 +0000268 case MP_BC_STORE_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000269 fastn[0] = POP();
Damience89a212013-10-15 22:25:17 +0100270 break;
271
Damiend99b0522013-12-21 18:17:45 +0000272 case MP_BC_STORE_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000273 fastn[-1] = POP();
Damience89a212013-10-15 22:25:17 +0100274 break;
275
Damiend99b0522013-12-21 18:17:45 +0000276 case MP_BC_STORE_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000277 fastn[-2] = POP();
Damience89a212013-10-15 22:25:17 +0100278 break;
279
Damiend99b0522013-12-21 18:17:45 +0000280 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100281 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000282 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100283 break;
284
Damiend99b0522013-12-21 18:17:45 +0000285 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000286 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000287 rt_set_cell(fastn[-unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000288 break;
289
Damiend99b0522013-12-21 18:17:45 +0000290 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100291 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000292 rt_store_name(qst, POP());
Damience89a212013-10-15 22:25:17 +0100293 break;
294
Damiend99b0522013-12-21 18:17:45 +0000295 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000296 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000297 rt_store_global(qst, POP());
Damien6addc892013-11-04 23:04:50 +0000298 break;
299
Damiend99b0522013-12-21 18:17:45 +0000300 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100301 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000302 rt_store_attr(sp[0], qst, sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000303 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100304 break;
305
Damiend99b0522013-12-21 18:17:45 +0000306 case MP_BC_STORE_SUBSCR:
Damien George20006db2014-01-18 14:10:48 +0000307 rt_store_subscr(sp[-1], sp[0], sp[-2]);
308 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100309 break;
310
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200311 case MP_BC_DELETE_NAME:
312 DECODE_QSTR;
313 rt_delete_name(qst);
314 break;
315
Damiend99b0522013-12-21 18:17:45 +0000316 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000317 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100318 PUSH(obj1);
319 break;
320
Damiend99b0522013-12-21 18:17:45 +0000321 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000322 sp += 2;
323 sp[0] = sp[-2];
324 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100325 break;
326
Damiend99b0522013-12-21 18:17:45 +0000327 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000328 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100329 break;
330
Damiend99b0522013-12-21 18:17:45 +0000331 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000332 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000333 sp[0] = sp[-1];
334 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000335 break;
336
Damiend99b0522013-12-21 18:17:45 +0000337 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100338 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000339 sp[0] = sp[-1];
340 sp[-1] = sp[-2];
341 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100342 break;
343
Damiend99b0522013-12-21 18:17:45 +0000344 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000345 DECODE_SLABEL;
346 ip += unum;
Damience89a212013-10-15 22:25:17 +0100347 break;
348
Damiend99b0522013-12-21 18:17:45 +0000349 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000350 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100351 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000352 ip += unum;
Damience89a212013-10-15 22:25:17 +0100353 }
354 break;
355
Damiend99b0522013-12-21 18:17:45 +0000356 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000357 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100358 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000359 ip += unum;
Damience89a212013-10-15 22:25:17 +0100360 }
361 break;
362
Damiend99b0522013-12-21 18:17:45 +0000363 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000364 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000365 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000366 ip += unum;
367 } else {
Damien George20006db2014-01-18 14:10:48 +0000368 sp--;
Damien94658e22013-11-09 20:12:32 +0000369 }
370 break;
371
Damiend99b0522013-12-21 18:17:45 +0000372 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000373 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000374 if (rt_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000375 sp--;
Damien94658e22013-11-09 20:12:32 +0000376 } else {
377 ip += unum;
378 }
379 break;
380
Damience89a212013-10-15 22:25:17 +0100381 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000382 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100383 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000384 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100385 break;
386 */
387
Damien Georgec689c192014-03-29 14:06:14 +0000388 case MP_BC_SETUP_WITH:
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200389 obj1 = TOP();
390 SET_TOP(rt_load_attr(obj1, MP_QSTR___exit__));
Damien Georgec689c192014-03-29 14:06:14 +0000391 rt_load_method(obj1, MP_QSTR___enter__, sp + 1);
392 obj2 = rt_call_method_n_kw(0, 0, sp + 1);
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200393 PUSH_EXC_BLOCK();
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200394 PUSH(obj2);
395 break;
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200396
397 case MP_BC_WITH_CLEANUP: {
Paul Sokolovsky40d6d292014-03-29 16:49:33 +0200398 // Arriving here, there's "exception control block" on top of stack,
399 // and __exit__ bound method underneath it. Bytecode calls __exit__,
400 // and "deletes" it off stack, shifting "exception control block"
401 // to its place.
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200402 static const mp_obj_t no_exc[] = {mp_const_none, mp_const_none, mp_const_none};
403 if (TOP() == mp_const_none) {
404 sp--;
405 obj1 = TOP();
406 SET_TOP(mp_const_none);
407 obj2 = rt_call_function_n_kw(obj1, 3, 0, no_exc);
408 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
409 mp_obj_t cause = POP();
410 switch (MP_OBJ_SMALL_INT_VALUE(cause)) {
411 case UNWIND_RETURN: {
412 mp_obj_t retval = POP();
413 obj2 = rt_call_function_n_kw(TOP(), 3, 0, no_exc);
414 SET_TOP(retval);
415 PUSH(cause);
416 break;
417 }
418 case UNWIND_JUMP: {
419 obj2 = rt_call_function_n_kw(sp[-2], 3, 0, no_exc);
420 // Pop __exit__ boundmethod at sp[-2]
421 sp[-2] = sp[-1];
422 sp[-1] = sp[0];
423 SET_TOP(cause);
424 break;
425 }
426 default:
427 assert(0);
428 }
429 } else if (mp_obj_is_exception_type(TOP())) {
430 mp_obj_t args[3] = {sp[0], sp[-1], sp[-2]};
431 obj2 = rt_call_function_n_kw(sp[-3], 3, 0, args);
432 // Pop __exit__ boundmethod at sp[-3]
433 // TODO: Once semantics is proven, optimize for case when obj2 == True
434 sp[-3] = sp[-2];
435 sp[-2] = sp[-1];
436 sp[-1] = sp[0];
437 sp--;
438 if (rt_is_true(obj2)) {
439 // This is what CPython does
440 //PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_SILENCED));
441 // But what we need to do is - pop exception from value stack...
442 sp -= 3;
Paul Sokolovsky40d6d292014-03-29 16:49:33 +0200443 // ... pop "with" exception handler, and signal END_FINALLY
444 // to just execute finally handler normally (by pushing None
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200445 // on value stack)
446 assert(exc_sp >= exc_stack);
447 assert(exc_sp->opcode == MP_BC_SETUP_WITH);
448 exc_sp--;
449 PUSH(mp_const_none);
450 }
451 } else {
452 assert(0);
453 }
454 break;
455 }
456
Damien Georgecbddb272014-02-01 20:08:18 +0000457 case MP_BC_UNWIND_JUMP:
458 DECODE_SLABEL;
459 PUSH((void*)(ip + unum)); // push destination ip for jump
460 PUSH((void*)(machine_uint_t)(*ip)); // push number of exception handlers to unwind
461unwind_jump:
462 unum = (machine_uint_t)POP(); // get number of exception handlers to unwind
463 while (unum > 0) {
464 unum -= 1;
465 assert(exc_sp >= exc_stack);
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200466 if (exc_sp->opcode == MP_BC_SETUP_FINALLY || exc_sp->opcode == MP_BC_SETUP_WITH) {
Damien Georgecbddb272014-02-01 20:08:18 +0000467 // We're going to run "finally" code as a coroutine
468 // (not calling it recursively). Set up a sentinel
469 // on a stack so it can return back to us when it is
470 // done (when END_FINALLY reached).
471 PUSH((void*)unum); // push number of exception handlers left to unwind
472 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP)); // push sentinel
473 ip = exc_sp->handler; // get exception handler byte code address
474 exc_sp--; // pop exception handler
475 goto dispatch_loop; // run the exception handler
476 }
477 exc_sp--;
478 }
479 ip = (const byte*)POP(); // pop destination ip for jump
Damien George600ae732014-01-21 23:48:04 +0000480 break;
481
Damien02a7c412013-12-29 18:48:37 +0000482 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000483 case MP_BC_SETUP_EXCEPT:
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200484 case MP_BC_SETUP_FINALLY:
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200485 PUSH_EXC_BLOCK();
Damience89a212013-10-15 22:25:17 +0100486 break;
487
Damiend99b0522013-12-21 18:17:45 +0000488 case MP_BC_END_FINALLY:
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200489 // not fully implemented
Damience89a212013-10-15 22:25:17 +0100490 // if TOS is an exception, reraises the exception (3 values on TOS)
Damience89a212013-10-15 22:25:17 +0100491 // if TOS is None, just pops it and continues
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200492 // if TOS is an integer, does something else
Damience89a212013-10-15 22:25:17 +0100493 // else error
Paul Sokolovsky682f9e62014-03-29 02:52:17 +0200494 if (mp_obj_is_exception_type(TOP())) {
495 nlr_jump(sp[-1]);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200496 }
497 if (TOP() == mp_const_none) {
498 sp--;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200499 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
500 // We finished "finally" coroutine and now dispatch back
501 // to our caller, based on TOS value
502 mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
503 switch (reason) {
504 case UNWIND_RETURN:
505 goto unwind_return;
Damien Georgecbddb272014-02-01 20:08:18 +0000506 case UNWIND_JUMP:
507 goto unwind_jump;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200508 }
509 assert(0);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200510 } else {
511 assert(0);
512 }
Damience89a212013-10-15 22:25:17 +0100513 break;
514
Damiend99b0522013-12-21 18:17:45 +0000515 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000516 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100517 break;
518
Damiend99b0522013-12-21 18:17:45 +0000519 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000520 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien George66eaf842014-03-26 19:27:58 +0000521 obj1 = rt_iternext_allow_raise(TOP());
522 if (obj1 == MP_OBJ_NULL) {
Damien George20006db2014-01-18 14:10:48 +0000523 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000524 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100525 } else {
526 PUSH(obj1); // push the next iteration value
527 }
528 break;
529
Damien02a7c412013-12-29 18:48:37 +0000530 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000531 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000532 // we are exiting an exception handler, so pop the last one of the exception-stack
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200533 assert(exc_sp >= exc_stack);
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200534 POP_EXC_BLOCK();
Damience89a212013-10-15 22:25:17 +0100535 break;
536
Damien George600ae732014-01-21 23:48:04 +0000537 // matched against: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000538 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100539 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100540 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200541 assert(exc_sp >= exc_stack);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200542 assert(currently_in_except_block);
Damiend99b0522013-12-21 18:17:45 +0000543 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100544 //exc_sp--; // discard ip
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200545 POP_EXC_BLOCK();
Damien George20006db2014-01-18 14:10:48 +0000546 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100547 break;
548
Damien George9aa2a522014-02-01 23:04:09 +0000549 case MP_BC_NOT:
550 if (TOP() == mp_const_true) {
551 SET_TOP(mp_const_false);
552 } else {
553 SET_TOP(mp_const_true);
554 }
555 break;
556
Damiend99b0522013-12-21 18:17:45 +0000557 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000558 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000559 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000560 break;
561
Damiend99b0522013-12-21 18:17:45 +0000562 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100563 unum = *ip++;
564 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000565 obj1 = TOP();
566 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100567 break;
568
Damiend99b0522013-12-21 18:17:45 +0000569 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100570 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000571 sp -= unum - 1;
572 SET_TOP(rt_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100573 break;
574
Damiend99b0522013-12-21 18:17:45 +0000575 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100576 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000577 sp -= unum - 1;
578 SET_TOP(rt_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100579 break;
580
Damiend99b0522013-12-21 18:17:45 +0000581 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100582 DECODE_UINT;
583 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien George20006db2014-01-18 14:10:48 +0000584 rt_list_append(sp[-unum], sp[0]);
585 sp--;
Damienc226dca2013-10-16 16:12:52 +0100586 break;
587
Damiend99b0522013-12-21 18:17:45 +0000588 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100589 DECODE_UINT;
590 PUSH(rt_build_map(unum));
591 break;
592
Damiend99b0522013-12-21 18:17:45 +0000593 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000594 sp -= 2;
595 rt_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100596 break;
597
Damiend99b0522013-12-21 18:17:45 +0000598 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100599 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000600 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
601 rt_store_map(sp[-unum - 1], sp[0], sp[-1]);
602 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100603 break;
604
Damiend99b0522013-12-21 18:17:45 +0000605 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100606 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000607 sp -= unum - 1;
608 SET_TOP(rt_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100609 break;
610
Damiend99b0522013-12-21 18:17:45 +0000611 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100612 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000613 // I think it's guaranteed by the compiler that sp[-unum] is a set
614 rt_store_set(sp[-unum], sp[0]);
615 sp--;
Damienc12aa462013-10-16 20:57:49 +0100616 break;
617
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200618#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200619 case MP_BC_BUILD_SLICE:
620 DECODE_UINT;
621 if (unum == 2) {
622 obj2 = POP();
623 obj1 = TOP();
624 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
625 } else {
626 printf("3-argument slice is not supported\n");
627 assert(0);
628 }
629 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200630#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200631
Damiend99b0522013-12-21 18:17:45 +0000632 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000633 DECODE_UINT;
Damien George932bf1c2014-01-18 23:42:49 +0000634 rt_unpack_sequence(sp[0], unum, sp);
Damien George20006db2014-01-18 14:10:48 +0000635 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000636 break;
637
Damiend99b0522013-12-21 18:17:45 +0000638 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100639 DECODE_UINT;
Damien Georged1e443d2014-03-29 11:39:36 +0000640 PUSH(rt_make_function_from_id(unum, false, MP_OBJ_NULL));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200641 break;
642
643 case MP_BC_MAKE_FUNCTION_DEFARGS:
644 DECODE_UINT;
Damien Georged1e443d2014-03-29 11:39:36 +0000645 SET_TOP(rt_make_function_from_id(unum, false, TOP()));
Damience89a212013-10-15 22:25:17 +0100646 break;
647
Damiend99b0522013-12-21 18:17:45 +0000648 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000649 DECODE_UINT;
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200650 SET_TOP(rt_make_closure_from_id(unum, TOP(), MP_OBJ_NULL));
651 break;
652
653 case MP_BC_MAKE_CLOSURE_DEFARGS:
654 DECODE_UINT;
655 obj1 = POP();
656 SET_TOP(rt_make_closure_from_id(unum, obj1, TOP()));
Damien9ecbcff2013-12-11 00:41:43 +0000657 break;
658
Damiend99b0522013-12-21 18:17:45 +0000659 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100660 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000661 // unum & 0xff == n_positional
662 // (unum >> 8) & 0xff == n_keyword
663 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
664 SET_TOP(rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100665 break;
666
Damiend99b0522013-12-21 18:17:45 +0000667 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100668 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000669 // unum & 0xff == n_positional
670 // (unum >> 8) & 0xff == n_keyword
671 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
672 SET_TOP(rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100673 break;
674
Damiend99b0522013-12-21 18:17:45 +0000675 case MP_BC_RETURN_VALUE:
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200676unwind_return:
677 while (exc_sp >= exc_stack) {
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200678 if (exc_sp->opcode == MP_BC_SETUP_FINALLY || exc_sp->opcode == MP_BC_SETUP_WITH) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200679 // We're going to run "finally" code as a coroutine
680 // (not calling it recursively). Set up a sentinel
681 // on a stack so it can return back to us when it is
682 // done (when END_FINALLY reached).
683 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN));
684 ip = exc_sp->handler;
685 // We don't need to do anything with sp, finally is just
686 // syntactic sugar for sequential execution??
687 // sp =
688 exc_sp--;
689 goto dispatch_loop;
690 }
691 exc_sp--;
692 }
Damience89a212013-10-15 22:25:17 +0100693 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100694 *sp_in_out = sp;
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200695 assert(exc_sp == exc_stack - 1);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000696 return MP_VM_RETURN_NORMAL;
Damienbd254452013-10-16 20:39:12 +0100697
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200698 case MP_BC_RAISE_VARARGS:
699 unum = *ip++;
Paul Sokolovskyc4030762014-03-26 14:42:17 +0200700 assert(unum <= 1);
701 if (unum == 0) {
Paul Sokolovskyd1096762014-03-29 19:44:15 +0200702 if (!currently_in_except_block) {
703 nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "No active exception to reraise"));
704 }
Paul Sokolovskyc4030762014-03-26 14:42:17 +0200705 // This assumes that nlr.ret_val holds last raised
706 // exception and is not overwritten since then.
707 obj1 = nlr.ret_val;
708 } else {
709 obj1 = POP();
710 }
Damien Georgec5966122014-02-15 16:10:44 +0000711 nlr_jump(rt_make_raise_obj(obj1));
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200712
Damiend99b0522013-12-21 18:17:45 +0000713 case MP_BC_YIELD_VALUE:
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200714yield:
Damienbd254452013-10-16 20:39:12 +0100715 nlr_pop();
716 *ip_in_out = ip;
Damienbd254452013-10-16 20:39:12 +0100717 *sp_in_out = sp;
Paul Sokolovsky16734202014-03-22 23:20:07 +0200718 *exc_sp_in_out = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000719 return MP_VM_RETURN_YIELD;
Damience89a212013-10-15 22:25:17 +0100720
Paul Sokolovsky40d6d292014-03-29 16:49:33 +0200721 case MP_BC_YIELD_FROM: {
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200722//#define EXC_MATCH(exc, type) MP_OBJ_IS_TYPE(exc, type)
723#define EXC_MATCH(exc, type) mp_obj_exception_match(exc, type)
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200724#define GENERATOR_EXIT_IF_NEEDED(t) if (t != MP_OBJ_NULL && EXC_MATCH(t, &mp_type_GeneratorExit)) { nlr_jump(t); }
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200725 mp_vm_return_kind_t ret_kind;
726 obj1 = POP();
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200727 mp_obj_t t_exc = MP_OBJ_NULL;
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200728 if (inject_exc != MP_OBJ_NULL) {
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200729 t_exc = inject_exc;
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200730 inject_exc = MP_OBJ_NULL;
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200731 ret_kind = mp_obj_gen_resume(TOP(), mp_const_none, t_exc, &obj2);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200732 } else {
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200733 ret_kind = mp_obj_gen_resume(TOP(), obj1, MP_OBJ_NULL, &obj2);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200734 }
735
736 if (ret_kind == MP_VM_RETURN_YIELD) {
737 ip--;
738 PUSH(obj2);
739 goto yield;
740 }
741 if (ret_kind == MP_VM_RETURN_NORMAL) {
742 // Pop exhausted gen
743 sp--;
744 if (obj2 == MP_OBJ_NULL) {
745 // Optimize StopIteration
746 // TODO: get StopIteration's value
747 PUSH(mp_const_none);
748 } else {
749 PUSH(obj2);
750 }
751
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200752 // If we injected GeneratorExit downstream, then even
753 // if it was swallowed, we re-raise GeneratorExit
754 GENERATOR_EXIT_IF_NEEDED(t_exc);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200755 break;
756 }
757 if (ret_kind == MP_VM_RETURN_EXCEPTION) {
758 // Pop exhausted gen
759 sp--;
760 if (EXC_MATCH(obj2, &mp_type_StopIteration)) {
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200761 PUSH(mp_obj_exception_get_value(obj2));
762 // If we injected GeneratorExit downstream, then even
763 // if it was swallowed, we re-raise GeneratorExit
764 GENERATOR_EXIT_IF_NEEDED(t_exc);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200765 break;
766 } else {
767 nlr_jump(obj2);
768 }
769 }
770 }
771
Damiend99b0522013-12-21 18:17:45 +0000772 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000773 DECODE_QSTR;
774 obj1 = POP();
Damien Georgecbd2f742014-01-19 11:48:48 +0000775 SET_TOP(rt_import_name(qst, obj1, TOP()));
Damiendb4c3612013-12-10 17:27:24 +0000776 break;
777
Damiend99b0522013-12-21 18:17:45 +0000778 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000779 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000780 obj1 = rt_import_from(TOP(), qst);
Damiendb4c3612013-12-10 17:27:24 +0000781 PUSH(obj1);
782 break;
783
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200784 case MP_BC_IMPORT_STAR:
Damien Georgeaa9b74f2014-02-14 23:06:33 +0000785 rt_import_all(POP());
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200786 break;
787
Damience89a212013-10-15 22:25:17 +0100788 default:
Damien03c9cfb2013-11-05 22:06:08 +0000789 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100790 assert(0);
791 nlr_pop();
Damien Georgec8f78bc2014-02-15 22:55:00 +0000792 return MP_VM_RETURN_NORMAL;
Damien429d7192013-10-04 19:53:11 +0100793 }
Damience89a212013-10-15 22:25:17 +0100794 }
Damien429d7192013-10-04 19:53:11 +0100795
Damience89a212013-10-15 22:25:17 +0100796 } else {
797 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100798
Damien George9e6e9352014-03-26 18:37:06 +0000799 // check if it's a StopIteration within a for block
800 if (*save_ip == MP_BC_FOR_ITER && mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
801 ip = save_ip + 1;
802 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
803 --sp; // pop the exhausted iterator
804 ip += unum; // jump to after for-block
805 goto outer_dispatch_loop; // continue with dispatch loop
806 }
807
Damien George08335002014-01-18 23:24:36 +0000808 // set file and line number that the exception occurred at
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200809 // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
810 // But consider how to handle nested exceptions.
Damien Georgeb04be052014-03-29 13:52:51 +0000811 // TODO need a better way of not adding traceback to constant objects (right now, just GeneratorExit_obj)
812 if (mp_obj_is_exception_instance(nlr.ret_val) && nlr.ret_val != &mp_const_GeneratorExit_obj) {
Damien George08335002014-01-18 23:24:36 +0000813 machine_uint_t code_info_size = code_info[0] | (code_info[1] << 8) | (code_info[2] << 16) | (code_info[3] << 24);
Damien Georgecbd2f742014-01-19 11:48:48 +0000814 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
815 qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
Damien George08335002014-01-18 23:24:36 +0000816 machine_uint_t source_line = 1;
817 machine_uint_t bc = save_ip - code_info - code_info_size;
Damien Georgecbd2f742014-01-19 11:48:48 +0000818 //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
Damien George28eb5772014-01-25 11:43:20 +0000819 for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
820 bc -= *ci & 31;
821 source_line += *ci >> 5;
Damien George08335002014-01-18 23:24:36 +0000822 }
Damien George136b1492014-01-19 12:38:49 +0000823 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +0000824 }
825
Damien8f9e2ee2013-12-29 16:54:59 +0000826 while (currently_in_except_block) {
827 // nested exception
828
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200829 assert(exc_sp >= exc_stack);
Damien8f9e2ee2013-12-29 16:54:59 +0000830
831 // TODO make a proper message for nested exception
832 // at the moment we are just raising the very last exception (the one that caused the nested exception)
833
834 // move up to previous exception handler
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200835 POP_EXC_BLOCK();
Damien8f9e2ee2013-12-29 16:54:59 +0000836 }
837
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200838 if (exc_sp >= exc_stack) {
Damien8f9e2ee2013-12-29 16:54:59 +0000839 // set flag to indicate that we are now handling an exception
840 currently_in_except_block = 1;
841
Damience89a212013-10-15 22:25:17 +0100842 // catch exception and pass to byte code
Paul Sokolovsky16734202014-03-22 23:20:07 +0200843 sp = MP_TAGPTR_PTR(exc_sp->val_sp);
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200844 ip = exc_sp->handler;
Damienc9f91972013-10-15 23:46:01 +0100845 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000846 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100847 PUSH(nlr.ret_val);
Paul Sokolovsky682f9e62014-03-29 02:52:17 +0200848 PUSH(mp_obj_get_type(nlr.ret_val));
Damien8f9e2ee2013-12-29 16:54:59 +0000849
Damience89a212013-10-15 22:25:17 +0100850 } else {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000851 // propagate exception to higher level
852 // TODO what to do about ip and sp? they don't really make sense at this point
853 fastn[0] = nlr.ret_val; // must put exception here because sp is invalid
854 return MP_VM_RETURN_EXCEPTION;
Damience89a212013-10-15 22:25:17 +0100855 }
Damien429d7192013-10-04 19:53:11 +0100856 }
857 }
858}