blob: 72881067d4e82577c18b4a219ec9ef32b7dd5413 [file] [log] [blame]
Damienc226dca2013-10-16 16:12:52 +01001// in principle, rt_xxx functions are called only by vm/native/viper and make assumptions about args
Damiend99b0522013-12-21 18:17:45 +00002// mp_xxx functions are safer and can be called by anyone
Damienbd254452013-10-16 20:39:12 +01003// note that rt_assign_xxx are called only from emit*, and maybe we can rename them to reflect this
Damienc226dca2013-10-16 16:12:52 +01004
Damien429d7192013-10-04 19:53:11 +01005#include <stdint.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include <assert.h>
10
Damience89a212013-10-15 22:25:17 +010011#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010012#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "mpconfig.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000014#include "mpqstr.h"
Damien660365e2013-12-17 18:27:24 +000015#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000016#include "runtime0.h"
17#include "runtime.h"
18#include "map.h"
Damien660365e2013-12-17 18:27:24 +000019#include "builtin.h"
20
Damien7f5dacf2013-10-10 11:24:39 +010021#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010022#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000023#define WRITE_CODE (1)
Damien7f5dacf2013-10-10 11:24:39 +010024#define DEBUG_printf(args...) printf(args)
25#define DEBUG_OP_printf(args...) printf(args)
26#else // don't print debugging info
Damiena3977762013-10-09 23:10:10 +010027#define DEBUG_printf(args...) (void)0
Damien429d7192013-10-04 19:53:11 +010028#define DEBUG_OP_printf(args...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010029#endif
Damien429d7192013-10-04 19:53:11 +010030
Damieneb19efb2013-10-10 22:06:54 +010031// locals and globals need to be pointers because they can be the same in outer module scope
Damiend99b0522013-12-21 18:17:45 +000032static mp_map_t *map_locals;
33static mp_map_t *map_globals;
34static mp_map_t map_builtins;
Damienbd254452013-10-16 20:39:12 +010035
Damien429d7192013-10-04 19:53:11 +010036typedef enum {
Damiend99b0522013-12-21 18:17:45 +000037 MP_CODE_NONE,
38 MP_CODE_BYTE,
39 MP_CODE_NATIVE,
40 MP_CODE_INLINE_ASM,
41} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010042
Damiend99b0522013-12-21 18:17:45 +000043typedef struct _mp_code_t {
44 mp_code_kind_t kind;
Damien429d7192013-10-04 19:53:11 +010045 int n_args;
Damienbd254452013-10-16 20:39:12 +010046 int n_locals;
47 int n_stack;
48 bool is_generator;
Damien429d7192013-10-04 19:53:11 +010049 union {
50 struct {
Damien429d7192013-10-04 19:53:11 +010051 byte *code;
52 uint len;
53 } u_byte;
Damien826005c2013-10-05 23:17:28 +010054 struct {
Damiend99b0522013-12-21 18:17:45 +000055 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010056 } u_native;
57 struct {
Damiene4af64f2013-10-06 12:04:13 +010058 void *fun;
Damien826005c2013-10-05 23:17:28 +010059 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010060 };
Damiend99b0522013-12-21 18:17:45 +000061} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010062
63static int next_unique_code_id;
Damiend99b0522013-12-21 18:17:45 +000064static mp_code_t *unique_codes;
Damien429d7192013-10-04 19:53:11 +010065
Damien0446a0d2013-11-17 13:16:36 +000066#ifdef WRITE_CODE
67FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010068#endif
Damien429d7192013-10-04 19:53:11 +010069
Damien8b3a7c22013-10-23 20:20:17 +010070void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +010071 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damiend99b0522013-12-21 18:17:45 +000072 map_locals = map_globals = mp_map_new(MP_MAP_QSTR, 1);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000073 mp_qstr_map_lookup(map_globals, MP_QSTR___name__, true)->value = mp_obj_new_str(MP_QSTR___main__);
Damien429d7192013-10-04 19:53:11 +010074
Damienb86e3f92013-12-29 17:17:43 +000075 // init built-in hash table
Damiend99b0522013-12-21 18:17:45 +000076 mp_map_init(&map_builtins, MP_MAP_QSTR, 3);
Damienb86e3f92013-12-29 17:17:43 +000077
78 // built-in exceptions (TODO, make these proper classes)
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000079 mp_qstr_map_lookup(&map_builtins, MP_QSTR_AttributeError, true)->value = mp_obj_new_exception(MP_QSTR_AttributeError);
80 mp_qstr_map_lookup(&map_builtins, MP_QSTR_IndexError, true)->value = mp_obj_new_exception(MP_QSTR_IndexError);
81 mp_qstr_map_lookup(&map_builtins, MP_QSTR_KeyError, true)->value = mp_obj_new_exception(MP_QSTR_KeyError);
82 mp_qstr_map_lookup(&map_builtins, MP_QSTR_NameError, true)->value = mp_obj_new_exception(MP_QSTR_NameError);
83 mp_qstr_map_lookup(&map_builtins, MP_QSTR_TypeError, true)->value = mp_obj_new_exception(MP_QSTR_TypeError);
84 mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, true)->value = mp_obj_new_exception(MP_QSTR_SyntaxError);
85 mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, true)->value = mp_obj_new_exception(MP_QSTR_ValueError);
Damienb86e3f92013-12-29 17:17:43 +000086
Damien Georgee9906ac2014-01-04 18:44:46 +000087 // built-in objects
88 mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, true)->value = mp_const_ellipsis;
89
Damienb86e3f92013-12-29 17:17:43 +000090 // built-in core functions
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000091 mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__);
92 mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__);
Damienb86e3f92013-12-29 17:17:43 +000093
94 // built-in user functions
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000095 mp_qstr_map_lookup(&map_builtins, MP_QSTR_abs, true)->value = rt_make_function_1(mp_builtin_abs);
96 mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, true)->value = rt_make_function_1(mp_builtin_all);
97 mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, true)->value = rt_make_function_1(mp_builtin_any);
98 mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, true)->value = rt_make_function_var(0, mp_builtin_bool);
99 mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, true)->value = rt_make_function_1(mp_builtin_callable);
100 mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, true)->value = rt_make_function_1(mp_builtin_chr);
Damien George209d1b12014-01-01 17:03:35 +0000101#if MICROPY_ENABLE_FLOAT
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000102 mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, true)->value = (mp_obj_t)&mp_builtin_complex_obj;
Damien George209d1b12014-01-01 17:03:35 +0000103#endif
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000104 mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, true)->value = rt_make_function_0(mp_builtin_dict);
105 mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, true)->value = rt_make_function_2(mp_builtin_divmod);
Damien George209d1b12014-01-01 17:03:35 +0000106#if MICROPY_ENABLE_FLOAT
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000107 mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, true)->value = (mp_obj_t)&mp_builtin_float_obj;
Damien George209d1b12014-01-01 17:03:35 +0000108#endif
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000109 mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, true)->value = (mp_obj_t)&mp_builtin_hash_obj;
110 mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, true)->value = (mp_obj_t)&mp_builtin_int_obj;
111 mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, true)->value = (mp_obj_t)&mp_builtin_iter_obj;
112 mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, true)->value = rt_make_function_1(mp_builtin_len);
113 mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, true)->value = rt_make_function_var(0, mp_builtin_list);
114 mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, true)->value = rt_make_function_var(1, mp_builtin_max);
115 mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, true)->value = rt_make_function_var(1, mp_builtin_min);
116 mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, true)->value = (mp_obj_t)&mp_builtin_next_obj;
117 mp_qstr_map_lookup(&map_builtins, MP_QSTR_ord, true)->value = rt_make_function_1(mp_builtin_ord);
118 mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, true)->value = rt_make_function_var(2, mp_builtin_pow);
119 mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, true)->value = rt_make_function_var(0, mp_builtin_print);
120 mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, true)->value = rt_make_function_var(1, mp_builtin_range);
121 mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, true)->value = (mp_obj_t)&mp_builtin_set_obj;
122 mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum);
123 mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, true)->value = (mp_obj_t)&mp_builtin_type_obj;
Damien429d7192013-10-04 19:53:11 +0100124
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000125 next_unique_code_id = 1; // 0 indicates "no code"
Damien429d7192013-10-04 19:53:11 +0100126 unique_codes = NULL;
127
Damien0446a0d2013-11-17 13:16:36 +0000128#ifdef WRITE_CODE
129 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100130#endif
Damien429d7192013-10-04 19:53:11 +0100131}
132
Damien8b3a7c22013-10-23 20:20:17 +0100133void rt_deinit(void) {
Damien0446a0d2013-11-17 13:16:36 +0000134#ifdef WRITE_CODE
135 if (fp_write_code != NULL) {
136 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100137 }
Damiena1ddfcc2013-10-10 23:25:50 +0100138#endif
Damien429d7192013-10-04 19:53:11 +0100139}
140
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000141int rt_get_unique_code_id(void) {
142 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100143}
144
Damien8b3a7c22013-10-23 20:20:17 +0100145static void alloc_unique_codes(void) {
Damien429d7192013-10-04 19:53:11 +0100146 if (unique_codes == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000147 unique_codes = m_new(mp_code_t, next_unique_code_id + 10); // XXX hack until we fix the REPL allocation problem
Damien826005c2013-10-05 23:17:28 +0100148 for (int i = 0; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000149 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100150 }
Damien429d7192013-10-04 19:53:11 +0100151 }
Damien826005c2013-10-05 23:17:28 +0100152}
153
Damien George6baf76e2013-12-30 22:32:17 +0000154void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) {
Damien826005c2013-10-05 23:17:28 +0100155 alloc_unique_codes();
156
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000157 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000158 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien826005c2013-10-05 23:17:28 +0100159 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100160 unique_codes[unique_code_id].n_locals = n_locals;
161 unique_codes[unique_code_id].n_stack = n_stack;
162 unique_codes[unique_code_id].is_generator = is_generator;
Damien826005c2013-10-05 23:17:28 +0100163 unique_codes[unique_code_id].u_byte.code = code;
164 unique_codes[unique_code_id].u_byte.len = len;
165
Damien9ecbcff2013-12-11 00:41:43 +0000166 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000167
168#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100169 DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d\n", unique_code_id, code, len, n_args);
Damien0446a0d2013-11-17 13:16:36 +0000170 for (int i = 0; i < 128 && i < len; i++) {
171 if (i > 0 && i % 16 == 0) {
172 DEBUG_printf("\n");
173 }
174 DEBUG_printf(" %02x", code[i]);
175 }
176 DEBUG_printf("\n");
Damiend99b0522013-12-21 18:17:45 +0000177 extern void mp_show_byte_code(const byte *code, int len);
178 mp_show_byte_code(code, len);
Damien0446a0d2013-11-17 13:16:36 +0000179
180#ifdef WRITE_CODE
181 if (fp_write_code != NULL) {
182 fwrite(code, len, 1, fp_write_code);
183 fflush(fp_write_code);
184 }
185#endif
186#endif
Damien826005c2013-10-05 23:17:28 +0100187}
188
Damiend99b0522013-12-21 18:17:45 +0000189void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100190 alloc_unique_codes();
191
Damienb05d7072013-10-05 13:37:10 +0100192 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000193 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien429d7192013-10-04 19:53:11 +0100194 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100195 unique_codes[unique_code_id].n_locals = 0;
196 unique_codes[unique_code_id].n_stack = 0;
197 unique_codes[unique_code_id].is_generator = false;
Damien429d7192013-10-04 19:53:11 +0100198 unique_codes[unique_code_id].u_native.fun = fun;
199
Damien George8cc96a32013-12-30 18:23:50 +0000200 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000201
Damiena1ddfcc2013-10-10 23:25:50 +0100202#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100203 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
204 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
205 for (int i = 0; i < 128 && i < len; i++) {
206 if (i > 0 && i % 16 == 0) {
207 DEBUG_printf("\n");
208 }
209 DEBUG_printf(" %02x", fun_data[i]);
210 }
211 DEBUG_printf("\n");
212
Damien0446a0d2013-11-17 13:16:36 +0000213#ifdef WRITE_CODE
214 if (fp_write_code != NULL) {
215 fwrite(fun_data, len, 1, fp_write_code);
216 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100217 }
Damiena1ddfcc2013-10-10 23:25:50 +0100218#endif
219#endif
Damien429d7192013-10-04 19:53:11 +0100220}
221
Damiend99b0522013-12-21 18:17:45 +0000222void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100223 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100224
Damien826005c2013-10-05 23:17:28 +0100225 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000226 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien826005c2013-10-05 23:17:28 +0100227 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100228 unique_codes[unique_code_id].n_locals = 0;
229 unique_codes[unique_code_id].n_stack = 0;
230 unique_codes[unique_code_id].is_generator = false;
Damien826005c2013-10-05 23:17:28 +0100231 unique_codes[unique_code_id].u_inline_asm.fun = fun;
232
Damiena1ddfcc2013-10-10 23:25:50 +0100233#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100234 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
235 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
236 for (int i = 0; i < 128 && i < len; i++) {
237 if (i > 0 && i % 16 == 0) {
238 DEBUG_printf("\n");
239 }
240 DEBUG_printf(" %02x", fun_data[i]);
241 }
242 DEBUG_printf("\n");
243
Damien0446a0d2013-11-17 13:16:36 +0000244#ifdef WRITE_CODE
245 if (fp_write_code != NULL) {
246 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100247 }
Damiena1ddfcc2013-10-10 23:25:50 +0100248#endif
249#endif
Damien429d7192013-10-04 19:53:11 +0100250}
251
Damiend99b0522013-12-21 18:17:45 +0000252static bool fit_small_int(mp_small_int_t o) {
253 return true;
254}
255
256int rt_is_true(mp_obj_t arg) {
257 DEBUG_OP_printf("is true %p\n", arg);
258 if (MP_OBJ_IS_SMALL_INT(arg)) {
259 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
260 return 0;
261 } else {
262 return 1;
263 }
264 } else if (arg == mp_const_none) {
265 return 0;
266 } else if (arg == mp_const_false) {
267 return 0;
268 } else if (arg == mp_const_true) {
269 return 1;
270 } else {
271 assert(0);
272 return 0;
273 }
274}
275
276mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
277 return mp_obj_list_append(self_in, arg);
278}
279
Damien7410e442013-11-02 19:47:57 +0000280#define PARSE_DEC_IN_INTG (1)
281#define PARSE_DEC_IN_FRAC (2)
282#define PARSE_DEC_IN_EXP (3)
283
Damiend99b0522013-12-21 18:17:45 +0000284mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000285#if MICROPY_ENABLE_FLOAT
286 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
287 const char *s = qstr_str(qstr);
288 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000289 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000290 bool exp_neg = false;
291 int exp_val = 0;
292 int exp_extra = 0;
293 bool imag = false;
294 for (; *s; s++) {
295 int dig = *s;
296 if ('0' <= dig && dig <= '9') {
297 dig -= '0';
298 if (in == PARSE_DEC_IN_EXP) {
299 exp_val = 10 * exp_val + dig;
300 } else {
301 dec_val = 10 * dec_val + dig;
302 if (in == PARSE_DEC_IN_FRAC) {
303 exp_extra -= 1;
304 }
305 }
306 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
307 in = PARSE_DEC_IN_FRAC;
308 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
309 in = PARSE_DEC_IN_EXP;
310 if (s[1] == '+') {
311 s++;
312 } else if (s[1] == '-') {
313 s++;
314 exp_neg = true;
315 }
316 } else if (dig == 'J' || dig == 'j') {
317 s++;
318 imag = true;
319 break;
320 } else {
321 // unknown character
322 break;
323 }
324 }
325 if (*s != 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000326 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000327 }
328 if (exp_neg) {
329 exp_val = -exp_val;
330 }
331 exp_val += exp_extra;
332 for (; exp_val > 0; exp_val--) {
333 dec_val *= 10;
334 }
335 for (; exp_val < 0; exp_val++) {
336 dec_val *= 0.1;
337 }
338 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000339 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000340 } else {
Damiend99b0522013-12-21 18:17:45 +0000341 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000342 }
343#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000344 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000345#endif
346}
347
Damiend99b0522013-12-21 18:17:45 +0000348mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100349 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000350 return mp_obj_new_str(qstr);
Damien429d7192013-10-04 19:53:11 +0100351}
352
Damiend99b0522013-12-21 18:17:45 +0000353mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100354 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100355 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000356 mp_map_elem_t *elem = mp_qstr_map_lookup(map_locals, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100357 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000358 elem = mp_qstr_map_lookup(map_globals, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100359 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000360 elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100361 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000362 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damiena3977762013-10-09 23:10:10 +0100363 }
364 }
365 }
366 return elem->value;
367}
368
Damiend99b0522013-12-21 18:17:45 +0000369mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100370 // logic: search globals, builtins
371 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000372 mp_map_elem_t *elem = mp_qstr_map_lookup(map_globals, qstr, false);
Damien429d7192013-10-04 19:53:11 +0100373 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000374 elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
Damien429d7192013-10-04 19:53:11 +0100375 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000376 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100377 }
378 }
379 return elem->value;
380}
381
Damiend99b0522013-12-21 18:17:45 +0000382mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100383 DEBUG_OP_printf("load_build_class\n");
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000384 mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, false);
Damien429d7192013-10-04 19:53:11 +0100385 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000386 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
Damien429d7192013-10-04 19:53:11 +0100387 }
388 return elem->value;
389}
390
Damiend99b0522013-12-21 18:17:45 +0000391mp_obj_t rt_get_cell(mp_obj_t cell) {
392 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000393}
394
Damiend99b0522013-12-21 18:17:45 +0000395void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
396 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000397}
398
Damiend99b0522013-12-21 18:17:45 +0000399void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100400 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damiend99b0522013-12-21 18:17:45 +0000401 mp_qstr_map_lookup(map_locals, qstr, true)->value = obj;
Damiena3977762013-10-09 23:10:10 +0100402}
403
Damiend99b0522013-12-21 18:17:45 +0000404void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100405 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damiend99b0522013-12-21 18:17:45 +0000406 mp_qstr_map_lookup(map_globals, qstr, true)->value = obj;
Damien429d7192013-10-04 19:53:11 +0100407}
408
Damiend99b0522013-12-21 18:17:45 +0000409mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000410 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000411 if (MP_OBJ_IS_SMALL_INT(arg)) {
412 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000413 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000414 case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
Damien7410e442013-11-02 19:47:57 +0000415 case RT_UNARY_OP_POSITIVE: break;
416 case RT_UNARY_OP_NEGATIVE: val = -val; break;
417 case RT_UNARY_OP_INVERT: val = ~val; break;
418 default: assert(0); val = 0;
419 }
420 if (fit_small_int(val)) {
Damiend99b0522013-12-21 18:17:45 +0000421 return MP_OBJ_NEW_SMALL_INT(val);
422 } else {
423 // TODO make a bignum
424 assert(0);
425 return mp_const_none;
Damien7410e442013-11-02 19:47:57 +0000426 }
Damiend99b0522013-12-21 18:17:45 +0000427 } else { // will be an object (small ints are caught in previous if)
428 mp_obj_base_t *o = arg;
429 if (o->type->unary_op != NULL) {
430 mp_obj_t result = o->type->unary_op(op, arg);
431 if (result != NULL) {
432 return result;
433 }
Damien7410e442013-11-02 19:47:57 +0000434 }
Damiend99b0522013-12-21 18:17:45 +0000435 // TODO specify in error message what the operator is
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000436 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", o->type->name));
Damien7410e442013-11-02 19:47:57 +0000437 }
Damien429d7192013-10-04 19:53:11 +0100438}
439
Damiend99b0522013-12-21 18:17:45 +0000440mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100441 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000442
443 // TODO correctly distinguish inplace operators for mutable objects
444 // lookup logic that CPython uses for +=:
445 // check for implemented +=
446 // then check for implemented +
447 // then check for implemented seq.inplace_concat
448 // then check for implemented seq.concat
449 // then fail
450 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
451
Damiend99b0522013-12-21 18:17:45 +0000452 if (MP_OBJ_IS_SMALL_INT(lhs) && MP_OBJ_IS_SMALL_INT(rhs)) {
453 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
454 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien429d7192013-10-04 19:53:11 +0100455 switch (op) {
Damien7b2d3f32013-10-22 16:53:02 +0100456 case RT_BINARY_OP_OR:
Damien7410e442013-11-02 19:47:57 +0000457 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100458 case RT_BINARY_OP_XOR:
Damien7410e442013-11-02 19:47:57 +0000459 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100460 case RT_BINARY_OP_AND:
Damien7410e442013-11-02 19:47:57 +0000461 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100462 case RT_BINARY_OP_LSHIFT:
Damien7410e442013-11-02 19:47:57 +0000463 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100464 case RT_BINARY_OP_RSHIFT:
Damien7410e442013-11-02 19:47:57 +0000465 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
Damien429d7192013-10-04 19:53:11 +0100466 case RT_BINARY_OP_ADD:
Damien7410e442013-11-02 19:47:57 +0000467 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
Damienbd254452013-10-16 20:39:12 +0100468 case RT_BINARY_OP_SUBTRACT:
Damien7410e442013-11-02 19:47:57 +0000469 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100470 case RT_BINARY_OP_MULTIPLY:
Damien7410e442013-11-02 19:47:57 +0000471 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100472 case RT_BINARY_OP_FLOOR_DIVIDE:
Damien7410e442013-11-02 19:47:57 +0000473 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
Damien3ef4abb2013-10-12 16:53:13 +0100474#if MICROPY_ENABLE_FLOAT
Damien7b2d3f32013-10-22 16:53:02 +0100475 case RT_BINARY_OP_TRUE_DIVIDE:
Damiend99b0522013-12-21 18:17:45 +0000476 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien429d7192013-10-04 19:53:11 +0100477#endif
Damiena3dcd9e2013-12-17 21:35:38 +0000478
479 // TODO implement modulo as specified by Python
480 case RT_BINARY_OP_MODULO:
481 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
482
483 // TODO check for negative power, and overflow
Damien4ebb32f2013-11-02 14:33:10 +0000484 case RT_BINARY_OP_POWER:
485 case RT_BINARY_OP_INPLACE_POWER:
Damiena3dcd9e2013-12-17 21:35:38 +0000486 {
487 int ans = 1;
488 while (rhs_val > 0) {
489 if (rhs_val & 1) {
490 ans *= lhs_val;
491 }
492 lhs_val *= lhs_val;
493 rhs_val /= 2;
Damien4ebb32f2013-11-02 14:33:10 +0000494 }
Damiena3dcd9e2013-12-17 21:35:38 +0000495 lhs_val = ans;
496 break;
497 }
498
Damien George8cc96a32013-12-30 18:23:50 +0000499 default: assert(0);
Damien429d7192013-10-04 19:53:11 +0100500 }
Damien7410e442013-11-02 19:47:57 +0000501 if (fit_small_int(lhs_val)) {
Damiend99b0522013-12-21 18:17:45 +0000502 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien429d7192013-10-04 19:53:11 +0100503 }
Damiend99b0522013-12-21 18:17:45 +0000504 } else if (MP_OBJ_IS_OBJ(lhs)) {
505 mp_obj_base_t *o = lhs;
506 if (o->type->binary_op != NULL) {
507 mp_obj_t result = o->type->binary_op(op, lhs, rhs);
508 if (result != NULL) {
509 return result;
Damien7410e442013-11-02 19:47:57 +0000510 }
Damien7410e442013-11-02 19:47:57 +0000511 }
Damien429d7192013-10-04 19:53:11 +0100512 }
Damiend99b0522013-12-21 18:17:45 +0000513
514 // TODO specify in error message what the operator is
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000515 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "unsupported operand type for binary operator: '%s'", mp_obj_get_type_str(lhs)));
Damien429d7192013-10-04 19:53:11 +0100516}
517
Damiend99b0522013-12-21 18:17:45 +0000518mp_obj_t rt_compare_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100519 DEBUG_OP_printf("compare %d %p %p\n", op, lhs, rhs);
Damien7b2d3f32013-10-22 16:53:02 +0100520
521 // deal with == and !=
522 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000523 if (mp_obj_equal(lhs, rhs)) {
Damien7b2d3f32013-10-22 16:53:02 +0100524 if (op == RT_COMPARE_OP_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000525 return mp_const_true;
Damien7b2d3f32013-10-22 16:53:02 +0100526 } else {
Damiend99b0522013-12-21 18:17:45 +0000527 return mp_const_false;
Damien7b2d3f32013-10-22 16:53:02 +0100528 }
529 } else {
530 if (op == RT_COMPARE_OP_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000531 return mp_const_false;
Damien7b2d3f32013-10-22 16:53:02 +0100532 } else {
Damiend99b0522013-12-21 18:17:45 +0000533 return mp_const_true;
Damien7b2d3f32013-10-22 16:53:02 +0100534 }
535 }
536 }
537
Damienb86e3f92013-12-29 17:17:43 +0000538 // deal with exception_match
539 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
540 // TODO properly! at the moment it just compares the exception identifier for equality
541 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
542 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
543 return mp_const_true;
544 } else {
545 return mp_const_false;
546 }
547 }
548 }
549
Damien7b2d3f32013-10-22 16:53:02 +0100550 // deal with small ints
Damiend99b0522013-12-21 18:17:45 +0000551 if (MP_OBJ_IS_SMALL_INT(lhs) && MP_OBJ_IS_SMALL_INT(rhs)) {
552 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
553 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien429d7192013-10-04 19:53:11 +0100554 int cmp;
555 switch (op) {
Damien4ebb32f2013-11-02 14:33:10 +0000556 case RT_COMPARE_OP_LESS: cmp = lhs_val < rhs_val; break;
557 case RT_COMPARE_OP_MORE: cmp = lhs_val > rhs_val; break;
558 case RT_COMPARE_OP_LESS_EQUAL: cmp = lhs_val <= rhs_val; break;
559 case RT_COMPARE_OP_MORE_EQUAL: cmp = lhs_val >= rhs_val; break;
Damien429d7192013-10-04 19:53:11 +0100560 default: assert(0); cmp = 0;
561 }
562 if (cmp) {
Damiend99b0522013-12-21 18:17:45 +0000563 return mp_const_true;
Damien429d7192013-10-04 19:53:11 +0100564 } else {
Damiend99b0522013-12-21 18:17:45 +0000565 return mp_const_false;
Damien429d7192013-10-04 19:53:11 +0100566 }
567 }
Damien7b2d3f32013-10-22 16:53:02 +0100568
Damien4ebb32f2013-11-02 14:33:10 +0000569#if MICROPY_ENABLE_FLOAT
570 // deal with floats
Damiend99b0522013-12-21 18:17:45 +0000571 if (MP_OBJ_IS_TYPE(lhs, &float_type) || MP_OBJ_IS_TYPE(rhs, &float_type)) {
572 mp_float_t lhs_val = mp_obj_get_float(lhs);
573 mp_float_t rhs_val = mp_obj_get_float(rhs);
Damien4ebb32f2013-11-02 14:33:10 +0000574 int cmp;
575 switch (op) {
576 case RT_COMPARE_OP_LESS: cmp = lhs_val < rhs_val; break;
577 case RT_COMPARE_OP_MORE: cmp = lhs_val > rhs_val; break;
578 case RT_COMPARE_OP_LESS_EQUAL: cmp = lhs_val <= rhs_val; break;
579 case RT_COMPARE_OP_MORE_EQUAL: cmp = lhs_val >= rhs_val; break;
580 default: assert(0); cmp = 0;
581 }
582 if (cmp) {
Damiend99b0522013-12-21 18:17:45 +0000583 return mp_const_true;
Damien4ebb32f2013-11-02 14:33:10 +0000584 } else {
Damiend99b0522013-12-21 18:17:45 +0000585 return mp_const_false;
Damien4ebb32f2013-11-02 14:33:10 +0000586 }
587 }
588#endif
589
Damien7b2d3f32013-10-22 16:53:02 +0100590 // not implemented
Damien429d7192013-10-04 19:53:11 +0100591 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000592 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100593}
594
Damiend99b0522013-12-21 18:17:45 +0000595mp_obj_t rt_make_function_from_id(int unique_code_id) {
Damienb05d7072013-10-05 13:37:10 +0100596 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
597 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100598 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000599 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100600 }
Damiend99b0522013-12-21 18:17:45 +0000601
602 // make the function, depending on the code kind
603 mp_code_t *c = &unique_codes[unique_code_id];
604 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100605 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000606 case MP_CODE_BYTE:
Damien George6baf76e2013-12-30 22:32:17 +0000607 fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_stack, c->u_byte.code);
Damien826005c2013-10-05 23:17:28 +0100608 break;
Damiend99b0522013-12-21 18:17:45 +0000609 case MP_CODE_NATIVE:
Damien429d7192013-10-04 19:53:11 +0100610 switch (c->n_args) {
Damiend99b0522013-12-21 18:17:45 +0000611 case 0: fun = rt_make_function_0(c->u_native.fun); break;
612 case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break;
613 case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break;
614 default: assert(0); fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100615 }
Damien429d7192013-10-04 19:53:11 +0100616 break;
Damiend99b0522013-12-21 18:17:45 +0000617 case MP_CODE_INLINE_ASM:
618 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100619 break;
620 default:
621 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000622 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100623 }
Damienbd254452013-10-16 20:39:12 +0100624
625 // check for generator functions and if so wrap in generator object
626 if (c->is_generator) {
Damien George6baf76e2013-12-30 22:32:17 +0000627 fun = mp_obj_new_gen_wrap(c->n_locals, c->n_stack, fun);
Damienbd254452013-10-16 20:39:12 +0100628 }
629
Damiend99b0522013-12-21 18:17:45 +0000630 return fun;
Damien429d7192013-10-04 19:53:11 +0100631}
632
Damiend99b0522013-12-21 18:17:45 +0000633mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000634 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000635 // make function object
636 mp_obj_t ffun = rt_make_function_from_id(unique_code_id);
Damien9ecbcff2013-12-11 00:41:43 +0000637 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000638 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000639}
640
Damiend99b0522013-12-21 18:17:45 +0000641mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damieneb19efb2013-10-10 22:06:54 +0100642 return rt_call_function_n(fun, 0, NULL);
643}
644
Damiend99b0522013-12-21 18:17:45 +0000645mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damieneb19efb2013-10-10 22:06:54 +0100646 return rt_call_function_n(fun, 1, &arg);
647}
648
Damiend99b0522013-12-21 18:17:45 +0000649mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
650 mp_obj_t args[2];
Damieneb19efb2013-10-10 22:06:54 +0100651 args[1] = arg1;
652 args[0] = arg2;
653 return rt_call_function_n(fun, 2, args);
654}
655
Damieneb19efb2013-10-10 22:06:54 +0100656// args are in reverse order in the array
Damiend99b0522013-12-21 18:17:45 +0000657mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
658 // TODO improve this: fun object can specify its type and we parse here the arguments,
659 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100660
Damiend99b0522013-12-21 18:17:45 +0000661 DEBUG_OP_printf("calling function %p(n_args=%d, args=%p)\n", fun_in, n_args, args);
Damieneb19efb2013-10-10 22:06:54 +0100662
Damiend99b0522013-12-21 18:17:45 +0000663 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000664 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
Damien429d7192013-10-04 19:53:11 +0100665 } else {
Damiend99b0522013-12-21 18:17:45 +0000666 mp_obj_base_t *fun = fun_in;
667 if (fun->type->call_n != NULL) {
668 return fun->type->call_n(fun_in, n_args, args);
669 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000670 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name));
Damiend99b0522013-12-21 18:17:45 +0000671 }
Damien429d7192013-10-04 19:53:11 +0100672 }
Damien429d7192013-10-04 19:53:11 +0100673}
674
Damien86c7fc72013-11-26 15:16:41 +0000675// args are in reverse order in the array; keyword arguments come first, value then key
676// eg: (value1, key1, value0, key0, arg1, arg0)
Damiend99b0522013-12-21 18:17:45 +0000677mp_obj_t rt_call_function_n_kw(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000678 // TODO
679 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000680 return mp_const_none;
Damien86c7fc72013-11-26 15:16:41 +0000681}
682
Damiena3977762013-10-09 23:10:10 +0100683// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
684// if n_args==0 then there are only self/NULL and fun
Damiend99b0522013-12-21 18:17:45 +0000685mp_obj_t rt_call_method_n(uint n_args, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000686 DEBUG_OP_printf("call method %p(self=%p, n_args=%u)\n", args[n_args + 1], args[n_args], n_args);
Damiena3977762013-10-09 23:10:10 +0100687 return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
688}
689
Damien86c7fc72013-11-26 15:16:41 +0000690// args contains: kw_val(n_kw-1) kw_key(n_kw-1) ... kw_val(0) kw_key(0) arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
Damiend99b0522013-12-21 18:17:45 +0000691mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000692 uint n = n_args + 2 * n_kw;
693 DEBUG_OP_printf("call method %p(self=%p, n_args=%u, n_kw=%u)\n", args[n + 1], args[n], n_args, n_kw);
694 return rt_call_function_n_kw(args[n + 1], n_args + ((args[n] == NULL) ? 0 : 1), n_kw, args);
695}
696
Damien429d7192013-10-04 19:53:11 +0100697// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000698mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
699 return mp_obj_new_tuple_reverse(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100700}
701
702// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000703mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
704 return mp_obj_new_list_reverse(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100705}
706
Damiend99b0522013-12-21 18:17:45 +0000707mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
708 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100709}
710
Damiend99b0522013-12-21 18:17:45 +0000711mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000712 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100713 return set;
714}
715
Damien86c7fc72013-11-26 15:16:41 +0000716// unpacked items are stored in order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000717void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
718 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
719 uint seq_len;
720 mp_obj_t *seq_items;
721 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
722 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
723 } else {
724 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000725 }
Damiend99b0522013-12-21 18:17:45 +0000726 if (seq_len < num) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000727 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_ValueError, "need more than %d values to unpack", (void*)(machine_uint_t)seq_len));
Damiend99b0522013-12-21 18:17:45 +0000728 } else if (seq_len > num) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000729 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_ValueError, "too many values to unpack (expected %d)", (void*)(machine_uint_t)num));
Damiend99b0522013-12-21 18:17:45 +0000730 }
731 memcpy(items, seq_items, num * sizeof(mp_obj_t));
Damien86c7fc72013-11-26 15:16:41 +0000732 } else {
733 // TODO call rt_getiter and extract via rt_iternext
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000734 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(seq_in)));
Damien86c7fc72013-11-26 15:16:41 +0000735 }
736}
737
Damiend99b0522013-12-21 18:17:45 +0000738mp_obj_t rt_build_map(int n_args) {
739 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100740}
741
Damiend99b0522013-12-21 18:17:45 +0000742mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
743 // map should always be a dict
744 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100745}
746
Damiend99b0522013-12-21 18:17:45 +0000747mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damiena3977762013-10-09 23:10:10 +0100748 DEBUG_OP_printf("load attr %s\n", qstr_str(attr));
Damiend99b0522013-12-21 18:17:45 +0000749 if (MP_OBJ_IS_TYPE(base, &class_type)) {
750 mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, false);
Damiena3977762013-10-09 23:10:10 +0100751 if (elem == NULL) {
Damien George28708622014-01-02 21:30:26 +0000752 // TODO what about generic method lookup?
753 goto no_attr;
Damiena3977762013-10-09 23:10:10 +0100754 }
755 return elem->value;
Damiend99b0522013-12-21 18:17:45 +0000756 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
757 return mp_obj_instance_load_attr(base, attr);
Damien George28708622014-01-02 21:30:26 +0000758 } else if (MP_OBJ_IS_TYPE(base, &module_type)) {
Damien George66028ab2014-01-03 14:03:48 +0000759 DEBUG_OP_printf("lookup module map %p\n", mp_obj_module_get_globals(base));
Damien George28708622014-01-02 21:30:26 +0000760 mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_module_get_globals(base), attr, false);
761 if (elem == NULL) {
762 // TODO what about generic method lookup?
763 goto no_attr;
764 }
765 return elem->value;
Damiend99b0522013-12-21 18:17:45 +0000766 } else if (MP_OBJ_IS_OBJ(base)) {
767 // generic method lookup
768 mp_obj_base_t *o = base;
769 const mp_method_t *meth = &o->type->methods[0];
Damiend57eba52013-11-02 23:58:14 +0000770 for (; meth->name != NULL; meth++) {
771 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damiend99b0522013-12-21 18:17:45 +0000772 return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
773 }
774 }
775 }
Damien George28708622014-01-02 21:30:26 +0000776
777no_attr:
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000778 nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiend99b0522013-12-21 18:17:45 +0000779}
780
781void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
782 DEBUG_OP_printf("load method %s\n", qstr_str(attr));
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000783 if (MP_OBJ_IS_TYPE(base, &gen_instance_type) && attr == MP_QSTR___next__) {
Damiend9d62012013-12-21 18:38:03 +0000784 dest[1] = (mp_obj_t)&mp_builtin_next_obj;
Damiend99b0522013-12-21 18:17:45 +0000785 dest[0] = base;
786 return;
787 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
788 mp_obj_instance_load_method(base, attr, dest);
789 return;
790 } else if (MP_OBJ_IS_OBJ(base)) {
791 // generic method lookup
792 mp_obj_base_t *o = base;
793 const mp_method_t *meth = &o->type->methods[0];
794 for (; meth->name != NULL; meth++) {
795 if (strcmp(meth->name, qstr_str(attr)) == 0) {
796 dest[1] = (mp_obj_t)meth->fun;
Damiend57eba52013-11-02 23:58:14 +0000797 dest[0] = base;
798 return;
799 }
800 }
Damiena3977762013-10-09 23:10:10 +0100801 }
802
Damiend99b0522013-12-21 18:17:45 +0000803 // no method; fallback to load_attr
Damiena3977762013-10-09 23:10:10 +0100804 dest[1] = rt_load_attr(base, attr);
805 dest[0] = NULL;
806}
807
Damiend99b0522013-12-21 18:17:45 +0000808void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100809 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damiend99b0522013-12-21 18:17:45 +0000810 if (MP_OBJ_IS_TYPE(base, &class_type)) {
Damienec63cce2013-10-22 22:58:17 +0100811 // TODO CPython allows STORE_ATTR to a class, but is this the correct implementation?
Damiend99b0522013-12-21 18:17:45 +0000812 mp_map_t *locals = mp_obj_class_get_locals(base);
813 mp_qstr_map_lookup(locals, attr, true)->value = value;
814 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
815 mp_obj_instance_store_attr(base, attr, value);
Damien George28708622014-01-02 21:30:26 +0000816 } else if (MP_OBJ_IS_TYPE(base, &module_type)) {
817 // TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
818 mp_map_t *globals = mp_obj_module_get_globals(base);
819 mp_qstr_map_lookup(globals, attr, true)->value = value;
Damiena3977762013-10-09 23:10:10 +0100820 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000821 nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100822 }
823}
824
Damiend99b0522013-12-21 18:17:45 +0000825void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100826 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000827 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100828 // list store
Damiend99b0522013-12-21 18:17:45 +0000829 mp_obj_list_store(base, index, value);
830 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
831 // dict store
832 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100833 } else {
834 assert(0);
835 }
836}
837
Damiend99b0522013-12-21 18:17:45 +0000838mp_obj_t rt_getiter(mp_obj_t o_in) {
839 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000840 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100841 } else {
Damiend99b0522013-12-21 18:17:45 +0000842 mp_obj_base_t *o = o_in;
843 if (o->type->getiter != NULL) {
844 return o->type->getiter(o_in);
845 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000846 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not iterable", o->type->name));
Damiend99b0522013-12-21 18:17:45 +0000847 }
Damience89a212013-10-15 22:25:17 +0100848 }
849}
850
Damiend99b0522013-12-21 18:17:45 +0000851mp_obj_t rt_iternext(mp_obj_t o_in) {
852 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000853 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "? 'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100854 } else {
Damiend99b0522013-12-21 18:17:45 +0000855 mp_obj_base_t *o = o_in;
856 if (o->type->iternext != NULL) {
857 return o->type->iternext(o_in);
858 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000859 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "? '%s' object is not iterable", o->type->name));
Damiend99b0522013-12-21 18:17:45 +0000860 }
Damience89a212013-10-15 22:25:17 +0100861 }
862}
863
Damiend99b0522013-12-21 18:17:45 +0000864mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000865 // build args array
Damiend99b0522013-12-21 18:17:45 +0000866 mp_obj_t args[5];
867 args[0] = mp_obj_new_str(name);
868 args[1] = mp_const_none; // TODO should be globals
869 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000870 args[3] = fromlist;
871 args[4] = level; // must be 0; we don't yet support other values
872
873 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000874 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000875}
876
Damiend99b0522013-12-21 18:17:45 +0000877mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
878 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000879 /* TODO convert AttributeError to ImportError
880 if (fail) {
881 (ImportError, "cannot import name %s", qstr_str(name), NULL)
882 }
883 */
884 return x;
885}
886
Damien George66028ab2014-01-03 14:03:48 +0000887mp_map_t *rt_locals_get(void) {
888 return map_locals;
889}
890
891void rt_locals_set(mp_map_t *m) {
892 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
893 map_locals = m;
894}
895
896mp_map_t *rt_globals_get(void) {
897 return map_globals;
898}
899
900void rt_globals_set(mp_map_t *m) {
901 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
902 map_globals = m;
903}
904
Damien6ba13142013-11-02 20:34:54 +0000905// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100906void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000907 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100908 rt_load_const_str,
909 rt_load_name,
910 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100911 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +0100912 rt_load_attr,
913 rt_load_method,
914 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +0100915 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +0100916 rt_store_subscr,
917 rt_is_true,
918 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +0100919 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +0100920 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +0100921 rt_list_append,
Damien429d7192013-10-04 19:53:11 +0100922 rt_build_map,
923 rt_store_map,
924 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +0100925 rt_store_set,
Damien429d7192013-10-04 19:53:11 +0100926 rt_make_function_from_id,
Damieneb19efb2013-10-10 22:06:54 +0100927 rt_call_function_n,
Damien7f5dacf2013-10-10 11:24:39 +0100928 rt_call_method_n,
Damien429d7192013-10-04 19:53:11 +0100929 rt_binary_op,
930 rt_compare_op,
Damiend2755ec2013-10-16 23:58:48 +0100931 rt_getiter,
932 rt_iternext,
Damien429d7192013-10-04 19:53:11 +0100933};
934
935/*
936void rt_f_vector(rt_fun_kind_t fun_kind) {
937 (rt_f_table[fun_kind])();
938}
939*/