objfun.c 20 KB
Newer Older
1
2
3
4
5
6
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
7
 * Copyright (c) 2014 Paul Sokolovsky
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

xbe's avatar
xbe committed
28
#include <stdbool.h>
29
30
#include <string.h>
#include <assert.h>
stijn's avatar
stijn committed
31
#include <alloca.h>
32

33
#include "mpconfig.h"
34
35
#include "nlr.h"
#include "misc.h"
36
#include "qstr.h"
37
#include "obj.h"
38
#include "objtuple.h"
Paul Sokolovsky's avatar
Paul Sokolovsky committed
39
#include "objfun.h"
Damien George's avatar
Damien George committed
40
#include "runtime0.h"
41
42
43
#include "runtime.h"
#include "bc.h"

44
45
46
#if 0 // print debugging info
#define DEBUG_PRINT (1)
#else // don't print debugging info
47
#define DEBUG_printf(...) (void)0
48
49
#endif

50
51
52
53
54
/******************************************************************************/
/* native functions                                                           */

// mp_obj_fun_native_t defined in obj.h

55
56
57
58
59
60
61
STATIC mp_obj_t fun_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    switch (op) {
        case MP_BINARY_OP_EQUAL:
            // These objects can be equal only if it's the same underlying structure,
            // we don't even need to check for 2nd arg type.
            return MP_BOOL(lhs_in == rhs_in);
    }
62
    return MP_OBJ_NULL; // op not supported
63
64
}

65
STATIC mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
66
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_fun_native));
67
    mp_obj_fun_native_t *self = self_in;
68

69
    // check number of arguments
70
    mp_arg_check_num(n_args, n_kw, self->n_args_min, self->n_args_max, self->is_kw);
71

72
    if (self->is_kw) {
73
74
        // function allows keywords

75
76
77
        // we create a map directly from the given args array
        mp_map_t kw_args;
        mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
78

79
        return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args);
80

81
    } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) {
82
83
84
85
86
87
88
89
90
91
92
        // function requires a fixed number of arguments

        // dispatch function call
        switch (self->n_args_min) {
            case 0:
                return ((mp_fun_0_t)self->fun)();

            case 1:
                return ((mp_fun_1_t)self->fun)(args[0]);

            case 2:
93
                return ((mp_fun_2_t)self->fun)(args[0], args[1]);
94

95
            case 3:
96
                return ((mp_fun_3_t)self->fun)(args[0], args[1], args[2]);
97

98
99
100
101
102
103
            default:
                assert(0);
                return mp_const_none;
        }

    } else {
104
        // function takes a variable number of arguments, but no keywords
105

106
        return ((mp_fun_var_t)self->fun)(n_args, args);
107
108
109
    }
}

110
const mp_obj_type_t mp_type_fun_native = {
111
    { &mp_type_type },
112
    .name = MP_QSTR_function,
113
    .call = fun_native_call,
114
    .binary_op = fun_binary_op,
115
116
};

117
// fun must have the correct signature for n_args fixed arguments
Damien George's avatar
Damien George committed
118
mp_obj_t mp_make_function_n(int n_args, void *fun) {
119
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
120
    o->base.type = &mp_type_fun_native;
121
    o->is_kw = false;
122
123
    o->n_args_min = n_args;
    o->n_args_max = n_args;
124
125
126
127
    o->fun = fun;
    return o;
}

Damien George's avatar
Damien George committed
128
mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun) {
129
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
130
    o->base.type = &mp_type_fun_native;
131
    o->is_kw = false;
132
    o->n_args_min = n_args_min;
133
    o->n_args_max = MP_OBJ_FUN_ARGS_MAX;
134
135
136
137
138
    o->fun = fun;
    return o;
}

// min and max are inclusive
Damien George's avatar
Damien George committed
139
mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) {
140
    mp_obj_fun_native_t *o = m_new_obj(mp_obj_fun_native_t);
141
    o->base.type = &mp_type_fun_native;
142
    o->is_kw = false;
143
144
145
146
147
148
149
150
151
    o->n_args_min = n_args_min;
    o->n_args_max = n_args_max;
    o->fun = fun;
    return o;
}

/******************************************************************************/
/* byte code functions                                                        */

