blob: a823f49e535d2d34ea95b4e4f8532106bbdee169 [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
Damien George571e6f22016-10-18 11:49:27 +110053STATIC mp_obj_t fun_builtin_0_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
54 (void)args;
55 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_0));
56 mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
57 mp_arg_check_num(n_args, n_kw, 0, 0, false);
58 return self->fun._0();
59}
Damien George3c658a42014-08-24 16:28:17 +010060
Damien George571e6f22016-10-18 11:49:27 +110061const mp_obj_type_t mp_type_fun_builtin_0 = {
62 { &mp_type_type },
63 .name = MP_QSTR_function,
64 .call = fun_builtin_0_call,
65 .unary_op = mp_generic_unary_op,
66};
67
68STATIC mp_obj_t fun_builtin_1_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
69 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_1));
70 mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
71 mp_arg_check_num(n_args, n_kw, 1, 1, false);
72 return self->fun._1(args[0]);
73}
74
75const mp_obj_type_t mp_type_fun_builtin_1 = {
76 { &mp_type_type },
77 .name = MP_QSTR_function,
78 .call = fun_builtin_1_call,
79 .unary_op = mp_generic_unary_op,
80};
81
82STATIC mp_obj_t fun_builtin_2_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
83 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_2));
84 mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
85 mp_arg_check_num(n_args, n_kw, 2, 2, false);
86 return self->fun._2(args[0], args[1]);
87}
88
89const mp_obj_type_t mp_type_fun_builtin_2 = {
90 { &mp_type_type },
91 .name = MP_QSTR_function,
92 .call = fun_builtin_2_call,
93 .unary_op = mp_generic_unary_op,
94};
95
96STATIC mp_obj_t fun_builtin_3_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
97 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_3));
98 mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
99 mp_arg_check_num(n_args, n_kw, 3, 3, false);
100 return self->fun._3(args[0], args[1], args[2]);
101}
102
103const mp_obj_type_t mp_type_fun_builtin_3 = {
104 { &mp_type_type },
105 .name = MP_QSTR_function,
106 .call = fun_builtin_3_call,
107 .unary_op = mp_generic_unary_op,
108};
109
110STATIC mp_obj_t fun_builtin_var_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
111 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_builtin_var));
112 mp_obj_fun_builtin_var_t *self = MP_OBJ_TO_PTR(self_in);
Damien George20006db2014-01-18 14:10:48 +0000113
John R. Lenton88cb1e62014-01-13 19:55:18 +0000114 // check number of arguments
Damien Georgea3f94e02014-04-20 00:13:22 +0100115 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 +0000116
John R. Lentonc06763a2014-01-07 17:29:16 +0000117 if (self->is_kw) {
Damien George20006db2014-01-18 14:10:48 +0000118 // function allows keywords
119
Damien George0a587b82014-02-08 18:53:41 +0000120 // we create a map directly from the given args array
121 mp_map_t kw_args;
122 mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
Damien George20006db2014-01-18 14:10:48 +0000123
Damien George1b0aab62016-01-03 11:53:44 +0000124 return self->fun.kw(n_args, args, &kw_args);
Damien George20006db2014-01-18 14:10:48 +0000125
Damiend99b0522013-12-21 18:17:45 +0000126 } else {
Damien George20006db2014-01-18 14:10:48 +0000127 // function takes a variable number of arguments, but no keywords
Damiend99b0522013-12-21 18:17:45 +0000128
Damien George1b0aab62016-01-03 11:53:44 +0000129 return self->fun.var(n_args, args);
Damiend99b0522013-12-21 18:17:45 +0000130 }
131}
132
Damien George571e6f22016-10-18 11:49:27 +1100133const mp_obj_type_t mp_type_fun_builtin_var = {
Damien Georgec5966122014-02-15 16:10:44 +0000134 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000135 .name = MP_QSTR_function,
Damien George571e6f22016-10-18 11:49:27 +1100136 .call = fun_builtin_var_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000137 .unary_op = mp_generic_unary_op,
Damiend99b0522013-12-21 18:17:45 +0000138};
139
Damiend99b0522013-12-21 18:17:45 +0000140/******************************************************************************/
141/* byte code functions */
142
stijn3cc17c62015-02-14 18:44:31 +0100143qstr mp_obj_code_get_name(const byte *code_info) {
Damien Georgeb534e1b2014-09-04 14:44:01 +0100144 mp_decode_uint(&code_info); // skip code_info_size entry
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000145 #if MICROPY_PERSISTENT_CODE
146 return code_info[0] | (code_info[1] << 8);
147 #else
stijn3cc17c62015-02-14 18:44:31 +0100148 return mp_decode_uint(&code_info);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000149 #endif
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300150}
151
Damien George99886182015-04-06 22:38:53 +0100152#if MICROPY_EMIT_NATIVE
153STATIC const mp_obj_type_t mp_type_fun_native;
154#endif
155
stijn3cc17c62015-02-14 18:44:31 +0100156qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
Damien George999cedb2015-11-27 17:01:44 +0000157 const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
Damien George99886182015-04-06 22:38:53 +0100158 #if MICROPY_EMIT_NATIVE
159 if (fun->base.type == &mp_type_fun_native) {
160 // TODO native functions don't have name stored
161 return MP_QSTR_;
162 }
163 #endif
Damien George9b7f5832015-03-18 17:47:47 +0000164
165 const byte *bc = fun->bytecode;
166 mp_decode_uint(&bc); // skip n_state
167 mp_decode_uint(&bc); // skip n_exc_stack
Damien George3a3db4d2015-10-22 23:45:37 +0100168 bc++; // skip scope_params
Damien George713ea182015-10-23 01:23:11 +0100169 bc++; // skip n_pos_args
170 bc++; // skip n_kwonly_args
Damien George3a3db4d2015-10-22 23:45:37 +0100171 bc++; // skip n_def_pos_args
Damien George9b7f5832015-03-18 17:47:47 +0000172 return mp_obj_code_get_name(bc);
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300173}
174
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300175#if MICROPY_CPYTHON_COMPAT
Damien George7f9d1d62015-04-09 23:56:15 +0100176STATIC 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 +0000177 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +0000178 mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
179 mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300180}
181#endif
182
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200183#if DEBUG_PRINT
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100184STATIC void dump_args(const mp_obj_t *a, size_t sz) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200185 DEBUG_printf("%p: ", a);
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100186 for (size_t i = 0; i < sz; i++) {
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200187 DEBUG_printf("%p ", a[i]);
188 }
189 DEBUG_printf("\n");
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200190}
Damien Georgef78b6df2014-03-31 15:59:25 +0100191#else
192#define dump_args(...) (void)0
193#endif
Paul Sokolovskyac2e28c2014-02-16 18:30:49 +0200194
Damien Georgeaabd83e2014-06-07 14:16:08 +0100195// With this macro you can tune the maximum number of function state bytes
196// that will be allocated on the stack. Any function that needs more
Damien George6e56bb62015-06-08 22:07:27 +0100197// than this will try to use the heap, with fallback to stack allocation.
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800198#define VM_MAX_STATE_ON_STACK (11 * sizeof(mp_uint_t))
Damien Georgeaabd83e2014-06-07 14:16:08 +0100199
200// Set this to enable a simple stack overflow check.
201#define VM_DETECT_STACK_OVERFLOW (0)
202
Damien George12a5e172015-04-01 23:31:30 +0100203#if MICROPY_STACKLESS
Damien George581a59a2016-08-27 23:21:00 +1000204mp_code_state_t *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 +0200205 MP_STACK_CHECK();
Damien George7a30e872015-12-17 12:32:41 +0000206 mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200207
Damien George9b7f5832015-03-18 17:47:47 +0000208 // get start of bytecode
209 const byte *ip = self->bytecode;
Paul Sokolovsky20397572015-03-28 01:14:44 +0200210
211 // bytecode prelude: state size and exception stack size
Damien George1d899e12015-12-17 12:33:42 +0000212 size_t n_state = mp_decode_uint(&ip);
213 size_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200214
215 // allocate state for locals and stack
Damien George1d899e12015-12-17 12:33:42 +0000216 size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
Damien George581a59a2016-08-27 23:21:00 +1000217 mp_code_state_t *code_state;
218 code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
Paul Sokolovsky332a9092015-03-28 01:14:44 +0200219 if (!code_state) {
220 return NULL;
221 }
Paul Sokolovsky20397572015-03-28 01:14:44 +0200222
Damien George9b7f5832015-03-18 17:47:47 +0000223 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
Paul Sokolovsky20397572015-03-28 01:14:44 +0200224 code_state->n_state = n_state;
Damien George1d899e12015-12-17 12:33:42 +0000225 mp_setup_code_state(code_state, self, n_args, n_kw, args);
Paul Sokolovsky20397572015-03-28 01:14:44 +0200226
227 // execute the byte code with the correct globals context
228 code_state->old_globals = mp_globals_get();
229 mp_globals_set(self->globals);
230
231 return code_state;
232}
Damien George12a5e172015-04-01 23:31:30 +0100233#endif
Paul Sokolovsky20397572015-03-28 01:14:44 +0200234
Damien Georgea0c97812016-01-03 09:59:18 +0000235STATIC 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 +0300236 MP_STACK_CHECK();
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300237
Damien Georgeeaaebf32014-09-23 10:59:05 +0100238 DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300239 DEBUG_printf("Input pos args: ");
240 dump_args(args, n_args);
241 DEBUG_printf("Input kw args: ");
242 dump_args(args + n_args, n_kw * 2);
Damien George999cedb2015-11-27 17:01:44 +0000243 mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300244 DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
245
Damien George9b7f5832015-03-18 17:47:47 +0000246 // get start of bytecode
247 const byte *ip = self->bytecode;
Damien George1084b0f2014-10-25 15:07:02 +0100248
249 // bytecode prelude: state size and exception stack size
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100250 size_t n_state = mp_decode_uint(&ip);
251 size_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300252
253#if VM_DETECT_STACK_OVERFLOW
254 n_state += 1;
255#endif
256
257 // allocate state for locals and stack
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100258 size_t state_size = n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t);
Damien George581a59a2016-08-27 23:21:00 +1000259 mp_code_state_t *code_state = NULL;
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300260 if (state_size > VM_MAX_STATE_ON_STACK) {
Damien George581a59a2016-08-27 23:21:00 +1000261 code_state = m_new_obj_var_maybe(mp_code_state_t, byte, state_size);
Damien George6e56bb62015-06-08 22:07:27 +0100262 }
263 if (code_state == NULL) {
Damien George581a59a2016-08-27 23:21:00 +1000264 code_state = alloca(sizeof(mp_code_state_t) + state_size);
Damien George6e56bb62015-06-08 22:07:27 +0100265 state_size = 0; // indicate that we allocated using alloca
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300266 }
267
Damien George9b7f5832015-03-18 17:47:47 +0000268 code_state->ip = (byte*)(ip - self->bytecode); // offset to after n_state/n_exc_stack
Paul Sokolovsky49df7952014-06-11 17:56:43 +0300269 code_state->n_state = n_state;
Damien George9f6976b2015-11-27 12:50:54 +0000270 mp_setup_code_state(code_state, self, n_args, n_kw, args);
Damien George049a7a82014-06-08 00:37:40 +0100271
272 // execute the byte code with the correct globals context
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800273 code_state->old_globals = mp_globals_get();
Damien George049a7a82014-06-08 00:37:40 +0100274 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100275 mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
Paul Sokolovsky53e5e0f2015-02-15 17:12:58 +0800276 mp_globals_set(code_state->old_globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100277
278#if VM_DETECT_STACK_OVERFLOW
279 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
280 if (code_state->sp < code_state->state) {
281 printf("VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
282 assert(0);
283 }
284 }
285 // We can't check the case when an exception is returned in state[n_state - 1]
286 // and there are no arguments, because in this case our detection slot may have
287 // been overwritten by the returned exception (which is allowed).
Damien George049a7a82014-06-08 00:37:40 +0100288 if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && self->n_pos_args + self->n_kwonly_args == 0)) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100289 // Just check to see that we have at least 1 null object left in the state.
290 bool overflow = true;
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100291 for (size_t i = 0; i < n_state - self->n_pos_args - self->n_kwonly_args; i++) {
Damien Georgeaabd83e2014-06-07 14:16:08 +0100292 if (code_state->state[i] == MP_OBJ_NULL) {
293 overflow = false;
294 break;
295 }
296 }
297 if (overflow) {
298 printf("VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
299 assert(0);
300 }
301 }
302#endif
303
Damien George049a7a82014-06-08 00:37:40 +0100304 mp_obj_t result;
Damien George0c595fa2016-09-27 23:08:10 +1000305 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
306 // return value is in *sp
307 result = *code_state->sp;
308 } else {
309 // must be an exception because normal functions can't yield
310 assert(vm_return_kind == MP_VM_RETURN_EXCEPTION);
311 // return value is in fastn[0]==state[n_state - 1]
312 result = code_state->state[n_state - 1];
Damien Georgeaabd83e2014-06-07 14:16:08 +0100313 }
314
315 // free the state if it was allocated on the heap
Damien George6e56bb62015-06-08 22:07:27 +0100316 if (state_size != 0) {
Damien George581a59a2016-08-27 23:21:00 +1000317 m_del_var(mp_code_state_t, byte, state_size, code_state);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100318 }
319
Damien Georgec8f78bc2014-02-15 22:55:00 +0000320 if (vm_return_kind == MP_VM_RETURN_NORMAL) {
321 return result;
322 } else { // MP_VM_RETURN_EXCEPTION
Damien Georgeea13f402014-04-05 18:32:08 +0100323 nlr_raise(result);
Damien Georgec8f78bc2014-02-15 22:55:00 +0000324 }
Damiend99b0522013-12-21 18:17:45 +0000325}
326
stijn3cc17c62015-02-14 18:44:31 +0100327#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000328STATIC void fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
329 if (dest[0] != MP_OBJ_NULL) {
330 // not load attribute
331 return;
332 }
Damien George4dea9222015-04-09 15:29:54 +0000333 if (attr == MP_QSTR___name__) {
stijn3cc17c62015-02-14 18:44:31 +0100334 dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
335 }
336}
337#endif
338
Damien George3e1a5c12014-03-29 13:43:38 +0000339const mp_obj_type_t mp_type_fun_bc = {
Damien Georgec5966122014-02-15 16:10:44 +0000340 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000341 .name = MP_QSTR_function,
Paul Sokolovsky68551a82014-05-01 01:32:58 +0300342#if MICROPY_CPYTHON_COMPAT
343 .print = fun_bc_print,
344#endif
Damien George20006db2014-01-18 14:10:48 +0000345 .call = fun_bc_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000346 .unary_op = mp_generic_unary_op,
stijn3cc17c62015-02-14 18:44:31 +0100347#if MICROPY_PY_FUNCTION_ATTRS
Damien Georgeb1bbe962015-04-01 14:10:50 +0000348 .attr = fun_bc_attr,
stijn3cc17c62015-02-14 18:44:31 +0100349#endif
Damiend99b0522013-12-21 18:17:45 +0000350};
351
Damien George713ea182015-10-23 01:23:11 +0100352mp_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 Georgedbcdb9f2017-02-16 16:34:53 +1100353 size_t n_def_args = 0;
354 size_t n_extra_args = 0;
Damien George999cedb2015-11-27 17:01:44 +0000355 mp_obj_tuple_t *def_args = MP_OBJ_TO_PTR(def_args_in);
356 if (def_args_in != MP_OBJ_NULL) {
357 assert(MP_OBJ_IS_TYPE(def_args_in, &mp_type_tuple));
Paul Sokolovsky90750022014-02-01 15:05:04 +0200358 n_def_args = def_args->len;
Damien George2e482cd2014-02-16 00:01:29 +0000359 n_extra_args = def_args->len;
Paul Sokolovsky90750022014-02-01 15:05:04 +0200360 }
Damien Georgef0778a72014-06-07 22:01:00 +0100361 if (def_kw_args != MP_OBJ_NULL) {
362 n_extra_args += 1;
363 }
Damien George2e482cd2014-02-16 00:01:29 +0000364 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 +0000365 o->base.type = &mp_type_fun_bc;
Damien Georged17926d2014-03-30 13:35:08 +0100366 o->globals = mp_globals_get();
Damien George66028ab2014-01-03 14:03:48 +0000367 o->bytecode = code;
Damien George713ea182015-10-23 01:23:11 +0100368 o->const_table = const_table;
Damien George999cedb2015-11-27 17:01:44 +0000369 if (def_args != NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100370 memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
Damien Georgef0778a72014-06-07 22:01:00 +0100371 }
372 if (def_kw_args != MP_OBJ_NULL) {
Damien George049a7a82014-06-08 00:37:40 +0100373 o->extra_args[n_def_args] = def_kw_args;
Damien George2827d622014-04-27 15:50:52 +0100374 }
Damien George999cedb2015-11-27 17:01:44 +0000375 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000376}
377
Damiend99b0522013-12-21 18:17:45 +0000378/******************************************************************************/
Damien George3c658a42014-08-24 16:28:17 +0100379/* native functions */
380
381#if MICROPY_EMIT_NATIVE
382
Damien Georgea0c97812016-01-03 09:59:18 +0000383STATIC 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 +0100384 MP_STACK_CHECK();
385 mp_obj_fun_bc_t *self = self_in;
386 mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void*)self->bytecode);
387 return fun(self_in, n_args, n_kw, args);
Damien George3c658a42014-08-24 16:28:17 +0100388}
389
390STATIC const mp_obj_type_t mp_type_fun_native = {
391 { &mp_type_type },
392 .name = MP_QSTR_function,
393 .call = fun_native_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000394 .unary_op = mp_generic_unary_op,
Damien George3c658a42014-08-24 16:28:17 +0100395};
396
Damien George713ea182015-10-23 01:23:11 +0100397mp_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) {
398 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 +0100399 o->base.type = &mp_type_fun_native;
Damien George3c658a42014-08-24 16:28:17 +0100400 return o;
401}
402
403#endif // MICROPY_EMIT_NATIVE
404
405/******************************************************************************/
Damien George2ac4af62014-08-15 16:45:41 +0100406/* viper functions */
407
408#if MICROPY_EMIT_NATIVE
409
410typedef struct _mp_obj_fun_viper_t {
411 mp_obj_base_t base;
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100412 size_t n_args;
Damien George3c658a42014-08-24 16:28:17 +0100413 void *fun_data; // GC must be able to trace this pointer
Damien George2ac4af62014-08-15 16:45:41 +0100414 mp_uint_t type_sig;
415} mp_obj_fun_viper_t;
416
Damien Georgebbf5cd02015-01-12 22:45:35 +0000417typedef mp_uint_t (*viper_fun_0_t)(void);
Damien George2ac4af62014-08-15 16:45:41 +0100418typedef mp_uint_t (*viper_fun_1_t)(mp_uint_t);
419typedef mp_uint_t (*viper_fun_2_t)(mp_uint_t, mp_uint_t);
420typedef mp_uint_t (*viper_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damien Georgee45c1db2015-07-23 14:11:29 +0100421typedef 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 +0100422
Damien Georgea0c97812016-01-03 09:59:18 +0000423STATIC 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 +0100424 mp_obj_fun_viper_t *self = self_in;
425
426 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
427
Damien George3c658a42014-08-24 16:28:17 +0100428 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
429
Damien George2ac4af62014-08-15 16:45:41 +0100430 mp_uint_t ret;
431 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100432 ret = ((viper_fun_0_t)fun)();
Damien George2ac4af62014-08-15 16:45:41 +0100433 } else if (n_args == 1) {
Damien George5f3e0052016-02-02 23:16:05 +0000434 ret = ((viper_fun_1_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4));
Damien George2ac4af62014-08-15 16:45:41 +0100435 } else if (n_args == 2) {
Damien George5f3e0052016-02-02 23:16:05 +0000436 ret = ((viper_fun_2_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4), mp_convert_obj_to_native(args[1], self->type_sig >> 8));
Damien George2ac4af62014-08-15 16:45:41 +0100437 } else if (n_args == 3) {
Damien George5f3e0052016-02-02 23:16:05 +0000438 ret = ((viper_fun_3_t)fun)(mp_convert_obj_to_native(args[0], self->type_sig >> 4), mp_convert_obj_to_native(args[1], self->type_sig >> 8), mp_convert_obj_to_native(args[2], self->type_sig >> 12));
Damien Georgec71edae2016-09-27 23:05:51 +1000439 } else {
440 // compiler allows at most 4 arguments
441 assert(n_args == 4);
Damien Georgee45c1db2015-07-23 14:11:29 +0100442 ret = ((viper_fun_4_t)fun)(
Damien George5f3e0052016-02-02 23:16:05 +0000443 mp_convert_obj_to_native(args[0], self->type_sig >> 4),
444 mp_convert_obj_to_native(args[1], self->type_sig >> 8),
445 mp_convert_obj_to_native(args[2], self->type_sig >> 12),
446 mp_convert_obj_to_native(args[3], self->type_sig >> 16)
Damien Georgee45c1db2015-07-23 14:11:29 +0100447 );
Damien George2ac4af62014-08-15 16:45:41 +0100448 }
449
Damien Georgee6c0dff2014-08-15 23:47:59 +0100450 return mp_convert_native_to_obj(ret, self->type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100451}
452
453STATIC const mp_obj_type_t mp_type_fun_viper = {
454 { &mp_type_type },
455 .name = MP_QSTR_function,
456 .call = fun_viper_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000457 .unary_op = mp_generic_unary_op,
Damien George2ac4af62014-08-15 16:45:41 +0100458};
459
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100460mp_obj_t mp_obj_new_fun_viper(size_t n_args, void *fun_data, mp_uint_t type_sig) {
Damien George2ac4af62014-08-15 16:45:41 +0100461 mp_obj_fun_viper_t *o = m_new_obj(mp_obj_fun_viper_t);
462 o->base.type = &mp_type_fun_viper;
463 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100464 o->fun_data = fun_data;
Damien George2ac4af62014-08-15 16:45:41 +0100465 o->type_sig = type_sig;
466 return o;
467}
468
469#endif // MICROPY_EMIT_NATIVE
470
471/******************************************************************************/
Damiend99b0522013-12-21 18:17:45 +0000472/* inline assembler functions */
473
Damien Georgead297a12016-12-09 13:17:49 +1100474#if MICROPY_EMIT_INLINE_ASM
Damien George2ac4af62014-08-15 16:45:41 +0100475
Damiend99b0522013-12-21 18:17:45 +0000476typedef struct _mp_obj_fun_asm_t {
477 mp_obj_base_t base;
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100478 size_t n_args;
Damien George3c658a42014-08-24 16:28:17 +0100479 void *fun_data; // GC must be able to trace this pointer
Damien George8f54c082016-01-15 15:20:43 +0000480 mp_uint_t type_sig;
Damiend99b0522013-12-21 18:17:45 +0000481} mp_obj_fun_asm_t;
482
Damien Georgebbf5cd02015-01-12 22:45:35 +0000483typedef mp_uint_t (*inline_asm_fun_0_t)(void);
Damien George40f3c022014-07-03 13:25:24 +0100484typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
485typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
486typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
Damien George9a583162016-03-16 08:22:26 +0000487typedef mp_uint_t (*inline_asm_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
Damiend99b0522013-12-21 18:17:45 +0000488
489// convert a Micro Python object to a sensible value for inline asm
Damien George40f3c022014-07-03 13:25:24 +0100490STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
Damiend99b0522013-12-21 18:17:45 +0000491 // TODO for byte_array, pass pointer to the array
492 if (MP_OBJ_IS_SMALL_INT(obj)) {
493 return MP_OBJ_SMALL_INT_VALUE(obj);
494 } else if (obj == mp_const_none) {
495 return 0;
496 } else if (obj == mp_const_false) {
497 return 0;
498 } else if (obj == mp_const_true) {
499 return 1;
Damien George32f0b792015-02-13 10:43:05 +0000500 } else if (MP_OBJ_IS_TYPE(obj, &mp_type_int)) {
501 return mp_obj_int_get_truncated(obj);
Damien George5fa93b62014-01-22 14:35:10 +0000502 } else if (MP_OBJ_IS_STR(obj)) {
Damiend99b0522013-12-21 18:17:45 +0000503 // pointer to the string (it's probably constant though!)
Damien Georgea7329612014-09-29 16:26:39 +0100504 mp_uint_t l;
Damien George40f3c022014-07-03 13:25:24 +0100505 return (mp_uint_t)mp_obj_str_get_data(obj, &l);
Damiend99b0522013-12-21 18:17:45 +0000506 } else {
Damien George87210872014-04-13 00:30:32 +0100507 mp_obj_type_t *type = mp_obj_get_type(obj);
508 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100509#if MICROPY_PY_BUILTINS_FLOAT
Damien George87210872014-04-13 00:30:32 +0100510 } else if (type == &mp_type_float) {
511 // convert float to int (could also pass in float registers)
Damien George40f3c022014-07-03 13:25:24 +0100512 return (mp_int_t)mp_obj_float_get(obj);
Damien George87210872014-04-13 00:30:32 +0100513#endif
Krzysztof Blazewicz7e480e82017-03-04 12:29:20 +0100514 } else if (type == &mp_type_tuple || type == &mp_type_list) {
Damien George87210872014-04-13 00:30:32 +0100515 // pointer to start of tuple (could pass length, but then could use len(x) for that)
Damien Georgea7329612014-09-29 16:26:39 +0100516 mp_uint_t len;
Damien George87210872014-04-13 00:30:32 +0100517 mp_obj_t *items;
Krzysztof Blazewicz7e480e82017-03-04 12:29:20 +0100518 mp_obj_get_array(obj, &len, &items);
Damien George40f3c022014-07-03 13:25:24 +0100519 return (mp_uint_t)items;
Damien George87210872014-04-13 00:30:32 +0100520 } else {
Damien George57a4b4f2014-04-18 22:29:21 +0100521 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100522 if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
Damien George8a1cab92014-04-13 12:08:52 +0100523 // supports the buffer protocol, return a pointer to the data
Damien George40f3c022014-07-03 13:25:24 +0100524 return (mp_uint_t)bufinfo.buf;
Damien George8a1cab92014-04-13 12:08:52 +0100525 } else {
526 // just pass along a pointer to the object
Damien George40f3c022014-07-03 13:25:24 +0100527 return (mp_uint_t)obj;
Damien George8a1cab92014-04-13 12:08:52 +0100528 }
Damien George87210872014-04-13 00:30:32 +0100529 }
Damiend99b0522013-12-21 18:17:45 +0000530 }
531}
532
Damien Georgea0c97812016-01-03 09:59:18 +0000533STATIC 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 +0000534 mp_obj_fun_asm_t *self = self_in;
535
Damien Georgeccc85ea2014-05-10 13:40:46 +0100536 mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
Damiend99b0522013-12-21 18:17:45 +0000537
Damien George3c658a42014-08-24 16:28:17 +0100538 void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
539
Damien George40f3c022014-07-03 13:25:24 +0100540 mp_uint_t ret;
Damiend99b0522013-12-21 18:17:45 +0000541 if (n_args == 0) {
Damien George3c658a42014-08-24 16:28:17 +0100542 ret = ((inline_asm_fun_0_t)fun)();
Damiend99b0522013-12-21 18:17:45 +0000543 } else if (n_args == 1) {
Damien George3c658a42014-08-24 16:28:17 +0100544 ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
Damiend99b0522013-12-21 18:17:45 +0000545 } else if (n_args == 2) {
Damien George3c658a42014-08-24 16:28:17 +0100546 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 +0000547 } else if (n_args == 3) {
Damien George3c658a42014-08-24 16:28:17 +0100548 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 +0000549 } else {
Damien George9a583162016-03-16 08:22:26 +0000550 // compiler allows at most 4 arguments
551 assert(n_args == 4);
552 ret = ((inline_asm_fun_4_t)fun)(
553 convert_obj_for_inline_asm(args[0]),
554 convert_obj_for_inline_asm(args[1]),
555 convert_obj_for_inline_asm(args[2]),
556 convert_obj_for_inline_asm(args[3])
557 );
Damiend99b0522013-12-21 18:17:45 +0000558 }
559
Damien George8f54c082016-01-15 15:20:43 +0000560 return mp_convert_native_to_obj(ret, self->type_sig);
Damiend99b0522013-12-21 18:17:45 +0000561}
562
Damien George3e1a5c12014-03-29 13:43:38 +0000563STATIC const mp_obj_type_t mp_type_fun_asm = {
Damien Georgec5966122014-02-15 16:10:44 +0000564 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000565 .name = MP_QSTR_function,
Damien George20006db2014-01-18 14:10:48 +0000566 .call = fun_asm_call,
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000567 .unary_op = mp_generic_unary_op,
Damiend99b0522013-12-21 18:17:45 +0000568};
569
Damien Georgedbcdb9f2017-02-16 16:34:53 +1100570mp_obj_t mp_obj_new_fun_asm(size_t n_args, void *fun_data, mp_uint_t type_sig) {
Damiend99b0522013-12-21 18:17:45 +0000571 mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000572 o->base.type = &mp_type_fun_asm;
Damiend99b0522013-12-21 18:17:45 +0000573 o->n_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +0100574 o->fun_data = fun_data;
Damien George8f54c082016-01-15 15:20:43 +0000575 o->type_sig = type_sig;
Damiend99b0522013-12-21 18:17:45 +0000576 return o;
577}
Damien George2ac4af62014-08-15 16:45:41 +0100578
Damien Georgead297a12016-12-09 13:17:49 +1100579#endif // MICROPY_EMIT_INLINE_ASM