blob: fabed0f02eb88db2a8e5697f2aa4785a4d0c5913 [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
123 case PYBC_LOAD_NAME:
124 DECODE_QSTR;
125 PUSH(rt_load_name(qstr));
126 break;
127
128 case PYBC_LOAD_GLOBAL:
129 DECODE_QSTR;
130 PUSH(rt_load_global(qstr));
131 break;
132
133 case PYBC_LOAD_ATTR:
134 DECODE_QSTR;
Damiendb4c3612013-12-10 17:27:24 +0000135 SET_TOP(rt_load_attr(TOP(), qstr));
Damience89a212013-10-15 22:25:17 +0100136 break;
137
138 case PYBC_LOAD_METHOD:
139 DECODE_QSTR;
140 sp -= 1;
141 rt_load_method(sp[1], qstr, sp);
142 break;
143
144 case PYBC_LOAD_BUILD_CLASS:
145 PUSH(rt_load_build_class());
146 break;
147
148 case PYBC_STORE_FAST_0:
149 fast0 = POP();
150 break;
151
152 case PYBC_STORE_FAST_1:
153 fast1 = POP();
154 break;
155
156 case PYBC_STORE_FAST_2:
157 fast2 = POP();
158 break;
159
160 case PYBC_STORE_FAST_N:
161 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100162 fastn[unum] = POP();
Damience89a212013-10-15 22:25:17 +0100163 break;
164
165 case PYBC_STORE_NAME:
166 DECODE_QSTR;
167 rt_store_name(qstr, POP());
168 break;
169
Damien6addc892013-11-04 23:04:50 +0000170 case PYBC_STORE_GLOBAL:
171 DECODE_QSTR;
172 rt_store_global(qstr, POP());
173 break;
174
Damience89a212013-10-15 22:25:17 +0100175 case PYBC_STORE_ATTR:
176 DECODE_QSTR;
177 rt_store_attr(sp[0], qstr, sp[1]);
178 sp += 2;
179 break;
180
181 case PYBC_STORE_SUBSCR:
182 rt_store_subscr(sp[1], sp[0], sp[2]);
183 sp += 3;
184 break;
185
186 case PYBC_DUP_TOP:
Damiendb4c3612013-12-10 17:27:24 +0000187 obj1 = TOP();
Damience89a212013-10-15 22:25:17 +0100188 PUSH(obj1);
189 break;
190
191 case PYBC_DUP_TOP_TWO:
192 sp -= 2;
193 sp[0] = sp[2];
194 sp[1] = sp[3];
195 break;
196
197 case PYBC_POP_TOP:
198 ++sp;
199 break;
200
Damien4ebb32f2013-11-02 14:33:10 +0000201 case PYBC_ROT_TWO:
202 obj1 = sp[0];
203 sp[0] = sp[1];
204 sp[1] = obj1;
205 break;
206
Damience89a212013-10-15 22:25:17 +0100207 case PYBC_ROT_THREE:
208 obj1 = sp[0];
209 sp[0] = sp[1];
210 sp[1] = sp[2];
211 sp[2] = obj1;
212 break;
213
214 case PYBC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000215 DECODE_SLABEL;
216 ip += unum;
Damience89a212013-10-15 22:25:17 +0100217 break;
218
219 case PYBC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000220 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100221 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000222 ip += unum;
Damience89a212013-10-15 22:25:17 +0100223 }
224 break;
225
226 case PYBC_POP_JUMP_IF_FALSE:
Damien03c9cfb2013-11-05 22:06:08 +0000227 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100228 if (!rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000229 ip += unum;
Damience89a212013-10-15 22:25:17 +0100230 }
231 break;
232
Damien94658e22013-11-09 20:12:32 +0000233 case PYBC_JUMP_IF_TRUE_OR_POP:
234 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000235 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000236 ip += unum;
237 } else {
238 sp++;
239 }
240 break;
241
242 case PYBC_JUMP_IF_FALSE_OR_POP:
243 DECODE_SLABEL;
Damiendb4c3612013-12-10 17:27:24 +0000244 if (rt_is_true(TOP())) {
Damien94658e22013-11-09 20:12:32 +0000245 sp++;
246 } else {
247 ip += unum;
248 }
249 break;
250
Damience89a212013-10-15 22:25:17 +0100251 /* we are trying to get away without using this opcode
252 case PYBC_SETUP_LOOP:
253 DECODE_UINT;
Damien03c9cfb2013-11-05 22:06:08 +0000254 // push_block(PYBC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100255 break;
256 */
257
258 case PYBC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000259 DECODE_ULABEL; // except labels are always forward
260 *++exc_sp = (machine_uint_t)ip + unum;
Damienc9f91972013-10-15 23:46:01 +0100261 *++exc_sp = (machine_uint_t)sp;
Damience89a212013-10-15 22:25:17 +0100262 break;
263
264 case PYBC_END_FINALLY:
265 // not implemented
266 // if TOS is an exception, reraises the exception (3 values on TOS)
267 // if TOS is an integer, does something else
268 // if TOS is None, just pops it and continues
269 // else error
270 assert(0);
271 break;
272
273 case PYBC_GET_ITER:
Damiendb4c3612013-12-10 17:27:24 +0000274 SET_TOP(rt_getiter(TOP()));
Damience89a212013-10-15 22:25:17 +0100275 break;
276
277 case PYBC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000278 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damiendb4c3612013-12-10 17:27:24 +0000279 obj1 = rt_iternext(TOP());
Damience89a212013-10-15 22:25:17 +0100280 if (obj1 == py_const_stop_iteration) {
281 ++sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000282 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100283 } else {
284 PUSH(obj1); // push the next iteration value
285 }
286 break;
287
288 case PYBC_POP_BLOCK:
289 // pops block and restores the stack
290 assert(0);
291 break;
292
293 case PYBC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100294 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100295 // 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 +0100296 assert(exc_sp >= &exc_stack[0]);
297 //sp = (py_obj_t*)(*exc_sp--);
298 //exc_sp--; // discard ip
299 exc_sp -= 2;
300 //sp += 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100301 break;
302
Damien7410e442013-11-02 19:47:57 +0000303 case PYBC_UNARY_OP:
304 unum = *ip++;
Damiendb4c3612013-12-10 17:27:24 +0000305 SET_TOP(rt_unary_op(unum, TOP()));
Damien7410e442013-11-02 19:47:57 +0000306 break;
307
Damience89a212013-10-15 22:25:17 +0100308 case PYBC_BINARY_OP:
309 unum = *ip++;
310 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000311 obj1 = TOP();
312 SET_TOP(rt_binary_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100313 break;
314
315 case PYBC_COMPARE_OP:
316 unum = *ip++;
317 obj2 = POP();
Damiendb4c3612013-12-10 17:27:24 +0000318 obj1 = TOP();
319 SET_TOP(rt_compare_op(unum, obj1, obj2));
Damience89a212013-10-15 22:25:17 +0100320 break;
321
Damienc226dca2013-10-16 16:12:52 +0100322 case PYBC_BUILD_TUPLE:
323 DECODE_UINT;
324 obj1 = rt_build_tuple(unum, sp);
325 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000326 SET_TOP(obj1);
Damienc226dca2013-10-16 16:12:52 +0100327 break;
328
Damience89a212013-10-15 22:25:17 +0100329 case PYBC_BUILD_LIST:
330 DECODE_UINT;
331 obj1 = rt_build_list(unum, sp);
332 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000333 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100334 break;
335
Damienc226dca2013-10-16 16:12:52 +0100336 case PYBC_LIST_APPEND:
337 DECODE_UINT;
338 // I think it's guaranteed by the compiler that sp[unum] is a list
339 rt_list_append(sp[unum], sp[0]);
340 sp++;
341 break;
342
Damience89a212013-10-15 22:25:17 +0100343 case PYBC_BUILD_MAP:
344 DECODE_UINT;
345 PUSH(rt_build_map(unum));
346 break;
347
348 case PYBC_STORE_MAP:
349 sp += 2;
350 rt_store_map(sp[0], sp[-2], sp[-1]);
351 break;
352
Damien5fd09662013-10-16 20:54:01 +0100353 case PYBC_MAP_ADD:
354 DECODE_UINT;
355 // I think it's guaranteed by the compiler that sp[unum + 1] is a map
356 rt_store_map(sp[unum + 1], sp[0], sp[1]);
357 sp += 2;
358 break;
359
Damience89a212013-10-15 22:25:17 +0100360 case PYBC_BUILD_SET:
361 DECODE_UINT;
362 obj1 = rt_build_set(unum, sp);
363 sp += unum - 1;
Damiendb4c3612013-12-10 17:27:24 +0000364 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100365 break;
366
Damienc12aa462013-10-16 20:57:49 +0100367 case PYBC_SET_ADD:
368 DECODE_UINT;
369 // I think it's guaranteed by the compiler that sp[unum] is a set
370 rt_store_set(sp[unum], sp[0]);
371 sp++;
372 break;
373
Damien6f3e7fc2013-11-26 15:15:50 +0000374 case PYBC_UNPACK_SEQUENCE:
375 DECODE_UINT;
376 rt_unpack_sequence(sp[0], unum, sp - unum + 1);
377 sp -= unum - 1;
378 break;
379
Damience89a212013-10-15 22:25:17 +0100380 case PYBC_MAKE_FUNCTION:
381 DECODE_UINT;
382 PUSH(rt_make_function_from_id(unum));
383 break;
384
385 case PYBC_CALL_FUNCTION:
386 DECODE_UINT;
387 assert((unum & 0xff00) == 0); // n_keyword
388 unum &= 0xff; // n_positional
389 sp += unum;
390 *sp = rt_call_function_n(*sp, unum, sp - unum);
391 break;
392
393 case PYBC_CALL_METHOD:
394 DECODE_UINT;
Damien6f3e7fc2013-11-26 15:15:50 +0000395 if ((unum & 0xff00) == 0) {
396 // no keywords
397 unum &= 0xff;
398 obj1 = rt_call_method_n(unum, sp);
399 sp += unum + 1;
400 } else {
401 // keywords
402 obj1 = rt_call_method_n_kw(unum & 0xff, (unum >> 8) & 0xff, sp);
403 sp += (unum & 0xff) + ((unum >> 7) & 0x1fe) + 1;
404 }
Damiendb4c3612013-12-10 17:27:24 +0000405 SET_TOP(obj1);
Damience89a212013-10-15 22:25:17 +0100406 break;
407
408 case PYBC_RETURN_VALUE:
409 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100410 *sp_in_out = sp;
Damienc9f91972013-10-15 23:46:01 +0100411 assert(exc_sp == &exc_stack[-1]);
Damienbd254452013-10-16 20:39:12 +0100412 return false;
413
414 case PYBC_YIELD_VALUE:
415 nlr_pop();
416 *ip_in_out = ip;
417 fastn[0] = fast0;
418 fastn[1] = fast1;
419 fastn[2] = fast2;
420 *sp_in_out = sp;
421 return true;
Damience89a212013-10-15 22:25:17 +0100422
Damiendb4c3612013-12-10 17:27:24 +0000423 case PYBC_IMPORT_NAME:
424 DECODE_QSTR;
425 obj1 = POP();
426 SET_TOP(rt_import_name(qstr, obj1, TOP()));
427 break;
428
429 case PYBC_IMPORT_FROM:
430 DECODE_QSTR;
431 obj1 = rt_import_from(TOP(), qstr);
432 PUSH(obj1);
433 break;
434
Damience89a212013-10-15 22:25:17 +0100435 default:
Damien03c9cfb2013-11-05 22:06:08 +0000436 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100437 assert(0);
438 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100439 return false;
Damien429d7192013-10-04 19:53:11 +0100440 }
Damience89a212013-10-15 22:25:17 +0100441 }
Damien429d7192013-10-04 19:53:11 +0100442
Damience89a212013-10-15 22:25:17 +0100443 } else {
444 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100445
Damienc9f91972013-10-15 23:46:01 +0100446 if (exc_sp >= &exc_stack[0]) {
Damience89a212013-10-15 22:25:17 +0100447 // catch exception and pass to byte code
Damienc9f91972013-10-15 23:46:01 +0100448 sp = (py_obj_t*)(exc_sp[0]);
449 ip = (const byte*)(exc_sp[-1]);
450 // push(traceback, exc-val, exc-type)
451 PUSH(py_const_none);
452 PUSH(nlr.ret_val);
453 PUSH(py_const_none);
Damience89a212013-10-15 22:25:17 +0100454 } else {
455 // re-raise exception
Damienbd254452013-10-16 20:39:12 +0100456 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100457 nlr_jump(nlr.ret_val);
458 }
Damien429d7192013-10-04 19:53:11 +0100459 }
460 }
461}