blob: b2d9c7e9609dfb48deaf7e766383e04d47751061 [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"
20
Damien7f5dacf2013-10-10 11:24:39 +010021#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010022#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000023#define WRITE_CODE (1)
Damien7f5dacf2013-10-10 11:24:39 +010024#define DEBUG_printf(args...) printf(args)
25#define DEBUG_OP_printf(args...) printf(args)
26#else // don't print debugging info
Damiena3977762013-10-09 23:10:10 +010027#define DEBUG_printf(args...) (void)0
Damien429d7192013-10-04 19:53:11 +010028#define DEBUG_OP_printf(args...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010029#endif
Damien429d7192013-10-04 19:53:11 +010030
Damieneb19efb2013-10-10 22:06:54 +010031// locals and globals need to be pointers because they can be the same in outer module scope
Damiend99b0522013-12-21 18:17:45 +000032static mp_map_t *map_locals;
33static mp_map_t *map_globals;
34static mp_map_t map_builtins;
Damienbd254452013-10-16 20:39:12 +010035
Damien429d7192013-10-04 19:53:11 +010036typedef enum {
Damiend99b0522013-12-21 18:17:45 +000037 MP_CODE_NONE,
38 MP_CODE_BYTE,
39 MP_CODE_NATIVE,
40 MP_CODE_INLINE_ASM,
41} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010042
Damiend99b0522013-12-21 18:17:45 +000043typedef struct _mp_code_t {
44 mp_code_kind_t kind;
Damien429d7192013-10-04 19:53:11 +010045 int n_args;
Damienbd254452013-10-16 20:39:12 +010046 int n_locals;
47 int n_stack;
48 bool is_generator;
Damien429d7192013-10-04 19:53:11 +010049 union {
50 struct {
Damien429d7192013-10-04 19:53:11 +010051 byte *code;
52 uint len;
53 } u_byte;
Damien826005c2013-10-05 23:17:28 +010054 struct {
Damiend99b0522013-12-21 18:17:45 +000055 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010056 } u_native;
57 struct {
Damiene4af64f2013-10-06 12:04:13 +010058 void *fun;
Damien826005c2013-10-05 23:17:28 +010059 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010060 };
Damiend99b0522013-12-21 18:17:45 +000061} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010062
63static int next_unique_code_id;
John R. Lenton9c83ec02014-01-07 23:06:46 +000064static machine_uint_t unique_codes_alloc = 0;
65static mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010066
Damien0446a0d2013-11-17 13:16:36 +000067#ifdef WRITE_CODE
68FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010069#endif
Damien429d7192013-10-04 19:53:11 +010070
Damien George38a2da62014-01-08 17:33:12 +000071// a good optimising compiler will inline this if necessary
72static void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
73 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
74}
75
Damien8b3a7c22013-10-23 20:20:17 +010076void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +010077 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +000078 map_locals = map_globals = mp_map_new(1);
79 mp_map_add_qstr(map_globals, MP_QSTR___name__, mp_obj_new_str(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +010080
Damienb86e3f92013-12-29 17:17:43 +000081 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +000082 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +000083
84 // built-in exceptions (TODO, make these proper classes)
Damien George38a2da62014-01-08 17:33:12 +000085 mp_map_add_qstr(&map_builtins, MP_QSTR_AttributeError, mp_obj_new_exception(MP_QSTR_AttributeError));
86 mp_map_add_qstr(&map_builtins, MP_QSTR_IndexError, mp_obj_new_exception(MP_QSTR_IndexError));
87 mp_map_add_qstr(&map_builtins, MP_QSTR_KeyError, mp_obj_new_exception(MP_QSTR_KeyError));
88 mp_map_add_qstr(&map_builtins, MP_QSTR_NameError, mp_obj_new_exception(MP_QSTR_NameError));
89 mp_map_add_qstr(&map_builtins, MP_QSTR_TypeError, mp_obj_new_exception(MP_QSTR_TypeError));
90 mp_map_add_qstr(&map_builtins, MP_QSTR_SyntaxError, mp_obj_new_exception(MP_QSTR_SyntaxError));
91 mp_map_add_qstr(&map_builtins, MP_QSTR_ValueError, mp_obj_new_exception(MP_QSTR_ValueError));
92 mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
Paul Sokolovskyb81e1fd2014-01-11 16:33:32 +020093 mp_map_add_qstr(&map_builtins, MP_QSTR_AssertionError, mp_obj_new_exception(MP_QSTR_AssertionError));
Damienb86e3f92013-12-29 17:17:43 +000094
Damien Georgee9906ac2014-01-04 18:44:46 +000095 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +000096 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +000097
Damienb86e3f92013-12-29 17:17:43 +000098 // built-in core functions
Damien George38a2da62014-01-08 17:33:12 +000099 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 +0000100 mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj);
Damienb86e3f92013-12-29 17:17:43 +0000101
Damien George71c51812014-01-04 20:21:15 +0000102 // built-in types
Damien George38a2da62014-01-08 17:33:12 +0000103 mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
Damien George71c51812014-01-04 20:21:15 +0000104#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000105 mp_map_add_qstr(&map_builtins, MP_QSTR_complex, (mp_obj_t)&complex_type);
Damien George71c51812014-01-04 20:21:15 +0000106#endif
Damien George38a2da62014-01-08 17:33:12 +0000107 mp_map_add_qstr(&map_builtins, MP_QSTR_dict, (mp_obj_t)&dict_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_float, (mp_obj_t)&float_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_int, (mp_obj_t)&int_type);
112 mp_map_add_qstr(&map_builtins, MP_QSTR_list, (mp_obj_t)&list_type);
113 mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type);
114 mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
Damien George93a9b5b2014-01-08 18:48:12 +0000115 mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
Damien George71c51812014-01-04 20:21:15 +0000116
Damien George23005372014-01-13 19:39:01 +0000117 // built-in user functions
118 mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj);
119 mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj);
120 mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj);
121 mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
122 mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
123 mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
Damien George38a2da62014-01-08 17:33:12 +0000124 mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
Damien George004cdce2014-01-09 21:43:51 +0000125 mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
126 mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);
Damien George38a2da62014-01-08 17:33:12 +0000127 mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj);
Damien George23005372014-01-13 19:39:01 +0000128 mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj);
129 mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj);
130 mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj);
Damien George38a2da62014-01-08 17:33:12 +0000131 mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj);
Damien George23005372014-01-13 19:39:01 +0000132 mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj);
133 mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj);
134 mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj);
135 mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj);
136 mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
Damien429d7192013-10-04 19:53:11 +0100137
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000138 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000139 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100140 unique_codes = NULL;
141
Damien0446a0d2013-11-17 13:16:36 +0000142#ifdef WRITE_CODE
143 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100144#endif
Damien429d7192013-10-04 19:53:11 +0100145}
146
Damien8b3a7c22013-10-23 20:20:17 +0100147void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000148 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Damien0446a0d2013-11-17 13:16:36 +0000149#ifdef WRITE_CODE
150 if (fp_write_code != NULL) {
151 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100152 }
Damiena1ddfcc2013-10-10 23:25:50 +0100153#endif
Damien429d7192013-10-04 19:53:11 +0100154}
155
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000156int rt_get_unique_code_id(void) {
157 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100158}
159
Damien8b3a7c22013-10-23 20:20:17 +0100160static void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000161 if (next_unique_code_id > unique_codes_alloc) {
162 // increase size of unique_codes table
163 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
164 for (int i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000165 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100166 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000167 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100168 }
Damien826005c2013-10-05 23:17:28 +0100169}
170
Damien George6baf76e2013-12-30 22:32:17 +0000171void 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 +0100172 alloc_unique_codes();
173
John R. Lenton9c83ec02014-01-07 23:06:46 +0000174 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 +0000175 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien826005c2013-10-05 23:17:28 +0100176 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100177 unique_codes[unique_code_id].n_locals = n_locals;
178 unique_codes[unique_code_id].n_stack = n_stack;
179 unique_codes[unique_code_id].is_generator = is_generator;
Damien826005c2013-10-05 23:17:28 +0100180 unique_codes[unique_code_id].u_byte.code = code;
181 unique_codes[unique_code_id].u_byte.len = len;
182
Damien9ecbcff2013-12-11 00:41:43 +0000183 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000184
185#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100186 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 +0000187 for (int i = 0; i < 128 && i < len; i++) {
188 if (i > 0 && i % 16 == 0) {
189 DEBUG_printf("\n");
190 }
191 DEBUG_printf(" %02x", code[i]);
192 }
193 DEBUG_printf("\n");
Damien George062478e2014-01-09 20:57:50 +0000194#if MICROPY_SHOW_BC
Damiend99b0522013-12-21 18:17:45 +0000195 extern void mp_show_byte_code(const byte *code, int len);
196 mp_show_byte_code(code, len);
Damien George062478e2014-01-09 20:57:50 +0000197#endif
Damien0446a0d2013-11-17 13:16:36 +0000198
199#ifdef WRITE_CODE
200 if (fp_write_code != NULL) {
201 fwrite(code, len, 1, fp_write_code);
202 fflush(fp_write_code);
203 }
204#endif
205#endif
Damien826005c2013-10-05 23:17:28 +0100206}
207
Damiend99b0522013-12-21 18:17:45 +0000208void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100209 alloc_unique_codes();
210
John R. Lenton9c83ec02014-01-07 23:06:46 +0000211 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 +0000212 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien429d7192013-10-04 19:53:11 +0100213 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100214 unique_codes[unique_code_id].n_locals = 0;
215 unique_codes[unique_code_id].n_stack = 0;
216 unique_codes[unique_code_id].is_generator = false;
Damien429d7192013-10-04 19:53:11 +0100217 unique_codes[unique_code_id].u_native.fun = fun;
218
Damien George8cc96a32013-12-30 18:23:50 +0000219 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000220
Damiena1ddfcc2013-10-10 23:25:50 +0100221#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100222 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
223 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
224 for (int i = 0; i < 128 && i < len; i++) {
225 if (i > 0 && i % 16 == 0) {
226 DEBUG_printf("\n");
227 }
228 DEBUG_printf(" %02x", fun_data[i]);
229 }
230 DEBUG_printf("\n");
231
Damien0446a0d2013-11-17 13:16:36 +0000232#ifdef WRITE_CODE
233 if (fp_write_code != NULL) {
234 fwrite(fun_data, len, 1, fp_write_code);
235 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100236 }
Damiena1ddfcc2013-10-10 23:25:50 +0100237#endif
238#endif
Damien429d7192013-10-04 19:53:11 +0100239}
240
Damiend99b0522013-12-21 18:17:45 +0000241void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100242 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100243
John R. Lenton9c83ec02014-01-07 23:06:46 +0000244 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 +0000245 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien826005c2013-10-05 23:17:28 +0100246 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100247 unique_codes[unique_code_id].n_locals = 0;
248 unique_codes[unique_code_id].n_stack = 0;
249 unique_codes[unique_code_id].is_generator = false;
Damien826005c2013-10-05 23:17:28 +0100250 unique_codes[unique_code_id].u_inline_asm.fun = fun;
251
Damiena1ddfcc2013-10-10 23:25:50 +0100252#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100253 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
254 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
255 for (int i = 0; i < 128 && i < len; i++) {
256 if (i > 0 && i % 16 == 0) {
257 DEBUG_printf("\n");
258 }
259 DEBUG_printf(" %02x", fun_data[i]);
260 }
261 DEBUG_printf("\n");
262
Damien0446a0d2013-11-17 13:16:36 +0000263#ifdef WRITE_CODE
264 if (fp_write_code != NULL) {
265 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100266 }
Damiena1ddfcc2013-10-10 23:25:50 +0100267#endif
268#endif
Damien429d7192013-10-04 19:53:11 +0100269}
270
Damiend99b0522013-12-21 18:17:45 +0000271int rt_is_true(mp_obj_t arg) {
272 DEBUG_OP_printf("is true %p\n", arg);
273 if (MP_OBJ_IS_SMALL_INT(arg)) {
274 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
275 return 0;
276 } else {
277 return 1;
278 }
279 } else if (arg == mp_const_none) {
280 return 0;
281 } else if (arg == mp_const_false) {
282 return 0;
283 } else if (arg == mp_const_true) {
284 return 1;
285 } else {
286 assert(0);
287 return 0;
288 }
289}
290
291mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
292 return mp_obj_list_append(self_in, arg);
293}
294
Damien7410e442013-11-02 19:47:57 +0000295#define PARSE_DEC_IN_INTG (1)
296#define PARSE_DEC_IN_FRAC (2)
297#define PARSE_DEC_IN_EXP (3)
298
Damiend99b0522013-12-21 18:17:45 +0000299mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000300#if MICROPY_ENABLE_FLOAT
301 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
302 const char *s = qstr_str(qstr);
303 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000304 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000305 bool exp_neg = false;
306 int exp_val = 0;
307 int exp_extra = 0;
308 bool imag = false;
309 for (; *s; s++) {
310 int dig = *s;
311 if ('0' <= dig && dig <= '9') {
312 dig -= '0';
313 if (in == PARSE_DEC_IN_EXP) {
314 exp_val = 10 * exp_val + dig;
315 } else {
316 dec_val = 10 * dec_val + dig;
317 if (in == PARSE_DEC_IN_FRAC) {
318 exp_extra -= 1;
319 }
320 }
321 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
322 in = PARSE_DEC_IN_FRAC;
323 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
324 in = PARSE_DEC_IN_EXP;
325 if (s[1] == '+') {
326 s++;
327 } else if (s[1] == '-') {
328 s++;
329 exp_neg = true;
330 }
331 } else if (dig == 'J' || dig == 'j') {
332 s++;
333 imag = true;
334 break;
335 } else {
336 // unknown character
337 break;
338 }
339 }
340 if (*s != 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000341 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000342 }
343 if (exp_neg) {
344 exp_val = -exp_val;
345 }
346 exp_val += exp_extra;
347 for (; exp_val > 0; exp_val--) {
348 dec_val *= 10;
349 }
350 for (; exp_val < 0; exp_val++) {
351 dec_val *= 0.1;
352 }
353 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000354 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000355 } else {
Damiend99b0522013-12-21 18:17:45 +0000356 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000357 }
358#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000359 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000360#endif
361}
362
Damiend99b0522013-12-21 18:17:45 +0000363mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100364 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000365 return mp_obj_new_str(qstr);
Damien429d7192013-10-04 19:53:11 +0100366}
367
Damiend99b0522013-12-21 18:17:45 +0000368mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100369 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100370 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000371 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100372 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000373 elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100374 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000375 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100376 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000377 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 +0100378 }
379 }
380 }
381 return elem->value;
382}
383
Damiend99b0522013-12-21 18:17:45 +0000384mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100385 // logic: search globals, builtins
386 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000387 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100388 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000389 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100390 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000391 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 +0100392 }
393 }
394 return elem->value;
395}
396
Damiend99b0522013-12-21 18:17:45 +0000397mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100398 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000399 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 +0100400 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000401 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
Damien429d7192013-10-04 19:53:11 +0100402 }
403 return elem->value;
404}
405
Damiend99b0522013-12-21 18:17:45 +0000406mp_obj_t rt_get_cell(mp_obj_t cell) {
407 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000408}
409
Damiend99b0522013-12-21 18:17:45 +0000410void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
411 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000412}
413
Damiend99b0522013-12-21 18:17:45 +0000414void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100415 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000416 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 +0100417}
418
Damiend99b0522013-12-21 18:17:45 +0000419void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100420 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000421 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 +0100422}
423
Damiend99b0522013-12-21 18:17:45 +0000424mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000425 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000426 if (MP_OBJ_IS_SMALL_INT(arg)) {
427 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000428 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000429 case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
Damien7410e442013-11-02 19:47:57 +0000430 case RT_UNARY_OP_POSITIVE: break;
431 case RT_UNARY_OP_NEGATIVE: val = -val; break;
432 case RT_UNARY_OP_INVERT: val = ~val; break;
433 default: assert(0); val = 0;
434 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200435 if (MP_OBJ_FITS_SMALL_INT(val)) {
Damiend99b0522013-12-21 18:17:45 +0000436 return MP_OBJ_NEW_SMALL_INT(val);
Damien7410e442013-11-02 19:47:57 +0000437 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200438 return mp_obj_new_int(val);
Damiend99b0522013-12-21 18:17:45 +0000439 } else { // will be an object (small ints are caught in previous if)
440 mp_obj_base_t *o = arg;
441 if (o->type->unary_op != NULL) {
442 mp_obj_t result = o->type->unary_op(op, arg);
443 if (result != NULL) {
444 return result;
445 }
Damien7410e442013-11-02 19:47:57 +0000446 }
Damiend99b0522013-12-21 18:17:45 +0000447 // TODO specify in error message what the operator is
Damien George6c73ca12014-01-08 18:11:23 +0000448 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 +0000449 }
Damien429d7192013-10-04 19:53:11 +0100450}
451
Damiend99b0522013-12-21 18:17:45 +0000452mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100453 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000454
455 // TODO correctly distinguish inplace operators for mutable objects
456 // lookup logic that CPython uses for +=:
457 // check for implemented +=
458 // then check for implemented +
459 // then check for implemented seq.inplace_concat
460 // then check for implemented seq.concat
461 // then fail
462 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
463
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200464 // deal with is, is not
465 if (op == RT_COMPARE_OP_IS) {
466 // TODO: may need to handle strings specially, CPython appears to
467 // assume all strings are interned (so "is" == "==" for strings)
468 return MP_BOOL(lhs == rhs);
469 }
470 if (op == RT_COMPARE_OP_IS_NOT) {
471 // TODO: may need to handle strings specially, CPython appears to
472 // assume all strings are interned (so "is" == "==" for strings)
473 return MP_BOOL(lhs != rhs);
474 }
475
Damien Georgebcbeea02014-01-11 10:47:22 +0000476 // deal with == and != for all types
477 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
478 if (mp_obj_equal(lhs, rhs)) {
479 if (op == RT_COMPARE_OP_EQUAL) {
480 return mp_const_true;
481 } else {
482 return mp_const_false;
483 }
484 } else {
485 if (op == RT_COMPARE_OP_EQUAL) {
486 return mp_const_false;
487 } else {
488 return mp_const_true;
489 }
490 }
491 }
492
493 // deal with exception_match for all types
494 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
495 // TODO properly! at the moment it just compares the exception identifier for equality
496 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
497 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
498 return mp_const_true;
499 } else {
500 return mp_const_false;
501 }
502 }
503 }
504
Damien George1a9951d2014-01-06 22:13:00 +0000505 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000506 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000507 if (MP_OBJ_IS_SMALL_INT(rhs)) {
508 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
509 switch (op) {
510 case RT_BINARY_OP_OR:
511 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
512 case RT_BINARY_OP_XOR:
513 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
514 case RT_BINARY_OP_AND:
515 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
516 case RT_BINARY_OP_LSHIFT:
517 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
518 case RT_BINARY_OP_RSHIFT:
519 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
520 case RT_BINARY_OP_ADD:
521 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
522 case RT_BINARY_OP_SUBTRACT:
523 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
524 case RT_BINARY_OP_MULTIPLY:
525 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
526 case RT_BINARY_OP_FLOOR_DIVIDE:
527 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
528 #if MICROPY_ENABLE_FLOAT
529 case RT_BINARY_OP_TRUE_DIVIDE:
530 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
531 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000532
Damien George1a9951d2014-01-06 22:13:00 +0000533 // TODO implement modulo as specified by Python
534 case RT_BINARY_OP_MODULO:
535 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000536
Damien George1a9951d2014-01-06 22:13:00 +0000537 // TODO check for negative power, and overflow
538 case RT_BINARY_OP_POWER:
539 case RT_BINARY_OP_INPLACE_POWER:
540 {
541 int ans = 1;
542 while (rhs_val > 0) {
543 if (rhs_val & 1) {
544 ans *= lhs_val;
545 }
546 lhs_val *= lhs_val;
547 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000548 }
Damien George1a9951d2014-01-06 22:13:00 +0000549 lhs_val = ans;
550 break;
Damien4ebb32f2013-11-02 14:33:10 +0000551 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000552 case RT_COMPARE_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
553 case RT_COMPARE_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
554 case RT_COMPARE_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
555 case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000556
Damien George1a9951d2014-01-06 22:13:00 +0000557 default: assert(0);
558 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200559 // TODO: We just should make mp_obj_new_int() inline and use that
560 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000561 return MP_OBJ_NEW_SMALL_INT(lhs_val);
562 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200563 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000564 } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
565 return mp_obj_float_binary_op(op, lhs_val, rhs);
566 } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
567 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien429d7192013-10-04 19:53:11 +0100568 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000569 } else {
John R. Lentonb8698fc2014-01-11 00:58:59 +0000570 if (MP_OBJ_IS_OBJ(lhs)) {
571 mp_obj_base_t *o = lhs;
572 if (o->type->binary_op != NULL) {
573 mp_obj_t result = o->type->binary_op(op, lhs, rhs);
574 if (result != NULL) {
575 return result;
576 }
Damien7410e442013-11-02 19:47:57 +0000577 }
Damien7410e442013-11-02 19:47:57 +0000578 }
Damien429d7192013-10-04 19:53:11 +0100579 }
Damiend99b0522013-12-21 18:17:45 +0000580
581 // TODO specify in error message what the operator is
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200582 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError,
583 "unsupported operand types for binary operator: '%s', '%s'",
584 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
Damien429d7192013-10-04 19:53:11 +0100585}
586
Damiend99b0522013-12-21 18:17:45 +0000587mp_obj_t rt_make_function_from_id(int unique_code_id) {
Damienb05d7072013-10-05 13:37:10 +0100588 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
589 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100590 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000591 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100592 }
Damiend99b0522013-12-21 18:17:45 +0000593
594 // make the function, depending on the code kind
595 mp_code_t *c = &unique_codes[unique_code_id];
596 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100597 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000598 case MP_CODE_BYTE:
Damien George6baf76e2013-12-30 22:32:17 +0000599 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 +0100600 break;
Damiend99b0522013-12-21 18:17:45 +0000601 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000602 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100603 break;
Damiend99b0522013-12-21 18:17:45 +0000604 case MP_CODE_INLINE_ASM:
605 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100606 break;
607 default:
608 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000609 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100610 }
Damienbd254452013-10-16 20:39:12 +0100611
612 // check for generator functions and if so wrap in generator object
613 if (c->is_generator) {
Damien George6baf76e2013-12-30 22:32:17 +0000614 fun = mp_obj_new_gen_wrap(c->n_locals, c->n_stack, fun);
Damienbd254452013-10-16 20:39:12 +0100615 }
616
Damiend99b0522013-12-21 18:17:45 +0000617 return fun;
Damien429d7192013-10-04 19:53:11 +0100618}
619
Damiend99b0522013-12-21 18:17:45 +0000620mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000621 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000622 // make function object
623 mp_obj_t ffun = rt_make_function_from_id(unique_code_id);
Damien9ecbcff2013-12-11 00:41:43 +0000624 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000625 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000626}
627
Damiend99b0522013-12-21 18:17:45 +0000628mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damieneb19efb2013-10-10 22:06:54 +0100629 return rt_call_function_n(fun, 0, NULL);
630}
631
Damiend99b0522013-12-21 18:17:45 +0000632mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damieneb19efb2013-10-10 22:06:54 +0100633 return rt_call_function_n(fun, 1, &arg);
634}
635
Damiend99b0522013-12-21 18:17:45 +0000636mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
637 mp_obj_t args[2];
Damieneb19efb2013-10-10 22:06:54 +0100638 args[1] = arg1;
639 args[0] = arg2;
640 return rt_call_function_n(fun, 2, args);
641}
642
Damieneb19efb2013-10-10 22:06:54 +0100643// args are in reverse order in the array
Damiend99b0522013-12-21 18:17:45 +0000644mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
645 // TODO improve this: fun object can specify its type and we parse here the arguments,
646 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100647
Damiend99b0522013-12-21 18:17:45 +0000648 DEBUG_OP_printf("calling function %p(n_args=%d, args=%p)\n", fun_in, n_args, args);
Damieneb19efb2013-10-10 22:06:54 +0100649
Damiend99b0522013-12-21 18:17:45 +0000650 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000651 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
Damien429d7192013-10-04 19:53:11 +0100652 } else {
Damiend99b0522013-12-21 18:17:45 +0000653 mp_obj_base_t *fun = fun_in;
654 if (fun->type->call_n != NULL) {
655 return fun->type->call_n(fun_in, n_args, args);
656 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000657 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name));
Damiend99b0522013-12-21 18:17:45 +0000658 }
Damien429d7192013-10-04 19:53:11 +0100659 }
Damien429d7192013-10-04 19:53:11 +0100660}
661
Damien86c7fc72013-11-26 15:16:41 +0000662// args are in reverse order in the array; keyword arguments come first, value then key
663// eg: (value1, key1, value0, key0, arg1, arg0)
John R. Lenton9c83ec02014-01-07 23:06:46 +0000664mp_obj_t rt_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
665 // TODO merge this and _n into a single, smarter thing
666 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
667
668 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
669 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
670 } else {
671 mp_obj_base_t *fun = fun_in;
672 if (fun->type->call_n_kw != NULL) {
673 return fun->type->call_n_kw(fun_in, n_args, n_kw, args);
674 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000675 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 +0000676 }
677 }
Damien86c7fc72013-11-26 15:16:41 +0000678}
679
Damiena3977762013-10-09 23:10:10 +0100680// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
681// if n_args==0 then there are only self/NULL and fun
Damiend99b0522013-12-21 18:17:45 +0000682mp_obj_t rt_call_method_n(uint n_args, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000683 DEBUG_OP_printf("call method %p(self=%p, n_args=%u)\n", args[n_args + 1], args[n_args], n_args);
Damiena3977762013-10-09 23:10:10 +0100684 return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
685}
686
Damien86c7fc72013-11-26 15:16:41 +0000687// args contains: kw_val(n_kw-1) kw_key(n_kw-1) ... kw_val(0) kw_key(0) arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
Damiend99b0522013-12-21 18:17:45 +0000688mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000689 uint n = n_args + 2 * n_kw;
690 DEBUG_OP_printf("call method %p(self=%p, n_args=%u, n_kw=%u)\n", args[n + 1], args[n], n_args, n_kw);
691 return rt_call_function_n_kw(args[n + 1], n_args + ((args[n] == NULL) ? 0 : 1), n_kw, args);
692}
693
Damien429d7192013-10-04 19:53:11 +0100694// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000695mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
696 return mp_obj_new_tuple_reverse(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100697}
698
699// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000700mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
701 return mp_obj_new_list_reverse(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100702}
703
Damiend99b0522013-12-21 18:17:45 +0000704mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
705 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100706}
707
Damiend99b0522013-12-21 18:17:45 +0000708mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000709 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100710 return set;
711}
712
Damien86c7fc72013-11-26 15:16:41 +0000713// unpacked items are stored in order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000714void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
715 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
716 uint seq_len;
717 mp_obj_t *seq_items;
718 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
719 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
720 } else {
721 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000722 }
Damiend99b0522013-12-21 18:17:45 +0000723 if (seq_len < num) {
Damien George6c73ca12014-01-08 18:11:23 +0000724 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 +0000725 } else if (seq_len > num) {
Damien George6c73ca12014-01-08 18:11:23 +0000726 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 +0000727 }
728 memcpy(items, seq_items, num * sizeof(mp_obj_t));
Damien86c7fc72013-11-26 15:16:41 +0000729 } else {
730 // TODO call rt_getiter and extract via rt_iternext
Damien George6c73ca12014-01-08 18:11:23 +0000731 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 +0000732 }
733}
734
Damiend99b0522013-12-21 18:17:45 +0000735mp_obj_t rt_build_map(int n_args) {
736 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100737}
738
Damiend99b0522013-12-21 18:17:45 +0000739mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
740 // map should always be a dict
741 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100742}
743
Damiend99b0522013-12-21 18:17:45 +0000744mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000745 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
746 // use load_method
747 mp_obj_t dest[2];
748 rt_load_method(base, attr, dest);
749 if (dest[0] == NULL) {
750 // load_method returned just a normal attribute
751 return dest[1];
752 } else {
753 // load_method returned a method, so build a bound method object
754 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000755 }
Damiend99b0522013-12-21 18:17:45 +0000756}
757
758void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000759 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
760
761 // clear output to indicate no attribute/method found yet
762 dest[0] = MP_OBJ_NULL;
763 dest[1] = MP_OBJ_NULL;
764
765 // get the type
766 mp_obj_type_t *type = mp_obj_get_type(base);
767
768 // if this type can do its own load, then call it
769 if (type->load_attr != NULL) {
770 type->load_attr(base, attr, dest);
771 }
772
773 // if nothing found yet, look for built-in and generic names
774 if (dest[1] == NULL) {
775 if (attr == MP_QSTR___next__ && type->iternext != NULL) {
776 dest[1] = (mp_obj_t)&mp_builtin_next_obj;
777 dest[0] = base;
778 } else {
779 // generic method lookup
Damien Georgeeae16442014-01-11 19:22:29 +0000780 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000781 const mp_method_t *meth = type->methods;
782 if (meth != NULL) {
783 for (; meth->name != NULL; meth++) {
784 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000785 // check if the methods are functions, static or class methods
786 // see http://docs.python.org/3.3/howto/descriptor.html
787 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
788 // return just the function
789 dest[1] = ((mp_obj_staticmethod_t*)meth->fun)->fun;
790 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
791 // return a bound method, with self being the type of this object
792 dest[1] = ((mp_obj_classmethod_t*)meth->fun)->fun;
793 dest[0] = mp_obj_get_type(base);
794 } else {
795 // return a bound method, with self being this object
796 dest[1] = (mp_obj_t)meth->fun;
797 dest[0] = base;
798 }
Damien George062478e2014-01-09 20:57:50 +0000799 break;
800 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000801 }
Damiend57eba52013-11-02 23:58:14 +0000802 }
803 }
Damiena3977762013-10-09 23:10:10 +0100804 }
805
Damien George062478e2014-01-09 20:57:50 +0000806 if (dest[1] == NULL) {
807 // no attribute/method called attr
808 // following CPython, we give a more detailed error message for type objects
809 if (MP_OBJ_IS_TYPE(base, &mp_const_type)) {
810 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)));
811 } else {
812 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)));
813 }
814 }
Damiena3977762013-10-09 23:10:10 +0100815}
816
Damiend99b0522013-12-21 18:17:45 +0000817void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100818 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000819 mp_obj_type_t *type = mp_obj_get_type(base);
820 if (type->store_attr != NULL) {
821 if (type->store_attr(base, attr, value)) {
822 return;
823 }
Damiena3977762013-10-09 23:10:10 +0100824 }
Damien George062478e2014-01-09 20:57:50 +0000825 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 +0100826}
827
Damiend99b0522013-12-21 18:17:45 +0000828void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100829 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000830 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100831 // list store
Damiend99b0522013-12-21 18:17:45 +0000832 mp_obj_list_store(base, index, value);
833 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
834 // dict store
835 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100836 } else {
837 assert(0);
838 }
839}
840
Damiend99b0522013-12-21 18:17:45 +0000841mp_obj_t rt_getiter(mp_obj_t o_in) {
842 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000843 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100844 } else {
Damiend99b0522013-12-21 18:17:45 +0000845 mp_obj_base_t *o = o_in;
846 if (o->type->getiter != NULL) {
847 return o->type->getiter(o_in);
848 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000849 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 +0000850 }
Damience89a212013-10-15 22:25:17 +0100851 }
852}
853
Damiend99b0522013-12-21 18:17:45 +0000854mp_obj_t rt_iternext(mp_obj_t o_in) {
855 if (MP_OBJ_IS_SMALL_INT(o_in)) {
John R. Lenton88f30432014-01-06 22:58:17 +0000856 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not an iterator"));
Damience89a212013-10-15 22:25:17 +0100857 } else {
Damiend99b0522013-12-21 18:17:45 +0000858 mp_obj_base_t *o = o_in;
859 if (o->type->iternext != NULL) {
860 return o->type->iternext(o_in);
861 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000862 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 +0000863 }
Damience89a212013-10-15 22:25:17 +0100864 }
865}
866
Damiend99b0522013-12-21 18:17:45 +0000867mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000868 // build args array
Damiend99b0522013-12-21 18:17:45 +0000869 mp_obj_t args[5];
870 args[0] = mp_obj_new_str(name);
871 args[1] = mp_const_none; // TODO should be globals
872 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000873 args[3] = fromlist;
874 args[4] = level; // must be 0; we don't yet support other values
875
876 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000877 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000878}
879
Damiend99b0522013-12-21 18:17:45 +0000880mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
881 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000882 /* TODO convert AttributeError to ImportError
883 if (fail) {
884 (ImportError, "cannot import name %s", qstr_str(name), NULL)
885 }
886 */
887 return x;
888}
889
Damien George66028ab2014-01-03 14:03:48 +0000890mp_map_t *rt_locals_get(void) {
891 return map_locals;
892}
893
894void rt_locals_set(mp_map_t *m) {
895 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
896 map_locals = m;
897}
898
899mp_map_t *rt_globals_get(void) {
900 return map_globals;
901}
902
903void rt_globals_set(mp_map_t *m) {
904 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
905 map_globals = m;
906}
907
Damien6ba13142013-11-02 20:34:54 +0000908// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100909void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000910 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100911 rt_load_const_str,
912 rt_load_name,
913 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100914 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +0100915 rt_load_attr,
916 rt_load_method,
917 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +0100918 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +0100919 rt_store_subscr,
920 rt_is_true,
921 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +0100922 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +0100923 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +0100924 rt_list_append,
Damien429d7192013-10-04 19:53:11 +0100925 rt_build_map,
926 rt_store_map,
927 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +0100928 rt_store_set,
Damien429d7192013-10-04 19:53:11 +0100929 rt_make_function_from_id,
Damieneb19efb2013-10-10 22:06:54 +0100930 rt_call_function_n,
Damien7f5dacf2013-10-10 11:24:39 +0100931 rt_call_method_n,
Damien429d7192013-10-04 19:53:11 +0100932 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +0100933 rt_getiter,
934 rt_iternext,
Damien429d7192013-10-04 19:53:11 +0100935};
936
937/*
938void rt_f_vector(rt_fun_kind_t fun_kind) {
939 (rt_f_table[fun_kind])();
940}
941*/