152
const char *mp_obj_code_get_name(const byte *code_info) {
153
154
155
156
    qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
    return qstr_str(block_name);
}

Paul Sokolovsky's avatar
Paul Sokolovsky committed
157
158
const char *mp_obj_fun_get_name(mp_const_obj_t fun_in) {
    const mp_obj_fun_bc_t *fun = fun_in;
159
    const byte *code_info = fun->bytecode;
160
161
162
    return mp_obj_code_get_name(code_info);
}

163
164
165
166
167
168
169
#if MICROPY_CPYTHON_COMPAT
STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
    mp_obj_fun_bc_t *o = o_in;
    print(env, "<function %s at 0x%x>", mp_obj_fun_get_name(o), o);
}
#endif

170
#if DEBUG_PRINT
171
STATIC void dump_args(const mp_obj_t *a, int sz) {
172
173
174
175
176
177
    DEBUG_printf("%p: ", a);
    for (int i = 0; i < sz; i++) {
        DEBUG_printf("%p ", a[i]);
    }
    DEBUG_printf("\n");
}
178
179
180
#else
#define dump_args(...) (void)0
#endif
181

182
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, uint given) {
183
184
185
186
187
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
    // Generic message, to be reused for other argument issues
    nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
        "argument num/types mismatch"));
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
188
189
    nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
        "function takes %d positional arguments but %d were given", expected, given));
190
191
192
193
194
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
    nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
        "%s() takes %d positional arguments but %d were given",
        mp_obj_fun_get_name(f), expected, given));
#endif
195
196
}

197
198
// If it's possible to call a function without allocating new argument array,
// this function returns true, together with pointers to 2 subarrays to be used
199
// as arguments. Otherwise, it returns false. It is expected that this function
200
201
202
203
204
// will be accompanied by another, mp_obj_fun_prepare_full_args(), which will
// instead take pointer to full-length out-array, and will fill it in. Rationale
// being that a caller can try this function and if it succeeds, the function call
// can be made without allocating extra memory. Otherwise, caller can allocate memory
// and try "full" function. These functions are expected to be refactoring of
205
// code in fun_bc_call() and eventually replace it.
206
207
208
bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args,
                            uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2) {
    mp_obj_fun_bc_t *self = self_in;
Paul Sokolovsky's avatar
Paul Sokolovsky committed
209
210
    DEBUG_printf("mp_obj_fun_prepare_simple_args: given: %d pos, %d kw, expected: %d pos (%d default)\n",
        n_args, n_kw, self->n_pos_args, self->n_def_args);
211
212

    assert(n_kw == 0);
213
    assert(self->n_kwonly_args == 0);
214
215
216
217
218
219
    assert(self->takes_var_args == 0);
    assert(self->takes_kw_args == 0);

    mp_obj_t *extra_args = self->extra_args + self->n_def_args;
    uint n_extra_args = 0;

220
221
    if (n_args > self->n_pos_args) {
        goto arg_error;
222
    } else {
223
224
225
226
227
228
        if (n_args >= self->n_pos_args - self->n_def_args) {
            extra_args -= self->n_pos_args - n_args;
            n_extra_args += self->n_pos_args - n_args;
        } else {
            fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args);
        }
229
230
231
232
233
234
235
236
    }
    *out_args1 = args;
    *out_args1_len = n_args;
    *out_args2 = extra_args;
    *out_args2_len = n_extra_args;
    return true;

arg_error:
237
    fun_pos_args_mismatch(self, self->n_pos_args, n_args);
238
239
}

240
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
241
242
243
244
245
    // This function is pretty complicated.  It's main aim is to be efficient in speed and RAM
    // usage for the common case of positional only args.
    //
    // extra_args layout: def_args, var_arg tuple, kwonly args, var_kw dict

246
247
    DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
    DEBUG_printf("Input pos args: ");
248
    dump_args(args, n_args);
249
250
    DEBUG_printf("Input kw args: ");
    dump_args(args + n_args, n_kw * 2);
251
    mp_obj_fun_bc_t *self = self_in;
252
    DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
253

254
    const mp_obj_t *kwargs = args + n_args;
