LCOV - code coverage report
Current view: top level - py - objfun.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-333-g49ce7a607.info Lines: 115 115 100.0 %
Date: 2024-04-22 10:07:55 Functions: 12 12 100.0 %
Branches: 64 88 72.7 %

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

Generated by: LCOV version 1.15-5-g462f71d