blob: 35dcbea52333eb73a5eae124041d0c4c6577a132 [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
Damiend99b0522013-12-21 18:17:45 +0000102 case MP_BC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +0000103 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +0000104 ip += 3;
Damiend99b0522013-12-21 18:17:45 +0000105 PUSH((mp_obj_t)(unum << 1 | 1));
Damience89a212013-10-15 22:25:17 +0100106 break;
107
Damiend99b0522013-12-21 18:17:45 +0000108 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000109 DECODE_QSTR;
110 PUSH(rt_load_const_dec(qstr));
111 break;
112
Damiend99b0522013-12-21 18:17:45 +0000113 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100114 DECODE_QSTR;
115 PUSH(rt_load_const_str(qstr)); // TODO
116 break;
117
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200118 case MP_BC_LOAD_CONST_BYTES:
119 DECODE_QSTR;
120 PUSH(rt_load_const_str(qstr)); // TODO
121 break;
122
Damiend99b0522013-12-21 18:17:45 +0000123 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100124 DECODE_QSTR;
125 PUSH(rt_load_const_str(qstr));
126 break;
127
Damiend99b0522013-12-21 18:17:45 +0000128 case MP_BC_LOAD_FAST_0:
Damience89a212013-10-15 22:25:17 +0100129 PUSH(fast0);
130 break;
131
Damiend99b0522013-12-21 18:17:45 +0000132 case MP_BC_LOAD_FAST_1:
Damience89a212013-10-15 22:25:17 +0100133 PUSH(fast1);
134 break;
135
Damiend99b0522013-12-21 18:17:45 +0000136 case MP_BC_LOAD_FAST_2:
Damience89a212013-10-15 22:25:17 +0100137 PUSH(fast2);
138 break;
139
Damiend99b0522013-12-21 18:17:45 +0000140 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100141 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100142 PUSH(fastn[unum]);
Damience89a212013-10-15 22:25:17 +0100143 break;
144
Damiend99b0522013-12-21 18:17:45 +0000145 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000146 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000147 PUSH(rt_get_cell(fastn[unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000148 break;
149
Damiend99b0522013-12-21 18:17:45 +0000150 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100151 DECODE_QSTR;
152 PUSH(rt_load_name(qstr));
153 break;
154
Damiend99b0522013-12-21 18:17:45 +0000155 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100156 DECODE_QSTR;
157 PUSH(rt_load_global(qstr));
158 break;
159
Damiend99b0522013-12-21 18:17:45 +0000160 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100161 DECODE_QSTR;
Damiendb4c3612013-12-10 17:27:24 +0000162 SET_TOP(rt_load_attr(TOP(), qstr));
Damience89a212013-10-15 22:25:17 +0100163 break;
164
Damiend99b0522013-12-21 18:17:45 +0000165 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100166 DECODE_QSTR;
167 sp -= 1;
168 rt_load_method(sp[1], qstr, sp);
169 break;
170
Damiend99b0522013-12-21 18:17:45 +0000171 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100172 PUSH(rt_load_build_class());
173 break;
174
Damiend99b0522013-12-21 18:17:45 +0000175 case MP_BC_STORE_FAST_0:
Damience89a212013-10-15 22:25:17 +0100176 fast0 = POP();
177 break;
178
Damiend99b0522013-12-21 18:17:45 +0000179 case MP_BC_STORE_FAST_1:
Damience89a212013-10-15 22:25:17 +0100180 fast1 = POP();
181 break;
182
Damiend99b0522013-12-21 18:17:45 +0000183 case MP_BC_STORE_FAST_2:
Damience89a212013-10-15 22:25:17 +0100184 fast2 = POP();
185 break;
186
Damiend99b0522013-12-21 18:17:45 +0000187 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100188 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100189 fastn[unum] = POP();
Damience89a212013-10-15 22:25:17 +0100190 break;
191
Damiend99b0522013-12-21 18:17:45 +0000192 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000193 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000194 rt_set_cell(fastn[unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000195 break;
196
Damiend99b0522013-12-21 18:17:45 +0000197 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100198 DECODE_QSTR;
199 rt_store_name(qstr, POP());
200 break;
201
Damiend99b0522013-12-21 18:17:45 +0000202 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000203 DECODE_QSTR;
204 rt_store_global(qstr, POP());
205 break;
206
Damiend99b0522013-12-21 18:17:45 +0000207 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100208 DECODE_QSTR;
209 rt_store_attr(sp[0], qstr, sp[1]);
210 sp += 2;
211 break;
212
Damiend99b0522013-12-21 18:17:45 +0000213 case MP_BC_STORE_SUBSCR:
Damience89a212013-10-15 22:25:17 +0100214 rt_store_subscr(sp[1], sp[0], sp[2]);
215 sp += 3;
216 break;
217
Damiend99b0522013-12-21 18:17:45 +0000218 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000219 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100220 PUSH(obj1);
221 break;
222
Damiend99b0522013-12-21 18:17:45 +0000223 case MP_BC_DUP_TOP_TWO:
Damience89a212013-10-15 22:25:17 +0100224 sp -= 2;
225 sp[0] = sp[2];
226 sp[1] = sp[3];
227 break;
228
Damiend99b0522013-12-21 18:17:45 +0000229 case MP_BC_POP_TOP:
Damience89a212013-10-15 22:25:17 +0100230 ++sp;
231 break;
232
Damiend99b0522013-12-21 18:17:45 +0000233 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000234 obj1 = sp[0];
235 sp[0] = sp[1];
236 sp[1] = obj1;
237 break;
238
Damiend99b0522013-12-21 18:17:45 +0000239 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100240 obj1 = sp[0];
241 sp[0] = sp[1];
242 sp[1] = sp[2];
243 sp[2] = obj1;
244 break;
245
Damiend99b0522013-12-21 18:17:45 +0000246 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000247 DECODE_SLABEL;
248 ip += unum;
Damience89a212013-10-15 22:25:17 +0100249 break;
250
Damiend99b0522013-12-21 18:17:45 +0000251 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000252 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100253 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000254 ip += unum;
Damience89a212013-10-15 22:25:17 +0100255 }
256 break;
257
Damiend99b0522013-12-21 18:17:45 +0000258 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000259 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100260 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000261 ip += unum;
Damience89a212013-10-15 22:25:17 +0100262 }
263 break;
264
Damiend99b0522013-12-21 18:17:45 +0000265 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000266 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000267 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000268 ip += unum;
269 } else {
270 sp++;
271 }
272 break;
273
Damiend99b0522013-12-21 18:17:45 +0000274 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000275 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000276 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000277 sp++;
278 } else {
279 ip += unum;
280 }
281 break;
282
Damience89a212013-10-15 22:25:17 +0100283 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000284 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100285 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000286 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100287 break;
288 */
289
Damien02a7c412013-12-29 18:48:37 +0000290 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000291 case MP_BC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000292 DECODE_ULABEL; // except labels are always forward
293 *++exc_sp = (machine_uint_t)ip + unum;
Damien8f9e2ee2013-12-29 16:54:59 +0000294 *++exc_sp = (((machine_uint_t)sp) | currently_in_except_block);
295 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100296 break;
297
Damiend99b0522013-12-21 18:17:45 +0000298 case MP_BC_END_FINALLY:
Damience89a212013-10-15 22:25:17 +0100299 // not implemented
300 // if TOS is an exception, reraises the exception (3 values on TOS)
301 // if TOS is an integer, does something else
302 // if TOS is None, just pops it and continues
303 // else error
304 assert(0);
305 break;
306
Damiend99b0522013-12-21 18:17:45 +0000307 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000308 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100309 break;
310
Damiend99b0522013-12-21 18:17:45 +0000311 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000312 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000313 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000314 if (obj1 == mp_const_stop_iteration) {
Damience89a212013-10-15 22:25:17 +0100315 ++sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000316 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100317 } else {
318 PUSH(obj1); // push the next iteration value
319 }
320 break;
321
Damien02a7c412013-12-29 18:48:37 +0000322 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000323 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000324 // we are exiting an exception handler, so pop the last one of the exception-stack
325 assert(exc_sp >= &exc_stack[0]);
326 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
327 exc_sp -= 2; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100328 break;
329
Damien02a7c412013-12-29 18:48:37 +0000330 // matched againts: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000331 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100332 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100333 // 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 +0100334 assert(exc_sp >= &exc_stack[0]);
Damiend99b0522013-12-21 18:17:45 +0000335 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100336 //exc_sp--; // discard ip
Damien8f9e2ee2013-12-29 16:54:59 +0000337 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
338 exc_sp -= 2; // pop back to previous exception handler
Damienc9f91972013-10-15 23:46:01 +0100339 //sp += 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100340 break;
341
Damiend99b0522013-12-21 18:17:45 +0000342 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000343 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000344 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000345 break;
346
Damiend99b0522013-12-21 18:17:45 +0000347 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100348 unum = *ip++;
349 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000350 obj1 = TOP();
351 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100352 break;
353
Damiend99b0522013-12-21 18:17:45 +0000354 case MP_BC_COMPARE_OP:
Damience89a212013-10-15 22:25:17 +0100355 unum = *ip++;
356 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000357 obj1 = TOP();
358 SET_TOP(rt_compare_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100359 break;
360
Damiend99b0522013-12-21 18:17:45 +0000361 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100362 DECODE_UINT;
363 obj1 = rt_build_tuple(unum, sp);
364 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000365 SET_TOP(obj1);
Damienc226dca2013-10-16 16:12:52 +0100366 break;
367
Damiend99b0522013-12-21 18:17:45 +0000368 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100369 DECODE_UINT;
370 obj1 = rt_build_list(unum, sp);
371 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000372 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100373 break;
374
Damiend99b0522013-12-21 18:17:45 +0000375 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100376 DECODE_UINT;
377 // I think it's guaranteed by the compiler that sp[unum] is a list
378 rt_list_append(sp[unum], sp[0]);
379 sp++;
380 break;
381
Damiend99b0522013-12-21 18:17:45 +0000382 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100383 DECODE_UINT;
384 PUSH(rt_build_map(unum));
385 break;
386
Damiend99b0522013-12-21 18:17:45 +0000387 case MP_BC_STORE_MAP:
Damience89a212013-10-15 22:25:17 +0100388 sp += 2;
389 rt_store_map(sp[0], sp[-2], sp[-1]);
390 break;
391
Damiend99b0522013-12-21 18:17:45 +0000392 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100393 DECODE_UINT;
394 // I think it's guaranteed by the compiler that sp[unum + 1] is a map
395 rt_store_map(sp[unum + 1], sp[0], sp[1]);
396 sp += 2;
397 break;
398
Damiend99b0522013-12-21 18:17:45 +0000399 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100400 DECODE_UINT;
401 obj1 = rt_build_set(unum, sp);
402 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000403 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100404 break;
405
Damiend99b0522013-12-21 18:17:45 +0000406 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100407 DECODE_UINT;
408 // I think it's guaranteed by the compiler that sp[unum] is a set
409 rt_store_set(sp[unum], sp[0]);
410 sp++;
411 break;
412
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200413 case MP_BC_BUILD_SLICE:
414 DECODE_UINT;
415 if (unum == 2) {
416 obj2 = POP();
417 obj1 = TOP();
418 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
419 } else {
420 printf("3-argument slice is not supported\n");
421 assert(0);
422 }
423 break;
424
Damiend99b0522013-12-21 18:17:45 +0000425 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000426 DECODE_UINT;
427 rt_unpack_sequence(sp[0], unum, sp - unum + 1);
428 sp -= unum - 1;
429 break;
430
Damiend99b0522013-12-21 18:17:45 +0000431 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100432 DECODE_UINT;
433 PUSH(rt_make_function_from_id(unum));
434 break;
435
Damiend99b0522013-12-21 18:17:45 +0000436 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000437 DECODE_UINT;
438 obj1 = POP();
439 PUSH(rt_make_closure_from_id(unum, obj1));
440 break;
441
Damiend99b0522013-12-21 18:17:45 +0000442 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100443 DECODE_UINT;
444 assert((unum & 0xff00) == 0); // n_keyword
445 unum &= 0xff; // n_positional
446 sp += unum;
447 *sp = rt_call_function_n(*sp, unum, sp - unum);
448 break;
449
Damiend99b0522013-12-21 18:17:45 +0000450 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100451 DECODE_UINT;
Damien6f3e7fc2013-11-26 15:15:50 +0000452 if ((unum & 0xff00) == 0) {
453 // no keywords
454 unum &= 0xff;
455 obj1 = rt_call_method_n(unum, sp);
456 sp += unum + 1;
457 } else {
458 // keywords
459 obj1 = rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp);
460 sp += (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
461 }
Damiendb4c3612013-12-10 17:27:24 +0000462 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100463 break;
464
Damiend99b0522013-12-21 18:17:45 +0000465 case MP_BC_RETURN_VALUE:
Damience89a212013-10-15 22:25:17 +0100466 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100467 *sp_in_out = sp;
Damien George66327002014-01-02 18:20:41 +0000468 assert(exc_sp == &exc_stack[0] - 1);
Damienbd254452013-10-16 20:39:12 +0100469 return false;
470
Damiend99b0522013-12-21 18:17:45 +0000471 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100472 nlr_pop();
473 *ip_in_out = ip;
474 fastn[0] = fast0;
475 fastn[1] = fast1;
476 fastn[2] = fast2;
477 *sp_in_out = sp;
478 return true;
Damience89a212013-10-15 22:25:17 +0100479
Damiend99b0522013-12-21 18:17:45 +0000480 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000481 DECODE_QSTR;
482 obj1 = POP();
483 SET_TOP(rt_import_name(qstr, obj1, TOP()));
484 break;
485
Damiend99b0522013-12-21 18:17:45 +0000486 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000487 DECODE_QSTR;
488 obj1 = rt_import_from(TOP(), qstr);
489 PUSH(obj1);
490 break;
491
Damience89a212013-10-15 22:25:17 +0100492 default:
Damien03c9cfb2013-11-05 22:06:08 +0000493 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100494 assert(0);
495 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100496 return false;
Damien429d7192013-10-04 19:53:11 +0100497 }
Damience89a212013-10-15 22:25:17 +0100498 }
Damien429d7192013-10-04 19:53:11 +0100499
Damience89a212013-10-15 22:25:17 +0100500 } else {
501 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100502
Damien8f9e2ee2013-12-29 16:54:59 +0000503 while (currently_in_except_block) {
504 // nested exception
505
506 assert(exc_sp >= &exc_stack[0]);
507
508 // TODO make a proper message for nested exception
509 // at the moment we are just raising the very last exception (the one that caused the nested exception)
510
511 // move up to previous exception handler
512 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
513 exc_sp -= 2; // pop back to previous exception handler
514 }
515
Damienc9f91972013-10-15 23:46:01 +0100516 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000517 // set flag to indicate that we are now handling an exception
518 currently_in_except_block = 1;
519
Damience89a212013-10-15 22:25:17 +0100520 // catch exception and pass to byte code
Damien8f9e2ee2013-12-29 16:54:59 +0000521 sp = (mp_obj_t*)(exc_sp[0] & (~((machine_uint_t)1)));
Damienc9f91972013-10-15 23:46:01 +0100522 ip = (const byte*)(exc_sp[-1]);
523 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000524 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100525 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000526 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000527
Damience89a212013-10-15 22:25:17 +0100528 } else {
Damien8f9e2ee2013-12-29 16:54:59 +0000529 // re-raise exception to higher level
Damienbd254452013-10-16 20:39:12 +0100530 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100531 nlr_jump(nlr.ret_val);
532 }
Damien429d7192013-10-04 19:53:11 +0100533 }
534 }
535}