blob: 09e4237affc127315ecf64ca3630f947f2ae0ccc [file] [log] [blame]
Damienc226dca2013-10-16 16:12:52 +01001// in principle, rt_xxx functions are called only by vm/native/viper and make assumptions about args
Damiend99b0522013-12-21 18:17:45 +00002// mp_xxx functions are safer and can be called by anyone
Damienbd254452013-10-16 20:39:12 +01003// note that rt_assign_xxx are called only from emit*, and maybe we can rename them to reflect this
Damienc226dca2013-10-16 16:12:52 +01004
Damien429d7192013-10-04 19:53:11 +01005#include <stdint.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9#include <assert.h>
10
Damience89a212013-10-15 22:25:17 +010011#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010012#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "mpconfig.h"
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000014#include "mpqstr.h"
Damien660365e2013-12-17 18:27:24 +000015#include "obj.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"
Damien660365e2013-12-17 18:27:24 +000021
Damien7f5dacf2013-10-10 11:24:39 +010022#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010023#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000024#define WRITE_CODE (1)
Damien7f5dacf2013-10-10 11:24:39 +010025#define DEBUG_printf(args...) printf(args)
26#define DEBUG_OP_printf(args...) printf(args)
27#else // don't print debugging info
Damiena3977762013-10-09 23:10:10 +010028#define DEBUG_printf(args...) (void)0
Damien429d7192013-10-04 19:53:11 +010029#define DEBUG_OP_printf(args...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010030#endif
Damien429d7192013-10-04 19:53:11 +010031
Damieneb19efb2013-10-10 22:06:54 +010032// locals and globals need to be pointers because they can be the same in outer module scope
Damiend99b0522013-12-21 18:17:45 +000033static mp_map_t *map_locals;
34static mp_map_t *map_globals;
35static mp_map_t map_builtins;
Damienbd254452013-10-16 20:39:12 +010036
Damien429d7192013-10-04 19:53:11 +010037typedef enum {
Damiend99b0522013-12-21 18:17:45 +000038 MP_CODE_NONE,
39 MP_CODE_BYTE,
40 MP_CODE_NATIVE,
41 MP_CODE_INLINE_ASM,
42} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010043
Damiend99b0522013-12-21 18:17:45 +000044typedef struct _mp_code_t {
45 mp_code_kind_t kind;
Damien429d7192013-10-04 19:53:11 +010046 int n_args;
Damienbd254452013-10-16 20:39:12 +010047 int n_locals;
48 int n_stack;
49 bool is_generator;
Damien429d7192013-10-04 19:53:11 +010050 union {
51 struct {
Damien429d7192013-10-04 19:53:11 +010052 byte *code;
53 uint len;
54 } u_byte;
Damien826005c2013-10-05 23:17:28 +010055 struct {
Damiend99b0522013-12-21 18:17:45 +000056 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010057 } u_native;
58 struct {
Damiene4af64f2013-10-06 12:04:13 +010059 void *fun;
Damien826005c2013-10-05 23:17:28 +010060 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010061 };
Damiend99b0522013-12-21 18:17:45 +000062} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010063
64static int next_unique_code_id;
John R. Lenton9c83ec02014-01-07 23:06:46 +000065static machine_uint_t unique_codes_alloc = 0;
66static mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010067
Damien0446a0d2013-11-17 13:16:36 +000068#ifdef WRITE_CODE
69FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010070#endif
Damien429d7192013-10-04 19:53:11 +010071
Damien George38a2da62014-01-08 17:33:12 +000072// a good optimising compiler will inline this if necessary
73static void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
74 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
75}
76
Damien8b3a7c22013-10-23 20:20:17 +010077void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +010078 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +000079 map_locals = map_globals = mp_map_new(1);
80 mp_map_add_qstr(map_globals, MP_QSTR___name__, mp_obj_new_str(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +010081
Damienb86e3f92013-12-29 17:17:43 +000082 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +000083 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +000084
85 // built-in exceptions (TODO, make these proper classes)
Damien George38a2da62014-01-08 17:33:12 +000086 mp_map_add_qstr(&map_builtins, MP_QSTR_AttributeError, mp_obj_new_exception(MP_QSTR_AttributeError));
87 mp_map_add_qstr(&map_builtins, MP_QSTR_IndexError, mp_obj_new_exception(MP_QSTR_IndexError));
88 mp_map_add_qstr(&map_builtins, MP_QSTR_KeyError, mp_obj_new_exception(MP_QSTR_KeyError));
89 mp_map_add_qstr(&map_builtins, MP_QSTR_NameError, mp_obj_new_exception(MP_QSTR_NameError));
90 mp_map_add_qstr(&map_builtins, MP_QSTR_TypeError, mp_obj_new_exception(MP_QSTR_TypeError));
91 mp_map_add_qstr(&map_builtins, MP_QSTR_SyntaxError, mp_obj_new_exception(MP_QSTR_SyntaxError));
92 mp_map_add_qstr(&map_builtins, MP_QSTR_ValueError, mp_obj_new_exception(MP_QSTR_ValueError));
Paul Sokolovsky166bb402014-01-18 12:46:43 +020093 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
94 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
95 mp_map_add_qstr(&map_builtins, MP_QSTR_OverflowError, mp_obj_new_exception(MP_QSTR_OverflowError));
Damien George38a2da62014-01-08 17:33:12 +000096 mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
Paul Sokolovskyb81e1fd2014-01-11 16:33:32 +020097 mp_map_add_qstr(&map_builtins, MP_QSTR_AssertionError, mp_obj_new_exception(MP_QSTR_AssertionError));
Damienb86e3f92013-12-29 17:17:43 +000098
Damien Georgee9906ac2014-01-04 18:44:46 +000099 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000100 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000101
Damienb86e3f92013-12-29 17:17:43 +0000102 // built-in core functions
Damien George38a2da62014-01-08 17:33:12 +0000103 mp_map_add_qstr(&map_builtins, MP_QSTR___build_class__, (mp_obj_t)&mp_builtin___build_class___obj);
Damien George23005372014-01-13 19:39:01 +0000104 mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj);
Damienb86e3f92013-12-29 17:17:43 +0000105
Damien George71c51812014-01-04 20:21:15 +0000106 // built-in types
Damien George38a2da62014-01-08 17:33:12 +0000107 mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
Damien George71c51812014-01-04 20:21:15 +0000108#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000109 mp_map_add_qstr(&map_builtins, MP_QSTR_complex, (mp_obj_t)&complex_type);
Damien George71c51812014-01-04 20:21:15 +0000110#endif
Damien George38a2da62014-01-08 17:33:12 +0000111 mp_map_add_qstr(&map_builtins, MP_QSTR_dict, (mp_obj_t)&dict_type);
John R. Lenton9daa7892014-01-14 23:55:01 +0000112 mp_map_add_qstr(&map_builtins, MP_QSTR_enumerate, (mp_obj_t)&enumerate_type);
John R. Lentonfca456b2014-01-15 01:37:08 +0000113 mp_map_add_qstr(&map_builtins, MP_QSTR_filter, (mp_obj_t)&filter_type);
Damien George71c51812014-01-04 20:21:15 +0000114#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000115 mp_map_add_qstr(&map_builtins, MP_QSTR_float, (mp_obj_t)&float_type);
Damien George71c51812014-01-04 20:21:15 +0000116#endif
Damien George38a2da62014-01-08 17:33:12 +0000117 mp_map_add_qstr(&map_builtins, MP_QSTR_int, (mp_obj_t)&int_type);
118 mp_map_add_qstr(&map_builtins, MP_QSTR_list, (mp_obj_t)&list_type);
John R. Lenton39b174e2014-01-15 01:10:09 +0000119 mp_map_add_qstr(&map_builtins, MP_QSTR_map, (mp_obj_t)&map_type);
Damien George38a2da62014-01-08 17:33:12 +0000120 mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type);
121 mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
Damien George93a9b5b2014-01-08 18:48:12 +0000122 mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
John R. Lenton07205ec2014-01-13 02:31:00 +0000123 mp_map_add_qstr(&map_builtins, MP_QSTR_zip, (mp_obj_t)&zip_type);
Damien George71c51812014-01-04 20:21:15 +0000124
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200125 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
126 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
127 rt_store_name(MP_QSTR_array, m_array);
128
Damien George23005372014-01-13 19:39:01 +0000129 // built-in user functions
130 mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj);
131 mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj);
132 mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj);
133 mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
134 mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
135 mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
Damien Georged02c6d82014-01-15 22:14:03 +0000136 mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj);
Damien George38a2da62014-01-08 17:33:12 +0000137 mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
Damien George004cdce2014-01-09 21:43:51 +0000138 mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
139 mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);
Damien George38a2da62014-01-08 17:33:12 +0000140 mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj);
Damien George23005372014-01-13 19:39:01 +0000141 mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj);
142 mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj);
143 mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj);
Damien George38a2da62014-01-08 17:33:12 +0000144 mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj);
Damien George23005372014-01-13 19:39:01 +0000145 mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj);
146 mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj);
147 mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj);
148 mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj);
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000149 mp_map_add_qstr(&map_builtins, MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj);
John R. Lenton5c768392014-01-13 05:12:50 +0000150 mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj);
Damien George23005372014-01-13 19:39:01 +0000151 mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200152 mp_map_add_qstr(&map_builtins, MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200153 mp_map_add_qstr(&map_builtins, MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj);
Damien429d7192013-10-04 19:53:11 +0100154
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200155#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +0200156 // Precreate sys module, so "import sys" didn't throw exceptions.
157 mp_obj_new_module(qstr_from_str_static("sys"));
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200158#endif
159
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +0200160 mp_obj_t m_mp = mp_obj_new_module(qstr_from_str_static("micropython"));
161 rt_store_name(qstr_from_str_static("micropython"), m_mp);
162#if MICROPY_MEM_STATS
163 rt_store_attr(m_mp, qstr_from_str_static("mem_total"), (mp_obj_t)&mp_builtin_mem_total_obj);
164 rt_store_attr(m_mp, qstr_from_str_static("mem_current"), (mp_obj_t)&mp_builtin_mem_current_obj);
165 rt_store_attr(m_mp, qstr_from_str_static("mem_peak"), (mp_obj_t)&mp_builtin_mem_peak_obj);
166#endif
167
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000168 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000169 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100170 unique_codes = NULL;
171
Damien0446a0d2013-11-17 13:16:36 +0000172#ifdef WRITE_CODE
173 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100174#endif
Damien429d7192013-10-04 19:53:11 +0100175}
176
Damien8b3a7c22013-10-23 20:20:17 +0100177void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000178 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Damien0446a0d2013-11-17 13:16:36 +0000179#ifdef WRITE_CODE
180 if (fp_write_code != NULL) {
181 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100182 }
Damiena1ddfcc2013-10-10 23:25:50 +0100183#endif
Damien429d7192013-10-04 19:53:11 +0100184}
185
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000186int rt_get_unique_code_id(void) {
187 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100188}
189
Damien8b3a7c22013-10-23 20:20:17 +0100190static void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000191 if (next_unique_code_id > unique_codes_alloc) {
192 // increase size of unique_codes table
193 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
194 for (int i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000195 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100196 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000197 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100198 }
Damien826005c2013-10-05 23:17:28 +0100199}
200
Damien George6baf76e2013-12-30 22:32:17 +0000201void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) {
Damien826005c2013-10-05 23:17:28 +0100202 alloc_unique_codes();
203
John R. Lenton9c83ec02014-01-07 23:06:46 +0000204 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 +0000205 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien826005c2013-10-05 23:17:28 +0100206 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100207 unique_codes[unique_code_id].n_locals = n_locals;
208 unique_codes[unique_code_id].n_stack = n_stack;
209 unique_codes[unique_code_id].is_generator = is_generator;
Damien826005c2013-10-05 23:17:28 +0100210 unique_codes[unique_code_id].u_byte.code = code;
211 unique_codes[unique_code_id].u_byte.len = len;
212
Damien9ecbcff2013-12-11 00:41:43 +0000213 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000214
215#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100216 DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d\n", unique_code_id, code, len, n_args);
Damien0446a0d2013-11-17 13:16:36 +0000217 for (int i = 0; i < 128 && i < len; i++) {
218 if (i > 0 && i % 16 == 0) {
219 DEBUG_printf("\n");
220 }
221 DEBUG_printf(" %02x", code[i]);
222 }
223 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000224#if MICROPY_DEBUG_PRINTERS
225 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000226#endif
Damien0446a0d2013-11-17 13:16:36 +0000227
228#ifdef WRITE_CODE
229 if (fp_write_code != NULL) {
230 fwrite(code, len, 1, fp_write_code);
231 fflush(fp_write_code);
232 }
233#endif
234#endif
Damien826005c2013-10-05 23:17:28 +0100235}
236
Damiend99b0522013-12-21 18:17:45 +0000237void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100238 alloc_unique_codes();
239
John R. Lenton9c83ec02014-01-07 23:06:46 +0000240 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 +0000241 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien429d7192013-10-04 19:53:11 +0100242 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100243 unique_codes[unique_code_id].n_locals = 0;
244 unique_codes[unique_code_id].n_stack = 0;
245 unique_codes[unique_code_id].is_generator = false;
Damien429d7192013-10-04 19:53:11 +0100246 unique_codes[unique_code_id].u_native.fun = fun;
247
Damien George8cc96a32013-12-30 18:23:50 +0000248 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000249
Damiena1ddfcc2013-10-10 23:25:50 +0100250#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100251 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
252 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
253 for (int i = 0; i < 128 && i < len; i++) {
254 if (i > 0 && i % 16 == 0) {
255 DEBUG_printf("\n");
256 }
257 DEBUG_printf(" %02x", fun_data[i]);
258 }
259 DEBUG_printf("\n");
260
Damien0446a0d2013-11-17 13:16:36 +0000261#ifdef WRITE_CODE
262 if (fp_write_code != NULL) {
263 fwrite(fun_data, len, 1, fp_write_code);
264 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100265 }
Damiena1ddfcc2013-10-10 23:25:50 +0100266#endif
267#endif
Damien429d7192013-10-04 19:53:11 +0100268}
269
Damiend99b0522013-12-21 18:17:45 +0000270void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100271 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100272
John R. Lenton9c83ec02014-01-07 23:06:46 +0000273 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 +0000274 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien826005c2013-10-05 23:17:28 +0100275 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100276 unique_codes[unique_code_id].n_locals = 0;
277 unique_codes[unique_code_id].n_stack = 0;
278 unique_codes[unique_code_id].is_generator = false;
Damien826005c2013-10-05 23:17:28 +0100279 unique_codes[unique_code_id].u_inline_asm.fun = fun;
280
Damiena1ddfcc2013-10-10 23:25:50 +0100281#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100282 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
283 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
284 for (int i = 0; i < 128 && i < len; i++) {
285 if (i > 0 && i % 16 == 0) {
286 DEBUG_printf("\n");
287 }
288 DEBUG_printf(" %02x", fun_data[i]);
289 }
290 DEBUG_printf("\n");
291
Damien0446a0d2013-11-17 13:16:36 +0000292#ifdef WRITE_CODE
293 if (fp_write_code != NULL) {
294 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100295 }
Damiena1ddfcc2013-10-10 23:25:50 +0100296#endif
297#endif
Damien429d7192013-10-04 19:53:11 +0100298}
299
Damiend99b0522013-12-21 18:17:45 +0000300int rt_is_true(mp_obj_t arg) {
301 DEBUG_OP_printf("is true %p\n", arg);
302 if (MP_OBJ_IS_SMALL_INT(arg)) {
303 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
304 return 0;
305 } else {
306 return 1;
307 }
308 } else if (arg == mp_const_none) {
309 return 0;
310 } else if (arg == mp_const_false) {
311 return 0;
312 } else if (arg == mp_const_true) {
313 return 1;
Paul Sokolovsky10744dd2014-01-16 23:54:17 +0200314 } else if (MP_OBJ_IS_QSTR(arg)) {
315 // TODO: \0
316 return *qstr_str(MP_OBJ_QSTR_VALUE(arg)) != 0;
317 } else if (MP_OBJ_IS_TYPE(arg, &str_type)) {
318 // TODO: \0
319 return *qstr_str(mp_obj_str_get(arg)) != 0;
320 } else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
321 uint len;
322 mp_obj_t *dummy;
323 mp_obj_list_get(arg, &len, &dummy);
324 return len != 0;
325 } else if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
326 uint len;
327 mp_obj_t *dummy;
328 mp_obj_tuple_get(arg, &len, &dummy);
329 return len != 0;
330 } else if (MP_OBJ_IS_TYPE(arg, &dict_type)) {
331 return mp_obj_dict_len(arg) != 0;
Damiend99b0522013-12-21 18:17:45 +0000332 } else {
333 assert(0);
334 return 0;
335 }
336}
337
338mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
339 return mp_obj_list_append(self_in, arg);
340}
341
Damien7410e442013-11-02 19:47:57 +0000342#define PARSE_DEC_IN_INTG (1)
343#define PARSE_DEC_IN_FRAC (2)
344#define PARSE_DEC_IN_EXP (3)
345
Damiend99b0522013-12-21 18:17:45 +0000346mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000347#if MICROPY_ENABLE_FLOAT
348 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
349 const char *s = qstr_str(qstr);
350 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000351 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000352 bool exp_neg = false;
353 int exp_val = 0;
354 int exp_extra = 0;
355 bool imag = false;
356 for (; *s; s++) {
357 int dig = *s;
358 if ('0' <= dig && dig <= '9') {
359 dig -= '0';
360 if (in == PARSE_DEC_IN_EXP) {
361 exp_val = 10 * exp_val + dig;
362 } else {
363 dec_val = 10 * dec_val + dig;
364 if (in == PARSE_DEC_IN_FRAC) {
365 exp_extra -= 1;
366 }
367 }
368 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
369 in = PARSE_DEC_IN_FRAC;
370 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
371 in = PARSE_DEC_IN_EXP;
372 if (s[1] == '+') {
373 s++;
374 } else if (s[1] == '-') {
375 s++;
376 exp_neg = true;
377 }
378 } else if (dig == 'J' || dig == 'j') {
379 s++;
380 imag = true;
381 break;
382 } else {
383 // unknown character
384 break;
385 }
386 }
387 if (*s != 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000388 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000389 }
390 if (exp_neg) {
391 exp_val = -exp_val;
392 }
393 exp_val += exp_extra;
394 for (; exp_val > 0; exp_val--) {
395 dec_val *= 10;
396 }
397 for (; exp_val < 0; exp_val++) {
398 dec_val *= 0.1;
399 }
400 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000401 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000402 } else {
Damiend99b0522013-12-21 18:17:45 +0000403 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000404 }
405#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000406 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000407#endif
408}
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));
Damiend99b0522013-12-21 18:17:45 +0000412 return mp_obj_new_str(qstr);
Damien429d7192013-10-04 19:53:11 +0100413}
414
Damiend99b0522013-12-21 18:17:45 +0000415mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100416 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100417 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000418 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100419 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000420 elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100421 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000422 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100423 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000424 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damiena3977762013-10-09 23:10:10 +0100425 }
426 }
427 }
428 return elem->value;
429}
430
Damiend99b0522013-12-21 18:17:45 +0000431mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100432 // logic: search globals, builtins
433 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000434 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100435 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000436 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100437 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000438 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100439 }
440 }
441 return elem->value;
442}
443
Damiend99b0522013-12-21 18:17:45 +0000444mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100445 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000446 mp_map_elem_t *elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100447 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000448 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
Damien429d7192013-10-04 19:53:11 +0100449 }
450 return elem->value;
451}
452
Damiend99b0522013-12-21 18:17:45 +0000453mp_obj_t rt_get_cell(mp_obj_t cell) {
454 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000455}
456
Damiend99b0522013-12-21 18:17:45 +0000457void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
458 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000459}
460
Damiend99b0522013-12-21 18:17:45 +0000461void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100462 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000463 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 +0100464}
465
Damiend99b0522013-12-21 18:17:45 +0000466void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100467 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000468 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 +0100469}
470
Damiend99b0522013-12-21 18:17:45 +0000471mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000472 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000473 if (MP_OBJ_IS_SMALL_INT(arg)) {
474 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000475 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000476 case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
Damien7410e442013-11-02 19:47:57 +0000477 case RT_UNARY_OP_POSITIVE: break;
478 case RT_UNARY_OP_NEGATIVE: val = -val; break;
479 case RT_UNARY_OP_INVERT: val = ~val; break;
480 default: assert(0); val = 0;
481 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200482 if (MP_OBJ_FITS_SMALL_INT(val)) {
Damiend99b0522013-12-21 18:17:45 +0000483 return MP_OBJ_NEW_SMALL_INT(val);
Damien7410e442013-11-02 19:47:57 +0000484 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200485 return mp_obj_new_int(val);
Damiend99b0522013-12-21 18:17:45 +0000486 } else { // will be an object (small ints are caught in previous if)
487 mp_obj_base_t *o = arg;
488 if (o->type->unary_op != NULL) {
489 mp_obj_t result = o->type->unary_op(op, arg);
490 if (result != NULL) {
491 return result;
492 }
Damien7410e442013-11-02 19:47:57 +0000493 }
Damiend99b0522013-12-21 18:17:45 +0000494 // TODO specify in error message what the operator is
Damien George6c73ca12014-01-08 18:11:23 +0000495 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", o->type->name));
Damien7410e442013-11-02 19:47:57 +0000496 }
Damien429d7192013-10-04 19:53:11 +0100497}
498
Damiend99b0522013-12-21 18:17:45 +0000499mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100500 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000501
502 // TODO correctly distinguish inplace operators for mutable objects
503 // lookup logic that CPython uses for +=:
504 // check for implemented +=
505 // then check for implemented +
506 // then check for implemented seq.inplace_concat
507 // then check for implemented seq.concat
508 // then fail
509 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
510
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200511 // deal with is, is not
512 if (op == RT_COMPARE_OP_IS) {
513 // TODO: may need to handle strings specially, CPython appears to
514 // assume all strings are interned (so "is" == "==" for strings)
515 return MP_BOOL(lhs == rhs);
516 }
517 if (op == RT_COMPARE_OP_IS_NOT) {
518 // TODO: may need to handle strings specially, CPython appears to
519 // assume all strings are interned (so "is" == "==" for strings)
520 return MP_BOOL(lhs != rhs);
521 }
522
Damien Georgebcbeea02014-01-11 10:47:22 +0000523 // deal with == and != for all types
524 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
525 if (mp_obj_equal(lhs, rhs)) {
526 if (op == RT_COMPARE_OP_EQUAL) {
527 return mp_const_true;
528 } else {
529 return mp_const_false;
530 }
531 } else {
532 if (op == RT_COMPARE_OP_EQUAL) {
533 return mp_const_false;
534 } else {
535 return mp_const_true;
536 }
537 }
538 }
539
540 // deal with exception_match for all types
541 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
542 // TODO properly! at the moment it just compares the exception identifier for equality
543 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
544 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
545 return mp_const_true;
546 } else {
547 return mp_const_false;
548 }
549 }
550 }
551
Damien George1a9951d2014-01-06 22:13:00 +0000552 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000553 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000554 if (MP_OBJ_IS_SMALL_INT(rhs)) {
555 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
556 switch (op) {
557 case RT_BINARY_OP_OR:
558 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
559 case RT_BINARY_OP_XOR:
560 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
561 case RT_BINARY_OP_AND:
562 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
563 case RT_BINARY_OP_LSHIFT:
564 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
565 case RT_BINARY_OP_RSHIFT:
566 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
567 case RT_BINARY_OP_ADD:
568 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
569 case RT_BINARY_OP_SUBTRACT:
570 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
571 case RT_BINARY_OP_MULTIPLY:
572 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
573 case RT_BINARY_OP_FLOOR_DIVIDE:
574 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
575 #if MICROPY_ENABLE_FLOAT
576 case RT_BINARY_OP_TRUE_DIVIDE:
577 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
578 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000579
Damien George1a9951d2014-01-06 22:13:00 +0000580 // TODO implement modulo as specified by Python
581 case RT_BINARY_OP_MODULO:
582 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000583
Damien George1a9951d2014-01-06 22:13:00 +0000584 // TODO check for negative power, and overflow
585 case RT_BINARY_OP_POWER:
586 case RT_BINARY_OP_INPLACE_POWER:
587 {
588 int ans = 1;
589 while (rhs_val > 0) {
590 if (rhs_val & 1) {
591 ans *= lhs_val;
592 }
593 lhs_val *= lhs_val;
594 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000595 }
Damien George1a9951d2014-01-06 22:13:00 +0000596 lhs_val = ans;
597 break;
Damien4ebb32f2013-11-02 14:33:10 +0000598 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000599 case RT_COMPARE_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
600 case RT_COMPARE_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
601 case RT_COMPARE_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
602 case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000603
Damien George1a9951d2014-01-06 22:13:00 +0000604 default: assert(0);
605 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200606 // TODO: We just should make mp_obj_new_int() inline and use that
607 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000608 return MP_OBJ_NEW_SMALL_INT(lhs_val);
609 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200610 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000611 } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
612 return mp_obj_float_binary_op(op, lhs_val, rhs);
613 } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
614 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien429d7192013-10-04 19:53:11 +0100615 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000616 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000617
John R. Lentonc1bef212014-01-11 12:39:33 +0000618 /* deal with `in` and `not in`
619 *
620 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
621 * needs to go below
622 */
623 if (op == RT_COMPARE_OP_IN || op == RT_COMPARE_OP_NOT_IN) {
624 if (!MP_OBJ_IS_SMALL_INT(rhs)) {
625 mp_obj_base_t *o = rhs;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000626 if (o->type->binary_op != NULL) {
John R. Lentonc1bef212014-01-11 12:39:33 +0000627 mp_obj_t res = o->type->binary_op(op, rhs, lhs);
628 if (res != NULL) {
629 return res;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000630 }
Damien7410e442013-11-02 19:47:57 +0000631 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000632 if (o->type->getiter != NULL) {
633 /* second attempt, walk the iterator */
634 mp_obj_t next = NULL;
635 mp_obj_t iter = rt_getiter(rhs);
636 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
637 if (mp_obj_equal(next, lhs)) {
638 return MP_BOOL(op == RT_COMPARE_OP_IN);
639 }
640 }
641 return MP_BOOL(op != RT_COMPARE_OP_IN);
642 }
Damien7410e442013-11-02 19:47:57 +0000643 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000644
645 nlr_jump(mp_obj_new_exception_msg_varg(
646 MP_QSTR_TypeError, "'%s' object is not iterable",
647 mp_obj_get_type_str(rhs)));
648 return mp_const_none;
649 }
650
651 if (MP_OBJ_IS_OBJ(lhs)) {
652 mp_obj_base_t *o = lhs;
653 if (o->type->binary_op != NULL) {
654 mp_obj_t result = o->type->binary_op(op, lhs, rhs);
655 if (result != NULL) {
656 return result;
657 }
658 }
659 // TODO implement dispatch for reverse binary ops
Damien429d7192013-10-04 19:53:11 +0100660 }
Damiend99b0522013-12-21 18:17:45 +0000661
662 // TODO specify in error message what the operator is
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200663 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError,
664 "unsupported operand types for binary operator: '%s', '%s'",
665 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000666 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100667}
668
Damiend99b0522013-12-21 18:17:45 +0000669mp_obj_t rt_make_function_from_id(int unique_code_id) {
Damienb05d7072013-10-05 13:37:10 +0100670 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
671 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100672 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000673 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100674 }
Damiend99b0522013-12-21 18:17:45 +0000675
676 // make the function, depending on the code kind
677 mp_code_t *c = &unique_codes[unique_code_id];
678 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100679 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000680 case MP_CODE_BYTE:
Damien George6baf76e2013-12-30 22:32:17 +0000681 fun = mp_obj_new_fun_bc(c->n_args, c->n_locals + c->n_stack, c->u_byte.code);
Damien826005c2013-10-05 23:17:28 +0100682 break;
Damiend99b0522013-12-21 18:17:45 +0000683 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000684 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100685 break;
Damiend99b0522013-12-21 18:17:45 +0000686 case MP_CODE_INLINE_ASM:
687 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100688 break;
689 default:
690 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000691 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100692 }
Damienbd254452013-10-16 20:39:12 +0100693
694 // check for generator functions and if so wrap in generator object
695 if (c->is_generator) {
Damien George6baf76e2013-12-30 22:32:17 +0000696 fun = mp_obj_new_gen_wrap(c->n_locals, c->n_stack, fun);
Damienbd254452013-10-16 20:39:12 +0100697 }
698
Damiend99b0522013-12-21 18:17:45 +0000699 return fun;
Damien429d7192013-10-04 19:53:11 +0100700}
701
Damiend99b0522013-12-21 18:17:45 +0000702mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000703 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000704 // make function object
705 mp_obj_t ffun = rt_make_function_from_id(unique_code_id);
Damien9ecbcff2013-12-11 00:41:43 +0000706 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000707 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000708}
709
Damiend99b0522013-12-21 18:17:45 +0000710mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000711 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100712}
713
Damiend99b0522013-12-21 18:17:45 +0000714mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000715 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100716}
717
Damiend99b0522013-12-21 18:17:45 +0000718mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
719 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000720 args[0] = arg1;
721 args[1] = arg2;
722 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100723}
724
Damien George20006db2014-01-18 14:10:48 +0000725// args contains, eg: arg0 arg1 key0 value0 key1 value1
726mp_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 +0000727 // TODO improve this: fun object can specify its type and we parse here the arguments,
728 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100729
John R. Lenton9c83ec02014-01-07 23:06:46 +0000730 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
731
732 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
733 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
734 } else {
735 mp_obj_base_t *fun = fun_in;
Damien George20006db2014-01-18 14:10:48 +0000736 if (fun->type->call != NULL) {
737 return fun->type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000738 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000739 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000740 }
741 }
Damien86c7fc72013-11-26 15:16:41 +0000742}
743
Damien George20006db2014-01-18 14:10:48 +0000744// 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)
745// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000746mp_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 +0000747 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);
748 int adjust = (args[1] == NULL) ? 0 : 1;
749 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000750}
751
Damiend99b0522013-12-21 18:17:45 +0000752mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000753 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100754}
755
Damiend99b0522013-12-21 18:17:45 +0000756mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000757 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100758}
759
Damiend99b0522013-12-21 18:17:45 +0000760mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
761 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100762}
763
Damiend99b0522013-12-21 18:17:45 +0000764mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000765 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100766 return set;
767}
768
Damien George932bf1c2014-01-18 23:42:49 +0000769// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000770void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
771 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
772 uint seq_len;
773 mp_obj_t *seq_items;
774 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
775 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
776 } else {
777 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000778 }
Damiend99b0522013-12-21 18:17:45 +0000779 if (seq_len < num) {
Damien George6c73ca12014-01-08 18:11:23 +0000780 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ValueError, "need more than %d values to unpack", (void*)(machine_uint_t)seq_len));
Damiend99b0522013-12-21 18:17:45 +0000781 } else if (seq_len > num) {
Damien George6c73ca12014-01-08 18:11:23 +0000782 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_ValueError, "too many values to unpack (expected %d)", (void*)(machine_uint_t)num));
Damiend99b0522013-12-21 18:17:45 +0000783 }
Damien George932bf1c2014-01-18 23:42:49 +0000784 for (uint i = 0; i < num; i++) {
785 items[i] = seq_items[num - 1 - i];
786 }
Damien86c7fc72013-11-26 15:16:41 +0000787 } else {
788 // TODO call rt_getiter and extract via rt_iternext
Damien George6c73ca12014-01-08 18:11:23 +0000789 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(seq_in)));
Damien86c7fc72013-11-26 15:16:41 +0000790 }
791}
792
Damiend99b0522013-12-21 18:17:45 +0000793mp_obj_t rt_build_map(int n_args) {
794 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100795}
796
Damiend99b0522013-12-21 18:17:45 +0000797mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
798 // map should always be a dict
799 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100800}
801
Damiend99b0522013-12-21 18:17:45 +0000802mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000803 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
804 // use load_method
805 mp_obj_t dest[2];
806 rt_load_method(base, attr, dest);
Damien George20006db2014-01-18 14:10:48 +0000807 if (dest[1] == NULL) {
Damien George062478e2014-01-09 20:57:50 +0000808 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000809 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000810 } else {
811 // load_method returned a method, so build a bound method object
812 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000813 }
Damiend99b0522013-12-21 18:17:45 +0000814}
815
816void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000817 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
818
819 // clear output to indicate no attribute/method found yet
820 dest[0] = MP_OBJ_NULL;
821 dest[1] = MP_OBJ_NULL;
822
823 // get the type
824 mp_obj_type_t *type = mp_obj_get_type(base);
825
826 // if this type can do its own load, then call it
827 if (type->load_attr != NULL) {
828 type->load_attr(base, attr, dest);
829 }
830
831 // if nothing found yet, look for built-in and generic names
Damien George20006db2014-01-18 14:10:48 +0000832 if (dest[0] == NULL) {
Damien George062478e2014-01-09 20:57:50 +0000833 if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000834 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
835 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000836 } else if (type->load_attr == NULL) {
837 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000838 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000839 const mp_method_t *meth = type->methods;
840 if (meth != NULL) {
841 for (; meth->name != NULL; meth++) {
842 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000843 // check if the methods are functions, static or class methods
844 // see http://docs.python.org/3.3/howto/descriptor.html
845 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
846 // return just the function
Damien George20006db2014-01-18 14:10:48 +0000847 dest[0] = ((mp_obj_staticmethod_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000848 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
849 // return a bound method, with self being the type of this object
Damien George20006db2014-01-18 14:10:48 +0000850 dest[0] = ((mp_obj_classmethod_t*)meth->fun)->fun;
851 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000852 } else {
853 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000854 dest[0] = (mp_obj_t)meth->fun;
855 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000856 }
Damien George062478e2014-01-09 20:57:50 +0000857 break;
858 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000859 }
Damiend57eba52013-11-02 23:58:14 +0000860 }
861 }
Damiena3977762013-10-09 23:10:10 +0100862 }
863
Damien George20006db2014-01-18 14:10:48 +0000864 if (dest[0] == NULL) {
Damien George062478e2014-01-09 20:57:50 +0000865 // no attribute/method called attr
866 // following CPython, we give a more detailed error message for type objects
867 if (MP_OBJ_IS_TYPE(base, &mp_const_type)) {
868 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_AttributeError, "type object '%s' has no attribute '%s'", ((mp_obj_type_t*)base)->name, qstr_str(attr)));
869 } else {
870 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
871 }
872 }
Damiena3977762013-10-09 23:10:10 +0100873}
874
Damiend99b0522013-12-21 18:17:45 +0000875void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100876 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000877 mp_obj_type_t *type = mp_obj_get_type(base);
878 if (type->store_attr != NULL) {
879 if (type->store_attr(base, attr, value)) {
880 return;
881 }
Damiena3977762013-10-09 23:10:10 +0100882 }
Damien George062478e2014-01-09 20:57:50 +0000883 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100884}
885
Damiend99b0522013-12-21 18:17:45 +0000886void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100887 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000888 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100889 // list store
Damiend99b0522013-12-21 18:17:45 +0000890 mp_obj_list_store(base, index, value);
891 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
892 // dict store
893 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100894 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200895 mp_obj_type_t *type = mp_obj_get_type(base);
896 if (type->store_item != NULL) {
897 bool r = type->store_item(base, index, value);
898 if (r) {
899 return;
900 }
901 // TODO: call base classes here?
902 }
903 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
Damien429d7192013-10-04 19:53:11 +0100904 }
905}
906
Damiend99b0522013-12-21 18:17:45 +0000907mp_obj_t rt_getiter(mp_obj_t o_in) {
908 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000909 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100910 } else {
Damiend99b0522013-12-21 18:17:45 +0000911 mp_obj_base_t *o = o_in;
912 if (o->type->getiter != NULL) {
913 return o->type->getiter(o_in);
914 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000915 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not iterable", o->type->name));
Damiend99b0522013-12-21 18:17:45 +0000916 }
Damience89a212013-10-15 22:25:17 +0100917 }
918}
919
Damiend99b0522013-12-21 18:17:45 +0000920mp_obj_t rt_iternext(mp_obj_t o_in) {
921 if (MP_OBJ_IS_SMALL_INT(o_in)) {
John R. Lenton88f30432014-01-06 22:58:17 +0000922 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not an iterator"));
Damience89a212013-10-15 22:25:17 +0100923 } else {
Damiend99b0522013-12-21 18:17:45 +0000924 mp_obj_base_t *o = o_in;
925 if (o->type->iternext != NULL) {
926 return o->type->iternext(o_in);
927 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000928 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not an iterator", o->type->name));
Damiend99b0522013-12-21 18:17:45 +0000929 }
Damience89a212013-10-15 22:25:17 +0100930 }
931}
932
Damiend99b0522013-12-21 18:17:45 +0000933mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000934 // build args array
Damiend99b0522013-12-21 18:17:45 +0000935 mp_obj_t args[5];
936 args[0] = mp_obj_new_str(name);
937 args[1] = mp_const_none; // TODO should be globals
938 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000939 args[3] = fromlist;
940 args[4] = level; // must be 0; we don't yet support other values
941
942 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000943 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000944}
945
Damiend99b0522013-12-21 18:17:45 +0000946mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
947 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000948 /* TODO convert AttributeError to ImportError
949 if (fail) {
950 (ImportError, "cannot import name %s", qstr_str(name), NULL)
951 }
952 */
953 return x;
954}
955
Damien George66028ab2014-01-03 14:03:48 +0000956mp_map_t *rt_locals_get(void) {
957 return map_locals;
958}
959
960void rt_locals_set(mp_map_t *m) {
961 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
962 map_locals = m;
963}
964
965mp_map_t *rt_globals_get(void) {
966 return map_globals;
967}
968
969void rt_globals_set(mp_map_t *m) {
970 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
971 map_globals = m;
972}
973
Damien6ba13142013-11-02 20:34:54 +0000974// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100975void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000976 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100977 rt_load_const_str,
978 rt_load_name,
979 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100980 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +0100981 rt_load_attr,
982 rt_load_method,
983 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +0100984 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +0100985 rt_store_subscr,
986 rt_is_true,
987 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +0100988 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +0100989 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +0100990 rt_list_append,
Damien429d7192013-10-04 19:53:11 +0100991 rt_build_map,
992 rt_store_map,
993 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +0100994 rt_store_set,
Damien429d7192013-10-04 19:53:11 +0100995 rt_make_function_from_id,
Damien George20006db2014-01-18 14:10:48 +0000996 rt_call_function_n_kw,
997 rt_call_method_n_kw,
Damien429d7192013-10-04 19:53:11 +0100998 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +0100999 rt_getiter,
1000 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001001};
1002
1003/*
1004void rt_f_vector(rt_fun_kind_t fun_kind) {
1005 (rt_f_table[fun_kind])();
1006}
1007*/