blob: 8e6eb06e2fd5ea6d4346e26ab1a5416908a83bee [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));
Paul Sokolovsky166bb402014-01-18 12:46:43 +020092 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
93 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
94 mp_map_add_qstr(&map_builtins, MP_QSTR_OverflowError, mp_obj_new_exception(MP_QSTR_OverflowError));
Damien George38a2da62014-01-08 17:33:12 +000095 mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
Paul Sokolovskyb81e1fd2014-01-11 16:33:32 +020096 mp_map_add_qstr(&map_builtins, MP_QSTR_AssertionError, mp_obj_new_exception(MP_QSTR_AssertionError));
Damienb86e3f92013-12-29 17:17:43 +000097
Damien Georgee9906ac2014-01-04 18:44:46 +000098 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +000099 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000100
Damienb86e3f92013-12-29 17:17:43 +0000101 // built-in core functions
Damien George38a2da62014-01-08 17:33:12 +0000102 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 +0000103 mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj);
Damienb86e3f92013-12-29 17:17:43 +0000104
Damien George71c51812014-01-04 20:21:15 +0000105 // built-in types
Damien George38a2da62014-01-08 17:33:12 +0000106 mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
Damien George71c51812014-01-04 20:21:15 +0000107#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000108 mp_map_add_qstr(&map_builtins, MP_QSTR_complex, (mp_obj_t)&complex_type);
Damien George71c51812014-01-04 20:21:15 +0000109#endif
Damien George38a2da62014-01-08 17:33:12 +0000110 mp_map_add_qstr(&map_builtins, MP_QSTR_dict, (mp_obj_t)&dict_type);
John R. Lenton9daa7892014-01-14 23:55:01 +0000111 mp_map_add_qstr(&map_builtins, MP_QSTR_enumerate, (mp_obj_t)&enumerate_type);
John R. Lentonfca456b2014-01-15 01:37:08 +0000112 mp_map_add_qstr(&map_builtins, MP_QSTR_filter, (mp_obj_t)&filter_type);
Damien George71c51812014-01-04 20:21:15 +0000113#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000114 mp_map_add_qstr(&map_builtins, MP_QSTR_float, (mp_obj_t)&float_type);
Damien George71c51812014-01-04 20:21:15 +0000115#endif
Damien George38a2da62014-01-08 17:33:12 +0000116 mp_map_add_qstr(&map_builtins, MP_QSTR_int, (mp_obj_t)&int_type);
117 mp_map_add_qstr(&map_builtins, MP_QSTR_list, (mp_obj_t)&list_type);
John R. Lenton39b174e2014-01-15 01:10:09 +0000118 mp_map_add_qstr(&map_builtins, MP_QSTR_map, (mp_obj_t)&map_type);
Damien George38a2da62014-01-08 17:33:12 +0000119 mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type);
120 mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
Damien George93a9b5b2014-01-08 18:48:12 +0000121 mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
John R. Lenton07205ec2014-01-13 02:31:00 +0000122 mp_map_add_qstr(&map_builtins, MP_QSTR_zip, (mp_obj_t)&zip_type);
Damien George71c51812014-01-04 20:21:15 +0000123
Damien George23005372014-01-13 19:39:01 +0000124 // built-in user functions
125 mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj);
126 mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj);
127 mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj);
128 mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
129 mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
130 mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
Damien Georged02c6d82014-01-15 22:14:03 +0000131 mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj);
Damien George38a2da62014-01-08 17:33:12 +0000132 mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
Damien George004cdce2014-01-09 21:43:51 +0000133 mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
134 mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);
Damien George38a2da62014-01-08 17:33:12 +0000135 mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj);
Damien George23005372014-01-13 19:39:01 +0000136 mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj);
137 mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj);
138 mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj);
Damien George38a2da62014-01-08 17:33:12 +0000139 mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj);
Damien George23005372014-01-13 19:39:01 +0000140 mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj);
141 mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj);
142 mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj);
143 mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj);
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000144 mp_map_add_qstr(&map_builtins, MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj);
John R. Lenton5c768392014-01-13 05:12:50 +0000145 mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj);
Damien George23005372014-01-13 19:39:01 +0000146 mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200147 mp_map_add_qstr(&map_builtins, MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj);
Damien429d7192013-10-04 19:53:11 +0100148
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200149#if MICROPY_CPYTHON_COMPAT
150 // Add (empty) micropython module, so it was possible to "import micropython",
151 // which can be a placeholder module on CPython.
152 mp_obj_t m = mp_obj_new_module(qstr_from_str_static("micropython"));
153 rt_store_name(qstr_from_str_static("micropython"), m);
154#endif
155
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000156 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000157 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100158 unique_codes = NULL;
159
Damien0446a0d2013-11-17 13:16:36 +0000160#ifdef WRITE_CODE
161 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100162#endif
Damien429d7192013-10-04 19:53:11 +0100163}
164
Damien8b3a7c22013-10-23 20:20:17 +0100165void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000166 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Damien0446a0d2013-11-17 13:16:36 +0000167#ifdef WRITE_CODE
168 if (fp_write_code != NULL) {
169 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100170 }
Damiena1ddfcc2013-10-10 23:25:50 +0100171#endif
Damien429d7192013-10-04 19:53:11 +0100172}
173
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000174int rt_get_unique_code_id(void) {
175 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100176}
177
Damien8b3a7c22013-10-23 20:20:17 +0100178static void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000179 if (next_unique_code_id > unique_codes_alloc) {
180 // increase size of unique_codes table
181 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
182 for (int i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000183 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100184 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000185 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100186 }
Damien826005c2013-10-05 23:17:28 +0100187}
188
Damien George6baf76e2013-12-30 22:32:17 +0000189void 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 +0100190 alloc_unique_codes();
191
John R. Lenton9c83ec02014-01-07 23:06:46 +0000192 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 +0000193 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damien826005c2013-10-05 23:17:28 +0100194 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100195 unique_codes[unique_code_id].n_locals = n_locals;
196 unique_codes[unique_code_id].n_stack = n_stack;
197 unique_codes[unique_code_id].is_generator = is_generator;
Damien826005c2013-10-05 23:17:28 +0100198 unique_codes[unique_code_id].u_byte.code = code;
199 unique_codes[unique_code_id].u_byte.len = len;
200
Damien9ecbcff2013-12-11 00:41:43 +0000201 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000202
203#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100204 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 +0000205 for (int i = 0; i < 128 && i < len; i++) {
206 if (i > 0 && i % 16 == 0) {
207 DEBUG_printf("\n");
208 }
209 DEBUG_printf(" %02x", code[i]);
210 }
211 DEBUG_printf("\n");
Damien George062478e2014-01-09 20:57:50 +0000212#if MICROPY_SHOW_BC
Damiend99b0522013-12-21 18:17:45 +0000213 extern void mp_show_byte_code(const byte *code, int len);
214 mp_show_byte_code(code, len);
Damien George062478e2014-01-09 20:57:50 +0000215#endif
Damien0446a0d2013-11-17 13:16:36 +0000216
217#ifdef WRITE_CODE
218 if (fp_write_code != NULL) {
219 fwrite(code, len, 1, fp_write_code);
220 fflush(fp_write_code);
221 }
222#endif
223#endif
Damien826005c2013-10-05 23:17:28 +0100224}
225
Damiend99b0522013-12-21 18:17:45 +0000226void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100227 alloc_unique_codes();
228
John R. Lenton9c83ec02014-01-07 23:06:46 +0000229 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 +0000230 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damien429d7192013-10-04 19:53:11 +0100231 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100232 unique_codes[unique_code_id].n_locals = 0;
233 unique_codes[unique_code_id].n_stack = 0;
234 unique_codes[unique_code_id].is_generator = false;
Damien429d7192013-10-04 19:53:11 +0100235 unique_codes[unique_code_id].u_native.fun = fun;
236
Damien George8cc96a32013-12-30 18:23:50 +0000237 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000238
Damiena1ddfcc2013-10-10 23:25:50 +0100239#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100240 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
241 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
242 for (int i = 0; i < 128 && i < len; i++) {
243 if (i > 0 && i % 16 == 0) {
244 DEBUG_printf("\n");
245 }
246 DEBUG_printf(" %02x", fun_data[i]);
247 }
248 DEBUG_printf("\n");
249
Damien0446a0d2013-11-17 13:16:36 +0000250#ifdef WRITE_CODE
251 if (fp_write_code != NULL) {
252 fwrite(fun_data, len, 1, fp_write_code);
253 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100254 }
Damiena1ddfcc2013-10-10 23:25:50 +0100255#endif
256#endif
Damien429d7192013-10-04 19:53:11 +0100257}
258
Damiend99b0522013-12-21 18:17:45 +0000259void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100260 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100261
John R. Lenton9c83ec02014-01-07 23:06:46 +0000262 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 +0000263 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damien826005c2013-10-05 23:17:28 +0100264 unique_codes[unique_code_id].n_args = n_args;
Damienbd254452013-10-16 20:39:12 +0100265 unique_codes[unique_code_id].n_locals = 0;
266 unique_codes[unique_code_id].n_stack = 0;
267 unique_codes[unique_code_id].is_generator = false;
Damien826005c2013-10-05 23:17:28 +0100268 unique_codes[unique_code_id].u_inline_asm.fun = fun;
269
Damiena1ddfcc2013-10-10 23:25:50 +0100270#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100271 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
272 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
273 for (int i = 0; i < 128 && i < len; i++) {
274 if (i > 0 && i % 16 == 0) {
275 DEBUG_printf("\n");
276 }
277 DEBUG_printf(" %02x", fun_data[i]);
278 }
279 DEBUG_printf("\n");
280
Damien0446a0d2013-11-17 13:16:36 +0000281#ifdef WRITE_CODE
282 if (fp_write_code != NULL) {
283 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100284 }
Damiena1ddfcc2013-10-10 23:25:50 +0100285#endif
286#endif
Damien429d7192013-10-04 19:53:11 +0100287}
288
Damiend99b0522013-12-21 18:17:45 +0000289int rt_is_true(mp_obj_t arg) {
290 DEBUG_OP_printf("is true %p\n", arg);
291 if (MP_OBJ_IS_SMALL_INT(arg)) {
292 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
293 return 0;
294 } else {
295 return 1;
296 }
297 } else if (arg == mp_const_none) {
298 return 0;
299 } else if (arg == mp_const_false) {
300 return 0;
301 } else if (arg == mp_const_true) {
302 return 1;
Paul Sokolovsky10744dd2014-01-16 23:54:17 +0200303 } else if (MP_OBJ_IS_QSTR(arg)) {
304 // TODO: \0
305 return *qstr_str(MP_OBJ_QSTR_VALUE(arg)) != 0;
306 } else if (MP_OBJ_IS_TYPE(arg, &str_type)) {
307 // TODO: \0
308 return *qstr_str(mp_obj_str_get(arg)) != 0;
309 } else if (MP_OBJ_IS_TYPE(arg, &list_type)) {
310 uint len;
311 mp_obj_t *dummy;
312 mp_obj_list_get(arg, &len, &dummy);
313 return len != 0;
314 } else if (MP_OBJ_IS_TYPE(arg, &tuple_type)) {
315 uint len;
316 mp_obj_t *dummy;
317 mp_obj_tuple_get(arg, &len, &dummy);
318 return len != 0;
319 } else if (MP_OBJ_IS_TYPE(arg, &dict_type)) {
320 return mp_obj_dict_len(arg) != 0;
Damiend99b0522013-12-21 18:17:45 +0000321 } else {
322 assert(0);
323 return 0;
324 }
325}
326
327mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
328 return mp_obj_list_append(self_in, arg);
329}
330
Damien7410e442013-11-02 19:47:57 +0000331#define PARSE_DEC_IN_INTG (1)
332#define PARSE_DEC_IN_FRAC (2)
333#define PARSE_DEC_IN_EXP (3)
334
Damiend99b0522013-12-21 18:17:45 +0000335mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000336#if MICROPY_ENABLE_FLOAT
337 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
338 const char *s = qstr_str(qstr);
339 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000340 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000341 bool exp_neg = false;
342 int exp_val = 0;
343 int exp_extra = 0;
344 bool imag = false;
345 for (; *s; s++) {
346 int dig = *s;
347 if ('0' <= dig && dig <= '9') {
348 dig -= '0';
349 if (in == PARSE_DEC_IN_EXP) {
350 exp_val = 10 * exp_val + dig;
351 } else {
352 dec_val = 10 * dec_val + dig;
353 if (in == PARSE_DEC_IN_FRAC) {
354 exp_extra -= 1;
355 }
356 }
357 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
358 in = PARSE_DEC_IN_FRAC;
359 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
360 in = PARSE_DEC_IN_EXP;
361 if (s[1] == '+') {
362 s++;
363 } else if (s[1] == '-') {
364 s++;
365 exp_neg = true;
366 }
367 } else if (dig == 'J' || dig == 'j') {
368 s++;
369 imag = true;
370 break;
371 } else {
372 // unknown character
373 break;
374 }
375 }
376 if (*s != 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000377 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000378 }
379 if (exp_neg) {
380 exp_val = -exp_val;
381 }
382 exp_val += exp_extra;
383 for (; exp_val > 0; exp_val--) {
384 dec_val *= 10;
385 }
386 for (; exp_val < 0; exp_val++) {
387 dec_val *= 0.1;
388 }
389 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000390 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000391 } else {
Damiend99b0522013-12-21 18:17:45 +0000392 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000393 }
394#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000395 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000396#endif
397}
398
Damiend99b0522013-12-21 18:17:45 +0000399mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100400 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damiend99b0522013-12-21 18:17:45 +0000401 return mp_obj_new_str(qstr);
Damien429d7192013-10-04 19:53:11 +0100402}
403
Damiend99b0522013-12-21 18:17:45 +0000404mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100405 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100406 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000407 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100408 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000409 elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100410 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000411 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100412 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000413 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 +0100414 }
415 }
416 }
417 return elem->value;
418}
419
Damiend99b0522013-12-21 18:17:45 +0000420mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100421 // logic: search globals, builtins
422 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000423 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100424 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000425 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100426 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000427 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 +0100428 }
429 }
430 return elem->value;
431}
432
Damiend99b0522013-12-21 18:17:45 +0000433mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100434 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000435 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 +0100436 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000437 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
Damien429d7192013-10-04 19:53:11 +0100438 }
439 return elem->value;
440}
441
Damiend99b0522013-12-21 18:17:45 +0000442mp_obj_t rt_get_cell(mp_obj_t cell) {
443 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000444}
445
Damiend99b0522013-12-21 18:17:45 +0000446void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
447 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000448}
449
Damiend99b0522013-12-21 18:17:45 +0000450void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100451 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000452 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 +0100453}
454
Damiend99b0522013-12-21 18:17:45 +0000455void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100456 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000457 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 +0100458}
459
Damiend99b0522013-12-21 18:17:45 +0000460mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000461 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000462 if (MP_OBJ_IS_SMALL_INT(arg)) {
463 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000464 switch (op) {
Damiend99b0522013-12-21 18:17:45 +0000465 case RT_UNARY_OP_NOT: if (val != 0) { return mp_const_true;} else { return mp_const_false; }
Damien7410e442013-11-02 19:47:57 +0000466 case RT_UNARY_OP_POSITIVE: break;
467 case RT_UNARY_OP_NEGATIVE: val = -val; break;
468 case RT_UNARY_OP_INVERT: val = ~val; break;
469 default: assert(0); val = 0;
470 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200471 if (MP_OBJ_FITS_SMALL_INT(val)) {
Damiend99b0522013-12-21 18:17:45 +0000472 return MP_OBJ_NEW_SMALL_INT(val);
Damien7410e442013-11-02 19:47:57 +0000473 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200474 return mp_obj_new_int(val);
Damiend99b0522013-12-21 18:17:45 +0000475 } else { // will be an object (small ints are caught in previous if)
476 mp_obj_base_t *o = arg;
477 if (o->type->unary_op != NULL) {
478 mp_obj_t result = o->type->unary_op(op, arg);
479 if (result != NULL) {
480 return result;
481 }
Damien7410e442013-11-02 19:47:57 +0000482 }
Damiend99b0522013-12-21 18:17:45 +0000483 // TODO specify in error message what the operator is
Damien George6c73ca12014-01-08 18:11:23 +0000484 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 +0000485 }
Damien429d7192013-10-04 19:53:11 +0100486}
487
Damiend99b0522013-12-21 18:17:45 +0000488mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100489 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000490
491 // TODO correctly distinguish inplace operators for mutable objects
492 // lookup logic that CPython uses for +=:
493 // check for implemented +=
494 // then check for implemented +
495 // then check for implemented seq.inplace_concat
496 // then check for implemented seq.concat
497 // then fail
498 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
499
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200500 // deal with is, is not
501 if (op == RT_COMPARE_OP_IS) {
502 // TODO: may need to handle strings specially, CPython appears to
503 // assume all strings are interned (so "is" == "==" for strings)
504 return MP_BOOL(lhs == rhs);
505 }
506 if (op == RT_COMPARE_OP_IS_NOT) {
507 // TODO: may need to handle strings specially, CPython appears to
508 // assume all strings are interned (so "is" == "==" for strings)
509 return MP_BOOL(lhs != rhs);
510 }
511
Damien Georgebcbeea02014-01-11 10:47:22 +0000512 // deal with == and != for all types
513 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
514 if (mp_obj_equal(lhs, rhs)) {
515 if (op == RT_COMPARE_OP_EQUAL) {
516 return mp_const_true;
517 } else {
518 return mp_const_false;
519 }
520 } else {
521 if (op == RT_COMPARE_OP_EQUAL) {
522 return mp_const_false;
523 } else {
524 return mp_const_true;
525 }
526 }
527 }
528
529 // deal with exception_match for all types
530 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
531 // TODO properly! at the moment it just compares the exception identifier for equality
532 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
533 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
534 return mp_const_true;
535 } else {
536 return mp_const_false;
537 }
538 }
539 }
540
Damien George1a9951d2014-01-06 22:13:00 +0000541 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000542 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000543 if (MP_OBJ_IS_SMALL_INT(rhs)) {
544 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
545 switch (op) {
546 case RT_BINARY_OP_OR:
547 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
548 case RT_BINARY_OP_XOR:
549 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
550 case RT_BINARY_OP_AND:
551 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
552 case RT_BINARY_OP_LSHIFT:
553 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
554 case RT_BINARY_OP_RSHIFT:
555 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
556 case RT_BINARY_OP_ADD:
557 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
558 case RT_BINARY_OP_SUBTRACT:
559 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
560 case RT_BINARY_OP_MULTIPLY:
561 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
562 case RT_BINARY_OP_FLOOR_DIVIDE:
563 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
564 #if MICROPY_ENABLE_FLOAT
565 case RT_BINARY_OP_TRUE_DIVIDE:
566 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
567 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000568
Damien George1a9951d2014-01-06 22:13:00 +0000569 // TODO implement modulo as specified by Python
570 case RT_BINARY_OP_MODULO:
571 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000572
Damien George1a9951d2014-01-06 22:13:00 +0000573 // TODO check for negative power, and overflow
574 case RT_BINARY_OP_POWER:
575 case RT_BINARY_OP_INPLACE_POWER:
576 {
577 int ans = 1;
578 while (rhs_val > 0) {
579 if (rhs_val & 1) {
580 ans *= lhs_val;
581 }
582 lhs_val *= lhs_val;
583 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000584 }
Damien George1a9951d2014-01-06 22:13:00 +0000585 lhs_val = ans;
586 break;
Damien4ebb32f2013-11-02 14:33:10 +0000587 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000588 case RT_COMPARE_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
589 case RT_COMPARE_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
590 case RT_COMPARE_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
591 case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000592
Damien George1a9951d2014-01-06 22:13:00 +0000593 default: assert(0);
594 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200595 // TODO: We just should make mp_obj_new_int() inline and use that
596 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000597 return MP_OBJ_NEW_SMALL_INT(lhs_val);
598 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200599 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000600 } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
601 return mp_obj_float_binary_op(op, lhs_val, rhs);
602 } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
603 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien429d7192013-10-04 19:53:11 +0100604 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000605 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000606
John R. Lentonc1bef212014-01-11 12:39:33 +0000607 /* deal with `in` and `not in`
608 *
609 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
610 * needs to go below
611 */
612 if (op == RT_COMPARE_OP_IN || op == RT_COMPARE_OP_NOT_IN) {
613 if (!MP_OBJ_IS_SMALL_INT(rhs)) {
614 mp_obj_base_t *o = rhs;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000615 if (o->type->binary_op != NULL) {
John R. Lentonc1bef212014-01-11 12:39:33 +0000616 mp_obj_t res = o->type->binary_op(op, rhs, lhs);
617 if (res != NULL) {
618 return res;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000619 }
Damien7410e442013-11-02 19:47:57 +0000620 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000621 if (o->type->getiter != NULL) {
622 /* second attempt, walk the iterator */
623 mp_obj_t next = NULL;
624 mp_obj_t iter = rt_getiter(rhs);
625 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
626 if (mp_obj_equal(next, lhs)) {
627 return MP_BOOL(op == RT_COMPARE_OP_IN);
628 }
629 }
630 return MP_BOOL(op != RT_COMPARE_OP_IN);
631 }
Damien7410e442013-11-02 19:47:57 +0000632 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000633
634 nlr_jump(mp_obj_new_exception_msg_varg(
635 MP_QSTR_TypeError, "'%s' object is not iterable",
636 mp_obj_get_type_str(rhs)));
637 return mp_const_none;
638 }
639
640 if (MP_OBJ_IS_OBJ(lhs)) {
641 mp_obj_base_t *o = lhs;
642 if (o->type->binary_op != NULL) {
643 mp_obj_t result = o->type->binary_op(op, lhs, rhs);
644 if (result != NULL) {
645 return result;
646 }
647 }
648 // TODO implement dispatch for reverse binary ops
Damien429d7192013-10-04 19:53:11 +0100649 }
Damiend99b0522013-12-21 18:17:45 +0000650
651 // TODO specify in error message what the operator is
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200652 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError,
653 "unsupported operand types for binary operator: '%s', '%s'",
654 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000655 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100656}
657
Damiend99b0522013-12-21 18:17:45 +0000658mp_obj_t rt_make_function_from_id(int unique_code_id) {
Damienb05d7072013-10-05 13:37:10 +0100659 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
660 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100661 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000662 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100663 }
Damiend99b0522013-12-21 18:17:45 +0000664
665 // make the function, depending on the code kind
666 mp_code_t *c = &unique_codes[unique_code_id];
667 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100668 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000669 case MP_CODE_BYTE:
Damien George6baf76e2013-12-30 22:32:17 +0000670 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 +0100671 break;
Damiend99b0522013-12-21 18:17:45 +0000672 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000673 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100674 break;
Damiend99b0522013-12-21 18:17:45 +0000675 case MP_CODE_INLINE_ASM:
676 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100677 break;
678 default:
679 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000680 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100681 }
Damienbd254452013-10-16 20:39:12 +0100682
683 // check for generator functions and if so wrap in generator object
684 if (c->is_generator) {
Damien George6baf76e2013-12-30 22:32:17 +0000685 fun = mp_obj_new_gen_wrap(c->n_locals, c->n_stack, fun);
Damienbd254452013-10-16 20:39:12 +0100686 }
687
Damiend99b0522013-12-21 18:17:45 +0000688 return fun;
Damien429d7192013-10-04 19:53:11 +0100689}
690
Damiend99b0522013-12-21 18:17:45 +0000691mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000692 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000693 // make function object
694 mp_obj_t ffun = rt_make_function_from_id(unique_code_id);
Damien9ecbcff2013-12-11 00:41:43 +0000695 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000696 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000697}
698
Damiend99b0522013-12-21 18:17:45 +0000699mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damieneb19efb2013-10-10 22:06:54 +0100700 return rt_call_function_n(fun, 0, NULL);
701}
702
Damiend99b0522013-12-21 18:17:45 +0000703mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damieneb19efb2013-10-10 22:06:54 +0100704 return rt_call_function_n(fun, 1, &arg);
705}
706
Damiend99b0522013-12-21 18:17:45 +0000707mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
708 mp_obj_t args[2];
Damieneb19efb2013-10-10 22:06:54 +0100709 args[1] = arg1;
710 args[0] = arg2;
711 return rt_call_function_n(fun, 2, args);
712}
713
Damieneb19efb2013-10-10 22:06:54 +0100714// args are in reverse order in the array
Damiend99b0522013-12-21 18:17:45 +0000715mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
716 // TODO improve this: fun object can specify its type and we parse here the arguments,
717 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100718
Damiend99b0522013-12-21 18:17:45 +0000719 DEBUG_OP_printf("calling function %p(n_args=%d, args=%p)\n", fun_in, n_args, args);
Damieneb19efb2013-10-10 22:06:54 +0100720
Damiend99b0522013-12-21 18:17:45 +0000721 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000722 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
Damien429d7192013-10-04 19:53:11 +0100723 } else {
Damiend99b0522013-12-21 18:17:45 +0000724 mp_obj_base_t *fun = fun_in;
725 if (fun->type->call_n != NULL) {
726 return fun->type->call_n(fun_in, n_args, args);
727 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000728 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 +0000729 }
Damien429d7192013-10-04 19:53:11 +0100730 }
Damien429d7192013-10-04 19:53:11 +0100731}
732
Damien86c7fc72013-11-26 15:16:41 +0000733// args are in reverse order in the array; keyword arguments come first, value then key
734// eg: (value1, key1, value0, key0, arg1, arg0)
John R. Lenton9c83ec02014-01-07 23:06:46 +0000735mp_obj_t rt_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
736 // TODO merge this and _n into a single, smarter thing
737 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
738
739 if (MP_OBJ_IS_SMALL_INT(fun_in)) {
740 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
741 } else {
742 mp_obj_base_t *fun = fun_in;
743 if (fun->type->call_n_kw != NULL) {
744 return fun->type->call_n_kw(fun_in, n_args, n_kw, args);
745 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000746 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 +0000747 }
748 }
Damien86c7fc72013-11-26 15:16:41 +0000749}
750
Damiena3977762013-10-09 23:10:10 +0100751// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun
752// if n_args==0 then there are only self/NULL and fun
Damiend99b0522013-12-21 18:17:45 +0000753mp_obj_t rt_call_method_n(uint n_args, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000754 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 +0100755 return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args);
756}
757
Damien86c7fc72013-11-26 15:16:41 +0000758// 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 +0000759mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien86c7fc72013-11-26 15:16:41 +0000760 uint n = n_args + 2 * n_kw;
761 DEBUG_OP_printf("call method %p(self=%p, n_args=%u, n_kw=%u)\n", args[n + 1], args[n], n_args, n_kw);
762 return rt_call_function_n_kw(args[n + 1], n_args + ((args[n] == NULL) ? 0 : 1), n_kw, args);
763}
764
Damien429d7192013-10-04 19:53:11 +0100765// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000766mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
767 return mp_obj_new_tuple_reverse(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100768}
769
770// items are in reverse order
Damiend99b0522013-12-21 18:17:45 +0000771mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
772 return mp_obj_new_list_reverse(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100773}
774
Damiend99b0522013-12-21 18:17:45 +0000775mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
776 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100777}
778
Damiend99b0522013-12-21 18:17:45 +0000779mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000780 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100781 return set;
782}
783
Damien86c7fc72013-11-26 15:16:41 +0000784// unpacked items are stored in order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000785void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
786 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
787 uint seq_len;
788 mp_obj_t *seq_items;
789 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
790 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
791 } else {
792 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000793 }
Damiend99b0522013-12-21 18:17:45 +0000794 if (seq_len < num) {
Damien George6c73ca12014-01-08 18:11:23 +0000795 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 +0000796 } else if (seq_len > num) {
Damien George6c73ca12014-01-08 18:11:23 +0000797 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 +0000798 }
799 memcpy(items, seq_items, num * sizeof(mp_obj_t));
Damien86c7fc72013-11-26 15:16:41 +0000800 } else {
801 // TODO call rt_getiter and extract via rt_iternext
Damien George6c73ca12014-01-08 18:11:23 +0000802 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 +0000803 }
804}
805
Damiend99b0522013-12-21 18:17:45 +0000806mp_obj_t rt_build_map(int n_args) {
807 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100808}
809
Damiend99b0522013-12-21 18:17:45 +0000810mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
811 // map should always be a dict
812 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100813}
814
Damiend99b0522013-12-21 18:17:45 +0000815mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000816 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
817 // use load_method
818 mp_obj_t dest[2];
819 rt_load_method(base, attr, dest);
820 if (dest[0] == NULL) {
821 // load_method returned just a normal attribute
822 return dest[1];
823 } else {
824 // load_method returned a method, so build a bound method object
825 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000826 }
Damiend99b0522013-12-21 18:17:45 +0000827}
828
829void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000830 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
831
832 // clear output to indicate no attribute/method found yet
833 dest[0] = MP_OBJ_NULL;
834 dest[1] = MP_OBJ_NULL;
835
836 // get the type
837 mp_obj_type_t *type = mp_obj_get_type(base);
838
839 // if this type can do its own load, then call it
840 if (type->load_attr != NULL) {
841 type->load_attr(base, attr, dest);
842 }
843
844 // if nothing found yet, look for built-in and generic names
845 if (dest[1] == NULL) {
846 if (attr == MP_QSTR___next__ && type->iternext != NULL) {
847 dest[1] = (mp_obj_t)&mp_builtin_next_obj;
848 dest[0] = base;
849 } else {
850 // generic method lookup
Damien Georgeeae16442014-01-11 19:22:29 +0000851 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000852 const mp_method_t *meth = type->methods;
853 if (meth != NULL) {
854 for (; meth->name != NULL; meth++) {
855 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000856 // check if the methods are functions, static or class methods
857 // see http://docs.python.org/3.3/howto/descriptor.html
858 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
859 // return just the function
860 dest[1] = ((mp_obj_staticmethod_t*)meth->fun)->fun;
861 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
862 // return a bound method, with self being the type of this object
863 dest[1] = ((mp_obj_classmethod_t*)meth->fun)->fun;
864 dest[0] = mp_obj_get_type(base);
865 } else {
866 // return a bound method, with self being this object
867 dest[1] = (mp_obj_t)meth->fun;
868 dest[0] = base;
869 }
Damien George062478e2014-01-09 20:57:50 +0000870 break;
871 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000872 }
Damiend57eba52013-11-02 23:58:14 +0000873 }
874 }
Damiena3977762013-10-09 23:10:10 +0100875 }
876
Damien George062478e2014-01-09 20:57:50 +0000877 if (dest[1] == NULL) {
878 // no attribute/method called attr
879 // following CPython, we give a more detailed error message for type objects
880 if (MP_OBJ_IS_TYPE(base, &mp_const_type)) {
881 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)));
882 } else {
883 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)));
884 }
885 }
Damiena3977762013-10-09 23:10:10 +0100886}
887
Damiend99b0522013-12-21 18:17:45 +0000888void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100889 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000890 mp_obj_type_t *type = mp_obj_get_type(base);
891 if (type->store_attr != NULL) {
892 if (type->store_attr(base, attr, value)) {
893 return;
894 }
Damiena3977762013-10-09 23:10:10 +0100895 }
Damien George062478e2014-01-09 20:57:50 +0000896 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 +0100897}
898
Damiend99b0522013-12-21 18:17:45 +0000899void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100900 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000901 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100902 // list store
Damiend99b0522013-12-21 18:17:45 +0000903 mp_obj_list_store(base, index, value);
904 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
905 // dict store
906 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100907 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200908 mp_obj_type_t *type = mp_obj_get_type(base);
909 if (type->store_item != NULL) {
910 bool r = type->store_item(base, index, value);
911 if (r) {
912 return;
913 }
914 // TODO: call base classes here?
915 }
916 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
Damien429d7192013-10-04 19:53:11 +0100917 }
918}
919
Damiend99b0522013-12-21 18:17:45 +0000920mp_obj_t rt_getiter(mp_obj_t o_in) {
921 if (MP_OBJ_IS_SMALL_INT(o_in)) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000922 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not iterable"));
Damience89a212013-10-15 22:25:17 +0100923 } else {
Damiend99b0522013-12-21 18:17:45 +0000924 mp_obj_base_t *o = o_in;
925 if (o->type->getiter != NULL) {
926 return o->type->getiter(o_in);
927 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000928 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not iterable", o->type->name));
Damiend99b0522013-12-21 18:17:45 +0000929 }
Damience89a212013-10-15 22:25:17 +0100930 }
931}
932
Damiend99b0522013-12-21 18:17:45 +0000933mp_obj_t rt_iternext(mp_obj_t o_in) {
934 if (MP_OBJ_IS_SMALL_INT(o_in)) {
John R. Lenton88f30432014-01-06 22:58:17 +0000935 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not an iterator"));
Damience89a212013-10-15 22:25:17 +0100936 } else {
Damiend99b0522013-12-21 18:17:45 +0000937 mp_obj_base_t *o = o_in;
938 if (o->type->iternext != NULL) {
939 return o->type->iternext(o_in);
940 } else {
Damien George6c73ca12014-01-08 18:11:23 +0000941 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 +0000942 }
Damience89a212013-10-15 22:25:17 +0100943 }
944}
945
Damiend99b0522013-12-21 18:17:45 +0000946mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000947 // build args array
Damiend99b0522013-12-21 18:17:45 +0000948 mp_obj_t args[5];
949 args[0] = mp_obj_new_str(name);
950 args[1] = mp_const_none; // TODO should be globals
951 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000952 args[3] = fromlist;
953 args[4] = level; // must be 0; we don't yet support other values
954
955 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000956 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000957}
958
Damiend99b0522013-12-21 18:17:45 +0000959mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
960 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000961 /* TODO convert AttributeError to ImportError
962 if (fail) {
963 (ImportError, "cannot import name %s", qstr_str(name), NULL)
964 }
965 */
966 return x;
967}
968
Damien George66028ab2014-01-03 14:03:48 +0000969mp_map_t *rt_locals_get(void) {
970 return map_locals;
971}
972
973void rt_locals_set(mp_map_t *m) {
974 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
975 map_locals = m;
976}
977
978mp_map_t *rt_globals_get(void) {
979 return map_globals;
980}
981
982void rt_globals_set(mp_map_t *m) {
983 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
984 map_globals = m;
985}
986
Damien6ba13142013-11-02 20:34:54 +0000987// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100988void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000989 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100990 rt_load_const_str,
991 rt_load_name,
992 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100993 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +0100994 rt_load_attr,
995 rt_load_method,
996 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +0100997 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +0100998 rt_store_subscr,
999 rt_is_true,
1000 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +01001001 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001002 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001003 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001004 rt_build_map,
1005 rt_store_map,
1006 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001007 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001008 rt_make_function_from_id,
Damieneb19efb2013-10-10 22:06:54 +01001009 rt_call_function_n,
Damien7f5dacf2013-10-10 11:24:39 +01001010 rt_call_method_n,
Damien429d7192013-10-04 19:53:11 +01001011 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001012 rt_getiter,
1013 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001014};
1015
1016/*
1017void rt_f_vector(rt_fun_kind_t fun_kind) {
1018 (rt_f_table[fun_kind])();
1019}
1020*/