blob: 409d1d8262da28e7f2035c43fdece99c8dac42bc [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;
John R. Lenton9c83ec02014-01-07 23:06:46 +000064static machine_uint_t unique_codes_alloc = 0;
65static mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010066
Damien0446a0d2013-11-17 13:16:36 +000067#ifdef WRITE_CODE
68FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010069#endif
Damien429d7192013-10-04 19:53:11 +010070
Damien George38a2da62014-01-08 17:33:12 +000071// a good optimising compiler will inline this if necessary
72static void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
73 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
74}
75
Damien8b3a7c22013-10-23 20:20:17 +010076void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +010077 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +000078 map_locals = map_globals = mp_map_new(1);
79 mp_map_add_qstr(map_globals, MP_QSTR___name__, mp_obj_new_str(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +010080
Damienb86e3f92013-12-29 17:17:43 +000081 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +000082 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +000083
84 // built-in exceptions (TODO, make these proper classes)
Damien George38a2da62014-01-08 17:33:12 +000085 mp_map_add_qstr(&map_builtins, MP_QSTR_AttributeError, mp_obj_new_exception(MP_QSTR_AttributeError));
86 mp_map_add_qstr(&map_builtins, MP_QSTR_IndexError, mp_obj_new_exception(MP_QSTR_IndexError));
87 mp_map_add_qstr(&map_builtins, MP_QSTR_KeyError, mp_obj_new_exception(MP_QSTR_KeyError));
88 mp_map_add_qstr(&map_builtins, MP_QSTR_NameError, mp_obj_new_exception(MP_QSTR_NameError));
89 mp_map_add_qstr(&map_builtins, MP_QSTR_TypeError, mp_obj_new_exception(MP_QSTR_TypeError));
90 mp_map_add_qstr(&map_builtins, MP_QSTR_SyntaxError, mp_obj_new_exception(MP_QSTR_SyntaxError));
91 mp_map_add_qstr(&map_builtins, MP_QSTR_ValueError, mp_obj_new_exception(MP_QSTR_ValueError));
92 mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
Damienb86e3f92013-12-29 17:17:43 +000093
Damien Georgee9906ac2014-01-04 18:44:46 +000094 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +000095 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +000096
Damienb86e3f92013-12-29 17:17:43 +000097 // built-in core functions
Damien George38a2da62014-01-08 17:33:12 +000098 mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj);
99 mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, rt_make_function_1(mp_builtin___repl_print__));
Damienb86e3f92013-12-29 17:17:43 +0000100
Damien George71c51812014-01-04 20:21:15 +0000101 // built-in types
Damien George38a2da62014-01-08 17:33:12 +0000102 mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
Damien George71c51812014-01-04 20:21:15 +0000103#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000104 mp_map_add_qstr(&map_builtins, MP_QSTR_complex, (mp_obj_t)&complex_type);
Damien George71c51812014-01-04 20:21:15 +0000105#endif
Damien George38a2da62014-01-08 17:33:12 +0000106 mp_map_add_qstr(&map_builtins, MP_QSTR_dict, (mp_obj_t)&dict_type);
Damien George71c51812014-01-04 20:21:15 +0000107#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000108 mp_map_add_qstr(&map_builtins, MP_QSTR_float, (mp_obj_t)&float_type);
Damien George71c51812014-01-04 20:21:15 +0000109#endif
Damien George38a2da62014-01-08 17:33:12 +0000110 mp_map_add_qstr(&map_builtins, MP_QSTR_int, (mp_obj_t)&int_type);
111 mp_map_add_qstr(&map_builtins, MP_QSTR_list, (mp_obj_t)&list_type);
112 mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type);
113 mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
Damien George93a9b5b2014-01-08 18:48:12 +0000114 mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
Damien George71c51812014-01-04 20:21:15 +0000115
116 // built-in user functions; TODO covert all to &mp_builtin_xxx's
Damien George38a2da62014-01-08 17:33:12 +0000117 mp_map_add_qstr(&map_builtins, MP_QSTR_abs, rt_make_function_1(mp_builtin_abs));
118 mp_map_add_qstr(&map_builtins, MP_QSTR_all, rt_make_function_1(mp_builtin_all));
119 mp_map_add_qstr(&map_builtins, MP_QSTR_any, rt_make_function_1(mp_builtin_any));
120 mp_map_add_qstr(&map_builtins, MP_QSTR_callable, rt_make_function_1(mp_builtin_callable));
121 mp_map_add_qstr(&map_builtins, MP_QSTR_chr, rt_make_function_1(mp_builtin_chr));
122 mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, rt_make_function_2(mp_builtin_divmod));
123 mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
Damien George004cdce2014-01-09 21:43:51 +0000124 mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
125 mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);
Damien George38a2da62014-01-08 17:33:12 +0000126 mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj);
127 mp_map_add_qstr(&map_builtins, MP_QSTR_len, rt_make_function_1(mp_builtin_len));
128 mp_map_add_qstr(&map_builtins, MP_QSTR_max, rt_make_function_var(1, mp_builtin_max));
129 mp_map_add_qstr(&map_builtins, MP_QSTR_min, rt_make_function_var(1, mp_builtin_min));
130 mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj);
131 mp_map_add_qstr(&map_builtins, MP_QSTR_ord, rt_make_function_1(mp_builtin_ord));
132 mp_map_add_qstr(&map_builtins, MP_QSTR_pow, rt_make_function_var(2, mp_builtin_pow));
133 mp_map_add_qstr(&map_builtins, MP_QSTR_print, rt_make_function_var(0, mp_builtin_print));
134 mp_map_add_qstr(&map_builtins, MP_QSTR_range, rt_make_function_var(1, mp_builtin_range));
135 mp_map_add_qstr(&map_builtins, MP_QSTR_sum, rt_make_function_var(1, mp_builtin_sum));
Damien429d7192013-10-04 19:53:11 +0100136
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000137 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000138 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100139 unique_codes = NULL;
140
Damien0446a0d2013-11-17 13:16:36 +0000141#ifdef WRITE_CODE
142 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100143#endif
Damien429d7192013-10-04 19:53:11 +0100144}
145
Damien8b3a7c22013-10-23 20:20:17 +0100146void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000147 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Damien0446a0d2013-11-17 13:16:36 +0000148#ifdef WRITE_CODE
149 if (fp_write_code != NULL) {
150 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100151 }
Damiena1ddfcc2013-10-10 23:25:50 +0100152#endif
Damien429d7192013-10-04 19:53:11 +0100153}
154
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000155int rt_get_unique_code_id(void) {
156 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100157}
158
Damien8b3a7c22013-10-23 20:20:17 +0100159static void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000160 if (next_unique_code_id > unique_codes_alloc) {
161 // increase size of unique_codes table
162 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
163 for (int i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000164 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100165 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000166 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100167 }
Damien826005c2013-10-05 23:17:28 +0100168}
169
Damien George6baf76e2013-12-30 22:32:17 +0000170void 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 +0100171 alloc_unique_codes();
172
John R. Lenton9c83ec02014-01-07 23:06:46 +0000173 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
Damiend99b0522013-12-21 18:17:45 +0000174 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien826005c2013-10-05 23:17:28 +0100175 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100176 unique_codes[unique_code_id].n_locals = n_locals;
177 unique_codes[unique_code_id].n_stack = n_stack;
178 unique_codes[unique_code_id].is_generator = is_generator;
Damien826005c2013-10-05 23:17:28 +0100179 unique_codes[unique_code_id].u_byte.code = code;
180 unique_codes[unique_code_id].u_byte.len = len;
181
Damien9ecbcff2013-12-11 00:41:43 +0000182 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000183
184#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100185 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 +0000186 for (int i = 0; i < 128 && i < len; i++) {
187 if (i > 0 && i % 16 == 0) {
188 DEBUG_printf("\n");
189 }
190 DEBUG_printf(" %02x", code[i]);
191 }
192 DEBUG_printf("\n");
Damien George062478e2014-01-09 20:57:50 +0000193#if MICROPY_SHOW_BC
Damiend99b0522013-12-21 18:17:45 +0000194 extern void mp_show_byte_code(const byte *code, int len);
195 mp_show_byte_code(code, len);
Damien George062478e2014-01-09 20:57:50 +0000196#endif
Damien0446a0d2013-11-17 13:16:36 +0000197
198#ifdef WRITE_CODE
199 if (fp_write_code != NULL) {
200 fwrite(code, len, 1, fp_write_code);
201 fflush(fp_write_code);
202 }
203#endif
204#endif
Damien826005c2013-10-05 23:17:28 +0100205}
206
Damiend99b0522013-12-21 18:17:45 +0000207void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100208 alloc_unique_codes();
209
John R. Lenton9c83ec02014-01-07 23:06:46 +0000210 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
Damiend99b0522013-12-21 18:17:45 +0000211 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien429d7192013-10-04 19:53:11 +0100212 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100213 unique_codes[unique_code_id].n_locals = 0;
214 unique_codes[unique_code_id].n_stack = 0;
215 unique_codes[unique_code_id].is_generator = false;
Damien429d7192013-10-04 19:53:11 +0100216 unique_codes[unique_code_id].u_native.fun = fun;
217
Damien George8cc96a32013-12-30 18:23:50 +0000218 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000219
Damiena1ddfcc2013-10-10 23:25:50 +0100220#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100221 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
222 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
223 for (int i = 0; i < 128 && i < len; i++) {
224 if (i > 0 && i % 16 == 0) {
225 DEBUG_printf("\n");
226 }
227 DEBUG_printf(" %02x", fun_data[i]);
228 }
229 DEBUG_printf("\n");
230
Damien0446a0d2013-11-17 13:16:36 +0000231#ifdef WRITE_CODE
232 if (fp_write_code != NULL) {
233 fwrite(fun_data, len, 1, fp_write_code);
234 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100235 }
Damiena1ddfcc2013-10-10 23:25:50 +0100236#endif
237#endif
Damien429d7192013-10-04 19:53:11 +0100238}
239
Damiend99b0522013-12-21 18:17:45 +0000240void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100241 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100242
John R. Lenton9c83ec02014-01-07 23:06:46 +0000243 assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
Damiend99b0522013-12-21 18:17:45 +0000244 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien826005c2013-10-05 23:17:28 +0100245 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100246 unique_codes[unique_code_id].n_locals = 0;
247 unique_codes[unique_code_id].n_stack = 0;
248 unique_codes[unique_code_id].is_generator = false;
Damien826005c2013-10-05 23:17:28 +0100249 unique_codes[unique_code_id].u_inline_asm.fun = fun;
250
Damiena1ddfcc2013-10-10 23:25:50 +0100251#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100252 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
253 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
254 for (int i = 0; i < 128 && i < len; i++) {
255 if (i > 0 && i % 16 == 0) {
256 DEBUG_printf("\n");
257 }
258 DEBUG_printf(" %02x", fun_data[i]);
259 }
260 DEBUG_printf("\n");
261
Damien0446a0d2013-11-17 13:16:36 +0000262#ifdef WRITE_CODE
263 if (fp_write_code != NULL) {
264 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100265 }
Damiena1ddfcc2013-10-10 23:25:50 +0100266#endif
267#endif
Damien429d7192013-10-04 19:53:11 +0100268}
269
Damiend99b0522013-12-21 18:17:45 +0000270static bool fit_small_int(mp_small_int_t o) {
271 return true;
272}
273
274int rt_is_true(mp_obj_t arg) {
275 DEBUG_OP_printf("is true %p\n", arg);
276 if (MP_OBJ_IS_SMALL_INT(arg)) {
277 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
278 return 0;
279 } else {
280 return 1;
281 }
282 } else if (arg == mp_const_none) {
283 return 0;
284 } else if (arg == mp_const_false) {
285 return 0;
286 } else if (arg == mp_const_true) {
287 return 1;
288 } else {
289 assert(0);
290 return 0;
291 }
292}
293
294mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
295 return mp_obj_list_append(self_in, arg);
296}
297
Damien7410e442013-11-02 19:47:57 +0000298#define PARSE_DEC_IN_INTG (1)
299#define PARSE_DEC_IN_FRAC (2)
300#define PARSE_DEC_IN_EXP (3)
301
Damiend99b0522013-12-21 18:17:45 +0000302mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000303#if MICROPY_ENABLE_FLOAT
304 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
305 const char *s = qstr_str(qstr);
306 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000307 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000308 bool exp_neg = false;
309 int exp_val = 0;
310 int exp_extra = 0;
311 bool imag = false;
312 for (; *s; s++) {
313 int dig = *s;
314 if ('0' <= dig && dig <= '9') {
315 dig -= '0';
316 if (in == PARSE_DEC_IN_EXP) {
317 exp_val = 10 * exp_val + dig;
318 } else {
319 dec_val = 10 * dec_val + dig;
320 if (in == PARSE_DEC_IN_FRAC) {
321 exp_extra -= 1;
322 }
323 }
324 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
325 in = PARSE_DEC_IN_FRAC;
326 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
327 in = PARSE_DEC_IN_EXP;
328 if (s[1] == '+') {
329 s++;
330 } else if (s[1] == '-') {
331 s++;
332 exp_neg = true;
333 }
334 } else if (dig == 'J' || dig == 'j') {
335 s++;
336 imag = true;
337 break;
338 } else {
339 // unknown character
340 break;
341 }
342 }
343 if (*s != 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000344 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000345 }
346 if (exp_neg) {
347 exp_val = -exp_val;
348 }
349 exp_val += exp_extra;
350 for (; exp_val > 0; exp_val--) {
351 dec_val *= 10;
352 }
353 for (; exp_val < 0; exp_val++) {
354 dec_val *= 0.1;
355 }
356 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000357 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000358 } else {
Damiend99b0522013-12-21 18:17:45 +0000359 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000360 }
361#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000362 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000363#endif
364}
365
Damiend99b0522013-12-21 18:17:45 +0000366mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100367 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000368 return mp_obj_new_str(qstr);
Damien429d7192013-10-04 19:53:11 +0100369}
370
Damiend99b0522013-12-21 18:17:45 +0000371mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100372 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100373 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000374 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100375 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000376 elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100377 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000378 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100379 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000380 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damiena3977762013-10-09 23:10:10 +0100381 }
382 }
383 }
384 return elem->value;
385}
386
Damiend99b0522013-12-21 18:17:45 +0000387mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100388 // logic: search globals, builtins
389 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000390 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100391 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000392 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100393 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000394 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100395 }
396 }
397 return elem->value;
398}
399
Damiend99b0522013-12-21 18:17:45 +0000400mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100401 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000402 mp_map_elem_t *elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100403 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000404 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
Damien429d7192013-10-04 19:53:11 +0100405 }
406 return elem->value;
407}
408
Damiend99b0522013-12-21 18:17:45 +0000409mp_obj_t rt_get_cell(mp_obj_t cell) {
410 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000411}
412
Damiend99b0522013-12-21 18:17:45 +0000413void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
414 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000415}
416
Damiend99b0522013-12-21 18:17:45 +0000417void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100418 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000419 mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = obj;
Damiena3977762013-10-09 23:10:10 +0100420}
421
Damiend99b0522013-12-21 18:17:45 +0000422void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100423 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000424 mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = obj;
Damien429d7192013-10-04 19:53:11 +0100425}
426
Damiend99b0522013-12-21 18:17:45 +0000427mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000428 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000429 if (MP_OBJ_IS_SMALL_INT(arg)) {
430 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000431 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000432 case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
Damien7410e442013-11-02 19:47:57 +0000433 case RT_UNARY_OP_POSITIVE: break;
434 case RT_UNARY_OP_NEGATIVE: val = -val; break;
435 case RT_UNARY_OP_INVERT: val = ~val; break;
436 default: assert(0); val = 0;
437 }
438 if (fit_small_int(val)) {
Damiend99b0522013-12-21 18:17:45 +0000439 return MP_OBJ_NEW_SMALL_INT(val);
440 } else {
441 // TODO make a bignum
442 assert(0);
443 return mp_const_none;
Damien7410e442013-11-02 19:47:57 +0000444 }
Damiend99b0522013-12-21 18:17:45 +0000445 } else { // will be an object (small ints are caught in previous if)
446 mp_obj_base_t *o = arg;
447 if (o->type->unary_op != NULL) {
448 mp_obj_t result = o->type->unary_op(op, arg);
449 if (result != NULL) {
450 return result;
451 }
Damien7410e442013-11-02 19:47:57 +0000452 }
Damiend99b0522013-12-21 18:17:45 +0000453 // TODO specify in error message what the operator is
Damien George6c73ca12014-01-08 18:11:23 +0000454 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", o->type->name));
Damien7410e442013-11-02 19:47:57 +0000455 }
Damien429d7192013-10-04 19:53:11 +0100456}
457
Damiend99b0522013-12-21 18:17:45 +0000458mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100459 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000460
461 // TODO correctly distinguish inplace operators for mutable objects
462 // lookup logic that CPython uses for +=:
463 // check for implemented +=
464 // then check for implemented +
465 // then check for implemented seq.inplace_concat
466 // then check for implemented seq.concat
467 // then fail
468 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
469
Damien Georgebcbeea02014-01-11 10:47:22 +0000470 // deal with == and != for all types
471 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
472 if (mp_obj_equal(lhs, rhs)) {
473 if (op == RT_COMPARE_OP_EQUAL) {
474 return mp_const_true;
475 } else {
476 return mp_const_false;
477 }
478 } else {
479 if (op == RT_COMPARE_OP_EQUAL) {
480 return mp_const_false;
481 } else {
482 return mp_const_true;
483 }
484 }
485 }
486
487 // deal with exception_match for all types
488 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
489 // TODO properly! at the moment it just compares the exception identifier for equality
490 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
491 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
492 return mp_const_true;
493 } else {
494 return mp_const_false;
495 }
496 }
497 }
498
Damien George1a9951d2014-01-06 22:13:00 +0000499 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000500 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000501 if (MP_OBJ_IS_SMALL_INT(rhs)) {
502 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
503 switch (op) {
504 case RT_BINARY_OP_OR:
505 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
506 case RT_BINARY_OP_XOR:
507 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
508 case RT_BINARY_OP_AND:
509 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
510 case RT_BINARY_OP_LSHIFT:
511 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
512 case RT_BINARY_OP_RSHIFT:
513 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
514 case RT_BINARY_OP_ADD:
515 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
516 case RT_BINARY_OP_SUBTRACT:
517 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
518 case RT_BINARY_OP_MULTIPLY:
519 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
520 case RT_BINARY_OP_FLOOR_DIVIDE:
521 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
522 #if MICROPY_ENABLE_FLOAT
523 case RT_BINARY_OP_TRUE_DIVIDE:
524 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
525 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000526
Damien George1a9951d2014-01-06 22:13:00 +0000527 // TODO implement modulo as specified by Python
528 case RT_BINARY_OP_MODULO:
529 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000530
Damien George1a9951d2014-01-06 22:13:00 +0000531 // TODO check for negative power, and overflow
532 case RT_BINARY_OP_POWER:
533 case RT_BINARY_OP_INPLACE_POWER:
534 {
535 int ans = 1;
536 while (rhs_val > 0) {
537 if (rhs_val & 1) {
538 ans *= lhs_val;
539 }
540 lhs_val *= lhs_val;
541 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000542 }
Damien George1a9951d2014-01-06 22:13:00 +0000543 lhs_val = ans;
544 break;
Damien4ebb32f2013-11-02 14:33:10 +0000545 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000546 case RT_COMPARE_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
547 case RT_COMPARE_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
548 case RT_COMPARE_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
549 case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000550
Damien George1a9951d2014-01-06 22:13:00 +0000551 default: assert(0);
552 }
553 if (fit_small_int(lhs_val)) {
554 return MP_OBJ_NEW_SMALL_INT(lhs_val);
555 }
556 } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
557 return mp_obj_float_binary_op(op, lhs_val, rhs);
558 } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
559 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien429d7192013-10-04 19:53:11 +0100560 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000561 } else {
John R. Lentonb8698fc2014-01-11 00:58:59 +0000562 if (MP_OBJ_IS_OBJ(lhs)) {
563 mp_obj_base_t *o = lhs;
564 if (o->type->binary_op != NULL) {
565 mp_obj_t result = o->type->binary_op(op, lhs, rhs);
566 if (result != NULL) {
567 return result;
568 }
Damien7410e442013-11-02 19:47:57 +0000569 }
Damien7410e442013-11-02 19:47:57 +0000570 }
Damien429d7192013-10-04 19:53:11 +0100571 }
Damiend99b0522013-12-21 18:17:45 +0000572
573 // TODO specify in error message what the operator is
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200574 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError,
575 "unsupported operand types for binary operator: '%s', '%s'",
576 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
Damien429d7192013-10-04 19:53:11 +0100577}
578
Damiend99b0522013-12-21 18:17:45 +0000579mp_obj_t rt_make_function_from_id(int unique_code_id) {
Damienb05d7072013-10-05 13:37:10 +0100580 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
581 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100582 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000583 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100584 }
Damiend99b0522013-12-21 18:17:45 +0000585
586 // make the function, depending on the code kind
587 mp_code_t *c = &unique_codes[unique_code_id];
588 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100589 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000590 case MP_CODE_BYTE:
Damien George6baf76e2013-12-30 22:32:17 +0000591 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 +0100592 break;
Damiend99b0522013-12-21 18:17:45 +0000593 case MP_CODE_NATIVE:
Damien429d7192013-10-04 19:53:11 +0100594 switch (c->n_args) {
Damiend99b0522013-12-21 18:17:45 +0000595 case 0: fun = rt_make_function_0(c->u_native.fun); break;
596 case 1: fun = rt_make_function_1((mp_fun_1_t)c->u_native.fun); break;
597 case 2: fun = rt_make_function_2((mp_fun_2_t)c->u_native.fun); break;
598 default: assert(0); fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100599 }
Damien429d7192013-10-04 19:53:11 +0100600 break;
Damiend99b0522013-12-21 18:17:45 +0000601 case MP_CODE_INLINE_ASM:
602 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100603 break;
604 default:
605 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000606 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100607 }
Damienbd254452013-10-16 20:39:12 +0100608
609 // check for generator functions and if so wrap in generator object
610 if (c->is_generator) {
Damien George6baf76e2013-12-30 22:32:17 +0000611 fun = mp_obj_new_gen_wrap(c->n_locals, c->n_stack, fun);
Damienbd254452013-10-16 20:39:12 +0100612 }
613
Damiend99b0522013-12-21 18:17:45 +0000614 return fun;
Damien429d7192013-10-04 19:53:11 +0100615}
616
Damiend99b0522013-12-21 18:17:45 +0000617mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000618 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000619 // make function object
620 mp_obj_t ffun = rt_make_function_from_id(unique_code_id);
Damien9ecbcff2013-12-11 00:41:43 +0000621 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000622 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000623}
624
Damiend99b0522013-12-21 18:17:45 +0000625mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damieneb19efb2013-10-10 22:06:54 +0100626 return rt_call_function_n(fun, 0, NULL);
627}
628
Damiend99b0522013-12-21 18:17:45 +0000629mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damieneb19efb2013-10-10 22:06:54 +0100630 return rt_call_function_n(fun, 1, &arg);
631}
632
Damiend99b0522013-12-21 18:17:45 +0000633mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
634 mp_obj_t args[2];
Damieneb19efb2013-10-10 22:06:54 +0100635 args[1] = arg1;
636 args[0] = arg2;
637 return rt_call_function_n(fun, 2, args);
638}
639
Damieneb19efb2013-10-10 22:06:54 +0100640// args are in reverse order in the array
Damiend99b0522013-12-21 18:17:45 +0000641mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
642 // TODO improve this: fun object can specify its type and we parse here the arguments,
643 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100644
Damiend99b0522013-12-21 18:17:45 +0000645 DEBUG_OP_printf("calling function %p(n_args=%d, args=%p)\n", fun_in, n_args, args);
Damieneb19efb2013-10-10 22:06:54 +0100646
Damiend99b0522013-12-21 18:17:45 +0000647 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000648 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
Damien429d7192013-10-04 19:53:11 +0100649 } else {
Damiend99b0522013-12-21 18:17:45 +0000650 mp_obj_base_t *fun = fun_in;
651 if (fun->type->call_n != NULL) {
652 return fun->type->call_n(fun_in, n_args, args);
653 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000654 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name));
Damiend99b0522013-12-21 18:17:45 +0000655 }
Damien429d7192013-10-04 19:53:11 +0100656 }
Damien429d7192013-10-04 19:53:11 +0100657}
658
Damien86c7fc72013-11-26 15:16:41 +0000659// args are in reverse order in the array; keyword arguments come first, value then key
660// eg: (value1, key1, value0, key0, arg1, arg0)
John R. Lenton9c83ec02014-01-07 23:06:46 +0000661mp_obj_t rt_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
662 // TODO merge this and _n into a single, smarter thing
663 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
664
665 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
666 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
667 } else {
668 mp_obj_base_t *fun = fun_in;
669 if (fun->type->call_n_kw != NULL) {
670 return fun->type->call_n_kw(fun_in, n_args, n_kw, args);
671 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000672 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000673 }
674 }
Damien86c7fc72013-11-26 15:16:41 +0000675}
676
Damiena3977762013-10-09 23:10:10 +0100677// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
678// if n_args==0 then there are only self/NULL and fun
Damiend99b0522013-12-21 18:17:45 +0000679mp_obj_t rt_call_method_n(uint n_args, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000680 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 +0100681 return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
682}
683
Damien86c7fc72013-11-26 15:16:41 +0000684// 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 +0000685mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000686 uint n = n_args + 2 * n_kw;
687 DEBUG_OP_printf("call method %p(self=%p, n_args=%u, n_kw=%u)\n", args[n + 1], args[n], n_args, n_kw);
688 return rt_call_function_n_kw(args[n + 1], n_args + ((args[n] == NULL) ? 0 : 1), n_kw, args);
689}
690
Damien429d7192013-10-04 19:53:11 +0100691// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000692mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
693 return mp_obj_new_tuple_reverse(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100694}
695
696// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000697mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
698 return mp_obj_new_list_reverse(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100699}
700
Damiend99b0522013-12-21 18:17:45 +0000701mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
702 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100703}
704
Damiend99b0522013-12-21 18:17:45 +0000705mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000706 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100707 return set;
708}
709
Damien86c7fc72013-11-26 15:16:41 +0000710// unpacked items are stored in order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000711void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
712 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
713 uint seq_len;
714 mp_obj_t *seq_items;
715 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
716 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
717 } else {
718 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000719 }
Damiend99b0522013-12-21 18:17:45 +0000720 if (seq_len < num) {
Damien George6c73ca12014-01-08 18:11:23 +0000721 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ValueError, "need more than %d values to unpack", (void*)(machine_uint_t)seq_len));
Damiend99b0522013-12-21 18:17:45 +0000722 } else if (seq_len > num) {
Damien George6c73ca12014-01-08 18:11:23 +0000723 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ValueError, "too many values to unpack (expected %d)", (void*)(machine_uint_t)num));
Damiend99b0522013-12-21 18:17:45 +0000724 }
725 memcpy(items, seq_items, num * sizeof(mp_obj_t));
Damien86c7fc72013-11-26 15:16:41 +0000726 } else {
727 // TODO call rt_getiter and extract via rt_iternext
Damien George6c73ca12014-01-08 18:11:23 +0000728 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(seq_in)));
Damien86c7fc72013-11-26 15:16:41 +0000729 }
730}
731
Damiend99b0522013-12-21 18:17:45 +0000732mp_obj_t rt_build_map(int n_args) {
733 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100734}
735
Damiend99b0522013-12-21 18:17:45 +0000736mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
737 // map should always be a dict
738 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100739}
740
Damiend99b0522013-12-21 18:17:45 +0000741mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000742 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
743 // use load_method
744 mp_obj_t dest[2];
745 rt_load_method(base, attr, dest);
746 if (dest[0] == NULL) {
747 // load_method returned just a normal attribute
748 return dest[1];
749 } else {
750 // load_method returned a method, so build a bound method object
751 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000752 }
Damiend99b0522013-12-21 18:17:45 +0000753}
754
755void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000756 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
757
758 // clear output to indicate no attribute/method found yet
759 dest[0] = MP_OBJ_NULL;
760 dest[1] = MP_OBJ_NULL;
761
762 // get the type
763 mp_obj_type_t *type = mp_obj_get_type(base);
764
765 // if this type can do its own load, then call it
766 if (type->load_attr != NULL) {
767 type->load_attr(base, attr, dest);
768 }
769
770 // if nothing found yet, look for built-in and generic names
771 if (dest[1] == NULL) {
772 if (attr == MP_QSTR___next__ && type->iternext != NULL) {
773 dest[1] = (mp_obj_t)&mp_builtin_next_obj;
774 dest[0] = base;
775 } else {
776 // generic method lookup
Damien Georgeeae16442014-01-11 19:22:29 +0000777 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000778 const mp_method_t *meth = type->methods;
779 if (meth != NULL) {
780 for (; meth->name != NULL; meth++) {
781 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000782 // check if the methods are functions, static or class methods
783 // see http://docs.python.org/3.3/howto/descriptor.html
784 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
785 // return just the function
786 dest[1] = ((mp_obj_staticmethod_t*)meth->fun)->fun;
787 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
788 // return a bound method, with self being the type of this object
789 dest[1] = ((mp_obj_classmethod_t*)meth->fun)->fun;
790 dest[0] = mp_obj_get_type(base);
791 } else {
792 // return a bound method, with self being this object
793 dest[1] = (mp_obj_t)meth->fun;
794 dest[0] = base;
795 }
Damien George062478e2014-01-09 20:57:50 +0000796 break;
797 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000798 }
Damiend57eba52013-11-02 23:58:14 +0000799 }
800 }
Damiena3977762013-10-09 23:10:10 +0100801 }
802
Damien George062478e2014-01-09 20:57:50 +0000803 if (dest[1] == NULL) {
804 // no attribute/method called attr
805 // following CPython, we give a more detailed error message for type objects
806 if (MP_OBJ_IS_TYPE(base, &mp_const_type)) {
807 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_AttributeError, "type object '%s' has no attribute '%s'", ((mp_obj_type_t*)base)->name, qstr_str(attr)));
808 } else {
809 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
810 }
811 }
Damiena3977762013-10-09 23:10:10 +0100812}
813
Damiend99b0522013-12-21 18:17:45 +0000814void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100815 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000816 mp_obj_type_t *type = mp_obj_get_type(base);
817 if (type->store_attr != NULL) {
818 if (type->store_attr(base, attr, value)) {
819 return;
820 }
Damiena3977762013-10-09 23:10:10 +0100821 }
Damien George062478e2014-01-09 20:57:50 +0000822 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100823}
824
Damiend99b0522013-12-21 18:17:45 +0000825void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100826 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000827 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100828 // list store
Damiend99b0522013-12-21 18:17:45 +0000829 mp_obj_list_store(base, index, value);
830 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
831 // dict store
832 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100833 } else {
834 assert(0);
835 }
836}
837
Damiend99b0522013-12-21 18:17:45 +0000838mp_obj_t rt_getiter(mp_obj_t o_in) {
839 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000840 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100841 } else {
Damiend99b0522013-12-21 18:17:45 +0000842 mp_obj_base_t *o = o_in;
843 if (o->type->getiter != NULL) {
844 return o->type->getiter(o_in);
845 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000846 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not iterable", o->type->name));
Damiend99b0522013-12-21 18:17:45 +0000847 }
Damience89a212013-10-15 22:25:17 +0100848 }
849}
850
Damiend99b0522013-12-21 18:17:45 +0000851mp_obj_t rt_iternext(mp_obj_t o_in) {
852 if (MP_OBJ_IS_SMALL_INT(o_in)) {
John R. Lenton88f30432014-01-06 22:58:17 +0000853 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not an iterator"));
Damience89a212013-10-15 22:25:17 +0100854 } else {
Damiend99b0522013-12-21 18:17:45 +0000855 mp_obj_base_t *o = o_in;
856 if (o->type->iternext != NULL) {
857 return o->type->iternext(o_in);
858 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000859 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not an iterator", o->type->name));
Damiend99b0522013-12-21 18:17:45 +0000860 }
Damience89a212013-10-15 22:25:17 +0100861 }
862}
863
Damiend99b0522013-12-21 18:17:45 +0000864mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000865 // build args array
Damiend99b0522013-12-21 18:17:45 +0000866 mp_obj_t args[5];
867 args[0] = mp_obj_new_str(name);
868 args[1] = mp_const_none; // TODO should be globals
869 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000870 args[3] = fromlist;
871 args[4] = level; // must be 0; we don't yet support other values
872
873 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000874 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000875}
876
Damiend99b0522013-12-21 18:17:45 +0000877mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
878 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000879 /* TODO convert AttributeError to ImportError
880 if (fail) {
881 (ImportError, "cannot import name %s", qstr_str(name), NULL)
882 }
883 */
884 return x;
885}
886
Damien George66028ab2014-01-03 14:03:48 +0000887mp_map_t *rt_locals_get(void) {
888 return map_locals;
889}
890
891void rt_locals_set(mp_map_t *m) {
892 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
893 map_locals = m;
894}
895
896mp_map_t *rt_globals_get(void) {
897 return map_globals;
898}
899
900void rt_globals_set(mp_map_t *m) {
901 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
902 map_globals = m;
903}
904
Damien6ba13142013-11-02 20:34:54 +0000905// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100906void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000907 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100908 rt_load_const_str,
909 rt_load_name,
910 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100911 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +0100912 rt_load_attr,
913 rt_load_method,
914 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +0100915 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +0100916 rt_store_subscr,
917 rt_is_true,
918 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +0100919 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +0100920 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +0100921 rt_list_append,
Damien429d7192013-10-04 19:53:11 +0100922 rt_build_map,
923 rt_store_map,
924 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +0100925 rt_store_set,
Damien429d7192013-10-04 19:53:11 +0100926 rt_make_function_from_id,
Damieneb19efb2013-10-10 22:06:54 +0100927 rt_call_function_n,
Damien7f5dacf2013-10-10 11:24:39 +0100928 rt_call_method_n,
Damien429d7192013-10-04 19:53:11 +0100929 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +0100930 rt_getiter,
931 rt_iternext,
Damien429d7192013-10-04 19:53:11 +0100932};
933
934/*
935void rt_f_vector(rt_fun_kind_t fun_kind) {
936 (rt_f_table[fun_kind])();
937}
938*/