blob: c41146ac8ffe5ad600b97874a63e1b5593f17f39 [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
Damienbd254452013-10-16 20:39:12 +010016// (value) stack grows down (to be compatible with native code when passing pointers to the stack), top element is pointed to
17// exception stack grows up, top element is pointed to
18
Damien429d7192013-10-04 19:53:11 +010019#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
Damien03c9cfb2013-11-05 22:06:08 +000020#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
21#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Damien Georgecbd2f742014-01-19 11:48:48 +000022#define DECODE_QSTR do { qst = *ip++; if (qst > 127) { qst = ((qst & 0x3f) << 8) | (*ip++); } } while (0)
Damien George20006db2014-01-18 14:10:48 +000023#define PUSH(val) *++sp = (val)
24#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000025#define TOP() (*sp)
26#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010027
Damiend99b0522013-12-21 18:17:45 +000028mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, uint n_state) {
Damien Georgee02b2d42014-01-19 01:14:37 +000029 n_state += 1; // XXX there is a bug somewhere which doesn't count enough state... (conwaylife and mandel have the bug)
30
Damien George20006db2014-01-18 14:10:48 +000031 // allocate state for locals and stack
32 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000033 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000034 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000035 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000036 }
Damien George20006db2014-01-18 14:10:48 +000037 mp_obj_t *sp = &state[0] - 1;
38
Damienbd254452013-10-16 20:39:12 +010039 // init args
40 for (int i = 0; i < n_args; i++) {
41 assert(i < 8);
Damien George20006db2014-01-18 14:10:48 +000042 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010043 }
Damien George08335002014-01-18 23:24:36 +000044
Damienbd254452013-10-16 20:39:12 +010045 const byte *ip = code;
Damien George6baf76e2013-12-30 22:32:17 +000046
Damien George08335002014-01-18 23:24:36 +000047 // get code info size
48 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
49 ip += code_info_size;
50
Damien George6baf76e2013-12-30 22:32:17 +000051 // execute prelude to make any cells (closed over variables)
52 {
53 for (uint n_local = *ip++; n_local > 0; n_local--) {
54 uint local_num = *ip++;
55 if (local_num < n_args) {
Damien George20006db2014-01-18 14:10:48 +000056 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
Damien George6baf76e2013-12-30 22:32:17 +000057 } else {
Damien George20006db2014-01-18 14:10:48 +000058 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +000059 }
60 }
61 }
62
63 // execute the byte code
Damien George08335002014-01-18 23:24:36 +000064 if (mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp)) {
Damienbd254452013-10-16 20:39:12 +010065 // it shouldn't yield
66 assert(0);
67 }
Damien George6baf76e2013-12-30 22:32:17 +000068
Damien4ebb32f2013-11-02 14:33:10 +000069 // TODO check fails if, eg, return from within for loop
70 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010071 return *sp;
72}
73
Damien George20006db2014-01-18 14:10:48 +000074// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
75// sp points to bottom of stack which grows up
Damien George08335002014-01-18 23:24:36 +000076bool 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 +010077 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
78
Damienbd254452013-10-16 20:39:12 +010079 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +000080 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +010081 machine_uint_t unum;
Damien Georgecbd2f742014-01-19 11:48:48 +000082 qstr qst;
Damiend99b0522013-12-21 18:17:45 +000083 mp_obj_t obj1, obj2;
Damien George20006db2014-01-18 14:10:48 +000084 mp_obj_t fast0 = fastn[0], fast1 = fastn[-1], fast2 = fastn[-2];
Damience89a212013-10-15 22:25:17 +010085 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +010086
Damien8f9e2ee2013-12-29 16:54:59 +000087 volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
88 machine_uint_t exc_stack[8]; // on the exception stack we store (ip, sp | X) for each block, X = previous value of currently_in_except_block
Damien George66327002014-01-02 18:20:41 +000089 machine_uint_t *volatile exc_sp = &exc_stack[0] - 1; // stack grows up, exc_sp points to top of stack
Damien George08335002014-01-18 23:24:36 +000090 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 +010091
Damien Georgee02b2d42014-01-19 01:14:37 +000092 // TODO if an exception occurs, do fast[0,1,2] become invalid??
93
Damience89a212013-10-15 22:25:17 +010094 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +010095 for (;;) {
Damience89a212013-10-15 22:25:17 +010096 if (nlr_push(&nlr) == 0) {
97 // loop to execute byte code
98 for (;;) {
Damien George08335002014-01-18 23:24:36 +000099 save_ip = ip;
Damience89a212013-10-15 22:25:17 +0100100 int op = *ip++;
101 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000102 case MP_BC_LOAD_CONST_FALSE:
103 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +0100104 break;
Damien429d7192013-10-04 19:53:11 +0100105
Damiend99b0522013-12-21 18:17:45 +0000106 case MP_BC_LOAD_CONST_NONE:
107 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100108 break;
Damien429d7192013-10-04 19:53:11 +0100109
Damiend99b0522013-12-21 18:17:45 +0000110 case MP_BC_LOAD_CONST_TRUE:
111 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100112 break;
Damien429d7192013-10-04 19:53:11 +0100113
Damien Georgee9906ac2014-01-04 18:44:46 +0000114 case MP_BC_LOAD_CONST_ELLIPSIS:
115 PUSH(mp_const_ellipsis);
116 break;
117
Damiend99b0522013-12-21 18:17:45 +0000118 case MP_BC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +0000119 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +0000120 ip += 3;
Paul Sokolovskye5ee1692014-01-06 17:49:46 +0200121 PUSH(MP_OBJ_NEW_SMALL_INT(unum));
Damience89a212013-10-15 22:25:17 +0100122 break;
123
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200124 case MP_BC_LOAD_CONST_INT:
125 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000126 PUSH(mp_obj_new_int_from_long_str(qstr_str(qst)));
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200127 break;
128
Damiend99b0522013-12-21 18:17:45 +0000129 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000130 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000131 PUSH(rt_load_const_dec(qst));
Damien7410e442013-11-02 19:47:57 +0000132 break;
133
Damiend99b0522013-12-21 18:17:45 +0000134 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100135 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000136 PUSH(rt_load_const_str(qst)); // TODO
Damience89a212013-10-15 22:25:17 +0100137 break;
138
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200139 case MP_BC_LOAD_CONST_BYTES:
140 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000141 PUSH(rt_load_const_str(qst)); // TODO
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200142 break;
143
Damiend99b0522013-12-21 18:17:45 +0000144 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100145 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000146 PUSH(rt_load_const_str(qst));
Damience89a212013-10-15 22:25:17 +0100147 break;
148
Damiend99b0522013-12-21 18:17:45 +0000149 case MP_BC_LOAD_FAST_0:
Damience89a212013-10-15 22:25:17 +0100150 PUSH(fast0);
151 break;
152
Damiend99b0522013-12-21 18:17:45 +0000153 case MP_BC_LOAD_FAST_1:
Damience89a212013-10-15 22:25:17 +0100154 PUSH(fast1);
155 break;
156
Damiend99b0522013-12-21 18:17:45 +0000157 case MP_BC_LOAD_FAST_2:
Damience89a212013-10-15 22:25:17 +0100158 PUSH(fast2);
159 break;
160
Damiend99b0522013-12-21 18:17:45 +0000161 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100162 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000163 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100164 break;
165
Damiend99b0522013-12-21 18:17:45 +0000166 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000167 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000168 if (unum == 0) {
169 obj1 = fast0;
170 } else if (unum == 1) {
171 obj1 = fast1;
172 } else if (unum == 2) {
173 obj1 = fast2;
174 } else {
175 obj1 = fastn[-unum];
176 }
177 PUSH(rt_get_cell(obj1));
Damien9ecbcff2013-12-11 00:41:43 +0000178 break;
179
Damiend99b0522013-12-21 18:17:45 +0000180 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100181 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000182 PUSH(rt_load_name(qst));
Damience89a212013-10-15 22:25:17 +0100183 break;
184
Damiend99b0522013-12-21 18:17:45 +0000185 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100186 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000187 PUSH(rt_load_global(qst));
Damience89a212013-10-15 22:25:17 +0100188 break;
189
Damiend99b0522013-12-21 18:17:45 +0000190 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100191 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000192 SET_TOP(rt_load_attr(TOP(), qst));
Damience89a212013-10-15 22:25:17 +0100193 break;
194
Damiend99b0522013-12-21 18:17:45 +0000195 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100196 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000197 rt_load_method(*sp, qst, sp);
Damien George20006db2014-01-18 14:10:48 +0000198 sp += 1;
Damience89a212013-10-15 22:25:17 +0100199 break;
200
Damiend99b0522013-12-21 18:17:45 +0000201 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100202 PUSH(rt_load_build_class());
203 break;
204
Damiend99b0522013-12-21 18:17:45 +0000205 case MP_BC_STORE_FAST_0:
Damience89a212013-10-15 22:25:17 +0100206 fast0 = POP();
207 break;
208
Damiend99b0522013-12-21 18:17:45 +0000209 case MP_BC_STORE_FAST_1:
Damience89a212013-10-15 22:25:17 +0100210 fast1 = POP();
211 break;
212
Damiend99b0522013-12-21 18:17:45 +0000213 case MP_BC_STORE_FAST_2:
Damience89a212013-10-15 22:25:17 +0100214 fast2 = POP();
215 break;
216
Damiend99b0522013-12-21 18:17:45 +0000217 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100218 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000219 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100220 break;
221
Damiend99b0522013-12-21 18:17:45 +0000222 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000223 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000224 if (unum == 0) {
225 obj1 = fast0;
226 } else if (unum == 1) {
227 obj1 = fast1;
228 } else if (unum == 2) {
229 obj1 = fast2;
230 } else {
231 obj1 = fastn[-unum];
232 }
233 rt_set_cell(obj1, POP());
Damien9ecbcff2013-12-11 00:41:43 +0000234 break;
235
Damiend99b0522013-12-21 18:17:45 +0000236 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100237 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000238 rt_store_name(qst, POP());
Damience89a212013-10-15 22:25:17 +0100239 break;
240
Damiend99b0522013-12-21 18:17:45 +0000241 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000242 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000243 rt_store_global(qst, POP());
Damien6addc892013-11-04 23:04:50 +0000244 break;
245
Damiend99b0522013-12-21 18:17:45 +0000246 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100247 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000248 rt_store_attr(sp[0], qst, sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000249 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100250 break;
251
Damiend99b0522013-12-21 18:17:45 +0000252 case MP_BC_STORE_SUBSCR:
Damien George20006db2014-01-18 14:10:48 +0000253 rt_store_subscr(sp[-1], sp[0], sp[-2]);
254 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100255 break;
256
Damiend99b0522013-12-21 18:17:45 +0000257 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000258 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100259 PUSH(obj1);
260 break;
261
Damiend99b0522013-12-21 18:17:45 +0000262 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000263 sp += 2;
264 sp[0] = sp[-2];
265 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100266 break;
267
Damiend99b0522013-12-21 18:17:45 +0000268 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000269 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100270 break;
271
Damiend99b0522013-12-21 18:17:45 +0000272 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000273 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000274 sp[0] = sp[-1];
275 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000276 break;
277
Damiend99b0522013-12-21 18:17:45 +0000278 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100279 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000280 sp[0] = sp[-1];
281 sp[-1] = sp[-2];
282 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100283 break;
284
Damiend99b0522013-12-21 18:17:45 +0000285 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000286 DECODE_SLABEL;
287 ip += unum;
Damience89a212013-10-15 22:25:17 +0100288 break;
289
Damiend99b0522013-12-21 18:17:45 +0000290 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000291 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100292 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000293 ip += unum;
Damience89a212013-10-15 22:25:17 +0100294 }
295 break;
296
Damiend99b0522013-12-21 18:17:45 +0000297 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000298 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100299 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000300 ip += unum;
Damience89a212013-10-15 22:25:17 +0100301 }
302 break;
303
Damiend99b0522013-12-21 18:17:45 +0000304 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000305 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000306 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000307 ip += unum;
308 } else {
Damien George20006db2014-01-18 14:10:48 +0000309 sp--;
Damien94658e22013-11-09 20:12:32 +0000310 }
311 break;
312
Damiend99b0522013-12-21 18:17:45 +0000313 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000314 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000315 if (rt_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000316 sp--;
Damien94658e22013-11-09 20:12:32 +0000317 } else {
318 ip += unum;
319 }
320 break;
321
Damience89a212013-10-15 22:25:17 +0100322 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000323 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100324 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000325 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100326 break;
327 */
328
Damien02a7c412013-12-29 18:48:37 +0000329 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000330 case MP_BC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000331 DECODE_ULABEL; // except labels are always forward
332 *++exc_sp = (machine_uint_t)ip + unum;
Damien8f9e2ee2013-12-29 16:54:59 +0000333 *++exc_sp = (((machine_uint_t)sp) | currently_in_except_block);
334 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100335 break;
336
Damiend99b0522013-12-21 18:17:45 +0000337 case MP_BC_END_FINALLY:
Damience89a212013-10-15 22:25:17 +0100338 // not implemented
339 // if TOS is an exception, reraises the exception (3 values on TOS)
340 // if TOS is an integer, does something else
341 // if TOS is None, just pops it and continues
342 // else error
343 assert(0);
344 break;
345
Damiend99b0522013-12-21 18:17:45 +0000346 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000347 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100348 break;
349
Damiend99b0522013-12-21 18:17:45 +0000350 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000351 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000352 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000353 if (obj1 == mp_const_stop_iteration) {
Damien George20006db2014-01-18 14:10:48 +0000354 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000355 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100356 } else {
357 PUSH(obj1); // push the next iteration value
358 }
359 break;
360
Damien02a7c412013-12-29 18:48:37 +0000361 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000362 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000363 // we are exiting an exception handler, so pop the last one of the exception-stack
364 assert(exc_sp >= &exc_stack[0]);
365 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
366 exc_sp -= 2; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100367 break;
368
Damien02a7c412013-12-29 18:48:37 +0000369 // matched againts: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000370 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100371 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100372 // 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 +0100373 assert(exc_sp >= &exc_stack[0]);
Damiend99b0522013-12-21 18:17:45 +0000374 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100375 //exc_sp--; // discard ip
Damien8f9e2ee2013-12-29 16:54:59 +0000376 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
377 exc_sp -= 2; // pop back to previous exception handler
Damien George20006db2014-01-18 14:10:48 +0000378 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100379 break;
380
Damiend99b0522013-12-21 18:17:45 +0000381 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000382 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000383 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000384 break;
385
Damiend99b0522013-12-21 18:17:45 +0000386 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100387 unum = *ip++;
388 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000389 obj1 = TOP();
390 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100391 break;
392
Damiend99b0522013-12-21 18:17:45 +0000393 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100394 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000395 sp -= unum - 1;
396 SET_TOP(rt_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100397 break;
398
Damiend99b0522013-12-21 18:17:45 +0000399 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100400 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000401 sp -= unum - 1;
402 SET_TOP(rt_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100403 break;
404
Damiend99b0522013-12-21 18:17:45 +0000405 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100406 DECODE_UINT;
407 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien George20006db2014-01-18 14:10:48 +0000408 rt_list_append(sp[-unum], sp[0]);
409 sp--;
Damienc226dca2013-10-16 16:12:52 +0100410 break;
411
Damiend99b0522013-12-21 18:17:45 +0000412 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100413 DECODE_UINT;
414 PUSH(rt_build_map(unum));
415 break;
416
Damiend99b0522013-12-21 18:17:45 +0000417 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000418 sp -= 2;
419 rt_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100420 break;
421
Damiend99b0522013-12-21 18:17:45 +0000422 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100423 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000424 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
425 rt_store_map(sp[-unum - 1], sp[0], sp[-1]);
426 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100427 break;
428
Damiend99b0522013-12-21 18:17:45 +0000429 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100430 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000431 sp -= unum - 1;
432 SET_TOP(rt_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100433 break;
434
Damiend99b0522013-12-21 18:17:45 +0000435 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100436 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000437 // I think it's guaranteed by the compiler that sp[-unum] is a set
438 rt_store_set(sp[-unum], sp[0]);
439 sp--;
Damienc12aa462013-10-16 20:57:49 +0100440 break;
441
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200442#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200443 case MP_BC_BUILD_SLICE:
444 DECODE_UINT;
445 if (unum == 2) {
446 obj2 = POP();
447 obj1 = TOP();
448 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
449 } else {
450 printf("3-argument slice is not supported\n");
451 assert(0);
452 }
453 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200454#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200455
Damiend99b0522013-12-21 18:17:45 +0000456 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000457 DECODE_UINT;
Damien George932bf1c2014-01-18 23:42:49 +0000458 rt_unpack_sequence(sp[0], unum, sp);
Damien George20006db2014-01-18 14:10:48 +0000459 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000460 break;
461
Damiend99b0522013-12-21 18:17:45 +0000462 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100463 DECODE_UINT;
464 PUSH(rt_make_function_from_id(unum));
465 break;
466
Damiend99b0522013-12-21 18:17:45 +0000467 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000468 DECODE_UINT;
469 obj1 = POP();
470 PUSH(rt_make_closure_from_id(unum, obj1));
471 break;
472
Damiend99b0522013-12-21 18:17:45 +0000473 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100474 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000475 // unum & 0xff == n_positional
476 // (unum >> 8) & 0xff == n_keyword
477 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
478 SET_TOP(rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100479 break;
480
Damiend99b0522013-12-21 18:17:45 +0000481 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100482 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000483 // unum & 0xff == n_positional
484 // (unum >> 8) & 0xff == n_keyword
485 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
486 SET_TOP(rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100487 break;
488
Damiend99b0522013-12-21 18:17:45 +0000489 case MP_BC_RETURN_VALUE:
Damience89a212013-10-15 22:25:17 +0100490 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100491 *sp_in_out = sp;
Damien George66327002014-01-02 18:20:41 +0000492 assert(exc_sp == &exc_stack[0] - 1);
Damienbd254452013-10-16 20:39:12 +0100493 return false;
494
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200495 case MP_BC_RAISE_VARARGS:
496 unum = *ip++;
497 assert(unum == 1);
498 obj1 = POP();
499 nlr_jump(obj1);
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200500
Damiend99b0522013-12-21 18:17:45 +0000501 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100502 nlr_pop();
503 *ip_in_out = ip;
504 fastn[0] = fast0;
Damien George20006db2014-01-18 14:10:48 +0000505 fastn[-1] = fast1;
506 fastn[-2] = fast2;
Damienbd254452013-10-16 20:39:12 +0100507 *sp_in_out = sp;
508 return true;
Damience89a212013-10-15 22:25:17 +0100509
Damiend99b0522013-12-21 18:17:45 +0000510 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000511 DECODE_QSTR;
512 obj1 = POP();
Damien Georgecbd2f742014-01-19 11:48:48 +0000513 SET_TOP(rt_import_name(qst, obj1, TOP()));
Damiendb4c3612013-12-10 17:27:24 +0000514 break;
515
Damiend99b0522013-12-21 18:17:45 +0000516 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000517 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000518 obj1 = rt_import_from(TOP(), qst);
Damiendb4c3612013-12-10 17:27:24 +0000519 PUSH(obj1);
520 break;
521
Damience89a212013-10-15 22:25:17 +0100522 default:
Damien03c9cfb2013-11-05 22:06:08 +0000523 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100524 assert(0);
525 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100526 return false;
Damien429d7192013-10-04 19:53:11 +0100527 }
Damience89a212013-10-15 22:25:17 +0100528 }
Damien429d7192013-10-04 19:53:11 +0100529
Damience89a212013-10-15 22:25:17 +0100530 } else {
531 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100532
Damien George08335002014-01-18 23:24:36 +0000533 // set file and line number that the exception occurred at
534 if (MP_OBJ_IS_TYPE(nlr.ret_val, &exception_type)) {
535 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 +0000536 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
537 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 +0000538 machine_uint_t source_line = 1;
539 machine_uint_t bc = save_ip - code_info - code_info_size;
Damien Georgecbd2f742014-01-19 11:48:48 +0000540 //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
541 for (const byte* ci = code_info + 12; bc >= ci[0]; ci += 2) {
Damien George08335002014-01-18 23:24:36 +0000542 bc -= ci[0];
543 source_line += ci[1];
544 if (ci[0] == 0 && ci[1] == 0) {
545 break;
546 }
547 }
Damien George136b1492014-01-19 12:38:49 +0000548 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +0000549 }
550
Damien8f9e2ee2013-12-29 16:54:59 +0000551 while (currently_in_except_block) {
552 // nested exception
553
554 assert(exc_sp >= &exc_stack[0]);
555
556 // TODO make a proper message for nested exception
557 // at the moment we are just raising the very last exception (the one that caused the nested exception)
558
559 // move up to previous exception handler
560 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
561 exc_sp -= 2; // pop back to previous exception handler
562 }
563
Damienc9f91972013-10-15 23:46:01 +0100564 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000565 // set flag to indicate that we are now handling an exception
566 currently_in_except_block = 1;
567
Damience89a212013-10-15 22:25:17 +0100568 // catch exception and pass to byte code
Damien8f9e2ee2013-12-29 16:54:59 +0000569 sp = (mp_obj_t*)(exc_sp[0] & (~((machine_uint_t)1)));
Damienc9f91972013-10-15 23:46:01 +0100570 ip = (const byte*)(exc_sp[-1]);
571 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000572 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100573 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000574 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000575
Damience89a212013-10-15 22:25:17 +0100576 } else {
Damien8f9e2ee2013-12-29 16:54:59 +0000577 // re-raise exception to higher level
Damienbd254452013-10-16 20:39:12 +0100578 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100579 nlr_jump(nlr.ret_val);
580 }
Damien429d7192013-10-04 19:53:11 +0100581 }
582 }
583}