blob: 3f2691522356a7188a226d2204885cb3d19ee532 [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:
Damien George20006db2014-01-18 14:10:48 +000086 return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
John R. Lenton45a87442014-01-04 01:15:01 +000087
Damiend99b0522013-12-21 18:17:45 +000088 default:
89 assert(0);
90 return mp_const_none;
91 }
92
93 } else {
Damien George20006db2014-01-18 14:10:48 +000094 // function takes a variable number of arguments, but no keywords
Damiend99b0522013-12-21 18:17:45 +000095
Damien George20006db2014-01-18 14:10:48 +000096 return ((mp_fun_var_t)self->fun)(n_args, args);
Damiend99b0522013-12-21 18:17:45 +000097 }
98}
99
Damien George3c658a42014-08-24 16:28:17 +0100100const mp_obj_type_t mp_type_fun_builtin = {
Damien Georgec5966122014-02-15 16:10:44 +0000101 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000102 .name = MP_QSTR_function,
Damien George3c658a42014-08-24 16:28:17 +0100103 .call = fun_builtin_call,
Damiend99b0522013-12-21 18:17:45 +0000104};
105
Damiend99b0522013-12-21 18:17:45 +0000106/******************************************************************************/
107/* byte code functions */
108
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300109const char *mp_obj_code_get_name(const byte *code_info) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100110 mp_decode_uint(&code_info); // skip code_info_size entry
111 return qstr_str(mp_decode_uint(&code_info));
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300112}
113
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300114const char *mp_obj_fun_get_name(mp_const_obj_t fun_in) {
115 const mp_obj_fun_bc_t *fun = fun_in;
Paul Sokolovsky418aca92014-05-03 14:10:34 +0300116 const byte *code_info = fun->bytecode;
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300117 return mp_obj_code_get_name(code_info);
118}
119
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300120#if MICROPY_CPYTHON_COMPAT
121STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
122 mp_obj_fun_bc_t *o = o_in;
123 print(env, "<function %s at 0x%x>", mp_obj_fun_get_name(o), o);
124}
125#endif
126
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200127#if DEBUG_PRINT
Damien George39dc1452014-10-03 19:52:22 +0100128STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200129 DEBUG_printf("%p: ", a);
Damien George39dc1452014-10-03 19:52:22 +0100130 for (mp_uint_t i = 0; i < sz; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200131 DEBUG_printf("%p ", a[i]);
132 }
133 DEBUG_printf("\n");
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200134}
Damien Georgef78b6df2014-03-31 15:59:25 +0100135#else
136#define dump_args(...) (void)0
137#endif
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200138
Damien Georgeaabd83e2014-06-07 14:16:08 +0100139// With this macro you can tune the maximum number of function state bytes
140// that will be allocated on the stack. Any function that needs more
141// than this will use the heap.
Damien George40f3c022014-07-03 13:25:24 +0100142#define VM_MAX_STATE_ON_STACK (10 * sizeof(mp_uint_t))
Damien Georgeaabd83e2014-06-07 14:16:08 +0100143
144// Set this to enable a simple stack overflow check.
145#define VM_DETECT_STACK_OVERFLOW (0)
146
Damien Georgeecc88e92014-08-30 00:35:11 +0100147STATIC 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 +0300148 MP_STACK_CHECK();
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300149
Damien Georgeeaaebf32014-09-23 10:59:05 +0100150 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300151 DEBUG_printf("Input pos args: ");
152 dump_args(args, n_args);
153 DEBUG_printf("Input kw args: ");
154 dump_args(args + n_args, n_kw * 2);
155 mp_obj_fun_bc_t *self = self_in;
156 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
157
Damien Georgeb534e1b2014-09-04 14:44:01 +0100158 // skip code-info block
159 const byte *code_info = self->bytecode;
160 mp_uint_t code_info_size = mp_decode_uint(&code_info);
161 const byte *ip = self->bytecode + code_info_size;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300162
Damien George1084b0f2014-10-25 15:07:02 +0100163 // bytecode prelude: skip arg names
164 ip += (self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t);
165
166 // bytecode prelude: state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +0100167 mp_uint_t n_state = mp_decode_uint(&ip);
168 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300169
170#if VM_DETECT_STACK_OVERFLOW
171 n_state += 1;
172#endif
173
174 // allocate state for locals and stack
Damien George3c658a42014-08-24 16:28:17 +0100175 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 +0300176 mp_code_state *code_state;
177 if (state_size > VM_MAX_STATE_ON_STACK) {
178 code_state = m_new_obj_var(mp_code_state, byte, state_size);
179 } else {
180 code_state = alloca(sizeof(mp_code_state) + state_size);
181 }
182
183 code_state->n_state = n_state;
184 code_state->ip = ip;
185 mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
Damien George049a7a82014-06-08 00:37:40 +0100186
187 // execute the byte code with the correct globals context
188 mp_obj_dict_t *old_globals = mp_globals_get();
189 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100190 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
Damien George049a7a82014-06-08 00:37:40 +0100191 mp_globals_set(old_globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100192
193#if VM_DETECT_STACK_OVERFLOW
194 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
195 if (code_state->sp < code_state->state) {
196 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
197 assert(0);
198 }
199 }
200 // We can't check the case when an exception is returned in state[n_state - 1]
201 // and there are no arguments, because in this case our detection slot may have
202 // been overwritten by the returned exception (which is allowed).
Damien George049a7a82014-06-08 00:37:40 +0100203 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100204 // Just check to see that we have at least 1 null object left in the state.
205 bool overflow = true;
Damien George3c658a42014-08-24 16:28:17 +0100206 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 +0100207 if (code_state->state[i] == MP_OBJ_NULL) {
208 overflow = false;
209 break;
210 }
211 }
212 if (overflow) {
213 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
214 assert(0);
215 }
216 }
217#endif
218
Damien George049a7a82014-06-08 00:37:40 +0100219 mp_obj_t result;
Damien Georgeaabd83e2014-06-07 14:16:08 +0100220 switch (vm_return_kind) {
221 case MP_VM_RETURN_NORMAL:
222 // return value is in *sp
223 result = *code_state->sp;
224 break;
225
226 case MP_VM_RETURN_EXCEPTION:
227 // return value is in state[n_state - 1]
228 result = code_state->state[n_state - 1];
229 break;
230
231 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
232 default:
233 assert(0);
234 result = mp_const_none;
235 vm_return_kind = MP_VM_RETURN_NORMAL;
236 break;
237 }
238
239 // free the state if it was allocated on the heap
240 if (state_size > VM_MAX_STATE_ON_STACK) {
241 m_del_var(mp_code_state, byte, state_size, code_state);
242 }
243
Damien Georgec8f78bc2014-02-15 22:55:00 +0000244 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
245 return result;
246 } else { // MP_VM_RETURN_EXCEPTION
Damien Georgeea13f402014-04-05 18:32:08 +0100247 nlr_raise(result);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000248 }
Damiend99b0522013-12-21 18:17:45 +0000249}
250
Damien George3e1a5c12014-03-29 13:43:38 +0000251const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000252 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000253 .name = MP_QSTR_function,
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300254#if MICROPY_CPYTHON_COMPAT
255 .print = fun_bc_print,
256#endif
Damien George20006db2014-01-18 14:10:48 +0000257 .call = fun_bc_call,
Damiend99b0522013-12-21 18:17:45 +0000258};
259
Damien George1084b0f2014-10-25 15:07:02 +0100260mp_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 +0100261 mp_uint_t n_def_args = 0;
262 mp_uint_t n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200263 mp_obj_tuple_t *def_args = def_args_in;
264 if (def_args != MP_OBJ_NULL) {
Damien George69b89d22014-04-11 13:38:30 +0000265 assert(MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200266 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000267 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200268 }
Damien Georgef0778a72014-06-07 22:01:00 +0100269 if (def_kw_args != MP_OBJ_NULL) {
270 n_extra_args += 1;
271 }
Damien George2e482cd2014-02-16 00:01:29 +0000272 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 +0000273 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100274 o->globals = mp_globals_get();
Damien George2827d622014-04-27 15:50:52 +0100275 o->n_pos_args = n_pos_args;
276 o->n_kwonly_args = n_kwonly_args;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200277 o->n_def_args = n_def_args;
Damien Georgef0778a72014-06-07 22:01:00 +0100278 o->has_def_kw_args = def_kw_args != MP_OBJ_NULL;
Damien George2e482cd2014-02-16 00:01:29 +0000279 o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
280 o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
Damien George66028ab2014-01-03 14:03:48 +0000281 o->bytecode = code;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200282 if (def_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100283 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Damien Georgef0778a72014-06-07 22:01:00 +0100284 }
285 if (def_kw_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100286 o->extra_args[n_def_args] = def_kw_args;
Damien George2827d622014-04-27 15:50:52 +0100287 }
Damiend99b0522013-12-21 18:17:45 +0000288 return o;
289}
290
Damiend99b0522013-12-21 18:17:45 +0000291/******************************************************************************/
Damien George3c658a42014-08-24 16:28:17 +0100292/* native functions */
293
294#if MICROPY_EMIT_NATIVE
295
296typedef struct _mp_obj_fun_native_t {
297 mp_obj_base_t base;
298 mp_uint_t n_args;
299 void *fun_data; // GC must be able to trace this pointer
300 // TODO add mp_map_t *globals
301} mp_obj_fun_native_t;
302
303typedef mp_obj_t (*native_fun_0_t)();
304typedef mp_obj_t (*native_fun_1_t)(mp_obj_t);
305typedef mp_obj_t (*native_fun_2_t)(mp_obj_t, mp_obj_t);
306typedef mp_obj_t (*native_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
307
Damien Georgeecc88e92014-08-30 00:35:11 +0100308STATIC 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 +0100309 mp_obj_fun_native_t *self = self_in;
310
311 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
312
313 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
314
315 switch (n_args) {
316 case 0:
317 return ((native_fun_0_t)fun)();
318
319 case 1:
320 return ((native_fun_1_t)fun)(args[0]);
321
322 case 2:
323 return ((native_fun_2_t)fun)(args[0], args[1]);
324
325 case 3:
326 return ((native_fun_3_t)fun)(args[0], args[1], args[2]);
327
328 default:
329 assert(0);
330 return mp_const_none;
331 }
332}
333
334STATIC const mp_obj_type_t mp_type_fun_native = {
335 { &mp_type_type },
336 .name = MP_QSTR_function,
337 .call = fun_native_call,
Damien George3c658a42014-08-24 16:28:17 +0100338};
339
340mp_obj_t mp_obj_new_fun_native(mp_uint_t n_args, void *fun_data) {
341 assert(0 <= n_args && n_args <= 3);
342 mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
343 o->base.type = &mp_type_fun_native;
344 o->n_args = n_args;
345 o->fun_data = fun_data;
346 return o;
347}
348
349#endif // MICROPY_EMIT_NATIVE
350
351/******************************************************************************/
Damien George2ac4af62014-08-15 16:45:41 +0100352/* viper functions */
353
354#if MICROPY_EMIT_NATIVE
355
356typedef struct _mp_obj_fun_viper_t {
357 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100358 mp_uint_t n_args;
359 void *fun_data; // GC must be able to trace this pointer
Damien George2ac4af62014-08-15 16:45:41 +0100360 mp_uint_t type_sig;
361} mp_obj_fun_viper_t;
362
363typedef mp_uint_t (*viper_fun_0_t)();
364typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
365typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
366typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
367
Damien Georgeecc88e92014-08-30 00:35:11 +0100368STATIC 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 +0100369 mp_obj_fun_viper_t *self = self_in;
370
371 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
372
Damien George3c658a42014-08-24 16:28:17 +0100373 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
374
Damien George2ac4af62014-08-15 16:45:41 +0100375 mp_uint_t ret;
376 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100377 ret = ((viper_fun_0_t)fun)();
Damien George2ac4af62014-08-15 16:45:41 +0100378 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100379 ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2));
Damien George2ac4af62014-08-15 16:45:41 +0100380 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100381 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 +0100382 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100383 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 +0100384 } else {
385 assert(0);
386 ret = 0;
387 }
388
Damien Georgee6c0dff2014-08-15 23:47:59 +0100389 return mp_convert_native_to_obj(ret, self->type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100390}
391
392STATIC const mp_obj_type_t mp_type_fun_viper = {
393 { &mp_type_type },
394 .name = MP_QSTR_function,
395 .call = fun_viper_call,
Damien George2ac4af62014-08-15 16:45:41 +0100396};
397
Damien George3c658a42014-08-24 16:28:17 +0100398mp_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 +0100399 mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
400 o->base.type = &mp_type_fun_viper;
401 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100402 o->fun_data = fun_data;
Damien George2ac4af62014-08-15 16:45:41 +0100403 o->type_sig = type_sig;
404 return o;
405}
406
407#endif // MICROPY_EMIT_NATIVE
408
409/******************************************************************************/
Damiend99b0522013-12-21 18:17:45 +0000410/* inline assembler functions */
411
Damien George2ac4af62014-08-15 16:45:41 +0100412#if MICROPY_EMIT_INLINE_THUMB
413
Damiend99b0522013-12-21 18:17:45 +0000414typedef struct _mp_obj_fun_asm_t {
415 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100416 mp_uint_t n_args;
417 void *fun_data; // GC must be able to trace this pointer
Damiend99b0522013-12-21 18:17:45 +0000418} mp_obj_fun_asm_t;
419
Damien George40f3c022014-07-03 13:25:24 +0100420typedef mp_uint_t (*inline_asm_fun_0_t)();
421typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
422typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
423typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damiend99b0522013-12-21 18:17:45 +0000424
425// convert a Micro Python object to a sensible value for inline asm
Damien George40f3c022014-07-03 13:25:24 +0100426STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000427 // TODO for byte_array, pass pointer to the array
428 if (MP_OBJ_IS_SMALL_INT(obj)) {
429 return MP_OBJ_SMALL_INT_VALUE(obj);
430 } else if (obj == mp_const_none) {
431 return 0;
432 } else if (obj == mp_const_false) {
433 return 0;
434 } else if (obj == mp_const_true) {
435 return 1;
Damien George5fa93b62014-01-22 14:35:10 +0000436 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000437 // pointer to the string (it's probably constant though!)
Damien Georgea7329612014-09-29 16:26:39 +0100438 mp_uint_t l;
Damien George40f3c022014-07-03 13:25:24 +0100439 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000440 } else {
Damien George87210872014-04-13 00:30:32 +0100441 mp_obj_type_t *type = mp_obj_get_type(obj);
442 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100443#if MICROPY_PY_BUILTINS_FLOAT
Damien George87210872014-04-13 00:30:32 +0100444 } else if (type == &mp_type_float) {
445 // convert float to int (could also pass in float registers)
Damien George40f3c022014-07-03 13:25:24 +0100446 return (mp_int_t)mp_obj_float_get(obj);
Damien George87210872014-04-13 00:30:32 +0100447#endif
448 } else if (type == &mp_type_tuple) {
449 // pointer to start of tuple (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100450 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100451 mp_obj_t *items;
452 mp_obj_tuple_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100453 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100454 } else if (type == &mp_type_list) {
455 // pointer to start of list (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100456 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100457 mp_obj_t *items;
458 mp_obj_list_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100459 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100460 } else {
Damien George57a4b4f2014-04-18 22:29:21 +0100461 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100462 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
Damien George8a1cab92014-04-13 12:08:52 +0100463 // supports the buffer protocol, return a pointer to the data
Damien George40f3c022014-07-03 13:25:24 +0100464 return (mp_uint_t)bufinfo.buf;
Damien George8a1cab92014-04-13 12:08:52 +0100465 } else {
466 // just pass along a pointer to the object
Damien George40f3c022014-07-03 13:25:24 +0100467 return (mp_uint_t)obj;
Damien George8a1cab92014-04-13 12:08:52 +0100468 }
Damien George87210872014-04-13 00:30:32 +0100469 }
Damiend99b0522013-12-21 18:17:45 +0000470 }
471}
472
473// convert a return value from inline asm to a sensible Micro Python object
Damien George40f3c022014-07-03 13:25:24 +0100474STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000475 return MP_OBJ_NEW_SMALL_INT(val);
476}
477
Damien Georgeecc88e92014-08-30 00:35:11 +0100478STATIC 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 +0000479 mp_obj_fun_asm_t *self = self_in;
480
Damien Georgeccc85ea2014-05-10 13:40:46 +0100481 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
Damiend99b0522013-12-21 18:17:45 +0000482
Damien George3c658a42014-08-24 16:28:17 +0100483 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
484
Damien George40f3c022014-07-03 13:25:24 +0100485 mp_uint_t ret;
Damiend99b0522013-12-21 18:17:45 +0000486 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100487 ret = ((inline_asm_fun_0_t)fun)();
Damiend99b0522013-12-21 18:17:45 +0000488 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100489 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000490 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100491 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 +0000492 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100493 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 +0000494 } else {
495 assert(0);
496 ret = 0;
497 }
498
499 return convert_val_from_inline_asm(ret);
500}
501
Damien George3e1a5c12014-03-29 13:43:38 +0000502STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000503 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000504 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000505 .call = fun_asm_call,
Damiend99b0522013-12-21 18:17:45 +0000506};
507
Damien George3c658a42014-08-24 16:28:17 +0100508mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) {
Damiend99b0522013-12-21 18:17:45 +0000509 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000510 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000511 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100512 o->fun_data = fun_data;
Damiend99b0522013-12-21 18:17:45 +0000513 return o;
514}
Damien George2ac4af62014-08-15 16:45:41 +0100515
516#endif // MICROPY_EMIT_INLINE_THUMB