blob: b86d6f8dc6390b14a44b0e8060252fa7956c3c56 [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 George3c658a42014-08-24 16:28:17 +010046// This binary_op method is used for all function types, and is also
47// used to determine if an object is of generic function type.
Damien Georgeecc88e92014-08-30 00:35:11 +010048mp_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 +030049 switch (op) {
50 case MP_BINARY_OP_EQUAL:
51 // These objects can be equal only if it's the same underlying structure,
52 // we don't even need to check for 2nd arg type.
53 return MP_BOOL(lhs_in == rhs_in);
54 }
Damien George6ac5dce2014-05-21 19:42:43 +010055 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky586bfce2014-04-05 13:50:06 +030056}
57
Damien George3c658a42014-08-24 16:28:17 +010058/******************************************************************************/
59/* builtin functions */
60
61// mp_obj_fun_builtin_t defined in obj.h
62
Damien Georgeecc88e92014-08-30 00:35:11 +010063STATIC 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 +010064 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin));
65 mp_obj_fun_builtin_t *self = self_in;
Damien George20006db2014-01-18 14:10:48 +000066
John R. Lenton88cb1e62014-01-13 19:55:18 +000067 // check number of arguments
Damien Georgea3f94e02014-04-20 00:13:22 +010068 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 +000069
John R. Lentonc06763a2014-01-07 17:29:16 +000070 if (self->is_kw) {
Damien George20006db2014-01-18 14:10:48 +000071 // function allows keywords
72
Damien George0a587b82014-02-08 18:53:41 +000073 // we create a map directly from the given args array
74 mp_map_t kw_args;
75 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
Damien George20006db2014-01-18 14:10:48 +000076
Damien George0a587b82014-02-08 18:53:41 +000077 return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
Damien George20006db2014-01-18 14:10:48 +000078
Paul Sokolovsky9d95a2b2014-01-26 01:58:51 +020079 } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
Damiend99b0522013-12-21 18:17:45 +000080 // function requires a fixed number of arguments
81
Damiend99b0522013-12-21 18:17:45 +000082 // dispatch function call
83 switch (self->n_args_min) {
84 case 0:
85 return ((mp_fun_0_t)self->fun)();
86
87 case 1:
88 return ((mp_fun_1_t)self->fun)(args[0]);
89
90 case 2:
Damien George20006db2014-01-18 14:10:48 +000091 return ((mp_fun_2_t)self->fun)(args[0], args[1]);
Damiend99b0522013-12-21 18:17:45 +000092
John R. Lenton45a87442014-01-04 01:15:01 +000093 case 3:
Damien George20006db2014-01-18 14:10:48 +000094 return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
John R. Lenton45a87442014-01-04 01:15:01 +000095
Damiend99b0522013-12-21 18:17:45 +000096 default:
97 assert(0);
98 return mp_const_none;
99 }
100
101 } else {
Damien George20006db2014-01-18 14:10:48 +0000102 // function takes a variable number of arguments, but no keywords
Damiend99b0522013-12-21 18:17:45 +0000103
Damien George20006db2014-01-18 14:10:48 +0000104 return ((mp_fun_var_t)self->fun)(n_args, args);
Damiend99b0522013-12-21 18:17:45 +0000105 }
106}
107
Damien George3c658a42014-08-24 16:28:17 +0100108const mp_obj_type_t mp_type_fun_builtin = {
Damien Georgec5966122014-02-15 16:10:44 +0000109 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000110 .name = MP_QSTR_function,
Damien George3c658a42014-08-24 16:28:17 +0100111 .call = fun_builtin_call,
112 .binary_op = mp_obj_fun_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000113};
114
Damiend99b0522013-12-21 18:17:45 +0000115/******************************************************************************/
116/* byte code functions */
117
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300118const char *mp_obj_code_get_name(const byte *code_info) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100119 mp_decode_uint(&code_info); // skip code_info_size entry
120 return qstr_str(mp_decode_uint(&code_info));
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300121}
122
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300123const char *mp_obj_fun_get_name(mp_const_obj_t fun_in) {
124 const mp_obj_fun_bc_t *fun = fun_in;
Paul Sokolovsky418aca92014-05-03 14:10:34 +0300125 const byte *code_info = fun->bytecode;
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300126 return mp_obj_code_get_name(code_info);
127}
128
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300129#if MICROPY_CPYTHON_COMPAT
130STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
131 mp_obj_fun_bc_t *o = o_in;
132 print(env, "<function %s at 0x%x>", mp_obj_fun_get_name(o), o);
133}
134#endif
135
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200136#if DEBUG_PRINT
Damien George39dc1452014-10-03 19:52:22 +0100137STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200138 DEBUG_printf("%p: ", a);
Damien George39dc1452014-10-03 19:52:22 +0100139 for (mp_uint_t i = 0; i < sz; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200140 DEBUG_printf("%p ", a[i]);
141 }
142 DEBUG_printf("\n");
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200143}
Damien Georgef78b6df2014-03-31 15:59:25 +0100144#else
145#define dump_args(...) (void)0
146#endif
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200147
Damien Georgeaabd83e2014-06-07 14:16:08 +0100148// With this macro you can tune the maximum number of function state bytes
149// that will be allocated on the stack. Any function that needs more
150// than this will use the heap.
Damien George40f3c022014-07-03 13:25:24 +0100151#define VM_MAX_STATE_ON_STACK (10 * sizeof(mp_uint_t))
Damien Georgeaabd83e2014-06-07 14:16:08 +0100152
153// Set this to enable a simple stack overflow check.
154#define VM_DETECT_STACK_OVERFLOW (0)
155
Damien Georgeecc88e92014-08-30 00:35:11 +0100156STATIC 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 +0300157 MP_STACK_CHECK();
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300158
Damien Georgeeaaebf32014-09-23 10:59:05 +0100159 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300160 DEBUG_printf("Input pos args: ");
161 dump_args(args, n_args);
162 DEBUG_printf("Input kw args: ");
163 dump_args(args + n_args, n_kw * 2);
164 mp_obj_fun_bc_t *self = self_in;
165 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
166
Damien Georgeb534e1b2014-09-04 14:44:01 +0100167 // skip code-info block
168 const byte *code_info = self->bytecode;
169 mp_uint_t code_info_size = mp_decode_uint(&code_info);
170 const byte *ip = self->bytecode + code_info_size;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300171
Damien George1084b0f2014-10-25 15:07:02 +0100172 // bytecode prelude: skip arg names
173 ip += (self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t);
174
175 // bytecode prelude: state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +0100176 mp_uint_t n_state = mp_decode_uint(&ip);
177 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300178
179#if VM_DETECT_STACK_OVERFLOW
180 n_state += 1;
181#endif
182
183 // allocate state for locals and stack
Damien George3c658a42014-08-24 16:28:17 +0100184 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 +0300185 mp_code_state *code_state;
186 if (state_size > VM_MAX_STATE_ON_STACK) {
187 code_state = m_new_obj_var(mp_code_state, byte, state_size);
188 } else {
189 code_state = alloca(sizeof(mp_code_state) + state_size);
190 }
191
192 code_state->n_state = n_state;
193 code_state->ip = ip;
194 mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
Damien George049a7a82014-06-08 00:37:40 +0100195
196 // execute the byte code with the correct globals context
197 mp_obj_dict_t *old_globals = mp_globals_get();
198 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100199 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
Damien George049a7a82014-06-08 00:37:40 +0100200 mp_globals_set(old_globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100201
202#if VM_DETECT_STACK_OVERFLOW
203 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
204 if (code_state->sp < code_state->state) {
205 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
206 assert(0);
207 }
208 }
209 // We can't check the case when an exception is returned in state[n_state - 1]
210 // and there are no arguments, because in this case our detection slot may have
211 // been overwritten by the returned exception (which is allowed).
Damien George049a7a82014-06-08 00:37:40 +0100212 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100213 // Just check to see that we have at least 1 null object left in the state.
214 bool overflow = true;
Damien George3c658a42014-08-24 16:28:17 +0100215 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 +0100216 if (code_state->state[i] == MP_OBJ_NULL) {
217 overflow = false;
218 break;
219 }
220 }
221 if (overflow) {
222 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
223 assert(0);
224 }
225 }
226#endif
227
Damien George049a7a82014-06-08 00:37:40 +0100228 mp_obj_t result;
Damien Georgeaabd83e2014-06-07 14:16:08 +0100229 switch (vm_return_kind) {
230 case MP_VM_RETURN_NORMAL:
231 // return value is in *sp
232 result = *code_state->sp;
233 break;
234
235 case MP_VM_RETURN_EXCEPTION:
236 // return value is in state[n_state - 1]
237 result = code_state->state[n_state - 1];
238 break;
239
240 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
241 default:
242 assert(0);
243 result = mp_const_none;
244 vm_return_kind = MP_VM_RETURN_NORMAL;
245 break;
246 }
247
248 // free the state if it was allocated on the heap
249 if (state_size > VM_MAX_STATE_ON_STACK) {
250 m_del_var(mp_code_state, byte, state_size, code_state);
251 }
252
Damien Georgec8f78bc2014-02-15 22:55:00 +0000253 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
254 return result;
255 } else { // MP_VM_RETURN_EXCEPTION
Damien Georgeea13f402014-04-05 18:32:08 +0100256 nlr_raise(result);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000257 }
Damiend99b0522013-12-21 18:17:45 +0000258}
259
Damien George3e1a5c12014-03-29 13:43:38 +0000260const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000261 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000262 .name = MP_QSTR_function,
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300263#if MICROPY_CPYTHON_COMPAT
264 .print = fun_bc_print,
265#endif
Damien George20006db2014-01-18 14:10:48 +0000266 .call = fun_bc_call,
Damien George3c658a42014-08-24 16:28:17 +0100267 .binary_op = mp_obj_fun_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000268};
269
Damien George1084b0f2014-10-25 15:07:02 +0100270mp_obj_t mp_obj_new_fun_bc(mp_uint_t scope_flags, 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) {
Damien George3c658a42014-08-24 16:28:17 +0100271 mp_uint_t n_def_args = 0;
272 mp_uint_t n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200273 mp_obj_tuple_t *def_args = def_args_in;
274 if (def_args != MP_OBJ_NULL) {
Damien George69b89d22014-04-11 13:38:30 +0000275 assert(MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200276 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000277 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200278 }
Damien Georgef0778a72014-06-07 22:01:00 +0100279 if (def_kw_args != MP_OBJ_NULL) {
280 n_extra_args += 1;
281 }
Damien George2e482cd2014-02-16 00:01:29 +0000282 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 +0000283 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100284 o->globals = mp_globals_get();
Damien George2827d622014-04-27 15:50:52 +0100285 o->n_pos_args = n_pos_args;
286 o->n_kwonly_args = n_kwonly_args;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200287 o->n_def_args = n_def_args;
Damien Georgef0778a72014-06-07 22:01:00 +0100288 o->has_def_kw_args = def_kw_args != MP_OBJ_NULL;
Damien George2e482cd2014-02-16 00:01:29 +0000289 o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
290 o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
Damien George66028ab2014-01-03 14:03:48 +0000291 o->bytecode = code;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200292 if (def_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100293 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Damien Georgef0778a72014-06-07 22:01:00 +0100294 }
295 if (def_kw_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100296 o->extra_args[n_def_args] = def_kw_args;
Damien George2827d622014-04-27 15:50:52 +0100297 }
Damiend99b0522013-12-21 18:17:45 +0000298 return o;
299}
300
Damiend99b0522013-12-21 18:17:45 +0000301/******************************************************************************/
Damien George3c658a42014-08-24 16:28:17 +0100302/* native functions */
303
304#if MICROPY_EMIT_NATIVE
305
306typedef struct _mp_obj_fun_native_t {
307 mp_obj_base_t base;
308 mp_uint_t n_args;
309 void *fun_data; // GC must be able to trace this pointer
310 // TODO add mp_map_t *globals
311} mp_obj_fun_native_t;
312
313typedef mp_obj_t (*native_fun_0_t)();
314typedef mp_obj_t (*native_fun_1_t)(mp_obj_t);
315typedef mp_obj_t (*native_fun_2_t)(mp_obj_t, mp_obj_t);
316typedef mp_obj_t (*native_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
317
Damien Georgeecc88e92014-08-30 00:35:11 +0100318STATIC 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 +0100319 mp_obj_fun_native_t *self = self_in;
320
321 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
322
323 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
324
325 switch (n_args) {
326 case 0:
327 return ((native_fun_0_t)fun)();
328
329 case 1:
330 return ((native_fun_1_t)fun)(args[0]);
331
332 case 2:
333 return ((native_fun_2_t)fun)(args[0], args[1]);
334
335 case 3:
336 return ((native_fun_3_t)fun)(args[0], args[1], args[2]);
337
338 default:
339 assert(0);
340 return mp_const_none;
341 }
342}
343
344STATIC const mp_obj_type_t mp_type_fun_native = {
345 { &mp_type_type },
346 .name = MP_QSTR_function,
347 .call = fun_native_call,
348 .binary_op = mp_obj_fun_binary_op,
349};
350
351mp_obj_t mp_obj_new_fun_native(mp_uint_t n_args, void *fun_data) {
352 assert(0 <= n_args && n_args <= 3);
353 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
354 o->base.type = &mp_type_fun_native;
355 o->n_args = n_args;
356 o->fun_data = fun_data;
357 return o;
358}
359
360#endif // MICROPY_EMIT_NATIVE
361
362/******************************************************************************/
Damien George2ac4af62014-08-15 16:45:41 +0100363/* viper functions */
364
365#if MICROPY_EMIT_NATIVE
366
367typedef struct _mp_obj_fun_viper_t {
368 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100369 mp_uint_t n_args;
370 void *fun_data; // GC must be able to trace this pointer
Damien George2ac4af62014-08-15 16:45:41 +0100371 mp_uint_t type_sig;
372} mp_obj_fun_viper_t;
373
374typedef mp_uint_t (*viper_fun_0_t)();
375typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
376typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
377typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
378
Damien Georgeecc88e92014-08-30 00:35:11 +0100379STATIC 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 +0100380 mp_obj_fun_viper_t *self = self_in;
381
382 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
383
Damien George3c658a42014-08-24 16:28:17 +0100384 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
385
Damien George2ac4af62014-08-15 16:45:41 +0100386 mp_uint_t ret;
387 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100388 ret = ((viper_fun_0_t)fun)();
Damien George2ac4af62014-08-15 16:45:41 +0100389 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100390 ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2));
Damien George2ac4af62014-08-15 16:45:41 +0100391 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100392 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 +0100393 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100394 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 +0100395 } else {
396 assert(0);
397 ret = 0;
398 }
399
Damien Georgee6c0dff2014-08-15 23:47:59 +0100400 return mp_convert_native_to_obj(ret, self->type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100401}
402
403STATIC const mp_obj_type_t mp_type_fun_viper = {
404 { &mp_type_type },
405 .name = MP_QSTR_function,
406 .call = fun_viper_call,
Damien George3c658a42014-08-24 16:28:17 +0100407 .binary_op = mp_obj_fun_binary_op,
Damien George2ac4af62014-08-15 16:45:41 +0100408};
409
Damien George3c658a42014-08-24 16:28:17 +0100410mp_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 +0100411 mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
412 o->base.type = &mp_type_fun_viper;
413 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100414 o->fun_data = fun_data;
Damien George2ac4af62014-08-15 16:45:41 +0100415 o->type_sig = type_sig;
416 return o;
417}
418
419#endif // MICROPY_EMIT_NATIVE
420
421/******************************************************************************/
Damiend99b0522013-12-21 18:17:45 +0000422/* inline assembler functions */
423
Damien George2ac4af62014-08-15 16:45:41 +0100424#if MICROPY_EMIT_INLINE_THUMB
425
Damiend99b0522013-12-21 18:17:45 +0000426typedef struct _mp_obj_fun_asm_t {
427 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100428 mp_uint_t n_args;
429 void *fun_data; // GC must be able to trace this pointer
Damiend99b0522013-12-21 18:17:45 +0000430} mp_obj_fun_asm_t;
431
Damien George40f3c022014-07-03 13:25:24 +0100432typedef mp_uint_t (*inline_asm_fun_0_t)();
433typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
434typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
435typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damiend99b0522013-12-21 18:17:45 +0000436
437// convert a Micro Python object to a sensible value for inline asm
Damien George40f3c022014-07-03 13:25:24 +0100438STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000439 // TODO for byte_array, pass pointer to the array
440 if (MP_OBJ_IS_SMALL_INT(obj)) {
441 return MP_OBJ_SMALL_INT_VALUE(obj);
442 } else if (obj == mp_const_none) {
443 return 0;
444 } else if (obj == mp_const_false) {
445 return 0;
446 } else if (obj == mp_const_true) {
447 return 1;
Damien George5fa93b62014-01-22 14:35:10 +0000448 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000449 // pointer to the string (it's probably constant though!)
Damien Georgea7329612014-09-29 16:26:39 +0100450 mp_uint_t l;
Damien George40f3c022014-07-03 13:25:24 +0100451 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000452 } else {
Damien George87210872014-04-13 00:30:32 +0100453 mp_obj_type_t *type = mp_obj_get_type(obj);
454 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100455#if MICROPY_PY_BUILTINS_FLOAT
Damien George87210872014-04-13 00:30:32 +0100456 } else if (type == &mp_type_float) {
457 // convert float to int (could also pass in float registers)
Damien George40f3c022014-07-03 13:25:24 +0100458 return (mp_int_t)mp_obj_float_get(obj);
Damien George87210872014-04-13 00:30:32 +0100459#endif
460 } else if (type == &mp_type_tuple) {
461 // pointer to start of tuple (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100462 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100463 mp_obj_t *items;
464 mp_obj_tuple_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100465 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100466 } else if (type == &mp_type_list) {
467 // pointer to start of list (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100468 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100469 mp_obj_t *items;
470 mp_obj_list_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100471 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100472 } else {
Damien George57a4b4f2014-04-18 22:29:21 +0100473 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100474 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
Damien George8a1cab92014-04-13 12:08:52 +0100475 // supports the buffer protocol, return a pointer to the data
Damien George40f3c022014-07-03 13:25:24 +0100476 return (mp_uint_t)bufinfo.buf;
Damien George8a1cab92014-04-13 12:08:52 +0100477 } else {
478 // just pass along a pointer to the object
Damien George40f3c022014-07-03 13:25:24 +0100479 return (mp_uint_t)obj;
Damien George8a1cab92014-04-13 12:08:52 +0100480 }
Damien George87210872014-04-13 00:30:32 +0100481 }
Damiend99b0522013-12-21 18:17:45 +0000482 }
483}
484
485// convert a return value from inline asm to a sensible Micro Python object
Damien George40f3c022014-07-03 13:25:24 +0100486STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000487 return MP_OBJ_NEW_SMALL_INT(val);
488}
489
Damien Georgeecc88e92014-08-30 00:35:11 +0100490STATIC 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 +0000491 mp_obj_fun_asm_t *self = self_in;
492
Damien Georgeccc85ea2014-05-10 13:40:46 +0100493 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
Damiend99b0522013-12-21 18:17:45 +0000494
Damien George3c658a42014-08-24 16:28:17 +0100495 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
496
Damien George40f3c022014-07-03 13:25:24 +0100497 mp_uint_t ret;
Damiend99b0522013-12-21 18:17:45 +0000498 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100499 ret = ((inline_asm_fun_0_t)fun)();
Damiend99b0522013-12-21 18:17:45 +0000500 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100501 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000502 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100503 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 +0000504 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100505 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 +0000506 } else {
507 assert(0);
508 ret = 0;
509 }
510
511 return convert_val_from_inline_asm(ret);
512}
513
Damien George3e1a5c12014-03-29 13:43:38 +0000514STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000515 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000516 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000517 .call = fun_asm_call,
Damien George3c658a42014-08-24 16:28:17 +0100518 .binary_op = mp_obj_fun_binary_op,
Damiend99b0522013-12-21 18:17:45 +0000519};
520
Damien George3c658a42014-08-24 16:28:17 +0100521mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) {
Damiend99b0522013-12-21 18:17:45 +0000522 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000523 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000524 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100525 o->fun_data = fun_data;
Damiend99b0522013-12-21 18:17:45 +0000526 return o;
527}
Damien George2ac4af62014-08-15 16:45:41 +0100528
529#endif // MICROPY_EMIT_INLINE_THUMB