blob: 7034ce13c06c482c052d171f2d6c046054dd2e5f [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"
Damien George20773972014-02-22 18:12:43 +000016#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000017#include "runtime0.h"
18#include "runtime.h"
19#include "map.h"
Damien660365e2013-12-17 18:27:24 +000020#include "builtin.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020021#include "objarray.h"
Damien George5fa93b62014-01-22 14:35:10 +000022#include "bc.h"
Damien660365e2013-12-17 18:27:24 +000023
Damien7f5dacf2013-10-10 11:24:39 +010024#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010025#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000026#define WRITE_CODE (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020027#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000028#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010029#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000030#define DEBUG_printf(...) (void)0
31#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010032#endif
Damien429d7192013-10-04 19:53:11 +010033
Damieneb19efb2013-10-10 22:06:54 +010034// locals and globals need to be pointers because they can be the same in outer module scope
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020035STATIC mp_map_t *map_locals;
36STATIC mp_map_t *map_globals;
37STATIC mp_map_t map_builtins;
38STATIC mp_map_t map_loaded_modules; // TODO: expose as sys.modules
Damienbd254452013-10-16 20:39:12 +010039
Damien429d7192013-10-04 19:53:11 +010040typedef enum {
Damiend99b0522013-12-21 18:17:45 +000041 MP_CODE_NONE,
42 MP_CODE_BYTE,
43 MP_CODE_NATIVE,
44 MP_CODE_INLINE_ASM,
45} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010046
Damiend99b0522013-12-21 18:17:45 +000047typedef struct _mp_code_t {
Damien George51047752014-02-26 17:40:52 +000048 mp_code_kind_t kind : 8;
49 uint scope_flags : 8;
50 uint n_args : 16;
51 uint n_state : 16;
Damien429d7192013-10-04 19:53:11 +010052 union {
53 struct {
Damien429d7192013-10-04 19:53:11 +010054 byte *code;
55 uint len;
56 } u_byte;
Damien826005c2013-10-05 23:17:28 +010057 struct {
Damiend99b0522013-12-21 18:17:45 +000058 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010059 } u_native;
60 struct {
Damiene4af64f2013-10-06 12:04:13 +010061 void *fun;
Damien826005c2013-10-05 23:17:28 +010062 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010063 };
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020064 qstr *arg_names;
Damiend99b0522013-12-21 18:17:45 +000065} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010066
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020067STATIC uint next_unique_code_id;
68STATIC machine_uint_t unique_codes_alloc = 0;
69STATIC mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010070
Damien0446a0d2013-11-17 13:16:36 +000071#ifdef WRITE_CODE
72FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010073#endif
Damien429d7192013-10-04 19:53:11 +010074
Damien Georgeaea532e2014-02-06 22:57:51 +000075// builtins
76// we put this table in ROM because it's always needed and takes up quite a bit of room in RAM
77// 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
78// 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
79// if we wanted to allow dynamic modification of the builtins, we could provide an mp_map_t object which is searched before this one
80
81typedef struct _mp_builtin_elem_t {
82 qstr qstr;
83 mp_obj_t fun;
84} mp_builtin_elem_t;
85
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020086STATIC const mp_builtin_elem_t builtin_table[] = {
Damien Georgeaea532e2014-02-06 22:57:51 +000087 // built-in core functions
88 { MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj },
89 { MP_QSTR___import__, (mp_obj_t)&mp_builtin___import___obj },
90 { MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
91
92 // built-in types
93 { MP_QSTR_bool, (mp_obj_t)&bool_type },
94#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000095 { MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
Damien Georgeaea532e2014-02-06 22:57:51 +000096#endif
97 { MP_QSTR_dict, (mp_obj_t)&dict_type },
98 { MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
99 { MP_QSTR_filter, (mp_obj_t)&filter_type },
100#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000101 { MP_QSTR_float, (mp_obj_t)&mp_type_float },
Damien Georgeaea532e2014-02-06 22:57:51 +0000102#endif
103 { MP_QSTR_int, (mp_obj_t)&int_type },
104 { MP_QSTR_list, (mp_obj_t)&list_type },
105 { MP_QSTR_map, (mp_obj_t)&map_type },
106 { MP_QSTR_set, (mp_obj_t)&set_type },
107 { MP_QSTR_super, (mp_obj_t)&super_type },
108 { MP_QSTR_tuple, (mp_obj_t)&tuple_type },
Damien Georgec5966122014-02-15 16:10:44 +0000109 { MP_QSTR_type, (mp_obj_t)&mp_type_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000110 { MP_QSTR_zip, (mp_obj_t)&zip_type },
111
112 { MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
113 { MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
114
115 // built-in user functions
116 { MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
117 { MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
118 { MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
119 { MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj },
120 { MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
121 { MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
122 { MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
123 { MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
124 { MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
125 { MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
126 { MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
127 { MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
128 { MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
129 { MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
130 { MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
131 { MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
132 { MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
133 { MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
134 { MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
135 { MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
136 { MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
137 { MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
138 { MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
139 { MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
140 { MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
141 { MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
142 { MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj },
143 { MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
144
Damien Georgec5966122014-02-15 16:10:44 +0000145 // built-in exceptions
146 { MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
147 { MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
148 { MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
149 { MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
150 { MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
151 { MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
152 { MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
153 { MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
154 { MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
155 { MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
156 { MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
157 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
158 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
159 { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
160 { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
161 { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
162 { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
163
Paul Sokolovsky910843e2014-02-14 12:02:34 +0200164 // Extra builtins as defined by a port
165 MICROPY_EXTRA_BUILTINS
166
Damien Georgeaea532e2014-02-06 22:57:51 +0000167 { MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
168};
169
Damien George38a2da62014-01-08 17:33:12 +0000170// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200171STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +0000172 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
173}
174
Damien8b3a7c22013-10-23 20:20:17 +0100175void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +0100176 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +0000177 map_locals = map_globals = mp_map_new(1);
Damien George5fa93b62014-01-22 14:35:10 +0000178 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +0100179
Damienb86e3f92013-12-29 17:17:43 +0000180 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +0000181 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +0000182
Damien George0d028742014-01-22 23:59:20 +0000183 // init loaded modules table
184 mp_map_init(&map_loaded_modules, 3);
185
Damien Georgee9906ac2014-01-04 18:44:46 +0000186 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000187 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000188
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200189 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
190 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200191
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200192 mp_obj_t m_collections = mp_obj_new_module(MP_QSTR_collections);
193 rt_store_attr(m_collections, MP_QSTR_namedtuple, (mp_obj_t)&mp_namedtuple_obj);
194
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 George0c36da02014-03-08 15:24:39 +0000206 // we pre-import the micropython module
207 // probably shouldn't do this, so we are compatible with CPython
208 rt_store_name(MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +0200209
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200210 // TODO: wastes one mp_code_t structure in mem
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000211 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000212 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100213 unique_codes = NULL;
214
Damien0446a0d2013-11-17 13:16:36 +0000215#ifdef WRITE_CODE
216 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100217#endif
Damien429d7192013-10-04 19:53:11 +0100218}
219
Damien8b3a7c22013-10-23 20:20:17 +0100220void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000221 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200222 mp_map_free(map_globals);
223 mp_map_deinit(&map_loaded_modules);
224 mp_map_deinit(&map_builtins);
Damien0446a0d2013-11-17 13:16:36 +0000225#ifdef WRITE_CODE
226 if (fp_write_code != NULL) {
227 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100228 }
Damiena1ddfcc2013-10-10 23:25:50 +0100229#endif
Damien429d7192013-10-04 19:53:11 +0100230}
231
Damien Georged0691cc2014-01-29 20:30:52 +0000232uint rt_get_unique_code_id(void) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000233 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100234}
235
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200236STATIC void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000237 if (next_unique_code_id > unique_codes_alloc) {
Damien Georged0691cc2014-01-29 20:30:52 +0000238 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 +0000239 // increase size of unique_codes table
240 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
Damien Georged0691cc2014-01-29 20:30:52 +0000241 for (uint i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000242 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100243 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000244 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100245 }
Damien826005c2013-10-05 23:17:28 +0100246}
247
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200248void 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 +0100249 alloc_unique_codes();
250
John R. Lenton9c83ec02014-01-07 23:06:46 +0000251 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 +0000252 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien George8725f8f2014-02-15 19:33:11 +0000253 unique_codes[unique_code_id].scope_flags = scope_flags;
Damien Georged0691cc2014-01-29 20:30:52 +0000254 unique_codes[unique_code_id].n_args = n_args;
255 unique_codes[unique_code_id].n_state = n_locals + n_stack;
Damien826005c2013-10-05 23:17:28 +0100256 unique_codes[unique_code_id].u_byte.code = code;
257 unique_codes[unique_code_id].u_byte.len = len;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200258 unique_codes[unique_code_id].arg_names = arg_names;
Damien826005c2013-10-05 23:17:28 +0100259
Damien9ecbcff2013-12-11 00:41:43 +0000260 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000261
262#ifdef DEBUG_PRINT
Damien George08d07552014-01-29 18:58:52 +0000263 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 +0000264 for (int i = 0; i < 128 && i < len; i++) {
265 if (i > 0 && i % 16 == 0) {
266 DEBUG_printf("\n");
267 }
268 DEBUG_printf(" %02x", code[i]);
269 }
270 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000271#if MICROPY_DEBUG_PRINTERS
272 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000273#endif
Damien0446a0d2013-11-17 13:16:36 +0000274#endif
Damien826005c2013-10-05 23:17:28 +0100275}
276
Damien Georged0691cc2014-01-29 20:30:52 +0000277void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100278 alloc_unique_codes();
279
John R. Lenton9c83ec02014-01-07 23:06:46 +0000280 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 +0000281 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien George8725f8f2014-02-15 19:33:11 +0000282 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000283 unique_codes[unique_code_id].n_args = n_args;
284 unique_codes[unique_code_id].n_state = 0;
Damien429d7192013-10-04 19:53:11 +0100285 unique_codes[unique_code_id].u_native.fun = fun;
286
Damien George8cc96a32013-12-30 18:23:50 +0000287 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000288
Damiena1ddfcc2013-10-10 23:25:50 +0100289#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100290 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
291 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
292 for (int i = 0; i < 128 && i < len; i++) {
293 if (i > 0 && i % 16 == 0) {
294 DEBUG_printf("\n");
295 }
296 DEBUG_printf(" %02x", fun_data[i]);
297 }
298 DEBUG_printf("\n");
299
Damien0446a0d2013-11-17 13:16:36 +0000300#ifdef WRITE_CODE
301 if (fp_write_code != NULL) {
302 fwrite(fun_data, len, 1, fp_write_code);
303 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100304 }
Damiena1ddfcc2013-10-10 23:25:50 +0100305#endif
306#endif
Damien429d7192013-10-04 19:53:11 +0100307}
308
Damien Georged0691cc2014-01-29 20:30:52 +0000309void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100310 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100311
John R. Lenton9c83ec02014-01-07 23:06:46 +0000312 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 +0000313 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien George8725f8f2014-02-15 19:33:11 +0000314 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000315 unique_codes[unique_code_id].n_args = n_args;
316 unique_codes[unique_code_id].n_state = 0;
Damien826005c2013-10-05 23:17:28 +0100317 unique_codes[unique_code_id].u_inline_asm.fun = fun;
318
Damiena1ddfcc2013-10-10 23:25:50 +0100319#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100320 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
321 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
322 for (int i = 0; i < 128 && i < len; i++) {
323 if (i > 0 && i % 16 == 0) {
324 DEBUG_printf("\n");
325 }
326 DEBUG_printf(" %02x", fun_data[i]);
327 }
328 DEBUG_printf("\n");
329
Damien0446a0d2013-11-17 13:16:36 +0000330#ifdef WRITE_CODE
331 if (fp_write_code != NULL) {
332 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100333 }
Damiena1ddfcc2013-10-10 23:25:50 +0100334#endif
335#endif
Damien429d7192013-10-04 19:53:11 +0100336}
337
Damiend99b0522013-12-21 18:17:45 +0000338int rt_is_true(mp_obj_t arg) {
339 DEBUG_OP_printf("is true %p\n", arg);
Damien George09a0c642014-01-30 10:05:33 +0000340 if (arg == mp_const_false) {
341 return 0;
342 } else if (arg == mp_const_true) {
343 return 1;
344 } else if (arg == mp_const_none) {
345 return 0;
346 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000347 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
348 return 0;
349 } else {
350 return 1;
351 }
Damiend99b0522013-12-21 18:17:45 +0000352 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200353 mp_obj_type_t *type = mp_obj_get_type(arg);
354 if (type->unary_op != NULL) {
355 mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
Damien George09a0c642014-01-30 10:05:33 +0000356 if (result != MP_OBJ_NULL) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200357 return result == mp_const_true;
358 }
359 }
360
Damien Georgee4b6a072014-01-28 23:27:35 +0000361 mp_obj_t len = mp_obj_len_maybe(arg);
362 if (len != MP_OBJ_NULL) {
363 // obj has a length, truth determined if len != 0
364 return len != MP_OBJ_NEW_SMALL_INT(0);
365 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200366 // any other obj is true per Python semantics
Damien Georgee4b6a072014-01-28 23:27:35 +0000367 return 1;
368 }
Damiend99b0522013-12-21 18:17:45 +0000369 }
370}
371
372mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
373 return mp_obj_list_append(self_in, arg);
374}
375
Damiend99b0522013-12-21 18:17:45 +0000376mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000377 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000378 uint len;
379 const byte* data = qstr_data(qstr, &len);
380 return mp_parse_num_decimal((const char*)data, len);
Damien7410e442013-11-02 19:47:57 +0000381}
382
Damiend99b0522013-12-21 18:17:45 +0000383mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100384 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000385 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100386}
387
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200388mp_obj_t rt_load_const_bytes(qstr qstr) {
389 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
390 uint len;
391 const byte *data = qstr_data(qstr, &len);
392 return mp_obj_new_bytes(data, len);
393}
394
Damiend99b0522013-12-21 18:17:45 +0000395mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100396 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100397 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000398 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 +0000399 if (elem != NULL) {
400 return elem->value;
401 } else {
402 return rt_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100403 }
Damiena3977762013-10-09 23:10:10 +0100404}
405
Damiend99b0522013-12-21 18:17:45 +0000406mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100407 // logic: search globals, builtins
408 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000409 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100410 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000411 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100412 if (elem == NULL) {
Damien Georgeaea532e2014-02-06 22:57:51 +0000413 for (const mp_builtin_elem_t *e = &builtin_table[0]; e->qstr != MP_QSTR_; e++) {
414 if (e->qstr == qstr) {
415 return e->fun;
416 }
417 }
Damien Georgec5966122014-02-15 16:10:44 +0000418 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 +0100419 }
420 }
421 return elem->value;
422}
423
Damiend99b0522013-12-21 18:17:45 +0000424mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100425 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000426 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 +0000427 if (elem != NULL) {
428 return elem->value;
429 } else {
430 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100431 }
Damien429d7192013-10-04 19:53:11 +0100432}
433
Damiend99b0522013-12-21 18:17:45 +0000434mp_obj_t rt_get_cell(mp_obj_t cell) {
435 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000436}
437
Damiend99b0522013-12-21 18:17:45 +0000438void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
439 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000440}
441
Damiend99b0522013-12-21 18:17:45 +0000442void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100443 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000444 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 +0100445}
446
Damiend99b0522013-12-21 18:17:45 +0000447void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100448 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000449 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 +0100450}
451
Damiend99b0522013-12-21 18:17:45 +0000452mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000453 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000454
Damiend99b0522013-12-21 18:17:45 +0000455 if (MP_OBJ_IS_SMALL_INT(arg)) {
456 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000457 switch (op) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200458 case RT_UNARY_OP_BOOL: return MP_BOOL(val != 0);
Damien7410e442013-11-02 19:47:57 +0000459 case RT_UNARY_OP_POSITIVE: break;
460 case RT_UNARY_OP_NEGATIVE: val = -val; break;
461 case RT_UNARY_OP_INVERT: val = ~val; break;
462 default: assert(0); val = 0;
463 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200464 if (MP_OBJ_FITS_SMALL_INT(val)) {
Damiend99b0522013-12-21 18:17:45 +0000465 return MP_OBJ_NEW_SMALL_INT(val);
Damien7410e442013-11-02 19:47:57 +0000466 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200467 return mp_obj_new_int(val);
Damien George1e708fe2014-01-23 18:27:51 +0000468 } else {
469 mp_obj_type_t *type = mp_obj_get_type(arg);
470 if (type->unary_op != NULL) {
471 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000472 if (result != NULL) {
473 return result;
474 }
Damien7410e442013-11-02 19:47:57 +0000475 }
Damiend99b0522013-12-21 18:17:45 +0000476 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000477 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 +0000478 }
Damien429d7192013-10-04 19:53:11 +0100479}
480
Damiend99b0522013-12-21 18:17:45 +0000481mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100482 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000483
484 // TODO correctly distinguish inplace operators for mutable objects
485 // lookup logic that CPython uses for +=:
486 // check for implemented +=
487 // then check for implemented +
488 // then check for implemented seq.inplace_concat
489 // then check for implemented seq.concat
490 // then fail
491 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
492
Damien George9aa2a522014-02-01 23:04:09 +0000493 // deal with is
494 if (op == RT_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200495 return MP_BOOL(lhs == rhs);
496 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200497
Damien Georgebcbeea02014-01-11 10:47:22 +0000498 // deal with == and != for all types
Damien George9aa2a522014-02-01 23:04:09 +0000499 if (op == RT_BINARY_OP_EQUAL || op == RT_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000500 if (mp_obj_equal(lhs, rhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000501 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000502 return mp_const_true;
503 } else {
504 return mp_const_false;
505 }
506 } else {
Damien George9aa2a522014-02-01 23:04:09 +0000507 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000508 return mp_const_false;
509 } else {
510 return mp_const_true;
511 }
512 }
513 }
514
515 // deal with exception_match for all types
Damien George9aa2a522014-02-01 23:04:09 +0000516 if (op == RT_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000517 // rhs must be issubclass(rhs, BaseException)
518 if (mp_obj_is_exception_type(rhs)) {
519 // if lhs is an instance of an exception, then extract and use its type
520 if (mp_obj_is_exception_instance(lhs)) {
521 lhs = mp_obj_get_type(lhs);
522 }
Damien George71510152014-03-03 22:38:13 +0000523 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000524 return mp_const_true;
525 } else {
526 return mp_const_false;
527 }
528 }
529 }
530
Damien George1a9951d2014-01-06 22:13:00 +0000531 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000532 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000533 if (MP_OBJ_IS_SMALL_INT(rhs)) {
534 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
535 switch (op) {
536 case RT_BINARY_OP_OR:
537 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
538 case RT_BINARY_OP_XOR:
539 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
540 case RT_BINARY_OP_AND:
541 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
542 case RT_BINARY_OP_LSHIFT:
543 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
544 case RT_BINARY_OP_RSHIFT:
545 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
546 case RT_BINARY_OP_ADD:
547 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
548 case RT_BINARY_OP_SUBTRACT:
549 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
550 case RT_BINARY_OP_MULTIPLY:
551 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
552 case RT_BINARY_OP_FLOOR_DIVIDE:
553 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
554 #if MICROPY_ENABLE_FLOAT
555 case RT_BINARY_OP_TRUE_DIVIDE:
556 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
557 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000558
Damien George1a9951d2014-01-06 22:13:00 +0000559 // TODO implement modulo as specified by Python
560 case RT_BINARY_OP_MODULO:
561 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000562
Damien George1a9951d2014-01-06 22:13:00 +0000563 // TODO check for negative power, and overflow
564 case RT_BINARY_OP_POWER:
565 case RT_BINARY_OP_INPLACE_POWER:
566 {
567 int ans = 1;
568 while (rhs_val > 0) {
569 if (rhs_val & 1) {
570 ans *= lhs_val;
571 }
572 lhs_val *= lhs_val;
573 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000574 }
Damien George1a9951d2014-01-06 22:13:00 +0000575 lhs_val = ans;
576 break;
Damien4ebb32f2013-11-02 14:33:10 +0000577 }
Damien George9aa2a522014-02-01 23:04:09 +0000578 case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
579 case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
580 case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
581 case RT_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000582
Damien George1a9951d2014-01-06 22:13:00 +0000583 default: assert(0);
584 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200585 // TODO: We just should make mp_obj_new_int() inline and use that
586 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000587 return MP_OBJ_NEW_SMALL_INT(lhs_val);
588 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200589 return mp_obj_new_int(lhs_val);
Damien George3f759b72014-01-31 00:42:12 +0000590#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000591 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000592 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000593 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000594 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000595#endif
Damien429d7192013-10-04 19:53:11 +0100596 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000597 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000598
Damien George9aa2a522014-02-01 23:04:09 +0000599 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000600 *
601 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000602 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000603 */
Damien George9aa2a522014-02-01 23:04:09 +0000604 if (op == RT_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000605 mp_obj_type_t *type = mp_obj_get_type(rhs);
606 if (type->binary_op != NULL) {
607 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000608 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000609 return res;
610 }
611 }
612 if (type->getiter != NULL) {
613 /* second attempt, walk the iterator */
614 mp_obj_t next = NULL;
615 mp_obj_t iter = rt_getiter(rhs);
616 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
617 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000618 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000619 }
Damien7410e442013-11-02 19:47:57 +0000620 }
Damien George9aa2a522014-02-01 23:04:09 +0000621 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000622 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000623
624 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000625 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000626 mp_obj_get_type_str(rhs)));
627 return mp_const_none;
628 }
629
Damien George5fa93b62014-01-22 14:35:10 +0000630 // generic binary_op supplied by type
631 mp_obj_type_t *type = mp_obj_get_type(lhs);
632 if (type->binary_op != NULL) {
633 mp_obj_t result = type->binary_op(op, lhs, rhs);
634 if (result != MP_OBJ_NULL) {
635 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000636 }
Damien429d7192013-10-04 19:53:11 +0100637 }
Damiend99b0522013-12-21 18:17:45 +0000638
Damien George5fa93b62014-01-22 14:35:10 +0000639 // TODO implement dispatch for reverse binary ops
640
Damiend99b0522013-12-21 18:17:45 +0000641 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000642 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200643 "unsupported operand types for binary operator: '%s', '%s'",
644 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000645 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100646}
647
Paul Sokolovsky90750022014-02-01 15:05:04 +0200648mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100649 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
650 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100651 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000652 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100653 }
Damiend99b0522013-12-21 18:17:45 +0000654
655 // make the function, depending on the code kind
656 mp_code_t *c = &unique_codes[unique_code_id];
657 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100658 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000659 case MP_CODE_BYTE:
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200660 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 +0100661 break;
Damiend99b0522013-12-21 18:17:45 +0000662 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000663 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100664 break;
Damiend99b0522013-12-21 18:17:45 +0000665 case MP_CODE_INLINE_ASM:
666 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100667 break;
668 default:
669 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000670 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100671 }
Damienbd254452013-10-16 20:39:12 +0100672
673 // check for generator functions and if so wrap in generator object
Damien George8725f8f2014-02-15 19:33:11 +0000674 if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien Georged0691cc2014-01-29 20:30:52 +0000675 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100676 }
677
Damiend99b0522013-12-21 18:17:45 +0000678 return fun;
Damien429d7192013-10-04 19:53:11 +0100679}
680
Damiend99b0522013-12-21 18:17:45 +0000681mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000682 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000683 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200684 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000685 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000686 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000687}
688
Damiend99b0522013-12-21 18:17:45 +0000689mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000690 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100691}
692
Damiend99b0522013-12-21 18:17:45 +0000693mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000694 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100695}
696
Damiend99b0522013-12-21 18:17:45 +0000697mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
698 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000699 args[0] = arg1;
700 args[1] = arg2;
701 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100702}
703
Damien Georgecd82e022014-02-02 13:11:48 +0000704// wrapper that accepts n_args and n_kw in one argument
705// native emitter can only pass at most 3 arguments to a function
706mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
707 return rt_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
708}
709
Damien George20006db2014-01-18 14:10:48 +0000710// args contains, eg: arg0 arg1 key0 value0 key1 value1
711mp_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 +0000712 // TODO improve this: fun object can specify its type and we parse here the arguments,
713 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100714
John R. Lenton9c83ec02014-01-07 23:06:46 +0000715 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
716
Damien George8b56beb2014-01-31 23:49:49 +0000717 // get the type
718 mp_obj_type_t *type = mp_obj_get_type(fun_in);
719
720 // do the call
721 if (type->call != NULL) {
722 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000723 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000724 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 +0000725 }
Damien86c7fc72013-11-26 15:16:41 +0000726}
727
Damien George20006db2014-01-18 14:10:48 +0000728// 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)
729// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000730mp_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 +0000731 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);
732 int adjust = (args[1] == NULL) ? 0 : 1;
733 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000734}
735
Damiend99b0522013-12-21 18:17:45 +0000736mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000737 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100738}
739
Damiend99b0522013-12-21 18:17:45 +0000740mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000741 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100742}
743
Damiend99b0522013-12-21 18:17:45 +0000744mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
745 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100746}
747
Damiend99b0522013-12-21 18:17:45 +0000748mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000749 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100750 return set;
751}
752
Damien George932bf1c2014-01-18 23:42:49 +0000753// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000754void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200755 uint seq_len;
Damiend99b0522013-12-21 18:17:45 +0000756 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
Damiend99b0522013-12-21 18:17:45 +0000757 mp_obj_t *seq_items;
758 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
759 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
760 } else {
761 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000762 }
Damiend99b0522013-12-21 18:17:45 +0000763 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200764 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000765 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200766 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000767 }
Damien George932bf1c2014-01-18 23:42:49 +0000768 for (uint i = 0; i < num; i++) {
769 items[i] = seq_items[num - 1 - i];
770 }
Damien86c7fc72013-11-26 15:16:41 +0000771 } else {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200772 mp_obj_t iterable = rt_getiter(seq_in);
773
774 for (seq_len = 0; seq_len < num; seq_len++) {
775 mp_obj_t el = rt_iternext(iterable);
776 if (el == mp_const_stop_iteration) {
777 goto too_short;
778 }
779 items[num - 1 - seq_len] = el;
780 }
781 if (rt_iternext(iterable) != mp_const_stop_iteration) {
782 goto too_long;
783 }
Damien86c7fc72013-11-26 15:16:41 +0000784 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200785 return;
786
787too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000788 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 +0200789too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000790 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 +0000791}
792
Damiend99b0522013-12-21 18:17:45 +0000793mp_obj_t rt_build_map(int n_args) {
794 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100795}
796
Damiend99b0522013-12-21 18:17:45 +0000797mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
798 // map should always be a dict
799 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100800}
801
Damiend99b0522013-12-21 18:17:45 +0000802mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000803 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
804 // use load_method
805 mp_obj_t dest[2];
806 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000807 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000808 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000809 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000810 } else {
811 // load_method returned a method, so build a bound method object
812 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000813 }
Damiend99b0522013-12-21 18:17:45 +0000814}
815
Damien George7c9c6672014-01-25 00:17:36 +0000816// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
817// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
818// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200819STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000820 // clear output to indicate no attribute/method found yet
821 dest[0] = MP_OBJ_NULL;
822 dest[1] = MP_OBJ_NULL;
823
824 // get the type
825 mp_obj_type_t *type = mp_obj_get_type(base);
826
827 // if this type can do its own load, then call it
828 if (type->load_attr != NULL) {
829 type->load_attr(base, attr, dest);
830 }
831
832 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000833 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000834 if (attr == MP_QSTR___class__) {
835 // a.__class__ is equivalent to type(a)
836 dest[0] = type;
837 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000838 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
839 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000840 } else if (type->load_attr == NULL) {
841 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000842 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000843 const mp_method_t *meth = type->methods;
844 if (meth != NULL) {
845 for (; meth->name != NULL; meth++) {
846 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000847 // check if the methods are functions, static or class methods
848 // see http://docs.python.org/3.3/howto/descriptor.html
849 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
850 // return just the function
Damien George64131f32014-02-06 20:31:44 +0000851 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000852 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
853 // return a bound method, with self being the type of this object
Damien George64131f32014-02-06 20:31:44 +0000854 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien George20006db2014-01-18 14:10:48 +0000855 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000856 } else {
857 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000858 dest[0] = (mp_obj_t)meth->fun;
859 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000860 }
Damien George062478e2014-01-09 20:57:50 +0000861 break;
862 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000863 }
Damiend57eba52013-11-02 23:58:14 +0000864 }
865 }
Damiena3977762013-10-09 23:10:10 +0100866 }
Damien George7c9c6672014-01-25 00:17:36 +0000867}
Damiena3977762013-10-09 23:10:10 +0100868
Damien George7c9c6672014-01-25 00:17:36 +0000869void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
870 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
871
872 rt_load_method_maybe(base, attr, dest);
873
874 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000875 // no attribute/method called attr
876 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000877 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
878 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 +0000879 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000880 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 +0000881 }
882 }
Damiena3977762013-10-09 23:10:10 +0100883}
884
Damiend99b0522013-12-21 18:17:45 +0000885void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100886 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000887 mp_obj_type_t *type = mp_obj_get_type(base);
888 if (type->store_attr != NULL) {
889 if (type->store_attr(base, attr, value)) {
890 return;
891 }
Damiena3977762013-10-09 23:10:10 +0100892 }
Damien Georgec5966122014-02-15 16:10:44 +0000893 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 +0100894}
895
Damiend99b0522013-12-21 18:17:45 +0000896void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100897 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000898 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100899 // list store
Damiend99b0522013-12-21 18:17:45 +0000900 mp_obj_list_store(base, index, value);
901 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
902 // dict store
903 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100904 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200905 mp_obj_type_t *type = mp_obj_get_type(base);
906 if (type->store_item != NULL) {
907 bool r = type->store_item(base, index, value);
908 if (r) {
909 return;
910 }
911 // TODO: call base classes here?
912 }
Damien Georgec5966122014-02-15 16:10:44 +0000913 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 +0100914 }
915}
916
Damiend99b0522013-12-21 18:17:45 +0000917mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000918 mp_obj_type_t *type = mp_obj_get_type(o_in);
919 if (type->getiter != NULL) {
920 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100921 } else {
Damien George7c9c6672014-01-25 00:17:36 +0000922 // check for __getitem__ method
923 mp_obj_t dest[2];
Damien George7d0bfbe2014-02-08 19:01:47 +0000924 rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000925 if (dest[0] != MP_OBJ_NULL) {
926 // __getitem__ exists, create an iterator
927 return mp_obj_new_getitem_iter(dest);
928 } else {
929 // object not iterable
Damien Georgec5966122014-02-15 16:10:44 +0000930 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 +0000931 }
Damience89a212013-10-15 22:25:17 +0100932 }
933}
934
Damiend99b0522013-12-21 18:17:45 +0000935mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000936 mp_obj_type_t *type = mp_obj_get_type(o_in);
937 if (type->iternext != NULL) {
938 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100939 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000940 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", type->name));
941 }
942}
943
944mp_obj_t rt_make_raise_obj(mp_obj_t o) {
945 DEBUG_printf("raise %p\n", o);
946 if (mp_obj_is_exception_type(o)) {
947 // o is an exception type (it is derived from BaseException (or is BaseException))
948 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +0000949 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
950 // could have const instances in ROM which we return here instead
Damien Georgec5966122014-02-15 16:10:44 +0000951 return rt_call_function_n_kw(o, 0, 0, NULL);
952 } else if (mp_obj_is_exception_instance(o)) {
953 // o is an instance of an exception, so use it as the exception
954 return o;
955 } else {
956 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
957 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +0100958 }
959}
960
Damiend99b0522013-12-21 18:17:45 +0000961mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +0000962 DEBUG_printf("import name %s\n", qstr_str(name));
963
Damiendb4c3612013-12-10 17:27:24 +0000964 // build args array
Damiend99b0522013-12-21 18:17:45 +0000965 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +0000966 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +0000967 args[1] = mp_const_none; // TODO should be globals
968 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000969 args[3] = fromlist;
970 args[4] = level; // must be 0; we don't yet support other values
971
972 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000973 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000974}
975
Damiend99b0522013-12-21 18:17:45 +0000976mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +0000977 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
978
Damiend99b0522013-12-21 18:17:45 +0000979 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000980 /* TODO convert AttributeError to ImportError
981 if (fail) {
982 (ImportError, "cannot import name %s", qstr_str(name), NULL)
983 }
984 */
985 return x;
986}
987
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200988void rt_import_all(mp_obj_t module) {
989 DEBUG_printf("import all %p\n", module);
990
991 mp_map_t *map = mp_obj_module_get_globals(module);
992 for (uint i = 0; i < map->alloc; i++) {
993 if (map->table[i].key != MP_OBJ_NULL) {
994 rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
995 }
996 }
997}
998
Damien George66028ab2014-01-03 14:03:48 +0000999mp_map_t *rt_locals_get(void) {
1000 return map_locals;
1001}
1002
1003void rt_locals_set(mp_map_t *m) {
1004 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
1005 map_locals = m;
1006}
1007
1008mp_map_t *rt_globals_get(void) {
1009 return map_globals;
1010}
1011
1012void rt_globals_set(mp_map_t *m) {
1013 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
1014 map_globals = m;
1015}
1016
Damien George0d028742014-01-22 23:59:20 +00001017mp_map_t *rt_loaded_modules_get(void) {
1018 return &map_loaded_modules;
1019}
1020
Damien6ba13142013-11-02 20:34:54 +00001021// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +01001022void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +00001023 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +01001024 rt_load_const_str,
1025 rt_load_name,
1026 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +01001027 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001028 rt_load_attr,
1029 rt_load_method,
1030 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001031 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001032 rt_store_subscr,
1033 rt_is_true,
1034 rt_unary_op,
Damien Georgecd82e022014-02-02 13:11:48 +00001035 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001036 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001037 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001038 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001039 rt_build_map,
1040 rt_store_map,
1041 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001042 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001043 rt_make_function_from_id,
Damien Georgecd82e022014-02-02 13:11:48 +00001044 rt_call_function_n_kw_for_native,
Damien George20006db2014-01-18 14:10:48 +00001045 rt_call_method_n_kw,
Damiend2755ec2013-10-16 23:58:48 +01001046 rt_getiter,
1047 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001048};
1049
1050/*
1051void rt_f_vector(rt_fun_kind_t fun_kind) {
1052 (rt_f_table[fun_kind])();
1053}
1054*/