Branch data Line data Source code
1 : : /*
2 : : * This file is part of the MicroPython project, http://micropython.org/
3 : : *
4 : : * The MIT License (MIT)
5 : : *
6 : : * Copyright (c) 2013, 2014 Damien P. George
7 : : * Copyright (c) 2014 Paul Sokolovsky
8 : : *
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 : :
28 : : #include <string.h>
29 : : #include <assert.h>
30 : :
31 : : #include "py/objtuple.h"
32 : : #include "py/objfun.h"
33 : : #include "py/runtime.h"
34 : : #include "py/bc.h"
35 : : #include "py/stackctrl.h"
36 : :
37 : : #if MICROPY_DEBUG_VERBOSE // print debugging info
38 : : #define DEBUG_PRINT (1)
39 : : #else // don't print debugging info
40 : : #define DEBUG_PRINT (0)
41 : : #define DEBUG_printf(...) (void)0
42 : : #endif
43 : :
44 : : // Note: the "name" entry in mp_obj_type_t for a function type must be
45 : : // MP_QSTR_function because it is used to determine if an object is of generic
46 : : // function type.
47 : :
48 : : /******************************************************************************/
49 : : /* builtin functions */
50 : :
51 : 315262 : STATIC 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) {
52 : 315262 : (void)args;
53 [ + - - + ]: 315262 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_0));
54 : 315262 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
55 : 315262 : mp_arg_check_num(n_args, n_kw, 0, 0, false);
56 : 315262 : return self->fun._0();
57 : : }
58 : :
59 : : MP_DEFINE_CONST_OBJ_TYPE(
60 : : mp_type_fun_builtin_0, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
61 : : call, fun_builtin_0_call
62 : : );
63 : :
64 : 525799 : STATIC 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) {
65 [ + - - + ]: 525799 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_1));
66 : 525799 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
67 : 525799 : mp_arg_check_num(n_args, n_kw, 1, 1, false);
68 : 525799 : return self->fun._1(args[0]);
69 : : }
70 : :
71 : : MP_DEFINE_CONST_OBJ_TYPE(
72 : : mp_type_fun_builtin_1, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
73 : : call, fun_builtin_1_call
74 : : );
75 : :
76 : 504366 : STATIC 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) {
77 [ + - - + ]: 504366 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_2));
78 : 504366 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
79 : 504366 : mp_arg_check_num(n_args, n_kw, 2, 2, false);
80 : 504354 : return self->fun._2(args[0], args[1]);
81 : : }
82 : :
83 : : MP_DEFINE_CONST_OBJ_TYPE(
84 : : mp_type_fun_builtin_2, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
85 : : call, fun_builtin_2_call
86 : : );
87 : :
88 : 7350 : STATIC 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) {
89 [ + - - + ]: 7350 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_3));
90 : 7350 : mp_obj_fun_builtin_fixed_t *self = MP_OBJ_TO_PTR(self_in);
91 : 7350 : mp_arg_check_num(n_args, n_kw, 3, 3, false);
92 : 7348 : return self->fun._3(args[0], args[1], args[2]);
93 : : }
94 : :
95 : : MP_DEFINE_CONST_OBJ_TYPE(
96 : : mp_type_fun_builtin_3, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
97 : : call, fun_builtin_3_call
98 : : );
99 : :
100 : 953789 : STATIC 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) {
101 [ + - - + ]: 953789 : assert(mp_obj_is_type(self_in, &mp_type_fun_builtin_var));
102 : 953789 : mp_obj_fun_builtin_var_t *self = MP_OBJ_TO_PTR(self_in);
103 : :
104 : : // check number of arguments
105 : 953789 : mp_arg_check_num_sig(n_args, n_kw, self->sig);
106 : :
107 [ + + ]: 953781 : if (self->sig & 1) {
108 : : // function allows keywords
109 : :
110 : : // we create a map directly from the given args array
111 : 492935 : mp_map_t kw_args;
112 : 492935 : mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
113 : :
114 : 492935 : return self->fun.kw(n_args, args, &kw_args);
115 : :
116 : : } else {
117 : : // function takes a variable number of arguments, but no keywords
118 : :
119 : 460846 : return self->fun.var(n_args, args);
120 : : }
121 : : }
122 : :
123 : : MP_DEFINE_CONST_OBJ_TYPE(
124 : : mp_type_fun_builtin_var, MP_QSTR_function, MP_TYPE_FLAG_BINDS_SELF | MP_TYPE_FLAG_BUILTIN_FUN,
125 : : call, fun_builtin_var_call
126 : : );
127 : :
128 : : /******************************************************************************/
129 : : /* byte code functions */
130 : :
131 : 31 : STATIC qstr mp_obj_code_get_name(const mp_obj_fun_bc_t *fun, const byte *code_info) {
132 [ - + ]: 31 : MP_BC_PRELUDE_SIZE_DECODE(code_info);
133 : 31 : mp_uint_t name = mp_decode_uint_value(code_info);
134 : : #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
135 : 31 : name = fun->context->constants.qstr_table[name];
136 : : #endif
137 : 31 : return name;
138 : : }
139 : :
140 : : #if MICROPY_EMIT_NATIVE
141 : : STATIC const mp_obj_type_t mp_type_fun_native;
142 : : #endif
143 : :
144 : 47 : qstr mp_obj_fun_get_name(mp_const_obj_t fun_in) {
145 : 47 : const mp_obj_fun_bc_t *fun = MP_OBJ_TO_PTR(fun_in);
146 : : #if MICROPY_EMIT_NATIVE
147 [ + + + + ]: 47 : if (fun->base.type == &mp_type_fun_native || fun->base.type == &mp_type_native_gen_wrap) {
148 : : // TODO native functions don't have name stored
149 : : return MP_QSTR_;
150 : : }
151 : : #endif
152 : :
153 : 31 : const byte *bc = fun->bytecode;
154 [ + + ]: 47 : MP_BC_PRELUDE_SIG_DECODE(bc);
155 : 31 : return mp_obj_code_get_name(fun, bc);
156 : : }
157 : :
158 : : #if MICROPY_CPYTHON_COMPAT
159 : 5 : STATIC void fun_bc_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
160 : 5 : (void)kind;
161 : 5 : mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(o_in);
162 : 5 : mp_printf(print, "<function %q at 0x%p>", mp_obj_fun_get_name(o_in), o);
163 : 5 : }
164 : : #endif
165 : :
166 : : #if DEBUG_PRINT
167 : : STATIC void dump_args(const mp_obj_t *a, size_t sz) {
168 : : DEBUG_printf("%p: ", a);
169 : : for (size_t i = 0; i < sz; i++) {
170 : : DEBUG_printf("%p ", a[i]);
171 : : }
172 : : DEBUG_printf("\n");
173 : : }
174 : : #else
175 : : #define dump_args(...) (void)0
176 : : #endif
177 : :
178 : : // With this macro you can tune the maximum number of function state bytes
179 : : // that will be allocated on the stack. Any function that needs more
180 : : // than this will try to use the heap, with fallback to stack allocation.
181 : : #define VM_MAX_STATE_ON_STACK (sizeof(mp_uint_t) * 11)
182 : :
183 : : #define DECODE_CODESTATE_SIZE(bytecode, n_state_out_var, state_size_out_var) \
184 : : { \
185 : : const uint8_t *ip = bytecode; \
186 : : size_t n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_args; \
187 : : MP_BC_PRELUDE_SIG_DECODE_INTO(ip, n_state_out_var, n_exc_stack, scope_flags, n_pos_args, n_kwonly_args, n_def_args); \
188 : : (void)scope_flags; (void)n_pos_args; (void)n_kwonly_args; (void)n_def_args; \
189 : : \
190 : : /* state size in bytes */ \
191 : : state_size_out_var = n_state_out_var * sizeof(mp_obj_t) \
192 : : + n_exc_stack * sizeof(mp_exc_stack_t); \
193 : : }
194 : :
195 : : #define INIT_CODESTATE(code_state, _fun_bc, _n_state, n_args, n_kw, args) \
196 : : code_state->fun_bc = _fun_bc; \
197 : : code_state->n_state = _n_state; \
198 : : mp_setup_code_state(code_state, n_args, n_kw, args); \
199 : : code_state->old_globals = mp_globals_get();
200 : :
201 : : #if MICROPY_STACKLESS
202 : : mp_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) {
203 : : MP_STACK_CHECK();
204 : : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
205 : :
206 : : size_t n_state, state_size;
207 : : DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
208 : :
209 : : mp_code_state_t *code_state;
210 : : #if MICROPY_ENABLE_PYSTACK
211 : : code_state = mp_pystack_alloc(sizeof(mp_code_state_t) + state_size);
212 : : #else
213 : : // If we use m_new_obj_var(), then on no memory, MemoryError will be
214 : : // raised. But this is not correct exception for a function call,
215 : : // RuntimeError should be raised instead. So, we use m_new_obj_var_maybe(),
216 : : // return NULL, then vm.c takes the needed action (either raise
217 : : // RuntimeError or fallback to stack allocation).
218 : : code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
219 : : if (!code_state) {
220 : : return NULL;
221 : : }
222 : : #endif
223 : :
224 : : INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
225 : :
226 : : // execute the byte code with the correct globals context
227 : : mp_globals_set(self->context->module.globals);
228 : :
229 : : return code_state;
230 : : }
231 : : #endif
232 : :
233 : 7246678 : STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
234 : 7246678 : MP_STACK_CHECK();
235 : :
236 : 8047853 : DEBUG_printf("Input n_args: " UINT_FMT ", n_kw: " UINT_FMT "\n", n_args, n_kw);
237 : 8047853 : DEBUG_printf("Input pos args: ");
238 : 8047853 : dump_args(args, n_args);
239 : 8047853 : DEBUG_printf("Input kw args: ");
240 : 8047853 : dump_args(args + n_args, n_kw * 2);
241 : :
242 : 8047853 : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
243 : :
244 : 8047853 : size_t n_state, state_size;
245 [ + + ]: 8318979 : DECODE_CODESTATE_SIZE(self->bytecode, n_state, state_size);
246 : :
247 : : // allocate state for locals and stack
248 : 8047853 : mp_code_state_t *code_state = NULL;
249 : : #if MICROPY_ENABLE_PYSTACK
250 : : code_state = mp_pystack_alloc(offsetof(mp_code_state_t, state) + state_size);
251 : : #else
252 [ + + ]: 8047853 : if (state_size > VM_MAX_STATE_ON_STACK) {
253 : 250859 : code_state = m_new_obj_var_maybe(mp_code_state_t, state, byte, state_size);
254 : : #if MICROPY_DEBUG_VM_STACK_OVERFLOW
255 : : if (code_state != NULL) {
256 : : memset(code_state->state, 0, state_size);
257 : : }
258 : : #endif
259 : : }
260 [ + + ]: 250643 : if (code_state == NULL) {
261 : 7797010 : code_state = alloca(offsetof(mp_code_state_t, state) + state_size);
262 : : #if MICROPY_DEBUG_VM_STACK_OVERFLOW
263 : : memset(code_state->state, 0, state_size);
264 : : #endif
265 : 7797010 : state_size = 0; // indicate that we allocated using alloca
266 : : }
267 : : #endif
268 : :
269 : 8047637 : INIT_CODESTATE(code_state, self, n_state, n_args, n_kw, args);
270 : :
271 : : // execute the byte code with the correct globals context
272 : 8030877 : mp_globals_set(self->context->module.globals);
273 : 7423572 : mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(code_state, MP_OBJ_NULL);
274 : 7927970 : mp_globals_set(code_state->old_globals);
275 : :
276 : : #if MICROPY_DEBUG_VM_STACK_OVERFLOW
277 : : if (vm_return_kind == MP_VM_RETURN_NORMAL) {
278 : : if (code_state->sp < code_state->state) {
279 : : mp_printf(MICROPY_DEBUG_PRINTER, "VM stack underflow: " INT_FMT "\n", code_state->sp - code_state->state);
280 : : assert(0);
281 : : }
282 : : }
283 : : const byte *bytecode_ptr = self->bytecode;
284 : : size_t n_state_unused, n_exc_stack_unused, scope_flags_unused;
285 : : size_t n_pos_args, n_kwonly_args, n_def_args_unused;
286 : : MP_BC_PRELUDE_SIG_DECODE_INTO(bytecode_ptr, n_state_unused, n_exc_stack_unused,
287 : : scope_flags_unused, n_pos_args, n_kwonly_args, n_def_args_unused);
288 : : // We can't check the case when an exception is returned in state[0]
289 : : // and there are no arguments, because in this case our detection slot may have
290 : : // been overwritten by the returned exception (which is allowed).
291 : : if (!(vm_return_kind == MP_VM_RETURN_EXCEPTION && n_pos_args + n_kwonly_args == 0)) {
292 : : // Just check to see that we have at least 1 null object left in the state.
293 : : bool overflow = true;
294 : : for (size_t i = 0; i < n_state - n_pos_args - n_kwonly_args; ++i) {
295 : : if (code_state->state[i] == MP_OBJ_NULL) {
296 : : overflow = false;
297 : : break;
298 : : }
299 : : }
300 : : if (overflow) {
301 : : mp_printf(MICROPY_DEBUG_PRINTER, "VM stack overflow state=%p n_state+1=" UINT_FMT "\n", code_state->state, n_state);
302 : : assert(0);
303 : : }
304 : : }
305 : : #endif
306 : :
307 : 8088685 : mp_obj_t result;
308 [ + + ]: 8088685 : if (vm_return_kind == MP_VM_RETURN_NORMAL) {
309 : : // return value is in *sp
310 : 8024890 : result = *code_state->sp;
311 : : } else {
312 : : // must be an exception because normal functions can't yield
313 [ - + ]: 63795 : assert(vm_return_kind == MP_VM_RETURN_EXCEPTION);
314 : : // returned exception is in state[0]
315 : 63795 : result = code_state->state[0];
316 : : }
317 : :
318 : : #if MICROPY_ENABLE_PYSTACK
319 : : mp_pystack_free(code_state);
320 : : #else
321 : : // free the state if it was allocated on the heap
322 [ + + ]: 8088685 : if (state_size != 0) {
323 : 250811 : m_del_var(mp_code_state_t, state, byte, state_size, code_state);
324 : : }
325 : : #endif
326 : :
327 [ + + ]: 8088180 : if (vm_return_kind == MP_VM_RETURN_NORMAL) {
328 : 8024385 : return result;
329 : : } else { // MP_VM_RETURN_EXCEPTION
330 : 63795 : nlr_raise(result);
331 : : }
332 : : }
333 : :
334 : : #if MICROPY_PY_FUNCTION_ATTRS
335 : 8993 : void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
336 [ + + ]: 8993 : if (dest[0] != MP_OBJ_NULL) {
337 : : // not load attribute
338 : : return;
339 : : }
340 [ + + ]: 8983 : if (attr == MP_QSTR___name__) {
341 : 26 : dest[0] = MP_OBJ_NEW_QSTR(mp_obj_fun_get_name(self_in));
342 : : }
343 [ + + ]: 8983 : if (attr == MP_QSTR___globals__) {
344 : 32 : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
345 : 32 : dest[0] = MP_OBJ_FROM_PTR(self->context->module.globals);
346 : : }
347 : : }
348 : : #endif
349 : :
350 : : #if MICROPY_CPYTHON_COMPAT
351 : : #define FUN_BC_TYPE_PRINT print, fun_bc_print,
352 : : #else
353 : : #define FUN_BC_TYPE_PRINT
354 : : #endif
355 : :
356 : : #if MICROPY_PY_FUNCTION_ATTRS
357 : : #define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr,
358 : : #else
359 : : #define FUN_BC_TYPE_ATTR
360 : : #endif
361 : :
362 : : MP_DEFINE_CONST_OBJ_TYPE(
363 : : mp_type_fun_bc,
364 : : MP_QSTR_function,
365 : : MP_TYPE_FLAG_BINDS_SELF,
366 : : FUN_BC_TYPE_PRINT
367 : : FUN_BC_TYPE_ATTR
368 : : call, fun_bc_call
369 : : );
370 : :
371 : 17660 : mp_obj_t mp_obj_new_fun_bc(const mp_obj_t *def_args, const byte *code, const mp_module_context_t *context, struct _mp_raw_code_t *const *child_table) {
372 : 17660 : size_t n_def_args = 0;
373 : 17660 : size_t n_extra_args = 0;
374 : 17660 : mp_obj_tuple_t *def_pos_args = NULL;
375 : 17660 : mp_obj_t def_kw_args = MP_OBJ_NULL;
376 [ + + + + ]: 17660 : if (def_args != NULL && def_args[0] != MP_OBJ_NULL) {
377 [ - + - + : 287 : assert(mp_obj_is_type(def_args[0], &mp_type_tuple));
- + - + +
- - + ]
378 : 287 : def_pos_args = MP_OBJ_TO_PTR(def_args[0]);
379 : 287 : n_def_args = def_pos_args->len;
380 : 287 : n_extra_args = def_pos_args->len;
381 : : }
382 [ + + + + ]: 17660 : if (def_args != NULL && def_args[1] != MP_OBJ_NULL) {
383 [ - + - + : 37 : assert(mp_obj_is_type(def_args[1], &mp_type_dict));
- + - + +
- - + ]
384 : 37 : def_kw_args = def_args[1];
385 : 37 : n_extra_args += 1;
386 : : }
387 : 17660 : mp_obj_fun_bc_t *o = mp_obj_malloc_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args, &mp_type_fun_bc);
388 : 17661 : o->bytecode = code;
389 : 17661 : o->context = context;
390 : 17661 : o->child_table = child_table;
391 [ + + ]: 17661 : if (def_pos_args != NULL) {
392 : 287 : memcpy(o->extra_args, def_pos_args->items, n_def_args * sizeof(mp_obj_t));
393 : : }
394 [ + + ]: 17661 : if (def_kw_args != MP_OBJ_NULL) {
395 : 37 : o->extra_args[n_def_args] = def_kw_args;
396 : : }
397 : 17661 : return MP_OBJ_FROM_PTR(o);
398 : : }
399 : :
400 : : /******************************************************************************/
401 : : /* native functions */
402 : :
403 : : #if MICROPY_EMIT_NATIVE
404 : :
405 : 572769 : STATIC mp_obj_t fun_native_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
406 : 572769 : MP_STACK_CHECK();
407 : 572766 : mp_obj_fun_bc_t *self = MP_OBJ_TO_PTR(self_in);
408 : 572766 : mp_call_fun_t fun = MICROPY_MAKE_POINTER_CALLABLE((void *)self->bytecode);
409 : 572766 : return fun(self_in, n_args, n_kw, args);
410 : : }
411 : :
412 : : #if MICROPY_CPYTHON_COMPAT
413 : : #define FUN_BC_TYPE_PRINT print, fun_bc_print,
414 : : #else
415 : : #define FUN_BC_TYPE_PRINT
416 : : #endif
417 : : #if MICROPY_PY_FUNCTION_ATTRS
418 : : #define FUN_BC_TYPE_ATTR attr, mp_obj_fun_bc_attr,
419 : : #else
420 : : #define FUN_BC_TYPE_ATTR
421 : : #endif
422 : :
423 : : STATIC MP_DEFINE_CONST_OBJ_TYPE(
424 : : mp_type_fun_native,
425 : : MP_QSTR_function,
426 : : MP_TYPE_FLAG_BINDS_SELF,
427 : : FUN_BC_TYPE_PRINT
428 : : FUN_BC_TYPE_ATTR
429 : : call, fun_native_call
430 : : );
431 : :
432 : 8423 : mp_obj_t mp_obj_new_fun_native(const mp_obj_t *def_args, const void *fun_data, const mp_module_context_t *mc, struct _mp_raw_code_t *const *child_table) {
433 : 8423 : mp_obj_fun_bc_t *o = MP_OBJ_TO_PTR(mp_obj_new_fun_bc(def_args, (const byte *)fun_data, mc, child_table));
434 : 8423 : o->base.type = &mp_type_fun_native;
435 : 8423 : return MP_OBJ_FROM_PTR(o);
436 : : }
437 : :
438 : : #endif // MICROPY_EMIT_NATIVE
439 : :
440 : : /******************************************************************************/
441 : : /* inline assembler functions */
442 : :
443 : : #if MICROPY_EMIT_INLINE_ASM
444 : :
445 : : typedef struct _mp_obj_fun_asm_t {
446 : : mp_obj_base_t base;
447 : : size_t n_args;
448 : : const void *fun_data; // GC must be able to trace this pointer
449 : : mp_uint_t type_sig;
450 : : } mp_obj_fun_asm_t;
451 : :
452 : : typedef mp_uint_t (*inline_asm_fun_0_t)(void);
453 : : typedef mp_uint_t (*inline_asm_fun_1_t)(mp_uint_t);
454 : : typedef mp_uint_t (*inline_asm_fun_2_t)(mp_uint_t, mp_uint_t);
455 : : typedef mp_uint_t (*inline_asm_fun_3_t)(mp_uint_t, mp_uint_t, mp_uint_t);
456 : : typedef mp_uint_t (*inline_asm_fun_4_t)(mp_uint_t, mp_uint_t, mp_uint_t, mp_uint_t);
457 : :
458 : : // convert a MicroPython object to a sensible value for inline asm
459 : : STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
460 : : // TODO for byte_array, pass pointer to the array
461 : : if (mp_obj_is_small_int(obj)) {
462 : : return MP_OBJ_SMALL_INT_VALUE(obj);
463 : : } else if (obj == mp_const_none) {
464 : : return 0;
465 : : } else if (obj == mp_const_false) {
466 : : return 0;
467 : : } else if (obj == mp_const_true) {
468 : : return 1;
469 : : } else if (mp_obj_is_exact_type(obj, &mp_type_int)) {
470 : : return mp_obj_int_get_truncated(obj);
471 : : } else if (mp_obj_is_str(obj)) {
472 : : // pointer to the string (it's probably constant though!)
473 : : size_t l;
474 : : return (mp_uint_t)mp_obj_str_get_data(obj, &l);
475 : : } else {
476 : : const mp_obj_type_t *type = mp_obj_get_type(obj);
477 : : #if MICROPY_PY_BUILTINS_FLOAT
478 : : if (type == &mp_type_float) {
479 : : // convert float to int (could also pass in float registers)
480 : : return (mp_int_t)mp_obj_float_get(obj);
481 : : }
482 : : #endif
483 : : if (type == &mp_type_tuple || type == &mp_type_list) {
484 : : // pointer to start of tuple (could pass length, but then could use len(x) for that)
485 : : size_t len;
486 : : mp_obj_t *items;
487 : : mp_obj_get_array(obj, &len, &items);
488 : : return (mp_uint_t)items;
489 : : } else {
490 : : mp_buffer_info_t bufinfo;
491 : : if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
492 : : // supports the buffer protocol, return a pointer to the data
493 : : return (mp_uint_t)bufinfo.buf;
494 : : } else {
495 : : // just pass along a pointer to the object
496 : : return (mp_uint_t)obj;
497 : : }
498 : : }
499 : : }
500 : : }
501 : :
502 : : STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
503 : : mp_obj_fun_asm_t *self = MP_OBJ_TO_PTR(self_in);
504 : :
505 : : mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
506 : :
507 : : const void *fun = MICROPY_MAKE_POINTER_CALLABLE(self->fun_data);
508 : :
509 : : mp_uint_t ret;
510 : : if (n_args == 0) {
511 : : ret = ((inline_asm_fun_0_t)fun)();
512 : : } else if (n_args == 1) {
513 : : ret = ((inline_asm_fun_1_t)fun)(convert_obj_for_inline_asm(args[0]));
514 : : } else if (n_args == 2) {
515 : : ret = ((inline_asm_fun_2_t)fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
516 : : } else if (n_args == 3) {
517 : : 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]));
518 : : } else {
519 : : // compiler allows at most 4 arguments
520 : : assert(n_args == 4);
521 : : ret = ((inline_asm_fun_4_t)fun)(
522 : : convert_obj_for_inline_asm(args[0]),
523 : : convert_obj_for_inline_asm(args[1]),
524 : : convert_obj_for_inline_asm(args[2]),
525 : : convert_obj_for_inline_asm(args[3])
526 : : );
527 : : }
528 : :
529 : : return mp_native_to_obj(ret, self->type_sig);
530 : : }
531 : :
532 : : STATIC MP_DEFINE_CONST_OBJ_TYPE(
533 : : mp_type_fun_asm,
534 : : MP_QSTR_function,
535 : : MP_TYPE_FLAG_BINDS_SELF,
536 : : call, fun_asm_call
537 : : );
538 : :
539 : : mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig) {
540 : : mp_obj_fun_asm_t *o = mp_obj_malloc(mp_obj_fun_asm_t, &mp_type_fun_asm);
541 : : o->n_args = n_args;
542 : : o->fun_data = fun_data;
543 : : o->type_sig = type_sig;
544 : : return MP_OBJ_FROM_PTR(o);
545 : : }
546 : :
547 : : #endif // MICROPY_EMIT_INLINE_ASM
|