blob: b08ae3d4e78f5397b47dcc8935e4eb9c7e5b7f0e [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 George55baff42014-01-21 21:40:13 +000014#include "qstr.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"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020020#include "objarray.h"
Damien George5fa93b62014-01-22 14:35:10 +000021#include "bc.h"
Damien660365e2013-12-17 18:27:24 +000022
Damien7f5dacf2013-10-10 11:24:39 +010023#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010024#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000025#define WRITE_CODE (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020026#define DEBUG_printf DEBUG_printf
27#define DEBUG_OP_printf(args...) DEBUG_printf(args)
Damien7f5dacf2013-10-10 11:24:39 +010028#else // don't print debugging info
Damiena3977762013-10-09 23:10:10 +010029#define DEBUG_printf(args...) (void)0
Damien429d7192013-10-04 19:53:11 +010030#define DEBUG_OP_printf(args...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010031#endif
Damien429d7192013-10-04 19:53:11 +010032
Damieneb19efb2013-10-10 22:06:54 +010033// locals and globals need to be pointers because they can be the same in outer module scope
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020034STATIC mp_map_t *map_locals;
35STATIC mp_map_t *map_globals;
36STATIC mp_map_t map_builtins;
37STATIC mp_map_t map_loaded_modules; // TODO: expose as sys.modules
Damienbd254452013-10-16 20:39:12 +010038
Damien429d7192013-10-04 19:53:11 +010039typedef enum {
Damiend99b0522013-12-21 18:17:45 +000040 MP_CODE_NONE,
41 MP_CODE_BYTE,
42 MP_CODE_NATIVE,
43 MP_CODE_INLINE_ASM,
44} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010045
Damiend99b0522013-12-21 18:17:45 +000046typedef struct _mp_code_t {
Damien Georged0691cc2014-01-29 20:30:52 +000047 struct {
48 mp_code_kind_t kind : 8;
Damien George8725f8f2014-02-15 19:33:11 +000049 uint scope_flags : 8;
Damien Georged0691cc2014-01-29 20:30:52 +000050 };
51 struct {
52 uint n_args : 16;
53 uint n_state : 16;
54 };
Damien429d7192013-10-04 19:53:11 +010055 union {
56 struct {
Damien429d7192013-10-04 19:53:11 +010057 byte *code;
58 uint len;
59 } u_byte;
Damien826005c2013-10-05 23:17:28 +010060 struct {
Damiend99b0522013-12-21 18:17:45 +000061 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010062 } u_native;
63 struct {
Damiene4af64f2013-10-06 12:04:13 +010064 void *fun;
Damien826005c2013-10-05 23:17:28 +010065 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010066 };
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020067 qstr *arg_names;
Damiend99b0522013-12-21 18:17:45 +000068} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010069
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020070STATIC uint next_unique_code_id;
71STATIC machine_uint_t unique_codes_alloc = 0;
72STATIC mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010073
Damien0446a0d2013-11-17 13:16:36 +000074#ifdef WRITE_CODE
75FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010076#endif
Damien429d7192013-10-04 19:53:11 +010077
Damien Georgeaea532e2014-02-06 22:57:51 +000078// builtins
79// we put this table in ROM because it's always needed and takes up quite a bit of room in RAM
80// in fact, it uses less ROM here in table form than the equivalent in code form initialising a dynamic mp_map_t object in RAM
81// at the moment it's a linear table, but we could convert it to a const mp_map_t table with a simple preprocessing script
82// if we wanted to allow dynamic modification of the builtins, we could provide an mp_map_t object which is searched before this one
83
84typedef struct _mp_builtin_elem_t {
85 qstr qstr;
86 mp_obj_t fun;
87} mp_builtin_elem_t;
88
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020089STATIC const mp_builtin_elem_t builtin_table[] = {
Damien Georgeaea532e2014-02-06 22:57:51 +000090 // built-in core functions
91 { MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj },
92 { MP_QSTR___import__, (mp_obj_t)&mp_builtin___import___obj },
93 { MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
94
95 // built-in types
96 { MP_QSTR_bool, (mp_obj_t)&bool_type },
97#if MICROPY_ENABLE_FLOAT
98 { MP_QSTR_complex, (mp_obj_t)&complex_type },
99#endif
100 { MP_QSTR_dict, (mp_obj_t)&dict_type },
101 { MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
102 { MP_QSTR_filter, (mp_obj_t)&filter_type },
103#if MICROPY_ENABLE_FLOAT
104 { MP_QSTR_float, (mp_obj_t)&float_type },
105#endif
106 { MP_QSTR_int, (mp_obj_t)&int_type },
107 { MP_QSTR_list, (mp_obj_t)&list_type },
108 { MP_QSTR_map, (mp_obj_t)&map_type },
109 { MP_QSTR_set, (mp_obj_t)&set_type },
110 { MP_QSTR_super, (mp_obj_t)&super_type },
111 { MP_QSTR_tuple, (mp_obj_t)&tuple_type },
Damien Georgec5966122014-02-15 16:10:44 +0000112 { MP_QSTR_type, (mp_obj_t)&mp_type_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000113 { MP_QSTR_zip, (mp_obj_t)&zip_type },
114
115 { MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
116 { MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
117
118 // built-in user functions
119 { MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
120 { MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
121 { MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
122 { MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj },
123 { MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
124 { MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
125 { MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
126 { MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
127 { MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
128 { MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
129 { MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
130 { MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
131 { MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
132 { MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
133 { MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
134 { MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
135 { MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
136 { MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
137 { MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
138 { MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
139 { MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
140 { MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
141 { MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
142 { MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
143 { MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
144 { MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
145 { MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj },
146 { MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
147
Damien Georgec5966122014-02-15 16:10:44 +0000148 // built-in exceptions
149 { MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
150 { MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
151 { MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
152 { MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
153 { MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
154 { MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
155 { MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
156 { MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
157 { MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
158 { MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
159 { MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
160 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
161 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
162 { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
163 { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
164 { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
165 { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
166
Paul Sokolovsky910843e2014-02-14 12:02:34 +0200167 // Extra builtins as defined by a port
168 MICROPY_EXTRA_BUILTINS
169
Damien Georgeaea532e2014-02-06 22:57:51 +0000170 { MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
171};
172
Damien George38a2da62014-01-08 17:33:12 +0000173// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200174STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +0000175 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
176}
177
Damien8b3a7c22013-10-23 20:20:17 +0100178void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +0100179 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +0000180 map_locals = map_globals = mp_map_new(1);
Damien George5fa93b62014-01-22 14:35:10 +0000181 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +0100182
Damienb86e3f92013-12-29 17:17:43 +0000183 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +0000184 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +0000185
Damien George0d028742014-01-22 23:59:20 +0000186 // init loaded modules table
187 mp_map_init(&map_loaded_modules, 3);
188
Damien Georgee9906ac2014-01-04 18:44:46 +0000189 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000190 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000191
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200192 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
193 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200194
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200195#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +0200196 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200197 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
198 // Avoid warning of unused var
199 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200200#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200201 // init sys.path
202 // for efficiency, left to platform-specific startup code
203 //sys_path = mp_obj_new_list(0, NULL);
204 //rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200205
Damien George91d457a2014-01-20 10:30:24 +0000206 mp_module_micropython_init();
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +0200207
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200208 // TODO: wastes one mp_code_t structure in mem
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000209 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000210 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100211 unique_codes = NULL;
212
Damien0446a0d2013-11-17 13:16:36 +0000213#ifdef WRITE_CODE
214 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100215#endif
Damien429d7192013-10-04 19:53:11 +0100216}
217
Damien8b3a7c22013-10-23 20:20:17 +0100218void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000219 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200220 mp_map_free(map_globals);
221 mp_map_deinit(&map_loaded_modules);
222 mp_map_deinit(&map_builtins);
Damien0446a0d2013-11-17 13:16:36 +0000223#ifdef WRITE_CODE
224 if (fp_write_code != NULL) {
225 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100226 }
Damiena1ddfcc2013-10-10 23:25:50 +0100227#endif
Damien429d7192013-10-04 19:53:11 +0100228}
229
Damien Georged0691cc2014-01-29 20:30:52 +0000230uint rt_get_unique_code_id(void) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000231 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100232}
233
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200234STATIC void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000235 if (next_unique_code_id > unique_codes_alloc) {
Damien Georged0691cc2014-01-29 20:30:52 +0000236 DEBUG_printf("allocate more unique codes: " UINT_FMT " -> %u\n", unique_codes_alloc, next_unique_code_id);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000237 // increase size of unique_codes table
238 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
Damien Georged0691cc2014-01-29 20:30:52 +0000239 for (uint i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000240 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100241 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000242 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100243 }
Damien826005c2013-10-05 23:17:28 +0100244}
245
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200246void rt_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, uint scope_flags, qstr *arg_names) {
Damien826005c2013-10-05 23:17:28 +0100247 alloc_unique_codes();
248
John R. Lenton9c83ec02014-01-07 23:06:46 +0000249 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 +0000250 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien George8725f8f2014-02-15 19:33:11 +0000251 unique_codes[unique_code_id].scope_flags = scope_flags;
Damien Georged0691cc2014-01-29 20:30:52 +0000252 unique_codes[unique_code_id].n_args = n_args;
253 unique_codes[unique_code_id].n_state = n_locals + n_stack;
Damien826005c2013-10-05 23:17:28 +0100254 unique_codes[unique_code_id].u_byte.code = code;
255 unique_codes[unique_code_id].u_byte.len = len;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200256 unique_codes[unique_code_id].arg_names = arg_names;
Damien826005c2013-10-05 23:17:28 +0100257
Damien9ecbcff2013-12-11 00:41:43 +0000258 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000259
260#ifdef DEBUG_PRINT
Damien George08d07552014-01-29 18:58:52 +0000261 DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d n_locals=%d n_stack=%d\n", unique_code_id, code, len, n_args, n_locals, n_stack);
Damien0446a0d2013-11-17 13:16:36 +0000262 for (int i = 0; i < 128 && i < len; i++) {
263 if (i > 0 && i % 16 == 0) {
264 DEBUG_printf("\n");
265 }
266 DEBUG_printf(" %02x", code[i]);
267 }
268 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000269#if MICROPY_DEBUG_PRINTERS
270 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000271#endif
Damien0446a0d2013-11-17 13:16:36 +0000272#endif
Damien826005c2013-10-05 23:17:28 +0100273}
274
Damien Georged0691cc2014-01-29 20:30:52 +0000275void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100276 alloc_unique_codes();
277
John R. Lenton9c83ec02014-01-07 23:06:46 +0000278 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 +0000279 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien George8725f8f2014-02-15 19:33:11 +0000280 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000281 unique_codes[unique_code_id].n_args = n_args;
282 unique_codes[unique_code_id].n_state = 0;
Damien429d7192013-10-04 19:53:11 +0100283 unique_codes[unique_code_id].u_native.fun = fun;
284
Damien George8cc96a32013-12-30 18:23:50 +0000285 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000286
Damiena1ddfcc2013-10-10 23:25:50 +0100287#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100288 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
289 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
290 for (int i = 0; i < 128 && i < len; i++) {
291 if (i > 0 && i % 16 == 0) {
292 DEBUG_printf("\n");
293 }
294 DEBUG_printf(" %02x", fun_data[i]);
295 }
296 DEBUG_printf("\n");
297
Damien0446a0d2013-11-17 13:16:36 +0000298#ifdef WRITE_CODE
299 if (fp_write_code != NULL) {
300 fwrite(fun_data, len, 1, fp_write_code);
301 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100302 }
Damiena1ddfcc2013-10-10 23:25:50 +0100303#endif
304#endif
Damien429d7192013-10-04 19:53:11 +0100305}
306
Damien Georged0691cc2014-01-29 20:30:52 +0000307void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100308 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100309
John R. Lenton9c83ec02014-01-07 23:06:46 +0000310 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 +0000311 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien George8725f8f2014-02-15 19:33:11 +0000312 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000313 unique_codes[unique_code_id].n_args = n_args;
314 unique_codes[unique_code_id].n_state = 0;
Damien826005c2013-10-05 23:17:28 +0100315 unique_codes[unique_code_id].u_inline_asm.fun = fun;
316
Damiena1ddfcc2013-10-10 23:25:50 +0100317#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100318 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
319 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
320 for (int i = 0; i < 128 && i < len; i++) {
321 if (i > 0 && i % 16 == 0) {
322 DEBUG_printf("\n");
323 }
324 DEBUG_printf(" %02x", fun_data[i]);
325 }
326 DEBUG_printf("\n");
327
Damien0446a0d2013-11-17 13:16:36 +0000328#ifdef WRITE_CODE
329 if (fp_write_code != NULL) {
330 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100331 }
Damiena1ddfcc2013-10-10 23:25:50 +0100332#endif
333#endif
Damien429d7192013-10-04 19:53:11 +0100334}
335
Damiend99b0522013-12-21 18:17:45 +0000336int rt_is_true(mp_obj_t arg) {
337 DEBUG_OP_printf("is true %p\n", arg);
Damien George09a0c642014-01-30 10:05:33 +0000338 if (arg == mp_const_false) {
339 return 0;
340 } else if (arg == mp_const_true) {
341 return 1;
342 } else if (arg == mp_const_none) {
343 return 0;
344 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000345 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
346 return 0;
347 } else {
348 return 1;
349 }
Damiend99b0522013-12-21 18:17:45 +0000350 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200351 mp_obj_type_t *type = mp_obj_get_type(arg);
352 if (type->unary_op != NULL) {
353 mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
Damien George09a0c642014-01-30 10:05:33 +0000354 if (result != MP_OBJ_NULL) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200355 return result == mp_const_true;
356 }
357 }
358
Damien Georgee4b6a072014-01-28 23:27:35 +0000359 mp_obj_t len = mp_obj_len_maybe(arg);
360 if (len != MP_OBJ_NULL) {
361 // obj has a length, truth determined if len != 0
362 return len != MP_OBJ_NEW_SMALL_INT(0);
363 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200364 // any other obj is true per Python semantics
Damien Georgee4b6a072014-01-28 23:27:35 +0000365 return 1;
366 }
Damiend99b0522013-12-21 18:17:45 +0000367 }
368}
369
370mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
371 return mp_obj_list_append(self_in, arg);
372}
373
Damien7410e442013-11-02 19:47:57 +0000374#define PARSE_DEC_IN_INTG (1)
375#define PARSE_DEC_IN_FRAC (2)
376#define PARSE_DEC_IN_EXP (3)
377
Damiend99b0522013-12-21 18:17:45 +0000378mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000379#if MICROPY_ENABLE_FLOAT
380 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
381 const char *s = qstr_str(qstr);
382 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000383 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000384 bool exp_neg = false;
385 int exp_val = 0;
386 int exp_extra = 0;
387 bool imag = false;
388 for (; *s; s++) {
389 int dig = *s;
390 if ('0' <= dig && dig <= '9') {
391 dig -= '0';
392 if (in == PARSE_DEC_IN_EXP) {
393 exp_val = 10 * exp_val + dig;
394 } else {
395 dec_val = 10 * dec_val + dig;
396 if (in == PARSE_DEC_IN_FRAC) {
397 exp_extra -= 1;
398 }
399 }
400 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
401 in = PARSE_DEC_IN_FRAC;
402 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
403 in = PARSE_DEC_IN_EXP;
404 if (s[1] == '+') {
405 s++;
406 } else if (s[1] == '-') {
407 s++;
408 exp_neg = true;
409 }
410 } else if (dig == 'J' || dig == 'j') {
411 s++;
412 imag = true;
413 break;
414 } else {
415 // unknown character
416 break;
417 }
418 }
419 if (*s != 0) {
Damien Georgec5966122014-02-15 16:10:44 +0000420 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000421 }
422 if (exp_neg) {
423 exp_val = -exp_val;
424 }
425 exp_val += exp_extra;
426 for (; exp_val > 0; exp_val--) {
427 dec_val *= 10;
428 }
429 for (; exp_val < 0; exp_val++) {
430 dec_val *= 0.1;
431 }
432 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000433 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000434 } else {
Damiend99b0522013-12-21 18:17:45 +0000435 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000436 }
437#else
Damien Georgec5966122014-02-15 16:10:44 +0000438 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000439#endif
440}
441
Damiend99b0522013-12-21 18:17:45 +0000442mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100443 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000444 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100445}
446
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200447mp_obj_t rt_load_const_bytes(qstr qstr) {
448 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
449 uint len;
450 const byte *data = qstr_data(qstr, &len);
451 return mp_obj_new_bytes(data, len);
452}
453
Damiend99b0522013-12-21 18:17:45 +0000454mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100455 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100456 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000457 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000458 if (elem != NULL) {
459 return elem->value;
460 } else {
461 return rt_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100462 }
Damiena3977762013-10-09 23:10:10 +0100463}
464
Damiend99b0522013-12-21 18:17:45 +0000465mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100466 // logic: search globals, builtins
467 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000468 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100469 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000470 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100471 if (elem == NULL) {
Damien Georgeaea532e2014-02-06 22:57:51 +0000472 for (const mp_builtin_elem_t *e = &builtin_table[0]; e->qstr != MP_QSTR_; e++) {
473 if (e->qstr == qstr) {
474 return e->fun;
475 }
476 }
Damien Georgec5966122014-02-15 16:10:44 +0000477 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100478 }
479 }
480 return elem->value;
481}
482
Damiend99b0522013-12-21 18:17:45 +0000483mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100484 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000485 mp_map_elem_t *elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000486 if (elem != NULL) {
487 return elem->value;
488 } else {
489 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100490 }
Damien429d7192013-10-04 19:53:11 +0100491}
492
Damiend99b0522013-12-21 18:17:45 +0000493mp_obj_t rt_get_cell(mp_obj_t cell) {
494 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000495}
496
Damiend99b0522013-12-21 18:17:45 +0000497void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
498 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000499}
500
Damiend99b0522013-12-21 18:17:45 +0000501void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100502 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000503 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 +0100504}
505
Damiend99b0522013-12-21 18:17:45 +0000506void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100507 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000508 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 +0100509}
510
Damiend99b0522013-12-21 18:17:45 +0000511mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000512 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000513
Damiend99b0522013-12-21 18:17:45 +0000514 if (MP_OBJ_IS_SMALL_INT(arg)) {
515 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000516 switch (op) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200517 case RT_UNARY_OP_BOOL: return MP_BOOL(val != 0);
Damien7410e442013-11-02 19:47:57 +0000518 case RT_UNARY_OP_POSITIVE: break;
519 case RT_UNARY_OP_NEGATIVE: val = -val; break;
520 case RT_UNARY_OP_INVERT: val = ~val; break;
521 default: assert(0); val = 0;
522 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200523 if (MP_OBJ_FITS_SMALL_INT(val)) {
Damiend99b0522013-12-21 18:17:45 +0000524 return MP_OBJ_NEW_SMALL_INT(val);
Damien7410e442013-11-02 19:47:57 +0000525 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200526 return mp_obj_new_int(val);
Damien George1e708fe2014-01-23 18:27:51 +0000527 } else {
528 mp_obj_type_t *type = mp_obj_get_type(arg);
529 if (type->unary_op != NULL) {
530 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000531 if (result != NULL) {
532 return result;
533 }
Damien7410e442013-11-02 19:47:57 +0000534 }
Damiend99b0522013-12-21 18:17:45 +0000535 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000536 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bad operand type for unary operator: '%s'", type->name));
Damien7410e442013-11-02 19:47:57 +0000537 }
Damien429d7192013-10-04 19:53:11 +0100538}
539
Damiend99b0522013-12-21 18:17:45 +0000540mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100541 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000542
543 // TODO correctly distinguish inplace operators for mutable objects
544 // lookup logic that CPython uses for +=:
545 // check for implemented +=
546 // then check for implemented +
547 // then check for implemented seq.inplace_concat
548 // then check for implemented seq.concat
549 // then fail
550 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
551
Damien George9aa2a522014-02-01 23:04:09 +0000552 // deal with is
553 if (op == RT_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200554 return MP_BOOL(lhs == rhs);
555 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200556
Damien Georgebcbeea02014-01-11 10:47:22 +0000557 // deal with == and != for all types
Damien George9aa2a522014-02-01 23:04:09 +0000558 if (op == RT_BINARY_OP_EQUAL || op == RT_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000559 if (mp_obj_equal(lhs, rhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000560 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000561 return mp_const_true;
562 } else {
563 return mp_const_false;
564 }
565 } else {
Damien George9aa2a522014-02-01 23:04:09 +0000566 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000567 return mp_const_false;
568 } else {
569 return mp_const_true;
570 }
571 }
572 }
573
574 // deal with exception_match for all types
Damien George9aa2a522014-02-01 23:04:09 +0000575 if (op == RT_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000576 // rhs must be issubclass(rhs, BaseException)
577 if (mp_obj_is_exception_type(rhs)) {
578 // if lhs is an instance of an exception, then extract and use its type
579 if (mp_obj_is_exception_instance(lhs)) {
580 lhs = mp_obj_get_type(lhs);
581 }
582 if (mp_obj_is_subclass(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000583 return mp_const_true;
584 } else {
585 return mp_const_false;
586 }
587 }
588 }
589
Damien George1a9951d2014-01-06 22:13:00 +0000590 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000591 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000592 if (MP_OBJ_IS_SMALL_INT(rhs)) {
593 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
594 switch (op) {
595 case RT_BINARY_OP_OR:
596 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
597 case RT_BINARY_OP_XOR:
598 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
599 case RT_BINARY_OP_AND:
600 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
601 case RT_BINARY_OP_LSHIFT:
602 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
603 case RT_BINARY_OP_RSHIFT:
604 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
605 case RT_BINARY_OP_ADD:
606 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
607 case RT_BINARY_OP_SUBTRACT:
608 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
609 case RT_BINARY_OP_MULTIPLY:
610 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
611 case RT_BINARY_OP_FLOOR_DIVIDE:
612 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
613 #if MICROPY_ENABLE_FLOAT
614 case RT_BINARY_OP_TRUE_DIVIDE:
615 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
616 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000617
Damien George1a9951d2014-01-06 22:13:00 +0000618 // TODO implement modulo as specified by Python
619 case RT_BINARY_OP_MODULO:
620 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000621
Damien George1a9951d2014-01-06 22:13:00 +0000622 // TODO check for negative power, and overflow
623 case RT_BINARY_OP_POWER:
624 case RT_BINARY_OP_INPLACE_POWER:
625 {
626 int ans = 1;
627 while (rhs_val > 0) {
628 if (rhs_val & 1) {
629 ans *= lhs_val;
630 }
631 lhs_val *= lhs_val;
632 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000633 }
Damien George1a9951d2014-01-06 22:13:00 +0000634 lhs_val = ans;
635 break;
Damien4ebb32f2013-11-02 14:33:10 +0000636 }
Damien George9aa2a522014-02-01 23:04:09 +0000637 case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
638 case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
639 case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
640 case RT_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000641
Damien George1a9951d2014-01-06 22:13:00 +0000642 default: assert(0);
643 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200644 // TODO: We just should make mp_obj_new_int() inline and use that
645 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000646 return MP_OBJ_NEW_SMALL_INT(lhs_val);
647 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200648 return mp_obj_new_int(lhs_val);
Damien George3f759b72014-01-31 00:42:12 +0000649#if MICROPY_ENABLE_FLOAT
Damien George1a9951d2014-01-06 22:13:00 +0000650 } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
651 return mp_obj_float_binary_op(op, lhs_val, rhs);
652 } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
653 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000654#endif
Damien429d7192013-10-04 19:53:11 +0100655 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000656 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000657
Damien George9aa2a522014-02-01 23:04:09 +0000658 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000659 *
660 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000661 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000662 */
Damien George9aa2a522014-02-01 23:04:09 +0000663 if (op == RT_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000664 mp_obj_type_t *type = mp_obj_get_type(rhs);
665 if (type->binary_op != NULL) {
666 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000667 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000668 return res;
669 }
670 }
671 if (type->getiter != NULL) {
672 /* second attempt, walk the iterator */
673 mp_obj_t next = NULL;
674 mp_obj_t iter = rt_getiter(rhs);
675 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
676 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000677 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000678 }
Damien7410e442013-11-02 19:47:57 +0000679 }
Damien George9aa2a522014-02-01 23:04:09 +0000680 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000681 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000682
683 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000684 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000685 mp_obj_get_type_str(rhs)));
686 return mp_const_none;
687 }
688
Damien George5fa93b62014-01-22 14:35:10 +0000689 // generic binary_op supplied by type
690 mp_obj_type_t *type = mp_obj_get_type(lhs);
691 if (type->binary_op != NULL) {
692 mp_obj_t result = type->binary_op(op, lhs, rhs);
693 if (result != MP_OBJ_NULL) {
694 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000695 }
Damien429d7192013-10-04 19:53:11 +0100696 }
Damiend99b0522013-12-21 18:17:45 +0000697
Damien George5fa93b62014-01-22 14:35:10 +0000698 // TODO implement dispatch for reverse binary ops
699
Damiend99b0522013-12-21 18:17:45 +0000700 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000701 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200702 "unsupported operand types for binary operator: '%s', '%s'",
703 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000704 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100705}
706
Paul Sokolovsky90750022014-02-01 15:05:04 +0200707mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100708 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
709 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100710 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000711 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100712 }
Damiend99b0522013-12-21 18:17:45 +0000713
714 // make the function, depending on the code kind
715 mp_code_t *c = &unique_codes[unique_code_id];
716 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100717 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000718 case MP_CODE_BYTE:
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200719 fun = mp_obj_new_fun_bc(c->scope_flags, c->arg_names, c->n_args, def_args, c->n_state, c->u_byte.code);
Damien826005c2013-10-05 23:17:28 +0100720 break;
Damiend99b0522013-12-21 18:17:45 +0000721 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000722 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100723 break;
Damiend99b0522013-12-21 18:17:45 +0000724 case MP_CODE_INLINE_ASM:
725 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100726 break;
727 default:
728 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000729 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100730 }
Damienbd254452013-10-16 20:39:12 +0100731
732 // check for generator functions and if so wrap in generator object
Damien George8725f8f2014-02-15 19:33:11 +0000733 if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien Georged0691cc2014-01-29 20:30:52 +0000734 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100735 }
736
Damiend99b0522013-12-21 18:17:45 +0000737 return fun;
Damien429d7192013-10-04 19:53:11 +0100738}
739
Damiend99b0522013-12-21 18:17:45 +0000740mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000741 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000742 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200743 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000744 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000745 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000746}
747
Damiend99b0522013-12-21 18:17:45 +0000748mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000749 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100750}
751
Damiend99b0522013-12-21 18:17:45 +0000752mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000753 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100754}
755
Damiend99b0522013-12-21 18:17:45 +0000756mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
757 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000758 args[0] = arg1;
759 args[1] = arg2;
760 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100761}
762
Damien Georgecd82e022014-02-02 13:11:48 +0000763// wrapper that accepts n_args and n_kw in one argument
764// native emitter can only pass at most 3 arguments to a function
765mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
766 return rt_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
767}
768
Damien George20006db2014-01-18 14:10:48 +0000769// args contains, eg: arg0 arg1 key0 value0 key1 value1
770mp_obj_t rt_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damiend99b0522013-12-21 18:17:45 +0000771 // TODO improve this: fun object can specify its type and we parse here the arguments,
772 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100773
John R. Lenton9c83ec02014-01-07 23:06:46 +0000774 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
775
Damien George8b56beb2014-01-31 23:49:49 +0000776 // get the type
777 mp_obj_type_t *type = mp_obj_get_type(fun_in);
778
779 // do the call
780 if (type->call != NULL) {
781 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000782 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000783 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not callable", type->name));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000784 }
Damien86c7fc72013-11-26 15:16:41 +0000785}
786
Damien George20006db2014-01-18 14:10:48 +0000787// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
788// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000789mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +0000790 DEBUG_OP_printf("call method (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p)\n", args[0], args[1], n_args, n_kw, args);
791 int adjust = (args[1] == NULL) ? 0 : 1;
792 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000793}
794
Damiend99b0522013-12-21 18:17:45 +0000795mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000796 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100797}
798
Damiend99b0522013-12-21 18:17:45 +0000799mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000800 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100801}
802
Damiend99b0522013-12-21 18:17:45 +0000803mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
804 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100805}
806
Damiend99b0522013-12-21 18:17:45 +0000807mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000808 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100809 return set;
810}
811
Damien George932bf1c2014-01-18 23:42:49 +0000812// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000813void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200814 uint seq_len;
Damiend99b0522013-12-21 18:17:45 +0000815 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
Damiend99b0522013-12-21 18:17:45 +0000816 mp_obj_t *seq_items;
817 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
818 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
819 } else {
820 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000821 }
Damiend99b0522013-12-21 18:17:45 +0000822 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200823 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000824 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200825 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000826 }
Damien George932bf1c2014-01-18 23:42:49 +0000827 for (uint i = 0; i < num; i++) {
828 items[i] = seq_items[num - 1 - i];
829 }
Damien86c7fc72013-11-26 15:16:41 +0000830 } else {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200831 mp_obj_t iterable = rt_getiter(seq_in);
832
833 for (seq_len = 0; seq_len < num; seq_len++) {
834 mp_obj_t el = rt_iternext(iterable);
835 if (el == mp_const_stop_iteration) {
836 goto too_short;
837 }
838 items[num - 1 - seq_len] = el;
839 }
840 if (rt_iternext(iterable) != mp_const_stop_iteration) {
841 goto too_long;
842 }
Damien86c7fc72013-11-26 15:16:41 +0000843 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200844 return;
845
846too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000847 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200848too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000849 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num));
Damien86c7fc72013-11-26 15:16:41 +0000850}
851
Damiend99b0522013-12-21 18:17:45 +0000852mp_obj_t rt_build_map(int n_args) {
853 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100854}
855
Damiend99b0522013-12-21 18:17:45 +0000856mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
857 // map should always be a dict
858 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100859}
860
Damiend99b0522013-12-21 18:17:45 +0000861mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000862 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
863 // use load_method
864 mp_obj_t dest[2];
865 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000866 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000867 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000868 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000869 } else {
870 // load_method returned a method, so build a bound method object
871 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000872 }
Damiend99b0522013-12-21 18:17:45 +0000873}
874
Damien George7c9c6672014-01-25 00:17:36 +0000875// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
876// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
877// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200878STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000879 // clear output to indicate no attribute/method found yet
880 dest[0] = MP_OBJ_NULL;
881 dest[1] = MP_OBJ_NULL;
882
883 // get the type
884 mp_obj_type_t *type = mp_obj_get_type(base);
885
886 // if this type can do its own load, then call it
887 if (type->load_attr != NULL) {
888 type->load_attr(base, attr, dest);
889 }
890
891 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000892 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000893 if (attr == MP_QSTR___class__) {
894 // a.__class__ is equivalent to type(a)
895 dest[0] = type;
896 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000897 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
898 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000899 } else if (type->load_attr == NULL) {
900 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000901 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000902 const mp_method_t *meth = type->methods;
903 if (meth != NULL) {
904 for (; meth->name != NULL; meth++) {
905 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000906 // check if the methods are functions, static or class methods
907 // see http://docs.python.org/3.3/howto/descriptor.html
908 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
909 // return just the function
Damien George64131f32014-02-06 20:31:44 +0000910 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000911 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
912 // return a bound method, with self being the type of this object
Damien George64131f32014-02-06 20:31:44 +0000913 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien George20006db2014-01-18 14:10:48 +0000914 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000915 } else {
916 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000917 dest[0] = (mp_obj_t)meth->fun;
918 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000919 }
Damien George062478e2014-01-09 20:57:50 +0000920 break;
921 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000922 }
Damiend57eba52013-11-02 23:58:14 +0000923 }
924 }
Damiena3977762013-10-09 23:10:10 +0100925 }
Damien George7c9c6672014-01-25 00:17:36 +0000926}
Damiena3977762013-10-09 23:10:10 +0100927
Damien George7c9c6672014-01-25 00:17:36 +0000928void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
929 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
930
931 rt_load_method_maybe(base, attr, dest);
932
933 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000934 // no attribute/method called attr
935 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000936 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
937 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "type object '%s' has no attribute '%s'", ((mp_obj_type_t*)base)->name, qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000938 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000939 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000940 }
941 }
Damiena3977762013-10-09 23:10:10 +0100942}
943
Damiend99b0522013-12-21 18:17:45 +0000944void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100945 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000946 mp_obj_type_t *type = mp_obj_get_type(base);
947 if (type->store_attr != NULL) {
948 if (type->store_attr(base, attr, value)) {
949 return;
950 }
Damiena3977762013-10-09 23:10:10 +0100951 }
Damien Georgec5966122014-02-15 16:10:44 +0000952 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100953}
954
Damiend99b0522013-12-21 18:17:45 +0000955void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100956 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000957 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100958 // list store
Damiend99b0522013-12-21 18:17:45 +0000959 mp_obj_list_store(base, index, value);
960 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
961 // dict store
962 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100963 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200964 mp_obj_type_t *type = mp_obj_get_type(base);
965 if (type->store_item != NULL) {
966 bool r = type->store_item(base, index, value);
967 if (r) {
968 return;
969 }
970 // TODO: call base classes here?
971 }
Damien Georgec5966122014-02-15 16:10:44 +0000972 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
Damien429d7192013-10-04 19:53:11 +0100973 }
974}
975
Damiend99b0522013-12-21 18:17:45 +0000976mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000977 mp_obj_type_t *type = mp_obj_get_type(o_in);
978 if (type->getiter != NULL) {
979 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100980 } else {
Damien George7c9c6672014-01-25 00:17:36 +0000981 // check for __getitem__ method
982 mp_obj_t dest[2];
Damien George7d0bfbe2014-02-08 19:01:47 +0000983 rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000984 if (dest[0] != MP_OBJ_NULL) {
985 // __getitem__ exists, create an iterator
986 return mp_obj_new_getitem_iter(dest);
987 } else {
988 // object not iterable
Damien Georgec5966122014-02-15 16:10:44 +0000989 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", type->name));
Damien George7c9c6672014-01-25 00:17:36 +0000990 }
Damience89a212013-10-15 22:25:17 +0100991 }
992}
993
Damiend99b0522013-12-21 18:17:45 +0000994mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000995 mp_obj_type_t *type = mp_obj_get_type(o_in);
996 if (type->iternext != NULL) {
997 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100998 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000999 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", type->name));
1000 }
1001}
1002
1003mp_obj_t rt_make_raise_obj(mp_obj_t o) {
1004 DEBUG_printf("raise %p\n", o);
1005 if (mp_obj_is_exception_type(o)) {
1006 // o is an exception type (it is derived from BaseException (or is BaseException))
1007 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001008 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1009 // could have const instances in ROM which we return here instead
Damien Georgec5966122014-02-15 16:10:44 +00001010 return rt_call_function_n_kw(o, 0, 0, NULL);
1011 } else if (mp_obj_is_exception_instance(o)) {
1012 // o is an instance of an exception, so use it as the exception
1013 return o;
1014 } else {
1015 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1016 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001017 }
1018}
1019
Damiend99b0522013-12-21 18:17:45 +00001020mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001021 DEBUG_printf("import name %s\n", qstr_str(name));
1022
Damiendb4c3612013-12-10 17:27:24 +00001023 // build args array
Damiend99b0522013-12-21 18:17:45 +00001024 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001025 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001026 args[1] = mp_const_none; // TODO should be globals
1027 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001028 args[3] = fromlist;
1029 args[4] = level; // must be 0; we don't yet support other values
1030
1031 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001032 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001033}
1034
Damiend99b0522013-12-21 18:17:45 +00001035mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001036 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1037
Damiend99b0522013-12-21 18:17:45 +00001038 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +00001039 /* TODO convert AttributeError to ImportError
1040 if (fail) {
1041 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1042 }
1043 */
1044 return x;
1045}
1046
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001047void rt_import_all(mp_obj_t module) {
1048 DEBUG_printf("import all %p\n", module);
1049
1050 mp_map_t *map = mp_obj_module_get_globals(module);
1051 for (uint i = 0; i < map->alloc; i++) {
1052 if (map->table[i].key != MP_OBJ_NULL) {
1053 rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
1054 }
1055 }
1056}
1057
Damien George66028ab2014-01-03 14:03:48 +00001058mp_map_t *rt_locals_get(void) {
1059 return map_locals;
1060}
1061
1062void rt_locals_set(mp_map_t *m) {
1063 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
1064 map_locals = m;
1065}
1066
1067mp_map_t *rt_globals_get(void) {
1068 return map_globals;
1069}
1070
1071void rt_globals_set(mp_map_t *m) {
1072 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
1073 map_globals = m;
1074}
1075
Damien George0d028742014-01-22 23:59:20 +00001076mp_map_t *rt_loaded_modules_get(void) {
1077 return &map_loaded_modules;
1078}
1079
Damien6ba13142013-11-02 20:34:54 +00001080// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +01001081void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +00001082 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +01001083 rt_load_const_str,
1084 rt_load_name,
1085 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +01001086 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001087 rt_load_attr,
1088 rt_load_method,
1089 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001090 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001091 rt_store_subscr,
1092 rt_is_true,
1093 rt_unary_op,
Damien Georgecd82e022014-02-02 13:11:48 +00001094 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001095 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001096 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001097 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001098 rt_build_map,
1099 rt_store_map,
1100 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001101 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001102 rt_make_function_from_id,
Damien Georgecd82e022014-02-02 13:11:48 +00001103 rt_call_function_n_kw_for_native,
Damien George20006db2014-01-18 14:10:48 +00001104 rt_call_method_n_kw,
Damiend2755ec2013-10-16 23:58:48 +01001105 rt_getiter,
1106 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001107};
1108
1109/*
1110void rt_f_vector(rt_fun_kind_t fun_kind) {
1111 (rt_f_table[fun_kind])();
1112}
1113*/