blob: 17a9e778d196d39876552d36793fcb842b901dd7 [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>
Rachel Dowdall56402792014-03-22 20:19:24 +00008#include <math.h>
Damien429d7192013-10-04 19:53:11 +01009
Damience89a212013-10-15 22:25:17 +010010#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000012#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000013#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000014#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000015#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000016#include "runtime0.h"
17#include "runtime.h"
18#include "map.h"
Damien660365e2013-12-17 18:27:24 +000019#include "builtin.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020020#include "objarray.h"
Damien George5fa93b62014-01-22 14:35:10 +000021#include "bc.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000022#include "intdivmod.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 },
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +020094 { MP_QSTR_bytes, (mp_obj_t)&bytes_type },
Damien Georgeaea532e2014-02-06 22:57:51 +000095#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000096 { MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
Damien Georgeaea532e2014-02-06 22:57:51 +000097#endif
98 { MP_QSTR_dict, (mp_obj_t)&dict_type },
99 { MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
100 { MP_QSTR_filter, (mp_obj_t)&filter_type },
101#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000102 { MP_QSTR_float, (mp_obj_t)&mp_type_float },
Damien Georgeaea532e2014-02-06 22:57:51 +0000103#endif
104 { MP_QSTR_int, (mp_obj_t)&int_type },
105 { MP_QSTR_list, (mp_obj_t)&list_type },
106 { MP_QSTR_map, (mp_obj_t)&map_type },
Damien George3ec0a1a2014-03-22 21:31:28 +0000107 { MP_QSTR_object, (mp_obj_t)&mp_type_object },
Damien Georgeaea532e2014-02-06 22:57:51 +0000108 { MP_QSTR_set, (mp_obj_t)&set_type },
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200109 { MP_QSTR_str, (mp_obj_t)&str_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000110 { MP_QSTR_super, (mp_obj_t)&super_type },
111 { MP_QSTR_tuple, (mp_obj_t)&tuple_type },
Damien Georgec5966122014-02-15 16:10:44 +0000112 { MP_QSTR_type, (mp_obj_t)&mp_type_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000113 { MP_QSTR_zip, (mp_obj_t)&zip_type },
114
115 { MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
116 { MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
117
118 // built-in user functions
119 { MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
120 { MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
121 { MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
Damien Georgeaea532e2014-02-06 22:57:51 +0000122 { MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
123 { MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
124 { MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
125 { MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
126 { MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
127 { MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
128 { MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
129 { MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
130 { MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
131 { MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
132 { MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
133 { MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
134 { MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
135 { MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
136 { MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
137 { MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
138 { MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
139 { MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
140 { MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
141 { MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
142 { MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
143 { MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
Damien Georgeaea532e2014-02-06 22:57:51 +0000144 { MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
145
Damien Georgec5966122014-02-15 16:10:44 +0000146 // built-in exceptions
147 { MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000148 { MP_QSTR_ArithmeticError, (mp_obj_t)&mp_type_ArithmeticError },
Damien Georgec5966122014-02-15 16:10:44 +0000149 { MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
150 { MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000151 { MP_QSTR_BufferError, (mp_obj_t)&mp_type_BufferError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000152 { MP_QSTR_EOFError, (mp_obj_t)&mp_type_EOFError },
153 { MP_QSTR_EnvironmentError, (mp_obj_t)&mp_type_EnvironmentError },
154 { MP_QSTR_Exception, (mp_obj_t)&mp_type_Exception },
155 { MP_QSTR_FloatingPointError, (mp_obj_t)&mp_type_FloatingPointError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000156 { MP_QSTR_GeneratorExit, (mp_obj_t)&mp_type_GeneratorExit },
157 { MP_QSTR_IOError, (mp_obj_t)&mp_type_IOError },
Damien Georgec5966122014-02-15 16:10:44 +0000158 { MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
159 { MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
160 { MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
161 { MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000162 { MP_QSTR_LookupError, (mp_obj_t)&mp_type_LookupError },
163 { MP_QSTR_MemoryError, (mp_obj_t)&mp_type_MemoryError },
Damien Georgec5966122014-02-15 16:10:44 +0000164 { MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000165 { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
166 { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
167 { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000168 { MP_QSTR_ReferenceError, (mp_obj_t)&mp_type_ReferenceError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000169 { MP_QSTR_RuntimeError, (mp_obj_t)&mp_type_RuntimeError },
Damien Georgec5966122014-02-15 16:10:44 +0000170 { MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000171 { MP_QSTR_SystemError, (mp_obj_t)&mp_type_SystemError },
172 { MP_QSTR_SystemExit, (mp_obj_t)&mp_type_SystemExit },
173 { MP_QSTR_TabError, (mp_obj_t)&mp_type_TabError },
Damien Georgec5966122014-02-15 16:10:44 +0000174 { MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000175 { MP_QSTR_UnboundLocalError, (mp_obj_t)&mp_type_UnboundLocalError },
Damien Georgec5966122014-02-15 16:10:44 +0000176 { MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000177 { MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
178 { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
Damien Georgec5966122014-02-15 16:10:44 +0000179 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
180 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
Damien Georgec5966122014-02-15 16:10:44 +0000181
Paul Sokolovsky910843e2014-02-14 12:02:34 +0200182 // Extra builtins as defined by a port
183 MICROPY_EXTRA_BUILTINS
184
Damien Georgeaea532e2014-02-06 22:57:51 +0000185 { MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
186};
187
Damien George38a2da62014-01-08 17:33:12 +0000188// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200189STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +0000190 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
191}
192
Damien8b3a7c22013-10-23 20:20:17 +0100193void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +0100194 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +0000195 map_locals = map_globals = mp_map_new(1);
Damien George5fa93b62014-01-22 14:35:10 +0000196 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +0100197
Damienb86e3f92013-12-29 17:17:43 +0000198 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +0000199 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +0000200
Damien George0d028742014-01-22 23:59:20 +0000201 // init loaded modules table
202 mp_map_init(&map_loaded_modules, 3);
203
Damien Georgee9906ac2014-01-04 18:44:46 +0000204 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000205 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000206
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200207 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
208 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200209
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200210 mp_obj_t m_collections = mp_obj_new_module(MP_QSTR_collections);
211 rt_store_attr(m_collections, MP_QSTR_namedtuple, (mp_obj_t)&mp_namedtuple_obj);
212
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200213#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +0200214 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200215 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
216 // Avoid warning of unused var
217 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200218#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200219 // init sys.path
220 // for efficiency, left to platform-specific startup code
221 //sys_path = mp_obj_new_list(0, NULL);
222 //rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200223
Damien George0c36da02014-03-08 15:24:39 +0000224 // we pre-import the micropython module
225 // probably shouldn't do this, so we are compatible with CPython
226 rt_store_name(MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +0200227
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200228 // TODO: wastes one mp_code_t structure in mem
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000229 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000230 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100231 unique_codes = NULL;
232
Damien0446a0d2013-11-17 13:16:36 +0000233#ifdef WRITE_CODE
234 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100235#endif
Damien429d7192013-10-04 19:53:11 +0100236}
237
Damien8b3a7c22013-10-23 20:20:17 +0100238void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000239 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200240 mp_map_free(map_globals);
241 mp_map_deinit(&map_loaded_modules);
242 mp_map_deinit(&map_builtins);
Damien0446a0d2013-11-17 13:16:36 +0000243#ifdef WRITE_CODE
244 if (fp_write_code != NULL) {
245 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100246 }
Damiena1ddfcc2013-10-10 23:25:50 +0100247#endif
Damien429d7192013-10-04 19:53:11 +0100248}
249
Damien Georged0691cc2014-01-29 20:30:52 +0000250uint rt_get_unique_code_id(void) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000251 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100252}
253
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200254STATIC void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000255 if (next_unique_code_id > unique_codes_alloc) {
Damien Georged0691cc2014-01-29 20:30:52 +0000256 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 +0000257 // increase size of unique_codes table
258 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
Damien Georged0691cc2014-01-29 20:30:52 +0000259 for (uint i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000260 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100261 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000262 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100263 }
Damien826005c2013-10-05 23:17:28 +0100264}
265
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200266void 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 +0100267 alloc_unique_codes();
268
John R. Lenton9c83ec02014-01-07 23:06:46 +0000269 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 +0000270 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien George8725f8f2014-02-15 19:33:11 +0000271 unique_codes[unique_code_id].scope_flags = scope_flags;
Damien Georged0691cc2014-01-29 20:30:52 +0000272 unique_codes[unique_code_id].n_args = n_args;
273 unique_codes[unique_code_id].n_state = n_locals + n_stack;
Damien826005c2013-10-05 23:17:28 +0100274 unique_codes[unique_code_id].u_byte.code = code;
275 unique_codes[unique_code_id].u_byte.len = len;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200276 unique_codes[unique_code_id].arg_names = arg_names;
Damien826005c2013-10-05 23:17:28 +0100277
Damien9ecbcff2013-12-11 00:41:43 +0000278 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000279
280#ifdef DEBUG_PRINT
Damien George08d07552014-01-29 18:58:52 +0000281 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 +0000282 for (int i = 0; i < 128 && i < len; i++) {
283 if (i > 0 && i % 16 == 0) {
284 DEBUG_printf("\n");
285 }
286 DEBUG_printf(" %02x", code[i]);
287 }
288 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000289#if MICROPY_DEBUG_PRINTERS
290 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000291#endif
Damien0446a0d2013-11-17 13:16:36 +0000292#endif
Damien826005c2013-10-05 23:17:28 +0100293}
294
Damien Georged0691cc2014-01-29 20:30:52 +0000295void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100296 alloc_unique_codes();
297
John R. Lenton9c83ec02014-01-07 23:06:46 +0000298 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 +0000299 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien George8725f8f2014-02-15 19:33:11 +0000300 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000301 unique_codes[unique_code_id].n_args = n_args;
302 unique_codes[unique_code_id].n_state = 0;
Damien429d7192013-10-04 19:53:11 +0100303 unique_codes[unique_code_id].u_native.fun = fun;
304
Damien George8cc96a32013-12-30 18:23:50 +0000305 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000306
Damiena1ddfcc2013-10-10 23:25:50 +0100307#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100308 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
309 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
310 for (int i = 0; i < 128 && i < len; i++) {
311 if (i > 0 && i % 16 == 0) {
312 DEBUG_printf("\n");
313 }
314 DEBUG_printf(" %02x", fun_data[i]);
315 }
316 DEBUG_printf("\n");
317
Damien0446a0d2013-11-17 13:16:36 +0000318#ifdef WRITE_CODE
319 if (fp_write_code != NULL) {
320 fwrite(fun_data, len, 1, fp_write_code);
321 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100322 }
Damiena1ddfcc2013-10-10 23:25:50 +0100323#endif
324#endif
Damien429d7192013-10-04 19:53:11 +0100325}
326
Damien Georged0691cc2014-01-29 20:30:52 +0000327void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100328 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100329
John R. Lenton9c83ec02014-01-07 23:06:46 +0000330 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 +0000331 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien George8725f8f2014-02-15 19:33:11 +0000332 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000333 unique_codes[unique_code_id].n_args = n_args;
334 unique_codes[unique_code_id].n_state = 0;
Damien826005c2013-10-05 23:17:28 +0100335 unique_codes[unique_code_id].u_inline_asm.fun = fun;
336
Damiena1ddfcc2013-10-10 23:25:50 +0100337#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100338 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
339 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
340 for (int i = 0; i < 128 && i < len; i++) {
341 if (i > 0 && i % 16 == 0) {
342 DEBUG_printf("\n");
343 }
344 DEBUG_printf(" %02x", fun_data[i]);
345 }
346 DEBUG_printf("\n");
347
Damien0446a0d2013-11-17 13:16:36 +0000348#ifdef WRITE_CODE
349 if (fp_write_code != NULL) {
350 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100351 }
Damiena1ddfcc2013-10-10 23:25:50 +0100352#endif
353#endif
Damien429d7192013-10-04 19:53:11 +0100354}
355
Damiend99b0522013-12-21 18:17:45 +0000356int rt_is_true(mp_obj_t arg) {
357 DEBUG_OP_printf("is true %p\n", arg);
Damien George09a0c642014-01-30 10:05:33 +0000358 if (arg == mp_const_false) {
359 return 0;
360 } else if (arg == mp_const_true) {
361 return 1;
362 } else if (arg == mp_const_none) {
363 return 0;
364 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000365 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
366 return 0;
367 } else {
368 return 1;
369 }
Damiend99b0522013-12-21 18:17:45 +0000370 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200371 mp_obj_type_t *type = mp_obj_get_type(arg);
372 if (type->unary_op != NULL) {
373 mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
Damien George09a0c642014-01-30 10:05:33 +0000374 if (result != MP_OBJ_NULL) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200375 return result == mp_const_true;
376 }
377 }
378
Damien Georgee4b6a072014-01-28 23:27:35 +0000379 mp_obj_t len = mp_obj_len_maybe(arg);
380 if (len != MP_OBJ_NULL) {
381 // obj has a length, truth determined if len != 0
382 return len != MP_OBJ_NEW_SMALL_INT(0);
383 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200384 // any other obj is true per Python semantics
Damien Georgee4b6a072014-01-28 23:27:35 +0000385 return 1;
386 }
Damiend99b0522013-12-21 18:17:45 +0000387 }
388}
389
390mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
391 return mp_obj_list_append(self_in, arg);
392}
393
Damiend99b0522013-12-21 18:17:45 +0000394mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000395 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000396 uint len;
397 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +0000398 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +0000399}
400
Damiend99b0522013-12-21 18:17:45 +0000401mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100402 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000403 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100404}
405
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200406mp_obj_t rt_load_const_bytes(qstr qstr) {
407 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
408 uint len;
409 const byte *data = qstr_data(qstr, &len);
410 return mp_obj_new_bytes(data, len);
411}
412
Damiend99b0522013-12-21 18:17:45 +0000413mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100414 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100415 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000416 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 +0000417 if (elem != NULL) {
418 return elem->value;
419 } else {
420 return rt_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100421 }
Damiena3977762013-10-09 23:10:10 +0100422}
423
Damiend99b0522013-12-21 18:17:45 +0000424mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100425 // logic: search globals, builtins
426 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000427 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100428 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000429 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100430 if (elem == NULL) {
Damien Georgeaea532e2014-02-06 22:57:51 +0000431 for (const mp_builtin_elem_t *e = &builtin_table[0]; e->qstr != MP_QSTR_; e++) {
432 if (e->qstr == qstr) {
433 return e->fun;
434 }
435 }
Damien Georgec5966122014-02-15 16:10:44 +0000436 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 +0100437 }
438 }
439 return elem->value;
440}
441
Damiend99b0522013-12-21 18:17:45 +0000442mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100443 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000444 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 +0000445 if (elem != NULL) {
446 return elem->value;
447 } else {
448 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100449 }
Damien429d7192013-10-04 19:53:11 +0100450}
451
Damiend99b0522013-12-21 18:17:45 +0000452mp_obj_t rt_get_cell(mp_obj_t cell) {
453 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000454}
455
Damiend99b0522013-12-21 18:17:45 +0000456void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
457 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000458}
459
Damiend99b0522013-12-21 18:17:45 +0000460void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100461 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000462 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 +0100463}
464
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200465void rt_delete_name(qstr qstr) {
466 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
467 mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
468}
469
Damiend99b0522013-12-21 18:17:45 +0000470void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100471 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000472 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 +0100473}
474
Damiend99b0522013-12-21 18:17:45 +0000475mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000476 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000477
Damiend99b0522013-12-21 18:17:45 +0000478 if (MP_OBJ_IS_SMALL_INT(arg)) {
479 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000480 switch (op) {
Damien George9d68e9c2014-03-12 15:38:15 +0000481 case RT_UNARY_OP_BOOL:
482 return MP_BOOL(val != 0);
483 case RT_UNARY_OP_POSITIVE:
484 return arg;
485 case RT_UNARY_OP_NEGATIVE:
486 // check for overflow
487 if (val == MP_SMALL_INT_MIN) {
488 return mp_obj_new_int(-val);
489 } else {
490 return MP_OBJ_NEW_SMALL_INT(-val);
491 }
492 case RT_UNARY_OP_INVERT:
493 return MP_OBJ_NEW_SMALL_INT(~val);
494 default:
495 assert(0);
496 return arg;
Damien7410e442013-11-02 19:47:57 +0000497 }
Damien George1e708fe2014-01-23 18:27:51 +0000498 } else {
499 mp_obj_type_t *type = mp_obj_get_type(arg);
500 if (type->unary_op != NULL) {
501 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000502 if (result != NULL) {
503 return result;
504 }
Damien7410e442013-11-02 19:47:57 +0000505 }
Damiend99b0522013-12-21 18:17:45 +0000506 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000507 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 +0000508 }
Damien429d7192013-10-04 19:53:11 +0100509}
510
Damiend99b0522013-12-21 18:17:45 +0000511mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100512 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000513
514 // TODO correctly distinguish inplace operators for mutable objects
515 // lookup logic that CPython uses for +=:
516 // check for implemented +=
517 // then check for implemented +
518 // then check for implemented seq.inplace_concat
519 // then check for implemented seq.concat
520 // then fail
521 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
522
Damien George9aa2a522014-02-01 23:04:09 +0000523 // deal with is
524 if (op == RT_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200525 return MP_BOOL(lhs == rhs);
526 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200527
Damien Georgebcbeea02014-01-11 10:47:22 +0000528 // deal with == and != for all types
Damien George9aa2a522014-02-01 23:04:09 +0000529 if (op == RT_BINARY_OP_EQUAL || op == RT_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000530 if (mp_obj_equal(lhs, rhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000531 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000532 return mp_const_true;
533 } else {
534 return mp_const_false;
535 }
536 } else {
Damien George9aa2a522014-02-01 23:04:09 +0000537 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000538 return mp_const_false;
539 } else {
540 return mp_const_true;
541 }
542 }
543 }
544
545 // deal with exception_match for all types
Damien George9aa2a522014-02-01 23:04:09 +0000546 if (op == RT_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000547 // rhs must be issubclass(rhs, BaseException)
548 if (mp_obj_is_exception_type(rhs)) {
549 // if lhs is an instance of an exception, then extract and use its type
550 if (mp_obj_is_exception_instance(lhs)) {
551 lhs = mp_obj_get_type(lhs);
552 }
Damien George71510152014-03-03 22:38:13 +0000553 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000554 return mp_const_true;
555 } else {
556 return mp_const_false;
557 }
558 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200559 assert(0);
560 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000561 }
562
Damien George1a9951d2014-01-06 22:13:00 +0000563 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000564 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000565 if (MP_OBJ_IS_SMALL_INT(rhs)) {
566 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000567 // This is a binary operation: lhs_val op rhs_val
568 // We need to be careful to handle overflow; see CERT INT32-C
569 // Operations that can overflow:
570 // + result always fits in machine_int_t, then handled by SMALL_INT check
571 // - result always fits in machine_int_t, then handled by SMALL_INT check
572 // * checked explicitly
573 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
574 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
575 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000576 switch (op) {
577 case RT_BINARY_OP_OR:
578 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
579 case RT_BINARY_OP_XOR:
580 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
581 case RT_BINARY_OP_AND:
582 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
583 case RT_BINARY_OP_LSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000584 case RT_BINARY_OP_INPLACE_LSHIFT: {
585 if (rhs_val < 0) {
586 // negative shift not allowed
587 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
588 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
589 // left-shift will overflow, so use higher precision integer
590 lhs = mp_obj_new_int_from_ll(lhs_val);
591 goto generic_binary_op;
592 } else {
593 // use standard precision
594 lhs_val <<= rhs_val;
595 }
596 break;
597 }
Damien George1a9951d2014-01-06 22:13:00 +0000598 case RT_BINARY_OP_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000599 case RT_BINARY_OP_INPLACE_RSHIFT:
600 if (rhs_val < 0) {
601 // negative shift not allowed
602 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
603 } else {
604 // standard precision is enough for right-shift
605 lhs_val >>= rhs_val;
606 }
607 break;
Damien George1a9951d2014-01-06 22:13:00 +0000608 case RT_BINARY_OP_ADD:
609 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
610 case RT_BINARY_OP_SUBTRACT:
611 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
612 case RT_BINARY_OP_MULTIPLY:
Damien George9d68e9c2014-03-12 15:38:15 +0000613 case RT_BINARY_OP_INPLACE_MULTIPLY: {
614
615 // If long long type exists and is larger than machine_int_t, then
616 // we can use the following code to perform overflow-checked multiplication.
617 // Otherwise (eg in x64 case) we must use the branching code below.
618 #if 0
619 // compute result using long long precision
620 long long res = (long long)lhs_val * (long long)rhs_val;
621 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
622 // result overflowed SMALL_INT, so return higher precision integer
623 return mp_obj_new_int_from_ll(res);
624 } else {
625 // use standard precision
626 lhs_val = (mp_small_int_t)res;
627 }
628 #endif
629
630 if (lhs_val > 0) { // lhs_val is positive
631 if (rhs_val > 0) { // lhs_val and rhs_val are positive
632 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
633 goto mul_overflow;
634 }
635 } else { // lhs_val positive, rhs_val nonpositive
636 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
637 goto mul_overflow;
638 }
639 } // lhs_val positive, rhs_val nonpositive
640 } else { // lhs_val is nonpositive
641 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
642 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
643 goto mul_overflow;
644 }
645 } else { // lhs_val and rhs_val are nonpositive
646 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
647 goto mul_overflow;
648 }
649 } // End if lhs_val and rhs_val are nonpositive
650 } // End if lhs_val is nonpositive
651
652 // use standard precision
653 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
654
655 mul_overflow:
656 // use higher precision
657 lhs = mp_obj_new_int_from_ll(lhs_val);
658 goto generic_binary_op;
659
660 break;
661 }
Damien George1a9951d2014-01-06 22:13:00 +0000662 case RT_BINARY_OP_FLOOR_DIVIDE:
Rachel Dowdall56402792014-03-22 20:19:24 +0000663 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE:
664 {
665 lhs_val = python_floor_divide(lhs_val, rhs_val);
666 break;
667 }
Damien George9d68e9c2014-03-12 15:38:15 +0000668 #if MICROPY_ENABLE_FLOAT
Damien George1a9951d2014-01-06 22:13:00 +0000669 case RT_BINARY_OP_TRUE_DIVIDE:
670 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 +0000671 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000672
Damien George1a9951d2014-01-06 22:13:00 +0000673 case RT_BINARY_OP_MODULO:
Rachel Dowdallcde86312014-03-22 17:29:27 +0000674 case RT_BINARY_OP_INPLACE_MODULO:
675 {
676 lhs_val = python_modulo(lhs_val, rhs_val);
677 break;
678 }
Damien George1a9951d2014-01-06 22:13:00 +0000679 case RT_BINARY_OP_POWER:
680 case RT_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000681 if (rhs_val < 0) {
682 #if MICROPY_ENABLE_FLOAT
683 lhs = mp_obj_new_float(lhs_val);
684 goto generic_binary_op;
685 #else
686 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
687 #endif
688 } else {
689 // TODO check for overflow
690 machine_int_t ans = 1;
691 while (rhs_val > 0) {
692 if (rhs_val & 1) {
693 ans *= lhs_val;
694 }
695 lhs_val *= lhs_val;
696 rhs_val /= 2;
Damien George1a9951d2014-01-06 22:13:00 +0000697 }
Damien George9d68e9c2014-03-12 15:38:15 +0000698 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000699 }
Damien George1a9951d2014-01-06 22:13:00 +0000700 break;
Damien George9aa2a522014-02-01 23:04:09 +0000701 case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
702 case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
703 case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
704 case RT_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000705
Damien George1a9951d2014-01-06 22:13:00 +0000706 default: assert(0);
707 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200708 // TODO: We just should make mp_obj_new_int() inline and use that
709 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000710 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000711 } else {
712 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000713 }
Damien George3f759b72014-01-31 00:42:12 +0000714#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000715 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000716 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000717 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000718 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000719#endif
Damien429d7192013-10-04 19:53:11 +0100720 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000721 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000722
Damien George9aa2a522014-02-01 23:04:09 +0000723 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000724 *
725 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000726 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000727 */
Damien George9aa2a522014-02-01 23:04:09 +0000728 if (op == RT_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000729 mp_obj_type_t *type = mp_obj_get_type(rhs);
730 if (type->binary_op != NULL) {
731 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000732 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000733 return res;
734 }
735 }
736 if (type->getiter != NULL) {
737 /* second attempt, walk the iterator */
738 mp_obj_t next = NULL;
739 mp_obj_t iter = rt_getiter(rhs);
740 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
741 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000742 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000743 }
Damien7410e442013-11-02 19:47:57 +0000744 }
Damien George9aa2a522014-02-01 23:04:09 +0000745 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000746 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000747
748 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000749 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000750 mp_obj_get_type_str(rhs)));
751 return mp_const_none;
752 }
753
Damien George5fa93b62014-01-22 14:35:10 +0000754 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000755 mp_obj_type_t *type;
756generic_binary_op:
757 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000758 if (type->binary_op != NULL) {
759 mp_obj_t result = type->binary_op(op, lhs, rhs);
760 if (result != MP_OBJ_NULL) {
761 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000762 }
Damien429d7192013-10-04 19:53:11 +0100763 }
Damiend99b0522013-12-21 18:17:45 +0000764
Damien George5fa93b62014-01-22 14:35:10 +0000765 // TODO implement dispatch for reverse binary ops
766
Damiend99b0522013-12-21 18:17:45 +0000767 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000768 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200769 "unsupported operand types for binary operator: '%s', '%s'",
770 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000771 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100772}
773
Paul Sokolovsky90750022014-02-01 15:05:04 +0200774mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100775 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
776 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100777 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000778 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100779 }
Damiend99b0522013-12-21 18:17:45 +0000780
781 // make the function, depending on the code kind
782 mp_code_t *c = &unique_codes[unique_code_id];
783 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100784 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000785 case MP_CODE_BYTE:
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200786 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 +0100787 break;
Damiend99b0522013-12-21 18:17:45 +0000788 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000789 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100790 break;
Damiend99b0522013-12-21 18:17:45 +0000791 case MP_CODE_INLINE_ASM:
792 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100793 break;
794 default:
795 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000796 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100797 }
Damienbd254452013-10-16 20:39:12 +0100798
799 // check for generator functions and if so wrap in generator object
Damien George8725f8f2014-02-15 19:33:11 +0000800 if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien Georged0691cc2014-01-29 20:30:52 +0000801 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100802 }
803
Damiend99b0522013-12-21 18:17:45 +0000804 return fun;
Damien429d7192013-10-04 19:53:11 +0100805}
806
Damiend99b0522013-12-21 18:17:45 +0000807mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000808 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000809 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200810 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000811 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000812 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000813}
814
Damiend99b0522013-12-21 18:17:45 +0000815mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000816 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100817}
818
Damiend99b0522013-12-21 18:17:45 +0000819mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000820 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100821}
822
Damiend99b0522013-12-21 18:17:45 +0000823mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
824 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000825 args[0] = arg1;
826 args[1] = arg2;
827 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100828}
829
Damien Georgecd82e022014-02-02 13:11:48 +0000830// wrapper that accepts n_args and n_kw in one argument
831// native emitter can only pass at most 3 arguments to a function
832mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
833 return rt_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
834}
835
Damien George20006db2014-01-18 14:10:48 +0000836// args contains, eg: arg0 arg1 key0 value0 key1 value1
837mp_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 +0000838 // TODO improve this: fun object can specify its type and we parse here the arguments,
839 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100840
John R. Lenton9c83ec02014-01-07 23:06:46 +0000841 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
842
Damien George8b56beb2014-01-31 23:49:49 +0000843 // get the type
844 mp_obj_type_t *type = mp_obj_get_type(fun_in);
845
846 // do the call
847 if (type->call != NULL) {
848 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000849 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000850 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 +0000851 }
Damien86c7fc72013-11-26 15:16:41 +0000852}
853
Damien George20006db2014-01-18 14:10:48 +0000854// 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)
855// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000856mp_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 +0000857 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);
858 int adjust = (args[1] == NULL) ? 0 : 1;
859 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000860}
861
Damiend99b0522013-12-21 18:17:45 +0000862mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000863 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100864}
865
Damiend99b0522013-12-21 18:17:45 +0000866mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000867 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100868}
869
Damiend99b0522013-12-21 18:17:45 +0000870mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
871 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100872}
873
Damiend99b0522013-12-21 18:17:45 +0000874mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000875 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100876 return set;
877}
878
Damien George932bf1c2014-01-18 23:42:49 +0000879// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000880void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200881 uint seq_len;
Damiend99b0522013-12-21 18:17:45 +0000882 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
Damiend99b0522013-12-21 18:17:45 +0000883 mp_obj_t *seq_items;
884 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
885 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
886 } else {
887 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000888 }
Damiend99b0522013-12-21 18:17:45 +0000889 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200890 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000891 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200892 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000893 }
Damien George932bf1c2014-01-18 23:42:49 +0000894 for (uint i = 0; i < num; i++) {
895 items[i] = seq_items[num - 1 - i];
896 }
Damien86c7fc72013-11-26 15:16:41 +0000897 } else {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200898 mp_obj_t iterable = rt_getiter(seq_in);
899
900 for (seq_len = 0; seq_len < num; seq_len++) {
901 mp_obj_t el = rt_iternext(iterable);
902 if (el == mp_const_stop_iteration) {
903 goto too_short;
904 }
905 items[num - 1 - seq_len] = el;
906 }
907 if (rt_iternext(iterable) != mp_const_stop_iteration) {
908 goto too_long;
909 }
Damien86c7fc72013-11-26 15:16:41 +0000910 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200911 return;
912
913too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000914 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 +0200915too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000916 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 +0000917}
918
Damiend99b0522013-12-21 18:17:45 +0000919mp_obj_t rt_build_map(int n_args) {
920 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100921}
922
Damiend99b0522013-12-21 18:17:45 +0000923mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
924 // map should always be a dict
925 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100926}
927
Damiend99b0522013-12-21 18:17:45 +0000928mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000929 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
930 // use load_method
931 mp_obj_t dest[2];
932 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000933 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000934 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000935 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000936 } else {
937 // load_method returned a method, so build a bound method object
938 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000939 }
Damiend99b0522013-12-21 18:17:45 +0000940}
941
Damien George7c9c6672014-01-25 00:17:36 +0000942// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
943// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
944// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200945STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000946 // clear output to indicate no attribute/method found yet
947 dest[0] = MP_OBJ_NULL;
948 dest[1] = MP_OBJ_NULL;
949
950 // get the type
951 mp_obj_type_t *type = mp_obj_get_type(base);
952
953 // if this type can do its own load, then call it
954 if (type->load_attr != NULL) {
955 type->load_attr(base, attr, dest);
956 }
957
958 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000959 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000960 if (attr == MP_QSTR___class__) {
961 // a.__class__ is equivalent to type(a)
962 dest[0] = type;
963 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000964 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
965 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000966 } else if (type->load_attr == NULL) {
967 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000968 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000969 const mp_method_t *meth = type->methods;
970 if (meth != NULL) {
971 for (; meth->name != NULL; meth++) {
972 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000973 // check if the methods are functions, static or class methods
974 // see http://docs.python.org/3.3/howto/descriptor.html
975 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
976 // return just the function
Damien George64131f32014-02-06 20:31:44 +0000977 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000978 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
979 // return a bound method, with self being the type of this object
Damien George64131f32014-02-06 20:31:44 +0000980 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien George20006db2014-01-18 14:10:48 +0000981 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000982 } else {
983 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000984 dest[0] = (mp_obj_t)meth->fun;
985 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000986 }
Damien George062478e2014-01-09 20:57:50 +0000987 break;
988 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000989 }
Damiend57eba52013-11-02 23:58:14 +0000990 }
991 }
Damiena3977762013-10-09 23:10:10 +0100992 }
Damien George7c9c6672014-01-25 00:17:36 +0000993}
Damiena3977762013-10-09 23:10:10 +0100994
Damien George7c9c6672014-01-25 00:17:36 +0000995void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
996 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
997
998 rt_load_method_maybe(base, attr, dest);
999
1000 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +00001001 // no attribute/method called attr
1002 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +00001003 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +02001004 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
1005 "type object '%s' has no attribute '%s'", qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +00001006 } else {
Damien Georgec5966122014-02-15 16:10:44 +00001007 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 +00001008 }
1009 }
Damiena3977762013-10-09 23:10:10 +01001010}
1011
Damiend99b0522013-12-21 18:17:45 +00001012void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001013 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +00001014 mp_obj_type_t *type = mp_obj_get_type(base);
1015 if (type->store_attr != NULL) {
1016 if (type->store_attr(base, attr, value)) {
1017 return;
1018 }
Damiena3977762013-10-09 23:10:10 +01001019 }
Damien Georgec5966122014-02-15 16:10:44 +00001020 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 +01001021}
1022
Damiend99b0522013-12-21 18:17:45 +00001023void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001024 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +00001025 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +01001026 // list store
Damiend99b0522013-12-21 18:17:45 +00001027 mp_obj_list_store(base, index, value);
1028 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
1029 // dict store
1030 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +01001031 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +02001032 mp_obj_type_t *type = mp_obj_get_type(base);
1033 if (type->store_item != NULL) {
1034 bool r = type->store_item(base, index, value);
1035 if (r) {
1036 return;
1037 }
1038 // TODO: call base classes here?
1039 }
Damien Georgec5966122014-02-15 16:10:44 +00001040 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 +01001041 }
1042}
1043
Damiend99b0522013-12-21 18:17:45 +00001044mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001045 mp_obj_type_t *type = mp_obj_get_type(o_in);
1046 if (type->getiter != NULL) {
1047 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +01001048 } else {
Damien George7c9c6672014-01-25 00:17:36 +00001049 // check for __getitem__ method
1050 mp_obj_t dest[2];
Damien George7d0bfbe2014-02-08 19:01:47 +00001051 rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George7c9c6672014-01-25 00:17:36 +00001052 if (dest[0] != MP_OBJ_NULL) {
1053 // __getitem__ exists, create an iterator
1054 return mp_obj_new_getitem_iter(dest);
1055 } else {
1056 // object not iterable
Damien George0ec6bd42014-03-09 16:29:36 +00001057 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 +00001058 }
Damience89a212013-10-15 22:25:17 +01001059 }
1060}
1061
Damiend99b0522013-12-21 18:17:45 +00001062mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001063 mp_obj_type_t *type = mp_obj_get_type(o_in);
1064 if (type->iternext != NULL) {
1065 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +01001066 } else {
Damien George0ec6bd42014-03-09 16:29:36 +00001067 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 +00001068 }
1069}
1070
1071mp_obj_t rt_make_raise_obj(mp_obj_t o) {
1072 DEBUG_printf("raise %p\n", o);
1073 if (mp_obj_is_exception_type(o)) {
1074 // o is an exception type (it is derived from BaseException (or is BaseException))
1075 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001076 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1077 // could have const instances in ROM which we return here instead
Damien Georgec5966122014-02-15 16:10:44 +00001078 return rt_call_function_n_kw(o, 0, 0, NULL);
1079 } else if (mp_obj_is_exception_instance(o)) {
1080 // o is an instance of an exception, so use it as the exception
1081 return o;
1082 } else {
1083 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1084 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001085 }
1086}
1087
Damiend99b0522013-12-21 18:17:45 +00001088mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001089 DEBUG_printf("import name %s\n", qstr_str(name));
1090
Damiendb4c3612013-12-10 17:27:24 +00001091 // build args array
Damiend99b0522013-12-21 18:17:45 +00001092 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001093 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001094 args[1] = mp_const_none; // TODO should be globals
1095 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001096 args[3] = fromlist;
1097 args[4] = level; // must be 0; we don't yet support other values
1098
1099 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001100 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001101}
1102
Damiend99b0522013-12-21 18:17:45 +00001103mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001104 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1105
Damiend99b0522013-12-21 18:17:45 +00001106 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +00001107 /* TODO convert AttributeError to ImportError
1108 if (fail) {
1109 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1110 }
1111 */
1112 return x;
1113}
1114
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001115void rt_import_all(mp_obj_t module) {
1116 DEBUG_printf("import all %p\n", module);
1117
1118 mp_map_t *map = mp_obj_module_get_globals(module);
1119 for (uint i = 0; i < map->alloc; i++) {
1120 if (map->table[i].key != MP_OBJ_NULL) {
1121 rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
1122 }
1123 }
1124}
1125
Damien George66028ab2014-01-03 14:03:48 +00001126mp_map_t *rt_locals_get(void) {
1127 return map_locals;
1128}
1129
1130void rt_locals_set(mp_map_t *m) {
1131 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
1132 map_locals = m;
1133}
1134
1135mp_map_t *rt_globals_get(void) {
1136 return map_globals;
1137}
1138
1139void rt_globals_set(mp_map_t *m) {
1140 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
1141 map_globals = m;
1142}
1143
Damien George0d028742014-01-22 23:59:20 +00001144mp_map_t *rt_loaded_modules_get(void) {
1145 return &map_loaded_modules;
1146}
1147
Damien6ba13142013-11-02 20:34:54 +00001148// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +01001149void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +00001150 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +01001151 rt_load_const_str,
1152 rt_load_name,
1153 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +01001154 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001155 rt_load_attr,
1156 rt_load_method,
1157 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001158 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001159 rt_store_subscr,
1160 rt_is_true,
1161 rt_unary_op,
Damien Georgecd82e022014-02-02 13:11:48 +00001162 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001163 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001164 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001165 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001166 rt_build_map,
1167 rt_store_map,
1168 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001169 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001170 rt_make_function_from_id,
Damien Georgecd82e022014-02-02 13:11:48 +00001171 rt_call_function_n_kw_for_native,
Damien George20006db2014-01-18 14:10:48 +00001172 rt_call_method_n_kw,
Damiend2755ec2013-10-16 23:58:48 +01001173 rt_getiter,
1174 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001175};
1176
1177/*
1178void rt_f_vector(rt_fun_kind_t fun_kind) {
1179 (rt_f_table[fun_kind])();
1180}
1181*/