Damien George's avatar
Damien George committed
255
256
257
    mp_obj_t *extra_args = self->extra_args + self->n_def_args;
    uint n_extra_args = 0;

258
259
    // check positional arguments

260
    if (n_args > self->n_pos_args) {
Damien George's avatar
Damien George committed
261
262
        // given more than enough arguments
        if (!self->takes_var_args) {
263
            fun_pos_args_mismatch(self, self->n_pos_args, n_args);
Damien George's avatar
Damien George committed
264
265
        }
        // put extra arguments in varargs tuple
266
        *extra_args = mp_obj_new_tuple(n_args - self->n_pos_args, args + self->n_pos_args);
Damien George's avatar
Damien George committed
267
        n_extra_args = 1;
268
        n_args = self->n_pos_args;
269
    } else {
Damien George's avatar
Damien George committed
270
        if (self->takes_var_args) {
271
            DEBUG_printf("passing empty tuple as *args\n");
Damien George's avatar
Damien George committed
272
273
274
            *extra_args = mp_const_empty_tuple;
            n_extra_args = 1;
        }
275
276
277
        // Apply processing and check below only if we don't have kwargs,
        // otherwise, kw handling code below has own extensive checks.
        if (n_kw == 0) {
278
            if (n_args >= self->n_pos_args - self->n_def_args) {
279
                // given enough arguments, but may need to use some default arguments
280
281
                extra_args -= self->n_pos_args - n_args;
                n_extra_args += self->n_pos_args - n_args;
282
            } else {
283
                fun_pos_args_mismatch(self, self->n_pos_args - self->n_def_args, n_args);
284
285
            }
        }
286
    }
Damien George's avatar
Damien George committed
287

288
289
    // check keyword arguments

290
    if (n_kw != 0) {
291
292
293
294
295
        // We cannot use dynamically-sized array here, because GCC indeed
        // deallocates it on leaving defining scope (unlike most static stack allocs).
        // So, we have 2 choices: allocate it unconditionally at the top of function
        // (wastes stack), or use alloca which is guaranteed to dealloc on func exit.
        //mp_obj_t flat_args[self->n_args];
296
297
        mp_obj_t *flat_args = alloca((self->n_pos_args + self->n_kwonly_args) * sizeof(mp_obj_t));
        for (int i = self->n_pos_args + self->n_kwonly_args - 1; i >= 0; i--) {
298
299
300
301
            flat_args[i] = MP_OBJ_NULL;
        }
        memcpy(flat_args, args, sizeof(*args) * n_args);
        DEBUG_printf("Initial args: ");
302
        dump_args(flat_args, self->n_pos_args + self->n_kwonly_args);
303
304
305
306

        mp_obj_t dict = MP_OBJ_NULL;
        if (self->takes_kw_args) {
            dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
307
308
        }
        for (uint i = 0; i < n_kw; i++) {
309
            qstr arg_name = MP_OBJ_QSTR_VALUE(kwargs[2 * i]);
310
            for (uint j = 0; j < self->n_pos_args + self->n_kwonly_args; j++) {
311
312
                if (arg_name == self->args[j]) {
                    if (flat_args[j] != MP_OBJ_NULL) {
313
                        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
314
315
316
317
318
319
320
321
                            "function got multiple values for argument '%s'", qstr_str(arg_name)));
                    }
                    flat_args[j] = kwargs[2 * i + 1];
                    goto continue2;
                }
            }
            // Didn't find name match with positional args
            if (!self->takes_kw_args) {
322
                nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
323
            }
324
            mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
325
326
327
continue2:;
        }
        DEBUG_printf("Args with kws flattened: ");
328
        dump_args(flat_args, self->n_pos_args + self->n_kwonly_args);
329

330
331
        // Now fill in defaults for positional args
        mp_obj_t *d = &flat_args[self->n_pos_args - 1];
332
        mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
333
        for (int i = self->n_def_args; i > 0; i--, d--, s--) {
334
            if (*d == MP_OBJ_NULL) {
335
                *d = *s;
336
337
338
            }
        }
        DEBUG_printf("Args after filling defaults: ");
339
        dump_args(flat_args, self->n_pos_args + self->n_kwonly_args);
340

341
        // Check that all mandatory positional args are specified
