blob: 6bc71abff7ea742fa3199e1db6f7b02655785220 [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
Damien George71c51812014-01-04 20:21:15 +000094 // built-in types
95 mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, true)->value = (mp_obj_t)&bool_type;
96#if MICROPY_ENABLE_FLOAT
97 mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, true)->value = (mp_obj_t)&complex_type;
98#endif
99 mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, true)->value = (mp_obj_t)&dict_type;
100#if MICROPY_ENABLE_FLOAT
101 mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, true)->value = (mp_obj_t)&float_type;
102#endif
103 mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, true)->value = (mp_obj_t)&int_type;
104 mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, true)->value = (mp_obj_t)&list_type;
105 mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, true)->value = (mp_obj_t)&set_type;
106 mp_qstr_map_lookup(&map_builtins, MP_QSTR_tuple, true)->value = (mp_obj_t)&tuple_type;
107 mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, true)->value = (mp_obj_t)&mp_builtin_type_obj; // TODO
108
109 // built-in user functions; TODO covert all to &mp_builtin_xxx's
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000110 mp_qstr_map_lookup(&map_builtins, MP_QSTR_abs, true)->value = rt_make_function_1(mp_builtin_abs);
111 mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, true)->value = rt_make_function_1(mp_builtin_all);
112 mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, true)->value = rt_make_function_1(mp_builtin_any);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000113 mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, true)->value = rt_make_function_1(mp_builtin_callable);
114 mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, true)->value = rt_make_function_1(mp_builtin_chr);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000115 mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, true)->value = rt_make_function_2(mp_builtin_divmod);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000116 mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, true)->value = (mp_obj_t)&mp_builtin_hash_obj;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000117 mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, true)->value = (mp_obj_t)&mp_builtin_iter_obj;
118 mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, true)->value = rt_make_function_1(mp_builtin_len);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000119 mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, true)->value = rt_make_function_var(1, mp_builtin_max);
120 mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, true)->value = rt_make_function_var(1, mp_builtin_min);
121 mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, true)->value = (mp_obj_t)&mp_builtin_next_obj;
122 mp_qstr_map_lookup(&map_builtins, MP_QSTR_ord, true)->value = rt_make_function_1(mp_builtin_ord);
123 mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, true)->value = rt_make_function_var(2, mp_builtin_pow);
124 mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, true)->value = rt_make_function_var(0, mp_builtin_print);
125 mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, true)->value = rt_make_function_var(1, mp_builtin_range);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000126 mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum);
Damien429d7192013-10-04 19:53:11 +0100127
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000128 next_unique_code_id = 1; // 0 indicates "no code"
Damien429d7192013-10-04 19:53:11 +0100129 unique_codes = NULL;
130
Damien0446a0d2013-11-17 13:16:36 +0000131#ifdef WRITE_CODE
132 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100133#endif
Damien429d7192013-10-04 19:53:11 +0100134}
135
Damien8b3a7c22013-10-23 20:20:17 +0100136void rt_deinit(void) {
Damien0446a0d2013-11-17 13:16:36 +0000137#ifdef WRITE_CODE
138 if (fp_write_code != NULL) {
139 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100140 }
Damiena1ddfcc2013-10-10 23:25:50 +0100141#endif
Damien429d7192013-10-04 19:53:11 +0100142}
143
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000144int rt_get_unique_code_id(void) {
145 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100146}
147
Damien8b3a7c22013-10-23 20:20:17 +0100148static void alloc_unique_codes(void) {
Damien429d7192013-10-04 19:53:11 +0100149 if (unique_codes == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000150 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 +0100151 for (int i = 0; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000152 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100153 }
Damien429d7192013-10-04 19:53:11 +0100154 }
Damien826005c2013-10-05 23:17:28 +0100155}
156
Damien George6baf76e2013-12-30 22:32:17 +0000157void 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 +0100158 alloc_unique_codes();
159
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000160 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000161 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien826005c2013-10-05 23:17:28 +0100162 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100163 unique_codes[unique_code_id].n_locals = n_locals;
164 unique_codes[unique_code_id].n_stack = n_stack;
165 unique_codes[unique_code_id].is_generator = is_generator;
Damien826005c2013-10-05 23:17:28 +0100166 unique_codes[unique_code_id].u_byte.code = code;
167 unique_codes[unique_code_id].u_byte.len = len;
168
Damien9ecbcff2013-12-11 00:41:43 +0000169 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000170
171#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100172 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 +0000173 for (int i = 0; i < 128 && i < len; i++) {
174 if (i > 0 && i % 16 == 0) {
175 DEBUG_printf("\n");
176 }
177 DEBUG_printf(" %02x", code[i]);
178 }
179 DEBUG_printf("\n");
Damiend99b0522013-12-21 18:17:45 +0000180 extern void mp_show_byte_code(const byte *code, int len);
181 mp_show_byte_code(code, len);
Damien0446a0d2013-11-17 13:16:36 +0000182
183#ifdef WRITE_CODE
184 if (fp_write_code != NULL) {
185 fwrite(code, len, 1, fp_write_code);
186 fflush(fp_write_code);
187 }
188#endif
189#endif
Damien826005c2013-10-05 23:17:28 +0100190}
191
Damiend99b0522013-12-21 18:17:45 +0000192void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100193 alloc_unique_codes();
194
Damienb05d7072013-10-05 13:37:10 +0100195 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000196 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien429d7192013-10-04 19:53:11 +0100197 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100198 unique_codes[unique_code_id].n_locals = 0;
199 unique_codes[unique_code_id].n_stack = 0;
200 unique_codes[unique_code_id].is_generator = false;
Damien429d7192013-10-04 19:53:11 +0100201 unique_codes[unique_code_id].u_native.fun = fun;
202
Damien George8cc96a32013-12-30 18:23:50 +0000203 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000204
Damiena1ddfcc2013-10-10 23:25:50 +0100205#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100206 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
207 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
208 for (int i = 0; i < 128 && i < len; i++) {
209 if (i > 0 && i % 16 == 0) {
210 DEBUG_printf("\n");
211 }
212 DEBUG_printf(" %02x", fun_data[i]);
213 }
214 DEBUG_printf("\n");
215
Damien0446a0d2013-11-17 13:16:36 +0000216#ifdef WRITE_CODE
217 if (fp_write_code != NULL) {
218 fwrite(fun_data, len, 1, fp_write_code);
219 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100220 }
Damiena1ddfcc2013-10-10 23:25:50 +0100221#endif
222#endif
Damien429d7192013-10-04 19:53:11 +0100223}
224
Damiend99b0522013-12-21 18:17:45 +0000225void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100226 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100227
Damien826005c2013-10-05 23:17:28 +0100228 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000229 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien826005c2013-10-05 23:17:28 +0100230 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100231 unique_codes[unique_code_id].n_locals = 0;
232 unique_codes[unique_code_id].n_stack = 0;
233 unique_codes[unique_code_id].is_generator = false;
Damien826005c2013-10-05 23:17:28 +0100234 unique_codes[unique_code_id].u_inline_asm.fun = fun;
235
Damiena1ddfcc2013-10-10 23:25:50 +0100236#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100237 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
238 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
239 for (int i = 0; i < 128 && i < len; i++) {
240 if (i > 0 && i % 16 == 0) {
241 DEBUG_printf("\n");
242 }
243 DEBUG_printf(" %02x", fun_data[i]);
244 }
245 DEBUG_printf("\n");
246
Damien0446a0d2013-11-17 13:16:36 +0000247#ifdef WRITE_CODE
248 if (fp_write_code != NULL) {
249 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100250 }
Damiena1ddfcc2013-10-10 23:25:50 +0100251#endif
252#endif
Damien429d7192013-10-04 19:53:11 +0100253}
254
Damiend99b0522013-12-21 18:17:45 +0000255static bool fit_small_int(mp_small_int_t o) {
256 return true;
257}
258
259int rt_is_true(mp_obj_t arg) {
260 DEBUG_OP_printf("is true %p\n", arg);
261 if (MP_OBJ_IS_SMALL_INT(arg)) {
262 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
263 return 0;
264 } else {
265 return 1;
266 }
267 } else if (arg == mp_const_none) {
268 return 0;
269 } else if (arg == mp_const_false) {
270 return 0;
271 } else if (arg == mp_const_true) {
272 return 1;
273 } else {
274 assert(0);
275 return 0;
276 }
277}
278
279mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
280 return mp_obj_list_append(self_in, arg);
281}
282
Damien7410e442013-11-02 19:47:57 +0000283#define PARSE_DEC_IN_INTG (1)
284#define PARSE_DEC_IN_FRAC (2)
285#define PARSE_DEC_IN_EXP (3)
286
Damiend99b0522013-12-21 18:17:45 +0000287mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000288#if MICROPY_ENABLE_FLOAT
289 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
290 const char *s = qstr_str(qstr);
291 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000292 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000293 bool exp_neg = false;
294 int exp_val = 0;
295 int exp_extra = 0;
296 bool imag = false;
297 for (; *s; s++) {
298 int dig = *s;
299 if ('0' <= dig && dig <= '9') {
300 dig -= '0';
301 if (in == PARSE_DEC_IN_EXP) {
302 exp_val = 10 * exp_val + dig;
303 } else {
304 dec_val = 10 * dec_val + dig;
305 if (in == PARSE_DEC_IN_FRAC) {
306 exp_extra -= 1;
307 }
308 }
309 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
310 in = PARSE_DEC_IN_FRAC;
311 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
312 in = PARSE_DEC_IN_EXP;
313 if (s[1] == '+') {
314 s++;
315 } else if (s[1] == '-') {
316 s++;
317 exp_neg = true;
318 }
319 } else if (dig == 'J' || dig == 'j') {
320 s++;
321 imag = true;
322 break;
323 } else {
324 // unknown character
325 break;
326 }
327 }
328 if (*s != 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000329 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000330 }
331 if (exp_neg) {
332 exp_val = -exp_val;
333 }
334 exp_val += exp_extra;
335 for (; exp_val > 0; exp_val--) {
336 dec_val *= 10;
337 }
338 for (; exp_val < 0; exp_val++) {
339 dec_val *= 0.1;
340 }
341 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000342 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000343 } else {
Damiend99b0522013-12-21 18:17:45 +0000344 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000345 }
346#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000347 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000348#endif
349}
350
Damiend99b0522013-12-21 18:17:45 +0000351mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100352 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000353 return mp_obj_new_str(qstr);
Damien429d7192013-10-04 19:53:11 +0100354}
355
Damiend99b0522013-12-21 18:17:45 +0000356mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100357 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100358 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000359 mp_map_elem_t *elem = mp_qstr_map_lookup(map_locals, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100360 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000361 elem = mp_qstr_map_lookup(map_globals, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100362 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000363 elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100364 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000365 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 +0100366 }
367 }
368 }
369 return elem->value;
370}
371
Damiend99b0522013-12-21 18:17:45 +0000372mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100373 // logic: search globals, builtins
374 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000375 mp_map_elem_t *elem = mp_qstr_map_lookup(map_globals, qstr, false);
Damien429d7192013-10-04 19:53:11 +0100376 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000377 elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
Damien429d7192013-10-04 19:53:11 +0100378 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000379 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 +0100380 }
381 }
382 return elem->value;
383}
384
Damiend99b0522013-12-21 18:17:45 +0000385mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100386 DEBUG_OP_printf("load_build_class\n");
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000387 mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, false);
Damien429d7192013-10-04 19:53:11 +0100388 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000389 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
Damien429d7192013-10-04 19:53:11 +0100390 }
391 return elem->value;
392}
393
Damiend99b0522013-12-21 18:17:45 +0000394mp_obj_t rt_get_cell(mp_obj_t cell) {
395 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000396}
397
Damiend99b0522013-12-21 18:17:45 +0000398void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
399 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000400}
401
Damiend99b0522013-12-21 18:17:45 +0000402void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100403 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damiend99b0522013-12-21 18:17:45 +0000404 mp_qstr_map_lookup(map_locals, qstr, true)->value = obj;
Damiena3977762013-10-09 23:10:10 +0100405}
406
Damiend99b0522013-12-21 18:17:45 +0000407void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100408 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damiend99b0522013-12-21 18:17:45 +0000409 mp_qstr_map_lookup(map_globals, qstr, true)->value = obj;
Damien429d7192013-10-04 19:53:11 +0100410}
411
Damiend99b0522013-12-21 18:17:45 +0000412mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000413 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000414 if (MP_OBJ_IS_SMALL_INT(arg)) {
415 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000416 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000417 case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
Damien7410e442013-11-02 19:47:57 +0000418 case RT_UNARY_OP_POSITIVE: break;
419 case RT_UNARY_OP_NEGATIVE: val = -val; break;
420 case RT_UNARY_OP_INVERT: val = ~val; break;
421 default: assert(0); val = 0;
422 }
423 if (fit_small_int(val)) {
Damiend99b0522013-12-21 18:17:45 +0000424 return MP_OBJ_NEW_SMALL_INT(val);
425 } else {
426 // TODO make a bignum
427 assert(0);
428 return mp_const_none;
Damien7410e442013-11-02 19:47:57 +0000429 }
Damiend99b0522013-12-21 18:17:45 +0000430 } else { // will be an object (small ints are caught in previous if)
431 mp_obj_base_t *o = arg;
432 if (o->type->unary_op != NULL) {
433 mp_obj_t result = o->type->unary_op(op, arg);
434 if (result != NULL) {
435 return result;
436 }
Damien7410e442013-11-02 19:47:57 +0000437 }
Damiend99b0522013-12-21 18:17:45 +0000438 // TODO specify in error message what the operator is
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000439 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 +0000440 }
Damien429d7192013-10-04 19:53:11 +0100441}
442
Damiend99b0522013-12-21 18:17:45 +0000443mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100444 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000445
446 // TODO correctly distinguish inplace operators for mutable objects
447 // lookup logic that CPython uses for +=:
448 // check for implemented +=
449 // then check for implemented +
450 // then check for implemented seq.inplace_concat
451 // then check for implemented seq.concat
452 // then fail
453 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
454
Damien Georgee2e3d112014-01-06 22:13:00 +0000455 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000456 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien Georgee2e3d112014-01-06 22:13:00 +0000457 if (MP_OBJ_IS_SMALL_INT(rhs)) {
458 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
459 switch (op) {
460 case RT_BINARY_OP_OR:
461 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
462 case RT_BINARY_OP_XOR:
463 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
464 case RT_BINARY_OP_AND:
465 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
466 case RT_BINARY_OP_LSHIFT:
467 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
468 case RT_BINARY_OP_RSHIFT:
469 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
470 case RT_BINARY_OP_ADD:
471 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
472 case RT_BINARY_OP_SUBTRACT:
473 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
474 case RT_BINARY_OP_MULTIPLY:
475 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
476 case RT_BINARY_OP_FLOOR_DIVIDE:
477 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
478 #if MICROPY_ENABLE_FLOAT
479 case RT_BINARY_OP_TRUE_DIVIDE:
480 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
481 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000482
Damien Georgee2e3d112014-01-06 22:13:00 +0000483 // TODO implement modulo as specified by Python
484 case RT_BINARY_OP_MODULO:
485 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000486
Damien Georgee2e3d112014-01-06 22:13:00 +0000487 // TODO check for negative power, and overflow
488 case RT_BINARY_OP_POWER:
489 case RT_BINARY_OP_INPLACE_POWER:
490 {
491 int ans = 1;
492 while (rhs_val > 0) {
493 if (rhs_val & 1) {
494 ans *= lhs_val;
495 }
496 lhs_val *= lhs_val;
497 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000498 }
Damien Georgee2e3d112014-01-06 22:13:00 +0000499 lhs_val = ans;
500 break;
Damien4ebb32f2013-11-02 14:33:10 +0000501 }
Damiena3dcd9e2013-12-17 21:35:38 +0000502
Damien Georgee2e3d112014-01-06 22:13:00 +0000503 default: assert(0);
504 }
505 if (fit_small_int(lhs_val)) {
506 return MP_OBJ_NEW_SMALL_INT(lhs_val);
507 }
508 } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
509 return mp_obj_float_binary_op(op, lhs_val, rhs);
510 } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
511 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien429d7192013-10-04 19:53:11 +0100512 }
Damiend99b0522013-12-21 18:17:45 +0000513 } else if (MP_OBJ_IS_OBJ(lhs)) {
514 mp_obj_base_t *o = lhs;
515 if (o->type->binary_op != NULL) {
516 mp_obj_t result = o->type->binary_op(op, lhs, rhs);
517 if (result != NULL) {
518 return result;
Damien7410e442013-11-02 19:47:57 +0000519 }
Damien7410e442013-11-02 19:47:57 +0000520 }
Damien429d7192013-10-04 19:53:11 +0100521 }
Damiend99b0522013-12-21 18:17:45 +0000522
523 // TODO specify in error message what the operator is
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000524 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 +0100525}
526
Damiend99b0522013-12-21 18:17:45 +0000527mp_obj_t rt_compare_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100528 DEBUG_OP_printf("compare %d %p %p\n", op, lhs, rhs);
Damien7b2d3f32013-10-22 16:53:02 +0100529
530 // deal with == and !=
531 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000532 if (mp_obj_equal(lhs, rhs)) {
Damien7b2d3f32013-10-22 16:53:02 +0100533 if (op == RT_COMPARE_OP_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000534 return mp_const_true;
Damien7b2d3f32013-10-22 16:53:02 +0100535 } else {
Damiend99b0522013-12-21 18:17:45 +0000536 return mp_const_false;
Damien7b2d3f32013-10-22 16:53:02 +0100537 }
538 } else {
539 if (op == RT_COMPARE_OP_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000540 return mp_const_false;
Damien7b2d3f32013-10-22 16:53:02 +0100541 } else {
Damiend99b0522013-12-21 18:17:45 +0000542 return mp_const_true;
Damien7b2d3f32013-10-22 16:53:02 +0100543 }
544 }
545 }
546
Damienb86e3f92013-12-29 17:17:43 +0000547 // deal with exception_match
548 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
549 // TODO properly! at the moment it just compares the exception identifier for equality
550 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
551 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
552 return mp_const_true;
553 } else {
554 return mp_const_false;
555 }
556 }
557 }
558
Damien7b2d3f32013-10-22 16:53:02 +0100559 // deal with small ints
Damiend99b0522013-12-21 18:17:45 +0000560 if (MP_OBJ_IS_SMALL_INT(lhs) && MP_OBJ_IS_SMALL_INT(rhs)) {
561 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
562 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien429d7192013-10-04 19:53:11 +0100563 int cmp;
564 switch (op) {
Damien4ebb32f2013-11-02 14:33:10 +0000565 case RT_COMPARE_OP_LESS: cmp = lhs_val < rhs_val; break;
566 case RT_COMPARE_OP_MORE: cmp = lhs_val > rhs_val; break;
567 case RT_COMPARE_OP_LESS_EQUAL: cmp = lhs_val <= rhs_val; break;
568 case RT_COMPARE_OP_MORE_EQUAL: cmp = lhs_val >= rhs_val; break;
Damien429d7192013-10-04 19:53:11 +0100569 default: assert(0); cmp = 0;
570 }
571 if (cmp) {
Damiend99b0522013-12-21 18:17:45 +0000572 return mp_const_true;
Damien429d7192013-10-04 19:53:11 +0100573 } else {
Damiend99b0522013-12-21 18:17:45 +0000574 return mp_const_false;
Damien429d7192013-10-04 19:53:11 +0100575 }
576 }
Damien7b2d3f32013-10-22 16:53:02 +0100577
Damien4ebb32f2013-11-02 14:33:10 +0000578#if MICROPY_ENABLE_FLOAT
579 // deal with floats
Damiend99b0522013-12-21 18:17:45 +0000580 if (MP_OBJ_IS_TYPE(lhs, &float_type) || MP_OBJ_IS_TYPE(rhs, &float_type)) {
581 mp_float_t lhs_val = mp_obj_get_float(lhs);
582 mp_float_t rhs_val = mp_obj_get_float(rhs);
Damien4ebb32f2013-11-02 14:33:10 +0000583 int cmp;
584 switch (op) {
585 case RT_COMPARE_OP_LESS: cmp = lhs_val < rhs_val; break;
586 case RT_COMPARE_OP_MORE: cmp = lhs_val > rhs_val; break;
587 case RT_COMPARE_OP_LESS_EQUAL: cmp = lhs_val <= rhs_val; break;
588 case RT_COMPARE_OP_MORE_EQUAL: cmp = lhs_val >= rhs_val; break;
589 default: assert(0); cmp = 0;
590 }
591 if (cmp) {
Damiend99b0522013-12-21 18:17:45 +0000592 return mp_const_true;
Damien4ebb32f2013-11-02 14:33:10 +0000593 } else {
Damiend99b0522013-12-21 18:17:45 +0000594 return mp_const_false;
Damien4ebb32f2013-11-02 14:33:10 +0000595 }
596 }
597#endif
598
Damien7b2d3f32013-10-22 16:53:02 +0100599 // not implemented
Damien429d7192013-10-04 19:53:11 +0100600 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000601 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100602}
603
Damiend99b0522013-12-21 18:17:45 +0000604mp_obj_t rt_make_function_from_id(int unique_code_id) {
Damienb05d7072013-10-05 13:37:10 +0100605 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
606 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100607 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000608 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100609 }
Damiend99b0522013-12-21 18:17:45 +0000610
611 // make the function, depending on the code kind
612 mp_code_t *c = &unique_codes[unique_code_id];
613 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100614 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000615 case MP_CODE_BYTE:
Damien George6baf76e2013-12-30 22:32:17 +0000616 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 +0100617 break;
Damiend99b0522013-12-21 18:17:45 +0000618 case MP_CODE_NATIVE:
Damien429d7192013-10-04 19:53:11 +0100619 switch (c->n_args) {
Damiend99b0522013-12-21 18:17:45 +0000620 case 0: fun = rt_make_function_0(c->u_native.fun); break;
621 case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break;
622 case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break;
623 default: assert(0); fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100624 }
Damien429d7192013-10-04 19:53:11 +0100625 break;
Damiend99b0522013-12-21 18:17:45 +0000626 case MP_CODE_INLINE_ASM:
627 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100628 break;
629 default:
630 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000631 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100632 }
Damienbd254452013-10-16 20:39:12 +0100633
634 // check for generator functions and if so wrap in generator object
635 if (c->is_generator) {
Damien George6baf76e2013-12-30 22:32:17 +0000636 fun = mp_obj_new_gen_wrap(c->n_locals, c->n_stack, fun);
Damienbd254452013-10-16 20:39:12 +0100637 }
638
Damiend99b0522013-12-21 18:17:45 +0000639 return fun;
Damien429d7192013-10-04 19:53:11 +0100640}
641
Damiend99b0522013-12-21 18:17:45 +0000642mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000643 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000644 // make function object
645 mp_obj_t ffun = rt_make_function_from_id(unique_code_id);
Damien9ecbcff2013-12-11 00:41:43 +0000646 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000647 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000648}
649
Damiend99b0522013-12-21 18:17:45 +0000650mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damieneb19efb2013-10-10 22:06:54 +0100651 return rt_call_function_n(fun, 0, NULL);
652}
653
Damiend99b0522013-12-21 18:17:45 +0000654mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damieneb19efb2013-10-10 22:06:54 +0100655 return rt_call_function_n(fun, 1, &arg);
656}
657
Damiend99b0522013-12-21 18:17:45 +0000658mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
659 mp_obj_t args[2];
Damieneb19efb2013-10-10 22:06:54 +0100660 args[1] = arg1;
661 args[0] = arg2;
662 return rt_call_function_n(fun, 2, args);
663}
664
Damieneb19efb2013-10-10 22:06:54 +0100665// args are in reverse order in the array
Damiend99b0522013-12-21 18:17:45 +0000666mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
667 // TODO improve this: fun object can specify its type and we parse here the arguments,
668 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100669
Damiend99b0522013-12-21 18:17:45 +0000670 DEBUG_OP_printf("calling function %p(n_args=%d, args=%p)\n", fun_in, n_args, args);
Damieneb19efb2013-10-10 22:06:54 +0100671
Damiend99b0522013-12-21 18:17:45 +0000672 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000673 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
Damien429d7192013-10-04 19:53:11 +0100674 } else {
Damiend99b0522013-12-21 18:17:45 +0000675 mp_obj_base_t *fun = fun_in;
676 if (fun->type->call_n != NULL) {
677 return fun->type->call_n(fun_in, n_args, args);
678 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000679 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 +0000680 }
Damien429d7192013-10-04 19:53:11 +0100681 }
Damien429d7192013-10-04 19:53:11 +0100682}
683
Damien86c7fc72013-11-26 15:16:41 +0000684// args are in reverse order in the array; keyword arguments come first, value then key
685// eg: (value1, key1, value0, key0, arg1, arg0)
Damiend99b0522013-12-21 18:17:45 +0000686mp_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 +0000687 // TODO
688 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000689 return mp_const_none;
Damien86c7fc72013-11-26 15:16:41 +0000690}
691
Damiena3977762013-10-09 23:10:10 +0100692// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
693// if n_args==0 then there are only self/NULL and fun
Damiend99b0522013-12-21 18:17:45 +0000694mp_obj_t rt_call_method_n(uint n_args, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000695 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 +0100696 return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
697}
698
Damien86c7fc72013-11-26 15:16:41 +0000699// 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 +0000700mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000701 uint n = n_args + 2 * n_kw;
702 DEBUG_OP_printf("call method %p(self=%p, n_args=%u, n_kw=%u)\n", args[n + 1], args[n], n_args, n_kw);
703 return rt_call_function_n_kw(args[n + 1], n_args + ((args[n] == NULL) ? 0 : 1), n_kw, args);
704}
705
Damien429d7192013-10-04 19:53:11 +0100706// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000707mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
708 return mp_obj_new_tuple_reverse(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100709}
710
711// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000712mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
713 return mp_obj_new_list_reverse(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Damiend99b0522013-12-21 18:17:45 +0000716mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
717 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100718}
719
Damiend99b0522013-12-21 18:17:45 +0000720mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000721 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100722 return set;
723}
724
Damien86c7fc72013-11-26 15:16:41 +0000725// unpacked items are stored in order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000726void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
727 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
728 uint seq_len;
729 mp_obj_t *seq_items;
730 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
731 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
732 } else {
733 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000734 }
Damiend99b0522013-12-21 18:17:45 +0000735 if (seq_len < num) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000736 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 +0000737 } else if (seq_len > num) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000738 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 +0000739 }
740 memcpy(items, seq_items, num * sizeof(mp_obj_t));
Damien86c7fc72013-11-26 15:16:41 +0000741 } else {
742 // TODO call rt_getiter and extract via rt_iternext
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000743 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 +0000744 }
745}
746
Damiend99b0522013-12-21 18:17:45 +0000747mp_obj_t rt_build_map(int n_args) {
748 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100749}
750
Damiend99b0522013-12-21 18:17:45 +0000751mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
752 // map should always be a dict
753 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100754}
755
Damiend99b0522013-12-21 18:17:45 +0000756mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damiena3977762013-10-09 23:10:10 +0100757 DEBUG_OP_printf("load attr %s\n", qstr_str(attr));
Damiend99b0522013-12-21 18:17:45 +0000758 if (MP_OBJ_IS_TYPE(base, &class_type)) {
759 mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, false);
Damiena3977762013-10-09 23:10:10 +0100760 if (elem == NULL) {
Damien George28708622014-01-02 21:30:26 +0000761 // TODO what about generic method lookup?
762 goto no_attr;
Damiena3977762013-10-09 23:10:10 +0100763 }
764 return elem->value;
Damiend99b0522013-12-21 18:17:45 +0000765 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
766 return mp_obj_instance_load_attr(base, attr);
Damien George28708622014-01-02 21:30:26 +0000767 } else if (MP_OBJ_IS_TYPE(base, &module_type)) {
Damien George66028ab2014-01-03 14:03:48 +0000768 DEBUG_OP_printf("lookup module map %p\n", mp_obj_module_get_globals(base));
Damien George28708622014-01-02 21:30:26 +0000769 mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_module_get_globals(base), attr, false);
770 if (elem == NULL) {
771 // TODO what about generic method lookup?
772 goto no_attr;
773 }
774 return elem->value;
Damiend99b0522013-12-21 18:17:45 +0000775 } else if (MP_OBJ_IS_OBJ(base)) {
776 // generic method lookup
777 mp_obj_base_t *o = base;
778 const mp_method_t *meth = &o->type->methods[0];
Damiend57eba52013-11-02 23:58:14 +0000779 for (; meth->name != NULL; meth++) {
780 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damiend99b0522013-12-21 18:17:45 +0000781 return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
782 }
783 }
784 }
Damien George28708622014-01-02 21:30:26 +0000785
786no_attr:
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000787 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 +0000788}
789
790void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
791 DEBUG_OP_printf("load method %s\n", qstr_str(attr));
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000792 if (MP_OBJ_IS_TYPE(base, &gen_instance_type) && attr == MP_QSTR___next__) {
Damiend9d62012013-12-21 18:38:03 +0000793 dest[1] = (mp_obj_t)&mp_builtin_next_obj;
Damiend99b0522013-12-21 18:17:45 +0000794 dest[0] = base;
795 return;
796 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
797 mp_obj_instance_load_method(base, attr, dest);
798 return;
799 } else if (MP_OBJ_IS_OBJ(base)) {
800 // generic method lookup
801 mp_obj_base_t *o = base;
802 const mp_method_t *meth = &o->type->methods[0];
803 for (; meth->name != NULL; meth++) {
804 if (strcmp(meth->name, qstr_str(attr)) == 0) {
805 dest[1] = (mp_obj_t)meth->fun;
Damiend57eba52013-11-02 23:58:14 +0000806 dest[0] = base;
807 return;
808 }
809 }
Damiena3977762013-10-09 23:10:10 +0100810 }
811
Damiend99b0522013-12-21 18:17:45 +0000812 // no method; fallback to load_attr
Damiena3977762013-10-09 23:10:10 +0100813 dest[1] = rt_load_attr(base, attr);
814 dest[0] = NULL;
815}
816
Damiend99b0522013-12-21 18:17:45 +0000817void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100818 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damiend99b0522013-12-21 18:17:45 +0000819 if (MP_OBJ_IS_TYPE(base, &class_type)) {
Damienec63cce2013-10-22 22:58:17 +0100820 // TODO CPython allows STORE_ATTR to a class, but is this the correct implementation?
Damiend99b0522013-12-21 18:17:45 +0000821 mp_map_t *locals = mp_obj_class_get_locals(base);
822 mp_qstr_map_lookup(locals, attr, true)->value = value;
823 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
824 mp_obj_instance_store_attr(base, attr, value);
Damien George28708622014-01-02 21:30:26 +0000825 } else if (MP_OBJ_IS_TYPE(base, &module_type)) {
826 // TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
827 mp_map_t *globals = mp_obj_module_get_globals(base);
828 mp_qstr_map_lookup(globals, attr, true)->value = value;
Damiena3977762013-10-09 23:10:10 +0100829 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000830 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 +0100831 }
832}
833
Damiend99b0522013-12-21 18:17:45 +0000834void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100835 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000836 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100837 // list store
Damiend99b0522013-12-21 18:17:45 +0000838 mp_obj_list_store(base, index, value);
839 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
840 // dict store
841 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100842 } else {
843 assert(0);
844 }
845}
846
Damiend99b0522013-12-21 18:17:45 +0000847mp_obj_t rt_getiter(mp_obj_t o_in) {
848 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000849 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100850 } else {
Damiend99b0522013-12-21 18:17:45 +0000851 mp_obj_base_t *o = o_in;
852 if (o->type->getiter != NULL) {
853 return o->type->getiter(o_in);
854 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000855 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 +0000856 }
Damience89a212013-10-15 22:25:17 +0100857 }
858}
859
Damiend99b0522013-12-21 18:17:45 +0000860mp_obj_t rt_iternext(mp_obj_t o_in) {
861 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000862 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "? 'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100863 } else {
Damiend99b0522013-12-21 18:17:45 +0000864 mp_obj_base_t *o = o_in;
865 if (o->type->iternext != NULL) {
866 return o->type->iternext(o_in);
867 } else {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000868 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 +0000869 }
Damience89a212013-10-15 22:25:17 +0100870 }
871}
872
Damiend99b0522013-12-21 18:17:45 +0000873mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000874 // build args array
Damiend99b0522013-12-21 18:17:45 +0000875 mp_obj_t args[5];
876 args[0] = mp_obj_new_str(name);
877 args[1] = mp_const_none; // TODO should be globals
878 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000879 args[3] = fromlist;
880 args[4] = level; // must be 0; we don't yet support other values
881
882 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000883 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000884}
885
Damiend99b0522013-12-21 18:17:45 +0000886mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
887 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000888 /* TODO convert AttributeError to ImportError
889 if (fail) {
890 (ImportError, "cannot import name %s", qstr_str(name), NULL)
891 }
892 */
893 return x;
894}
895
Damien George66028ab2014-01-03 14:03:48 +0000896mp_map_t *rt_locals_get(void) {
897 return map_locals;
898}
899
900void rt_locals_set(mp_map_t *m) {
901 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
902 map_locals = m;
903}
904
905mp_map_t *rt_globals_get(void) {
906 return map_globals;
907}
908
909void rt_globals_set(mp_map_t *m) {
910 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
911 map_globals = m;
912}
913
Damien6ba13142013-11-02 20:34:54 +0000914// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100915void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000916 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100917 rt_load_const_str,
918 rt_load_name,
919 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100920 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +0100921 rt_load_attr,
922 rt_load_method,
923 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +0100924 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +0100925 rt_store_subscr,
926 rt_is_true,
927 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +0100928 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +0100929 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +0100930 rt_list_append,
Damien429d7192013-10-04 19:53:11 +0100931 rt_build_map,
932 rt_store_map,
933 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +0100934 rt_store_set,
Damien429d7192013-10-04 19:53:11 +0100935 rt_make_function_from_id,
Damieneb19efb2013-10-10 22:06:54 +0100936 rt_call_function_n,
Damien7f5dacf2013-10-10 11:24:39 +0100937 rt_call_method_n,
Damien429d7192013-10-04 19:53:11 +0100938 rt_binary_op,
939 rt_compare_op,
Damiend2755ec2013-10-16 23:58:48 +0100940 rt_getiter,
941 rt_iternext,
Damien429d7192013-10-04 19:53:11 +0100942};
943
944/*
945void rt_f_vector(rt_fun_kind_t fun_kind) {
946 (rt_f_table[fun_kind])();
947}
948*/