blob: 94f319056654755f27050b347cca1e74e9d02d31 [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 },
107 { MP_QSTR_set, (mp_obj_t)&set_type },
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200108 { MP_QSTR_str, (mp_obj_t)&str_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000109 { MP_QSTR_super, (mp_obj_t)&super_type },
110 { MP_QSTR_tuple, (mp_obj_t)&tuple_type },
Damien Georgec5966122014-02-15 16:10:44 +0000111 { MP_QSTR_type, (mp_obj_t)&mp_type_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000112 { MP_QSTR_zip, (mp_obj_t)&zip_type },
113
114 { MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
115 { MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
116
117 // built-in user functions
118 { MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
119 { MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
120 { MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
Damien Georgeaea532e2014-02-06 22:57:51 +0000121 { MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
122 { MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
123 { MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
124 { MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
125 { MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
126 { MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
127 { MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
128 { MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
129 { MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
130 { MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
131 { MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
132 { MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
133 { MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
134 { MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
135 { MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
136 { MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
137 { MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
138 { MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
139 { MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
140 { MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
141 { MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
142 { MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
Damien Georgeaea532e2014-02-06 22:57:51 +0000143 { MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
144
Damien Georgec5966122014-02-15 16:10:44 +0000145 // built-in exceptions
146 { MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000147 { MP_QSTR_ArithmeticError, (mp_obj_t)&mp_type_ArithmeticError },
Damien Georgec5966122014-02-15 16:10:44 +0000148 { MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
149 { MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000150 { MP_QSTR_BufferError, (mp_obj_t)&mp_type_BufferError },
151 { MP_QSTR_BytesWarning, (mp_obj_t)&mp_type_BytesWarning },
152 { MP_QSTR_DeprecationWarning, (mp_obj_t)&mp_type_DeprecationWarning },
153 { MP_QSTR_EOFError, (mp_obj_t)&mp_type_EOFError },
154 { MP_QSTR_EnvironmentError, (mp_obj_t)&mp_type_EnvironmentError },
155 { MP_QSTR_Exception, (mp_obj_t)&mp_type_Exception },
156 { MP_QSTR_FloatingPointError, (mp_obj_t)&mp_type_FloatingPointError },
157 { MP_QSTR_FutureWarning, (mp_obj_t)&mp_type_FutureWarning },
158 { MP_QSTR_GeneratorExit, (mp_obj_t)&mp_type_GeneratorExit },
159 { MP_QSTR_IOError, (mp_obj_t)&mp_type_IOError },
Damien Georgec5966122014-02-15 16:10:44 +0000160 { MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000161 { MP_QSTR_ImportWarning, (mp_obj_t)&mp_type_ImportWarning },
Damien Georgec5966122014-02-15 16:10:44 +0000162 { MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
163 { MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
164 { MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000165 { MP_QSTR_LookupError, (mp_obj_t)&mp_type_LookupError },
166 { MP_QSTR_MemoryError, (mp_obj_t)&mp_type_MemoryError },
Damien Georgec5966122014-02-15 16:10:44 +0000167 { MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000168 { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
169 { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
170 { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
171 { MP_QSTR_PendingDeprecationWarning, (mp_obj_t)&mp_type_PendingDeprecationWarning },
172 { MP_QSTR_ReferenceError, (mp_obj_t)&mp_type_ReferenceError },
173 { MP_QSTR_ResourceWarning, (mp_obj_t)&mp_type_ResourceWarning },
174 { MP_QSTR_RuntimeError, (mp_obj_t)&mp_type_RuntimeError },
175 { MP_QSTR_RuntimeWarning, (mp_obj_t)&mp_type_RuntimeWarning },
Damien Georgec5966122014-02-15 16:10:44 +0000176 { MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000177 { MP_QSTR_SyntaxWarning, (mp_obj_t)&mp_type_SyntaxWarning },
178 { MP_QSTR_SystemError, (mp_obj_t)&mp_type_SystemError },
179 { MP_QSTR_SystemExit, (mp_obj_t)&mp_type_SystemExit },
180 { MP_QSTR_TabError, (mp_obj_t)&mp_type_TabError },
Damien Georgec5966122014-02-15 16:10:44 +0000181 { MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000182 { MP_QSTR_UnboundLocalError, (mp_obj_t)&mp_type_UnboundLocalError },
183 { MP_QSTR_UserWarning, (mp_obj_t)&mp_type_UserWarning },
Damien Georgec5966122014-02-15 16:10:44 +0000184 { MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000185 { MP_QSTR_Warning, (mp_obj_t)&mp_type_Warning },
186 { MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
187 { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
Damien Georgec5966122014-02-15 16:10:44 +0000188 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
189 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
Damien Georgec5966122014-02-15 16:10:44 +0000190
Paul Sokolovsky910843e2014-02-14 12:02:34 +0200191 // Extra builtins as defined by a port
192 MICROPY_EXTRA_BUILTINS
193
Damien Georgeaea532e2014-02-06 22:57:51 +0000194 { MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
195};
196
Damien George38a2da62014-01-08 17:33:12 +0000197// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200198STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +0000199 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
200}
201
Damien8b3a7c22013-10-23 20:20:17 +0100202void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +0100203 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +0000204 map_locals = map_globals = mp_map_new(1);
Damien George5fa93b62014-01-22 14:35:10 +0000205 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +0100206
Damienb86e3f92013-12-29 17:17:43 +0000207 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +0000208 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +0000209
Damien George0d028742014-01-22 23:59:20 +0000210 // init loaded modules table
211 mp_map_init(&map_loaded_modules, 3);
212
Damien Georgee9906ac2014-01-04 18:44:46 +0000213 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000214 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000215
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200216 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
217 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200218
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200219 mp_obj_t m_collections = mp_obj_new_module(MP_QSTR_collections);
220 rt_store_attr(m_collections, MP_QSTR_namedtuple, (mp_obj_t)&mp_namedtuple_obj);
221
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200222#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +0200223 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200224 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
225 // Avoid warning of unused var
226 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200227#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200228 // init sys.path
229 // for efficiency, left to platform-specific startup code
230 //sys_path = mp_obj_new_list(0, NULL);
231 //rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200232
Damien George0c36da02014-03-08 15:24:39 +0000233 // we pre-import the micropython module
234 // probably shouldn't do this, so we are compatible with CPython
235 rt_store_name(MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +0200236
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200237 // TODO: wastes one mp_code_t structure in mem
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000238 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000239 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100240 unique_codes = NULL;
241
Damien0446a0d2013-11-17 13:16:36 +0000242#ifdef WRITE_CODE
243 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100244#endif
Damien429d7192013-10-04 19:53:11 +0100245}
246
Damien8b3a7c22013-10-23 20:20:17 +0100247void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000248 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200249 mp_map_free(map_globals);
250 mp_map_deinit(&map_loaded_modules);
251 mp_map_deinit(&map_builtins);
Damien0446a0d2013-11-17 13:16:36 +0000252#ifdef WRITE_CODE
253 if (fp_write_code != NULL) {
254 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100255 }
Damiena1ddfcc2013-10-10 23:25:50 +0100256#endif
Damien429d7192013-10-04 19:53:11 +0100257}
258
Damien Georged0691cc2014-01-29 20:30:52 +0000259uint rt_get_unique_code_id(void) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000260 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100261}
262
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200263STATIC void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000264 if (next_unique_code_id > unique_codes_alloc) {
Damien Georged0691cc2014-01-29 20:30:52 +0000265 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 +0000266 // increase size of unique_codes table
267 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
Damien Georged0691cc2014-01-29 20:30:52 +0000268 for (uint i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000269 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100270 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000271 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100272 }
Damien826005c2013-10-05 23:17:28 +0100273}
274
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200275void 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 +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_BYTE;
Damien George8725f8f2014-02-15 19:33:11 +0000280 unique_codes[unique_code_id].scope_flags = scope_flags;
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 = n_locals + n_stack;
Damien826005c2013-10-05 23:17:28 +0100283 unique_codes[unique_code_id].u_byte.code = code;
284 unique_codes[unique_code_id].u_byte.len = len;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200285 unique_codes[unique_code_id].arg_names = arg_names;
Damien826005c2013-10-05 23:17:28 +0100286
Damien9ecbcff2013-12-11 00:41:43 +0000287 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000288
289#ifdef DEBUG_PRINT
Damien George08d07552014-01-29 18:58:52 +0000290 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 +0000291 for (int i = 0; i < 128 && i < len; i++) {
292 if (i > 0 && i % 16 == 0) {
293 DEBUG_printf("\n");
294 }
295 DEBUG_printf(" %02x", code[i]);
296 }
297 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000298#if MICROPY_DEBUG_PRINTERS
299 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000300#endif
Damien0446a0d2013-11-17 13:16:36 +0000301#endif
Damien826005c2013-10-05 23:17:28 +0100302}
303
Damien Georged0691cc2014-01-29 20:30:52 +0000304void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100305 alloc_unique_codes();
306
John R. Lenton9c83ec02014-01-07 23:06:46 +0000307 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 +0000308 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien George8725f8f2014-02-15 19:33:11 +0000309 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000310 unique_codes[unique_code_id].n_args = n_args;
311 unique_codes[unique_code_id].n_state = 0;
Damien429d7192013-10-04 19:53:11 +0100312 unique_codes[unique_code_id].u_native.fun = fun;
313
Damien George8cc96a32013-12-30 18:23:50 +0000314 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000315
Damiena1ddfcc2013-10-10 23:25:50 +0100316#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100317 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
318 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
319 for (int i = 0; i < 128 && i < len; i++) {
320 if (i > 0 && i % 16 == 0) {
321 DEBUG_printf("\n");
322 }
323 DEBUG_printf(" %02x", fun_data[i]);
324 }
325 DEBUG_printf("\n");
326
Damien0446a0d2013-11-17 13:16:36 +0000327#ifdef WRITE_CODE
328 if (fp_write_code != NULL) {
329 fwrite(fun_data, len, 1, fp_write_code);
330 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100331 }
Damiena1ddfcc2013-10-10 23:25:50 +0100332#endif
333#endif
Damien429d7192013-10-04 19:53:11 +0100334}
335
Damien Georged0691cc2014-01-29 20:30:52 +0000336void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100337 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100338
John R. Lenton9c83ec02014-01-07 23:06:46 +0000339 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 +0000340 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien George8725f8f2014-02-15 19:33:11 +0000341 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000342 unique_codes[unique_code_id].n_args = n_args;
343 unique_codes[unique_code_id].n_state = 0;
Damien826005c2013-10-05 23:17:28 +0100344 unique_codes[unique_code_id].u_inline_asm.fun = fun;
345
Damiena1ddfcc2013-10-10 23:25:50 +0100346#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100347 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
348 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
349 for (int i = 0; i < 128 && i < len; i++) {
350 if (i > 0 && i % 16 == 0) {
351 DEBUG_printf("\n");
352 }
353 DEBUG_printf(" %02x", fun_data[i]);
354 }
355 DEBUG_printf("\n");
356
Damien0446a0d2013-11-17 13:16:36 +0000357#ifdef WRITE_CODE
358 if (fp_write_code != NULL) {
359 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100360 }
Damiena1ddfcc2013-10-10 23:25:50 +0100361#endif
362#endif
Damien429d7192013-10-04 19:53:11 +0100363}
364
Damiend99b0522013-12-21 18:17:45 +0000365int rt_is_true(mp_obj_t arg) {
366 DEBUG_OP_printf("is true %p\n", arg);
Damien George09a0c642014-01-30 10:05:33 +0000367 if (arg == mp_const_false) {
368 return 0;
369 } else if (arg == mp_const_true) {
370 return 1;
371 } else if (arg == mp_const_none) {
372 return 0;
373 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000374 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
375 return 0;
376 } else {
377 return 1;
378 }
Damiend99b0522013-12-21 18:17:45 +0000379 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200380 mp_obj_type_t *type = mp_obj_get_type(arg);
381 if (type->unary_op != NULL) {
382 mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
Damien George09a0c642014-01-30 10:05:33 +0000383 if (result != MP_OBJ_NULL) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200384 return result == mp_const_true;
385 }
386 }
387
Damien Georgee4b6a072014-01-28 23:27:35 +0000388 mp_obj_t len = mp_obj_len_maybe(arg);
389 if (len != MP_OBJ_NULL) {
390 // obj has a length, truth determined if len != 0
391 return len != MP_OBJ_NEW_SMALL_INT(0);
392 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200393 // any other obj is true per Python semantics
Damien Georgee4b6a072014-01-28 23:27:35 +0000394 return 1;
395 }
Damiend99b0522013-12-21 18:17:45 +0000396 }
397}
398
399mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
400 return mp_obj_list_append(self_in, arg);
401}
402
Damiend99b0522013-12-21 18:17:45 +0000403mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000404 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000405 uint len;
406 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +0000407 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +0000408}
409
Damiend99b0522013-12-21 18:17:45 +0000410mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100411 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000412 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100413}
414
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200415mp_obj_t rt_load_const_bytes(qstr qstr) {
416 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
417 uint len;
418 const byte *data = qstr_data(qstr, &len);
419 return mp_obj_new_bytes(data, len);
420}
421
Damiend99b0522013-12-21 18:17:45 +0000422mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100423 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100424 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000425 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 +0000426 if (elem != NULL) {
427 return elem->value;
428 } else {
429 return rt_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100430 }
Damiena3977762013-10-09 23:10:10 +0100431}
432
Damiend99b0522013-12-21 18:17:45 +0000433mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100434 // logic: search globals, builtins
435 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000436 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100437 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000438 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100439 if (elem == NULL) {
Damien Georgeaea532e2014-02-06 22:57:51 +0000440 for (const mp_builtin_elem_t *e = &builtin_table[0]; e->qstr != MP_QSTR_; e++) {
441 if (e->qstr == qstr) {
442 return e->fun;
443 }
444 }
Damien Georgec5966122014-02-15 16:10:44 +0000445 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 +0100446 }
447 }
448 return elem->value;
449}
450
Damiend99b0522013-12-21 18:17:45 +0000451mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100452 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000453 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 +0000454 if (elem != NULL) {
455 return elem->value;
456 } else {
457 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100458 }
Damien429d7192013-10-04 19:53:11 +0100459}
460
Damiend99b0522013-12-21 18:17:45 +0000461mp_obj_t rt_get_cell(mp_obj_t cell) {
462 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000463}
464
Damiend99b0522013-12-21 18:17:45 +0000465void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
466 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000467}
468
Damiend99b0522013-12-21 18:17:45 +0000469void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100470 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000471 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 +0100472}
473
Damiend99b0522013-12-21 18:17:45 +0000474void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100475 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000476 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 +0100477}
478
Damiend99b0522013-12-21 18:17:45 +0000479mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000480 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000481
Damiend99b0522013-12-21 18:17:45 +0000482 if (MP_OBJ_IS_SMALL_INT(arg)) {
483 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000484 switch (op) {
Damien George9d68e9c2014-03-12 15:38:15 +0000485 case RT_UNARY_OP_BOOL:
486 return MP_BOOL(val != 0);
487 case RT_UNARY_OP_POSITIVE:
488 return arg;
489 case RT_UNARY_OP_NEGATIVE:
490 // check for overflow
491 if (val == MP_SMALL_INT_MIN) {
492 return mp_obj_new_int(-val);
493 } else {
494 return MP_OBJ_NEW_SMALL_INT(-val);
495 }
496 case RT_UNARY_OP_INVERT:
497 return MP_OBJ_NEW_SMALL_INT(~val);
498 default:
499 assert(0);
500 return arg;
Damien7410e442013-11-02 19:47:57 +0000501 }
Damien George1e708fe2014-01-23 18:27:51 +0000502 } else {
503 mp_obj_type_t *type = mp_obj_get_type(arg);
504 if (type->unary_op != NULL) {
505 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000506 if (result != NULL) {
507 return result;
508 }
Damien7410e442013-11-02 19:47:57 +0000509 }
Damiend99b0522013-12-21 18:17:45 +0000510 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000511 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 +0000512 }
Damien429d7192013-10-04 19:53:11 +0100513}
514
Damiend99b0522013-12-21 18:17:45 +0000515mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100516 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000517
518 // TODO correctly distinguish inplace operators for mutable objects
519 // lookup logic that CPython uses for +=:
520 // check for implemented +=
521 // then check for implemented +
522 // then check for implemented seq.inplace_concat
523 // then check for implemented seq.concat
524 // then fail
525 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
526
Damien George9aa2a522014-02-01 23:04:09 +0000527 // deal with is
528 if (op == RT_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200529 return MP_BOOL(lhs == rhs);
530 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200531
Damien Georgebcbeea02014-01-11 10:47:22 +0000532 // deal with == and != for all types
Damien George9aa2a522014-02-01 23:04:09 +0000533 if (op == RT_BINARY_OP_EQUAL || op == RT_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000534 if (mp_obj_equal(lhs, rhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000535 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000536 return mp_const_true;
537 } else {
538 return mp_const_false;
539 }
540 } else {
Damien George9aa2a522014-02-01 23:04:09 +0000541 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000542 return mp_const_false;
543 } else {
544 return mp_const_true;
545 }
546 }
547 }
548
549 // deal with exception_match for all types
Damien George9aa2a522014-02-01 23:04:09 +0000550 if (op == RT_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000551 // rhs must be issubclass(rhs, BaseException)
552 if (mp_obj_is_exception_type(rhs)) {
553 // if lhs is an instance of an exception, then extract and use its type
554 if (mp_obj_is_exception_instance(lhs)) {
555 lhs = mp_obj_get_type(lhs);
556 }
Damien George71510152014-03-03 22:38:13 +0000557 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000558 return mp_const_true;
559 } else {
560 return mp_const_false;
561 }
562 }
563 }
564
Damien George1a9951d2014-01-06 22:13:00 +0000565 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000566 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000567 if (MP_OBJ_IS_SMALL_INT(rhs)) {
568 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000569 // This is a binary operation: lhs_val op rhs_val
570 // We need to be careful to handle overflow; see CERT INT32-C
571 // Operations that can overflow:
572 // + result always fits in machine_int_t, then handled by SMALL_INT check
573 // - result always fits in machine_int_t, then handled by SMALL_INT check
574 // * checked explicitly
575 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
576 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
577 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000578 switch (op) {
579 case RT_BINARY_OP_OR:
580 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
581 case RT_BINARY_OP_XOR:
582 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
583 case RT_BINARY_OP_AND:
584 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
585 case RT_BINARY_OP_LSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000586 case RT_BINARY_OP_INPLACE_LSHIFT: {
587 if (rhs_val < 0) {
588 // negative shift not allowed
589 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
590 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
591 // left-shift will overflow, so use higher precision integer
592 lhs = mp_obj_new_int_from_ll(lhs_val);
593 goto generic_binary_op;
594 } else {
595 // use standard precision
596 lhs_val <<= rhs_val;
597 }
598 break;
599 }
Damien George1a9951d2014-01-06 22:13:00 +0000600 case RT_BINARY_OP_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000601 case RT_BINARY_OP_INPLACE_RSHIFT:
602 if (rhs_val < 0) {
603 // negative shift not allowed
604 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
605 } else {
606 // standard precision is enough for right-shift
607 lhs_val >>= rhs_val;
608 }
609 break;
Damien George1a9951d2014-01-06 22:13:00 +0000610 case RT_BINARY_OP_ADD:
611 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
612 case RT_BINARY_OP_SUBTRACT:
613 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
614 case RT_BINARY_OP_MULTIPLY:
Damien George9d68e9c2014-03-12 15:38:15 +0000615 case RT_BINARY_OP_INPLACE_MULTIPLY: {
616
617 // If long long type exists and is larger than machine_int_t, then
618 // we can use the following code to perform overflow-checked multiplication.
619 // Otherwise (eg in x64 case) we must use the branching code below.
620 #if 0
621 // compute result using long long precision
622 long long res = (long long)lhs_val * (long long)rhs_val;
623 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
624 // result overflowed SMALL_INT, so return higher precision integer
625 return mp_obj_new_int_from_ll(res);
626 } else {
627 // use standard precision
628 lhs_val = (mp_small_int_t)res;
629 }
630 #endif
631
632 if (lhs_val > 0) { // lhs_val is positive
633 if (rhs_val > 0) { // lhs_val and rhs_val are positive
634 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
635 goto mul_overflow;
636 }
637 } else { // lhs_val positive, rhs_val nonpositive
638 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
639 goto mul_overflow;
640 }
641 } // lhs_val positive, rhs_val nonpositive
642 } else { // lhs_val is nonpositive
643 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
644 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
645 goto mul_overflow;
646 }
647 } else { // lhs_val and rhs_val are nonpositive
648 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
649 goto mul_overflow;
650 }
651 } // End if lhs_val and rhs_val are nonpositive
652 } // End if lhs_val is nonpositive
653
654 // use standard precision
655 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
656
657 mul_overflow:
658 // use higher precision
659 lhs = mp_obj_new_int_from_ll(lhs_val);
660 goto generic_binary_op;
661
662 break;
663 }
Damien George1a9951d2014-01-06 22:13:00 +0000664 case RT_BINARY_OP_FLOOR_DIVIDE:
Rachel Dowdall56402792014-03-22 20:19:24 +0000665 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE:
666 {
667 lhs_val = python_floor_divide(lhs_val, rhs_val);
668 break;
669 }
Damien George9d68e9c2014-03-12 15:38:15 +0000670 #if MICROPY_ENABLE_FLOAT
Damien George1a9951d2014-01-06 22:13:00 +0000671 case RT_BINARY_OP_TRUE_DIVIDE:
672 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 +0000673 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000674
Damien George1a9951d2014-01-06 22:13:00 +0000675 case RT_BINARY_OP_MODULO:
Rachel Dowdallcde86312014-03-22 17:29:27 +0000676 case RT_BINARY_OP_INPLACE_MODULO:
677 {
678 lhs_val = python_modulo(lhs_val, rhs_val);
679 break;
680 }
Damien George1a9951d2014-01-06 22:13:00 +0000681 case RT_BINARY_OP_POWER:
682 case RT_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000683 if (rhs_val < 0) {
684 #if MICROPY_ENABLE_FLOAT
685 lhs = mp_obj_new_float(lhs_val);
686 goto generic_binary_op;
687 #else
688 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
689 #endif
690 } else {
691 // TODO check for overflow
692 machine_int_t ans = 1;
693 while (rhs_val > 0) {
694 if (rhs_val & 1) {
695 ans *= lhs_val;
696 }
697 lhs_val *= lhs_val;
698 rhs_val /= 2;
Damien George1a9951d2014-01-06 22:13:00 +0000699 }
Damien George9d68e9c2014-03-12 15:38:15 +0000700 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000701 }
Damien George1a9951d2014-01-06 22:13:00 +0000702 break;
Damien George9aa2a522014-02-01 23:04:09 +0000703 case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
704 case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
705 case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
706 case RT_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000707
Damien George1a9951d2014-01-06 22:13:00 +0000708 default: assert(0);
709 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200710 // TODO: We just should make mp_obj_new_int() inline and use that
711 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000712 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000713 } else {
714 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000715 }
Damien George3f759b72014-01-31 00:42:12 +0000716#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000717 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000718 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000719 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000720 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000721#endif
Damien429d7192013-10-04 19:53:11 +0100722 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000723 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000724
Damien George9aa2a522014-02-01 23:04:09 +0000725 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000726 *
727 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000728 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000729 */
Damien George9aa2a522014-02-01 23:04:09 +0000730 if (op == RT_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000731 mp_obj_type_t *type = mp_obj_get_type(rhs);
732 if (type->binary_op != NULL) {
733 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000734 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000735 return res;
736 }
737 }
738 if (type->getiter != NULL) {
739 /* second attempt, walk the iterator */
740 mp_obj_t next = NULL;
741 mp_obj_t iter = rt_getiter(rhs);
742 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
743 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000744 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000745 }
Damien7410e442013-11-02 19:47:57 +0000746 }
Damien George9aa2a522014-02-01 23:04:09 +0000747 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000748 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000749
750 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000751 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000752 mp_obj_get_type_str(rhs)));
753 return mp_const_none;
754 }
755
Damien George5fa93b62014-01-22 14:35:10 +0000756 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000757 mp_obj_type_t *type;
758generic_binary_op:
759 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000760 if (type->binary_op != NULL) {
761 mp_obj_t result = type->binary_op(op, lhs, rhs);
762 if (result != MP_OBJ_NULL) {
763 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000764 }
Damien429d7192013-10-04 19:53:11 +0100765 }
Damiend99b0522013-12-21 18:17:45 +0000766
Damien George5fa93b62014-01-22 14:35:10 +0000767 // TODO implement dispatch for reverse binary ops
768
Damiend99b0522013-12-21 18:17:45 +0000769 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000770 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200771 "unsupported operand types for binary operator: '%s', '%s'",
772 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000773 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100774}
775
Paul Sokolovsky90750022014-02-01 15:05:04 +0200776mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100777 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
778 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100779 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000780 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100781 }
Damiend99b0522013-12-21 18:17:45 +0000782
783 // make the function, depending on the code kind
784 mp_code_t *c = &unique_codes[unique_code_id];
785 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100786 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000787 case MP_CODE_BYTE:
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200788 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 +0100789 break;
Damiend99b0522013-12-21 18:17:45 +0000790 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000791 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100792 break;
Damiend99b0522013-12-21 18:17:45 +0000793 case MP_CODE_INLINE_ASM:
794 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100795 break;
796 default:
797 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000798 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100799 }
Damienbd254452013-10-16 20:39:12 +0100800
801 // check for generator functions and if so wrap in generator object
Damien George8725f8f2014-02-15 19:33:11 +0000802 if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien Georged0691cc2014-01-29 20:30:52 +0000803 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100804 }
805
Damiend99b0522013-12-21 18:17:45 +0000806 return fun;
Damien429d7192013-10-04 19:53:11 +0100807}
808
Damiend99b0522013-12-21 18:17:45 +0000809mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000810 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000811 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200812 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000813 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000814 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000815}
816
Damiend99b0522013-12-21 18:17:45 +0000817mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000818 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100819}
820
Damiend99b0522013-12-21 18:17:45 +0000821mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000822 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100823}
824
Damiend99b0522013-12-21 18:17:45 +0000825mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
826 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000827 args[0] = arg1;
828 args[1] = arg2;
829 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100830}
831
Damien Georgecd82e022014-02-02 13:11:48 +0000832// wrapper that accepts n_args and n_kw in one argument
833// native emitter can only pass at most 3 arguments to a function
834mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
835 return rt_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
836}
837
Damien George20006db2014-01-18 14:10:48 +0000838// args contains, eg: arg0 arg1 key0 value0 key1 value1
839mp_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 +0000840 // TODO improve this: fun object can specify its type and we parse here the arguments,
841 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100842
John R. Lenton9c83ec02014-01-07 23:06:46 +0000843 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
844
Damien George8b56beb2014-01-31 23:49:49 +0000845 // get the type
846 mp_obj_type_t *type = mp_obj_get_type(fun_in);
847
848 // do the call
849 if (type->call != NULL) {
850 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000851 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000852 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 +0000853 }
Damien86c7fc72013-11-26 15:16:41 +0000854}
855
Damien George20006db2014-01-18 14:10:48 +0000856// 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)
857// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000858mp_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 +0000859 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);
860 int adjust = (args[1] == NULL) ? 0 : 1;
861 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000862}
863
Damiend99b0522013-12-21 18:17:45 +0000864mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000865 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100866}
867
Damiend99b0522013-12-21 18:17:45 +0000868mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000869 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100870}
871
Damiend99b0522013-12-21 18:17:45 +0000872mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
873 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100874}
875
Damiend99b0522013-12-21 18:17:45 +0000876mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000877 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100878 return set;
879}
880
Damien George932bf1c2014-01-18 23:42:49 +0000881// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000882void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200883 uint seq_len;
Damiend99b0522013-12-21 18:17:45 +0000884 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
Damiend99b0522013-12-21 18:17:45 +0000885 mp_obj_t *seq_items;
886 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
887 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
888 } else {
889 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000890 }
Damiend99b0522013-12-21 18:17:45 +0000891 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200892 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000893 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200894 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000895 }
Damien George932bf1c2014-01-18 23:42:49 +0000896 for (uint i = 0; i < num; i++) {
897 items[i] = seq_items[num - 1 - i];
898 }
Damien86c7fc72013-11-26 15:16:41 +0000899 } else {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200900 mp_obj_t iterable = rt_getiter(seq_in);
901
902 for (seq_len = 0; seq_len < num; seq_len++) {
903 mp_obj_t el = rt_iternext(iterable);
904 if (el == mp_const_stop_iteration) {
905 goto too_short;
906 }
907 items[num - 1 - seq_len] = el;
908 }
909 if (rt_iternext(iterable) != mp_const_stop_iteration) {
910 goto too_long;
911 }
Damien86c7fc72013-11-26 15:16:41 +0000912 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200913 return;
914
915too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000916 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 +0200917too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000918 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 +0000919}
920
Damiend99b0522013-12-21 18:17:45 +0000921mp_obj_t rt_build_map(int n_args) {
922 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100923}
924
Damiend99b0522013-12-21 18:17:45 +0000925mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
926 // map should always be a dict
927 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100928}
929
Damiend99b0522013-12-21 18:17:45 +0000930mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000931 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
932 // use load_method
933 mp_obj_t dest[2];
934 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000935 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000936 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000937 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000938 } else {
939 // load_method returned a method, so build a bound method object
940 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000941 }
Damiend99b0522013-12-21 18:17:45 +0000942}
943
Damien George7c9c6672014-01-25 00:17:36 +0000944// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
945// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
946// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200947STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000948 // clear output to indicate no attribute/method found yet
949 dest[0] = MP_OBJ_NULL;
950 dest[1] = MP_OBJ_NULL;
951
952 // get the type
953 mp_obj_type_t *type = mp_obj_get_type(base);
954
955 // if this type can do its own load, then call it
956 if (type->load_attr != NULL) {
957 type->load_attr(base, attr, dest);
958 }
959
960 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000961 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000962 if (attr == MP_QSTR___class__) {
963 // a.__class__ is equivalent to type(a)
964 dest[0] = type;
965 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000966 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
967 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000968 } else if (type->load_attr == NULL) {
969 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000970 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000971 const mp_method_t *meth = type->methods;
972 if (meth != NULL) {
973 for (; meth->name != NULL; meth++) {
974 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000975 // check if the methods are functions, static or class methods
976 // see http://docs.python.org/3.3/howto/descriptor.html
977 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
978 // return just the function
Damien George64131f32014-02-06 20:31:44 +0000979 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000980 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
981 // return a bound method, with self being the type of this object
Damien George64131f32014-02-06 20:31:44 +0000982 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien George20006db2014-01-18 14:10:48 +0000983 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000984 } else {
985 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000986 dest[0] = (mp_obj_t)meth->fun;
987 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000988 }
Damien George062478e2014-01-09 20:57:50 +0000989 break;
990 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000991 }
Damiend57eba52013-11-02 23:58:14 +0000992 }
993 }
Damiena3977762013-10-09 23:10:10 +0100994 }
Damien George7c9c6672014-01-25 00:17:36 +0000995}
Damiena3977762013-10-09 23:10:10 +0100996
Damien George7c9c6672014-01-25 00:17:36 +0000997void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
998 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
999
1000 rt_load_method_maybe(base, attr, dest);
1001
1002 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +00001003 // no attribute/method called attr
1004 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +00001005 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
1006 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 +00001007 } else {
Damien Georgec5966122014-02-15 16:10:44 +00001008 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 +00001009 }
1010 }
Damiena3977762013-10-09 23:10:10 +01001011}
1012
Damiend99b0522013-12-21 18:17:45 +00001013void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001014 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +00001015 mp_obj_type_t *type = mp_obj_get_type(base);
1016 if (type->store_attr != NULL) {
1017 if (type->store_attr(base, attr, value)) {
1018 return;
1019 }
Damiena3977762013-10-09 23:10:10 +01001020 }
Damien Georgec5966122014-02-15 16:10:44 +00001021 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 +01001022}
1023
Damiend99b0522013-12-21 18:17:45 +00001024void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001025 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +00001026 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +01001027 // list store
Damiend99b0522013-12-21 18:17:45 +00001028 mp_obj_list_store(base, index, value);
1029 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
1030 // dict store
1031 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +01001032 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +02001033 mp_obj_type_t *type = mp_obj_get_type(base);
1034 if (type->store_item != NULL) {
1035 bool r = type->store_item(base, index, value);
1036 if (r) {
1037 return;
1038 }
1039 // TODO: call base classes here?
1040 }
Damien Georgec5966122014-02-15 16:10:44 +00001041 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 +01001042 }
1043}
1044
Damiend99b0522013-12-21 18:17:45 +00001045mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001046 mp_obj_type_t *type = mp_obj_get_type(o_in);
1047 if (type->getiter != NULL) {
1048 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +01001049 } else {
Damien George7c9c6672014-01-25 00:17:36 +00001050 // check for __getitem__ method
1051 mp_obj_t dest[2];
Damien George7d0bfbe2014-02-08 19:01:47 +00001052 rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George7c9c6672014-01-25 00:17:36 +00001053 if (dest[0] != MP_OBJ_NULL) {
1054 // __getitem__ exists, create an iterator
1055 return mp_obj_new_getitem_iter(dest);
1056 } else {
1057 // object not iterable
Damien George0ec6bd42014-03-09 16:29:36 +00001058 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 +00001059 }
Damience89a212013-10-15 22:25:17 +01001060 }
1061}
1062
Damiend99b0522013-12-21 18:17:45 +00001063mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001064 mp_obj_type_t *type = mp_obj_get_type(o_in);
1065 if (type->iternext != NULL) {
1066 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +01001067 } else {
Damien George0ec6bd42014-03-09 16:29:36 +00001068 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 +00001069 }
1070}
1071
1072mp_obj_t rt_make_raise_obj(mp_obj_t o) {
1073 DEBUG_printf("raise %p\n", o);
1074 if (mp_obj_is_exception_type(o)) {
1075 // o is an exception type (it is derived from BaseException (or is BaseException))
1076 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001077 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1078 // could have const instances in ROM which we return here instead
Damien Georgec5966122014-02-15 16:10:44 +00001079 return rt_call_function_n_kw(o, 0, 0, NULL);
1080 } else if (mp_obj_is_exception_instance(o)) {
1081 // o is an instance of an exception, so use it as the exception
1082 return o;
1083 } else {
1084 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1085 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001086 }
1087}
1088
Damiend99b0522013-12-21 18:17:45 +00001089mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001090 DEBUG_printf("import name %s\n", qstr_str(name));
1091
Damiendb4c3612013-12-10 17:27:24 +00001092 // build args array
Damiend99b0522013-12-21 18:17:45 +00001093 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001094 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001095 args[1] = mp_const_none; // TODO should be globals
1096 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001097 args[3] = fromlist;
1098 args[4] = level; // must be 0; we don't yet support other values
1099
1100 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001101 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001102}
1103
Damiend99b0522013-12-21 18:17:45 +00001104mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001105 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1106
Damiend99b0522013-12-21 18:17:45 +00001107 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +00001108 /* TODO convert AttributeError to ImportError
1109 if (fail) {
1110 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1111 }
1112 */
1113 return x;
1114}
1115
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001116void rt_import_all(mp_obj_t module) {
1117 DEBUG_printf("import all %p\n", module);
1118
1119 mp_map_t *map = mp_obj_module_get_globals(module);
1120 for (uint i = 0; i < map->alloc; i++) {
1121 if (map->table[i].key != MP_OBJ_NULL) {
1122 rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
1123 }
1124 }
1125}
1126
Damien George66028ab2014-01-03 14:03:48 +00001127mp_map_t *rt_locals_get(void) {
1128 return map_locals;
1129}
1130
1131void rt_locals_set(mp_map_t *m) {
1132 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
1133 map_locals = m;
1134}
1135
1136mp_map_t *rt_globals_get(void) {
1137 return map_globals;
1138}
1139
1140void rt_globals_set(mp_map_t *m) {
1141 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
1142 map_globals = m;
1143}
1144
Damien George0d028742014-01-22 23:59:20 +00001145mp_map_t *rt_loaded_modules_get(void) {
1146 return &map_loaded_modules;
1147}
1148
Damien6ba13142013-11-02 20:34:54 +00001149// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +01001150void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +00001151 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +01001152 rt_load_const_str,
1153 rt_load_name,
1154 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +01001155 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001156 rt_load_attr,
1157 rt_load_method,
1158 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001159 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001160 rt_store_subscr,
1161 rt_is_true,
1162 rt_unary_op,
Damien Georgecd82e022014-02-02 13:11:48 +00001163 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001164 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001165 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001166 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001167 rt_build_map,
1168 rt_store_map,
1169 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001170 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001171 rt_make_function_from_id,
Damien Georgecd82e022014-02-02 13:11:48 +00001172 rt_call_function_n_kw_for_native,
Damien George20006db2014-01-18 14:10:48 +00001173 rt_call_method_n_kw,
Damiend2755ec2013-10-16 23:58:48 +01001174 rt_getiter,
1175 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001176};
1177
1178/*
1179void rt_f_vector(rt_fun_kind_t fun_kind) {
1180 (rt_f_table[fun_kind])();
1181}
1182*/