blob: 52d9268184c746ebe394029d5443738a9c57ddf5 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
Paul Sokolovsky55ca0752014-03-30 17:35:53 +03002#include <string.h>
Damien429d7192013-10-04 19:53:11 +01003#include <assert.h>
4
Damience89a212013-10-15 22:25:17 +01005#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01006#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "obj.h"
Damien429d7192013-10-04 19:53:11 +010010#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "bc0.h"
Damieneb19efb2013-10-10 22:06:54 +010012#include "bc.h"
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +020013#include "objgenerator.h"
Damien429d7192013-10-04 19:53:11 +010014
Paul Sokolovsky85193422014-01-31 19:45:15 +020015// Value stack grows up (this makes it incompatible with native C stack, but
16// makes sure that arguments to functions are in natural order arg1..argN
17// (Python semantics mandates left-to-right evaluation order, including for
18// function arguments). Stack pointer is pre-incremented and points at the
19// top element.
20// Exception stack also grows up, top element is also pointed at.
21
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020022// Exception stack unwind reasons (WHY_* in CPython-speak)
Damien Georgecbddb272014-02-01 20:08:18 +000023// TODO perhaps compress this to RETURN=0, JUMP>0, with number of unwinds
24// left to do encoded in the JUMP number
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020025typedef enum {
26 UNWIND_RETURN = 1,
Damien Georgecbddb272014-02-01 20:08:18 +000027 UNWIND_JUMP,
Paul Sokolovsky6472dea2014-02-01 00:55:05 +020028} mp_unwind_reason_t;
29
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020030#define DECODE_UINT { \
31 unum = 0; \
32 do { \
33 unum = (unum << 7) + (*ip & 0x7f); \
34 } while ((*ip++ & 0x80) != 0); \
35}
Damien03c9cfb2013-11-05 22:06:08 +000036#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
37#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Paul Sokolovsky0f96ec82014-02-18 21:21:22 +020038#define DECODE_QSTR { \
39 qst = 0; \
40 do { \
41 qst = (qst << 7) + (*ip & 0x7f); \
42 } while ((*ip++ & 0x80) != 0); \
43}
Damien George20006db2014-01-18 14:10:48 +000044#define PUSH(val) *++sp = (val)
45#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000046#define TOP() (*sp)
47#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010048
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +020049#define PUSH_EXC_BLOCK() \
Paul Sokolovsky4fff26a2014-03-29 02:49:07 +020050 DECODE_ULABEL; /* except labels are always forward */ \
51 ++exc_sp; \
52 exc_sp->opcode = op; \
53 exc_sp->handler = ip + unum; \
54 exc_sp->val_sp = MP_TAGPTR_MAKE(sp, currently_in_except_block); \
Damien Georged7592a12014-03-30 00:54:48 +000055 exc_sp->prev_exc = MP_OBJ_NULL; \
Paul Sokolovsky4fff26a2014-03-29 02:49:07 +020056 currently_in_except_block = 0; /* in a try block now */
57
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +020058#define POP_EXC_BLOCK() \
59 currently_in_except_block = MP_TAGPTR_TAG(exc_sp->val_sp); /* restore previous state */ \
60 exc_sp--; /* pop back to previous exception handler */
61
Damien Georgebee17b02014-03-27 11:07:04 +000062mp_vm_return_kind_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, const mp_obj_t *args2, uint n_args2, mp_obj_t *ret) {
Damien George8dcc0c72014-03-27 10:55:21 +000063 const byte *ip = code;
64
65 // get code info size, and skip line number table
66 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
67 ip += code_info_size;
68
Damien Georgebee17b02014-03-27 11:07:04 +000069 // bytecode prelude: state size and exception stack size; 16 bit uints
70 machine_uint_t n_state = ip[0] | (ip[1] << 8);
71 machine_uint_t n_exc_stack = ip[2] | (ip[3] << 8);
72 ip += 4;
Damien George8dcc0c72014-03-27 10:55:21 +000073
Damien George20006db2014-01-18 14:10:48 +000074 // allocate state for locals and stack
75 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000076 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000077 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000078 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000079 }
Damien George20006db2014-01-18 14:10:48 +000080 mp_obj_t *sp = &state[0] - 1;
81
Damien George8dcc0c72014-03-27 10:55:21 +000082 // allocate state for exceptions
Damien George89f94b52014-03-30 00:57:09 +000083 mp_exc_stack_t exc_state[4];
84 mp_exc_stack_t *exc_stack = &exc_state[0];
Damien George8dcc0c72014-03-27 10:55:21 +000085 if (n_exc_stack > 4) {
Damien George89f94b52014-03-30 00:57:09 +000086 exc_stack = m_new(mp_exc_stack_t, n_exc_stack);
Damien George8dcc0c72014-03-27 10:55:21 +000087 }
Damien George89f94b52014-03-30 00:57:09 +000088 mp_exc_stack_t *exc_sp = &exc_stack[0] - 1;
Damien George8dcc0c72014-03-27 10:55:21 +000089
Damienbd254452013-10-16 20:39:12 +010090 // init args
Damien Georgefb083ea2014-02-01 18:29:40 +000091 for (uint i = 0; i < n_args; i++) {
Damien George20006db2014-01-18 14:10:48 +000092 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010093 }
Damien Georgefb083ea2014-02-01 18:29:40 +000094 for (uint i = 0; i < n_args2; i++) {
95 state[n_state - 1 - n_args - i] = args2[i];
96 }
Damien George08335002014-01-18 23:24:36 +000097
Damien George8dcc0c72014-03-27 10:55:21 +000098 // bytecode prelude: initialise closed over variables
99 for (uint n_local = *ip++; n_local > 0; n_local--) {
100 uint local_num = *ip++;
101 if (local_num < n_args + n_args2) {
102 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
103 } else {
104 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +0000105 }
106 }
107
108 // execute the byte code
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200109 mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp, exc_stack, &exc_sp, MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +0000110
Damien Georgec8f78bc2014-02-15 22:55:00 +0000111 switch (vm_return_kind) {
112 case MP_VM_RETURN_NORMAL:
113 *ret = *sp;
114 return MP_VM_RETURN_NORMAL;
115 case MP_VM_RETURN_EXCEPTION:
116 *ret = state[n_state - 1];
117 return MP_VM_RETURN_EXCEPTION;
118 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
119 default:
120 assert(0);
121 *ret = mp_const_none;
122 return MP_VM_RETURN_NORMAL;
123 }
Damienbd254452013-10-16 20:39:12 +0100124}
125
Damien George20006db2014-01-18 14:10:48 +0000126// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
127// sp points to bottom of stack which grows up
Damien Georgec8f78bc2014-02-15 22:55:00 +0000128// returns:
129// MP_VM_RETURN_NORMAL, sp valid, return value in *sp
130// MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
131// MP_VM_RETURN_EXCEPTION, exception in fastn[0]
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200132mp_vm_return_kind_t mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out,
133 mp_obj_t *fastn, mp_obj_t **sp_in_out,
Damien George89f94b52014-03-30 00:57:09 +0000134 mp_exc_stack_t *exc_stack, mp_exc_stack_t **exc_sp_in_out,
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200135 volatile mp_obj_t inject_exc) {
Damienc9f91972013-10-15 23:46:01 +0100136 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
137
Damienbd254452013-10-16 20:39:12 +0100138 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +0000139 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +0100140 machine_uint_t unum;
Damien Georgecbd2f742014-01-19 11:48:48 +0000141 qstr qst;
Damiend99b0522013-12-21 18:17:45 +0000142 mp_obj_t obj1, obj2;
Damience89a212013-10-15 22:25:17 +0100143 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +0100144
Paul Sokolovsky16734202014-03-22 23:20:07 +0200145 volatile bool currently_in_except_block = MP_TAGPTR_TAG(*exc_sp_in_out); // 0 or 1, to detect nested exceptions
Damien George89f94b52014-03-30 00:57:09 +0000146 mp_exc_stack_t *volatile exc_sp = MP_TAGPTR_PTR(*exc_sp_in_out); // stack grows up, exc_sp points to top of stack
Damien George08335002014-01-18 23:24:36 +0000147 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 +0100148
Damience89a212013-10-15 22:25:17 +0100149 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +0100150 for (;;) {
Damien George9e6e9352014-03-26 18:37:06 +0000151outer_dispatch_loop:
Damience89a212013-10-15 22:25:17 +0100152 if (nlr_push(&nlr) == 0) {
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200153 // If we have exception to inject, now that we finish setting up
154 // execution context, raise it. This works as if RAISE_VARARGS
155 // bytecode was executed.
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200156 // Injecting exc into yield from generator is a special case,
157 // handled by MP_BC_YIELD_FROM itself
158 if (inject_exc != MP_OBJ_NULL && *ip != MP_BC_YIELD_FROM) {
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200159 mp_obj_t t = inject_exc;
160 inject_exc = MP_OBJ_NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100161 nlr_jump(mp_make_raise_obj(t));
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200162 }
Damience89a212013-10-15 22:25:17 +0100163 // loop to execute byte code
164 for (;;) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200165dispatch_loop:
Damien George08335002014-01-18 23:24:36 +0000166 save_ip = ip;
Damience89a212013-10-15 22:25:17 +0100167 int op = *ip++;
168 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000169 case MP_BC_LOAD_CONST_FALSE:
170 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +0100171 break;
Damien429d7192013-10-04 19:53:11 +0100172
Damiend99b0522013-12-21 18:17:45 +0000173 case MP_BC_LOAD_CONST_NONE:
174 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100175 break;
Damien429d7192013-10-04 19:53:11 +0100176
Damiend99b0522013-12-21 18:17:45 +0000177 case MP_BC_LOAD_CONST_TRUE:
178 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100179 break;
Damien429d7192013-10-04 19:53:11 +0100180
Damien Georgee9906ac2014-01-04 18:44:46 +0000181 case MP_BC_LOAD_CONST_ELLIPSIS:
Damien George07ddab52014-03-29 13:15:08 +0000182 PUSH((mp_obj_t)&mp_const_ellipsis_obj);
Damien Georgee9906ac2014-01-04 18:44:46 +0000183 break;
184
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200185 case MP_BC_LOAD_CONST_SMALL_INT: {
Damien George4d79d5d2014-02-20 00:00:04 +0000186 machine_int_t num = 0;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200187 if ((ip[0] & 0x40) != 0) {
188 // Number is negative
189 num--;
190 }
191 do {
192 num = (num << 7) | (*ip & 0x7f);
193 } while ((*ip++ & 0x80) != 0);
194 PUSH(MP_OBJ_NEW_SMALL_INT(num));
Damience89a212013-10-15 22:25:17 +0100195 break;
Paul Sokolovsky047cd402014-02-19 15:47:59 +0200196 }
Damience89a212013-10-15 22:25:17 +0100197
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200198 case MP_BC_LOAD_CONST_INT:
199 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000200 PUSH(mp_obj_new_int_from_long_str(qstr_str(qst)));
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200201 break;
202
Damiend99b0522013-12-21 18:17:45 +0000203 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000204 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100205 PUSH(mp_load_const_dec(qst));
Damien7410e442013-11-02 19:47:57 +0000206 break;
207
Damiend99b0522013-12-21 18:17:45 +0000208 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100209 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100210 PUSH(mp_load_const_str(qst)); // TODO
Damience89a212013-10-15 22:25:17 +0100211 break;
212
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200213 case MP_BC_LOAD_CONST_BYTES:
214 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100215 PUSH(mp_load_const_bytes(qst));
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200216 break;
217
Damiend99b0522013-12-21 18:17:45 +0000218 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100219 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100220 PUSH(mp_load_const_str(qst));
Damience89a212013-10-15 22:25:17 +0100221 break;
222
Damiend99b0522013-12-21 18:17:45 +0000223 case MP_BC_LOAD_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000224 PUSH(fastn[0]);
Damience89a212013-10-15 22:25:17 +0100225 break;
226
Damiend99b0522013-12-21 18:17:45 +0000227 case MP_BC_LOAD_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000228 PUSH(fastn[-1]);
Damience89a212013-10-15 22:25:17 +0100229 break;
230
Damiend99b0522013-12-21 18:17:45 +0000231 case MP_BC_LOAD_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000232 PUSH(fastn[-2]);
Damience89a212013-10-15 22:25:17 +0100233 break;
234
Damiend99b0522013-12-21 18:17:45 +0000235 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100236 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000237 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100238 break;
239
Damiend99b0522013-12-21 18:17:45 +0000240 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000241 DECODE_UINT;
Damien Georged17926d2014-03-30 13:35:08 +0100242 PUSH(mp_get_cell(fastn[-unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000243 break;
244
Damiend99b0522013-12-21 18:17:45 +0000245 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100246 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100247 PUSH(mp_load_name(qst));
Damience89a212013-10-15 22:25:17 +0100248 break;
249
Damiend99b0522013-12-21 18:17:45 +0000250 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100251 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100252 PUSH(mp_load_global(qst));
Damience89a212013-10-15 22:25:17 +0100253 break;
254
Damiend99b0522013-12-21 18:17:45 +0000255 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100256 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100257 SET_TOP(mp_load_attr(TOP(), qst));
Damience89a212013-10-15 22:25:17 +0100258 break;
259
Damiend99b0522013-12-21 18:17:45 +0000260 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100261 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100262 mp_load_method(*sp, qst, sp);
Damien George20006db2014-01-18 14:10:48 +0000263 sp += 1;
Damience89a212013-10-15 22:25:17 +0100264 break;
265
Damiend99b0522013-12-21 18:17:45 +0000266 case MP_BC_LOAD_BUILD_CLASS:
Damien Georged17926d2014-03-30 13:35:08 +0100267 PUSH(mp_load_build_class());
Damience89a212013-10-15 22:25:17 +0100268 break;
269
Damiend99b0522013-12-21 18:17:45 +0000270 case MP_BC_STORE_FAST_0:
Damien Georged0691cc2014-01-29 20:30:52 +0000271 fastn[0] = POP();
Damience89a212013-10-15 22:25:17 +0100272 break;
273
Damiend99b0522013-12-21 18:17:45 +0000274 case MP_BC_STORE_FAST_1:
Damien Georged0691cc2014-01-29 20:30:52 +0000275 fastn[-1] = POP();
Damience89a212013-10-15 22:25:17 +0100276 break;
277
Damiend99b0522013-12-21 18:17:45 +0000278 case MP_BC_STORE_FAST_2:
Damien Georged0691cc2014-01-29 20:30:52 +0000279 fastn[-2] = POP();
Damience89a212013-10-15 22:25:17 +0100280 break;
281
Damiend99b0522013-12-21 18:17:45 +0000282 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100283 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000284 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100285 break;
286
Damiend99b0522013-12-21 18:17:45 +0000287 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000288 DECODE_UINT;
Damien Georged17926d2014-03-30 13:35:08 +0100289 mp_set_cell(fastn[-unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000290 break;
291
Damiend99b0522013-12-21 18:17:45 +0000292 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100293 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100294 mp_store_name(qst, POP());
Damience89a212013-10-15 22:25:17 +0100295 break;
296
Damiend99b0522013-12-21 18:17:45 +0000297 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000298 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100299 mp_store_global(qst, POP());
Damien6addc892013-11-04 23:04:50 +0000300 break;
301
Damiend99b0522013-12-21 18:17:45 +0000302 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100303 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100304 mp_store_attr(sp[0], qst, sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000305 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100306 break;
307
Damiend99b0522013-12-21 18:17:45 +0000308 case MP_BC_STORE_SUBSCR:
Damien Georged17926d2014-03-30 13:35:08 +0100309 mp_store_subscr(sp[-1], sp[0], sp[-2]);
Damien George20006db2014-01-18 14:10:48 +0000310 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100311 break;
312
Paul Sokolovsky14b82032014-03-30 13:39:43 +0300313 case MP_BC_DELETE_FAST_N:
314 DECODE_UINT;
315 fastn[-unum] = MP_OBJ_NULL;
316 break;
317
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200318 case MP_BC_DELETE_NAME:
319 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100320 mp_delete_name(qst);
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200321 break;
322
Damiend99b0522013-12-21 18:17:45 +0000323 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000324 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100325 PUSH(obj1);
326 break;
327
Damiend99b0522013-12-21 18:17:45 +0000328 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000329 sp += 2;
330 sp[0] = sp[-2];
331 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100332 break;
333
Damiend99b0522013-12-21 18:17:45 +0000334 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000335 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100336 break;
337
Damiend99b0522013-12-21 18:17:45 +0000338 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000339 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000340 sp[0] = sp[-1];
341 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000342 break;
343
Damiend99b0522013-12-21 18:17:45 +0000344 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100345 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000346 sp[0] = sp[-1];
347 sp[-1] = sp[-2];
348 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100349 break;
350
Damiend99b0522013-12-21 18:17:45 +0000351 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000352 DECODE_SLABEL;
353 ip += unum;
Damience89a212013-10-15 22:25:17 +0100354 break;
355
Damiend99b0522013-12-21 18:17:45 +0000356 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000357 DECODE_SLABEL;
Damien Georged17926d2014-03-30 13:35:08 +0100358 if (mp_obj_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000359 ip += unum;
Damience89a212013-10-15 22:25:17 +0100360 }
361 break;
362
Damiend99b0522013-12-21 18:17:45 +0000363 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000364 DECODE_SLABEL;
Damien Georged17926d2014-03-30 13:35:08 +0100365 if (!mp_obj_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000366 ip += unum;
Damience89a212013-10-15 22:25:17 +0100367 }
368 break;
369
Damiend99b0522013-12-21 18:17:45 +0000370 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000371 DECODE_SLABEL;
Damien Georged17926d2014-03-30 13:35:08 +0100372 if (mp_obj_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000373 ip += unum;
374 } else {
Damien George20006db2014-01-18 14:10:48 +0000375 sp--;
Damien94658e22013-11-09 20:12:32 +0000376 }
377 break;
378
Damiend99b0522013-12-21 18:17:45 +0000379 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000380 DECODE_SLABEL;
Damien Georged17926d2014-03-30 13:35:08 +0100381 if (mp_obj_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000382 sp--;
Damien94658e22013-11-09 20:12:32 +0000383 } else {
384 ip += unum;
385 }
386 break;
387
Damience89a212013-10-15 22:25:17 +0100388 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000389 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100390 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000391 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100392 break;
393 */
394
Damien Georgec689c192014-03-29 14:06:14 +0000395 case MP_BC_SETUP_WITH:
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200396 obj1 = TOP();
Damien Georged17926d2014-03-30 13:35:08 +0100397 SET_TOP(mp_load_attr(obj1, MP_QSTR___exit__));
398 mp_load_method(obj1, MP_QSTR___enter__, sp + 1);
399 obj2 = mp_call_method_n_kw(0, 0, sp + 1);
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200400 PUSH_EXC_BLOCK();
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200401 PUSH(obj2);
402 break;
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200403
404 case MP_BC_WITH_CLEANUP: {
Paul Sokolovsky40d6d292014-03-29 16:49:33 +0200405 // Arriving here, there's "exception control block" on top of stack,
406 // and __exit__ bound method underneath it. Bytecode calls __exit__,
407 // and "deletes" it off stack, shifting "exception control block"
408 // to its place.
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200409 static const mp_obj_t no_exc[] = {mp_const_none, mp_const_none, mp_const_none};
410 if (TOP() == mp_const_none) {
411 sp--;
412 obj1 = TOP();
413 SET_TOP(mp_const_none);
Damien Georged17926d2014-03-30 13:35:08 +0100414 obj2 = mp_call_function_n_kw(obj1, 3, 0, no_exc);
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200415 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
416 mp_obj_t cause = POP();
417 switch (MP_OBJ_SMALL_INT_VALUE(cause)) {
418 case UNWIND_RETURN: {
419 mp_obj_t retval = POP();
Damien Georged17926d2014-03-30 13:35:08 +0100420 obj2 = mp_call_function_n_kw(TOP(), 3, 0, no_exc);
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200421 SET_TOP(retval);
422 PUSH(cause);
423 break;
424 }
425 case UNWIND_JUMP: {
Damien Georged17926d2014-03-30 13:35:08 +0100426 obj2 = mp_call_function_n_kw(sp[-2], 3, 0, no_exc);
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200427 // Pop __exit__ boundmethod at sp[-2]
428 sp[-2] = sp[-1];
429 sp[-1] = sp[0];
430 SET_TOP(cause);
431 break;
432 }
433 default:
434 assert(0);
435 }
436 } else if (mp_obj_is_exception_type(TOP())) {
437 mp_obj_t args[3] = {sp[0], sp[-1], sp[-2]};
Damien Georged17926d2014-03-30 13:35:08 +0100438 obj2 = mp_call_function_n_kw(sp[-3], 3, 0, args);
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200439 // Pop __exit__ boundmethod at sp[-3]
440 // TODO: Once semantics is proven, optimize for case when obj2 == True
441 sp[-3] = sp[-2];
442 sp[-2] = sp[-1];
443 sp[-1] = sp[0];
444 sp--;
Damien Georged17926d2014-03-30 13:35:08 +0100445 if (mp_obj_is_true(obj2)) {
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200446 // This is what CPython does
447 //PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_SILENCED));
448 // But what we need to do is - pop exception from value stack...
449 sp -= 3;
Paul Sokolovsky40d6d292014-03-29 16:49:33 +0200450 // ... pop "with" exception handler, and signal END_FINALLY
451 // to just execute finally handler normally (by pushing None
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200452 // on value stack)
453 assert(exc_sp >= exc_stack);
454 assert(exc_sp->opcode == MP_BC_SETUP_WITH);
Paul Sokolovsky69975df2014-03-29 23:21:43 +0200455 POP_EXC_BLOCK();
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200456 PUSH(mp_const_none);
457 }
458 } else {
459 assert(0);
460 }
461 break;
462 }
463
Damien Georgecbddb272014-02-01 20:08:18 +0000464 case MP_BC_UNWIND_JUMP:
465 DECODE_SLABEL;
466 PUSH((void*)(ip + unum)); // push destination ip for jump
467 PUSH((void*)(machine_uint_t)(*ip)); // push number of exception handlers to unwind
468unwind_jump:
469 unum = (machine_uint_t)POP(); // get number of exception handlers to unwind
470 while (unum > 0) {
471 unum -= 1;
472 assert(exc_sp >= exc_stack);
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200473 if (exc_sp->opcode == MP_BC_SETUP_FINALLY || exc_sp->opcode == MP_BC_SETUP_WITH) {
Damien Georgecbddb272014-02-01 20:08:18 +0000474 // We're going to run "finally" code as a coroutine
475 // (not calling it recursively). Set up a sentinel
476 // on a stack so it can return back to us when it is
477 // done (when END_FINALLY reached).
478 PUSH((void*)unum); // push number of exception handlers left to unwind
479 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_JUMP)); // push sentinel
480 ip = exc_sp->handler; // get exception handler byte code address
481 exc_sp--; // pop exception handler
482 goto dispatch_loop; // run the exception handler
483 }
484 exc_sp--;
485 }
486 ip = (const byte*)POP(); // pop destination ip for jump
Damien George600ae732014-01-21 23:48:04 +0000487 break;
488
Damien02a7c412013-12-29 18:48:37 +0000489 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000490 case MP_BC_SETUP_EXCEPT:
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200491 case MP_BC_SETUP_FINALLY:
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200492 PUSH_EXC_BLOCK();
Damience89a212013-10-15 22:25:17 +0100493 break;
494
Damiend99b0522013-12-21 18:17:45 +0000495 case MP_BC_END_FINALLY:
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200496 // not fully implemented
Damience89a212013-10-15 22:25:17 +0100497 // if TOS is an exception, reraises the exception (3 values on TOS)
Damience89a212013-10-15 22:25:17 +0100498 // if TOS is None, just pops it and continues
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200499 // if TOS is an integer, does something else
Damience89a212013-10-15 22:25:17 +0100500 // else error
Paul Sokolovsky682f9e62014-03-29 02:52:17 +0200501 if (mp_obj_is_exception_type(TOP())) {
502 nlr_jump(sp[-1]);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200503 }
504 if (TOP() == mp_const_none) {
505 sp--;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200506 } else if (MP_OBJ_IS_SMALL_INT(TOP())) {
507 // We finished "finally" coroutine and now dispatch back
508 // to our caller, based on TOS value
509 mp_unwind_reason_t reason = MP_OBJ_SMALL_INT_VALUE(POP());
510 switch (reason) {
511 case UNWIND_RETURN:
512 goto unwind_return;
Damien Georgecbddb272014-02-01 20:08:18 +0000513 case UNWIND_JUMP:
514 goto unwind_jump;
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200515 }
516 assert(0);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200517 } else {
518 assert(0);
519 }
Damience89a212013-10-15 22:25:17 +0100520 break;
521
Damiend99b0522013-12-21 18:17:45 +0000522 case MP_BC_GET_ITER:
Damien Georged17926d2014-03-30 13:35:08 +0100523 SET_TOP(mp_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100524 break;
525
Damiend99b0522013-12-21 18:17:45 +0000526 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000527 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damien Georged17926d2014-03-30 13:35:08 +0100528 obj1 = mp_iternext_allow_raise(TOP());
Damien George66eaf842014-03-26 19:27:58 +0000529 if (obj1 == MP_OBJ_NULL) {
Damien George20006db2014-01-18 14:10:48 +0000530 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000531 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100532 } else {
533 PUSH(obj1); // push the next iteration value
534 }
535 break;
536
Damien02a7c412013-12-29 18:48:37 +0000537 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000538 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000539 // we are exiting an exception handler, so pop the last one of the exception-stack
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200540 assert(exc_sp >= exc_stack);
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200541 POP_EXC_BLOCK();
Damience89a212013-10-15 22:25:17 +0100542 break;
543
Damien George600ae732014-01-21 23:48:04 +0000544 // matched against: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000545 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100546 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100547 // pops block, checks it's an exception block, and restores the stack, saving the 3 exception values to local threadstate
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200548 assert(exc_sp >= exc_stack);
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200549 assert(currently_in_except_block);
Damiend99b0522013-12-21 18:17:45 +0000550 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100551 //exc_sp--; // discard ip
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200552 POP_EXC_BLOCK();
Damien George20006db2014-01-18 14:10:48 +0000553 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100554 break;
555
Damien George9aa2a522014-02-01 23:04:09 +0000556 case MP_BC_NOT:
557 if (TOP() == mp_const_true) {
558 SET_TOP(mp_const_false);
559 } else {
560 SET_TOP(mp_const_true);
561 }
562 break;
563
Damiend99b0522013-12-21 18:17:45 +0000564 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000565 unum = *ip++;
Damien Georged17926d2014-03-30 13:35:08 +0100566 SET_TOP(mp_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000567 break;
568
Damiend99b0522013-12-21 18:17:45 +0000569 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100570 unum = *ip++;
571 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000572 obj1 = TOP();
Damien Georged17926d2014-03-30 13:35:08 +0100573 SET_TOP(mp_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100574 break;
575
Damiend99b0522013-12-21 18:17:45 +0000576 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100577 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000578 sp -= unum - 1;
Damien Georged17926d2014-03-30 13:35:08 +0100579 SET_TOP(mp_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100580 break;
581
Damiend99b0522013-12-21 18:17:45 +0000582 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100583 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000584 sp -= unum - 1;
Damien Georged17926d2014-03-30 13:35:08 +0100585 SET_TOP(mp_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100586 break;
587
Damiend99b0522013-12-21 18:17:45 +0000588 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100589 DECODE_UINT;
590 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien Georged17926d2014-03-30 13:35:08 +0100591 mp_list_append(sp[-unum], sp[0]);
Damien George20006db2014-01-18 14:10:48 +0000592 sp--;
Damienc226dca2013-10-16 16:12:52 +0100593 break;
594
Damiend99b0522013-12-21 18:17:45 +0000595 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100596 DECODE_UINT;
Damien Georged17926d2014-03-30 13:35:08 +0100597 PUSH(mp_build_map(unum));
Damience89a212013-10-15 22:25:17 +0100598 break;
599
Damiend99b0522013-12-21 18:17:45 +0000600 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000601 sp -= 2;
Damien Georged17926d2014-03-30 13:35:08 +0100602 mp_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100603 break;
604
Damiend99b0522013-12-21 18:17:45 +0000605 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100606 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000607 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
Damien Georged17926d2014-03-30 13:35:08 +0100608 mp_store_map(sp[-unum - 1], sp[0], sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000609 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100610 break;
611
Damiend99b0522013-12-21 18:17:45 +0000612 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100613 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000614 sp -= unum - 1;
Damien Georged17926d2014-03-30 13:35:08 +0100615 SET_TOP(mp_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100616 break;
617
Damiend99b0522013-12-21 18:17:45 +0000618 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100619 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000620 // I think it's guaranteed by the compiler that sp[-unum] is a set
Damien Georged17926d2014-03-30 13:35:08 +0100621 mp_store_set(sp[-unum], sp[0]);
Damien George20006db2014-01-18 14:10:48 +0000622 sp--;
Damienc12aa462013-10-16 20:57:49 +0100623 break;
624
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200625#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200626 case MP_BC_BUILD_SLICE:
627 DECODE_UINT;
628 if (unum == 2) {
629 obj2 = POP();
630 obj1 = TOP();
631 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
632 } else {
633 printf("3-argument slice is not supported\n");
634 assert(0);
635 }
636 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200637#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200638
Damiend99b0522013-12-21 18:17:45 +0000639 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000640 DECODE_UINT;
Damien Georged17926d2014-03-30 13:35:08 +0100641 mp_unpack_sequence(sp[0], unum, sp);
Damien George20006db2014-01-18 14:10:48 +0000642 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000643 break;
644
Damiend99b0522013-12-21 18:17:45 +0000645 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100646 DECODE_UINT;
Damien Georged17926d2014-03-30 13:35:08 +0100647 PUSH(mp_make_function_from_id(unum, false, MP_OBJ_NULL));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200648 break;
649
650 case MP_BC_MAKE_FUNCTION_DEFARGS:
651 DECODE_UINT;
Damien Georged17926d2014-03-30 13:35:08 +0100652 SET_TOP(mp_make_function_from_id(unum, false, TOP()));
Damience89a212013-10-15 22:25:17 +0100653 break;
654
Damiend99b0522013-12-21 18:17:45 +0000655 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000656 DECODE_UINT;
Damien Georged17926d2014-03-30 13:35:08 +0100657 SET_TOP(mp_make_closure_from_id(unum, TOP(), MP_OBJ_NULL));
Paul Sokolovsky2447a5b2014-03-26 23:14:59 +0200658 break;
659
660 case MP_BC_MAKE_CLOSURE_DEFARGS:
661 DECODE_UINT;
662 obj1 = POP();
Damien Georged17926d2014-03-30 13:35:08 +0100663 SET_TOP(mp_make_closure_from_id(unum, obj1, TOP()));
Damien9ecbcff2013-12-11 00:41:43 +0000664 break;
665
Damiend99b0522013-12-21 18:17:45 +0000666 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100667 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000668 // unum & 0xff == n_positional
669 // (unum >> 8) & 0xff == n_keyword
670 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
Damien Georged17926d2014-03-30 13:35:08 +0100671 SET_TOP(mp_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100672 break;
673
Damien George230fec72014-03-30 21:21:24 +0100674 case MP_BC_CALL_FUNCTION_VAR:
Paul Sokolovsky55ca0752014-03-30 17:35:53 +0300675 DECODE_UINT;
676 // unum & 0xff == n_positional
677 // (unum >> 8) & 0xff == n_keyword
678 // We have folowing stack layout here:
Damien George230fec72014-03-30 21:21:24 +0100679 // fun arg0 arg1 ... kw0 val0 kw1 val1 ... seq <- TOS
680 obj1 = POP();
681 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
682 SET_TOP(mp_call_method_n_kw_var(false, unum, sp, obj1, MP_OBJ_NULL));
Paul Sokolovsky55ca0752014-03-30 17:35:53 +0300683 break;
Damien George230fec72014-03-30 21:21:24 +0100684
685 case MP_BC_CALL_FUNCTION_KW:
686 DECODE_UINT;
687 // unum & 0xff == n_positional
688 // (unum >> 8) & 0xff == n_keyword
689 // We have folowing stack layout here:
690 // fun arg0 arg1 ... kw0 val0 kw1 val1 ... dict <- TOS
691 obj1 = POP();
692 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
693 SET_TOP(mp_call_method_n_kw_var(false, unum, sp, MP_OBJ_NULL, obj1));
694 break;
695
696 case MP_BC_CALL_FUNCTION_VAR_KW:
697 DECODE_UINT;
698 // unum & 0xff == n_positional
699 // (unum >> 8) & 0xff == n_keyword
700 // We have folowing stack layout here:
701 // fun arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS
702 obj2 = POP();
703 obj1 = POP();
704 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
705 SET_TOP(mp_call_method_n_kw_var(false, unum, sp, obj1, obj2));
706 break;
Paul Sokolovsky55ca0752014-03-30 17:35:53 +0300707
Damiend99b0522013-12-21 18:17:45 +0000708 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100709 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000710 // unum & 0xff == n_positional
711 // (unum >> 8) & 0xff == n_keyword
712 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
Damien Georged17926d2014-03-30 13:35:08 +0100713 SET_TOP(mp_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100714 break;
715
Damien George230fec72014-03-30 21:21:24 +0100716 case MP_BC_CALL_METHOD_VAR:
717 DECODE_UINT;
718 // unum & 0xff == n_positional
719 // (unum >> 8) & 0xff == n_keyword
720 // We have folowing stack layout here:
721 // fun self arg0 arg1 ... kw0 val0 kw1 val1 ... seq <- TOS
722 obj1 = POP();
723 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
724 SET_TOP(mp_call_method_n_kw_var(true, unum, sp, obj1, MP_OBJ_NULL));
725 break;
726
727 case MP_BC_CALL_METHOD_KW:
728 DECODE_UINT;
729 // unum & 0xff == n_positional
730 // (unum >> 8) & 0xff == n_keyword
731 // We have folowing stack layout here:
732 // fun self arg0 arg1 ... kw0 val0 kw1 val1 ... dict <- TOS
733 obj1 = POP();
734 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
735 SET_TOP(mp_call_method_n_kw_var(true, unum, sp, MP_OBJ_NULL, obj1));
736 break;
737
738 case MP_BC_CALL_METHOD_VAR_KW:
739 DECODE_UINT;
740 // unum & 0xff == n_positional
741 // (unum >> 8) & 0xff == n_keyword
742 // We have folowing stack layout here:
743 // fun self arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS
744 obj2 = POP();
745 obj1 = POP();
746 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
747 SET_TOP(mp_call_method_n_kw_var(true, unum, sp, obj1, obj2));
748 break;
749
Damiend99b0522013-12-21 18:17:45 +0000750 case MP_BC_RETURN_VALUE:
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200751unwind_return:
752 while (exc_sp >= exc_stack) {
Paul Sokolovsky44307d52014-03-29 04:10:11 +0200753 if (exc_sp->opcode == MP_BC_SETUP_FINALLY || exc_sp->opcode == MP_BC_SETUP_WITH) {
Paul Sokolovsky6472dea2014-02-01 00:55:05 +0200754 // We're going to run "finally" code as a coroutine
755 // (not calling it recursively). Set up a sentinel
756 // on a stack so it can return back to us when it is
757 // done (when END_FINALLY reached).
758 PUSH(MP_OBJ_NEW_SMALL_INT(UNWIND_RETURN));
759 ip = exc_sp->handler;
760 // We don't need to do anything with sp, finally is just
761 // syntactic sugar for sequential execution??
762 // sp =
763 exc_sp--;
764 goto dispatch_loop;
765 }
766 exc_sp--;
767 }
Damience89a212013-10-15 22:25:17 +0100768 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100769 *sp_in_out = sp;
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200770 assert(exc_sp == exc_stack - 1);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000771 return MP_VM_RETURN_NORMAL;
Damienbd254452013-10-16 20:39:12 +0100772
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200773 case MP_BC_RAISE_VARARGS:
774 unum = *ip++;
Paul Sokolovskyc4030762014-03-26 14:42:17 +0200775 assert(unum <= 1);
776 if (unum == 0) {
Damien Georged7592a12014-03-30 00:54:48 +0000777 // search for the inner-most previous exception, to reraise it
778 obj1 = MP_OBJ_NULL;
Damien George89f94b52014-03-30 00:57:09 +0000779 for (mp_exc_stack_t *e = exc_sp; e >= exc_stack; e--) {
Damien Georged7592a12014-03-30 00:54:48 +0000780 if (e->prev_exc != MP_OBJ_NULL) {
781 obj1 = e->prev_exc;
782 break;
783 }
784 }
785 if (obj1 == MP_OBJ_NULL) {
Paul Sokolovskyd1096762014-03-29 19:44:15 +0200786 nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "No active exception to reraise"));
787 }
Paul Sokolovskyc4030762014-03-26 14:42:17 +0200788 } else {
789 obj1 = POP();
790 }
Damien Georged17926d2014-03-30 13:35:08 +0100791 nlr_jump(mp_make_raise_obj(obj1));
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200792
Damiend99b0522013-12-21 18:17:45 +0000793 case MP_BC_YIELD_VALUE:
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200794yield:
Damienbd254452013-10-16 20:39:12 +0100795 nlr_pop();
796 *ip_in_out = ip;
Damienbd254452013-10-16 20:39:12 +0100797 *sp_in_out = sp;
Paul Sokolovsky16734202014-03-22 23:20:07 +0200798 *exc_sp_in_out = MP_TAGPTR_MAKE(exc_sp, currently_in_except_block);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000799 return MP_VM_RETURN_YIELD;
Damience89a212013-10-15 22:25:17 +0100800
Paul Sokolovsky40d6d292014-03-29 16:49:33 +0200801 case MP_BC_YIELD_FROM: {
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200802//#define EXC_MATCH(exc, type) MP_OBJ_IS_TYPE(exc, type)
803#define EXC_MATCH(exc, type) mp_obj_exception_match(exc, type)
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200804#define GENERATOR_EXIT_IF_NEEDED(t) if (t != MP_OBJ_NULL && EXC_MATCH(t, &mp_type_GeneratorExit)) { nlr_jump(t); }
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200805 mp_vm_return_kind_t ret_kind;
806 obj1 = POP();
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200807 mp_obj_t t_exc = MP_OBJ_NULL;
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200808 if (inject_exc != MP_OBJ_NULL) {
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200809 t_exc = inject_exc;
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200810 inject_exc = MP_OBJ_NULL;
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200811 ret_kind = mp_obj_gen_resume(TOP(), mp_const_none, t_exc, &obj2);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200812 } else {
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200813 ret_kind = mp_obj_gen_resume(TOP(), obj1, MP_OBJ_NULL, &obj2);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200814 }
815
816 if (ret_kind == MP_VM_RETURN_YIELD) {
817 ip--;
818 PUSH(obj2);
819 goto yield;
820 }
821 if (ret_kind == MP_VM_RETURN_NORMAL) {
822 // Pop exhausted gen
823 sp--;
824 if (obj2 == MP_OBJ_NULL) {
825 // Optimize StopIteration
826 // TODO: get StopIteration's value
827 PUSH(mp_const_none);
828 } else {
829 PUSH(obj2);
830 }
831
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200832 // If we injected GeneratorExit downstream, then even
833 // if it was swallowed, we re-raise GeneratorExit
834 GENERATOR_EXIT_IF_NEEDED(t_exc);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200835 break;
836 }
837 if (ret_kind == MP_VM_RETURN_EXCEPTION) {
838 // Pop exhausted gen
839 sp--;
840 if (EXC_MATCH(obj2, &mp_type_StopIteration)) {
Paul Sokolovsky55234f42014-03-26 19:24:03 +0200841 PUSH(mp_obj_exception_get_value(obj2));
842 // If we injected GeneratorExit downstream, then even
843 // if it was swallowed, we re-raise GeneratorExit
844 GENERATOR_EXIT_IF_NEEDED(t_exc);
Paul Sokolovskycf21a4e2014-03-26 17:36:12 +0200845 break;
846 } else {
847 nlr_jump(obj2);
848 }
849 }
850 }
851
Damiend99b0522013-12-21 18:17:45 +0000852 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000853 DECODE_QSTR;
854 obj1 = POP();
Damien Georged17926d2014-03-30 13:35:08 +0100855 SET_TOP(mp_import_name(qst, obj1, TOP()));
Damiendb4c3612013-12-10 17:27:24 +0000856 break;
857
Damiend99b0522013-12-21 18:17:45 +0000858 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000859 DECODE_QSTR;
Damien Georged17926d2014-03-30 13:35:08 +0100860 obj1 = mp_import_from(TOP(), qst);
Damiendb4c3612013-12-10 17:27:24 +0000861 PUSH(obj1);
862 break;
863
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200864 case MP_BC_IMPORT_STAR:
Damien Georged17926d2014-03-30 13:35:08 +0100865 mp_import_all(POP());
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200866 break;
867
Damience89a212013-10-15 22:25:17 +0100868 default:
Damien03c9cfb2013-11-05 22:06:08 +0000869 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100870 assert(0);
871 nlr_pop();
Damien Georgec8f78bc2014-02-15 22:55:00 +0000872 return MP_VM_RETURN_NORMAL;
Damien429d7192013-10-04 19:53:11 +0100873 }
Damience89a212013-10-15 22:25:17 +0100874 }
Damien429d7192013-10-04 19:53:11 +0100875
Damience89a212013-10-15 22:25:17 +0100876 } else {
877 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100878
Damien George9e6e9352014-03-26 18:37:06 +0000879 // check if it's a StopIteration within a for block
880 if (*save_ip == MP_BC_FOR_ITER && mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
881 ip = save_ip + 1;
882 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
883 --sp; // pop the exhausted iterator
884 ip += unum; // jump to after for-block
885 goto outer_dispatch_loop; // continue with dispatch loop
886 }
887
Damien George08335002014-01-18 23:24:36 +0000888 // set file and line number that the exception occurred at
Paul Sokolovsky382e8ee2014-01-30 13:49:18 +0200889 // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
890 // But consider how to handle nested exceptions.
Damien Georgeb04be052014-03-29 13:52:51 +0000891 // TODO need a better way of not adding traceback to constant objects (right now, just GeneratorExit_obj)
892 if (mp_obj_is_exception_instance(nlr.ret_val) && nlr.ret_val != &mp_const_GeneratorExit_obj) {
Damien George08335002014-01-18 23:24:36 +0000893 machine_uint_t code_info_size = code_info[0] | (code_info[1] << 8) | (code_info[2] << 16) | (code_info[3] << 24);
Damien Georgecbd2f742014-01-19 11:48:48 +0000894 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
895 qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
Damien George08335002014-01-18 23:24:36 +0000896 machine_uint_t source_line = 1;
897 machine_uint_t bc = save_ip - code_info - code_info_size;
Damien Georgecbd2f742014-01-19 11:48:48 +0000898 //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
Damien George28eb5772014-01-25 11:43:20 +0000899 for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
900 bc -= *ci & 31;
901 source_line += *ci >> 5;
Damien George08335002014-01-18 23:24:36 +0000902 }
Damien George136b1492014-01-19 12:38:49 +0000903 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +0000904 }
905
Damien8f9e2ee2013-12-29 16:54:59 +0000906 while (currently_in_except_block) {
907 // nested exception
908
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200909 assert(exc_sp >= exc_stack);
Damien8f9e2ee2013-12-29 16:54:59 +0000910
911 // TODO make a proper message for nested exception
912 // at the moment we are just raising the very last exception (the one that caused the nested exception)
913
914 // move up to previous exception handler
Paul Sokolovskya0ad77b2014-03-29 23:16:27 +0200915 POP_EXC_BLOCK();
Damien8f9e2ee2013-12-29 16:54:59 +0000916 }
917
Paul Sokolovskyc0abc282014-03-22 13:49:31 +0200918 if (exc_sp >= exc_stack) {
Damien8f9e2ee2013-12-29 16:54:59 +0000919 // set flag to indicate that we are now handling an exception
920 currently_in_except_block = 1;
921
Damience89a212013-10-15 22:25:17 +0100922 // catch exception and pass to byte code
Paul Sokolovsky16734202014-03-22 23:20:07 +0200923 sp = MP_TAGPTR_PTR(exc_sp->val_sp);
Paul Sokolovskyc7a0b142014-01-31 19:40:09 +0200924 ip = exc_sp->handler;
Damien Georged7592a12014-03-30 00:54:48 +0000925 // save this exception in the stack so it can be used in a reraise, if needed
926 exc_sp->prev_exc = nlr.ret_val;
Damienc9f91972013-10-15 23:46:01 +0100927 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000928 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100929 PUSH(nlr.ret_val);
Paul Sokolovsky682f9e62014-03-29 02:52:17 +0200930 PUSH(mp_obj_get_type(nlr.ret_val));
Damien8f9e2ee2013-12-29 16:54:59 +0000931
Damience89a212013-10-15 22:25:17 +0100932 } else {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000933 // propagate exception to higher level
934 // TODO what to do about ip and sp? they don't really make sense at this point
935 fastn[0] = nlr.ret_val; // must put exception here because sp is invalid
936 return MP_VM_RETURN_EXCEPTION;
Damience89a212013-10-15 22:25:17 +0100937 }
Damien429d7192013-10-04 19:53:11 +0100938 }
939 }
940}