blob: f55d44ca293a6b08fd798a460997a0de2646c298 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Damiend99b0522013-12-21 18:17:45 +000028#include <string.h>
29#include <assert.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/objtuple.h"
33#include "py/objfun.h"
34#include "py/runtime0.h"
35#include "py/runtime.h"
36#include "py/bc.h"
37#include "py/stackctrl.h"
Damiend99b0522013-12-21 18:17:45 +000038
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020039#if 0 // print debugging info
40#define DEBUG_PRINT (1)
41#else // don't print debugging info
Damien George7860c2a2014-11-05 21:16:41 +000042#define DEBUG_PRINT (0)
Damien George41eb6082014-02-26 22:40:35 +000043#define DEBUG_printf(...) (void)0
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020044#endif
45
Damien Georgee233a552015-01-11 21:07:15 +000046// Note: the "name" entry in mp_obj_type_t for a function type must be
47// MP_QSTR_function because it is used to determine if an object is of generic
48// function type.
Paul Sokolovsky586bfce2014-04-05 13:50:06 +030049
Damien George3c658a42014-08-24 16:28:17 +010050/******************************************************************************/
51/* builtin functions */
52
53// mp_obj_fun_builtin_t defined in obj.h
54
Damien Georgeecc88e92014-08-30 00:35:11 +010055STATIC mp_obj_t fun_builtin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George3c658a42014-08-24 16:28:17 +010056 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin));
57 mp_obj_fun_builtin_t *self = self_in;
Damien George20006db2014-01-18 14:10:48 +000058
John R. Lenton88cb1e62014-01-13 19:55:18 +000059 // check number of arguments
Damien Georgea3f94e02014-04-20 00:13:22 +010060 mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
Damien George20006db2014-01-18 14:10:48 +000061
John R. Lentonc06763a2014-01-07 17:29:16 +000062 if (self->is_kw) {
Damien George20006db2014-01-18 14:10:48 +000063 // function allows keywords
64
Damien George0a587b82014-02-08 18:53:41 +000065 // we create a map directly from the given args array
66 mp_map_t kw_args;
67 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
Damien George20006db2014-01-18 14:10:48 +000068
Damien George0a587b82014-02-08 18:53:41 +000069 return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
Damien George20006db2014-01-18 14:10:48 +000070
Paul Sokolovsky9d95a2b2014-01-26 01:58:51 +020071 } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
Damiend99b0522013-12-21 18:17:45 +000072 // function requires a fixed number of arguments
73
Damiend99b0522013-12-21 18:17:45 +000074 // dispatch function call
75 switch (self->n_args_min) {
76 case 0:
77 return ((mp_fun_0_t)self->fun)();
78
79 case 1:
80 return ((mp_fun_1_t)self->fun)(args[0]);
81
82 case 2:
Damien George20006db2014-01-18 14:10:48 +000083 return ((mp_fun_2_t)self->fun)(args[0], args[1]);
Damiend99b0522013-12-21 18:17:45 +000084
John R. Lenton45a87442014-01-04 01:15:01 +000085 case 3:
Damiend99b0522013-12-21 18:17:45 +000086 default:
Damien Georged2d64f02015-01-14 21:32:42 +000087 return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
Damiend99b0522013-12-21 18:17:45 +000088 }
89
90 } else {
Damien George20006db2014-01-18 14:10:48 +000091 // function takes a variable number of arguments, but no keywords
Damiend99b0522013-12-21 18:17:45 +000092
Damien George20006db2014-01-18 14:10:48 +000093 return ((mp_fun_var_t)self->fun)(n_args, args);
Damiend99b0522013-12-21 18:17:45 +000094 }
95}
96
Damien George3c658a42014-08-24 16:28:17 +010097const mp_obj_type_t mp_type_fun_builtin = {
Damien Georgec5966122014-02-15 16:10:44 +000098 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000099 .name = MP_QSTR_function,
Damien George3c658a42014-08-24 16:28:17 +0100100 .call = fun_builtin_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000101 .unary_op = mp_generic_unary_op,
Damiend99b0522013-12-21 18:17:45 +0000102};
103
Damiend99b0522013-12-21 18:17:45 +0000104/******************************************************************************/
105/* byte code functions */
106
stijn3cc17c62015-02-14 18:44:31 +0100107qstr mp_obj_code_get_name(const byte *code_info) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100108 mp_decode_uint(&code_info); // skip code_info_size entry
stijn3cc17c62015-02-14 18:44:31 +0100109 return mp_decode_uint(&code_info);
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300110}
111
Damien George99886182015-04-06 22:38:53 +0100112#if MICROPY_EMIT_NATIVE
113STATIC const mp_obj_type_t mp_type_fun_native;
114#endif
115
stijn3cc17c62015-02-14 18:44:31 +0100116qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300117 const mp_obj_fun_bc_t *fun = fun_in;
Damien George99886182015-04-06 22:38:53 +0100118 #if MICROPY_EMIT_NATIVE
119 if (fun->base.type == &mp_type_fun_native) {
120 // TODO native functions don't have name stored
121 return MP_QSTR_;
122 }
123 #endif
Damien George9b7f5832015-03-18 17:47:47 +0000124
125 const byte *bc = fun->bytecode;
126 mp_decode_uint(&bc); // skip n_state
127 mp_decode_uint(&bc); // skip n_exc_stack
Damien George3a3db4d2015-10-22 23:45:37 +0100128 bc++; // skip scope_params
Damien George713ea182015-10-23 01:23:11 +0100129 bc++; // skip n_pos_args
130 bc++; // skip n_kwonly_args
Damien George3a3db4d2015-10-22 23:45:37 +0100131 bc++; // skip n_def_pos_args
Damien George9b7f5832015-03-18 17:47:47 +0000132 return mp_obj_code_get_name(bc);
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300133}
134
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300135#if MICROPY_CPYTHON_COMPAT
Damien George7f9d1d62015-04-09 23:56:15 +0100136STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000137 (void)kind;
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300138 mp_obj_fun_bc_t *o = o_in;
Damien George044c4732015-04-11 13:03:37 +0100139 mp_printf(print, "<function %q at 0x%x>", mp_obj_fun_get_name(o), o);
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300140}
141#endif
142
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200143#if DEBUG_PRINT
Damien George39dc1452014-10-03 19:52:22 +0100144STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200145 DEBUG_printf("%p: ", a);
Damien George39dc1452014-10-03 19:52:22 +0100146 for (mp_uint_t i = 0; i < sz; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200147 DEBUG_printf("%p ", a[i]);
148 }
149 DEBUG_printf("\n");
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200150}
Damien Georgef78b6df2014-03-31 15:59:25 +0100151#else
152#define dump_args(...) (void)0
153#endif
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200154
Damien Georgeaabd83e2014-06-07 14:16:08 +0100155// With this macro you can tune the maximum number of function state bytes
156// that will be allocated on the stack. Any function that needs more
Damien George6e56bb62015-06-08 22:07:27 +0100157// than this will try to use the heap, with fallback to stack allocation.
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800158#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
Damien Georgeaabd83e2014-06-07 14:16:08 +0100159
160// Set this to enable a simple stack overflow check.
161#define VM_DETECT_STACK_OVERFLOW (0)
162
Damien George12a5e172015-04-01 23:31:30 +0100163#if MICROPY_STACKLESS
Paul Sokolovsky20397572015-03-28 01:14:44 +0200164mp_code_state *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
165 MP_STACK_CHECK();
166 mp_obj_fun_bc_t *self = self_in;
167
Damien George9b7f5832015-03-18 17:47:47 +0000168 // get start of bytecode
169 const byte *ip = self->bytecode;
Paul Sokolovsky20397572015-03-28 01:14:44 +0200170
171 // bytecode prelude: state size and exception stack size
172 mp_uint_t n_state = mp_decode_uint(&ip);
173 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
174
175 // allocate state for locals and stack
176 mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
177 mp_code_state *code_state;
Paul Sokolovsky332a9092015-03-28 01:14:44 +0200178 code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
179 if (!code_state) {
180 return NULL;
181 }
Paul Sokolovsky20397572015-03-28 01:14:44 +0200182
Damien George9b7f5832015-03-18 17:47:47 +0000183 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
Paul Sokolovsky20397572015-03-28 01:14:44 +0200184 code_state->n_state = n_state;
Paul Sokolovsky20397572015-03-28 01:14:44 +0200185 mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
186
187 // execute the byte code with the correct globals context
188 code_state->old_globals = mp_globals_get();
189 mp_globals_set(self->globals);
190
191 return code_state;
192}
Damien George12a5e172015-04-01 23:31:30 +0100193#endif
Paul Sokolovsky20397572015-03-28 01:14:44 +0200194
Damien Georgeecc88e92014-08-30 00:35:11 +0100195STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300196 MP_STACK_CHECK();
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300197
Damien Georgeeaaebf32014-09-23 10:59:05 +0100198 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300199 DEBUG_printf("Input pos args: ");
200 dump_args(args, n_args);
201 DEBUG_printf("Input kw args: ");
202 dump_args(args + n_args, n_kw * 2);
203 mp_obj_fun_bc_t *self = self_in;
204 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
205
Damien George9b7f5832015-03-18 17:47:47 +0000206 // get start of bytecode
207 const byte *ip = self->bytecode;
Damien George1084b0f2014-10-25 15:07:02 +0100208
209 // bytecode prelude: state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +0100210 mp_uint_t n_state = mp_decode_uint(&ip);
211 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300212
213#if VM_DETECT_STACK_OVERFLOW
214 n_state += 1;
215#endif
216
217 // allocate state for locals and stack
Damien George3c658a42014-08-24 16:28:17 +0100218 mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
Damien George6e56bb62015-06-08 22:07:27 +0100219 mp_code_state *code_state = NULL;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300220 if (state_size > VM_MAX_STATE_ON_STACK) {
Damien George6e56bb62015-06-08 22:07:27 +0100221 code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
222 }
223 if (code_state == NULL) {
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300224 code_state = alloca(sizeof(mp_code_state) + state_size);
Damien George6e56bb62015-06-08 22:07:27 +0100225 state_size = 0; // indicate that we allocated using alloca
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300226 }
227
Damien George9b7f5832015-03-18 17:47:47 +0000228 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300229 code_state->n_state = n_state;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300230 mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
Damien George049a7a82014-06-08 00:37:40 +0100231
232 // execute the byte code with the correct globals context
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800233 code_state->old_globals = mp_globals_get();
Damien George049a7a82014-06-08 00:37:40 +0100234 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100235 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800236 mp_globals_set(code_state->old_globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100237
238#if VM_DETECT_STACK_OVERFLOW
239 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
240 if (code_state->sp < code_state->state) {
241 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
242 assert(0);
243 }
244 }
245 // We can't check the case when an exception is returned in state[n_state - 1]
246 // and there are no arguments, because in this case our detection slot may have
247 // been overwritten by the returned exception (which is allowed).
Damien George049a7a82014-06-08 00:37:40 +0100248 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100249 // Just check to see that we have at least 1 null object left in the state.
250 bool overflow = true;
Damien George3c658a42014-08-24 16:28:17 +0100251 for (mp_uint_t i = 0; i < n_state - self->n_pos_args - self->n_kwonly_args; i++) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100252 if (code_state->state[i] == MP_OBJ_NULL) {
253 overflow = false;
254 break;
255 }
256 }
257 if (overflow) {
258 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
259 assert(0);
260 }
261 }
262#endif
263
Damien George049a7a82014-06-08 00:37:40 +0100264 mp_obj_t result;
Damien Georgeaabd83e2014-06-07 14:16:08 +0100265 switch (vm_return_kind) {
266 case MP_VM_RETURN_NORMAL:
267 // return value is in *sp
268 result = *code_state->sp;
269 break;
270
271 case MP_VM_RETURN_EXCEPTION:
272 // return value is in state[n_state - 1]
273 result = code_state->state[n_state - 1];
274 break;
275
276 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
277 default:
278 assert(0);
279 result = mp_const_none;
280 vm_return_kind = MP_VM_RETURN_NORMAL;
281 break;
282 }
283
284 // free the state if it was allocated on the heap
Damien George6e56bb62015-06-08 22:07:27 +0100285 if (state_size != 0) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100286 m_del_var(mp_code_state, byte, state_size, code_state);
287 }
288
Damien Georgec8f78bc2014-02-15 22:55:00 +0000289 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
290 return result;
291 } else { // MP_VM_RETURN_EXCEPTION
Damien Georgeea13f402014-04-05 18:32:08 +0100292 nlr_raise(result);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000293 }
Damiend99b0522013-12-21 18:17:45 +0000294}
295
stijn3cc17c62015-02-14 18:44:31 +0100296#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000297STATIC void fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
298 if (dest[0] != MP_OBJ_NULL) {
299 // not load attribute
300 return;
301 }
Damien George4dea9222015-04-09 15:29:54 +0000302 if (attr == MP_QSTR___name__) {
stijn3cc17c62015-02-14 18:44:31 +0100303 dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
304 }
305}
306#endif
307
Damien George3e1a5c12014-03-29 13:43:38 +0000308const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000309 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000310 .name = MP_QSTR_function,
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300311#if MICROPY_CPYTHON_COMPAT
312 .print = fun_bc_print,
313#endif
Damien George20006db2014-01-18 14:10:48 +0000314 .call = fun_bc_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000315 .unary_op = mp_generic_unary_op,
stijn3cc17c62015-02-14 18:44:31 +0100316#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000317 .attr = fun_bc_attr,
stijn3cc17c62015-02-14 18:44:31 +0100318#endif
Damiend99b0522013-12-21 18:17:45 +0000319};
320
Damien George713ea182015-10-23 01:23:11 +0100321mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code, const mp_uint_t *const_table) {
Damien George3c658a42014-08-24 16:28:17 +0100322 mp_uint_t n_def_args = 0;
323 mp_uint_t n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200324 mp_obj_tuple_t *def_args = def_args_in;
325 if (def_args != MP_OBJ_NULL) {
Damien George69b89d22014-04-11 13:38:30 +0000326 assert(MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200327 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000328 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200329 }
Damien Georgef0778a72014-06-07 22:01:00 +0100330 if (def_kw_args != MP_OBJ_NULL) {
331 n_extra_args += 1;
332 }
Damien George2e482cd2014-02-16 00:01:29 +0000333 mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
Damien George3e1a5c12014-03-29 13:43:38 +0000334 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100335 o->globals = mp_globals_get();
Damien George66028ab2014-01-03 14:03:48 +0000336 o->bytecode = code;
Damien George713ea182015-10-23 01:23:11 +0100337 o->const_table = const_table;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200338 if (def_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100339 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Damien Georgef0778a72014-06-07 22:01:00 +0100340 }
341 if (def_kw_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100342 o->extra_args[n_def_args] = def_kw_args;
Damien George2827d622014-04-27 15:50:52 +0100343 }
Damiend99b0522013-12-21 18:17:45 +0000344 return o;
345}
346
Damiend99b0522013-12-21 18:17:45 +0000347/******************************************************************************/
Damien George3c658a42014-08-24 16:28:17 +0100348/* native functions */
349
350#if MICROPY_EMIT_NATIVE
351
Damien Georgeecc88e92014-08-30 00:35:11 +0100352STATIC mp_obj_t fun_native_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George99886182015-04-06 22:38:53 +0100353 MP_STACK_CHECK();
354 mp_obj_fun_bc_t *self = self_in;
355 mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
356 return fun(self_in, n_args, n_kw, args);
Damien George3c658a42014-08-24 16:28:17 +0100357}
358
359STATIC const mp_obj_type_t mp_type_fun_native = {
360 { &mp_type_type },
361 .name = MP_QSTR_function,
362 .call = fun_native_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000363 .unary_op = mp_generic_unary_op,
Damien George3c658a42014-08-24 16:28:17 +0100364};
365
Damien George713ea182015-10-23 01:23:11 +0100366mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table) {
367 mp_obj_fun_bc_t *o = mp_obj_new_fun_bc(def_args_in, def_kw_args, (const byte*)fun_data, const_table);
Damien George3c658a42014-08-24 16:28:17 +0100368 o->base.type = &mp_type_fun_native;
Damien George3c658a42014-08-24 16:28:17 +0100369 return o;
370}
371
372#endif // MICROPY_EMIT_NATIVE
373
374/******************************************************************************/
Damien George2ac4af62014-08-15 16:45:41 +0100375/* viper functions */
376
377#if MICROPY_EMIT_NATIVE
378
379typedef struct _mp_obj_fun_viper_t {
380 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100381 mp_uint_t n_args;
382 void *fun_data; // GC must be able to trace this pointer
Damien George2ac4af62014-08-15 16:45:41 +0100383 mp_uint_t type_sig;
384} mp_obj_fun_viper_t;
385
Damien Georgebbf5cd02015-01-12 22:45:35 +0000386typedef mp_uint_t (*viper_fun_0_t)(void);
Damien George2ac4af62014-08-15 16:45:41 +0100387typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
388typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
389typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damien Georgee45c1db2015-07-23 14:11:29 +0100390typedef mp_uint_t (*viper_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
Damien George2ac4af62014-08-15 16:45:41 +0100391
Damien Georgeecc88e92014-08-30 00:35:11 +0100392STATIC mp_obj_t fun_viper_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George2ac4af62014-08-15 16:45:41 +0100393 mp_obj_fun_viper_t *self = self_in;
394
395 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
396
Damien George3c658a42014-08-24 16:28:17 +0100397 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
398
Damien George2ac4af62014-08-15 16:45:41 +0100399 mp_uint_t ret;
400 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100401 ret = ((viper_fun_0_t)fun)();
Damien George2ac4af62014-08-15 16:45:41 +0100402 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100403 ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2));
Damien George2ac4af62014-08-15 16:45:41 +0100404 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100405 ret = ((viper_fun_2_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2), mp_convert_obj_to_native(args[1], self->type_sig >> 4));
Damien George2ac4af62014-08-15 16:45:41 +0100406 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100407 ret = ((viper_fun_3_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2), mp_convert_obj_to_native(args[1], self->type_sig >> 4), mp_convert_obj_to_native(args[2], self->type_sig >> 6));
Damien Georgee45c1db2015-07-23 14:11:29 +0100408 } else if (n_args == 4) {
409 ret = ((viper_fun_4_t)fun)(
410 mp_convert_obj_to_native(args[0], self->type_sig >> 2),
411 mp_convert_obj_to_native(args[1], self->type_sig >> 4),
412 mp_convert_obj_to_native(args[2], self->type_sig >> 6),
413 mp_convert_obj_to_native(args[3], self->type_sig >> 8)
414 );
Damien George2ac4af62014-08-15 16:45:41 +0100415 } else {
Damien Georgee45c1db2015-07-23 14:11:29 +0100416 // TODO 5 or more arguments not supported for viper call
Damien George2ac4af62014-08-15 16:45:41 +0100417 assert(0);
418 ret = 0;
419 }
420
Damien Georgee6c0dff2014-08-15 23:47:59 +0100421 return mp_convert_native_to_obj(ret, self->type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100422}
423
424STATIC const mp_obj_type_t mp_type_fun_viper = {
425 { &mp_type_type },
426 .name = MP_QSTR_function,
427 .call = fun_viper_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000428 .unary_op = mp_generic_unary_op,
Damien George2ac4af62014-08-15 16:45:41 +0100429};
430
Damien George3c658a42014-08-24 16:28:17 +0100431mp_obj_t mp_obj_new_fun_viper(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig) {
Damien George2ac4af62014-08-15 16:45:41 +0100432 mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
433 o->base.type = &mp_type_fun_viper;
434 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100435 o->fun_data = fun_data;
Damien George2ac4af62014-08-15 16:45:41 +0100436 o->type_sig = type_sig;
437 return o;
438}
439
440#endif // MICROPY_EMIT_NATIVE
441
442/******************************************************************************/
Damiend99b0522013-12-21 18:17:45 +0000443/* inline assembler functions */
444
Damien George2ac4af62014-08-15 16:45:41 +0100445#if MICROPY_EMIT_INLINE_THUMB
446
Damiend99b0522013-12-21 18:17:45 +0000447typedef struct _mp_obj_fun_asm_t {
448 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100449 mp_uint_t n_args;
450 void *fun_data; // GC must be able to trace this pointer
Damiend99b0522013-12-21 18:17:45 +0000451} mp_obj_fun_asm_t;
452
Damien Georgebbf5cd02015-01-12 22:45:35 +0000453typedef mp_uint_t (*inline_asm_fun_0_t)(void);
Damien George40f3c022014-07-03 13:25:24 +0100454typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
455typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
456typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damiend99b0522013-12-21 18:17:45 +0000457
458// convert a Micro Python object to a sensible value for inline asm
Damien George40f3c022014-07-03 13:25:24 +0100459STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000460 // TODO for byte_array, pass pointer to the array
461 if (MP_OBJ_IS_SMALL_INT(obj)) {
462 return MP_OBJ_SMALL_INT_VALUE(obj);
463 } else if (obj == mp_const_none) {
464 return 0;
465 } else if (obj == mp_const_false) {
466 return 0;
467 } else if (obj == mp_const_true) {
468 return 1;
Damien George32f0b792015-02-13 10:43:05 +0000469 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
470 return mp_obj_int_get_truncated(obj);
Damien George5fa93b62014-01-22 14:35:10 +0000471 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000472 // pointer to the string (it's probably constant though!)
Damien Georgea7329612014-09-29 16:26:39 +0100473 mp_uint_t l;
Damien George40f3c022014-07-03 13:25:24 +0100474 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000475 } else {
Damien George87210872014-04-13 00:30:32 +0100476 mp_obj_type_t *type = mp_obj_get_type(obj);
477 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100478#if MICROPY_PY_BUILTINS_FLOAT
Damien George87210872014-04-13 00:30:32 +0100479 } else if (type == &mp_type_float) {
480 // convert float to int (could also pass in float registers)
Damien George40f3c022014-07-03 13:25:24 +0100481 return (mp_int_t)mp_obj_float_get(obj);
Damien George87210872014-04-13 00:30:32 +0100482#endif
483 } else if (type == &mp_type_tuple) {
484 // pointer to start of tuple (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100485 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100486 mp_obj_t *items;
487 mp_obj_tuple_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100488 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100489 } else if (type == &mp_type_list) {
490 // pointer to start of list (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100491 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100492 mp_obj_t *items;
493 mp_obj_list_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100494 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100495 } else {
Damien George57a4b4f2014-04-18 22:29:21 +0100496 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100497 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
Damien George8a1cab92014-04-13 12:08:52 +0100498 // supports the buffer protocol, return a pointer to the data
Damien George40f3c022014-07-03 13:25:24 +0100499 return (mp_uint_t)bufinfo.buf;
Damien George8a1cab92014-04-13 12:08:52 +0100500 } else {
501 // just pass along a pointer to the object
Damien George40f3c022014-07-03 13:25:24 +0100502 return (mp_uint_t)obj;
Damien George8a1cab92014-04-13 12:08:52 +0100503 }
Damien George87210872014-04-13 00:30:32 +0100504 }
Damiend99b0522013-12-21 18:17:45 +0000505 }
506}
507
508// convert a return value from inline asm to a sensible Micro Python object
Damien George40f3c022014-07-03 13:25:24 +0100509STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000510 return MP_OBJ_NEW_SMALL_INT(val);
511}
512
Damien Georgeecc88e92014-08-30 00:35:11 +0100513STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damiend99b0522013-12-21 18:17:45 +0000514 mp_obj_fun_asm_t *self = self_in;
515
Damien Georgeccc85ea2014-05-10 13:40:46 +0100516 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
Damiend99b0522013-12-21 18:17:45 +0000517
Damien George3c658a42014-08-24 16:28:17 +0100518 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
519
Damien George40f3c022014-07-03 13:25:24 +0100520 mp_uint_t ret;
Damiend99b0522013-12-21 18:17:45 +0000521 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100522 ret = ((inline_asm_fun_0_t)fun)();
Damiend99b0522013-12-21 18:17:45 +0000523 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100524 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000525 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100526 ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100528 ret = ((inline_asm_fun_3_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]), convert_obj_for_inline_asm(args[2]));
Damiend99b0522013-12-21 18:17:45 +0000529 } else {
530 assert(0);
531 ret = 0;
532 }
533
534 return convert_val_from_inline_asm(ret);
535}
536
Damien George3e1a5c12014-03-29 13:43:38 +0000537STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000538 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000539 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000540 .call = fun_asm_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000541 .unary_op = mp_generic_unary_op,
Damiend99b0522013-12-21 18:17:45 +0000542};
543
Damien George3c658a42014-08-24 16:28:17 +0100544mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) {
Damiend99b0522013-12-21 18:17:45 +0000545 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000546 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000547 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100548 o->fun_data = fun_data;
Damiend99b0522013-12-21 18:17:45 +0000549 return o;
550}
Damien George2ac4af62014-08-15 16:45:41 +0100551
552#endif // MICROPY_EMIT_INLINE_THUMB