blob: 7db6a7d9c406e7ff5d0ad13dcd9d624bc627bcde [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"
Damienc025ebb2013-10-12 14:30:21 +01009#include "mpyconfig.h"
Damien429d7192013-10-04 19:53:11 +010010#include "runtime.h"
Damieneb19efb2013-10-10 22:06:54 +010011#include "bc.h"
Damien429d7192013-10-04 19:53:11 +010012
Damienbd254452013-10-16 20:39:12 +010013// (value) stack grows down (to be compatible with native code when passing pointers to the stack), top element is pointed to
14// exception stack grows up, top element is pointed to
15
Damien429d7192013-10-04 19:53:11 +010016#define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
Damien03c9cfb2013-11-05 22:06:08 +000017#define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
18#define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
Damien429d7192013-10-04 19:53:11 +010019#define DECODE_QSTR do { qstr = *ip++; if (qstr > 127) { qstr = ((qstr & 0x3f) << 8) | (*ip++); } } while (0)
20#define PUSH(val) *--sp = (val)
21#define POP() (*sp++)
Damiendb4c3612013-12-10 17:27:24 +000022#define TOP() (*sp)
23#define SET_TOP(val) *sp = (val)
Damien429d7192013-10-04 19:53:11 +010024
Damiena3977762013-10-09 23:10:10 +010025// args are in reverse order in array
Damien40fdfe32013-11-05 22:16:22 +000026py_obj_t py_execute_byte_code(const byte *code, const py_obj_t *args, uint n_args, uint n_state) {
27 py_obj_t temp_state[10]; // TODO allocate properly
28 py_obj_t *state = &temp_state[0];
29 py_obj_t *sp = &state[10];
30 if (n_state > 10) {
31 state = m_new(py_obj_t, n_state);
32 sp = &state[n_state];
33 }
Damienbd254452013-10-16 20:39:12 +010034 // init args
35 for (int i = 0; i < n_args; i++) {
36 assert(i < 8);
37 state[i] = args[n_args - 1 - i];
38 }
Damienbd254452013-10-16 20:39:12 +010039 const byte *ip = code;
Damien03c9cfb2013-11-05 22:06:08 +000040 if (py_execute_byte_code_2(&ip, &state[0], &sp)) {
Damienbd254452013-10-16 20:39:12 +010041 // it shouldn't yield
42 assert(0);
43 }
Damien4ebb32f2013-11-02 14:33:10 +000044 // TODO check fails if, eg, return from within for loop
45 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010046 return *sp;
47}
48
49// fastn has items in normal order
50// sp points to top of stack which grows down
Damien03c9cfb2013-11-05 22:06:08 +000051bool py_execute_byte_code_2(const byte **ip_in_out, py_obj_t *fastn, py_obj_t **sp_in_out) {
Damienc9f91972013-10-15 23:46:01 +010052 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
53
Damienbd254452013-10-16 20:39:12 +010054 const byte *ip = *ip_in_out;
55 py_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +010056 machine_uint_t unum;
Damien429d7192013-10-04 19:53:11 +010057 qstr qstr;
58 py_obj_t obj1, obj2;
Damienbd254452013-10-16 20:39:12 +010059 py_obj_t fast0 = fastn[0], fast1 = fastn[1], fast2 = fastn[2];
Damience89a212013-10-15 22:25:17 +010060 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +010061
Damienc9f91972013-10-15 23:46:01 +010062 // on the exception stack we store (ip, sp) for each block
63 machine_uint_t exc_stack[8];
64 machine_uint_t *volatile exc_sp = &exc_stack[-1]; // stack grows up, exc_sp points to top of stack
65
Damience89a212013-10-15 22:25:17 +010066 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +010067 for (;;) {
Damience89a212013-10-15 22:25:17 +010068 if (nlr_push(&nlr) == 0) {
69 // loop to execute byte code
70 for (;;) {
71 int op = *ip++;
72 switch (op) {
73 case PYBC_LOAD_CONST_FALSE:
74 PUSH(py_const_false);
75 break;
Damien429d7192013-10-04 19:53:11 +010076
Damience89a212013-10-15 22:25:17 +010077 case PYBC_LOAD_CONST_NONE:
78 PUSH(py_const_none);
79 break;
Damien429d7192013-10-04 19:53:11 +010080
Damience89a212013-10-15 22:25:17 +010081 case PYBC_LOAD_CONST_TRUE:
82 PUSH(py_const_true);
83 break;
Damien429d7192013-10-04 19:53:11 +010084
Damience89a212013-10-15 22:25:17 +010085 case PYBC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +000086 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +000087 ip += 3;
Damien03c9cfb2013-11-05 22:06:08 +000088 PUSH((py_obj_t)(unum << 1 | 1));
Damience89a212013-10-15 22:25:17 +010089 break;
90
Damien7410e442013-11-02 19:47:57 +000091 case PYBC_LOAD_CONST_DEC:
92 DECODE_QSTR;
93 PUSH(rt_load_const_dec(qstr));
94 break;
95
Damience89a212013-10-15 22:25:17 +010096 case PYBC_LOAD_CONST_ID:
97 DECODE_QSTR;
98 PUSH(rt_load_const_str(qstr)); // TODO
99 break;
100
101 case PYBC_LOAD_CONST_STRING:
102 DECODE_QSTR;
103 PUSH(rt_load_const_str(qstr));
104 break;
105
106 case PYBC_LOAD_FAST_0:
107 PUSH(fast0);
108 break;
109
110 case PYBC_LOAD_FAST_1:
111 PUSH(fast1);
112 break;
113
114 case PYBC_LOAD_FAST_2:
115 PUSH(fast2);
116 break;
117
118 case PYBC_LOAD_FAST_N:
119 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100120 PUSH(fastn[unum]);
Damience89a212013-10-15 22:25:17 +0100121 break;
122
Damien9ecbcff2013-12-11 00:41:43 +0000123 case PYBC_LOAD_DEREF:
124 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000125 PUSH(rt_get_cell(fastn[unum]));
Damien9ecbcff2013-12-11 00:41:43 +0000126 break;
127
128 case PYBC_LOAD_CLOSURE:
129 DECODE_UINT;
130 PUSH(fastn[unum]);
131 break;
132
Damience89a212013-10-15 22:25:17 +0100133 case PYBC_LOAD_NAME:
134 DECODE_QSTR;
135 PUSH(rt_load_name(qstr));
136 break;
137
138 case PYBC_LOAD_GLOBAL:
139 DECODE_QSTR;
140 PUSH(rt_load_global(qstr));
141 break;
142
143 case PYBC_LOAD_ATTR:
144 DECODE_QSTR;
Damiendb4c3612013-12-10 17:27:24 +0000145 SET_TOP(rt_load_attr(TOP(), qstr));
Damience89a212013-10-15 22:25:17 +0100146 break;
147
148 case PYBC_LOAD_METHOD:
149 DECODE_QSTR;
150 sp -= 1;
151 rt_load_method(sp[1], qstr, sp);
152 break;
153
154 case PYBC_LOAD_BUILD_CLASS:
155 PUSH(rt_load_build_class());
156 break;
157
158 case PYBC_STORE_FAST_0:
159 fast0 = POP();
160 break;
161
162 case PYBC_STORE_FAST_1:
163 fast1 = POP();
164 break;
165
166 case PYBC_STORE_FAST_2:
167 fast2 = POP();
168 break;
169
170 case PYBC_STORE_FAST_N:
171 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100172 fastn[unum] = POP();
Damience89a212013-10-15 22:25:17 +0100173 break;
174
Damien9ecbcff2013-12-11 00:41:43 +0000175 case PYBC_STORE_DEREF:
176 DECODE_UINT;
Damien660365e2013-12-17 18:27:24 +0000177 rt_set_cell(fastn[unum], POP());
Damien9ecbcff2013-12-11 00:41:43 +0000178 break;
179
Damience89a212013-10-15 22:25:17 +0100180 case PYBC_STORE_NAME:
181 DECODE_QSTR;
182 rt_store_name(qstr, POP());
183 break;
184
Damien6addc892013-11-04 23:04:50 +0000185 case PYBC_STORE_GLOBAL:
186 DECODE_QSTR;
187 rt_store_global(qstr, POP());
188 break;
189
Damience89a212013-10-15 22:25:17 +0100190 case PYBC_STORE_ATTR:
191 DECODE_QSTR;
192 rt_store_attr(sp[0], qstr, sp[1]);
193 sp += 2;
194 break;
195
196 case PYBC_STORE_SUBSCR:
197 rt_store_subscr(sp[1], sp[0], sp[2]);
198 sp += 3;
199 break;
200
201 case PYBC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000202 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100203 PUSH(obj1);
204 break;
205
206 case PYBC_DUP_TOP_TWO:
207 sp -= 2;
208 sp[0] = sp[2];
209 sp[1] = sp[3];
210 break;
211
212 case PYBC_POP_TOP:
213 ++sp;
214 break;
215
Damien4ebb32f2013-11-02 14:33:10 +0000216 case PYBC_ROT_TWO:
217 obj1 = sp[0];
218 sp[0] = sp[1];
219 sp[1] = obj1;
220 break;
221
Damience89a212013-10-15 22:25:17 +0100222 case PYBC_ROT_THREE:
223 obj1 = sp[0];
224 sp[0] = sp[1];
225 sp[1] = sp[2];
226 sp[2] = obj1;
227 break;
228
229 case PYBC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000230 DECODE_SLABEL;
231 ip += unum;
Damience89a212013-10-15 22:25:17 +0100232 break;
233
234 case PYBC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000235 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100236 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000237 ip += unum;
Damience89a212013-10-15 22:25:17 +0100238 }
239 break;
240
241 case PYBC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000242 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100243 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000244 ip += unum;
Damience89a212013-10-15 22:25:17 +0100245 }
246 break;
247
Damien94658e22013-11-09 20:12:32 +0000248 case PYBC_JUMP_IF_TRUE_OR_POP:
249 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000250 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000251 ip += unum;
252 } else {
253 sp++;
254 }
255 break;
256
257 case PYBC_JUMP_IF_FALSE_OR_POP:
258 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000259 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000260 sp++;
261 } else {
262 ip += unum;
263 }
264 break;
265
Damience89a212013-10-15 22:25:17 +0100266 /* we are trying to get away without using this opcode
267 case PYBC_SETUP_LOOP:
268 DECODE_UINT;
Damien03c9cfb2013-11-05 22:06:08 +0000269 // push_block(PYBC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100270 break;
271 */
272
273 case PYBC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000274 DECODE_ULABEL; // except labels are always forward
275 *++exc_sp = (machine_uint_t)ip + unum;
Damienc9f91972013-10-15 23:46:01 +0100276 *++exc_sp = (machine_uint_t)sp;
Damience89a212013-10-15 22:25:17 +0100277 break;
278
279 case PYBC_END_FINALLY:
280 // not implemented
281 // if TOS is an exception, reraises the exception (3 values on TOS)
282 // if TOS is an integer, does something else
283 // if TOS is None, just pops it and continues
284 // else error
285 assert(0);
286 break;
287
288 case PYBC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000289 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100290 break;
291
292 case PYBC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000293 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000294 obj1 = rt_iternext(TOP());
Damience89a212013-10-15 22:25:17 +0100295 if (obj1 == py_const_stop_iteration) {
296 ++sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000297 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100298 } else {
299 PUSH(obj1); // push the next iteration value
300 }
301 break;
302
303 case PYBC_POP_BLOCK:
304 // pops block and restores the stack
305 assert(0);
306 break;
307
308 case PYBC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100309 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100310 // 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 +0100311 assert(exc_sp >= &exc_stack[0]);
312 //sp = (py_obj_t*)(*exc_sp--);
313 //exc_sp--; // discard ip
314 exc_sp -= 2;
315 //sp += 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100316 break;
317
Damien7410e442013-11-02 19:47:57 +0000318 case PYBC_UNARY_OP:
319 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000320 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000321 break;
322
Damience89a212013-10-15 22:25:17 +0100323 case PYBC_BINARY_OP:
324 unum = *ip++;
325 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000326 obj1 = TOP();
327 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100328 break;
329
330 case PYBC_COMPARE_OP:
331 unum = *ip++;
332 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000333 obj1 = TOP();
334 SET_TOP(rt_compare_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100335 break;
336
Damienc226dca2013-10-16 16:12:52 +0100337 case PYBC_BUILD_TUPLE:
338 DECODE_UINT;
339 obj1 = rt_build_tuple(unum, sp);
340 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000341 SET_TOP(obj1);
Damienc226dca2013-10-16 16:12:52 +0100342 break;
343
Damience89a212013-10-15 22:25:17 +0100344 case PYBC_BUILD_LIST:
345 DECODE_UINT;
346 obj1 = rt_build_list(unum, sp);
347 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000348 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100349 break;
350
Damienc226dca2013-10-16 16:12:52 +0100351 case PYBC_LIST_APPEND:
352 DECODE_UINT;
353 // I think it's guaranteed by the compiler that sp[unum] is a list
354 rt_list_append(sp[unum], sp[0]);
355 sp++;
356 break;
357
Damience89a212013-10-15 22:25:17 +0100358 case PYBC_BUILD_MAP:
359 DECODE_UINT;
360 PUSH(rt_build_map(unum));
361 break;
362
363 case PYBC_STORE_MAP:
364 sp += 2;
365 rt_store_map(sp[0], sp[-2], sp[-1]);
366 break;
367
Damien5fd09662013-10-16 20:54:01 +0100368 case PYBC_MAP_ADD:
369 DECODE_UINT;
370 // I think it's guaranteed by the compiler that sp[unum + 1] is a map
371 rt_store_map(sp[unum + 1], sp[0], sp[1]);
372 sp += 2;
373 break;
374
Damience89a212013-10-15 22:25:17 +0100375 case PYBC_BUILD_SET:
376 DECODE_UINT;
377 obj1 = rt_build_set(unum, sp);
378 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000379 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100380 break;
381
Damienc12aa462013-10-16 20:57:49 +0100382 case PYBC_SET_ADD:
383 DECODE_UINT;
384 // I think it's guaranteed by the compiler that sp[unum] is a set
385 rt_store_set(sp[unum], sp[0]);
386 sp++;
387 break;
388
Damien6f3e7fc2013-11-26 15:15:50 +0000389 case PYBC_UNPACK_SEQUENCE:
390 DECODE_UINT;
391 rt_unpack_sequence(sp[0], unum, sp - unum + 1);
392 sp -= unum - 1;
393 break;
394
Damience89a212013-10-15 22:25:17 +0100395 case PYBC_MAKE_FUNCTION:
396 DECODE_UINT;
397 PUSH(rt_make_function_from_id(unum));
398 break;
399
Damien9ecbcff2013-12-11 00:41:43 +0000400 case PYBC_MAKE_CLOSURE:
401 DECODE_UINT;
402 obj1 = POP();
403 PUSH(rt_make_closure_from_id(unum, obj1));
404 break;
405
Damience89a212013-10-15 22:25:17 +0100406 case PYBC_CALL_FUNCTION:
407 DECODE_UINT;
408 assert((unum & 0xff00) == 0); // n_keyword
409 unum &= 0xff; // n_positional
410 sp += unum;
411 *sp = rt_call_function_n(*sp, unum, sp - unum);
412 break;
413
414 case PYBC_CALL_METHOD:
415 DECODE_UINT;
Damien6f3e7fc2013-11-26 15:15:50 +0000416 if ((unum & 0xff00) == 0) {
417 // no keywords
418 unum &= 0xff;
419 obj1 = rt_call_method_n(unum, sp);
420 sp += unum + 1;
421 } else {
422 // keywords
423 obj1 = rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp);
424 sp += (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
425 }
Damiendb4c3612013-12-10 17:27:24 +0000426 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100427 break;
428
429 case PYBC_RETURN_VALUE:
430 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100431 *sp_in_out = sp;
Damienc9f91972013-10-15 23:46:01 +0100432 assert(exc_sp == &exc_stack[-1]);
Damienbd254452013-10-16 20:39:12 +0100433 return false;
434
435 case PYBC_YIELD_VALUE:
436 nlr_pop();
437 *ip_in_out = ip;
438 fastn[0] = fast0;
439 fastn[1] = fast1;
440 fastn[2] = fast2;
441 *sp_in_out = sp;
442 return true;
Damience89a212013-10-15 22:25:17 +0100443
Damiendb4c3612013-12-10 17:27:24 +0000444 case PYBC_IMPORT_NAME:
445 DECODE_QSTR;
446 obj1 = POP();
447 SET_TOP(rt_import_name(qstr, obj1, TOP()));
448 break;
449
450 case PYBC_IMPORT_FROM:
451 DECODE_QSTR;
452 obj1 = rt_import_from(TOP(), qstr);
453 PUSH(obj1);
454 break;
455
Damience89a212013-10-15 22:25:17 +0100456 default:
Damien03c9cfb2013-11-05 22:06:08 +0000457 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100458 assert(0);
459 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100460 return false;
Damien429d7192013-10-04 19:53:11 +0100461 }
Damience89a212013-10-15 22:25:17 +0100462 }
Damien429d7192013-10-04 19:53:11 +0100463
Damience89a212013-10-15 22:25:17 +0100464 } else {
465 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100466
Damienc9f91972013-10-15 23:46:01 +0100467 if (exc_sp >= &exc_stack[0]) {
Damience89a212013-10-15 22:25:17 +0100468 // catch exception and pass to byte code
Damienc9f91972013-10-15 23:46:01 +0100469 sp = (py_obj_t*)(exc_sp[0]);
470 ip = (const byte*)(exc_sp[-1]);
471 // push(traceback, exc-val, exc-type)
472 PUSH(py_const_none);
473 PUSH(nlr.ret_val);
474 PUSH(py_const_none);
Damience89a212013-10-15 22:25:17 +0100475 } else {
476 // re-raise exception
Damienbd254452013-10-16 20:39:12 +0100477 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100478 nlr_jump(nlr.ret_val);
479 }
Damien429d7192013-10-04 19:53:11 +0100480 }
481 }
482}