blob: e2bb3de642b191c0d6e059d8d7c258cba5b7831a [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)
22#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
Damiena3977762013-10-09 23:10:10 +010027// args are in reverse order in array
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) {
29 mp_obj_t temp_state[10]; // TODO allocate properly
30 mp_obj_t *state = &temp_state[0];
31 mp_obj_t *sp = &state[10];
Damien40fdfe32013-11-05 22:16:22 +000032 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000033 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000034 sp = &state[n_state];
35 }
Damienbd254452013-10-16 20:39:12 +010036 // init args
37 for (int i = 0; i < n_args; i++) {
38 assert(i < 8);
39 state[i] = args[n_args - 1 - i];
40 }
Damienbd254452013-10-16 20:39:12 +010041 const byte *ip = code;
Damien George6baf76e2013-12-30 22:32:17 +000042
43 // execute prelude to make any cells (closed over variables)
44 {
45 for (uint n_local = *ip++; n_local > 0; n_local--) {
46 uint local_num = *ip++;
47 if (local_num < n_args) {
48 state[local_num] = mp_obj_new_cell(state[local_num]);
49 } else {
50 state[local_num] = mp_obj_new_cell(MP_OBJ_NULL);
51 }
52 }
53 }
54
55 // execute the byte code
Damiend99b0522013-12-21 18:17:45 +000056 if (mp_execute_byte_code_2(&ip, &state[0], &sp)) {
Damienbd254452013-10-16 20:39:12 +010057 // it shouldn't yield
58 assert(0);
59 }
Damien George6baf76e2013-12-30 22:32:17 +000060
Damien4ebb32f2013-11-02 14:33:10 +000061 // TODO check fails if, eg, return from within for loop
62 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010063 return *sp;
64}
65
66// fastn has items in normal order
67// sp points to top of stack which grows down
Damiend99b0522013-12-21 18:17:45 +000068bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out) {
Damienc9f91972013-10-15 23:46:01 +010069 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
70
Damienbd254452013-10-16 20:39:12 +010071 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +000072 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +010073 machine_uint_t unum;
Damien429d7192013-10-04 19:53:11 +010074 qstr qstr;
Damiend99b0522013-12-21 18:17:45 +000075 mp_obj_t obj1, obj2;
76 mp_obj_t fast0 = fastn[0], fast1 = fastn[1], fast2 = fastn[2];
Damience89a212013-10-15 22:25:17 +010077 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +010078
Damien8f9e2ee2013-12-29 16:54:59 +000079 volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
80 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 +000081 machine_uint_t *volatile exc_sp = &exc_stack[0] - 1; // stack grows up, exc_sp points to top of stack
Damienc9f91972013-10-15 23:46:01 +010082
Damience89a212013-10-15 22:25:17 +010083 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +010084 for (;;) {
Damience89a212013-10-15 22:25:17 +010085 if (nlr_push(&nlr) == 0) {
86 // loop to execute byte code
87 for (;;) {
88 int op = *ip++;
89 switch (op) {
Damiend99b0522013-12-21 18:17:45 +000090 case MP_BC_LOAD_CONST_FALSE:
91 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +010092 break;
Damien429d7192013-10-04 19:53:11 +010093
Damiend99b0522013-12-21 18:17:45 +000094 case MP_BC_LOAD_CONST_NONE:
95 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +010096 break;
Damien429d7192013-10-04 19:53:11 +010097
Damiend99b0522013-12-21 18:17:45 +000098 case MP_BC_LOAD_CONST_TRUE:
99 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100100 break;
Damien429d7192013-10-04 19:53:11 +0100101
Damien Georgee9906ac2014-01-04 18:44:46 +0000102 case MP_BC_LOAD_CONST_ELLIPSIS:
103 PUSH(mp_const_ellipsis);
104 break;
105
Damiend99b0522013-12-21 18:17:45 +0000106 case MP_BC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +0000107 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +0000108 ip += 3;
Paul Sokolovskye5ee1692014-01-06 17:49:46 +0200109 PUSH(MP_OBJ_NEW_SMALL_INT(unum));
Damience89a212013-10-15 22:25:17 +0100110 break;
111
Damiend99b0522013-12-21 18:17:45 +0000112 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000113 DECODE_QSTR;
114 PUSH(rt_load_const_dec(qstr));
115 break;
116
Damiend99b0522013-12-21 18:17:45 +0000117 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100118 DECODE_QSTR;
119 PUSH(rt_load_const_str(qstr)); // TODO
120 break;
121
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200122 case MP_BC_LOAD_CONST_BYTES:
123 DECODE_QSTR;
124 PUSH(rt_load_const_str(qstr)); // TODO
125 break;
126
Damiend99b0522013-12-21 18:17:45 +0000127 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100128 DECODE_QSTR;
129 PUSH(rt_load_const_str(qstr));
130 break;
131
Damiend99b0522013-12-21 18:17:45 +0000132 case MP_BC_LOAD_FAST_0:
Damience89a212013-10-15 22:25:17 +0100133 PUSH(fast0);
134 break;
135
Damiend99b0522013-12-21 18:17:45 +0000136 case MP_BC_LOAD_FAST_1:
Damience89a212013-10-15 22:25:17 +0100137 PUSH(fast1);
138 break;
139
Damiend99b0522013-12-21 18:17:45 +0000140 case MP_BC_LOAD_FAST_2:
Damience89a212013-10-15 22:25:17 +0100141 PUSH(fast2);
142 break;
143
Damiend99b0522013-12-21 18:17:45 +0000144 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100145 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100146 PUSH(fastn[unum]);
Damience89a212013-10-15 22:25:17 +0100147 break;
148
Damiend99b0522013-12-21 18:17:45 +0000149 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000150 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000151 PUSH(rt_get_cell(fastn[unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000152 break;
153
Damiend99b0522013-12-21 18:17:45 +0000154 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100155 DECODE_QSTR;
156 PUSH(rt_load_name(qstr));
157 break;
158
Damiend99b0522013-12-21 18:17:45 +0000159 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100160 DECODE_QSTR;
161 PUSH(rt_load_global(qstr));
162 break;
163
Damiend99b0522013-12-21 18:17:45 +0000164 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100165 DECODE_QSTR;
Damiendb4c3612013-12-10 17:27:24 +0000166 SET_TOP(rt_load_attr(TOP(), qstr));
Damience89a212013-10-15 22:25:17 +0100167 break;
168
Damiend99b0522013-12-21 18:17:45 +0000169 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100170 DECODE_QSTR;
171 sp -= 1;
172 rt_load_method(sp[1], qstr, sp);
173 break;
174
Damiend99b0522013-12-21 18:17:45 +0000175 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100176 PUSH(rt_load_build_class());
177 break;
178
Damiend99b0522013-12-21 18:17:45 +0000179 case MP_BC_STORE_FAST_0:
Damience89a212013-10-15 22:25:17 +0100180 fast0 = POP();
181 break;
182
Damiend99b0522013-12-21 18:17:45 +0000183 case MP_BC_STORE_FAST_1:
Damience89a212013-10-15 22:25:17 +0100184 fast1 = POP();
185 break;
186
Damiend99b0522013-12-21 18:17:45 +0000187 case MP_BC_STORE_FAST_2:
Damience89a212013-10-15 22:25:17 +0100188 fast2 = POP();
189 break;
190
Damiend99b0522013-12-21 18:17:45 +0000191 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100192 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100193 fastn[unum] = POP();
Damience89a212013-10-15 22:25:17 +0100194 break;
195
Damiend99b0522013-12-21 18:17:45 +0000196 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000197 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000198 rt_set_cell(fastn[unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000199 break;
200
Damiend99b0522013-12-21 18:17:45 +0000201 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100202 DECODE_QSTR;
203 rt_store_name(qstr, POP());
204 break;
205
Damiend99b0522013-12-21 18:17:45 +0000206 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000207 DECODE_QSTR;
208 rt_store_global(qstr, POP());
209 break;
210
Damiend99b0522013-12-21 18:17:45 +0000211 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100212 DECODE_QSTR;
213 rt_store_attr(sp[0], qstr, sp[1]);
214 sp += 2;
215 break;
216
Damiend99b0522013-12-21 18:17:45 +0000217 case MP_BC_STORE_SUBSCR:
Damience89a212013-10-15 22:25:17 +0100218 rt_store_subscr(sp[1], sp[0], sp[2]);
219 sp += 3;
220 break;
221
Damiend99b0522013-12-21 18:17:45 +0000222 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000223 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100224 PUSH(obj1);
225 break;
226
Damiend99b0522013-12-21 18:17:45 +0000227 case MP_BC_DUP_TOP_TWO:
Damience89a212013-10-15 22:25:17 +0100228 sp -= 2;
229 sp[0] = sp[2];
230 sp[1] = sp[3];
231 break;
232
Damiend99b0522013-12-21 18:17:45 +0000233 case MP_BC_POP_TOP:
Damience89a212013-10-15 22:25:17 +0100234 ++sp;
235 break;
236
Damiend99b0522013-12-21 18:17:45 +0000237 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000238 obj1 = sp[0];
239 sp[0] = sp[1];
240 sp[1] = obj1;
241 break;
242
Damiend99b0522013-12-21 18:17:45 +0000243 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100244 obj1 = sp[0];
245 sp[0] = sp[1];
246 sp[1] = sp[2];
247 sp[2] = obj1;
248 break;
249
Damiend99b0522013-12-21 18:17:45 +0000250 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000251 DECODE_SLABEL;
252 ip += unum;
Damience89a212013-10-15 22:25:17 +0100253 break;
254
Damiend99b0522013-12-21 18:17:45 +0000255 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000256 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100257 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000258 ip += unum;
Damience89a212013-10-15 22:25:17 +0100259 }
260 break;
261
Damiend99b0522013-12-21 18:17:45 +0000262 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000263 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100264 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000265 ip += unum;
Damience89a212013-10-15 22:25:17 +0100266 }
267 break;
268
Damiend99b0522013-12-21 18:17:45 +0000269 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000270 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000271 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000272 ip += unum;
273 } else {
274 sp++;
275 }
276 break;
277
Damiend99b0522013-12-21 18:17:45 +0000278 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000279 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000280 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000281 sp++;
282 } else {
283 ip += unum;
284 }
285 break;
286
Damience89a212013-10-15 22:25:17 +0100287 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000288 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100289 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000290 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100291 break;
292 */
293
Damien02a7c412013-12-29 18:48:37 +0000294 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000295 case MP_BC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000296 DECODE_ULABEL; // except labels are always forward
297 *++exc_sp = (machine_uint_t)ip + unum;
Damien8f9e2ee2013-12-29 16:54:59 +0000298 *++exc_sp = (((machine_uint_t)sp) | currently_in_except_block);
299 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100300 break;
301
Damiend99b0522013-12-21 18:17:45 +0000302 case MP_BC_END_FINALLY:
Damience89a212013-10-15 22:25:17 +0100303 // not implemented
304 // if TOS is an exception, reraises the exception (3 values on TOS)
305 // if TOS is an integer, does something else
306 // if TOS is None, just pops it and continues
307 // else error
308 assert(0);
309 break;
310
Damiend99b0522013-12-21 18:17:45 +0000311 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000312 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100313 break;
314
Damiend99b0522013-12-21 18:17:45 +0000315 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000316 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000317 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000318 if (obj1 == mp_const_stop_iteration) {
Damience89a212013-10-15 22:25:17 +0100319 ++sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000320 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100321 } else {
322 PUSH(obj1); // push the next iteration value
323 }
324 break;
325
Damien02a7c412013-12-29 18:48:37 +0000326 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000327 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000328 // we are exiting an exception handler, so pop the last one of the exception-stack
329 assert(exc_sp >= &exc_stack[0]);
330 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
331 exc_sp -= 2; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100332 break;
333
Damien02a7c412013-12-29 18:48:37 +0000334 // matched againts: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000335 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100336 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100337 // 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 +0100338 assert(exc_sp >= &exc_stack[0]);
Damiend99b0522013-12-21 18:17:45 +0000339 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100340 //exc_sp--; // discard ip
Damien8f9e2ee2013-12-29 16:54:59 +0000341 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
342 exc_sp -= 2; // pop back to previous exception handler
Damienc9f91972013-10-15 23:46:01 +0100343 //sp += 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100344 break;
345
Damiend99b0522013-12-21 18:17:45 +0000346 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000347 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000348 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000349 break;
350
Damiend99b0522013-12-21 18:17:45 +0000351 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100352 unum = *ip++;
353 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000354 obj1 = TOP();
355 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100356 break;
357
Damiend99b0522013-12-21 18:17:45 +0000358 case MP_BC_COMPARE_OP:
Damience89a212013-10-15 22:25:17 +0100359 unum = *ip++;
360 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000361 obj1 = TOP();
362 SET_TOP(rt_compare_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100363 break;
364
Damiend99b0522013-12-21 18:17:45 +0000365 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100366 DECODE_UINT;
367 obj1 = rt_build_tuple(unum, sp);
368 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000369 SET_TOP(obj1);
Damienc226dca2013-10-16 16:12:52 +0100370 break;
371
Damiend99b0522013-12-21 18:17:45 +0000372 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100373 DECODE_UINT;
374 obj1 = rt_build_list(unum, sp);
375 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000376 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100377 break;
378
Damiend99b0522013-12-21 18:17:45 +0000379 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100380 DECODE_UINT;
381 // I think it's guaranteed by the compiler that sp[unum] is a list
382 rt_list_append(sp[unum], sp[0]);
383 sp++;
384 break;
385
Damiend99b0522013-12-21 18:17:45 +0000386 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100387 DECODE_UINT;
388 PUSH(rt_build_map(unum));
389 break;
390
Damiend99b0522013-12-21 18:17:45 +0000391 case MP_BC_STORE_MAP:
Damience89a212013-10-15 22:25:17 +0100392 sp += 2;
393 rt_store_map(sp[0], sp[-2], sp[-1]);
394 break;
395
Damiend99b0522013-12-21 18:17:45 +0000396 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100397 DECODE_UINT;
398 // I think it's guaranteed by the compiler that sp[unum + 1] is a map
399 rt_store_map(sp[unum + 1], sp[0], sp[1]);
400 sp += 2;
401 break;
402
Damiend99b0522013-12-21 18:17:45 +0000403 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100404 DECODE_UINT;
405 obj1 = rt_build_set(unum, sp);
406 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000407 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100408 break;
409
Damiend99b0522013-12-21 18:17:45 +0000410 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100411 DECODE_UINT;
412 // I think it's guaranteed by the compiler that sp[unum] is a set
413 rt_store_set(sp[unum], sp[0]);
414 sp++;
415 break;
416
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200417#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200418 case MP_BC_BUILD_SLICE:
419 DECODE_UINT;
420 if (unum == 2) {
421 obj2 = POP();
422 obj1 = TOP();
423 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
424 } else {
425 printf("3-argument slice is not supported\n");
426 assert(0);
427 }
428 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200429#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200430
Damiend99b0522013-12-21 18:17:45 +0000431 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000432 DECODE_UINT;
433 rt_unpack_sequence(sp[0], unum, sp - unum + 1);
434 sp -= unum - 1;
435 break;
436
Damiend99b0522013-12-21 18:17:45 +0000437 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100438 DECODE_UINT;
439 PUSH(rt_make_function_from_id(unum));
440 break;
441
Damiend99b0522013-12-21 18:17:45 +0000442 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000443 DECODE_UINT;
444 obj1 = POP();
445 PUSH(rt_make_closure_from_id(unum, obj1));
446 break;
447
Damiend99b0522013-12-21 18:17:45 +0000448 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100449 DECODE_UINT;
450 assert((unum & 0xff00) == 0); // n_keyword
451 unum &= 0xff; // n_positional
452 sp += unum;
453 *sp = rt_call_function_n(*sp, unum, sp - unum);
454 break;
455
Damiend99b0522013-12-21 18:17:45 +0000456 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100457 DECODE_UINT;
Damien6f3e7fc2013-11-26 15:15:50 +0000458 if ((unum & 0xff00) == 0) {
459 // no keywords
460 unum &= 0xff;
461 obj1 = rt_call_method_n(unum, sp);
462 sp += unum + 1;
463 } else {
464 // keywords
465 obj1 = rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp);
466 sp += (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
467 }
Damiendb4c3612013-12-10 17:27:24 +0000468 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100469 break;
470
Damiend99b0522013-12-21 18:17:45 +0000471 case MP_BC_RETURN_VALUE:
Damience89a212013-10-15 22:25:17 +0100472 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100473 *sp_in_out = sp;
Damien George66327002014-01-02 18:20:41 +0000474 assert(exc_sp == &exc_stack[0] - 1);
Damienbd254452013-10-16 20:39:12 +0100475 return false;
476
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200477 case MP_BC_RAISE_VARARGS:
478 unum = *ip++;
479 assert(unum == 1);
480 obj1 = POP();
481 nlr_jump(obj1);
482 return false;
483
Damiend99b0522013-12-21 18:17:45 +0000484 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100485 nlr_pop();
486 *ip_in_out = ip;
487 fastn[0] = fast0;
488 fastn[1] = fast1;
489 fastn[2] = fast2;
490 *sp_in_out = sp;
491 return true;
Damience89a212013-10-15 22:25:17 +0100492
Damiend99b0522013-12-21 18:17:45 +0000493 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000494 DECODE_QSTR;
495 obj1 = POP();
496 SET_TOP(rt_import_name(qstr, obj1, TOP()));
497 break;
498
Damiend99b0522013-12-21 18:17:45 +0000499 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000500 DECODE_QSTR;
501 obj1 = rt_import_from(TOP(), qstr);
502 PUSH(obj1);
503 break;
504
Damience89a212013-10-15 22:25:17 +0100505 default:
Damien03c9cfb2013-11-05 22:06:08 +0000506 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100507 assert(0);
508 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100509 return false;
Damien429d7192013-10-04 19:53:11 +0100510 }
Damience89a212013-10-15 22:25:17 +0100511 }
Damien429d7192013-10-04 19:53:11 +0100512
Damience89a212013-10-15 22:25:17 +0100513 } else {
514 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100515
Damien8f9e2ee2013-12-29 16:54:59 +0000516 while (currently_in_except_block) {
517 // nested exception
518
519 assert(exc_sp >= &exc_stack[0]);
520
521 // TODO make a proper message for nested exception
522 // at the moment we are just raising the very last exception (the one that caused the nested exception)
523
524 // move up to previous exception handler
525 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
526 exc_sp -= 2; // pop back to previous exception handler
527 }
528
Damienc9f91972013-10-15 23:46:01 +0100529 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000530 // set flag to indicate that we are now handling an exception
531 currently_in_except_block = 1;
532
Damience89a212013-10-15 22:25:17 +0100533 // catch exception and pass to byte code
Damien8f9e2ee2013-12-29 16:54:59 +0000534 sp = (mp_obj_t*)(exc_sp[0] & (~((machine_uint_t)1)));
Damienc9f91972013-10-15 23:46:01 +0100535 ip = (const byte*)(exc_sp[-1]);
536 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000537 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100538 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000539 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000540
Damience89a212013-10-15 22:25:17 +0100541 } else {
Damien8f9e2ee2013-12-29 16:54:59 +0000542 // re-raise exception to higher level
Damienbd254452013-10-16 20:39:12 +0100543 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100544 nlr_jump(nlr.ret_val);
545 }
Damien429d7192013-10-04 19:53:11 +0100546 }
547 }
548}