blob: 95c3a44159bafb83516794b4bc8a548217823ddf [file] [log] [blame]
Damienc226dca2013-10-16 16:12:52 +01001// in principle, rt_xxx functions are called only by vm/native/viper and make assumptions about args
Damiend99b0522013-12-21 18:17:45 +00002// mp_xxx functions are safer and can be called by anyone
Damienbd254452013-10-16 20:39:12 +01003// note that rt_assign_xxx are called only from emit*, and maybe we can rename them to reflect this
Damienc226dca2013-10-16 16:12:52 +01004
Damien429d7192013-10-04 19:53:11 +01005#include <stdio.h>
6#include <string.h>
7#include <assert.h>
8
Damience89a212013-10-15 22:25:17 +01009#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010010#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000011#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000012#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000013#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000014#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000015#include "runtime0.h"
16#include "runtime.h"
17#include "map.h"
Damien660365e2013-12-17 18:27:24 +000018#include "builtin.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020019#include "objarray.h"
Damien George5fa93b62014-01-22 14:35:10 +000020#include "bc.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000021#include "intdivmod.h"
Damien660365e2013-12-17 18:27:24 +000022
Damien7f5dacf2013-10-10 11:24:39 +010023#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010024#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000025#define WRITE_CODE (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020026#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000027#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010028#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000029#define DEBUG_printf(...) (void)0
30#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010031#endif
Damien429d7192013-10-04 19:53:11 +010032
Damieneb19efb2013-10-10 22:06:54 +010033// locals and globals need to be pointers because they can be the same in outer module scope
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020034STATIC mp_map_t *map_locals;
35STATIC mp_map_t *map_globals;
36STATIC mp_map_t map_builtins;
37STATIC mp_map_t map_loaded_modules; // TODO: expose as sys.modules
Damienbd254452013-10-16 20:39:12 +010038
Damien429d7192013-10-04 19:53:11 +010039typedef enum {
Damiend99b0522013-12-21 18:17:45 +000040 MP_CODE_NONE,
41 MP_CODE_BYTE,
42 MP_CODE_NATIVE,
43 MP_CODE_INLINE_ASM,
44} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010045
Damiend99b0522013-12-21 18:17:45 +000046typedef struct _mp_code_t {
Damien George51047752014-02-26 17:40:52 +000047 mp_code_kind_t kind : 8;
48 uint scope_flags : 8;
49 uint n_args : 16;
50 uint n_state : 16;
Damien429d7192013-10-04 19:53:11 +010051 union {
52 struct {
Damien429d7192013-10-04 19:53:11 +010053 byte *code;
54 uint len;
55 } u_byte;
Damien826005c2013-10-05 23:17:28 +010056 struct {
Damiend99b0522013-12-21 18:17:45 +000057 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010058 } u_native;
59 struct {
Damiene4af64f2013-10-06 12:04:13 +010060 void *fun;
Damien826005c2013-10-05 23:17:28 +010061 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010062 };
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020063 qstr *arg_names;
Damiend99b0522013-12-21 18:17:45 +000064} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010065
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020066STATIC uint next_unique_code_id;
67STATIC machine_uint_t unique_codes_alloc = 0;
68STATIC mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010069
Damien0446a0d2013-11-17 13:16:36 +000070#ifdef WRITE_CODE
71FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010072#endif
Damien429d7192013-10-04 19:53:11 +010073
Damien Georgeaea532e2014-02-06 22:57:51 +000074// builtins
75// we put this table in ROM because it's always needed and takes up quite a bit of room in RAM
76// 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
77// 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
78// if we wanted to allow dynamic modification of the builtins, we could provide an mp_map_t object which is searched before this one
79
80typedef struct _mp_builtin_elem_t {
81 qstr qstr;
82 mp_obj_t fun;
83} mp_builtin_elem_t;
84
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020085STATIC const mp_builtin_elem_t builtin_table[] = {
Damien Georgeaea532e2014-02-06 22:57:51 +000086 // built-in core functions
87 { MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj },
88 { MP_QSTR___import__, (mp_obj_t)&mp_builtin___import___obj },
89 { MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj },
90
91 // built-in types
92 { MP_QSTR_bool, (mp_obj_t)&bool_type },
Paul Sokolovsky1ecea7c2014-03-21 23:46:59 +020093 { MP_QSTR_bytes, (mp_obj_t)&bytes_type },
Damien Georgeaea532e2014-02-06 22:57:51 +000094#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +000095 { MP_QSTR_complex, (mp_obj_t)&mp_type_complex },
Damien Georgeaea532e2014-02-06 22:57:51 +000096#endif
97 { MP_QSTR_dict, (mp_obj_t)&dict_type },
98 { MP_QSTR_enumerate, (mp_obj_t)&enumerate_type },
99 { MP_QSTR_filter, (mp_obj_t)&filter_type },
100#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000101 { MP_QSTR_float, (mp_obj_t)&mp_type_float },
Damien Georgeaea532e2014-02-06 22:57:51 +0000102#endif
103 { MP_QSTR_int, (mp_obj_t)&int_type },
104 { MP_QSTR_list, (mp_obj_t)&list_type },
105 { MP_QSTR_map, (mp_obj_t)&map_type },
106 { MP_QSTR_set, (mp_obj_t)&set_type },
Paul Sokolovskybe020c22014-03-21 11:39:01 +0200107 { MP_QSTR_str, (mp_obj_t)&str_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000108 { MP_QSTR_super, (mp_obj_t)&super_type },
109 { MP_QSTR_tuple, (mp_obj_t)&tuple_type },
Damien Georgec5966122014-02-15 16:10:44 +0000110 { MP_QSTR_type, (mp_obj_t)&mp_type_type },
Damien Georgeaea532e2014-02-06 22:57:51 +0000111 { MP_QSTR_zip, (mp_obj_t)&zip_type },
112
113 { MP_QSTR_classmethod, (mp_obj_t)&mp_type_classmethod },
114 { MP_QSTR_staticmethod, (mp_obj_t)&mp_type_staticmethod },
115
116 // built-in user functions
117 { MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj },
118 { MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj },
119 { MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj },
Damien Georgeaea532e2014-02-06 22:57:51 +0000120 { MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj },
121 { MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj },
122 { MP_QSTR_dir, (mp_obj_t)&mp_builtin_dir_obj },
123 { MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj },
124 { MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj },
125 { MP_QSTR_exec, (mp_obj_t)&mp_builtin_exec_obj },
126 { MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj },
127 { MP_QSTR_id, (mp_obj_t)&mp_builtin_id_obj },
128 { MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj },
129 { MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj },
130 { MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj },
131 { MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj },
132 { MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj },
133 { MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj },
134 { MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj },
135 { MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj },
136 { MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj },
137 { MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj },
138 { MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj },
139 { MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj },
140 { MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj },
141 { MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj },
Damien Georgeaea532e2014-02-06 22:57:51 +0000142 { MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj },
143
Damien Georgec5966122014-02-15 16:10:44 +0000144 // built-in exceptions
145 { MP_QSTR_BaseException, (mp_obj_t)&mp_type_BaseException },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000146 { MP_QSTR_ArithmeticError, (mp_obj_t)&mp_type_ArithmeticError },
Damien Georgec5966122014-02-15 16:10:44 +0000147 { MP_QSTR_AssertionError, (mp_obj_t)&mp_type_AssertionError },
148 { MP_QSTR_AttributeError, (mp_obj_t)&mp_type_AttributeError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000149 { MP_QSTR_BufferError, (mp_obj_t)&mp_type_BufferError },
150 { MP_QSTR_BytesWarning, (mp_obj_t)&mp_type_BytesWarning },
151 { MP_QSTR_DeprecationWarning, (mp_obj_t)&mp_type_DeprecationWarning },
152 { 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 },
156 { MP_QSTR_FutureWarning, (mp_obj_t)&mp_type_FutureWarning },
157 { MP_QSTR_GeneratorExit, (mp_obj_t)&mp_type_GeneratorExit },
158 { MP_QSTR_IOError, (mp_obj_t)&mp_type_IOError },
Damien Georgec5966122014-02-15 16:10:44 +0000159 { MP_QSTR_ImportError, (mp_obj_t)&mp_type_ImportError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000160 { MP_QSTR_ImportWarning, (mp_obj_t)&mp_type_ImportWarning },
Damien Georgec5966122014-02-15 16:10:44 +0000161 { MP_QSTR_IndentationError, (mp_obj_t)&mp_type_IndentationError },
162 { MP_QSTR_IndexError, (mp_obj_t)&mp_type_IndexError },
163 { MP_QSTR_KeyError, (mp_obj_t)&mp_type_KeyError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000164 { MP_QSTR_LookupError, (mp_obj_t)&mp_type_LookupError },
165 { MP_QSTR_MemoryError, (mp_obj_t)&mp_type_MemoryError },
Damien Georgec5966122014-02-15 16:10:44 +0000166 { MP_QSTR_NameError, (mp_obj_t)&mp_type_NameError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000167 { MP_QSTR_NotImplementedError, (mp_obj_t)&mp_type_NotImplementedError },
168 { MP_QSTR_OSError, (mp_obj_t)&mp_type_OSError },
169 { MP_QSTR_OverflowError, (mp_obj_t)&mp_type_OverflowError },
170 { MP_QSTR_PendingDeprecationWarning, (mp_obj_t)&mp_type_PendingDeprecationWarning },
171 { MP_QSTR_ReferenceError, (mp_obj_t)&mp_type_ReferenceError },
172 { MP_QSTR_ResourceWarning, (mp_obj_t)&mp_type_ResourceWarning },
173 { MP_QSTR_RuntimeError, (mp_obj_t)&mp_type_RuntimeError },
174 { MP_QSTR_RuntimeWarning, (mp_obj_t)&mp_type_RuntimeWarning },
Damien Georgec5966122014-02-15 16:10:44 +0000175 { MP_QSTR_SyntaxError, (mp_obj_t)&mp_type_SyntaxError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000176 { MP_QSTR_SyntaxWarning, (mp_obj_t)&mp_type_SyntaxWarning },
177 { MP_QSTR_SystemError, (mp_obj_t)&mp_type_SystemError },
178 { MP_QSTR_SystemExit, (mp_obj_t)&mp_type_SystemExit },
179 { MP_QSTR_TabError, (mp_obj_t)&mp_type_TabError },
Damien Georgec5966122014-02-15 16:10:44 +0000180 { MP_QSTR_TypeError, (mp_obj_t)&mp_type_TypeError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000181 { MP_QSTR_UnboundLocalError, (mp_obj_t)&mp_type_UnboundLocalError },
182 { MP_QSTR_UserWarning, (mp_obj_t)&mp_type_UserWarning },
Damien Georgec5966122014-02-15 16:10:44 +0000183 { MP_QSTR_ValueError, (mp_obj_t)&mp_type_ValueError },
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000184 { MP_QSTR_Warning, (mp_obj_t)&mp_type_Warning },
185 { MP_QSTR_ZeroDivisionError, (mp_obj_t)&mp_type_ZeroDivisionError },
186 { MP_QSTR_StopIteration, (mp_obj_t)&mp_type_StopIteration },
Damien Georgec5966122014-02-15 16:10:44 +0000187 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
188 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
Damien Georgec5966122014-02-15 16:10:44 +0000189
Paul Sokolovsky910843e2014-02-14 12:02:34 +0200190 // Extra builtins as defined by a port
191 MICROPY_EXTRA_BUILTINS
192
Damien Georgeaea532e2014-02-06 22:57:51 +0000193 { MP_QSTR_, MP_OBJ_NULL }, // end of list sentinel
194};
195
Damien George38a2da62014-01-08 17:33:12 +0000196// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200197STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +0000198 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
199}
200
Damien8b3a7c22013-10-23 20:20:17 +0100201void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +0100202 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +0000203 map_locals = map_globals = mp_map_new(1);
Damien George5fa93b62014-01-22 14:35:10 +0000204 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +0100205
Damienb86e3f92013-12-29 17:17:43 +0000206 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +0000207 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +0000208
Damien George0d028742014-01-22 23:59:20 +0000209 // init loaded modules table
210 mp_map_init(&map_loaded_modules, 3);
211
Damien Georgee9906ac2014-01-04 18:44:46 +0000212 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000213 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000214
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200215 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
216 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200217
Paul Sokolovskyd08fd682014-02-27 22:22:04 +0200218 mp_obj_t m_collections = mp_obj_new_module(MP_QSTR_collections);
219 rt_store_attr(m_collections, MP_QSTR_namedtuple, (mp_obj_t)&mp_namedtuple_obj);
220
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200221#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +0200222 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200223 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
224 // Avoid warning of unused var
225 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200226#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200227 // init sys.path
228 // for efficiency, left to platform-specific startup code
229 //sys_path = mp_obj_new_list(0, NULL);
230 //rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200231
Damien George0c36da02014-03-08 15:24:39 +0000232 // we pre-import the micropython module
233 // probably shouldn't do this, so we are compatible with CPython
234 rt_store_name(MP_QSTR_micropython, (mp_obj_t)&mp_module_micropython);
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +0200235
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200236 // TODO: wastes one mp_code_t structure in mem
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000237 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000238 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100239 unique_codes = NULL;
240
Damien0446a0d2013-11-17 13:16:36 +0000241#ifdef WRITE_CODE
242 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100243#endif
Damien429d7192013-10-04 19:53:11 +0100244}
245
Damien8b3a7c22013-10-23 20:20:17 +0100246void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000247 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200248 mp_map_free(map_globals);
249 mp_map_deinit(&map_loaded_modules);
250 mp_map_deinit(&map_builtins);
Damien0446a0d2013-11-17 13:16:36 +0000251#ifdef WRITE_CODE
252 if (fp_write_code != NULL) {
253 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100254 }
Damiena1ddfcc2013-10-10 23:25:50 +0100255#endif
Damien429d7192013-10-04 19:53:11 +0100256}
257
Damien Georged0691cc2014-01-29 20:30:52 +0000258uint rt_get_unique_code_id(void) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000259 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100260}
261
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200262STATIC void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000263 if (next_unique_code_id > unique_codes_alloc) {
Damien Georged0691cc2014-01-29 20:30:52 +0000264 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 +0000265 // increase size of unique_codes table
266 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
Damien Georged0691cc2014-01-29 20:30:52 +0000267 for (uint i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000268 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100269 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000270 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100271 }
Damien826005c2013-10-05 23:17:28 +0100272}
273
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200274void 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 +0100275 alloc_unique_codes();
276
John R. Lenton9c83ec02014-01-07 23:06:46 +0000277 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 +0000278 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien George8725f8f2014-02-15 19:33:11 +0000279 unique_codes[unique_code_id].scope_flags = scope_flags;
Damien Georged0691cc2014-01-29 20:30:52 +0000280 unique_codes[unique_code_id].n_args = n_args;
281 unique_codes[unique_code_id].n_state = n_locals + n_stack;
Damien826005c2013-10-05 23:17:28 +0100282 unique_codes[unique_code_id].u_byte.code = code;
283 unique_codes[unique_code_id].u_byte.len = len;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200284 unique_codes[unique_code_id].arg_names = arg_names;
Damien826005c2013-10-05 23:17:28 +0100285
Damien9ecbcff2013-12-11 00:41:43 +0000286 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000287
288#ifdef DEBUG_PRINT
Damien George08d07552014-01-29 18:58:52 +0000289 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 +0000290 for (int i = 0; i < 128 && i < len; i++) {
291 if (i > 0 && i % 16 == 0) {
292 DEBUG_printf("\n");
293 }
294 DEBUG_printf(" %02x", code[i]);
295 }
296 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000297#if MICROPY_DEBUG_PRINTERS
298 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000299#endif
Damien0446a0d2013-11-17 13:16:36 +0000300#endif
Damien826005c2013-10-05 23:17:28 +0100301}
302
Damien Georged0691cc2014-01-29 20:30:52 +0000303void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100304 alloc_unique_codes();
305
John R. Lenton9c83ec02014-01-07 23:06:46 +0000306 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 +0000307 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien George8725f8f2014-02-15 19:33:11 +0000308 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000309 unique_codes[unique_code_id].n_args = n_args;
310 unique_codes[unique_code_id].n_state = 0;
Damien429d7192013-10-04 19:53:11 +0100311 unique_codes[unique_code_id].u_native.fun = fun;
312
Damien George8cc96a32013-12-30 18:23:50 +0000313 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000314
Damiena1ddfcc2013-10-10 23:25:50 +0100315#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100316 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
317 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
318 for (int i = 0; i < 128 && i < len; i++) {
319 if (i > 0 && i % 16 == 0) {
320 DEBUG_printf("\n");
321 }
322 DEBUG_printf(" %02x", fun_data[i]);
323 }
324 DEBUG_printf("\n");
325
Damien0446a0d2013-11-17 13:16:36 +0000326#ifdef WRITE_CODE
327 if (fp_write_code != NULL) {
328 fwrite(fun_data, len, 1, fp_write_code);
329 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100330 }
Damiena1ddfcc2013-10-10 23:25:50 +0100331#endif
332#endif
Damien429d7192013-10-04 19:53:11 +0100333}
334
Damien Georged0691cc2014-01-29 20:30:52 +0000335void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100336 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100337
John R. Lenton9c83ec02014-01-07 23:06:46 +0000338 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 +0000339 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien George8725f8f2014-02-15 19:33:11 +0000340 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000341 unique_codes[unique_code_id].n_args = n_args;
342 unique_codes[unique_code_id].n_state = 0;
Damien826005c2013-10-05 23:17:28 +0100343 unique_codes[unique_code_id].u_inline_asm.fun = fun;
344
Damiena1ddfcc2013-10-10 23:25:50 +0100345#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100346 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
347 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
348 for (int i = 0; i < 128 && i < len; i++) {
349 if (i > 0 && i % 16 == 0) {
350 DEBUG_printf("\n");
351 }
352 DEBUG_printf(" %02x", fun_data[i]);
353 }
354 DEBUG_printf("\n");
355
Damien0446a0d2013-11-17 13:16:36 +0000356#ifdef WRITE_CODE
357 if (fp_write_code != NULL) {
358 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100359 }
Damiena1ddfcc2013-10-10 23:25:50 +0100360#endif
361#endif
Damien429d7192013-10-04 19:53:11 +0100362}
363
Damiend99b0522013-12-21 18:17:45 +0000364int rt_is_true(mp_obj_t arg) {
365 DEBUG_OP_printf("is true %p\n", arg);
Damien George09a0c642014-01-30 10:05:33 +0000366 if (arg == mp_const_false) {
367 return 0;
368 } else if (arg == mp_const_true) {
369 return 1;
370 } else if (arg == mp_const_none) {
371 return 0;
372 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000373 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
374 return 0;
375 } else {
376 return 1;
377 }
Damiend99b0522013-12-21 18:17:45 +0000378 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200379 mp_obj_type_t *type = mp_obj_get_type(arg);
380 if (type->unary_op != NULL) {
381 mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
Damien George09a0c642014-01-30 10:05:33 +0000382 if (result != MP_OBJ_NULL) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200383 return result == mp_const_true;
384 }
385 }
386
Damien Georgee4b6a072014-01-28 23:27:35 +0000387 mp_obj_t len = mp_obj_len_maybe(arg);
388 if (len != MP_OBJ_NULL) {
389 // obj has a length, truth determined if len != 0
390 return len != MP_OBJ_NEW_SMALL_INT(0);
391 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200392 // any other obj is true per Python semantics
Damien Georgee4b6a072014-01-28 23:27:35 +0000393 return 1;
394 }
Damiend99b0522013-12-21 18:17:45 +0000395 }
396}
397
398mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
399 return mp_obj_list_append(self_in, arg);
400}
401
Damiend99b0522013-12-21 18:17:45 +0000402mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000403 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000404 uint len;
405 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +0000406 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +0000407}
408
Damiend99b0522013-12-21 18:17:45 +0000409mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100410 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000411 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100412}
413
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200414mp_obj_t rt_load_const_bytes(qstr qstr) {
415 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
416 uint len;
417 const byte *data = qstr_data(qstr, &len);
418 return mp_obj_new_bytes(data, len);
419}
420
Damiend99b0522013-12-21 18:17:45 +0000421mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100422 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100423 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000424 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 +0000425 if (elem != NULL) {
426 return elem->value;
427 } else {
428 return rt_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100429 }
Damiena3977762013-10-09 23:10:10 +0100430}
431
Damiend99b0522013-12-21 18:17:45 +0000432mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100433 // logic: search globals, builtins
434 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000435 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100436 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000437 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100438 if (elem == NULL) {
Damien Georgeaea532e2014-02-06 22:57:51 +0000439 for (const mp_builtin_elem_t *e = &builtin_table[0]; e->qstr != MP_QSTR_; e++) {
440 if (e->qstr == qstr) {
441 return e->fun;
442 }
443 }
Damien Georgec5966122014-02-15 16:10:44 +0000444 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 +0100445 }
446 }
447 return elem->value;
448}
449
Damiend99b0522013-12-21 18:17:45 +0000450mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100451 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000452 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 +0000453 if (elem != NULL) {
454 return elem->value;
455 } else {
456 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100457 }
Damien429d7192013-10-04 19:53:11 +0100458}
459
Damiend99b0522013-12-21 18:17:45 +0000460mp_obj_t rt_get_cell(mp_obj_t cell) {
461 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000462}
463
Damiend99b0522013-12-21 18:17:45 +0000464void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
465 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000466}
467
Damiend99b0522013-12-21 18:17:45 +0000468void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100469 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000470 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 +0100471}
472
Damiend99b0522013-12-21 18:17:45 +0000473void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100474 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000475 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 +0100476}
477
Damiend99b0522013-12-21 18:17:45 +0000478mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000479 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000480
Damiend99b0522013-12-21 18:17:45 +0000481 if (MP_OBJ_IS_SMALL_INT(arg)) {
482 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000483 switch (op) {
Damien George9d68e9c2014-03-12 15:38:15 +0000484 case RT_UNARY_OP_BOOL:
485 return MP_BOOL(val != 0);
486 case RT_UNARY_OP_POSITIVE:
487 return arg;
488 case RT_UNARY_OP_NEGATIVE:
489 // check for overflow
490 if (val == MP_SMALL_INT_MIN) {
491 return mp_obj_new_int(-val);
492 } else {
493 return MP_OBJ_NEW_SMALL_INT(-val);
494 }
495 case RT_UNARY_OP_INVERT:
496 return MP_OBJ_NEW_SMALL_INT(~val);
497 default:
498 assert(0);
499 return arg;
Damien7410e442013-11-02 19:47:57 +0000500 }
Damien George1e708fe2014-01-23 18:27:51 +0000501 } else {
502 mp_obj_type_t *type = mp_obj_get_type(arg);
503 if (type->unary_op != NULL) {
504 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000505 if (result != NULL) {
506 return result;
507 }
Damien7410e442013-11-02 19:47:57 +0000508 }
Damiend99b0522013-12-21 18:17:45 +0000509 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000510 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 +0000511 }
Damien429d7192013-10-04 19:53:11 +0100512}
513
Damiend99b0522013-12-21 18:17:45 +0000514mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100515 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000516
517 // TODO correctly distinguish inplace operators for mutable objects
518 // lookup logic that CPython uses for +=:
519 // check for implemented +=
520 // then check for implemented +
521 // then check for implemented seq.inplace_concat
522 // then check for implemented seq.concat
523 // then fail
524 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
525
Damien George9aa2a522014-02-01 23:04:09 +0000526 // deal with is
527 if (op == RT_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200528 return MP_BOOL(lhs == rhs);
529 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200530
Damien Georgebcbeea02014-01-11 10:47:22 +0000531 // deal with == and != for all types
Damien George9aa2a522014-02-01 23:04:09 +0000532 if (op == RT_BINARY_OP_EQUAL || op == RT_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000533 if (mp_obj_equal(lhs, rhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000534 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000535 return mp_const_true;
536 } else {
537 return mp_const_false;
538 }
539 } else {
Damien George9aa2a522014-02-01 23:04:09 +0000540 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000541 return mp_const_false;
542 } else {
543 return mp_const_true;
544 }
545 }
546 }
547
548 // deal with exception_match for all types
Damien George9aa2a522014-02-01 23:04:09 +0000549 if (op == RT_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000550 // rhs must be issubclass(rhs, BaseException)
551 if (mp_obj_is_exception_type(rhs)) {
552 // if lhs is an instance of an exception, then extract and use its type
553 if (mp_obj_is_exception_instance(lhs)) {
554 lhs = mp_obj_get_type(lhs);
555 }
Damien George71510152014-03-03 22:38:13 +0000556 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000557 return mp_const_true;
558 } else {
559 return mp_const_false;
560 }
561 }
562 }
563
Damien George1a9951d2014-01-06 22:13:00 +0000564 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000565 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000566 if (MP_OBJ_IS_SMALL_INT(rhs)) {
567 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000568 // This is a binary operation: lhs_val op rhs_val
569 // We need to be careful to handle overflow; see CERT INT32-C
570 // Operations that can overflow:
571 // + result always fits in machine_int_t, then handled by SMALL_INT check
572 // - result always fits in machine_int_t, then handled by SMALL_INT check
573 // * checked explicitly
574 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
575 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
576 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000577 switch (op) {
578 case RT_BINARY_OP_OR:
579 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
580 case RT_BINARY_OP_XOR:
581 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
582 case RT_BINARY_OP_AND:
583 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
584 case RT_BINARY_OP_LSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000585 case RT_BINARY_OP_INPLACE_LSHIFT: {
586 if (rhs_val < 0) {
587 // negative shift not allowed
588 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
589 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
590 // left-shift will overflow, so use higher precision integer
591 lhs = mp_obj_new_int_from_ll(lhs_val);
592 goto generic_binary_op;
593 } else {
594 // use standard precision
595 lhs_val <<= rhs_val;
596 }
597 break;
598 }
Damien George1a9951d2014-01-06 22:13:00 +0000599 case RT_BINARY_OP_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000600 case RT_BINARY_OP_INPLACE_RSHIFT:
601 if (rhs_val < 0) {
602 // negative shift not allowed
603 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
604 } else {
605 // standard precision is enough for right-shift
606 lhs_val >>= rhs_val;
607 }
608 break;
Damien George1a9951d2014-01-06 22:13:00 +0000609 case RT_BINARY_OP_ADD:
610 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
611 case RT_BINARY_OP_SUBTRACT:
612 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
613 case RT_BINARY_OP_MULTIPLY:
Damien George9d68e9c2014-03-12 15:38:15 +0000614 case RT_BINARY_OP_INPLACE_MULTIPLY: {
615
616 // If long long type exists and is larger than machine_int_t, then
617 // we can use the following code to perform overflow-checked multiplication.
618 // Otherwise (eg in x64 case) we must use the branching code below.
619 #if 0
620 // compute result using long long precision
621 long long res = (long long)lhs_val * (long long)rhs_val;
622 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
623 // result overflowed SMALL_INT, so return higher precision integer
624 return mp_obj_new_int_from_ll(res);
625 } else {
626 // use standard precision
627 lhs_val = (mp_small_int_t)res;
628 }
629 #endif
630
631 if (lhs_val > 0) { // lhs_val is positive
632 if (rhs_val > 0) { // lhs_val and rhs_val are positive
633 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
634 goto mul_overflow;
635 }
636 } else { // lhs_val positive, rhs_val nonpositive
637 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
638 goto mul_overflow;
639 }
640 } // lhs_val positive, rhs_val nonpositive
641 } else { // lhs_val is nonpositive
642 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
643 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
644 goto mul_overflow;
645 }
646 } else { // lhs_val and rhs_val are nonpositive
647 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
648 goto mul_overflow;
649 }
650 } // End if lhs_val and rhs_val are nonpositive
651 } // End if lhs_val is nonpositive
652
653 // use standard precision
654 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
655
656 mul_overflow:
657 // use higher precision
658 lhs = mp_obj_new_int_from_ll(lhs_val);
659 goto generic_binary_op;
660
661 break;
662 }
Damien George1a9951d2014-01-06 22:13:00 +0000663 case RT_BINARY_OP_FLOOR_DIVIDE:
664 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
Damien George9d68e9c2014-03-12 15:38:15 +0000665 #if MICROPY_ENABLE_FLOAT
Damien George1a9951d2014-01-06 22:13:00 +0000666 case RT_BINARY_OP_TRUE_DIVIDE:
667 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 +0000668 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000669
Damien George1a9951d2014-01-06 22:13:00 +0000670 case RT_BINARY_OP_MODULO:
Rachel Dowdallcde86312014-03-22 17:29:27 +0000671 case RT_BINARY_OP_INPLACE_MODULO:
672 {
673 lhs_val = python_modulo(lhs_val, rhs_val);
674 break;
675 }
Damien George1a9951d2014-01-06 22:13:00 +0000676 case RT_BINARY_OP_POWER:
677 case RT_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000678 if (rhs_val < 0) {
679 #if MICROPY_ENABLE_FLOAT
680 lhs = mp_obj_new_float(lhs_val);
681 goto generic_binary_op;
682 #else
683 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
684 #endif
685 } else {
686 // TODO check for overflow
687 machine_int_t ans = 1;
688 while (rhs_val > 0) {
689 if (rhs_val & 1) {
690 ans *= lhs_val;
691 }
692 lhs_val *= lhs_val;
693 rhs_val /= 2;
Damien George1a9951d2014-01-06 22:13:00 +0000694 }
Damien George9d68e9c2014-03-12 15:38:15 +0000695 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000696 }
Damien George1a9951d2014-01-06 22:13:00 +0000697 break;
Damien George9aa2a522014-02-01 23:04:09 +0000698 case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
699 case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
700 case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
701 case RT_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000702
Damien George1a9951d2014-01-06 22:13:00 +0000703 default: assert(0);
704 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200705 // TODO: We just should make mp_obj_new_int() inline and use that
706 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000707 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000708 } else {
709 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000710 }
Damien George3f759b72014-01-31 00:42:12 +0000711#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000712 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000713 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000714 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000715 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000716#endif
Damien429d7192013-10-04 19:53:11 +0100717 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000718 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000719
Damien George9aa2a522014-02-01 23:04:09 +0000720 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000721 *
722 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000723 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000724 */
Damien George9aa2a522014-02-01 23:04:09 +0000725 if (op == RT_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000726 mp_obj_type_t *type = mp_obj_get_type(rhs);
727 if (type->binary_op != NULL) {
728 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000729 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000730 return res;
731 }
732 }
733 if (type->getiter != NULL) {
734 /* second attempt, walk the iterator */
735 mp_obj_t next = NULL;
736 mp_obj_t iter = rt_getiter(rhs);
737 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
738 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000739 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000740 }
Damien7410e442013-11-02 19:47:57 +0000741 }
Damien George9aa2a522014-02-01 23:04:09 +0000742 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000743 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000744
745 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000746 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000747 mp_obj_get_type_str(rhs)));
748 return mp_const_none;
749 }
750
Damien George5fa93b62014-01-22 14:35:10 +0000751 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000752 mp_obj_type_t *type;
753generic_binary_op:
754 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000755 if (type->binary_op != NULL) {
756 mp_obj_t result = type->binary_op(op, lhs, rhs);
757 if (result != MP_OBJ_NULL) {
758 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000759 }
Damien429d7192013-10-04 19:53:11 +0100760 }
Damiend99b0522013-12-21 18:17:45 +0000761
Damien George5fa93b62014-01-22 14:35:10 +0000762 // TODO implement dispatch for reverse binary ops
763
Damiend99b0522013-12-21 18:17:45 +0000764 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000765 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200766 "unsupported operand types for binary operator: '%s', '%s'",
767 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000768 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100769}
770
Paul Sokolovsky90750022014-02-01 15:05:04 +0200771mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100772 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
773 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100774 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000775 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100776 }
Damiend99b0522013-12-21 18:17:45 +0000777
778 // make the function, depending on the code kind
779 mp_code_t *c = &unique_codes[unique_code_id];
780 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100781 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000782 case MP_CODE_BYTE:
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200783 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 +0100784 break;
Damiend99b0522013-12-21 18:17:45 +0000785 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000786 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100787 break;
Damiend99b0522013-12-21 18:17:45 +0000788 case MP_CODE_INLINE_ASM:
789 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100790 break;
791 default:
792 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000793 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100794 }
Damienbd254452013-10-16 20:39:12 +0100795
796 // check for generator functions and if so wrap in generator object
Damien George8725f8f2014-02-15 19:33:11 +0000797 if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien Georged0691cc2014-01-29 20:30:52 +0000798 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100799 }
800
Damiend99b0522013-12-21 18:17:45 +0000801 return fun;
Damien429d7192013-10-04 19:53:11 +0100802}
803
Damiend99b0522013-12-21 18:17:45 +0000804mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000805 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000806 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200807 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000808 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000809 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000810}
811
Damiend99b0522013-12-21 18:17:45 +0000812mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000813 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100814}
815
Damiend99b0522013-12-21 18:17:45 +0000816mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000817 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100818}
819
Damiend99b0522013-12-21 18:17:45 +0000820mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
821 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000822 args[0] = arg1;
823 args[1] = arg2;
824 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100825}
826
Damien Georgecd82e022014-02-02 13:11:48 +0000827// wrapper that accepts n_args and n_kw in one argument
828// native emitter can only pass at most 3 arguments to a function
829mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
830 return rt_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
831}
832
Damien George20006db2014-01-18 14:10:48 +0000833// args contains, eg: arg0 arg1 key0 value0 key1 value1
834mp_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 +0000835 // TODO improve this: fun object can specify its type and we parse here the arguments,
836 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100837
John R. Lenton9c83ec02014-01-07 23:06:46 +0000838 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
839
Damien George8b56beb2014-01-31 23:49:49 +0000840 // get the type
841 mp_obj_type_t *type = mp_obj_get_type(fun_in);
842
843 // do the call
844 if (type->call != NULL) {
845 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000846 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000847 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 +0000848 }
Damien86c7fc72013-11-26 15:16:41 +0000849}
850
Damien George20006db2014-01-18 14:10:48 +0000851// 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)
852// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000853mp_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 +0000854 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);
855 int adjust = (args[1] == NULL) ? 0 : 1;
856 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000857}
858
Damiend99b0522013-12-21 18:17:45 +0000859mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000860 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100861}
862
Damiend99b0522013-12-21 18:17:45 +0000863mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000864 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100865}
866
Damiend99b0522013-12-21 18:17:45 +0000867mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
868 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100869}
870
Damiend99b0522013-12-21 18:17:45 +0000871mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000872 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100873 return set;
874}
875
Damien George932bf1c2014-01-18 23:42:49 +0000876// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000877void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200878 uint seq_len;
Damiend99b0522013-12-21 18:17:45 +0000879 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
Damiend99b0522013-12-21 18:17:45 +0000880 mp_obj_t *seq_items;
881 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
882 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
883 } else {
884 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000885 }
Damiend99b0522013-12-21 18:17:45 +0000886 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200887 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000888 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200889 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000890 }
Damien George932bf1c2014-01-18 23:42:49 +0000891 for (uint i = 0; i < num; i++) {
892 items[i] = seq_items[num - 1 - i];
893 }
Damien86c7fc72013-11-26 15:16:41 +0000894 } else {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200895 mp_obj_t iterable = rt_getiter(seq_in);
896
897 for (seq_len = 0; seq_len < num; seq_len++) {
898 mp_obj_t el = rt_iternext(iterable);
899 if (el == mp_const_stop_iteration) {
900 goto too_short;
901 }
902 items[num - 1 - seq_len] = el;
903 }
904 if (rt_iternext(iterable) != mp_const_stop_iteration) {
905 goto too_long;
906 }
Damien86c7fc72013-11-26 15:16:41 +0000907 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200908 return;
909
910too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000911 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 +0200912too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000913 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 +0000914}
915
Damiend99b0522013-12-21 18:17:45 +0000916mp_obj_t rt_build_map(int n_args) {
917 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100918}
919
Damiend99b0522013-12-21 18:17:45 +0000920mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
921 // map should always be a dict
922 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100923}
924
Damiend99b0522013-12-21 18:17:45 +0000925mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000926 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
927 // use load_method
928 mp_obj_t dest[2];
929 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000930 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000931 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000932 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000933 } else {
934 // load_method returned a method, so build a bound method object
935 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000936 }
Damiend99b0522013-12-21 18:17:45 +0000937}
938
Damien George7c9c6672014-01-25 00:17:36 +0000939// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
940// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
941// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200942STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000943 // clear output to indicate no attribute/method found yet
944 dest[0] = MP_OBJ_NULL;
945 dest[1] = MP_OBJ_NULL;
946
947 // get the type
948 mp_obj_type_t *type = mp_obj_get_type(base);
949
950 // if this type can do its own load, then call it
951 if (type->load_attr != NULL) {
952 type->load_attr(base, attr, dest);
953 }
954
955 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000956 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000957 if (attr == MP_QSTR___class__) {
958 // a.__class__ is equivalent to type(a)
959 dest[0] = type;
960 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000961 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
962 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000963 } else if (type->load_attr == NULL) {
964 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000965 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000966 const mp_method_t *meth = type->methods;
967 if (meth != NULL) {
968 for (; meth->name != NULL; meth++) {
969 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000970 // check if the methods are functions, static or class methods
971 // see http://docs.python.org/3.3/howto/descriptor.html
972 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
973 // return just the function
Damien George64131f32014-02-06 20:31:44 +0000974 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000975 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
976 // return a bound method, with self being the type of this object
Damien George64131f32014-02-06 20:31:44 +0000977 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien George20006db2014-01-18 14:10:48 +0000978 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000979 } else {
980 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000981 dest[0] = (mp_obj_t)meth->fun;
982 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000983 }
Damien George062478e2014-01-09 20:57:50 +0000984 break;
985 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000986 }
Damiend57eba52013-11-02 23:58:14 +0000987 }
988 }
Damiena3977762013-10-09 23:10:10 +0100989 }
Damien George7c9c6672014-01-25 00:17:36 +0000990}
Damiena3977762013-10-09 23:10:10 +0100991
Damien George7c9c6672014-01-25 00:17:36 +0000992void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
993 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
994
995 rt_load_method_maybe(base, attr, dest);
996
997 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000998 // no attribute/method called attr
999 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +00001000 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
1001 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 +00001002 } else {
Damien Georgec5966122014-02-15 16:10:44 +00001003 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 +00001004 }
1005 }
Damiena3977762013-10-09 23:10:10 +01001006}
1007
Damiend99b0522013-12-21 18:17:45 +00001008void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001009 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +00001010 mp_obj_type_t *type = mp_obj_get_type(base);
1011 if (type->store_attr != NULL) {
1012 if (type->store_attr(base, attr, value)) {
1013 return;
1014 }
Damiena3977762013-10-09 23:10:10 +01001015 }
Damien Georgec5966122014-02-15 16:10:44 +00001016 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 +01001017}
1018
Damiend99b0522013-12-21 18:17:45 +00001019void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +01001020 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +00001021 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +01001022 // list store
Damiend99b0522013-12-21 18:17:45 +00001023 mp_obj_list_store(base, index, value);
1024 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
1025 // dict store
1026 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +01001027 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +02001028 mp_obj_type_t *type = mp_obj_get_type(base);
1029 if (type->store_item != NULL) {
1030 bool r = type->store_item(base, index, value);
1031 if (r) {
1032 return;
1033 }
1034 // TODO: call base classes here?
1035 }
Damien Georgec5966122014-02-15 16:10:44 +00001036 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 +01001037 }
1038}
1039
Damiend99b0522013-12-21 18:17:45 +00001040mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001041 mp_obj_type_t *type = mp_obj_get_type(o_in);
1042 if (type->getiter != NULL) {
1043 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +01001044 } else {
Damien George7c9c6672014-01-25 00:17:36 +00001045 // check for __getitem__ method
1046 mp_obj_t dest[2];
Damien George7d0bfbe2014-02-08 19:01:47 +00001047 rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George7c9c6672014-01-25 00:17:36 +00001048 if (dest[0] != MP_OBJ_NULL) {
1049 // __getitem__ exists, create an iterator
1050 return mp_obj_new_getitem_iter(dest);
1051 } else {
1052 // object not iterable
Damien George0ec6bd42014-03-09 16:29:36 +00001053 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 +00001054 }
Damience89a212013-10-15 22:25:17 +01001055 }
1056}
1057
Damiend99b0522013-12-21 18:17:45 +00001058mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +00001059 mp_obj_type_t *type = mp_obj_get_type(o_in);
1060 if (type->iternext != NULL) {
1061 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +01001062 } else {
Damien George0ec6bd42014-03-09 16:29:36 +00001063 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 +00001064 }
1065}
1066
1067mp_obj_t rt_make_raise_obj(mp_obj_t o) {
1068 DEBUG_printf("raise %p\n", o);
1069 if (mp_obj_is_exception_type(o)) {
1070 // o is an exception type (it is derived from BaseException (or is BaseException))
1071 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001072 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1073 // could have const instances in ROM which we return here instead
Damien Georgec5966122014-02-15 16:10:44 +00001074 return rt_call_function_n_kw(o, 0, 0, NULL);
1075 } else if (mp_obj_is_exception_instance(o)) {
1076 // o is an instance of an exception, so use it as the exception
1077 return o;
1078 } else {
1079 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1080 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001081 }
1082}
1083
Damiend99b0522013-12-21 18:17:45 +00001084mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001085 DEBUG_printf("import name %s\n", qstr_str(name));
1086
Damiendb4c3612013-12-10 17:27:24 +00001087 // build args array
Damiend99b0522013-12-21 18:17:45 +00001088 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001089 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001090 args[1] = mp_const_none; // TODO should be globals
1091 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001092 args[3] = fromlist;
1093 args[4] = level; // must be 0; we don't yet support other values
1094
1095 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001096 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001097}
1098
Damiend99b0522013-12-21 18:17:45 +00001099mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001100 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1101
Damiend99b0522013-12-21 18:17:45 +00001102 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +00001103 /* TODO convert AttributeError to ImportError
1104 if (fail) {
1105 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1106 }
1107 */
1108 return x;
1109}
1110
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001111void rt_import_all(mp_obj_t module) {
1112 DEBUG_printf("import all %p\n", module);
1113
1114 mp_map_t *map = mp_obj_module_get_globals(module);
1115 for (uint i = 0; i < map->alloc; i++) {
1116 if (map->table[i].key != MP_OBJ_NULL) {
1117 rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
1118 }
1119 }
1120}
1121
Damien George66028ab2014-01-03 14:03:48 +00001122mp_map_t *rt_locals_get(void) {
1123 return map_locals;
1124}
1125
1126void rt_locals_set(mp_map_t *m) {
1127 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
1128 map_locals = m;
1129}
1130
1131mp_map_t *rt_globals_get(void) {
1132 return map_globals;
1133}
1134
1135void rt_globals_set(mp_map_t *m) {
1136 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
1137 map_globals = m;
1138}
1139
Damien George0d028742014-01-22 23:59:20 +00001140mp_map_t *rt_loaded_modules_get(void) {
1141 return &map_loaded_modules;
1142}
1143
Damien6ba13142013-11-02 20:34:54 +00001144// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +01001145void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +00001146 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +01001147 rt_load_const_str,
1148 rt_load_name,
1149 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +01001150 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001151 rt_load_attr,
1152 rt_load_method,
1153 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001154 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001155 rt_store_subscr,
1156 rt_is_true,
1157 rt_unary_op,
Damien Georgecd82e022014-02-02 13:11:48 +00001158 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001159 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001160 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001161 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001162 rt_build_map,
1163 rt_store_map,
1164 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001165 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001166 rt_make_function_from_id,
Damien Georgecd82e022014-02-02 13:11:48 +00001167 rt_call_function_n_kw_for_native,
Damien George20006db2014-01-18 14:10:48 +00001168 rt_call_method_n_kw,
Damiend2755ec2013-10-16 23:58:48 +01001169 rt_getiter,
1170 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001171};
1172
1173/*
1174void rt_f_vector(rt_fun_kind_t fun_kind) {
1175 (rt_f_table[fun_kind])();
1176}
1177*/