blob: d9e2298c4cb35f6dfcdab56eef35e2e9f8e9282a [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_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +000076 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +000077 uint len;
78 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +000079 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +000080}
81
Damien Georged17926d2014-03-30 13:35:08 +010082mp_obj_t mp_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010083 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +000084 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +010085}
86
Damien Georged17926d2014-03-30 13:35:08 +010087mp_obj_t mp_load_const_bytes(qstr qstr) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020088 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
89 uint len;
90 const byte *data = qstr_data(qstr, &len);
91 return mp_obj_new_bytes(data, len);
92}
93
Damien Georged17926d2014-03-30 13:35:08 +010094mp_obj_t mp_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010095 // logic: search locals, globals, builtins
Damiena3977762013-10-09 23:10:10 +010096 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +000097 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 +000098 if (elem != NULL) {
99 return elem->value;
100 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100101 return mp_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100102 }
Damiena3977762013-10-09 23:10:10 +0100103}
104
Damien Georged17926d2014-03-30 13:35:08 +0100105mp_obj_t mp_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100106 // logic: search globals, builtins
107 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George38a2da62014-01-08 17:33:12 +0000108 mp_map_elem_t *elem = mp_map_lookup(map_globals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100109 if (elem == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000110 elem = mp_map_lookup(&map_builtins, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100111 if (elem == NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000112 mp_obj_t o = mp_builtin_tables_lookup_object(qstr);
113 if (o != MP_OBJ_NULL) {
114 return o;
Damien Georgeaea532e2014-02-06 22:57:51 +0000115 }
Damien Georgec5966122014-02-15 16:10:44 +0000116 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 +0100117 }
118 }
119 return elem->value;
120}
121
Damien Georged17926d2014-03-30 13:35:08 +0100122mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100123 DEBUG_OP_printf("load_build_class\n");
Damien Georgecaac5422014-03-25 14:18:18 +0000124 // lookup __build_class__ in dynamic table of builtins first
Damien George38a2da62014-01-08 17:33:12 +0000125 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 +0000126 if (elem != NULL) {
Damien Georgecaac5422014-03-25 14:18:18 +0000127 // found user-defined __build_class__, return it
Damien Georgeaea532e2014-02-06 22:57:51 +0000128 return elem->value;
129 } else {
Damien Georgecaac5422014-03-25 14:18:18 +0000130 // no user-defined __build_class__, return builtin one
Damien Georgeaea532e2014-02-06 22:57:51 +0000131 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100132 }
Damien429d7192013-10-04 19:53:11 +0100133}
134
Damien Georged17926d2014-03-30 13:35:08 +0100135void mp_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100136 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000137 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 +0100138}
139
Damien Georged17926d2014-03-30 13:35:08 +0100140void mp_delete_name(qstr qstr) {
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200141 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
142 mp_map_lookup(map_locals, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP_REMOVE_IF_FOUND);
143}
144
Damien Georged17926d2014-03-30 13:35:08 +0100145void mp_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100146 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George38a2da62014-01-08 17:33:12 +0000147 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 +0100148}
149
Damien Georged17926d2014-03-30 13:35:08 +0100150mp_obj_t mp_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000151 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000152
Damiend99b0522013-12-21 18:17:45 +0000153 if (MP_OBJ_IS_SMALL_INT(arg)) {
154 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000155 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100156 case MP_UNARY_OP_BOOL:
Damien George9d68e9c2014-03-12 15:38:15 +0000157 return MP_BOOL(val != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100158 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000159 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100160 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000161 // check for overflow
162 if (val == MP_SMALL_INT_MIN) {
163 return mp_obj_new_int(-val);
164 } else {
165 return MP_OBJ_NEW_SMALL_INT(-val);
166 }
Damien Georged17926d2014-03-30 13:35:08 +0100167 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000168 return MP_OBJ_NEW_SMALL_INT(~val);
169 default:
170 assert(0);
171 return arg;
Damien7410e442013-11-02 19:47:57 +0000172 }
Damien George1e708fe2014-01-23 18:27:51 +0000173 } else {
174 mp_obj_type_t *type = mp_obj_get_type(arg);
175 if (type->unary_op != NULL) {
176 mp_obj_t result = type->unary_op(op, arg);
Damiend99b0522013-12-21 18:17:45 +0000177 if (result != NULL) {
178 return result;
179 }
Damien7410e442013-11-02 19:47:57 +0000180 }
Damiend99b0522013-12-21 18:17:45 +0000181 // TODO specify in error message what the operator is
Damien George0ec6bd42014-03-09 16:29:36 +0000182 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 +0000183 }
Damien429d7192013-10-04 19:53:11 +0100184}
185
Damien Georged17926d2014-03-30 13:35:08 +0100186mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100187 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000188
189 // TODO correctly distinguish inplace operators for mutable objects
190 // lookup logic that CPython uses for +=:
191 // check for implemented +=
192 // then check for implemented +
193 // then check for implemented seq.inplace_concat
194 // then check for implemented seq.concat
195 // then fail
196 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
197
Damien George9aa2a522014-02-01 23:04:09 +0000198 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100199 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200200 return MP_BOOL(lhs == rhs);
201 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200202
Damien Georgebcbeea02014-01-11 10:47:22 +0000203 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100204 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000205 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100206 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000207 return mp_const_true;
208 } else {
209 return mp_const_false;
210 }
211 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100212 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000213 return mp_const_false;
214 } else {
215 return mp_const_true;
216 }
217 }
218 }
219
220 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100221 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000222 // rhs must be issubclass(rhs, BaseException)
223 if (mp_obj_is_exception_type(rhs)) {
224 // if lhs is an instance of an exception, then extract and use its type
225 if (mp_obj_is_exception_instance(lhs)) {
226 lhs = mp_obj_get_type(lhs);
227 }
Damien George71510152014-03-03 22:38:13 +0000228 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000229 return mp_const_true;
230 } else {
231 return mp_const_false;
232 }
233 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200234 assert(0);
235 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000236 }
237
Damien George1a9951d2014-01-06 22:13:00 +0000238 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000239 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000240 if (MP_OBJ_IS_SMALL_INT(rhs)) {
241 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000242 // This is a binary operation: lhs_val op rhs_val
243 // We need to be careful to handle overflow; see CERT INT32-C
244 // Operations that can overflow:
245 // + result always fits in machine_int_t, then handled by SMALL_INT check
246 // - result always fits in machine_int_t, then handled by SMALL_INT check
247 // * checked explicitly
248 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
249 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
250 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000251 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100252 case MP_BINARY_OP_OR:
253 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
254 case MP_BINARY_OP_XOR:
255 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
256 case MP_BINARY_OP_AND:
257 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
258 case MP_BINARY_OP_LSHIFT:
259 case MP_BINARY_OP_INPLACE_LSHIFT: {
Damien George9d68e9c2014-03-12 15:38:15 +0000260 if (rhs_val < 0) {
261 // negative shift not allowed
262 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
263 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
264 // left-shift will overflow, so use higher precision integer
265 lhs = mp_obj_new_int_from_ll(lhs_val);
266 goto generic_binary_op;
267 } else {
268 // use standard precision
269 lhs_val <<= rhs_val;
270 }
271 break;
272 }
Damien Georged17926d2014-03-30 13:35:08 +0100273 case MP_BINARY_OP_RSHIFT:
274 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000275 if (rhs_val < 0) {
276 // negative shift not allowed
277 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
278 } else {
279 // standard precision is enough for right-shift
280 lhs_val >>= rhs_val;
281 }
282 break;
Damien Georged17926d2014-03-30 13:35:08 +0100283 case MP_BINARY_OP_ADD:
284 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
285 case MP_BINARY_OP_SUBTRACT:
286 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
287 case MP_BINARY_OP_MULTIPLY:
288 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000289
290 // If long long type exists and is larger than machine_int_t, then
291 // we can use the following code to perform overflow-checked multiplication.
292 // Otherwise (eg in x64 case) we must use the branching code below.
293 #if 0
294 // compute result using long long precision
295 long long res = (long long)lhs_val * (long long)rhs_val;
296 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
297 // result overflowed SMALL_INT, so return higher precision integer
298 return mp_obj_new_int_from_ll(res);
299 } else {
300 // use standard precision
301 lhs_val = (mp_small_int_t)res;
302 }
303 #endif
304
305 if (lhs_val > 0) { // lhs_val is positive
306 if (rhs_val > 0) { // lhs_val and rhs_val are positive
307 if (lhs_val > (MP_SMALL_INT_MAX / rhs_val)) {
308 goto mul_overflow;
309 }
310 } else { // lhs_val positive, rhs_val nonpositive
311 if (rhs_val < (MP_SMALL_INT_MIN / lhs_val)) {
312 goto mul_overflow;
313 }
314 } // lhs_val positive, rhs_val nonpositive
315 } else { // lhs_val is nonpositive
316 if (rhs_val > 0) { // lhs_val is nonpositive, rhs_val is positive
317 if (lhs_val < (MP_SMALL_INT_MIN / rhs_val)) {
318 goto mul_overflow;
319 }
320 } else { // lhs_val and rhs_val are nonpositive
321 if (lhs_val != 0 && rhs_val < (MP_SMALL_INT_MAX / lhs_val)) {
322 goto mul_overflow;
323 }
324 } // End if lhs_val and rhs_val are nonpositive
325 } // End if lhs_val is nonpositive
326
327 // use standard precision
328 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
329
330 mul_overflow:
331 // use higher precision
332 lhs = mp_obj_new_int_from_ll(lhs_val);
333 goto generic_binary_op;
334
335 break;
336 }
Damien Georged17926d2014-03-30 13:35:08 +0100337 case MP_BINARY_OP_FLOOR_DIVIDE:
338 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300339 if (rhs_val == 0) {
340 goto zero_division;
341 }
Rachel Dowdall56402792014-03-22 20:19:24 +0000342 lhs_val = python_floor_divide(lhs_val, rhs_val);
343 break;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300344
Damien George9d68e9c2014-03-12 15:38:15 +0000345 #if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100346 case MP_BINARY_OP_TRUE_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300347 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
348 if (rhs_val == 0) {
Damien George70f33cd2014-04-02 17:06:05 +0100349 goto zero_division;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300350 }
351 return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000352 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000353
Damien Georged17926d2014-03-30 13:35:08 +0100354 case MP_BINARY_OP_MODULO:
355 case MP_BINARY_OP_INPLACE_MODULO:
Rachel Dowdallcde86312014-03-22 17:29:27 +0000356 {
357 lhs_val = python_modulo(lhs_val, rhs_val);
358 break;
359 }
Damien Georged17926d2014-03-30 13:35:08 +0100360 case MP_BINARY_OP_POWER:
361 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000362 if (rhs_val < 0) {
363 #if MICROPY_ENABLE_FLOAT
364 lhs = mp_obj_new_float(lhs_val);
365 goto generic_binary_op;
366 #else
367 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
368 #endif
369 } else {
Damien George9d68e9c2014-03-12 15:38:15 +0000370 machine_int_t ans = 1;
371 while (rhs_val > 0) {
372 if (rhs_val & 1) {
Damien George5bf565e2014-04-04 00:16:32 +0100373 machine_int_t old = ans;
Damien George9d68e9c2014-03-12 15:38:15 +0000374 ans *= lhs_val;
Damien George5bf565e2014-04-04 00:16:32 +0100375 if (ans < old) {
376 goto power_overflow;
377 }
Damien George9d68e9c2014-03-12 15:38:15 +0000378 }
Damien George5bf565e2014-04-04 00:16:32 +0100379 if (rhs_val == 1) {
380 break;
381 }
Damien George9d68e9c2014-03-12 15:38:15 +0000382 rhs_val /= 2;
Damien George5bf565e2014-04-04 00:16:32 +0100383 machine_int_t old = lhs_val;
384 lhs_val *= lhs_val;
385 if (lhs_val < old) {
386 goto power_overflow;
387 }
Damien George1a9951d2014-01-06 22:13:00 +0000388 }
Damien George9d68e9c2014-03-12 15:38:15 +0000389 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000390 }
Damien George1a9951d2014-01-06 22:13:00 +0000391 break;
Damien George5bf565e2014-04-04 00:16:32 +0100392
393 power_overflow:
394 // use higher precision
395 lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
396 goto generic_binary_op;
397
Damien Georged17926d2014-03-30 13:35:08 +0100398 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
399 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
400 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
401 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000402
Damien George1a9951d2014-01-06 22:13:00 +0000403 default: assert(0);
404 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200405 // TODO: We just should make mp_obj_new_int() inline and use that
406 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000407 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000408 } else {
409 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000410 }
Damien George3f759b72014-01-31 00:42:12 +0000411#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000412 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien George1a9951d2014-01-06 22:13:00 +0000413 return mp_obj_float_binary_op(op, lhs_val, rhs);
Damien George0c36da02014-03-08 15:24:39 +0000414 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien George1a9951d2014-01-06 22:13:00 +0000415 return mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
Damien George3f759b72014-01-31 00:42:12 +0000416#endif
Damien429d7192013-10-04 19:53:11 +0100417 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000418 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000419
Damien George9aa2a522014-02-01 23:04:09 +0000420 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000421 *
422 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000423 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000424 */
Damien Georged17926d2014-03-30 13:35:08 +0100425 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000426 mp_obj_type_t *type = mp_obj_get_type(rhs);
427 if (type->binary_op != NULL) {
428 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George48697f12014-02-01 23:32:29 +0000429 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000430 return res;
431 }
432 }
433 if (type->getiter != NULL) {
434 /* second attempt, walk the iterator */
435 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100436 mp_obj_t iter = mp_getiter(rhs);
437 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000438 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000439 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000440 }
Damien7410e442013-11-02 19:47:57 +0000441 }
Damien George9aa2a522014-02-01 23:04:09 +0000442 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000443 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000444
445 nlr_jump(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000446 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000447 mp_obj_get_type_str(rhs)));
448 return mp_const_none;
449 }
450
Damien George5fa93b62014-01-22 14:35:10 +0000451 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000452 mp_obj_type_t *type;
453generic_binary_op:
454 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000455 if (type->binary_op != NULL) {
456 mp_obj_t result = type->binary_op(op, lhs, rhs);
457 if (result != MP_OBJ_NULL) {
458 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000459 }
Damien429d7192013-10-04 19:53:11 +0100460 }
Damiend99b0522013-12-21 18:17:45 +0000461
Damien George5fa93b62014-01-22 14:35:10 +0000462 // TODO implement dispatch for reverse binary ops
463
Damiend99b0522013-12-21 18:17:45 +0000464 // TODO specify in error message what the operator is
Damien Georgec5966122014-02-15 16:10:44 +0000465 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200466 "unsupported operand types for binary operator: '%s', '%s'",
467 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000468 return mp_const_none;
Damien George70f33cd2014-04-02 17:06:05 +0100469
470zero_division:
471 nlr_jump(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
Damien429d7192013-10-04 19:53:11 +0100472}
473
Damien Georged17926d2014-03-30 13:35:08 +0100474mp_obj_t mp_call_function_0(mp_obj_t fun) {
475 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100476}
477
Damien Georged17926d2014-03-30 13:35:08 +0100478mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
479 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100480}
481
Damien Georged17926d2014-03-30 13:35:08 +0100482mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000483 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000484 args[0] = arg1;
485 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100486 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100487}
488
Damien Georgecd82e022014-02-02 13:11:48 +0000489// wrapper that accepts n_args and n_kw in one argument
490// native emitter can only pass at most 3 arguments to a function
Damien Georged17926d2014-03-30 13:35:08 +0100491mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
492 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 +0000493}
494
Damien George20006db2014-01-18 14:10:48 +0000495// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien Georged17926d2014-03-30 13:35:08 +0100496mp_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 +0000497 // TODO improve this: fun object can specify its type and we parse here the arguments,
498 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100499
John R. Lenton9c83ec02014-01-07 23:06:46 +0000500 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
501
Damien George8b56beb2014-01-31 23:49:49 +0000502 // get the type
503 mp_obj_type_t *type = mp_obj_get_type(fun_in);
504
505 // do the call
506 if (type->call != NULL) {
507 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000508 } else {
Damien George0ec6bd42014-03-09 16:29:36 +0000509 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 +0000510 }
Damien86c7fc72013-11-26 15:16:41 +0000511}
512
Damien George20006db2014-01-18 14:10:48 +0000513// 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)
514// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien Georged17926d2014-03-30 13:35:08 +0100515mp_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 +0000516 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);
517 int adjust = (args[1] == NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100518 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000519}
520
Damien George523b5752014-03-31 11:59:23 +0100521mp_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 +0100522 mp_obj_t fun = *args++;
523 mp_obj_t self = MP_OBJ_NULL;
524 if (have_self) {
525 self = *args++; // may be MP_OBJ_NULL
526 }
527 uint n_args = n_args_n_kw & 0xff;
528 uint n_kw = (n_args_n_kw >> 8) & 0xff;
Damien George523b5752014-03-31 11:59:23 +0100529 mp_obj_t pos_seq = args[n_args + 2 * n_kw]; // map be MP_OBJ_NULL
530 mp_obj_t kw_dict = args[n_args + 2 * n_kw + 1]; // map be MP_OBJ_NULL
Damien George230fec72014-03-30 21:21:24 +0100531
532 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);
533
534 // We need to create the following array of objects:
535 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
536 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
537
538 // The new args array
539 mp_obj_t *args2;
540 uint args2_alloc;
541 uint args2_len = 0;
542
543 // Try to get a hint for the size of the kw_dict
544 uint kw_dict_len = 0;
545 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
546 kw_dict_len = mp_obj_dict_len(kw_dict);
547 }
548
549 // Extract the pos_seq sequence to the new args array.
550 // Note that it can be arbitrary iterator.
551 if (pos_seq == MP_OBJ_NULL) {
552 // no sequence
553
554 // allocate memory for the new array of args
555 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
556 args2 = m_new(mp_obj_t, args2_alloc);
557
558 // copy the self
559 if (self != MP_OBJ_NULL) {
560 args2[args2_len++] = self;
561 }
562
563 // copy the fixed pos args
564 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
565 args2_len += n_args;
566
567 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
568 // optimise the case of a tuple and list
569
570 // get the items
571 uint len;
572 mp_obj_t *items;
573 mp_obj_get_array(pos_seq, &len, &items);
574
575 // allocate memory for the new array of args
576 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
577 args2 = m_new(mp_obj_t, args2_alloc);
578
579 // copy the self
580 if (self != MP_OBJ_NULL) {
581 args2[args2_len++] = self;
582 }
583
584 // copy the fixed and variable position args
585 m_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
586 args2_len += n_args + len;
587
588 } else {
589 // generic iterator
590
591 // allocate memory for the new array of args
592 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
593 args2 = m_new(mp_obj_t, args2_alloc);
594
595 // copy the self
596 if (self != MP_OBJ_NULL) {
597 args2[args2_len++] = self;
598 }
599
600 // copy the fixed position args
601 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
602
603 // extract the variable position args from the iterator
604 mp_obj_t iterable = mp_getiter(pos_seq);
605 mp_obj_t item;
606 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
607 if (args2_len >= args2_alloc) {
608 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
609 args2_alloc *= 2;
610 }
611 args2[args2_len++] = item;
612 }
613 }
614
615 // The size of the args2 array now is the number of positional args.
616 uint pos_args_len = args2_len;
617
618 // Copy the fixed kw args.
619 m_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
620 args2_len += 2 * n_kw;
621
622 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
623 // Note that it can be arbitrary iterator.
624 if (kw_dict == MP_OBJ_NULL) {
625 // pass
626 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
627 // dictionary
628 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
629 assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above
630 for (uint i = 0; i < map->alloc; i++) {
631 if (map->table[i].key != MP_OBJ_NULL) {
632 args2[args2_len++] = map->table[i].key;
633 args2[args2_len++] = map->table[i].value;
634 }
635 }
636 } else {
637 // generic mapping
638 // TODO is calling 'items' on the mapping the correct thing to do here?
639 mp_obj_t dest[2];
640 mp_load_method(kw_dict, MP_QSTR_items, dest);
641 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
642 mp_obj_t item;
643 while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
644 if (args2_len + 1 >= args2_alloc) {
645 uint new_alloc = args2_alloc * 2;
646 if (new_alloc < 4) {
647 new_alloc = 4;
648 }
649 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
650 args2_alloc = new_alloc;
651 }
652 mp_obj_t *items;
653 mp_obj_get_array_fixed_n(item, 2, &items);
654 args2[args2_len++] = items[0];
655 args2[args2_len++] = items[1];
656 }
657 }
658
659 mp_obj_t res = mp_call_function_n_kw(fun, pos_args_len, (args2_len - pos_args_len) / 2, args2);
660 m_del(mp_obj_t, args2, args2_alloc);
661
662 return res;
663}
664
Damien George932bf1c2014-01-18 23:42:49 +0000665// unpacked items are stored in reverse order into the array pointed to by items
Damien Georged17926d2014-03-30 13:35:08 +0100666void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200667 uint seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000668 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 +0000669 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000670 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000671 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
672 } else {
673 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000674 }
Damiend99b0522013-12-21 18:17:45 +0000675 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200676 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000677 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200678 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000679 }
Damien George932bf1c2014-01-18 23:42:49 +0000680 for (uint i = 0; i < num; i++) {
681 items[i] = seq_items[num - 1 - i];
682 }
Damien86c7fc72013-11-26 15:16:41 +0000683 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100684 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200685
686 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100687 mp_obj_t el = mp_iternext(iterable);
Damien George66eaf842014-03-26 19:27:58 +0000688 if (el == MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200689 goto too_short;
690 }
691 items[num - 1 - seq_len] = el;
692 }
Damien Georged17926d2014-03-30 13:35:08 +0100693 if (mp_iternext(iterable) != MP_OBJ_NULL) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200694 goto too_long;
695 }
Damien86c7fc72013-11-26 15:16:41 +0000696 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200697 return;
698
699too_short:
Damien Georgec5966122014-02-15 16:10:44 +0000700 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 +0200701too_long:
Damien Georgec5966122014-02-15 16:10:44 +0000702 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 +0000703}
704
Damien Georged17926d2014-03-30 13:35:08 +0100705mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000706 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
707 // use load_method
708 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100709 mp_load_method(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000710 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000711 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000712 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000713 } else {
714 // load_method returned a method, so build a bound method object
715 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000716 }
Damiend99b0522013-12-21 18:17:45 +0000717}
718
Damien George7c9c6672014-01-25 00:17:36 +0000719// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
720// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
721// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Damien Georgee44d26a2014-03-31 22:57:56 +0100722void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000723 // clear output to indicate no attribute/method found yet
724 dest[0] = MP_OBJ_NULL;
725 dest[1] = MP_OBJ_NULL;
726
727 // get the type
728 mp_obj_type_t *type = mp_obj_get_type(base);
729
Damien Georgee44d26a2014-03-31 22:57:56 +0100730 // look for built-in names
731 if (0) {
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300732#if MICROPY_CPYTHON_COMPAT
Damien Georgee44d26a2014-03-31 22:57:56 +0100733 } else if (attr == MP_QSTR___class__) {
734 // a.__class__ is equivalent to type(a)
735 dest[0] = type;
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300736#endif
Damien Georgee44d26a2014-03-31 22:57:56 +0100737
738 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
739 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
740 dest[1] = base;
741
742 } else if (type->load_attr != NULL) {
743 // this type can do its own load, so call it
744 type->load_attr(base, attr, dest);
745
746 } else if (type->locals_dict != NULL) {
747 // generic method lookup
748 // this is a lookup in the object (ie not class or type)
749 assert(MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)); // Micro Python restriction, for now
750 mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
751 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
752 if (elem != NULL) {
753 // check if the methods are functions, static or class methods
754 // see http://docs.python.org/3.3/howto/descriptor.html
755 if (MP_OBJ_IS_TYPE(elem->value, &mp_type_staticmethod)) {
756 // return just the function
757 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
758 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_classmethod)) {
759 // return a bound method, with self being the type of this object
760 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
761 dest[1] = mp_obj_get_type(base);
762 } else if (mp_obj_is_callable(elem->value)) {
763 // return a bound method, with self being this object
764 dest[0] = elem->value;
765 dest[1] = base;
766 } else {
767 // class member is a value, so just return that value
768 dest[0] = elem->value;
Damiend57eba52013-11-02 23:58:14 +0000769 }
770 }
Damiena3977762013-10-09 23:10:10 +0100771 }
Damien George7c9c6672014-01-25 00:17:36 +0000772}
Damiena3977762013-10-09 23:10:10 +0100773
Damien Georged17926d2014-03-30 13:35:08 +0100774void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +0000775 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
776
Damien Georged17926d2014-03-30 13:35:08 +0100777 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000778
779 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000780 // no attribute/method called attr
781 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000782 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200783 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
784 "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 +0000785 } else {
Damien Georgec5966122014-02-15 16:10:44 +0000786 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 +0000787 }
788 }
Damiena3977762013-10-09 23:10:10 +0100789}
790
Damien Georged17926d2014-03-30 13:35:08 +0100791void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100792 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000793 mp_obj_type_t *type = mp_obj_get_type(base);
794 if (type->store_attr != NULL) {
795 if (type->store_attr(base, attr, value)) {
796 return;
797 }
Damiena3977762013-10-09 23:10:10 +0100798 }
Damien Georgec5966122014-02-15 16:10:44 +0000799 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 +0100800}
801
Damien Georged17926d2014-03-30 13:35:08 +0100802void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100803 DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000804 if (MP_OBJ_IS_TYPE(base, &mp_type_list)) {
Damien429d7192013-10-04 19:53:11 +0100805 // list store
Damiend99b0522013-12-21 18:17:45 +0000806 mp_obj_list_store(base, index, value);
Damien George3e1a5c12014-03-29 13:43:38 +0000807 } else if (MP_OBJ_IS_TYPE(base, &mp_type_dict)) {
Damiend99b0522013-12-21 18:17:45 +0000808 // dict store
809 mp_obj_dict_store(base, index, value);
Damien429d7192013-10-04 19:53:11 +0100810 } else {
Paul Sokolovsky6d8edf62014-01-18 13:10:51 +0200811 mp_obj_type_t *type = mp_obj_get_type(base);
812 if (type->store_item != NULL) {
813 bool r = type->store_item(base, index, value);
814 if (r) {
815 return;
816 }
817 // TODO: call base classes here?
818 }
Damien Georgec5966122014-02-15 16:10:44 +0000819 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 +0100820 }
821}
822
Damien Georged17926d2014-03-30 13:35:08 +0100823mp_obj_t mp_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000824 mp_obj_type_t *type = mp_obj_get_type(o_in);
825 if (type->getiter != NULL) {
826 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100827 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000828 // check for __iter__ method
Damien George7c9c6672014-01-25 00:17:36 +0000829 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100830 mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000831 if (dest[0] != MP_OBJ_NULL) {
Damien George9e6e9352014-03-26 18:37:06 +0000832 // __iter__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100833 return mp_call_method_n_kw(0, 0, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000834 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100835 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000836 if (dest[0] != MP_OBJ_NULL) {
837 // __getitem__ exists, create an iterator
838 return mp_obj_new_getitem_iter(dest);
839 } else {
840 // object not iterable
841 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
842 }
Damien George7c9c6672014-01-25 00:17:36 +0000843 }
Damience89a212013-10-15 22:25:17 +0100844 }
845}
846
Damien George66eaf842014-03-26 19:27:58 +0000847// may return MP_OBJ_NULL as an optimisation instead of raise StopIteration()
848// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +0100849mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000850 mp_obj_type_t *type = mp_obj_get_type(o_in);
851 if (type->iternext != NULL) {
852 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100853 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000854 // check for __next__ method
855 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100856 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000857 if (dest[0] != MP_OBJ_NULL) {
858 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100859 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000860 } else {
861 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
862 }
Damien Georgec5966122014-02-15 16:10:44 +0000863 }
864}
865
Damien George66eaf842014-03-26 19:27:58 +0000866// will always return MP_OBJ_NULL instead of raising StopIteration() (or any subclass thereof)
867// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +0100868mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George66eaf842014-03-26 19:27:58 +0000869 mp_obj_type_t *type = mp_obj_get_type(o_in);
870 if (type->iternext != NULL) {
871 return type->iternext(o_in);
872 } else {
873 // check for __next__ method
874 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100875 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +0000876 if (dest[0] != MP_OBJ_NULL) {
877 // __next__ exists, call it and return its result
878 nlr_buf_t nlr;
879 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100880 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +0000881 nlr_pop();
882 return ret;
883 } else {
884 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
885 return MP_OBJ_NULL;
886 } else {
887 nlr_jump(nlr.ret_val);
888 }
889 }
890 } else {
891 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
892 }
893 }
894}
895
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300896// TODO: Unclear what to do with StopIterarion exception here.
897mp_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 +0300898 assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300899 mp_obj_type_t *type = mp_obj_get_type(self_in);
900
901 if (type == &mp_type_gen_instance) {
902 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
903 }
904
905 if (type->iternext != NULL && send_value == mp_const_none) {
906 mp_obj_t ret = type->iternext(self_in);
907 if (ret != MP_OBJ_NULL) {
908 *ret_val = ret;
909 return MP_VM_RETURN_YIELD;
910 } else {
911 // Emulate raise StopIteration()
912 // Special case, handled in vm.c
913 *ret_val = MP_OBJ_NULL;
914 return MP_VM_RETURN_NORMAL;
915 }
916 }
917
918 mp_obj_t dest[3]; // Reserve slot for send() arg
919
920 if (send_value == mp_const_none) {
921 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
922 if (dest[0] != MP_OBJ_NULL) {
923 *ret_val = mp_call_method_n_kw(0, 0, dest);
924 return MP_VM_RETURN_YIELD;
925 }
926 }
927
928 if (send_value != MP_OBJ_NULL) {
929 mp_load_method(self_in, MP_QSTR_send, dest);
930 dest[2] = send_value;
931 *ret_val = mp_call_method_n_kw(1, 0, dest);
932 return MP_VM_RETURN_YIELD;
933 }
934
935 if (throw_value != MP_OBJ_NULL) {
936 if (mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
937 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
938 if (dest[0] != MP_OBJ_NULL) {
939 *ret_val = mp_call_method_n_kw(0, 0, dest);
940 // We assume one can't "yield" from close()
941 return MP_VM_RETURN_NORMAL;
942 }
943 }
Paul Sokolovskya2109d92014-03-31 04:14:30 +0300944 mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
945 if (dest[0] != MP_OBJ_NULL) {
946 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
947 // If .throw() method returned, we assume it's value to yield
948 // - any exception would be thrown with nlr_jump().
949 return MP_VM_RETURN_YIELD;
950 }
951 // If there's nowhere to throw exception into, then we assume that object
952 // is just incapable to handle it, so any exception thrown into it
953 // will be propagated up. This behavior is approved by test_pep380.py
954 // test_delegation_of_close_to_non_generator(),
955 // test_delegating_throw_to_non_generator()
956 *ret_val = throw_value;
957 return MP_VM_RETURN_EXCEPTION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300958 }
959
960 assert(0);
961 return MP_VM_RETURN_NORMAL; // Should be unreachable
962}
963
Damien Georged17926d2014-03-30 13:35:08 +0100964mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +0000965 DEBUG_printf("raise %p\n", o);
966 if (mp_obj_is_exception_type(o)) {
967 // o is an exception type (it is derived from BaseException (or is BaseException))
968 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +0000969 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
970 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +0100971 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +0000972 } else if (mp_obj_is_exception_instance(o)) {
973 // o is an instance of an exception, so use it as the exception
974 return o;
975 } else {
976 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
977 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +0100978 }
979}
980
Damien Georged17926d2014-03-30 13:35:08 +0100981mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +0000982 DEBUG_printf("import name %s\n", qstr_str(name));
983
Damiendb4c3612013-12-10 17:27:24 +0000984 // build args array
Damiend99b0522013-12-21 18:17:45 +0000985 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +0000986 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +0000987 args[1] = mp_const_none; // TODO should be globals
988 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +0000989 args[3] = fromlist;
990 args[4] = level; // must be 0; we don't yet support other values
991
992 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +0000993 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +0000994}
995
Damien Georged17926d2014-03-30 13:35:08 +0100996mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +0000997 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
998
Damien Georged17926d2014-03-30 13:35:08 +0100999 mp_obj_t x = mp_load_attr(module, name);
Damiendb4c3612013-12-10 17:27:24 +00001000 /* TODO convert AttributeError to ImportError
1001 if (fail) {
1002 (ImportError, "cannot import name %s", qstr_str(name), NULL)
1003 }
1004 */
1005 return x;
1006}
1007
Damien Georged17926d2014-03-30 13:35:08 +01001008void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001009 DEBUG_printf("import all %p\n", module);
1010
1011 mp_map_t *map = mp_obj_module_get_globals(module);
1012 for (uint i = 0; i < map->alloc; i++) {
1013 if (map->table[i].key != MP_OBJ_NULL) {
Damien Georged17926d2014-03-30 13:35:08 +01001014 mp_store_name(MP_OBJ_QSTR_VALUE(map->table[i].key), map->table[i].value);
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001015 }
1016 }
1017}
1018
Damien Georged17926d2014-03-30 13:35:08 +01001019mp_map_t *mp_locals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +00001020 return map_locals;
1021}
1022
Damien Georged17926d2014-03-30 13:35:08 +01001023void mp_locals_set(mp_map_t *m) {
1024 DEBUG_OP_printf("mp_locals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +00001025 map_locals = m;
1026}
1027
Damien Georged17926d2014-03-30 13:35:08 +01001028mp_map_t *mp_globals_get(void) {
Damien George66028ab2014-01-03 14:03:48 +00001029 return map_globals;
1030}
1031
Damien Georged17926d2014-03-30 13:35:08 +01001032void mp_globals_set(mp_map_t *m) {
1033 DEBUG_OP_printf("mp_globals_set(%p)\n", m);
Damien George66028ab2014-01-03 14:03:48 +00001034 map_globals = m;
1035}
1036
Damien6ba13142013-11-02 20:34:54 +00001037// these must correspond to the respective enum
Damien Georged17926d2014-03-30 13:35:08 +01001038void *const mp_fun_table[MP_F_NUMBER_OF] = {
1039 mp_load_const_dec,
1040 mp_load_const_str,
1041 mp_load_name,
1042 mp_load_global,
1043 mp_load_build_class,
1044 mp_load_attr,
1045 mp_load_method,
1046 mp_store_name,
1047 mp_store_attr,
1048 mp_store_subscr,
1049 mp_obj_is_true,
1050 mp_unary_op,
1051 mp_binary_op,
Damien George15d18062014-03-31 16:28:13 +01001052 mp_obj_new_tuple,
1053 mp_obj_new_list,
1054 mp_obj_list_append,
1055 mp_obj_new_dict,
1056 mp_obj_dict_store,
1057 mp_obj_new_set,
1058 mp_obj_set_store,
Damien Georged17926d2014-03-30 13:35:08 +01001059 mp_make_function_from_id,
1060 mp_call_function_n_kw_for_native,
1061 mp_call_method_n_kw,
1062 mp_getiter,
1063 mp_iternext,
Damien429d7192013-10-04 19:53:11 +01001064};
1065
1066/*
Damien Georged17926d2014-03-30 13:35:08 +01001067void mp_f_vector(mp_fun_kind_t fun_kind) {
1068 (mp_f_table[fun_kind])();
Damien429d7192013-10-04 19:53:11 +01001069}
1070*/