blob: 8b9d3f553b555c30c1fbe27c3e557616498a912a [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,
Damiend99b0522013-12-21 18:17:45 +0000101};
102
Damiend99b0522013-12-21 18:17:45 +0000103/******************************************************************************/
104/* byte code functions */
105
stijn3cc17c62015-02-14 18:44:31 +0100106qstr mp_obj_code_get_name(const byte *code_info) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100107 mp_decode_uint(&code_info); // skip code_info_size entry
stijn3cc17c62015-02-14 18:44:31 +0100108 return mp_decode_uint(&code_info);
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300109}
110
Damien George99886182015-04-06 22:38:53 +0100111#if MICROPY_EMIT_NATIVE
112STATIC const mp_obj_type_t mp_type_fun_native;
113#endif
114
stijn3cc17c62015-02-14 18:44:31 +0100115qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300116 const mp_obj_fun_bc_t *fun = fun_in;
Damien George99886182015-04-06 22:38:53 +0100117 #if MICROPY_EMIT_NATIVE
118 if (fun->base.type == &mp_type_fun_native) {
119 // TODO native functions don't have name stored
120 return MP_QSTR_;
121 }
122 #endif
Paul Sokolovsky418aca92014-05-03 14:10:34 +0300123 const byte *code_info = fun->bytecode;
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300124 return mp_obj_code_get_name(code_info);
125}
126
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300127#if MICROPY_CPYTHON_COMPAT
Damien George7f9d1d62015-04-09 23:56:15 +0100128STATIC 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 +0000129 (void)kind;
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300130 mp_obj_fun_bc_t *o = o_in;
Damien George7f9d1d62015-04-09 23:56:15 +0100131 mp_printf(print, "<function %s at 0x%x>", qstr_str(mp_obj_fun_get_name(o)), o);
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300132}
133#endif
134
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200135#if DEBUG_PRINT
Damien George39dc1452014-10-03 19:52:22 +0100136STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200137 DEBUG_printf("%p: ", a);
Damien George39dc1452014-10-03 19:52:22 +0100138 for (mp_uint_t i = 0; i < sz; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200139 DEBUG_printf("%p ", a[i]);
140 }
141 DEBUG_printf("\n");
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200142}
Damien Georgef78b6df2014-03-31 15:59:25 +0100143#else
144#define dump_args(...) (void)0
145#endif
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200146
Damien Georgeaabd83e2014-06-07 14:16:08 +0100147// With this macro you can tune the maximum number of function state bytes
148// that will be allocated on the stack. Any function that needs more
149// than this will use the heap.
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800150#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
Damien Georgeaabd83e2014-06-07 14:16:08 +0100151
152// Set this to enable a simple stack overflow check.
153#define VM_DETECT_STACK_OVERFLOW (0)
154
Damien George12a5e172015-04-01 23:31:30 +0100155#if MICROPY_STACKLESS
Paul Sokolovsky20397572015-03-28 01:14:44 +0200156mp_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) {
157 MP_STACK_CHECK();
158 mp_obj_fun_bc_t *self = self_in;
159
160 // skip code-info block
161 const byte *code_info = self->bytecode;
162 mp_uint_t code_info_size = mp_decode_uint(&code_info);
163 const byte *ip = self->bytecode + code_info_size;
164
165 // bytecode prelude: skip arg names
166 ip += (self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t);
167
168 // bytecode prelude: state size and exception stack size
169 mp_uint_t n_state = mp_decode_uint(&ip);
170 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
171
172 // allocate state for locals and stack
173 mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
174 mp_code_state *code_state;
Paul Sokolovsky332a9092015-03-28 01:14:44 +0200175 code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
176 if (!code_state) {
177 return NULL;
178 }
Paul Sokolovsky20397572015-03-28 01:14:44 +0200179
180 code_state->n_state = n_state;
Damien George99886182015-04-06 22:38:53 +0100181 code_state->code_info = 0; // offset to code-info
182 code_state->ip = ip - self->bytecode; // offset to prelude
Paul Sokolovsky20397572015-03-28 01:14:44 +0200183 mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
184
185 // execute the byte code with the correct globals context
186 code_state->old_globals = mp_globals_get();
187 mp_globals_set(self->globals);
188
189 return code_state;
190}
Damien George12a5e172015-04-01 23:31:30 +0100191#endif
Paul Sokolovsky20397572015-03-28 01:14:44 +0200192
Damien Georgeecc88e92014-08-30 00:35:11 +0100193STATIC 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 +0300194 MP_STACK_CHECK();
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300195
Damien Georgeeaaebf32014-09-23 10:59:05 +0100196 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300197 DEBUG_printf("Input pos args: ");
198 dump_args(args, n_args);
199 DEBUG_printf("Input kw args: ");
200 dump_args(args + n_args, n_kw * 2);
201 mp_obj_fun_bc_t *self = self_in;
202 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
203
Damien Georgeb534e1b2014-09-04 14:44:01 +0100204 // skip code-info block
205 const byte *code_info = self->bytecode;
206 mp_uint_t code_info_size = mp_decode_uint(&code_info);
207 const byte *ip = self->bytecode + code_info_size;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300208
Damien George1084b0f2014-10-25 15:07:02 +0100209 // bytecode prelude: skip arg names
210 ip += (self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t);
211
212 // bytecode prelude: state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +0100213 mp_uint_t n_state = mp_decode_uint(&ip);
214 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300215
216#if VM_DETECT_STACK_OVERFLOW
217 n_state += 1;
218#endif
219
220 // allocate state for locals and stack
Damien George3c658a42014-08-24 16:28:17 +0100221 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 +0300222 mp_code_state *code_state;
223 if (state_size > VM_MAX_STATE_ON_STACK) {
224 code_state = m_new_obj_var(mp_code_state, byte, state_size);
225 } else {
226 code_state = alloca(sizeof(mp_code_state) + state_size);
227 }
228
229 code_state->n_state = n_state;
Damien George99886182015-04-06 22:38:53 +0100230 code_state->code_info = 0; // offset to code-info
231 code_state->ip = (byte*)(ip - self->bytecode); // offset to prelude
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300232 mp_setup_code_state(code_state, self_in, n_args, n_kw, args);
Damien George049a7a82014-06-08 00:37:40 +0100233
234 // execute the byte code with the correct globals context
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800235 code_state->old_globals = mp_globals_get();
Damien George049a7a82014-06-08 00:37:40 +0100236 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100237 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800238 mp_globals_set(code_state->old_globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100239
240#if VM_DETECT_STACK_OVERFLOW
241 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
242 if (code_state->sp < code_state->state) {
243 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
244 assert(0);
245 }
246 }
247 // We can't check the case when an exception is returned in state[n_state - 1]
248 // and there are no arguments, because in this case our detection slot may have
249 // been overwritten by the returned exception (which is allowed).
Damien George049a7a82014-06-08 00:37:40 +0100250 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100251 // Just check to see that we have at least 1 null object left in the state.
252 bool overflow = true;
Damien George3c658a42014-08-24 16:28:17 +0100253 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 +0100254 if (code_state->state[i] == MP_OBJ_NULL) {
255 overflow = false;
256 break;
257 }
258 }
259 if (overflow) {
260 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
261 assert(0);
262 }
263 }
264#endif
265
Damien George049a7a82014-06-08 00:37:40 +0100266 mp_obj_t result;
Damien Georgeaabd83e2014-06-07 14:16:08 +0100267 switch (vm_return_kind) {
268 case MP_VM_RETURN_NORMAL:
269 // return value is in *sp
270 result = *code_state->sp;
271 break;
272
273 case MP_VM_RETURN_EXCEPTION:
274 // return value is in state[n_state - 1]
275 result = code_state->state[n_state - 1];
276 break;
277
278 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
279 default:
280 assert(0);
281 result = mp_const_none;
282 vm_return_kind = MP_VM_RETURN_NORMAL;
283 break;
284 }
285
286 // free the state if it was allocated on the heap
287 if (state_size > VM_MAX_STATE_ON_STACK) {
288 m_del_var(mp_code_state, byte, state_size, code_state);
289 }
290
Damien Georgec8f78bc2014-02-15 22:55:00 +0000291 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
292 return result;
293 } else { // MP_VM_RETURN_EXCEPTION
Damien Georgeea13f402014-04-05 18:32:08 +0100294 nlr_raise(result);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000295 }
Damiend99b0522013-12-21 18:17:45 +0000296}
297
stijn3cc17c62015-02-14 18:44:31 +0100298#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000299STATIC void fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
300 if (dest[0] != MP_OBJ_NULL) {
301 // not load attribute
302 return;
303 }
Damien George4dea9222015-04-09 15:29:54 +0000304 if (attr == MP_QSTR___name__) {
stijn3cc17c62015-02-14 18:44:31 +0100305 dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
306 }
307}
308#endif
309
Damien George3e1a5c12014-03-29 13:43:38 +0000310const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000311 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000312 .name = MP_QSTR_function,
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300313#if MICROPY_CPYTHON_COMPAT
314 .print = fun_bc_print,
315#endif
Damien George20006db2014-01-18 14:10:48 +0000316 .call = fun_bc_call,
stijn3cc17c62015-02-14 18:44:31 +0100317#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000318 .attr = fun_bc_attr,
stijn3cc17c62015-02-14 18:44:31 +0100319#endif
Damiend99b0522013-12-21 18:17:45 +0000320};
321
Damien George1084b0f2014-10-25 15:07:02 +0100322mp_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 +0100323 mp_uint_t n_def_args = 0;
324 mp_uint_t n_extra_args = 0;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200325 mp_obj_tuple_t *def_args = def_args_in;
326 if (def_args != MP_OBJ_NULL) {
Damien George69b89d22014-04-11 13:38:30 +0000327 assert(MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200328 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000329 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200330 }
Damien Georgef0778a72014-06-07 22:01:00 +0100331 if (def_kw_args != MP_OBJ_NULL) {
332 n_extra_args += 1;
333 }
Damien George2e482cd2014-02-16 00:01:29 +0000334 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 +0000335 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100336 o->globals = mp_globals_get();
Damien George2827d622014-04-27 15:50:52 +0100337 o->n_pos_args = n_pos_args;
338 o->n_kwonly_args = n_kwonly_args;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200339 o->n_def_args = n_def_args;
Damien Georgef0778a72014-06-07 22:01:00 +0100340 o->has_def_kw_args = def_kw_args != MP_OBJ_NULL;
Damien George2e482cd2014-02-16 00:01:29 +0000341 o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
342 o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
Damien George66028ab2014-01-03 14:03:48 +0000343 o->bytecode = code;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200344 if (def_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100345 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Damien Georgef0778a72014-06-07 22:01:00 +0100346 }
347 if (def_kw_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100348 o->extra_args[n_def_args] = def_kw_args;
Damien George2827d622014-04-27 15:50:52 +0100349 }
Damiend99b0522013-12-21 18:17:45 +0000350 return o;
351}
352
Damiend99b0522013-12-21 18:17:45 +0000353/******************************************************************************/
Damien George3c658a42014-08-24 16:28:17 +0100354/* native functions */
355
356#if MICROPY_EMIT_NATIVE
357
Damien Georgeecc88e92014-08-30 00:35:11 +0100358STATIC 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 +0100359 MP_STACK_CHECK();
360 mp_obj_fun_bc_t *self = self_in;
361 mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
362 return fun(self_in, n_args, n_kw, args);
Damien George3c658a42014-08-24 16:28:17 +0100363}
364
365STATIC const mp_obj_type_t mp_type_fun_native = {
366 { &mp_type_type },
367 .name = MP_QSTR_function,
368 .call = fun_native_call,
Damien George3c658a42014-08-24 16:28:17 +0100369};
370
Damien George99886182015-04-06 22:38:53 +0100371mp_obj_t mp_obj_new_fun_native(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 void *fun_data) {
372 mp_obj_fun_bc_t *o = mp_obj_new_fun_bc(scope_flags, n_pos_args, n_kwonly_args, def_args_in, def_kw_args, (const byte*)fun_data);
Damien George3c658a42014-08-24 16:28:17 +0100373 o->base.type = &mp_type_fun_native;
Damien George3c658a42014-08-24 16:28:17 +0100374 return o;
375}
376
377#endif // MICROPY_EMIT_NATIVE
378
379/******************************************************************************/
Damien George2ac4af62014-08-15 16:45:41 +0100380/* viper functions */
381
382#if MICROPY_EMIT_NATIVE
383
384typedef struct _mp_obj_fun_viper_t {
385 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100386 mp_uint_t n_args;
387 void *fun_data; // GC must be able to trace this pointer
Damien George2ac4af62014-08-15 16:45:41 +0100388 mp_uint_t type_sig;
389} mp_obj_fun_viper_t;
390
Damien Georgebbf5cd02015-01-12 22:45:35 +0000391typedef mp_uint_t (*viper_fun_0_t)(void);
Damien George2ac4af62014-08-15 16:45:41 +0100392typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
393typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
394typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
395
Damien Georgeecc88e92014-08-30 00:35:11 +0100396STATIC 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 +0100397 mp_obj_fun_viper_t *self = self_in;
398
399 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
400
Damien George3c658a42014-08-24 16:28:17 +0100401 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
402
Damien George2ac4af62014-08-15 16:45:41 +0100403 mp_uint_t ret;
404 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100405 ret = ((viper_fun_0_t)fun)();
Damien George2ac4af62014-08-15 16:45:41 +0100406 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100407 ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 2));
Damien George2ac4af62014-08-15 16:45:41 +0100408 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100409 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 +0100410 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100411 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 +0100412 } else {
413 assert(0);
414 ret = 0;
415 }
416
Damien Georgee6c0dff2014-08-15 23:47:59 +0100417 return mp_convert_native_to_obj(ret, self->type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100418}
419
420STATIC const mp_obj_type_t mp_type_fun_viper = {
421 { &mp_type_type },
422 .name = MP_QSTR_function,
423 .call = fun_viper_call,
Damien George2ac4af62014-08-15 16:45:41 +0100424};
425
Damien George3c658a42014-08-24 16:28:17 +0100426mp_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 +0100427 mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
428 o->base.type = &mp_type_fun_viper;
429 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100430 o->fun_data = fun_data;
Damien George2ac4af62014-08-15 16:45:41 +0100431 o->type_sig = type_sig;
432 return o;
433}
434
435#endif // MICROPY_EMIT_NATIVE
436
437/******************************************************************************/
Damiend99b0522013-12-21 18:17:45 +0000438/* inline assembler functions */
439
Damien George2ac4af62014-08-15 16:45:41 +0100440#if MICROPY_EMIT_INLINE_THUMB
441
Damiend99b0522013-12-21 18:17:45 +0000442typedef struct _mp_obj_fun_asm_t {
443 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100444 mp_uint_t n_args;
445 void *fun_data; // GC must be able to trace this pointer
Damiend99b0522013-12-21 18:17:45 +0000446} mp_obj_fun_asm_t;
447
Damien Georgebbf5cd02015-01-12 22:45:35 +0000448typedef mp_uint_t (*inline_asm_fun_0_t)(void);
Damien George40f3c022014-07-03 13:25:24 +0100449typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
450typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
451typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damiend99b0522013-12-21 18:17:45 +0000452
453// convert a Micro Python object to a sensible value for inline asm
Damien George40f3c022014-07-03 13:25:24 +0100454STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000455 // TODO for byte_array, pass pointer to the array
456 if (MP_OBJ_IS_SMALL_INT(obj)) {
457 return MP_OBJ_SMALL_INT_VALUE(obj);
458 } else if (obj == mp_const_none) {
459 return 0;
460 } else if (obj == mp_const_false) {
461 return 0;
462 } else if (obj == mp_const_true) {
463 return 1;
Damien George32f0b792015-02-13 10:43:05 +0000464 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
465 return mp_obj_int_get_truncated(obj);
Damien George5fa93b62014-01-22 14:35:10 +0000466 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000467 // pointer to the string (it's probably constant though!)
Damien Georgea7329612014-09-29 16:26:39 +0100468 mp_uint_t l;
Damien George40f3c022014-07-03 13:25:24 +0100469 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000470 } else {
Damien George87210872014-04-13 00:30:32 +0100471 mp_obj_type_t *type = mp_obj_get_type(obj);
472 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100473#if MICROPY_PY_BUILTINS_FLOAT
Damien George87210872014-04-13 00:30:32 +0100474 } else if (type == &mp_type_float) {
475 // convert float to int (could also pass in float registers)
Damien George40f3c022014-07-03 13:25:24 +0100476 return (mp_int_t)mp_obj_float_get(obj);
Damien George87210872014-04-13 00:30:32 +0100477#endif
478 } else if (type == &mp_type_tuple) {
479 // pointer to start of tuple (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100480 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100481 mp_obj_t *items;
482 mp_obj_tuple_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100483 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100484 } else if (type == &mp_type_list) {
485 // pointer to start of list (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100486 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100487 mp_obj_t *items;
488 mp_obj_list_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100489 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100490 } else {
Damien George57a4b4f2014-04-18 22:29:21 +0100491 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100492 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
Damien George8a1cab92014-04-13 12:08:52 +0100493 // supports the buffer protocol, return a pointer to the data
Damien George40f3c022014-07-03 13:25:24 +0100494 return (mp_uint_t)bufinfo.buf;
Damien George8a1cab92014-04-13 12:08:52 +0100495 } else {
496 // just pass along a pointer to the object
Damien George40f3c022014-07-03 13:25:24 +0100497 return (mp_uint_t)obj;
Damien George8a1cab92014-04-13 12:08:52 +0100498 }
Damien George87210872014-04-13 00:30:32 +0100499 }
Damiend99b0522013-12-21 18:17:45 +0000500 }
501}
502
503// convert a return value from inline asm to a sensible Micro Python object
Damien George40f3c022014-07-03 13:25:24 +0100504STATIC mp_obj_t convert_val_from_inline_asm(mp_uint_t val) {
Damiend99b0522013-12-21 18:17:45 +0000505 return MP_OBJ_NEW_SMALL_INT(val);
506}
507
Damien Georgeecc88e92014-08-30 00:35:11 +0100508STATIC 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 +0000509 mp_obj_fun_asm_t *self = self_in;
510
Damien Georgeccc85ea2014-05-10 13:40:46 +0100511 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
Damiend99b0522013-12-21 18:17:45 +0000512
Damien George3c658a42014-08-24 16:28:17 +0100513 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
514
Damien George40f3c022014-07-03 13:25:24 +0100515 mp_uint_t ret;
Damiend99b0522013-12-21 18:17:45 +0000516 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100517 ret = ((inline_asm_fun_0_t)fun)();
Damiend99b0522013-12-21 18:17:45 +0000518 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100519 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000520 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100521 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 +0000522 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100523 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 +0000524 } else {
525 assert(0);
526 ret = 0;
527 }
528
529 return convert_val_from_inline_asm(ret);
530}
531
Damien George3e1a5c12014-03-29 13:43:38 +0000532STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000533 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000534 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000535 .call = fun_asm_call,
Damiend99b0522013-12-21 18:17:45 +0000536};
537
Damien George3c658a42014-08-24 16:28:17 +0100538mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data) {
Damiend99b0522013-12-21 18:17:45 +0000539 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000540 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000541 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100542 o->fun_data = fun_data;
Damiend99b0522013-12-21 18:17:45 +0000543 return o;
544}
Damien George2ac4af62014-08-15 16:45:41 +0100545
546#endif // MICROPY_EMIT_INLINE_THUMB