blob: cacb6aca0fba86c65f64f0c36efa0dbe0c8f1d13 [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"
10#include "obj.h"
Damien429d7192013-10-04 19:53:11 +010011#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000012#include "bc0.h"
Damieneb19efb2013-10-10 22:06:54 +010013#include "bc.h"
Damien429d7192013-10-04 19:53:11 +010014
Damienbd254452013-10-16 20:39:12 +010015// (value) stack grows down (to be compatible with native code when passing pointers to the stack), top element is pointed to
16// exception stack grows up, top element is pointed to
17
Damien429d7192013-10-04 19:53:11 +010018#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
Damien03c9cfb2013-11-05 22:06:08 +000019#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
20#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Damien429d7192013-10-04 19:53:11 +010021#define DECODE_QSTR do { qstr = *ip++; if (qstr > 127) { qstr = ((qstr & 0x3f) << 8) | (*ip++); } } while (0)
Damien George20006db2014-01-18 14:10:48 +000022#define PUSH(val) *++sp = (val)
23#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000024#define TOP() (*sp)
25#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010026
Damiend99b0522013-12-21 18:17:45 +000027mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, uint n_state) {
Damien George20006db2014-01-18 14:10:48 +000028 // allocate state for locals and stack
29 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000030 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000031 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000032 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000033 }
Damien George20006db2014-01-18 14:10:48 +000034 mp_obj_t *sp = &state[0] - 1;
35
Damienbd254452013-10-16 20:39:12 +010036 // init args
37 for (int i = 0; i < n_args; i++) {
38 assert(i < 8);
Damien George20006db2014-01-18 14:10:48 +000039 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010040 }
Damien George08335002014-01-18 23:24:36 +000041
Damienbd254452013-10-16 20:39:12 +010042 const byte *ip = code;
Damien George6baf76e2013-12-30 22:32:17 +000043
Damien George08335002014-01-18 23:24:36 +000044 // get code info size
45 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
46 ip += code_info_size;
47
Damien George6baf76e2013-12-30 22:32:17 +000048 // execute prelude to make any cells (closed over variables)
49 {
50 for (uint n_local = *ip++; n_local > 0; n_local--) {
51 uint local_num = *ip++;
52 if (local_num < n_args) {
Damien George20006db2014-01-18 14:10:48 +000053 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
Damien George6baf76e2013-12-30 22:32:17 +000054 } else {
Damien George20006db2014-01-18 14:10:48 +000055 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +000056 }
57 }
58 }
59
60 // execute the byte code
Damien George08335002014-01-18 23:24:36 +000061 if (mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp)) {
Damienbd254452013-10-16 20:39:12 +010062 // it shouldn't yield
63 assert(0);
64 }
Damien George6baf76e2013-12-30 22:32:17 +000065
Damien4ebb32f2013-11-02 14:33:10 +000066 // TODO check fails if, eg, return from within for loop
67 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010068 return *sp;
69}
70
Damien George20006db2014-01-18 14:10:48 +000071// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
72// sp points to bottom of stack which grows up
Damien George08335002014-01-18 23:24:36 +000073bool 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 +010074 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
75
Damienbd254452013-10-16 20:39:12 +010076 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +000077 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +010078 machine_uint_t unum;
Damien429d7192013-10-04 19:53:11 +010079 qstr qstr;
Damiend99b0522013-12-21 18:17:45 +000080 mp_obj_t obj1, obj2;
Damien George20006db2014-01-18 14:10:48 +000081 mp_obj_t fast0 = fastn[0], fast1 = fastn[-1], fast2 = fastn[-2];
Damience89a212013-10-15 22:25:17 +010082 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +010083
Damien8f9e2ee2013-12-29 16:54:59 +000084 volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
85 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 +000086 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 +000087 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 +010088
Damience89a212013-10-15 22:25:17 +010089 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +010090 for (;;) {
Damience89a212013-10-15 22:25:17 +010091 if (nlr_push(&nlr) == 0) {
92 // loop to execute byte code
93 for (;;) {
Damien George08335002014-01-18 23:24:36 +000094 save_ip = ip;
Damience89a212013-10-15 22:25:17 +010095 int op = *ip++;
96 switch (op) {
Damiend99b0522013-12-21 18:17:45 +000097 case MP_BC_LOAD_CONST_FALSE:
98 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +010099 break;
Damien429d7192013-10-04 19:53:11 +0100100
Damiend99b0522013-12-21 18:17:45 +0000101 case MP_BC_LOAD_CONST_NONE:
102 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100103 break;
Damien429d7192013-10-04 19:53:11 +0100104
Damiend99b0522013-12-21 18:17:45 +0000105 case MP_BC_LOAD_CONST_TRUE:
106 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100107 break;
Damien429d7192013-10-04 19:53:11 +0100108
Damien Georgee9906ac2014-01-04 18:44:46 +0000109 case MP_BC_LOAD_CONST_ELLIPSIS:
110 PUSH(mp_const_ellipsis);
111 break;
112
Damiend99b0522013-12-21 18:17:45 +0000113 case MP_BC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +0000114 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +0000115 ip += 3;
Paul Sokolovskye5ee1692014-01-06 17:49:46 +0200116 PUSH(MP_OBJ_NEW_SMALL_INT(unum));
Damience89a212013-10-15 22:25:17 +0100117 break;
118
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200119 case MP_BC_LOAD_CONST_INT:
120 DECODE_QSTR;
121 PUSH(mp_obj_new_int_from_long_str(qstr_str(qstr)));
122 break;
123
Damiend99b0522013-12-21 18:17:45 +0000124 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000125 DECODE_QSTR;
126 PUSH(rt_load_const_dec(qstr));
127 break;
128
Damiend99b0522013-12-21 18:17:45 +0000129 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100130 DECODE_QSTR;
131 PUSH(rt_load_const_str(qstr)); // TODO
132 break;
133
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200134 case MP_BC_LOAD_CONST_BYTES:
135 DECODE_QSTR;
136 PUSH(rt_load_const_str(qstr)); // TODO
137 break;
138
Damiend99b0522013-12-21 18:17:45 +0000139 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100140 DECODE_QSTR;
141 PUSH(rt_load_const_str(qstr));
142 break;
143
Damiend99b0522013-12-21 18:17:45 +0000144 case MP_BC_LOAD_FAST_0:
Damience89a212013-10-15 22:25:17 +0100145 PUSH(fast0);
146 break;
147
Damiend99b0522013-12-21 18:17:45 +0000148 case MP_BC_LOAD_FAST_1:
Damience89a212013-10-15 22:25:17 +0100149 PUSH(fast1);
150 break;
151
Damiend99b0522013-12-21 18:17:45 +0000152 case MP_BC_LOAD_FAST_2:
Damience89a212013-10-15 22:25:17 +0100153 PUSH(fast2);
154 break;
155
Damiend99b0522013-12-21 18:17:45 +0000156 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100157 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000158 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100159 break;
160
Damiend99b0522013-12-21 18:17:45 +0000161 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000162 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000163 if (unum == 0) {
164 obj1 = fast0;
165 } else if (unum == 1) {
166 obj1 = fast1;
167 } else if (unum == 2) {
168 obj1 = fast2;
169 } else {
170 obj1 = fastn[-unum];
171 }
172 PUSH(rt_get_cell(obj1));
Damien9ecbcff2013-12-11 00:41:43 +0000173 break;
174
Damiend99b0522013-12-21 18:17:45 +0000175 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100176 DECODE_QSTR;
177 PUSH(rt_load_name(qstr));
178 break;
179
Damiend99b0522013-12-21 18:17:45 +0000180 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100181 DECODE_QSTR;
182 PUSH(rt_load_global(qstr));
183 break;
184
Damiend99b0522013-12-21 18:17:45 +0000185 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100186 DECODE_QSTR;
Damiendb4c3612013-12-10 17:27:24 +0000187 SET_TOP(rt_load_attr(TOP(), qstr));
Damience89a212013-10-15 22:25:17 +0100188 break;
189
Damiend99b0522013-12-21 18:17:45 +0000190 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100191 DECODE_QSTR;
Damien George20006db2014-01-18 14:10:48 +0000192 rt_load_method(*sp, qstr, sp);
193 sp += 1;
Damience89a212013-10-15 22:25:17 +0100194 break;
195
Damiend99b0522013-12-21 18:17:45 +0000196 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100197 PUSH(rt_load_build_class());
198 break;
199
Damiend99b0522013-12-21 18:17:45 +0000200 case MP_BC_STORE_FAST_0:
Damience89a212013-10-15 22:25:17 +0100201 fast0 = POP();
202 break;
203
Damiend99b0522013-12-21 18:17:45 +0000204 case MP_BC_STORE_FAST_1:
Damience89a212013-10-15 22:25:17 +0100205 fast1 = POP();
206 break;
207
Damiend99b0522013-12-21 18:17:45 +0000208 case MP_BC_STORE_FAST_2:
Damience89a212013-10-15 22:25:17 +0100209 fast2 = POP();
210 break;
211
Damiend99b0522013-12-21 18:17:45 +0000212 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100213 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000214 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100215 break;
216
Damiend99b0522013-12-21 18:17:45 +0000217 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000218 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000219 if (unum == 0) {
220 obj1 = fast0;
221 } else if (unum == 1) {
222 obj1 = fast1;
223 } else if (unum == 2) {
224 obj1 = fast2;
225 } else {
226 obj1 = fastn[-unum];
227 }
228 rt_set_cell(obj1, POP());
Damien9ecbcff2013-12-11 00:41:43 +0000229 break;
230
Damiend99b0522013-12-21 18:17:45 +0000231 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100232 DECODE_QSTR;
233 rt_store_name(qstr, POP());
234 break;
235
Damiend99b0522013-12-21 18:17:45 +0000236 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000237 DECODE_QSTR;
238 rt_store_global(qstr, POP());
239 break;
240
Damiend99b0522013-12-21 18:17:45 +0000241 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100242 DECODE_QSTR;
Damien George20006db2014-01-18 14:10:48 +0000243 rt_store_attr(sp[0], qstr, sp[-1]);
244 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100245 break;
246
Damiend99b0522013-12-21 18:17:45 +0000247 case MP_BC_STORE_SUBSCR:
Damien George20006db2014-01-18 14:10:48 +0000248 rt_store_subscr(sp[-1], sp[0], sp[-2]);
249 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100250 break;
251
Damiend99b0522013-12-21 18:17:45 +0000252 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000253 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100254 PUSH(obj1);
255 break;
256
Damiend99b0522013-12-21 18:17:45 +0000257 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000258 sp += 2;
259 sp[0] = sp[-2];
260 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100261 break;
262
Damiend99b0522013-12-21 18:17:45 +0000263 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000264 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100265 break;
266
Damiend99b0522013-12-21 18:17:45 +0000267 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000268 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000269 sp[0] = sp[-1];
270 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000271 break;
272
Damiend99b0522013-12-21 18:17:45 +0000273 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100274 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000275 sp[0] = sp[-1];
276 sp[-1] = sp[-2];
277 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100278 break;
279
Damiend99b0522013-12-21 18:17:45 +0000280 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000281 DECODE_SLABEL;
282 ip += unum;
Damience89a212013-10-15 22:25:17 +0100283 break;
284
Damiend99b0522013-12-21 18:17:45 +0000285 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000286 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100287 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000288 ip += unum;
Damience89a212013-10-15 22:25:17 +0100289 }
290 break;
291
Damiend99b0522013-12-21 18:17:45 +0000292 case MP_BC_POP_JUMP_IF_FALSE:
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_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000300 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000301 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000302 ip += unum;
303 } else {
Damien George20006db2014-01-18 14:10:48 +0000304 sp--;
Damien94658e22013-11-09 20:12:32 +0000305 }
306 break;
307
Damiend99b0522013-12-21 18:17:45 +0000308 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000309 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000310 if (rt_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000311 sp--;
Damien94658e22013-11-09 20:12:32 +0000312 } else {
313 ip += unum;
314 }
315 break;
316
Damience89a212013-10-15 22:25:17 +0100317 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000318 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100319 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000320 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100321 break;
322 */
323
Damien02a7c412013-12-29 18:48:37 +0000324 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000325 case MP_BC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000326 DECODE_ULABEL; // except labels are always forward
327 *++exc_sp = (machine_uint_t)ip + unum;
Damien8f9e2ee2013-12-29 16:54:59 +0000328 *++exc_sp = (((machine_uint_t)sp) | currently_in_except_block);
329 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100330 break;
331
Damiend99b0522013-12-21 18:17:45 +0000332 case MP_BC_END_FINALLY:
Damience89a212013-10-15 22:25:17 +0100333 // not implemented
334 // if TOS is an exception, reraises the exception (3 values on TOS)
335 // if TOS is an integer, does something else
336 // if TOS is None, just pops it and continues
337 // else error
338 assert(0);
339 break;
340
Damiend99b0522013-12-21 18:17:45 +0000341 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000342 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100343 break;
344
Damiend99b0522013-12-21 18:17:45 +0000345 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000346 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000347 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000348 if (obj1 == mp_const_stop_iteration) {
Damien George20006db2014-01-18 14:10:48 +0000349 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000350 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100351 } else {
352 PUSH(obj1); // push the next iteration value
353 }
354 break;
355
Damien02a7c412013-12-29 18:48:37 +0000356 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000357 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000358 // we are exiting an exception handler, so pop the last one of the exception-stack
359 assert(exc_sp >= &exc_stack[0]);
360 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
361 exc_sp -= 2; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100362 break;
363
Damien02a7c412013-12-29 18:48:37 +0000364 // matched againts: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000365 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100366 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100367 // 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 +0100368 assert(exc_sp >= &exc_stack[0]);
Damiend99b0522013-12-21 18:17:45 +0000369 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100370 //exc_sp--; // discard ip
Damien8f9e2ee2013-12-29 16:54:59 +0000371 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
372 exc_sp -= 2; // pop back to previous exception handler
Damien George20006db2014-01-18 14:10:48 +0000373 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100374 break;
375
Damiend99b0522013-12-21 18:17:45 +0000376 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000377 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000378 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000379 break;
380
Damiend99b0522013-12-21 18:17:45 +0000381 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100382 unum = *ip++;
383 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000384 obj1 = TOP();
385 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100386 break;
387
Damiend99b0522013-12-21 18:17:45 +0000388 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100389 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000390 sp -= unum - 1;
391 SET_TOP(rt_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100392 break;
393
Damiend99b0522013-12-21 18:17:45 +0000394 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100395 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000396 sp -= unum - 1;
397 SET_TOP(rt_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100398 break;
399
Damiend99b0522013-12-21 18:17:45 +0000400 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100401 DECODE_UINT;
402 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien George20006db2014-01-18 14:10:48 +0000403 rt_list_append(sp[-unum], sp[0]);
404 sp--;
Damienc226dca2013-10-16 16:12:52 +0100405 break;
406
Damiend99b0522013-12-21 18:17:45 +0000407 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100408 DECODE_UINT;
409 PUSH(rt_build_map(unum));
410 break;
411
Damiend99b0522013-12-21 18:17:45 +0000412 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000413 sp -= 2;
414 rt_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100415 break;
416
Damiend99b0522013-12-21 18:17:45 +0000417 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100418 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000419 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
420 rt_store_map(sp[-unum - 1], sp[0], sp[-1]);
421 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100422 break;
423
Damiend99b0522013-12-21 18:17:45 +0000424 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100425 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000426 sp -= unum - 1;
427 SET_TOP(rt_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100428 break;
429
Damiend99b0522013-12-21 18:17:45 +0000430 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100431 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000432 // I think it's guaranteed by the compiler that sp[-unum] is a set
433 rt_store_set(sp[-unum], sp[0]);
434 sp--;
Damienc12aa462013-10-16 20:57:49 +0100435 break;
436
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200437#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200438 case MP_BC_BUILD_SLICE:
439 DECODE_UINT;
440 if (unum == 2) {
441 obj2 = POP();
442 obj1 = TOP();
443 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
444 } else {
445 printf("3-argument slice is not supported\n");
446 assert(0);
447 }
448 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200449#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200450
Damiend99b0522013-12-21 18:17:45 +0000451 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000452 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000453 rt_unpack_sequence(sp[0], unum, sp + unum - 1);
454 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000455 break;
456
Damiend99b0522013-12-21 18:17:45 +0000457 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100458 DECODE_UINT;
459 PUSH(rt_make_function_from_id(unum));
460 break;
461
Damiend99b0522013-12-21 18:17:45 +0000462 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000463 DECODE_UINT;
464 obj1 = POP();
465 PUSH(rt_make_closure_from_id(unum, obj1));
466 break;
467
Damiend99b0522013-12-21 18:17:45 +0000468 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100469 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000470 // unum & 0xff == n_positional
471 // (unum >> 8) & 0xff == n_keyword
472 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
473 SET_TOP(rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100474 break;
475
Damiend99b0522013-12-21 18:17:45 +0000476 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100477 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000478 // unum & 0xff == n_positional
479 // (unum >> 8) & 0xff == n_keyword
480 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
481 SET_TOP(rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100482 break;
483
Damiend99b0522013-12-21 18:17:45 +0000484 case MP_BC_RETURN_VALUE:
Damience89a212013-10-15 22:25:17 +0100485 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100486 *sp_in_out = sp;
Damien George66327002014-01-02 18:20:41 +0000487 assert(exc_sp == &exc_stack[0] - 1);
Damienbd254452013-10-16 20:39:12 +0100488 return false;
489
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200490 case MP_BC_RAISE_VARARGS:
491 unum = *ip++;
492 assert(unum == 1);
493 obj1 = POP();
494 nlr_jump(obj1);
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200495
Damiend99b0522013-12-21 18:17:45 +0000496 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100497 nlr_pop();
498 *ip_in_out = ip;
499 fastn[0] = fast0;
Damien George20006db2014-01-18 14:10:48 +0000500 fastn[-1] = fast1;
501 fastn[-2] = fast2;
Damienbd254452013-10-16 20:39:12 +0100502 *sp_in_out = sp;
503 return true;
Damience89a212013-10-15 22:25:17 +0100504
Damiend99b0522013-12-21 18:17:45 +0000505 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000506 DECODE_QSTR;
507 obj1 = POP();
508 SET_TOP(rt_import_name(qstr, obj1, TOP()));
509 break;
510
Damiend99b0522013-12-21 18:17:45 +0000511 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000512 DECODE_QSTR;
513 obj1 = rt_import_from(TOP(), qstr);
514 PUSH(obj1);
515 break;
516
Damience89a212013-10-15 22:25:17 +0100517 default:
Damien03c9cfb2013-11-05 22:06:08 +0000518 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100519 assert(0);
520 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100521 return false;
Damien429d7192013-10-04 19:53:11 +0100522 }
Damience89a212013-10-15 22:25:17 +0100523 }
Damien429d7192013-10-04 19:53:11 +0100524
Damience89a212013-10-15 22:25:17 +0100525 } else {
526 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100527
Damien George08335002014-01-18 23:24:36 +0000528 // set file and line number that the exception occurred at
529 if (MP_OBJ_IS_TYPE(nlr.ret_val, &exception_type)) {
530 machine_uint_t code_info_size = code_info[0] | (code_info[1] << 8) | (code_info[2] << 16) | (code_info[3] << 24);
531 qstr = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
532 machine_uint_t source_line = 1;
533 machine_uint_t bc = save_ip - code_info - code_info_size;
534 //printf("find %lu %d %d\n", bc, code_info[8], code_info[9]);
535 for (const byte* ci = code_info + 8; bc > ci[0]; ci += 2) {
536 bc -= ci[0];
537 source_line += ci[1];
538 if (ci[0] == 0 && ci[1] == 0) {
539 break;
540 }
541 }
542 mp_obj_exception_set_source_info(nlr.ret_val, qstr, source_line);
543 }
544
Damien8f9e2ee2013-12-29 16:54:59 +0000545 while (currently_in_except_block) {
546 // nested exception
547
548 assert(exc_sp >= &exc_stack[0]);
549
550 // TODO make a proper message for nested exception
551 // at the moment we are just raising the very last exception (the one that caused the nested exception)
552
553 // move up to previous exception handler
554 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
555 exc_sp -= 2; // pop back to previous exception handler
556 }
557
Damienc9f91972013-10-15 23:46:01 +0100558 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000559 // set flag to indicate that we are now handling an exception
560 currently_in_except_block = 1;
561
Damience89a212013-10-15 22:25:17 +0100562 // catch exception and pass to byte code
Damien8f9e2ee2013-12-29 16:54:59 +0000563 sp = (mp_obj_t*)(exc_sp[0] & (~((machine_uint_t)1)));
Damienc9f91972013-10-15 23:46:01 +0100564 ip = (const byte*)(exc_sp[-1]);
565 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000566 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100567 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000568 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000569
Damience89a212013-10-15 22:25:17 +0100570 } else {
Damien8f9e2ee2013-12-29 16:54:59 +0000571 // re-raise exception to higher level
Damienbd254452013-10-16 20:39:12 +0100572 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100573 nlr_jump(nlr.ret_val);
574 }
Damien429d7192013-10-04 19:53:11 +0100575 }
576 }
577}