blob: 77e596c5d0f0c9f37334cf70c348d6453f7f2b42 [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 George55baff42014-01-21 21:40:13 +000014#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000015#include "obj.h"
Damiend99b0522013-12-21 18:17:45 +000016#include "runtime0.h"
17#include "runtime.h"
18#include "map.h"
Damien660365e2013-12-17 18:27:24 +000019#include "builtin.h"
Paul Sokolovsky427905c2014-01-18 19:24:47 +020020#include "objarray.h"
Damien George5fa93b62014-01-22 14:35:10 +000021#include "bc.h"
Damien660365e2013-12-17 18:27:24 +000022
Damien7f5dacf2013-10-10 11:24:39 +010023#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010024#define DEBUG_PRINT (1)
Damien0446a0d2013-11-17 13:16:36 +000025#define WRITE_CODE (1)
Damien7f5dacf2013-10-10 11:24:39 +010026#define DEBUG_printf(args...) printf(args)
27#define DEBUG_OP_printf(args...) printf(args)
28#else // don't print debugging info
Damiena3977762013-10-09 23:10:10 +010029#define DEBUG_printf(args...) (void)0
Damien429d7192013-10-04 19:53:11 +010030#define DEBUG_OP_printf(args...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010031#endif
Damien429d7192013-10-04 19:53:11 +010032
Damieneb19efb2013-10-10 22:06:54 +010033// locals and globals need to be pointers because they can be the same in outer module scope
Damiend99b0522013-12-21 18:17:45 +000034static mp_map_t *map_locals;
35static mp_map_t *map_globals;
36static mp_map_t map_builtins;
Damien George0d028742014-01-22 23:59:20 +000037static mp_map_t map_loaded_modules; // TODO: expose as sys.modules
Damienbd254452013-10-16 20:39:12 +010038
Damien429d7192013-10-04 19:53:11 +010039typedef enum {
Damiend99b0522013-12-21 18:17:45 +000040 MP_CODE_NONE,
41 MP_CODE_BYTE,
42 MP_CODE_NATIVE,
43 MP_CODE_INLINE_ASM,
44} mp_code_kind_t;
Damien429d7192013-10-04 19:53:11 +010045
Damiend99b0522013-12-21 18:17:45 +000046typedef struct _mp_code_t {
Damien Georged0691cc2014-01-29 20:30:52 +000047 struct {
48 mp_code_kind_t kind : 8;
49 bool is_generator : 1;
50 };
51 struct {
52 uint n_args : 16;
53 uint n_state : 16;
54 };
Damien429d7192013-10-04 19:53:11 +010055 union {
56 struct {
Damien429d7192013-10-04 19:53:11 +010057 byte *code;
58 uint len;
59 } u_byte;
Damien826005c2013-10-05 23:17:28 +010060 struct {
Damiend99b0522013-12-21 18:17:45 +000061 mp_fun_t fun;
Damien826005c2013-10-05 23:17:28 +010062 } u_native;
63 struct {
Damiene4af64f2013-10-06 12:04:13 +010064 void *fun;
Damien826005c2013-10-05 23:17:28 +010065 } u_inline_asm;
Damien429d7192013-10-04 19:53:11 +010066 };
Damiend99b0522013-12-21 18:17:45 +000067} mp_code_t;
Damien429d7192013-10-04 19:53:11 +010068
Damien Georged0691cc2014-01-29 20:30:52 +000069static uint next_unique_code_id;
John R. Lenton9c83ec02014-01-07 23:06:46 +000070static machine_uint_t unique_codes_alloc = 0;
71static mp_code_t *unique_codes = NULL;
Damien429d7192013-10-04 19:53:11 +010072
Damien0446a0d2013-11-17 13:16:36 +000073#ifdef WRITE_CODE
74FILE *fp_write_code = NULL;
Damiena1ddfcc2013-10-10 23:25:50 +010075#endif
Damien429d7192013-10-04 19:53:11 +010076
Damien George38a2da62014-01-08 17:33:12 +000077// a good optimising compiler will inline this if necessary
78static void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
79 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
80}
81
Damien8b3a7c22013-10-23 20:20:17 +010082void rt_init(void) {
Damieneb19efb2013-10-10 22:06:54 +010083 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +000084 map_locals = map_globals = mp_map_new(1);
Damien George5fa93b62014-01-22 14:35:10 +000085 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien429d7192013-10-04 19:53:11 +010086
Damienb86e3f92013-12-29 17:17:43 +000087 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +000088 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +000089
Damien George0d028742014-01-22 23:59:20 +000090 // init loaded modules table
91 mp_map_init(&map_loaded_modules, 3);
92
Damien George7c9c6672014-01-25 00:17:36 +000093 // built-in exceptions (TODO, make these proper classes, and const if possible)
Damien George38a2da62014-01-08 17:33:12 +000094 mp_map_add_qstr(&map_builtins, MP_QSTR_AttributeError, mp_obj_new_exception(MP_QSTR_AttributeError));
95 mp_map_add_qstr(&map_builtins, MP_QSTR_IndexError, mp_obj_new_exception(MP_QSTR_IndexError));
96 mp_map_add_qstr(&map_builtins, MP_QSTR_KeyError, mp_obj_new_exception(MP_QSTR_KeyError));
97 mp_map_add_qstr(&map_builtins, MP_QSTR_NameError, mp_obj_new_exception(MP_QSTR_NameError));
98 mp_map_add_qstr(&map_builtins, MP_QSTR_TypeError, mp_obj_new_exception(MP_QSTR_TypeError));
99 mp_map_add_qstr(&map_builtins, MP_QSTR_SyntaxError, mp_obj_new_exception(MP_QSTR_SyntaxError));
100 mp_map_add_qstr(&map_builtins, MP_QSTR_ValueError, mp_obj_new_exception(MP_QSTR_ValueError));
Paul Sokolovsky166bb402014-01-18 12:46:43 +0200101 // Somehow CPython managed to have OverflowError not inherit from ValueError ;-/
102 // TODO: For MICROPY_CPYTHON_COMPAT==0 use ValueError to avoid exc proliferation
103 mp_map_add_qstr(&map_builtins, MP_QSTR_OverflowError, mp_obj_new_exception(MP_QSTR_OverflowError));
Damien George38a2da62014-01-08 17:33:12 +0000104 mp_map_add_qstr(&map_builtins, MP_QSTR_OSError, mp_obj_new_exception(MP_QSTR_OSError));
Paul Sokolovskyb81e1fd2014-01-11 16:33:32 +0200105 mp_map_add_qstr(&map_builtins, MP_QSTR_AssertionError, mp_obj_new_exception(MP_QSTR_AssertionError));
Damien George7c9c6672014-01-25 00:17:36 +0000106 mp_map_add_qstr(&map_builtins, MP_QSTR_StopIteration, mp_obj_new_exception(MP_QSTR_StopIteration));
Damienb86e3f92013-12-29 17:17:43 +0000107
Damien Georgee9906ac2014-01-04 18:44:46 +0000108 // built-in objects
Damien George38a2da62014-01-08 17:33:12 +0000109 mp_map_add_qstr(&map_builtins, MP_QSTR_Ellipsis, mp_const_ellipsis);
Damien Georgee9906ac2014-01-04 18:44:46 +0000110
Damienb86e3f92013-12-29 17:17:43 +0000111 // built-in core functions
Damien George38a2da62014-01-08 17:33:12 +0000112 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 +0000113 mp_map_add_qstr(&map_builtins, MP_QSTR___repl_print__, (mp_obj_t)&mp_builtin___repl_print___obj);
Damienb86e3f92013-12-29 17:17:43 +0000114
Damien George71c51812014-01-04 20:21:15 +0000115 // built-in types
Damien George38a2da62014-01-08 17:33:12 +0000116 mp_map_add_qstr(&map_builtins, MP_QSTR_bool, (mp_obj_t)&bool_type);
Damien George71c51812014-01-04 20:21:15 +0000117#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000118 mp_map_add_qstr(&map_builtins, MP_QSTR_complex, (mp_obj_t)&complex_type);
Damien George71c51812014-01-04 20:21:15 +0000119#endif
Damien George38a2da62014-01-08 17:33:12 +0000120 mp_map_add_qstr(&map_builtins, MP_QSTR_dict, (mp_obj_t)&dict_type);
John R. Lenton9daa7892014-01-14 23:55:01 +0000121 mp_map_add_qstr(&map_builtins, MP_QSTR_enumerate, (mp_obj_t)&enumerate_type);
John R. Lentonfca456b2014-01-15 01:37:08 +0000122 mp_map_add_qstr(&map_builtins, MP_QSTR_filter, (mp_obj_t)&filter_type);
Damien George71c51812014-01-04 20:21:15 +0000123#if MICROPY_ENABLE_FLOAT
Damien George38a2da62014-01-08 17:33:12 +0000124 mp_map_add_qstr(&map_builtins, MP_QSTR_float, (mp_obj_t)&float_type);
Damien George71c51812014-01-04 20:21:15 +0000125#endif
Damien George38a2da62014-01-08 17:33:12 +0000126 mp_map_add_qstr(&map_builtins, MP_QSTR_int, (mp_obj_t)&int_type);
127 mp_map_add_qstr(&map_builtins, MP_QSTR_list, (mp_obj_t)&list_type);
John R. Lenton39b174e2014-01-15 01:10:09 +0000128 mp_map_add_qstr(&map_builtins, MP_QSTR_map, (mp_obj_t)&map_type);
Damien George38a2da62014-01-08 17:33:12 +0000129 mp_map_add_qstr(&map_builtins, MP_QSTR_set, (mp_obj_t)&set_type);
130 mp_map_add_qstr(&map_builtins, MP_QSTR_tuple, (mp_obj_t)&tuple_type);
Damien George93a9b5b2014-01-08 18:48:12 +0000131 mp_map_add_qstr(&map_builtins, MP_QSTR_type, (mp_obj_t)&mp_const_type);
John R. Lenton07205ec2014-01-13 02:31:00 +0000132 mp_map_add_qstr(&map_builtins, MP_QSTR_zip, (mp_obj_t)&zip_type);
Damien George71c51812014-01-04 20:21:15 +0000133
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200134 mp_obj_t m_array = mp_obj_new_module(MP_QSTR_array);
135 rt_store_attr(m_array, MP_QSTR_array, (mp_obj_t)&array_type);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200136
Damien George23005372014-01-13 19:39:01 +0000137 // built-in user functions
138 mp_map_add_qstr(&map_builtins, MP_QSTR_abs, (mp_obj_t)&mp_builtin_abs_obj);
139 mp_map_add_qstr(&map_builtins, MP_QSTR_all, (mp_obj_t)&mp_builtin_all_obj);
140 mp_map_add_qstr(&map_builtins, MP_QSTR_any, (mp_obj_t)&mp_builtin_any_obj);
Paul Sokolovskya80ff042014-01-20 20:32:50 +0200141 mp_map_add_qstr(&map_builtins, MP_QSTR_bytes, (mp_obj_t)&mp_builtin_bytes_obj);
Damien George23005372014-01-13 19:39:01 +0000142 mp_map_add_qstr(&map_builtins, MP_QSTR_callable, (mp_obj_t)&mp_builtin_callable_obj);
143 mp_map_add_qstr(&map_builtins, MP_QSTR_chr, (mp_obj_t)&mp_builtin_chr_obj);
144 mp_map_add_qstr(&map_builtins, MP_QSTR_divmod, (mp_obj_t)&mp_builtin_divmod_obj);
Damien Georged02c6d82014-01-15 22:14:03 +0000145 mp_map_add_qstr(&map_builtins, MP_QSTR_eval, (mp_obj_t)&mp_builtin_eval_obj);
Damien George38a2da62014-01-08 17:33:12 +0000146 mp_map_add_qstr(&map_builtins, MP_QSTR_hash, (mp_obj_t)&mp_builtin_hash_obj);
Damien George004cdce2014-01-09 21:43:51 +0000147 mp_map_add_qstr(&map_builtins, MP_QSTR_isinstance, (mp_obj_t)&mp_builtin_isinstance_obj);
148 mp_map_add_qstr(&map_builtins, MP_QSTR_issubclass, (mp_obj_t)&mp_builtin_issubclass_obj);
Damien George38a2da62014-01-08 17:33:12 +0000149 mp_map_add_qstr(&map_builtins, MP_QSTR_iter, (mp_obj_t)&mp_builtin_iter_obj);
Damien George23005372014-01-13 19:39:01 +0000150 mp_map_add_qstr(&map_builtins, MP_QSTR_len, (mp_obj_t)&mp_builtin_len_obj);
151 mp_map_add_qstr(&map_builtins, MP_QSTR_max, (mp_obj_t)&mp_builtin_max_obj);
152 mp_map_add_qstr(&map_builtins, MP_QSTR_min, (mp_obj_t)&mp_builtin_min_obj);
Damien George38a2da62014-01-08 17:33:12 +0000153 mp_map_add_qstr(&map_builtins, MP_QSTR_next, (mp_obj_t)&mp_builtin_next_obj);
Damien George23005372014-01-13 19:39:01 +0000154 mp_map_add_qstr(&map_builtins, MP_QSTR_ord, (mp_obj_t)&mp_builtin_ord_obj);
155 mp_map_add_qstr(&map_builtins, MP_QSTR_pow, (mp_obj_t)&mp_builtin_pow_obj);
156 mp_map_add_qstr(&map_builtins, MP_QSTR_print, (mp_obj_t)&mp_builtin_print_obj);
157 mp_map_add_qstr(&map_builtins, MP_QSTR_range, (mp_obj_t)&mp_builtin_range_obj);
Damien Georgee2fb2ba2014-01-15 21:40:48 +0000158 mp_map_add_qstr(&map_builtins, MP_QSTR_repr, (mp_obj_t)&mp_builtin_repr_obj);
John R. Lenton5c768392014-01-13 05:12:50 +0000159 mp_map_add_qstr(&map_builtins, MP_QSTR_sorted, (mp_obj_t)&mp_builtin_sorted_obj);
Damien George23005372014-01-13 19:39:01 +0000160 mp_map_add_qstr(&map_builtins, MP_QSTR_sum, (mp_obj_t)&mp_builtin_sum_obj);
Paul Sokolovsky36c44992014-01-13 19:20:46 +0200161 mp_map_add_qstr(&map_builtins, MP_QSTR_str, (mp_obj_t)&mp_builtin_str_obj);
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200162 mp_map_add_qstr(&map_builtins, MP_QSTR_bytearray, (mp_obj_t)&mp_builtin_bytearray_obj);
Damien429d7192013-10-04 19:53:11 +0100163
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200164#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +0200165 // Precreate sys module, so "import sys" didn't throw exceptions.
Damien George55baff42014-01-21 21:40:13 +0000166 mp_obj_new_module(QSTR_FROM_STR_STATIC("sys"));
Paul Sokolovskydcac8802014-01-16 19:19:50 +0200167#endif
168
Damien George91d457a2014-01-20 10:30:24 +0000169 mp_module_micropython_init();
Paul Sokolovsky440cc3f2014-01-20 01:53:15 +0200170
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200171 // TODO: wastes one mp_code_t structure in mem
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000172 next_unique_code_id = 1; // 0 indicates "no code"
John R. Lenton9c83ec02014-01-07 23:06:46 +0000173 unique_codes_alloc = 0;
Damien429d7192013-10-04 19:53:11 +0100174 unique_codes = NULL;
175
Damien0446a0d2013-11-17 13:16:36 +0000176#ifdef WRITE_CODE
177 fp_write_code = fopen("out-code", "wb");
Damiena1ddfcc2013-10-10 23:25:50 +0100178#endif
Damien429d7192013-10-04 19:53:11 +0100179}
180
Damien8b3a7c22013-10-23 20:20:17 +0100181void rt_deinit(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000182 m_del(mp_code_t, unique_codes, unique_codes_alloc);
Paul Sokolovskyc1d200e2014-01-25 00:04:14 +0200183 mp_map_free(map_globals);
184 mp_map_deinit(&map_loaded_modules);
185 mp_map_deinit(&map_builtins);
Damien0446a0d2013-11-17 13:16:36 +0000186#ifdef WRITE_CODE
187 if (fp_write_code != NULL) {
188 fclose(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100189 }
Damiena1ddfcc2013-10-10 23:25:50 +0100190#endif
Damien429d7192013-10-04 19:53:11 +0100191}
192
Damien Georged0691cc2014-01-29 20:30:52 +0000193uint rt_get_unique_code_id(void) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000194 return next_unique_code_id++;
Damien429d7192013-10-04 19:53:11 +0100195}
196
Damien8b3a7c22013-10-23 20:20:17 +0100197static void alloc_unique_codes(void) {
John R. Lenton9c83ec02014-01-07 23:06:46 +0000198 if (next_unique_code_id > unique_codes_alloc) {
Damien Georged0691cc2014-01-29 20:30:52 +0000199 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 +0000200 // increase size of unique_codes table
201 unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
Damien Georged0691cc2014-01-29 20:30:52 +0000202 for (uint i = unique_codes_alloc; i < next_unique_code_id; i++) {
Damiend99b0522013-12-21 18:17:45 +0000203 unique_codes[i].kind = MP_CODE_NONE;
Damien826005c2013-10-05 23:17:28 +0100204 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000205 unique_codes_alloc = next_unique_code_id;
Damien429d7192013-10-04 19:53:11 +0100206 }
Damien826005c2013-10-05 23:17:28 +0100207}
208
Damien Georged0691cc2014-01-29 20:30:52 +0000209void rt_assign_byte_code(uint 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 +0100210 alloc_unique_codes();
211
John R. Lenton9c83ec02014-01-07 23:06:46 +0000212 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 +0000213 unique_codes[unique_code_id].kind = MP_CODE_BYTE;
Damienbd254452013-10-16 20:39:12 +0100214 unique_codes[unique_code_id].is_generator = is_generator;
Damien Georged0691cc2014-01-29 20:30:52 +0000215 unique_codes[unique_code_id].n_args = n_args;
216 unique_codes[unique_code_id].n_state = n_locals + n_stack;
Damien826005c2013-10-05 23:17:28 +0100217 unique_codes[unique_code_id].u_byte.code = code;
218 unique_codes[unique_code_id].u_byte.len = len;
219
Damien9ecbcff2013-12-11 00:41:43 +0000220 //printf("byte code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000221
222#ifdef DEBUG_PRINT
Damien George08d07552014-01-29 18:58:52 +0000223 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 +0000224 for (int i = 0; i < 128 && i < len; i++) {
225 if (i > 0 && i % 16 == 0) {
226 DEBUG_printf("\n");
227 }
228 DEBUG_printf(" %02x", code[i]);
229 }
230 DEBUG_printf("\n");
Damien Georgecbd2f742014-01-19 11:48:48 +0000231#if MICROPY_DEBUG_PRINTERS
232 mp_byte_code_print(code, len);
Damien George062478e2014-01-09 20:57:50 +0000233#endif
Damien0446a0d2013-11-17 13:16:36 +0000234
235#ifdef WRITE_CODE
236 if (fp_write_code != NULL) {
237 fwrite(code, len, 1, fp_write_code);
238 fflush(fp_write_code);
239 }
240#endif
241#endif
Damien826005c2013-10-05 23:17:28 +0100242}
243
Damien Georged0691cc2014-01-29 20:30:52 +0000244void rt_assign_native_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100245 alloc_unique_codes();
246
John R. Lenton9c83ec02014-01-07 23:06:46 +0000247 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 +0000248 unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
Damienbd254452013-10-16 20:39:12 +0100249 unique_codes[unique_code_id].is_generator = false;
Damien Georged0691cc2014-01-29 20:30:52 +0000250 unique_codes[unique_code_id].n_args = n_args;
251 unique_codes[unique_code_id].n_state = 0;
Damien429d7192013-10-04 19:53:11 +0100252 unique_codes[unique_code_id].u_native.fun = fun;
253
Damien George8cc96a32013-12-30 18:23:50 +0000254 //printf("native code: %d bytes\n", len);
Damien0446a0d2013-11-17 13:16:36 +0000255
Damiena1ddfcc2013-10-10 23:25:50 +0100256#ifdef DEBUG_PRINT
Damien429d7192013-10-04 19:53:11 +0100257 DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
258 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
259 for (int i = 0; i < 128 && i < len; i++) {
260 if (i > 0 && i % 16 == 0) {
261 DEBUG_printf("\n");
262 }
263 DEBUG_printf(" %02x", fun_data[i]);
264 }
265 DEBUG_printf("\n");
266
Damien0446a0d2013-11-17 13:16:36 +0000267#ifdef WRITE_CODE
268 if (fp_write_code != NULL) {
269 fwrite(fun_data, len, 1, fp_write_code);
270 fflush(fp_write_code);
Damien429d7192013-10-04 19:53:11 +0100271 }
Damiena1ddfcc2013-10-10 23:25:50 +0100272#endif
273#endif
Damien429d7192013-10-04 19:53:11 +0100274}
275
Damien Georged0691cc2014-01-29 20:30:52 +0000276void rt_assign_inline_asm_code(uint unique_code_id, void *fun, uint len, int n_args) {
Damien826005c2013-10-05 23:17:28 +0100277 alloc_unique_codes();
Damien429d7192013-10-04 19:53:11 +0100278
John R. Lenton9c83ec02014-01-07 23:06:46 +0000279 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 +0000280 unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
Damienbd254452013-10-16 20:39:12 +0100281 unique_codes[unique_code_id].is_generator = false;
Damien Georged0691cc2014-01-29 20:30:52 +0000282 unique_codes[unique_code_id].n_args = n_args;
283 unique_codes[unique_code_id].n_state = 0;
Damien826005c2013-10-05 23:17:28 +0100284 unique_codes[unique_code_id].u_inline_asm.fun = fun;
285
Damiena1ddfcc2013-10-10 23:25:50 +0100286#ifdef DEBUG_PRINT
Damien826005c2013-10-05 23:17:28 +0100287 DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args);
288 byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
289 for (int i = 0; i < 128 && i < len; i++) {
290 if (i > 0 && i % 16 == 0) {
291 DEBUG_printf("\n");
292 }
293 DEBUG_printf(" %02x", fun_data[i]);
294 }
295 DEBUG_printf("\n");
296
Damien0446a0d2013-11-17 13:16:36 +0000297#ifdef WRITE_CODE
298 if (fp_write_code != NULL) {
299 fwrite(fun_data, len, 1, fp_write_code);
Damien826005c2013-10-05 23:17:28 +0100300 }
Damiena1ddfcc2013-10-10 23:25:50 +0100301#endif
302#endif
Damien429d7192013-10-04 19:53:11 +0100303}
304
Damiend99b0522013-12-21 18:17:45 +0000305int rt_is_true(mp_obj_t arg) {
306 DEBUG_OP_printf("is true %p\n", arg);
Damien George09a0c642014-01-30 10:05:33 +0000307 if (arg == mp_const_false) {
308 return 0;
309 } else if (arg == mp_const_true) {
310 return 1;
311 } else if (arg == mp_const_none) {
312 return 0;
313 } else if (MP_OBJ_IS_SMALL_INT(arg)) {
Damiend99b0522013-12-21 18:17:45 +0000314 if (MP_OBJ_SMALL_INT_VALUE(arg) == 0) {
315 return 0;
316 } else {
317 return 1;
318 }
Damiend99b0522013-12-21 18:17:45 +0000319 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200320 mp_obj_type_t *type = mp_obj_get_type(arg);
321 if (type->unary_op != NULL) {
322 mp_obj_t result = type->unary_op(RT_UNARY_OP_BOOL, arg);
Damien George09a0c642014-01-30 10:05:33 +0000323 if (result != MP_OBJ_NULL) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200324 return result == mp_const_true;
325 }
326 }
327
Damien Georgee4b6a072014-01-28 23:27:35 +0000328 mp_obj_t len = mp_obj_len_maybe(arg);
329 if (len != MP_OBJ_NULL) {
330 // obj has a length, truth determined if len != 0
331 return len != MP_OBJ_NEW_SMALL_INT(0);
332 } else {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200333 // any other obj is true per Python semantics
Damien Georgee4b6a072014-01-28 23:27:35 +0000334 return 1;
335 }
Damiend99b0522013-12-21 18:17:45 +0000336 }
337}
338
339mp_obj_t rt_list_append(mp_obj_t self_in, mp_obj_t arg) {
340 return mp_obj_list_append(self_in, arg);
341}
342
Damien7410e442013-11-02 19:47:57 +0000343#define PARSE_DEC_IN_INTG (1)
344#define PARSE_DEC_IN_FRAC (2)
345#define PARSE_DEC_IN_EXP (3)
346
Damiend99b0522013-12-21 18:17:45 +0000347mp_obj_t rt_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000348#if MICROPY_ENABLE_FLOAT
349 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
350 const char *s = qstr_str(qstr);
351 int in = PARSE_DEC_IN_INTG;
Damiend99b0522013-12-21 18:17:45 +0000352 mp_float_t dec_val = 0;
Damien7410e442013-11-02 19:47:57 +0000353 bool exp_neg = false;
354 int exp_val = 0;
355 int exp_extra = 0;
356 bool imag = false;
357 for (; *s; s++) {
358 int dig = *s;
359 if ('0' <= dig && dig <= '9') {
360 dig -= '0';
361 if (in == PARSE_DEC_IN_EXP) {
362 exp_val = 10 * exp_val + dig;
363 } else {
364 dec_val = 10 * dec_val + dig;
365 if (in == PARSE_DEC_IN_FRAC) {
366 exp_extra -= 1;
367 }
368 }
369 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
370 in = PARSE_DEC_IN_FRAC;
371 } else if (in != PARSE_DEC_IN_EXP && (dig == 'E' || dig == 'e')) {
372 in = PARSE_DEC_IN_EXP;
373 if (s[1] == '+') {
374 s++;
375 } else if (s[1] == '-') {
376 s++;
377 exp_neg = true;
378 }
379 } else if (dig == 'J' || dig == 'j') {
380 s++;
381 imag = true;
382 break;
383 } else {
384 // unknown character
385 break;
386 }
387 }
388 if (*s != 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000389 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "invalid syntax for number"));
Damien7410e442013-11-02 19:47:57 +0000390 }
391 if (exp_neg) {
392 exp_val = -exp_val;
393 }
394 exp_val += exp_extra;
395 for (; exp_val > 0; exp_val--) {
396 dec_val *= 10;
397 }
398 for (; exp_val < 0; exp_val++) {
399 dec_val *= 0.1;
400 }
401 if (imag) {
Damiend99b0522013-12-21 18:17:45 +0000402 return mp_obj_new_complex(0, dec_val);
Damien7410e442013-11-02 19:47:57 +0000403 } else {
Damiend99b0522013-12-21 18:17:45 +0000404 return mp_obj_new_float(dec_val);
Damien7410e442013-11-02 19:47:57 +0000405 }
406#else
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000407 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_SyntaxError, "decimal numbers not supported"));
Damien7410e442013-11-02 19:47:57 +0000408#endif
409}
410
Damiend99b0522013-12-21 18:17:45 +0000411mp_obj_t rt_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100412 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000413 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100414}
415
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200416mp_obj_t rt_load_const_bytes(qstr qstr) {
417 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
418 uint len;
419 const byte *data = qstr_data(qstr, &len);
420 return mp_obj_new_bytes(data, len);
421}
422
Damiend99b0522013-12-21 18:17:45 +0000423mp_obj_t rt_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100424 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100425 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000426 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100427 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000428 elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100429 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000430 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damiena3977762013-10-09 23:10:10 +0100431 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000432 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 +0100433 }
434 }
435 }
436 return elem->value;
437}
438
Damiend99b0522013-12-21 18:17:45 +0000439mp_obj_t rt_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100440 // logic: search globals, builtins
441 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000442 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100443 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000444 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100445 if (elem == NULL) {
Damien George6c73ca12014-01-08 18:11:23 +0000446 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 +0100447 }
448 }
449 return elem->value;
450}
451
Damiend99b0522013-12-21 18:17:45 +0000452mp_obj_t rt_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100453 DEBUG_OP_printf("load_build_class\n");
Damien George38a2da62014-01-08 17:33:12 +0000454 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 +0100455 if (elem == NULL) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000456 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
Damien429d7192013-10-04 19:53:11 +0100457 }
458 return elem->value;
459}
460
Damiend99b0522013-12-21 18:17:45 +0000461mp_obj_t rt_get_cell(mp_obj_t cell) {
462 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000463}
464
Damiend99b0522013-12-21 18:17:45 +0000465void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
466 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000467}
468
Damiend99b0522013-12-21 18:17:45 +0000469void rt_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100470 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000471 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 +0100472}
473
Damiend99b0522013-12-21 18:17:45 +0000474void rt_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100475 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000476 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 +0100477}
478
Damiend99b0522013-12-21 18:17:45 +0000479mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000480 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damiend99b0522013-12-21 18:17:45 +0000481 if (MP_OBJ_IS_SMALL_INT(arg)) {
482 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000483 switch (op) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200484 case RT_UNARY_OP_BOOL: return MP_BOOL(val != 0);
Damien7410e442013-11-02 19:47:57 +0000485 case RT_UNARY_OP_POSITIVE: break;
486 case RT_UNARY_OP_NEGATIVE: val = -val; break;
487 case RT_UNARY_OP_INVERT: val = ~val; break;
488 default: assert(0); val = 0;
489 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200490 if (MP_OBJ_FITS_SMALL_INT(val)) {
Damiend99b0522013-12-21 18:17:45 +0000491 return MP_OBJ_NEW_SMALL_INT(val);
Damien7410e442013-11-02 19:47:57 +0000492 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200493 return mp_obj_new_int(val);
Damien George1e708fe2014-01-23 18:27:51 +0000494 } else {
495 mp_obj_type_t *type = mp_obj_get_type(arg);
496 if (type->unary_op != NULL) {
497 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000498 if (result != NULL) {
499 return result;
500 }
Damien7410e442013-11-02 19:47:57 +0000501 }
Damiend99b0522013-12-21 18:17:45 +0000502 // TODO specify in error message what the operator is
Damien George1e708fe2014-01-23 18:27:51 +0000503 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "bad operand type for unary operator: '%s'", type->name));
Damien7410e442013-11-02 19:47:57 +0000504 }
Damien429d7192013-10-04 19:53:11 +0100505}
506
Damiend99b0522013-12-21 18:17:45 +0000507mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100508 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000509
510 // TODO correctly distinguish inplace operators for mutable objects
511 // lookup logic that CPython uses for +=:
512 // check for implemented +=
513 // then check for implemented +
514 // then check for implemented seq.inplace_concat
515 // then check for implemented seq.concat
516 // then fail
517 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
518
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200519 // deal with is, is not
520 if (op == RT_COMPARE_OP_IS) {
521 // TODO: may need to handle strings specially, CPython appears to
522 // assume all strings are interned (so "is" == "==" for strings)
523 return MP_BOOL(lhs == rhs);
524 }
525 if (op == RT_COMPARE_OP_IS_NOT) {
526 // TODO: may need to handle strings specially, CPython appears to
527 // assume all strings are interned (so "is" == "==" for strings)
528 return MP_BOOL(lhs != rhs);
529 }
530
Damien Georgebcbeea02014-01-11 10:47:22 +0000531 // deal with == and != for all types
532 if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) {
533 if (mp_obj_equal(lhs, rhs)) {
534 if (op == RT_COMPARE_OP_EQUAL) {
535 return mp_const_true;
536 } else {
537 return mp_const_false;
538 }
539 } else {
540 if (op == RT_COMPARE_OP_EQUAL) {
541 return mp_const_false;
542 } else {
543 return mp_const_true;
544 }
545 }
546 }
547
548 // deal with exception_match for all types
549 if (op == RT_COMPARE_OP_EXCEPTION_MATCH) {
550 // TODO properly! at the moment it just compares the exception identifier for equality
551 if (MP_OBJ_IS_TYPE(lhs, &exception_type) && MP_OBJ_IS_TYPE(rhs, &exception_type)) {
552 if (mp_obj_exception_get_type(lhs) == mp_obj_exception_get_type(rhs)) {
553 return mp_const_true;
554 } else {
555 return mp_const_false;
556 }
557 }
558 }
559
Damien George1a9951d2014-01-06 22:13:00 +0000560 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000561 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000562 if (MP_OBJ_IS_SMALL_INT(rhs)) {
563 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
564 switch (op) {
565 case RT_BINARY_OP_OR:
566 case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
567 case RT_BINARY_OP_XOR:
568 case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
569 case RT_BINARY_OP_AND:
570 case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
571 case RT_BINARY_OP_LSHIFT:
572 case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break;
573 case RT_BINARY_OP_RSHIFT:
574 case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break;
575 case RT_BINARY_OP_ADD:
576 case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
577 case RT_BINARY_OP_SUBTRACT:
578 case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
579 case RT_BINARY_OP_MULTIPLY:
580 case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
581 case RT_BINARY_OP_FLOOR_DIVIDE:
582 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break;
583 #if MICROPY_ENABLE_FLOAT
584 case RT_BINARY_OP_TRUE_DIVIDE:
585 case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
586 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000587
Damien George1a9951d2014-01-06 22:13:00 +0000588 // TODO implement modulo as specified by Python
589 case RT_BINARY_OP_MODULO:
590 case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break;
Damiena3dcd9e2013-12-17 21:35:38 +0000591
Damien George1a9951d2014-01-06 22:13:00 +0000592 // TODO check for negative power, and overflow
593 case RT_BINARY_OP_POWER:
594 case RT_BINARY_OP_INPLACE_POWER:
595 {
596 int ans = 1;
597 while (rhs_val > 0) {
598 if (rhs_val & 1) {
599 ans *= lhs_val;
600 }
601 lhs_val *= lhs_val;
602 rhs_val /= 2;
Damiena3dcd9e2013-12-17 21:35:38 +0000603 }
Damien George1a9951d2014-01-06 22:13:00 +0000604 lhs_val = ans;
605 break;
Damien4ebb32f2013-11-02 14:33:10 +0000606 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000607 case RT_COMPARE_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
608 case RT_COMPARE_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
609 case RT_COMPARE_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
610 case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000611
Damien George1a9951d2014-01-06 22:13:00 +0000612 default: assert(0);
613 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200614 // TODO: We just should make mp_obj_new_int() inline and use that
615 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000616 return MP_OBJ_NEW_SMALL_INT(lhs_val);
617 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200618 return mp_obj_new_int(lhs_val);
Damien George3f759b72014-01-31 00:42:12 +0000619#if MICROPY_ENABLE_FLOAT
Damien George1a9951d2014-01-06 22:13:00 +0000620 } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
621 return mp_obj_float_binary_op(op, lhs_val, rhs);
622 } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {
623 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000624#endif
Damien429d7192013-10-04 19:53:11 +0100625 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000626 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000627
John R. Lentonc1bef212014-01-11 12:39:33 +0000628 /* deal with `in` and `not in`
629 *
630 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
631 * needs to go below
632 */
633 if (op == RT_COMPARE_OP_IN || op == RT_COMPARE_OP_NOT_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000634 mp_obj_type_t *type = mp_obj_get_type(rhs);
635 if (type->binary_op != NULL) {
636 mp_obj_t res = type->binary_op(op, rhs, lhs);
637 if (res != NULL) {
638 return res;
639 }
640 }
641 if (type->getiter != NULL) {
642 /* second attempt, walk the iterator */
643 mp_obj_t next = NULL;
644 mp_obj_t iter = rt_getiter(rhs);
645 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
646 if (mp_obj_equal(next, lhs)) {
647 return MP_BOOL(op == RT_COMPARE_OP_IN);
John R. Lentonb8698fc2014-01-11 00:58:59 +0000648 }
Damien7410e442013-11-02 19:47:57 +0000649 }
Damien George5fa93b62014-01-22 14:35:10 +0000650 return MP_BOOL(op != RT_COMPARE_OP_IN);
Damien7410e442013-11-02 19:47:57 +0000651 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000652
653 nlr_jump(mp_obj_new_exception_msg_varg(
654 MP_QSTR_TypeError, "'%s' object is not iterable",
655 mp_obj_get_type_str(rhs)));
656 return mp_const_none;
657 }
658
Damien George5fa93b62014-01-22 14:35:10 +0000659 // generic binary_op supplied by type
660 mp_obj_type_t *type = mp_obj_get_type(lhs);
661 if (type->binary_op != NULL) {
662 mp_obj_t result = type->binary_op(op, lhs, rhs);
663 if (result != MP_OBJ_NULL) {
664 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000665 }
Damien429d7192013-10-04 19:53:11 +0100666 }
Damiend99b0522013-12-21 18:17:45 +0000667
Damien George5fa93b62014-01-22 14:35:10 +0000668 // TODO implement dispatch for reverse binary ops
669
Damiend99b0522013-12-21 18:17:45 +0000670 // TODO specify in error message what the operator is
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200671 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError,
672 "unsupported operand types for binary operator: '%s', '%s'",
673 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000674 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100675}
676
Paul Sokolovsky90750022014-02-01 15:05:04 +0200677mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args) {
Damienb05d7072013-10-05 13:37:10 +0100678 DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id);
679 if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) {
Damien429d7192013-10-04 19:53:11 +0100680 // illegal code id
Damiend99b0522013-12-21 18:17:45 +0000681 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100682 }
Damiend99b0522013-12-21 18:17:45 +0000683
684 // make the function, depending on the code kind
685 mp_code_t *c = &unique_codes[unique_code_id];
686 mp_obj_t fun;
Damien429d7192013-10-04 19:53:11 +0100687 switch (c->kind) {
Damiend99b0522013-12-21 18:17:45 +0000688 case MP_CODE_BYTE:
Paul Sokolovsky90750022014-02-01 15:05:04 +0200689 fun = mp_obj_new_fun_bc(c->n_args, def_args, c->n_state, c->u_byte.code);
Damien826005c2013-10-05 23:17:28 +0100690 break;
Damiend99b0522013-12-21 18:17:45 +0000691 case MP_CODE_NATIVE:
Damien Georgef62d33a2014-01-13 19:50:05 +0000692 fun = rt_make_function_n(c->n_args, c->u_native.fun);
Damien429d7192013-10-04 19:53:11 +0100693 break;
Damiend99b0522013-12-21 18:17:45 +0000694 case MP_CODE_INLINE_ASM:
695 fun = mp_obj_new_fun_asm(c->n_args, c->u_inline_asm.fun);
Damien429d7192013-10-04 19:53:11 +0100696 break;
697 default:
698 assert(0);
Damiend99b0522013-12-21 18:17:45 +0000699 fun = mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100700 }
Damienbd254452013-10-16 20:39:12 +0100701
702 // check for generator functions and if so wrap in generator object
703 if (c->is_generator) {
Damien Georged0691cc2014-01-29 20:30:52 +0000704 fun = mp_obj_new_gen_wrap(fun);
Damienbd254452013-10-16 20:39:12 +0100705 }
706
Damiend99b0522013-12-21 18:17:45 +0000707 return fun;
Damien429d7192013-10-04 19:53:11 +0100708}
709
Damiend99b0522013-12-21 18:17:45 +0000710mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple) {
Damien George6baf76e2013-12-30 22:32:17 +0000711 DEBUG_OP_printf("make_closure_from_id %d\n", unique_code_id);
Damiend99b0522013-12-21 18:17:45 +0000712 // make function object
Paul Sokolovsky90750022014-02-01 15:05:04 +0200713 mp_obj_t ffun = rt_make_function_from_id(unique_code_id, MP_OBJ_NULL);
Damien9ecbcff2013-12-11 00:41:43 +0000714 // wrap function in closure object
Damiend99b0522013-12-21 18:17:45 +0000715 return mp_obj_new_closure(ffun, closure_tuple);
Damien9ecbcff2013-12-11 00:41:43 +0000716}
717
Damiend99b0522013-12-21 18:17:45 +0000718mp_obj_t rt_call_function_0(mp_obj_t fun) {
Damien George20006db2014-01-18 14:10:48 +0000719 return rt_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100720}
721
Damiend99b0522013-12-21 18:17:45 +0000722mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg) {
Damien George20006db2014-01-18 14:10:48 +0000723 return rt_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100724}
725
Damiend99b0522013-12-21 18:17:45 +0000726mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
727 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000728 args[0] = arg1;
729 args[1] = arg2;
730 return rt_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100731}
732
Damien George20006db2014-01-18 14:10:48 +0000733// args contains, eg: arg0 arg1 key0 value0 key1 value1
734mp_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 +0000735 // TODO improve this: fun object can specify its type and we parse here the arguments,
736 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100737
John R. Lenton9c83ec02014-01-07 23:06:46 +0000738 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
739
Damien George8b56beb2014-01-31 23:49:49 +0000740 // get the type
741 mp_obj_type_t *type = mp_obj_get_type(fun_in);
742
743 // do the call
744 if (type->call != NULL) {
745 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000746 } else {
Damien George8b56beb2014-01-31 23:49:49 +0000747 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not callable", type->name));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000748 }
Damien86c7fc72013-11-26 15:16:41 +0000749}
750
Damien George20006db2014-01-18 14:10:48 +0000751// 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)
752// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damiend99b0522013-12-21 18:17:45 +0000753mp_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 +0000754 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);
755 int adjust = (args[1] == NULL) ? 0 : 1;
756 return rt_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000757}
758
Damiend99b0522013-12-21 18:17:45 +0000759mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000760 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100761}
762
Damiend99b0522013-12-21 18:17:45 +0000763mp_obj_t rt_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000764 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100765}
766
Damiend99b0522013-12-21 18:17:45 +0000767mp_obj_t rt_build_set(int n_args, mp_obj_t *items) {
768 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100769}
770
Damiend99b0522013-12-21 18:17:45 +0000771mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000772 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100773 return set;
774}
775
Damien George932bf1c2014-01-18 23:42:49 +0000776// unpacked items are stored in reverse order into the array pointed to by items
Damiend99b0522013-12-21 18:17:45 +0000777void rt_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
778 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type) || MP_OBJ_IS_TYPE(seq_in, &list_type)) {
779 uint seq_len;
780 mp_obj_t *seq_items;
781 if (MP_OBJ_IS_TYPE(seq_in, &tuple_type)) {
782 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
783 } else {
784 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000785 }
Damiend99b0522013-12-21 18:17:45 +0000786 if (seq_len < num) {
Damien George6c73ca12014-01-08 18:11:23 +0000787 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 +0000788 } else if (seq_len > num) {
Damien George6c73ca12014-01-08 18:11:23 +0000789 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 +0000790 }
Damien George932bf1c2014-01-18 23:42:49 +0000791 for (uint i = 0; i < num; i++) {
792 items[i] = seq_items[num - 1 - i];
793 }
Damien86c7fc72013-11-26 15:16:41 +0000794 } else {
795 // TODO call rt_getiter and extract via rt_iternext
Damien George6c73ca12014-01-08 18:11:23 +0000796 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 +0000797 }
798}
799
Damiend99b0522013-12-21 18:17:45 +0000800mp_obj_t rt_build_map(int n_args) {
801 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100802}
803
Damiend99b0522013-12-21 18:17:45 +0000804mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
805 // map should always be a dict
806 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100807}
808
Damiend99b0522013-12-21 18:17:45 +0000809mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000810 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
811 // use load_method
812 mp_obj_t dest[2];
813 rt_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000814 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000815 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000816 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000817 } else {
818 // load_method returned a method, so build a bound method object
819 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000820 }
Damiend99b0522013-12-21 18:17:45 +0000821}
822
Damien George7c9c6672014-01-25 00:17:36 +0000823// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
824// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
825// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
826static void rt_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000827 // clear output to indicate no attribute/method found yet
828 dest[0] = MP_OBJ_NULL;
829 dest[1] = MP_OBJ_NULL;
830
831 // get the type
832 mp_obj_type_t *type = mp_obj_get_type(base);
833
834 // if this type can do its own load, then call it
835 if (type->load_attr != NULL) {
836 type->load_attr(base, attr, dest);
837 }
838
839 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000840 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000841 if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000842 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
843 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000844 } else if (type->load_attr == NULL) {
845 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000846 // this is a lookup in the object (ie not class or type)
Damien George062478e2014-01-09 20:57:50 +0000847 const mp_method_t *meth = type->methods;
848 if (meth != NULL) {
849 for (; meth->name != NULL; meth++) {
850 if (strcmp(meth->name, qstr_str(attr)) == 0) {
Damien Georgeeae16442014-01-11 19:22:29 +0000851 // check if the methods are functions, static or class methods
852 // see http://docs.python.org/3.3/howto/descriptor.html
853 if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_staticmethod)) {
854 // return just the function
Damien George20006db2014-01-18 14:10:48 +0000855 dest[0] = ((mp_obj_staticmethod_t*)meth->fun)->fun;
Damien Georgeeae16442014-01-11 19:22:29 +0000856 } else if (MP_OBJ_IS_TYPE(meth->fun, &mp_type_classmethod)) {
857 // return a bound method, with self being the type of this object
Damien George20006db2014-01-18 14:10:48 +0000858 dest[0] = ((mp_obj_classmethod_t*)meth->fun)->fun;
859 dest[1] = mp_obj_get_type(base);
Damien Georgeeae16442014-01-11 19:22:29 +0000860 } else {
861 // return a bound method, with self being this object
Damien George20006db2014-01-18 14:10:48 +0000862 dest[0] = (mp_obj_t)meth->fun;
863 dest[1] = base;
Damien Georgeeae16442014-01-11 19:22:29 +0000864 }
Damien George062478e2014-01-09 20:57:50 +0000865 break;
866 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000867 }
Damiend57eba52013-11-02 23:58:14 +0000868 }
869 }
Damiena3977762013-10-09 23:10:10 +0100870 }
Damien George7c9c6672014-01-25 00:17:36 +0000871}
Damiena3977762013-10-09 23:10:10 +0100872
Damien George7c9c6672014-01-25 00:17:36 +0000873void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
874 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
875
876 rt_load_method_maybe(base, attr, dest);
877
878 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000879 // no attribute/method called attr
880 // following CPython, we give a more detailed error message for type objects
881 if (MP_OBJ_IS_TYPE(base, &mp_const_type)) {
882 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)));
883 } else {
884 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)));
885 }
886 }
Damiena3977762013-10-09 23:10:10 +0100887}
888
Damiend99b0522013-12-21 18:17:45 +0000889void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100890 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000891 mp_obj_type_t *type = mp_obj_get_type(base);
892 if (type->store_attr != NULL) {
893 if (type->store_attr(base, attr, value)) {
894 return;
895 }
Damiena3977762013-10-09 23:10:10 +0100896 }
Damien George062478e2014-01-09 20:57:50 +0000897 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 +0100898}
899
Damiend99b0522013-12-21 18:17:45 +0000900void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100901 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damiend99b0522013-12-21 18:17:45 +0000902 if (MP_OBJ_IS_TYPE(base, &list_type)) {
Damien429d7192013-10-04 19:53:11 +0100903 // list store
Damiend99b0522013-12-21 18:17:45 +0000904 mp_obj_list_store(base, index, value);
905 } else if (MP_OBJ_IS_TYPE(base, &dict_type)) {
906 // dict store
907 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100908 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200909 mp_obj_type_t *type = mp_obj_get_type(base);
910 if (type->store_item != NULL) {
911 bool r = type->store_item(base, index, value);
912 if (r) {
913 return;
914 }
915 // TODO: call base classes here?
916 }
917 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 +0100918 }
919}
920
Damiend99b0522013-12-21 18:17:45 +0000921mp_obj_t rt_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000922 mp_obj_type_t *type = mp_obj_get_type(o_in);
923 if (type->getiter != NULL) {
924 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100925 } else {
Damien George7c9c6672014-01-25 00:17:36 +0000926 // check for __getitem__ method
927 mp_obj_t dest[2];
928 rt_load_method_maybe(o_in, qstr_from_str("__getitem__"), dest);
929 if (dest[0] != MP_OBJ_NULL) {
930 // __getitem__ exists, create an iterator
931 return mp_obj_new_getitem_iter(dest);
932 } else {
933 // object not iterable
934 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not iterable", type->name));
935 }
Damience89a212013-10-15 22:25:17 +0100936 }
937}
938
Damiend99b0522013-12-21 18:17:45 +0000939mp_obj_t rt_iternext(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000940 mp_obj_type_t *type = mp_obj_get_type(o_in);
941 if (type->iternext != NULL) {
942 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100943 } else {
Damien George5fa93b62014-01-22 14:35:10 +0000944 nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "'%s' object is not an iterator", type->name));
Damience89a212013-10-15 22:25:17 +0100945 }
946}
947
Damiend99b0522013-12-21 18:17:45 +0000948mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damiendb4c3612013-12-10 17:27:24 +0000949 // build args array
Damiend99b0522013-12-21 18:17:45 +0000950 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +0000951 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +0000952 args[1] = mp_const_none; // TODO should be globals
953 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000954 args[3] = fromlist;
955 args[4] = level; // must be 0; we don't yet support other values
956
957 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000958 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000959}
960
Damiend99b0522013-12-21 18:17:45 +0000961mp_obj_t rt_import_from(mp_obj_t module, qstr name) {
962 mp_obj_t x = rt_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000963 /* TODO convert AttributeError to ImportError
964 if (fail) {
965 (ImportError, "cannot import name %s", qstr_str(name), NULL)
966 }
967 */
968 return x;
969}
970
Damien George66028ab2014-01-03 14:03:48 +0000971mp_map_t *rt_locals_get(void) {
972 return map_locals;
973}
974
975void rt_locals_set(mp_map_t *m) {
976 DEBUG_OP_printf("rt_locals_set(%p)\n", m);
977 map_locals = m;
978}
979
980mp_map_t *rt_globals_get(void) {
981 return map_globals;
982}
983
984void rt_globals_set(mp_map_t *m) {
985 DEBUG_OP_printf("rt_globals_set(%p)\n", m);
986 map_globals = m;
987}
988
Damien George0d028742014-01-22 23:59:20 +0000989mp_map_t *rt_loaded_modules_get(void) {
990 return &map_loaded_modules;
991}
992
Damien6ba13142013-11-02 20:34:54 +0000993// these must correspond to the respective enum
Damiendf4b4f32013-10-19 18:28:01 +0100994void *const rt_fun_table[RT_F_NUMBER_OF] = {
Damien6ba13142013-11-02 20:34:54 +0000995 rt_load_const_dec,
Damien429d7192013-10-04 19:53:11 +0100996 rt_load_const_str,
997 rt_load_name,
998 rt_load_global,
Damien7f5dacf2013-10-10 11:24:39 +0100999 rt_load_build_class,
Damien429d7192013-10-04 19:53:11 +01001000 rt_load_attr,
1001 rt_load_method,
1002 rt_store_name,
Damien7f5dacf2013-10-10 11:24:39 +01001003 rt_store_attr,
Damien429d7192013-10-04 19:53:11 +01001004 rt_store_subscr,
1005 rt_is_true,
1006 rt_unary_op,
Damiend2755ec2013-10-16 23:58:48 +01001007 rt_build_tuple,
Damien429d7192013-10-04 19:53:11 +01001008 rt_build_list,
Damiend2755ec2013-10-16 23:58:48 +01001009 rt_list_append,
Damien429d7192013-10-04 19:53:11 +01001010 rt_build_map,
1011 rt_store_map,
1012 rt_build_set,
Damiend2755ec2013-10-16 23:58:48 +01001013 rt_store_set,
Damien429d7192013-10-04 19:53:11 +01001014 rt_make_function_from_id,
Damien George20006db2014-01-18 14:10:48 +00001015 rt_call_function_n_kw,
1016 rt_call_method_n_kw,
Damien429d7192013-10-04 19:53:11 +01001017 rt_binary_op,
Damiend2755ec2013-10-16 23:58:48 +01001018 rt_getiter,
1019 rt_iternext,
Damien429d7192013-10-04 19:53:11 +01001020};
1021
1022/*
1023void rt_f_vector(rt_fun_kind_t fun_kind) {
1024 (rt_f_table[fun_kind])();
1025}
1026*/