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