LCOV - code coverage report
Current view: top level - py - asmbase.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.24.0-7-g548babf8a.info Lines: 41 45 91.1 %
Date: 2024-10-30 09:06:48 Functions: 6 7 85.7 %
Branches: 18 24 75.0 %

           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) 2016 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 <assert.h>
      28                 :            : #include <string.h>
      29                 :            : 
      30                 :            : #include "py/obj.h"
      31                 :            : #include "py/misc.h"
      32                 :            : #include "py/asmbase.h"
      33                 :            : #include "py/persistentcode.h"
      34                 :            : 
      35                 :            : #if MICROPY_EMIT_MACHINE_CODE
      36                 :            : 
      37                 :       1481 : void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels) {
      38                 :       1481 :     as->max_num_labels = max_num_labels;
      39                 :       1481 :     as->label_offsets = m_new(size_t, max_num_labels);
      40                 :       1481 : }
      41                 :            : 
      42                 :       1465 : void mp_asm_base_deinit(mp_asm_base_t *as, bool free_code) {
      43         [ -  + ]:       1465 :     if (free_code) {
      44                 :          0 :         MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
      45                 :            :     }
      46                 :       1465 :     m_del(size_t, as->label_offsets, as->max_num_labels);
      47                 :       1465 : }
      48                 :            : 
      49                 :      13661 : void mp_asm_base_start_pass(mp_asm_base_t *as, int pass) {
      50         [ +  + ]:      13661 :     if (pass < MP_ASM_PASS_EMIT) {
      51                 :            :         // Reset labels so we can detect backwards jumps (and verify unique assignment)
      52                 :       9138 :         memset(as->label_offsets, -1, as->max_num_labels * sizeof(size_t));
      53                 :            :     } else {
      54                 :            :         // allocating executable RAM is platform specific
      55                 :       4523 :         MP_PLAT_ALLOC_EXEC(as->code_offset, (void **)&as->code_base, &as->code_size);
      56         [ -  + ]:       4523 :         assert(as->code_base != NULL);
      57                 :            :     }
      58                 :      13661 :     as->pass = pass;
      59                 :      13661 :     as->suppress = false;
      60                 :      13661 :     as->code_offset = 0;
      61                 :      13661 : }
      62                 :            : 
      63                 :            : // all functions must go through this one to emit bytes
      64                 :            : // if as->pass < MP_ASM_PASS_EMIT, then this function just counts the number
      65                 :            : // of bytes needed and returns NULL, and callers should not store any data
      66                 :            : // It also returns NULL if generated code should be suppressed at this point.
      67                 :    5332873 : uint8_t *mp_asm_base_get_cur_to_write_bytes(void *as_in, size_t num_bytes_to_write) {
      68                 :    5332873 :     mp_asm_base_t *as = as_in;
      69                 :    5332873 :     uint8_t *c = NULL;
      70         [ +  + ]:    5332873 :     if (as->suppress) {
      71                 :            :         return c;
      72                 :            :     }
      73         [ +  + ]:    5287012 :     if (as->pass == MP_ASM_PASS_EMIT) {
      74         [ -  + ]:    1737215 :         assert(as->code_offset + num_bytes_to_write <= as->code_size);
      75                 :    1737215 :         c = as->code_base + as->code_offset;
      76                 :            :     }
      77                 :    5287012 :     as->code_offset += num_bytes_to_write;
      78                 :    5287012 :     return c;
      79                 :            : }
      80                 :            : 
      81                 :      79624 : void mp_asm_base_label_assign(mp_asm_base_t *as, size_t label) {
      82         [ -  + ]:      79624 :     assert(label < as->max_num_labels);
      83                 :            : 
      84                 :            :     // Assigning a label ends any dead-code region, and all following machine
      85                 :            :     // code should be emitted (until another mp_asm_base_suppress_code() call).
      86                 :      79624 :     as->suppress = false;
      87                 :            : 
      88         [ +  + ]:      79624 :     if (as->pass < MP_ASM_PASS_EMIT) {
      89                 :            :         // assign label offset
      90         [ -  + ]:      53184 :         assert(as->label_offsets[label] == (size_t)-1);
      91                 :      53184 :         as->label_offsets[label] = as->code_offset;
      92                 :            :     } else {
      93                 :            :         // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
      94         [ -  + ]:      26440 :         assert(as->label_offsets[label] == as->code_offset);
      95                 :            :         #if MICROPY_DYNAMIC_COMPILER && MICROPY_EMIT_NATIVE_DEBUG
      96                 :            :         if (mp_dynamic_compiler.native_arch == MP_NATIVE_ARCH_DEBUG) {
      97                 :            :             mp_printf(MICROPY_EMIT_NATIVE_DEBUG_PRINTER, "label(label_%u)\n", (unsigned int)label);
      98                 :            :         }
      99                 :            :         #endif
     100                 :            :     }
     101                 :      79624 : }
     102                 :            : 
     103                 :            : // align must be a multiple of 2
     104                 :          0 : void mp_asm_base_align(mp_asm_base_t *as, unsigned int align) {
     105                 :          0 :     as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
     106                 :          0 : }
     107                 :            : 
     108                 :            : // this function assumes a little endian machine
     109                 :     111665 : void mp_asm_base_data(mp_asm_base_t *as, unsigned int bytesize, uintptr_t val) {
     110                 :     111665 :     uint8_t *c = mp_asm_base_get_cur_to_write_bytes(as, bytesize);
     111         [ +  + ]:     111665 :     if (c != NULL) {
     112         [ +  + ]:      61032 :         for (unsigned int i = 0; i < bytesize; i++) {
     113                 :      46798 :             *c++ = val;
     114                 :      46798 :             val >>= 8;
     115                 :            :         }
     116                 :            :     }
     117                 :     111665 : }
     118                 :            : 
     119                 :            : #endif // MICROPY_EMIT_MACHINE_CODE

Generated by: LCOV version 1.15-5-g462f71d