342
343
        while (d >= flat_args) {
            if (*d-- == MP_OBJ_NULL) {
344
                nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
345
346
347
348
                    "function missing required positional argument #%d", d - flat_args));
            }
        }

349
350
351
352
353
354
355
356
        // Check that all mandatory keyword args are specified
        for (int i = 0; i < self->n_kwonly_args; i++) {
            if (flat_args[self->n_pos_args + i] == MP_OBJ_NULL) {
                nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
                    "function missing required keyword argument '%s'", qstr_str(self->args[self->n_pos_args + i])));
            }
        }

357
        args = flat_args;
358
        n_args = self->n_pos_args + self->n_kwonly_args;
359
360
361
362

        if (self->takes_kw_args) {
            extra_args[n_extra_args] = dict;
            n_extra_args += 1;
363
364
365
        }
    } else {
        // no keyword arguments given
366
367
368
369
        if (self->n_kwonly_args != 0) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
                "function missing keyword-only argument"));
        }
370
371
372
373
        if (self->takes_kw_args) {
            extra_args[n_extra_args] = mp_obj_new_dict(0);
            n_extra_args += 1;
        }
374
    }
375

376
    mp_obj_dict_t *old_globals = mp_globals_get();
Damien George's avatar
Damien George committed
377
    mp_globals_set(self->globals);
378
    mp_obj_t result;
379
380
381
    DEBUG_printf("Calling: args=%p, n_args=%d, extra_args=%p, n_extra_args=%d\n", args, n_args, extra_args, n_extra_args);
    dump_args(args, n_args);
    dump_args(extra_args, n_extra_args);
382
    mp_vm_return_kind_t vm_return_kind = mp_execute_bytecode(self->bytecode, args, n_args, extra_args, n_extra_args, &result);
Damien George's avatar
Damien George committed
383
    mp_globals_set(old_globals);
384

385
386
387
    if (vm_return_kind == MP_VM_RETURN_NORMAL) {
        return result;
    } else { // MP_VM_RETURN_EXCEPTION
388
        nlr_raise(result);
389
    }
390
391
}

392
const mp_obj_type_t mp_type_fun_bc = {
393
    { &mp_type_type },
394
    .name = MP_QSTR_function,
395
396
397
#if MICROPY_CPYTHON_COMPAT
    .print = fun_bc_print,
#endif
398
    .call = fun_bc_call,
399
    .binary_op = fun_binary_op,
400
401
};

402
mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n_kwonly_args, mp_obj_t def_args_in, const byte *code) {
Damien George's avatar
Damien George committed
403
404
    uint n_def_args = 0;
    uint n_extra_args = 0;
405
406
    mp_obj_tuple_t *def_args = def_args_in;
    if (def_args != MP_OBJ_NULL) {
407
        assert(MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
408
        n_def_args = def_args->len;
Damien George's avatar
Damien George committed
409
410
411
412
        n_extra_args = def_args->len;
    }
    if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
        n_extra_args += 1;
413
    }
414
415
416
    if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
        n_extra_args += 1;
    }
Damien George's avatar
Damien George committed
417
    mp_obj_fun_bc_t *o = m_new_obj_var(mp_obj_fun_bc_t, mp_obj_t, n_extra_args);
418
    o->base.type = &mp_type_fun_bc;
Damien George's avatar
Damien George committed
419
    o->globals = mp_globals_get();
420
    o->args = args;
421
422
    o->n_pos_args = n_pos_args;
    o->n_kwonly_args = n_kwonly_args;
423
    o->n_def_args = n_def_args;
Damien George's avatar
Damien George committed
424
425
    o->takes_var_args = (scope_flags & MP_SCOPE_FLAG_VARARGS) != 0;
    o->takes_kw_args = (scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0;
426
    o->bytecode = code;
427
    memset(o->extra_args, 0, n_extra_args * sizeof(mp_obj_t));
428
    if (def_args != MP_OBJ_NULL) {
Damien George's avatar
Damien George committed
429
        memcpy(o->extra_args, def_args->items, n_def_args * sizeof(mp_obj_t));
430
    }
431
432
433
434
435
436
    if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
        o->extra_args[n_def_args] = MP_OBJ_NULL;
    }
    if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
        o->extra_args[n_extra_args - 1] = MP_OBJ_NULL;
    }
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
    return o;
}

