blob: 77762a13af0811875121184109ce96bc9836f3c7 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdint.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
6
Damience89a212013-10-15 22:25:17 +01007#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01008#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "obj.h"
Damien429d7192013-10-04 19:53:11 +010012#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "bc0.h"
Damieneb19efb2013-10-10 22:06:54 +010014#include "bc.h"
Damien429d7192013-10-04 19:53:11 +010015
Paul Sokolovsky85193422014-01-31 19:45:15 +020016// Value stack grows up (this makes it incompatible with native C stack, but
17// makes sure that arguments to functions are in natural order arg1..argN
18// (Python semantics mandates left-to-right evaluation order, including for
19// function arguments). Stack pointer is pre-incremented and points at the
20// top element.
21// Exception stack also grows up, top element is also pointed at.
22
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +020023// Exception stack entry
24typedef struct _mp_exc_stack {
25 const byte *handler;
26 // bit 0 is saved currently_in_except_block value
27 machine_uint_t val_sp;
28 // We might only have 2 interesting cases here: SETUP_EXCEPT & SETUP_FINALLY,
29 // consider storing it in bit 1 of val_sp. TODO: SETUP_WITH?
30 byte opcode;
31} mp_exc_stack;
32
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020033// Exception stack unwind reasons (WHY_* in CPython-speak)
Damien Georgecbddb272014-02-01 20:08:18 +000034// TODO perhaps compress this to RETURN=0, JUMP>0, with number of unwinds
35// left to do encoded in the JUMP number
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020036typedef enum {
37 UNWIND_RETURN = 1,
Damien Georgecbddb272014-02-01 20:08:18 +000038 UNWIND_JUMP,
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020039} mp_unwind_reason_t;
40
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020041#define DECODE_UINT { \
42 unum = 0; \
43 do { \
44 unum = (unum << 7) + (*ip & 0x7f); \
45 } while ((*ip++ & 0x80) != 0); \
46}
Damien03c9cfb2013-11-05 22:06:08 +000047#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
48#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020049#define DECODE_QSTR { \
50 qst = 0; \
51 do { \
52 qst = (qst << 7) + (*ip & 0x7f); \
53 } while ((*ip++ & 0x80) != 0); \
54}
Damien George20006db2014-01-18 14:10:48 +000055#define PUSH(val) *++sp = (val)
56#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000057#define TOP() (*sp)
58#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010059
Damien Georgec8f78bc2014-02-15 22:55:00 +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, uint n_state, mp_obj_t *ret) {
Damien George20006db2014-01-18 14:10:48 +000061 // allocate state for locals and stack
62 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000063 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000064 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000065 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000066 }
Damien George20006db2014-01-18 14:10:48 +000067 mp_obj_t *sp = &state[0] - 1;
68
Damienbd254452013-10-16 20:39:12 +010069 // init args
Damien Georgefb083ea2014-02-01 18:29:40 +000070 for (uint i = 0; i < n_args; i++) {
Damien George20006db2014-01-18 14:10:48 +000071 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010072 }
Damien Georgefb083ea2014-02-01 18:29:40 +000073 for (uint i = 0; i < n_args2; i++) {
74 state[n_state - 1 - n_args - i] = args2[i];
75 }
Damien George08335002014-01-18 23:24:36 +000076
Damienbd254452013-10-16 20:39:12 +010077 const byte *ip = code;
Damien George6baf76e2013-12-30 22:32:17 +000078
Damien George08335002014-01-18 23:24:36 +000079 // get code info size
80 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
81 ip += code_info_size;
82
Damien George6baf76e2013-12-30 22:32:17 +000083 // execute prelude to make any cells (closed over variables)
84 {
85 for (uint n_local = *ip++; n_local > 0; n_local--) {
86 uint local_num = *ip++;
Damien Georgefb083ea2014-02-01 18:29:40 +000087 if (local_num < n_args + n_args2) {
Damien George20006db2014-01-18 14:10:48 +000088 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
Damien George6baf76e2013-12-30 22:32:17 +000089 } else {
Damien George20006db2014-01-18 14:10:48 +000090 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +000091 }
92 }
93 }
94
95 // execute the byte code
Damien Georgec8f78bc2014-02-15 22:55:00 +000096 mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp);
Damien George6baf76e2013-12-30 22:32:17 +000097
Damien Georgec8f78bc2014-02-15 22:55:00 +000098 switch (vm_return_kind) {
99 case MP_VM_RETURN_NORMAL:
100 *ret = *sp;
101 return MP_VM_RETURN_NORMAL;
102 case MP_VM_RETURN_EXCEPTION:
103 *ret = state[n_state - 1];
104 return MP_VM_RETURN_EXCEPTION;
105 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
106 default:
107 assert(0);
108 *ret = mp_const_none;
109 return MP_VM_RETURN_NORMAL;
110 }
Damienbd254452013-10-16 20:39:12 +0100111}
112
Damien George20006db2014-01-18 14:10:48 +0000113// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
114// sp points to bottom of stack which grows up
Damien Georgec8f78bc2014-02-15 22:55:00 +0000115// returns:
116// MP_VM_RETURN_NORMAL, sp valid, return value in *sp
117// MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
118// MP_VM_RETURN_EXCEPTION, exception in fastn[0]
119mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out) {
Damienc9f91972013-10-15 23:46:01 +0100120 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
121
Damienbd254452013-10-16 20:39:12 +0100122 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +0000123 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +0100124 machine_uint_t unum;
Damien Georgecbd2f742014-01-19 11:48:48 +0000125 qstr qst;
Damiend99b0522013-12-21 18:17:45 +0000126 mp_obj_t obj1, obj2;
Damience89a212013-10-15 22:25:17 +0100127 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +0100128
Damien8f9e2ee2013-12-29 16:54:59 +0000129 volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200130 mp_exc_stack exc_stack[4];
131 mp_exc_stack *volatile exc_sp = &exc_stack[0] - 1; // stack grows up, exc_sp points to top of stack
Damien George08335002014-01-18 23:24:36 +0000132 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 +0100133
Damience89a212013-10-15 22:25:17 +0100134 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +0100135 for (;;) {
Damience89a212013-10-15 22:25:17 +0100136 if (nlr_push(&nlr) == 0) {
137 // loop to execute byte code
138 for (;;) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200139dispatch_loop:
Damien George08335002014-01-18 23:24:36 +0000140 save_ip = ip;
Damience89a212013-10-15 22:25:17 +0100141 int op = *ip++;
142 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000143 case MP_BC_LOAD_CONST_FALSE:
144 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +0100145 break;
Damien429d7192013-10-04 19:53:11 +0100146
Damiend99b0522013-12-21 18:17:45 +0000147 case MP_BC_LOAD_CONST_NONE:
148 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100149 break;
Damien429d7192013-10-04 19:53:11 +0100150
Damiend99b0522013-12-21 18:17:45 +0000151 case MP_BC_LOAD_CONST_TRUE:
152 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100153 break;
Damien429d7192013-10-04 19:53:11 +0100154
Damien Georgee9906ac2014-01-04 18:44:46 +0000155 case MP_BC_LOAD_CONST_ELLIPSIS:
156 PUSH(mp_const_ellipsis);
157 break;
158
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200159 case MP_BC_LOAD_CONST_SMALL_INT: {
Damien George4d79d5d2014-02-20 00:00:04 +0000160 machine_int_t num = 0;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200161 if ((ip[0] & 0x40) != 0) {
162 // Number is negative
163 num--;
164 }
165 do {
166 num = (num << 7) | (*ip & 0x7f);
167 } while ((*ip++ & 0x80) != 0);
168 PUSH(MP_OBJ_NEW_SMALL_INT(num));
Damience89a212013-10-15 22:25:17 +0100169 break;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200170 }
Damience89a212013-10-15 22:25:17 +0100171
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200172 case MP_BC_LOAD_CONST_INT:
173 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000174 PUSH(mp_obj_new_int_from_long_str(qstr_str(qst)));
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200175 break;
176
Damiend99b0522013-12-21 18:17:45 +0000177 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000178 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000179 PUSH(rt_load_const_dec(qst));
Damien7410e442013-11-02 19:47:57 +0000180 break;
181
Damiend99b0522013-12-21 18:17:45 +0000182 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100183 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000184 PUSH(rt_load_const_str(qst)); // TODO
Damience89a212013-10-15 22:25:17 +0100185 break;
186
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200187 case MP_BC_LOAD_CONST_BYTES:
188 DECODE_QSTR;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200189 PUSH(rt_load_const_bytes(qst));
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200190 break;
191
Damiend99b0522013-12-21 18:17:45 +0000192 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100193 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000194 PUSH(rt_load_const_str(qst));
Damience89a212013-10-15 22:25:17 +0100195 break;
196
Damiend99b0522013-12-21 18:17:45 +0000197 case MP_BC_LOAD_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000198 PUSH(fastn[0]);
Damience89a212013-10-15 22:25:17 +0100199 break;
200
Damiend99b0522013-12-21 18:17:45 +0000201 case MP_BC_LOAD_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000202 PUSH(fastn[-1]);
Damience89a212013-10-15 22:25:17 +0100203 break;
204
Damiend99b0522013-12-21 18:17:45 +0000205 case MP_BC_LOAD_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000206 PUSH(fastn[-2]);
Damience89a212013-10-15 22:25:17 +0100207 break;
208
Damiend99b0522013-12-21 18:17:45 +0000209 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100210 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000211 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100212 break;
213
Damiend99b0522013-12-21 18:17:45 +0000214 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000215 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000216 PUSH(rt_get_cell(fastn[-unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000217 break;
218
Damiend99b0522013-12-21 18:17:45 +0000219 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100220 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000221 PUSH(rt_load_name(qst));
Damience89a212013-10-15 22:25:17 +0100222 break;
223
Damiend99b0522013-12-21 18:17:45 +0000224 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100225 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000226 PUSH(rt_load_global(qst));
Damience89a212013-10-15 22:25:17 +0100227 break;
228
Damiend99b0522013-12-21 18:17:45 +0000229 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100230 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000231 SET_TOP(rt_load_attr(TOP(), qst));
Damience89a212013-10-15 22:25:17 +0100232 break;
233
Damiend99b0522013-12-21 18:17:45 +0000234 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100235 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000236 rt_load_method(*sp, qst, sp);
Damien George20006db2014-01-18 14:10:48 +0000237 sp += 1;
Damience89a212013-10-15 22:25:17 +0100238 break;
239
Damiend99b0522013-12-21 18:17:45 +0000240 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100241 PUSH(rt_load_build_class());
242 break;
243
Damiend99b0522013-12-21 18:17:45 +0000244 case MP_BC_STORE_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000245 fastn[0] = POP();
Damience89a212013-10-15 22:25:17 +0100246 break;
247
Damiend99b0522013-12-21 18:17:45 +0000248 case MP_BC_STORE_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000249 fastn[-1] = POP();
Damience89a212013-10-15 22:25:17 +0100250 break;
251
Damiend99b0522013-12-21 18:17:45 +0000252 case MP_BC_STORE_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000253 fastn[-2] = POP();
Damience89a212013-10-15 22:25:17 +0100254 break;
255
Damiend99b0522013-12-21 18:17:45 +0000256 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100257 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000258 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100259 break;
260
Damiend99b0522013-12-21 18:17:45 +0000261 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000262 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000263 rt_set_cell(fastn[-unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000264 break;
265
Damiend99b0522013-12-21 18:17:45 +0000266 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100267 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000268 rt_store_name(qst, POP());
Damience89a212013-10-15 22:25:17 +0100269 break;
270
Damiend99b0522013-12-21 18:17:45 +0000271 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000272 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000273 rt_store_global(qst, POP());
Damien6addc892013-11-04 23:04:50 +0000274 break;
275
Damiend99b0522013-12-21 18:17:45 +0000276 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100277 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000278 rt_store_attr(sp[0], qst, sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000279 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100280 break;
281
Damiend99b0522013-12-21 18:17:45 +0000282 case MP_BC_STORE_SUBSCR:
Damien George20006db2014-01-18 14:10:48 +0000283 rt_store_subscr(sp[-1], sp[0], sp[-2]);
284 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100285 break;
286
Damiend99b0522013-12-21 18:17:45 +0000287 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000288 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100289 PUSH(obj1);
290 break;
291
Damiend99b0522013-12-21 18:17:45 +0000292 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000293 sp += 2;
294 sp[0] = sp[-2];
295 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100296 break;
297
Damiend99b0522013-12-21 18:17:45 +0000298 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000299 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100300 break;
301
Damiend99b0522013-12-21 18:17:45 +0000302 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000303 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000304 sp[0] = sp[-1];
305 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000306 break;
307
Damiend99b0522013-12-21 18:17:45 +0000308 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100309 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000310 sp[0] = sp[-1];
311 sp[-1] = sp[-2];
312 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100313 break;
314
Damiend99b0522013-12-21 18:17:45 +0000315 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000316 DECODE_SLABEL;
317 ip += unum;
Damience89a212013-10-15 22:25:17 +0100318 break;
319
Damiend99b0522013-12-21 18:17:45 +0000320 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000321 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100322 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000323 ip += unum;
Damience89a212013-10-15 22:25:17 +0100324 }
325 break;
326
Damiend99b0522013-12-21 18:17:45 +0000327 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000328 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100329 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000330 ip += unum;
Damience89a212013-10-15 22:25:17 +0100331 }
332 break;
333
Damiend99b0522013-12-21 18:17:45 +0000334 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000335 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000336 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000337 ip += unum;
338 } else {
Damien George20006db2014-01-18 14:10:48 +0000339 sp--;
Damien94658e22013-11-09 20:12:32 +0000340 }
341 break;
342
Damiend99b0522013-12-21 18:17:45 +0000343 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000344 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000345 if (rt_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000346 sp--;
Damien94658e22013-11-09 20:12:32 +0000347 } else {
348 ip += unum;
349 }
350 break;
351
Damience89a212013-10-15 22:25:17 +0100352 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000353 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100354 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000355 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100356 break;
357 */
358
Damien Georgecbddb272014-02-01 20:08:18 +0000359 case MP_BC_UNWIND_JUMP:
360 DECODE_SLABEL;
361 PUSH((void*)(ip + unum)); // push destination ip for jump
362 PUSH((void*)(machine_uint_t)(*ip)); // push number of exception handlers to unwind
363unwind_jump:
364 unum = (machine_uint_t)POP(); // get number of exception handlers to unwind
365 while (unum > 0) {
366 unum -= 1;
367 assert(exc_sp >= exc_stack);
368 if (exc_sp->opcode == MP_BC_SETUP_FINALLY) {
369 // We're going to run "finally" code as a coroutine
370 // (not calling it recursively). Set up a sentinel
371 // on a stack so it can return back to us when it is
372 // done (when END_FINALLY reached).
373 PUSH((void*)unum); // push number of exception handlers left to unwind
374 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP)); // push sentinel
375 ip = exc_sp->handler; // get exception handler byte code address
376 exc_sp--; // pop exception handler
377 goto dispatch_loop; // run the exception handler
378 }
379 exc_sp--;
380 }
381 ip = (const byte*)POP(); // pop destination ip for jump
Damien George600ae732014-01-21 23:48:04 +0000382 break;
383
Damien02a7c412013-12-29 18:48:37 +0000384 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000385 case MP_BC_SETUP_EXCEPT:
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200386 case MP_BC_SETUP_FINALLY:
Damien03c9cfb2013-11-05 22:06:08 +0000387 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200388 ++exc_sp;
389 exc_sp->opcode = op;
390 exc_sp->handler = ip + unum;
391 exc_sp->val_sp = (((machine_uint_t)sp) | currently_in_except_block);
Damien8f9e2ee2013-12-29 16:54:59 +0000392 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100393 break;
394
Damiend99b0522013-12-21 18:17:45 +0000395 case MP_BC_END_FINALLY:
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200396 // not fully implemented
Damience89a212013-10-15 22:25:17 +0100397 // if TOS is an exception, reraises the exception (3 values on TOS)
Damience89a212013-10-15 22:25:17 +0100398 // if TOS is None, just pops it and continues
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200399 // if TOS is an integer, does something else
Damience89a212013-10-15 22:25:17 +0100400 // else error
Damien Georgec5966122014-02-15 16:10:44 +0000401 if (mp_obj_is_exception_instance(TOP())) {
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200402 nlr_jump(TOP());
403 }
404 if (TOP() == mp_const_none) {
405 sp--;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200406 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
407 // We finished "finally" coroutine and now dispatch back
408 // to our caller, based on TOS value
409 mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
410 switch (reason) {
411 case UNWIND_RETURN:
412 goto unwind_return;
Damien Georgecbddb272014-02-01 20:08:18 +0000413 case UNWIND_JUMP:
414 goto unwind_jump;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200415 }
416 assert(0);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200417 } else {
418 assert(0);
419 }
Damience89a212013-10-15 22:25:17 +0100420 break;
421
Damiend99b0522013-12-21 18:17:45 +0000422 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000423 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100424 break;
425
Damiend99b0522013-12-21 18:17:45 +0000426 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000427 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000428 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000429 if (obj1 == mp_const_stop_iteration) {
Damien George20006db2014-01-18 14:10:48 +0000430 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000431 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100432 } else {
433 PUSH(obj1); // push the next iteration value
434 }
435 break;
436
Damien02a7c412013-12-29 18:48:37 +0000437 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000438 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000439 // we are exiting an exception handler, so pop the last one of the exception-stack
440 assert(exc_sp >= &exc_stack[0]);
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200441 currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
442 exc_sp--; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100443 break;
444
Damien George600ae732014-01-21 23:48:04 +0000445 // matched against: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000446 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100447 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100448 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
Damienc9f91972013-10-15 23:46:01 +0100449 assert(exc_sp >= &exc_stack[0]);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200450 assert(currently_in_except_block);
Damiend99b0522013-12-21 18:17:45 +0000451 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100452 //exc_sp--; // discard ip
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200453 currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
454 exc_sp--; // pop back to previous exception handler
Damien George20006db2014-01-18 14:10:48 +0000455 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100456 break;
457
Damien George9aa2a522014-02-01 23:04:09 +0000458 case MP_BC_NOT:
459 if (TOP() == mp_const_true) {
460 SET_TOP(mp_const_false);
461 } else {
462 SET_TOP(mp_const_true);
463 }
464 break;
465
Damiend99b0522013-12-21 18:17:45 +0000466 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000467 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000468 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000469 break;
470
Damiend99b0522013-12-21 18:17:45 +0000471 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100472 unum = *ip++;
473 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000474 obj1 = TOP();
475 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100476 break;
477
Damiend99b0522013-12-21 18:17:45 +0000478 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100479 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000480 sp -= unum - 1;
481 SET_TOP(rt_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100482 break;
483
Damiend99b0522013-12-21 18:17:45 +0000484 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100485 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000486 sp -= unum - 1;
487 SET_TOP(rt_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100488 break;
489
Damiend99b0522013-12-21 18:17:45 +0000490 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100491 DECODE_UINT;
492 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien George20006db2014-01-18 14:10:48 +0000493 rt_list_append(sp[-unum], sp[0]);
494 sp--;
Damienc226dca2013-10-16 16:12:52 +0100495 break;
496
Damiend99b0522013-12-21 18:17:45 +0000497 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100498 DECODE_UINT;
499 PUSH(rt_build_map(unum));
500 break;
501
Damiend99b0522013-12-21 18:17:45 +0000502 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000503 sp -= 2;
504 rt_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100505 break;
506
Damiend99b0522013-12-21 18:17:45 +0000507 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100508 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000509 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
510 rt_store_map(sp[-unum - 1], sp[0], sp[-1]);
511 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100512 break;
513
Damiend99b0522013-12-21 18:17:45 +0000514 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100515 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000516 sp -= unum - 1;
517 SET_TOP(rt_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100518 break;
519
Damiend99b0522013-12-21 18:17:45 +0000520 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100521 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000522 // I think it's guaranteed by the compiler that sp[-unum] is a set
523 rt_store_set(sp[-unum], sp[0]);
524 sp--;
Damienc12aa462013-10-16 20:57:49 +0100525 break;
526
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200527#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200528 case MP_BC_BUILD_SLICE:
529 DECODE_UINT;
530 if (unum == 2) {
531 obj2 = POP();
532 obj1 = TOP();
533 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
534 } else {
535 printf("3-argument slice is not supported\n");
536 assert(0);
537 }
538 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200539#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200540
Damiend99b0522013-12-21 18:17:45 +0000541 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000542 DECODE_UINT;
Damien George932bf1c2014-01-18 23:42:49 +0000543 rt_unpack_sequence(sp[0], unum, sp);
Damien George20006db2014-01-18 14:10:48 +0000544 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000545 break;
546
Damiend99b0522013-12-21 18:17:45 +0000547 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100548 DECODE_UINT;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200549 PUSH(rt_make_function_from_id(unum, MP_OBJ_NULL));
550 break;
551
552 case MP_BC_MAKE_FUNCTION_DEFARGS:
553 DECODE_UINT;
554 SET_TOP(rt_make_function_from_id(unum, TOP()));
Damience89a212013-10-15 22:25:17 +0100555 break;
556
Damiend99b0522013-12-21 18:17:45 +0000557 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000558 DECODE_UINT;
Damien George08d07552014-01-29 18:58:52 +0000559 SET_TOP(rt_make_closure_from_id(unum, TOP()));
Damien9ecbcff2013-12-11 00:41:43 +0000560 break;
561
Damiend99b0522013-12-21 18:17:45 +0000562 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100563 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000564 // unum & 0xff == n_positional
565 // (unum >> 8) & 0xff == n_keyword
566 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
567 SET_TOP(rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100568 break;
569
Damiend99b0522013-12-21 18:17:45 +0000570 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100571 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000572 // unum & 0xff == n_positional
573 // (unum >> 8) & 0xff == n_keyword
574 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
575 SET_TOP(rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100576 break;
577
Damiend99b0522013-12-21 18:17:45 +0000578 case MP_BC_RETURN_VALUE:
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200579unwind_return:
580 while (exc_sp >= exc_stack) {
581 if (exc_sp->opcode == MP_BC_SETUP_FINALLY) {
582 // We're going to run "finally" code as a coroutine
583 // (not calling it recursively). Set up a sentinel
584 // on a stack so it can return back to us when it is
585 // done (when END_FINALLY reached).
586 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN));
587 ip = exc_sp->handler;
588 // We don't need to do anything with sp, finally is just
589 // syntactic sugar for sequential execution??
590 // sp =
591 exc_sp--;
592 goto dispatch_loop;
593 }
594 exc_sp--;
595 }
Damience89a212013-10-15 22:25:17 +0100596 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100597 *sp_in_out = sp;
Damien George66327002014-01-02 18:20:41 +0000598 assert(exc_sp == &exc_stack[0] - 1);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000599 return MP_VM_RETURN_NORMAL;
Damienbd254452013-10-16 20:39:12 +0100600
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200601 case MP_BC_RAISE_VARARGS:
602 unum = *ip++;
603 assert(unum == 1);
604 obj1 = POP();
Damien Georgec5966122014-02-15 16:10:44 +0000605 nlr_jump(rt_make_raise_obj(obj1));
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200606
Damiend99b0522013-12-21 18:17:45 +0000607 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100608 nlr_pop();
609 *ip_in_out = ip;
Damienbd254452013-10-16 20:39:12 +0100610 *sp_in_out = sp;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000611 return MP_VM_RETURN_YIELD;
Damience89a212013-10-15 22:25:17 +0100612
Damiend99b0522013-12-21 18:17:45 +0000613 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000614 DECODE_QSTR;
615 obj1 = POP();
Damien Georgecbd2f742014-01-19 11:48:48 +0000616 SET_TOP(rt_import_name(qst, obj1, TOP()));
Damiendb4c3612013-12-10 17:27:24 +0000617 break;
618
Damiend99b0522013-12-21 18:17:45 +0000619 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000620 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000621 obj1 = rt_import_from(TOP(), qst);
Damiendb4c3612013-12-10 17:27:24 +0000622 PUSH(obj1);
623 break;
624
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200625 case MP_BC_IMPORT_STAR:
Damien Georgeaa9b74f2014-02-14 23:06:33 +0000626 rt_import_all(POP());
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200627 break;
628
Damience89a212013-10-15 22:25:17 +0100629 default:
Damien03c9cfb2013-11-05 22:06:08 +0000630 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100631 assert(0);
632 nlr_pop();
Damien Georgec8f78bc2014-02-15 22:55:00 +0000633 return MP_VM_RETURN_NORMAL;
Damien429d7192013-10-04 19:53:11 +0100634 }
Damience89a212013-10-15 22:25:17 +0100635 }
Damien429d7192013-10-04 19:53:11 +0100636
Damience89a212013-10-15 22:25:17 +0100637 } else {
638 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100639
Damien George08335002014-01-18 23:24:36 +0000640 // set file and line number that the exception occurred at
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200641 // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
642 // But consider how to handle nested exceptions.
Damien Georgec5966122014-02-15 16:10:44 +0000643 if (mp_obj_is_exception_instance(nlr.ret_val)) {
Damien George08335002014-01-18 23:24:36 +0000644 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 +0000645 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
646 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 +0000647 machine_uint_t source_line = 1;
648 machine_uint_t bc = save_ip - code_info - code_info_size;
Damien Georgecbd2f742014-01-19 11:48:48 +0000649 //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
Damien George28eb5772014-01-25 11:43:20 +0000650 for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
651 bc -= *ci & 31;
652 source_line += *ci >> 5;
Damien George08335002014-01-18 23:24:36 +0000653 }
Damien George136b1492014-01-19 12:38:49 +0000654 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +0000655 }
656
Damien8f9e2ee2013-12-29 16:54:59 +0000657 while (currently_in_except_block) {
658 // nested exception
659
660 assert(exc_sp >= &exc_stack[0]);
661
662 // TODO make a proper message for nested exception
663 // at the moment we are just raising the very last exception (the one that caused the nested exception)
664
665 // move up to previous exception handler
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200666 currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
667 exc_sp--; // pop back to previous exception handler
Damien8f9e2ee2013-12-29 16:54:59 +0000668 }
669
Damienc9f91972013-10-15 23:46:01 +0100670 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000671 // set flag to indicate that we are now handling an exception
672 currently_in_except_block = 1;
673
Damience89a212013-10-15 22:25:17 +0100674 // catch exception and pass to byte code
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200675 sp = (mp_obj_t*)(exc_sp->val_sp & (~((machine_uint_t)1)));
676 ip = exc_sp->handler;
Damienc9f91972013-10-15 23:46:01 +0100677 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000678 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100679 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000680 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000681
Damience89a212013-10-15 22:25:17 +0100682 } else {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000683 // propagate exception to higher level
684 // TODO what to do about ip and sp? they don't really make sense at this point
685 fastn[0] = nlr.ret_val; // must put exception here because sp is invalid
686 return MP_VM_RETURN_EXCEPTION;
Damience89a212013-10-15 22:25:17 +0100687 }
Damien429d7192013-10-04 19:53:11 +0100688 }
689 }
690}