blob: df7d162133361d75f027f795b124ff87618a45a5 [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 Georgea0c97812016-01-03 09:59:18 +000055STATIC mp_obj_t fun_builtin_call(mp_obj_t self_in, size_t n_args, size_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));
Damien George999cedb2015-11-27 17:01:44 +000057 mp_obj_fun_builtin_t *self = MP_OBJ_TO_PTR(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 George1b0aab62016-01-03 11:53:44 +000069 return self->fun.kw(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:
Damien George1b0aab62016-01-03 11:53:44 +000077 return self->fun._0();
Damiend99b0522013-12-21 18:17:45 +000078
79 case 1:
Damien George1b0aab62016-01-03 11:53:44 +000080 return self->fun._1(args[0]);
Damiend99b0522013-12-21 18:17:45 +000081
82 case 2:
Damien George1b0aab62016-01-03 11:53:44 +000083 return self->fun._2(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 George1b0aab62016-01-03 11:53:44 +000087 return self->fun._3(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 George1b0aab62016-01-03 11:53:44 +000093 return self->fun.var(n_args, args);
Damiend99b0522013-12-21 18:17:45 +000094 }
95}
96
Damien George3c658a42014-08-24 16:28:17 +010097const mp_obj_type_t mp_type_fun_builtin = {
Damien Georgec5966122014-02-15 16:10:44 +000098 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000099 .name = MP_QSTR_function,
Damien George3c658a42014-08-24 16:28:17 +0100100 .call = fun_builtin_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000101 .unary_op = mp_generic_unary_op,
Damiend99b0522013-12-21 18:17:45 +0000102};
103
Damiend99b0522013-12-21 18:17:45 +0000104/******************************************************************************/
105/* byte code functions */
106
stijn3cc17c62015-02-14 18:44:31 +0100107qstr mp_obj_code_get_name(const byte *code_info) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100108 mp_decode_uint(&code_info); // skip code_info_size entry
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000109 #if MICROPY_PERSISTENT_CODE
110 return code_info[0] | (code_info[1] << 8);
111 #else
stijn3cc17c62015-02-14 18:44:31 +0100112 return mp_decode_uint(&code_info);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000113 #endif
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300114}
115
Damien George99886182015-04-06 22:38:53 +0100116#if MICROPY_EMIT_NATIVE
117STATIC const mp_obj_type_t mp_type_fun_native;
118#endif
119
stijn3cc17c62015-02-14 18:44:31 +0100120qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
Damien George999cedb2015-11-27 17:01:44 +0000121 const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
Damien George99886182015-04-06 22:38:53 +0100122 #if MICROPY_EMIT_NATIVE
123 if (fun->base.type == &mp_type_fun_native) {
124 // TODO native functions don't have name stored
125 return MP_QSTR_;
126 }
127 #endif
Damien George9b7f5832015-03-18 17:47:47 +0000128
129 const byte *bc = fun->bytecode;
130 mp_decode_uint(&bc); // skip n_state
131 mp_decode_uint(&bc); // skip n_exc_stack
Damien George3a3db4d2015-10-22 23:45:37 +0100132 bc++; // skip scope_params
Damien George713ea182015-10-23 01:23:11 +0100133 bc++; // skip n_pos_args
134 bc++; // skip n_kwonly_args
Damien George3a3db4d2015-10-22 23:45:37 +0100135 bc++; // skip n_def_pos_args
Damien George9b7f5832015-03-18 17:47:47 +0000136 return mp_obj_code_get_name(bc);
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300137}
138
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300139#if MICROPY_CPYTHON_COMPAT
Damien George7f9d1d62015-04-09 23:56:15 +0100140STATIC 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 +0000141 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +0000142 mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
143 mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300144}
145#endif
146
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200147#if DEBUG_PRINT
Damien George39dc1452014-10-03 19:52:22 +0100148STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200149 DEBUG_printf("%p: ", a);
Damien George39dc1452014-10-03 19:52:22 +0100150 for (mp_uint_t i = 0; i < sz; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200151 DEBUG_printf("%p ", a[i]);
152 }
153 DEBUG_printf("\n");
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200154}
Damien Georgef78b6df2014-03-31 15:59:25 +0100155#else
156#define dump_args(...) (void)0
157#endif
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200158
Damien Georgeaabd83e2014-06-07 14:16:08 +0100159// With this macro you can tune the maximum number of function state bytes
160// that will be allocated on the stack. Any function that needs more
Damien George6e56bb62015-06-08 22:07:27 +0100161// than this will try to use the heap, with fallback to stack allocation.
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800162#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
Damien Georgeaabd83e2014-06-07 14:16:08 +0100163
164// Set this to enable a simple stack overflow check.
165#define VM_DETECT_STACK_OVERFLOW (0)
166
Damien George12a5e172015-04-01 23:31:30 +0100167#if MICROPY_STACKLESS
Damien George1d899e12015-12-17 12:33:42 +0000168mp_code_state *mp_obj_fun_bc_prepare_codestate(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Paul Sokolovsky20397572015-03-28 01:14:44 +0200169 MP_STACK_CHECK();
Damien George7a30e872015-12-17 12:32:41 +0000170 mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200171
Damien George9b7f5832015-03-18 17:47:47 +0000172 // get start of bytecode
173 const byte *ip = self->bytecode;
Paul Sokolovsky20397572015-03-28 01:14:44 +0200174
175 // bytecode prelude: state size and exception stack size
Damien George1d899e12015-12-17 12:33:42 +0000176 size_t n_state = mp_decode_uint(&ip);
177 size_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200178
179 // allocate state for locals and stack
Damien George1d899e12015-12-17 12:33:42 +0000180 size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200181 mp_code_state *code_state;
Paul Sokolovsky332a9092015-03-28 01:14:44 +0200182 code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
183 if (!code_state) {
184 return NULL;
185 }
Paul Sokolovsky20397572015-03-28 01:14:44 +0200186
Damien George9b7f5832015-03-18 17:47:47 +0000187 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
Paul Sokolovsky20397572015-03-28 01:14:44 +0200188 code_state->n_state = n_state;
Damien George1d899e12015-12-17 12:33:42 +0000189 mp_setup_code_state(code_state, self, n_args, n_kw, args);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200190
191 // execute the byte code with the correct globals context
192 code_state->old_globals = mp_globals_get();
193 mp_globals_set(self->globals);
194
195 return code_state;
196}
Damien George12a5e172015-04-01 23:31:30 +0100197#endif
Paul Sokolovsky20397572015-03-28 01:14:44 +0200198
Damien Georgea0c97812016-01-03 09:59:18 +0000199STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300200 MP_STACK_CHECK();
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300201
Damien Georgeeaaebf32014-09-23 10:59:05 +0100202 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300203 DEBUG_printf("Input pos args: ");
204 dump_args(args, n_args);
205 DEBUG_printf("Input kw args: ");
206 dump_args(args + n_args, n_kw * 2);
Damien George999cedb2015-11-27 17:01:44 +0000207 mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300208 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
209
Damien George9b7f5832015-03-18 17:47:47 +0000210 // get start of bytecode
211 const byte *ip = self->bytecode;
Damien George1084b0f2014-10-25 15:07:02 +0100212
213 // bytecode prelude: state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +0100214 mp_uint_t n_state = mp_decode_uint(&ip);
215 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300216
217#if VM_DETECT_STACK_OVERFLOW
218 n_state += 1;
219#endif
220
221 // allocate state for locals and stack
Damien George3c658a42014-08-24 16:28:17 +0100222 mp_uint_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
Damien George6e56bb62015-06-08 22:07:27 +0100223 mp_code_state *code_state = NULL;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300224 if (state_size > VM_MAX_STATE_ON_STACK) {
Damien George6e56bb62015-06-08 22:07:27 +0100225 code_state = m_new_obj_var_maybe(mp_code_state, byte, state_size);
226 }
227 if (code_state == NULL) {
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300228 code_state = alloca(sizeof(mp_code_state) + state_size);
Damien George6e56bb62015-06-08 22:07:27 +0100229 state_size = 0; // indicate that we allocated using alloca
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300230 }
231
Damien George9b7f5832015-03-18 17:47:47 +0000232 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300233 code_state->n_state = n_state;
Damien George9f6976b2015-11-27 12:50:54 +0000234 mp_setup_code_state(code_state, self, n_args, n_kw, args);
Damien George049a7a82014-06-08 00:37:40 +0100235
236 // execute the byte code with the correct globals context
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800237 code_state->old_globals = mp_globals_get();
Damien George049a7a82014-06-08 00:37:40 +0100238 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100239 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800240 mp_globals_set(code_state->old_globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100241
242#if VM_DETECT_STACK_OVERFLOW
243 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
244 if (code_state->sp < code_state->state) {
245 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
246 assert(0);
247 }
248 }
249 // We can't check the case when an exception is returned in state[n_state - 1]
250 // and there are no arguments, because in this case our detection slot may have
251 // been overwritten by the returned exception (which is allowed).
Damien George049a7a82014-06-08 00:37:40 +0100252 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100253 // Just check to see that we have at least 1 null object left in the state.
254 bool overflow = true;
Damien George3c658a42014-08-24 16:28:17 +0100255 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 +0100256 if (code_state->state[i] == MP_OBJ_NULL) {
257 overflow = false;
258 break;
259 }
260 }
261 if (overflow) {
262 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
263 assert(0);
264 }
265 }
266#endif
267
Damien George049a7a82014-06-08 00:37:40 +0100268 mp_obj_t result;
Damien Georgeaabd83e2014-06-07 14:16:08 +0100269 switch (vm_return_kind) {
270 case MP_VM_RETURN_NORMAL:
271 // return value is in *sp
272 result = *code_state->sp;
273 break;
274
275 case MP_VM_RETURN_EXCEPTION:
276 // return value is in state[n_state - 1]
277 result = code_state->state[n_state - 1];
278 break;
279
280 case MP_VM_RETURN_YIELD: // byte-code shouldn't yield
281 default:
282 assert(0);
283 result = mp_const_none;
284 vm_return_kind = MP_VM_RETURN_NORMAL;
285 break;
286 }
287
288 // free the state if it was allocated on the heap
Damien George6e56bb62015-06-08 22:07:27 +0100289 if (state_size != 0) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100290 m_del_var(mp_code_state, byte, state_size, code_state);
291 }
292
Damien Georgec8f78bc2014-02-15 22:55:00 +0000293 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
294 return result;
295 } else { // MP_VM_RETURN_EXCEPTION
Damien Georgeea13f402014-04-05 18:32:08 +0100296 nlr_raise(result);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000297 }
Damiend99b0522013-12-21 18:17:45 +0000298}
299
stijn3cc17c62015-02-14 18:44:31 +0100300#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000301STATIC void fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
302 if (dest[0] != MP_OBJ_NULL) {
303 // not load attribute
304 return;
305 }
Damien George4dea9222015-04-09 15:29:54 +0000306 if (attr == MP_QSTR___name__) {
stijn3cc17c62015-02-14 18:44:31 +0100307 dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
308 }
309}
310#endif
311
Damien George3e1a5c12014-03-29 13:43:38 +0000312const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000313 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000314 .name = MP_QSTR_function,
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300315#if MICROPY_CPYTHON_COMPAT
316 .print = fun_bc_print,
317#endif
Damien George20006db2014-01-18 14:10:48 +0000318 .call = fun_bc_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000319 .unary_op = mp_generic_unary_op,
stijn3cc17c62015-02-14 18:44:31 +0100320#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000321 .attr = fun_bc_attr,
stijn3cc17c62015-02-14 18:44:31 +0100322#endif
Damiend99b0522013-12-21 18:17:45 +0000323};
324
Damien George713ea182015-10-23 01:23:11 +0100325mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args_in, mp_obj_t def_kw_args, const byte *code, const mp_uint_t *const_table) {
Damien George3c658a42014-08-24 16:28:17 +0100326 mp_uint_t n_def_args = 0;
327 mp_uint_t n_extra_args = 0;
Damien George999cedb2015-11-27 17:01:44 +0000328 mp_obj_tuple_t *def_args = MP_OBJ_TO_PTR(def_args_in);
329 if (def_args_in != MP_OBJ_NULL) {
330 assert(MP_OBJ_IS_TYPE(def_args_in, &mp_type_tuple));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200331 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000332 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200333 }
Damien Georgef0778a72014-06-07 22:01:00 +0100334 if (def_kw_args != MP_OBJ_NULL) {
335 n_extra_args += 1;
336 }
Damien George2e482cd2014-02-16 00:01:29 +0000337 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 +0000338 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100339 o->globals = mp_globals_get();
Damien George66028ab2014-01-03 14:03:48 +0000340 o->bytecode = code;
Damien George713ea182015-10-23 01:23:11 +0100341 o->const_table = const_table;
Damien George999cedb2015-11-27 17:01:44 +0000342 if (def_args != NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100343 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Damien Georgef0778a72014-06-07 22:01:00 +0100344 }
345 if (def_kw_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100346 o->extra_args[n_def_args] = def_kw_args;
Damien George2827d622014-04-27 15:50:52 +0100347 }
Damien George999cedb2015-11-27 17:01:44 +0000348 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000349}
350
Damiend99b0522013-12-21 18:17:45 +0000351/******************************************************************************/
Damien George3c658a42014-08-24 16:28:17 +0100352/* native functions */
353
354#if MICROPY_EMIT_NATIVE
355
Damien Georgea0c97812016-01-03 09:59:18 +0000356STATIC mp_obj_t fun_native_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien George99886182015-04-06 22:38:53 +0100357 MP_STACK_CHECK();
358 mp_obj_fun_bc_t *self = self_in;
359 mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
360 return fun(self_in, n_args, n_kw, args);
Damien George3c658a42014-08-24 16:28:17 +0100361}
362
363STATIC const mp_obj_type_t mp_type_fun_native = {
364 { &mp_type_type },
365 .name = MP_QSTR_function,
366 .call = fun_native_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000367 .unary_op = mp_generic_unary_op,
Damien George3c658a42014-08-24 16:28:17 +0100368};
369
Damien George713ea182015-10-23 01:23:11 +0100370mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table) {
371 mp_obj_fun_bc_t *o = mp_obj_new_fun_bc(def_args_in, def_kw_args, (const byte*)fun_data, const_table);
Damien George3c658a42014-08-24 16:28:17 +0100372 o->base.type = &mp_type_fun_native;
Damien George3c658a42014-08-24 16:28:17 +0100373 return o;
374}
375
376#endif // MICROPY_EMIT_NATIVE
377
378/******************************************************************************/
Damien George2ac4af62014-08-15 16:45:41 +0100379/* viper functions */
380
381#if MICROPY_EMIT_NATIVE
382
383typedef struct _mp_obj_fun_viper_t {
384 mp_obj_base_t base;
Damien George3c658a42014-08-24 16:28:17 +0100385 mp_uint_t n_args;
386 void *fun_data; // GC must be able to trace this pointer
Damien George2ac4af62014-08-15 16:45:41 +0100387 mp_uint_t type_sig;
388} mp_obj_fun_viper_t;
389
Damien Georgebbf5cd02015-01-12 22:45:35 +0000390typedef mp_uint_t (*viper_fun_0_t)(void);
Damien George2ac4af62014-08-15 16:45:41 +0100391typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
392typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
393typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damien Georgee45c1db2015-07-23 14:11:29 +0100394typedef mp_uint_t (*viper_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
Damien George2ac4af62014-08-15 16:45:41 +0100395
Damien Georgea0c97812016-01-03 09:59:18 +0000396STATIC mp_obj_t fun_viper_call(mp_obj_t self_in, size_t n_args, size_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 Georgee45c1db2015-07-23 14:11:29 +0100412 } else if (n_args == 4) {
413 ret = ((viper_fun_4_t)fun)(
414 mp_convert_obj_to_native(args[0], self->type_sig >> 2),
415 mp_convert_obj_to_native(args[1], self->type_sig >> 4),
416 mp_convert_obj_to_native(args[2], self->type_sig >> 6),
417 mp_convert_obj_to_native(args[3], self->type_sig >> 8)
418 );
Damien George2ac4af62014-08-15 16:45:41 +0100419 } else {
Damien Georgee45c1db2015-07-23 14:11:29 +0100420 // TODO 5 or more arguments not supported for viper call
Damien George2ac4af62014-08-15 16:45:41 +0100421 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 Georgec2a4e4e2015-05-11 12:25:19 +0000432 .unary_op = mp_generic_unary_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
Damien George8f54c082016-01-15 15:20:43 +0000455 mp_uint_t type_sig;
Damiend99b0522013-12-21 18:17:45 +0000456} mp_obj_fun_asm_t;
457
Damien Georgebbf5cd02015-01-12 22:45:35 +0000458typedef mp_uint_t (*inline_asm_fun_0_t)(void);
Damien George40f3c022014-07-03 13:25:24 +0100459typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
460typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
461typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damiend99b0522013-12-21 18:17:45 +0000462
463// convert a Micro Python object to a sensible value for inline asm
Damien George40f3c022014-07-03 13:25:24 +0100464STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000465 // TODO for byte_array, pass pointer to the array
466 if (MP_OBJ_IS_SMALL_INT(obj)) {
467 return MP_OBJ_SMALL_INT_VALUE(obj);
468 } else if (obj == mp_const_none) {
469 return 0;
470 } else if (obj == mp_const_false) {
471 return 0;
472 } else if (obj == mp_const_true) {
473 return 1;
Damien George32f0b792015-02-13 10:43:05 +0000474 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
475 return mp_obj_int_get_truncated(obj);
Damien George5fa93b62014-01-22 14:35:10 +0000476 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000477 // pointer to the string (it's probably constant though!)
Damien Georgea7329612014-09-29 16:26:39 +0100478 mp_uint_t l;
Damien George40f3c022014-07-03 13:25:24 +0100479 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000480 } else {
Damien George87210872014-04-13 00:30:32 +0100481 mp_obj_type_t *type = mp_obj_get_type(obj);
482 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100483#if MICROPY_PY_BUILTINS_FLOAT
Damien George87210872014-04-13 00:30:32 +0100484 } else if (type == &mp_type_float) {
485 // convert float to int (could also pass in float registers)
Damien George40f3c022014-07-03 13:25:24 +0100486 return (mp_int_t)mp_obj_float_get(obj);
Damien George87210872014-04-13 00:30:32 +0100487#endif
488 } else if (type == &mp_type_tuple) {
489 // pointer to start of tuple (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100490 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100491 mp_obj_t *items;
492 mp_obj_tuple_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100493 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100494 } else if (type == &mp_type_list) {
495 // pointer to start of list (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100496 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100497 mp_obj_t *items;
498 mp_obj_list_get(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100499 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100500 } else {
Damien George57a4b4f2014-04-18 22:29:21 +0100501 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100502 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
Damien George8a1cab92014-04-13 12:08:52 +0100503 // supports the buffer protocol, return a pointer to the data
Damien George40f3c022014-07-03 13:25:24 +0100504 return (mp_uint_t)bufinfo.buf;
Damien George8a1cab92014-04-13 12:08:52 +0100505 } else {
506 // just pass along a pointer to the object
Damien George40f3c022014-07-03 13:25:24 +0100507 return (mp_uint_t)obj;
Damien George8a1cab92014-04-13 12:08:52 +0100508 }
Damien George87210872014-04-13 00:30:32 +0100509 }
Damiend99b0522013-12-21 18:17:45 +0000510 }
511}
512
Damien Georgea0c97812016-01-03 09:59:18 +0000513STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damiend99b0522013-12-21 18:17:45 +0000514 mp_obj_fun_asm_t *self = self_in;
515
Damien Georgeccc85ea2014-05-10 13:40:46 +0100516 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
Damiend99b0522013-12-21 18:17:45 +0000517
Damien George3c658a42014-08-24 16:28:17 +0100518 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
519
Damien George40f3c022014-07-03 13:25:24 +0100520 mp_uint_t ret;
Damiend99b0522013-12-21 18:17:45 +0000521 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100522 ret = ((inline_asm_fun_0_t)fun)();
Damiend99b0522013-12-21 18:17:45 +0000523 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100524 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000525 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100526 ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
Damiend99b0522013-12-21 18:17:45 +0000527 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100528 ret = ((inline_asm_fun_3_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]), convert_obj_for_inline_asm(args[2]));
Damiend99b0522013-12-21 18:17:45 +0000529 } else {
530 assert(0);
531 ret = 0;
532 }
533
Damien George8f54c082016-01-15 15:20:43 +0000534 return mp_convert_native_to_obj(ret, self->type_sig);
Damiend99b0522013-12-21 18:17:45 +0000535}
536
Damien George3e1a5c12014-03-29 13:43:38 +0000537STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000538 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000539 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000540 .call = fun_asm_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000541 .unary_op = mp_generic_unary_op,
Damiend99b0522013-12-21 18:17:45 +0000542};
543
Damien George8f54c082016-01-15 15:20:43 +0000544mp_obj_t mp_obj_new_fun_asm(mp_uint_t n_args, void *fun_data, mp_uint_t type_sig) {
Damiend99b0522013-12-21 18:17:45 +0000545 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000546 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000547 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100548 o->fun_data = fun_data;
Damien George8f54c082016-01-15 15:20:43 +0000549 o->type_sig = type_sig;
Damiend99b0522013-12-21 18:17:45 +0000550 return o;
551}
Damien George2ac4af62014-08-15 16:45:41 +0100552
553#endif // MICROPY_EMIT_INLINE_THUMB