blob: eb89a78a0779a4a3b9efc0cdcf4e45b47b364dee [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
xbeefe34222014-03-16 00:14:26 -070028#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +000029#include <string.h>
30#include <assert.h>
31
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000033#include "nlr.h"
34#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000036#include "obj.h"
Paul Sokolovsky90750022014-02-01 15:05:04 +020037#include "objtuple.h"
Paul Sokolovskyf26a3072014-04-17 05:44:51 +030038#include "objfun.h"
Damien George2e482cd2014-02-16 00:01:29 +000039#include "runtime0.h"
Damiend99b0522013-12-21 18:17:45 +000040#include "runtime.h"
41#include "bc.h"
Paul Sokolovsky23668692014-06-25 03:03:34 +030042#include "stackctrl.h"
Damiend99b0522013-12-21 18:17:45 +000043
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020044#if 0 // print debugging info
45#define DEBUG_PRINT (1)
46#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000047#define DEBUG_printf(...) (void)0
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +020048#endif
49
Damien George3c658a42014-08-24 16:28:17 +010050// This binary_op method is used for all function types, and is also
51// used to determine if an object is of generic function type.
Damien Georgeecc88e92014-08-30 00:35:11 +010052mp_obj_t mp_obj_fun_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Paul Sokolovsky586bfce2014-04-05 13:50:06 +030053 switch (op) {
54 case MP_BINARY_OP_EQUAL:
55 // These objects can be equal only if it's the same underlying structure,
56 // we don't even need to check for 2nd arg type.
57 return MP_BOOL(lhs_in == rhs_in);
58 }
Damien George6ac5dce2014-05-21 19:42:43 +010059 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky586bfce2014-04-05 13:50:06 +030060}
61
Damien George3c658a42014-08-24 16:28:17 +010062/******************************************************************************/
63/* builtin functions */
64
65// mp_obj_fun_builtin_t defined in obj.h
66
Damien Georgeecc88e92014-08-30 00:35:11 +010067STATIC 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 +010068 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin));
69 mp_obj_fun_builtin_t *self = self_in;
Damien George20006db2014-01-18 14:10:48 +000070
John R. Lenton88cb1e62014-01-13 19:55:18 +000071 // check number of arguments
Damien Georgea3f94e02014-04-20 00:13:22 +010072 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 +000073
John R. Lentonc06763a2014-01-07 17:29:16 +000074 if (self->is_kw) {
Damien George20006db2014-01-18 14:10:48 +000075 // function allows keywords
76
Damien George0a587b82014-02-08 18:53:41 +000077 // we create a map directly from the given args array
78 mp_map_t kw_args;
79 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
Damien George20006db2014-01-18 14:10:48 +000080
Damien George0a587b82014-02-08 18:53:41 +000081 return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
Damien George20006db2014-01-18 14:10:48 +000082
Paul Sokolovsky9d95a2b2014-01-26 01:58:51 +020083 } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
Damiend99b0522013-12-21 18:17:45 +000084 // function requires a fixed number of arguments
85
Damiend99b0522013-12-21 18:17:45 +000086 // dispatch function call
87 switch (self->n_args_min) {
88 case 0:
89 return ((mp_fun_0_t)self->fun)();
90
91 case 1:
92 return ((mp_fun_1_t)self->fun)(args[0]);
93
94 case 2:
Damien George20006db2014-01-18 14:10:48 +000095 return ((mp_fun_2_t)self->fun)(args[0], args[1]);
Damiend99b0522013-12-21 18:17:45 +000096
John R. Lenton45a87442014-01-04 01:15:01 +000097 case 3:
Damien George20006db2014-01-18 14:10:48 +000098 return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
John R. Lenton45a87442014-01-04 01:15:01 +000099
Damiend99b0522013-12-21 18:17:45 +0000100 default:
101 assert(0);
102 return mp_const_none;
103 }
104
105 } else {
Damien George20006db2014-01-18 14:10:48 +0000106 // function takes a variable number of arguments, but no keywords
Damiend99b0522013-12-21 18:17:45 +0000107
Damien George20006db2014-01-18 14:10:48 +0000108 return ((mp_fun_var_t)self->fun)(n_args, args);
Damiend99b0522013-12-21 18:17:45 +0000109 }
110}
111
Damien George3c658a42014-08-24 16:28:17 +0100112const mp_obj_type_t mp_type_fun_builtin = {
Damien Georgec5966122014-02-15 16:10:44 +0000113 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000114 .name = MP_QSTR_function,
Damien George3c658a42014-08-24 16:28:17 +0100115 .call = fun_builtin_call,
116 .binary_op = mp_obj_fun_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000117};
118
Damien George3c658a42014-08-24 16:28:17 +0100119#if 0 // currently unused, and semi-obsolete
Damien Georged17926d2014-03-30 13:35:08 +0100120mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun) {
Damien George3c658a42014-08-24 16:28:17 +0100121 mp_obj_fun_builtin_t *o = m_new_obj(mp_obj_fun_builtin_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000122 o->base.type = &mp_type_fun_native;
Dave Hylands44332ec2014-01-13 08:42:43 -0800123 o->is_kw = false;
Damiend99b0522013-12-21 18:17:45 +0000124 o->n_args_min = n_args_min;
Damien Georged5e81822014-02-26 17:47:05 +0000125 o->n_args_max = MP_OBJ_FUN_ARGS_MAX;
Damiend99b0522013-12-21 18:17:45 +0000126 o->fun = fun;
127 return o;
128}
129
130// min and max are inclusive
Damien Georged17926d2014-03-30 13:35:08 +0100131mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
Damien George3c658a42014-08-24 16:28:17 +0100132 mp_obj_fun_builtin_t *o = m_new_obj(mp_obj_fun_builtin_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000133 o->base.type = &mp_type_fun_native;
Dave Hylands44332ec2014-01-13 08:42:43 -0800134 o->is_kw = false;
Damiend99b0522013-12-21 18:17:45 +0000135 o->n_args_min = n_args_min;
136 o->n_args_max = n_args_max;
137 o->fun = fun;
138 return o;
139}
Damien George3c658a42014-08-24 16:28:17 +0100140#endif
Damiend99b0522013-12-21 18:17:45 +0000141
142/******************************************************************************/
143/* byte code functions */
144
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300145const char *mp_obj_code_get_name(const byte *code_info) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100146 mp_decode_uint(&code_info); // skip code_info_size entry
147 return qstr_str(mp_decode_uint(&code_info));
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300148}
149
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300150const char *mp_obj_fun_get_name(mp_const_obj_t fun_in) {
151 const mp_obj_fun_bc_t *fun = fun_in;
Paul Sokolovsky418aca92014-05-03 14:10:34 +0300152 const byte *code_info = fun->bytecode;
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300153 return mp_obj_code_get_name(code_info);
154}
155
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300156#if MICROPY_CPYTHON_COMPAT
157STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
158 mp_obj_fun_bc_t *o = o_in;
159 print(env, "<function %s at 0x%x>", mp_obj_fun_get_name(o), o);
160}
161#endif
162
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200163#if DEBUG_PRINT
Damien Georgef78b6df2014-03-31 15:59:25 +0100164STATIC void dump_args(const mp_obj_t *a, int sz) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200165 DEBUG_printf("%p: ", a);
166 for (int i = 0; i < sz; i++) {
167 DEBUG_printf("%p ", a[i]);
168 }
169 DEBUG_printf("\n");
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200170}
Damien Georgef78b6df2014-03-31 15:59:25 +0100171#else
172#define dump_args(...) (void)0
173#endif
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200174
Damien Georgeaabd83e2014-06-07 14:16:08 +0100175// With this macro you can tune the maximum number of function state bytes
176// that will be allocated on the stack. Any function that needs more
177// than this will use the heap.
Damien George40f3c022014-07-03 13:25:24 +0100178#define VM_MAX_STATE_ON_STACK (10 * sizeof(mp_uint_t))
Damien Georgeaabd83e2014-06-07 14:16:08 +0100179
180// Set this to enable a simple stack overflow check.
181#define VM_DETECT_STACK_OVERFLOW (0)
182
Damien Georgeecc88e92014-08-30 00:35:11 +0100183STATIC 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 +0300184 MP_STACK_CHECK();
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300185
Damien Georgeeaaebf32014-09-23 10:59:05 +0100186 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300187 DEBUG_printf("Input pos args: ");
188 dump_args(args, n_args);
189 DEBUG_printf("Input kw args: ");
190 dump_args(args + n_args, n_kw * 2);
191 mp_obj_fun_bc_t *self = self_in;
192 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
193
Damien Georgeb534e1b2014-09-04 14:44:01 +0100194 // skip code-info block
195 const byte *code_info = self->bytecode;
196 mp_uint_t code_info_size = mp_decode_uint(&code_info);
197 const byte *ip = self->bytecode + code_info_size;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300198
199 // bytecode prelude: state size and exception stack size; 16 bit uints
Damien Georgeb534e1b2014-09-04 14:44:01 +0100200 mp_uint_t n_state = mp_decode_uint(&ip);
201 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300202
203#if VM_DETECT_STACK_OVERFLOW
204 n_state += 1;
205#endif
206
207 // allocate state for locals and stack
Damien George3c658a42014-08-24 16:28:17 +0100208 mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300209 mp_code_state *code_state;
210 if (state_size > VM_MAX_STATE_ON_STACK) {
211 code_state = m_new_obj_var(mp_code_state, byte, state_size);
212 } else {
213 code_state = alloca(sizeof(mp_code_state) + state_size);
214 }
215
216 code_state->n_state = n_state;
217 code_state->ip = ip;
218 mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
Damien George049a7a82014-06-08 00:37:40 +0100219
220 // execute the byte code with the correct globals context
221 mp_obj_dict_t *old_globals = mp_globals_get();
222 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100223 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
Damien George049a7a82014-06-08 00:37:40 +0100224 mp_globals_set(old_globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100225
226#if VM_DETECT_STACK_OVERFLOW
227 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
228 if (code_state->sp < code_state->state) {
229 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
230 assert(0);
231 }
232 }
233 // We can't check the case when an exception is returned in state[n_state - 1]
234 // and there are no arguments, because in this case our detection slot may have
235 // been overwritten by the returned exception (which is allowed).
Damien George049a7a82014-06-08 00:37:40 +0100236 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100237 // Just check to see that we have at least 1 null object left in the state.
238 bool overflow = true;
Damien George3c658a42014-08-24 16:28:17 +0100239 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 +0100240 if (code_state->state[i] == MP_OBJ_NULL) {
241 overflow = false;
242 break;
243 }
244 }
245 if (overflow) {
246 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
247 assert(0);
248 }
249 }
250#endif
251
Damien George049a7a82014-06-08 00:37:40 +0100252 mp_obj_t result;
Damien Georgeaabd83e2014-06-07 14:16:08 +0100253 switch (vm_return_kind) {
254 case MP_VM_RETURN_NORMAL:
255 // return value is in *sp
256 result = *code_state->sp;
257 break;
258
259 case MP_VM_RETURN_EXCEPTION:
260 // return value is in state[n_state - 1]
261 result = code_state->state[n_state - 1];
262 break;
263
264 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
265 default:
266 assert(0);
267 result = mp_const_none;
268 vm_return_kind = MP_VM_RETURN_NORMAL;
269 break;
270 }
271
272 // free the state if it was allocated on the heap
273 if (state_size > VM_MAX_STATE_ON_STACK) {
274 m_del_var(mp_code_state, byte, state_size, code_state);
275 }
276
Damien Georgec8f78bc2014-02-15 22:55:00 +0000277 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
278 return result;
279 } else { // MP_VM_RETURN_EXCEPTION
Damien Georgeea13f402014-04-05 18:32:08 +0100280 nlr_raise(result);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000281 }
Damiend99b0522013-12-21 18:17:45 +0000282}
283
Damien George3e1a5c12014-03-29 13:43:38 +0000284const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000285 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000286 .name = MP_QSTR_function,
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300287#if MICROPY_CPYTHON_COMPAT
288 .print = fun_bc_print,
289#endif
Damien George20006db2014-01-18 14:10:48 +0000290 .call = fun_bc_call,
Damien George3c658a42014-08-24 16:28:17 +0100291 .binary_op = mp_obj_fun_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000292};
293
Damien George3c658a42014-08-24 16:28:17 +0100294mp_obj_t mp_obj_new_fun_bc(mp_uint_t scope_flags, qstr *args, mp_uint_t n_pos_args, mp_uint_t n_kwonly_args, mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code) {
295 mp_uint_t n_def_args = 0;
296 mp_uint_t n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200297 mp_obj_tuple_t *def_args = def_args_in;
298 if (def_args != MP_OBJ_NULL) {
Damien George69b89d22014-04-11 13:38:30 +0000299 assert(MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200300 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000301 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200302 }
Damien Georgef0778a72014-06-07 22:01:00 +0100303 if (def_kw_args != MP_OBJ_NULL) {
304 n_extra_args += 1;
305 }
Damien George2e482cd2014-02-16 00:01:29 +0000306 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 +0000307 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100308 o->globals = mp_globals_get();
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200309 o->args = args;
Damien George2827d622014-04-27 15:50:52 +0100310 o->n_pos_args = n_pos_args;
311 o->n_kwonly_args = n_kwonly_args;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200312 o->n_def_args = n_def_args;
Damien Georgef0778a72014-06-07 22:01:00 +0100313 o->has_def_kw_args = def_kw_args != MP_OBJ_NULL;
Damien George2e482cd2014-02-16 00:01:29 +0000314 o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
315 o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
Damien George66028ab2014-01-03 14:03:48 +0000316 o->bytecode = code;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200317 if (def_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100318 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Damien Georgef0778a72014-06-07 22:01:00 +0100319 }
320 if (def_kw_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100321 o->extra_args[n_def_args] = def_kw_args;
Damien George2827d622014-04-27 15:50:52 +0100322 }
Damiend99b0522013-12-21 18:17:45 +0000323 return o;
324}
325
Damiend99b0522013-12-21 18:17:45 +0000326/******************************************************************************/
Damien George3c658a42014-08-24 16:28:17 +0100327/* native functions */
328
329#if MICROPY_EMIT_NATIVE
330
331typedef struct _mp_obj_fun_native_t {
332 mp_obj_base_t base;
333 mp_uint_t n_args;
334 void *fun_data; // GC must be able to trace this pointer
335 // TODO add mp_map_t *globals
336} mp_obj_fun_native_t;
337
338typedef mp_obj_t (*native_fun_0_t)();
339typedef mp_obj_t (*native_fun_1_t)(mp_obj_t);
340typedef mp_obj_t (*native_fun_2_t)(mp_obj_t, mp_obj_t);
341typedef mp_obj_t (*native_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
342
Damien Georgeecc88e92014-08-30 00:35:11 +0100343STATIC 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 George3c658a42014-08-24 16:28:17 +0100344 mp_obj_fun_native_t *self = self_in;
345
346 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
347
348 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
349
350 switch (n_args) {
351 case 0:
352 return ((native_fun_0_t)fun)();
353
354 case 1:
355 return ((native_fun_1_t)fun)(args[0]);
356
357 case 2:
358 return ((native_fun_2_t)fun)(args[0], args[1]);
359
360 case 3:
361 return ((native_fun_3_t)fun)(args[0], args[1], args[2]);
362
363 default:
364 assert(0);
365 return mp_const_none;
366 }
367}
368
369STATIC const mp_obj_type_t mp_type_fun_native = {
370 { &mp_type_type },
371 .name = MP_QSTR_function,
372 .call = fun_native_call,
373 .binary_op = mp_obj_fun_binary_op,
374};
375
376mp_obj_t mp_obj_new_fun_native(mp_uint_t n_args, void *fun_data) {
377 assert(0 <= n_args && n_args <= 3);
378 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
379 o->base.type = &mp_type_fun_native;
380 o->n_args = n_args;
381 o->fun_data = fun_data;
382 return o;
383}
384
385#endif // MICROPY_EMIT_NATIVE
386
387/******************************************************************************/
Damien George2ac4af62014-08-15 16:45:41 +0100388/* viper functions */
389
390#if MICROPY_EMIT_NATIVE
391
392typedef struct _mp_obj_fun_viper_t {
393 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100394 mp_uint_t n_args;
395 void *fun_data; // GC must be able to trace this pointer
Damien George2ac4af62014-08-15 16:45:41 +0100396 mp_uint_t type_sig;
397} mp_obj_fun_viper_t;
398
399typedef mp_uint_t (*viper_fun_0_t)();
400typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
401typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
402typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
403
Damien Georgeecc88e92014-08-30 00:35:11 +0100404STATIC 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 +0100405 mp_obj_fun_viper_t *self = self_in;
406
407 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
408
Damien George3c658a42014-08-24 16:28:17 +0100409 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
410
Damien George2ac4af62014-08-15 16:45:41 +0100411 mp_uint_t ret;
412 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100413 ret = ((viper_fun_0_t)fun)();
Damien George2ac4af62014-08-15 16:45:41 +0100414 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100415 ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2));
Damien George2ac4af62014-08-15 16:45:41 +0100416 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100417 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 +0100418 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100419 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 George2ac4af62014-08-15 16:45:41 +0100420 } else {
421 assert(0);
422 ret = 0;
423 }
424
Damien Georgee6c0dff2014-08-15 23:47:59 +0100425 return mp_convert_native_to_obj(ret, self->type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100426}
427
428STATIC const mp_obj_type_t mp_type_fun_viper = {
429 { &mp_type_type },
430 .name = MP_QSTR_function,
431 .call = fun_viper_call,
Damien George3c658a42014-08-24 16:28:17 +0100432 .binary_op = mp_obj_fun_binary_op,
Damien George2ac4af62014-08-15 16:45:41 +0100433};
434
Damien George3c658a42014-08-24 16:28:17 +0100435mp_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 +0100436 mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
437 o->base.type = &mp_type_fun_viper;
438 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100439 o->fun_data = fun_data;
Damien George2ac4af62014-08-15 16:45:41 +0100440 o->type_sig = type_sig;
441 return o;
442}
443
444#endif // MICROPY_EMIT_NATIVE
445
446/******************************************************************************/
Damiend99b0522013-12-21 18:17:45 +0000447/* inline assembler functions */
448
Damien George2ac4af62014-08-15 16:45:41 +0100449#if MICROPY_EMIT_INLINE_THUMB
450
Damiend99b0522013-12-21 18:17:45 +0000451typedef struct _mp_obj_fun_asm_t {
452 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100453 mp_uint_t n_args;
454 void *fun_data; // GC must be able to trace this pointer
Damiend99b0522013-12-21 18:17:45 +0000455} mp_obj_fun_asm_t;
456
Damien George40f3c022014-07-03 13:25:24 +0100457typedef mp_uint_t (*inline_asm_fun_0_t)();
458typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
459typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
460typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damiend99b0522013-12-21 18:17:45 +0000461
462// convert a Micro Python object to a sensible value for inline asm
Damien George40f3c022014-07-03 13:25:24 +0100463STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000464 // TODO for byte_array, pass pointer to the array
465 if (MP_OBJ_IS_SMALL_INT(obj)) {
466 return MP_OBJ_SMALL_INT_VALUE(obj);
467 } else if (obj == mp_const_none) {
468 return 0;
469 } else if (obj == mp_const_false) {
470 return 0;
471 } else if (obj == mp_const_true) {
472 return 1;
Damien George5fa93b62014-01-22 14:35:10 +0000473 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000474 // pointer to the string (it's probably constant though!)
Damien George5fa93b62014-01-22 14:35:10 +0000475 uint l;
Damien George40f3c022014-07-03 13:25:24 +0100476 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000477 } else {
Damien George87210872014-04-13 00:30:32 +0100478 mp_obj_type_t *type = mp_obj_get_type(obj);
479 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100480#if MICROPY_PY_BUILTINS_FLOAT
Damien George87210872014-04-13 00:30:32 +0100481 } else if (type == &mp_type_float) {
482 // convert float to int (could also pass in float registers)
Damien George40f3c022014-07-03 13:25:24 +0100483 return (mp_int_t)mp_obj_float_get(obj);
Damien George87210872014-04-13 00:30:32 +0100484#endif
485 } else if (type == &mp_type_tuple) {
486 // pointer to start of tuple (could pass length, but then could use len(x) for that)
487 uint len;
488 mp_obj_t *items;
489 mp_obj_tuple_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100490 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100491 } else if (type == &mp_type_list) {
492 // pointer to start of list (could pass length, but then could use len(x) for that)
493 uint len;
494 mp_obj_t *items;
495 mp_obj_list_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100496 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100497 } else {
Damien George57a4b4f2014-04-18 22:29:21 +0100498 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100499 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
Damien George8a1cab92014-04-13 12:08:52 +0100500 // supports the buffer protocol, return a pointer to the data
Damien George40f3c022014-07-03 13:25:24 +0100501 return (mp_uint_t)bufinfo.buf;
Damien George8a1cab92014-04-13 12:08:52 +0100502 } else {
503 // just pass along a pointer to the object
Damien George40f3c022014-07-03 13:25:24 +0100504 return (mp_uint_t)obj;
Damien George8a1cab92014-04-13 12:08:52 +0100505 }
Damien George87210872014-04-13 00:30:32 +0100506 }
Damiend99b0522013-12-21 18:17:45 +0000507 }
508}
509
510// convert a return value from inline asm to a sensible Micro Python object
Damien George40f3c022014-07-03 13:25:24 +0100511STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000512 return MP_OBJ_NEW_SMALL_INT(val);
513}
514
Damien Georgeecc88e92014-08-30 00:35:11 +0100515STATIC 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 +0000516 mp_obj_fun_asm_t *self = self_in;
517
Damien Georgeccc85ea2014-05-10 13:40:46 +0100518 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
Damiend99b0522013-12-21 18:17:45 +0000519
Damien George3c658a42014-08-24 16:28:17 +0100520 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
521
Damien George40f3c022014-07-03 13:25:24 +0100522 mp_uint_t ret;
Damiend99b0522013-12-21 18:17:45 +0000523 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100524 ret = ((inline_asm_fun_0_t)fun)();
Damiend99b0522013-12-21 18:17:45 +0000525 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100526 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100528 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 +0000529 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100530 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 +0000531 } else {
532 assert(0);
533 ret = 0;
534 }
535
536 return convert_val_from_inline_asm(ret);
537}
538
Damien George3e1a5c12014-03-29 13:43:38 +0000539STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000540 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000541 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000542 .call = fun_asm_call,
Damien George3c658a42014-08-24 16:28:17 +0100543 .binary_op = mp_obj_fun_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000544};
545
Damien George3c658a42014-08-24 16:28:17 +0100546mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) {
Damiend99b0522013-12-21 18:17:45 +0000547 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000548 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000549 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100550 o->fun_data = fun_data;
Damiend99b0522013-12-21 18:17:45 +0000551 return o;
552}
Damien George2ac4af62014-08-15 16:45:41 +0100553
554#endif // MICROPY_EMIT_INLINE_THUMB