LCOV - code coverage report
Current view: top level - py - asmx64.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-342-g49af8cad4.info Lines: 267 276 96.7 %
Date: 2024-04-25 10:54:52 Functions: 51 52 98.1 %
Branches: 60 76 78.9 %

           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                 :            : #include <stdint.h>
      28                 :            : #include <stdio.h>
      29                 :            : #include <assert.h>
      30                 :            : #include <string.h>
      31                 :            : 
      32                 :            : #include "py/mpconfig.h"
      33                 :            : 
      34                 :            : // wrapper around everything in this file
      35                 :            : #if MICROPY_EMIT_X64
      36                 :            : 
      37                 :            : #include "py/asmx64.h"
      38                 :            : 
      39                 :            : /* all offsets are measured in multiples of 8 bytes */
      40                 :            : #define WORD_SIZE                (8)
      41                 :            : 
      42                 :            : #define OPCODE_NOP               (0x90)
      43                 :            : #define OPCODE_PUSH_R64          (0x50) /* +rq */
      44                 :            : #define OPCODE_PUSH_I64          (0x68)
      45                 :            : #define OPCODE_PUSH_M64          (0xff) /* /6 */
      46                 :            : #define OPCODE_POP_R64           (0x58) /* +rq */
      47                 :            : #define OPCODE_RET               (0xc3)
      48                 :            : #define OPCODE_MOV_I8_TO_R8      (0xb0) /* +rb */
      49                 :            : #define OPCODE_MOV_I64_TO_R64    (0xb8) /* +rq */
      50                 :            : #define OPCODE_MOV_I32_TO_RM32   (0xc7)
      51                 :            : #define OPCODE_MOV_R8_TO_RM8     (0x88) /* /r */
      52                 :            : #define OPCODE_MOV_R64_TO_RM64   (0x89) /* /r */
      53                 :            : #define OPCODE_MOV_RM64_TO_R64   (0x8b) /* /r */
      54                 :            : #define OPCODE_MOVZX_RM8_TO_R64  (0xb6) /* 0x0f 0xb6/r */
      55                 :            : #define OPCODE_MOVZX_RM16_TO_R64 (0xb7) /* 0x0f 0xb7/r */
      56                 :            : #define OPCODE_LEA_MEM_TO_R64    (0x8d) /* /r */
      57                 :            : #define OPCODE_NOT_RM64          (0xf7) /* /2 */
      58                 :            : #define OPCODE_NEG_RM64          (0xf7) /* /3 */
      59                 :            : #define OPCODE_AND_R64_TO_RM64   (0x21) /* /r */
      60                 :            : #define OPCODE_OR_R64_TO_RM64    (0x09) /* /r */
      61                 :            : #define OPCODE_XOR_R64_TO_RM64   (0x31) /* /r */
      62                 :            : #define OPCODE_ADD_R64_TO_RM64   (0x01) /* /r */
      63                 :            : #define OPCODE_ADD_I32_TO_RM32   (0x81) /* /0 */
      64                 :            : #define OPCODE_ADD_I8_TO_RM32    (0x83) /* /0 */
      65                 :            : #define OPCODE_SUB_R64_FROM_RM64 (0x29)
      66                 :            : #define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
      67                 :            : #define OPCODE_SUB_I8_FROM_RM64  (0x83) /* /5 */
      68                 :            : // #define OPCODE_SHL_RM32_BY_I8    (0xc1) /* /4 */
      69                 :            : // #define OPCODE_SHR_RM32_BY_I8    (0xc1) /* /5 */
      70                 :            : // #define OPCODE_SAR_RM32_BY_I8    (0xc1) /* /7 */
      71                 :            : #define OPCODE_SHL_RM64_CL       (0xd3) /* /4 */
      72                 :            : #define OPCODE_SHR_RM64_CL       (0xd3) /* /5 */
      73                 :            : #define OPCODE_SAR_RM64_CL       (0xd3) /* /7 */
      74                 :            : // #define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
      75                 :            : // #define OPCODE_CMP_I8_WITH_RM32  (0x83) /* /7 */
      76                 :            : #define OPCODE_CMP_R64_WITH_RM64 (0x39) /* /r */
      77                 :            : // #define OPCODE_CMP_RM32_WITH_R32 (0x3b)
      78                 :            : #define OPCODE_TEST_R8_WITH_RM8  (0x84) /* /r */
      79                 :            : #define OPCODE_TEST_R64_WITH_RM64 (0x85) /* /r */
      80                 :            : #define OPCODE_JMP_REL8          (0xeb)
      81                 :            : #define OPCODE_JMP_REL32         (0xe9)
      82                 :            : #define OPCODE_JMP_RM64          (0xff) /* /4 */
      83                 :            : #define OPCODE_JCC_REL8          (0x70) /* | jcc type */
      84                 :            : #define OPCODE_JCC_REL32_A       (0x0f)
      85                 :            : #define OPCODE_JCC_REL32_B       (0x80) /* | jcc type */
      86                 :            : #define OPCODE_SETCC_RM8_A       (0x0f)
      87                 :            : #define OPCODE_SETCC_RM8_B       (0x90) /* | jcc type, /0 */
      88                 :            : #define OPCODE_CALL_REL32        (0xe8)
      89                 :            : #define OPCODE_CALL_RM32         (0xff) /* /2 */
      90                 :            : #define OPCODE_LEAVE             (0xc9)
      91                 :            : 
      92                 :            : #define MODRM_R64(x)    (((x) & 0x7) << 3)
      93                 :            : #define MODRM_RM_DISP0  (0x00)
      94                 :            : #define MODRM_RM_DISP8  (0x40)
      95                 :            : #define MODRM_RM_DISP32 (0x80)
      96                 :            : #define MODRM_RM_REG    (0xc0)
      97                 :            : #define MODRM_RM_R64(x) ((x) & 0x7)
      98                 :            : 
      99                 :            : #define OP_SIZE_PREFIX (0x66)
     100                 :            : 
     101                 :            : #define REX_PREFIX  (0x40)
     102                 :            : #define REX_W       (0x08)  // width
     103                 :            : #define REX_R       (0x04)  // register
     104                 :            : #define REX_X       (0x02)  // index
     105                 :            : #define REX_B       (0x01)  // base
     106                 :            : #define REX_W_FROM_R64(r64) ((r64) >> 0 & 0x08)
     107                 :            : #define REX_R_FROM_R64(r64) ((r64) >> 1 & 0x04)
     108                 :            : #define REX_X_FROM_R64(r64) ((r64) >> 2 & 0x02)
     109                 :            : #define REX_B_FROM_R64(r64) ((r64) >> 3 & 0x01)
     110                 :            : 
     111                 :            : #define IMM32_L0(x) ((x) & 0xff)
     112                 :            : #define IMM32_L1(x) (((x) >> 8) & 0xff)
     113                 :            : #define IMM32_L2(x) (((x) >> 16) & 0xff)
     114                 :            : #define IMM32_L3(x) (((x) >> 24) & 0xff)
     115                 :            : #define IMM64_L4(x) (((x) >> 32) & 0xff)
     116                 :            : #define IMM64_L5(x) (((x) >> 40) & 0xff)
     117                 :            : #define IMM64_L6(x) (((x) >> 48) & 0xff)
     118                 :            : #define IMM64_L7(x) (((x) >> 56) & 0xff)
     119                 :            : 
     120                 :            : #define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
     121                 :            : #define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
     122                 :            : #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
     123                 :            : 
     124                 :    4660478 : static inline byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int n) {
     125                 :    4660478 :     return mp_asm_base_get_cur_to_write_bytes(&as->base, n);
     126                 :            : }
     127                 :            : 
     128                 :    2301446 : static void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
     129                 :    2301446 :     byte *c = asm_x64_get_cur_to_write_bytes(as, 1);
     130         [ +  + ]:    2301446 :     if (c != NULL) {
     131                 :     758127 :         c[0] = b1;
     132                 :            :     }
     133                 :    2301446 : }
     134                 :            : 
     135                 :    1349968 : static void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
     136                 :    1349968 :     byte *c = asm_x64_get_cur_to_write_bytes(as, 2);
     137         [ +  + ]:    1349968 :     if (c != NULL) {
     138                 :     446809 :         c[0] = b1;
     139                 :     446809 :         c[1] = b2;
     140                 :            :     }
     141                 :    1349968 : }
     142                 :            : 
     143                 :     178164 : static void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
     144                 :     178164 :     byte *c = asm_x64_get_cur_to_write_bytes(as, 3);
     145         [ +  + ]:     178164 :     if (c != NULL) {
     146                 :      58971 :         c[0] = b1;
     147                 :      58971 :         c[1] = b2;
     148                 :      58971 :         c[2] = b3;
     149                 :            :     }
     150                 :     178164 : }
     151                 :            : 
     152                 :     828962 : static void asm_x64_write_word32(asm_x64_t *as, int w32) {
     153                 :     828962 :     byte *c = asm_x64_get_cur_to_write_bytes(as, 4);
     154         [ +  + ]:     828962 :     if (c != NULL) {
     155                 :     273936 :         c[0] = IMM32_L0(w32);
     156                 :     273936 :         c[1] = IMM32_L1(w32);
     157                 :     273936 :         c[2] = IMM32_L2(w32);
     158                 :     273936 :         c[3] = IMM32_L3(w32);
     159                 :            :     }
     160                 :     828962 : }
     161                 :            : 
     162                 :       1938 : static void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
     163                 :       1938 :     byte *c = asm_x64_get_cur_to_write_bytes(as, 8);
     164         [ +  + ]:       1938 :     if (c != NULL) {
     165                 :        646 :         c[0] = IMM32_L0(w64);
     166                 :        646 :         c[1] = IMM32_L1(w64);
     167                 :        646 :         c[2] = IMM32_L2(w64);
     168                 :        646 :         c[3] = IMM32_L3(w64);
     169                 :        646 :         c[4] = IMM64_L4(w64);
     170                 :        646 :         c[5] = IMM64_L5(w64);
     171                 :        646 :         c[6] = IMM64_L6(w64);
     172                 :        646 :         c[7] = IMM64_L7(w64);
     173                 :            :     }
     174                 :       1938 : }
     175                 :            : 
     176                 :            : /* unused
     177                 :            : static void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
     178                 :            :     byte* c;
     179                 :            :     assert(offset + 4 <= as->code_size);
     180                 :            :     c = as->code_base + offset;
     181                 :            :     c[0] = IMM32_L0(w32);
     182                 :            :     c[1] = IMM32_L1(w32);
     183                 :            :     c[2] = IMM32_L2(w32);
     184                 :            :     c[3] = IMM32_L3(w32);
     185                 :            : }
     186                 :            : */
     187                 :            : 
     188                 :     925283 : static void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
     189                 :     925283 :     uint8_t rm_disp;
     190   [ +  +  +  + ]:     925283 :     if (disp_offset == 0 && (disp_r64 & 7) != ASM_X64_REG_RBP) {
     191                 :            :         rm_disp = MODRM_RM_DISP0;
     192   [ +  +  +  - ]:     912880 :     } else if (SIGNED_FIT8(disp_offset)) {
     193                 :            :         rm_disp = MODRM_RM_DISP8;
     194                 :            :     } else {
     195                 :     419070 :         rm_disp = MODRM_RM_DISP32;
     196                 :            :     }
     197                 :     925283 :     asm_x64_write_byte_1(as, MODRM_R64(r64) | rm_disp | MODRM_RM_R64(disp_r64));
     198         [ +  + ]:     925283 :     if ((disp_r64 & 7) == ASM_X64_REG_RSP) {
     199                 :            :         // Special case for rsp and r12, they need a SIB byte
     200                 :     456991 :         asm_x64_write_byte_1(as, 0x24);
     201                 :            :     }
     202         [ +  + ]:     925283 :     if (rm_disp == MODRM_RM_DISP8) {
     203                 :     493810 :         asm_x64_write_byte_1(as, IMM32_L0(disp_offset));
     204         [ +  + ]:     431473 :     } else if (rm_disp == MODRM_RM_DISP32) {
     205                 :     419070 :         asm_x64_write_word32(as, disp_offset);
     206                 :            :     }
     207                 :     925283 : }
     208                 :            : 
     209                 :     142659 : static void asm_x64_generic_r64_r64(asm_x64_t *as, int dest_r64, int src_r64, int op) {
     210                 :     142659 :     asm_x64_write_byte_3(as, REX_PREFIX | REX_W | REX_R_FROM_R64(src_r64) | REX_B_FROM_R64(dest_r64), op, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
     211                 :     142659 : }
     212                 :            : 
     213                 :          0 : void asm_x64_nop(asm_x64_t *as) {
     214                 :          0 :     asm_x64_write_byte_1(as, OPCODE_NOP);
     215                 :          0 : }
     216                 :            : 
     217                 :      49292 : void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
     218         [ +  + ]:      49292 :     if (src_r64 < 8) {
     219                 :      24646 :         asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
     220                 :            :     } else {
     221                 :      24646 :         asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_PUSH_R64 | (src_r64 & 7));
     222                 :            :     }
     223                 :      49292 : }
     224                 :            : 
     225                 :            : /*
     226                 :            : void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
     227                 :            :     asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
     228                 :            :     asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
     229                 :            : }
     230                 :            : */
     231                 :            : 
     232                 :            : /*
     233                 :            : void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
     234                 :            :     assert(src_r64 < 8);
     235                 :            :     asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
     236                 :            :     asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
     237                 :            : }
     238                 :            : */
     239                 :            : 
     240                 :      52260 : void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
     241         [ +  + ]:      52260 :     if (dest_r64 < 8) {
     242                 :      26130 :         asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
     243                 :            :     } else {
     244                 :      26130 :         asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_POP_R64 | (dest_r64 & 7));
     245                 :            :     }
     246                 :      52260 : }
     247                 :            : 
     248                 :      13065 : static void asm_x64_ret(asm_x64_t *as) {
     249                 :      13065 :     asm_x64_write_byte_1(as, OPCODE_RET);
     250                 :      13065 : }
     251                 :            : 
     252                 :     108890 : void asm_x64_mov_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
     253                 :     108890 :     asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_MOV_R64_TO_RM64);
     254                 :     108890 : }
     255                 :            : 
     256                 :         60 : void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
     257         [ +  - ]:         60 :     if (src_r64 < 8 && dest_r64 < 8) {
     258                 :         60 :         asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
     259                 :            :     } else {
     260                 :          0 :         asm_x64_write_byte_2(as, REX_PREFIX | REX_R_FROM_R64(src_r64) | REX_B_FROM_R64(dest_r64), OPCODE_MOV_R8_TO_RM8);
     261                 :            :     }
     262                 :         60 :     asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
     263                 :         60 : }
     264                 :            : 
     265                 :         24 : void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
     266         [ +  - ]:         24 :     if (src_r64 < 8 && dest_r64 < 8) {
     267                 :         24 :         asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64);
     268                 :            :     } else {
     269                 :          0 :         asm_x64_write_byte_3(as, OP_SIZE_PREFIX, REX_PREFIX | REX_R_FROM_R64(src_r64) | REX_B_FROM_R64(dest_r64), OPCODE_MOV_R64_TO_RM64);
     270                 :            :     }
     271                 :         24 :     asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
     272                 :         24 : }
     273                 :            : 
     274                 :         32 : void asm_x64_mov_r32_to_mem32(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
     275         [ +  - ]:         32 :     if (src_r64 < 8 && dest_r64 < 8) {
     276                 :         32 :         asm_x64_write_byte_1(as, OPCODE_MOV_R64_TO_RM64);
     277                 :            :     } else {
     278                 :          0 :         asm_x64_write_byte_2(as, REX_PREFIX | REX_R_FROM_R64(src_r64) | REX_B_FROM_R64(dest_r64), OPCODE_MOV_R64_TO_RM64);
     279                 :            :     }
     280                 :         32 :     asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
     281                 :         32 : }
     282                 :            : 
     283                 :     231489 : void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
     284                 :            :     // use REX prefix for 64 bit operation
     285                 :     231489 :     asm_x64_write_byte_2(as, REX_PREFIX | REX_W | REX_R_FROM_R64(src_r64) | REX_B_FROM_R64(dest_r64), OPCODE_MOV_R64_TO_RM64);
     286                 :     231489 :     asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
     287                 :     231489 : }
     288                 :            : 
     289                 :         90 : void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
     290         [ +  - ]:         90 :     if (src_r64 < 8 && dest_r64 < 8) {
     291                 :         90 :         asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R64);
     292                 :            :     } else {
     293                 :          0 :         asm_x64_write_byte_3(as, REX_PREFIX | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), 0x0f, OPCODE_MOVZX_RM8_TO_R64);
     294                 :            :     }
     295                 :         90 :     asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
     296                 :         90 : }
     297                 :            : 
     298                 :         24 : void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
     299         [ +  - ]:         24 :     if (src_r64 < 8 && dest_r64 < 8) {
     300                 :         24 :         asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R64);
     301                 :            :     } else {
     302                 :          0 :         asm_x64_write_byte_3(as, REX_PREFIX | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), 0x0f, OPCODE_MOVZX_RM16_TO_R64);
     303                 :            :     }
     304                 :         24 :     asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
     305                 :         24 : }
     306                 :            : 
     307                 :         24 : void asm_x64_mov_mem32_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
     308         [ +  - ]:         24 :     if (src_r64 < 8 && dest_r64 < 8) {
     309                 :         24 :         asm_x64_write_byte_1(as, OPCODE_MOV_RM64_TO_R64);
     310                 :            :     } else {
     311                 :          0 :         asm_x64_write_byte_2(as, REX_PREFIX | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), OPCODE_MOV_RM64_TO_R64);
     312                 :            :     }
     313                 :         24 :     asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
     314                 :         24 : }
     315                 :            : 
     316                 :     612688 : void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
     317                 :            :     // use REX prefix for 64 bit operation
     318                 :     612688 :     asm_x64_write_byte_2(as, REX_PREFIX | REX_W | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), OPCODE_MOV_RM64_TO_R64);
     319                 :     612688 :     asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
     320                 :     612688 : }
     321                 :            : 
     322                 :      80852 : static void asm_x64_lea_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
     323                 :            :     // use REX prefix for 64 bit operation
     324                 :      80852 :     asm_x64_write_byte_2(as, REX_PREFIX | REX_W | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64), OPCODE_LEA_MEM_TO_R64);
     325                 :      80852 :     asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
     326                 :      80852 : }
     327                 :            : 
     328                 :            : /*
     329                 :            : void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
     330                 :            :     assert(dest_r64 < 8);
     331                 :            :     asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
     332                 :            : }
     333                 :            : */
     334                 :            : 
     335                 :     321412 : size_t asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
     336                 :            :     // cpu defaults to i32 to r64, with zero extension
     337         [ +  + ]:     321412 :     if (dest_r64 < 8) {
     338                 :     321349 :         asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
     339                 :            :     } else {
     340                 :         63 :         asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7));
     341                 :            :     }
     342                 :     321412 :     size_t loc = mp_asm_base_get_code_pos(&as->base);
     343                 :     321412 :     asm_x64_write_word32(as, src_i32);
     344                 :     321412 :     return loc;
     345                 :            : }
     346                 :            : 
     347                 :       1938 : void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
     348                 :            :     // cpu defaults to i32 to r64
     349                 :            :     // to mov i64 to r64 need to use REX prefix
     350                 :       1938 :     asm_x64_write_byte_2(as,
     351                 :            :         REX_PREFIX | REX_W | (dest_r64 < 8 ? 0 : REX_B),
     352         [ +  - ]:       1938 :         OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7));
     353                 :       1938 :     asm_x64_write_word64(as, src_i64);
     354                 :       1938 : }
     355                 :            : 
     356                 :     323350 : void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
     357                 :            :     // TODO use movzx, movsx if possible
     358         [ +  + ]:     323350 :     if (UNSIGNED_FIT32(src_i64)) {
     359                 :            :         // 5 bytes
     360                 :     321412 :         asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
     361                 :            :     } else {
     362                 :            :         // 10 bytes
     363                 :       1938 :         asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
     364                 :            :     }
     365                 :     323350 : }
     366                 :            : 
     367                 :          6 : void asm_x64_not_r64(asm_x64_t *as, int dest_r64) {
     368                 :          6 :     asm_x64_generic_r64_r64(as, dest_r64, 2, OPCODE_NOT_RM64);
     369                 :          6 : }
     370                 :            : 
     371                 :          6 : void asm_x64_neg_r64(asm_x64_t *as, int dest_r64) {
     372                 :          6 :     asm_x64_generic_r64_r64(as, dest_r64, 3, OPCODE_NEG_RM64);
     373                 :          6 : }
     374                 :            : 
     375                 :         60 : void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
     376                 :         60 :     asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_AND_R64_TO_RM64);
     377                 :         60 : }
     378                 :            : 
     379                 :         60 : void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
     380                 :         60 :     asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_OR_R64_TO_RM64);
     381                 :         60 : }
     382                 :            : 
     383                 :      10261 : void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
     384                 :      10261 :     asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_XOR_R64_TO_RM64);
     385                 :      10261 : }
     386                 :            : 
     387                 :         48 : void asm_x64_shl_r64_cl(asm_x64_t *as, int dest_r64) {
     388                 :         48 :     asm_x64_generic_r64_r64(as, dest_r64, 4, OPCODE_SHL_RM64_CL);
     389                 :         48 : }
     390                 :            : 
     391                 :          6 : void asm_x64_shr_r64_cl(asm_x64_t *as, int dest_r64) {
     392                 :          6 :     asm_x64_generic_r64_r64(as, dest_r64, 5, OPCODE_SHR_RM64_CL);
     393                 :          6 : }
     394                 :            : 
     395                 :         42 : void asm_x64_sar_r64_cl(asm_x64_t *as, int dest_r64) {
     396                 :         42 :     asm_x64_generic_r64_r64(as, dest_r64, 7, OPCODE_SAR_RM64_CL);
     397                 :         42 : }
     398                 :            : 
     399                 :       6595 : void asm_x64_add_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
     400                 :       6595 :     asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_ADD_R64_TO_RM64);
     401                 :       6595 : }
     402                 :            : 
     403                 :         24 : void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
     404                 :         24 :     asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_SUB_R64_FROM_RM64);
     405                 :         24 : }
     406                 :            : 
     407                 :         24 : void asm_x64_mul_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
     408                 :            :     // imul reg64, reg/mem64 -- 0x0f 0xaf /r
     409                 :         24 :     asm_x64_write_byte_1(as, REX_PREFIX | REX_W | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64));
     410                 :         24 :     asm_x64_write_byte_3(as, 0x0f, 0xaf, MODRM_R64(dest_r64) | MODRM_RM_REG | MODRM_RM_R64(src_r64));
     411                 :         24 : }
     412                 :            : 
     413                 :            : /*
     414                 :            : void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
     415                 :            :     if (SIGNED_FIT8(src_i32)) {
     416                 :            :         // defaults to 32 bit operation
     417                 :            :         asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
     418                 :            :         asm_x64_write_byte_1(as, src_i32 & 0xff);
     419                 :            :     } else {
     420                 :            :         // defaults to 32 bit operation
     421                 :            :         asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
     422                 :            :         asm_x64_write_word32(as, src_i32);
     423                 :            :     }
     424                 :            : }
     425                 :            : */
     426                 :            : 
     427                 :      25388 : static void asm_x64_sub_r64_i32(asm_x64_t *as, int dest_r64, int src_i32) {
     428         [ -  + ]:      25388 :     assert(dest_r64 < 8);
     429   [ +  +  +  + ]:      25388 :     if (SIGNED_FIT8(src_i32)) {
     430                 :            :         // use REX prefix for 64 bit operation
     431                 :       9790 :         asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
     432                 :       9790 :         asm_x64_write_byte_1(as, src_i32 & 0xff);
     433                 :            :     } else {
     434                 :            :         // use REX prefix for 64 bit operation
     435                 :      15598 :         asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
     436                 :      15598 :         asm_x64_write_word32(as, src_i32);
     437                 :            :     }
     438                 :      25388 : }
     439                 :            : 
     440                 :            : /*
     441                 :            : void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
     442                 :            :     asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
     443                 :            :     asm_x64_write_byte_1(as, imm);
     444                 :            : }
     445                 :            : 
     446                 :            : void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
     447                 :            :     asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
     448                 :            :     asm_x64_write_byte_1(as, imm);
     449                 :            : }
     450                 :            : 
     451                 :            : void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
     452                 :            :     asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
     453                 :            :     asm_x64_write_byte_1(as, imm);
     454                 :            : }
     455                 :            : */
     456                 :            : 
     457                 :        990 : void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
     458                 :        990 :     asm_x64_generic_r64_r64(as, src_r64_b, src_r64_a, OPCODE_CMP_R64_WITH_RM64);
     459                 :        990 : }
     460                 :            : 
     461                 :            : /*
     462                 :            : void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
     463                 :            :     if (SIGNED_FIT8(src_i32)) {
     464                 :            :         asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
     465                 :            :         asm_x64_write_byte_1(as, src_i32 & 0xff);
     466                 :            :     } else {
     467                 :            :         asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
     468                 :            :         asm_x64_write_word32(as, src_i32);
     469                 :            :     }
     470                 :            : }
     471                 :            : */
     472                 :            : 
     473                 :      19568 : void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
     474         [ -  + ]:      19568 :     assert(src_r64_a < 8);
     475         [ -  + ]:      19568 :     assert(src_r64_b < 8);
     476                 :      19568 :     asm_x64_write_byte_2(as, OPCODE_TEST_R8_WITH_RM8, MODRM_R64(src_r64_a) | MODRM_RM_REG | MODRM_RM_R64(src_r64_b));
     477                 :      19568 : }
     478                 :            : 
     479                 :      15671 : void asm_x64_test_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
     480                 :      15671 :     asm_x64_generic_r64_r64(as, src_r64_b, src_r64_a, OPCODE_TEST_R64_WITH_RM64);
     481                 :      15671 : }
     482                 :            : 
     483                 :        268 : void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
     484         [ -  + ]:        268 :     assert(dest_r8 < 8);
     485                 :        268 :     asm_x64_write_byte_3(as, OPCODE_SETCC_RM8_A, OPCODE_SETCC_RM8_B | jcc_type, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r8));
     486                 :        268 : }
     487                 :            : 
     488                 :       3230 : void asm_x64_jmp_reg(asm_x64_t *as, int src_r64) {
     489         [ -  + ]:       3230 :     assert(src_r64 < 8);
     490                 :       3230 :     asm_x64_write_byte_2(as, OPCODE_JMP_RM64, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(src_r64));
     491                 :       3230 : }
     492                 :            : 
     493                 :      76451 : static mp_uint_t get_label_dest(asm_x64_t *as, mp_uint_t label) {
     494         [ -  + ]:      76451 :     assert(label < as->base.max_num_labels);
     495                 :      76451 :     return as->base.label_offsets[label];
     496                 :            : }
     497                 :            : 
     498                 :      30665 : void asm_x64_jmp_label(asm_x64_t *as, mp_uint_t label) {
     499                 :      30665 :     mp_uint_t dest = get_label_dest(as, label);
     500                 :      30665 :     mp_int_t rel = dest - as->base.code_offset;
     501         [ +  + ]:      30665 :     if (dest != (mp_uint_t)-1 && rel < 0) {
     502                 :            :         // is a backwards jump, so we know the size of the jump on the first pass
     503                 :            :         // calculate rel assuming 8 bit relative jump
     504                 :       1584 :         rel -= 2;
     505         [ +  + ]:       1584 :         if (SIGNED_FIT8(rel)) {
     506                 :        423 :             asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
     507                 :            :         } else {
     508                 :       1161 :             rel += 2;
     509                 :       1161 :             goto large_jump;
     510                 :            :         }
     511                 :            :     } else {
     512                 :            :         // is a forwards jump, so need to assume it's large
     513                 :      29081 :     large_jump:
     514                 :      30242 :         rel -= 5;
     515                 :      30242 :         asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
     516                 :      30242 :         asm_x64_write_word32(as, rel);
     517                 :            :     }
     518                 :      30665 : }
     519                 :            : 
     520                 :      35961 : void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, mp_uint_t label) {
     521                 :      35961 :     mp_uint_t dest = get_label_dest(as, label);
     522                 :      35961 :     mp_int_t rel = dest - as->base.code_offset;
     523         [ +  + ]:      35961 :     if (dest != (mp_uint_t)-1 && rel < 0) {
     524                 :            :         // is a backwards jump, so we know the size of the jump on the first pass
     525                 :            :         // calculate rel assuming 8 bit relative jump
     526                 :       4071 :         rel -= 2;
     527         [ +  + ]:       4071 :         if (SIGNED_FIT8(rel)) {
     528                 :       3146 :             asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
     529                 :            :         } else {
     530                 :        925 :             rel += 2;
     531                 :        925 :             goto large_jump;
     532                 :            :         }
     533                 :            :     } else {
     534                 :            :         // is a forwards jump, so need to assume it's large
     535                 :      31890 :     large_jump:
     536                 :      32815 :         rel -= 6;
     537                 :      32815 :         asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
     538                 :      32815 :         asm_x64_write_word32(as, rel);
     539                 :            :     }
     540                 :      35961 : }
     541                 :            : 
     542                 :      12323 : void asm_x64_entry(asm_x64_t *as, int num_locals) {
     543         [ -  + ]:      12323 :     assert(num_locals >= 0);
     544                 :      12323 :     asm_x64_push_r64(as, ASM_X64_REG_RBP);
     545                 :      12323 :     asm_x64_push_r64(as, ASM_X64_REG_RBX);
     546                 :      12323 :     asm_x64_push_r64(as, ASM_X64_REG_R12);
     547                 :      12323 :     asm_x64_push_r64(as, ASM_X64_REG_R13);
     548                 :      12323 :     num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
     549                 :      12323 :     asm_x64_sub_r64_i32(as, ASM_X64_REG_RSP, num_locals * WORD_SIZE);
     550                 :      12323 :     as->num_locals = num_locals;
     551                 :      12323 : }
     552                 :            : 
     553                 :      13065 : void asm_x64_exit(asm_x64_t *as) {
     554                 :      13065 :     asm_x64_sub_r64_i32(as, ASM_X64_REG_RSP, -as->num_locals * WORD_SIZE);
     555                 :      13065 :     asm_x64_pop_r64(as, ASM_X64_REG_R13);
     556                 :      13065 :     asm_x64_pop_r64(as, ASM_X64_REG_R12);
     557                 :      13065 :     asm_x64_pop_r64(as, ASM_X64_REG_RBX);
     558                 :      13065 :     asm_x64_pop_r64(as, ASM_X64_REG_RBP);
     559                 :      13065 :     asm_x64_ret(as);
     560                 :      13065 : }
     561                 :            : 
     562                 :            : // locals:
     563                 :            : //  - stored on the stack in ascending order
     564                 :            : //  - numbered 0 through as->num_locals-1
     565                 :            : //  - RSP points to the first local
     566                 :            : //
     567                 :            : //  | RSP
     568                 :            : //  v
     569                 :            : //  l0  l1  l2  ...  l(n-1)
     570                 :            : //  ^                ^
     571                 :            : //  | low address    | high address in RAM
     572                 :            : //
     573                 :     468822 : static int asm_x64_local_offset_from_rsp(asm_x64_t *as, int local_num) {
     574                 :     468822 :     (void)as;
     575                 :            :     // Stack is full descending, RSP points to local0
     576                 :     468822 :     return local_num * WORD_SIZE;
     577                 :            : }
     578                 :            : 
     579                 :     157963 : void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
     580                 :     157963 :     asm_x64_mov_mem64_to_r64(as, ASM_X64_REG_RSP, asm_x64_local_offset_from_rsp(as, src_local_num), dest_r64);
     581                 :     157963 : }
     582                 :            : 
     583                 :     218044 : void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
     584                 :     218044 :     asm_x64_mov_r64_to_mem64(as, src_r64, ASM_X64_REG_RSP, asm_x64_local_offset_from_rsp(as, dest_local_num));
     585                 :     218044 : }
     586                 :            : 
     587                 :      92815 : void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
     588                 :      92815 :     int offset = asm_x64_local_offset_from_rsp(as, local_num);
     589         [ +  + ]:      92815 :     if (offset == 0) {
     590                 :      11963 :         asm_x64_mov_r64_r64(as, dest_r64, ASM_X64_REG_RSP);
     591                 :            :     } else {
     592                 :      80852 :         asm_x64_lea_disp_to_r64(as, ASM_X64_REG_RSP, offset, dest_r64);
     593                 :            :     }
     594                 :      92815 : }
     595                 :            : 
     596                 :       9825 : void asm_x64_mov_reg_pcrel(asm_x64_t *as, int dest_r64, mp_uint_t label) {
     597                 :       9825 :     mp_uint_t dest = get_label_dest(as, label);
     598                 :       9825 :     mp_int_t rel = dest - (as->base.code_offset + 7);
     599                 :       9825 :     asm_x64_write_byte_3(as, REX_PREFIX | REX_W | REX_R_FROM_R64(dest_r64), OPCODE_LEA_MEM_TO_R64, MODRM_R64(dest_r64) | MODRM_RM_R64(5));
     600                 :       9825 :     asm_x64_write_word32(as, rel);
     601                 :       9825 : }
     602                 :            : 
     603                 :            : /*
     604                 :            : void asm_x64_push_local(asm_x64_t *as, int local_num) {
     605                 :            :     asm_x64_push_disp(as, ASM_X64_REG_RSP, asm_x64_local_offset_from_rsp(as, local_num));
     606                 :            : }
     607                 :            : 
     608                 :            : void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64) {
     609                 :            :     asm_x64_mov_r64_r64(as, temp_r64, ASM_X64_REG_RSP);
     610                 :            :     asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_rsp(as, local_num), temp_r64);
     611                 :            :     asm_x64_push_r64(as, temp_r64);
     612                 :            : }
     613                 :            : */
     614                 :            : 
     615                 :            : /*
     616                 :            :    can't use these because code might be relocated when resized
     617                 :            : 
     618                 :            : void asm_x64_call(asm_x64_t *as, void* func) {
     619                 :            :     asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
     620                 :            :     asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
     621                 :            :     asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
     622                 :            :     asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
     623                 :            : }
     624                 :            : 
     625                 :            : void asm_x64_call_i1(asm_x64_t *as, void* func, int i1) {
     626                 :            :     asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
     627                 :            :     asm_x64_sub_i32_from_r32(as, 12, ASM_X64_REG_RSP);
     628                 :            :     asm_x64_push_i32(as, i1);
     629                 :            :     asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
     630                 :            :     asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
     631                 :            :     asm_x64_add_i32_to_r32(as, 16, ASM_X64_REG_RSP);
     632                 :            :     asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
     633                 :            : }
     634                 :            : */
     635                 :            : 
     636                 :     312842 : void asm_x64_call_ind(asm_x64_t *as, size_t fun_id, int temp_r64) {
     637         [ -  + ]:     312842 :     assert(temp_r64 < 8);
     638                 :     312842 :     asm_x64_mov_mem64_to_r64(as, ASM_X64_REG_FUN_TABLE, fun_id * WORD_SIZE, temp_r64);
     639                 :     312842 :     asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64));
     640                 :     312842 : }
     641                 :            : 
     642                 :            : #endif // MICROPY_EMIT_X64

Generated by: LCOV version 1.15-5-g462f71d