/******************************************************************************/
/* inline assembler functions                                                 */

typedef struct _mp_obj_fun_asm_t {
    mp_obj_base_t base;
    int n_args;
    void *fun;
} mp_obj_fun_asm_t;

typedef machine_uint_t (*inline_asm_fun_0_t)();
typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t);
typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t);
typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t);

// convert a Micro Python object to a sensible value for inline asm
455
STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
456
457
458
459
460
461
462
463
464
    // TODO for byte_array, pass pointer to the array
    if (MP_OBJ_IS_SMALL_INT(obj)) {
        return MP_OBJ_SMALL_INT_VALUE(obj);
    } else if (obj == mp_const_none) {
        return 0;
    } else if (obj == mp_const_false) {
        return 0;
    } else if (obj == mp_const_true) {
        return 1;
465
    } else if (MP_OBJ_IS_STR(obj)) {
466
        // pointer to the string (it's probably constant though!)
467
468
        uint l;
        return (machine_uint_t)mp_obj_str_get_data(obj, &l);
469
470
471
    } else {
        mp_obj_type_t *type = mp_obj_get_type(obj);
        if (0) {
472
#if MICROPY_PY_BUILTINS_FLOAT
473
474
475
        } else if (type == &mp_type_float) {
            // convert float to int (could also pass in float registers)
            return (machine_int_t)mp_obj_float_get(obj);
476
#endif
477
478
479
480
481
482
483
484
485
486
487
488
489
        } else if (type == &mp_type_tuple) {
            // pointer to start of tuple (could pass length, but then could use len(x) for that)
            uint len;
            mp_obj_t *items;
            mp_obj_tuple_get(obj, &len, &items);
            return (machine_uint_t)items;
        } else if (type == &mp_type_list) {
            // pointer to start of list (could pass length, but then could use len(x) for that)
            uint len;
            mp_obj_t *items;
            mp_obj_list_get(obj, &len, &items);
            return (machine_uint_t)items;
        } else {
490
            mp_buffer_info_t bufinfo;
491
            if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
492
493
494
495
496
497
                // supports the buffer protocol, return a pointer to the data
                return (machine_uint_t)bufinfo.buf;
            } else {
                // just pass along a pointer to the object
                return (machine_uint_t)obj;
            }
498
        }
499
500
501
502
    }
}

// convert a return value from inline asm to a sensible Micro Python object
503
STATIC mp_obj_t convert_val_from_inline_asm(machine_uint_t val) {
504
505
506
    return MP_OBJ_NEW_SMALL_INT(val);
}

507
STATIC mp_obj_t fun_asm_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
508
509
    mp_obj_fun_asm_t *self = self_in;

510
    mp_arg_check_num(n_args, n_kw, self->n_args, self->n_args, false);
511
512
513
514
515
516
517

    machine_uint_t ret;
    if (n_args == 0) {
        ret = ((inline_asm_fun_0_t)self->fun)();
    } else if (n_args == 1) {
        ret = ((inline_asm_fun_1_t)self->fun)(convert_obj_for_inline_asm(args[0]));
    } else if (n_args == 2) {
518
        ret = ((inline_asm_fun_2_t)self->fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]));
519
    } else if (n_args == 3) {
520
        ret = ((inline_asm_fun_3_t)self->fun)(convert_obj_for_inline_asm(args[0]), convert_obj_for_inline_asm(args[1]), convert_obj_for_inline_asm(args[2]));
521
522
523
524
525
526
527
528
    } else {
        assert(0);
        ret = 0;
    }

    return convert_val_from_inline_asm(ret);
}

529
STATIC const mp_obj_type_t mp_type_fun_asm = {
530
    { &mp_type_type },
531
    .name = MP_QSTR_function,
532
    .call = fun_asm_call,
533
    .binary_op = fun_binary_op,
534
535
536
537
};

mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {
    mp_obj_fun_asm_t *o = m_new_obj(mp_obj_fun_asm_t);
538
    o->base.type = &mp_type_fun_asm;
539
540
541
542
    o->n_args = n_args;
    o->fun = fun;
    return o;
}