blob: 0f5d1229f296821203c461c562d1d4a0b879e070 [file] [log] [blame]
Damienc226dca2013-10-16 16:12:52 +01001// in principle, rt_xxx functions are called only by vm/native/viper and make assumptions about args
Damiend99b0522013-12-21 18:17:45 +00002// mp_xxx functions are safer and can be called by anyone
Damienbd254452013-10-16 20:39:12 +01003// note that rt_assign_xxx are called only from emit*, and maybe we can rename them to reflect this
Damienc226dca2013-10-16 16:12:52 +01004
Damien429d7192013-10-04 19:53:11 +01005#include <stdio.h>
6#include <string.h>
7#include <assert.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00008#include <math.h>
Damien429d7192013-10-04 19:53:11 +01009
Damience89a212013-10-15 22:25:17 +010010#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010011#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000012#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000013#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000014#include "obj.h"
Damien Georgecaac5422014-03-25 14:18:18 +000015#include "objmodule.h"
Damien George20773972014-02-22 18:12:43 +000016#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000017#include "runtime0.h"
18#include "runtime.h"
19#include "map.h"
Damien660365e2013-12-17 18:27:24 +000020#include "builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000021#include "builtintables.h"
Damien George5fa93b62014-01-22 14:35:10 +000022#include "bc.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000023#include "intdivmod.h"
Damien660365e2013-12-17 18:27:24 +000024
Damien7f5dacf2013-10-10 11:24:39 +010025#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010026#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000027#define WRITE_CODE (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020028#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000029#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010030#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000031#define DEBUG_printf(...) (void)0
32#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010033#endif
Damien429d7192013-10-04 19:53:11 +010034
Damieneb19efb2013-10-10 22:06:54 +010035// locals and globals need to be pointers because they can be the same in outer module scope
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020036STATIC mp_map_t *map_locals;
37STATIC mp_map_t *map_globals;
38STATIC mp_map_t map_builtins;
Damienbd254452013-10-16 20:39:12 +010039
Damien429d7192013-10-04 19:53:11 +010040typedef enum {
Damiend99b0522013-12-21 18:17:45 +000041 MP_CODE_NONE,
42 MP_CODE_BYTE,
43 MP_CODE_NATIVE,
44 MP_CODE_INLINE_ASM,
45} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010046
Damiend99b0522013-12-21 18:17:45 +000047typedef struct _mp_code_t {
Damien George51047752014-02-26 17:40:52 +000048 mp_code_kind_t kind : 8;
49 uint scope_flags : 8;
50 uint n_args : 16;
51 uint n_state : 16;
Damien429d7192013-10-04 19:53:11 +010052 union {
53 struct {
Damien429d7192013-10-04 19:53:11 +010054 byte *code;
55 uint len;
56 } u_byte;
Damien826005c2013-10-05 23:17:28 +010057 struct {
Damiend99b0522013-12-21 18:17:45 +000058 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010059 } u_native;
60 struct {
Damiene4af64f2013-10-06 12:04:13 +010061 void *fun;
Damien826005c2013-10-05 23:17:28 +010062 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010063 };
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020064 qstr *arg_names;
Damiend99b0522013-12-21 18:17:45 +000065} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010066
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020067STATIC uint next_unique_code_id;
68STATIC machine_uint_t unique_codes_alloc = 0;
69STATIC mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010070
Damien0446a0d2013-11-17 13:16:36 +000071#ifdef WRITE_CODE
72FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010073#endif
Damien429d7192013-10-04 19:53:11 +010074
Damien George38a2da62014-01-08 17:33:12 +000075// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020076STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +000077 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
78}
79
Damien8b3a7c22013-10-23 20:20:17 +010080void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +010081 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +000082 map_locals = map_globals = mp_map_new(1);
Damien429d7192013-10-04 19:53:11 +010083
Damienb86e3f92013-12-29 17:17:43 +000084 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +000085 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +000086
Damien Georgecaac5422014-03-25 14:18:18 +000087 // init global module stuff
88 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000089
Damien Georgecaac5422014-03-25 14:18:18 +000090 // add some builtins that can't be done in ROM
91 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien George38a2da62014-01-08 17:33:12 +000092 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +000093
Paul Sokolovskydcac8802014-01-16 19:19:50 +020094#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +020095 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020096 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
97 // Avoid warning of unused var
98 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +020099#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +0200100 // init sys.path
101 // for efficiency, left to platform-specific startup code
102 //sys_path = mp_obj_new_list(0, NULL);
103 //rt_store_attr(m_sys, MP_QSTR_path, sys_path);
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200104
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200105 // TODO: wastes one mp_code_t structure in mem
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000106 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000107 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100108 unique_codes = NULL;
109
Damien0446a0d2013-11-17 13:16:36 +0000110#ifdef WRITE_CODE
111 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100112#endif
Damien429d7192013-10-04 19:53:11 +0100113}
114
Damien8b3a7c22013-10-23 20:20:17 +0100115void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000116 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200117 mp_map_free(map_globals);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200118 mp_map_deinit(&map_builtins);
Damien Georgecaac5422014-03-25 14:18:18 +0000119 mp_module_deinit();
Damien0446a0d2013-11-17 13:16:36 +0000120#ifdef WRITE_CODE
121 if (fp_write_code != NULL) {
122 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100123 }
Damiena1ddfcc2013-10-10 23:25:50 +0100124#endif
Damien429d7192013-10-04 19:53:11 +0100125}
126
Damien Georged0691cc2014-01-29 20:30:52 +0000127uint rt_get_unique_code_id(void) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000128 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100129}
130
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200131STATIC void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000132 if (next_unique_code_id > unique_codes_alloc) {
Damien Georged0691cc2014-01-29 20:30:52 +0000133 DEBUG_printf("allocate more unique codes: " UINT_FMT " -> %u\n", unique_codes_alloc, next_unique_code_id);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000134 // increase size of unique_codes table
135 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
Damien Georged0691cc2014-01-29 20:30:52 +0000136 for (uint i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000137 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100138 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000139 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100140 }
Damien826005c2013-10-05 23:17:28 +0100141}
142
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200143void rt_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, uint scope_flags, qstr *arg_names) {
Damien826005c2013-10-05 23:17:28 +0100144 alloc_unique_codes();
145
John R. Lenton9c83ec02014-01-07 23:06:46 +0000146 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 +0000147 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien George8725f8f2014-02-15 19:33:11 +0000148 unique_codes[unique_code_id].scope_flags = scope_flags;
Damien Georged0691cc2014-01-29 20:30:52 +0000149 unique_codes[unique_code_id].n_args = n_args;
150 unique_codes[unique_code_id].n_state = n_locals + n_stack;
Damien826005c2013-10-05 23:17:28 +0100151 unique_codes[unique_code_id].u_byte.code = code;
152 unique_codes[unique_code_id].u_byte.len = len;
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200153 unique_codes[unique_code_id].arg_names = arg_names;
Damien826005c2013-10-05 23:17:28 +0100154
Damien9ecbcff2013-12-11 00:41:43 +0000155 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000156
157#ifdef DEBUG_PRINT
Damien George08d07552014-01-29 18:58:52 +0000158 DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d n_locals=%d n_stack=%d\n", unique_code_id, code, len, n_args, n_locals, n_stack);
Damien0446a0d2013-11-17 13:16:36 +0000159 for (int i = 0; i < 128 && i < len; i++) {
160 if (i > 0 && i % 16 == 0) {
161 DEBUG_printf("\n");
162 }
163 DEBUG_printf(" %02x", code[i]);
164 }
165 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000166#if MICROPY_DEBUG_PRINTERS
167 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000168#endif
Damien0446a0d2013-11-17 13:16:36 +0000169#endif
Damien826005c2013-10-05 23:17:28 +0100170}
171
Damien Georged0691cc2014-01-29 20:30:52 +0000172void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100173 alloc_unique_codes();
174
John R. Lenton9c83ec02014-01-07 23:06:46 +0000175 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 +0000176 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien George8725f8f2014-02-15 19:33:11 +0000177 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000178 unique_codes[unique_code_id].n_args = n_args;
179 unique_codes[unique_code_id].n_state = 0;
Damien429d7192013-10-04 19:53:11 +0100180 unique_codes[unique_code_id].u_native.fun = fun;
181
Damien George8cc96a32013-12-30 18:23:50 +0000182 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000183
Damiena1ddfcc2013-10-10 23:25:50 +0100184#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100185 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
186 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
187 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", fun_data[i]);
192 }
193 DEBUG_printf("\n");
194
Damien0446a0d2013-11-17 13:16:36 +0000195#ifdef WRITE_CODE
196 if (fp_write_code != NULL) {
197 fwrite(fun_data, len, 1, fp_write_code);
198 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100199 }
Damiena1ddfcc2013-10-10 23:25:50 +0100200#endif
201#endif
Damien429d7192013-10-04 19:53:11 +0100202}
203
Damien Georged0691cc2014-01-29 20:30:52 +0000204void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100205 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100206
John R. Lenton9c83ec02014-01-07 23:06:46 +0000207 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 +0000208 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien George8725f8f2014-02-15 19:33:11 +0000209 unique_codes[unique_code_id].scope_flags = 0;
Damien Georged0691cc2014-01-29 20:30:52 +0000210 unique_codes[unique_code_id].n_args = n_args;
211 unique_codes[unique_code_id].n_state = 0;
Damien826005c2013-10-05 23:17:28 +0100212 unique_codes[unique_code_id].u_inline_asm.fun = fun;
213
Damiena1ddfcc2013-10-10 23:25:50 +0100214#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100215 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
216 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
217 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", fun_data[i]);
222 }
223 DEBUG_printf("\n");
224
Damien0446a0d2013-11-17 13:16:36 +0000225#ifdef WRITE_CODE
226 if (fp_write_code != NULL) {
227 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100228 }
Damiena1ddfcc2013-10-10 23:25:50 +0100229#endif
230#endif
Damien429d7192013-10-04 19:53:11 +0100231}
232
Damiend99b0522013-12-21 18:17:45 +0000233int rt_is_true(mp_obj_t arg) {
234 DEBUG_OP_printf("is true %p\n", arg);
Damien George09a0c642014-01-30 10:05:33 +0000235 if (arg == mp_const_false) {
236 return 0;
237 } else if (arg == mp_const_true) {
238 return 1;
239 } else if (arg == mp_const_none) {
240 return 0;
241 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000242 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
243 return 0;
244 } else {
245 return 1;
246 }
Damiend99b0522013-12-21 18:17:45 +0000247 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200248 mp_obj_type_t *type = mp_obj_get_type(arg);
249 if (type->unary_op != NULL) {
250 mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
Damien George09a0c642014-01-30 10:05:33 +0000251 if (result != MP_OBJ_NULL) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200252 return result == mp_const_true;
253 }
254 }
255
Damien Georgee4b6a072014-01-28 23:27:35 +0000256 mp_obj_t len = mp_obj_len_maybe(arg);
257 if (len != MP_OBJ_NULL) {
258 // obj has a length, truth determined if len != 0
259 return len != MP_OBJ_NEW_SMALL_INT(0);
260 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200261 // any other obj is true per Python semantics
Damien Georgee4b6a072014-01-28 23:27:35 +0000262 return 1;
263 }
Damiend99b0522013-12-21 18:17:45 +0000264 }
265}
266
267mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
268 return mp_obj_list_append(self_in, arg);
269}
270
Damiend99b0522013-12-21 18:17:45 +0000271mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000272 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000273 uint len;
274 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +0000275 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +0000276}
277
Damiend99b0522013-12-21 18:17:45 +0000278mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100279 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000280 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100281}
282
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200283mp_obj_t rt_load_const_bytes(qstr qstr) {
284 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
285 uint len;
286 const byte *data = qstr_data(qstr, &len);
287 return mp_obj_new_bytes(data, len);
288}
289
Damiend99b0522013-12-21 18:17:45 +0000290mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100291 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100292 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000293 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000294 if (elem != NULL) {
295 return elem->value;
296 } else {
297 return rt_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100298 }
Damiena3977762013-10-09 23:10:10 +0100299}
300
Damiend99b0522013-12-21 18:17:45 +0000301mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100302 // logic: search globals, builtins
303 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000304 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100305 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000306 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100307 if (elem == NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000308 mp_obj_t o = mp_builtin_tables_lookup_object(qstr);
309 if (o != MP_OBJ_NULL) {
310 return o;
Damien Georgeaea532e2014-02-06 22:57:51 +0000311 }
Damien Georgec5966122014-02-15 16:10:44 +0000312 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100313 }
314 }
315 return elem->value;
316}
317
Damiend99b0522013-12-21 18:17:45 +0000318mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100319 DEBUG_OP_printf("load_build_class\n");
Damien Georgecaac5422014-03-25 14:18:18 +0000320 // lookup __build_class__ in dynamic table of builtins first
Damien George38a2da62014-01-08 17:33:12 +0000321 mp_map_elem_t *elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000322 if (elem != NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000323 // found user-defined __build_class__, return it
Damien Georgeaea532e2014-02-06 22:57:51 +0000324 return elem->value;
325 } else {
Damien Georgecaac5422014-03-25 14:18:18 +0000326 // no user-defined __build_class__, return builtin one
Damien Georgeaea532e2014-02-06 22:57:51 +0000327 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100328 }
Damien429d7192013-10-04 19:53:11 +0100329}
330
Damiend99b0522013-12-21 18:17:45 +0000331mp_obj_t rt_get_cell(mp_obj_t cell) {
332 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000333}
334
Damiend99b0522013-12-21 18:17:45 +0000335void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
336 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000337}
338
Damiend99b0522013-12-21 18:17:45 +0000339void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100340 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000341 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 +0100342}
343
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200344void rt_delete_name(qstr qstr) {
345 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
346 mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
347}
348
Damiend99b0522013-12-21 18:17:45 +0000349void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100350 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000351 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 +0100352}
353
Damiend99b0522013-12-21 18:17:45 +0000354mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000355 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000356
Damiend99b0522013-12-21 18:17:45 +0000357 if (MP_OBJ_IS_SMALL_INT(arg)) {
358 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000359 switch (op) {
Damien George9d68e9c2014-03-12 15:38:15 +0000360 case RT_UNARY_OP_BOOL:
361 return MP_BOOL(val != 0);
362 case RT_UNARY_OP_POSITIVE:
363 return arg;
364 case RT_UNARY_OP_NEGATIVE:
365 // check for overflow
366 if (val == MP_SMALL_INT_MIN) {
367 return mp_obj_new_int(-val);
368 } else {
369 return MP_OBJ_NEW_SMALL_INT(-val);
370 }
371 case RT_UNARY_OP_INVERT:
372 return MP_OBJ_NEW_SMALL_INT(~val);
373 default:
374 assert(0);
375 return arg;
Damien7410e442013-11-02 19:47:57 +0000376 }
Damien George1e708fe2014-01-23 18:27:51 +0000377 } else {
378 mp_obj_type_t *type = mp_obj_get_type(arg);
379 if (type->unary_op != NULL) {
380 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000381 if (result != NULL) {
382 return result;
383 }
Damien7410e442013-11-02 19:47:57 +0000384 }
Damiend99b0522013-12-21 18:17:45 +0000385 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000386 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bad operand type for unary operator: '%s'", mp_obj_get_type_str(arg)));
Damien7410e442013-11-02 19:47:57 +0000387 }
Damien429d7192013-10-04 19:53:11 +0100388}
389
Damiend99b0522013-12-21 18:17:45 +0000390mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100391 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000392
393 // TODO correctly distinguish inplace operators for mutable objects
394 // lookup logic that CPython uses for +=:
395 // check for implemented +=
396 // then check for implemented +
397 // then check for implemented seq.inplace_concat
398 // then check for implemented seq.concat
399 // then fail
400 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
401
Damien George9aa2a522014-02-01 23:04:09 +0000402 // deal with is
403 if (op == RT_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200404 return MP_BOOL(lhs == rhs);
405 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200406
Damien Georgebcbeea02014-01-11 10:47:22 +0000407 // deal with == and != for all types
Damien George9aa2a522014-02-01 23:04:09 +0000408 if (op == RT_BINARY_OP_EQUAL || op == RT_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000409 if (mp_obj_equal(lhs, rhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000410 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000411 return mp_const_true;
412 } else {
413 return mp_const_false;
414 }
415 } else {
Damien George9aa2a522014-02-01 23:04:09 +0000416 if (op == RT_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000417 return mp_const_false;
418 } else {
419 return mp_const_true;
420 }
421 }
422 }
423
424 // deal with exception_match for all types
Damien George9aa2a522014-02-01 23:04:09 +0000425 if (op == RT_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000426 // rhs must be issubclass(rhs, BaseException)
427 if (mp_obj_is_exception_type(rhs)) {
428 // if lhs is an instance of an exception, then extract and use its type
429 if (mp_obj_is_exception_instance(lhs)) {
430 lhs = mp_obj_get_type(lhs);
431 }
Damien George71510152014-03-03 22:38:13 +0000432 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000433 return mp_const_true;
434 } else {
435 return mp_const_false;
436 }
437 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200438 assert(0);
439 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000440 }
441
Damien George1a9951d2014-01-06 22:13:00 +0000442 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000443 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000444 if (MP_OBJ_IS_SMALL_INT(rhs)) {
445 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000446 // This is a binary operation: lhs_val op rhs_val
447 // We need to be careful to handle overflow; see CERT INT32-C
448 // Operations that can overflow:
449 // + result always fits in machine_int_t, then handled by SMALL_INT check
450 // - result always fits in machine_int_t, then handled by SMALL_INT check
451 // * checked explicitly
452 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
453 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
454 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000455 switch (op) {
456 case RT_BINARY_OP_OR:
457 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
458 case RT_BINARY_OP_XOR:
459 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
460 case RT_BINARY_OP_AND:
461 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
462 case RT_BINARY_OP_LSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000463 case RT_BINARY_OP_INPLACE_LSHIFT: {
464 if (rhs_val < 0) {
465 // negative shift not allowed
466 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
467 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
468 // left-shift will overflow, so use higher precision integer
469 lhs = mp_obj_new_int_from_ll(lhs_val);
470 goto generic_binary_op;
471 } else {
472 // use standard precision
473 lhs_val <<= rhs_val;
474 }
475 break;
476 }
Damien George1a9951d2014-01-06 22:13:00 +0000477 case RT_BINARY_OP_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000478 case RT_BINARY_OP_INPLACE_RSHIFT:
479 if (rhs_val < 0) {
480 // negative shift not allowed
481 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
482 } else {
483 // standard precision is enough for right-shift
484 lhs_val >>= rhs_val;
485 }
486 break;
Damien George1a9951d2014-01-06 22:13:00 +0000487 case RT_BINARY_OP_ADD:
488 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
489 case RT_BINARY_OP_SUBTRACT:
490 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
491 case RT_BINARY_OP_MULTIPLY:
Damien George9d68e9c2014-03-12 15:38:15 +0000492 case RT_BINARY_OP_INPLACE_MULTIPLY: {
493
494 // If long long type exists and is larger than machine_int_t, then
495 // we can use the following code to perform overflow-checked multiplication.
496 // Otherwise (eg in x64 case) we must use the branching code below.
497 #if 0
498 // compute result using long long precision
499 long long res = (long long)lhs_val * (long long)rhs_val;
500 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
501 // result overflowed SMALL_INT, so return higher precision integer
502 return mp_obj_new_int_from_ll(res);
503 } else {
504 // use standard precision
505 lhs_val = (mp_small_int_t)res;
506 }
507 #endif
508
509 if (lhs_val > 0) { // lhs_val is positive
510 if (rhs_val > 0) { // lhs_val and rhs_val are positive
511 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
512 goto mul_overflow;
513 }
514 } else { // lhs_val positive, rhs_val nonpositive
515 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
516 goto mul_overflow;
517 }
518 } // lhs_val positive, rhs_val nonpositive
519 } else { // lhs_val is nonpositive
520 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
521 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
522 goto mul_overflow;
523 }
524 } else { // lhs_val and rhs_val are nonpositive
525 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
526 goto mul_overflow;
527 }
528 } // End if lhs_val and rhs_val are nonpositive
529 } // End if lhs_val is nonpositive
530
531 // use standard precision
532 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
533
534 mul_overflow:
535 // use higher precision
536 lhs = mp_obj_new_int_from_ll(lhs_val);
537 goto generic_binary_op;
538
539 break;
540 }
Damien George1a9951d2014-01-06 22:13:00 +0000541 case RT_BINARY_OP_FLOOR_DIVIDE:
Rachel Dowdall56402792014-03-22 20:19:24 +0000542 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE:
543 {
544 lhs_val = python_floor_divide(lhs_val, rhs_val);
545 break;
546 }
Damien George9d68e9c2014-03-12 15:38:15 +0000547 #if MICROPY_ENABLE_FLOAT
Damien George1a9951d2014-01-06 22:13:00 +0000548 case RT_BINARY_OP_TRUE_DIVIDE:
549 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000550 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000551
Damien George1a9951d2014-01-06 22:13:00 +0000552 case RT_BINARY_OP_MODULO:
Rachel Dowdallcde86312014-03-22 17:29:27 +0000553 case RT_BINARY_OP_INPLACE_MODULO:
554 {
555 lhs_val = python_modulo(lhs_val, rhs_val);
556 break;
557 }
Damien George1a9951d2014-01-06 22:13:00 +0000558 case RT_BINARY_OP_POWER:
559 case RT_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000560 if (rhs_val < 0) {
561 #if MICROPY_ENABLE_FLOAT
562 lhs = mp_obj_new_float(lhs_val);
563 goto generic_binary_op;
564 #else
565 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
566 #endif
567 } else {
568 // TODO check for overflow
569 machine_int_t ans = 1;
570 while (rhs_val > 0) {
571 if (rhs_val & 1) {
572 ans *= lhs_val;
573 }
574 lhs_val *= lhs_val;
575 rhs_val /= 2;
Damien George1a9951d2014-01-06 22:13:00 +0000576 }
Damien George9d68e9c2014-03-12 15:38:15 +0000577 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000578 }
Damien George1a9951d2014-01-06 22:13:00 +0000579 break;
Damien George9aa2a522014-02-01 23:04:09 +0000580 case RT_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
581 case RT_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
582 case RT_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
583 case RT_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000584
Damien George1a9951d2014-01-06 22:13:00 +0000585 default: assert(0);
586 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200587 // TODO: We just should make mp_obj_new_int() inline and use that
588 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000589 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000590 } else {
591 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000592 }
Damien George3f759b72014-01-31 00:42:12 +0000593#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000594 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000595 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000596 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000597 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000598#endif
Damien429d7192013-10-04 19:53:11 +0100599 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000600 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000601
Damien George9aa2a522014-02-01 23:04:09 +0000602 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000603 *
604 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000605 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000606 */
Damien George9aa2a522014-02-01 23:04:09 +0000607 if (op == RT_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000608 mp_obj_type_t *type = mp_obj_get_type(rhs);
609 if (type->binary_op != NULL) {
610 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000611 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000612 return res;
613 }
614 }
615 if (type->getiter != NULL) {
616 /* second attempt, walk the iterator */
617 mp_obj_t next = NULL;
618 mp_obj_t iter = rt_getiter(rhs);
619 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
620 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000621 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000622 }
Damien7410e442013-11-02 19:47:57 +0000623 }
Damien George9aa2a522014-02-01 23:04:09 +0000624 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000625 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000626
627 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000628 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000629 mp_obj_get_type_str(rhs)));
630 return mp_const_none;
631 }
632
Damien George5fa93b62014-01-22 14:35:10 +0000633 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000634 mp_obj_type_t *type;
635generic_binary_op:
636 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000637 if (type->binary_op != NULL) {
638 mp_obj_t result = type->binary_op(op, lhs, rhs);
639 if (result != MP_OBJ_NULL) {
640 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000641 }
Damien429d7192013-10-04 19:53:11 +0100642 }
Damiend99b0522013-12-21 18:17:45 +0000643
Damien George5fa93b62014-01-22 14:35:10 +0000644 // TODO implement dispatch for reverse binary ops
645
Damiend99b0522013-12-21 18:17:45 +0000646 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000647 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200648 "unsupported operand types for binary operator: '%s', '%s'",
649 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000650 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100651}
652
Paul Sokolovsky90750022014-02-01 15:05:04 +0200653mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100654 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
655 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100656 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000657 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100658 }
Damiend99b0522013-12-21 18:17:45 +0000659
660 // make the function, depending on the code kind
661 mp_code_t *c = &unique_codes[unique_code_id];
662 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100663 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000664 case MP_CODE_BYTE:
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200665 fun = mp_obj_new_fun_bc(c->scope_flags, c->arg_names, c->n_args, def_args, c->n_state, c->u_byte.code);
Damien826005c2013-10-05 23:17:28 +0100666 break;
Damiend99b0522013-12-21 18:17:45 +0000667 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000668 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100669 break;
Damiend99b0522013-12-21 18:17:45 +0000670 case MP_CODE_INLINE_ASM:
671 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100672 break;
673 default:
674 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000675 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100676 }
Damienbd254452013-10-16 20:39:12 +0100677
678 // check for generator functions and if so wrap in generator object
Damien George8725f8f2014-02-15 19:33:11 +0000679 if ((c->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien Georged0691cc2014-01-29 20:30:52 +0000680 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100681 }
682
Damiend99b0522013-12-21 18:17:45 +0000683 return fun;
Damien429d7192013-10-04 19:53:11 +0100684}
685
Damiend99b0522013-12-21 18:17:45 +0000686mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000687 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000688 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200689 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000690 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000691 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000692}
693
Damiend99b0522013-12-21 18:17:45 +0000694mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000695 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100696}
697
Damiend99b0522013-12-21 18:17:45 +0000698mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000699 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100700}
701
Damiend99b0522013-12-21 18:17:45 +0000702mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
703 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000704 args[0] = arg1;
705 args[1] = arg2;
706 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100707}
708
Damien Georgecd82e022014-02-02 13:11:48 +0000709// wrapper that accepts n_args and n_kw in one argument
710// native emitter can only pass at most 3 arguments to a function
711mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
712 return rt_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
713}
714
Damien George20006db2014-01-18 14:10:48 +0000715// args contains, eg: arg0 arg1 key0 value0 key1 value1
716mp_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 +0000717 // TODO improve this: fun object can specify its type and we parse here the arguments,
718 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100719
John R. Lenton9c83ec02014-01-07 23:06:46 +0000720 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
721
Damien George8b56beb2014-01-31 23:49:49 +0000722 // get the type
723 mp_obj_type_t *type = mp_obj_get_type(fun_in);
724
725 // do the call
726 if (type->call != NULL) {
727 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000728 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000729 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000730 }
Damien86c7fc72013-11-26 15:16:41 +0000731}
732
Damien George20006db2014-01-18 14:10:48 +0000733// 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)
734// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000735mp_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 +0000736 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);
737 int adjust = (args[1] == NULL) ? 0 : 1;
738 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000739}
740
Damiend99b0522013-12-21 18:17:45 +0000741mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000742 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100743}
744
Damiend99b0522013-12-21 18:17:45 +0000745mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000746 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100747}
748
Damiend99b0522013-12-21 18:17:45 +0000749mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
750 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100751}
752
Damiend99b0522013-12-21 18:17:45 +0000753mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000754 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100755 return set;
756}
757
Damien George932bf1c2014-01-18 23:42:49 +0000758// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000759void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200760 uint seq_len;
Damiend99b0522013-12-21 18:17:45 +0000761 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
Damiend99b0522013-12-21 18:17:45 +0000762 mp_obj_t *seq_items;
763 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
764 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
765 } else {
766 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000767 }
Damiend99b0522013-12-21 18:17:45 +0000768 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200769 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000770 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200771 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000772 }
Damien George932bf1c2014-01-18 23:42:49 +0000773 for (uint i = 0; i < num; i++) {
774 items[i] = seq_items[num - 1 - i];
775 }
Damien86c7fc72013-11-26 15:16:41 +0000776 } else {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200777 mp_obj_t iterable = rt_getiter(seq_in);
778
779 for (seq_len = 0; seq_len < num; seq_len++) {
780 mp_obj_t el = rt_iternext(iterable);
781 if (el == mp_const_stop_iteration) {
782 goto too_short;
783 }
784 items[num - 1 - seq_len] = el;
785 }
786 if (rt_iternext(iterable) != mp_const_stop_iteration) {
787 goto too_long;
788 }
Damien86c7fc72013-11-26 15:16:41 +0000789 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200790 return;
791
792too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000793 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200794too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000795 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num));
Damien86c7fc72013-11-26 15:16:41 +0000796}
797
Damiend99b0522013-12-21 18:17:45 +0000798mp_obj_t rt_build_map(int n_args) {
799 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100800}
801
Damiend99b0522013-12-21 18:17:45 +0000802mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
803 // map should always be a dict
804 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100805}
806
Damiend99b0522013-12-21 18:17:45 +0000807mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000808 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
809 // use load_method
810 mp_obj_t dest[2];
811 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000812 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000813 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000814 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000815 } else {
816 // load_method returned a method, so build a bound method object
817 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000818 }
Damiend99b0522013-12-21 18:17:45 +0000819}
820
Damien George7c9c6672014-01-25 00:17:36 +0000821// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
822// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
823// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200824STATIC void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000825 // clear output to indicate no attribute/method found yet
826 dest[0] = MP_OBJ_NULL;
827 dest[1] = MP_OBJ_NULL;
828
829 // get the type
830 mp_obj_type_t *type = mp_obj_get_type(base);
831
832 // if this type can do its own load, then call it
833 if (type->load_attr != NULL) {
834 type->load_attr(base, attr, dest);
835 }
836
837 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000838 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000839 if (attr == MP_QSTR___class__) {
840 // a.__class__ is equivalent to type(a)
841 dest[0] = type;
842 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000843 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
844 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000845 } else if (type->load_attr == NULL) {
846 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000847 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000848 const mp_method_t *meth = type->methods;
849 if (meth != NULL) {
850 for (; meth->name != NULL; meth++) {
851 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000852 // check if the methods are functions, static or class methods
853 // see http://docs.python.org/3.3/howto/descriptor.html
854 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
855 // return just the function
Damien George64131f32014-02-06 20:31:44 +0000856 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000857 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
858 // return a bound method, with self being the type of this object
Damien George64131f32014-02-06 20:31:44 +0000859 dest[0] = ((mp_obj_static_class_method_t*)meth->fun)->fun;
Damien George20006db2014-01-18 14:10:48 +0000860 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000861 } else {
862 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000863 dest[0] = (mp_obj_t)meth->fun;
864 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000865 }
Damien George062478e2014-01-09 20:57:50 +0000866 break;
867 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000868 }
Damiend57eba52013-11-02 23:58:14 +0000869 }
870 }
Damiena3977762013-10-09 23:10:10 +0100871 }
Damien George7c9c6672014-01-25 00:17:36 +0000872}
Damiena3977762013-10-09 23:10:10 +0100873
Damien George7c9c6672014-01-25 00:17:36 +0000874void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
875 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
876
877 rt_load_method_maybe(base, attr, dest);
878
879 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000880 // no attribute/method called attr
881 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000882 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200883 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
884 "type object '%s' has no attribute '%s'", qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000885 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000886 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000887 }
888 }
Damiena3977762013-10-09 23:10:10 +0100889}
890
Damiend99b0522013-12-21 18:17:45 +0000891void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100892 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000893 mp_obj_type_t *type = mp_obj_get_type(base);
894 if (type->store_attr != NULL) {
895 if (type->store_attr(base, attr, value)) {
896 return;
897 }
Damiena3977762013-10-09 23:10:10 +0100898 }
Damien Georgec5966122014-02-15 16:10:44 +0000899 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100900}
901
Damiend99b0522013-12-21 18:17:45 +0000902void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100903 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000904 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100905 // list store
Damiend99b0522013-12-21 18:17:45 +0000906 mp_obj_list_store(base, index, value);
907 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
908 // dict store
909 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100910 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200911 mp_obj_type_t *type = mp_obj_get_type(base);
912 if (type->store_item != NULL) {
913 bool r = type->store_item(base, index, value);
914 if (r) {
915 return;
916 }
917 // TODO: call base classes here?
918 }
Damien Georgec5966122014-02-15 16:10:44 +0000919 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
Damien429d7192013-10-04 19:53:11 +0100920 }
921}
922
Damiend99b0522013-12-21 18:17:45 +0000923mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000924 mp_obj_type_t *type = mp_obj_get_type(o_in);
925 if (type->getiter != NULL) {
926 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100927 } else {
Damien George7c9c6672014-01-25 00:17:36 +0000928 // check for __getitem__ method
929 mp_obj_t dest[2];
Damien George7d0bfbe2014-02-08 19:01:47 +0000930 rt_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000931 if (dest[0] != MP_OBJ_NULL) {
932 // __getitem__ exists, create an iterator
933 return mp_obj_new_getitem_iter(dest);
934 } else {
935 // object not iterable
Damien George0ec6bd42014-03-09 16:29:36 +0000936 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
Damien George7c9c6672014-01-25 00:17:36 +0000937 }
Damience89a212013-10-15 22:25:17 +0100938 }
939}
940
Damiend99b0522013-12-21 18:17:45 +0000941mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000942 mp_obj_type_t *type = mp_obj_get_type(o_in);
943 if (type->iternext != NULL) {
944 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100945 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000946 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
Damien Georgec5966122014-02-15 16:10:44 +0000947 }
948}
949
950mp_obj_t rt_make_raise_obj(mp_obj_t o) {
951 DEBUG_printf("raise %p\n", o);
952 if (mp_obj_is_exception_type(o)) {
953 // o is an exception type (it is derived from BaseException (or is BaseException))
954 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +0000955 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
956 // could have const instances in ROM which we return here instead
Damien Georgec5966122014-02-15 16:10:44 +0000957 return rt_call_function_n_kw(o, 0, 0, NULL);
958 } else if (mp_obj_is_exception_instance(o)) {
959 // o is an instance of an exception, so use it as the exception
960 return o;
961 } else {
962 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
963 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +0100964 }
965}
966
Damiend99b0522013-12-21 18:17:45 +0000967mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +0000968 DEBUG_printf("import name %s\n", qstr_str(name));
969
Damiendb4c3612013-12-10 17:27:24 +0000970 // build args array
Damiend99b0522013-12-21 18:17:45 +0000971 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +0000972 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +0000973 args[1] = mp_const_none; // TODO should be globals
974 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000975 args[3] = fromlist;
976 args[4] = level; // must be 0; we don't yet support other values
977
978 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000979 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000980}
981
Damiend99b0522013-12-21 18:17:45 +0000982mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +0000983 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
984
Damiend99b0522013-12-21 18:17:45 +0000985 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000986 /* TODO convert AttributeError to ImportError
987 if (fail) {
988 (ImportError, "cannot import name %s", qstr_str(name), NULL)
989 }
990 */
991 return x;
992}
993
Paul Sokolovskyda1ce932014-02-14 00:22:06 +0200994void rt_import_all(mp_obj_t module) {
995 DEBUG_printf("import all %p\n", module);
996
997 mp_map_t *map = mp_obj_module_get_globals(module);
998 for (uint i = 0; i < map->alloc; i++) {
999 if (map->table[i].key != MP_OBJ_NULL) {
1000 rt_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
1001 }
1002 }
1003}
1004
Damien George66028ab2014-01-03 14:03:48 +00001005mp_map_t *rt_locals_get(void) {
1006 return map_locals;
1007}
1008
1009void rt_locals_set(mp_map_t *m) {
1010 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
1011 map_locals = m;
1012}
1013
1014mp_map_t *rt_globals_get(void) {
1015 return map_globals;
1016}
1017
1018void rt_globals_set(mp_map_t *m) {
1019 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
1020 map_globals = m;
1021}
1022
Damien6ba13142013-11-02 20:34:54 +00001023// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +01001024void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +00001025 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +01001026 rt_load_const_str,
1027 rt_load_name,
1028 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +01001029 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001030 rt_load_attr,
1031 rt_load_method,
1032 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001033 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001034 rt_store_subscr,
1035 rt_is_true,
1036 rt_unary_op,
Damien Georgecd82e022014-02-02 13:11:48 +00001037 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001038 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001039 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001040 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001041 rt_build_map,
1042 rt_store_map,
1043 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001044 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001045 rt_make_function_from_id,
Damien Georgecd82e022014-02-02 13:11:48 +00001046 rt_call_function_n_kw_for_native,
Damien George20006db2014-01-18 14:10:48 +00001047 rt_call_method_n_kw,
Damiend2755ec2013-10-16 23:58:48 +01001048 rt_getiter,
1049 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001050};
1051
1052/*
1053void rt_f_vector(rt_fun_kind_t fun_kind) {
1054 (rt_f_table[fun_kind])();
1055}
1056*/