blob: 2ab97ed18218c8b540cfd1336de0594d3ef6441d [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 <stdio.h>
6#include <string.h>
7#include <assert.h>
8
Damience89a212013-10-15 22:25:17 +01009#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000012#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000013#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000014#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
16#include "runtime.h"
17#include "map.h"
Damien660365e2013-12-17 18:27:24 +000018#include "builtin.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020019#include "objarray.h"
Damien George5fa93b62014-01-22 14:35:10 +000020#include "bc.h"
Damien660365e2013-12-17 18:27:24 +000021
Damien7f5dacf2013-10-10 11:24:39 +010022#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010023#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000024#define WRITE_CODE (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020025#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000026#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010027#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000028#define DEBUG_printf(...) (void)0
29#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010030#endif
Damien429d7192013-10-04 19:53:11 +010031
Damieneb19efb2013-10-10 22:06:54 +010032// locals and globals need to be pointers because they can be the same in outer module scope
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020033STATIC mp_map_t *map_locals;
34STATIC mp_map_t *map_globals;
35STATIC mp_map_t map_builtins;
36STATIC mp_map_t map_loaded_modules; // TODO: expose as sys.modules
Damienbd254452013-10-16 20:39:12 +010037
Damien429d7192013-10-04 19:53:11 +010038typedef enum {
Damiend99b0522013-12-21 18:17:45 +000039 MP_CODE_NONE,
40 MP_CODE_BYTE,
41 MP_CODE_NATIVE,
42 MP_CODE_INLINE_ASM,
43} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010044
Damiend99b0522013-12-21 18:17:45 +000045typedef struct _mp_code_t {
Damien George51047752014-02-26 17:40:52 +000046 mp_code_kind_t kind : 8;
47 uint scope_flags : 8;
48 uint n_args : 16;
49 uint n_state : 16;
Damien429d7192013-10-04 19:53:11 +010050 union {
51 struct {
Damien429d7192013-10-04 19:53:11 +010052 byte *code;
53 uint len;
54 } u_byte;
Damien826005c2013-10-05 23:17:28 +010055 struct {
Damiend99b0522013-12-21 18:17:45 +000056 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010057 } u_native;
58 struct {
Damiene4af64f2013-10-06 12:04:13 +010059 void *fun;
Damien826005c2013-10-05 23:17:28 +010060 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010061 };
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020062 qstr *arg_names;
Damiend99b0522013-12-21 18:17:45 +000063} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010064
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020065STATIC uint next_unique_code_id;
66STATIC machine_uint_t unique_codes_alloc = 0;
67STATIC mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010068
Damien0446a0d2013-11-17 13:16:36 +000069#ifdef WRITE_CODE
70FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010071#endif
Damien429d7192013-10-04 19:53:11 +010072
Damien Georgeaea532e2014-02-06 22:57:51 +000073// builtins
74// we put this table in ROM because it's always needed and takes up quite a bit of room in RAM
75// 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
76// 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
77// if we wanted to allow dynamic modification of the builtins, we could provide an mp_map_t object which is searched before this one
78
79typedef struct _mp_builtin_elem_t {
80 qstr qstr;
81 mp_obj_t fun;
82} mp_builtin_elem_t;
83
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020084STATIC const mp_builtin_elem_t builtin_table[] = {
Damien Georgeaea532e2014-02-06 22:57:51 +000085 // built-in core functions
86 { MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj },
87 { MP_QSTR___import__, (mp_obj_t)&mp_builtin___import___obj },
88 { MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
89
90 // built-in types
91 { MP_QSTR_bool, (mp_obj_t)&bool_type },
92#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000093 { MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
Damien Georgeaea532e2014-02-06 22:57:51 +000094#endif
95 { MP_QSTR_dict, (mp_obj_t)&dict_type },
96 { MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
97 { MP_QSTR_filter, (mp_obj_t)&filter_type },
98#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000099 { MP_QSTR_float, (mp_obj_t)&mp_type_float },
Damien Georgeaea532e2014-02-06 22:57:51 +0000100#endif
101 { MP_QSTR_int, (mp_obj_t)&int_type },
102 { MP_QSTR_list, (mp_obj_t)&list_type },
103 { MP_QSTR_map, (mp_obj_t)&map_type },
104 { MP_QSTR_set, (mp_obj_t)&set_type },
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200105 { MP_QSTR_str, (mp_obj_t)&str_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000106 { MP_QSTR_super, (mp_obj_t)&super_type },
107 { MP_QSTR_tuple, (mp_obj_t)&tuple_type },
Damien Georgec5966122014-02-15 16:10:44 +0000108 { MP_QSTR_type, (mp_obj_t)&mp_type_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000109 { MP_QSTR_zip, (mp_obj_t)&zip_type },
110
111 { MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
112 { MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
113
114 // built-in user functions
115 { MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
116 { MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
117 { MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
118 { MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj },
119 { MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
120 { MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
121 { MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
122 { MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
123 { MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
124 { MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
125 { MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
126 { MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
127 { MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
128 { MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
129 { MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
130 { MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
131 { MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
132 { MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
133 { MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
134 { MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
135 { MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
136 { MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
137 { MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
138 { MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
139 { MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
140 { MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
Damien Georgeaea532e2014-02-06 22:57:51 +0000141 { MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
142
Damien Georgec5966122014-02-15 16:10:44 +0000143 // built-in exceptions
144 { MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
145 { MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
146 { MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
147 { MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
148 { MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
149 { MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
150 { MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
151 { MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
152 { MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
153 { MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
154 { MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
155 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
156 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
157 { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
158 { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
159 { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
160 { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
161
Paul Sokolovsky910843e2014-02-14 12:02:34 +0200162 // Extra builtins as defined by a port
163 MICROPY_EXTRA_BUILTINS
164
Damien Georgeaea532e2014-02-06 22:57:51 +0000165 { MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
166};
167
Damien George38a2da62014-01-08 17:33:12 +0000168// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200169STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +0000170 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
171}
172
Damien8b3a7c22013-10-23 20:20:17 +0100173void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +0100174 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +0000175 map_locals = map_globals = mp_map_new(1);
Damien George5fa93b62014-01-22 14:35:10 +0000176 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +0100177
Damienb86e3f92013-12-29 17:17:43 +0000178 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +0000179 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +0000180
Damien George0d028742014-01-22 23:59:20 +0000181 // init loaded modules table
182 mp_map_init(&map_loaded_modules, 3);
183
Damien Georgee9906ac2014-01-04 18:44:46 +0000184 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000185 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000186
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200187 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
188 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200189
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200190 mp_obj_t m_collections = mp_obj_new_module(MP_QSTR_collections);
191 rt_store_attr(m_collections, MP_QSTR_namedtuple, (mp_obj_t)&mp_namedtuple_obj);
192
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200193#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +0200194 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200195 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
196 // Avoid warning of unused var
197 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200198#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200199 // init sys.path
200 // for efficiency, left to platform-specific startup code
201 //sys_path = mp_obj_new_list(0, NULL);
202 //rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200203
Damien George0c36da02014-03-08 15:24:39 +0000204 // we pre-import the micropython module
205 // probably shouldn't do this, so we are compatible with CPython
206 rt_store_name(MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython);
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
Damiend99b0522013-12-21 18:17:45 +0000374mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000375 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000376 uint len;
377 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +0000378 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +0000379}
380
Damiend99b0522013-12-21 18:17:45 +0000381mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100382 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000383 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100384}
385
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200386mp_obj_t rt_load_const_bytes(qstr qstr) {
387 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
388 uint len;
389 const byte *data = qstr_data(qstr, &len);
390 return mp_obj_new_bytes(data, len);
391}
392
Damiend99b0522013-12-21 18:17:45 +0000393mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100394 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100395 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000396 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 +0000397 if (elem != NULL) {
398 return elem->value;
399 } else {
400 return rt_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100401 }
Damiena3977762013-10-09 23:10:10 +0100402}
403
Damiend99b0522013-12-21 18:17:45 +0000404mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100405 // logic: search globals, builtins
406 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000407 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100408 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000409 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100410 if (elem == NULL) {
Damien Georgeaea532e2014-02-06 22:57:51 +0000411 for (const mp_builtin_elem_t *e = &builtin_table[0]; e->qstr != MP_QSTR_; e++) {
412 if (e->qstr == qstr) {
413 return e->fun;
414 }
415 }
Damien Georgec5966122014-02-15 16:10:44 +0000416 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 +0100417 }
418 }
419 return elem->value;
420}
421
Damiend99b0522013-12-21 18:17:45 +0000422mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100423 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000424 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 +0000425 if (elem != NULL) {
426 return elem->value;
427 } else {
428 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100429 }
Damien429d7192013-10-04 19:53:11 +0100430}
431
Damiend99b0522013-12-21 18:17:45 +0000432mp_obj_t rt_get_cell(mp_obj_t cell) {
433 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000434}
435
Damiend99b0522013-12-21 18:17:45 +0000436void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
437 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000438}
439
Damiend99b0522013-12-21 18:17:45 +0000440void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100441 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000442 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 +0100443}
444
Damiend99b0522013-12-21 18:17:45 +0000445void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100446 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000447 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 +0100448}
449
Damiend99b0522013-12-21 18:17:45 +0000450mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000451 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000452
Damiend99b0522013-12-21 18:17:45 +0000453 if (MP_OBJ_IS_SMALL_INT(arg)) {
454 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000455 switch (op) {
Damien George9d68e9c2014-03-12 15:38:15 +0000456 case RT_UNARY_OP_BOOL:
457 return MP_BOOL(val != 0);
458 case RT_UNARY_OP_POSITIVE:
459 return arg;
460 case RT_UNARY_OP_NEGATIVE:
461 // check for overflow
462 if (val == MP_SMALL_INT_MIN) {
463 return mp_obj_new_int(-val);
464 } else {
465 return MP_OBJ_NEW_SMALL_INT(-val);
466 }
467 case RT_UNARY_OP_INVERT:
468 return MP_OBJ_NEW_SMALL_INT(~val);
469 default:
470 assert(0);
471 return arg;
Damien7410e442013-11-02 19:47:57 +0000472 }
Damien George1e708fe2014-01-23 18:27:51 +0000473 } else {
474 mp_obj_type_t *type = mp_obj_get_type(arg);
475 if (type->unary_op != NULL) {
476 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000477 if (result != NULL) {
478 return result;
479 }
Damien7410e442013-11-02 19:47:57 +0000480 }
Damiend99b0522013-12-21 18:17:45 +0000481 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000482 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bad operand type for unary operator: '%s'", mp_obj_get_type_str(arg)));
Damien7410e442013-11-02 19:47:57 +0000483 }
Damien429d7192013-10-04 19:53:11 +0100484}
485
Damiend99b0522013-12-21 18:17:45 +0000486mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100487 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000488
489 // TODO correctly distinguish inplace operators for mutable objects
490 // lookup logic that CPython uses for +=:
491 // check for implemented +=
492 // then check for implemented +
493 // then check for implemented seq.inplace_concat
494 // then check for implemented seq.concat
495 // then fail
496 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
497
Damien George9aa2a522014-02-01 23:04:09 +0000498 // deal with is
499 if (op == RT_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200500 return MP_BOOL(lhs == rhs);
501 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200502
Damien Georgebcbeea02014-01-11 10:47:22 +0000503 // deal with == and != for all types
Damien George9aa2a522014-02-01 23:04:09 +0000504 if (op == RT_BINARY_OP_EQUAL || op == RT_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000505 if (mp_obj_equal(lhs, rhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000506 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000507 return mp_const_true;
508 } else {
509 return mp_const_false;
510 }
511 } else {
Damien George9aa2a522014-02-01 23:04:09 +0000512 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000513 return mp_const_false;
514 } else {
515 return mp_const_true;
516 }
517 }
518 }
519
520 // deal with exception_match for all types
Damien George9aa2a522014-02-01 23:04:09 +0000521 if (op == RT_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000522 // rhs must be issubclass(rhs, BaseException)
523 if (mp_obj_is_exception_type(rhs)) {
524 // if lhs is an instance of an exception, then extract and use its type
525 if (mp_obj_is_exception_instance(lhs)) {
526 lhs = mp_obj_get_type(lhs);
527 }
Damien George71510152014-03-03 22:38:13 +0000528 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000529 return mp_const_true;
530 } else {
531 return mp_const_false;
532 }
533 }
534 }
535
Damien George1a9951d2014-01-06 22:13:00 +0000536 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000537 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000538 if (MP_OBJ_IS_SMALL_INT(rhs)) {
539 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000540 // This is a binary operation: lhs_val op rhs_val
541 // We need to be careful to handle overflow; see CERT INT32-C
542 // Operations that can overflow:
543 // + result always fits in machine_int_t, then handled by SMALL_INT check
544 // - result always fits in machine_int_t, then handled by SMALL_INT check
545 // * checked explicitly
546 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
547 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
548 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000549 switch (op) {
550 case RT_BINARY_OP_OR:
551 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
552 case RT_BINARY_OP_XOR:
553 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
554 case RT_BINARY_OP_AND:
555 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
556 case RT_BINARY_OP_LSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000557 case RT_BINARY_OP_INPLACE_LSHIFT: {
558 if (rhs_val < 0) {
559 // negative shift not allowed
560 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
561 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
562 // left-shift will overflow, so use higher precision integer
563 lhs = mp_obj_new_int_from_ll(lhs_val);
564 goto generic_binary_op;
565 } else {
566 // use standard precision
567 lhs_val <<= rhs_val;
568 }
569 break;
570 }
Damien George1a9951d2014-01-06 22:13:00 +0000571 case RT_BINARY_OP_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000572 case RT_BINARY_OP_INPLACE_RSHIFT:
573 if (rhs_val < 0) {
574 // negative shift not allowed
575 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
576 } else {
577 // standard precision is enough for right-shift
578 lhs_val >>= rhs_val;
579 }
580 break;
Damien George1a9951d2014-01-06 22:13:00 +0000581 case RT_BINARY_OP_ADD:
582 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
583 case RT_BINARY_OP_SUBTRACT:
584 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
585 case RT_BINARY_OP_MULTIPLY:
Damien George9d68e9c2014-03-12 15:38:15 +0000586 case RT_BINARY_OP_INPLACE_MULTIPLY: {
587
588 // If long long type exists and is larger than machine_int_t, then
589 // we can use the following code to perform overflow-checked multiplication.
590 // Otherwise (eg in x64 case) we must use the branching code below.
591 #if 0
592 // compute result using long long precision
593 long long res = (long long)lhs_val * (long long)rhs_val;
594 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
595 // result overflowed SMALL_INT, so return higher precision integer
596 return mp_obj_new_int_from_ll(res);
597 } else {
598 // use standard precision
599 lhs_val = (mp_small_int_t)res;
600 }
601 #endif
602
603 if (lhs_val > 0) { // lhs_val is positive
604 if (rhs_val > 0) { // lhs_val and rhs_val are positive
605 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
606 goto mul_overflow;
607 }
608 } else { // lhs_val positive, rhs_val nonpositive
609 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
610 goto mul_overflow;
611 }
612 } // lhs_val positive, rhs_val nonpositive
613 } else { // lhs_val is nonpositive
614 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
615 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
616 goto mul_overflow;
617 }
618 } else { // lhs_val and rhs_val are nonpositive
619 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
620 goto mul_overflow;
621 }
622 } // End if lhs_val and rhs_val are nonpositive
623 } // End if lhs_val is nonpositive
624
625 // use standard precision
626 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
627
628 mul_overflow:
629 // use higher precision
630 lhs = mp_obj_new_int_from_ll(lhs_val);
631 goto generic_binary_op;
632
633 break;
634 }
Damien George1a9951d2014-01-06 22:13:00 +0000635 case RT_BINARY_OP_FLOOR_DIVIDE:
636 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
Damien George9d68e9c2014-03-12 15:38:15 +0000637 #if MICROPY_ENABLE_FLOAT
Damien George1a9951d2014-01-06 22:13:00 +0000638 case RT_BINARY_OP_TRUE_DIVIDE:
639 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000640 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000641
Damien George1a9951d2014-01-06 22:13:00 +0000642 // TODO implement modulo as specified by Python
643 case RT_BINARY_OP_MODULO:
644 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000645
Damien George1a9951d2014-01-06 22:13:00 +0000646 case RT_BINARY_OP_POWER:
647 case RT_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000648 if (rhs_val < 0) {
649 #if MICROPY_ENABLE_FLOAT
650 lhs = mp_obj_new_float(lhs_val);
651 goto generic_binary_op;
652 #else
653 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
654 #endif
655 } else {
656 // TODO check for overflow
657 machine_int_t ans = 1;
658 while (rhs_val > 0) {
659 if (rhs_val & 1) {
660 ans *= lhs_val;
661 }
662 lhs_val *= lhs_val;
663 rhs_val /= 2;
Damien George1a9951d2014-01-06 22:13:00 +0000664 }
Damien George9d68e9c2014-03-12 15:38:15 +0000665 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000666 }
Damien George1a9951d2014-01-06 22:13:00 +0000667 break;
Damien George9aa2a522014-02-01 23:04:09 +0000668 case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
669 case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
670 case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
671 case RT_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000672
Damien George1a9951d2014-01-06 22:13:00 +0000673 default: assert(0);
674 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200675 // TODO: We just should make mp_obj_new_int() inline and use that
676 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000677 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000678 } else {
679 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000680 }
Damien George3f759b72014-01-31 00:42:12 +0000681#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000682 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000683 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000684 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000685 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000686#endif
Damien429d7192013-10-04 19:53:11 +0100687 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000688 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000689
Damien George9aa2a522014-02-01 23:04:09 +0000690 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000691 *
692 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000693 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000694 */
Damien George9aa2a522014-02-01 23:04:09 +0000695 if (op == RT_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000696 mp_obj_type_t *type = mp_obj_get_type(rhs);
697 if (type->binary_op != NULL) {
698 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000699 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000700 return res;
701 }
702 }
703 if (type->getiter != NULL) {
704 /* second attempt, walk the iterator */
705 mp_obj_t next = NULL;
706 mp_obj_t iter = rt_getiter(rhs);
707 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
708 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000709 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000710 }
Damien7410e442013-11-02 19:47:57 +0000711 }
Damien George9aa2a522014-02-01 23:04:09 +0000712 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000713 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000714
715 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000716 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000717 mp_obj_get_type_str(rhs)));
718 return mp_const_none;
719 }
720
Damien George5fa93b62014-01-22 14:35:10 +0000721 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000722 mp_obj_type_t *type;
723generic_binary_op:
724 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000725 if (type->binary_op != NULL) {
726 mp_obj_t result = type->binary_op(op, lhs, rhs);
727 if (result != MP_OBJ_NULL) {
728 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000729 }
Damien429d7192013-10-04 19:53:11 +0100730 }
Damiend99b0522013-12-21 18:17:45 +0000731
Damien George5fa93b62014-01-22 14:35:10 +0000732 // TODO implement dispatch for reverse binary ops
733
Damiend99b0522013-12-21 18:17:45 +0000734 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000735 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200736 "unsupported operand types for binary operator: '%s', '%s'",
737 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000738 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100739}
740
Paul Sokolovsky90750022014-02-01 15:05:04 +0200741mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100742 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
743 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100744 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000745 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100746 }
Damiend99b0522013-12-21 18:17:45 +0000747
748 // make the function, depending on the code kind
749 mp_code_t *c = &unique_codes[unique_code_id];
750 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100751 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000752 case MP_CODE_BYTE:
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200753 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 +0100754 break;
Damiend99b0522013-12-21 18:17:45 +0000755 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000756 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100757 break;
Damiend99b0522013-12-21 18:17:45 +0000758 case MP_CODE_INLINE_ASM:
759 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100760 break;
761 default:
762 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000763 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100764 }
Damienbd254452013-10-16 20:39:12 +0100765
766 // check for generator functions and if so wrap in generator object
Damien George8725f8f2014-02-15 19:33:11 +0000767 if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien Georged0691cc2014-01-29 20:30:52 +0000768 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100769 }
770
Damiend99b0522013-12-21 18:17:45 +0000771 return fun;
Damien429d7192013-10-04 19:53:11 +0100772}
773
Damiend99b0522013-12-21 18:17:45 +0000774mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000775 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000776 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200777 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000778 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000779 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000780}
781
Damiend99b0522013-12-21 18:17:45 +0000782mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000783 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100784}
785
Damiend99b0522013-12-21 18:17:45 +0000786mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000787 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100788}
789
Damiend99b0522013-12-21 18:17:45 +0000790mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
791 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000792 args[0] = arg1;
793 args[1] = arg2;
794 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100795}
796
Damien Georgecd82e022014-02-02 13:11:48 +0000797// wrapper that accepts n_args and n_kw in one argument
798// native emitter can only pass at most 3 arguments to a function
799mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
800 return rt_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
801}
802
Damien George20006db2014-01-18 14:10:48 +0000803// args contains, eg: arg0 arg1 key0 value0 key1 value1
804mp_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 +0000805 // TODO improve this: fun object can specify its type and we parse here the arguments,
806 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100807
John R. Lenton9c83ec02014-01-07 23:06:46 +0000808 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
809
Damien George8b56beb2014-01-31 23:49:49 +0000810 // get the type
811 mp_obj_type_t *type = mp_obj_get_type(fun_in);
812
813 // do the call
814 if (type->call != NULL) {
815 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000816 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000817 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000818 }
Damien86c7fc72013-11-26 15:16:41 +0000819}
820
Damien George20006db2014-01-18 14:10:48 +0000821// 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)
822// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000823mp_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 +0000824 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);
825 int adjust = (args[1] == NULL) ? 0 : 1;
826 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000827}
828
Damiend99b0522013-12-21 18:17:45 +0000829mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000830 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100831}
832
Damiend99b0522013-12-21 18:17:45 +0000833mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000834 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100835}
836
Damiend99b0522013-12-21 18:17:45 +0000837mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
838 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100839}
840
Damiend99b0522013-12-21 18:17:45 +0000841mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000842 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100843 return set;
844}
845
Damien George932bf1c2014-01-18 23:42:49 +0000846// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000847void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200848 uint seq_len;
Damiend99b0522013-12-21 18:17:45 +0000849 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
Damiend99b0522013-12-21 18:17:45 +0000850 mp_obj_t *seq_items;
851 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
852 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
853 } else {
854 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000855 }
Damiend99b0522013-12-21 18:17:45 +0000856 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200857 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000858 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200859 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000860 }
Damien George932bf1c2014-01-18 23:42:49 +0000861 for (uint i = 0; i < num; i++) {
862 items[i] = seq_items[num - 1 - i];
863 }
Damien86c7fc72013-11-26 15:16:41 +0000864 } else {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200865 mp_obj_t iterable = rt_getiter(seq_in);
866
867 for (seq_len = 0; seq_len < num; seq_len++) {
868 mp_obj_t el = rt_iternext(iterable);
869 if (el == mp_const_stop_iteration) {
870 goto too_short;
871 }
872 items[num - 1 - seq_len] = el;
873 }
874 if (rt_iternext(iterable) != mp_const_stop_iteration) {
875 goto too_long;
876 }
Damien86c7fc72013-11-26 15:16:41 +0000877 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200878 return;
879
880too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000881 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 +0200882too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000883 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 +0000884}
885
Damiend99b0522013-12-21 18:17:45 +0000886mp_obj_t rt_build_map(int n_args) {
887 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100888}
889
Damiend99b0522013-12-21 18:17:45 +0000890mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
891 // map should always be a dict
892 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100893}
894
Damiend99b0522013-12-21 18:17:45 +0000895mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000896 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
897 // use load_method
898 mp_obj_t dest[2];
899 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000900 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000901 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000902 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000903 } else {
904 // load_method returned a method, so build a bound method object
905 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000906 }
Damiend99b0522013-12-21 18:17:45 +0000907}
908
Damien George7c9c6672014-01-25 00:17:36 +0000909// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
910// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
911// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200912STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000913 // clear output to indicate no attribute/method found yet
914 dest[0] = MP_OBJ_NULL;
915 dest[1] = MP_OBJ_NULL;
916
917 // get the type
918 mp_obj_type_t *type = mp_obj_get_type(base);
919
920 // if this type can do its own load, then call it
921 if (type->load_attr != NULL) {
922 type->load_attr(base, attr, dest);
923 }
924
925 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000926 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000927 if (attr == MP_QSTR___class__) {
928 // a.__class__ is equivalent to type(a)
929 dest[0] = type;
930 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000931 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
932 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000933 } else if (type->load_attr == NULL) {
934 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000935 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000936 const mp_method_t *meth = type->methods;
937 if (meth != NULL) {
938 for (; meth->name != NULL; meth++) {
939 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000940 // check if the methods are functions, static or class methods
941 // see http://docs.python.org/3.3/howto/descriptor.html
942 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
943 // return just the function
Damien George64131f32014-02-06 20:31:44 +0000944 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000945 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
946 // return a bound method, with self being the type of this object
Damien George64131f32014-02-06 20:31:44 +0000947 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien George20006db2014-01-18 14:10:48 +0000948 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000949 } else {
950 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000951 dest[0] = (mp_obj_t)meth->fun;
952 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000953 }
Damien George062478e2014-01-09 20:57:50 +0000954 break;
955 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000956 }
Damiend57eba52013-11-02 23:58:14 +0000957 }
958 }
Damiena3977762013-10-09 23:10:10 +0100959 }
Damien George7c9c6672014-01-25 00:17:36 +0000960}
Damiena3977762013-10-09 23:10:10 +0100961
Damien George7c9c6672014-01-25 00:17:36 +0000962void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
963 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
964
965 rt_load_method_maybe(base, attr, dest);
966
967 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000968 // no attribute/method called attr
969 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000970 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
971 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 +0000972 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000973 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 +0000974 }
975 }
Damiena3977762013-10-09 23:10:10 +0100976}
977
Damiend99b0522013-12-21 18:17:45 +0000978void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100979 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000980 mp_obj_type_t *type = mp_obj_get_type(base);
981 if (type->store_attr != NULL) {
982 if (type->store_attr(base, attr, value)) {
983 return;
984 }
Damiena3977762013-10-09 23:10:10 +0100985 }
Damien Georgec5966122014-02-15 16:10:44 +0000986 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 +0100987}
988
Damiend99b0522013-12-21 18:17:45 +0000989void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100990 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000991 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100992 // list store
Damiend99b0522013-12-21 18:17:45 +0000993 mp_obj_list_store(base, index, value);
994 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
995 // dict store
996 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100997 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200998 mp_obj_type_t *type = mp_obj_get_type(base);
999 if (type->store_item != NULL) {
1000 bool r = type->store_item(base, index, value);
1001 if (r) {
1002 return;
1003 }
1004 // TODO: call base classes here?
1005 }
Damien Georgec5966122014-02-15 16:10:44 +00001006 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 +01001007 }
1008}
1009
Damiend99b0522013-12-21 18:17:45 +00001010mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001011 mp_obj_type_t *type = mp_obj_get_type(o_in);
1012 if (type->getiter != NULL) {
1013 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +01001014 } else {
Damien George7c9c6672014-01-25 00:17:36 +00001015 // check for __getitem__ method
1016 mp_obj_t dest[2];
Damien George7d0bfbe2014-02-08 19:01:47 +00001017 rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George7c9c6672014-01-25 00:17:36 +00001018 if (dest[0] != MP_OBJ_NULL) {
1019 // __getitem__ exists, create an iterator
1020 return mp_obj_new_getitem_iter(dest);
1021 } else {
1022 // object not iterable
Damien George0ec6bd42014-03-09 16:29:36 +00001023 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
Damien George7c9c6672014-01-25 00:17:36 +00001024 }
Damience89a212013-10-15 22:25:17 +01001025 }
1026}
1027
Damiend99b0522013-12-21 18:17:45 +00001028mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001029 mp_obj_type_t *type = mp_obj_get_type(o_in);
1030 if (type->iternext != NULL) {
1031 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +01001032 } else {
Damien George0ec6bd42014-03-09 16:29:36 +00001033 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
Damien Georgec5966122014-02-15 16:10:44 +00001034 }
1035}
1036
1037mp_obj_t rt_make_raise_obj(mp_obj_t o) {
1038 DEBUG_printf("raise %p\n", o);
1039 if (mp_obj_is_exception_type(o)) {
1040 // o is an exception type (it is derived from BaseException (or is BaseException))
1041 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001042 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1043 // could have const instances in ROM which we return here instead
Damien Georgec5966122014-02-15 16:10:44 +00001044 return rt_call_function_n_kw(o, 0, 0, NULL);
1045 } else if (mp_obj_is_exception_instance(o)) {
1046 // o is an instance of an exception, so use it as the exception
1047 return o;
1048 } else {
1049 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1050 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001051 }
1052}
1053
Damiend99b0522013-12-21 18:17:45 +00001054mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001055 DEBUG_printf("import name %s\n", qstr_str(name));
1056
Damiendb4c3612013-12-10 17:27:24 +00001057 // build args array
Damiend99b0522013-12-21 18:17:45 +00001058 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001059 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001060 args[1] = mp_const_none; // TODO should be globals
1061 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001062 args[3] = fromlist;
1063 args[4] = level; // must be 0; we don't yet support other values
1064
1065 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001066 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001067}
1068
Damiend99b0522013-12-21 18:17:45 +00001069mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001070 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1071
Damiend99b0522013-12-21 18:17:45 +00001072 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +00001073 /* TODO convert AttributeError to ImportError
1074 if (fail) {
1075 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1076 }
1077 */
1078 return x;
1079}
1080
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001081void rt_import_all(mp_obj_t module) {
1082 DEBUG_printf("import all %p\n", module);
1083
1084 mp_map_t *map = mp_obj_module_get_globals(module);
1085 for (uint i = 0; i < map->alloc; i++) {
1086 if (map->table[i].key != MP_OBJ_NULL) {
1087 rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
1088 }
1089 }
1090}
1091
Damien George66028ab2014-01-03 14:03:48 +00001092mp_map_t *rt_locals_get(void) {
1093 return map_locals;
1094}
1095
1096void rt_locals_set(mp_map_t *m) {
1097 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
1098 map_locals = m;
1099}
1100
1101mp_map_t *rt_globals_get(void) {
1102 return map_globals;
1103}
1104
1105void rt_globals_set(mp_map_t *m) {
1106 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
1107 map_globals = m;
1108}
1109
Damien George0d028742014-01-22 23:59:20 +00001110mp_map_t *rt_loaded_modules_get(void) {
1111 return &map_loaded_modules;
1112}
1113
Damien6ba13142013-11-02 20:34:54 +00001114// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +01001115void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +00001116 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +01001117 rt_load_const_str,
1118 rt_load_name,
1119 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +01001120 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001121 rt_load_attr,
1122 rt_load_method,
1123 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001124 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001125 rt_store_subscr,
1126 rt_is_true,
1127 rt_unary_op,
Damien Georgecd82e022014-02-02 13:11:48 +00001128 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001129 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001130 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001131 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001132 rt_build_map,
1133 rt_store_map,
1134 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001135 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001136 rt_make_function_from_id,
Damien Georgecd82e022014-02-02 13:11:48 +00001137 rt_call_function_n_kw_for_native,
Damien George20006db2014-01-18 14:10:48 +00001138 rt_call_method_n_kw,
Damiend2755ec2013-10-16 23:58:48 +01001139 rt_getiter,
1140 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001141};
1142
1143/*
1144void rt_f_vector(rt_fun_kind_t fun_kind) {
1145 (rt_f_table[fun_kind])();
1146}
1147*/