blob: 0cc26021e8cf58e1b74c56f4d7521aec58648997 [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"
Damien George55baff42014-01-21 21:40:13 +000010#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "obj.h"
Damien429d7192013-10-04 19:53:11 +010012#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "bc0.h"
Damieneb19efb2013-10-10 22:06:54 +010014#include "bc.h"
Damien429d7192013-10-04 19:53:11 +010015
Damienbd254452013-10-16 20:39:12 +010016// (value) stack grows down (to be compatible with native code when passing pointers to the stack), top element is pointed to
17// exception stack grows up, top element is pointed to
18
Damien429d7192013-10-04 19:53:11 +010019#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
Damien03c9cfb2013-11-05 22:06:08 +000020#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
21#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Damien Georgecbd2f742014-01-19 11:48:48 +000022#define DECODE_QSTR do { qst = *ip++; if (qst > 127) { qst = ((qst & 0x3f) << 8) | (*ip++); } } while (0)
Damien George20006db2014-01-18 14:10:48 +000023#define PUSH(val) *++sp = (val)
24#define POP() (*sp--)
Damiendb4c3612013-12-10 17:27:24 +000025#define TOP() (*sp)
26#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010027
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) {
Damien Georgee02b2d42014-01-19 01:14:37 +000029 n_state += 1; // XXX there is a bug somewhere which doesn't count enough state... (conwaylife and mandel have the bug)
30
Damien George20006db2014-01-18 14:10:48 +000031 // allocate state for locals and stack
32 mp_obj_t temp_state[10];
Damiend99b0522013-12-21 18:17:45 +000033 mp_obj_t *state = &temp_state[0];
Damien40fdfe32013-11-05 22:16:22 +000034 if (n_state > 10) {
Damiend99b0522013-12-21 18:17:45 +000035 state = m_new(mp_obj_t, n_state);
Damien40fdfe32013-11-05 22:16:22 +000036 }
Damien George20006db2014-01-18 14:10:48 +000037 mp_obj_t *sp = &state[0] - 1;
38
Damienbd254452013-10-16 20:39:12 +010039 // init args
40 for (int i = 0; i < n_args; i++) {
41 assert(i < 8);
Damien George20006db2014-01-18 14:10:48 +000042 state[n_state - 1 - i] = args[i];
Damienbd254452013-10-16 20:39:12 +010043 }
Damien George08335002014-01-18 23:24:36 +000044
Damienbd254452013-10-16 20:39:12 +010045 const byte *ip = code;
Damien George6baf76e2013-12-30 22:32:17 +000046
Damien George08335002014-01-18 23:24:36 +000047 // get code info size
48 machine_uint_t code_info_size = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
49 ip += code_info_size;
50
Damien George6baf76e2013-12-30 22:32:17 +000051 // execute prelude to make any cells (closed over variables)
52 {
53 for (uint n_local = *ip++; n_local > 0; n_local--) {
54 uint local_num = *ip++;
55 if (local_num < n_args) {
Damien George20006db2014-01-18 14:10:48 +000056 state[n_state - 1 - local_num] = mp_obj_new_cell(state[n_state - 1 - local_num]);
Damien George6baf76e2013-12-30 22:32:17 +000057 } else {
Damien George20006db2014-01-18 14:10:48 +000058 state[n_state - 1 - local_num] = mp_obj_new_cell(MP_OBJ_NULL);
Damien George6baf76e2013-12-30 22:32:17 +000059 }
60 }
61 }
62
63 // execute the byte code
Damien George08335002014-01-18 23:24:36 +000064 if (mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp)) {
Damienbd254452013-10-16 20:39:12 +010065 // it shouldn't yield
66 assert(0);
67 }
Damien George6baf76e2013-12-30 22:32:17 +000068
Damien4ebb32f2013-11-02 14:33:10 +000069 // TODO check fails if, eg, return from within for loop
70 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010071 return *sp;
72}
73
Damien George20006db2014-01-18 14:10:48 +000074// fastn has items in reverse order (fastn[0] is local[0], fastn[-1] is local[1], etc)
75// sp points to bottom of stack which grows up
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +020076// returns true if bytecode yielded
Damien George08335002014-01-18 23:24:36 +000077bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t **sp_in_out) {
Damienc9f91972013-10-15 23:46:01 +010078 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
79
Damienbd254452013-10-16 20:39:12 +010080 const byte *ip = *ip_in_out;
Damiend99b0522013-12-21 18:17:45 +000081 mp_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +010082 machine_uint_t unum;
Damien Georgecbd2f742014-01-19 11:48:48 +000083 qstr qst;
Damiend99b0522013-12-21 18:17:45 +000084 mp_obj_t obj1, obj2;
Damien George20006db2014-01-18 14:10:48 +000085 mp_obj_t fast0 = fastn[0], fast1 = fastn[-1], fast2 = fastn[-2];
Damience89a212013-10-15 22:25:17 +010086 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +010087
Damien8f9e2ee2013-12-29 16:54:59 +000088 volatile machine_uint_t currently_in_except_block = 0; // 0 or 1, to detect nested exceptions
89 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 +000090 machine_uint_t *volatile exc_sp = &exc_stack[0] - 1; // stack grows up, exc_sp points to top of stack
Damien George08335002014-01-18 23:24:36 +000091 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 +010092
Damien Georgee02b2d42014-01-19 01:14:37 +000093 // TODO if an exception occurs, do fast[0,1,2] become invalid??
94
Damience89a212013-10-15 22:25:17 +010095 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +010096 for (;;) {
Damience89a212013-10-15 22:25:17 +010097 if (nlr_push(&nlr) == 0) {
98 // loop to execute byte code
99 for (;;) {
Damien George08335002014-01-18 23:24:36 +0000100 save_ip = ip;
Damience89a212013-10-15 22:25:17 +0100101 int op = *ip++;
102 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000103 case MP_BC_LOAD_CONST_FALSE:
104 PUSH(mp_const_false);
Damience89a212013-10-15 22:25:17 +0100105 break;
Damien429d7192013-10-04 19:53:11 +0100106
Damiend99b0522013-12-21 18:17:45 +0000107 case MP_BC_LOAD_CONST_NONE:
108 PUSH(mp_const_none);
Damience89a212013-10-15 22:25:17 +0100109 break;
Damien429d7192013-10-04 19:53:11 +0100110
Damiend99b0522013-12-21 18:17:45 +0000111 case MP_BC_LOAD_CONST_TRUE:
112 PUSH(mp_const_true);
Damience89a212013-10-15 22:25:17 +0100113 break;
Damien429d7192013-10-04 19:53:11 +0100114
Damien Georgee9906ac2014-01-04 18:44:46 +0000115 case MP_BC_LOAD_CONST_ELLIPSIS:
116 PUSH(mp_const_ellipsis);
117 break;
118
Damiend99b0522013-12-21 18:17:45 +0000119 case MP_BC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +0000120 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +0000121 ip += 3;
Paul Sokolovskye5ee1692014-01-06 17:49:46 +0200122 PUSH(MP_OBJ_NEW_SMALL_INT(unum));
Damience89a212013-10-15 22:25:17 +0100123 break;
124
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200125 case MP_BC_LOAD_CONST_INT:
126 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000127 PUSH(mp_obj_new_int_from_long_str(qstr_str(qst)));
Paul Sokolovskya9f5abd2014-01-17 19:51:46 +0200128 break;
129
Damiend99b0522013-12-21 18:17:45 +0000130 case MP_BC_LOAD_CONST_DEC:
Damien7410e442013-11-02 19:47:57 +0000131 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000132 PUSH(rt_load_const_dec(qst));
Damien7410e442013-11-02 19:47:57 +0000133 break;
134
Damiend99b0522013-12-21 18:17:45 +0000135 case MP_BC_LOAD_CONST_ID:
Damience89a212013-10-15 22:25:17 +0100136 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000137 PUSH(rt_load_const_str(qst)); // TODO
Damience89a212013-10-15 22:25:17 +0100138 break;
139
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200140 case MP_BC_LOAD_CONST_BYTES:
141 DECODE_QSTR;
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200142 PUSH(rt_load_const_bytes(qst));
Paul Sokolovskybdf822b2014-01-02 18:46:27 +0200143 break;
144
Damiend99b0522013-12-21 18:17:45 +0000145 case MP_BC_LOAD_CONST_STRING:
Damience89a212013-10-15 22:25:17 +0100146 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000147 PUSH(rt_load_const_str(qst));
Damience89a212013-10-15 22:25:17 +0100148 break;
149
Damiend99b0522013-12-21 18:17:45 +0000150 case MP_BC_LOAD_FAST_0:
Damience89a212013-10-15 22:25:17 +0100151 PUSH(fast0);
152 break;
153
Damiend99b0522013-12-21 18:17:45 +0000154 case MP_BC_LOAD_FAST_1:
Damience89a212013-10-15 22:25:17 +0100155 PUSH(fast1);
156 break;
157
Damiend99b0522013-12-21 18:17:45 +0000158 case MP_BC_LOAD_FAST_2:
Damience89a212013-10-15 22:25:17 +0100159 PUSH(fast2);
160 break;
161
Damiend99b0522013-12-21 18:17:45 +0000162 case MP_BC_LOAD_FAST_N:
Damience89a212013-10-15 22:25:17 +0100163 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000164 PUSH(fastn[-unum]);
Damience89a212013-10-15 22:25:17 +0100165 break;
166
Damiend99b0522013-12-21 18:17:45 +0000167 case MP_BC_LOAD_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000168 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000169 if (unum == 0) {
170 obj1 = fast0;
171 } else if (unum == 1) {
172 obj1 = fast1;
173 } else if (unum == 2) {
174 obj1 = fast2;
175 } else {
176 obj1 = fastn[-unum];
177 }
178 PUSH(rt_get_cell(obj1));
Damien9ecbcff2013-12-11 00:41:43 +0000179 break;
180
Damiend99b0522013-12-21 18:17:45 +0000181 case MP_BC_LOAD_NAME:
Damience89a212013-10-15 22:25:17 +0100182 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000183 PUSH(rt_load_name(qst));
Damience89a212013-10-15 22:25:17 +0100184 break;
185
Damiend99b0522013-12-21 18:17:45 +0000186 case MP_BC_LOAD_GLOBAL:
Damience89a212013-10-15 22:25:17 +0100187 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000188 PUSH(rt_load_global(qst));
Damience89a212013-10-15 22:25:17 +0100189 break;
190
Damiend99b0522013-12-21 18:17:45 +0000191 case MP_BC_LOAD_ATTR:
Damience89a212013-10-15 22:25:17 +0100192 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000193 SET_TOP(rt_load_attr(TOP(), qst));
Damience89a212013-10-15 22:25:17 +0100194 break;
195
Damiend99b0522013-12-21 18:17:45 +0000196 case MP_BC_LOAD_METHOD:
Damience89a212013-10-15 22:25:17 +0100197 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000198 rt_load_method(*sp, qst, sp);
Damien George20006db2014-01-18 14:10:48 +0000199 sp += 1;
Damience89a212013-10-15 22:25:17 +0100200 break;
201
Damiend99b0522013-12-21 18:17:45 +0000202 case MP_BC_LOAD_BUILD_CLASS:
Damience89a212013-10-15 22:25:17 +0100203 PUSH(rt_load_build_class());
204 break;
205
Damiend99b0522013-12-21 18:17:45 +0000206 case MP_BC_STORE_FAST_0:
Damience89a212013-10-15 22:25:17 +0100207 fast0 = POP();
208 break;
209
Damiend99b0522013-12-21 18:17:45 +0000210 case MP_BC_STORE_FAST_1:
Damience89a212013-10-15 22:25:17 +0100211 fast1 = POP();
212 break;
213
Damiend99b0522013-12-21 18:17:45 +0000214 case MP_BC_STORE_FAST_2:
Damience89a212013-10-15 22:25:17 +0100215 fast2 = POP();
216 break;
217
Damiend99b0522013-12-21 18:17:45 +0000218 case MP_BC_STORE_FAST_N:
Damience89a212013-10-15 22:25:17 +0100219 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000220 fastn[-unum] = POP();
Damience89a212013-10-15 22:25:17 +0100221 break;
222
Damiend99b0522013-12-21 18:17:45 +0000223 case MP_BC_STORE_DEREF:
Damien9ecbcff2013-12-11 00:41:43 +0000224 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000225 if (unum == 0) {
226 obj1 = fast0;
227 } else if (unum == 1) {
228 obj1 = fast1;
229 } else if (unum == 2) {
230 obj1 = fast2;
231 } else {
232 obj1 = fastn[-unum];
233 }
234 rt_set_cell(obj1, POP());
Damien9ecbcff2013-12-11 00:41:43 +0000235 break;
236
Damiend99b0522013-12-21 18:17:45 +0000237 case MP_BC_STORE_NAME:
Damience89a212013-10-15 22:25:17 +0100238 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000239 rt_store_name(qst, POP());
Damience89a212013-10-15 22:25:17 +0100240 break;
241
Damiend99b0522013-12-21 18:17:45 +0000242 case MP_BC_STORE_GLOBAL:
Damien6addc892013-11-04 23:04:50 +0000243 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000244 rt_store_global(qst, POP());
Damien6addc892013-11-04 23:04:50 +0000245 break;
246
Damiend99b0522013-12-21 18:17:45 +0000247 case MP_BC_STORE_ATTR:
Damience89a212013-10-15 22:25:17 +0100248 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000249 rt_store_attr(sp[0], qst, sp[-1]);
Damien George20006db2014-01-18 14:10:48 +0000250 sp -= 2;
Damience89a212013-10-15 22:25:17 +0100251 break;
252
Damiend99b0522013-12-21 18:17:45 +0000253 case MP_BC_STORE_SUBSCR:
Damien George20006db2014-01-18 14:10:48 +0000254 rt_store_subscr(sp[-1], sp[0], sp[-2]);
255 sp -= 3;
Damience89a212013-10-15 22:25:17 +0100256 break;
257
Damiend99b0522013-12-21 18:17:45 +0000258 case MP_BC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000259 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100260 PUSH(obj1);
261 break;
262
Damiend99b0522013-12-21 18:17:45 +0000263 case MP_BC_DUP_TOP_TWO:
Damien George20006db2014-01-18 14:10:48 +0000264 sp += 2;
265 sp[0] = sp[-2];
266 sp[-1] = sp[-3];
Damience89a212013-10-15 22:25:17 +0100267 break;
268
Damiend99b0522013-12-21 18:17:45 +0000269 case MP_BC_POP_TOP:
Damien George20006db2014-01-18 14:10:48 +0000270 sp -= 1;
Damience89a212013-10-15 22:25:17 +0100271 break;
272
Damiend99b0522013-12-21 18:17:45 +0000273 case MP_BC_ROT_TWO:
Damien4ebb32f2013-11-02 14:33:10 +0000274 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000275 sp[0] = sp[-1];
276 sp[-1] = obj1;
Damien4ebb32f2013-11-02 14:33:10 +0000277 break;
278
Damiend99b0522013-12-21 18:17:45 +0000279 case MP_BC_ROT_THREE:
Damience89a212013-10-15 22:25:17 +0100280 obj1 = sp[0];
Damien George20006db2014-01-18 14:10:48 +0000281 sp[0] = sp[-1];
282 sp[-1] = sp[-2];
283 sp[-2] = obj1;
Damience89a212013-10-15 22:25:17 +0100284 break;
285
Damiend99b0522013-12-21 18:17:45 +0000286 case MP_BC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000287 DECODE_SLABEL;
288 ip += unum;
Damience89a212013-10-15 22:25:17 +0100289 break;
290
Damiend99b0522013-12-21 18:17:45 +0000291 case MP_BC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000292 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100293 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000294 ip += unum;
Damience89a212013-10-15 22:25:17 +0100295 }
296 break;
297
Damiend99b0522013-12-21 18:17:45 +0000298 case MP_BC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000299 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100300 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000301 ip += unum;
Damience89a212013-10-15 22:25:17 +0100302 }
303 break;
304
Damiend99b0522013-12-21 18:17:45 +0000305 case MP_BC_JUMP_IF_TRUE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000306 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000307 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000308 ip += unum;
309 } else {
Damien George20006db2014-01-18 14:10:48 +0000310 sp--;
Damien94658e22013-11-09 20:12:32 +0000311 }
312 break;
313
Damiend99b0522013-12-21 18:17:45 +0000314 case MP_BC_JUMP_IF_FALSE_OR_POP:
Damien94658e22013-11-09 20:12:32 +0000315 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000316 if (rt_is_true(TOP())) {
Damien George20006db2014-01-18 14:10:48 +0000317 sp--;
Damien94658e22013-11-09 20:12:32 +0000318 } else {
319 ip += unum;
320 }
321 break;
322
Damience89a212013-10-15 22:25:17 +0100323 /* we are trying to get away without using this opcode
Damiend99b0522013-12-21 18:17:45 +0000324 case MP_BC_SETUP_LOOP:
Damience89a212013-10-15 22:25:17 +0100325 DECODE_UINT;
Damiend99b0522013-12-21 18:17:45 +0000326 // push_block(MP_BC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100327 break;
328 */
329
Damien George600ae732014-01-21 23:48:04 +0000330 // TODO this might need more sophisticated handling when breaking from within an except
331 case MP_BC_BREAK_LOOP:
332 DECODE_ULABEL;
333 ip += unum;
334 break;
335
336 // TODO this might need more sophisticated handling when breaking from within an except
337 case MP_BC_CONTINUE_LOOP:
338 DECODE_ULABEL;
339 ip += unum;
340 break;
341
Damien02a7c412013-12-29 18:48:37 +0000342 // matched against: POP_BLOCK or POP_EXCEPT (anything else?)
Damiend99b0522013-12-21 18:17:45 +0000343 case MP_BC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000344 DECODE_ULABEL; // except labels are always forward
345 *++exc_sp = (machine_uint_t)ip + unum;
Damien8f9e2ee2013-12-29 16:54:59 +0000346 *++exc_sp = (((machine_uint_t)sp) | currently_in_except_block);
347 currently_in_except_block = 0; // in a try block now
Damience89a212013-10-15 22:25:17 +0100348 break;
349
Damiend99b0522013-12-21 18:17:45 +0000350 case MP_BC_END_FINALLY:
Damience89a212013-10-15 22:25:17 +0100351 // not implemented
352 // if TOS is an exception, reraises the exception (3 values on TOS)
353 // if TOS is an integer, does something else
354 // if TOS is None, just pops it and continues
355 // else error
356 assert(0);
357 break;
358
Damiend99b0522013-12-21 18:17:45 +0000359 case MP_BC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000360 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100361 break;
362
Damiend99b0522013-12-21 18:17:45 +0000363 case MP_BC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000364 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000365 obj1 = rt_iternext(TOP());
Damiend99b0522013-12-21 18:17:45 +0000366 if (obj1 == mp_const_stop_iteration) {
Damien George20006db2014-01-18 14:10:48 +0000367 --sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000368 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100369 } else {
370 PUSH(obj1); // push the next iteration value
371 }
372 break;
373
Damien02a7c412013-12-29 18:48:37 +0000374 // matched against: SETUP_EXCEPT, SETUP_FINALLY, SETUP_WITH
Damiend99b0522013-12-21 18:17:45 +0000375 case MP_BC_POP_BLOCK:
Damien02a7c412013-12-29 18:48:37 +0000376 // we are exiting an exception handler, so pop the last one of the exception-stack
377 assert(exc_sp >= &exc_stack[0]);
378 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
379 exc_sp -= 2; // pop back to previous exception handler
Damience89a212013-10-15 22:25:17 +0100380 break;
381
Damien George600ae732014-01-21 23:48:04 +0000382 // matched against: SETUP_EXCEPT
Damiend99b0522013-12-21 18:17:45 +0000383 case MP_BC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100384 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100385 // 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 +0100386 assert(exc_sp >= &exc_stack[0]);
Damiend99b0522013-12-21 18:17:45 +0000387 //sp = (mp_obj_t*)(*exc_sp--);
Damienc9f91972013-10-15 23:46:01 +0100388 //exc_sp--; // discard ip
Damien8f9e2ee2013-12-29 16:54:59 +0000389 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
390 exc_sp -= 2; // pop back to previous exception handler
Damien George20006db2014-01-18 14:10:48 +0000391 //sp -= 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100392 break;
393
Damiend99b0522013-12-21 18:17:45 +0000394 case MP_BC_UNARY_OP:
Damien7410e442013-11-02 19:47:57 +0000395 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000396 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000397 break;
398
Damiend99b0522013-12-21 18:17:45 +0000399 case MP_BC_BINARY_OP:
Damience89a212013-10-15 22:25:17 +0100400 unum = *ip++;
401 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000402 obj1 = TOP();
403 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100404 break;
405
Damiend99b0522013-12-21 18:17:45 +0000406 case MP_BC_BUILD_TUPLE:
Damienc226dca2013-10-16 16:12:52 +0100407 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000408 sp -= unum - 1;
409 SET_TOP(rt_build_tuple(unum, sp));
Damienc226dca2013-10-16 16:12:52 +0100410 break;
411
Damiend99b0522013-12-21 18:17:45 +0000412 case MP_BC_BUILD_LIST:
Damience89a212013-10-15 22:25:17 +0100413 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000414 sp -= unum - 1;
415 SET_TOP(rt_build_list(unum, sp));
Damience89a212013-10-15 22:25:17 +0100416 break;
417
Damiend99b0522013-12-21 18:17:45 +0000418 case MP_BC_LIST_APPEND:
Damienc226dca2013-10-16 16:12:52 +0100419 DECODE_UINT;
420 // I think it's guaranteed by the compiler that sp[unum] is a list
Damien George20006db2014-01-18 14:10:48 +0000421 rt_list_append(sp[-unum], sp[0]);
422 sp--;
Damienc226dca2013-10-16 16:12:52 +0100423 break;
424
Damiend99b0522013-12-21 18:17:45 +0000425 case MP_BC_BUILD_MAP:
Damience89a212013-10-15 22:25:17 +0100426 DECODE_UINT;
427 PUSH(rt_build_map(unum));
428 break;
429
Damiend99b0522013-12-21 18:17:45 +0000430 case MP_BC_STORE_MAP:
Damien George20006db2014-01-18 14:10:48 +0000431 sp -= 2;
432 rt_store_map(sp[0], sp[2], sp[1]);
Damience89a212013-10-15 22:25:17 +0100433 break;
434
Damiend99b0522013-12-21 18:17:45 +0000435 case MP_BC_MAP_ADD:
Damien5fd09662013-10-16 20:54:01 +0100436 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000437 // I think it's guaranteed by the compiler that sp[-unum - 1] is a map
438 rt_store_map(sp[-unum - 1], sp[0], sp[-1]);
439 sp -= 2;
Damien5fd09662013-10-16 20:54:01 +0100440 break;
441
Damiend99b0522013-12-21 18:17:45 +0000442 case MP_BC_BUILD_SET:
Damience89a212013-10-15 22:25:17 +0100443 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000444 sp -= unum - 1;
445 SET_TOP(rt_build_set(unum, sp));
Damience89a212013-10-15 22:25:17 +0100446 break;
447
Damiend99b0522013-12-21 18:17:45 +0000448 case MP_BC_SET_ADD:
Damienc12aa462013-10-16 20:57:49 +0100449 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000450 // I think it's guaranteed by the compiler that sp[-unum] is a set
451 rt_store_set(sp[-unum], sp[0]);
452 sp--;
Damienc12aa462013-10-16 20:57:49 +0100453 break;
454
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200455#if MICROPY_ENABLE_SLICE
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200456 case MP_BC_BUILD_SLICE:
457 DECODE_UINT;
458 if (unum == 2) {
459 obj2 = POP();
460 obj1 = TOP();
461 SET_TOP(mp_obj_new_slice(obj1, obj2, NULL));
462 } else {
463 printf("3-argument slice is not supported\n");
464 assert(0);
465 }
466 break;
Paul Sokolovskye606cb62014-01-04 01:34:23 +0200467#endif
Paul Sokolovskyded0a1e2014-01-03 02:48:56 +0200468
Damiend99b0522013-12-21 18:17:45 +0000469 case MP_BC_UNPACK_SEQUENCE:
Damien6f3e7fc2013-11-26 15:15:50 +0000470 DECODE_UINT;
Damien George932bf1c2014-01-18 23:42:49 +0000471 rt_unpack_sequence(sp[0], unum, sp);
Damien George20006db2014-01-18 14:10:48 +0000472 sp += unum - 1;
Damien6f3e7fc2013-11-26 15:15:50 +0000473 break;
474
Damiend99b0522013-12-21 18:17:45 +0000475 case MP_BC_MAKE_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100476 DECODE_UINT;
477 PUSH(rt_make_function_from_id(unum));
478 break;
479
Damiend99b0522013-12-21 18:17:45 +0000480 case MP_BC_MAKE_CLOSURE:
Damien9ecbcff2013-12-11 00:41:43 +0000481 DECODE_UINT;
482 obj1 = POP();
483 PUSH(rt_make_closure_from_id(unum, obj1));
484 break;
485
Damiend99b0522013-12-21 18:17:45 +0000486 case MP_BC_CALL_FUNCTION:
Damience89a212013-10-15 22:25:17 +0100487 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000488 // unum & 0xff == n_positional
489 // (unum >> 8) & 0xff == n_keyword
490 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe);
491 SET_TOP(rt_call_function_n_kw(*sp, unum & 0xff, (unum >> 8) & 0xff, sp + 1));
Damience89a212013-10-15 22:25:17 +0100492 break;
493
Damiend99b0522013-12-21 18:17:45 +0000494 case MP_BC_CALL_METHOD:
Damience89a212013-10-15 22:25:17 +0100495 DECODE_UINT;
Damien George20006db2014-01-18 14:10:48 +0000496 // unum & 0xff == n_positional
497 // (unum >> 8) & 0xff == n_keyword
498 sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
499 SET_TOP(rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp));
Damience89a212013-10-15 22:25:17 +0100500 break;
501
Damiend99b0522013-12-21 18:17:45 +0000502 case MP_BC_RETURN_VALUE:
Damience89a212013-10-15 22:25:17 +0100503 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100504 *sp_in_out = sp;
Damien George66327002014-01-02 18:20:41 +0000505 assert(exc_sp == &exc_stack[0] - 1);
Damienbd254452013-10-16 20:39:12 +0100506 return false;
507
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200508 case MP_BC_RAISE_VARARGS:
509 unum = *ip++;
510 assert(unum == 1);
511 obj1 = POP();
512 nlr_jump(obj1);
Paul Sokolovsky5388a3c2014-01-10 16:09:55 +0200513
Damiend99b0522013-12-21 18:17:45 +0000514 case MP_BC_YIELD_VALUE:
Damienbd254452013-10-16 20:39:12 +0100515 nlr_pop();
516 *ip_in_out = ip;
517 fastn[0] = fast0;
Damien George20006db2014-01-18 14:10:48 +0000518 fastn[-1] = fast1;
519 fastn[-2] = fast2;
Damienbd254452013-10-16 20:39:12 +0100520 *sp_in_out = sp;
521 return true;
Damience89a212013-10-15 22:25:17 +0100522
Damiend99b0522013-12-21 18:17:45 +0000523 case MP_BC_IMPORT_NAME:
Damiendb4c3612013-12-10 17:27:24 +0000524 DECODE_QSTR;
525 obj1 = POP();
Damien Georgecbd2f742014-01-19 11:48:48 +0000526 SET_TOP(rt_import_name(qst, obj1, TOP()));
Damiendb4c3612013-12-10 17:27:24 +0000527 break;
528
Damiend99b0522013-12-21 18:17:45 +0000529 case MP_BC_IMPORT_FROM:
Damiendb4c3612013-12-10 17:27:24 +0000530 DECODE_QSTR;
Damien Georgecbd2f742014-01-19 11:48:48 +0000531 obj1 = rt_import_from(TOP(), qst);
Damiendb4c3612013-12-10 17:27:24 +0000532 PUSH(obj1);
533 break;
534
Damience89a212013-10-15 22:25:17 +0100535 default:
Damien03c9cfb2013-11-05 22:06:08 +0000536 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100537 assert(0);
538 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100539 return false;
Damien429d7192013-10-04 19:53:11 +0100540 }
Damience89a212013-10-15 22:25:17 +0100541 }
Damien429d7192013-10-04 19:53:11 +0100542
Damience89a212013-10-15 22:25:17 +0100543 } else {
544 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100545
Damien George08335002014-01-18 23:24:36 +0000546 // set file and line number that the exception occurred at
547 if (MP_OBJ_IS_TYPE(nlr.ret_val, &exception_type)) {
548 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 +0000549 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
550 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 +0000551 machine_uint_t source_line = 1;
552 machine_uint_t bc = save_ip - code_info - code_info_size;
Damien Georgecbd2f742014-01-19 11:48:48 +0000553 //printf("find %lu %d %d\n", bc, code_info[12], code_info[13]);
Damien George28eb5772014-01-25 11:43:20 +0000554 for (const byte* ci = code_info + 12; *ci && bc >= ((*ci) & 31); ci++) {
555 bc -= *ci & 31;
556 source_line += *ci >> 5;
Damien George08335002014-01-18 23:24:36 +0000557 }
Damien George136b1492014-01-19 12:38:49 +0000558 mp_obj_exception_add_traceback(nlr.ret_val, source_file, source_line, block_name);
Damien George08335002014-01-18 23:24:36 +0000559 }
560
Damien8f9e2ee2013-12-29 16:54:59 +0000561 while (currently_in_except_block) {
562 // nested exception
563
564 assert(exc_sp >= &exc_stack[0]);
565
566 // TODO make a proper message for nested exception
567 // at the moment we are just raising the very last exception (the one that caused the nested exception)
568
569 // move up to previous exception handler
570 currently_in_except_block = (exc_sp[0] & 1); // restore previous state
571 exc_sp -= 2; // pop back to previous exception handler
572 }
573
Damienc9f91972013-10-15 23:46:01 +0100574 if (exc_sp >= &exc_stack[0]) {
Damien8f9e2ee2013-12-29 16:54:59 +0000575 // set flag to indicate that we are now handling an exception
576 currently_in_except_block = 1;
577
Damience89a212013-10-15 22:25:17 +0100578 // catch exception and pass to byte code
Damien8f9e2ee2013-12-29 16:54:59 +0000579 sp = (mp_obj_t*)(exc_sp[0] & (~((machine_uint_t)1)));
Damienc9f91972013-10-15 23:46:01 +0100580 ip = (const byte*)(exc_sp[-1]);
581 // push(traceback, exc-val, exc-type)
Damiend99b0522013-12-21 18:17:45 +0000582 PUSH(mp_const_none);
Damienc9f91972013-10-15 23:46:01 +0100583 PUSH(nlr.ret_val);
Damienb86e3f92013-12-29 17:17:43 +0000584 PUSH(nlr.ret_val); // TODO should be type(nlr.ret_val), I think...
Damien8f9e2ee2013-12-29 16:54:59 +0000585
Damience89a212013-10-15 22:25:17 +0100586 } else {
Damien8f9e2ee2013-12-29 16:54:59 +0000587 // re-raise exception to higher level
Damienbd254452013-10-16 20:39:12 +0100588 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100589 nlr_jump(nlr.ret_val);
590 }
Damien429d7192013-10-04 19:53:11 +0100591 }
592 }
593}