blob: 489886496264f08a21639f9569d86236f809cd0e [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
2#include <string.h>
3#include <assert.h>
4
Damience89a212013-10-15 22:25:17 +01005#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01006#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +00007#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +00009#include "obj.h"
Damien George230fec72014-03-30 21:21:24 +010010#include "objtuple.h"
Damien Georgecaac5422014-03-25 14:18:18 +000011#include "objmodule.h"
Damien George20773972014-02-22 18:12:43 +000012#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
14#include "runtime.h"
Damien George2326d522014-03-27 23:26:35 +000015#include "emitglue.h"
Damien660365e2013-12-17 18:27:24 +000016#include "builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000017#include "builtintables.h"
Damien George5fa93b62014-01-22 14:35:10 +000018#include "bc.h"
Rachel Dowdallcde86312014-03-22 17:29:27 +000019#include "intdivmod.h"
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +030020#include "objgenerator.h"
Damien660365e2013-12-17 18:27:24 +000021
Damien7f5dacf2013-10-10 11:24:39 +010022#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010023#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020024#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000025#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010026#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000027#define DEBUG_printf(...) (void)0
28#define DEBUG_OP_printf(...) (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
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020032STATIC mp_map_t *map_locals;
33STATIC mp_map_t *map_globals;
34STATIC mp_map_t map_builtins;
Damienbd254452013-10-16 20:39:12 +010035
Damien George38a2da62014-01-08 17:33:12 +000036// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020037STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +000038 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
39}
40
Damien Georged17926d2014-03-30 13:35:08 +010041void mp_init(void) {
Damien George2326d522014-03-27 23:26:35 +000042 mp_emit_glue_init();
43
Damieneb19efb2013-10-10 22:06:54 +010044 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George38a2da62014-01-08 17:33:12 +000045 map_locals = map_globals = mp_map_new(1);
Damien429d7192013-10-04 19:53:11 +010046
Damienb86e3f92013-12-29 17:17:43 +000047 // init built-in hash table
Damien George38a2da62014-01-08 17:33:12 +000048 mp_map_init(&map_builtins, 3);
Damienb86e3f92013-12-29 17:17:43 +000049
Damien Georgecaac5422014-03-25 14:18:18 +000050 // init global module stuff
51 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000052
Damien Georgecaac5422014-03-25 14:18:18 +000053 // add some builtins that can't be done in ROM
54 mp_map_add_qstr(map_globals, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Damien Georgee9906ac2014-01-04 18:44:46 +000055
Paul Sokolovskydcac8802014-01-16 19:19:50 +020056#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +020057 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020058 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
59 // Avoid warning of unused var
60 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +020061#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020062 // init sys.path
63 // for efficiency, left to platform-specific startup code
Damien Georged17926d2014-03-30 13:35:08 +010064 //mp_sys_path = mp_obj_new_list(0, NULL);
65 //mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Damien429d7192013-10-04 19:53:11 +010066}
67
Damien Georged17926d2014-03-30 13:35:08 +010068void mp_deinit(void) {
Damien George2326d522014-03-27 23:26:35 +000069 mp_map_free(map_globals);
70 mp_map_deinit(&map_builtins);
71 mp_module_deinit();
72 mp_emit_glue_deinit();
Damien429d7192013-10-04 19:53:11 +010073}
74
Damien Georged17926d2014-03-30 13:35:08 +010075mp_obj_t mp_list_append(mp_obj_t self_in, mp_obj_t arg) {
Damiend99b0522013-12-21 18:17:45 +000076 return mp_obj_list_append(self_in, arg);
77}
78
Damien Georged17926d2014-03-30 13:35:08 +010079mp_obj_t mp_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +000080 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +000081 uint len;
82 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +000083 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +000084}
85
Damien Georged17926d2014-03-30 13:35:08 +010086mp_obj_t mp_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010087 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +000088 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +010089}
90
Damien Georged17926d2014-03-30 13:35:08 +010091mp_obj_t mp_load_const_bytes(qstr qstr) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020092 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
93 uint len;
94 const byte *data = qstr_data(qstr, &len);
95 return mp_obj_new_bytes(data, len);
96}
97
Damien Georged17926d2014-03-30 13:35:08 +010098mp_obj_t mp_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010099 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +0100100 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000101 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000102 if (elem != NULL) {
103 return elem->value;
104 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100105 return mp_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100106 }
Damiena3977762013-10-09 23:10:10 +0100107}
108
Damien Georged17926d2014-03-30 13:35:08 +0100109mp_obj_t mp_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100110 // logic: search globals, builtins
111 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000112 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100113 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000114 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100115 if (elem == NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000116 mp_obj_t o = mp_builtin_tables_lookup_object(qstr);
117 if (o != MP_OBJ_NULL) {
118 return o;
Damien Georgeaea532e2014-02-06 22:57:51 +0000119 }
Damien Georgec5966122014-02-15 16:10:44 +0000120 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100121 }
122 }
123 return elem->value;
124}
125
Damien Georged17926d2014-03-30 13:35:08 +0100126mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100127 DEBUG_OP_printf("load_build_class\n");
Damien Georgecaac5422014-03-25 14:18:18 +0000128 // lookup __build_class__ in dynamic table of builtins first
Damien George38a2da62014-01-08 17:33:12 +0000129 mp_map_elem_t *elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(MP_QSTR___build_class__), MP_MAP_LOOKUP);
Damien Georgeaea532e2014-02-06 22:57:51 +0000130 if (elem != NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000131 // found user-defined __build_class__, return it
Damien Georgeaea532e2014-02-06 22:57:51 +0000132 return elem->value;
133 } else {
Damien Georgecaac5422014-03-25 14:18:18 +0000134 // no user-defined __build_class__, return builtin one
Damien Georgeaea532e2014-02-06 22:57:51 +0000135 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100136 }
Damien429d7192013-10-04 19:53:11 +0100137}
138
Damien Georged17926d2014-03-30 13:35:08 +0100139mp_obj_t mp_get_cell(mp_obj_t cell) {
Damiend99b0522013-12-21 18:17:45 +0000140 return mp_obj_cell_get(cell);
Damien660365e2013-12-17 18:27:24 +0000141}
142
Damien Georged17926d2014-03-30 13:35:08 +0100143void mp_set_cell(mp_obj_t cell, mp_obj_t val) {
Damiend99b0522013-12-21 18:17:45 +0000144 mp_obj_cell_set(cell, val);
Damien660365e2013-12-17 18:27:24 +0000145}
146
Damien Georged17926d2014-03-30 13:35:08 +0100147void mp_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100148 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000149 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 +0100150}
151
Damien Georged17926d2014-03-30 13:35:08 +0100152void mp_delete_name(qstr qstr) {
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200153 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
154 mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
155}
156
Damien Georged17926d2014-03-30 13:35:08 +0100157void mp_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100158 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000159 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 +0100160}
161
Damien Georged17926d2014-03-30 13:35:08 +0100162mp_obj_t mp_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000163 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000164
Damiend99b0522013-12-21 18:17:45 +0000165 if (MP_OBJ_IS_SMALL_INT(arg)) {
166 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000167 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100168 case MP_UNARY_OP_BOOL:
Damien George9d68e9c2014-03-12 15:38:15 +0000169 return MP_BOOL(val != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100170 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000171 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100172 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000173 // check for overflow
174 if (val == MP_SMALL_INT_MIN) {
175 return mp_obj_new_int(-val);
176 } else {
177 return MP_OBJ_NEW_SMALL_INT(-val);
178 }
Damien Georged17926d2014-03-30 13:35:08 +0100179 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000180 return MP_OBJ_NEW_SMALL_INT(~val);
181 default:
182 assert(0);
183 return arg;
Damien7410e442013-11-02 19:47:57 +0000184 }
Damien George1e708fe2014-01-23 18:27:51 +0000185 } else {
186 mp_obj_type_t *type = mp_obj_get_type(arg);
187 if (type->unary_op != NULL) {
188 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000189 if (result != NULL) {
190 return result;
191 }
Damien7410e442013-11-02 19:47:57 +0000192 }
Damiend99b0522013-12-21 18:17:45 +0000193 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000194 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bad operand type for unary operator: '%s'", mp_obj_get_type_str(arg)));
Damien7410e442013-11-02 19:47:57 +0000195 }
Damien429d7192013-10-04 19:53:11 +0100196}
197
Damien Georged17926d2014-03-30 13:35:08 +0100198mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100199 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000200
201 // TODO correctly distinguish inplace operators for mutable objects
202 // lookup logic that CPython uses for +=:
203 // check for implemented +=
204 // then check for implemented +
205 // then check for implemented seq.inplace_concat
206 // then check for implemented seq.concat
207 // then fail
208 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
209
Damien George9aa2a522014-02-01 23:04:09 +0000210 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100211 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200212 return MP_BOOL(lhs == rhs);
213 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200214
Damien Georgebcbeea02014-01-11 10:47:22 +0000215 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100216 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000217 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100218 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000219 return mp_const_true;
220 } else {
221 return mp_const_false;
222 }
223 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100224 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000225 return mp_const_false;
226 } else {
227 return mp_const_true;
228 }
229 }
230 }
231
232 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100233 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000234 // rhs must be issubclass(rhs, BaseException)
235 if (mp_obj_is_exception_type(rhs)) {
236 // if lhs is an instance of an exception, then extract and use its type
237 if (mp_obj_is_exception_instance(lhs)) {
238 lhs = mp_obj_get_type(lhs);
239 }
Damien George71510152014-03-03 22:38:13 +0000240 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000241 return mp_const_true;
242 } else {
243 return mp_const_false;
244 }
245 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200246 assert(0);
247 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000248 }
249
Damien George1a9951d2014-01-06 22:13:00 +0000250 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000251 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000252 if (MP_OBJ_IS_SMALL_INT(rhs)) {
253 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000254 // This is a binary operation: lhs_val op rhs_val
255 // We need to be careful to handle overflow; see CERT INT32-C
256 // Operations that can overflow:
257 // + result always fits in machine_int_t, then handled by SMALL_INT check
258 // - result always fits in machine_int_t, then handled by SMALL_INT check
259 // * checked explicitly
260 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
261 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
262 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000263 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100264 case MP_BINARY_OP_OR:
265 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
266 case MP_BINARY_OP_XOR:
267 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
268 case MP_BINARY_OP_AND:
269 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
270 case MP_BINARY_OP_LSHIFT:
271 case MP_BINARY_OP_INPLACE_LSHIFT: {
Damien George9d68e9c2014-03-12 15:38:15 +0000272 if (rhs_val < 0) {
273 // negative shift not allowed
274 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
275 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
276 // left-shift will overflow, so use higher precision integer
277 lhs = mp_obj_new_int_from_ll(lhs_val);
278 goto generic_binary_op;
279 } else {
280 // use standard precision
281 lhs_val <<= rhs_val;
282 }
283 break;
284 }
Damien Georged17926d2014-03-30 13:35:08 +0100285 case MP_BINARY_OP_RSHIFT:
286 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000287 if (rhs_val < 0) {
288 // negative shift not allowed
289 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
290 } else {
291 // standard precision is enough for right-shift
292 lhs_val >>= rhs_val;
293 }
294 break;
Damien Georged17926d2014-03-30 13:35:08 +0100295 case MP_BINARY_OP_ADD:
296 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
297 case MP_BINARY_OP_SUBTRACT:
298 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
299 case MP_BINARY_OP_MULTIPLY:
300 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000301
302 // If long long type exists and is larger than machine_int_t, then
303 // we can use the following code to perform overflow-checked multiplication.
304 // Otherwise (eg in x64 case) we must use the branching code below.
305 #if 0
306 // compute result using long long precision
307 long long res = (long long)lhs_val * (long long)rhs_val;
308 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
309 // result overflowed SMALL_INT, so return higher precision integer
310 return mp_obj_new_int_from_ll(res);
311 } else {
312 // use standard precision
313 lhs_val = (mp_small_int_t)res;
314 }
315 #endif
316
317 if (lhs_val > 0) { // lhs_val is positive
318 if (rhs_val > 0) { // lhs_val and rhs_val are positive
319 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
320 goto mul_overflow;
321 }
322 } else { // lhs_val positive, rhs_val nonpositive
323 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
324 goto mul_overflow;
325 }
326 } // lhs_val positive, rhs_val nonpositive
327 } else { // lhs_val is nonpositive
328 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
329 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
330 goto mul_overflow;
331 }
332 } else { // lhs_val and rhs_val are nonpositive
333 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
334 goto mul_overflow;
335 }
336 } // End if lhs_val and rhs_val are nonpositive
337 } // End if lhs_val is nonpositive
338
339 // use standard precision
340 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
341
342 mul_overflow:
343 // use higher precision
344 lhs = mp_obj_new_int_from_ll(lhs_val);
345 goto generic_binary_op;
346
347 break;
348 }
Damien Georged17926d2014-03-30 13:35:08 +0100349 case MP_BINARY_OP_FLOOR_DIVIDE:
350 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Rachel Dowdall56402792014-03-22 20:19:24 +0000351 {
352 lhs_val = python_floor_divide(lhs_val, rhs_val);
353 break;
354 }
Damien George9d68e9c2014-03-12 15:38:15 +0000355 #if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100356 case MP_BINARY_OP_TRUE_DIVIDE:
357 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000358 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000359
Damien Georged17926d2014-03-30 13:35:08 +0100360 case MP_BINARY_OP_MODULO:
361 case MP_BINARY_OP_INPLACE_MODULO:
Rachel Dowdallcde86312014-03-22 17:29:27 +0000362 {
363 lhs_val = python_modulo(lhs_val, rhs_val);
364 break;
365 }
Damien Georged17926d2014-03-30 13:35:08 +0100366 case MP_BINARY_OP_POWER:
367 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000368 if (rhs_val < 0) {
369 #if MICROPY_ENABLE_FLOAT
370 lhs = mp_obj_new_float(lhs_val);
371 goto generic_binary_op;
372 #else
373 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
374 #endif
375 } else {
376 // TODO check for overflow
377 machine_int_t ans = 1;
378 while (rhs_val > 0) {
379 if (rhs_val & 1) {
380 ans *= lhs_val;
381 }
382 lhs_val *= lhs_val;
383 rhs_val /= 2;
Damien George1a9951d2014-01-06 22:13:00 +0000384 }
Damien George9d68e9c2014-03-12 15:38:15 +0000385 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000386 }
Damien George1a9951d2014-01-06 22:13:00 +0000387 break;
Damien Georged17926d2014-03-30 13:35:08 +0100388 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
389 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
390 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
391 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000392
Damien George1a9951d2014-01-06 22:13:00 +0000393 default: assert(0);
394 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200395 // TODO: We just should make mp_obj_new_int() inline and use that
396 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000397 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000398 } else {
399 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000400 }
Damien George3f759b72014-01-31 00:42:12 +0000401#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000402 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000403 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000404 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000405 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000406#endif
Damien429d7192013-10-04 19:53:11 +0100407 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000408 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000409
Damien George9aa2a522014-02-01 23:04:09 +0000410 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000411 *
412 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000413 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000414 */
Damien Georged17926d2014-03-30 13:35:08 +0100415 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000416 mp_obj_type_t *type = mp_obj_get_type(rhs);
417 if (type->binary_op != NULL) {
418 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000419 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000420 return res;
421 }
422 }
423 if (type->getiter != NULL) {
424 /* second attempt, walk the iterator */
425 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100426 mp_obj_t iter = mp_getiter(rhs);
427 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000428 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000429 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000430 }
Damien7410e442013-11-02 19:47:57 +0000431 }
Damien George9aa2a522014-02-01 23:04:09 +0000432 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000433 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000434
435 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000436 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000437 mp_obj_get_type_str(rhs)));
438 return mp_const_none;
439 }
440
Damien George5fa93b62014-01-22 14:35:10 +0000441 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000442 mp_obj_type_t *type;
443generic_binary_op:
444 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000445 if (type->binary_op != NULL) {
446 mp_obj_t result = type->binary_op(op, lhs, rhs);
447 if (result != MP_OBJ_NULL) {
448 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000449 }
Damien429d7192013-10-04 19:53:11 +0100450 }
Damiend99b0522013-12-21 18:17:45 +0000451
Damien George5fa93b62014-01-22 14:35:10 +0000452 // TODO implement dispatch for reverse binary ops
453
Damiend99b0522013-12-21 18:17:45 +0000454 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000455 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200456 "unsupported operand types for binary operator: '%s', '%s'",
457 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000458 return mp_const_none;
Damien429d7192013-10-04 19:53:11 +0100459}
460
Damien Georged17926d2014-03-30 13:35:08 +0100461mp_obj_t mp_call_function_0(mp_obj_t fun) {
462 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100463}
464
Damien Georged17926d2014-03-30 13:35:08 +0100465mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
466 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100467}
468
Damien Georged17926d2014-03-30 13:35:08 +0100469mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000470 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000471 args[0] = arg1;
472 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100473 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100474}
475
Damien Georgecd82e022014-02-02 13:11:48 +0000476// wrapper that accepts n_args and n_kw in one argument
477// native emitter can only pass at most 3 arguments to a function
Damien Georged17926d2014-03-30 13:35:08 +0100478mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
479 return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
Damien Georgecd82e022014-02-02 13:11:48 +0000480}
481
Damien George20006db2014-01-18 14:10:48 +0000482// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien Georged17926d2014-03-30 13:35:08 +0100483mp_obj_t mp_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 +0000484 // TODO improve this: fun object can specify its type and we parse here the arguments,
485 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100486
John R. Lenton9c83ec02014-01-07 23:06:46 +0000487 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
488
Damien George8b56beb2014-01-31 23:49:49 +0000489 // get the type
490 mp_obj_type_t *type = mp_obj_get_type(fun_in);
491
492 // do the call
493 if (type->call != NULL) {
494 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000495 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000496 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
John R. Lenton9c83ec02014-01-07 23:06:46 +0000497 }
Damien86c7fc72013-11-26 15:16:41 +0000498}
499
Damien George20006db2014-01-18 14:10:48 +0000500// 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)
501// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien Georged17926d2014-03-30 13:35:08 +0100502mp_obj_t mp_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +0000503 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);
504 int adjust = (args[1] == NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100505 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000506}
507
Damien George230fec72014-03-30 21:21:24 +0100508mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_t *args, mp_obj_t pos_seq, mp_obj_t kw_dict) {
509 mp_obj_t fun = *args++;
510 mp_obj_t self = MP_OBJ_NULL;
511 if (have_self) {
512 self = *args++; // may be MP_OBJ_NULL
513 }
514 uint n_args = n_args_n_kw & 0xff;
515 uint n_kw = (n_args_n_kw >> 8) & 0xff;
516
517 DEBUG_OP_printf("call method var (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p, seq=%p, dict=%p)\n", fun, self, n_args, n_kw, args, pos_seq, kw_dict);
518
519 // We need to create the following array of objects:
520 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
521 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
522
523 // The new args array
524 mp_obj_t *args2;
525 uint args2_alloc;
526 uint args2_len = 0;
527
528 // Try to get a hint for the size of the kw_dict
529 uint kw_dict_len = 0;
530 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
531 kw_dict_len = mp_obj_dict_len(kw_dict);
532 }
533
534 // Extract the pos_seq sequence to the new args array.
535 // Note that it can be arbitrary iterator.
536 if (pos_seq == MP_OBJ_NULL) {
537 // no sequence
538
539 // allocate memory for the new array of args
540 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
541 args2 = m_new(mp_obj_t, args2_alloc);
542
543 // copy the self
544 if (self != MP_OBJ_NULL) {
545 args2[args2_len++] = self;
546 }
547
548 // copy the fixed pos args
549 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
550 args2_len += n_args;
551
552 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
553 // optimise the case of a tuple and list
554
555 // get the items
556 uint len;
557 mp_obj_t *items;
558 mp_obj_get_array(pos_seq, &len, &items);
559
560 // allocate memory for the new array of args
561 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
562 args2 = m_new(mp_obj_t, args2_alloc);
563
564 // copy the self
565 if (self != MP_OBJ_NULL) {
566 args2[args2_len++] = self;
567 }
568
569 // copy the fixed and variable position args
570 m_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
571 args2_len += n_args + len;
572
573 } else {
574 // generic iterator
575
576 // allocate memory for the new array of args
577 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
578 args2 = m_new(mp_obj_t, args2_alloc);
579
580 // copy the self
581 if (self != MP_OBJ_NULL) {
582 args2[args2_len++] = self;
583 }
584
585 // copy the fixed position args
586 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
587
588 // extract the variable position args from the iterator
589 mp_obj_t iterable = mp_getiter(pos_seq);
590 mp_obj_t item;
591 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
592 if (args2_len >= args2_alloc) {
593 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
594 args2_alloc *= 2;
595 }
596 args2[args2_len++] = item;
597 }
598 }
599
600 // The size of the args2 array now is the number of positional args.
601 uint pos_args_len = args2_len;
602
603 // Copy the fixed kw args.
604 m_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
605 args2_len += 2 * n_kw;
606
607 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
608 // Note that it can be arbitrary iterator.
609 if (kw_dict == MP_OBJ_NULL) {
610 // pass
611 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
612 // dictionary
613 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
614 assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above
615 for (uint i = 0; i < map->alloc; i++) {
616 if (map->table[i].key != MP_OBJ_NULL) {
617 args2[args2_len++] = map->table[i].key;
618 args2[args2_len++] = map->table[i].value;
619 }
620 }
621 } else {
622 // generic mapping
623 // TODO is calling 'items' on the mapping the correct thing to do here?
624 mp_obj_t dest[2];
625 mp_load_method(kw_dict, MP_QSTR_items, dest);
626 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
627 mp_obj_t item;
628 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
629 if (args2_len + 1 >= args2_alloc) {
630 uint new_alloc = args2_alloc * 2;
631 if (new_alloc < 4) {
632 new_alloc = 4;
633 }
634 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
635 args2_alloc = new_alloc;
636 }
637 mp_obj_t *items;
638 mp_obj_get_array_fixed_n(item, 2, &items);
639 args2[args2_len++] = items[0];
640 args2[args2_len++] = items[1];
641 }
642 }
643
644 mp_obj_t res = mp_call_function_n_kw(fun, pos_args_len, (args2_len - pos_args_len) / 2, args2);
645 m_del(mp_obj_t, args2, args2_alloc);
646
647 return res;
648}
649
Damien Georged17926d2014-03-30 13:35:08 +0100650mp_obj_t mp_build_tuple(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000651 return mp_obj_new_tuple(n_args, items);
Damienc226dca2013-10-16 16:12:52 +0100652}
653
Damien Georged17926d2014-03-30 13:35:08 +0100654mp_obj_t mp_build_list(int n_args, mp_obj_t *items) {
Damien George20006db2014-01-18 14:10:48 +0000655 return mp_obj_new_list(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100656}
657
Damien Georged17926d2014-03-30 13:35:08 +0100658mp_obj_t mp_build_set(int n_args, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000659 return mp_obj_new_set(n_args, items);
Damien429d7192013-10-04 19:53:11 +0100660}
661
Damien Georged17926d2014-03-30 13:35:08 +0100662mp_obj_t mp_store_set(mp_obj_t set, mp_obj_t item) {
Damiendae7eb72013-12-29 22:32:51 +0000663 mp_obj_set_store(set, item);
Damienc12aa462013-10-16 20:57:49 +0100664 return set;
665}
666
Damien George932bf1c2014-01-18 23:42:49 +0000667// unpacked items are stored in reverse order into the array pointed to by items
Damien Georged17926d2014-03-30 13:35:08 +0100668void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200669 uint seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000670 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
Damiend99b0522013-12-21 18:17:45 +0000671 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000672 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000673 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
674 } else {
675 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000676 }
Damiend99b0522013-12-21 18:17:45 +0000677 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200678 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000679 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200680 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000681 }
Damien George932bf1c2014-01-18 23:42:49 +0000682 for (uint i = 0; i < num; i++) {
683 items[i] = seq_items[num - 1 - i];
684 }
Damien86c7fc72013-11-26 15:16:41 +0000685 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100686 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200687
688 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100689 mp_obj_t el = mp_iternext(iterable);
Damien George66eaf842014-03-26 19:27:58 +0000690 if (el == MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200691 goto too_short;
692 }
693 items[num - 1 - seq_len] = el;
694 }
Damien Georged17926d2014-03-30 13:35:08 +0100695 if (mp_iternext(iterable) != MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200696 goto too_long;
697 }
Damien86c7fc72013-11-26 15:16:41 +0000698 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200699 return;
700
701too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000702 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200703too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000704 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num));
Damien86c7fc72013-11-26 15:16:41 +0000705}
706
Damien Georged17926d2014-03-30 13:35:08 +0100707mp_obj_t mp_build_map(int n_args) {
Damiend99b0522013-12-21 18:17:45 +0000708 return mp_obj_new_dict(n_args);
Damien429d7192013-10-04 19:53:11 +0100709}
710
Damien Georged17926d2014-03-30 13:35:08 +0100711mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
Damiend99b0522013-12-21 18:17:45 +0000712 // map should always be a dict
713 return mp_obj_dict_store(map, key, value);
Damien429d7192013-10-04 19:53:11 +0100714}
715
Damien Georged17926d2014-03-30 13:35:08 +0100716mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000717 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
718 // use load_method
719 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100720 mp_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000721 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000722 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000723 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000724 } else {
725 // load_method returned a method, so build a bound method object
726 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000727 }
Damiend99b0522013-12-21 18:17:45 +0000728}
729
Damien George7c9c6672014-01-25 00:17:36 +0000730// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
731// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
732// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Damien Georged17926d2014-03-30 13:35:08 +0100733STATIC void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000734 // clear output to indicate no attribute/method found yet
735 dest[0] = MP_OBJ_NULL;
736 dest[1] = MP_OBJ_NULL;
737
738 // get the type
739 mp_obj_type_t *type = mp_obj_get_type(base);
740
741 // if this type can do its own load, then call it
742 if (type->load_attr != NULL) {
743 type->load_attr(base, attr, dest);
744 }
745
746 // if nothing found yet, look for built-in and generic names
Damien George7c9c6672014-01-25 00:17:36 +0000747 if (dest[0] == MP_OBJ_NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +0000748 if (attr == MP_QSTR___class__) {
749 // a.__class__ is equivalent to type(a)
750 dest[0] = type;
751 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
Damien George20006db2014-01-18 14:10:48 +0000752 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
753 dest[1] = base;
Damien Georgef49ba1b2014-01-18 17:52:41 +0000754 } else if (type->load_attr == NULL) {
755 // generic method lookup if type didn't provide a specific one
Damien Georgeeae16442014-01-11 19:22:29 +0000756 // this is a lookup in the object (ie not class or type)
Damien George9b196cd2014-03-26 21:47:19 +0000757 if (type->locals_dict != NULL) {
Damien George3e1a5c12014-03-29 13:43:38 +0000758 assert(MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)); // Micro Python restriction, for now
Damien George9b196cd2014-03-26 21:47:19 +0000759 mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
760 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
761 if (elem != NULL) {
762 // check if the methods are functions, static or class methods
763 // see http://docs.python.org/3.3/howto/descriptor.html
764 if (MP_OBJ_IS_TYPE(elem->value, &mp_type_staticmethod)) {
765 // return just the function
766 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
767 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_classmethod)) {
768 // return a bound method, with self being the type of this object
769 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
770 dest[1] = mp_obj_get_type(base);
Damien Georgec3f11262014-03-26 22:35:10 +0000771 } else if (mp_obj_is_callable(elem->value)) {
Damien George9b196cd2014-03-26 21:47:19 +0000772 // return a bound method, with self being this object
Damien Georgec3f11262014-03-26 22:35:10 +0000773 dest[0] = elem->value;
Damien George9b196cd2014-03-26 21:47:19 +0000774 dest[1] = base;
Damien Georgec3f11262014-03-26 22:35:10 +0000775 } else {
776 // class member is a value, so just return that value
777 dest[0] = elem->value;
Damien George062478e2014-01-09 20:57:50 +0000778 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000779 }
Damiend57eba52013-11-02 23:58:14 +0000780 }
781 }
Damiena3977762013-10-09 23:10:10 +0100782 }
Damien George7c9c6672014-01-25 00:17:36 +0000783}
Damiena3977762013-10-09 23:10:10 +0100784
Damien Georged17926d2014-03-30 13:35:08 +0100785void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +0000786 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
787
Damien Georged17926d2014-03-30 13:35:08 +0100788 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000789
790 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000791 // no attribute/method called attr
792 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000793 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200794 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
795 "type object '%s' has no attribute '%s'", qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000796 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000797 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000798 }
799 }
Damiena3977762013-10-09 23:10:10 +0100800}
801
Damien Georged17926d2014-03-30 13:35:08 +0100802void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100803 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000804 mp_obj_type_t *type = mp_obj_get_type(base);
805 if (type->store_attr != NULL) {
806 if (type->store_attr(base, attr, value)) {
807 return;
808 }
Damiena3977762013-10-09 23:10:10 +0100809 }
Damien Georgec5966122014-02-15 16:10:44 +0000810 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100811}
812
Damien Georged17926d2014-03-30 13:35:08 +0100813void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100814 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000815 if (MP_OBJ_IS_TYPE(base, &mp_type_list)) {
Damien429d7192013-10-04 19:53:11 +0100816 // list store
Damiend99b0522013-12-21 18:17:45 +0000817 mp_obj_list_store(base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000818 } else if (MP_OBJ_IS_TYPE(base, &mp_type_dict)) {
Damiend99b0522013-12-21 18:17:45 +0000819 // dict store
820 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100821 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200822 mp_obj_type_t *type = mp_obj_get_type(base);
823 if (type->store_item != NULL) {
824 bool r = type->store_item(base, index, value);
825 if (r) {
826 return;
827 }
828 // TODO: call base classes here?
829 }
Damien Georgec5966122014-02-15 16:10:44 +0000830 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item assignment", mp_obj_get_type_str(base)));
Damien429d7192013-10-04 19:53:11 +0100831 }
832}
833
Damien Georged17926d2014-03-30 13:35:08 +0100834mp_obj_t mp_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000835 mp_obj_type_t *type = mp_obj_get_type(o_in);
836 if (type->getiter != NULL) {
837 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100838 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000839 // check for __iter__ method
Damien George7c9c6672014-01-25 00:17:36 +0000840 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100841 mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000842 if (dest[0] != MP_OBJ_NULL) {
Damien George9e6e9352014-03-26 18:37:06 +0000843 // __iter__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100844 return mp_call_method_n_kw(0, 0, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000845 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100846 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000847 if (dest[0] != MP_OBJ_NULL) {
848 // __getitem__ exists, create an iterator
849 return mp_obj_new_getitem_iter(dest);
850 } else {
851 // object not iterable
852 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
853 }
Damien George7c9c6672014-01-25 00:17:36 +0000854 }
Damience89a212013-10-15 22:25:17 +0100855 }
856}
857
Damien George66eaf842014-03-26 19:27:58 +0000858// may return MP_OBJ_NULL as an optimisation instead of raise StopIteration()
859// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +0100860mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000861 mp_obj_type_t *type = mp_obj_get_type(o_in);
862 if (type->iternext != NULL) {
863 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100864 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000865 // check for __next__ method
866 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100867 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000868 if (dest[0] != MP_OBJ_NULL) {
869 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100870 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000871 } else {
872 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
873 }
Damien Georgec5966122014-02-15 16:10:44 +0000874 }
875}
876
Damien George66eaf842014-03-26 19:27:58 +0000877// will always return MP_OBJ_NULL instead of raising StopIteration() (or any subclass thereof)
878// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +0100879mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George66eaf842014-03-26 19:27:58 +0000880 mp_obj_type_t *type = mp_obj_get_type(o_in);
881 if (type->iternext != NULL) {
882 return type->iternext(o_in);
883 } else {
884 // check for __next__ method
885 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100886 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +0000887 if (dest[0] != MP_OBJ_NULL) {
888 // __next__ exists, call it and return its result
889 nlr_buf_t nlr;
890 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100891 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +0000892 nlr_pop();
893 return ret;
894 } else {
895 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
896 return MP_OBJ_NULL;
897 } else {
898 nlr_jump(nlr.ret_val);
899 }
900 }
901 } else {
902 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
903 }
904 }
905}
906
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300907// TODO: Unclear what to do with StopIterarion exception here.
908mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
909 mp_obj_type_t *type = mp_obj_get_type(self_in);
910
911 if (type == &mp_type_gen_instance) {
912 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
913 }
914
915 if (type->iternext != NULL && send_value == mp_const_none) {
916 mp_obj_t ret = type->iternext(self_in);
917 if (ret != MP_OBJ_NULL) {
918 *ret_val = ret;
919 return MP_VM_RETURN_YIELD;
920 } else {
921 // Emulate raise StopIteration()
922 // Special case, handled in vm.c
923 *ret_val = MP_OBJ_NULL;
924 return MP_VM_RETURN_NORMAL;
925 }
926 }
927
928 mp_obj_t dest[3]; // Reserve slot for send() arg
929
930 if (send_value == mp_const_none) {
931 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
932 if (dest[0] != MP_OBJ_NULL) {
933 *ret_val = mp_call_method_n_kw(0, 0, dest);
934 return MP_VM_RETURN_YIELD;
935 }
936 }
937
938 if (send_value != MP_OBJ_NULL) {
939 mp_load_method(self_in, MP_QSTR_send, dest);
940 dest[2] = send_value;
941 *ret_val = mp_call_method_n_kw(1, 0, dest);
942 return MP_VM_RETURN_YIELD;
943 }
944
945 if (throw_value != MP_OBJ_NULL) {
946 if (mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
947 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
948 if (dest[0] != MP_OBJ_NULL) {
949 *ret_val = mp_call_method_n_kw(0, 0, dest);
950 // We assume one can't "yield" from close()
951 return MP_VM_RETURN_NORMAL;
952 }
953 }
954 mp_load_method(self_in, MP_QSTR_throw, dest);
955 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
956 return MP_VM_RETURN_YIELD;
957 }
958
959 assert(0);
960 return MP_VM_RETURN_NORMAL; // Should be unreachable
961}
962
Damien Georged17926d2014-03-30 13:35:08 +0100963mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +0000964 DEBUG_printf("raise %p\n", o);
965 if (mp_obj_is_exception_type(o)) {
966 // o is an exception type (it is derived from BaseException (or is BaseException))
967 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +0000968 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
969 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +0100970 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +0000971 } else if (mp_obj_is_exception_instance(o)) {
972 // o is an instance of an exception, so use it as the exception
973 return o;
974 } else {
975 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
976 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +0100977 }
978}
979
Damien Georged17926d2014-03-30 13:35:08 +0100980mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +0000981 DEBUG_printf("import name %s\n", qstr_str(name));
982
Damiendb4c3612013-12-10 17:27:24 +0000983 // build args array
Damiend99b0522013-12-21 18:17:45 +0000984 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +0000985 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +0000986 args[1] = mp_const_none; // TODO should be globals
987 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000988 args[3] = fromlist;
989 args[4] = level; // must be 0; we don't yet support other values
990
991 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000992 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000993}
994
Damien Georged17926d2014-03-30 13:35:08 +0100995mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +0000996 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
997
Damien Georged17926d2014-03-30 13:35:08 +0100998 mp_obj_t x = mp_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +0000999 /* TODO convert AttributeError to ImportError
1000 if (fail) {
1001 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1002 }
1003 */
1004 return x;
1005}
1006
Damien Georged17926d2014-03-30 13:35:08 +01001007void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001008 DEBUG_printf("import all %p\n", module);
1009
1010 mp_map_t *map = mp_obj_module_get_globals(module);
1011 for (uint i = 0; i < map->alloc; i++) {
1012 if (map->table[i].key != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +01001013 mp_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001014 }
1015 }
1016}
1017
Damien Georged17926d2014-03-30 13:35:08 +01001018mp_map_t *mp_locals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +00001019 return map_locals;
1020}
1021
Damien Georged17926d2014-03-30 13:35:08 +01001022void mp_locals_set(mp_map_t *m) {
1023 DEBUG_OP_printf("mp_locals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +00001024 map_locals = m;
1025}
1026
Damien Georged17926d2014-03-30 13:35:08 +01001027mp_map_t *mp_globals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +00001028 return map_globals;
1029}
1030
Damien Georged17926d2014-03-30 13:35:08 +01001031void mp_globals_set(mp_map_t *m) {
1032 DEBUG_OP_printf("mp_globals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +00001033 map_globals = m;
1034}
1035
Damien6ba13142013-11-02 20:34:54 +00001036// these must correspond to the respective enum
Damien Georged17926d2014-03-30 13:35:08 +01001037void *const mp_fun_table[MP_F_NUMBER_OF] = {
1038 mp_load_const_dec,
1039 mp_load_const_str,
1040 mp_load_name,
1041 mp_load_global,
1042 mp_load_build_class,
1043 mp_load_attr,
1044 mp_load_method,
1045 mp_store_name,
1046 mp_store_attr,
1047 mp_store_subscr,
1048 mp_obj_is_true,
1049 mp_unary_op,
1050 mp_binary_op,
1051 mp_build_tuple,
1052 mp_build_list,
1053 mp_list_append,
1054 mp_build_map,
1055 mp_store_map,
1056 mp_build_set,
1057 mp_store_set,
1058 mp_make_function_from_id,
1059 mp_call_function_n_kw_for_native,
1060 mp_call_method_n_kw,
1061 mp_getiter,
1062 mp_iternext,
Damien429d7192013-10-04 19:53:11 +01001063};
1064
1065/*
Damien Georged17926d2014-03-30 13:35:08 +01001066void mp_f_vector(mp_fun_kind_t fun_kind) {
1067 (mp_f_table[fun_kind])();
Damien429d7192013-10-04 19:53:11 +01001068}
1069*/