LCOV - code coverage report
Current view: top level - py - asmbase.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.19.1-724-gfb7d21153.info Lines: 41 45 91.1 %
Date: 2022-12-01 09:37:31 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                 :            : 
      34                 :            : #if MICROPY_EMIT_MACHINE_CODE
      35                 :            : 
      36                 :       1367 : void mp_asm_base_init(mp_asm_base_t *as, size_t max_num_labels) {
      37                 :       1367 :     as->max_num_labels = max_num_labels;
      38                 :       1367 :     as->label_offsets = m_new(size_t, max_num_labels);
      39                 :       1367 : }
      40                 :            : 
      41                 :       1351 : void mp_asm_base_deinit(mp_asm_base_t *as, bool free_code) {
      42         [ -  + ]:       1351 :     if (free_code) {
      43                 :          0 :         MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
      44                 :            :     }
      45                 :       1351 :     m_del(size_t, as->label_offsets, as->max_num_labels);
      46                 :       1351 : }
      47                 :            : 
      48                 :      11742 : void mp_asm_base_start_pass(mp_asm_base_t *as, int pass) {
      49         [ +  + ]:      11742 :     if (pass < MP_ASM_PASS_EMIT) {
      50                 :            :         // Reset labels so we can detect backwards jumps (and verify unique assignment)
      51                 :       7860 :         memset(as->label_offsets, -1, as->max_num_labels * sizeof(size_t));
      52                 :            :     } else {
      53                 :            :         // allocating executable RAM is platform specific
      54                 :       3882 :         MP_PLAT_ALLOC_EXEC(as->code_offset, (void **)&as->code_base, &as->code_size);
      55         [ -  + ]:       3882 :         assert(as->code_base != NULL);
      56                 :            :     }
      57                 :      11742 :     as->pass = pass;
      58                 :      11742 :     as->suppress = false;
      59                 :      11742 :     as->code_offset = 0;
      60                 :      11742 : }
      61                 :            : 
      62                 :            : // all functions must go through this one to emit bytes
      63                 :            : // if as->pass < MP_ASM_PASS_EMIT, then this function just counts the number
      64                 :            : // of bytes needed and returns NULL, and callers should not store any data
      65                 :            : // It also returns NULL if generated code should be suppressed at this point.
      66                 :    4575464 : uint8_t *mp_asm_base_get_cur_to_write_bytes(void *as_in, size_t num_bytes_to_write) {
      67                 :    4575464 :     mp_asm_base_t *as = as_in;
      68                 :    4575464 :     uint8_t *c = NULL;
      69         [ +  + ]:    4575464 :     if (as->suppress) {
      70                 :            :         return c;
      71                 :            :     }
      72         [ +  + ]:    4538933 :     if (as->pass == MP_ASM_PASS_EMIT) {
      73         [ -  + ]:    1490483 :         assert(as->code_offset + num_bytes_to_write <= as->code_size);
      74                 :    1490483 :         c = as->code_base + as->code_offset;
      75                 :            :     }
      76                 :    4538933 :     as->code_offset += num_bytes_to_write;
      77                 :    4538933 :     return c;
      78                 :            : }
      79                 :            : 
      80                 :      67699 : void mp_asm_base_label_assign(mp_asm_base_t *as, size_t label) {
      81         [ -  + ]:      67699 :     assert(label < as->max_num_labels);
      82                 :            : 
      83                 :            :     // Assiging a label ends any dead-code region, and all following machine
      84                 :            :     // code should be emitted (until another mp_asm_base_suppress_code() call).
      85                 :      67699 :     as->suppress = false;
      86                 :            : 
      87         [ +  + ]:      67699 :     if (as->pass < MP_ASM_PASS_EMIT) {
      88                 :            :         // assign label offset
      89         [ -  + ]:      45238 :         assert(as->label_offsets[label] == (size_t)-1);
      90                 :      45238 :         as->label_offsets[label] = as->code_offset;
      91                 :            :     } else {
      92                 :            :         // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
      93         [ -  + ]:      22461 :         assert(as->label_offsets[label] == as->code_offset);
      94                 :            :     }
      95                 :      67699 : }
      96                 :            : 
      97                 :            : // align must be a multiple of 2
      98                 :          0 : void mp_asm_base_align(mp_asm_base_t *as, unsigned int align) {
      99                 :          0 :     as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
     100                 :          0 : }
     101                 :            : 
     102                 :            : // this function assumes a little endian machine
     103                 :      86976 : void mp_asm_base_data(mp_asm_base_t *as, unsigned int bytesize, uintptr_t val) {
     104                 :      86976 :     uint8_t *c = mp_asm_base_get_cur_to_write_bytes(as, bytesize);
     105         [ +  + ]:      86976 :     if (c != NULL) {
     106         [ +  + ]:      20336 :         for (unsigned int i = 0; i < bytesize; i++) {
     107                 :      11792 :             *c++ = val;
     108                 :      11792 :             val >>= 8;
     109                 :            :         }
     110                 :            :     }
     111                 :      86976 : }
     112                 :            : 
     113                 :            : #endif // MICROPY_EMIT_MACHINE_CODE

Generated by: LCOV version 1.15-5-g462f71d