LCOV - code coverage report
Current view: top level - py - emitbc.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-148-g136058496.info Lines: 468 468 100.0 %
Date: 2024-12-20 19:05:39 Functions: 72 72 100.0 %
Branches: 161 176 91.5 %

           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-2019 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                 :            : #include <stdbool.h>
      28                 :            : #include <stdint.h>
      29                 :            : #include <stdio.h>
      30                 :            : #include <string.h>
      31                 :            : #include <unistd.h>
      32                 :            : #include <assert.h>
      33                 :            : 
      34                 :            : #include "py/mpstate.h"
      35                 :            : #include "py/smallint.h"
      36                 :            : #include "py/emit.h"
      37                 :            : #include "py/bc0.h"
      38                 :            : 
      39                 :            : #if MICROPY_ENABLE_COMPILER
      40                 :            : 
      41                 :            : #define DUMMY_DATA_SIZE (MP_ENCODE_UINT_MAX_BYTES)
      42                 :            : 
      43                 :            : struct _emit_t {
      44                 :            :     // Accessed as mp_obj_t, so must be aligned as such, and we rely on the
      45                 :            :     // memory allocator returning a suitably aligned pointer.
      46                 :            :     // Should work for cases when mp_obj_t is 64-bit on a 32-bit machine.
      47                 :            :     byte dummy_data[DUMMY_DATA_SIZE];
      48                 :            : 
      49                 :            :     pass_kind_t pass : 8;
      50                 :            : 
      51                 :            :     // Set to true if the code generator should suppress emitted code due to it
      52                 :            :     // being dead code.  This can happen when opcodes immediately follow an
      53                 :            :     // unconditional flow control (eg jump or raise).
      54                 :            :     bool suppress;
      55                 :            : 
      56                 :            :     int stack_size;
      57                 :            : 
      58                 :            :     mp_emit_common_t *emit_common;
      59                 :            :     scope_t *scope;
      60                 :            : 
      61                 :            :     mp_uint_t last_source_line_offset;
      62                 :            :     mp_uint_t last_source_line;
      63                 :            : 
      64                 :            :     size_t max_num_labels;
      65                 :            :     size_t *label_offsets;
      66                 :            : 
      67                 :            :     size_t code_info_offset;
      68                 :            :     size_t code_info_size;
      69                 :            :     size_t bytecode_offset;
      70                 :            :     size_t bytecode_size;
      71                 :            :     byte *code_base; // stores both byte code and code info
      72                 :            :     bool overflow;
      73                 :            : 
      74                 :            :     size_t n_info;
      75                 :            :     size_t n_cell;
      76                 :            : };
      77                 :            : 
      78                 :       3895 : emit_t *emit_bc_new(mp_emit_common_t *emit_common) {
      79                 :       3895 :     emit_t *emit = m_new0(emit_t, 1);
      80                 :       3895 :     emit->emit_common = emit_common;
      81                 :       3895 :     return emit;
      82                 :            : }
      83                 :            : 
      84                 :       3891 : void emit_bc_set_max_num_labels(emit_t *emit, mp_uint_t max_num_labels) {
      85                 :       3891 :     emit->max_num_labels = max_num_labels;
      86                 :       3891 :     emit->label_offsets = m_new(size_t, emit->max_num_labels);
      87                 :       3891 : }
      88                 :            : 
      89                 :       3873 : void emit_bc_free(emit_t *emit) {
      90                 :       3873 :     m_del(size_t, emit->label_offsets, emit->max_num_labels);
      91                 :       3872 :     m_del_obj(emit_t, emit);
      92                 :       3873 : }
      93                 :            : 
      94                 :            : // all functions must go through this one to emit code info
      95                 :     388758 : static uint8_t *emit_get_cur_to_write_code_info(void *emit_in, size_t num_bytes_to_write) {
      96                 :     388758 :     emit_t *emit = emit_in;
      97         [ +  + ]:     388758 :     if (emit->pass < MP_PASS_EMIT) {
      98                 :     206782 :         emit->code_info_offset += num_bytes_to_write;
      99                 :     206782 :         return emit->dummy_data;
     100                 :            :     } else {
     101         [ -  + ]:     181976 :         assert(emit->code_info_offset + num_bytes_to_write <= emit->code_info_size);
     102                 :     181976 :         byte *c = emit->code_base + emit->code_info_offset;
     103                 :     181976 :         emit->code_info_offset += num_bytes_to_write;
     104                 :     181976 :         return c;
     105                 :            :     }
     106                 :            : }
     107                 :            : 
     108                 :      42470 : static void emit_write_code_info_byte(emit_t *emit, byte val) {
     109                 :      42470 :     *emit_get_cur_to_write_code_info(emit, 1) = val;
     110                 :      42472 : }
     111                 :            : 
     112                 :      36970 : static void emit_write_code_info_qstr(emit_t *emit, qstr qst) {
     113                 :      36970 :     mp_encode_uint(emit, emit_get_cur_to_write_code_info, mp_emit_common_use_qstr(emit->emit_common, qst));
     114                 :      36971 : }
     115                 :            : 
     116                 :            : #if MICROPY_ENABLE_SOURCE_LINE
     117                 :     299785 : static void emit_write_code_info_bytes_lines(emit_t *emit, mp_uint_t bytes_to_skip, mp_uint_t lines_to_skip) {
     118         [ +  - ]:     299785 :     assert(bytes_to_skip > 0 || lines_to_skip > 0);
     119         [ +  + ]:     609102 :     while (bytes_to_skip > 0 || lines_to_skip > 0) {
     120                 :     309317 :         mp_uint_t b, l;
     121         [ +  + ]:     309317 :         if (lines_to_skip <= 6 || bytes_to_skip > 0xf) {
     122                 :            :             // use 0b0LLBBBBB encoding
     123                 :     290146 :             b = MIN(bytes_to_skip, 0x1f);
     124         [ +  + ]:     290146 :             if (b < bytes_to_skip) {
     125                 :            :                 // we can't skip any lines until we skip all the bytes
     126                 :            :                 l = 0;
     127                 :            :             } else {
     128                 :     289862 :                 l = MIN(lines_to_skip, 0x3);
     129                 :            :             }
     130                 :     290146 :             *emit_get_cur_to_write_code_info(emit, 1) = b | (l << 5);
     131                 :            :         } else {
     132                 :            :             // use 0b1LLLBBBB 0bLLLLLLLL encoding (l's LSB in second byte)
     133                 :      19171 :             b = MIN(bytes_to_skip, 0xf);
     134                 :      19171 :             l = MIN(lines_to_skip, 0x7ff);
     135                 :      19171 :             byte *ci = emit_get_cur_to_write_code_info(emit, 2);
     136                 :      19171 :             ci[0] = 0x80 | b | ((l >> 4) & 0x70);
     137                 :      19171 :             ci[1] = l;
     138                 :            :         }
     139                 :     309317 :         bytes_to_skip -= b;
     140                 :     309317 :         lines_to_skip -= l;
     141                 :            :     }
     142                 :     299785 : }
     143                 :            : #endif
     144                 :            : 
     145                 :            : // all functions must go through this one to emit byte code
     146                 :    2389600 : static uint8_t *emit_get_cur_to_write_bytecode(void *emit_in, size_t num_bytes_to_write) {
     147                 :    2389600 :     emit_t *emit = emit_in;
     148         [ +  + ]:    2389600 :     if (emit->suppress) {
     149                 :      11049 :         return emit->dummy_data;
     150                 :            :     }
     151         [ +  + ]:    2378551 :     if (emit->pass < MP_PASS_EMIT) {
     152                 :    1055821 :         emit->bytecode_offset += num_bytes_to_write;
     153                 :    1055821 :         return emit->dummy_data;
     154                 :            :     } else {
     155         [ -  + ]:    1322730 :         assert(emit->bytecode_offset + num_bytes_to_write <= emit->bytecode_size);
     156                 :    1322730 :         byte *c = emit->code_base + emit->code_info_size + emit->bytecode_offset;
     157                 :    1322730 :         emit->bytecode_offset += num_bytes_to_write;
     158                 :    1322730 :         return c;
     159                 :            :     }
     160                 :            : }
     161                 :            : 
     162                 :       1340 : static void emit_write_bytecode_raw_byte(emit_t *emit, byte b1) {
     163                 :       1340 :     byte *c = emit_get_cur_to_write_bytecode(emit, 1);
     164                 :       1340 :     c[0] = b1;
     165                 :       1340 : }
     166                 :            : 
     167                 :    1394949 : static void emit_write_bytecode_byte(emit_t *emit, int stack_adj, byte b1) {
     168                 :    1394949 :     mp_emit_bc_adjust_stack_size(emit, stack_adj);
     169                 :    1394946 :     byte *c = emit_get_cur_to_write_bytecode(emit, 1);
     170                 :    1394950 :     c[0] = b1;
     171                 :    1394950 : }
     172                 :            : 
     173                 :            : // Similar to mp_encode_uint(), just some extra handling to encode sign
     174                 :       7965 : static void emit_write_bytecode_byte_int(emit_t *emit, int stack_adj, byte b1, mp_int_t num) {
     175                 :       7965 :     emit_write_bytecode_byte(emit, stack_adj, b1);
     176                 :            : 
     177                 :            :     // We store each 7 bits in a separate byte, and that's how many bytes needed
     178                 :       7965 :     byte buf[MP_ENCODE_UINT_MAX_BYTES];
     179                 :       7965 :     byte *p = buf + sizeof(buf);
     180                 :            :     // We encode in little-ending order, but store in big-endian, to help decoding
     181                 :      16351 :     do {
     182                 :      16351 :         *--p = num & 0x7f;
     183                 :      16351 :         num >>= 7;
     184         [ +  + ]:      16351 :     } while (num != 0 && num != -1);
     185                 :            :     // Make sure that highest bit we stored (mask 0x40) matches sign
     186                 :            :     // of the number. If not, store extra byte just to encode sign
     187   [ +  +  +  + ]:       7965 :     if (num == -1 && (*p & 0x40) == 0) {
     188                 :        167 :         *--p = 0x7f;
     189   [ +  +  +  + ]:       7798 :     } else if (num == 0 && (*p & 0x40) != 0) {
     190                 :       2254 :         *--p = 0;
     191                 :            :     }
     192                 :            : 
     193                 :       7965 :     byte *c = emit_get_cur_to_write_bytecode(emit, buf + sizeof(buf) - p);
     194         [ +  + ]:      18771 :     while (p != buf + sizeof(buf) - 1) {
     195                 :      10806 :         *c++ = *p++ | 0x80;
     196                 :            :     }
     197                 :       7965 :     *c = *p;
     198                 :       7965 : }
     199                 :            : 
     200                 :     744416 : static void emit_write_bytecode_byte_uint(emit_t *emit, int stack_adj, byte b, mp_uint_t val) {
     201                 :     744416 :     emit_write_bytecode_byte(emit, stack_adj, b);
     202                 :     744415 :     mp_encode_uint(emit, emit_get_cur_to_write_bytecode, val);
     203                 :     744417 : }
     204                 :            : 
     205                 :      36577 : static void emit_write_bytecode_byte_const(emit_t *emit, int stack_adj, byte b, mp_uint_t n) {
     206                 :      36577 :     emit_write_bytecode_byte_uint(emit, stack_adj, b, n);
     207                 :      36577 : }
     208                 :            : 
     209                 :     362447 : static void emit_write_bytecode_byte_qstr(emit_t *emit, int stack_adj, byte b, qstr qst) {
     210                 :     362447 :     emit_write_bytecode_byte_uint(emit, stack_adj, b, mp_emit_common_use_qstr(emit->emit_common, qst));
     211                 :     362445 : }
     212                 :            : 
     213                 :      19998 : static void emit_write_bytecode_byte_obj(emit_t *emit, int stack_adj, byte b, mp_obj_t obj) {
     214                 :      19998 :     emit_write_bytecode_byte_const(emit, stack_adj, b, mp_emit_common_use_const_obj(emit->emit_common, obj));
     215                 :      19998 : }
     216                 :            : 
     217                 :      16579 : static void emit_write_bytecode_byte_child(emit_t *emit, int stack_adj, byte b, mp_raw_code_t *rc) {
     218         [ +  + ]:      19969 :     emit_write_bytecode_byte_const(emit, stack_adj, b,
     219                 :            :         mp_emit_common_alloc_const_child(emit->emit_common, rc));
     220                 :            :     #if MICROPY_PY_SYS_SETTRACE
     221                 :            :     rc->line_of_definition = emit->last_source_line;
     222                 :            :     #endif
     223                 :      16579 : }
     224                 :            : 
     225                 :            : // Emit a jump opcode to a destination label.
     226                 :            : // The offset to the label is relative to the ip following this instruction.
     227                 :            : // The offset is encoded as either 1 or 2 bytes, depending on how big it is.
     228                 :            : // The encoding of this jump opcode can change size from one pass to the next,
     229                 :            : // but it must only ever decrease in size on successive passes.
     230                 :     244458 : static void emit_write_bytecode_byte_label(emit_t *emit, int stack_adj, byte b1, mp_uint_t label) {
     231                 :     244458 :     mp_emit_bc_adjust_stack_size(emit, stack_adj);
     232                 :            : 
     233         [ +  + ]:     244458 :     if (emit->suppress) {
     234                 :            :         return;
     235                 :            :     }
     236                 :            : 
     237                 :            :     // Determine if the jump offset is signed or unsigned, based on the opcode.
     238                 :     240900 :     const bool is_signed = b1 <= MP_BC_POP_JUMP_IF_FALSE;
     239                 :            : 
     240                 :            :     // Default to a 2-byte encoding (the largest) with an unknown jump offset.
     241                 :     240900 :     unsigned int jump_encoding_size = 1;
     242                 :     240900 :     ssize_t bytecode_offset = 0;
     243                 :            : 
     244                 :            :     // Compute the jump size and offset only when code size is known.
     245         [ +  + ]:     240900 :     if (emit->pass >= MP_PASS_CODE_SIZE) {
     246                 :            :         // The -2 accounts for this jump opcode taking 2 bytes (at least).
     247                 :     189599 :         bytecode_offset = emit->label_offsets[label] - emit->bytecode_offset - 2;
     248                 :            : 
     249                 :            :         // Check if the bytecode_offset is small enough to use a 1-byte encoding.
     250   [ +  +  +  + ]:     189599 :         if ((is_signed && -64 <= bytecode_offset && bytecode_offset <= 63)
     251         [ +  + ]:     159619 :             || (!is_signed && (size_t)bytecode_offset <= 127)) {
     252                 :            :             // Use a 1-byte jump offset.
     253                 :     118476 :             jump_encoding_size = 0;
     254                 :            :         }
     255                 :            : 
     256                 :            :         // Adjust the offset depending on the size of the encoding of the offset.
     257                 :     189599 :         bytecode_offset -= jump_encoding_size;
     258                 :            : 
     259         [ -  + ]:     189599 :         assert(is_signed || bytecode_offset >= 0);
     260                 :            :     }
     261                 :            : 
     262                 :            :     // Emit the opcode.
     263                 :     240900 :     byte *c = emit_get_cur_to_write_bytecode(emit, 2 + jump_encoding_size);
     264                 :     240900 :     c[0] = b1;
     265         [ +  + ]:     240900 :     if (jump_encoding_size == 0) {
     266         [ +  + ]:     118476 :         if (is_signed) {
     267                 :      29980 :             bytecode_offset += 0x40;
     268                 :            :         }
     269         [ -  + ]:     118476 :         assert(0 <= bytecode_offset && bytecode_offset <= 0x7f);
     270                 :     118476 :         c[1] = bytecode_offset;
     271                 :            :     } else {
     272         [ +  + ]:     122424 :         if (is_signed) {
     273                 :      36547 :             bytecode_offset += 0x4000;
     274                 :            :         }
     275   [ +  +  +  + ]:     122424 :         if (emit->pass == MP_PASS_EMIT && !(0 <= bytecode_offset && bytecode_offset <= 0x7fff)) {
     276                 :         69 :             emit->overflow = true;
     277                 :            :         }
     278                 :     122424 :         c[1] = 0x80 | (bytecode_offset & 0x7f);
     279                 :     122424 :         c[2] = bytecode_offset >> 7;
     280                 :            :     }
     281                 :            : }
     282                 :            : 
     283                 :      26806 : void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
     284                 :      26806 :     emit->pass = pass;
     285                 :      26806 :     emit->stack_size = 0;
     286                 :      26806 :     emit->suppress = false;
     287                 :      26806 :     emit->scope = scope;
     288                 :      26806 :     emit->last_source_line_offset = 0;
     289                 :      26806 :     emit->last_source_line = 1;
     290                 :      26806 :     emit->bytecode_offset = 0;
     291                 :      26806 :     emit->code_info_offset = 0;
     292                 :      26806 :     emit->overflow = false;
     293                 :            : 
     294                 :            :     // Write local state size, exception stack size, scope flags and number of arguments
     295                 :            :     {
     296                 :      26806 :         mp_uint_t n_state = scope->num_locals + scope->stack_size;
     297         [ +  + ]:      26806 :         if (n_state == 0) {
     298                 :            :             // Need at least 1 entry in the state, in the case an exception is
     299                 :            :             // propagated through this function, the exception is returned in
     300                 :            :             // the highest slot in the state (fastn[0], see vm.c).
     301                 :      13670 :             n_state = 1;
     302                 :            :         }
     303                 :            :         #if MICROPY_DEBUG_VM_STACK_OVERFLOW
     304                 :            :         // An extra slot in the stack is needed to detect VM stack overflow
     305                 :            :         n_state += 1;
     306                 :            :         #endif
     307                 :            : 
     308                 :      26806 :         size_t n_exc_stack = scope->exc_stack_size;
     309         [ +  + ]:      30942 :         MP_BC_PRELUDE_SIG_ENCODE(n_state, n_exc_stack, scope, emit_write_code_info_byte, emit);
     310                 :            :     }
     311                 :            : 
     312                 :            :     // Write number of cells and size of the source code info
     313         [ +  + ]:      26807 :     if (emit->pass >= MP_PASS_CODE_SIZE) {
     314                 :      10970 :         size_t n_info = emit->n_info;
     315                 :      10970 :         size_t n_cell = emit->n_cell;
     316   [ +  +  +  + ]:      11165 :         MP_BC_PRELUDE_SIZE_ENCODE(n_info, n_cell, emit_write_code_info_byte, emit);
     317                 :            :     }
     318                 :            : 
     319                 :      26808 :     emit->n_info = emit->code_info_offset;
     320                 :            : 
     321                 :            :     // Write the name of this function.
     322                 :      26808 :     emit_write_code_info_qstr(emit, scope->simple_name);
     323                 :            : 
     324                 :            :     // Write argument names, needed to resolve positional args passed as keywords.
     325                 :            :     {
     326                 :            :         // For a given argument position (indexed by i) we need to find the
     327                 :            :         // corresponding id_info which is a parameter, as it has the correct
     328                 :            :         // qstr name to use as the argument name.  Note that it's not a simple
     329                 :            :         // 1-1 mapping (ie i!=j in general) because of possible closed-over
     330                 :            :         // variables.  In the case that the argument i has no corresponding
     331                 :            :         // parameter we use "*" as its name (since no argument can ever be named
     332                 :            :         // "*").  We could use a blank qstr but "*" is better for debugging.
     333                 :            :         // Note: there is some wasted RAM here for the case of storing a qstr
     334                 :            :         // for each closed-over variable, and maybe there is a better way to do
     335                 :            :         // it, but that would require changes to mp_setup_code_state.
     336         [ +  + ]:      36970 :         for (int i = 0; i < scope->num_pos_args + scope->num_kwonly_args; i++) {
     337                 :      18795 :             qstr qst = MP_QSTR__star_;
     338         [ +  + ]:      18795 :             for (int j = 0; j < scope->id_info_len; ++j) {
     339                 :      18048 :                 id_info_t *id = &scope->id_info[j];
     340   [ +  +  +  + ]:      18048 :                 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
     341                 :       9415 :                     qst = id->qst;
     342                 :       9415 :                     break;
     343                 :            :                 }
     344                 :            :             }
     345                 :      10162 :             emit_write_code_info_qstr(emit, qst);
     346                 :            :         }
     347                 :            :     }
     348                 :      26809 : }
     349                 :            : 
     350                 :      26802 : bool mp_emit_bc_end_pass(emit_t *emit) {
     351         [ +  + ]:      26802 :     if (emit->pass == MP_PASS_SCOPE) {
     352                 :            :         return true;
     353                 :            :     }
     354                 :            : 
     355                 :            :     // check stack is back to zero size
     356         [ -  + ]:      16382 :     assert(emit->stack_size == 0);
     357                 :            : 
     358                 :            :     // Calculate size of source code info section
     359                 :      16382 :     emit->n_info = emit->code_info_offset - emit->n_info;
     360                 :            : 
     361                 :            :     // Emit closure section of prelude
     362                 :      16382 :     emit->n_cell = 0;
     363         [ +  + ]:      83928 :     for (size_t i = 0; i < emit->scope->id_info_len; ++i) {
     364                 :      67544 :         id_info_t *id = &emit->scope->id_info[i];
     365         [ +  + ]:      67544 :         if (id->kind == ID_INFO_KIND_CELL) {
     366         [ -  + ]:        363 :             assert(id->local_num <= 255);
     367                 :        363 :             emit_write_code_info_byte(emit, id->local_num); // write the local which should be converted to a cell
     368                 :        365 :             ++emit->n_cell;
     369                 :            :         }
     370                 :            :     }
     371                 :            : 
     372         [ +  + ]:      16384 :     if (emit->pass == MP_PASS_CODE_SIZE) {
     373                 :            :         // calculate size of total code-info + bytecode, in bytes
     374                 :       5414 :         emit->code_info_size = emit->code_info_offset;
     375                 :       5414 :         emit->bytecode_size = emit->bytecode_offset;
     376                 :       5414 :         emit->code_base = m_new0(byte, emit->code_info_size + emit->bytecode_size);
     377                 :            : 
     378         [ +  + ]:      10970 :     } else if (emit->pass == MP_PASS_EMIT) {
     379                 :            :         // Code info and/or bytecode can shrink during this pass.
     380         [ -  + ]:       5557 :         assert(emit->code_info_offset <= emit->code_info_size);
     381         [ -  + ]:       5557 :         assert(emit->bytecode_offset <= emit->bytecode_size);
     382                 :            : 
     383         [ +  + ]:       5557 :         if (emit->code_info_offset != emit->code_info_size
     384         [ +  + ]:       5556 :             || emit->bytecode_offset != emit->bytecode_size) {
     385                 :            :             // Code info and/or bytecode changed size in this pass, so request the
     386                 :            :             // compiler to do another pass with these updated sizes.
     387                 :        143 :             emit->code_info_size = emit->code_info_offset;
     388                 :        143 :             emit->bytecode_size = emit->bytecode_offset;
     389                 :        143 :             return false;
     390                 :            :         }
     391                 :            : 
     392         [ +  + ]:       5414 :         if (emit->overflow) {
     393                 :          2 :             mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("bytecode overflow"));
     394                 :            :         }
     395                 :            : 
     396                 :            :         #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
     397                 :       5412 :         size_t bytecode_len = emit->code_info_size + emit->bytecode_size;
     398                 :            :         #if MICROPY_DEBUG_PRINTERS
     399                 :       5412 :         emit->scope->raw_code_data_len = bytecode_len;
     400                 :            :         #endif
     401                 :            :         #endif
     402                 :            : 
     403                 :            :         // Bytecode is finalised, assign it to the raw code object.
     404                 :       5412 :         mp_emit_glue_assign_bytecode(emit->scope->raw_code, emit->code_base,
     405                 :       5412 :             emit->emit_common->children,
     406                 :            :             #if MICROPY_PERSISTENT_CODE_SAVE
     407                 :            :             bytecode_len,
     408                 :            :             emit->emit_common->ct_cur_child,
     409                 :            :             #endif
     410                 :       5412 :             emit->scope->scope_flags);
     411                 :            :     }
     412                 :            : 
     413                 :            :     return true;
     414                 :            : }
     415                 :            : 
     416                 :    1819746 : void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
     417         [ +  + ]:    1819746 :     if (emit->pass == MP_PASS_SCOPE) {
     418                 :            :         return;
     419                 :            :     }
     420         [ -  + ]:    1514049 :     assert((mp_int_t)emit->stack_size + delta >= 0);
     421                 :    1514049 :     emit->stack_size += delta;
     422         [ +  + ]:    1514049 :     if (emit->stack_size > emit->scope->stack_size) {
     423                 :      20002 :         emit->scope->stack_size = emit->stack_size;
     424                 :            :     }
     425                 :            : }
     426                 :            : 
     427                 :     901222 : void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
     428                 :            :     #if MICROPY_ENABLE_SOURCE_LINE
     429         [ +  + ]:     901222 :     if (MP_STATE_VM(mp_optimise_value) >= 3) {
     430                 :            :         // If we compile with -O3, don't store line numbers.
     431                 :            :         return;
     432                 :            :     }
     433         [ +  + ]:     901174 :     if (source_line > emit->last_source_line) {
     434                 :     299785 :         mp_uint_t bytes_to_skip = emit->bytecode_offset - emit->last_source_line_offset;
     435                 :     299785 :         mp_uint_t lines_to_skip = source_line - emit->last_source_line;
     436                 :     299785 :         emit_write_code_info_bytes_lines(emit, bytes_to_skip, lines_to_skip);
     437                 :     299785 :         emit->last_source_line_offset = emit->bytecode_offset;
     438                 :     299785 :         emit->last_source_line = source_line;
     439                 :            :     }
     440                 :            :     #else
     441                 :            :     (void)emit;
     442                 :            :     (void)source_line;
     443                 :            :     #endif
     444                 :            : }
     445                 :            : 
     446                 :     254039 : void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l) {
     447                 :            :     // Assigning a label ends any dead-code region, and all following opcodes
     448                 :            :     // should be emitted (until another unconditional flow control).
     449                 :     254039 :     emit->suppress = false;
     450                 :            : 
     451         [ +  + ]:     254039 :     if (emit->pass == MP_PASS_SCOPE) {
     452                 :            :         return;
     453                 :            :     }
     454                 :            : 
     455                 :            :     // Label offsets can change from one pass to the next, but they must only
     456                 :            :     // decrease (ie code can only shrink).  There will be multiple MP_PASS_EMIT
     457                 :            :     // stages until the labels no longer change, which is when the code size
     458                 :            :     // stays constant after a MP_PASS_EMIT.
     459         [ -  + ]:     217977 :     assert(l < emit->max_num_labels);
     460   [ +  +  -  + ]:     217977 :     assert(emit->pass == MP_PASS_STACK_SIZE || emit->bytecode_offset <= emit->label_offsets[l]);
     461                 :            : 
     462                 :            :     // Assign label offset.
     463                 :     217977 :     emit->label_offsets[l] = emit->bytecode_offset;
     464                 :            : }
     465                 :            : 
     466                 :       6643 : void mp_emit_bc_import(emit_t *emit, qstr qst, int kind) {
     467                 :       6643 :     MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_NAME == MP_BC_IMPORT_NAME);
     468                 :       6643 :     MP_STATIC_ASSERT(MP_BC_IMPORT_NAME + MP_EMIT_IMPORT_FROM == MP_BC_IMPORT_FROM);
     469         [ +  + ]:       6643 :     int stack_adj = kind == MP_EMIT_IMPORT_FROM ? 1 : -1;
     470         [ +  + ]:       4866 :     if (kind == MP_EMIT_IMPORT_STAR) {
     471                 :        215 :         emit_write_bytecode_byte(emit, stack_adj, MP_BC_IMPORT_STAR);
     472                 :            :     } else {
     473                 :       6428 :         emit_write_bytecode_byte_qstr(emit, stack_adj, MP_BC_IMPORT_NAME + kind, qst);
     474                 :            :     }
     475                 :       6643 : }
     476                 :            : 
     477                 :      83769 : void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
     478                 :      83769 :     MP_STATIC_ASSERT(MP_BC_LOAD_CONST_FALSE + (MP_TOKEN_KW_NONE - MP_TOKEN_KW_FALSE) == MP_BC_LOAD_CONST_NONE);
     479                 :      83769 :     MP_STATIC_ASSERT(MP_BC_LOAD_CONST_FALSE + (MP_TOKEN_KW_TRUE - MP_TOKEN_KW_FALSE) == MP_BC_LOAD_CONST_TRUE);
     480         [ +  + ]:      83769 :     if (tok == MP_TOKEN_ELLIPSIS) {
     481                 :         12 :         emit_write_bytecode_byte_obj(emit, 1, MP_BC_LOAD_CONST_OBJ, MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj));
     482                 :            :     } else {
     483                 :      83757 :         emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_CONST_FALSE + (tok - MP_TOKEN_KW_FALSE));
     484                 :            :     }
     485                 :      83769 : }
     486                 :            : 
     487                 :      67822 : void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
     488         [ -  + ]:      67822 :     assert(MP_SMALL_INT_FITS(arg));
     489                 :      67822 :     if (-MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS <= arg
     490         [ +  + ]:      67822 :         && arg < MP_BC_LOAD_CONST_SMALL_INT_MULTI_NUM - MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS) {
     491                 :      59857 :         emit_write_bytecode_byte(emit, 1,
     492                 :      59857 :             MP_BC_LOAD_CONST_SMALL_INT_MULTI + MP_BC_LOAD_CONST_SMALL_INT_MULTI_EXCESS + arg);
     493                 :            :     } else {
     494                 :       7965 :         emit_write_bytecode_byte_int(emit, 1, MP_BC_LOAD_CONST_SMALL_INT, arg);
     495                 :            :     }
     496                 :      67822 : }
     497                 :            : 
     498                 :      45892 : void mp_emit_bc_load_const_str(emit_t *emit, qstr qst) {
     499                 :      45892 :     emit_write_bytecode_byte_qstr(emit, 1, MP_BC_LOAD_CONST_STRING, qst);
     500                 :      45888 : }
     501                 :            : 
     502                 :      19986 : void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj) {
     503                 :      19986 :     emit_write_bytecode_byte_obj(emit, 1, MP_BC_LOAD_CONST_OBJ, obj);
     504                 :      19986 : }
     505                 :            : 
     506                 :       2237 : void mp_emit_bc_load_null(emit_t *emit) {
     507                 :       2237 :     emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_NULL);
     508                 :       2237 : }
     509                 :            : 
     510                 :      31601 : void mp_emit_bc_load_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
     511                 :      31601 :     MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_LOAD_FAST_N);
     512                 :      31601 :     MP_STATIC_ASSERT(MP_BC_LOAD_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_LOAD_DEREF);
     513                 :      31601 :     (void)qst;
     514         [ +  + ]:      31601 :     if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
     515                 :      30594 :         emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_FAST_MULTI + local_num);
     516                 :            :     } else {
     517                 :       1007 :         emit_write_bytecode_byte_uint(emit, 1, MP_BC_LOAD_FAST_N + kind, local_num);
     518                 :            :     }
     519                 :      31601 : }
     520                 :            : 
     521                 :     197477 : void mp_emit_bc_load_global(emit_t *emit, qstr qst, int kind) {
     522                 :     197477 :     MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_LOAD_NAME);
     523                 :     197477 :     MP_STATIC_ASSERT(MP_BC_LOAD_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_LOAD_GLOBAL);
     524                 :     197477 :     (void)qst;
     525                 :     197477 :     emit_write_bytecode_byte_qstr(emit, 1, MP_BC_LOAD_NAME + kind, qst);
     526                 :     197476 : }
     527                 :            : 
     528                 :      28457 : void mp_emit_bc_load_method(emit_t *emit, qstr qst, bool is_super) {
     529                 :      28457 :     int stack_adj = 1 - 2 * is_super;
     530         [ +  + ]:      56801 :     emit_write_bytecode_byte_qstr(emit, stack_adj, is_super ? MP_BC_LOAD_SUPER_METHOD : MP_BC_LOAD_METHOD, qst);
     531                 :      28457 : }
     532                 :            : 
     533                 :       2673 : void mp_emit_bc_load_build_class(emit_t *emit) {
     534                 :       2673 :     emit_write_bytecode_byte(emit, 1, MP_BC_LOAD_BUILD_CLASS);
     535                 :       2673 : }
     536                 :            : 
     537                 :      10119 : void mp_emit_bc_subscr(emit_t *emit, int kind) {
     538         [ +  + ]:      10119 :     if (kind == MP_EMIT_SUBSCR_LOAD) {
     539                 :       7236 :         emit_write_bytecode_byte(emit, -1, MP_BC_LOAD_SUBSCR);
     540                 :            :     } else {
     541         [ +  + ]:       2883 :         if (kind == MP_EMIT_SUBSCR_DELETE) {
     542                 :        318 :             mp_emit_bc_load_null(emit);
     543                 :        318 :             mp_emit_bc_rot_three(emit);
     544                 :            :         }
     545                 :       2883 :         emit_write_bytecode_byte(emit, -3, MP_BC_STORE_SUBSCR);
     546                 :            :     }
     547                 :      10119 : }
     548                 :            : 
     549                 :      22719 : void mp_emit_bc_attr(emit_t *emit, qstr qst, int kind) {
     550         [ +  + ]:      22719 :     if (kind == MP_EMIT_ATTR_LOAD) {
     551                 :      18086 :         emit_write_bytecode_byte_qstr(emit, 0, MP_BC_LOAD_ATTR, qst);
     552                 :            :     } else {
     553         [ +  + ]:       4633 :         if (kind == MP_EMIT_ATTR_DELETE) {
     554                 :         84 :             mp_emit_bc_load_null(emit);
     555                 :         84 :             mp_emit_bc_rot_two(emit);
     556                 :            :         }
     557                 :       4633 :         emit_write_bytecode_byte_qstr(emit, -2, MP_BC_STORE_ATTR, qst);
     558                 :            :     }
     559                 :      22719 : }
     560                 :            : 
     561                 :       8217 : void mp_emit_bc_store_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
     562                 :       8217 :     MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_STORE_FAST_N);
     563                 :       8217 :     MP_STATIC_ASSERT(MP_BC_STORE_FAST_N + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_STORE_DEREF);
     564                 :       8217 :     (void)qst;
     565         [ +  + ]:       8217 :     if (kind == MP_EMIT_IDOP_LOCAL_FAST && local_num <= 15) {
     566                 :       7362 :         emit_write_bytecode_byte(emit, -1, MP_BC_STORE_FAST_MULTI + local_num);
     567                 :            :     } else {
     568                 :        855 :         emit_write_bytecode_byte_uint(emit, -1, MP_BC_STORE_FAST_N + kind, local_num);
     569                 :            :     }
     570                 :       8217 : }
     571                 :            : 
     572                 :      61045 : void mp_emit_bc_store_global(emit_t *emit, qstr qst, int kind) {
     573                 :      61045 :     MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_STORE_NAME);
     574                 :      61045 :     MP_STATIC_ASSERT(MP_BC_STORE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_STORE_GLOBAL);
     575                 :      61045 :     emit_write_bytecode_byte_qstr(emit, -1, MP_BC_STORE_NAME + kind, qst);
     576                 :      61045 : }
     577                 :            : 
     578                 :        416 : void mp_emit_bc_delete_local(emit_t *emit, qstr qst, mp_uint_t local_num, int kind) {
     579                 :        416 :     MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_FAST == MP_BC_DELETE_FAST);
     580                 :        416 :     MP_STATIC_ASSERT(MP_BC_DELETE_FAST + MP_EMIT_IDOP_LOCAL_DEREF == MP_BC_DELETE_DEREF);
     581                 :        416 :     (void)qst;
     582                 :        416 :     emit_write_bytecode_byte_uint(emit, 0, MP_BC_DELETE_FAST + kind, local_num);
     583                 :        416 : }
     584                 :            : 
     585                 :        433 : void mp_emit_bc_delete_global(emit_t *emit, qstr qst, int kind) {
     586                 :        433 :     MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_NAME == MP_BC_DELETE_NAME);
     587                 :        433 :     MP_STATIC_ASSERT(MP_BC_DELETE_NAME + MP_EMIT_IDOP_GLOBAL_GLOBAL == MP_BC_DELETE_GLOBAL);
     588                 :        433 :     emit_write_bytecode_byte_qstr(emit, 0, MP_BC_DELETE_NAME + kind, qst);
     589                 :        433 : }
     590                 :            : 
     591                 :      47701 : void mp_emit_bc_dup_top(emit_t *emit) {
     592                 :      47701 :     emit_write_bytecode_byte(emit, 1, MP_BC_DUP_TOP);
     593                 :      47701 : }
     594                 :            : 
     595                 :        745 : void mp_emit_bc_dup_top_two(emit_t *emit) {
     596                 :        745 :     emit_write_bytecode_byte(emit, 2, MP_BC_DUP_TOP_TWO);
     597                 :        745 : }
     598                 :            : 
     599                 :     151124 : void mp_emit_bc_pop_top(emit_t *emit) {
     600                 :     151124 :     emit_write_bytecode_byte(emit, -1, MP_BC_POP_TOP);
     601                 :     151125 : }
     602                 :            : 
     603                 :       1345 : void mp_emit_bc_rot_two(emit_t *emit) {
     604                 :       1345 :     emit_write_bytecode_byte(emit, 0, MP_BC_ROT_TWO);
     605                 :       1345 : }
     606                 :            : 
     607                 :        857 : void mp_emit_bc_rot_three(emit_t *emit) {
     608                 :        857 :     emit_write_bytecode_byte(emit, 0, MP_BC_ROT_THREE);
     609                 :        857 : }
     610                 :            : 
     611                 :       9351 : void mp_emit_bc_jump(emit_t *emit, mp_uint_t label) {
     612                 :       9351 :     emit_write_bytecode_byte_label(emit, 0, MP_BC_JUMP, label);
     613                 :       9351 :     emit->suppress = true;
     614                 :       9351 : }
     615                 :            : 
     616                 :      57130 : void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
     617         [ +  + ]:      57130 :     if (cond) {
     618                 :       5333 :         emit_write_bytecode_byte_label(emit, -1, MP_BC_POP_JUMP_IF_TRUE, label);
     619                 :            :     } else {
     620                 :      51797 :         emit_write_bytecode_byte_label(emit, -1, MP_BC_POP_JUMP_IF_FALSE, label);
     621                 :            :     }
     622                 :      57130 : }
     623                 :            : 
     624                 :        510 : void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
     625         [ +  + ]:        510 :     if (cond) {
     626                 :        255 :         emit_write_bytecode_byte_label(emit, -1, MP_BC_JUMP_IF_TRUE_OR_POP, label);
     627                 :            :     } else {
     628                 :        255 :         emit_write_bytecode_byte_label(emit, -1, MP_BC_JUMP_IF_FALSE_OR_POP, label);
     629                 :            :     }
     630                 :        510 : }
     631                 :            : 
     632                 :        782 : void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
     633         [ +  + ]:        782 :     if (except_depth == 0) {
     634         [ +  + ]:        540 :         if (label & MP_EMIT_BREAK_FROM_FOR) {
     635                 :            :             // need to pop the iterator if we are breaking out of a for loop
     636                 :        203 :             emit_write_bytecode_raw_byte(emit, MP_BC_POP_TOP);
     637                 :            :             // also pop the iter_buf
     638         [ +  + ]:        812 :             for (size_t i = 0; i < MP_OBJ_ITER_BUF_NSLOTS - 1; ++i) {
     639                 :        609 :                 emit_write_bytecode_raw_byte(emit, MP_BC_POP_TOP);
     640                 :            :             }
     641                 :            :         }
     642                 :        540 :         emit_write_bytecode_byte_label(emit, 0, MP_BC_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
     643                 :            :     } else {
     644                 :        242 :         emit_write_bytecode_byte_label(emit, 0, MP_BC_UNWIND_JUMP, label & ~MP_EMIT_BREAK_FROM_FOR);
     645                 :        242 :         emit_write_bytecode_raw_byte(emit, ((label & MP_EMIT_BREAK_FROM_FOR) ? 0x80 : 0) | except_depth);
     646                 :            :     }
     647                 :        782 :     emit->suppress = true;
     648                 :        782 : }
     649                 :            : 
     650                 :      83799 : void mp_emit_bc_setup_block(emit_t *emit, mp_uint_t label, int kind) {
     651                 :      83799 :     MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_WITH == MP_BC_SETUP_WITH);
     652                 :      83799 :     MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_EXCEPT == MP_BC_SETUP_EXCEPT);
     653                 :      83799 :     MP_STATIC_ASSERT(MP_BC_SETUP_WITH + MP_EMIT_SETUP_BLOCK_FINALLY == MP_BC_SETUP_FINALLY);
     654                 :            :     // The SETUP_WITH opcode pops ctx_mgr from the top of the stack
     655                 :            :     // and then pushes 3 entries: __exit__, ctx_mgr, as_value.
     656         [ +  + ]:      83799 :     int stack_adj = kind == MP_EMIT_SETUP_BLOCK_WITH ? 2 : 0;
     657                 :      83799 :     emit_write_bytecode_byte_label(emit, stack_adj, MP_BC_SETUP_WITH + kind, label);
     658                 :      83799 : }
     659                 :            : 
     660                 :      36743 : void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label) {
     661                 :      36743 :     mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE);
     662                 :      36743 :     mp_emit_bc_label_assign(emit, label);
     663                 :            :     // The +2 is to ensure we have enough stack space to call the __exit__ method
     664                 :      36743 :     emit_write_bytecode_byte(emit, 2, MP_BC_WITH_CLEANUP);
     665                 :            :     // Cancel the +2 above, plus the +2 from mp_emit_bc_setup_block(MP_EMIT_SETUP_BLOCK_WITH)
     666                 :      36743 :     mp_emit_bc_adjust_stack_size(emit, -4);
     667                 :      36743 : }
     668                 :            : 
     669                 :            : #if MICROPY_PY_ASYNC_AWAIT
     670                 :         70 : void mp_emit_bc_async_with_setup_finally(emit_t *emit, mp_uint_t label_aexit_no_exc, mp_uint_t label_finally_block, mp_uint_t label_ret_unwind_jump) {
     671                 :            :     // The async-with body has executed and no exception was raised, the execution fell through to this point.
     672                 :            :     // Stack: (..., ctx_mgr)
     673                 :            : 
     674                 :            :     // Finish async-with body and prepare to enter "finally" block.
     675                 :         70 :     mp_emit_bc_load_const_tok(emit, MP_TOKEN_KW_NONE); // to tell end_finally there's no exception
     676                 :         70 :     mp_emit_bc_rot_two(emit);
     677                 :         70 :     mp_emit_bc_jump(emit, label_aexit_no_exc); // jump to code to call __aexit__
     678                 :            : 
     679                 :            :     // Start of "finally" block which is entered via one of: an exception propagating out, a return, an unwind jump.
     680                 :         70 :     mp_emit_bc_label_assign(emit, label_finally_block);
     681                 :            : 
     682                 :            :     // Detect which case we have by the TOS being an exception or not.
     683                 :         70 :     mp_emit_bc_dup_top(emit);
     684                 :         70 :     mp_emit_bc_load_global(emit, MP_QSTR_BaseException, MP_EMIT_IDOP_GLOBAL_GLOBAL);
     685                 :         70 :     mp_emit_bc_binary_op(emit, MP_BINARY_OP_EXCEPTION_MATCH);
     686                 :         70 :     mp_emit_bc_pop_jump_if(emit, false, label_ret_unwind_jump); // if not an exception then we have return or unwind jump.
     687                 :         70 : }
     688                 :            : #endif
     689                 :            : 
     690                 :      83799 : void mp_emit_bc_end_finally(emit_t *emit) {
     691                 :      83799 :     emit_write_bytecode_byte(emit, -1, MP_BC_END_FINALLY);
     692                 :      83799 : }
     693                 :            : 
     694                 :       4155 : void mp_emit_bc_get_iter(emit_t *emit, bool use_stack) {
     695         [ +  + ]:       4155 :     int stack_adj = use_stack ? MP_OBJ_ITER_BUF_NSLOTS - 1 : 0;
     696                 :       4155 :     emit_write_bytecode_byte(emit, stack_adj, use_stack ? MP_BC_GET_ITER_STACK : MP_BC_GET_ITER);
     697                 :       4155 : }
     698                 :            : 
     699                 :       2567 : void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label) {
     700                 :       2567 :     emit_write_bytecode_byte_label(emit, 1, MP_BC_FOR_ITER, label);
     701                 :       2567 : }
     702                 :            : 
     703                 :       2567 : void mp_emit_bc_for_iter_end(emit_t *emit) {
     704                 :       2567 :     mp_emit_bc_adjust_stack_size(emit, -MP_OBJ_ITER_BUF_NSLOTS);
     705                 :       2567 : }
     706                 :            : 
     707                 :      90319 : void mp_emit_bc_pop_except_jump(emit_t *emit, mp_uint_t label, bool within_exc_handler) {
     708                 :      90319 :     (void)within_exc_handler;
     709                 :      90319 :     emit_write_bytecode_byte_label(emit, 0, MP_BC_POP_EXCEPT_JUMP, label);
     710                 :      90319 :     emit->suppress = true;
     711                 :      90319 : }
     712                 :            : 
     713                 :       2240 : void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op) {
     714                 :       2240 :     emit_write_bytecode_byte(emit, 0, MP_BC_UNARY_OP_MULTI + op);
     715                 :       2240 : }
     716                 :            : 
     717                 :      70579 : void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op) {
     718                 :      70579 :     bool invert = false;
     719         [ +  + ]:      70579 :     if (op == MP_BINARY_OP_NOT_IN) {
     720                 :            :         invert = true;
     721                 :            :         op = MP_BINARY_OP_IN;
     722         [ +  + ]:      70319 :     } else if (op == MP_BINARY_OP_IS_NOT) {
     723                 :       1110 :         invert = true;
     724                 :       1110 :         op = MP_BINARY_OP_IS;
     725                 :            :     }
     726                 :      70579 :     emit_write_bytecode_byte(emit, -1, MP_BC_BINARY_OP_MULTI + op);
     727         [ +  + ]:      70579 :     if (invert) {
     728                 :       1370 :         emit_write_bytecode_byte(emit, 0, MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NOT);
     729                 :            :     }
     730                 :      70579 : }
     731                 :            : 
     732                 :      13356 : void mp_emit_bc_build(emit_t *emit, mp_uint_t n_args, int kind) {
     733                 :      13356 :     MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_TUPLE == MP_BC_BUILD_TUPLE);
     734                 :      13356 :     MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_LIST == MP_BC_BUILD_LIST);
     735                 :      13356 :     MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_MAP == MP_BC_BUILD_MAP);
     736                 :      13356 :     MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SET == MP_BC_BUILD_SET);
     737                 :      13356 :     MP_STATIC_ASSERT(MP_BC_BUILD_TUPLE + MP_EMIT_BUILD_SLICE == MP_BC_BUILD_SLICE);
     738         [ +  + ]:      13356 :     int stack_adj = kind == MP_EMIT_BUILD_MAP ? 1 : 1 - n_args;
     739                 :      13356 :     emit_write_bytecode_byte_uint(emit, stack_adj, MP_BC_BUILD_TUPLE + kind, n_args);
     740                 :      13356 : }
     741                 :            : 
     742                 :       3747 : void mp_emit_bc_store_map(emit_t *emit) {
     743                 :       3747 :     emit_write_bytecode_byte(emit, -2, MP_BC_STORE_MAP);
     744                 :       3747 : }
     745                 :            : 
     746                 :        351 : void mp_emit_bc_store_comp(emit_t *emit, scope_kind_t kind, mp_uint_t collection_stack_index) {
     747                 :        351 :     int t;
     748                 :        351 :     int n;
     749         [ +  + ]:        351 :     if (kind == SCOPE_LIST_COMP) {
     750                 :            :         n = 0;
     751                 :            :         t = 0;
     752         [ +  + ]:         53 :     } else if (!MICROPY_PY_BUILTINS_SET || kind == SCOPE_DICT_COMP) {
     753                 :            :         n = 1;
     754                 :            :         t = 1;
     755                 :          5 :     } else if (MICROPY_PY_BUILTINS_SET) {
     756                 :          5 :         n = 0;
     757                 :          5 :         t = 2;
     758                 :            :     }
     759                 :            :     // the lower 2 bits of the opcode argument indicate the collection type
     760                 :        351 :     emit_write_bytecode_byte_uint(emit, -1 - n, MP_BC_STORE_COMP, ((collection_stack_index + n) << 2) | t);
     761                 :        351 : }
     762                 :            : 
     763                 :        781 : void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
     764                 :        781 :     emit_write_bytecode_byte_uint(emit, -1 + n_args, MP_BC_UNPACK_SEQUENCE, n_args);
     765                 :        781 : }
     766                 :            : 
     767                 :        177 : void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
     768                 :        177 :     emit_write_bytecode_byte_uint(emit, -1 + n_left + n_right + 1, MP_BC_UNPACK_EX, n_left | (n_right << 8));
     769                 :        177 : }
     770                 :            : 
     771                 :      16293 : void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
     772         [ +  + ]:      16293 :     if (n_pos_defaults == 0 && n_kw_defaults == 0) {
     773                 :      15308 :         emit_write_bytecode_byte_child(emit, 1, MP_BC_MAKE_FUNCTION, scope->raw_code);
     774                 :            :     } else {
     775                 :        985 :         emit_write_bytecode_byte_child(emit, -1, MP_BC_MAKE_FUNCTION_DEFARGS, scope->raw_code);
     776                 :            :     }
     777                 :      16293 : }
     778                 :            : 
     779                 :        286 : void mp_emit_bc_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) {
     780         [ +  + ]:        286 :     if (n_pos_defaults == 0 && n_kw_defaults == 0) {
     781                 :        277 :         int stack_adj = -n_closed_over + 1;
     782                 :        277 :         emit_write_bytecode_byte_child(emit, stack_adj, MP_BC_MAKE_CLOSURE, scope->raw_code);
     783                 :        277 :         emit_write_bytecode_raw_byte(emit, n_closed_over);
     784                 :            :     } else {
     785         [ -  + ]:          9 :         assert(n_closed_over <= 255);
     786                 :          9 :         int stack_adj = -2 - (mp_int_t)n_closed_over + 1;
     787                 :          9 :         emit_write_bytecode_byte_child(emit, stack_adj, MP_BC_MAKE_CLOSURE_DEFARGS, scope->raw_code);
     788                 :          9 :         emit_write_bytecode_raw_byte(emit, n_closed_over);
     789                 :            :     }
     790                 :        286 : }
     791                 :            : 
     792                 :     328456 : static void emit_bc_call_function_method_helper(emit_t *emit, int stack_adj, mp_uint_t bytecode_base, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
     793         [ +  + ]:     328456 :     if (star_flags) {
     794                 :            :         // each positional arg is one object, each kwarg is two objects, the key
     795                 :            :         // and the value and one extra object for the star args bitmap.
     796                 :        804 :         stack_adj -= (int)n_positional + 2 * (int)n_keyword + 1;
     797                 :        804 :         emit_write_bytecode_byte_uint(emit, stack_adj, bytecode_base + 1, (n_keyword << 8) | n_positional);
     798                 :            :     } else {
     799                 :     327652 :         stack_adj -= (int)n_positional + 2 * (int)n_keyword;
     800                 :     327652 :         emit_write_bytecode_byte_uint(emit, stack_adj, bytecode_base, (n_keyword << 8) | n_positional);
     801                 :            :     }
     802                 :     328457 : }
     803                 :            : 
     804                 :     300003 : void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
     805                 :     300003 :     emit_bc_call_function_method_helper(emit, 0, MP_BC_CALL_FUNCTION, n_positional, n_keyword, star_flags);
     806                 :     300004 : }
     807                 :            : 
     808                 :      28453 : void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
     809                 :      28453 :     emit_bc_call_function_method_helper(emit, -1, MP_BC_CALL_METHOD, n_positional, n_keyword, star_flags);
     810                 :      28453 : }
     811                 :            : 
     812                 :      32802 : void mp_emit_bc_return_value(emit_t *emit) {
     813                 :      32802 :     emit_write_bytecode_byte(emit, -1, MP_BC_RETURN_VALUE);
     814                 :      32804 :     emit->suppress = true;
     815                 :      32804 : }
     816                 :            : 
     817                 :       5997 : void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
     818                 :       5997 :     MP_STATIC_ASSERT(MP_BC_RAISE_LAST + 1 == MP_BC_RAISE_OBJ);
     819                 :       5997 :     MP_STATIC_ASSERT(MP_BC_RAISE_LAST + 2 == MP_BC_RAISE_FROM);
     820         [ -  + ]:       5997 :     assert(n_args <= 2);
     821                 :       5997 :     emit_write_bytecode_byte(emit, -n_args, MP_BC_RAISE_LAST + n_args);
     822                 :       5997 :     emit->suppress = true;
     823                 :       5997 : }
     824                 :            : 
     825                 :       2594 : void mp_emit_bc_yield(emit_t *emit, int kind) {
     826                 :       2594 :     MP_STATIC_ASSERT(MP_BC_YIELD_VALUE + 1 == MP_BC_YIELD_FROM);
     827                 :       2594 :     emit_write_bytecode_byte(emit, -kind, MP_BC_YIELD_VALUE + kind);
     828                 :       2594 :     emit->scope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
     829                 :       2594 : }
     830                 :            : 
     831                 :      45121 : void mp_emit_bc_start_except_handler(emit_t *emit) {
     832                 :      45121 :     mp_emit_bc_adjust_stack_size(emit, 4); // stack adjust for the exception instance, +3 for possible UNWIND_JUMP state
     833                 :      45121 : }
     834                 :            : 
     835                 :      45117 : void mp_emit_bc_end_except_handler(emit_t *emit) {
     836                 :      45117 :     mp_emit_bc_adjust_stack_size(emit, -3); // stack adjust
     837                 :      45117 : }
     838                 :            : 
     839                 :            : #if MICROPY_EMIT_NATIVE
     840                 :            : const emit_method_table_t emit_bc_method_table = {
     841                 :            :     #if MICROPY_DYNAMIC_COMPILER
     842                 :            :     NULL,
     843                 :            :     NULL,
     844                 :            :     #endif
     845                 :            : 
     846                 :            :     mp_emit_bc_start_pass,
     847                 :            :     mp_emit_bc_end_pass,
     848                 :            :     mp_emit_bc_adjust_stack_size,
     849                 :            :     mp_emit_bc_set_source_line,
     850                 :            : 
     851                 :            :     {
     852                 :            :         mp_emit_bc_load_local,
     853                 :            :         mp_emit_bc_load_global,
     854                 :            :     },
     855                 :            :     {
     856                 :            :         mp_emit_bc_store_local,
     857                 :            :         mp_emit_bc_store_global,
     858                 :            :     },
     859                 :            :     {
     860                 :            :         mp_emit_bc_delete_local,
     861                 :            :         mp_emit_bc_delete_global,
     862                 :            :     },
     863                 :            : 
     864                 :            :     mp_emit_bc_label_assign,
     865                 :            :     mp_emit_bc_import,
     866                 :            :     mp_emit_bc_load_const_tok,
     867                 :            :     mp_emit_bc_load_const_small_int,
     868                 :            :     mp_emit_bc_load_const_str,
     869                 :            :     mp_emit_bc_load_const_obj,
     870                 :            :     mp_emit_bc_load_null,
     871                 :            :     mp_emit_bc_load_method,
     872                 :            :     mp_emit_bc_load_build_class,
     873                 :            :     mp_emit_bc_subscr,
     874                 :            :     mp_emit_bc_attr,
     875                 :            :     mp_emit_bc_dup_top,
     876                 :            :     mp_emit_bc_dup_top_two,
     877                 :            :     mp_emit_bc_pop_top,
     878                 :            :     mp_emit_bc_rot_two,
     879                 :            :     mp_emit_bc_rot_three,
     880                 :            :     mp_emit_bc_jump,
     881                 :            :     mp_emit_bc_pop_jump_if,
     882                 :            :     mp_emit_bc_jump_if_or_pop,
     883                 :            :     mp_emit_bc_unwind_jump,
     884                 :            :     mp_emit_bc_setup_block,
     885                 :            :     mp_emit_bc_with_cleanup,
     886                 :            :     #if MICROPY_PY_ASYNC_AWAIT
     887                 :            :     mp_emit_bc_async_with_setup_finally,
     888                 :            :     #endif
     889                 :            :     mp_emit_bc_end_finally,
     890                 :            :     mp_emit_bc_get_iter,
     891                 :            :     mp_emit_bc_for_iter,
     892                 :            :     mp_emit_bc_for_iter_end,
     893                 :            :     mp_emit_bc_pop_except_jump,
     894                 :            :     mp_emit_bc_unary_op,
     895                 :            :     mp_emit_bc_binary_op,
     896                 :            :     mp_emit_bc_build,
     897                 :            :     mp_emit_bc_store_map,
     898                 :            :     mp_emit_bc_store_comp,
     899                 :            :     mp_emit_bc_unpack_sequence,
     900                 :            :     mp_emit_bc_unpack_ex,
     901                 :            :     mp_emit_bc_make_function,
     902                 :            :     mp_emit_bc_make_closure,
     903                 :            :     mp_emit_bc_call_function,
     904                 :            :     mp_emit_bc_call_method,
     905                 :            :     mp_emit_bc_return_value,
     906                 :            :     mp_emit_bc_raise_varargs,
     907                 :            :     mp_emit_bc_yield,
     908                 :            : 
     909                 :            :     mp_emit_bc_start_except_handler,
     910                 :            :     mp_emit_bc_end_except_handler,
     911                 :            : };
     912                 :            : #else
     913                 :            : const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops = {
     914                 :            :     mp_emit_bc_load_local,
     915                 :            :     mp_emit_bc_load_global,
     916                 :            : };
     917                 :            : 
     918                 :            : const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops = {
     919                 :            :     mp_emit_bc_store_local,
     920                 :            :     mp_emit_bc_store_global,
     921                 :            : };
     922                 :            : 
     923                 :            : const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops = {
     924                 :            :     mp_emit_bc_delete_local,
     925                 :            :     mp_emit_bc_delete_global,
     926                 :            : };
     927                 :            : #endif
     928                 :            : 
     929                 :            : #endif // MICROPY_ENABLE_COMPILER

Generated by: LCOV version 1.15-5-g462f71d