blob: e8e5caeb5634ab6adbb024fb0f86422010ed0728 [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++)
22
Damiena3977762013-10-09 23:10:10 +010023// args are in reverse order in array
Damienbd254452013-10-16 20:39:12 +010024py_obj_t py_execute_byte_code(const byte *code, const py_obj_t *args, uint n_args) {
25 py_obj_t state[18]; // TODO allocate properly
26 // init args
27 for (int i = 0; i < n_args; i++) {
28 assert(i < 8);
29 state[i] = args[n_args - 1 - i];
30 }
31 py_obj_t *sp = &state[18];
32 const byte *ip = code;
Damien03c9cfb2013-11-05 22:06:08 +000033 if (py_execute_byte_code_2(&ip, &state[0], &sp)) {
Damienbd254452013-10-16 20:39:12 +010034 // it shouldn't yield
35 assert(0);
36 }
Damien4ebb32f2013-11-02 14:33:10 +000037 // TODO check fails if, eg, return from within for loop
38 //assert(sp == &state[17]);
Damienbd254452013-10-16 20:39:12 +010039 return *sp;
40}
41
42// fastn has items in normal order
43// sp points to top of stack which grows down
Damien03c9cfb2013-11-05 22:06:08 +000044bool 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 +010045 // careful: be sure to declare volatile any variables read in the exception handler (written is ok, I think)
46
Damienbd254452013-10-16 20:39:12 +010047 const byte *ip = *ip_in_out;
48 py_obj_t *sp = *sp_in_out;
Damien429d7192013-10-04 19:53:11 +010049 machine_uint_t unum;
Damien429d7192013-10-04 19:53:11 +010050 qstr qstr;
51 py_obj_t obj1, obj2;
Damienbd254452013-10-16 20:39:12 +010052 py_obj_t fast0 = fastn[0], fast1 = fastn[1], fast2 = fastn[2];
Damience89a212013-10-15 22:25:17 +010053 nlr_buf_t nlr;
Damien429d7192013-10-04 19:53:11 +010054
Damienc9f91972013-10-15 23:46:01 +010055 // on the exception stack we store (ip, sp) for each block
56 machine_uint_t exc_stack[8];
57 machine_uint_t *volatile exc_sp = &exc_stack[-1]; // stack grows up, exc_sp points to top of stack
58
Damience89a212013-10-15 22:25:17 +010059 // outer exception handling loop
Damien429d7192013-10-04 19:53:11 +010060 for (;;) {
Damience89a212013-10-15 22:25:17 +010061 if (nlr_push(&nlr) == 0) {
62 // loop to execute byte code
63 for (;;) {
64 int op = *ip++;
65 switch (op) {
66 case PYBC_LOAD_CONST_FALSE:
67 PUSH(py_const_false);
68 break;
Damien429d7192013-10-04 19:53:11 +010069
Damience89a212013-10-15 22:25:17 +010070 case PYBC_LOAD_CONST_NONE:
71 PUSH(py_const_none);
72 break;
Damien429d7192013-10-04 19:53:11 +010073
Damience89a212013-10-15 22:25:17 +010074 case PYBC_LOAD_CONST_TRUE:
75 PUSH(py_const_true);
76 break;
Damien429d7192013-10-04 19:53:11 +010077
Damience89a212013-10-15 22:25:17 +010078 case PYBC_LOAD_CONST_SMALL_INT:
Damien03c9cfb2013-11-05 22:06:08 +000079 unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000;
Damien6addc892013-11-04 23:04:50 +000080 ip += 3;
Damien03c9cfb2013-11-05 22:06:08 +000081 PUSH((py_obj_t)(unum << 1 | 1));
Damience89a212013-10-15 22:25:17 +010082 break;
83
Damien7410e442013-11-02 19:47:57 +000084 case PYBC_LOAD_CONST_DEC:
85 DECODE_QSTR;
86 PUSH(rt_load_const_dec(qstr));
87 break;
88
Damience89a212013-10-15 22:25:17 +010089 case PYBC_LOAD_CONST_ID:
90 DECODE_QSTR;
91 PUSH(rt_load_const_str(qstr)); // TODO
92 break;
93
94 case PYBC_LOAD_CONST_STRING:
95 DECODE_QSTR;
96 PUSH(rt_load_const_str(qstr));
97 break;
98
99 case PYBC_LOAD_FAST_0:
100 PUSH(fast0);
101 break;
102
103 case PYBC_LOAD_FAST_1:
104 PUSH(fast1);
105 break;
106
107 case PYBC_LOAD_FAST_2:
108 PUSH(fast2);
109 break;
110
111 case PYBC_LOAD_FAST_N:
112 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100113 PUSH(fastn[unum]);
Damience89a212013-10-15 22:25:17 +0100114 break;
115
116 case PYBC_LOAD_NAME:
117 DECODE_QSTR;
118 PUSH(rt_load_name(qstr));
119 break;
120
121 case PYBC_LOAD_GLOBAL:
122 DECODE_QSTR;
123 PUSH(rt_load_global(qstr));
124 break;
125
126 case PYBC_LOAD_ATTR:
127 DECODE_QSTR;
128 *sp = rt_load_attr(*sp, qstr);
129 break;
130
131 case PYBC_LOAD_METHOD:
132 DECODE_QSTR;
133 sp -= 1;
134 rt_load_method(sp[1], qstr, sp);
135 break;
136
137 case PYBC_LOAD_BUILD_CLASS:
138 PUSH(rt_load_build_class());
139 break;
140
141 case PYBC_STORE_FAST_0:
142 fast0 = POP();
143 break;
144
145 case PYBC_STORE_FAST_1:
146 fast1 = POP();
147 break;
148
149 case PYBC_STORE_FAST_2:
150 fast2 = POP();
151 break;
152
153 case PYBC_STORE_FAST_N:
154 DECODE_UINT;
Damienbd254452013-10-16 20:39:12 +0100155 fastn[unum] = POP();
Damience89a212013-10-15 22:25:17 +0100156 break;
157
158 case PYBC_STORE_NAME:
159 DECODE_QSTR;
160 rt_store_name(qstr, POP());
161 break;
162
Damien6addc892013-11-04 23:04:50 +0000163 case PYBC_STORE_GLOBAL:
164 DECODE_QSTR;
165 rt_store_global(qstr, POP());
166 break;
167
Damience89a212013-10-15 22:25:17 +0100168 case PYBC_STORE_ATTR:
169 DECODE_QSTR;
170 rt_store_attr(sp[0], qstr, sp[1]);
171 sp += 2;
172 break;
173
174 case PYBC_STORE_SUBSCR:
175 rt_store_subscr(sp[1], sp[0], sp[2]);
176 sp += 3;
177 break;
178
179 case PYBC_DUP_TOP:
180 obj1 = *sp;
181 PUSH(obj1);
182 break;
183
184 case PYBC_DUP_TOP_TWO:
185 sp -= 2;
186 sp[0] = sp[2];
187 sp[1] = sp[3];
188 break;
189
190 case PYBC_POP_TOP:
191 ++sp;
192 break;
193
Damien4ebb32f2013-11-02 14:33:10 +0000194 case PYBC_ROT_TWO:
195 obj1 = sp[0];
196 sp[0] = sp[1];
197 sp[1] = obj1;
198 break;
199
Damience89a212013-10-15 22:25:17 +0100200 case PYBC_ROT_THREE:
201 obj1 = sp[0];
202 sp[0] = sp[1];
203 sp[1] = sp[2];
204 sp[2] = obj1;
205 break;
206
207 case PYBC_JUMP:
Damien03c9cfb2013-11-05 22:06:08 +0000208 DECODE_SLABEL;
209 ip += unum;
Damience89a212013-10-15 22:25:17 +0100210 break;
211
212 case PYBC_POP_JUMP_IF_TRUE:
Damien03c9cfb2013-11-05 22:06:08 +0000213 DECODE_SLABEL;
Damience89a212013-10-15 22:25:17 +0100214 if (rt_is_true(POP())) {
Damien03c9cfb2013-11-05 22:06:08 +0000215 ip += unum;
Damience89a212013-10-15 22:25:17 +0100216 }
217 break;
218
219 case PYBC_POP_JUMP_IF_FALSE:
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 /* we are trying to get away without using this opcode
227 case PYBC_SETUP_LOOP:
228 DECODE_UINT;
Damien03c9cfb2013-11-05 22:06:08 +0000229 // push_block(PYBC_SETUP_LOOP, ip + unum, sp)
Damience89a212013-10-15 22:25:17 +0100230 break;
231 */
232
233 case PYBC_SETUP_EXCEPT:
Damien03c9cfb2013-11-05 22:06:08 +0000234 DECODE_ULABEL; // except labels are always forward
235 *++exc_sp = (machine_uint_t)ip + unum;
Damienc9f91972013-10-15 23:46:01 +0100236 *++exc_sp = (machine_uint_t)sp;
Damience89a212013-10-15 22:25:17 +0100237 break;
238
239 case PYBC_END_FINALLY:
240 // not implemented
241 // if TOS is an exception, reraises the exception (3 values on TOS)
242 // if TOS is an integer, does something else
243 // if TOS is None, just pops it and continues
244 // else error
245 assert(0);
246 break;
247
248 case PYBC_GET_ITER:
249 *sp = rt_getiter(*sp);
250 break;
251
252 case PYBC_FOR_ITER:
Damien03c9cfb2013-11-05 22:06:08 +0000253 DECODE_ULABEL; // the jump offset if iteration finishes; for labels are always forward
Damience89a212013-10-15 22:25:17 +0100254 obj1 = rt_iternext(*sp);
255 if (obj1 == py_const_stop_iteration) {
256 ++sp; // pop the exhausted iterator
Damien03c9cfb2013-11-05 22:06:08 +0000257 ip += unum; // jump to after for-block
Damience89a212013-10-15 22:25:17 +0100258 } else {
259 PUSH(obj1); // push the next iteration value
260 }
261 break;
262
263 case PYBC_POP_BLOCK:
264 // pops block and restores the stack
265 assert(0);
266 break;
267
268 case PYBC_POP_EXCEPT:
Damienc9f91972013-10-15 23:46:01 +0100269 // TODO need to work out how blocks work etc
Damience89a212013-10-15 22:25:17 +0100270 // 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 +0100271 assert(exc_sp >= &exc_stack[0]);
272 //sp = (py_obj_t*)(*exc_sp--);
273 //exc_sp--; // discard ip
274 exc_sp -= 2;
275 //sp += 3; // pop 3 exception values
Damience89a212013-10-15 22:25:17 +0100276 break;
277
Damien7410e442013-11-02 19:47:57 +0000278 case PYBC_UNARY_OP:
279 unum = *ip++;
280 *sp = rt_unary_op(unum, *sp);
281 break;
282
Damience89a212013-10-15 22:25:17 +0100283 case PYBC_BINARY_OP:
284 unum = *ip++;
285 obj2 = POP();
286 obj1 = *sp;
287 *sp = rt_binary_op(unum, obj1, obj2);
288 break;
289
290 case PYBC_COMPARE_OP:
291 unum = *ip++;
292 obj2 = POP();
293 obj1 = *sp;
294 *sp = rt_compare_op(unum, obj1, obj2);
295 break;
296
Damienc226dca2013-10-16 16:12:52 +0100297 case PYBC_BUILD_TUPLE:
298 DECODE_UINT;
299 obj1 = rt_build_tuple(unum, sp);
300 sp += unum - 1;
301 *sp = obj1;
302 break;
303
Damience89a212013-10-15 22:25:17 +0100304 case PYBC_BUILD_LIST:
305 DECODE_UINT;
306 obj1 = rt_build_list(unum, sp);
307 sp += unum - 1;
308 *sp = obj1;
309 break;
310
Damienc226dca2013-10-16 16:12:52 +0100311 case PYBC_LIST_APPEND:
312 DECODE_UINT;
313 // I think it's guaranteed by the compiler that sp[unum] is a list
314 rt_list_append(sp[unum], sp[0]);
315 sp++;
316 break;
317
Damience89a212013-10-15 22:25:17 +0100318 case PYBC_BUILD_MAP:
319 DECODE_UINT;
320 PUSH(rt_build_map(unum));
321 break;
322
323 case PYBC_STORE_MAP:
324 sp += 2;
325 rt_store_map(sp[0], sp[-2], sp[-1]);
326 break;
327
Damien5fd09662013-10-16 20:54:01 +0100328 case PYBC_MAP_ADD:
329 DECODE_UINT;
330 // I think it's guaranteed by the compiler that sp[unum + 1] is a map
331 rt_store_map(sp[unum + 1], sp[0], sp[1]);
332 sp += 2;
333 break;
334
Damience89a212013-10-15 22:25:17 +0100335 case PYBC_BUILD_SET:
336 DECODE_UINT;
337 obj1 = rt_build_set(unum, sp);
338 sp += unum - 1;
339 *sp = obj1;
340 break;
341
Damienc12aa462013-10-16 20:57:49 +0100342 case PYBC_SET_ADD:
343 DECODE_UINT;
344 // I think it's guaranteed by the compiler that sp[unum] is a set
345 rt_store_set(sp[unum], sp[0]);
346 sp++;
347 break;
348
Damience89a212013-10-15 22:25:17 +0100349 case PYBC_MAKE_FUNCTION:
350 DECODE_UINT;
351 PUSH(rt_make_function_from_id(unum));
352 break;
353
354 case PYBC_CALL_FUNCTION:
355 DECODE_UINT;
356 assert((unum & 0xff00) == 0); // n_keyword
357 unum &= 0xff; // n_positional
358 sp += unum;
359 *sp = rt_call_function_n(*sp, unum, sp - unum);
360 break;
361
362 case PYBC_CALL_METHOD:
363 DECODE_UINT;
364 assert((unum & 0xff00) == 0); // n_keyword
365 unum &= 0xff;
366 obj1 = rt_call_method_n(unum, sp);
367 sp += unum + 1;
368 *sp = obj1;
369 break;
370
371 case PYBC_RETURN_VALUE:
372 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100373 *sp_in_out = sp;
Damienc9f91972013-10-15 23:46:01 +0100374 assert(exc_sp == &exc_stack[-1]);
Damienbd254452013-10-16 20:39:12 +0100375 return false;
376
377 case PYBC_YIELD_VALUE:
378 nlr_pop();
379 *ip_in_out = ip;
380 fastn[0] = fast0;
381 fastn[1] = fast1;
382 fastn[2] = fast2;
383 *sp_in_out = sp;
384 return true;
Damience89a212013-10-15 22:25:17 +0100385
386 default:
Damien03c9cfb2013-11-05 22:06:08 +0000387 printf("code %p, byte code 0x%02x not implemented\n", ip, op);
Damience89a212013-10-15 22:25:17 +0100388 assert(0);
389 nlr_pop();
Damienbd254452013-10-16 20:39:12 +0100390 return false;
Damien429d7192013-10-04 19:53:11 +0100391 }
Damience89a212013-10-15 22:25:17 +0100392 }
Damien429d7192013-10-04 19:53:11 +0100393
Damience89a212013-10-15 22:25:17 +0100394 } else {
395 // exception occurred
Damien429d7192013-10-04 19:53:11 +0100396
Damienc9f91972013-10-15 23:46:01 +0100397 if (exc_sp >= &exc_stack[0]) {
Damience89a212013-10-15 22:25:17 +0100398 // catch exception and pass to byte code
Damienc9f91972013-10-15 23:46:01 +0100399 sp = (py_obj_t*)(exc_sp[0]);
400 ip = (const byte*)(exc_sp[-1]);
401 // push(traceback, exc-val, exc-type)
402 PUSH(py_const_none);
403 PUSH(nlr.ret_val);
404 PUSH(py_const_none);
Damience89a212013-10-15 22:25:17 +0100405 } else {
406 // re-raise exception
Damienbd254452013-10-16 20:39:12 +0100407 // TODO what to do if this is a generator??
Damience89a212013-10-15 22:25:17 +0100408 nlr_jump(nlr.ret_val);
409 }
Damien429d7192013-10-04 19:53:11 +0100410 }
411 }
412}