LCOV - code coverage report
Current view: top level - py - emitnative.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 1448 1457 99.4 %
Date: 2022-12-01 09:37:31 Functions: 113 113 100.0 %
Branches: 522 581 89.8 %

           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                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : // Essentially normal Python has 1 type: Python objects
      28                 :            : // Viper has more than 1 type, and is just a more complicated (a superset of) Python.
      29                 :            : // If you declare everything in Viper as a Python object (ie omit type decls) then
      30                 :            : // it should in principle be exactly the same as Python native.
      31                 :            : // Having types means having more opcodes, like binary_op_nat_nat, binary_op_nat_obj etc.
      32                 :            : // In practice we won't have a VM but rather do this in asm which is actually very minimal.
      33                 :            : 
      34                 :            : // Because it breaks strict Python equivalence it should be a completely separate
      35                 :            : // decorator.  It breaks equivalence because overflow on integers wraps around.
      36                 :            : // It shouldn't break equivalence if you don't use the new types, but since the
      37                 :            : // type decls might be used in normal Python for other reasons, it's probably safest,
      38                 :            : // cleanest and clearest to make it a separate decorator.
      39                 :            : 
      40                 :            : // Actually, it does break equivalence because integers default to native integers,
      41                 :            : // not Python objects.
      42                 :            : 
      43                 :            : // for x in l[0:8]: can be compiled into a native loop if l has pointer type
      44                 :            : 
      45                 :            : #include <stdio.h>
      46                 :            : #include <string.h>
      47                 :            : #include <assert.h>
      48                 :            : 
      49                 :            : #include "py/emit.h"
      50                 :            : #include "py/nativeglue.h"
      51                 :            : #include "py/objfun.h"
      52                 :            : #include "py/objstr.h"
      53                 :            : 
      54                 :            : #if MICROPY_DEBUG_VERBOSE // print debugging info
      55                 :            : #define DEBUG_PRINT (1)
      56                 :            : #define DEBUG_printf DEBUG_printf
      57                 :            : #else // don't print debugging info
      58                 :            : #define DEBUG_printf(...) (void)0
      59                 :            : #endif
      60                 :            : 
      61                 :            : // wrapper around everything in this file
      62                 :            : #if N_X64 || N_X86 || N_THUMB || N_ARM || N_XTENSA || N_XTENSAWIN
      63                 :            : 
      64                 :            : // C stack layout for native functions:
      65                 :            : //  0:                          nlr_buf_t [optional]
      66                 :            : //                              return_value [optional word]
      67                 :            : //                              exc_handler_unwind [optional word]
      68                 :            : //  emit->code_state_start:     mp_code_state_native_t
      69                 :            : //  emit->stack_start:          Python object stack             | emit->n_state
      70                 :            : //                              locals (reversed, L0 at end)    |
      71                 :            : //
      72                 :            : // C stack layout for native generator functions:
      73                 :            : //  0=emit->stack_start:        nlr_buf_t
      74                 :            : //                              return_value
      75                 :            : //                              exc_handler_unwind [optional word]
      76                 :            : //
      77                 :            : //  Then REG_GENERATOR_STATE points to:
      78                 :            : //  0=emit->code_state_start:   mp_code_state_native_t
      79                 :            : //  emit->stack_start:          Python object stack             | emit->n_state
      80                 :            : //                              locals (reversed, L0 at end)    |
      81                 :            : //
      82                 :            : // C stack layout for viper functions:
      83                 :            : //  0:                          nlr_buf_t [optional]
      84                 :            : //                              return_value [optional word]
      85                 :            : //                              exc_handler_unwind [optional word]
      86                 :            : //  emit->code_state_start:     fun_obj, old_globals [optional]
      87                 :            : //  emit->stack_start:          Python object stack             | emit->n_state
      88                 :            : //                              locals (reversed, L0 at end)    |
      89                 :            : //                              (L0-L2 may be in regs instead)
      90                 :            : 
      91                 :            : // Native emitter needs to know the following sizes and offsets of C structs (on the target):
      92                 :            : #if MICROPY_DYNAMIC_COMPILER
      93                 :            : #define SIZEOF_NLR_BUF (2 + mp_dynamic_compiler.nlr_buf_num_regs + 1) // the +1 is conservative in case MICROPY_ENABLE_PYSTACK enabled
      94                 :            : #else
      95                 :            : #define SIZEOF_NLR_BUF (sizeof(nlr_buf_t) / sizeof(uintptr_t))
      96                 :            : #endif
      97                 :            : #define SIZEOF_CODE_STATE (sizeof(mp_code_state_native_t) / sizeof(uintptr_t))
      98                 :            : #define OFFSETOF_CODE_STATE_STATE (offsetof(mp_code_state_native_t, state) / sizeof(uintptr_t))
      99                 :            : #define OFFSETOF_CODE_STATE_FUN_BC (offsetof(mp_code_state_native_t, fun_bc) / sizeof(uintptr_t))
     100                 :            : #define OFFSETOF_CODE_STATE_IP (offsetof(mp_code_state_native_t, ip) / sizeof(uintptr_t))
     101                 :            : #define OFFSETOF_CODE_STATE_SP (offsetof(mp_code_state_native_t, sp) / sizeof(uintptr_t))
     102                 :            : #define OFFSETOF_CODE_STATE_N_STATE (offsetof(mp_code_state_native_t, n_state) / sizeof(uintptr_t))
     103                 :            : #define OFFSETOF_OBJ_FUN_BC_CONTEXT (offsetof(mp_obj_fun_bc_t, context) / sizeof(uintptr_t))
     104                 :            : #define OFFSETOF_OBJ_FUN_BC_CHILD_TABLE (offsetof(mp_obj_fun_bc_t, child_table) / sizeof(uintptr_t))
     105                 :            : #define OFFSETOF_OBJ_FUN_BC_BYTECODE (offsetof(mp_obj_fun_bc_t, bytecode) / sizeof(uintptr_t))
     106                 :            : #define OFFSETOF_MODULE_CONTEXT_QSTR_TABLE (offsetof(mp_module_context_t, constants.qstr_table) / sizeof(uintptr_t))
     107                 :            : #define OFFSETOF_MODULE_CONTEXT_OBJ_TABLE (offsetof(mp_module_context_t, constants.obj_table) / sizeof(uintptr_t))
     108                 :            : #define OFFSETOF_MODULE_CONTEXT_GLOBALS (offsetof(mp_module_context_t, module.globals) / sizeof(uintptr_t))
     109                 :            : 
     110                 :            : // If not already defined, set parent args to same as child call registers
     111                 :            : #ifndef REG_PARENT_RET
     112                 :            : #define REG_PARENT_RET REG_RET
     113                 :            : #define REG_PARENT_ARG_1 REG_ARG_1
     114                 :            : #define REG_PARENT_ARG_2 REG_ARG_2
     115                 :            : #define REG_PARENT_ARG_3 REG_ARG_3
     116                 :            : #define REG_PARENT_ARG_4 REG_ARG_4
     117                 :            : #endif
     118                 :            : 
     119                 :            : // Word index of nlr_buf_t.ret_val
     120                 :            : #define NLR_BUF_IDX_RET_VAL (1)
     121                 :            : 
     122                 :            : // Whether the viper function needs access to fun_obj
     123                 :            : #define NEED_FUN_OBJ(emit) ((emit)->scope->exc_stack_size > 0 \
     124                 :            :     || ((emit)->scope->scope_flags & (MP_SCOPE_FLAG_REFGLOBALS | MP_SCOPE_FLAG_HASCONSTS)))
     125                 :            : 
     126                 :            : // Whether the native/viper function needs to be wrapped in an exception handler
     127                 :            : #define NEED_GLOBAL_EXC_HANDLER(emit) ((emit)->scope->exc_stack_size > 0 \
     128                 :            :     || ((emit)->scope->scope_flags & (MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_REFGLOBALS)))
     129                 :            : 
     130                 :            : // Whether a slot is needed to store LOCAL_IDX_EXC_HANDLER_UNWIND
     131                 :            : #define NEED_EXC_HANDLER_UNWIND(emit) ((emit)->scope->exc_stack_size > 0)
     132                 :            : 
     133                 :            : // Whether registers can be used to store locals (only true if there are no
     134                 :            : // exception handlers, because otherwise an nlr_jump will restore registers to
     135                 :            : // their state at the start of the function and updates to locals will be lost)
     136                 :            : #define CAN_USE_REGS_FOR_LOCALS(emit) ((emit)->scope->exc_stack_size == 0 && !(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR))
     137                 :            : 
     138                 :            : // Indices within the local C stack for various variables
     139                 :            : #define LOCAL_IDX_EXC_VAL(emit) (NLR_BUF_IDX_RET_VAL)
     140                 :            : #define LOCAL_IDX_EXC_HANDLER_PC(emit) (NLR_BUF_IDX_LOCAL_1)
     141                 :            : #define LOCAL_IDX_EXC_HANDLER_UNWIND(emit) (SIZEOF_NLR_BUF + 1) // this needs a dedicated variable outside nlr_buf_t
     142                 :            : #define LOCAL_IDX_RET_VAL(emit) (SIZEOF_NLR_BUF) // needed when NEED_GLOBAL_EXC_HANDLER is true
     143                 :            : #define LOCAL_IDX_FUN_OBJ(emit) ((emit)->code_state_start + OFFSETOF_CODE_STATE_FUN_BC)
     144                 :            : #define LOCAL_IDX_OLD_GLOBALS(emit) ((emit)->code_state_start + OFFSETOF_CODE_STATE_IP)
     145                 :            : #define LOCAL_IDX_GEN_PC(emit) ((emit)->code_state_start + OFFSETOF_CODE_STATE_IP)
     146                 :            : #define LOCAL_IDX_LOCAL_VAR(emit, local_num) ((emit)->stack_start + (emit)->n_state - 1 - (local_num))
     147                 :            : 
     148                 :            : #if MICROPY_PERSISTENT_CODE_SAVE
     149                 :            : 
     150                 :            : // When building with the ability to save native code to .mpy files:
     151                 :            : //  - Qstrs are indirect via qstr_table, and REG_LOCAL_3 always points to qstr_table.
     152                 :            : //  - In a generator no registers are used to store locals, and REG_LOCAL_2 points to the generator state.
     153                 :            : //  - At most 2 registers hold local variables (see CAN_USE_REGS_FOR_LOCALS for when this is possible).
     154                 :            : 
     155                 :            : #define REG_GENERATOR_STATE (REG_LOCAL_2)
     156                 :            : #define REG_QSTR_TABLE (REG_LOCAL_3)
     157                 :            : #define MAX_REGS_FOR_LOCAL_VARS (2)
     158                 :            : 
     159                 :            : STATIC const uint8_t reg_local_table[MAX_REGS_FOR_LOCAL_VARS] = {REG_LOCAL_1, REG_LOCAL_2};
     160                 :            : 
     161                 :            : #else
     162                 :            : 
     163                 :            : // When building without the ability to save native code to .mpy files:
     164                 :            : //  - Qstrs values are written directly into the machine code.
     165                 :            : //  - In a generator no registers are used to store locals, and REG_LOCAL_3 points to the generator state.
     166                 :            : //  - At most 3 registers hold local variables (see CAN_USE_REGS_FOR_LOCALS for when this is possible).
     167                 :            : 
     168                 :            : #define REG_GENERATOR_STATE (REG_LOCAL_3)
     169                 :            : #define MAX_REGS_FOR_LOCAL_VARS (3)
     170                 :            : 
     171                 :            : STATIC const uint8_t reg_local_table[MAX_REGS_FOR_LOCAL_VARS] = {REG_LOCAL_1, REG_LOCAL_2, REG_LOCAL_3};
     172                 :            : 
     173                 :            : #endif
     174                 :            : 
     175                 :            : #define REG_LOCAL_LAST (reg_local_table[MAX_REGS_FOR_LOCAL_VARS - 1])
     176                 :            : 
     177                 :            : #define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
     178                 :            :         *emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
     179                 :            : } while (0)
     180                 :            : 
     181                 :            : typedef enum {
     182                 :            :     STACK_VALUE,
     183                 :            :     STACK_REG,
     184                 :            :     STACK_IMM,
     185                 :            : } stack_info_kind_t;
     186                 :            : 
     187                 :            : // these enums must be distinct and the bottom 4 bits
     188                 :            : // must correspond to the correct MP_NATIVE_TYPE_xxx value
     189                 :            : typedef enum {
     190                 :            :     VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
     191                 :            :     VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
     192                 :            :     VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
     193                 :            :     VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
     194                 :            :     VTYPE_PTR = 0x00 | MP_NATIVE_TYPE_PTR,
     195                 :            :     VTYPE_PTR8 = 0x00 | MP_NATIVE_TYPE_PTR8,
     196                 :            :     VTYPE_PTR16 = 0x00 | MP_NATIVE_TYPE_PTR16,
     197                 :            :     VTYPE_PTR32 = 0x00 | MP_NATIVE_TYPE_PTR32,
     198                 :            : 
     199                 :            :     VTYPE_PTR_NONE = 0x50 | MP_NATIVE_TYPE_PTR,
     200                 :            : 
     201                 :            :     VTYPE_UNBOUND = 0x60 | MP_NATIVE_TYPE_OBJ,
     202                 :            :     VTYPE_BUILTIN_CAST = 0x70 | MP_NATIVE_TYPE_OBJ,
     203                 :            : } vtype_kind_t;
     204                 :            : 
     205                 :         68 : STATIC qstr vtype_to_qstr(vtype_kind_t vtype) {
     206   [ -  +  -  +  :         68 :     switch (vtype) {
             -  -  +  +  
                      + ]
     207                 :            :         case VTYPE_PYOBJ:
     208                 :            :             return MP_QSTR_object;
     209                 :          0 :         case VTYPE_BOOL:
     210                 :          0 :             return MP_QSTR_bool;
     211                 :         32 :         case VTYPE_INT:
     212                 :         32 :             return MP_QSTR_int;
     213                 :          0 :         case VTYPE_UINT:
     214                 :          0 :             return MP_QSTR_uint;
     215                 :          4 :         case VTYPE_PTR:
     216                 :          4 :             return MP_QSTR_ptr;
     217                 :          0 :         case VTYPE_PTR8:
     218                 :          0 :             return MP_QSTR_ptr8;
     219                 :          0 :         case VTYPE_PTR16:
     220                 :          0 :             return MP_QSTR_ptr16;
     221                 :          4 :         case VTYPE_PTR32:
     222                 :          4 :             return MP_QSTR_ptr32;
     223                 :          8 :         case VTYPE_PTR_NONE:
     224                 :            :         default:
     225                 :          8 :             return MP_QSTR_None;
     226                 :            :     }
     227                 :            : }
     228                 :            : 
     229                 :            : typedef struct _stack_info_t {
     230                 :            :     vtype_kind_t vtype;
     231                 :            :     stack_info_kind_t kind;
     232                 :            :     union {
     233                 :            :         int u_reg;
     234                 :            :         mp_int_t u_imm;
     235                 :            :     } data;
     236                 :            : } stack_info_t;
     237                 :            : 
     238                 :            : #define UNWIND_LABEL_UNUSED (0x7fff)
     239                 :            : #define UNWIND_LABEL_DO_FINAL_UNWIND (0x7ffe)
     240                 :            : 
     241                 :            : typedef struct _exc_stack_entry_t {
     242                 :            :     uint16_t label : 15;
     243                 :            :     uint16_t is_finally : 1;
     244                 :            :     uint16_t unwind_label : 15;
     245                 :            :     uint16_t is_active : 1;
     246                 :            : } exc_stack_entry_t;
     247                 :            : 
     248                 :            : struct _emit_t {
     249                 :            :     mp_emit_common_t *emit_common;
     250                 :            :     mp_obj_t *error_slot;
     251                 :            :     uint *label_slot;
     252                 :            :     uint exit_label;
     253                 :            :     int pass;
     254                 :            : 
     255                 :            :     bool do_viper_types;
     256                 :            :     bool prelude_offset_uses_u16_encoding;
     257                 :            : 
     258                 :            :     mp_uint_t local_vtype_alloc;
     259                 :            :     vtype_kind_t *local_vtype;
     260                 :            : 
     261                 :            :     mp_uint_t stack_info_alloc;
     262                 :            :     stack_info_t *stack_info;
     263                 :            :     vtype_kind_t saved_stack_vtype;
     264                 :            : 
     265                 :            :     size_t exc_stack_alloc;
     266                 :            :     size_t exc_stack_size;
     267                 :            :     exc_stack_entry_t *exc_stack;
     268                 :            : 
     269                 :            :     int prelude_offset;
     270                 :            :     int prelude_ptr_index;
     271                 :            :     int start_offset;
     272                 :            :     int n_state;
     273                 :            :     uint16_t code_state_start;
     274                 :            :     uint16_t stack_start;
     275                 :            :     int stack_size;
     276                 :            :     uint16_t n_info;
     277                 :            :     uint16_t n_cell;
     278                 :            : 
     279                 :            :     scope_t *scope;
     280                 :            : 
     281                 :            :     ASM_T *as;
     282                 :            : };
     283                 :            : 
     284                 :            : STATIC void emit_load_reg_with_object(emit_t *emit, int reg, mp_obj_t obj);
     285                 :            : STATIC void emit_native_global_exc_entry(emit_t *emit);
     286                 :            : STATIC void emit_native_global_exc_exit(emit_t *emit);
     287                 :            : STATIC void emit_native_load_const_obj(emit_t *emit, mp_obj_t obj);
     288                 :            : 
     289                 :       1367 : emit_t *EXPORT_FUN(new)(mp_emit_common_t * emit_common, mp_obj_t *error_slot, uint *label_slot, mp_uint_t max_num_labels) {
     290                 :       1367 :     emit_t *emit = m_new0(emit_t, 1);
     291                 :       1367 :     emit->emit_common = emit_common;
     292                 :       1367 :     emit->error_slot = error_slot;
     293                 :       1367 :     emit->label_slot = label_slot;
     294                 :       1367 :     emit->stack_info_alloc = 8;
     295                 :       1367 :     emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
     296                 :       1367 :     emit->exc_stack_alloc = 8;
     297                 :       1367 :     emit->exc_stack = m_new(exc_stack_entry_t, emit->exc_stack_alloc);
     298                 :       1367 :     emit->as = m_new0(ASM_T, 1);
     299                 :       1367 :     mp_asm_base_init(&emit->as->base, max_num_labels);
     300                 :       1367 :     return emit;
     301                 :            : }
     302                 :            : 
     303                 :       1351 : void EXPORT_FUN(free)(emit_t * emit) {
     304                 :       1351 :     mp_asm_base_deinit(&emit->as->base, false);
     305                 :       1351 :     m_del_obj(ASM_T, emit->as);
     306                 :       1351 :     m_del(exc_stack_entry_t, emit->exc_stack, emit->exc_stack_alloc);
     307                 :       1351 :     m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
     308                 :       1351 :     m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
     309                 :       1351 :     m_del_obj(emit_t, emit);
     310                 :       1351 : }
     311                 :            : 
     312                 :            : STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val, int arg_reg);
     313                 :            : 
     314                 :      21139 : STATIC void emit_native_mov_reg_const(emit_t *emit, int reg_dest, int const_val) {
     315                 :      21139 :     ASM_LOAD_REG_REG_OFFSET(emit->as, reg_dest, REG_FUN_TABLE, const_val);
     316                 :      21139 : }
     317                 :            : 
     318                 :     183380 : STATIC void emit_native_mov_state_reg(emit_t *emit, int local_num, int reg_src) {
     319         [ +  + ]:     183380 :     if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
     320                 :      11121 :         ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, REG_GENERATOR_STATE, local_num);
     321                 :            :     } else {
     322                 :     172259 :         ASM_MOV_LOCAL_REG(emit->as, local_num, reg_src);
     323                 :            :     }
     324                 :     183380 : }
     325                 :            : 
     326                 :     118852 : STATIC void emit_native_mov_reg_state(emit_t *emit, int reg_dest, int local_num) {
     327         [ +  + ]:     118852 :     if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
     328                 :       6503 :         ASM_LOAD_REG_REG_OFFSET(emit->as, reg_dest, REG_GENERATOR_STATE, local_num);
     329                 :            :     } else {
     330                 :     112349 :         ASM_MOV_REG_LOCAL(emit->as, reg_dest, local_num);
     331                 :            :     }
     332                 :     118852 : }
     333                 :            : 
     334                 :      73672 : STATIC void emit_native_mov_reg_state_addr(emit_t *emit, int reg_dest, int local_num) {
     335         [ +  + ]:      73672 :     if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
     336                 :       5448 :         ASM_MOV_REG_IMM(emit->as, reg_dest, local_num * ASM_WORD_SIZE);
     337                 :       5448 :         ASM_ADD_REG_REG(emit->as, reg_dest, REG_GENERATOR_STATE);
     338                 :            :     } else {
     339                 :      68224 :         ASM_MOV_REG_LOCAL_ADDR(emit->as, reg_dest, local_num);
     340                 :            :     }
     341                 :      73672 : }
     342                 :            : 
     343                 :     117800 : STATIC void emit_native_mov_reg_qstr(emit_t *emit, int arg_reg, qstr qst) {
     344                 :            :     #if MICROPY_PERSISTENT_CODE_SAVE
     345                 :            :     ASM_LOAD16_REG_REG_OFFSET(emit->as, arg_reg, REG_QSTR_TABLE, mp_emit_common_use_qstr(emit->emit_common, qst));
     346                 :            :     #else
     347                 :     117800 :     ASM_MOV_REG_IMM(emit->as, arg_reg, qst);
     348                 :            :     #endif
     349                 :     117800 : }
     350                 :            : 
     351                 :      21552 : STATIC void emit_native_mov_reg_qstr_obj(emit_t *emit, int reg_dest, qstr qst) {
     352                 :            :     #if MICROPY_PERSISTENT_CODE_SAVE
     353                 :            :     emit_load_reg_with_object(emit, reg_dest, MP_OBJ_NEW_QSTR(qst));
     354                 :            :     #else
     355                 :      21552 :     ASM_MOV_REG_IMM(emit->as, reg_dest, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
     356                 :            :     #endif
     357                 :      21552 : }
     358                 :            : 
     359                 :            : #define emit_native_mov_state_imm_via(emit, local_num, imm, reg_temp) \
     360                 :            :     do { \
     361                 :            :         ASM_MOV_REG_IMM((emit)->as, (reg_temp), (imm)); \
     362                 :            :         emit_native_mov_state_reg((emit), (local_num), (reg_temp)); \
     363                 :            :     } while (false)
     364                 :            : 
     365                 :      11742 : STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
     366                 :      11742 :     DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
     367                 :            : 
     368                 :      11742 :     emit->pass = pass;
     369                 :      11742 :     emit->do_viper_types = scope->emit_options == MP_EMIT_OPT_VIPER;
     370                 :      11742 :     emit->stack_size = 0;
     371                 :      11742 :     emit->scope = scope;
     372                 :            : 
     373                 :            :     // allocate memory for keeping track of the types of locals
     374         [ +  + ]:      11742 :     if (emit->local_vtype_alloc < scope->num_locals) {
     375                 :        694 :         emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
     376                 :        694 :         emit->local_vtype_alloc = scope->num_locals;
     377                 :            :     }
     378                 :            : 
     379                 :            :     // set default type for arguments
     380                 :      11742 :     mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
     381         [ +  + ]:      11742 :     if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
     382                 :        120 :         num_args += 1;
     383                 :            :     }
     384         [ +  + ]:      11742 :     if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
     385                 :         54 :         num_args += 1;
     386                 :            :     }
     387         [ +  + ]:      20317 :     for (mp_uint_t i = 0; i < num_args; i++) {
     388                 :       8575 :         emit->local_vtype[i] = VTYPE_PYOBJ;
     389                 :            :     }
     390                 :            : 
     391                 :            :     // Set viper type for arguments
     392         [ +  + ]:      11742 :     if (emit->do_viper_types) {
     393         [ +  + ]:       2428 :         for (int i = 0; i < emit->scope->id_info_len; ++i) {
     394                 :       1720 :             id_info_t *id = &emit->scope->id_info[i];
     395         [ +  + ]:       1720 :             if (id->flags & ID_FLAG_IS_PARAM) {
     396         [ -  + ]:        982 :                 assert(id->local_num < emit->local_vtype_alloc);
     397                 :        982 :                 emit->local_vtype[id->local_num] = id->flags >> ID_FLAG_VIPER_TYPE_POS;
     398                 :            :             }
     399                 :            :         }
     400                 :            :     }
     401                 :            : 
     402                 :            :     // local variables begin unbound, and have unknown type
     403         [ +  + ]:      31813 :     for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
     404                 :      20071 :         emit->local_vtype[i] = VTYPE_UNBOUND;
     405                 :            :     }
     406                 :            : 
     407                 :            :     // values on stack begin unbound
     408         [ +  + ]:     115774 :     for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
     409                 :     104032 :         emit->stack_info[i].kind = STACK_VALUE;
     410                 :     104032 :         emit->stack_info[i].vtype = VTYPE_UNBOUND;
     411                 :            :     }
     412                 :            : 
     413         [ +  + ]:      19602 :     mp_asm_base_start_pass(&emit->as->base, pass == MP_PASS_EMIT ? MP_ASM_PASS_EMIT : MP_ASM_PASS_COMPUTE);
     414                 :            : 
     415                 :            :     // generate code for entry to function
     416                 :            : 
     417                 :            :     // Work out start of code state (mp_code_state_native_t or reduced version for viper)
     418                 :      11742 :     emit->code_state_start = 0;
     419   [ +  +  +  + ]:      11742 :     if (NEED_GLOBAL_EXC_HANDLER(emit)) {
     420                 :       8151 :         emit->code_state_start = SIZEOF_NLR_BUF; // for nlr_buf_t
     421                 :       8151 :         emit->code_state_start += 1;  // for return_value
     422         [ +  + ]:       8151 :         if (NEED_EXC_HANDLER_UNWIND(emit)) {
     423                 :       2163 :             emit->code_state_start += 1;
     424                 :            :         }
     425                 :            :     }
     426                 :            : 
     427                 :      11742 :     size_t fun_table_off = mp_emit_common_use_const_obj(emit->emit_common, MP_OBJ_FROM_PTR(&mp_fun_table));
     428                 :            : 
     429         [ +  + ]:      11742 :     if (emit->do_viper_types) {
     430                 :            :         // Work out size of state (locals plus stack)
     431                 :            :         // n_state counts all stack and locals, even those in registers
     432                 :        708 :         emit->n_state = scope->num_locals + scope->stack_size;
     433                 :        708 :         int num_locals_in_regs = 0;
     434   [ +  +  +  + ]:        708 :         if (CAN_USE_REGS_FOR_LOCALS(emit)) {
     435                 :        670 :             num_locals_in_regs = scope->num_locals;
     436         [ +  + ]:        670 :             if (num_locals_in_regs > MAX_REGS_FOR_LOCAL_VARS) {
     437                 :        120 :                 num_locals_in_regs = MAX_REGS_FOR_LOCAL_VARS;
     438                 :            :             }
     439                 :            :             // Need a spot for REG_LOCAL_LAST (see below)
     440         [ +  + ]:        670 :             if (scope->num_pos_args >= MAX_REGS_FOR_LOCAL_VARS + 1) {
     441                 :         24 :                 --num_locals_in_regs;
     442                 :            :             }
     443                 :            :         }
     444                 :            : 
     445                 :            :         // Work out where the locals and Python stack start within the C stack
     446   [ +  +  +  + ]:        708 :         if (NEED_GLOBAL_EXC_HANDLER(emit)) {
     447                 :            :             // Reserve 2 words for function object and old globals
     448                 :        288 :             emit->stack_start = emit->code_state_start + 2;
     449         [ +  + ]:        420 :         } else if (scope->scope_flags & MP_SCOPE_FLAG_HASCONSTS) {
     450                 :            :             // Reserve 1 word for function object, to access const table
     451                 :         12 :             emit->stack_start = emit->code_state_start + 1;
     452                 :            :         } else {
     453                 :        408 :             emit->stack_start = emit->code_state_start + 0;
     454                 :            :         }
     455                 :            : 
     456                 :            :         // Entry to function
     457                 :        708 :         ASM_ENTRY(emit->as, emit->stack_start + emit->n_state - num_locals_in_regs);
     458                 :            : 
     459                 :            :         #if N_X86
     460                 :            :         asm_x86_mov_arg_to_r32(emit->as, 0, REG_PARENT_ARG_1);
     461                 :            :         #endif
     462                 :            : 
     463                 :            :         // Load REG_FUN_TABLE with a pointer to mp_fun_table, found in the const_table
     464                 :        708 :         ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_PARENT_ARG_1, OFFSETOF_OBJ_FUN_BC_CONTEXT);
     465                 :            :         #if MICROPY_PERSISTENT_CODE_SAVE
     466                 :            :         ASM_LOAD_REG_REG_OFFSET(emit->as, REG_QSTR_TABLE, REG_FUN_TABLE, OFFSETOF_MODULE_CONTEXT_QSTR_TABLE);
     467                 :            :         #endif
     468                 :        708 :         ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_FUN_TABLE, OFFSETOF_MODULE_CONTEXT_OBJ_TABLE);
     469                 :        708 :         ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_FUN_TABLE, fun_table_off);
     470                 :            : 
     471                 :            :         // Store function object (passed as first arg) to stack if needed
     472   [ +  +  +  + ]:        708 :         if (NEED_FUN_OBJ(emit)) {
     473                 :        296 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_FUN_OBJ(emit), REG_PARENT_ARG_1);
     474                 :            :         }
     475                 :            : 
     476                 :            :         // Put n_args in REG_ARG_1, n_kw in REG_ARG_2, args array in REG_LOCAL_LAST
     477                 :            :         #if N_X86
     478                 :            :         asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_1);
     479                 :            :         asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_2);
     480                 :            :         asm_x86_mov_arg_to_r32(emit->as, 3, REG_LOCAL_LAST);
     481                 :            :         #else
     482                 :        708 :         ASM_MOV_REG_REG(emit->as, REG_ARG_1, REG_PARENT_ARG_2);
     483                 :        708 :         ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_PARENT_ARG_3);
     484                 :        708 :         ASM_MOV_REG_REG(emit->as, REG_LOCAL_LAST, REG_PARENT_ARG_4);
     485                 :            :         #endif
     486                 :            : 
     487                 :            :         // Check number of args matches this function, and call mp_arg_check_num_sig if not
     488                 :        708 :         ASM_JUMP_IF_REG_NONZERO(emit->as, REG_ARG_2, *emit->label_slot + 4, true);
     489                 :        708 :         ASM_MOV_REG_IMM(emit->as, REG_ARG_3, scope->num_pos_args);
     490                 :        708 :         ASM_JUMP_IF_REG_EQ(emit->as, REG_ARG_1, REG_ARG_3, *emit->label_slot + 5);
     491                 :        708 :         mp_asm_base_label_assign(&emit->as->base, *emit->label_slot + 4);
     492                 :        708 :         ASM_MOV_REG_IMM(emit->as, REG_ARG_3, MP_OBJ_FUN_MAKE_SIG(scope->num_pos_args, scope->num_pos_args, false));
     493                 :        708 :         ASM_CALL_IND(emit->as, MP_F_ARG_CHECK_NUM_SIG);
     494                 :        708 :         mp_asm_base_label_assign(&emit->as->base, *emit->label_slot + 5);
     495                 :            : 
     496                 :            :         // Store arguments into locals (reg or stack), converting to native if needed
     497         [ +  + ]:       1678 :         for (int i = 0; i < emit->scope->num_pos_args; i++) {
     498                 :        970 :             int r = REG_ARG_1;
     499                 :        970 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_1, REG_LOCAL_LAST, i);
     500         [ +  + ]:        970 :             if (emit->local_vtype[i] != VTYPE_PYOBJ) {
     501                 :        862 :                 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, emit->local_vtype[i], REG_ARG_2);
     502                 :        862 :                 r = REG_RET;
     503                 :            :             }
     504                 :            :             // REG_LOCAL_LAST points to the args array so be sure not to overwrite it if it's still needed
     505   [ +  +  +  -  :        970 :             if (i < MAX_REGS_FOR_LOCAL_VARS && CAN_USE_REGS_FOR_LOCALS(emit) && (i != MAX_REGS_FOR_LOCAL_VARS - 1 || emit->scope->num_pos_args == MAX_REGS_FOR_LOCAL_VARS)) {
          +  -  +  +  +  
                      + ]
     506                 :        904 :                 ASM_MOV_REG_REG(emit->as, reg_local_table[i], r);
     507                 :            :             } else {
     508                 :         66 :                 emit_native_mov_state_reg(emit, LOCAL_IDX_LOCAL_VAR(emit, i), r);
     509                 :            :             }
     510                 :            :         }
     511                 :            :         // Get local from the stack back into REG_LOCAL_LAST if this reg couldn't be written to above
     512   [ +  +  +  -  :        708 :         if (emit->scope->num_pos_args >= MAX_REGS_FOR_LOCAL_VARS + 1 && CAN_USE_REGS_FOR_LOCALS(emit)) {
                   +  - ]
     513                 :         24 :             ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_LAST, LOCAL_IDX_LOCAL_VAR(emit, MAX_REGS_FOR_LOCAL_VARS - 1));
     514                 :            :         }
     515                 :            : 
     516                 :        708 :         emit_native_global_exc_entry(emit);
     517                 :            : 
     518                 :            :     } else {
     519                 :            :         // work out size of state (locals plus stack)
     520                 :      11034 :         emit->n_state = scope->num_locals + scope->stack_size;
     521                 :            : 
     522         [ +  + ]:      11034 :         if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
     523                 :        696 :             mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->prelude_ptr_index);
     524                 :        696 :             mp_asm_base_data(&emit->as->base, ASM_WORD_SIZE, (uintptr_t)emit->start_offset);
     525                 :        696 :             ASM_ENTRY(emit->as, emit->code_state_start);
     526                 :            : 
     527                 :            :             // Reset the state size for the state pointed to by REG_GENERATOR_STATE
     528                 :        696 :             emit->code_state_start = 0;
     529                 :        696 :             emit->stack_start = SIZEOF_CODE_STATE;
     530                 :            : 
     531                 :            :             // Put address of code_state into REG_GENERATOR_STATE
     532                 :            :             #if N_X86
     533                 :            :             asm_x86_mov_arg_to_r32(emit->as, 0, REG_GENERATOR_STATE);
     534                 :            :             #else
     535                 :        696 :             ASM_MOV_REG_REG(emit->as, REG_GENERATOR_STATE, REG_PARENT_ARG_1);
     536                 :            :             #endif
     537                 :            : 
     538                 :            :             // Put throw value into LOCAL_IDX_EXC_VAL slot, for yield/yield-from
     539                 :            :             #if N_X86
     540                 :            :             asm_x86_mov_arg_to_r32(emit->as, 1, REG_PARENT_ARG_2);
     541                 :            :             #endif
     542                 :        696 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_PARENT_ARG_2);
     543                 :            : 
     544                 :            :             // Load REG_FUN_TABLE with a pointer to mp_fun_table, found in the const_table
     545                 :        696 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_GENERATOR_STATE, LOCAL_IDX_FUN_OBJ(emit));
     546                 :        696 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_TEMP0, OFFSETOF_OBJ_FUN_BC_CONTEXT);
     547                 :            :             #if MICROPY_PERSISTENT_CODE_SAVE
     548                 :            :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_QSTR_TABLE, REG_TEMP0, OFFSETOF_MODULE_CONTEXT_QSTR_TABLE);
     549                 :            :             #endif
     550                 :        696 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_TEMP0, OFFSETOF_MODULE_CONTEXT_OBJ_TABLE);
     551                 :        696 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_TEMP0, fun_table_off);
     552                 :            :         } else {
     553                 :            :             // The locals and stack start after the code_state structure
     554                 :      10338 :             emit->stack_start = emit->code_state_start + SIZEOF_CODE_STATE;
     555                 :            : 
     556                 :            :             // Allocate space on C-stack for code_state structure, which includes state
     557                 :      10338 :             ASM_ENTRY(emit->as, emit->stack_start + emit->n_state);
     558                 :            : 
     559                 :            :             // Prepare incoming arguments for call to mp_setup_code_state
     560                 :            : 
     561                 :            :             #if N_X86
     562                 :            :             asm_x86_mov_arg_to_r32(emit->as, 0, REG_PARENT_ARG_1);
     563                 :            :             asm_x86_mov_arg_to_r32(emit->as, 1, REG_PARENT_ARG_2);
     564                 :            :             asm_x86_mov_arg_to_r32(emit->as, 2, REG_PARENT_ARG_3);
     565                 :            :             asm_x86_mov_arg_to_r32(emit->as, 3, REG_PARENT_ARG_4);
     566                 :            :             #endif
     567                 :            : 
     568                 :            :             // Load REG_FUN_TABLE with a pointer to mp_fun_table, found in the const_table
     569                 :      10338 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_PARENT_ARG_1, OFFSETOF_OBJ_FUN_BC_CONTEXT);
     570                 :            :             #if MICROPY_PERSISTENT_CODE_SAVE
     571                 :            :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_QSTR_TABLE, REG_FUN_TABLE, OFFSETOF_MODULE_CONTEXT_QSTR_TABLE);
     572                 :            :             #endif
     573                 :      10338 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_FUN_TABLE, OFFSETOF_MODULE_CONTEXT_OBJ_TABLE);
     574                 :      10338 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_FUN_TABLE, REG_FUN_TABLE, fun_table_off);
     575                 :            : 
     576                 :            :             // Set code_state.fun_bc
     577                 :      10338 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_FUN_OBJ(emit), REG_PARENT_ARG_1);
     578                 :            : 
     579                 :            :             // Set code_state.ip, a pointer to the beginning of the prelude.  This pointer is found
     580                 :            :             // either directly in mp_obj_fun_bc_t.child_table (if there are no children), or in
     581                 :            :             // mp_obj_fun_bc_t.child_table[num_children] (if num_children > 0).
     582                 :      10338 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_PARENT_ARG_1, REG_PARENT_ARG_1, OFFSETOF_OBJ_FUN_BC_CHILD_TABLE);
     583         [ +  + ]:      10338 :             if (emit->prelude_ptr_index != 0) {
     584                 :       2507 :                 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_PARENT_ARG_1, REG_PARENT_ARG_1, emit->prelude_ptr_index);
     585                 :            :             }
     586                 :      10338 :             emit_native_mov_state_reg(emit, emit->code_state_start + OFFSETOF_CODE_STATE_IP, REG_PARENT_ARG_1);
     587                 :            : 
     588                 :            :             // Set code_state.n_state (only works on little endian targets due to n_state being uint16_t)
     589                 :      10338 :             emit_native_mov_state_imm_via(emit, emit->code_state_start + OFFSETOF_CODE_STATE_N_STATE, emit->n_state, REG_ARG_1);
     590                 :            : 
     591                 :            :             // Put address of code_state into first arg
     592                 :      10338 :             ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, emit->code_state_start);
     593                 :            : 
     594                 :            :             // Copy next 3 args if needed
     595                 :            :             #if REG_ARG_2 != REG_PARENT_ARG_2
     596                 :            :             ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_PARENT_ARG_2);
     597                 :            :             #endif
     598                 :            :             #if REG_ARG_3 != REG_PARENT_ARG_3
     599                 :            :             ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_PARENT_ARG_3);
     600                 :            :             #endif
     601                 :            :             #if REG_ARG_4 != REG_PARENT_ARG_4
     602                 :            :             ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_PARENT_ARG_4);
     603                 :            :             #endif
     604                 :            : 
     605                 :            :             // Call mp_setup_code_state to prepare code_state structure
     606                 :            :             #if N_THUMB
     607                 :            :             asm_thumb_bl_ind(emit->as, MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
     608                 :            :             #elif N_ARM
     609                 :            :             asm_arm_bl_ind(emit->as, MP_F_SETUP_CODE_STATE, ASM_ARM_REG_R4);
     610                 :            :             #else
     611                 :      10338 :             ASM_CALL_IND(emit->as, MP_F_SETUP_CODE_STATE);
     612                 :            :             #endif
     613                 :            :         }
     614                 :            : 
     615                 :      11034 :         emit_native_global_exc_entry(emit);
     616                 :            : 
     617                 :            :         // cache some locals in registers, but only if no exception handlers
     618   [ +  +  +  + ]:      11034 :         if (CAN_USE_REGS_FOR_LOCALS(emit)) {
     619   [ +  +  +  + ]:      15855 :             for (int i = 0; i < MAX_REGS_FOR_LOCAL_VARS && i < scope->num_locals; ++i) {
     620                 :       7500 :                 ASM_MOV_REG_LOCAL(emit->as, reg_local_table[i], LOCAL_IDX_LOCAL_VAR(emit, i));
     621                 :            :             }
     622                 :            :         }
     623                 :            : 
     624                 :            :         // set the type of closed over variables
     625         [ +  + ]:      59313 :         for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
     626                 :      48279 :             id_info_t *id = &scope->id_info[i];
     627         [ +  + ]:      48279 :             if (id->kind == ID_INFO_KIND_CELL) {
     628                 :        216 :                 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
     629                 :            :             }
     630                 :            :         }
     631                 :            :     }
     632                 :      11742 : }
     633                 :            : 
     634                 :      85368 : static inline void emit_native_write_code_info_byte(emit_t *emit, byte val) {
     635                 :      85368 :     mp_asm_base_data(&emit->as->base, 1, val);
     636                 :            : }
     637                 :            : 
     638                 :      18465 : static inline void emit_native_write_code_info_qstr(emit_t *emit, qstr qst) {
     639                 :      18465 :     mp_encode_uint(&emit->as->base, mp_asm_base_get_cur_to_write_bytes, mp_emit_common_use_qstr(emit->emit_common, qst));
     640                 :      18465 : }
     641                 :            : 
     642                 :      11726 : STATIC bool emit_native_end_pass(emit_t *emit) {
     643                 :      11726 :     emit_native_global_exc_exit(emit);
     644                 :            : 
     645         [ +  + ]:      11726 :     if (!emit->do_viper_types) {
     646                 :      11034 :         emit->prelude_offset = mp_asm_base_get_code_pos(&emit->as->base);
     647                 :      11034 :         emit->prelude_ptr_index = emit->emit_common->ct_cur_child;
     648                 :            : 
     649                 :      11034 :         size_t n_state = emit->n_state;
     650                 :      11034 :         size_t n_exc_stack = 0; // exc-stack not needed for native code
     651         [ +  + ]:      74295 :         MP_BC_PRELUDE_SIG_ENCODE(n_state, n_exc_stack, emit->scope, emit_native_write_code_info_byte, emit);
     652                 :            : 
     653                 :      11034 :         size_t n_info = emit->n_info;
     654                 :      11034 :         size_t n_cell = emit->n_cell;
     655   [ +  +  +  + ]:      11073 :         MP_BC_PRELUDE_SIZE_ENCODE(n_info, n_cell, emit_native_write_code_info_byte, emit);
     656                 :            : 
     657                 :            :         // bytecode prelude: source info (function and argument qstrs)
     658                 :      11034 :         size_t info_start = mp_asm_base_get_code_pos(&emit->as->base);
     659                 :      11034 :         emit_native_write_code_info_qstr(emit, emit->scope->simple_name);
     660         [ +  + ]:      18465 :         for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
     661                 :      13074 :             qstr qst = MP_QSTR__star_;
     662         [ +  + ]:      13074 :             for (int j = 0; j < emit->scope->id_info_len; ++j) {
     663                 :      12567 :                 id_info_t *id = &emit->scope->id_info[j];
     664   [ +  +  +  + ]:      12567 :                 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
     665                 :       6924 :                     qst = id->qst;
     666                 :       6924 :                     break;
     667                 :            :                 }
     668                 :            :             }
     669                 :       7431 :             emit_native_write_code_info_qstr(emit, qst);
     670                 :            :         }
     671                 :      11034 :         emit->n_info = mp_asm_base_get_code_pos(&emit->as->base) - info_start;
     672                 :            : 
     673                 :            :         // bytecode prelude: initialise closed over variables
     674                 :      11034 :         size_t cell_start = mp_asm_base_get_code_pos(&emit->as->base);
     675         [ +  + ]:      59313 :         for (int i = 0; i < emit->scope->id_info_len; i++) {
     676                 :      48279 :             id_info_t *id = &emit->scope->id_info[i];
     677         [ +  + ]:      48279 :             if (id->kind == ID_INFO_KIND_CELL) {
     678         [ -  + ]:        216 :                 assert(id->local_num <= 255);
     679                 :        216 :                 mp_asm_base_data(&emit->as->base, 1, id->local_num); // write the local which should be converted to a cell
     680                 :            :             }
     681                 :            :         }
     682                 :      11034 :         emit->n_cell = mp_asm_base_get_code_pos(&emit->as->base) - cell_start;
     683                 :            : 
     684                 :            :     }
     685                 :            : 
     686                 :      11726 :     ASM_END_PASS(emit->as);
     687                 :            : 
     688                 :            :     // check stack is back to zero size
     689         [ -  + ]:      11726 :     assert(emit->stack_size == 0);
     690         [ -  + ]:      11726 :     assert(emit->exc_stack_size == 0);
     691                 :            : 
     692         [ +  + ]:      11726 :     if (emit->pass == MP_PASS_EMIT) {
     693         [ +  + ]:       3882 :         void *f = mp_asm_base_get_code(&emit->as->base);
     694         [ +  + ]:       3882 :         mp_uint_t f_len = mp_asm_base_get_code_size(&emit->as->base);
     695                 :            : 
     696                 :       3882 :         mp_raw_code_t **children = emit->emit_common->children;
     697         [ +  + ]:       3882 :         if (!emit->do_viper_types) {
     698                 :            :             #if MICROPY_EMIT_NATIVE_PRELUDE_SEPARATE_FROM_MACHINE_CODE
     699                 :            :             // Executable code cannot be accessed byte-wise on this architecture, so copy
     700                 :            :             // the prelude to a separate memory region that is byte-wise readable.
     701                 :            :             void *buf = emit->as->base.code_base + emit->prelude_offset;
     702                 :            :             size_t n = emit->as->base.code_offset - emit->prelude_offset;
     703                 :            :             const uint8_t *prelude_ptr = memcpy(m_new(uint8_t, n), buf, n);
     704                 :            :             #else
     705                 :            :             // Point to the prelude directly, at the end of the machine code data.
     706                 :       3678 :             const uint8_t *prelude_ptr = (const uint8_t *)f + emit->prelude_offset;
     707                 :            :             #endif
     708                 :            : 
     709                 :            :             // Store the pointer to the prelude using the child_table.
     710         [ -  + ]:       3678 :             assert(emit->prelude_ptr_index == emit->emit_common->ct_cur_child);
     711         [ +  + ]:       3678 :             if (emit->prelude_ptr_index == 0) {
     712                 :            :                 children = (void *)prelude_ptr;
     713                 :            :             } else {
     714                 :        891 :                 children = m_renew(mp_raw_code_t *, children, emit->prelude_ptr_index, emit->prelude_ptr_index + 1);
     715                 :        891 :                 children[emit->prelude_ptr_index] = (void *)prelude_ptr;
     716                 :            :             }
     717                 :            :         }
     718                 :            : 
     719                 :       3882 :         mp_emit_glue_assign_native(emit->scope->raw_code,
     720                 :       3882 :             emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
     721                 :            :             f, f_len,
     722                 :            :             children,
     723                 :            :             #if MICROPY_PERSISTENT_CODE_SAVE
     724                 :            :             emit->emit_common->ct_cur_child,
     725                 :            :             emit->prelude_offset,
     726                 :            :             #endif
     727         [ +  + ]:       3882 :             emit->scope->scope_flags, 0, 0);
     728                 :            :     }
     729                 :            : 
     730                 :      11726 :     return true;
     731                 :            : }
     732                 :            : 
     733                 :     326858 : STATIC void ensure_extra_stack(emit_t *emit, size_t delta) {
     734         [ +  + ]:     326858 :     if (emit->stack_size + delta > emit->stack_info_alloc) {
     735                 :        331 :         size_t new_alloc = (emit->stack_size + delta + 8) & ~3;
     736                 :        331 :         emit->stack_info = m_renew(stack_info_t, emit->stack_info, emit->stack_info_alloc, new_alloc);
     737                 :        331 :         emit->stack_info_alloc = new_alloc;
     738                 :            :     }
     739                 :     326858 : }
     740                 :            : 
     741                 :     617870 : STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
     742         [ -  + ]:     617870 :     assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
     743         [ -  + ]:     617870 :     assert((mp_int_t)emit->stack_size + stack_size_delta <= (mp_int_t)emit->stack_info_alloc);
     744                 :     617870 :     emit->stack_size += stack_size_delta;
     745   [ +  -  +  + ]:     617870 :     if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
     746                 :      14357 :         emit->scope->stack_size = emit->stack_size;
     747                 :            :     }
     748                 :            :     #ifdef DEBUG_PRINT
     749                 :            :     DEBUG_printf("  adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
     750                 :            :     for (int i = 0; i < emit->stack_size; i++) {
     751                 :            :         stack_info_t *si = &emit->stack_info[i];
     752                 :            :         DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
     753                 :            :     }
     754                 :            :     DEBUG_printf("\n");
     755                 :            :     #endif
     756                 :     617870 : }
     757                 :            : 
     758                 :       7164 : STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
     759                 :       7164 :     DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
     760         [ +  + ]:       7164 :     if (delta > 0) {
     761                 :       6714 :         ensure_extra_stack(emit, delta);
     762                 :            :     }
     763                 :            :     // If we are adjusting the stack in a positive direction (pushing) then we
     764                 :            :     // need to fill in values for the stack kind and vtype of the newly-pushed
     765                 :            :     // entries.  These should be set to "value" (ie not reg or imm) because we
     766                 :            :     // should only need to adjust the stack due to a jump to this part in the
     767                 :            :     // code (and hence we have settled the stack before the jump).
     768         [ +  + ]:      14730 :     for (mp_int_t i = 0; i < delta; i++) {
     769                 :       7566 :         stack_info_t *si = &emit->stack_info[emit->stack_size + i];
     770                 :       7566 :         si->kind = STACK_VALUE;
     771                 :            :         // TODO we don't know the vtype to use here.  At the moment this is a
     772                 :            :         // hack to get the case of multi comparison working.
     773         [ +  + ]:       7566 :         if (delta == 1) {
     774                 :       6198 :             si->vtype = emit->saved_stack_vtype;
     775                 :            :         } else {
     776                 :       1368 :             si->vtype = VTYPE_PYOBJ;
     777                 :            :         }
     778                 :            :     }
     779                 :       7164 :     adjust_stack(emit, delta);
     780                 :       7164 : }
     781                 :            : 
     782                 :     218581 : STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
     783                 :     218581 :     (void)emit;
     784                 :     218581 :     (void)source_line;
     785                 :     218581 : }
     786                 :            : 
     787                 :            : // this must be called at start of emit functions
     788                 :            : STATIC void emit_native_pre(emit_t *emit) {
     789                 :            :     (void)emit;
     790                 :            : }
     791                 :            : 
     792                 :            : // depth==0 is top, depth==1 is before top, etc
     793                 :      11733 : STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
     794                 :      11733 :     return &emit->stack_info[emit->stack_size - 1 - depth];
     795                 :            : }
     796                 :            : 
     797                 :            : // depth==0 is top, depth==1 is before top, etc
     798                 :      96881 : STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
     799         [ +  + ]:      96881 :     if (emit->do_viper_types) {
     800                 :       4022 :         return peek_stack(emit, depth)->vtype;
     801                 :            :     } else {
     802                 :            :         // Type is always PYOBJ even if the intermediate stored value is not
     803                 :            :         return VTYPE_PYOBJ;
     804                 :            :     }
     805                 :            : }
     806                 :            : 
     807                 :            : // pos=1 is TOS, pos=2 is next, etc
     808                 :            : // use pos=0 for no skipping
     809                 :     228521 : STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
     810                 :     228521 :     skip_stack_pos = emit->stack_size - skip_stack_pos;
     811         [ +  + ]:     725757 :     for (int i = 0; i < emit->stack_size; i++) {
     812         [ +  + ]:     497236 :         if (i != skip_stack_pos) {
     813                 :     310979 :             stack_info_t *si = &emit->stack_info[i];
     814   [ +  +  +  + ]:     310979 :             if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
     815                 :      29178 :                 si->kind = STACK_VALUE;
     816                 :      29178 :                 emit_native_mov_state_reg(emit, emit->stack_start + i, si->data.u_reg);
     817                 :            :             }
     818                 :            :         }
     819                 :            :     }
     820                 :     228521 : }
     821                 :            : 
     822                 :            : // Ensures all unsettled registers that hold Python values are copied to the
     823                 :            : // concrete Python stack.  All registers are then free to use.
     824                 :     465344 : STATIC void need_reg_all(emit_t *emit) {
     825         [ +  + ]:    1042438 :     for (int i = 0; i < emit->stack_size; i++) {
     826                 :     577094 :         stack_info_t *si = &emit->stack_info[i];
     827         [ +  + ]:     577094 :         if (si->kind == STACK_REG) {
     828                 :      94143 :             DEBUG_printf("    reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
     829                 :      94143 :             si->kind = STACK_VALUE;
     830                 :      94143 :             emit_native_mov_state_reg(emit, emit->stack_start + i, si->data.u_reg);
     831                 :            :         }
     832                 :            :     }
     833                 :     465344 : }
     834                 :            : 
     835                 :      53397 : STATIC vtype_kind_t load_reg_stack_imm(emit_t *emit, int reg_dest, const stack_info_t *si, bool convert_to_pyobj) {
     836   [ +  +  +  + ]:      53397 :     if (!convert_to_pyobj && emit->do_viper_types) {
     837                 :        632 :         ASM_MOV_REG_IMM(emit->as, reg_dest, si->data.u_imm);
     838                 :        632 :         return si->vtype;
     839                 :            :     } else {
     840         [ +  + ]:      52765 :         if (si->vtype == VTYPE_PYOBJ) {
     841                 :        744 :             ASM_MOV_REG_IMM(emit->as, reg_dest, si->data.u_imm);
     842         [ +  + ]:      52021 :         } else if (si->vtype == VTYPE_BOOL) {
     843                 :       1581 :             emit_native_mov_reg_const(emit, reg_dest, MP_F_CONST_FALSE_OBJ + si->data.u_imm);
     844         [ +  + ]:      50440 :         } else if (si->vtype == VTYPE_INT || si->vtype == VTYPE_UINT) {
     845                 :      32067 :             ASM_MOV_REG_IMM(emit->as, reg_dest, (uintptr_t)MP_OBJ_NEW_SMALL_INT(si->data.u_imm));
     846         [ +  + ]:      18373 :         } else if (si->vtype == VTYPE_PTR_NONE) {
     847                 :      18369 :             emit_native_mov_reg_const(emit, reg_dest, MP_F_CONST_NONE_OBJ);
     848                 :            :         } else {
     849                 :          4 :             mp_raise_NotImplementedError(MP_ERROR_TEXT("conversion to object"));
     850                 :            :         }
     851                 :      52761 :         return VTYPE_PYOBJ;
     852                 :            :     }
     853                 :            : }
     854                 :            : 
     855                 :            : // Copies all unsettled registers and immediates that are Python values into the
     856                 :            : // concrete Python stack.  This ensures the concrete Python stack holds valid
     857                 :            : // values for the current stack_size.
     858                 :            : // This function may clobber REG_TEMP1.
     859                 :     109841 : STATIC void need_stack_settled(emit_t *emit) {
     860                 :     109841 :     DEBUG_printf("  need_stack_settled; stack_size=%d\n", emit->stack_size);
     861                 :     109841 :     need_reg_all(emit);
     862         [ +  + ]:     176231 :     for (int i = 0; i < emit->stack_size; i++) {
     863                 :      66390 :         stack_info_t *si = &emit->stack_info[i];
     864         [ +  + ]:      66390 :         if (si->kind == STACK_IMM) {
     865                 :       1809 :             DEBUG_printf("    imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
     866                 :       1809 :             si->kind = STACK_VALUE;
     867                 :            :             // using REG_TEMP1 to avoid clobbering REG_TEMP0 (aka REG_RET)
     868                 :       1809 :             si->vtype = load_reg_stack_imm(emit, REG_TEMP1, si, false);
     869                 :       1809 :             emit_native_mov_state_reg(emit, emit->stack_start + i, REG_TEMP1);
     870                 :            :         }
     871                 :            :     }
     872                 :     109841 : }
     873                 :            : 
     874                 :            : // pos=1 is TOS, pos=2 is next, etc
     875                 :     180796 : STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
     876                 :     180796 :     need_reg_single(emit, reg_dest, pos);
     877                 :     180796 :     stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
     878                 :     180796 :     *vtype = si->vtype;
     879   [ +  +  +  - ]:     180796 :     switch (si->kind) {
     880                 :      59987 :         case STACK_VALUE:
     881                 :      59987 :             emit_native_mov_reg_state(emit, reg_dest, emit->stack_start + emit->stack_size - pos);
     882                 :      59987 :             break;
     883                 :            : 
     884                 :      91755 :         case STACK_REG:
     885         [ +  + ]:      91755 :             if (si->data.u_reg != reg_dest) {
     886                 :      86085 :                 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
     887                 :            :             }
     888                 :            :             break;
     889                 :            : 
     890                 :      29054 :         case STACK_IMM:
     891                 :      29054 :             *vtype = load_reg_stack_imm(emit, reg_dest, si, false);
     892                 :      29054 :             break;
     893                 :            :     }
     894                 :     180796 : }
     895                 :            : 
     896                 :            : // does an efficient X=pop(); discard(); push(X)
     897                 :            : // needs a (non-temp) register in case the poped element was stored in the stack
     898                 :        521 : STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
     899                 :        521 :     stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
     900                 :        521 :     si[0] = si[1];
     901         [ +  + ]:        521 :     if (si->kind == STACK_VALUE) {
     902                 :            :         // if folded element was on the stack we need to put it in a register
     903                 :        507 :         emit_native_mov_reg_state(emit, reg_dest, emit->stack_start + emit->stack_size - 1);
     904                 :        507 :         si->kind = STACK_REG;
     905                 :        507 :         si->data.u_reg = reg_dest;
     906                 :            :     }
     907                 :        521 :     adjust_stack(emit, -1);
     908                 :        521 : }
     909                 :            : 
     910                 :            : // If stacked value is in a register and the register is not r1 or r2, then
     911                 :            : // *reg_dest is set to that register.  Otherwise the value is put in *reg_dest.
     912                 :       7341 : STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *reg_dest, int not_r1, int not_r2) {
     913                 :       7341 :     stack_info_t *si = peek_stack(emit, 0);
     914   [ +  +  +  +  :       7341 :     if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
                   +  - ]
     915                 :       5461 :         *vtype = si->vtype;
     916                 :       5461 :         *reg_dest = si->data.u_reg;
     917                 :       5461 :         need_reg_single(emit, *reg_dest, 1);
     918                 :            :     } else {
     919                 :       1880 :         emit_access_stack(emit, 1, vtype, *reg_dest);
     920                 :            :     }
     921                 :       7341 :     adjust_stack(emit, -1);
     922                 :       7341 : }
     923                 :            : 
     924                 :      38729 : STATIC void emit_pre_pop_discard(emit_t *emit) {
     925                 :      38729 :     adjust_stack(emit, -1);
     926                 :      38729 : }
     927                 :            : 
     928                 :     177155 : STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
     929                 :     177155 :     emit_access_stack(emit, 1, vtype, reg_dest);
     930                 :     177155 :     adjust_stack(emit, -1);
     931                 :     177155 : }
     932                 :            : 
     933                 :      22052 : STATIC void emit_pre_pop_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb) {
     934                 :      22052 :     emit_pre_pop_reg(emit, vtypea, rega);
     935                 :      22052 :     emit_pre_pop_reg(emit, vtypeb, regb);
     936                 :      22052 : }
     937                 :            : 
     938                 :       3336 : STATIC void emit_pre_pop_reg_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb, vtype_kind_t *vtypec, int regc) {
     939                 :       3336 :     emit_pre_pop_reg(emit, vtypea, rega);
     940                 :       3336 :     emit_pre_pop_reg(emit, vtypeb, regb);
     941                 :       3336 :     emit_pre_pop_reg(emit, vtypec, regc);
     942                 :       3336 : }
     943                 :            : 
     944                 :            : STATIC void emit_post(emit_t *emit) {
     945                 :            :     (void)emit;
     946                 :            : }
     947                 :            : 
     948                 :         14 : STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
     949                 :         14 :     stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
     950                 :         14 :     si->vtype = new_vtype;
     951                 :         14 : }
     952                 :            : 
     953                 :     251107 : STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
     954                 :     251107 :     ensure_extra_stack(emit, 1);
     955                 :     251107 :     stack_info_t *si = &emit->stack_info[emit->stack_size];
     956                 :     251107 :     si->vtype = vtype;
     957                 :     251107 :     si->kind = STACK_REG;
     958                 :     251107 :     si->data.u_reg = reg;
     959                 :     251107 :     adjust_stack(emit, 1);
     960                 :     251107 : }
     961                 :            : 
     962                 :      54325 : STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
     963                 :      54325 :     ensure_extra_stack(emit, 1);
     964                 :      54325 :     stack_info_t *si = &emit->stack_info[emit->stack_size];
     965                 :      54325 :     si->vtype = vtype;
     966                 :      54325 :     si->kind = STACK_IMM;
     967                 :      54325 :     si->data.u_imm = imm;
     968                 :      54325 :     adjust_stack(emit, 1);
     969                 :      54325 : }
     970                 :            : 
     971                 :       6201 : STATIC void emit_post_push_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb) {
     972                 :       6201 :     emit_post_push_reg(emit, vtypea, rega);
     973                 :       6201 :     emit_post_push_reg(emit, vtypeb, regb);
     974                 :       6201 : }
     975                 :            : 
     976                 :        123 : STATIC void emit_post_push_reg_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb, vtype_kind_t vtypec, int regc) {
     977                 :        123 :     emit_post_push_reg(emit, vtypea, rega);
     978                 :        123 :     emit_post_push_reg(emit, vtypeb, regb);
     979                 :        123 :     emit_post_push_reg(emit, vtypec, regc);
     980                 :        123 : }
     981                 :            : 
     982                 :        405 : STATIC void emit_post_push_reg_reg_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb, vtype_kind_t vtypec, int regc, vtype_kind_t vtyped, int regd) {
     983                 :        405 :     emit_post_push_reg(emit, vtypea, rega);
     984                 :        405 :     emit_post_push_reg(emit, vtypeb, regb);
     985                 :        405 :     emit_post_push_reg(emit, vtypec, regc);
     986                 :        405 :     emit_post_push_reg(emit, vtyped, regd);
     987                 :        405 : }
     988                 :            : 
     989                 :      73815 : STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
     990                 :      73815 :     need_reg_all(emit);
     991                 :      73815 :     ASM_CALL_IND(emit->as, fun_kind);
     992                 :      73815 : }
     993                 :            : 
     994                 :      69026 : STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val, int arg_reg) {
     995                 :      69026 :     need_reg_all(emit);
     996                 :      69026 :     ASM_MOV_REG_IMM(emit->as, arg_reg, arg_val);
     997                 :      69026 :     ASM_CALL_IND(emit->as, fun_kind);
     998                 :      69026 : }
     999                 :            : 
    1000                 :      13599 : STATIC void emit_call_with_2_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val1, int arg_reg1, mp_int_t arg_val2, int arg_reg2) {
    1001                 :      13599 :     need_reg_all(emit);
    1002                 :      13599 :     ASM_MOV_REG_IMM(emit->as, arg_reg1, arg_val1);
    1003                 :      13599 :     ASM_MOV_REG_IMM(emit->as, arg_reg2, arg_val2);
    1004                 :      13599 :     ASM_CALL_IND(emit->as, fun_kind);
    1005                 :      13599 : }
    1006                 :            : 
    1007                 :     117800 : STATIC void emit_call_with_qstr_arg(emit_t *emit, mp_fun_kind_t fun_kind, qstr qst, int arg_reg) {
    1008                 :     117800 :     need_reg_all(emit);
    1009                 :     117800 :     emit_native_mov_reg_qstr(emit, arg_reg, qst);
    1010                 :     117800 :     ASM_CALL_IND(emit->as, fun_kind);
    1011                 :     117800 : }
    1012                 :            : 
    1013                 :            : // vtype of all n_pop objects is VTYPE_PYOBJ
    1014                 :            : // Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
    1015                 :            : // If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
    1016                 :            : // Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
    1017                 :      58964 : STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
    1018                 :      58964 :     need_reg_all(emit);
    1019                 :            : 
    1020                 :            :     // First, store any immediate values to their respective place on the stack.
    1021         [ +  + ]:     174195 :     for (mp_uint_t i = 0; i < n_pop; i++) {
    1022                 :     115235 :         stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
    1023                 :            :         // must push any imm's to stack
    1024                 :            :         // must convert them to VTYPE_PYOBJ for viper code
    1025         [ +  + ]:     115235 :         if (si->kind == STACK_IMM) {
    1026                 :      22534 :             si->kind = STACK_VALUE;
    1027                 :      22534 :             si->vtype = load_reg_stack_imm(emit, reg_dest, si, true);
    1028                 :      22530 :             emit_native_mov_state_reg(emit, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
    1029                 :            :         }
    1030                 :            : 
    1031                 :            :         // verify that this value is on the stack
    1032         [ -  + ]:     115231 :         assert(si->kind == STACK_VALUE);
    1033                 :            :     }
    1034                 :            : 
    1035                 :            :     // Second, convert any non-VTYPE_PYOBJ to that type.
    1036         [ +  + ]:     174191 :     for (mp_uint_t i = 0; i < n_pop; i++) {
    1037                 :     115231 :         stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
    1038         [ +  + ]:     115231 :         if (si->vtype != VTYPE_PYOBJ) {
    1039                 :        544 :             mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
    1040                 :        544 :             emit_native_mov_reg_state(emit, REG_ARG_1, local_num);
    1041                 :        544 :             emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, si->vtype, REG_ARG_2); // arg2 = type
    1042                 :        544 :             emit_native_mov_state_reg(emit, local_num, REG_RET);
    1043                 :        544 :             si->vtype = VTYPE_PYOBJ;
    1044                 :     115231 :             DEBUG_printf("  convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
    1045                 :            :         }
    1046                 :            :     }
    1047                 :            : 
    1048                 :            :     // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
    1049                 :      58960 :     adjust_stack(emit, -n_pop);
    1050                 :      58960 :     emit_native_mov_reg_state_addr(emit, reg_dest, emit->stack_start + emit->stack_size);
    1051                 :      58960 : }
    1052                 :            : 
    1053                 :            : // vtype of all n_push objects is VTYPE_PYOBJ
    1054                 :      14712 : STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
    1055                 :      14712 :     need_reg_all(emit);
    1056                 :      14712 :     ensure_extra_stack(emit, n_push);
    1057         [ +  + ]:      46626 :     for (mp_uint_t i = 0; i < n_push; i++) {
    1058                 :      31914 :         emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
    1059                 :      31914 :         emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
    1060                 :            :     }
    1061                 :      14712 :     emit_native_mov_reg_state_addr(emit, reg_dest, emit->stack_start + emit->stack_size);
    1062                 :      14712 :     adjust_stack(emit, n_push);
    1063                 :      14712 : }
    1064                 :            : 
    1065                 :       5646 : STATIC void emit_native_push_exc_stack(emit_t *emit, uint label, bool is_finally) {
    1066         [ +  + ]:       5646 :     if (emit->exc_stack_size + 1 > emit->exc_stack_alloc) {
    1067                 :          6 :         size_t new_alloc = emit->exc_stack_alloc + 4;
    1068                 :          6 :         emit->exc_stack = m_renew(exc_stack_entry_t, emit->exc_stack, emit->exc_stack_alloc, new_alloc);
    1069                 :          6 :         emit->exc_stack_alloc = new_alloc;
    1070                 :            :     }
    1071                 :            : 
    1072                 :       5646 :     exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size++];
    1073                 :       5646 :     e->label = label;
    1074                 :       5646 :     e->is_finally = is_finally;
    1075                 :       5646 :     e->unwind_label = UNWIND_LABEL_UNUSED;
    1076                 :       5646 :     e->is_active = true;
    1077                 :            : 
    1078                 :       5646 :     ASM_MOV_REG_PCREL(emit->as, REG_RET, label);
    1079                 :       5646 :     ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
    1080                 :       5646 : }
    1081                 :            : 
    1082                 :      10452 : STATIC void emit_native_leave_exc_stack(emit_t *emit, bool start_of_handler) {
    1083         [ -  + ]:      10452 :     assert(emit->exc_stack_size > 0);
    1084                 :            : 
    1085                 :            :     // Get current exception handler and deactivate it
    1086                 :      10452 :     exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
    1087                 :      10452 :     e->is_active = false;
    1088                 :            : 
    1089                 :            :     // Find next innermost active exception handler, to restore as current handler
    1090   [ +  +  +  + ]:      11637 :     for (--e; e >= emit->exc_stack && !e->is_active; --e) {
    1091                 :       1185 :     }
    1092                 :            : 
    1093                 :            :     // Update the PC of the new exception handler
    1094         [ +  + ]:      10452 :     if (e < emit->exc_stack) {
    1095                 :            :         // No active handler, clear handler PC to zero
    1096         [ +  + ]:       9855 :         if (start_of_handler) {
    1097                 :            :             // Optimisation: PC is already cleared by global exc handler
    1098                 :            :             return;
    1099                 :            :         }
    1100                 :       5268 :         ASM_XOR_REG_REG(emit->as, REG_RET, REG_RET);
    1101                 :            :     } else {
    1102                 :            :         // Found new active handler, get its PC
    1103                 :        597 :         ASM_MOV_REG_PCREL(emit->as, REG_RET, e->label);
    1104                 :            :     }
    1105                 :       5865 :     ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
    1106                 :            : }
    1107                 :            : 
    1108                 :       5646 : STATIC exc_stack_entry_t *emit_native_pop_exc_stack(emit_t *emit) {
    1109         [ -  + ]:       5646 :     assert(emit->exc_stack_size > 0);
    1110                 :       5646 :     exc_stack_entry_t *e = &emit->exc_stack[--emit->exc_stack_size];
    1111         [ -  + ]:       5646 :     assert(e->is_active == false);
    1112                 :       5646 :     return e;
    1113                 :            : }
    1114                 :            : 
    1115                 :      10353 : STATIC void emit_load_reg_with_object(emit_t *emit, int reg, mp_obj_t obj) {
    1116                 :      10353 :     emit->scope->scope_flags |= MP_SCOPE_FLAG_HASCONSTS;
    1117                 :      10353 :     size_t table_off = mp_emit_common_use_const_obj(emit->emit_common, obj);
    1118                 :      10353 :     emit_native_mov_reg_state(emit, REG_TEMP0, LOCAL_IDX_FUN_OBJ(emit));
    1119                 :      10353 :     ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_TEMP0, OFFSETOF_OBJ_FUN_BC_CONTEXT);
    1120                 :      10353 :     ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_TEMP0, OFFSETOF_MODULE_CONTEXT_OBJ_TABLE);
    1121                 :      10353 :     ASM_LOAD_REG_REG_OFFSET(emit->as, reg, REG_TEMP0, table_off);
    1122                 :      10353 : }
    1123                 :            : 
    1124                 :       7587 : STATIC void emit_load_reg_with_child(emit_t *emit, int reg, mp_raw_code_t *rc) {
    1125         [ +  + ]:       7587 :     size_t table_off = mp_emit_common_alloc_const_child(emit->emit_common, rc);
    1126                 :       7587 :     emit_native_mov_reg_state(emit, REG_TEMP0, LOCAL_IDX_FUN_OBJ(emit));
    1127                 :       7587 :     ASM_LOAD_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_TEMP0, OFFSETOF_OBJ_FUN_BC_CHILD_TABLE);
    1128                 :       7587 :     ASM_LOAD_REG_REG_OFFSET(emit->as, reg, REG_TEMP0, table_off);
    1129                 :       7587 : }
    1130                 :            : 
    1131                 :      65185 : STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
    1132                 :      65185 :     DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
    1133                 :            : 
    1134                 :      65185 :     bool is_finally = false;
    1135         [ +  + ]:      65185 :     if (emit->exc_stack_size > 0) {
    1136                 :      13971 :         exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
    1137   [ +  +  +  + ]:      13971 :         is_finally = e->is_finally && e->label == l;
    1138                 :            :     }
    1139                 :            : 
    1140                 :        840 :     if (is_finally) {
    1141                 :            :         // Label is at start of finally handler: store TOS into exception slot
    1142                 :        840 :         vtype_kind_t vtype;
    1143                 :        840 :         emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
    1144                 :        840 :         ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
    1145                 :            :     }
    1146                 :            : 
    1147                 :      65185 :     emit_native_pre(emit);
    1148                 :            :     // need to commit stack because we can jump here from elsewhere
    1149                 :      65185 :     need_stack_settled(emit);
    1150                 :      65185 :     mp_asm_base_label_assign(&emit->as->base, l);
    1151                 :      65185 :     emit_post(emit);
    1152                 :            : 
    1153         [ +  + ]:      65185 :     if (is_finally) {
    1154                 :            :         // Label is at start of finally handler: pop exception stack
    1155                 :        840 :         emit_native_leave_exc_stack(emit, false);
    1156                 :            :     }
    1157                 :      65185 : }
    1158                 :            : 
    1159                 :      11742 : STATIC void emit_native_global_exc_entry(emit_t *emit) {
    1160                 :            :     // Note: 4 labels are reserved for this function, starting at *emit->label_slot
    1161                 :            : 
    1162                 :      11742 :     emit->exit_label = *emit->label_slot;
    1163                 :            : 
    1164   [ +  +  +  + ]:      11742 :     if (NEED_GLOBAL_EXC_HANDLER(emit)) {
    1165                 :       8151 :         mp_uint_t nlr_label = *emit->label_slot + 1;
    1166                 :       8151 :         mp_uint_t start_label = *emit->label_slot + 2;
    1167                 :       8151 :         mp_uint_t global_except_label = *emit->label_slot + 3;
    1168                 :            : 
    1169         [ +  + ]:       8151 :         if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
    1170                 :            :             // Set new globals
    1171                 :       7447 :             emit_native_mov_reg_state(emit, REG_ARG_1, LOCAL_IDX_FUN_OBJ(emit));
    1172                 :       7447 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_1, REG_ARG_1, OFFSETOF_OBJ_FUN_BC_CONTEXT);
    1173                 :       7447 :             ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_1, REG_ARG_1, OFFSETOF_MODULE_CONTEXT_GLOBALS);
    1174                 :       7447 :             emit_call(emit, MP_F_NATIVE_SWAP_GLOBALS);
    1175                 :            : 
    1176                 :            :             // Save old globals (or NULL if globals didn't change)
    1177                 :       7447 :             emit_native_mov_state_reg(emit, LOCAL_IDX_OLD_GLOBALS(emit), REG_RET);
    1178                 :            :         }
    1179                 :            : 
    1180         [ +  + ]:       8151 :         if (emit->scope->exc_stack_size == 0) {
    1181         [ +  + ]:       5988 :             if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
    1182                 :            :                 // Optimisation: if globals didn't change don't push the nlr context
    1183                 :       5434 :                 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, start_label, false);
    1184                 :            :             }
    1185                 :            : 
    1186                 :            :             // Wrap everything in an nlr context
    1187                 :       5988 :             ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 0);
    1188                 :       5988 :             emit_call(emit, MP_F_NLR_PUSH);
    1189                 :            :             #if N_NLR_SETJMP
    1190                 :            :             ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 2);
    1191                 :            :             emit_call(emit, MP_F_SETJMP);
    1192                 :            :             #endif
    1193                 :       5988 :             ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, start_label, true);
    1194                 :            :         } else {
    1195                 :            :             // Clear the unwind state
    1196                 :       2163 :             ASM_XOR_REG_REG(emit->as, REG_TEMP0, REG_TEMP0);
    1197                 :       2163 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_TEMP0);
    1198                 :            : 
    1199                 :            :             // Put PC of start code block into REG_LOCAL_1
    1200                 :       2163 :             ASM_MOV_REG_PCREL(emit->as, REG_LOCAL_1, start_label);
    1201                 :            : 
    1202                 :            :             // Wrap everything in an nlr context
    1203                 :       2163 :             emit_native_label_assign(emit, nlr_label);
    1204                 :       2163 :             ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 0);
    1205                 :       2163 :             emit_call(emit, MP_F_NLR_PUSH);
    1206                 :            :             #if N_NLR_SETJMP
    1207                 :            :             ASM_MOV_REG_LOCAL_ADDR(emit->as, REG_ARG_1, 2);
    1208                 :            :             emit_call(emit, MP_F_SETJMP);
    1209                 :            :             #endif
    1210                 :       2163 :             ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, global_except_label, true);
    1211                 :            : 
    1212                 :            :             // Clear PC of current code block, and jump there to resume execution
    1213                 :       2163 :             ASM_XOR_REG_REG(emit->as, REG_TEMP0, REG_TEMP0);
    1214                 :       2163 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_TEMP0);
    1215                 :       2163 :             ASM_JUMP_REG(emit->as, REG_LOCAL_1);
    1216                 :            : 
    1217                 :            :             // Global exception handler: check for valid exception handler
    1218                 :       2163 :             emit_native_label_assign(emit, global_except_label);
    1219                 :       2163 :             ASM_MOV_REG_LOCAL(emit->as, REG_LOCAL_1, LOCAL_IDX_EXC_HANDLER_PC(emit));
    1220                 :       2163 :             ASM_JUMP_IF_REG_NONZERO(emit->as, REG_LOCAL_1, nlr_label, false);
    1221                 :            :         }
    1222                 :            : 
    1223         [ +  + ]:       8151 :         if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
    1224                 :            :             // Restore old globals
    1225                 :       7447 :             emit_native_mov_reg_state(emit, REG_ARG_1, LOCAL_IDX_OLD_GLOBALS(emit));
    1226                 :       7447 :             emit_call(emit, MP_F_NATIVE_SWAP_GLOBALS);
    1227                 :            :         }
    1228                 :            : 
    1229         [ +  + ]:       8151 :         if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
    1230                 :            :             // Store return value in state[0]
    1231                 :        704 :             ASM_MOV_REG_LOCAL(emit->as, REG_TEMP0, LOCAL_IDX_EXC_VAL(emit));
    1232                 :        704 :             ASM_STORE_REG_REG_OFFSET(emit->as, REG_TEMP0, REG_GENERATOR_STATE, OFFSETOF_CODE_STATE_STATE);
    1233                 :            : 
    1234                 :            :             // Load return kind
    1235                 :        704 :             ASM_MOV_REG_IMM(emit->as, REG_PARENT_RET, MP_VM_RETURN_EXCEPTION);
    1236                 :            : 
    1237                 :        704 :             ASM_EXIT(emit->as);
    1238                 :            :         } else {
    1239                 :            :             // Re-raise exception out to caller
    1240                 :       7447 :             ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
    1241                 :       7447 :             emit_call(emit, MP_F_NATIVE_RAISE);
    1242                 :            :         }
    1243                 :            : 
    1244                 :            :         // Label for start of function
    1245                 :       8151 :         emit_native_label_assign(emit, start_label);
    1246                 :            : 
    1247         [ +  + ]:       8151 :         if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
    1248                 :        704 :             emit_native_mov_reg_state(emit, REG_TEMP0, LOCAL_IDX_GEN_PC(emit));
    1249                 :        704 :             ASM_JUMP_REG(emit->as, REG_TEMP0);
    1250                 :        704 :             emit->start_offset = mp_asm_base_get_code_pos(&emit->as->base);
    1251                 :            : 
    1252                 :            :             // This is the first entry of the generator
    1253                 :            : 
    1254                 :            :             // Check LOCAL_IDX_EXC_VAL for any injected value
    1255                 :        704 :             ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
    1256                 :        704 :             emit_call(emit, MP_F_NATIVE_RAISE);
    1257                 :            :         }
    1258                 :            :     }
    1259                 :      11742 : }
    1260                 :            : 
    1261                 :      11726 : STATIC void emit_native_global_exc_exit(emit_t *emit) {
    1262                 :            :     // Label for end of function
    1263                 :      11726 :     emit_native_label_assign(emit, emit->exit_label);
    1264                 :            : 
    1265   [ +  +  +  + ]:      11726 :     if (NEED_GLOBAL_EXC_HANDLER(emit)) {
    1266                 :            :         // Get old globals
    1267         [ +  + ]:       8139 :         if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
    1268                 :       7443 :             emit_native_mov_reg_state(emit, REG_ARG_1, LOCAL_IDX_OLD_GLOBALS(emit));
    1269                 :            : 
    1270         [ +  + ]:       7443 :             if (emit->scope->exc_stack_size == 0) {
    1271                 :            :                 // Optimisation: if globals didn't change then don't restore them and don't do nlr_pop
    1272                 :       5430 :                 ASM_JUMP_IF_REG_ZERO(emit->as, REG_ARG_1, emit->exit_label + 1, false);
    1273                 :            :             }
    1274                 :            : 
    1275                 :            :             // Restore old globals
    1276                 :       7443 :             emit_call(emit, MP_F_NATIVE_SWAP_GLOBALS);
    1277                 :            :         }
    1278                 :            : 
    1279                 :            :         // Pop the nlr context
    1280                 :       8139 :         emit_call(emit, MP_F_NLR_POP);
    1281                 :            : 
    1282         [ +  + ]:       8139 :         if (!(emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR)) {
    1283         [ +  + ]:       7443 :             if (emit->scope->exc_stack_size == 0) {
    1284                 :            :                 // Destination label for above optimisation
    1285                 :       5430 :                 emit_native_label_assign(emit, emit->exit_label + 1);
    1286                 :            :             }
    1287                 :            :         }
    1288                 :            : 
    1289                 :            :         // Load return value
    1290                 :       8139 :         ASM_MOV_REG_LOCAL(emit->as, REG_PARENT_RET, LOCAL_IDX_RET_VAL(emit));
    1291                 :            :     }
    1292                 :            : 
    1293                 :      11726 :     ASM_EXIT(emit->as);
    1294                 :      11726 : }
    1295                 :            : 
    1296                 :       2265 : STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
    1297                 :       2265 :     DEBUG_printf("import_name %s\n", qstr_str(qst));
    1298                 :            : 
    1299                 :            :     // get arguments from stack: arg2 = fromlist, arg3 = level
    1300                 :            :     // If using viper types these arguments must be converted to proper objects, and
    1301                 :            :     // to accomplish this viper types are turned off for the emit_pre_pop_reg_reg call.
    1302                 :       2265 :     bool orig_do_viper_types = emit->do_viper_types;
    1303                 :       2265 :     emit->do_viper_types = false;
    1304                 :       2265 :     vtype_kind_t vtype_fromlist;
    1305                 :       2265 :     vtype_kind_t vtype_level;
    1306                 :       2265 :     emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3);
    1307         [ -  + ]:       2265 :     assert(vtype_fromlist == VTYPE_PYOBJ);
    1308         [ -  + ]:       2265 :     assert(vtype_level == VTYPE_PYOBJ);
    1309                 :       2265 :     emit->do_viper_types = orig_do_viper_types;
    1310                 :            : 
    1311                 :       2265 :     emit_call_with_qstr_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
    1312                 :       2265 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1313                 :       2265 : }
    1314                 :            : 
    1315                 :        885 : STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
    1316                 :        885 :     DEBUG_printf("import_from %s\n", qstr_str(qst));
    1317                 :        885 :     emit_native_pre(emit);
    1318                 :        885 :     vtype_kind_t vtype_module;
    1319                 :        885 :     emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
    1320         [ -  + ]:        885 :     assert(vtype_module == VTYPE_PYOBJ);
    1321                 :        885 :     emit_call_with_qstr_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
    1322                 :        885 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1323                 :        885 : }
    1324                 :            : 
    1325                 :         96 : STATIC void emit_native_import_star(emit_t *emit) {
    1326                 :         96 :     DEBUG_printf("import_star\n");
    1327                 :         96 :     vtype_kind_t vtype_module;
    1328                 :         96 :     emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
    1329         [ -  + ]:         96 :     assert(vtype_module == VTYPE_PYOBJ);
    1330                 :         96 :     emit_call(emit, MP_F_IMPORT_ALL);
    1331                 :         96 :     emit_post(emit);
    1332                 :         96 : }
    1333                 :            : 
    1334                 :       3246 : STATIC void emit_native_import(emit_t *emit, qstr qst, int kind) {
    1335         [ +  + ]:       3246 :     if (kind == MP_EMIT_IMPORT_NAME) {
    1336                 :       2265 :         emit_native_import_name(emit, qst);
    1337         [ +  + ]:        981 :     } else if (kind == MP_EMIT_IMPORT_FROM) {
    1338                 :        885 :         emit_native_import_from(emit, qst);
    1339                 :            :     } else {
    1340                 :         96 :         emit_native_import_star(emit);
    1341                 :            :     }
    1342                 :       3246 : }
    1343                 :            : 
    1344                 :      19992 : STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
    1345                 :      19992 :     DEBUG_printf("load_const_tok(tok=%u)\n", tok);
    1346         [ +  + ]:      19992 :     if (tok == MP_TOKEN_ELLIPSIS) {
    1347                 :         12 :         emit_native_load_const_obj(emit, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
    1348                 :            :     } else {
    1349                 :      19980 :         emit_native_pre(emit);
    1350         [ +  + ]:      19980 :         if (tok == MP_TOKEN_KW_NONE) {
    1351                 :      18387 :             emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
    1352                 :            :         } else {
    1353                 :       1593 :             emit_post_push_imm(emit, VTYPE_BOOL, tok == MP_TOKEN_KW_FALSE ? 0 : 1);
    1354                 :            :         }
    1355                 :            :     }
    1356                 :      19992 : }
    1357                 :            : 
    1358                 :      32793 : STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
    1359                 :      32793 :     DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
    1360                 :      32793 :     emit_native_pre(emit);
    1361                 :      32793 :     emit_post_push_imm(emit, VTYPE_INT, arg);
    1362                 :      32793 : }
    1363                 :            : 
    1364                 :      21552 : STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
    1365                 :      21552 :     emit_native_pre(emit);
    1366                 :            :     // TODO: Eventually we want to be able to work with raw pointers in viper to
    1367                 :            :     // do native array access.  For now we just load them as any other object.
    1368                 :            :     /*
    1369                 :            :     if (emit->do_viper_types) {
    1370                 :            :         // load a pointer to the asciiz string?
    1371                 :            :         emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
    1372                 :            :     } else
    1373                 :            :     */
    1374                 :            :     {
    1375                 :      21552 :         need_reg_single(emit, REG_TEMP0, 0);
    1376                 :      21552 :         emit_native_mov_reg_qstr_obj(emit, REG_TEMP0, qst);
    1377                 :      21552 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_TEMP0);
    1378                 :            :     }
    1379                 :      21552 : }
    1380                 :            : 
    1381                 :      10353 : STATIC void emit_native_load_const_obj(emit_t *emit, mp_obj_t obj) {
    1382                 :      10353 :     emit_native_pre(emit);
    1383                 :      10353 :     need_reg_single(emit, REG_RET, 0);
    1384                 :      10353 :     emit_load_reg_with_object(emit, REG_RET, obj);
    1385                 :      10353 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1386                 :      10353 : }
    1387                 :            : 
    1388                 :        744 : STATIC void emit_native_load_null(emit_t *emit) {
    1389                 :        744 :     emit_native_pre(emit);
    1390                 :        744 :     emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
    1391                 :        744 : }
    1392                 :            : 
    1393                 :      22369 : STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
    1394                 :      22369 :     DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
    1395                 :      22369 :     vtype_kind_t vtype = emit->local_vtype[local_num];
    1396         [ +  + ]:      22369 :     if (vtype == VTYPE_UNBOUND) {
    1397                 :          4 :         EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("local '%q' used before type known"), qst);
    1398                 :            :     }
    1399                 :      22369 :     emit_native_pre(emit);
    1400   [ +  +  +  +  :      22369 :     if (local_num < MAX_REGS_FOR_LOCAL_VARS && CAN_USE_REGS_FOR_LOCALS(emit)) {
                   +  + ]
    1401                 :      13123 :         emit_post_push_reg(emit, vtype, reg_local_table[local_num]);
    1402                 :            :     } else {
    1403                 :       9246 :         need_reg_single(emit, REG_TEMP0, 0);
    1404                 :       9246 :         emit_native_mov_reg_state(emit, REG_TEMP0, LOCAL_IDX_LOCAL_VAR(emit, local_num));
    1405                 :       9246 :         emit_post_push_reg(emit, vtype, REG_TEMP0);
    1406                 :            :     }
    1407                 :      22369 : }
    1408                 :            : 
    1409                 :        303 : STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
    1410                 :        303 :     DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
    1411                 :        303 :     need_reg_single(emit, REG_RET, 0);
    1412                 :        303 :     emit_native_load_fast(emit, qst, local_num);
    1413                 :        303 :     vtype_kind_t vtype;
    1414                 :        303 :     int reg_base = REG_RET;
    1415                 :        303 :     emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
    1416                 :        303 :     ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
    1417                 :            :     // closed over vars are always Python objects
    1418                 :        303 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1419                 :        303 : }
    1420                 :            : 
    1421                 :      22192 : STATIC void emit_native_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
    1422         [ +  + ]:      22192 :     if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
    1423                 :      21889 :         emit_native_load_fast(emit, qst, local_num);
    1424                 :            :     } else {
    1425                 :        303 :         emit_native_load_deref(emit, qst, local_num);
    1426                 :            :     }
    1427                 :      22192 : }
    1428                 :            : 
    1429                 :      72486 : STATIC void emit_native_load_global(emit_t *emit, qstr qst, int kind) {
    1430                 :      72486 :     MP_STATIC_ASSERT(MP_F_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_LOAD_NAME);
    1431                 :      72486 :     MP_STATIC_ASSERT(MP_F_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_LOAD_GLOBAL);
    1432                 :      72486 :     emit_native_pre(emit);
    1433         [ +  + ]:      72486 :     if (kind == MP_EMIT_IDOP_GLOBAL_NAME) {
    1434                 :            :         DEBUG_printf("load_name(%s)\n", qstr_str(qst));
    1435                 :            :     } else {
    1436                 :      14082 :         DEBUG_printf("load_global(%s)\n", qstr_str(qst));
    1437         [ +  + ]:      14082 :         if (emit->do_viper_types) {
    1438                 :            :             // check for builtin casting operators
    1439                 :        594 :             int native_type = mp_native_type_from_qstr(qst);
    1440         [ +  + ]:        594 :             if (native_type >= MP_NATIVE_TYPE_BOOL) {
    1441                 :        100 :                 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, native_type);
    1442                 :        100 :                 return;
    1443                 :            :             }
    1444                 :            :         }
    1445                 :            :     }
    1446                 :      72386 :     emit_call_with_qstr_arg(emit, MP_F_LOAD_NAME + kind, qst, REG_ARG_1);
    1447                 :      72386 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1448                 :            : }
    1449                 :            : 
    1450                 :       7050 : STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
    1451                 :            :     // depends on type of subject:
    1452                 :            :     //  - integer, function, pointer to integers: error
    1453                 :            :     //  - pointer to structure: get member, quite easy
    1454                 :            :     //  - Python object: call mp_load_attr, and needs to be typed to convert result
    1455                 :       7050 :     vtype_kind_t vtype_base;
    1456                 :       7050 :     emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
    1457         [ -  + ]:       7050 :     assert(vtype_base == VTYPE_PYOBJ);
    1458                 :       7050 :     emit_call_with_qstr_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
    1459                 :       7050 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1460                 :       7050 : }
    1461                 :            : 
    1462                 :      12747 : STATIC void emit_native_load_method(emit_t *emit, qstr qst, bool is_super) {
    1463         [ +  + ]:      12747 :     if (is_super) {
    1464                 :         42 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, 3); // arg2 = dest ptr
    1465                 :         42 :         emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_2, 2); // arg2 = dest ptr
    1466                 :         42 :         emit_call_with_qstr_arg(emit, MP_F_LOAD_SUPER_METHOD, qst, REG_ARG_1); // arg1 = method name
    1467                 :            :     } else {
    1468                 :      12705 :         vtype_kind_t vtype_base;
    1469                 :      12705 :         emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
    1470         [ -  + ]:      12705 :         assert(vtype_base == VTYPE_PYOBJ);
    1471                 :      12705 :         emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
    1472                 :      12705 :         emit_call_with_qstr_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
    1473                 :            :     }
    1474                 :      12747 : }
    1475                 :            : 
    1476                 :       1308 : STATIC void emit_native_load_build_class(emit_t *emit) {
    1477                 :       1308 :     emit_native_pre(emit);
    1478                 :       1308 :     emit_call(emit, MP_F_LOAD_BUILD_CLASS);
    1479                 :       1308 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1480                 :       1308 : }
    1481                 :            : 
    1482                 :       3437 : STATIC void emit_native_load_subscr(emit_t *emit) {
    1483                 :       3437 :     DEBUG_printf("load_subscr\n");
    1484                 :            :     // need to compile: base[index]
    1485                 :            : 
    1486                 :            :     // pop: index, base
    1487                 :            :     // optimise case where index is an immediate
    1488                 :       3437 :     vtype_kind_t vtype_base = peek_vtype(emit, 1);
    1489                 :            : 
    1490         [ +  + ]:       3437 :     if (vtype_base == VTYPE_PYOBJ) {
    1491                 :            :         // standard Python subscr
    1492                 :            :         // TODO factor this implicit cast code with other uses of it
    1493                 :       3291 :         vtype_kind_t vtype_index = peek_vtype(emit, 0);
    1494         [ +  + ]:       3291 :         if (vtype_index == VTYPE_PYOBJ) {
    1495                 :       3285 :             emit_pre_pop_reg(emit, &vtype_index, REG_ARG_2);
    1496                 :            :         } else {
    1497                 :          6 :             emit_pre_pop_reg(emit, &vtype_index, REG_ARG_1);
    1498                 :          6 :             emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_index, REG_ARG_2); // arg2 = type
    1499                 :          6 :             ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
    1500                 :            :         }
    1501                 :       3291 :         emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
    1502                 :       3291 :         emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_SENTINEL, REG_ARG_3);
    1503                 :       3291 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    1504                 :            :     } else {
    1505                 :            :         // viper load
    1506                 :            :         // TODO The different machine architectures have very different
    1507                 :            :         // capabilities and requirements for loads, so probably best to
    1508                 :            :         // write a completely separate load-optimiser for each one.
    1509                 :        146 :         stack_info_t *top = peek_stack(emit, 0);
    1510         [ +  + ]:        146 :         if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
    1511                 :            :             // index is an immediate
    1512                 :         70 :             mp_int_t index_value = top->data.u_imm;
    1513                 :         70 :             emit_pre_pop_discard(emit); // discard index
    1514                 :         70 :             int reg_base = REG_ARG_1;
    1515                 :         70 :             int reg_index = REG_ARG_2;
    1516                 :         70 :             emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
    1517                 :         70 :             need_reg_single(emit, REG_RET, 0);
    1518   [ +  +  +  + ]:         70 :             switch (vtype_base) {
    1519                 :         42 :                 case VTYPE_PTR8: {
    1520                 :            :                     // pointer to 8-bit memory
    1521                 :            :                     // TODO optimise to use thumb ldrb r1, [r2, r3]
    1522         [ +  + ]:         42 :                     if (index_value != 0) {
    1523                 :            :                         // index is non-zero
    1524                 :            :                         #if N_THUMB
    1525                 :            :                         if (index_value > 0 && index_value < 32) {
    1526                 :            :                             asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
    1527                 :            :                             break;
    1528                 :            :                         }
    1529                 :            :                         #endif
    1530                 :         30 :                         need_reg_single(emit, reg_index, 0);
    1531                 :         30 :                         ASM_MOV_REG_IMM(emit->as, reg_index, index_value);
    1532                 :         30 :                         ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
    1533                 :         30 :                         reg_base = reg_index;
    1534                 :            :                     }
    1535                 :         42 :                     ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
    1536                 :         42 :                     break;
    1537                 :            :                 }
    1538                 :         12 :                 case VTYPE_PTR16: {
    1539                 :            :                     // pointer to 16-bit memory
    1540         [ +  + ]:         12 :                     if (index_value != 0) {
    1541                 :            :                         // index is a non-zero immediate
    1542                 :            :                         #if N_THUMB
    1543                 :            :                         if (index_value > 0 && index_value < 32) {
    1544                 :            :                             asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
    1545                 :            :                             break;
    1546                 :            :                         }
    1547                 :            :                         #endif
    1548                 :          6 :                         need_reg_single(emit, reg_index, 0);
    1549                 :          6 :                         ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 1);
    1550                 :          6 :                         ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
    1551                 :          6 :                         reg_base = reg_index;
    1552                 :            :                     }
    1553                 :         12 :                     ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
    1554                 :         12 :                     break;
    1555                 :            :                 }
    1556                 :         12 :                 case VTYPE_PTR32: {
    1557                 :            :                     // pointer to 32-bit memory
    1558         [ +  + ]:         12 :                     if (index_value != 0) {
    1559                 :            :                         // index is a non-zero immediate
    1560                 :            :                         #if N_THUMB
    1561                 :            :                         if (index_value > 0 && index_value < 32) {
    1562                 :            :                             asm_thumb_ldr_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
    1563                 :            :                             break;
    1564                 :            :                         }
    1565                 :            :                         #endif
    1566                 :          6 :                         need_reg_single(emit, reg_index, 0);
    1567                 :          6 :                         ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 2);
    1568                 :          6 :                         ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
    1569                 :          6 :                         reg_base = reg_index;
    1570                 :            :                     }
    1571                 :         12 :                     ASM_LOAD32_REG_REG(emit->as, REG_RET, reg_base); // load from (base+4*index)
    1572                 :         12 :                     break;
    1573                 :            :                 }
    1574                 :          4 :                 default:
    1575                 :         70 :                     EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1576                 :            :                         MP_ERROR_TEXT("can't load from '%q'"), vtype_to_qstr(vtype_base));
    1577                 :            :             }
    1578                 :            :         } else {
    1579                 :            :             // index is not an immediate
    1580                 :         76 :             vtype_kind_t vtype_index;
    1581                 :         76 :             int reg_index = REG_ARG_2;
    1582                 :         76 :             emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
    1583                 :         76 :             emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
    1584                 :         76 :             need_reg_single(emit, REG_RET, 0);
    1585         [ +  + ]:         76 :             if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
    1586                 :          4 :                 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1587                 :            :                     MP_ERROR_TEXT("can't load with '%q' index"), vtype_to_qstr(vtype_index));
    1588                 :            :             }
    1589   [ +  +  +  + ]:         76 :             switch (vtype_base) {
    1590                 :         48 :                 case VTYPE_PTR8: {
    1591                 :            :                     // pointer to 8-bit memory
    1592                 :            :                     // TODO optimise to use thumb ldrb r1, [r2, r3]
    1593                 :         48 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1594                 :         48 :                     ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
    1595                 :         48 :                     break;
    1596                 :            :                 }
    1597                 :         12 :                 case VTYPE_PTR16: {
    1598                 :            :                     // pointer to 16-bit memory
    1599                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1600                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1601                 :         12 :                     ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
    1602                 :         12 :                     break;
    1603                 :            :                 }
    1604                 :         12 :                 case VTYPE_PTR32: {
    1605                 :            :                     // pointer to word-size memory
    1606                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1607                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1608                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1609                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1610                 :         12 :                     ASM_LOAD32_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+4*index)
    1611                 :         12 :                     break;
    1612                 :            :                 }
    1613                 :          4 :                 default:
    1614                 :         76 :                     EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1615                 :            :                         MP_ERROR_TEXT("can't load from '%q'"), vtype_to_qstr(vtype_base));
    1616                 :            :             }
    1617                 :            :         }
    1618                 :        146 :         emit_post_push_reg(emit, VTYPE_INT, REG_RET);
    1619                 :            :     }
    1620                 :       3437 : }
    1621                 :            : 
    1622                 :       5431 : STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
    1623                 :       5431 :     vtype_kind_t vtype;
    1624   [ +  +  +  +  :       5431 :     if (local_num < MAX_REGS_FOR_LOCAL_VARS && CAN_USE_REGS_FOR_LOCALS(emit)) {
                   +  + ]
    1625                 :       1132 :         emit_pre_pop_reg(emit, &vtype, reg_local_table[local_num]);
    1626                 :            :     } else {
    1627                 :       4299 :         emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
    1628                 :       4299 :         emit_native_mov_state_reg(emit, LOCAL_IDX_LOCAL_VAR(emit, local_num), REG_TEMP0);
    1629                 :            :     }
    1630                 :       5431 :     emit_post(emit);
    1631                 :            : 
    1632                 :            :     // check types
    1633         [ +  + ]:       5431 :     if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
    1634                 :            :         // first time this local is assigned, so give it a type of the object stored in it
    1635                 :       3969 :         emit->local_vtype[local_num] = vtype;
    1636         [ +  + ]:       1462 :     } else if (emit->local_vtype[local_num] != vtype) {
    1637                 :            :         // type of local is not the same as object stored in it
    1638                 :          4 :         EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1639                 :            :             MP_ERROR_TEXT("local '%q' has type '%q' but source is '%q'"),
    1640                 :            :             qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
    1641                 :            :     }
    1642                 :       5431 : }
    1643                 :            : 
    1644                 :        177 : STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
    1645                 :        177 :     DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
    1646                 :        177 :     need_reg_single(emit, REG_TEMP0, 0);
    1647                 :        177 :     need_reg_single(emit, REG_TEMP1, 0);
    1648                 :        177 :     emit_native_load_fast(emit, qst, local_num);
    1649                 :        177 :     vtype_kind_t vtype;
    1650                 :        177 :     int reg_base = REG_TEMP0;
    1651                 :        177 :     emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
    1652                 :        177 :     int reg_src = REG_TEMP1;
    1653                 :        177 :     emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
    1654                 :        177 :     ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
    1655                 :        177 :     emit_post(emit);
    1656                 :        177 : }
    1657                 :            : 
    1658                 :       5401 : STATIC void emit_native_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
    1659         [ +  + ]:       5401 :     if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
    1660                 :       5224 :         emit_native_store_fast(emit, qst, local_num);
    1661                 :            :     } else {
    1662                 :        177 :         emit_native_store_deref(emit, qst, local_num);
    1663                 :            :     }
    1664                 :       5401 : }
    1665                 :            : 
    1666                 :      20016 : STATIC void emit_native_store_global(emit_t *emit, qstr qst, int kind) {
    1667                 :      20016 :     MP_STATIC_ASSERT(MP_F_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_STORE_NAME);
    1668                 :      20016 :     MP_STATIC_ASSERT(MP_F_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_STORE_GLOBAL);
    1669         [ +  + ]:      20016 :     if (kind == MP_EMIT_IDOP_GLOBAL_NAME) {
    1670                 :            :         // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
    1671                 :      19545 :         vtype_kind_t vtype;
    1672                 :      19545 :         emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
    1673         [ -  + ]:      19545 :         assert(vtype == VTYPE_PYOBJ);
    1674                 :            :     } else {
    1675                 :        471 :         vtype_kind_t vtype = peek_vtype(emit, 0);
    1676         [ +  + ]:        471 :         if (vtype == VTYPE_PYOBJ) {
    1677                 :        465 :             emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
    1678                 :            :         } else {
    1679                 :          6 :             emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
    1680                 :          6 :             emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
    1681                 :          6 :             ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
    1682                 :            :         }
    1683                 :            :     }
    1684                 :      20016 :     emit_call_with_qstr_arg(emit, MP_F_STORE_NAME + kind, qst, REG_ARG_1); // arg1 = name
    1685                 :      20016 :     emit_post(emit);
    1686                 :      20016 : }
    1687                 :            : 
    1688                 :       1749 : STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
    1689                 :       1749 :     vtype_kind_t vtype_base;
    1690                 :       1749 :     vtype_kind_t vtype_val = peek_vtype(emit, 1);
    1691         [ +  + ]:       1749 :     if (vtype_val == VTYPE_PYOBJ) {
    1692                 :       1737 :         emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
    1693                 :            :     } else {
    1694                 :         12 :         emit_access_stack(emit, 2, &vtype_val, REG_ARG_1); // arg1 = value
    1695                 :         12 :         emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_val, REG_ARG_2); // arg2 = type
    1696                 :         12 :         ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_RET); // arg3 = value (converted)
    1697                 :         12 :         emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
    1698                 :         12 :         adjust_stack(emit, -1); // pop value
    1699                 :            :     }
    1700         [ -  + ]:       1749 :     assert(vtype_base == VTYPE_PYOBJ);
    1701                 :       1749 :     emit_call_with_qstr_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
    1702                 :       1749 :     emit_post(emit);
    1703                 :       1749 : }
    1704                 :            : 
    1705                 :       1295 : STATIC void emit_native_store_subscr(emit_t *emit) {
    1706                 :       1295 :     DEBUG_printf("store_subscr\n");
    1707                 :            :     // need to compile: base[index] = value
    1708                 :            : 
    1709                 :            :     // pop: index, base, value
    1710                 :            :     // optimise case where index is an immediate
    1711                 :       1295 :     vtype_kind_t vtype_base = peek_vtype(emit, 1);
    1712                 :            : 
    1713         [ +  + ]:       1295 :     if (vtype_base == VTYPE_PYOBJ) {
    1714                 :            :         // standard Python subscr
    1715                 :       1167 :         vtype_kind_t vtype_index = peek_vtype(emit, 0);
    1716                 :       1167 :         vtype_kind_t vtype_value = peek_vtype(emit, 2);
    1717         [ +  + ]:       1167 :         if (vtype_index != VTYPE_PYOBJ || vtype_value != VTYPE_PYOBJ) {
    1718                 :            :             // need to implicitly convert non-objects to objects
    1719                 :            :             // TODO do this properly
    1720                 :          6 :             emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, 3);
    1721                 :          6 :             adjust_stack(emit, 3);
    1722                 :            :         }
    1723                 :       1167 :         emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
    1724                 :       1167 :         emit_call(emit, MP_F_OBJ_SUBSCR);
    1725                 :            :     } else {
    1726                 :            :         // viper store
    1727                 :            :         // TODO The different machine architectures have very different
    1728                 :            :         // capabilities and requirements for stores, so probably best to
    1729                 :            :         // write a completely separate store-optimiser for each one.
    1730                 :        128 :         stack_info_t *top = peek_stack(emit, 0);
    1731         [ +  + ]:        128 :         if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
    1732                 :            :             // index is an immediate
    1733                 :         48 :             mp_int_t index_value = top->data.u_imm;
    1734                 :         48 :             emit_pre_pop_discard(emit); // discard index
    1735                 :         48 :             vtype_kind_t vtype_value;
    1736                 :         48 :             int reg_base = REG_ARG_1;
    1737                 :         48 :             int reg_index = REG_ARG_2;
    1738                 :         48 :             int reg_value = REG_ARG_3;
    1739                 :         48 :             emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
    1740                 :            :             #if N_X64 || N_X86
    1741                 :            :             // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
    1742                 :         48 :             emit_pre_pop_reg(emit, &vtype_value, reg_value);
    1743                 :            :             #else
    1744                 :            :             emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
    1745                 :            :             #endif
    1746         [ +  + ]:         48 :             if (vtype_value != VTYPE_BOOL && vtype_value != VTYPE_INT && vtype_value != VTYPE_UINT) {
    1747                 :          4 :                 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1748                 :            :                     MP_ERROR_TEXT("can't store '%q'"), vtype_to_qstr(vtype_value));
    1749                 :            :             }
    1750   [ +  +  +  + ]:         48 :             switch (vtype_base) {
    1751                 :         12 :                 case VTYPE_PTR8: {
    1752                 :            :                     // pointer to 8-bit memory
    1753                 :            :                     // TODO optimise to use thumb strb r1, [r2, r3]
    1754         [ +  + ]:         12 :                     if (index_value != 0) {
    1755                 :            :                         // index is non-zero
    1756                 :            :                         #if N_THUMB
    1757                 :            :                         if (index_value > 0 && index_value < 32) {
    1758                 :            :                             asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
    1759                 :            :                             break;
    1760                 :            :                         }
    1761                 :            :                         #endif
    1762                 :          6 :                         ASM_MOV_REG_IMM(emit->as, reg_index, index_value);
    1763                 :            :                         #if N_ARM
    1764                 :            :                         asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
    1765                 :            :                         return;
    1766                 :            :                         #endif
    1767                 :          6 :                         ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
    1768                 :          6 :                         reg_base = reg_index;
    1769                 :            :                     }
    1770                 :         12 :                     ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
    1771                 :         12 :                     break;
    1772                 :            :                 }
    1773                 :         12 :                 case VTYPE_PTR16: {
    1774                 :            :                     // pointer to 16-bit memory
    1775         [ +  + ]:         12 :                     if (index_value != 0) {
    1776                 :            :                         // index is a non-zero immediate
    1777                 :            :                         #if N_THUMB
    1778                 :            :                         if (index_value > 0 && index_value < 32) {
    1779                 :            :                             asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
    1780                 :            :                             break;
    1781                 :            :                         }
    1782                 :            :                         #endif
    1783                 :          6 :                         ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 1);
    1784                 :          6 :                         ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
    1785                 :          6 :                         reg_base = reg_index;
    1786                 :            :                     }
    1787                 :         12 :                     ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
    1788                 :         12 :                     break;
    1789                 :            :                 }
    1790                 :         16 :                 case VTYPE_PTR32: {
    1791                 :            :                     // pointer to 32-bit memory
    1792         [ +  + ]:         16 :                     if (index_value != 0) {
    1793                 :            :                         // index is a non-zero immediate
    1794                 :            :                         #if N_THUMB
    1795                 :            :                         if (index_value > 0 && index_value < 32) {
    1796                 :            :                             asm_thumb_str_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
    1797                 :            :                             break;
    1798                 :            :                         }
    1799                 :            :                         #endif
    1800                 :            :                         #if N_ARM
    1801                 :            :                         ASM_MOV_REG_IMM(emit->as, reg_index, index_value);
    1802                 :            :                         asm_arm_str_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
    1803                 :            :                         return;
    1804                 :            :                         #endif
    1805                 :          6 :                         ASM_MOV_REG_IMM(emit->as, reg_index, index_value << 2);
    1806                 :          6 :                         ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
    1807                 :          6 :                         reg_base = reg_index;
    1808                 :            :                     }
    1809                 :         16 :                     ASM_STORE32_REG_REG(emit->as, reg_value, reg_base); // store value to (base+4*index)
    1810                 :         16 :                     break;
    1811                 :            :                 }
    1812                 :          8 :                 default:
    1813                 :         48 :                     EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1814                 :            :                         MP_ERROR_TEXT("can't store to '%q'"), vtype_to_qstr(vtype_base));
    1815                 :            :             }
    1816                 :            :         } else {
    1817                 :            :             // index is not an immediate
    1818                 :         80 :             vtype_kind_t vtype_index, vtype_value;
    1819                 :         80 :             int reg_index = REG_ARG_2;
    1820                 :         80 :             int reg_value = REG_ARG_3;
    1821                 :         80 :             emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
    1822                 :         80 :             emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
    1823         [ +  + ]:         80 :             if (vtype_index != VTYPE_INT && vtype_index != VTYPE_UINT) {
    1824                 :          8 :                 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1825                 :            :                     MP_ERROR_TEXT("can't store with '%q' index"), vtype_to_qstr(vtype_index));
    1826                 :            :             }
    1827                 :            :             #if N_X64 || N_X86
    1828                 :            :             // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
    1829                 :         80 :             emit_pre_pop_reg(emit, &vtype_value, reg_value);
    1830                 :            :             #else
    1831                 :            :             emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
    1832                 :            :             #endif
    1833         [ +  + ]:         80 :             if (vtype_value != VTYPE_BOOL && vtype_value != VTYPE_INT && vtype_value != VTYPE_UINT) {
    1834                 :          4 :                 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1835                 :            :                     MP_ERROR_TEXT("can't store '%q'"), vtype_to_qstr(vtype_value));
    1836                 :            :             }
    1837   [ +  +  +  + ]:         80 :             switch (vtype_base) {
    1838                 :         48 :                 case VTYPE_PTR8: {
    1839                 :            :                     // pointer to 8-bit memory
    1840                 :            :                     // TODO optimise to use thumb strb r1, [r2, r3]
    1841                 :            :                     #if N_ARM
    1842                 :            :                     asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
    1843                 :            :                     break;
    1844                 :            :                     #endif
    1845                 :         48 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1846                 :         48 :                     ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
    1847                 :         48 :                     break;
    1848                 :            :                 }
    1849                 :         12 :                 case VTYPE_PTR16: {
    1850                 :            :                     // pointer to 16-bit memory
    1851                 :            :                     #if N_ARM
    1852                 :            :                     asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
    1853                 :            :                     break;
    1854                 :            :                     #endif
    1855                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1856                 :         12 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1857                 :         12 :                     ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
    1858                 :         12 :                     break;
    1859                 :            :                 }
    1860                 :         16 :                 case VTYPE_PTR32: {
    1861                 :            :                     // pointer to 32-bit memory
    1862                 :            :                     #if N_ARM
    1863                 :            :                     asm_arm_str_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
    1864                 :            :                     break;
    1865                 :            :                     #endif
    1866                 :         16 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1867                 :         16 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1868                 :         16 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1869                 :         16 :                     ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
    1870                 :         16 :                     ASM_STORE32_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+4*index)
    1871                 :         16 :                     break;
    1872                 :            :                 }
    1873                 :          4 :                 default:
    1874                 :         80 :                     EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1875                 :            :                         MP_ERROR_TEXT("can't store to '%q'"), vtype_to_qstr(vtype_base));
    1876                 :            :             }
    1877                 :            :         }
    1878                 :            : 
    1879                 :            :     }
    1880                 :       1295 : }
    1881                 :            : 
    1882                 :        207 : STATIC void emit_native_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
    1883         [ +  - ]:        207 :     if (kind == MP_EMIT_IDOP_LOCAL_FAST) {
    1884                 :            :         // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
    1885                 :            :         // to mark deleted vars but then every var would need to be checked on
    1886                 :            :         // each access. Very inefficient, so just set value to None to enable GC.
    1887                 :        207 :         emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
    1888                 :        207 :         emit_native_store_fast(emit, qst, local_num);
    1889                 :            :     } else {
    1890                 :            :         // TODO implement me!
    1891                 :        207 :     }
    1892                 :        207 : }
    1893                 :            : 
    1894                 :        306 : STATIC void emit_native_delete_global(emit_t *emit, qstr qst, int kind) {
    1895                 :        306 :     MP_STATIC_ASSERT(MP_F_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_F_DELETE_NAME);
    1896                 :        306 :     MP_STATIC_ASSERT(MP_F_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_F_DELETE_GLOBAL);
    1897                 :        306 :     emit_native_pre(emit);
    1898                 :        306 :     emit_call_with_qstr_arg(emit, MP_F_DELETE_NAME + kind, qst, REG_ARG_1);
    1899                 :        306 :     emit_post(emit);
    1900                 :        306 : }
    1901                 :            : 
    1902                 :         42 : STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
    1903                 :         42 :     vtype_kind_t vtype_base;
    1904                 :         42 :     emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
    1905         [ -  + ]:         42 :     assert(vtype_base == VTYPE_PYOBJ);
    1906                 :         42 :     ASM_XOR_REG_REG(emit->as, REG_ARG_3, REG_ARG_3); // arg3 = value (null for delete)
    1907                 :         42 :     emit_call_with_qstr_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
    1908                 :         42 :     emit_post(emit);
    1909                 :         42 : }
    1910                 :            : 
    1911                 :        159 : STATIC void emit_native_delete_subscr(emit_t *emit) {
    1912                 :        159 :     vtype_kind_t vtype_index, vtype_base;
    1913                 :        159 :     emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
    1914         [ -  + ]:        159 :     assert(vtype_index == VTYPE_PYOBJ);
    1915         [ -  + ]:        159 :     assert(vtype_base == VTYPE_PYOBJ);
    1916                 :        159 :     emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
    1917                 :        159 : }
    1918                 :            : 
    1919                 :       4891 : STATIC void emit_native_subscr(emit_t *emit, int kind) {
    1920         [ +  + ]:       4891 :     if (kind == MP_EMIT_SUBSCR_LOAD) {
    1921                 :       3437 :         emit_native_load_subscr(emit);
    1922         [ +  + ]:       1454 :     } else if (kind == MP_EMIT_SUBSCR_STORE) {
    1923                 :       1295 :         emit_native_store_subscr(emit);
    1924                 :            :     } else {
    1925                 :        159 :         emit_native_delete_subscr(emit);
    1926                 :            :     }
    1927                 :       4891 : }
    1928                 :            : 
    1929                 :       8841 : STATIC void emit_native_attr(emit_t *emit, qstr qst, int kind) {
    1930         [ +  + ]:       8841 :     if (kind == MP_EMIT_ATTR_LOAD) {
    1931                 :       7050 :         emit_native_load_attr(emit, qst);
    1932         [ +  + ]:       1791 :     } else if (kind == MP_EMIT_ATTR_STORE) {
    1933                 :       1749 :         emit_native_store_attr(emit, qst);
    1934                 :            :     } else {
    1935                 :         42 :         emit_native_delete_attr(emit, qst);
    1936                 :            :     }
    1937                 :       8841 : }
    1938                 :            : 
    1939                 :       5652 : STATIC void emit_native_dup_top(emit_t *emit) {
    1940                 :       5652 :     DEBUG_printf("dup_top\n");
    1941                 :       5652 :     vtype_kind_t vtype;
    1942                 :       5652 :     int reg = REG_TEMP0;
    1943                 :       5652 :     emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
    1944                 :       5652 :     emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
    1945                 :       5652 : }
    1946                 :            : 
    1947                 :        405 : STATIC void emit_native_dup_top_two(emit_t *emit) {
    1948                 :        405 :     vtype_kind_t vtype0, vtype1;
    1949                 :        405 :     emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
    1950                 :        405 :     emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
    1951                 :        405 : }
    1952                 :            : 
    1953                 :      37835 : STATIC void emit_native_pop_top(emit_t *emit) {
    1954                 :      37835 :     DEBUG_printf("pop_top\n");
    1955                 :      37835 :     emit_pre_pop_discard(emit);
    1956                 :      37835 :     emit_post(emit);
    1957                 :      37835 : }
    1958                 :            : 
    1959                 :        549 : STATIC void emit_native_rot_two(emit_t *emit) {
    1960                 :        549 :     DEBUG_printf("rot_two\n");
    1961                 :        549 :     vtype_kind_t vtype0, vtype1;
    1962                 :        549 :     emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
    1963                 :        549 :     emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
    1964                 :        549 : }
    1965                 :            : 
    1966                 :        123 : STATIC void emit_native_rot_three(emit_t *emit) {
    1967                 :        123 :     DEBUG_printf("rot_three\n");
    1968                 :        123 :     vtype_kind_t vtype0, vtype1, vtype2;
    1969                 :        123 :     emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
    1970                 :        123 :     emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
    1971                 :        123 : }
    1972                 :            : 
    1973                 :      28719 : STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
    1974                 :      28719 :     DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
    1975                 :      28719 :     emit_native_pre(emit);
    1976                 :            :     // need to commit stack because we are jumping elsewhere
    1977                 :      28719 :     need_stack_settled(emit);
    1978                 :      28719 :     ASM_JUMP(emit->as, label);
    1979                 :      28719 :     emit_post(emit);
    1980                 :      28719 :     mp_asm_base_suppress_code(&emit->as->base);
    1981                 :      28719 : }
    1982                 :            : 
    1983                 :       9370 : STATIC void emit_native_jump_helper(emit_t *emit, bool cond, mp_uint_t label, bool pop) {
    1984                 :       9370 :     vtype_kind_t vtype = peek_vtype(emit, 0);
    1985         [ +  + ]:       9370 :     if (vtype == VTYPE_PYOBJ) {
    1986                 :       9102 :         emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
    1987         [ +  + ]:       9102 :         if (!pop) {
    1988                 :        225 :             adjust_stack(emit, 1);
    1989                 :            :         }
    1990                 :       9102 :         emit_call(emit, MP_F_OBJ_IS_TRUE);
    1991                 :            :     } else {
    1992                 :        268 :         emit_pre_pop_reg(emit, &vtype, REG_RET);
    1993         [ +  + ]:        268 :         if (!pop) {
    1994                 :         36 :             adjust_stack(emit, 1);
    1995                 :            :         }
    1996         [ +  + ]:        268 :         if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
    1997                 :          4 :             EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    1998                 :            :                 MP_ERROR_TEXT("can't implicitly convert '%q' to 'bool'"), vtype_to_qstr(vtype));
    1999                 :            :         }
    2000                 :            :     }
    2001                 :            :     // For non-pop need to save the vtype so that emit_native_adjust_stack_size
    2002                 :            :     // can use it.  This is a bit of a hack.
    2003         [ +  + ]:       9370 :     if (!pop) {
    2004                 :        261 :         emit->saved_stack_vtype = vtype;
    2005                 :            :     }
    2006                 :            :     // need to commit stack because we may jump elsewhere
    2007                 :       9370 :     need_stack_settled(emit);
    2008                 :            :     // Emit the jump
    2009         [ +  + ]:       9370 :     if (cond) {
    2010         [ +  + ]:       2334 :         ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label, vtype == VTYPE_PYOBJ);
    2011                 :            :     } else {
    2012         [ +  + ]:       7036 :         ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label, vtype == VTYPE_PYOBJ);
    2013                 :            :     }
    2014         [ +  + ]:       9370 :     if (!pop) {
    2015                 :        261 :         adjust_stack(emit, -1);
    2016                 :            :     }
    2017                 :       9370 :     emit_post(emit);
    2018                 :       9370 : }
    2019                 :            : 
    2020                 :       9109 : STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
    2021                 :       9109 :     DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
    2022                 :       9109 :     emit_native_jump_helper(emit, cond, label, true);
    2023                 :       9109 : }
    2024                 :            : 
    2025                 :        261 : STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
    2026                 :        261 :     DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
    2027                 :        261 :     emit_native_jump_helper(emit, cond, label, false);
    2028                 :        261 : }
    2029                 :            : 
    2030                 :      14925 : STATIC void emit_native_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
    2031         [ +  + ]:      14925 :     if (except_depth > 0) {
    2032                 :        270 :         exc_stack_entry_t *first_finally = NULL;
    2033                 :        270 :         exc_stack_entry_t *prev_finally = NULL;
    2034                 :        270 :         exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
    2035         [ +  + ]:        804 :         for (; except_depth > 0; --except_depth, --e) {
    2036   [ +  +  +  + ]:        534 :             if (e->is_finally && e->is_active) {
    2037                 :            :                 // Found an active finally handler
    2038         [ +  + ]:        213 :                 if (first_finally == NULL) {
    2039                 :        183 :                     first_finally = e;
    2040                 :            :                 }
    2041         [ +  + ]:        213 :                 if (prev_finally != NULL) {
    2042                 :            :                     // Mark prev finally as needed to unwind a jump
    2043                 :         30 :                     prev_finally->unwind_label = e->label;
    2044                 :            :                 }
    2045                 :            :                 prev_finally = e;
    2046                 :            :             }
    2047                 :            :         }
    2048         [ +  + ]:        270 :         if (prev_finally == NULL) {
    2049                 :            :             // No finally, handle the jump ourselves
    2050                 :            :             // First, restore the exception handler address for the jump
    2051         [ +  - ]:         87 :             if (e < emit->exc_stack) {
    2052                 :         87 :                 ASM_XOR_REG_REG(emit->as, REG_RET, REG_RET);
    2053                 :            :             } else {
    2054                 :          0 :                 ASM_MOV_REG_PCREL(emit->as, REG_RET, e->label);
    2055                 :            :             }
    2056                 :         87 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
    2057                 :            :         } else {
    2058                 :            :             // Last finally should do our jump for us
    2059                 :            :             // Mark finally as needing to decide the type of jump
    2060                 :        183 :             prev_finally->unwind_label = UNWIND_LABEL_DO_FINAL_UNWIND;
    2061                 :        183 :             ASM_MOV_REG_PCREL(emit->as, REG_RET, label & ~MP_EMIT_BREAK_FROM_FOR);
    2062                 :        183 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_UNWIND(emit), REG_RET);
    2063                 :            :             // Cancel any active exception (see also emit_native_pop_except_jump)
    2064                 :        183 :             ASM_MOV_REG_IMM(emit->as, REG_RET, (mp_uint_t)MP_OBJ_NULL);
    2065                 :        183 :             ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_RET);
    2066                 :            :             // Jump to the innermost active finally
    2067                 :        183 :             label = first_finally->label;
    2068                 :            :         }
    2069                 :            :     }
    2070                 :      14925 :     emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR);
    2071                 :      14925 : }
    2072                 :            : 
    2073                 :        177 : STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
    2074                 :            :     // the context manager is on the top of the stack
    2075                 :            :     // stack: (..., ctx_mgr)
    2076                 :            : 
    2077                 :            :     // get __exit__ method
    2078                 :        177 :     vtype_kind_t vtype;
    2079                 :        177 :     emit_access_stack(emit, 1, &vtype, REG_ARG_1); // arg1 = ctx_mgr
    2080         [ -  + ]:        177 :     assert(vtype == VTYPE_PYOBJ);
    2081                 :        177 :     emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
    2082                 :        177 :     emit_call_with_qstr_arg(emit, MP_F_LOAD_METHOD, MP_QSTR___exit__, REG_ARG_2);
    2083                 :            :     // stack: (..., ctx_mgr, __exit__, self)
    2084                 :            : 
    2085                 :        177 :     emit_pre_pop_reg(emit, &vtype, REG_ARG_3); // self
    2086                 :        177 :     emit_pre_pop_reg(emit, &vtype, REG_ARG_2); // __exit__
    2087                 :        177 :     emit_pre_pop_reg(emit, &vtype, REG_ARG_1); // ctx_mgr
    2088                 :        177 :     emit_post_push_reg(emit, vtype, REG_ARG_2); // __exit__
    2089                 :        177 :     emit_post_push_reg(emit, vtype, REG_ARG_3); // self
    2090                 :            :     // stack: (..., __exit__, self)
    2091                 :            :     // REG_ARG_1=ctx_mgr
    2092                 :            : 
    2093                 :            :     // get __enter__ method
    2094                 :        177 :     emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
    2095                 :        177 :     emit_call_with_qstr_arg(emit, MP_F_LOAD_METHOD, MP_QSTR___enter__, REG_ARG_2); // arg2 = method name
    2096                 :            :     // stack: (..., __exit__, self, __enter__, self)
    2097                 :            : 
    2098                 :            :     // call __enter__ method
    2099                 :        177 :     emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2); // pointer to items, including meth and self
    2100                 :        177 :     emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, 0, REG_ARG_1, 0, REG_ARG_2);
    2101                 :        177 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // push return value of __enter__
    2102                 :            :     // stack: (..., __exit__, self, as_value)
    2103                 :            : 
    2104                 :            :     // need to commit stack because we may jump elsewhere
    2105                 :        177 :     need_stack_settled(emit);
    2106                 :        177 :     emit_native_push_exc_stack(emit, label, true);
    2107                 :            : 
    2108                 :        177 :     emit_native_dup_top(emit);
    2109                 :            :     // stack: (..., __exit__, self, as_value, as_value)
    2110                 :        177 : }
    2111                 :            : 
    2112                 :       5646 : STATIC void emit_native_setup_block(emit_t *emit, mp_uint_t label, int kind) {
    2113         [ +  + ]:       5646 :     if (kind == MP_EMIT_SETUP_BLOCK_WITH) {
    2114                 :        177 :         emit_native_setup_with(emit, label);
    2115                 :            :     } else {
    2116                 :            :         // Set up except and finally
    2117                 :       5469 :         emit_native_pre(emit);
    2118                 :       5469 :         need_stack_settled(emit);
    2119                 :       5469 :         emit_native_push_exc_stack(emit, label, kind == MP_EMIT_SETUP_BLOCK_FINALLY);
    2120                 :       5469 :         emit_post(emit);
    2121                 :            :     }
    2122                 :       5646 : }
    2123                 :            : 
    2124                 :        177 : STATIC void emit_native_with_cleanup(emit_t *emit, mp_uint_t label) {
    2125                 :            :     // Note: 3 labels are reserved for this function, starting at *emit->label_slot
    2126                 :            : 
    2127                 :            :     // stack: (..., __exit__, self, as_value)
    2128                 :        177 :     emit_native_pre(emit);
    2129                 :        177 :     emit_native_leave_exc_stack(emit, false);
    2130                 :        177 :     adjust_stack(emit, -1);
    2131                 :            :     // stack: (..., __exit__, self)
    2132                 :            : 
    2133                 :            :     // Label for case where __exit__ is called from an unwind jump
    2134                 :        177 :     emit_native_label_assign(emit, *emit->label_slot + 2);
    2135                 :            : 
    2136                 :            :     // call __exit__
    2137                 :        177 :     emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
    2138                 :        177 :     emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
    2139                 :        177 :     emit_post_push_imm(emit, VTYPE_PTR_NONE, 0);
    2140                 :        177 :     emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 5);
    2141                 :        177 :     emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, 3, REG_ARG_1, 0, REG_ARG_2);
    2142                 :            : 
    2143                 :            :     // Replace exc with None and finish
    2144                 :        177 :     emit_native_jump(emit, *emit->label_slot);
    2145                 :            : 
    2146                 :            :     // nlr_catch
    2147                 :            :     // Don't use emit_native_label_assign because this isn't a real finally label
    2148                 :        177 :     mp_asm_base_label_assign(&emit->as->base, label);
    2149                 :            : 
    2150                 :            :     // Leave with's exception handler
    2151                 :        177 :     emit_native_leave_exc_stack(emit, true);
    2152                 :            : 
    2153                 :            :     // Adjust stack counter for: __exit__, self (implicitly discard as_value which is above self)
    2154                 :        177 :     emit_native_adjust_stack_size(emit, 2);
    2155                 :            :     // stack: (..., __exit__, self)
    2156                 :            : 
    2157                 :        177 :     ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit)); // get exc
    2158                 :            : 
    2159                 :            :     // Check if exc is MP_OBJ_NULL (i.e. zero) and jump to non-exc handler if it is
    2160                 :        177 :     ASM_JUMP_IF_REG_ZERO(emit->as, REG_ARG_1, *emit->label_slot + 2, false);
    2161                 :            : 
    2162                 :        177 :     ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_2, REG_ARG_1, 0); // get type(exc)
    2163                 :        177 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_ARG_2); // push type(exc)
    2164                 :        177 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_ARG_1); // push exc value
    2165                 :        177 :     emit_post_push_imm(emit, VTYPE_PTR_NONE, 0); // traceback info
    2166                 :            :     // Stack: (..., __exit__, self, type(exc), exc, traceback)
    2167                 :            : 
    2168                 :            :     // call __exit__ method
    2169                 :        177 :     emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 5);
    2170                 :        177 :     emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, 3, REG_ARG_1, 0, REG_ARG_2);
    2171                 :            :     // Stack: (...)
    2172                 :            : 
    2173                 :            :     // If REG_RET is true then we need to replace exception with None (swallow exception)
    2174                 :        177 :     if (REG_ARG_1 != REG_RET) {
    2175                 :        177 :         ASM_MOV_REG_REG(emit->as, REG_ARG_1, REG_RET);
    2176                 :            :     }
    2177                 :        177 :     emit_call(emit, MP_F_OBJ_IS_TRUE);
    2178                 :        177 :     ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, *emit->label_slot + 1, true);
    2179                 :            : 
    2180                 :            :     // Replace exception with MP_OBJ_NULL.
    2181                 :        177 :     emit_native_label_assign(emit, *emit->label_slot);
    2182                 :        177 :     ASM_MOV_REG_IMM(emit->as, REG_TEMP0, (mp_uint_t)MP_OBJ_NULL);
    2183                 :        177 :     ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
    2184                 :            : 
    2185                 :            :     // end of with cleanup nlr_catch block
    2186                 :        177 :     emit_native_label_assign(emit, *emit->label_slot + 1);
    2187                 :            : 
    2188                 :            :     // Exception is in nlr_buf.ret_val slot
    2189                 :        177 : }
    2190                 :            : 
    2191                 :       5646 : STATIC void emit_native_end_finally(emit_t *emit) {
    2192                 :            :     // logic:
    2193                 :            :     //   exc = pop_stack
    2194                 :            :     //   if exc == None: pass
    2195                 :            :     //   else: raise exc
    2196                 :            :     // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
    2197                 :       5646 :     emit_native_pre(emit);
    2198                 :       5646 :     ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
    2199                 :       5646 :     emit_call(emit, MP_F_NATIVE_RAISE);
    2200                 :            : 
    2201                 :            :     // Get state for this finally and see if we need to unwind
    2202                 :       5646 :     exc_stack_entry_t *e = emit_native_pop_exc_stack(emit);
    2203         [ +  + ]:       5646 :     if (e->unwind_label != UNWIND_LABEL_UNUSED) {
    2204                 :        210 :         ASM_MOV_REG_LOCAL(emit->as, REG_RET, LOCAL_IDX_EXC_HANDLER_UNWIND(emit));
    2205                 :        210 :         ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, *emit->label_slot, false);
    2206         [ +  + ]:        210 :         if (e->unwind_label == UNWIND_LABEL_DO_FINAL_UNWIND) {
    2207                 :        180 :             ASM_JUMP_REG(emit->as, REG_RET);
    2208                 :            :         } else {
    2209                 :         30 :             emit_native_jump(emit, e->unwind_label);
    2210                 :            :         }
    2211                 :        210 :         emit_native_label_assign(emit, *emit->label_slot);
    2212                 :            :     }
    2213                 :            : 
    2214                 :       5646 :     emit_post(emit);
    2215                 :       5646 : }
    2216                 :            : 
    2217                 :       1753 : STATIC void emit_native_get_iter(emit_t *emit, bool use_stack) {
    2218                 :            :     // perhaps the difficult one, as we want to rewrite for loops using native code
    2219                 :            :     // in cases where we iterate over a Python object, can we use normal runtime calls?
    2220                 :            : 
    2221                 :       1753 :     vtype_kind_t vtype;
    2222                 :       1753 :     emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
    2223         [ -  + ]:       1753 :     assert(vtype == VTYPE_PYOBJ);
    2224         [ +  + ]:       1753 :     if (use_stack) {
    2225                 :       1164 :         emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_2, MP_OBJ_ITER_BUF_NSLOTS);
    2226                 :       1164 :         emit_call(emit, MP_F_NATIVE_GETITER);
    2227                 :            :     } else {
    2228                 :            :         // mp_getiter will allocate the iter_buf on the heap
    2229                 :        589 :         ASM_MOV_REG_IMM(emit->as, REG_ARG_2, 0);
    2230                 :        589 :         emit_call(emit, MP_F_NATIVE_GETITER);
    2231                 :        589 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2232                 :            :     }
    2233                 :       1753 : }
    2234                 :            : 
    2235                 :       1242 : STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
    2236                 :       1242 :     emit_native_pre(emit);
    2237                 :       1242 :     emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, MP_OBJ_ITER_BUF_NSLOTS);
    2238                 :       1242 :     adjust_stack(emit, MP_OBJ_ITER_BUF_NSLOTS);
    2239                 :       1242 :     emit_call(emit, MP_F_NATIVE_ITERNEXT);
    2240                 :            :     #if MICROPY_DEBUG_MP_OBJ_SENTINELS
    2241                 :            :     ASM_MOV_REG_IMM(emit->as, REG_TEMP1, (mp_uint_t)MP_OBJ_STOP_ITERATION);
    2242                 :            :     ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
    2243                 :            :     #else
    2244                 :       1242 :     MP_STATIC_ASSERT(MP_OBJ_STOP_ITERATION == 0);
    2245                 :       1242 :     ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label, false);
    2246                 :            :     #endif
    2247                 :       1242 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2248                 :       1242 : }
    2249                 :            : 
    2250                 :       1242 : STATIC void emit_native_for_iter_end(emit_t *emit) {
    2251                 :            :     // adjust stack counter (we get here from for_iter ending, which popped the value for us)
    2252                 :       1242 :     emit_native_pre(emit);
    2253                 :       1242 :     adjust_stack(emit, -MP_OBJ_ITER_BUF_NSLOTS);
    2254                 :       1242 :     emit_post(emit);
    2255                 :       1242 : }
    2256                 :            : 
    2257                 :       9288 : STATIC void emit_native_pop_except_jump(emit_t *emit, mp_uint_t label, bool within_exc_handler) {
    2258         [ +  + ]:       9288 :     if (within_exc_handler) {
    2259                 :            :         // Cancel any active exception so subsequent handlers don't see it
    2260                 :       4659 :         ASM_MOV_REG_IMM(emit->as, REG_TEMP0, (mp_uint_t)MP_OBJ_NULL);
    2261                 :       4659 :         ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_VAL(emit), REG_TEMP0);
    2262                 :            :     } else {
    2263                 :       4629 :         emit_native_leave_exc_stack(emit, false);
    2264                 :            :     }
    2265                 :       9288 :     emit_native_jump(emit, label);
    2266                 :       9288 : }
    2267                 :            : 
    2268                 :       1173 : STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
    2269                 :       1173 :     vtype_kind_t vtype;
    2270                 :       1173 :     emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
    2271         [ +  + ]:       1173 :     if (vtype == VTYPE_PYOBJ) {
    2272                 :       1161 :         emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
    2273                 :       1161 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2274                 :            :     } else {
    2275                 :         12 :         adjust_stack(emit, 1);
    2276                 :         12 :         EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    2277                 :            :             MP_ERROR_TEXT("unary op %q not implemented"), mp_unary_op_method_name[op]);
    2278                 :            :     }
    2279                 :       1173 : }
    2280                 :            : 
    2281                 :      16994 : STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
    2282                 :      16994 :     DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
    2283                 :      16994 :     vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
    2284                 :      16994 :     vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
    2285         [ +  + ]:      16994 :     if ((vtype_lhs == VTYPE_INT || vtype_lhs == VTYPE_UINT)
    2286         [ +  + ]:        878 :         && (vtype_rhs == VTYPE_INT || vtype_rhs == VTYPE_UINT)) {
    2287                 :            :         // for integers, inplace and normal ops are equivalent, so use just normal ops
    2288         [ +  + ]:        874 :         if (MP_BINARY_OP_INPLACE_OR <= op && op <= MP_BINARY_OP_INPLACE_POWER) {
    2289                 :        186 :             op += MP_BINARY_OP_OR - MP_BINARY_OP_INPLACE_OR;
    2290                 :            :         }
    2291                 :            : 
    2292                 :            :         #if N_X64 || N_X86
    2293                 :            :         // special cases for x86 and shifting
    2294         [ +  + ]:        874 :         if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_RSHIFT) {
    2295                 :            :             #if N_X64
    2296                 :         96 :             emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
    2297                 :            :             #else
    2298                 :            :             emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
    2299                 :            :             #endif
    2300         [ +  + ]:         96 :             if (op == MP_BINARY_OP_LSHIFT) {
    2301                 :         48 :                 ASM_LSL_REG(emit->as, REG_RET);
    2302                 :            :             } else {
    2303         [ +  + ]:         48 :                 if (vtype_lhs == VTYPE_UINT) {
    2304                 :          6 :                     ASM_LSR_REG(emit->as, REG_RET);
    2305                 :            :                 } else {
    2306                 :         42 :                     ASM_ASR_REG(emit->as, REG_RET);
    2307                 :            :                 }
    2308                 :            :             }
    2309                 :         96 :             emit_post_push_reg(emit, vtype_lhs, REG_RET);
    2310                 :        212 :             return;
    2311                 :            :         }
    2312                 :            :         #endif
    2313                 :            : 
    2314                 :            :         // special cases for floor-divide and module because we dispatch to helper functions
    2315         [ +  + ]:        778 :         if (op == MP_BINARY_OP_FLOOR_DIVIDE || op == MP_BINARY_OP_MODULO) {
    2316                 :         20 :             emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
    2317         [ +  + ]:         20 :             if (vtype_lhs != VTYPE_INT) {
    2318                 :          8 :                 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    2319                 :            :                     MP_ERROR_TEXT("div/mod not implemented for uint"), mp_binary_op_method_name[op]);
    2320                 :            :             }
    2321         [ +  + ]:         20 :             if (op == MP_BINARY_OP_FLOOR_DIVIDE) {
    2322                 :         10 :                 emit_call(emit, MP_F_SMALL_INT_FLOOR_DIVIDE);
    2323                 :            :             } else {
    2324                 :         10 :                 emit_call(emit, MP_F_SMALL_INT_MODULO);
    2325                 :            :             }
    2326                 :         20 :             emit_post_push_reg(emit, VTYPE_INT, REG_RET);
    2327                 :         20 :             return;
    2328                 :            :         }
    2329                 :            : 
    2330                 :        758 :         int reg_rhs = REG_ARG_3;
    2331                 :        758 :         emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
    2332                 :        758 :         emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
    2333                 :            : 
    2334                 :            :         #if !(N_X64 || N_X86)
    2335                 :            :         if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_RSHIFT) {
    2336                 :            :             if (op == MP_BINARY_OP_LSHIFT) {
    2337                 :            :                 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2338                 :            :             } else {
    2339                 :            :                 if (vtype_lhs == VTYPE_UINT) {
    2340                 :            :                     ASM_LSR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2341                 :            :                 } else {
    2342                 :            :                     ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2343                 :            :                 }
    2344                 :            :             }
    2345                 :            :             emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
    2346                 :            :             return;
    2347                 :            :         }
    2348                 :            :         #endif
    2349                 :            : 
    2350         [ +  + ]:        758 :         if (op == MP_BINARY_OP_OR) {
    2351                 :         60 :             ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2352                 :         60 :             emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
    2353   [ +  +  +  +  :        698 :         } else if (op == MP_BINARY_OP_XOR) {
                +  +  + ]
    2354                 :         24 :             ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2355                 :         24 :             emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
    2356                 :            :         } else if (op == MP_BINARY_OP_AND) {
    2357                 :         60 :             ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2358                 :         60 :             emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
    2359                 :            :         } else if (op == MP_BINARY_OP_ADD) {
    2360                 :        294 :             ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2361                 :        294 :             emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
    2362                 :            :         } else if (op == MP_BINARY_OP_SUBTRACT) {
    2363                 :         24 :             ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2364                 :         24 :             emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
    2365                 :            :         } else if (op == MP_BINARY_OP_MULTIPLY) {
    2366                 :         24 :             ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
    2367                 :         24 :             emit_post_push_reg(emit, vtype_lhs, REG_ARG_2);
    2368                 :            :         } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
    2369                 :            :             // comparison ops are (in enum order):
    2370                 :            :             //  MP_BINARY_OP_LESS
    2371                 :            :             //  MP_BINARY_OP_MORE
    2372                 :            :             //  MP_BINARY_OP_EQUAL
    2373                 :            :             //  MP_BINARY_OP_LESS_EQUAL
    2374                 :            :             //  MP_BINARY_OP_MORE_EQUAL
    2375                 :            :             //  MP_BINARY_OP_NOT_EQUAL
    2376                 :            : 
    2377         [ +  + ]:        268 :             if (vtype_lhs != vtype_rhs) {
    2378                 :          4 :                 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("comparison of int and uint"));
    2379                 :            :             }
    2380                 :            : 
    2381         [ +  + ]:        268 :             size_t op_idx = op - MP_BINARY_OP_LESS + (vtype_lhs == VTYPE_UINT ? 0 : 6);
    2382                 :            : 
    2383                 :        268 :             need_reg_single(emit, REG_RET, 0);
    2384                 :            :             #if N_X64
    2385                 :        268 :             asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
    2386                 :        268 :             asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
    2387                 :        268 :             static byte ops[6 + 6] = {
    2388                 :            :                 // unsigned
    2389                 :            :                 ASM_X64_CC_JB,
    2390                 :            :                 ASM_X64_CC_JA,
    2391                 :            :                 ASM_X64_CC_JE,
    2392                 :            :                 ASM_X64_CC_JBE,
    2393                 :            :                 ASM_X64_CC_JAE,
    2394                 :            :                 ASM_X64_CC_JNE,
    2395                 :            :                 // signed
    2396                 :            :                 ASM_X64_CC_JL,
    2397                 :            :                 ASM_X64_CC_JG,
    2398                 :            :                 ASM_X64_CC_JE,
    2399                 :            :                 ASM_X64_CC_JLE,
    2400                 :            :                 ASM_X64_CC_JGE,
    2401                 :            :                 ASM_X64_CC_JNE,
    2402                 :            :             };
    2403                 :        268 :             asm_x64_setcc_r8(emit->as, ops[op_idx], REG_RET);
    2404                 :            :             #elif N_X86
    2405                 :            :             asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
    2406                 :            :             asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
    2407                 :            :             static byte ops[6 + 6] = {
    2408                 :            :                 // unsigned
    2409                 :            :                 ASM_X86_CC_JB,
    2410                 :            :                 ASM_X86_CC_JA,
    2411                 :            :                 ASM_X86_CC_JE,
    2412                 :            :                 ASM_X86_CC_JBE,
    2413                 :            :                 ASM_X86_CC_JAE,
    2414                 :            :                 ASM_X86_CC_JNE,
    2415                 :            :                 // signed
    2416                 :            :                 ASM_X86_CC_JL,
    2417                 :            :                 ASM_X86_CC_JG,
    2418                 :            :                 ASM_X86_CC_JE,
    2419                 :            :                 ASM_X86_CC_JLE,
    2420                 :            :                 ASM_X86_CC_JGE,
    2421                 :            :                 ASM_X86_CC_JNE,
    2422                 :            :             };
    2423                 :            :             asm_x86_setcc_r8(emit->as, ops[op_idx], REG_RET);
    2424                 :            :             #elif N_THUMB
    2425                 :            :             asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
    2426                 :            :             if (asm_thumb_allow_armv7m(emit->as)) {
    2427                 :            :                 static uint16_t ops[6 + 6] = {
    2428                 :            :                     // unsigned
    2429                 :            :                     ASM_THUMB_OP_ITE_CC,
    2430                 :            :                     ASM_THUMB_OP_ITE_HI,
    2431                 :            :                     ASM_THUMB_OP_ITE_EQ,
    2432                 :            :                     ASM_THUMB_OP_ITE_LS,
    2433                 :            :                     ASM_THUMB_OP_ITE_CS,
    2434                 :            :                     ASM_THUMB_OP_ITE_NE,
    2435                 :            :                     // signed
    2436                 :            :                     ASM_THUMB_OP_ITE_LT,
    2437                 :            :                     ASM_THUMB_OP_ITE_GT,
    2438                 :            :                     ASM_THUMB_OP_ITE_EQ,
    2439                 :            :                     ASM_THUMB_OP_ITE_LE,
    2440                 :            :                     ASM_THUMB_OP_ITE_GE,
    2441                 :            :                     ASM_THUMB_OP_ITE_NE,
    2442                 :            :                 };
    2443                 :            :                 asm_thumb_op16(emit->as, ops[op_idx]);
    2444                 :            :                 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1);
    2445                 :            :                 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0);
    2446                 :            :             } else {
    2447                 :            :                 static uint16_t ops[6 + 6] = {
    2448                 :            :                     // unsigned
    2449                 :            :                     ASM_THUMB_CC_CC,
    2450                 :            :                     ASM_THUMB_CC_HI,
    2451                 :            :                     ASM_THUMB_CC_EQ,
    2452                 :            :                     ASM_THUMB_CC_LS,
    2453                 :            :                     ASM_THUMB_CC_CS,
    2454                 :            :                     ASM_THUMB_CC_NE,
    2455                 :            :                     // signed
    2456                 :            :                     ASM_THUMB_CC_LT,
    2457                 :            :                     ASM_THUMB_CC_GT,
    2458                 :            :                     ASM_THUMB_CC_EQ,
    2459                 :            :                     ASM_THUMB_CC_LE,
    2460                 :            :                     ASM_THUMB_CC_GE,
    2461                 :            :                     ASM_THUMB_CC_NE,
    2462                 :            :                 };
    2463                 :            :                 asm_thumb_bcc_rel9(emit->as, ops[op_idx], 6);
    2464                 :            :                 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 0);
    2465                 :            :                 asm_thumb_b_rel12(emit->as, 4);
    2466                 :            :                 asm_thumb_mov_rlo_i8(emit->as, REG_RET, 1);
    2467                 :            :             }
    2468                 :            :             #elif N_ARM
    2469                 :            :             asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
    2470                 :            :             static uint ccs[6 + 6] = {
    2471                 :            :                 // unsigned
    2472                 :            :                 ASM_ARM_CC_CC,
    2473                 :            :                 ASM_ARM_CC_HI,
    2474                 :            :                 ASM_ARM_CC_EQ,
    2475                 :            :                 ASM_ARM_CC_LS,
    2476                 :            :                 ASM_ARM_CC_CS,
    2477                 :            :                 ASM_ARM_CC_NE,
    2478                 :            :                 // signed
    2479                 :            :                 ASM_ARM_CC_LT,
    2480                 :            :                 ASM_ARM_CC_GT,
    2481                 :            :                 ASM_ARM_CC_EQ,
    2482                 :            :                 ASM_ARM_CC_LE,
    2483                 :            :                 ASM_ARM_CC_GE,
    2484                 :            :                 ASM_ARM_CC_NE,
    2485                 :            :             };
    2486                 :            :             asm_arm_setcc_reg(emit->as, REG_RET, ccs[op_idx]);
    2487                 :            :             #elif N_XTENSA || N_XTENSAWIN
    2488                 :            :             static uint8_t ccs[6 + 6] = {
    2489                 :            :                 // unsigned
    2490                 :            :                 ASM_XTENSA_CC_LTU,
    2491                 :            :                 0x80 | ASM_XTENSA_CC_LTU, // for GTU we'll swap args
    2492                 :            :                 ASM_XTENSA_CC_EQ,
    2493                 :            :                 0x80 | ASM_XTENSA_CC_GEU, // for LEU we'll swap args
    2494                 :            :                 ASM_XTENSA_CC_GEU,
    2495                 :            :                 ASM_XTENSA_CC_NE,
    2496                 :            :                 // signed
    2497                 :            :                 ASM_XTENSA_CC_LT,
    2498                 :            :                 0x80 | ASM_XTENSA_CC_LT, // for GT we'll swap args
    2499                 :            :                 ASM_XTENSA_CC_EQ,
    2500                 :            :                 0x80 | ASM_XTENSA_CC_GE, // for LE we'll swap args
    2501                 :            :                 ASM_XTENSA_CC_GE,
    2502                 :            :                 ASM_XTENSA_CC_NE,
    2503                 :            :             };
    2504                 :            :             uint8_t cc = ccs[op_idx];
    2505                 :            :             if ((cc & 0x80) == 0) {
    2506                 :            :                 asm_xtensa_setcc_reg_reg_reg(emit->as, cc, REG_RET, REG_ARG_2, reg_rhs);
    2507                 :            :             } else {
    2508                 :            :                 asm_xtensa_setcc_reg_reg_reg(emit->as, cc & ~0x80, REG_RET, reg_rhs, REG_ARG_2);
    2509                 :            :             }
    2510                 :            :             #else
    2511                 :            :             #error not implemented
    2512                 :            :             #endif
    2513                 :        268 :             emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
    2514                 :            :         } else {
    2515                 :            :             // TODO other ops not yet implemented
    2516                 :          4 :             adjust_stack(emit, 1);
    2517                 :        758 :             EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    2518                 :            :                 MP_ERROR_TEXT("binary op %q not implemented"), mp_binary_op_method_name[op]);
    2519                 :            :         }
    2520   [ +  +  +  - ]:      32236 :     } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
    2521                 :      16116 :         emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
    2522                 :      16116 :         bool invert = false;
    2523         [ +  + ]:      16116 :         if (op == MP_BINARY_OP_NOT_IN) {
    2524                 :            :             invert = true;
    2525                 :            :             op = MP_BINARY_OP_IN;
    2526         [ +  + ]:      15981 :         } else if (op == MP_BINARY_OP_IS_NOT) {
    2527                 :        492 :             invert = true;
    2528                 :        492 :             op = MP_BINARY_OP_IS;
    2529                 :            :         }
    2530                 :      16116 :         emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
    2531         [ +  + ]:      16116 :         if (invert) {
    2532                 :        627 :             ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
    2533                 :        627 :             emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
    2534                 :            :         }
    2535                 :      16116 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2536                 :            :     } else {
    2537                 :          4 :         adjust_stack(emit, -1);
    2538                 :      16878 :         EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    2539                 :            :             MP_ERROR_TEXT("can't do binary op between '%q' and '%q'"),
    2540                 :            :             vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
    2541                 :            :     }
    2542                 :            : }
    2543                 :            : 
    2544                 :            : #if MICROPY_PY_BUILTINS_SLICE
    2545                 :            : STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args);
    2546                 :            : #endif
    2547                 :            : 
    2548                 :       6768 : STATIC void emit_native_build(emit_t *emit, mp_uint_t n_args, int kind) {
    2549                 :            :     // for viper: call runtime, with types of args
    2550                 :            :     //   if wrapped in byte_array, or something, allocates memory and fills it
    2551                 :       6768 :     MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_F_BUILD_TUPLE);
    2552                 :       6768 :     MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_F_BUILD_LIST);
    2553                 :       6768 :     MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_F_BUILD_MAP);
    2554                 :       6768 :     MP_STATIC_ASSERT(MP_F_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_F_BUILD_SET);
    2555                 :            :     #if MICROPY_PY_BUILTINS_SLICE
    2556         [ +  + ]:       6768 :     if (kind == MP_EMIT_BUILD_SLICE) {
    2557                 :        852 :         emit_native_build_slice(emit, n_args);
    2558                 :        852 :         return;
    2559                 :            :     }
    2560                 :            :     #endif
    2561                 :       5916 :     emit_native_pre(emit);
    2562         [ +  + ]:       5916 :     if (kind == MP_EMIT_BUILD_TUPLE || kind == MP_EMIT_BUILD_LIST || kind == MP_EMIT_BUILD_SET) {
    2563                 :       4908 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
    2564                 :            :     }
    2565                 :       5916 :     emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE + kind, n_args, REG_ARG_1);
    2566                 :       5916 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple/list/map/set
    2567                 :            : }
    2568                 :            : 
    2569                 :       1875 : STATIC void emit_native_store_map(emit_t *emit) {
    2570                 :       1875 :     vtype_kind_t vtype_key, vtype_value, vtype_map;
    2571                 :       1875 :     emit_pre_pop_reg_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3, &vtype_map, REG_ARG_1); // key, value, map
    2572         [ -  + ]:       1875 :     assert(vtype_key == VTYPE_PYOBJ);
    2573         [ -  + ]:       1875 :     assert(vtype_value == VTYPE_PYOBJ);
    2574         [ -  + ]:       1875 :     assert(vtype_map == VTYPE_PYOBJ);
    2575                 :       1875 :     emit_call(emit, MP_F_STORE_MAP);
    2576                 :       1875 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
    2577                 :       1875 : }
    2578                 :            : 
    2579                 :            : #if MICROPY_PY_BUILTINS_SLICE
    2580                 :        852 : STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
    2581                 :        852 :     DEBUG_printf("build_slice %d\n", n_args);
    2582         [ +  + ]:        852 :     if (n_args == 2) {
    2583                 :        681 :         vtype_kind_t vtype_start, vtype_stop;
    2584                 :        681 :         emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
    2585         [ -  + ]:        681 :         assert(vtype_start == VTYPE_PYOBJ);
    2586         [ -  + ]:        681 :         assert(vtype_stop == VTYPE_PYOBJ);
    2587                 :        681 :         emit_native_mov_reg_const(emit, REG_ARG_3, MP_F_CONST_NONE_OBJ); // arg3 = step
    2588                 :            :     } else {
    2589         [ -  + ]:        171 :         assert(n_args == 3);
    2590                 :        171 :         vtype_kind_t vtype_start, vtype_stop, vtype_step;
    2591                 :        171 :         emit_pre_pop_reg_reg_reg(emit, &vtype_step, REG_ARG_3, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop, arg3 = step
    2592         [ -  + ]:        171 :         assert(vtype_start == VTYPE_PYOBJ);
    2593         [ -  + ]:        171 :         assert(vtype_stop == VTYPE_PYOBJ);
    2594         [ -  + ]:        171 :         assert(vtype_step == VTYPE_PYOBJ);
    2595                 :            :     }
    2596                 :        852 :     emit_call(emit, MP_F_NEW_SLICE);
    2597                 :        852 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2598                 :        852 : }
    2599                 :            : #endif
    2600                 :            : 
    2601                 :        180 : STATIC void emit_native_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_index) {
    2602                 :        180 :     mp_fun_kind_t f;
    2603         [ +  + ]:        180 :     if (kind == SCOPE_LIST_COMP) {
    2604                 :        153 :         vtype_kind_t vtype_item;
    2605                 :        153 :         emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
    2606         [ -  + ]:        153 :         assert(vtype_item == VTYPE_PYOBJ);
    2607                 :        153 :         f = MP_F_LIST_APPEND;
    2608                 :            :     #if MICROPY_PY_BUILTINS_SET
    2609         [ +  + ]:         27 :     } else if (kind == SCOPE_SET_COMP) {
    2610                 :          3 :         vtype_kind_t vtype_item;
    2611                 :          3 :         emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
    2612         [ -  + ]:          3 :         assert(vtype_item == VTYPE_PYOBJ);
    2613                 :          3 :         f = MP_F_STORE_SET;
    2614                 :            :     #endif
    2615                 :            :     } else {
    2616                 :            :         // SCOPE_DICT_COMP
    2617                 :         24 :         vtype_kind_t vtype_key, vtype_value;
    2618                 :         24 :         emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
    2619         [ -  + ]:         24 :         assert(vtype_key == VTYPE_PYOBJ);
    2620         [ -  + ]:         24 :         assert(vtype_value == VTYPE_PYOBJ);
    2621                 :         24 :         f = MP_F_STORE_MAP;
    2622                 :            :     }
    2623                 :        180 :     vtype_kind_t vtype_collection;
    2624                 :        180 :     emit_access_stack(emit, collection_index, &vtype_collection, REG_ARG_1);
    2625         [ -  + ]:        180 :     assert(vtype_collection == VTYPE_PYOBJ);
    2626                 :        180 :     emit_call(emit, f);
    2627                 :        180 :     emit_post(emit);
    2628                 :        180 : }
    2629                 :            : 
    2630                 :        348 : STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
    2631                 :        348 :     DEBUG_printf("unpack_sequence %d\n", n_args);
    2632                 :        348 :     vtype_kind_t vtype_base;
    2633                 :        348 :     emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
    2634         [ -  + ]:        348 :     assert(vtype_base == VTYPE_PYOBJ);
    2635                 :        348 :     emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
    2636                 :        348 :     emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
    2637                 :        348 : }
    2638                 :            : 
    2639                 :         99 : STATIC void emit_native_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
    2640                 :         99 :     DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
    2641                 :         99 :     vtype_kind_t vtype_base;
    2642                 :         99 :     emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
    2643         [ -  + ]:         99 :     assert(vtype_base == VTYPE_PYOBJ);
    2644                 :         99 :     emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_left + n_right + 1); // arg3 = dest ptr
    2645                 :         99 :     emit_call_with_imm_arg(emit, MP_F_UNPACK_EX, n_left | (n_right << 8), REG_ARG_2); // arg2 = n_left + n_right
    2646                 :         99 : }
    2647                 :            : 
    2648                 :       7413 : STATIC void emit_native_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
    2649                 :            :     // call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them
    2650                 :       7413 :     emit_native_pre(emit);
    2651                 :       7413 :     emit_native_mov_reg_state(emit, REG_ARG_2, LOCAL_IDX_FUN_OBJ(emit));
    2652                 :       7413 :     ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_2, REG_ARG_2, OFFSETOF_OBJ_FUN_BC_CONTEXT);
    2653         [ +  + ]:       7413 :     if (n_pos_defaults == 0 && n_kw_defaults == 0) {
    2654                 :       7035 :         need_reg_all(emit);
    2655                 :       7035 :         ASM_MOV_REG_IMM(emit->as, REG_ARG_3, 0);
    2656                 :            :     } else {
    2657                 :        378 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2);
    2658                 :        378 :         need_reg_all(emit);
    2659                 :            :     }
    2660                 :       7413 :     emit_load_reg_with_child(emit, REG_ARG_1, scope->raw_code);
    2661                 :       7413 :     ASM_CALL_IND(emit->as, MP_F_MAKE_FUNCTION_FROM_RAW_CODE);
    2662                 :       7413 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2663                 :       7413 : }
    2664                 :            : 
    2665                 :        174 : STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
    2666                 :            :     // make function
    2667                 :        174 :     emit_native_pre(emit);
    2668                 :        174 :     emit_native_mov_reg_state(emit, REG_ARG_2, LOCAL_IDX_FUN_OBJ(emit));
    2669                 :        174 :     ASM_LOAD_REG_REG_OFFSET(emit->as, REG_ARG_2, REG_ARG_2, OFFSETOF_OBJ_FUN_BC_CONTEXT);
    2670         [ +  + ]:        174 :     if (n_pos_defaults == 0 && n_kw_defaults == 0) {
    2671                 :        171 :         need_reg_all(emit);
    2672                 :        171 :         ASM_MOV_REG_IMM(emit->as, REG_ARG_3, 0);
    2673                 :            :     } else {
    2674                 :          3 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2 + n_closed_over);
    2675                 :          3 :         adjust_stack(emit, 2 + n_closed_over);
    2676                 :          3 :         need_reg_all(emit);
    2677                 :            :     }
    2678                 :        174 :     emit_load_reg_with_child(emit, REG_ARG_1, scope->raw_code);
    2679                 :        174 :     ASM_CALL_IND(emit->as, MP_F_MAKE_FUNCTION_FROM_RAW_CODE);
    2680                 :            : 
    2681                 :            :     // make closure
    2682                 :            :     #if REG_ARG_1 != REG_RET
    2683                 :        174 :     ASM_MOV_REG_REG(emit->as, REG_ARG_1, REG_RET);
    2684                 :            :     #endif
    2685                 :        174 :     ASM_MOV_REG_IMM(emit->as, REG_ARG_2, n_closed_over);
    2686                 :        174 :     emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
    2687         [ +  + ]:        174 :     if (n_pos_defaults != 0 || n_kw_defaults != 0) {
    2688                 :          3 :         adjust_stack(emit, -2);
    2689                 :            :     }
    2690                 :        174 :     ASM_CALL_IND(emit->as, MP_F_NEW_CLOSURE);
    2691                 :        174 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2692                 :        174 : }
    2693                 :            : 
    2694                 :      39854 : STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
    2695                 :      39854 :     DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
    2696                 :            : 
    2697                 :            :     // TODO: in viper mode, call special runtime routine with type info for args,
    2698                 :            :     // and wanted type info for return, to remove need for boxing/unboxing
    2699                 :            : 
    2700                 :      39854 :     emit_native_pre(emit);
    2701                 :      39854 :     vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
    2702         [ +  + ]:      39854 :     if (vtype_fun == VTYPE_BUILTIN_CAST) {
    2703                 :            :         // casting operator
    2704         [ -  + ]:         96 :         assert(n_positional == 1 && n_keyword == 0);
    2705         [ -  + ]:         96 :         assert(!star_flags);
    2706                 :         96 :         DEBUG_printf("  cast to %d\n", vtype_fun);
    2707                 :         96 :         vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
    2708      [ +  +  + ]:         96 :         switch (peek_vtype(emit, 0)) {
    2709                 :         78 :             case VTYPE_PYOBJ: {
    2710                 :         78 :                 vtype_kind_t vtype;
    2711                 :         78 :                 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
    2712                 :         78 :                 emit_pre_pop_discard(emit);
    2713                 :         78 :                 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, vtype_cast, REG_ARG_2); // arg2 = type
    2714                 :         78 :                 emit_post_push_reg(emit, vtype_cast, REG_RET);
    2715                 :         78 :                 break;
    2716                 :            :             }
    2717                 :         14 :             case VTYPE_BOOL:
    2718                 :            :             case VTYPE_INT:
    2719                 :            :             case VTYPE_UINT:
    2720                 :            :             case VTYPE_PTR:
    2721                 :            :             case VTYPE_PTR8:
    2722                 :            :             case VTYPE_PTR16:
    2723                 :            :             case VTYPE_PTR32:
    2724                 :            :             case VTYPE_PTR_NONE:
    2725                 :         14 :                 emit_fold_stack_top(emit, REG_ARG_1);
    2726                 :         14 :                 emit_post_top_set_vtype(emit, vtype_cast);
    2727                 :         14 :                 break;
    2728                 :            :             default:
    2729                 :            :                 // this can happen when casting a cast: int(int)
    2730                 :          4 :                 mp_raise_NotImplementedError(MP_ERROR_TEXT("casting"));
    2731                 :            :         }
    2732                 :            :     } else {
    2733         [ -  + ]:      39758 :         assert(vtype_fun == VTYPE_PYOBJ);
    2734         [ +  + ]:      39758 :         if (star_flags) {
    2735                 :        321 :             emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 2); // pointer to args
    2736                 :        321 :             emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 0, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
    2737                 :        321 :             emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2738                 :            :         } else {
    2739         [ +  + ]:      39437 :             if (n_positional != 0 || n_keyword != 0) {
    2740                 :      36338 :                 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
    2741                 :            :             }
    2742                 :      39433 :             emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
    2743                 :      39433 :             emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
    2744                 :      39433 :             emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2745                 :            :         }
    2746                 :            :     }
    2747                 :      39846 : }
    2748                 :            : 
    2749                 :      12747 : STATIC void emit_native_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
    2750         [ +  + ]:      12747 :     if (star_flags) {
    2751                 :         99 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
    2752                 :         99 :         emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 1, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
    2753                 :         99 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2754                 :            :     } else {
    2755                 :      12648 :         emit_native_pre(emit);
    2756                 :      12648 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2 + n_positional + 2 * n_keyword); // pointer to items, including meth and self
    2757                 :      12648 :         emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
    2758                 :      12648 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
    2759                 :            :     }
    2760                 :      12747 : }
    2761                 :            : 
    2762                 :      14625 : STATIC void emit_native_return_value(emit_t *emit) {
    2763                 :      14625 :     DEBUG_printf("return_value\n");
    2764                 :            : 
    2765         [ +  + ]:      14625 :     if (emit->scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
    2766                 :            :         // Save pointer to current stack position for caller to access return value
    2767                 :        846 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_TEMP0, 1);
    2768                 :        846 :         emit_native_mov_state_reg(emit, OFFSETOF_CODE_STATE_SP, REG_TEMP0);
    2769                 :            : 
    2770                 :            :         // Put return type in return value slot
    2771                 :        846 :         ASM_MOV_REG_IMM(emit->as, REG_TEMP0, MP_VM_RETURN_NORMAL);
    2772                 :        846 :         ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_RET_VAL(emit), REG_TEMP0);
    2773                 :            : 
    2774                 :            :         // Do the unwinding jump to get to the return handler
    2775                 :        846 :         emit_native_unwind_jump(emit, emit->exit_label, emit->exc_stack_size);
    2776                 :        846 :         return;
    2777                 :            :     }
    2778                 :            : 
    2779         [ +  + ]:      13779 :     if (emit->do_viper_types) {
    2780                 :        996 :         vtype_kind_t return_vtype = emit->scope->scope_flags >> MP_SCOPE_FLAG_VIPERRET_POS;
    2781         [ +  + ]:        996 :         if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
    2782                 :        698 :             emit_pre_pop_discard(emit);
    2783         [ +  + ]:        698 :             if (return_vtype == VTYPE_PYOBJ) {
    2784                 :        508 :                 emit_native_mov_reg_const(emit, REG_PARENT_RET, MP_F_CONST_NONE_OBJ);
    2785                 :            :             } else {
    2786                 :        190 :                 ASM_MOV_REG_IMM(emit->as, REG_ARG_1, 0);
    2787                 :            :             }
    2788                 :            :         } else {
    2789                 :        298 :             vtype_kind_t vtype;
    2790         [ +  + ]:        476 :             emit_pre_pop_reg(emit, &vtype, return_vtype == VTYPE_PYOBJ ? REG_PARENT_RET : REG_ARG_1);
    2791         [ +  + ]:        298 :             if (vtype != return_vtype) {
    2792                 :        298 :                 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
    2793                 :            :                     MP_ERROR_TEXT("return expected '%q' but got '%q'"),
    2794                 :            :                     vtype_to_qstr(return_vtype), vtype_to_qstr(vtype));
    2795                 :            :             }
    2796                 :            :         }
    2797         [ +  + ]:        996 :         if (return_vtype != VTYPE_PYOBJ) {
    2798                 :        368 :             emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, return_vtype, REG_ARG_2);
    2799                 :            :             #if REG_RET != REG_PARENT_RET
    2800                 :            :             ASM_MOV_REG_REG(emit->as, REG_PARENT_RET, REG_RET);
    2801                 :            :             #endif
    2802                 :            :         }
    2803                 :            :     } else {
    2804                 :      12783 :         vtype_kind_t vtype;
    2805                 :      12783 :         emit_pre_pop_reg(emit, &vtype, REG_PARENT_RET);
    2806         [ -  + ]:      12783 :         assert(vtype == VTYPE_PYOBJ);
    2807                 :            :     }
    2808   [ +  +  +  + ]:      13779 :     if (NEED_GLOBAL_EXC_HANDLER(emit)) {
    2809                 :            :         // Save return value for the global exception handler to use
    2810                 :       9447 :         ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_RET_VAL(emit), REG_PARENT_RET);
    2811                 :            :     }
    2812                 :      13779 :     emit_native_unwind_jump(emit, emit->exit_label, emit->exc_stack_size);
    2813                 :            : }
    2814                 :            : 
    2815                 :       2698 : STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
    2816                 :       2698 :     (void)n_args;
    2817         [ -  + ]:       2698 :     assert(n_args == 1);
    2818                 :       2698 :     vtype_kind_t vtype_exc;
    2819                 :       2698 :     emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
    2820         [ +  + ]:       2698 :     if (vtype_exc != VTYPE_PYOBJ) {
    2821                 :          4 :         EMIT_NATIVE_VIPER_TYPE_ERROR(emit, MP_ERROR_TEXT("must raise an object"));
    2822                 :            :     }
    2823                 :            :     // TODO probably make this 1 call to the runtime (which could even call convert, native_raise(obj, type))
    2824                 :       2698 :     emit_call(emit, MP_F_NATIVE_RAISE);
    2825                 :       2698 :     mp_asm_base_suppress_code(&emit->as->base);
    2826                 :       2698 : }
    2827                 :            : 
    2828                 :        929 : STATIC void emit_native_yield(emit_t *emit, int kind) {
    2829                 :            :     // Note: 1 (yield) or 3 (yield from) labels are reserved for this function, starting at *emit->label_slot
    2830                 :            : 
    2831         [ +  + ]:        929 :     if (emit->do_viper_types) {
    2832                 :          8 :         mp_raise_NotImplementedError(MP_ERROR_TEXT("native yield"));
    2833                 :            :     }
    2834                 :        921 :     emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
    2835                 :            : 
    2836                 :        921 :     need_stack_settled(emit);
    2837                 :            : 
    2838         [ +  + ]:        921 :     if (kind == MP_EMIT_YIELD_FROM) {
    2839                 :            : 
    2840                 :            :         // Top of yield-from loop, conceptually implementing:
    2841                 :            :         //     for item in generator:
    2842                 :            :         //         yield item
    2843                 :            : 
    2844                 :            :         // Jump to start of loop
    2845                 :        507 :         emit_native_jump(emit, *emit->label_slot + 2);
    2846                 :            : 
    2847                 :            :         // Label for top of loop
    2848                 :        507 :         emit_native_label_assign(emit, *emit->label_slot + 1);
    2849                 :            :     }
    2850                 :            : 
    2851                 :            :     // Save pointer to current stack position for caller to access yielded value
    2852                 :        921 :     emit_get_stack_pointer_to_reg_for_pop(emit, REG_TEMP0, 1);
    2853                 :        921 :     emit_native_mov_state_reg(emit, OFFSETOF_CODE_STATE_SP, REG_TEMP0);
    2854                 :            : 
    2855                 :            :     // Put return type in return value slot
    2856                 :        921 :     ASM_MOV_REG_IMM(emit->as, REG_TEMP0, MP_VM_RETURN_YIELD);
    2857                 :        921 :     ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_RET_VAL(emit), REG_TEMP0);
    2858                 :            : 
    2859                 :            :     // Save re-entry PC
    2860                 :        921 :     ASM_MOV_REG_PCREL(emit->as, REG_TEMP0, *emit->label_slot);
    2861                 :        921 :     emit_native_mov_state_reg(emit, LOCAL_IDX_GEN_PC(emit), REG_TEMP0);
    2862                 :            : 
    2863                 :            :     // Jump to exit handler
    2864                 :        921 :     ASM_JUMP(emit->as, emit->exit_label);
    2865                 :            : 
    2866                 :            :     // Label re-entry point
    2867                 :        921 :     mp_asm_base_label_assign(&emit->as->base, *emit->label_slot);
    2868                 :            : 
    2869                 :            :     // Re-open any active exception handler
    2870         [ +  + ]:        921 :     if (emit->exc_stack_size > 0) {
    2871                 :            :         // Find innermost active exception handler, to restore as current handler
    2872                 :        195 :         exc_stack_entry_t *e = &emit->exc_stack[emit->exc_stack_size - 1];
    2873         [ +  + ]:        207 :         for (; e >= emit->exc_stack; --e) {
    2874         [ +  + ]:        201 :             if (e->is_active) {
    2875                 :            :                 // Found active handler, get its PC
    2876                 :        189 :                 ASM_MOV_REG_PCREL(emit->as, REG_RET, e->label);
    2877                 :        189 :                 ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_EXC_HANDLER_PC(emit), REG_RET);
    2878                 :        189 :                 break;
    2879                 :            :             }
    2880                 :            :         }
    2881                 :            :     }
    2882                 :            : 
    2883                 :        921 :     emit_native_adjust_stack_size(emit, 1); // send_value
    2884                 :            : 
    2885         [ +  + ]:        921 :     if (kind == MP_EMIT_YIELD_VALUE) {
    2886                 :            :         // Check LOCAL_IDX_EXC_VAL for any injected value
    2887                 :        414 :         ASM_MOV_REG_LOCAL(emit->as, REG_ARG_1, LOCAL_IDX_EXC_VAL(emit));
    2888                 :        414 :         emit_call(emit, MP_F_NATIVE_RAISE);
    2889                 :            :     } else {
    2890                 :            :         // Label loop entry
    2891                 :        507 :         emit_native_label_assign(emit, *emit->label_slot + 2);
    2892                 :            : 
    2893                 :            :         // Get the next item from the delegate generator
    2894                 :        507 :         vtype_kind_t vtype;
    2895                 :        507 :         emit_pre_pop_reg(emit, &vtype, REG_ARG_2); // send_value
    2896                 :        507 :         emit_access_stack(emit, 1, &vtype, REG_ARG_1); // generator
    2897                 :        507 :         ASM_MOV_REG_LOCAL(emit->as, REG_ARG_3, LOCAL_IDX_EXC_VAL(emit)); // throw_value
    2898                 :        507 :         emit_post_push_reg(emit, VTYPE_PYOBJ, REG_ARG_3);
    2899                 :        507 :         emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 1); // ret_value
    2900                 :        507 :         emit_call(emit, MP_F_NATIVE_YIELD_FROM);
    2901                 :            : 
    2902                 :            :         // If returned non-zero then generator continues
    2903                 :        507 :         ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, *emit->label_slot + 1, true);
    2904                 :            : 
    2905                 :            :         // Pop exhausted gen, replace with ret_value
    2906                 :        507 :         emit_native_adjust_stack_size(emit, 1); // ret_value
    2907                 :        507 :         emit_fold_stack_top(emit, REG_ARG_1);
    2908                 :            :     }
    2909                 :        921 : }
    2910                 :            : 
    2911                 :       4629 : STATIC void emit_native_start_except_handler(emit_t *emit) {
    2912                 :            :     // Protected block has finished so leave the current exception handler
    2913                 :       4629 :     emit_native_leave_exc_stack(emit, true);
    2914                 :            : 
    2915                 :            :     // Get and push nlr_buf.ret_val
    2916                 :       4629 :     ASM_MOV_REG_LOCAL(emit->as, REG_TEMP0, LOCAL_IDX_EXC_VAL(emit));
    2917                 :       4629 :     emit_post_push_reg(emit, VTYPE_PYOBJ, REG_TEMP0);
    2918                 :       4629 : }
    2919                 :            : 
    2920                 :       4629 : STATIC void emit_native_end_except_handler(emit_t *emit) {
    2921                 :       4629 :     adjust_stack(emit, -1); // pop the exception (end_finally didn't use it)
    2922                 :       4629 : }
    2923                 :            : 
    2924                 :            : const emit_method_table_t EXPORT_FUN(method_table) = {
    2925                 :            :     #if MICROPY_DYNAMIC_COMPILER
    2926                 :            :     EXPORT_FUN(new),
    2927                 :            :     EXPORT_FUN(free),
    2928                 :            :     #endif
    2929                 :            : 
    2930                 :            :     emit_native_start_pass,
    2931                 :            :     emit_native_end_pass,
    2932                 :            :     emit_native_adjust_stack_size,
    2933                 :            :     emit_native_set_source_line,
    2934                 :            : 
    2935                 :            :     {
    2936                 :            :         emit_native_load_local,
    2937                 :            :         emit_native_load_global,
    2938                 :            :     },
    2939                 :            :     {
    2940                 :            :         emit_native_store_local,
    2941                 :            :         emit_native_store_global,
    2942                 :            :     },
    2943                 :            :     {
    2944                 :            :         emit_native_delete_local,
    2945                 :            :         emit_native_delete_global,
    2946                 :            :     },
    2947                 :            : 
    2948                 :            :     emit_native_label_assign,
    2949                 :            :     emit_native_import,
    2950                 :            :     emit_native_load_const_tok,
    2951                 :            :     emit_native_load_const_small_int,
    2952                 :            :     emit_native_load_const_str,
    2953                 :            :     emit_native_load_const_obj,
    2954                 :            :     emit_native_load_null,
    2955                 :            :     emit_native_load_method,
    2956                 :            :     emit_native_load_build_class,
    2957                 :            :     emit_native_subscr,
    2958                 :            :     emit_native_attr,
    2959                 :            :     emit_native_dup_top,
    2960                 :            :     emit_native_dup_top_two,
    2961                 :            :     emit_native_pop_top,
    2962                 :            :     emit_native_rot_two,
    2963                 :            :     emit_native_rot_three,
    2964                 :            :     emit_native_jump,
    2965                 :            :     emit_native_pop_jump_if,
    2966                 :            :     emit_native_jump_if_or_pop,
    2967                 :            :     emit_native_unwind_jump,
    2968                 :            :     emit_native_setup_block,
    2969                 :            :     emit_native_with_cleanup,
    2970                 :            :     emit_native_end_finally,
    2971                 :            :     emit_native_get_iter,
    2972                 :            :     emit_native_for_iter,
    2973                 :            :     emit_native_for_iter_end,
    2974                 :            :     emit_native_pop_except_jump,
    2975                 :            :     emit_native_unary_op,
    2976                 :            :     emit_native_binary_op,
    2977                 :            :     emit_native_build,
    2978                 :            :     emit_native_store_map,
    2979                 :            :     emit_native_store_comp,
    2980                 :            :     emit_native_unpack_sequence,
    2981                 :            :     emit_native_unpack_ex,
    2982                 :            :     emit_native_make_function,
    2983                 :            :     emit_native_make_closure,
    2984                 :            :     emit_native_call_function,
    2985                 :            :     emit_native_call_method,
    2986                 :            :     emit_native_return_value,
    2987                 :            :     emit_native_raise_varargs,
    2988                 :            :     emit_native_yield,
    2989                 :            : 
    2990                 :            :     emit_native_start_except_handler,
    2991                 :            :     emit_native_end_except_handler,
    2992                 :            : };
    2993                 :            : 
    2994                 :            : #endif

Generated by: LCOV version 1.15-5-g462f71d