blob: 1a1db46ec513fa2e3a3130e65402688bf4567b0c [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"
Damien Georgeecf5b772014-04-04 11:13:51 +000019#include "smallint.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
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030036STATIC mp_map_t map_main;
37
38const mp_obj_module_t mp_module___main__ = {
39 .base = { &mp_type_module },
40 .name = MP_QSTR___main__,
41 .globals = (mp_map_t*)&map_main,
42};
43
Damien George38a2da62014-01-08 17:33:12 +000044// a good optimising compiler will inline this if necessary
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020045STATIC void mp_map_add_qstr(mp_map_t *map, qstr qstr, mp_obj_t value) {
Damien George38a2da62014-01-08 17:33:12 +000046 mp_map_lookup(map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
47}
48
Damien Georged17926d2014-03-30 13:35:08 +010049void mp_init(void) {
Damien George2326d522014-03-27 23:26:35 +000050 mp_emit_glue_init();
51
Damien Georgecaac5422014-03-25 14:18:18 +000052 // init global module stuff
53 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000054
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030055 mp_map_init(&map_main, 1);
Damien Georgecaac5422014-03-25 14:18:18 +000056 // add some builtins that can't be done in ROM
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030057 mp_map_add_qstr(&map_main, MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR___main__));
58
59 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
60 map_locals = map_globals = &map_main;
61
62 // init built-in hash table
63 mp_map_init(&map_builtins, 3);
Damien Georgee9906ac2014-01-04 18:44:46 +000064
Paul Sokolovskydcac8802014-01-16 19:19:50 +020065#if MICROPY_CPYTHON_COMPAT
Paul Sokolovsky159c0f72014-01-20 01:57:20 +020066 // Precreate sys module, so "import sys" didn't throw exceptions.
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020067 mp_obj_t m_sys = mp_obj_new_module(MP_QSTR_sys);
68 // Avoid warning of unused var
69 (void)m_sys;
Paul Sokolovskydcac8802014-01-16 19:19:50 +020070#endif
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020071 // init sys.path
72 // for efficiency, left to platform-specific startup code
Damien Georged17926d2014-03-30 13:35:08 +010073 //mp_sys_path = mp_obj_new_list(0, NULL);
74 //mp_store_attr(m_sys, MP_QSTR_path, mp_sys_path);
Damien429d7192013-10-04 19:53:11 +010075}
76
Damien Georged17926d2014-03-30 13:35:08 +010077void mp_deinit(void) {
Damien George2326d522014-03-27 23:26:35 +000078 mp_map_free(map_globals);
79 mp_map_deinit(&map_builtins);
80 mp_module_deinit();
81 mp_emit_glue_deinit();
Damien429d7192013-10-04 19:53:11 +010082}
83
Damien Georged17926d2014-03-30 13:35:08 +010084mp_obj_t mp_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +000085 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +000086 uint len;
87 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +000088 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +000089}
90
Damien Georged17926d2014-03-30 13:35:08 +010091mp_obj_t mp_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010092 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +000093 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +010094}
95
Damien Georged17926d2014-03-30 13:35:08 +010096mp_obj_t mp_load_const_bytes(qstr qstr) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020097 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
98 uint len;
99 const byte *data = qstr_data(qstr, &len);
100 return mp_obj_new_bytes(data, len);
101}
102
Damien Georged17926d2014-03-30 13:35:08 +0100103mp_obj_t mp_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100104 // logic: search locals, globals, builtins
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300105 DEBUG_OP_printf("load name %s\n", map_locals, qstr_str(qstr));
106 // If we're at the outer scope (locals == globals), dispatch to load_global right away
107 if (map_locals != map_globals) {
108 mp_map_elem_t *elem = mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
109 if (elem != NULL) {
110 return elem->value;
111 }
Damiena3977762013-10-09 23:10:10 +0100112 }
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300113 return mp_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100114}
115
Damien Georged17926d2014-03-30 13:35:08 +0100116mp_obj_t mp_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100117 // logic: search globals, builtins
118 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000119 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100120 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000121 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100122 if (elem == NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000123 mp_obj_t o = mp_builtin_tables_lookup_object(qstr);
124 if (o != MP_OBJ_NULL) {
125 return o;
Damien Georgeaea532e2014-02-06 22:57:51 +0000126 }
Damien Georgec5966122014-02-15 16:10:44 +0000127 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 +0100128 }
129 }
130 return elem->value;
131}
132
Damien Georged17926d2014-03-30 13:35:08 +0100133mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100134 DEBUG_OP_printf("load_build_class\n");
Damien Georgecaac5422014-03-25 14:18:18 +0000135 // lookup __build_class__ in dynamic table of builtins first
Damien George38a2da62014-01-08 17:33:12 +0000136 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 +0000137 if (elem != NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000138 // found user-defined __build_class__, return it
Damien Georgeaea532e2014-02-06 22:57:51 +0000139 return elem->value;
140 } else {
Damien Georgecaac5422014-03-25 14:18:18 +0000141 // no user-defined __build_class__, return builtin one
Damien Georgeaea532e2014-02-06 22:57:51 +0000142 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100143 }
Damien429d7192013-10-04 19:53:11 +0100144}
145
Damien Georged17926d2014-03-30 13:35:08 +0100146void mp_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100147 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000148 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 +0100149}
150
Damien Georged17926d2014-03-30 13:35:08 +0100151void mp_delete_name(qstr qstr) {
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200152 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
Damien George66edc5d2014-04-05 13:25:13 +0100153 // TODO raise NameError if qstr not found
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200154 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.
Damien Georgeecf5b772014-04-04 11:13:51 +0000304 // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
Damien George9d68e9c2014-03-12 15:38:15 +0000305 #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
Damien Georgeecf5b772014-04-04 11:13:51 +0000317 if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
318 // use higher precision
319 lhs = mp_obj_new_int_from_ll(lhs_val);
320 goto generic_binary_op;
321 } else {
322 // use standard precision
323 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
324 }
Damien George9d68e9c2014-03-12 15:38:15 +0000325 break;
326 }
Damien Georged17926d2014-03-30 13:35:08 +0100327 case MP_BINARY_OP_FLOOR_DIVIDE:
328 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300329 if (rhs_val == 0) {
330 goto zero_division;
331 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000332 lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
Rachel Dowdall56402792014-03-22 20:19:24 +0000333 break;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300334
Damien George9d68e9c2014-03-12 15:38:15 +0000335 #if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100336 case MP_BINARY_OP_TRUE_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300337 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
338 if (rhs_val == 0) {
Damien George70f33cd2014-04-02 17:06:05 +0100339 goto zero_division;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300340 }
341 return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000342 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000343
Damien Georged17926d2014-03-30 13:35:08 +0100344 case MP_BINARY_OP_MODULO:
Damien Georgeecf5b772014-04-04 11:13:51 +0000345 case MP_BINARY_OP_INPLACE_MODULO: {
346 lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
Rachel Dowdallcde86312014-03-22 17:29:27 +0000347 break;
348 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000349
Damien Georged17926d2014-03-30 13:35:08 +0100350 case MP_BINARY_OP_POWER:
351 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000352 if (rhs_val < 0) {
353 #if MICROPY_ENABLE_FLOAT
354 lhs = mp_obj_new_float(lhs_val);
355 goto generic_binary_op;
356 #else
357 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
358 #endif
359 } else {
Damien George9d68e9c2014-03-12 15:38:15 +0000360 machine_int_t ans = 1;
361 while (rhs_val > 0) {
362 if (rhs_val & 1) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000363 if (mp_small_int_mul_overflow(ans, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100364 goto power_overflow;
365 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000366 ans *= lhs_val;
Damien George9d68e9c2014-03-12 15:38:15 +0000367 }
Damien George5bf565e2014-04-04 00:16:32 +0100368 if (rhs_val == 1) {
369 break;
370 }
Damien George9d68e9c2014-03-12 15:38:15 +0000371 rhs_val /= 2;
Damien Georgeecf5b772014-04-04 11:13:51 +0000372 if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100373 goto power_overflow;
374 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000375 lhs_val *= lhs_val;
Damien George1a9951d2014-01-06 22:13:00 +0000376 }
Damien George9d68e9c2014-03-12 15:38:15 +0000377 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000378 }
Damien George1a9951d2014-01-06 22:13:00 +0000379 break;
Damien George5bf565e2014-04-04 00:16:32 +0100380
381 power_overflow:
382 // use higher precision
383 lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
384 goto generic_binary_op;
385
Damien Georged17926d2014-03-30 13:35:08 +0100386 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
387 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
388 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
389 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000390
Damien George1a9951d2014-01-06 22:13:00 +0000391 default: assert(0);
392 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200393 // TODO: We just should make mp_obj_new_int() inline and use that
394 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000395 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000396 } else {
397 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000398 }
Damien George3f759b72014-01-31 00:42:12 +0000399#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000400 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000401 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000402 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000403 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000404#endif
Damien429d7192013-10-04 19:53:11 +0100405 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000406 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000407
Damien George9aa2a522014-02-01 23:04:09 +0000408 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000409 *
410 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000411 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000412 */
Damien Georged17926d2014-03-30 13:35:08 +0100413 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000414 mp_obj_type_t *type = mp_obj_get_type(rhs);
415 if (type->binary_op != NULL) {
416 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000417 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000418 return res;
419 }
420 }
421 if (type->getiter != NULL) {
422 /* second attempt, walk the iterator */
423 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100424 mp_obj_t iter = mp_getiter(rhs);
425 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000426 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000427 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000428 }
Damien7410e442013-11-02 19:47:57 +0000429 }
Damien George9aa2a522014-02-01 23:04:09 +0000430 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000431 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000432
433 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000434 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000435 mp_obj_get_type_str(rhs)));
436 return mp_const_none;
437 }
438
Damien George5fa93b62014-01-22 14:35:10 +0000439 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000440 mp_obj_type_t *type;
441generic_binary_op:
442 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000443 if (type->binary_op != NULL) {
444 mp_obj_t result = type->binary_op(op, lhs, rhs);
445 if (result != MP_OBJ_NULL) {
446 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000447 }
Damien429d7192013-10-04 19:53:11 +0100448 }
Damiend99b0522013-12-21 18:17:45 +0000449
Damien George5fa93b62014-01-22 14:35:10 +0000450 // TODO implement dispatch for reverse binary ops
451
Damiend99b0522013-12-21 18:17:45 +0000452 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000453 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200454 "unsupported operand types for binary operator: '%s', '%s'",
455 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000456 return mp_const_none;
Damien George70f33cd2014-04-02 17:06:05 +0100457
458zero_division:
459 nlr_jump(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
Damien429d7192013-10-04 19:53:11 +0100460}
461
Damien Georged17926d2014-03-30 13:35:08 +0100462mp_obj_t mp_call_function_0(mp_obj_t fun) {
463 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100464}
465
Damien Georged17926d2014-03-30 13:35:08 +0100466mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
467 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100468}
469
Damien Georged17926d2014-03-30 13:35:08 +0100470mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000471 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000472 args[0] = arg1;
473 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100474 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100475}
476
Damien Georgecd82e022014-02-02 13:11:48 +0000477// wrapper that accepts n_args and n_kw in one argument
478// native emitter can only pass at most 3 arguments to a function
Damien Georged17926d2014-03-30 13:35:08 +0100479mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
480 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 +0000481}
482
Damien George20006db2014-01-18 14:10:48 +0000483// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien Georged17926d2014-03-30 13:35:08 +0100484mp_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 +0000485 // TODO improve this: fun object can specify its type and we parse here the arguments,
486 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100487
John R. Lenton9c83ec02014-01-07 23:06:46 +0000488 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
489
Damien George8b56beb2014-01-31 23:49:49 +0000490 // get the type
491 mp_obj_type_t *type = mp_obj_get_type(fun_in);
492
493 // do the call
494 if (type->call != NULL) {
495 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000496 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000497 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 +0000498 }
Damien86c7fc72013-11-26 15:16:41 +0000499}
500
Damien George20006db2014-01-18 14:10:48 +0000501// 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)
502// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien Georged17926d2014-03-30 13:35:08 +0100503mp_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 +0000504 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);
505 int adjust = (args[1] == NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100506 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000507}
508
Damien George523b5752014-03-31 11:59:23 +0100509mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_t *args) {
Damien George230fec72014-03-30 21:21:24 +0100510 mp_obj_t fun = *args++;
511 mp_obj_t self = MP_OBJ_NULL;
512 if (have_self) {
513 self = *args++; // may be MP_OBJ_NULL
514 }
515 uint n_args = n_args_n_kw & 0xff;
516 uint n_kw = (n_args_n_kw >> 8) & 0xff;
Damien George523b5752014-03-31 11:59:23 +0100517 mp_obj_t pos_seq = args[n_args + 2 * n_kw]; // map be MP_OBJ_NULL
518 mp_obj_t kw_dict = args[n_args + 2 * n_kw + 1]; // map be MP_OBJ_NULL
Damien George230fec72014-03-30 21:21:24 +0100519
520 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);
521
522 // We need to create the following array of objects:
523 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
524 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
525
526 // The new args array
527 mp_obj_t *args2;
528 uint args2_alloc;
529 uint args2_len = 0;
530
531 // Try to get a hint for the size of the kw_dict
532 uint kw_dict_len = 0;
533 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
534 kw_dict_len = mp_obj_dict_len(kw_dict);
535 }
536
537 // Extract the pos_seq sequence to the new args array.
538 // Note that it can be arbitrary iterator.
539 if (pos_seq == MP_OBJ_NULL) {
540 // no sequence
541
542 // allocate memory for the new array of args
543 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
544 args2 = m_new(mp_obj_t, args2_alloc);
545
546 // copy the self
547 if (self != MP_OBJ_NULL) {
548 args2[args2_len++] = self;
549 }
550
551 // copy the fixed pos args
552 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
553 args2_len += n_args;
554
555 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
556 // optimise the case of a tuple and list
557
558 // get the items
559 uint len;
560 mp_obj_t *items;
561 mp_obj_get_array(pos_seq, &len, &items);
562
563 // allocate memory for the new array of args
564 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
565 args2 = m_new(mp_obj_t, args2_alloc);
566
567 // copy the self
568 if (self != MP_OBJ_NULL) {
569 args2[args2_len++] = self;
570 }
571
572 // copy the fixed and variable position args
573 m_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
574 args2_len += n_args + len;
575
576 } else {
577 // generic iterator
578
579 // allocate memory for the new array of args
580 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
581 args2 = m_new(mp_obj_t, args2_alloc);
582
583 // copy the self
584 if (self != MP_OBJ_NULL) {
585 args2[args2_len++] = self;
586 }
587
588 // copy the fixed position args
589 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
590
591 // extract the variable position args from the iterator
592 mp_obj_t iterable = mp_getiter(pos_seq);
593 mp_obj_t item;
594 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
595 if (args2_len >= args2_alloc) {
596 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
597 args2_alloc *= 2;
598 }
599 args2[args2_len++] = item;
600 }
601 }
602
603 // The size of the args2 array now is the number of positional args.
604 uint pos_args_len = args2_len;
605
606 // Copy the fixed kw args.
607 m_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
608 args2_len += 2 * n_kw;
609
610 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
611 // Note that it can be arbitrary iterator.
612 if (kw_dict == MP_OBJ_NULL) {
613 // pass
614 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
615 // dictionary
616 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
617 assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above
618 for (uint i = 0; i < map->alloc; i++) {
619 if (map->table[i].key != MP_OBJ_NULL) {
620 args2[args2_len++] = map->table[i].key;
621 args2[args2_len++] = map->table[i].value;
622 }
623 }
624 } else {
625 // generic mapping
626 // TODO is calling 'items' on the mapping the correct thing to do here?
627 mp_obj_t dest[2];
628 mp_load_method(kw_dict, MP_QSTR_items, dest);
629 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
630 mp_obj_t item;
631 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
632 if (args2_len + 1 >= args2_alloc) {
633 uint new_alloc = args2_alloc * 2;
634 if (new_alloc < 4) {
635 new_alloc = 4;
636 }
637 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
638 args2_alloc = new_alloc;
639 }
640 mp_obj_t *items;
641 mp_obj_get_array_fixed_n(item, 2, &items);
642 args2[args2_len++] = items[0];
643 args2[args2_len++] = items[1];
644 }
645 }
646
647 mp_obj_t res = mp_call_function_n_kw(fun, pos_args_len, (args2_len - pos_args_len) / 2, args2);
648 m_del(mp_obj_t, args2, args2_alloc);
649
650 return res;
651}
652
Damien George932bf1c2014-01-18 23:42:49 +0000653// unpacked items are stored in reverse order into the array pointed to by items
Damien Georged17926d2014-03-30 13:35:08 +0100654void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200655 uint seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000656 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 +0000657 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000658 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000659 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
660 } else {
661 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000662 }
Damiend99b0522013-12-21 18:17:45 +0000663 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200664 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000665 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200666 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000667 }
Damien George932bf1c2014-01-18 23:42:49 +0000668 for (uint i = 0; i < num; i++) {
669 items[i] = seq_items[num - 1 - i];
670 }
Damien86c7fc72013-11-26 15:16:41 +0000671 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100672 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200673
674 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100675 mp_obj_t el = mp_iternext(iterable);
Damien George66eaf842014-03-26 19:27:58 +0000676 if (el == MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200677 goto too_short;
678 }
679 items[num - 1 - seq_len] = el;
680 }
Damien Georged17926d2014-03-30 13:35:08 +0100681 if (mp_iternext(iterable) != MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200682 goto too_long;
683 }
Damien86c7fc72013-11-26 15:16:41 +0000684 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200685 return;
686
687too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000688 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 +0200689too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000690 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 +0000691}
692
Damien Georged17926d2014-03-30 13:35:08 +0100693mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000694 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
695 // use load_method
696 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100697 mp_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000698 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000699 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000700 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000701 } else {
702 // load_method returned a method, so build a bound method object
703 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000704 }
Damiend99b0522013-12-21 18:17:45 +0000705}
706
Damien George7c9c6672014-01-25 00:17:36 +0000707// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
708// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
709// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Damien Georgee44d26a2014-03-31 22:57:56 +0100710void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000711 // clear output to indicate no attribute/method found yet
712 dest[0] = MP_OBJ_NULL;
713 dest[1] = MP_OBJ_NULL;
714
715 // get the type
716 mp_obj_type_t *type = mp_obj_get_type(base);
717
Damien Georgee44d26a2014-03-31 22:57:56 +0100718 // look for built-in names
719 if (0) {
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300720#if MICROPY_CPYTHON_COMPAT
Damien Georgee44d26a2014-03-31 22:57:56 +0100721 } else if (attr == MP_QSTR___class__) {
722 // a.__class__ is equivalent to type(a)
723 dest[0] = type;
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300724#endif
Damien Georgee44d26a2014-03-31 22:57:56 +0100725
726 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
727 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
728 dest[1] = base;
729
730 } else if (type->load_attr != NULL) {
731 // this type can do its own load, so call it
732 type->load_attr(base, attr, dest);
733
734 } else if (type->locals_dict != NULL) {
735 // generic method lookup
736 // this is a lookup in the object (ie not class or type)
737 assert(MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)); // Micro Python restriction, for now
738 mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
739 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
740 if (elem != NULL) {
741 // check if the methods are functions, static or class methods
742 // see http://docs.python.org/3.3/howto/descriptor.html
743 if (MP_OBJ_IS_TYPE(elem->value, &mp_type_staticmethod)) {
744 // return just the function
745 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
746 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_classmethod)) {
747 // return a bound method, with self being the type of this object
748 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
749 dest[1] = mp_obj_get_type(base);
750 } else if (mp_obj_is_callable(elem->value)) {
751 // return a bound method, with self being this object
752 dest[0] = elem->value;
753 dest[1] = base;
754 } else {
755 // class member is a value, so just return that value
756 dest[0] = elem->value;
Damiend57eba52013-11-02 23:58:14 +0000757 }
758 }
Damiena3977762013-10-09 23:10:10 +0100759 }
Damien George7c9c6672014-01-25 00:17:36 +0000760}
Damiena3977762013-10-09 23:10:10 +0100761
Damien Georged17926d2014-03-30 13:35:08 +0100762void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +0000763 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
764
Damien Georged17926d2014-03-30 13:35:08 +0100765 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000766
767 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000768 // no attribute/method called attr
769 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000770 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200771 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
772 "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 +0000773 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000774 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 +0000775 }
776 }
Damiena3977762013-10-09 23:10:10 +0100777}
778
Damien Georged17926d2014-03-30 13:35:08 +0100779void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100780 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000781 mp_obj_type_t *type = mp_obj_get_type(base);
782 if (type->store_attr != NULL) {
783 if (type->store_attr(base, attr, value)) {
784 return;
785 }
Damiena3977762013-10-09 23:10:10 +0100786 }
Damien Georgec5966122014-02-15 16:10:44 +0000787 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 +0100788}
789
Damien Georged17926d2014-03-30 13:35:08 +0100790void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100791 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000792 if (MP_OBJ_IS_TYPE(base, &mp_type_list)) {
Damien429d7192013-10-04 19:53:11 +0100793 // list store
Damiend99b0522013-12-21 18:17:45 +0000794 mp_obj_list_store(base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000795 } else if (MP_OBJ_IS_TYPE(base, &mp_type_dict)) {
Damiend99b0522013-12-21 18:17:45 +0000796 // dict store
797 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100798 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200799 mp_obj_type_t *type = mp_obj_get_type(base);
800 if (type->store_item != NULL) {
801 bool r = type->store_item(base, index, value);
802 if (r) {
803 return;
804 }
805 // TODO: call base classes here?
806 }
Damien Georgec5966122014-02-15 16:10:44 +0000807 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 +0100808 }
809}
810
Damien George66edc5d2014-04-05 13:25:13 +0100811void mp_delete_subscr(mp_obj_t base, mp_obj_t index) {
812 DEBUG_OP_printf("delete subscr %p[%p]\n", base, index);
813 /* list delete not implemented
814 if (MP_OBJ_IS_TYPE(base, &mp_type_list)) {
815 // list delete
816 mp_obj_list_delete(base, index);
817 } else */
818 if (MP_OBJ_IS_TYPE(base, &mp_type_dict)) {
819 // dict delete
820 mp_obj_dict_delete(base, index);
821 } else {
822 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object does not support item deletion", mp_obj_get_type_str(base)));
823 }
824}
825
Damien Georged17926d2014-03-30 13:35:08 +0100826mp_obj_t mp_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000827 mp_obj_type_t *type = mp_obj_get_type(o_in);
828 if (type->getiter != NULL) {
829 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100830 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000831 // check for __iter__ method
Damien George7c9c6672014-01-25 00:17:36 +0000832 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100833 mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000834 if (dest[0] != MP_OBJ_NULL) {
Damien George9e6e9352014-03-26 18:37:06 +0000835 // __iter__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100836 return mp_call_method_n_kw(0, 0, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000837 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100838 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000839 if (dest[0] != MP_OBJ_NULL) {
840 // __getitem__ exists, create an iterator
841 return mp_obj_new_getitem_iter(dest);
842 } else {
843 // object not iterable
844 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
845 }
Damien George7c9c6672014-01-25 00:17:36 +0000846 }
Damience89a212013-10-15 22:25:17 +0100847 }
848}
849
Damien George66eaf842014-03-26 19:27:58 +0000850// may return MP_OBJ_NULL as an optimisation instead of raise StopIteration()
851// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +0100852mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000853 mp_obj_type_t *type = mp_obj_get_type(o_in);
854 if (type->iternext != NULL) {
855 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100856 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000857 // check for __next__ method
858 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100859 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000860 if (dest[0] != MP_OBJ_NULL) {
861 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100862 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000863 } else {
864 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
865 }
Damien Georgec5966122014-02-15 16:10:44 +0000866 }
867}
868
Damien George66eaf842014-03-26 19:27:58 +0000869// will always return MP_OBJ_NULL instead of raising StopIteration() (or any subclass thereof)
870// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +0100871mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George66eaf842014-03-26 19:27:58 +0000872 mp_obj_type_t *type = mp_obj_get_type(o_in);
873 if (type->iternext != NULL) {
874 return type->iternext(o_in);
875 } else {
876 // check for __next__ method
877 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100878 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +0000879 if (dest[0] != MP_OBJ_NULL) {
880 // __next__ exists, call it and return its result
881 nlr_buf_t nlr;
882 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100883 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +0000884 nlr_pop();
885 return ret;
886 } else {
887 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
888 return MP_OBJ_NULL;
889 } else {
890 nlr_jump(nlr.ret_val);
891 }
892 }
893 } else {
894 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
895 }
896 }
897}
898
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300899// TODO: Unclear what to do with StopIterarion exception here.
900mp_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) {
Paul Sokolovsky7da06602014-03-31 04:19:12 +0300901 assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300902 mp_obj_type_t *type = mp_obj_get_type(self_in);
903
904 if (type == &mp_type_gen_instance) {
905 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
906 }
907
908 if (type->iternext != NULL && send_value == mp_const_none) {
909 mp_obj_t ret = type->iternext(self_in);
910 if (ret != MP_OBJ_NULL) {
911 *ret_val = ret;
912 return MP_VM_RETURN_YIELD;
913 } else {
914 // Emulate raise StopIteration()
915 // Special case, handled in vm.c
916 *ret_val = MP_OBJ_NULL;
917 return MP_VM_RETURN_NORMAL;
918 }
919 }
920
921 mp_obj_t dest[3]; // Reserve slot for send() arg
922
923 if (send_value == mp_const_none) {
924 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
925 if (dest[0] != MP_OBJ_NULL) {
926 *ret_val = mp_call_method_n_kw(0, 0, dest);
927 return MP_VM_RETURN_YIELD;
928 }
929 }
930
931 if (send_value != MP_OBJ_NULL) {
932 mp_load_method(self_in, MP_QSTR_send, dest);
933 dest[2] = send_value;
934 *ret_val = mp_call_method_n_kw(1, 0, dest);
935 return MP_VM_RETURN_YIELD;
936 }
937
938 if (throw_value != MP_OBJ_NULL) {
939 if (mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
940 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
941 if (dest[0] != MP_OBJ_NULL) {
942 *ret_val = mp_call_method_n_kw(0, 0, dest);
943 // We assume one can't "yield" from close()
944 return MP_VM_RETURN_NORMAL;
945 }
946 }
Paul Sokolovskya2109d92014-03-31 04:14:30 +0300947 mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
948 if (dest[0] != MP_OBJ_NULL) {
949 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
950 // If .throw() method returned, we assume it's value to yield
951 // - any exception would be thrown with nlr_jump().
952 return MP_VM_RETURN_YIELD;
953 }
954 // If there's nowhere to throw exception into, then we assume that object
955 // is just incapable to handle it, so any exception thrown into it
956 // will be propagated up. This behavior is approved by test_pep380.py
957 // test_delegation_of_close_to_non_generator(),
958 // test_delegating_throw_to_non_generator()
959 *ret_val = throw_value;
960 return MP_VM_RETURN_EXCEPTION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300961 }
962
963 assert(0);
964 return MP_VM_RETURN_NORMAL; // Should be unreachable
965}
966
Damien Georged17926d2014-03-30 13:35:08 +0100967mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +0000968 DEBUG_printf("raise %p\n", o);
969 if (mp_obj_is_exception_type(o)) {
970 // o is an exception type (it is derived from BaseException (or is BaseException))
971 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +0000972 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
973 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +0100974 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +0000975 } else if (mp_obj_is_exception_instance(o)) {
976 // o is an instance of an exception, so use it as the exception
977 return o;
978 } else {
979 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
980 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +0100981 }
982}
983
Damien Georged17926d2014-03-30 13:35:08 +0100984mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +0000985 DEBUG_printf("import name %s\n", qstr_str(name));
986
Damiendb4c3612013-12-10 17:27:24 +0000987 // build args array
Damiend99b0522013-12-21 18:17:45 +0000988 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +0000989 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +0000990 args[1] = mp_const_none; // TODO should be globals
991 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000992 args[3] = fromlist;
993 args[4] = level; // must be 0; we don't yet support other values
994
995 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000996 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000997}
998
Damien Georged17926d2014-03-30 13:35:08 +0100999mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001000 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1001
Damien Georged17926d2014-03-30 13:35:08 +01001002 mp_obj_t x = mp_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +00001003 /* TODO convert AttributeError to ImportError
1004 if (fail) {
1005 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1006 }
1007 */
1008 return x;
1009}
1010
Damien Georged17926d2014-03-30 13:35:08 +01001011void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001012 DEBUG_printf("import all %p\n", module);
1013
1014 mp_map_t *map = mp_obj_module_get_globals(module);
1015 for (uint i = 0; i < map->alloc; i++) {
1016 if (map->table[i].key != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +01001017 mp_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001018 }
1019 }
1020}
1021
Damien Georged17926d2014-03-30 13:35:08 +01001022mp_map_t *mp_locals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +00001023 return map_locals;
1024}
1025
Damien Georged17926d2014-03-30 13:35:08 +01001026void mp_locals_set(mp_map_t *m) {
1027 DEBUG_OP_printf("mp_locals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +00001028 map_locals = m;
1029}
1030
Damien Georged17926d2014-03-30 13:35:08 +01001031mp_map_t *mp_globals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +00001032 return map_globals;
1033}
1034
Damien Georged17926d2014-03-30 13:35:08 +01001035void mp_globals_set(mp_map_t *m) {
1036 DEBUG_OP_printf("mp_globals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +00001037 map_globals = m;
1038}
1039
Damien George6902eed2014-04-04 10:52:59 +00001040void *m_malloc_fail(int num_bytes) {
1041 DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
1042 nlr_jump((mp_obj_t)&mp_const_MemoryError_obj);
1043}
1044
Damien6ba13142013-11-02 20:34:54 +00001045// these must correspond to the respective enum
Damien Georged17926d2014-03-30 13:35:08 +01001046void *const mp_fun_table[MP_F_NUMBER_OF] = {
1047 mp_load_const_dec,
1048 mp_load_const_str,
1049 mp_load_name,
1050 mp_load_global,
1051 mp_load_build_class,
1052 mp_load_attr,
1053 mp_load_method,
1054 mp_store_name,
1055 mp_store_attr,
1056 mp_store_subscr,
1057 mp_obj_is_true,
1058 mp_unary_op,
1059 mp_binary_op,
Damien George15d18062014-03-31 16:28:13 +01001060 mp_obj_new_tuple,
1061 mp_obj_new_list,
1062 mp_obj_list_append,
1063 mp_obj_new_dict,
1064 mp_obj_dict_store,
1065 mp_obj_new_set,
1066 mp_obj_set_store,
Damien Georged17926d2014-03-30 13:35:08 +01001067 mp_make_function_from_id,
1068 mp_call_function_n_kw_for_native,
1069 mp_call_method_n_kw,
1070 mp_getiter,
1071 mp_iternext,
Damien429d7192013-10-04 19:53:11 +01001072};
1073
1074/*
Damien Georged17926d2014-03-30 13:35:08 +01001075void mp_f_vector(mp_fun_kind_t fun_kind) {
1076 (mp_f_table[fun_kind])();
Damien429d7192013-10-04 19:53:11 +01001077}
1078*/