blob: 22243c403e3da704015882063108409e6228eedb [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"
Damien429d7192013-10-04 19:53:11 +010012
Paul Sokolovsky85193422014-01-31 19:45:15 +020013// Value stack grows up (this makes it incompatible with native C stack, but
14// makes sure that arguments to functions are in natural order arg1..argN
15// (Python semantics mandates left-to-right evaluation order, including for
16// function arguments). Stack pointer is pre-incremented and points at the
17// top element.
18// Exception stack also grows up, top element is also pointed at.
19
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020020// Exception stack unwind reasons (WHY_* in CPython-speak)
Damien Georgecbddb272014-02-01 20:08:18 +000021// TODO perhaps compress this to RETURN=0, JUMP>0, with number of unwinds
22// left to do encoded in the JUMP number
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020023typedef enum {
24 UNWIND_RETURN = 1,
Damien Georgecbddb272014-02-01 20:08:18 +000025 UNWIND_JUMP,
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020026} mp_unwind_reason_t;
27
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020028#define DECODE_UINT { \
29 unum = 0; \
30 do { \
31 unum = (unum << 7) + (*ip & 0x7f); \
32 } while ((*ip++ & 0x80) != 0); \
33}
Damien03c9cfb2013-11-05 22:06:08 +000034#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
35#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020036#define DECODE_QSTR { \
37 qst = 0; \
38 do { \
39 qst = (qst << 7) + (*ip & 0x7f); \
40 } while ((*ip++ & 0x80) != 0); \
41}
Damien George20006db2014-01-18 14:10:48 +000042#define PUSH(val) *++sp = (val)
43#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000044#define TOP() (*sp)
45#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010046
Damien Georgec8f78bc2014-02-15 22:55:00 +000047mp_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, uint n_state, mp_obj_t *ret) {
Damien George20006db2014-01-18 14:10:48 +000048 // allocate state for locals and stack
49 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000050 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000051 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000052 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000053 }
Damien George20006db2014-01-18 14:10:48 +000054 mp_obj_t *sp = &state[0] - 1;
55
Damienbd254452013-10-16 20:39:12 +010056 // init args
Damien Georgefb083ea2014-02-01 18:29:40 +000057 for (uint i = 0; i < n_args; i++) {
Damien George20006db2014-01-18 14:10:48 +000058 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010059 }
Damien Georgefb083ea2014-02-01 18:29:40 +000060 for (uint i = 0; i < n_args2; i++) {
61 state[n_state - 1 - n_args - i] = args2[i];
62 }
Damien George08335002014-01-18 23:24:36 +000063
Damienbd254452013-10-16 20:39:12 +010064 const byte *ip = code;
Damien George6baf76e2013-12-30 22:32:17 +000065
Damien George08335002014-01-18 23:24:36 +000066 // get code info size
67 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
68 ip += code_info_size;
69
Damien George6baf76e2013-12-30 22:32:17 +000070 // execute prelude to make any cells (closed over variables)
71 {
72 for (uint n_local = *ip++; n_local > 0; n_local--) {
73 uint local_num = *ip++;
Damien Georgefb083ea2014-02-01 18:29:40 +000074 if (local_num < n_args + n_args2) {
Damien George20006db2014-01-18 14:10:48 +000075 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
Damien George6baf76e2013-12-30 22:32:17 +000076 } else {
Damien George20006db2014-01-18 14:10:48 +000077 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +000078 }
79 }
80 }
81
Paul Sokolovskyc0abc282014-03-22 13:49:31 +020082 mp_exc_stack exc_stack[4];
83 mp_exc_stack *exc_sp = &exc_stack[0] - 1;
Damien George6baf76e2013-12-30 22:32:17 +000084 // execute the byte code
Paul Sokolovsky48caa092014-03-22 17:50:12 +020085 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 +000086
Damien Georgec8f78bc2014-02-15 22:55:00 +000087 switch (vm_return_kind) {
88 case MP_VM_RETURN_NORMAL:
89 *ret = *sp;
90 return MP_VM_RETURN_NORMAL;
91 case MP_VM_RETURN_EXCEPTION:
92 *ret = state[n_state - 1];
93 return MP_VM_RETURN_EXCEPTION;
94 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
95 default:
96 assert(0);
97 *ret = mp_const_none;
98 return MP_VM_RETURN_NORMAL;
99 }
Damienbd254452013-10-16 20:39:12 +0100100}
101
Damien George20006db2014-01-18 14:10:48 +0000102// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
103// sp points to bottom of stack which grows up
Damien Georgec8f78bc2014-02-15 22:55:00 +0000104// returns:
105// MP_VM_RETURN_NORMAL, sp valid, return value in *sp
106// MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
107// MP_VM_RETURN_EXCEPTION, exception in fastn[0]
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200108mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out,
109 mp_obj_t *fastn, mp_obj_t **sp_in_out,
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200110 mp_exc_stack *exc_stack, mp_exc_stack **exc_sp_in_out,
111 volatile mp_obj_t inject_exc) {
Damienc9f91972013-10-15 23:46:01 +0100112 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
113
Damienbd254452013-10-16 20:39:12 +0100114 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +0000115 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +0100116 machine_uint_t unum;
Damien Georgecbd2f742014-01-19 11:48:48 +0000117 qstr qst;
Damiend99b0522013-12-21 18:17:45 +0000118 mp_obj_t obj1, obj2;
Damience89a212013-10-15 22:25:17 +0100119 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +0100120
Paul Sokolovsky16734202014-03-22 23:20:07 +0200121 volatile bool currently_in_except_block = MP_TAGPTR_TAG(*exc_sp_in_out); // 0 or 1, to detect nested exceptions
122 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 +0000123 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 +0100124
Damience89a212013-10-15 22:25:17 +0100125 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +0100126 for (;;) {
Damience89a212013-10-15 22:25:17 +0100127 if (nlr_push(&nlr) == 0) {
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200128 // If we have exception to inject, now that we finish setting up
129 // execution context, raise it. This works as if RAISE_VARARGS
130 // bytecode was executed.
131 if (inject_exc != MP_OBJ_NULL) {
132 mp_obj_t t = inject_exc;
133 inject_exc = MP_OBJ_NULL;
134 nlr_jump(rt_make_raise_obj(t));
135 }
Damience89a212013-10-15 22:25:17 +0100136 // loop to execute byte code
137 for (;;) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200138dispatch_loop:
Damien George08335002014-01-18 23:24:36 +0000139 save_ip = ip;
Damience89a212013-10-15 22:25:17 +0100140 int op = *ip++;
141 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000142 case MP_BC_LOAD_CONST_FALSE:
143 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +0100144 break;
Damien429d7192013-10-04 19:53:11 +0100145
Damiend99b0522013-12-21 18:17:45 +0000146 case MP_BC_LOAD_CONST_NONE:
147 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100148 break;
Damien429d7192013-10-04 19:53:11 +0100149
Damiend99b0522013-12-21 18:17:45 +0000150 case MP_BC_LOAD_CONST_TRUE:
151 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100152 break;
Damien429d7192013-10-04 19:53:11 +0100153
Damien Georgee9906ac2014-01-04 18:44:46 +0000154 case MP_BC_LOAD_CONST_ELLIPSIS:
155 PUSH(mp_const_ellipsis);
156 break;
157
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200158 case MP_BC_LOAD_CONST_SMALL_INT: {
Damien George4d79d5d2014-02-20 00:00:04 +0000159 machine_int_t num = 0;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200160 if ((ip[0] & 0x40) != 0) {
161 // Number is negative
162 num--;
163 }
164 do {
165 num = (num << 7) | (*ip & 0x7f);
166 } while ((*ip++ & 0x80) != 0);
167 PUSH(MP_OBJ_NEW_SMALL_INT(num));
Damience89a212013-10-15 22:25:17 +0100168 break;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200169 }
Damience89a212013-10-15 22:25:17 +0100170
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200171 case MP_BC_LOAD_CONST_INT:
172 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000173 PUSH(mp_obj_new_int_from_long_str(qstr_str(qst)));
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200174 break;
175
Damiend99b0522013-12-21 18:17:45 +0000176 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000177 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000178 PUSH(rt_load_const_dec(qst));
Damien7410e442013-11-02 19:47:57 +0000179 break;
180
Damiend99b0522013-12-21 18:17:45 +0000181 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100182 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000183 PUSH(rt_load_const_str(qst)); // TODO
Damience89a212013-10-15 22:25:17 +0100184 break;
185
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200186 case MP_BC_LOAD_CONST_BYTES:
187 DECODE_QSTR;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200188 PUSH(rt_load_const_bytes(qst));
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200189 break;
190
Damiend99b0522013-12-21 18:17:45 +0000191 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100192 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000193 PUSH(rt_load_const_str(qst));
Damience89a212013-10-15 22:25:17 +0100194 break;
195
Damiend99b0522013-12-21 18:17:45 +0000196 case MP_BC_LOAD_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000197 PUSH(fastn[0]);
Damience89a212013-10-15 22:25:17 +0100198 break;
199
Damiend99b0522013-12-21 18:17:45 +0000200 case MP_BC_LOAD_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000201 PUSH(fastn[-1]);
Damience89a212013-10-15 22:25:17 +0100202 break;
203
Damiend99b0522013-12-21 18:17:45 +0000204 case MP_BC_LOAD_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000205 PUSH(fastn[-2]);
Damience89a212013-10-15 22:25:17 +0100206 break;
207
Damiend99b0522013-12-21 18:17:45 +0000208 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100209 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000210 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100211 break;
212
Damiend99b0522013-12-21 18:17:45 +0000213 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000214 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000215 PUSH(rt_get_cell(fastn[-unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000216 break;
217
Damiend99b0522013-12-21 18:17:45 +0000218 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100219 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000220 PUSH(rt_load_name(qst));
Damience89a212013-10-15 22:25:17 +0100221 break;
222
Damiend99b0522013-12-21 18:17:45 +0000223 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100224 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000225 PUSH(rt_load_global(qst));
Damience89a212013-10-15 22:25:17 +0100226 break;
227
Damiend99b0522013-12-21 18:17:45 +0000228 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100229 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000230 SET_TOP(rt_load_attr(TOP(), qst));
Damience89a212013-10-15 22:25:17 +0100231 break;
232
Damiend99b0522013-12-21 18:17:45 +0000233 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100234 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000235 rt_load_method(*sp, qst, sp);
Damien George20006db2014-01-18 14:10:48 +0000236 sp += 1;
Damience89a212013-10-15 22:25:17 +0100237 break;
238
Damiend99b0522013-12-21 18:17:45 +0000239 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100240 PUSH(rt_load_build_class());
241 break;
242
Damiend99b0522013-12-21 18:17:45 +0000243 case MP_BC_STORE_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000244 fastn[0] = POP();
Damience89a212013-10-15 22:25:17 +0100245 break;
246
Damiend99b0522013-12-21 18:17:45 +0000247 case MP_BC_STORE_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000248 fastn[-1] = POP();
Damience89a212013-10-15 22:25:17 +0100249 break;
250
Damiend99b0522013-12-21 18:17:45 +0000251 case MP_BC_STORE_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000252 fastn[-2] = POP();
Damience89a212013-10-15 22:25:17 +0100253 break;
254
Damiend99b0522013-12-21 18:17:45 +0000255 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100256 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000257 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100258 break;
259
Damiend99b0522013-12-21 18:17:45 +0000260 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000261 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000262 rt_set_cell(fastn[-unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000263 break;
264
Damiend99b0522013-12-21 18:17:45 +0000265 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100266 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000267 rt_store_name(qst, POP());
Damience89a212013-10-15 22:25:17 +0100268 break;
269
Damiend99b0522013-12-21 18:17:45 +0000270 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000271 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000272 rt_store_global(qst, POP());
Damien6addc892013-11-04 23:04:50 +0000273 break;
274
Damiend99b0522013-12-21 18:17:45 +0000275 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100276 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000277 rt_store_attr(sp[0], qst, sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000278 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100279 break;
280
Damiend99b0522013-12-21 18:17:45 +0000281 case MP_BC_STORE_SUBSCR:
Damien George20006db2014-01-18 14:10:48 +0000282 rt_store_subscr(sp[-1], sp[0], sp[-2]);
283 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100284 break;
285
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200286 case MP_BC_DELETE_NAME:
287 DECODE_QSTR;
288 rt_delete_name(qst);
289 break;
290
Damiend99b0522013-12-21 18:17:45 +0000291 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000292 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100293 PUSH(obj1);
294 break;
295
Damiend99b0522013-12-21 18:17:45 +0000296 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000297 sp += 2;
298 sp[0] = sp[-2];
299 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100300 break;
301
Damiend99b0522013-12-21 18:17:45 +0000302 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000303 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100304 break;
305
Damiend99b0522013-12-21 18:17:45 +0000306 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000307 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000308 sp[0] = sp[-1];
309 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000310 break;
311
Damiend99b0522013-12-21 18:17:45 +0000312 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100313 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000314 sp[0] = sp[-1];
315 sp[-1] = sp[-2];
316 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100317 break;
318
Damiend99b0522013-12-21 18:17:45 +0000319 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000320 DECODE_SLABEL;
321 ip += unum;
Damience89a212013-10-15 22:25:17 +0100322 break;
323
Damiend99b0522013-12-21 18:17:45 +0000324 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000325 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100326 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000327 ip += unum;
Damience89a212013-10-15 22:25:17 +0100328 }
329 break;
330
Damiend99b0522013-12-21 18:17:45 +0000331 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000332 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100333 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000334 ip += unum;
Damience89a212013-10-15 22:25:17 +0100335 }
336 break;
337
Damiend99b0522013-12-21 18:17:45 +0000338 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000339 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000340 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000341 ip += unum;
342 } else {
Damien George20006db2014-01-18 14:10:48 +0000343 sp--;
Damien94658e22013-11-09 20:12:32 +0000344 }
345 break;
346
Damiend99b0522013-12-21 18:17:45 +0000347 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000348 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000349 if (rt_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000350 sp--;
Damien94658e22013-11-09 20:12:32 +0000351 } else {
352 ip += unum;
353 }
354 break;
355
Damience89a212013-10-15 22:25:17 +0100356 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000357 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100358 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000359 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100360 break;
361 */
362
Damien Georgecbddb272014-02-01 20:08:18 +0000363 case MP_BC_UNWIND_JUMP:
364 DECODE_SLABEL;
365 PUSH((void*)(ip + unum)); // push destination ip for jump
366 PUSH((void*)(machine_uint_t)(*ip)); // push number of exception handlers to unwind
367unwind_jump:
368 unum = (machine_uint_t)POP(); // get number of exception handlers to unwind
369 while (unum > 0) {
370 unum -= 1;
371 assert(exc_sp >= exc_stack);
372 if (exc_sp->opcode == MP_BC_SETUP_FINALLY) {
373 // We're going to run "finally" code as a coroutine
374 // (not calling it recursively). Set up a sentinel
375 // on a stack so it can return back to us when it is
376 // done (when END_FINALLY reached).
377 PUSH((void*)unum); // push number of exception handlers left to unwind
378 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP)); // push sentinel
379 ip = exc_sp->handler; // get exception handler byte code address
380 exc_sp--; // pop exception handler
381 goto dispatch_loop; // run the exception handler
382 }
383 exc_sp--;
384 }
385 ip = (const byte*)POP(); // pop destination ip for jump
Damien George600ae732014-01-21 23:48:04 +0000386 break;
387
Damien02a7c412013-12-29 18:48:37 +0000388 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000389 case MP_BC_SETUP_EXCEPT:
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200390 case MP_BC_SETUP_FINALLY:
Damien03c9cfb2013-11-05 22:06:08 +0000391 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200392 ++exc_sp;
393 exc_sp->opcode = op;
394 exc_sp->handler = ip + unum;
Paul Sokolovsky16734202014-03-22 23:20:07 +0200395 exc_sp->val_sp = MP_TAGPTR_MAKE(sp, currently_in_except_block);
Damien8f9e2ee2013-12-29 16:54:59 +0000396 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100397 break;
398
Damiend99b0522013-12-21 18:17:45 +0000399 case MP_BC_END_FINALLY:
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200400 // not fully implemented
Damience89a212013-10-15 22:25:17 +0100401 // if TOS is an exception, reraises the exception (3 values on TOS)
Damience89a212013-10-15 22:25:17 +0100402 // if TOS is None, just pops it and continues
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200403 // if TOS is an integer, does something else
Damience89a212013-10-15 22:25:17 +0100404 // else error
Damien Georgec5966122014-02-15 16:10:44 +0000405 if (mp_obj_is_exception_instance(TOP())) {
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200406 nlr_jump(TOP());
407 }
408 if (TOP() == mp_const_none) {
409 sp--;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200410 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
411 // We finished "finally" coroutine and now dispatch back
412 // to our caller, based on TOS value
413 mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
414 switch (reason) {
415 case UNWIND_RETURN:
416 goto unwind_return;
Damien Georgecbddb272014-02-01 20:08:18 +0000417 case UNWIND_JUMP:
418 goto unwind_jump;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200419 }
420 assert(0);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200421 } else {
422 assert(0);
423 }
Damience89a212013-10-15 22:25:17 +0100424 break;
425
Damiend99b0522013-12-21 18:17:45 +0000426 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000427 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100428 break;
429
Damiend99b0522013-12-21 18:17:45 +0000430 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000431 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000432 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000433 if (obj1 == mp_const_stop_iteration) {
Damien George20006db2014-01-18 14:10:48 +0000434 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000435 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100436 } else {
437 PUSH(obj1); // push the next iteration value
438 }
439 break;
440
Damien02a7c412013-12-29 18:48:37 +0000441 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000442 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000443 // we are exiting an exception handler, so pop the last one of the exception-stack
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200444 assert(exc_sp >= exc_stack);
Paul Sokolovsky16734202014-03-22 23:20:07 +0200445 currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); // restore previous state
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200446 exc_sp--; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100447 break;
448
Damien George600ae732014-01-21 23:48:04 +0000449 // matched against: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000450 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100451 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100452 // 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 +0200453 assert(exc_sp >= exc_stack);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200454 assert(currently_in_except_block);
Damiend99b0522013-12-21 18:17:45 +0000455 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100456 //exc_sp--; // discard ip
Paul Sokolovsky16734202014-03-22 23:20:07 +0200457 currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); // restore previous state
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200458 exc_sp--; // pop back to previous exception handler
Damien George20006db2014-01-18 14:10:48 +0000459 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100460 break;
461
Damien George9aa2a522014-02-01 23:04:09 +0000462 case MP_BC_NOT:
463 if (TOP() == mp_const_true) {
464 SET_TOP(mp_const_false);
465 } else {
466 SET_TOP(mp_const_true);
467 }
468 break;
469
Damiend99b0522013-12-21 18:17:45 +0000470 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000471 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000472 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000473 break;
474
Damiend99b0522013-12-21 18:17:45 +0000475 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100476 unum = *ip++;
477 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000478 obj1 = TOP();
479 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100480 break;
481
Damiend99b0522013-12-21 18:17:45 +0000482 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100483 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000484 sp -= unum - 1;
485 SET_TOP(rt_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100486 break;
487
Damiend99b0522013-12-21 18:17:45 +0000488 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100489 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000490 sp -= unum - 1;
491 SET_TOP(rt_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100492 break;
493
Damiend99b0522013-12-21 18:17:45 +0000494 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100495 DECODE_UINT;
496 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien George20006db2014-01-18 14:10:48 +0000497 rt_list_append(sp[-unum], sp[0]);
498 sp--;
Damienc226dca2013-10-16 16:12:52 +0100499 break;
500
Damiend99b0522013-12-21 18:17:45 +0000501 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100502 DECODE_UINT;
503 PUSH(rt_build_map(unum));
504 break;
505
Damiend99b0522013-12-21 18:17:45 +0000506 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000507 sp -= 2;
508 rt_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100509 break;
510
Damiend99b0522013-12-21 18:17:45 +0000511 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100512 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000513 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
514 rt_store_map(sp[-unum - 1], sp[0], sp[-1]);
515 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100516 break;
517
Damiend99b0522013-12-21 18:17:45 +0000518 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100519 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000520 sp -= unum - 1;
521 SET_TOP(rt_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100522 break;
523
Damiend99b0522013-12-21 18:17:45 +0000524 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100525 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000526 // I think it's guaranteed by the compiler that sp[-unum] is a set
527 rt_store_set(sp[-unum], sp[0]);
528 sp--;
Damienc12aa462013-10-16 20:57:49 +0100529 break;
530
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200531#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200532 case MP_BC_BUILD_SLICE:
533 DECODE_UINT;
534 if (unum == 2) {
535 obj2 = POP();
536 obj1 = TOP();
537 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
538 } else {
539 printf("3-argument slice is not supported\n");
540 assert(0);
541 }
542 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200543#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200544
Damiend99b0522013-12-21 18:17:45 +0000545 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000546 DECODE_UINT;
Damien George932bf1c2014-01-18 23:42:49 +0000547 rt_unpack_sequence(sp[0], unum, sp);
Damien George20006db2014-01-18 14:10:48 +0000548 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000549 break;
550
Damiend99b0522013-12-21 18:17:45 +0000551 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100552 DECODE_UINT;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200553 PUSH(rt_make_function_from_id(unum, MP_OBJ_NULL));
554 break;
555
556 case MP_BC_MAKE_FUNCTION_DEFARGS:
557 DECODE_UINT;
558 SET_TOP(rt_make_function_from_id(unum, TOP()));
Damience89a212013-10-15 22:25:17 +0100559 break;
560
Damiend99b0522013-12-21 18:17:45 +0000561 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000562 DECODE_UINT;
Damien George08d07552014-01-29 18:58:52 +0000563 SET_TOP(rt_make_closure_from_id(unum, TOP()));
Damien9ecbcff2013-12-11 00:41:43 +0000564 break;
565
Damiend99b0522013-12-21 18:17:45 +0000566 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100567 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000568 // unum & 0xff == n_positional
569 // (unum >> 8) & 0xff == n_keyword
570 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
571 SET_TOP(rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100572 break;
573
Damiend99b0522013-12-21 18:17:45 +0000574 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100575 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000576 // unum & 0xff == n_positional
577 // (unum >> 8) & 0xff == n_keyword
578 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
579 SET_TOP(rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100580 break;
581
Damiend99b0522013-12-21 18:17:45 +0000582 case MP_BC_RETURN_VALUE:
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200583unwind_return:
584 while (exc_sp >= exc_stack) {
585 if (exc_sp->opcode == MP_BC_SETUP_FINALLY) {
586 // We're going to run "finally" code as a coroutine
587 // (not calling it recursively). Set up a sentinel
588 // on a stack so it can return back to us when it is
589 // done (when END_FINALLY reached).
590 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN));
591 ip = exc_sp->handler;
592 // We don't need to do anything with sp, finally is just
593 // syntactic sugar for sequential execution??
594 // sp =
595 exc_sp--;
596 goto dispatch_loop;
597 }
598 exc_sp--;
599 }
Damience89a212013-10-15 22:25:17 +0100600 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100601 *sp_in_out = sp;
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200602 assert(exc_sp == exc_stack - 1);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000603 return MP_VM_RETURN_NORMAL;
Damienbd254452013-10-16 20:39:12 +0100604
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200605 case MP_BC_RAISE_VARARGS:
606 unum = *ip++;
607 assert(unum == 1);
608 obj1 = POP();
Damien Georgec5966122014-02-15 16:10:44 +0000609 nlr_jump(rt_make_raise_obj(obj1));
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200610
Damiend99b0522013-12-21 18:17:45 +0000611 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100612 nlr_pop();
613 *ip_in_out = ip;
Damienbd254452013-10-16 20:39:12 +0100614 *sp_in_out = sp;
Paul Sokolovsky16734202014-03-22 23:20:07 +0200615 *exc_sp_in_out = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000616 return MP_VM_RETURN_YIELD;
Damience89a212013-10-15 22:25:17 +0100617
Damiend99b0522013-12-21 18:17:45 +0000618 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000619 DECODE_QSTR;
620 obj1 = POP();
Damien Georgecbd2f742014-01-19 11:48:48 +0000621 SET_TOP(rt_import_name(qst, obj1, TOP()));
Damiendb4c3612013-12-10 17:27:24 +0000622 break;
623
Damiend99b0522013-12-21 18:17:45 +0000624 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000625 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000626 obj1 = rt_import_from(TOP(), qst);
Damiendb4c3612013-12-10 17:27:24 +0000627 PUSH(obj1);
628 break;
629
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200630 case MP_BC_IMPORT_STAR:
Damien Georgeaa9b74f2014-02-14 23:06:33 +0000631 rt_import_all(POP());
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200632 break;
633
Damience89a212013-10-15 22:25:17 +0100634 default:
Damien03c9cfb2013-11-05 22:06:08 +0000635 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100636 assert(0);
637 nlr_pop();
Damien Georgec8f78bc2014-02-15 22:55:00 +0000638 return MP_VM_RETURN_NORMAL;
Damien429d7192013-10-04 19:53:11 +0100639 }
Damience89a212013-10-15 22:25:17 +0100640 }
Damien429d7192013-10-04 19:53:11 +0100641
Damience89a212013-10-15 22:25:17 +0100642 } else {
643 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100644
Damien George08335002014-01-18 23:24:36 +0000645 // set file and line number that the exception occurred at
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200646 // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
647 // But consider how to handle nested exceptions.
Damien Georgec5966122014-02-15 16:10:44 +0000648 if (mp_obj_is_exception_instance(nlr.ret_val)) {
Damien George08335002014-01-18 23:24:36 +0000649 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 +0000650 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
651 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 +0000652 machine_uint_t source_line = 1;
653 machine_uint_t bc = save_ip - code_info - code_info_size;
Damien Georgecbd2f742014-01-19 11:48:48 +0000654 //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
Damien George28eb5772014-01-25 11:43:20 +0000655 for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
656 bc -= *ci & 31;
657 source_line += *ci >> 5;
Damien George08335002014-01-18 23:24:36 +0000658 }
Damien George136b1492014-01-19 12:38:49 +0000659 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +0000660 }
661
Damien8f9e2ee2013-12-29 16:54:59 +0000662 while (currently_in_except_block) {
663 // nested exception
664
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200665 assert(exc_sp >= exc_stack);
Damien8f9e2ee2013-12-29 16:54:59 +0000666
667 // TODO make a proper message for nested exception
668 // at the moment we are just raising the very last exception (the one that caused the nested exception)
669
670 // move up to previous exception handler
Paul Sokolovsky16734202014-03-22 23:20:07 +0200671 currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); // restore previous state
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200672 exc_sp--; // pop back to previous exception handler
Damien8f9e2ee2013-12-29 16:54:59 +0000673 }
674
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200675 if (exc_sp >= exc_stack) {
Damien8f9e2ee2013-12-29 16:54:59 +0000676 // set flag to indicate that we are now handling an exception
677 currently_in_except_block = 1;
678
Damience89a212013-10-15 22:25:17 +0100679 // catch exception and pass to byte code
Paul Sokolovsky16734202014-03-22 23:20:07 +0200680 sp = MP_TAGPTR_PTR(exc_sp->val_sp);
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200681 ip = exc_sp->handler;
Damienc9f91972013-10-15 23:46:01 +0100682 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000683 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100684 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000685 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000686
Damience89a212013-10-15 22:25:17 +0100687 } else {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000688 // propagate exception to higher level
689 // TODO what to do about ip and sp? they don't really make sense at this point
690 fastn[0] = nlr.ret_val; // must put exception here because sp is invalid
691 return MP_VM_RETURN_EXCEPTION;
Damience89a212013-10-15 22:25:17 +0100692 }
Damien429d7192013-10-04 19:53:11 +0100693 }
694 }
695}