blob: 7258b36cafad5530deb79a5e64b410c543fb7aa4 [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"
Damien660365e2013-12-17 18:27:24 +000014#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
16#include "runtime.h"
17#include "map.h"
Damien660365e2013-12-17 18:27:24 +000018#include "builtin.h"
19
Damien7f5dacf2013-10-10 11:24:39 +010020#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010021#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000022#define WRITE_CODE (1)
Damien7f5dacf2013-10-10 11:24:39 +010023#define DEBUG_printf(args...) printf(args)
24#define DEBUG_OP_printf(args...) printf(args)
25#else // don't print debugging info
Damiena3977762013-10-09 23:10:10 +010026#define DEBUG_printf(args...) (void)0
Damien429d7192013-10-04 19:53:11 +010027#define DEBUG_OP_printf(args...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010028#endif
Damien429d7192013-10-04 19:53:11 +010029
Damien660365e2013-12-17 18:27:24 +000030// TODO make these predefined so they don't take up RAM
31qstr rt_q_append;
32qstr rt_q_pop;
33qstr rt_q_sort;
34qstr rt_q_join;
35qstr rt_q_format;
36qstr rt_q___build_class__;
37qstr rt_q___next__;
38qstr rt_q_AttributeError;
39qstr rt_q_IndexError;
40qstr rt_q_KeyError;
41qstr rt_q_NameError;
42qstr rt_q_TypeError;
43qstr rt_q_SyntaxError;
44qstr rt_q_ValueError;
Damien4ebb32f2013-11-02 14:33:10 +000045
Damieneb19efb2013-10-10 22:06:54 +010046// locals and globals need to be pointers because they can be the same in outer module scope
Damiend99b0522013-12-21 18:17:45 +000047static mp_map_t *map_locals;
48static mp_map_t *map_globals;
49static mp_map_t map_builtins;
Damienbd254452013-10-16 20:39:12 +010050
Damien429d7192013-10-04 19:53:11 +010051typedef enum {
Damiend99b0522013-12-21 18:17:45 +000052 MP_CODE_NONE,
53 MP_CODE_BYTE,
54 MP_CODE_NATIVE,
55 MP_CODE_INLINE_ASM,
56} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010057
Damiend99b0522013-12-21 18:17:45 +000058typedef struct _mp_code_t {
59 mp_code_kind_t kind;
Damien429d7192013-10-04 19:53:11 +010060 int n_args;
Damienbd254452013-10-16 20:39:12 +010061 int n_locals;
Damien9ecbcff2013-12-11 00:41:43 +000062 int n_cells;
Damienbd254452013-10-16 20:39:12 +010063 int n_stack;
64 bool is_generator;
Damien429d7192013-10-04 19:53:11 +010065 union {
66 struct {
Damien429d7192013-10-04 19:53:11 +010067 byte *code;
68 uint len;
69 } u_byte;
Damien826005c2013-10-05 23:17:28 +010070 struct {
Damiend99b0522013-12-21 18:17:45 +000071 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010072 } u_native;
73 struct {
Damiene4af64f2013-10-06 12:04:13 +010074 void *fun;
Damien826005c2013-10-05 23:17:28 +010075 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010076 };
Damiend99b0522013-12-21 18:17:45 +000077} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010078
79static int next_unique_code_id;
Damiend99b0522013-12-21 18:17:45 +000080static mp_code_t *unique_codes;
Damien429d7192013-10-04 19:53:11 +010081
Damien0446a0d2013-11-17 13:16:36 +000082#ifdef WRITE_CODE
83FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010084#endif
Damien429d7192013-10-04 19:53:11 +010085
Damien8b3a7c22013-10-23 20:20:17 +010086void rt_init(void) {
Damien660365e2013-12-17 18:27:24 +000087 rt_q_append = qstr_from_str_static("append");
88 rt_q_pop = qstr_from_str_static("pop");
89 rt_q_sort = qstr_from_str_static("sort");
90 rt_q_join = qstr_from_str_static("join");
91 rt_q_format = qstr_from_str_static("format");
92 rt_q___build_class__ = qstr_from_str_static("__build_class__");
93 rt_q___next__ = qstr_from_str_static("__next__");
94 rt_q_AttributeError = qstr_from_str_static("AttributeError");
95 rt_q_IndexError = qstr_from_str_static("IndexError");
96 rt_q_KeyError = qstr_from_str_static("KeyError");
97 rt_q_NameError = qstr_from_str_static("NameError");
98 rt_q_TypeError = qstr_from_str_static("TypeError");
99 rt_q_SyntaxError = qstr_from_str_static("SyntaxError");
100 rt_q_ValueError = qstr_from_str_static("ValueError");
Damien429d7192013-10-04 19:53:11 +0100101
Damieneb19efb2013-10-10 22:06:54 +0100102 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damiend99b0522013-12-21 18:17:45 +0000103 map_locals = map_globals = mp_map_new(MP_MAP_QSTR, 1);
104 mp_qstr_map_lookup(map_globals, qstr_from_str_static("__name__"), true)->value = mp_obj_new_str(qstr_from_str_static("__main__"));
Damien429d7192013-10-04 19:53:11 +0100105
Damienb86e3f92013-12-29 17:17:43 +0000106 // init built-in hash table
Damiend99b0522013-12-21 18:17:45 +0000107 mp_map_init(&map_builtins, MP_MAP_QSTR, 3);
Damienb86e3f92013-12-29 17:17:43 +0000108
109 // built-in exceptions (TODO, make these proper classes)
110 mp_qstr_map_lookup(&map_builtins, rt_q_AttributeError, true)->value = mp_obj_new_exception(rt_q_AttributeError);
111 mp_qstr_map_lookup(&map_builtins, rt_q_IndexError, true)->value = mp_obj_new_exception(rt_q_IndexError);
112 mp_qstr_map_lookup(&map_builtins, rt_q_KeyError, true)->value = mp_obj_new_exception(rt_q_KeyError);
113 mp_qstr_map_lookup(&map_builtins, rt_q_NameError, true)->value = mp_obj_new_exception(rt_q_NameError);
114 mp_qstr_map_lookup(&map_builtins, rt_q_TypeError, true)->value = mp_obj_new_exception(rt_q_TypeError);
115 mp_qstr_map_lookup(&map_builtins, rt_q_SyntaxError, true)->value = mp_obj_new_exception(rt_q_SyntaxError);
116 mp_qstr_map_lookup(&map_builtins, rt_q_ValueError, true)->value = mp_obj_new_exception(rt_q_ValueError);
117
118 // built-in core functions
Damiend99b0522013-12-21 18:17:45 +0000119 mp_qstr_map_lookup(&map_builtins, rt_q___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__);
120 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("__repl_print__"), true)->value = rt_make_function_1(mp_builtin___repl_print__);
Damienb86e3f92013-12-29 17:17:43 +0000121
122 // built-in user functions
Damiend99b0522013-12-21 18:17:45 +0000123 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("abs"), true)->value = rt_make_function_1(mp_builtin_abs);
124 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("all"), true)->value = rt_make_function_1(mp_builtin_all);
125 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("any"), true)->value = rt_make_function_1(mp_builtin_any);
126 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("bool"), true)->value = rt_make_function_var(0, mp_builtin_bool);
127 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("callable"), true)->value = rt_make_function_1(mp_builtin_callable);
128#if MICROPY_ENABLE_FLOAT
129 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("complex"), true)->value = rt_make_function_var(0, mp_builtin_complex);
130#endif
131 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("chr"), true)->value = rt_make_function_1(mp_builtin_chr);
132 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("dict"), true)->value = rt_make_function_0(mp_builtin_dict);
133 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("divmod"), true)->value = rt_make_function_2(mp_builtin_divmod);
134 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("hash"), true)->value = (mp_obj_t)&mp_builtin_hash_obj;
135 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("iter"), true)->value = (mp_obj_t)&mp_builtin_iter_obj;
136 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("len"), true)->value = rt_make_function_1(mp_builtin_len);
137 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("list"), true)->value = rt_make_function_var(0, mp_builtin_list);
138 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("max"), true)->value = rt_make_function_var(1, mp_builtin_max);
139 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("min"), true)->value = rt_make_function_var(1, mp_builtin_min);
140 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("next"), true)->value = (mp_obj_t)&mp_builtin_next_obj;
141 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("ord"), true)->value = rt_make_function_1(mp_builtin_ord);
142 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("pow"), true)->value = rt_make_function_var(2, mp_builtin_pow);
143 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("print"), true)->value = rt_make_function_var(0, mp_builtin_print);
144 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("range"), true)->value = rt_make_function_var(1, mp_builtin_range);
145 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("set"), true)->value = (mp_obj_t)&mp_builtin_set_obj;
146 mp_qstr_map_lookup(&map_builtins, qstr_from_str_static("sum"), true)->value = rt_make_function_var(1, mp_builtin_sum);
Damien429d7192013-10-04 19:53:11 +0100147
Damien5ac1b2e2013-10-18 19:58:12 +0100148 next_unique_code_id = 2; // 1 is reserved for the __main__ module scope
Damien429d7192013-10-04 19:53:11 +0100149 unique_codes = NULL;
150
Damien0446a0d2013-11-17 13:16:36 +0000151#ifdef WRITE_CODE
152 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100153#endif
Damien429d7192013-10-04 19:53:11 +0100154}
155
Damien8b3a7c22013-10-23 20:20:17 +0100156void rt_deinit(void) {
Damien0446a0d2013-11-17 13:16:36 +0000157#ifdef WRITE_CODE
158 if (fp_write_code != NULL) {
159 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100160 }
Damiena1ddfcc2013-10-10 23:25:50 +0100161#endif
Damien429d7192013-10-04 19:53:11 +0100162}
163
Damien5ac1b2e2013-10-18 19:58:12 +0100164int rt_get_unique_code_id(bool is_main_module) {
165 if (is_main_module) {
166 return 1;
167 } else {
168 return next_unique_code_id++;
169 }
Damien429d7192013-10-04 19:53:11 +0100170}
171
Damien8b3a7c22013-10-23 20:20:17 +0100172static void alloc_unique_codes(void) {
Damien429d7192013-10-04 19:53:11 +0100173 if (unique_codes == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000174 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 +0100175 for (int i = 0; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000176 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100177 }
Damien429d7192013-10-04 19:53:11 +0100178 }
Damien826005c2013-10-05 23:17:28 +0100179}
180
Damien9ecbcff2013-12-11 00:41:43 +0000181void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_cells, int n_stack, bool is_generator) {
Damien826005c2013-10-05 23:17:28 +0100182 alloc_unique_codes();
183
184 assert(unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000185 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien826005c2013-10-05 23:17:28 +0100186 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100187 unique_codes[unique_code_id].n_locals = n_locals;
Damien9ecbcff2013-12-11 00:41:43 +0000188 unique_codes[unique_code_id].n_cells = n_cells;
Damienbd254452013-10-16 20:39:12 +0100189 unique_codes[unique_code_id].n_stack = n_stack;
190 unique_codes[unique_code_id].is_generator = is_generator;
Damien826005c2013-10-05 23:17:28 +0100191 unique_codes[unique_code_id].u_byte.code = code;
192 unique_codes[unique_code_id].u_byte.len = len;
193
Damien9ecbcff2013-12-11 00:41:43 +0000194 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000195
196#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100197 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 +0000198 for (int i = 0; i < 128 && i < len; i++) {
199 if (i > 0 && i % 16 == 0) {
200 DEBUG_printf("\n");
201 }
202 DEBUG_printf(" %02x", code[i]);
203 }
204 DEBUG_printf("\n");
Damiend99b0522013-12-21 18:17:45 +0000205 extern void mp_show_byte_code(const byte *code, int len);
206 mp_show_byte_code(code, len);
Damien0446a0d2013-11-17 13:16:36 +0000207
208#ifdef WRITE_CODE
209 if (fp_write_code != NULL) {
210 fwrite(code, len, 1, fp_write_code);
211 fflush(fp_write_code);
212 }
213#endif
214#endif
Damien826005c2013-10-05 23:17:28 +0100215}
216
Damiend99b0522013-12-21 18:17:45 +0000217void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100218 alloc_unique_codes();
219
Damienb05d7072013-10-05 13:37:10 +0100220 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000221 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien429d7192013-10-04 19:53:11 +0100222 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100223 unique_codes[unique_code_id].n_locals = 0;
Damien9ecbcff2013-12-11 00:41:43 +0000224 unique_codes[unique_code_id].n_cells = 0;
Damienbd254452013-10-16 20:39:12 +0100225 unique_codes[unique_code_id].n_stack = 0;
226 unique_codes[unique_code_id].is_generator = false;
Damien429d7192013-10-04 19:53:11 +0100227 unique_codes[unique_code_id].u_native.fun = fun;
228
Damien0446a0d2013-11-17 13:16:36 +0000229 printf("native code: %d bytes\n", len);
230
Damiena1ddfcc2013-10-10 23:25:50 +0100231#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100232 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
233 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
234 for (int i = 0; i < 128 && i < len; i++) {
235 if (i > 0 && i % 16 == 0) {
236 DEBUG_printf("\n");
237 }
238 DEBUG_printf(" %02x", fun_data[i]);
239 }
240 DEBUG_printf("\n");
241
Damien0446a0d2013-11-17 13:16:36 +0000242#ifdef WRITE_CODE
243 if (fp_write_code != NULL) {
244 fwrite(fun_data, len, 1, fp_write_code);
245 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100246 }
Damiena1ddfcc2013-10-10 23:25:50 +0100247#endif
248#endif
Damien429d7192013-10-04 19:53:11 +0100249}
250
Damiend99b0522013-12-21 18:17:45 +0000251void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100252 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100253
Damien826005c2013-10-05 23:17:28 +0100254 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000255 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien826005c2013-10-05 23:17:28 +0100256 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100257 unique_codes[unique_code_id].n_locals = 0;
Damien9ecbcff2013-12-11 00:41:43 +0000258 unique_codes[unique_code_id].n_cells = 0;
Damienbd254452013-10-16 20:39:12 +0100259 unique_codes[unique_code_id].n_stack = 0;
260 unique_codes[unique_code_id].is_generator = false;
Damien826005c2013-10-05 23:17:28 +0100261 unique_codes[unique_code_id].u_inline_asm.fun = fun;
262
Damiena1ddfcc2013-10-10 23:25:50 +0100263#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100264 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
265 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
266 for (int i = 0; i < 128 && i < len; i++) {
267 if (i > 0 && i % 16 == 0) {
268 DEBUG_printf("\n");
269 }
270 DEBUG_printf(" %02x", fun_data[i]);
271 }
272 DEBUG_printf("\n");
273
Damien0446a0d2013-11-17 13:16:36 +0000274#ifdef WRITE_CODE
275 if (fp_write_code != NULL) {
276 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100277 }
Damiena1ddfcc2013-10-10 23:25:50 +0100278#endif
279#endif
Damien429d7192013-10-04 19:53:11 +0100280}
281
Damiend99b0522013-12-21 18:17:45 +0000282mp_map_t *rt_get_map_locals(void) {
283 return map_locals;
284}
285
286void rt_set_map_locals(mp_map_t *m) {
287 map_locals = m;
288}
289
290static bool fit_small_int(mp_small_int_t o) {
291 return true;
292}
293
294int rt_is_true(mp_obj_t arg) {
295 DEBUG_OP_printf("is true %p\n", arg);
296 if (MP_OBJ_IS_SMALL_INT(arg)) {
297 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
298 return 0;
299 } else {
300 return 1;
301 }
302 } else if (arg == mp_const_none) {
303 return 0;
304 } else if (arg == mp_const_false) {
305 return 0;
306 } else if (arg == mp_const_true) {
307 return 1;
308 } else {
309 assert(0);
310 return 0;
311 }
312}
313
314mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
315 return mp_obj_list_append(self_in, arg);
316}
317
Damien7410e442013-11-02 19:47:57 +0000318#define PARSE_DEC_IN_INTG (1)
319#define PARSE_DEC_IN_FRAC (2)
320#define PARSE_DEC_IN_EXP (3)
321
Damiend99b0522013-12-21 18:17:45 +0000322mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000323#if MICROPY_ENABLE_FLOAT
324 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
325 const char *s = qstr_str(qstr);
326 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000327 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000328 bool exp_neg = false;
329 int exp_val = 0;
330 int exp_extra = 0;
331 bool imag = false;
332 for (; *s; s++) {
333 int dig = *s;
334 if ('0' <= dig && dig <= '9') {
335 dig -= '0';
336 if (in == PARSE_DEC_IN_EXP) {
337 exp_val = 10 * exp_val + dig;
338 } else {
339 dec_val = 10 * dec_val + dig;
340 if (in == PARSE_DEC_IN_FRAC) {
341 exp_extra -= 1;
342 }
343 }
344 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
345 in = PARSE_DEC_IN_FRAC;
346 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
347 in = PARSE_DEC_IN_EXP;
348 if (s[1] == '+') {
349 s++;
350 } else if (s[1] == '-') {
351 s++;
352 exp_neg = true;
353 }
354 } else if (dig == 'J' || dig == 'j') {
355 s++;
356 imag = true;
357 break;
358 } else {
359 // unknown character
360 break;
361 }
362 }
363 if (*s != 0) {
Damiend99b0522013-12-21 18:17:45 +0000364 nlr_jump(mp_obj_new_exception_msg(rt_q_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000365 }
366 if (exp_neg) {
367 exp_val = -exp_val;
368 }
369 exp_val += exp_extra;
370 for (; exp_val > 0; exp_val--) {
371 dec_val *= 10;
372 }
373 for (; exp_val < 0; exp_val++) {
374 dec_val *= 0.1;
375 }
376 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000377 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000378 } else {
Damiend99b0522013-12-21 18:17:45 +0000379 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000380 }
381#else
Damiend99b0522013-12-21 18:17:45 +0000382 nlr_jump(mp_obj_new_exception_msg(rt_q_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000383#endif
384}
385
Damiend99b0522013-12-21 18:17:45 +0000386mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100387 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000388 return mp_obj_new_str(qstr);
Damien429d7192013-10-04 19:53:11 +0100389}
390
Damiend99b0522013-12-21 18:17:45 +0000391mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100392 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100393 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000394 mp_map_elem_t *elem = mp_qstr_map_lookup(map_locals, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100395 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000396 elem = mp_qstr_map_lookup(map_globals, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100397 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000398 elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
Damiena3977762013-10-09 23:10:10 +0100399 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000400 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damiena3977762013-10-09 23:10:10 +0100401 }
402 }
403 }
404 return elem->value;
405}
406
Damiend99b0522013-12-21 18:17:45 +0000407mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100408 // logic: search globals, builtins
409 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000410 mp_map_elem_t *elem = mp_qstr_map_lookup(map_globals, qstr, false);
Damien429d7192013-10-04 19:53:11 +0100411 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000412 elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
Damien429d7192013-10-04 19:53:11 +0100413 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000414 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100415 }
416 }
417 return elem->value;
418}
419
Damiend99b0522013-12-21 18:17:45 +0000420mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100421 DEBUG_OP_printf("load_build_class\n");
Damiend99b0522013-12-21 18:17:45 +0000422 mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, rt_q___build_class__, false);
Damien429d7192013-10-04 19:53:11 +0100423 if (elem == NULL) {
424 printf("name doesn't exist: __build_class__\n");
425 assert(0);
426 }
427 return elem->value;
428}
429
Damiend99b0522013-12-21 18:17:45 +0000430mp_obj_t rt_get_cell(mp_obj_t cell) {
431 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000432}
433
Damiend99b0522013-12-21 18:17:45 +0000434void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
435 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000436}
437
Damiend99b0522013-12-21 18:17:45 +0000438void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100439 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damiend99b0522013-12-21 18:17:45 +0000440 mp_qstr_map_lookup(map_locals, qstr, true)->value = obj;
Damiena3977762013-10-09 23:10:10 +0100441}
442
Damiend99b0522013-12-21 18:17:45 +0000443void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100444 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damiend99b0522013-12-21 18:17:45 +0000445 mp_qstr_map_lookup(map_globals, qstr, true)->value = obj;
Damien429d7192013-10-04 19:53:11 +0100446}
447
Damiend99b0522013-12-21 18:17:45 +0000448mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000449 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000450 if (MP_OBJ_IS_SMALL_INT(arg)) {
451 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000452 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000453 case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
Damien7410e442013-11-02 19:47:57 +0000454 case RT_UNARY_OP_POSITIVE: break;
455 case RT_UNARY_OP_NEGATIVE: val = -val; break;
456 case RT_UNARY_OP_INVERT: val = ~val; break;
457 default: assert(0); val = 0;
458 }
459 if (fit_small_int(val)) {
Damiend99b0522013-12-21 18:17:45 +0000460 return MP_OBJ_NEW_SMALL_INT(val);
461 } else {
462 // TODO make a bignum
463 assert(0);
464 return mp_const_none;
Damien7410e442013-11-02 19:47:57 +0000465 }
Damiend99b0522013-12-21 18:17:45 +0000466 } else { // will be an object (small ints are caught in previous if)
467 mp_obj_base_t *o = arg;
468 if (o->type->unary_op != NULL) {
469 mp_obj_t result = o->type->unary_op(op, arg);
470 if (result != NULL) {
471 return result;
472 }
Damien7410e442013-11-02 19:47:57 +0000473 }
Damiend99b0522013-12-21 18:17:45 +0000474 // TODO specify in error message what the operator is
475 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "bad operand type for unary operator: '%s'", o->type->name));
Damien7410e442013-11-02 19:47:57 +0000476 }
Damien429d7192013-10-04 19:53:11 +0100477}
478
Damiend99b0522013-12-21 18:17:45 +0000479mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100480 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damiend99b0522013-12-21 18:17:45 +0000481 if (MP_OBJ_IS_SMALL_INT(lhs) && MP_OBJ_IS_SMALL_INT(rhs)) {
482 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
483 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien429d7192013-10-04 19:53:11 +0100484 switch (op) {
Damien7b2d3f32013-10-22 16:53:02 +0100485 case RT_BINARY_OP_OR:
Damien7410e442013-11-02 19:47:57 +0000486 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100487 case RT_BINARY_OP_XOR:
Damien7410e442013-11-02 19:47:57 +0000488 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100489 case RT_BINARY_OP_AND:
Damien7410e442013-11-02 19:47:57 +0000490 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100491 case RT_BINARY_OP_LSHIFT:
Damien7410e442013-11-02 19:47:57 +0000492 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100493 case RT_BINARY_OP_RSHIFT:
Damien7410e442013-11-02 19:47:57 +0000494 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
Damien429d7192013-10-04 19:53:11 +0100495 case RT_BINARY_OP_ADD:
Damien7410e442013-11-02 19:47:57 +0000496 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
Damienbd254452013-10-16 20:39:12 +0100497 case RT_BINARY_OP_SUBTRACT:
Damien7410e442013-11-02 19:47:57 +0000498 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100499 case RT_BINARY_OP_MULTIPLY:
Damien7410e442013-11-02 19:47:57 +0000500 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
Damien7b2d3f32013-10-22 16:53:02 +0100501 case RT_BINARY_OP_FLOOR_DIVIDE:
Damien7410e442013-11-02 19:47:57 +0000502 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
Damien3ef4abb2013-10-12 16:53:13 +0100503#if MICROPY_ENABLE_FLOAT
Damien7b2d3f32013-10-22 16:53:02 +0100504 case RT_BINARY_OP_TRUE_DIVIDE:
Damiend99b0522013-12-21 18:17:45 +0000505 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 +0100506#endif
Damiena3dcd9e2013-12-17 21:35:38 +0000507
508 // TODO implement modulo as specified by Python
509 case RT_BINARY_OP_MODULO:
510 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
511
512 // TODO check for negative power, and overflow
Damien4ebb32f2013-11-02 14:33:10 +0000513 case RT_BINARY_OP_POWER:
514 case RT_BINARY_OP_INPLACE_POWER:
Damiena3dcd9e2013-12-17 21:35:38 +0000515 {
516 int ans = 1;
517 while (rhs_val > 0) {
518 if (rhs_val & 1) {
519 ans *= lhs_val;
520 }
521 lhs_val *= lhs_val;
522 rhs_val /= 2;
Damien4ebb32f2013-11-02 14:33:10 +0000523 }
Damiena3dcd9e2013-12-17 21:35:38 +0000524 lhs_val = ans;
525 break;
526 }
527
Damien7410e442013-11-02 19:47:57 +0000528 default: printf("%d\n", op); assert(0);
Damien429d7192013-10-04 19:53:11 +0100529 }
Damien7410e442013-11-02 19:47:57 +0000530 if (fit_small_int(lhs_val)) {
Damiend99b0522013-12-21 18:17:45 +0000531 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien429d7192013-10-04 19:53:11 +0100532 }
Damiend99b0522013-12-21 18:17:45 +0000533 } else if (MP_OBJ_IS_OBJ(lhs)) {
534 mp_obj_base_t *o = lhs;
535 if (o->type->binary_op != NULL) {
536 mp_obj_t result = o->type->binary_op(op, lhs, rhs);
537 if (result != NULL) {
538 return result;
Damien7410e442013-11-02 19:47:57 +0000539 }
Damien7410e442013-11-02 19:47:57 +0000540 }
Damien429d7192013-10-04 19:53:11 +0100541 }
Damiend99b0522013-12-21 18:17:45 +0000542
543 // TODO specify in error message what the operator is
544 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "unsupported operand type for binary operator: '%s'", mp_obj_get_type_str(lhs)));
Damien429d7192013-10-04 19:53:11 +0100545}
546
Damiend99b0522013-12-21 18:17:45 +0000547mp_obj_t rt_compare_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100548 DEBUG_OP_printf("compare %d %p %p\n", op, lhs, rhs);
Damien7b2d3f32013-10-22 16:53:02 +0100549
550 // deal with == and !=
551 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000552 if (mp_obj_equal(lhs, rhs)) {
Damien7b2d3f32013-10-22 16:53:02 +0100553 if (op == RT_COMPARE_OP_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000554 return mp_const_true;
Damien7b2d3f32013-10-22 16:53:02 +0100555 } else {
Damiend99b0522013-12-21 18:17:45 +0000556 return mp_const_false;
Damien7b2d3f32013-10-22 16:53:02 +0100557 }
558 } else {
559 if (op == RT_COMPARE_OP_EQUAL) {
Damiend99b0522013-12-21 18:17:45 +0000560 return mp_const_false;
Damien7b2d3f32013-10-22 16:53:02 +0100561 } else {
Damiend99b0522013-12-21 18:17:45 +0000562 return mp_const_true;
Damien7b2d3f32013-10-22 16:53:02 +0100563 }
564 }
565 }
566
Damienb86e3f92013-12-29 17:17:43 +0000567 // deal with exception_match
568 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
569 // TODO properly! at the moment it just compares the exception identifier for equality
570 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
571 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
572 return mp_const_true;
573 } else {
574 return mp_const_false;
575 }
576 }
577 }
578
Damien7b2d3f32013-10-22 16:53:02 +0100579 // deal with small ints
Damiend99b0522013-12-21 18:17:45 +0000580 if (MP_OBJ_IS_SMALL_INT(lhs) && MP_OBJ_IS_SMALL_INT(rhs)) {
581 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
582 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien429d7192013-10-04 19:53:11 +0100583 int cmp;
584 switch (op) {
Damien4ebb32f2013-11-02 14:33:10 +0000585 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;
Damien429d7192013-10-04 19:53:11 +0100589 default: assert(0); cmp = 0;
590 }
591 if (cmp) {
Damiend99b0522013-12-21 18:17:45 +0000592 return mp_const_true;
Damien429d7192013-10-04 19:53:11 +0100593 } else {
Damiend99b0522013-12-21 18:17:45 +0000594 return mp_const_false;
Damien429d7192013-10-04 19:53:11 +0100595 }
596 }
Damien7b2d3f32013-10-22 16:53:02 +0100597
Damien4ebb32f2013-11-02 14:33:10 +0000598#if MICROPY_ENABLE_FLOAT
599 // deal with floats
Damiend99b0522013-12-21 18:17:45 +0000600 if (MP_OBJ_IS_TYPE(lhs, &float_type) || MP_OBJ_IS_TYPE(rhs, &float_type)) {
601 mp_float_t lhs_val = mp_obj_get_float(lhs);
602 mp_float_t rhs_val = mp_obj_get_float(rhs);
Damien4ebb32f2013-11-02 14:33:10 +0000603 int cmp;
604 switch (op) {
605 case RT_COMPARE_OP_LESS: cmp = lhs_val < rhs_val; break;
606 case RT_COMPARE_OP_MORE: cmp = lhs_val > rhs_val; break;
607 case RT_COMPARE_OP_LESS_EQUAL: cmp = lhs_val <= rhs_val; break;
608 case RT_COMPARE_OP_MORE_EQUAL: cmp = lhs_val >= rhs_val; break;
609 default: assert(0); cmp = 0;
610 }
611 if (cmp) {
Damiend99b0522013-12-21 18:17:45 +0000612 return mp_const_true;
Damien4ebb32f2013-11-02 14:33:10 +0000613 } else {
Damiend99b0522013-12-21 18:17:45 +0000614 return mp_const_false;
Damien4ebb32f2013-11-02 14:33:10 +0000615 }
616 }
617#endif
618
Damien7b2d3f32013-10-22 16:53:02 +0100619 // not implemented
Damien429d7192013-10-04 19:53:11 +0100620 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000621 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100622}
623
Damiend99b0522013-12-21 18:17:45 +0000624mp_obj_t rt_make_function_from_id(int unique_code_id) {
Damienb05d7072013-10-05 13:37:10 +0100625 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
626 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100627 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000628 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100629 }
Damiend99b0522013-12-21 18:17:45 +0000630
631 // make the function, depending on the code kind
632 mp_code_t *c = &unique_codes[unique_code_id];
633 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100634 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000635 case MP_CODE_BYTE:
636 fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_cells + c->n_stack, c->u_byte.code);
Damien826005c2013-10-05 23:17:28 +0100637 break;
Damiend99b0522013-12-21 18:17:45 +0000638 case MP_CODE_NATIVE:
Damien429d7192013-10-04 19:53:11 +0100639 switch (c->n_args) {
Damiend99b0522013-12-21 18:17:45 +0000640 case 0: fun = rt_make_function_0(c->u_native.fun); break;
641 case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break;
642 case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break;
643 default: assert(0); fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100644 }
Damien429d7192013-10-04 19:53:11 +0100645 break;
Damiend99b0522013-12-21 18:17:45 +0000646 case MP_CODE_INLINE_ASM:
647 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100648 break;
649 default:
650 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000651 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100652 }
Damienbd254452013-10-16 20:39:12 +0100653
654 // check for generator functions and if so wrap in generator object
655 if (c->is_generator) {
Damiend99b0522013-12-21 18:17:45 +0000656 fun = mp_obj_new_gen_wrap(c->n_locals, c->n_cells, c->n_stack, fun);
Damienbd254452013-10-16 20:39:12 +0100657 }
658
Damiend99b0522013-12-21 18:17:45 +0000659 return fun;
Damien429d7192013-10-04 19:53:11 +0100660}
661
Damiend99b0522013-12-21 18:17:45 +0000662mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
663 // make function object
664 mp_obj_t ffun = rt_make_function_from_id(unique_code_id);
Damien9ecbcff2013-12-11 00:41:43 +0000665 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000666 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000667}
668
Damiend99b0522013-12-21 18:17:45 +0000669mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damieneb19efb2013-10-10 22:06:54 +0100670 return rt_call_function_n(fun, 0, NULL);
671}
672
Damiend99b0522013-12-21 18:17:45 +0000673mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damieneb19efb2013-10-10 22:06:54 +0100674 return rt_call_function_n(fun, 1, &arg);
675}
676
Damiend99b0522013-12-21 18:17:45 +0000677mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
678 mp_obj_t args[2];
Damieneb19efb2013-10-10 22:06:54 +0100679 args[1] = arg1;
680 args[0] = arg2;
681 return rt_call_function_n(fun, 2, args);
682}
683
Damieneb19efb2013-10-10 22:06:54 +0100684// args are in reverse order in the array
Damiend99b0522013-12-21 18:17:45 +0000685mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
686 // TODO improve this: fun object can specify its type and we parse here the arguments,
687 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100688
Damiend99b0522013-12-21 18:17:45 +0000689 DEBUG_OP_printf("calling function %p(n_args=%d, args=%p)\n", fun_in, n_args, args);
Damieneb19efb2013-10-10 22:06:54 +0100690
Damiend99b0522013-12-21 18:17:45 +0000691 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
692 nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "'int' object is not callable"));
Damien429d7192013-10-04 19:53:11 +0100693 } else {
Damiend99b0522013-12-21 18:17:45 +0000694 mp_obj_base_t *fun = fun_in;
695 if (fun->type->call_n != NULL) {
696 return fun->type->call_n(fun_in, n_args, args);
697 } else {
698 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "'%s' object is not callable", fun->type->name));
699 }
Damien429d7192013-10-04 19:53:11 +0100700 }
Damien429d7192013-10-04 19:53:11 +0100701}
702
Damien86c7fc72013-11-26 15:16:41 +0000703// args are in reverse order in the array; keyword arguments come first, value then key
704// eg: (value1, key1, value0, key0, arg1, arg0)
Damiend99b0522013-12-21 18:17:45 +0000705mp_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 +0000706 // TODO
707 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000708 return mp_const_none;
Damien86c7fc72013-11-26 15:16:41 +0000709}
710
Damiena3977762013-10-09 23:10:10 +0100711// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
712// if n_args==0 then there are only self/NULL and fun
Damiend99b0522013-12-21 18:17:45 +0000713mp_obj_t rt_call_method_n(uint n_args, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000714 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 +0100715 return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
716}
717
Damien86c7fc72013-11-26 15:16:41 +0000718// 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 +0000719mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000720 uint n = n_args + 2 * n_kw;
721 DEBUG_OP_printf("call method %p(self=%p, n_args=%u, n_kw=%u)\n", args[n + 1], args[n], n_args, n_kw);
722 return rt_call_function_n_kw(args[n + 1], n_args + ((args[n] == NULL) ? 0 : 1), n_kw, args);
723}
724
Damien429d7192013-10-04 19:53:11 +0100725// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000726mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
727 return mp_obj_new_tuple_reverse(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100728}
729
730// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000731mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
732 return mp_obj_new_list_reverse(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100733}
734
Damiend99b0522013-12-21 18:17:45 +0000735mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
736 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100737}
738
Damiend99b0522013-12-21 18:17:45 +0000739mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
740 mp_set_lookup(set, item, true);
Damienc12aa462013-10-16 20:57:49 +0100741 return set;
742}
743
Damien86c7fc72013-11-26 15:16:41 +0000744// unpacked items are stored in order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000745void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
746 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
747 uint seq_len;
748 mp_obj_t *seq_items;
749 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
750 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
751 } else {
752 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000753 }
Damiend99b0522013-12-21 18:17:45 +0000754 if (seq_len < num) {
755 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_ValueError, "need more than %d values to unpack", (void*)(machine_uint_t)seq_len));
756 } else if (seq_len > num) {
757 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_ValueError, "too many values to unpack (expected %d)", (void*)(machine_uint_t)num));
758 }
759 memcpy(items, seq_items, num * sizeof(mp_obj_t));
Damien86c7fc72013-11-26 15:16:41 +0000760 } else {
761 // TODO call rt_getiter and extract via rt_iternext
Damiend99b0522013-12-21 18:17:45 +0000762 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(seq_in)));
Damien86c7fc72013-11-26 15:16:41 +0000763 }
764}
765
Damiend99b0522013-12-21 18:17:45 +0000766mp_obj_t rt_build_map(int n_args) {
767 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100768}
769
Damiend99b0522013-12-21 18:17:45 +0000770mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
771 // map should always be a dict
772 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100773}
774
Damiend99b0522013-12-21 18:17:45 +0000775mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damiena3977762013-10-09 23:10:10 +0100776 DEBUG_OP_printf("load attr %s\n", qstr_str(attr));
Damiend99b0522013-12-21 18:17:45 +0000777 if (MP_OBJ_IS_TYPE(base, &class_type)) {
778 mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, false);
Damiena3977762013-10-09 23:10:10 +0100779 if (elem == NULL) {
Damiend99b0522013-12-21 18:17:45 +0000780 nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100781 }
782 return elem->value;
Damiend99b0522013-12-21 18:17:45 +0000783 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
784 return mp_obj_instance_load_attr(base, attr);
785 } else if (MP_OBJ_IS_OBJ(base)) {
786 // generic method lookup
787 mp_obj_base_t *o = base;
788 const mp_method_t *meth = &o->type->methods[0];
Damiend57eba52013-11-02 23:58:14 +0000789 for (; meth->name != NULL; meth++) {
790 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damiend99b0522013-12-21 18:17:45 +0000791 return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
792 }
793 }
794 }
795 nlr_jump(mp_obj_new_exception_msg_2_args(rt_q_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
796}
797
798void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
799 DEBUG_OP_printf("load method %s\n", qstr_str(attr));
800 if (MP_OBJ_IS_TYPE(base, &gen_instance_type) && attr == rt_q___next__) {
Damiend9d62012013-12-21 18:38:03 +0000801 dest[1] = (mp_obj_t)&mp_builtin_next_obj;
Damiend99b0522013-12-21 18:17:45 +0000802 dest[0] = base;
803 return;
804 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
805 mp_obj_instance_load_method(base, attr, dest);
806 return;
807 } else if (MP_OBJ_IS_OBJ(base)) {
808 // generic method lookup
809 mp_obj_base_t *o = base;
810 const mp_method_t *meth = &o->type->methods[0];
811 for (; meth->name != NULL; meth++) {
812 if (strcmp(meth->name, qstr_str(attr)) == 0) {
813 dest[1] = (mp_obj_t)meth->fun;
Damiend57eba52013-11-02 23:58:14 +0000814 dest[0] = base;
815 return;
816 }
817 }
Damiena3977762013-10-09 23:10:10 +0100818 }
819
Damiend99b0522013-12-21 18:17:45 +0000820 // no method; fallback to load_attr
Damiena3977762013-10-09 23:10:10 +0100821 dest[1] = rt_load_attr(base, attr);
822 dest[0] = NULL;
823}
824
Damiend99b0522013-12-21 18:17:45 +0000825void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100826 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damiend99b0522013-12-21 18:17:45 +0000827 if (MP_OBJ_IS_TYPE(base, &class_type)) {
Damienec63cce2013-10-22 22:58:17 +0100828 // TODO CPython allows STORE_ATTR to a class, but is this the correct implementation?
Damiend99b0522013-12-21 18:17:45 +0000829 mp_map_t *locals = mp_obj_class_get_locals(base);
830 mp_qstr_map_lookup(locals, attr, true)->value = value;
831 } else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
832 mp_obj_instance_store_attr(base, attr, value);
Damiena3977762013-10-09 23:10:10 +0100833 } else {
Damiend99b0522013-12-21 18:17:45 +0000834 printf("?AttributeError: '%s' object has no attribute '%s'\n", mp_obj_get_type_str(base), qstr_str(attr));
Damiena3977762013-10-09 23:10:10 +0100835 assert(0);
836 }
837}
838
Damiend99b0522013-12-21 18:17:45 +0000839void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100840 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000841 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100842 // list store
Damiend99b0522013-12-21 18:17:45 +0000843 mp_obj_list_store(base, index, value);
844 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
845 // dict store
846 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100847 } else {
848 assert(0);
849 }
850}
851
Damiend99b0522013-12-21 18:17:45 +0000852mp_obj_t rt_getiter(mp_obj_t o_in) {
853 if (MP_OBJ_IS_SMALL_INT(o_in)) {
854 nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100855 } else {
Damiend99b0522013-12-21 18:17:45 +0000856 mp_obj_base_t *o = o_in;
857 if (o->type->getiter != NULL) {
858 return o->type->getiter(o_in);
859 } else {
860 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "'%s' object is not iterable", o->type->name));
861 }
Damience89a212013-10-15 22:25:17 +0100862 }
863}
864
Damiend99b0522013-12-21 18:17:45 +0000865mp_obj_t rt_iternext(mp_obj_t o_in) {
866 if (MP_OBJ_IS_SMALL_INT(o_in)) {
867 nlr_jump(mp_obj_new_exception_msg(rt_q_TypeError, "? 'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100868 } else {
Damiend99b0522013-12-21 18:17:45 +0000869 mp_obj_base_t *o = o_in;
870 if (o->type->iternext != NULL) {
871 return o->type->iternext(o_in);
872 } else {
873 nlr_jump(mp_obj_new_exception_msg_1_arg(rt_q_TypeError, "? '%s' object is not iterable", o->type->name));
874 }
Damience89a212013-10-15 22:25:17 +0100875 }
876}
877
Damiend99b0522013-12-21 18:17:45 +0000878mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000879 // build args array
Damiend99b0522013-12-21 18:17:45 +0000880 mp_obj_t args[5];
881 args[0] = mp_obj_new_str(name);
882 args[1] = mp_const_none; // TODO should be globals
883 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000884 args[3] = fromlist;
885 args[4] = level; // must be 0; we don't yet support other values
886
887 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000888 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000889}
890
Damiend99b0522013-12-21 18:17:45 +0000891mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
892 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000893 /* TODO convert AttributeError to ImportError
894 if (fail) {
895 (ImportError, "cannot import name %s", qstr_str(name), NULL)
896 }
897 */
898 return x;
899}
900
Damien6ba13142013-11-02 20:34:54 +0000901// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100902void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000903 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100904 rt_load_const_str,
905 rt_load_name,
906 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100907 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +0100908 rt_load_attr,
909 rt_load_method,
910 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +0100911 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +0100912 rt_store_subscr,
913 rt_is_true,
914 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +0100915 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +0100916 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +0100917 rt_list_append,
Damien429d7192013-10-04 19:53:11 +0100918 rt_build_map,
919 rt_store_map,
920 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +0100921 rt_store_set,
Damien429d7192013-10-04 19:53:11 +0100922 rt_make_function_from_id,
Damieneb19efb2013-10-10 22:06:54 +0100923 rt_call_function_n,
Damien7f5dacf2013-10-10 11:24:39 +0100924 rt_call_method_n,
Damien429d7192013-10-04 19:53:11 +0100925 rt_binary_op,
926 rt_compare_op,
Damiend2755ec2013-10-16 23:58:48 +0100927 rt_getiter,
928 rt_iternext,
Damien429d7192013-10-04 19:53:11 +0100929};
930
931/*
932void rt_f_vector(rt_fun_kind_t fun_kind) {
933 (rt_f_table[fun_kind])();
934}
935*/