blob: a23b0e89fb95d076736c442249f6e274703ad97d [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;
Damiend99b0522013-12-21 18:17:45 +000042 if (mp_execute_byte_code_2(&ip, &state[0], &sp)) {
Damienbd254452013-10-16 20:39:12 +010043 // it shouldn't yield
44 assert(0);
45 }
Damien4ebb32f2013-11-02 14:33:10 +000046 // TODO check fails if, eg, return from within for loop
47 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010048 return *sp;
49}
50
51// fastn has items in normal order
52// sp points to top of stack which grows down
Damiend99b0522013-12-21 18:17:45 +000053bool 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 +010054 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
55
Damienbd254452013-10-16 20:39:12 +010056 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +000057 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +010058 machine_uint_t unum;
Damien429d7192013-10-04 19:53:11 +010059 qstr qstr;
Damiend99b0522013-12-21 18:17:45 +000060 mp_obj_t obj1, obj2;
61 mp_obj_t fast0 = fastn[0], fast1 = fastn[1], fast2 = fastn[2];
Damience89a212013-10-15 22:25:17 +010062 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +010063
Damien8f9e2ee2013-12-29 16:54:59 +000064 volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
65 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
Damienc9f91972013-10-15 23:46:01 +010066 machine_uint_t *volatile exc_sp = &exc_stack[-1]; // stack grows up, exc_sp points to top of stack
67
Damience89a212013-10-15 22:25:17 +010068 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +010069 for (;;) {
Damience89a212013-10-15 22:25:17 +010070 if (nlr_push(&nlr) == 0) {
71 // loop to execute byte code
72 for (;;) {
73 int op = *ip++;
74 switch (op) {
Damiend99b0522013-12-21 18:17:45 +000075 case MP_BC_LOAD_CONST_FALSE:
76 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +010077 break;
Damien429d7192013-10-04 19:53:11 +010078
Damiend99b0522013-12-21 18:17:45 +000079 case MP_BC_LOAD_CONST_NONE:
80 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +010081 break;
Damien429d7192013-10-04 19:53:11 +010082
Damiend99b0522013-12-21 18:17:45 +000083 case MP_BC_LOAD_CONST_TRUE:
84 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +010085 break;
Damien429d7192013-10-04 19:53:11 +010086
Damiend99b0522013-12-21 18:17:45 +000087 case MP_BC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +000088 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +000089 ip += 3;
Damiend99b0522013-12-21 18:17:45 +000090 PUSH((mp_obj_t)(unum << 1 | 1));
Damience89a212013-10-15 22:25:17 +010091 break;
92
Damiend99b0522013-12-21 18:17:45 +000093 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +000094 DECODE_QSTR;
95 PUSH(rt_load_const_dec(qstr));
96 break;
97
Damiend99b0522013-12-21 18:17:45 +000098 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +010099 DECODE_QSTR;
100 PUSH(rt_load_const_str(qstr)); // TODO
101 break;
102
Damiend99b0522013-12-21 18:17:45 +0000103 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100104 DECODE_QSTR;
105 PUSH(rt_load_const_str(qstr));
106 break;
107
Damiend99b0522013-12-21 18:17:45 +0000108 case MP_BC_LOAD_FAST_0:
Damience89a212013-10-15 22:25:17 +0100109 PUSH(fast0);
110 break;
111
Damiend99b0522013-12-21 18:17:45 +0000112 case MP_BC_LOAD_FAST_1:
Damience89a212013-10-15 22:25:17 +0100113 PUSH(fast1);
114 break;
115
Damiend99b0522013-12-21 18:17:45 +0000116 case MP_BC_LOAD_FAST_2:
Damience89a212013-10-15 22:25:17 +0100117 PUSH(fast2);
118 break;
119
Damiend99b0522013-12-21 18:17:45 +0000120 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100121 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100122 PUSH(fastn[unum]);
Damience89a212013-10-15 22:25:17 +0100123 break;
124
Damiend99b0522013-12-21 18:17:45 +0000125 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000126 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000127 PUSH(rt_get_cell(fastn[unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000128 break;
129
Damiend99b0522013-12-21 18:17:45 +0000130 case MP_BC_LOAD_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000131 DECODE_UINT;
132 PUSH(fastn[unum]);
133 break;
134
Damiend99b0522013-12-21 18:17:45 +0000135 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100136 DECODE_QSTR;
137 PUSH(rt_load_name(qstr));
138 break;
139
Damiend99b0522013-12-21 18:17:45 +0000140 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100141 DECODE_QSTR;
142 PUSH(rt_load_global(qstr));
143 break;
144
Damiend99b0522013-12-21 18:17:45 +0000145 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100146 DECODE_QSTR;
Damiendb4c3612013-12-10 17:27:24 +0000147 SET_TOP(rt_load_attr(TOP(), qstr));
Damience89a212013-10-15 22:25:17 +0100148 break;
149
Damiend99b0522013-12-21 18:17:45 +0000150 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100151 DECODE_QSTR;
152 sp -= 1;
153 rt_load_method(sp[1], qstr, sp);
154 break;
155
Damiend99b0522013-12-21 18:17:45 +0000156 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100157 PUSH(rt_load_build_class());
158 break;
159
Damiend99b0522013-12-21 18:17:45 +0000160 case MP_BC_STORE_FAST_0:
Damience89a212013-10-15 22:25:17 +0100161 fast0 = POP();
162 break;
163
Damiend99b0522013-12-21 18:17:45 +0000164 case MP_BC_STORE_FAST_1:
Damience89a212013-10-15 22:25:17 +0100165 fast1 = POP();
166 break;
167
Damiend99b0522013-12-21 18:17:45 +0000168 case MP_BC_STORE_FAST_2:
Damience89a212013-10-15 22:25:17 +0100169 fast2 = POP();
170 break;
171
Damiend99b0522013-12-21 18:17:45 +0000172 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100173 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100174 fastn[unum] = POP();
Damience89a212013-10-15 22:25:17 +0100175 break;
176
Damiend99b0522013-12-21 18:17:45 +0000177 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000178 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000179 rt_set_cell(fastn[unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000180 break;
181
Damiend99b0522013-12-21 18:17:45 +0000182 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100183 DECODE_QSTR;
184 rt_store_name(qstr, POP());
185 break;
186
Damiend99b0522013-12-21 18:17:45 +0000187 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000188 DECODE_QSTR;
189 rt_store_global(qstr, POP());
190 break;
191
Damiend99b0522013-12-21 18:17:45 +0000192 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100193 DECODE_QSTR;
194 rt_store_attr(sp[0], qstr, sp[1]);
195 sp += 2;
196 break;
197
Damiend99b0522013-12-21 18:17:45 +0000198 case MP_BC_STORE_SUBSCR:
Damience89a212013-10-15 22:25:17 +0100199 rt_store_subscr(sp[1], sp[0], sp[2]);
200 sp += 3;
201 break;
202
Damiend99b0522013-12-21 18:17:45 +0000203 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000204 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100205 PUSH(obj1);
206 break;
207
Damiend99b0522013-12-21 18:17:45 +0000208 case MP_BC_DUP_TOP_TWO:
Damience89a212013-10-15 22:25:17 +0100209 sp -= 2;
210 sp[0] = sp[2];
211 sp[1] = sp[3];
212 break;
213
Damiend99b0522013-12-21 18:17:45 +0000214 case MP_BC_POP_TOP:
Damience89a212013-10-15 22:25:17 +0100215 ++sp;
216 break;
217
Damiend99b0522013-12-21 18:17:45 +0000218 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000219 obj1 = sp[0];
220 sp[0] = sp[1];
221 sp[1] = obj1;
222 break;
223
Damiend99b0522013-12-21 18:17:45 +0000224 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100225 obj1 = sp[0];
226 sp[0] = sp[1];
227 sp[1] = sp[2];
228 sp[2] = obj1;
229 break;
230
Damiend99b0522013-12-21 18:17:45 +0000231 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000232 DECODE_SLABEL;
233 ip += unum;
Damience89a212013-10-15 22:25:17 +0100234 break;
235
Damiend99b0522013-12-21 18:17:45 +0000236 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000237 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100238 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000239 ip += unum;
Damience89a212013-10-15 22:25:17 +0100240 }
241 break;
242
Damiend99b0522013-12-21 18:17:45 +0000243 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000244 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100245 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000246 ip += unum;
Damience89a212013-10-15 22:25:17 +0100247 }
248 break;
249
Damiend99b0522013-12-21 18:17:45 +0000250 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000251 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000252 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000253 ip += unum;
254 } else {
255 sp++;
256 }
257 break;
258
Damiend99b0522013-12-21 18:17:45 +0000259 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000260 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000261 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000262 sp++;
263 } else {
264 ip += unum;
265 }
266 break;
267
Damience89a212013-10-15 22:25:17 +0100268 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000269 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100270 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000271 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100272 break;
273 */
274
Damien02a7c412013-12-29 18:48:37 +0000275 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000276 case MP_BC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000277 DECODE_ULABEL; // except labels are always forward
278 *++exc_sp = (machine_uint_t)ip + unum;
Damien8f9e2ee2013-12-29 16:54:59 +0000279 *++exc_sp = (((machine_uint_t)sp) | currently_in_except_block);
280 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100281 break;
282
Damiend99b0522013-12-21 18:17:45 +0000283 case MP_BC_END_FINALLY:
Damience89a212013-10-15 22:25:17 +0100284 // not implemented
285 // if TOS is an exception, reraises the exception (3 values on TOS)
286 // if TOS is an integer, does something else
287 // if TOS is None, just pops it and continues
288 // else error
289 assert(0);
290 break;
291
Damiend99b0522013-12-21 18:17:45 +0000292 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000293 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100294 break;
295
Damiend99b0522013-12-21 18:17:45 +0000296 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000297 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000298 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000299 if (obj1 == mp_const_stop_iteration) {
Damience89a212013-10-15 22:25:17 +0100300 ++sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000301 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100302 } else {
303 PUSH(obj1); // push the next iteration value
304 }
305 break;
306
Damien02a7c412013-12-29 18:48:37 +0000307 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000308 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000309 // we are exiting an exception handler, so pop the last one of the exception-stack
310 assert(exc_sp >= &exc_stack[0]);
311 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
312 exc_sp -= 2; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100313 break;
314
Damien02a7c412013-12-29 18:48:37 +0000315 // matched againts: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000316 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100317 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100318 // 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 +0100319 assert(exc_sp >= &exc_stack[0]);
Damiend99b0522013-12-21 18:17:45 +0000320 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100321 //exc_sp--; // discard ip
Damien8f9e2ee2013-12-29 16:54:59 +0000322 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
323 exc_sp -= 2; // pop back to previous exception handler
Damienc9f91972013-10-15 23:46:01 +0100324 //sp += 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100325 break;
326
Damiend99b0522013-12-21 18:17:45 +0000327 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000328 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000329 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000330 break;
331
Damiend99b0522013-12-21 18:17:45 +0000332 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100333 unum = *ip++;
334 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000335 obj1 = TOP();
336 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100337 break;
338
Damiend99b0522013-12-21 18:17:45 +0000339 case MP_BC_COMPARE_OP:
Damience89a212013-10-15 22:25:17 +0100340 unum = *ip++;
341 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000342 obj1 = TOP();
343 SET_TOP(rt_compare_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100344 break;
345
Damiend99b0522013-12-21 18:17:45 +0000346 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100347 DECODE_UINT;
348 obj1 = rt_build_tuple(unum, sp);
349 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000350 SET_TOP(obj1);
Damienc226dca2013-10-16 16:12:52 +0100351 break;
352
Damiend99b0522013-12-21 18:17:45 +0000353 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100354 DECODE_UINT;
355 obj1 = rt_build_list(unum, sp);
356 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000357 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100358 break;
359
Damiend99b0522013-12-21 18:17:45 +0000360 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100361 DECODE_UINT;
362 // I think it's guaranteed by the compiler that sp[unum] is a list
363 rt_list_append(sp[unum], sp[0]);
364 sp++;
365 break;
366
Damiend99b0522013-12-21 18:17:45 +0000367 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100368 DECODE_UINT;
369 PUSH(rt_build_map(unum));
370 break;
371
Damiend99b0522013-12-21 18:17:45 +0000372 case MP_BC_STORE_MAP:
Damience89a212013-10-15 22:25:17 +0100373 sp += 2;
374 rt_store_map(sp[0], sp[-2], sp[-1]);
375 break;
376
Damiend99b0522013-12-21 18:17:45 +0000377 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100378 DECODE_UINT;
379 // I think it's guaranteed by the compiler that sp[unum + 1] is a map
380 rt_store_map(sp[unum + 1], sp[0], sp[1]);
381 sp += 2;
382 break;
383
Damiend99b0522013-12-21 18:17:45 +0000384 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100385 DECODE_UINT;
386 obj1 = rt_build_set(unum, sp);
387 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000388 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100389 break;
390
Damiend99b0522013-12-21 18:17:45 +0000391 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100392 DECODE_UINT;
393 // I think it's guaranteed by the compiler that sp[unum] is a set
394 rt_store_set(sp[unum], sp[0]);
395 sp++;
396 break;
397
Damiend99b0522013-12-21 18:17:45 +0000398 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000399 DECODE_UINT;
400 rt_unpack_sequence(sp[0], unum, sp - unum + 1);
401 sp -= unum - 1;
402 break;
403
Damiend99b0522013-12-21 18:17:45 +0000404 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100405 DECODE_UINT;
406 PUSH(rt_make_function_from_id(unum));
407 break;
408
Damiend99b0522013-12-21 18:17:45 +0000409 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000410 DECODE_UINT;
411 obj1 = POP();
412 PUSH(rt_make_closure_from_id(unum, obj1));
413 break;
414
Damiend99b0522013-12-21 18:17:45 +0000415 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100416 DECODE_UINT;
417 assert((unum & 0xff00) == 0); // n_keyword
418 unum &= 0xff; // n_positional
419 sp += unum;
420 *sp = rt_call_function_n(*sp, unum, sp - unum);
421 break;
422
Damiend99b0522013-12-21 18:17:45 +0000423 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100424 DECODE_UINT;
Damien6f3e7fc2013-11-26 15:15:50 +0000425 if ((unum & 0xff00) == 0) {
426 // no keywords
427 unum &= 0xff;
428 obj1 = rt_call_method_n(unum, sp);
429 sp += unum + 1;
430 } else {
431 // keywords
432 obj1 = rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp);
433 sp += (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
434 }
Damiendb4c3612013-12-10 17:27:24 +0000435 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100436 break;
437
Damiend99b0522013-12-21 18:17:45 +0000438 case MP_BC_RETURN_VALUE:
Damience89a212013-10-15 22:25:17 +0100439 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100440 *sp_in_out = sp;
Damienc9f91972013-10-15 23:46:01 +0100441 assert(exc_sp == &exc_stack[-1]);
Damienbd254452013-10-16 20:39:12 +0100442 return false;
443
Damiend99b0522013-12-21 18:17:45 +0000444 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100445 nlr_pop();
446 *ip_in_out = ip;
447 fastn[0] = fast0;
448 fastn[1] = fast1;
449 fastn[2] = fast2;
450 *sp_in_out = sp;
451 return true;
Damience89a212013-10-15 22:25:17 +0100452
Damiend99b0522013-12-21 18:17:45 +0000453 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000454 DECODE_QSTR;
455 obj1 = POP();
456 SET_TOP(rt_import_name(qstr, obj1, TOP()));
457 break;
458
Damiend99b0522013-12-21 18:17:45 +0000459 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000460 DECODE_QSTR;
461 obj1 = rt_import_from(TOP(), qstr);
462 PUSH(obj1);
463 break;
464
Damience89a212013-10-15 22:25:17 +0100465 default:
Damien03c9cfb2013-11-05 22:06:08 +0000466 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100467 assert(0);
468 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100469 return false;
Damien429d7192013-10-04 19:53:11 +0100470 }
Damience89a212013-10-15 22:25:17 +0100471 }
Damien429d7192013-10-04 19:53:11 +0100472
Damience89a212013-10-15 22:25:17 +0100473 } else {
474 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100475
Damien8f9e2ee2013-12-29 16:54:59 +0000476 while (currently_in_except_block) {
477 // nested exception
478
479 assert(exc_sp >= &exc_stack[0]);
480
481 // TODO make a proper message for nested exception
482 // at the moment we are just raising the very last exception (the one that caused the nested exception)
483
484 // move up to previous exception handler
485 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
486 exc_sp -= 2; // pop back to previous exception handler
487 }
488
Damienc9f91972013-10-15 23:46:01 +0100489 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000490 // set flag to indicate that we are now handling an exception
491 currently_in_except_block = 1;
492
Damience89a212013-10-15 22:25:17 +0100493 // catch exception and pass to byte code
Damien8f9e2ee2013-12-29 16:54:59 +0000494 sp = (mp_obj_t*)(exc_sp[0] & (~((machine_uint_t)1)));
Damienc9f91972013-10-15 23:46:01 +0100495 ip = (const byte*)(exc_sp[-1]);
496 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000497 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100498 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000499 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000500
Damience89a212013-10-15 22:25:17 +0100501 } else {
Damien8f9e2ee2013-12-29 16:54:59 +0000502 // re-raise exception to higher level
Damienbd254452013-10-16 20:39:12 +0100503 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100504 nlr_jump(nlr.ret_val);
505 }
Damien429d7192013-10-04 19:53:11 +0100506 }
507 }
508}