blob: 2c953137cde7fa7a6fc17ff89c0eba15487fc971 [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)
34typedef enum {
35 UNWIND_RETURN = 1,
36 UNWIND_BREAK,
37 UNWIND_CONTINUE,
38} mp_unwind_reason_t;
39
Damien429d7192013-10-04 19:53:11 +010040#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
Damien03c9cfb2013-11-05 22:06:08 +000041#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
42#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Damien Georgecbd2f742014-01-19 11:48:48 +000043#define DECODE_QSTR do { qst = *ip++; if (qst > 127) { qst = ((qst & 0x3f) << 8) | (*ip++); } } while (0)
Damien George20006db2014-01-18 14:10:48 +000044#define PUSH(val) *++sp = (val)
45#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000046#define TOP() (*sp)
47#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010048
Damien Georgefb083ea2014-02-01 18:29:40 +000049mp_obj_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) {
Damien George20006db2014-01-18 14:10:48 +000050 // allocate state for locals and stack
51 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000052 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000053 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000054 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000055 }
Damien George20006db2014-01-18 14:10:48 +000056 mp_obj_t *sp = &state[0] - 1;
57
Damienbd254452013-10-16 20:39:12 +010058 // init args
Damien Georgefb083ea2014-02-01 18:29:40 +000059 for (uint i = 0; i < n_args; i++) {
Damien George20006db2014-01-18 14:10:48 +000060 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010061 }
Damien Georgefb083ea2014-02-01 18:29:40 +000062 for (uint i = 0; i < n_args2; i++) {
63 state[n_state - 1 - n_args - i] = args2[i];
64 }
Damien George08335002014-01-18 23:24:36 +000065
Damienbd254452013-10-16 20:39:12 +010066 const byte *ip = code;
Damien George6baf76e2013-12-30 22:32:17 +000067
Damien George08335002014-01-18 23:24:36 +000068 // get code info size
69 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
70 ip += code_info_size;
71
Damien George6baf76e2013-12-30 22:32:17 +000072 // execute prelude to make any cells (closed over variables)
73 {
74 for (uint n_local = *ip++; n_local > 0; n_local--) {
75 uint local_num = *ip++;
Damien Georgefb083ea2014-02-01 18:29:40 +000076 if (local_num < n_args + n_args2) {
Damien George20006db2014-01-18 14:10:48 +000077 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
Damien George6baf76e2013-12-30 22:32:17 +000078 } else {
Damien George20006db2014-01-18 14:10:48 +000079 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +000080 }
81 }
82 }
83
84 // execute the byte code
Damien George08335002014-01-18 23:24:36 +000085 if (mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp)) {
Damienbd254452013-10-16 20:39:12 +010086 // it shouldn't yield
87 assert(0);
88 }
Damien George6baf76e2013-12-30 22:32:17 +000089
Damien4ebb32f2013-11-02 14:33:10 +000090 // TODO check fails if, eg, return from within for loop
91 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010092 return *sp;
93}
94
Damien George20006db2014-01-18 14:10:48 +000095// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
96// sp points to bottom of stack which grows up
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +020097// returns true if bytecode yielded
Damien George08335002014-01-18 23:24:36 +000098bool 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 +010099 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
100
Damienbd254452013-10-16 20:39:12 +0100101 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +0000102 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +0100103 machine_uint_t unum;
Damien Georgecbd2f742014-01-19 11:48:48 +0000104 qstr qst;
Damiend99b0522013-12-21 18:17:45 +0000105 mp_obj_t obj1, obj2;
Damience89a212013-10-15 22:25:17 +0100106 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +0100107
Damien8f9e2ee2013-12-29 16:54:59 +0000108 volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200109 mp_exc_stack exc_stack[4];
110 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 +0000111 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 +0100112
Damience89a212013-10-15 22:25:17 +0100113 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +0100114 for (;;) {
Damience89a212013-10-15 22:25:17 +0100115 if (nlr_push(&nlr) == 0) {
116 // loop to execute byte code
117 for (;;) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200118dispatch_loop:
Damien George08335002014-01-18 23:24:36 +0000119 save_ip = ip;
Damience89a212013-10-15 22:25:17 +0100120 int op = *ip++;
121 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000122 case MP_BC_LOAD_CONST_FALSE:
123 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +0100124 break;
Damien429d7192013-10-04 19:53:11 +0100125
Damiend99b0522013-12-21 18:17:45 +0000126 case MP_BC_LOAD_CONST_NONE:
127 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100128 break;
Damien429d7192013-10-04 19:53:11 +0100129
Damiend99b0522013-12-21 18:17:45 +0000130 case MP_BC_LOAD_CONST_TRUE:
131 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100132 break;
Damien429d7192013-10-04 19:53:11 +0100133
Damien Georgee9906ac2014-01-04 18:44:46 +0000134 case MP_BC_LOAD_CONST_ELLIPSIS:
135 PUSH(mp_const_ellipsis);
136 break;
137
Damiend99b0522013-12-21 18:17:45 +0000138 case MP_BC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +0000139 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +0000140 ip += 3;
Paul Sokolovskye5ee1692014-01-06 17:49:46 +0200141 PUSH(MP_OBJ_NEW_SMALL_INT(unum));
Damience89a212013-10-15 22:25:17 +0100142 break;
143
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200144 case MP_BC_LOAD_CONST_INT:
145 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000146 PUSH(mp_obj_new_int_from_long_str(qstr_str(qst)));
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200147 break;
148
Damiend99b0522013-12-21 18:17:45 +0000149 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000150 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000151 PUSH(rt_load_const_dec(qst));
Damien7410e442013-11-02 19:47:57 +0000152 break;
153
Damiend99b0522013-12-21 18:17:45 +0000154 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100155 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000156 PUSH(rt_load_const_str(qst)); // TODO
Damience89a212013-10-15 22:25:17 +0100157 break;
158
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200159 case MP_BC_LOAD_CONST_BYTES:
160 DECODE_QSTR;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200161 PUSH(rt_load_const_bytes(qst));
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200162 break;
163
Damiend99b0522013-12-21 18:17:45 +0000164 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100165 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000166 PUSH(rt_load_const_str(qst));
Damience89a212013-10-15 22:25:17 +0100167 break;
168
Damiend99b0522013-12-21 18:17:45 +0000169 case MP_BC_LOAD_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000170 PUSH(fastn[0]);
Damience89a212013-10-15 22:25:17 +0100171 break;
172
Damiend99b0522013-12-21 18:17:45 +0000173 case MP_BC_LOAD_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000174 PUSH(fastn[-1]);
Damience89a212013-10-15 22:25:17 +0100175 break;
176
Damiend99b0522013-12-21 18:17:45 +0000177 case MP_BC_LOAD_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000178 PUSH(fastn[-2]);
Damience89a212013-10-15 22:25:17 +0100179 break;
180
Damiend99b0522013-12-21 18:17:45 +0000181 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100182 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000183 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100184 break;
185
Damiend99b0522013-12-21 18:17:45 +0000186 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000187 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000188 PUSH(rt_get_cell(fastn[-unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000189 break;
190
Damiend99b0522013-12-21 18:17:45 +0000191 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100192 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000193 PUSH(rt_load_name(qst));
Damience89a212013-10-15 22:25:17 +0100194 break;
195
Damiend99b0522013-12-21 18:17:45 +0000196 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100197 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000198 PUSH(rt_load_global(qst));
Damience89a212013-10-15 22:25:17 +0100199 break;
200
Damiend99b0522013-12-21 18:17:45 +0000201 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100202 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000203 SET_TOP(rt_load_attr(TOP(), qst));
Damience89a212013-10-15 22:25:17 +0100204 break;
205
Damiend99b0522013-12-21 18:17:45 +0000206 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100207 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000208 rt_load_method(*sp, qst, sp);
Damien George20006db2014-01-18 14:10:48 +0000209 sp += 1;
Damience89a212013-10-15 22:25:17 +0100210 break;
211
Damiend99b0522013-12-21 18:17:45 +0000212 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100213 PUSH(rt_load_build_class());
214 break;
215
Damiend99b0522013-12-21 18:17:45 +0000216 case MP_BC_STORE_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000217 fastn[0] = POP();
Damience89a212013-10-15 22:25:17 +0100218 break;
219
Damiend99b0522013-12-21 18:17:45 +0000220 case MP_BC_STORE_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000221 fastn[-1] = POP();
Damience89a212013-10-15 22:25:17 +0100222 break;
223
Damiend99b0522013-12-21 18:17:45 +0000224 case MP_BC_STORE_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000225 fastn[-2] = POP();
Damience89a212013-10-15 22:25:17 +0100226 break;
227
Damiend99b0522013-12-21 18:17:45 +0000228 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100229 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000230 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100231 break;
232
Damiend99b0522013-12-21 18:17:45 +0000233 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000234 DECODE_UINT;
Damien Georged0691cc2014-01-29 20:30:52 +0000235 rt_set_cell(fastn[-unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000236 break;
237
Damiend99b0522013-12-21 18:17:45 +0000238 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100239 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000240 rt_store_name(qst, POP());
Damience89a212013-10-15 22:25:17 +0100241 break;
242
Damiend99b0522013-12-21 18:17:45 +0000243 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000244 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000245 rt_store_global(qst, POP());
Damien6addc892013-11-04 23:04:50 +0000246 break;
247
Damiend99b0522013-12-21 18:17:45 +0000248 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100249 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000250 rt_store_attr(sp[0], qst, sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000251 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100252 break;
253
Damiend99b0522013-12-21 18:17:45 +0000254 case MP_BC_STORE_SUBSCR:
Damien George20006db2014-01-18 14:10:48 +0000255 rt_store_subscr(sp[-1], sp[0], sp[-2]);
256 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100257 break;
258
Damiend99b0522013-12-21 18:17:45 +0000259 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000260 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100261 PUSH(obj1);
262 break;
263
Damiend99b0522013-12-21 18:17:45 +0000264 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000265 sp += 2;
266 sp[0] = sp[-2];
267 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100268 break;
269
Damiend99b0522013-12-21 18:17:45 +0000270 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000271 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100272 break;
273
Damiend99b0522013-12-21 18:17:45 +0000274 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000275 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000276 sp[0] = sp[-1];
277 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000278 break;
279
Damiend99b0522013-12-21 18:17:45 +0000280 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100281 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000282 sp[0] = sp[-1];
283 sp[-1] = sp[-2];
284 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100285 break;
286
Damiend99b0522013-12-21 18:17:45 +0000287 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000288 DECODE_SLABEL;
289 ip += unum;
Damience89a212013-10-15 22:25:17 +0100290 break;
291
Damiend99b0522013-12-21 18:17:45 +0000292 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000293 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100294 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000295 ip += unum;
Damience89a212013-10-15 22:25:17 +0100296 }
297 break;
298
Damiend99b0522013-12-21 18:17:45 +0000299 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000300 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100301 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000302 ip += unum;
Damience89a212013-10-15 22:25:17 +0100303 }
304 break;
305
Damiend99b0522013-12-21 18:17:45 +0000306 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000307 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000308 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000309 ip += unum;
310 } else {
Damien George20006db2014-01-18 14:10:48 +0000311 sp--;
Damien94658e22013-11-09 20:12:32 +0000312 }
313 break;
314
Damiend99b0522013-12-21 18:17:45 +0000315 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000316 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000317 if (rt_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000318 sp--;
Damien94658e22013-11-09 20:12:32 +0000319 } else {
320 ip += unum;
321 }
322 break;
323
Damience89a212013-10-15 22:25:17 +0100324 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000325 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100326 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000327 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100328 break;
329 */
330
Damien George600ae732014-01-21 23:48:04 +0000331 // TODO this might need more sophisticated handling when breaking from within an except
332 case MP_BC_BREAK_LOOP:
333 DECODE_ULABEL;
334 ip += unum;
335 break;
336
337 // TODO this might need more sophisticated handling when breaking from within an except
338 case MP_BC_CONTINUE_LOOP:
339 DECODE_ULABEL;
340 ip += unum;
341 break;
342
Damien02a7c412013-12-29 18:48:37 +0000343 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000344 case MP_BC_SETUP_EXCEPT:
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200345 case MP_BC_SETUP_FINALLY:
Damien03c9cfb2013-11-05 22:06:08 +0000346 DECODE_ULABEL; // except labels are always forward
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200347 ++exc_sp;
348 exc_sp->opcode = op;
349 exc_sp->handler = ip + unum;
350 exc_sp->val_sp = (((machine_uint_t)sp) | currently_in_except_block);
Damien8f9e2ee2013-12-29 16:54:59 +0000351 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100352 break;
353
Damiend99b0522013-12-21 18:17:45 +0000354 case MP_BC_END_FINALLY:
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200355 // not fully implemented
Damience89a212013-10-15 22:25:17 +0100356 // if TOS is an exception, reraises the exception (3 values on TOS)
Damience89a212013-10-15 22:25:17 +0100357 // if TOS is None, just pops it and continues
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200358 // if TOS is an integer, does something else
Damience89a212013-10-15 22:25:17 +0100359 // else error
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200360 if (MP_OBJ_IS_TYPE(TOP(), &exception_type)) {
361 nlr_jump(TOP());
362 }
363 if (TOP() == mp_const_none) {
364 sp--;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200365 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
366 // We finished "finally" coroutine and now dispatch back
367 // to our caller, based on TOS value
368 mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
369 switch (reason) {
370 case UNWIND_RETURN:
371 goto unwind_return;
372 // TODO
373 case UNWIND_BREAK:
374 case UNWIND_CONTINUE:
375 ;
376 }
377 assert(0);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200378 } else {
379 assert(0);
380 }
Damience89a212013-10-15 22:25:17 +0100381 break;
382
Damiend99b0522013-12-21 18:17:45 +0000383 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000384 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100385 break;
386
Damiend99b0522013-12-21 18:17:45 +0000387 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000388 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000389 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000390 if (obj1 == mp_const_stop_iteration) {
Damien George20006db2014-01-18 14:10:48 +0000391 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000392 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100393 } else {
394 PUSH(obj1); // push the next iteration value
395 }
396 break;
397
Damien02a7c412013-12-29 18:48:37 +0000398 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000399 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000400 // we are exiting an exception handler, so pop the last one of the exception-stack
401 assert(exc_sp >= &exc_stack[0]);
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200402 currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
403 exc_sp--; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100404 break;
405
Damien George600ae732014-01-21 23:48:04 +0000406 // matched against: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000407 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100408 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100409 // 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 +0100410 assert(exc_sp >= &exc_stack[0]);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200411 assert(currently_in_except_block);
Damiend99b0522013-12-21 18:17:45 +0000412 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100413 //exc_sp--; // discard ip
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200414 currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
415 exc_sp--; // pop back to previous exception handler
Damien George20006db2014-01-18 14:10:48 +0000416 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100417 break;
418
Damiend99b0522013-12-21 18:17:45 +0000419 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000420 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000421 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000422 break;
423
Damiend99b0522013-12-21 18:17:45 +0000424 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100425 unum = *ip++;
426 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000427 obj1 = TOP();
428 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100429 break;
430
Damiend99b0522013-12-21 18:17:45 +0000431 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100432 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000433 sp -= unum - 1;
434 SET_TOP(rt_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100435 break;
436
Damiend99b0522013-12-21 18:17:45 +0000437 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100438 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000439 sp -= unum - 1;
440 SET_TOP(rt_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100441 break;
442
Damiend99b0522013-12-21 18:17:45 +0000443 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100444 DECODE_UINT;
445 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien George20006db2014-01-18 14:10:48 +0000446 rt_list_append(sp[-unum], sp[0]);
447 sp--;
Damienc226dca2013-10-16 16:12:52 +0100448 break;
449
Damiend99b0522013-12-21 18:17:45 +0000450 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100451 DECODE_UINT;
452 PUSH(rt_build_map(unum));
453 break;
454
Damiend99b0522013-12-21 18:17:45 +0000455 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000456 sp -= 2;
457 rt_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100458 break;
459
Damiend99b0522013-12-21 18:17:45 +0000460 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100461 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000462 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
463 rt_store_map(sp[-unum - 1], sp[0], sp[-1]);
464 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100465 break;
466
Damiend99b0522013-12-21 18:17:45 +0000467 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100468 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000469 sp -= unum - 1;
470 SET_TOP(rt_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100471 break;
472
Damiend99b0522013-12-21 18:17:45 +0000473 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100474 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000475 // I think it's guaranteed by the compiler that sp[-unum] is a set
476 rt_store_set(sp[-unum], sp[0]);
477 sp--;
Damienc12aa462013-10-16 20:57:49 +0100478 break;
479
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200480#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200481 case MP_BC_BUILD_SLICE:
482 DECODE_UINT;
483 if (unum == 2) {
484 obj2 = POP();
485 obj1 = TOP();
486 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
487 } else {
488 printf("3-argument slice is not supported\n");
489 assert(0);
490 }
491 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200492#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200493
Damiend99b0522013-12-21 18:17:45 +0000494 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000495 DECODE_UINT;
Damien George932bf1c2014-01-18 23:42:49 +0000496 rt_unpack_sequence(sp[0], unum, sp);
Damien George20006db2014-01-18 14:10:48 +0000497 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000498 break;
499
Damiend99b0522013-12-21 18:17:45 +0000500 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100501 DECODE_UINT;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200502 PUSH(rt_make_function_from_id(unum, MP_OBJ_NULL));
503 break;
504
505 case MP_BC_MAKE_FUNCTION_DEFARGS:
506 DECODE_UINT;
507 SET_TOP(rt_make_function_from_id(unum, TOP()));
Damience89a212013-10-15 22:25:17 +0100508 break;
509
Damiend99b0522013-12-21 18:17:45 +0000510 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000511 DECODE_UINT;
Damien George08d07552014-01-29 18:58:52 +0000512 SET_TOP(rt_make_closure_from_id(unum, TOP()));
Damien9ecbcff2013-12-11 00:41:43 +0000513 break;
514
Damiend99b0522013-12-21 18:17:45 +0000515 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100516 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000517 // unum & 0xff == n_positional
518 // (unum >> 8) & 0xff == n_keyword
519 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
520 SET_TOP(rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100521 break;
522
Damiend99b0522013-12-21 18:17:45 +0000523 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100524 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000525 // unum & 0xff == n_positional
526 // (unum >> 8) & 0xff == n_keyword
527 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
528 SET_TOP(rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100529 break;
530
Damiend99b0522013-12-21 18:17:45 +0000531 case MP_BC_RETURN_VALUE:
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200532unwind_return:
533 while (exc_sp >= exc_stack) {
534 if (exc_sp->opcode == MP_BC_SETUP_FINALLY) {
535 // We're going to run "finally" code as a coroutine
536 // (not calling it recursively). Set up a sentinel
537 // on a stack so it can return back to us when it is
538 // done (when END_FINALLY reached).
539 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN));
540 ip = exc_sp->handler;
541 // We don't need to do anything with sp, finally is just
542 // syntactic sugar for sequential execution??
543 // sp =
544 exc_sp--;
545 goto dispatch_loop;
546 }
547 exc_sp--;
548 }
Damience89a212013-10-15 22:25:17 +0100549 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100550 *sp_in_out = sp;
Damien George66327002014-01-02 18:20:41 +0000551 assert(exc_sp == &exc_stack[0] - 1);
Damienbd254452013-10-16 20:39:12 +0100552 return false;
553
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200554 case MP_BC_RAISE_VARARGS:
555 unum = *ip++;
556 assert(unum == 1);
557 obj1 = POP();
558 nlr_jump(obj1);
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200559
Damiend99b0522013-12-21 18:17:45 +0000560 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100561 nlr_pop();
562 *ip_in_out = ip;
Damienbd254452013-10-16 20:39:12 +0100563 *sp_in_out = sp;
564 return true;
Damience89a212013-10-15 22:25:17 +0100565
Damiend99b0522013-12-21 18:17:45 +0000566 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000567 DECODE_QSTR;
568 obj1 = POP();
Damien Georgecbd2f742014-01-19 11:48:48 +0000569 SET_TOP(rt_import_name(qst, obj1, TOP()));
Damiendb4c3612013-12-10 17:27:24 +0000570 break;
571
Damiend99b0522013-12-21 18:17:45 +0000572 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000573 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000574 obj1 = rt_import_from(TOP(), qst);
Damiendb4c3612013-12-10 17:27:24 +0000575 PUSH(obj1);
576 break;
577
Damience89a212013-10-15 22:25:17 +0100578 default:
Damien03c9cfb2013-11-05 22:06:08 +0000579 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100580 assert(0);
581 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100582 return false;
Damien429d7192013-10-04 19:53:11 +0100583 }
Damience89a212013-10-15 22:25:17 +0100584 }
Damien429d7192013-10-04 19:53:11 +0100585
Damience89a212013-10-15 22:25:17 +0100586 } else {
587 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100588
Damien George08335002014-01-18 23:24:36 +0000589 // set file and line number that the exception occurred at
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200590 // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
591 // But consider how to handle nested exceptions.
Damien George08335002014-01-18 23:24:36 +0000592 if (MP_OBJ_IS_TYPE(nlr.ret_val, &exception_type)) {
593 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 +0000594 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
595 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 +0000596 machine_uint_t source_line = 1;
597 machine_uint_t bc = save_ip - code_info - code_info_size;
Damien Georgecbd2f742014-01-19 11:48:48 +0000598 //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
Damien George28eb5772014-01-25 11:43:20 +0000599 for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
600 bc -= *ci & 31;
601 source_line += *ci >> 5;
Damien George08335002014-01-18 23:24:36 +0000602 }
Damien George136b1492014-01-19 12:38:49 +0000603 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +0000604 }
605
Damien8f9e2ee2013-12-29 16:54:59 +0000606 while (currently_in_except_block) {
607 // nested exception
608
609 assert(exc_sp >= &exc_stack[0]);
610
611 // TODO make a proper message for nested exception
612 // at the moment we are just raising the very last exception (the one that caused the nested exception)
613
614 // move up to previous exception handler
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200615 currently_in_except_block = (exc_sp->val_sp & 1); // restore previous state
616 exc_sp--; // pop back to previous exception handler
Damien8f9e2ee2013-12-29 16:54:59 +0000617 }
618
Damienc9f91972013-10-15 23:46:01 +0100619 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000620 // set flag to indicate that we are now handling an exception
621 currently_in_except_block = 1;
622
Damience89a212013-10-15 22:25:17 +0100623 // catch exception and pass to byte code
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200624 sp = (mp_obj_t*)(exc_sp->val_sp & (~((machine_uint_t)1)));
625 ip = exc_sp->handler;
Damienc9f91972013-10-15 23:46:01 +0100626 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000627 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100628 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000629 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000630
Damience89a212013-10-15 22:25:17 +0100631 } else {
Damien8f9e2ee2013-12-29 16:54:59 +0000632 // re-raise exception to higher level
Damienbd254452013-10-16 20:39:12 +0100633 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100634 nlr_jump(nlr.ret_val);
635 }
Damien429d7192013-10-04 19:53:11 +0100636 }
637 }
638}