Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
py/asmx64.c@10:33521d742af1, 2016-04-27 (annotated)
- Committer:
- Colin Hogben
- Date:
- Wed Apr 27 22:11:29 2016 +0100
- Revision:
- 10:33521d742af1
- Parent:
- 0:5868e8752d44
Update README and version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pythontech | 0:5868e8752d44 | 1 | /* |
| pythontech | 0:5868e8752d44 | 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| pythontech | 0:5868e8752d44 | 3 | * |
| pythontech | 0:5868e8752d44 | 4 | * The MIT License (MIT) |
| pythontech | 0:5868e8752d44 | 5 | * |
| pythontech | 0:5868e8752d44 | 6 | * Copyright (c) 2013, 2014 Damien P. George |
| pythontech | 0:5868e8752d44 | 7 | * |
| pythontech | 0:5868e8752d44 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| pythontech | 0:5868e8752d44 | 9 | * of this software and associated documentation files (the "Software"), to deal |
| pythontech | 0:5868e8752d44 | 10 | * in the Software without restriction, including without limitation the rights |
| pythontech | 0:5868e8752d44 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| pythontech | 0:5868e8752d44 | 12 | * copies of the Software, and to permit persons to whom the Software is |
| pythontech | 0:5868e8752d44 | 13 | * furnished to do so, subject to the following conditions: |
| pythontech | 0:5868e8752d44 | 14 | * |
| pythontech | 0:5868e8752d44 | 15 | * The above copyright notice and this permission notice shall be included in |
| pythontech | 0:5868e8752d44 | 16 | * all copies or substantial portions of the Software. |
| pythontech | 0:5868e8752d44 | 17 | * |
| pythontech | 0:5868e8752d44 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| pythontech | 0:5868e8752d44 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| pythontech | 0:5868e8752d44 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| pythontech | 0:5868e8752d44 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| pythontech | 0:5868e8752d44 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| pythontech | 0:5868e8752d44 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| pythontech | 0:5868e8752d44 | 24 | * THE SOFTWARE. |
| pythontech | 0:5868e8752d44 | 25 | */ |
| pythontech | 0:5868e8752d44 | 26 | |
| pythontech | 0:5868e8752d44 | 27 | #include <stdint.h> |
| pythontech | 0:5868e8752d44 | 28 | #include <stdio.h> |
| pythontech | 0:5868e8752d44 | 29 | #include <assert.h> |
| pythontech | 0:5868e8752d44 | 30 | #include <string.h> |
| pythontech | 0:5868e8752d44 | 31 | |
| pythontech | 0:5868e8752d44 | 32 | #include "py/mpconfig.h" |
| pythontech | 0:5868e8752d44 | 33 | |
| pythontech | 0:5868e8752d44 | 34 | // wrapper around everything in this file |
| pythontech | 0:5868e8752d44 | 35 | #if MICROPY_EMIT_X64 |
| pythontech | 0:5868e8752d44 | 36 | |
| pythontech | 0:5868e8752d44 | 37 | #include "py/asmx64.h" |
| pythontech | 0:5868e8752d44 | 38 | |
| pythontech | 0:5868e8752d44 | 39 | /* all offsets are measured in multiples of 8 bytes */ |
| pythontech | 0:5868e8752d44 | 40 | #define WORD_SIZE (8) |
| pythontech | 0:5868e8752d44 | 41 | |
| pythontech | 0:5868e8752d44 | 42 | #define OPCODE_NOP (0x90) |
| pythontech | 0:5868e8752d44 | 43 | #define OPCODE_PUSH_R64 (0x50) /* +rq */ |
| pythontech | 0:5868e8752d44 | 44 | #define OPCODE_PUSH_I64 (0x68) |
| pythontech | 0:5868e8752d44 | 45 | #define OPCODE_PUSH_M64 (0xff) /* /6 */ |
| pythontech | 0:5868e8752d44 | 46 | #define OPCODE_POP_R64 (0x58) /* +rq */ |
| pythontech | 0:5868e8752d44 | 47 | #define OPCODE_RET (0xc3) |
| pythontech | 0:5868e8752d44 | 48 | #define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */ |
| pythontech | 0:5868e8752d44 | 49 | #define OPCODE_MOV_I64_TO_R64 (0xb8) /* +rq */ |
| pythontech | 0:5868e8752d44 | 50 | #define OPCODE_MOV_I32_TO_RM32 (0xc7) |
| pythontech | 0:5868e8752d44 | 51 | #define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */ |
| pythontech | 0:5868e8752d44 | 52 | #define OPCODE_MOV_R64_TO_RM64 (0x89) /* /r */ |
| pythontech | 0:5868e8752d44 | 53 | #define OPCODE_MOV_RM64_TO_R64 (0x8b) /* /r */ |
| pythontech | 0:5868e8752d44 | 54 | #define OPCODE_MOVZX_RM8_TO_R64 (0xb6) /* 0x0f 0xb6/r */ |
| pythontech | 0:5868e8752d44 | 55 | #define OPCODE_MOVZX_RM16_TO_R64 (0xb7) /* 0x0f 0xb7/r */ |
| pythontech | 0:5868e8752d44 | 56 | #define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */ |
| pythontech | 0:5868e8752d44 | 57 | #define OPCODE_AND_R64_TO_RM64 (0x21) /* /r */ |
| pythontech | 0:5868e8752d44 | 58 | #define OPCODE_OR_R64_TO_RM64 (0x09) /* /r */ |
| pythontech | 0:5868e8752d44 | 59 | #define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */ |
| pythontech | 0:5868e8752d44 | 60 | #define OPCODE_ADD_R64_TO_RM64 (0x01) /* /r */ |
| pythontech | 0:5868e8752d44 | 61 | #define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */ |
| pythontech | 0:5868e8752d44 | 62 | #define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */ |
| pythontech | 0:5868e8752d44 | 63 | #define OPCODE_SUB_R64_FROM_RM64 (0x29) |
| pythontech | 0:5868e8752d44 | 64 | #define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */ |
| pythontech | 0:5868e8752d44 | 65 | #define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */ |
| pythontech | 0:5868e8752d44 | 66 | //#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */ |
| pythontech | 0:5868e8752d44 | 67 | //#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */ |
| pythontech | 0:5868e8752d44 | 68 | //#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */ |
| pythontech | 0:5868e8752d44 | 69 | #define OPCODE_SHL_RM64_CL (0xd3) /* /4 */ |
| pythontech | 0:5868e8752d44 | 70 | #define OPCODE_SAR_RM64_CL (0xd3) /* /7 */ |
| pythontech | 0:5868e8752d44 | 71 | //#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */ |
| pythontech | 0:5868e8752d44 | 72 | //#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */ |
| pythontech | 0:5868e8752d44 | 73 | #define OPCODE_CMP_R64_WITH_RM64 (0x39) /* /r */ |
| pythontech | 0:5868e8752d44 | 74 | //#define OPCODE_CMP_RM32_WITH_R32 (0x3b) |
| pythontech | 0:5868e8752d44 | 75 | #define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */ |
| pythontech | 0:5868e8752d44 | 76 | #define OPCODE_JMP_REL8 (0xeb) |
| pythontech | 0:5868e8752d44 | 77 | #define OPCODE_JMP_REL32 (0xe9) |
| pythontech | 0:5868e8752d44 | 78 | #define OPCODE_JCC_REL8 (0x70) /* | jcc type */ |
| pythontech | 0:5868e8752d44 | 79 | #define OPCODE_JCC_REL32_A (0x0f) |
| pythontech | 0:5868e8752d44 | 80 | #define OPCODE_JCC_REL32_B (0x80) /* | jcc type */ |
| pythontech | 0:5868e8752d44 | 81 | #define OPCODE_SETCC_RM8_A (0x0f) |
| pythontech | 0:5868e8752d44 | 82 | #define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */ |
| pythontech | 0:5868e8752d44 | 83 | #define OPCODE_CALL_REL32 (0xe8) |
| pythontech | 0:5868e8752d44 | 84 | #define OPCODE_CALL_RM32 (0xff) /* /2 */ |
| pythontech | 0:5868e8752d44 | 85 | #define OPCODE_LEAVE (0xc9) |
| pythontech | 0:5868e8752d44 | 86 | |
| pythontech | 0:5868e8752d44 | 87 | #define MODRM_R64(x) (((x) & 0x7) << 3) |
| pythontech | 0:5868e8752d44 | 88 | #define MODRM_RM_DISP0 (0x00) |
| pythontech | 0:5868e8752d44 | 89 | #define MODRM_RM_DISP8 (0x40) |
| pythontech | 0:5868e8752d44 | 90 | #define MODRM_RM_DISP32 (0x80) |
| pythontech | 0:5868e8752d44 | 91 | #define MODRM_RM_REG (0xc0) |
| pythontech | 0:5868e8752d44 | 92 | #define MODRM_RM_R64(x) ((x) & 0x7) |
| pythontech | 0:5868e8752d44 | 93 | |
| pythontech | 0:5868e8752d44 | 94 | #define OP_SIZE_PREFIX (0x66) |
| pythontech | 0:5868e8752d44 | 95 | |
| pythontech | 0:5868e8752d44 | 96 | #define REX_PREFIX (0x40) |
| pythontech | 0:5868e8752d44 | 97 | #define REX_W (0x08) // width |
| pythontech | 0:5868e8752d44 | 98 | #define REX_R (0x04) // register |
| pythontech | 0:5868e8752d44 | 99 | #define REX_X (0x02) // index |
| pythontech | 0:5868e8752d44 | 100 | #define REX_B (0x01) // base |
| pythontech | 0:5868e8752d44 | 101 | #define REX_W_FROM_R64(r64) ((r64) >> 0 & 0x08) |
| pythontech | 0:5868e8752d44 | 102 | #define REX_R_FROM_R64(r64) ((r64) >> 1 & 0x04) |
| pythontech | 0:5868e8752d44 | 103 | #define REX_X_FROM_R64(r64) ((r64) >> 2 & 0x02) |
| pythontech | 0:5868e8752d44 | 104 | #define REX_B_FROM_R64(r64) ((r64) >> 3 & 0x01) |
| pythontech | 0:5868e8752d44 | 105 | |
| pythontech | 0:5868e8752d44 | 106 | #define IMM32_L0(x) ((x) & 0xff) |
| pythontech | 0:5868e8752d44 | 107 | #define IMM32_L1(x) (((x) >> 8) & 0xff) |
| pythontech | 0:5868e8752d44 | 108 | #define IMM32_L2(x) (((x) >> 16) & 0xff) |
| pythontech | 0:5868e8752d44 | 109 | #define IMM32_L3(x) (((x) >> 24) & 0xff) |
| pythontech | 0:5868e8752d44 | 110 | #define IMM64_L4(x) (((x) >> 32) & 0xff) |
| pythontech | 0:5868e8752d44 | 111 | #define IMM64_L5(x) (((x) >> 40) & 0xff) |
| pythontech | 0:5868e8752d44 | 112 | #define IMM64_L6(x) (((x) >> 48) & 0xff) |
| pythontech | 0:5868e8752d44 | 113 | #define IMM64_L7(x) (((x) >> 56) & 0xff) |
| pythontech | 0:5868e8752d44 | 114 | |
| pythontech | 0:5868e8752d44 | 115 | #define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0) |
| pythontech | 0:5868e8752d44 | 116 | #define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0) |
| pythontech | 0:5868e8752d44 | 117 | #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80) |
| pythontech | 0:5868e8752d44 | 118 | |
| pythontech | 0:5868e8752d44 | 119 | struct _asm_x64_t { |
| pythontech | 0:5868e8752d44 | 120 | uint pass; |
| pythontech | 0:5868e8752d44 | 121 | mp_uint_t code_offset; |
| pythontech | 0:5868e8752d44 | 122 | mp_uint_t code_size; |
| pythontech | 0:5868e8752d44 | 123 | byte *code_base; |
| pythontech | 0:5868e8752d44 | 124 | byte dummy_data[8]; |
| pythontech | 0:5868e8752d44 | 125 | |
| pythontech | 0:5868e8752d44 | 126 | mp_uint_t max_num_labels; |
| pythontech | 0:5868e8752d44 | 127 | mp_uint_t *label_offsets; |
| pythontech | 0:5868e8752d44 | 128 | int num_locals; |
| pythontech | 0:5868e8752d44 | 129 | }; |
| pythontech | 0:5868e8752d44 | 130 | |
| pythontech | 0:5868e8752d44 | 131 | asm_x64_t *asm_x64_new(mp_uint_t max_num_labels) { |
| pythontech | 0:5868e8752d44 | 132 | asm_x64_t *as; |
| pythontech | 0:5868e8752d44 | 133 | |
| pythontech | 0:5868e8752d44 | 134 | as = m_new0(asm_x64_t, 1); |
| pythontech | 0:5868e8752d44 | 135 | as->max_num_labels = max_num_labels; |
| pythontech | 0:5868e8752d44 | 136 | as->label_offsets = m_new(mp_uint_t, max_num_labels); |
| pythontech | 0:5868e8752d44 | 137 | |
| pythontech | 0:5868e8752d44 | 138 | return as; |
| pythontech | 0:5868e8752d44 | 139 | } |
| pythontech | 0:5868e8752d44 | 140 | |
| pythontech | 0:5868e8752d44 | 141 | void asm_x64_free(asm_x64_t *as, bool free_code) { |
| pythontech | 0:5868e8752d44 | 142 | if (free_code) { |
| pythontech | 0:5868e8752d44 | 143 | MP_PLAT_FREE_EXEC(as->code_base, as->code_size); |
| pythontech | 0:5868e8752d44 | 144 | } |
| pythontech | 0:5868e8752d44 | 145 | m_del(mp_uint_t, as->label_offsets, as->max_num_labels); |
| pythontech | 0:5868e8752d44 | 146 | m_del_obj(asm_x64_t, as); |
| pythontech | 0:5868e8752d44 | 147 | } |
| pythontech | 0:5868e8752d44 | 148 | |
| pythontech | 0:5868e8752d44 | 149 | void asm_x64_start_pass(asm_x64_t *as, uint pass) { |
| pythontech | 0:5868e8752d44 | 150 | if (pass == ASM_X64_PASS_COMPUTE) { |
| pythontech | 0:5868e8752d44 | 151 | // reset all labels |
| pythontech | 0:5868e8752d44 | 152 | memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t)); |
| pythontech | 0:5868e8752d44 | 153 | } if (pass == ASM_X64_PASS_EMIT) { |
| pythontech | 0:5868e8752d44 | 154 | MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size); |
| pythontech | 0:5868e8752d44 | 155 | if (as->code_base == NULL) { |
| pythontech | 0:5868e8752d44 | 156 | assert(0); |
| pythontech | 0:5868e8752d44 | 157 | } |
| pythontech | 0:5868e8752d44 | 158 | //printf("code_size: %u\n", as->code_size); |
| pythontech | 0:5868e8752d44 | 159 | } |
| pythontech | 0:5868e8752d44 | 160 | as->pass = pass; |
| pythontech | 0:5868e8752d44 | 161 | as->code_offset = 0; |
| pythontech | 0:5868e8752d44 | 162 | } |
| pythontech | 0:5868e8752d44 | 163 | |
| pythontech | 0:5868e8752d44 | 164 | void asm_x64_end_pass(asm_x64_t *as) { |
| pythontech | 0:5868e8752d44 | 165 | // could check labels are resolved... |
| pythontech | 0:5868e8752d44 | 166 | (void)as; |
| pythontech | 0:5868e8752d44 | 167 | } |
| pythontech | 0:5868e8752d44 | 168 | |
| pythontech | 0:5868e8752d44 | 169 | // all functions must go through this one to emit bytes |
| pythontech | 0:5868e8752d44 | 170 | STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) { |
| pythontech | 0:5868e8752d44 | 171 | //printf("emit %d\n", num_bytes_to_write); |
| pythontech | 0:5868e8752d44 | 172 | if (as->pass < ASM_X64_PASS_EMIT) { |
| pythontech | 0:5868e8752d44 | 173 | as->code_offset += num_bytes_to_write; |
| pythontech | 0:5868e8752d44 | 174 | return as->dummy_data; |
| pythontech | 0:5868e8752d44 | 175 | } else { |
| pythontech | 0:5868e8752d44 | 176 | assert(as->code_offset + num_bytes_to_write <= as->code_size); |
| pythontech | 0:5868e8752d44 | 177 | byte *c = as->code_base + as->code_offset; |
| pythontech | 0:5868e8752d44 | 178 | as->code_offset += num_bytes_to_write; |
| pythontech | 0:5868e8752d44 | 179 | return c; |
| pythontech | 0:5868e8752d44 | 180 | } |
| pythontech | 0:5868e8752d44 | 181 | } |
| pythontech | 0:5868e8752d44 | 182 | |
| pythontech | 0:5868e8752d44 | 183 | mp_uint_t asm_x64_get_code_pos(asm_x64_t *as) { |
| pythontech | 0:5868e8752d44 | 184 | return as->code_offset; |
| pythontech | 0:5868e8752d44 | 185 | } |
| pythontech | 0:5868e8752d44 | 186 | |
| pythontech | 0:5868e8752d44 | 187 | mp_uint_t asm_x64_get_code_size(asm_x64_t *as) { |
| pythontech | 0:5868e8752d44 | 188 | return as->code_size; |
| pythontech | 0:5868e8752d44 | 189 | } |
| pythontech | 0:5868e8752d44 | 190 | |
| pythontech | 0:5868e8752d44 | 191 | void *asm_x64_get_code(asm_x64_t *as) { |
| pythontech | 0:5868e8752d44 | 192 | return as->code_base; |
| pythontech | 0:5868e8752d44 | 193 | } |
| pythontech | 0:5868e8752d44 | 194 | |
| pythontech | 0:5868e8752d44 | 195 | STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) { |
| pythontech | 0:5868e8752d44 | 196 | byte* c = asm_x64_get_cur_to_write_bytes(as, 1); |
| pythontech | 0:5868e8752d44 | 197 | c[0] = b1; |
| pythontech | 0:5868e8752d44 | 198 | } |
| pythontech | 0:5868e8752d44 | 199 | |
| pythontech | 0:5868e8752d44 | 200 | STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) { |
| pythontech | 0:5868e8752d44 | 201 | byte* c = asm_x64_get_cur_to_write_bytes(as, 2); |
| pythontech | 0:5868e8752d44 | 202 | c[0] = b1; |
| pythontech | 0:5868e8752d44 | 203 | c[1] = b2; |
| pythontech | 0:5868e8752d44 | 204 | } |
| pythontech | 0:5868e8752d44 | 205 | |
| pythontech | 0:5868e8752d44 | 206 | STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) { |
| pythontech | 0:5868e8752d44 | 207 | byte* c = asm_x64_get_cur_to_write_bytes(as, 3); |
| pythontech | 0:5868e8752d44 | 208 | c[0] = b1; |
| pythontech | 0:5868e8752d44 | 209 | c[1] = b2; |
| pythontech | 0:5868e8752d44 | 210 | c[2] = b3; |
| pythontech | 0:5868e8752d44 | 211 | } |
| pythontech | 0:5868e8752d44 | 212 | |
| pythontech | 0:5868e8752d44 | 213 | STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) { |
| pythontech | 0:5868e8752d44 | 214 | byte* c = asm_x64_get_cur_to_write_bytes(as, 4); |
| pythontech | 0:5868e8752d44 | 215 | c[0] = IMM32_L0(w32); |
| pythontech | 0:5868e8752d44 | 216 | c[1] = IMM32_L1(w32); |
| pythontech | 0:5868e8752d44 | 217 | c[2] = IMM32_L2(w32); |
| pythontech | 0:5868e8752d44 | 218 | c[3] = IMM32_L3(w32); |
| pythontech | 0:5868e8752d44 | 219 | } |
| pythontech | 0:5868e8752d44 | 220 | |
| pythontech | 0:5868e8752d44 | 221 | STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) { |
| pythontech | 0:5868e8752d44 | 222 | byte* c = asm_x64_get_cur_to_write_bytes(as, 8); |
| pythontech | 0:5868e8752d44 | 223 | c[0] = IMM32_L0(w64); |
| pythontech | 0:5868e8752d44 | 224 | c[1] = IMM32_L1(w64); |
| pythontech | 0:5868e8752d44 | 225 | c[2] = IMM32_L2(w64); |
| pythontech | 0:5868e8752d44 | 226 | c[3] = IMM32_L3(w64); |
| pythontech | 0:5868e8752d44 | 227 | c[4] = IMM64_L4(w64); |
| pythontech | 0:5868e8752d44 | 228 | c[5] = IMM64_L5(w64); |
| pythontech | 0:5868e8752d44 | 229 | c[6] = IMM64_L6(w64); |
| pythontech | 0:5868e8752d44 | 230 | c[7] = IMM64_L7(w64); |
| pythontech | 0:5868e8752d44 | 231 | } |
| pythontech | 0:5868e8752d44 | 232 | |
| pythontech | 0:5868e8752d44 | 233 | // align must be a multiple of 2 |
| pythontech | 0:5868e8752d44 | 234 | void asm_x64_align(asm_x64_t* as, mp_uint_t align) { |
| pythontech | 0:5868e8752d44 | 235 | // TODO fill unused data with NOPs? |
| pythontech | 0:5868e8752d44 | 236 | as->code_offset = (as->code_offset + align - 1) & (~(align - 1)); |
| pythontech | 0:5868e8752d44 | 237 | } |
| pythontech | 0:5868e8752d44 | 238 | |
| pythontech | 0:5868e8752d44 | 239 | void asm_x64_data(asm_x64_t* as, mp_uint_t bytesize, mp_uint_t val) { |
| pythontech | 0:5868e8752d44 | 240 | byte *c = asm_x64_get_cur_to_write_bytes(as, bytesize); |
| pythontech | 0:5868e8752d44 | 241 | // machine is little endian |
| pythontech | 0:5868e8752d44 | 242 | for (uint i = 0; i < bytesize; i++) { |
| pythontech | 0:5868e8752d44 | 243 | *c++ = val; |
| pythontech | 0:5868e8752d44 | 244 | val >>= 8; |
| pythontech | 0:5868e8752d44 | 245 | } |
| pythontech | 0:5868e8752d44 | 246 | } |
| pythontech | 0:5868e8752d44 | 247 | |
| pythontech | 0:5868e8752d44 | 248 | /* unused |
| pythontech | 0:5868e8752d44 | 249 | STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) { |
| pythontech | 0:5868e8752d44 | 250 | byte* c; |
| pythontech | 0:5868e8752d44 | 251 | assert(offset + 4 <= as->code_size); |
| pythontech | 0:5868e8752d44 | 252 | c = as->code_base + offset; |
| pythontech | 0:5868e8752d44 | 253 | c[0] = IMM32_L0(w32); |
| pythontech | 0:5868e8752d44 | 254 | c[1] = IMM32_L1(w32); |
| pythontech | 0:5868e8752d44 | 255 | c[2] = IMM32_L2(w32); |
| pythontech | 0:5868e8752d44 | 256 | c[3] = IMM32_L3(w32); |
| pythontech | 0:5868e8752d44 | 257 | } |
| pythontech | 0:5868e8752d44 | 258 | */ |
| pythontech | 0:5868e8752d44 | 259 | |
| pythontech | 0:5868e8752d44 | 260 | STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) { |
| pythontech | 0:5868e8752d44 | 261 | assert(disp_r64 != ASM_X64_REG_RSP); |
| pythontech | 0:5868e8752d44 | 262 | |
| pythontech | 0:5868e8752d44 | 263 | if (disp_r64 == ASM_X64_REG_R12) { |
| pythontech | 0:5868e8752d44 | 264 | // special case for r12; not fully implemented |
| pythontech | 0:5868e8752d44 | 265 | assert(SIGNED_FIT8(disp_offset)); |
| pythontech | 0:5868e8752d44 | 266 | asm_x64_write_byte_3(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), 0x24, IMM32_L0(disp_offset)); |
| pythontech | 0:5868e8752d44 | 267 | return; |
| pythontech | 0:5868e8752d44 | 268 | } |
| pythontech | 0:5868e8752d44 | 269 | |
| pythontech | 0:5868e8752d44 | 270 | if (disp_offset == 0 && disp_r64 != ASM_X64_REG_RBP) { |
| pythontech | 0:5868e8752d44 | 271 | asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64)); |
| pythontech | 0:5868e8752d44 | 272 | } else if (SIGNED_FIT8(disp_offset)) { |
| pythontech | 0:5868e8752d44 | 273 | asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset)); |
| pythontech | 0:5868e8752d44 | 274 | } else { |
| pythontech | 0:5868e8752d44 | 275 | asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64)); |
| pythontech | 0:5868e8752d44 | 276 | asm_x64_write_word32(as, disp_offset); |
| pythontech | 0:5868e8752d44 | 277 | } |
| pythontech | 0:5868e8752d44 | 278 | } |
| pythontech | 0:5868e8752d44 | 279 | |
| pythontech | 0:5868e8752d44 | 280 | STATIC void asm_x64_generic_r64_r64(asm_x64_t *as, int dest_r64, int src_r64, int op) { |
| pythontech | 0:5868e8752d44 | 281 | 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)); |
| pythontech | 0:5868e8752d44 | 282 | } |
| pythontech | 0:5868e8752d44 | 283 | |
| pythontech | 0:5868e8752d44 | 284 | void asm_x64_nop(asm_x64_t *as) { |
| pythontech | 0:5868e8752d44 | 285 | asm_x64_write_byte_1(as, OPCODE_NOP); |
| pythontech | 0:5868e8752d44 | 286 | } |
| pythontech | 0:5868e8752d44 | 287 | |
| pythontech | 0:5868e8752d44 | 288 | void asm_x64_push_r64(asm_x64_t *as, int src_r64) { |
| pythontech | 0:5868e8752d44 | 289 | if (src_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 290 | asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64); |
| pythontech | 0:5868e8752d44 | 291 | } else { |
| pythontech | 0:5868e8752d44 | 292 | asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_PUSH_R64 | (src_r64 & 7)); |
| pythontech | 0:5868e8752d44 | 293 | } |
| pythontech | 0:5868e8752d44 | 294 | } |
| pythontech | 0:5868e8752d44 | 295 | |
| pythontech | 0:5868e8752d44 | 296 | /* |
| pythontech | 0:5868e8752d44 | 297 | void asm_x64_push_i32(asm_x64_t *as, int src_i32) { |
| pythontech | 0:5868e8752d44 | 298 | asm_x64_write_byte_1(as, OPCODE_PUSH_I64); |
| pythontech | 0:5868e8752d44 | 299 | asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits |
| pythontech | 0:5868e8752d44 | 300 | } |
| pythontech | 0:5868e8752d44 | 301 | */ |
| pythontech | 0:5868e8752d44 | 302 | |
| pythontech | 0:5868e8752d44 | 303 | /* |
| pythontech | 0:5868e8752d44 | 304 | void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) { |
| pythontech | 0:5868e8752d44 | 305 | assert(src_r64 < 8); |
| pythontech | 0:5868e8752d44 | 306 | asm_x64_write_byte_1(as, OPCODE_PUSH_M64); |
| pythontech | 0:5868e8752d44 | 307 | asm_x64_write_r64_disp(as, 6, src_r64, src_offset); |
| pythontech | 0:5868e8752d44 | 308 | } |
| pythontech | 0:5868e8752d44 | 309 | */ |
| pythontech | 0:5868e8752d44 | 310 | |
| pythontech | 0:5868e8752d44 | 311 | void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 312 | if (dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 313 | asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64); |
| pythontech | 0:5868e8752d44 | 314 | } else { |
| pythontech | 0:5868e8752d44 | 315 | asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_POP_R64 | (dest_r64 & 7)); |
| pythontech | 0:5868e8752d44 | 316 | } |
| pythontech | 0:5868e8752d44 | 317 | } |
| pythontech | 0:5868e8752d44 | 318 | |
| pythontech | 0:5868e8752d44 | 319 | STATIC void asm_x64_ret(asm_x64_t *as) { |
| pythontech | 0:5868e8752d44 | 320 | asm_x64_write_byte_1(as, OPCODE_RET); |
| pythontech | 0:5868e8752d44 | 321 | } |
| pythontech | 0:5868e8752d44 | 322 | |
| pythontech | 0:5868e8752d44 | 323 | void asm_x64_mov_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) { |
| pythontech | 0:5868e8752d44 | 324 | asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_MOV_R64_TO_RM64); |
| pythontech | 0:5868e8752d44 | 325 | } |
| pythontech | 0:5868e8752d44 | 326 | |
| pythontech | 0:5868e8752d44 | 327 | void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) { |
| pythontech | 0:5868e8752d44 | 328 | if (src_r64 < 8 && dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 329 | asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8); |
| pythontech | 0:5868e8752d44 | 330 | } else { |
| pythontech | 0:5868e8752d44 | 331 | 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); |
| pythontech | 0:5868e8752d44 | 332 | } |
| pythontech | 0:5868e8752d44 | 333 | asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp); |
| pythontech | 0:5868e8752d44 | 334 | } |
| pythontech | 0:5868e8752d44 | 335 | |
| pythontech | 0:5868e8752d44 | 336 | void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) { |
| pythontech | 0:5868e8752d44 | 337 | if (src_r64 < 8 && dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 338 | asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64); |
| pythontech | 0:5868e8752d44 | 339 | } else { |
| pythontech | 0:5868e8752d44 | 340 | 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); |
| pythontech | 0:5868e8752d44 | 341 | } |
| pythontech | 0:5868e8752d44 | 342 | asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp); |
| pythontech | 0:5868e8752d44 | 343 | } |
| pythontech | 0:5868e8752d44 | 344 | |
| pythontech | 0:5868e8752d44 | 345 | void asm_x64_mov_r32_to_mem32(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) { |
| pythontech | 0:5868e8752d44 | 346 | if (src_r64 < 8 && dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 347 | asm_x64_write_byte_1(as, OPCODE_MOV_R64_TO_RM64); |
| pythontech | 0:5868e8752d44 | 348 | } else { |
| pythontech | 0:5868e8752d44 | 349 | 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); |
| pythontech | 0:5868e8752d44 | 350 | } |
| pythontech | 0:5868e8752d44 | 351 | asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp); |
| pythontech | 0:5868e8752d44 | 352 | } |
| pythontech | 0:5868e8752d44 | 353 | |
| pythontech | 0:5868e8752d44 | 354 | void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) { |
| pythontech | 0:5868e8752d44 | 355 | // use REX prefix for 64 bit operation |
| pythontech | 0:5868e8752d44 | 356 | 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); |
| pythontech | 0:5868e8752d44 | 357 | asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp); |
| pythontech | 0:5868e8752d44 | 358 | } |
| pythontech | 0:5868e8752d44 | 359 | |
| pythontech | 0:5868e8752d44 | 360 | void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 361 | assert(src_r64 < 8); |
| pythontech | 0:5868e8752d44 | 362 | if (dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 363 | asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R64); |
| pythontech | 0:5868e8752d44 | 364 | } else { |
| pythontech | 0:5868e8752d44 | 365 | asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM8_TO_R64); |
| pythontech | 0:5868e8752d44 | 366 | } |
| pythontech | 0:5868e8752d44 | 367 | asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); |
| pythontech | 0:5868e8752d44 | 368 | } |
| pythontech | 0:5868e8752d44 | 369 | |
| pythontech | 0:5868e8752d44 | 370 | void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 371 | assert(src_r64 < 8); |
| pythontech | 0:5868e8752d44 | 372 | if (dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 373 | asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R64); |
| pythontech | 0:5868e8752d44 | 374 | } else { |
| pythontech | 0:5868e8752d44 | 375 | asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM16_TO_R64); |
| pythontech | 0:5868e8752d44 | 376 | } |
| pythontech | 0:5868e8752d44 | 377 | asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); |
| pythontech | 0:5868e8752d44 | 378 | } |
| pythontech | 0:5868e8752d44 | 379 | |
| pythontech | 0:5868e8752d44 | 380 | void asm_x64_mov_mem32_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 381 | assert(src_r64 < 8); |
| pythontech | 0:5868e8752d44 | 382 | if (dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 383 | asm_x64_write_byte_1(as, OPCODE_MOV_RM64_TO_R64); |
| pythontech | 0:5868e8752d44 | 384 | } else { |
| pythontech | 0:5868e8752d44 | 385 | asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_RM64_TO_R64); |
| pythontech | 0:5868e8752d44 | 386 | } |
| pythontech | 0:5868e8752d44 | 387 | asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); |
| pythontech | 0:5868e8752d44 | 388 | } |
| pythontech | 0:5868e8752d44 | 389 | |
| pythontech | 0:5868e8752d44 | 390 | void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 391 | // use REX prefix for 64 bit operation |
| pythontech | 0:5868e8752d44 | 392 | 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); |
| pythontech | 0:5868e8752d44 | 393 | asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); |
| pythontech | 0:5868e8752d44 | 394 | } |
| pythontech | 0:5868e8752d44 | 395 | |
| pythontech | 0:5868e8752d44 | 396 | STATIC void asm_x64_lea_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 397 | // use REX prefix for 64 bit operation |
| pythontech | 0:5868e8752d44 | 398 | assert(src_r64 < 8); |
| pythontech | 0:5868e8752d44 | 399 | assert(dest_r64 < 8); |
| pythontech | 0:5868e8752d44 | 400 | asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64); |
| pythontech | 0:5868e8752d44 | 401 | asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp); |
| pythontech | 0:5868e8752d44 | 402 | } |
| pythontech | 0:5868e8752d44 | 403 | |
| pythontech | 0:5868e8752d44 | 404 | /* |
| pythontech | 0:5868e8752d44 | 405 | void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 406 | assert(dest_r64 < 8); |
| pythontech | 0:5868e8752d44 | 407 | asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8); |
| pythontech | 0:5868e8752d44 | 408 | } |
| pythontech | 0:5868e8752d44 | 409 | */ |
| pythontech | 0:5868e8752d44 | 410 | |
| pythontech | 0:5868e8752d44 | 411 | STATIC void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 412 | // cpu defaults to i32 to r64, with zero extension |
| pythontech | 0:5868e8752d44 | 413 | if (dest_r64 < 8) { |
| pythontech | 0:5868e8752d44 | 414 | asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64); |
| pythontech | 0:5868e8752d44 | 415 | } else { |
| pythontech | 0:5868e8752d44 | 416 | asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7)); |
| pythontech | 0:5868e8752d44 | 417 | } |
| pythontech | 0:5868e8752d44 | 418 | asm_x64_write_word32(as, src_i32); |
| pythontech | 0:5868e8752d44 | 419 | } |
| pythontech | 0:5868e8752d44 | 420 | |
| pythontech | 0:5868e8752d44 | 421 | void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 422 | // cpu defaults to i32 to r64 |
| pythontech | 0:5868e8752d44 | 423 | // to mov i64 to r64 need to use REX prefix |
| pythontech | 0:5868e8752d44 | 424 | assert(dest_r64 < 8); |
| pythontech | 0:5868e8752d44 | 425 | asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64); |
| pythontech | 0:5868e8752d44 | 426 | asm_x64_write_word64(as, src_i64); |
| pythontech | 0:5868e8752d44 | 427 | } |
| pythontech | 0:5868e8752d44 | 428 | |
| pythontech | 0:5868e8752d44 | 429 | void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 430 | // TODO use movzx, movsx if possible |
| pythontech | 0:5868e8752d44 | 431 | if (UNSIGNED_FIT32(src_i64)) { |
| pythontech | 0:5868e8752d44 | 432 | // 5 bytes |
| pythontech | 0:5868e8752d44 | 433 | asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64); |
| pythontech | 0:5868e8752d44 | 434 | } else { |
| pythontech | 0:5868e8752d44 | 435 | // 10 bytes |
| pythontech | 0:5868e8752d44 | 436 | asm_x64_mov_i64_to_r64(as, src_i64, dest_r64); |
| pythontech | 0:5868e8752d44 | 437 | } |
| pythontech | 0:5868e8752d44 | 438 | } |
| pythontech | 0:5868e8752d44 | 439 | |
| pythontech | 0:5868e8752d44 | 440 | // src_i64 is stored as a full word in the code, and aligned to machine-word boundary |
| pythontech | 0:5868e8752d44 | 441 | void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 442 | // mov instruction uses 2 bytes for the instruction, before the i64 |
| pythontech | 0:5868e8752d44 | 443 | while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) { |
| pythontech | 0:5868e8752d44 | 444 | asm_x64_nop(as); |
| pythontech | 0:5868e8752d44 | 445 | } |
| pythontech | 0:5868e8752d44 | 446 | asm_x64_mov_i64_to_r64(as, src_i64, dest_r64); |
| pythontech | 0:5868e8752d44 | 447 | } |
| pythontech | 0:5868e8752d44 | 448 | |
| pythontech | 0:5868e8752d44 | 449 | void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) { |
| pythontech | 0:5868e8752d44 | 450 | asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_AND_R64_TO_RM64); |
| pythontech | 0:5868e8752d44 | 451 | } |
| pythontech | 0:5868e8752d44 | 452 | |
| pythontech | 0:5868e8752d44 | 453 | void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) { |
| pythontech | 0:5868e8752d44 | 454 | asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_OR_R64_TO_RM64); |
| pythontech | 0:5868e8752d44 | 455 | } |
| pythontech | 0:5868e8752d44 | 456 | |
| pythontech | 0:5868e8752d44 | 457 | void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) { |
| pythontech | 0:5868e8752d44 | 458 | asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_XOR_R64_TO_RM64); |
| pythontech | 0:5868e8752d44 | 459 | } |
| pythontech | 0:5868e8752d44 | 460 | |
| pythontech | 0:5868e8752d44 | 461 | void asm_x64_shl_r64_cl(asm_x64_t* as, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 462 | asm_x64_generic_r64_r64(as, dest_r64, 4, OPCODE_SHL_RM64_CL); |
| pythontech | 0:5868e8752d44 | 463 | } |
| pythontech | 0:5868e8752d44 | 464 | |
| pythontech | 0:5868e8752d44 | 465 | void asm_x64_sar_r64_cl(asm_x64_t* as, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 466 | asm_x64_generic_r64_r64(as, dest_r64, 7, OPCODE_SAR_RM64_CL); |
| pythontech | 0:5868e8752d44 | 467 | } |
| pythontech | 0:5868e8752d44 | 468 | |
| pythontech | 0:5868e8752d44 | 469 | void asm_x64_add_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) { |
| pythontech | 0:5868e8752d44 | 470 | asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_ADD_R64_TO_RM64); |
| pythontech | 0:5868e8752d44 | 471 | } |
| pythontech | 0:5868e8752d44 | 472 | |
| pythontech | 0:5868e8752d44 | 473 | void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) { |
| pythontech | 0:5868e8752d44 | 474 | asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_SUB_R64_FROM_RM64); |
| pythontech | 0:5868e8752d44 | 475 | } |
| pythontech | 0:5868e8752d44 | 476 | |
| pythontech | 0:5868e8752d44 | 477 | void asm_x64_mul_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) { |
| pythontech | 0:5868e8752d44 | 478 | // imul reg64, reg/mem64 -- 0x0f 0xaf /r |
| pythontech | 0:5868e8752d44 | 479 | asm_x64_write_byte_1(as, REX_PREFIX | REX_W | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64)); |
| pythontech | 0:5868e8752d44 | 480 | asm_x64_write_byte_3(as, 0x0f, 0xaf, MODRM_R64(dest_r64) | MODRM_RM_REG | MODRM_RM_R64(src_r64)); |
| pythontech | 0:5868e8752d44 | 481 | } |
| pythontech | 0:5868e8752d44 | 482 | |
| pythontech | 0:5868e8752d44 | 483 | /* |
| pythontech | 0:5868e8752d44 | 484 | void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) { |
| pythontech | 0:5868e8752d44 | 485 | if (SIGNED_FIT8(src_i32)) { |
| pythontech | 0:5868e8752d44 | 486 | // defaults to 32 bit operation |
| pythontech | 0:5868e8752d44 | 487 | asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32)); |
| pythontech | 0:5868e8752d44 | 488 | asm_x64_write_byte_1(as, src_i32 & 0xff); |
| pythontech | 0:5868e8752d44 | 489 | } else { |
| pythontech | 0:5868e8752d44 | 490 | // defaults to 32 bit operation |
| pythontech | 0:5868e8752d44 | 491 | asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32)); |
| pythontech | 0:5868e8752d44 | 492 | asm_x64_write_word32(as, src_i32); |
| pythontech | 0:5868e8752d44 | 493 | } |
| pythontech | 0:5868e8752d44 | 494 | } |
| pythontech | 0:5868e8752d44 | 495 | */ |
| pythontech | 0:5868e8752d44 | 496 | |
| pythontech | 0:5868e8752d44 | 497 | STATIC void asm_x64_sub_r64_i32(asm_x64_t *as, int dest_r64, int src_i32) { |
| pythontech | 0:5868e8752d44 | 498 | assert(dest_r64 < 8); |
| pythontech | 0:5868e8752d44 | 499 | if (SIGNED_FIT8(src_i32)) { |
| pythontech | 0:5868e8752d44 | 500 | // use REX prefix for 64 bit operation |
| pythontech | 0:5868e8752d44 | 501 | 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)); |
| pythontech | 0:5868e8752d44 | 502 | asm_x64_write_byte_1(as, src_i32 & 0xff); |
| pythontech | 0:5868e8752d44 | 503 | } else { |
| pythontech | 0:5868e8752d44 | 504 | // use REX prefix for 64 bit operation |
| pythontech | 0:5868e8752d44 | 505 | 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)); |
| pythontech | 0:5868e8752d44 | 506 | asm_x64_write_word32(as, src_i32); |
| pythontech | 0:5868e8752d44 | 507 | } |
| pythontech | 0:5868e8752d44 | 508 | } |
| pythontech | 0:5868e8752d44 | 509 | |
| pythontech | 0:5868e8752d44 | 510 | /* |
| pythontech | 0:5868e8752d44 | 511 | void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) { |
| pythontech | 0:5868e8752d44 | 512 | asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32)); |
| pythontech | 0:5868e8752d44 | 513 | asm_x64_write_byte_1(as, imm); |
| pythontech | 0:5868e8752d44 | 514 | } |
| pythontech | 0:5868e8752d44 | 515 | |
| pythontech | 0:5868e8752d44 | 516 | void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) { |
| pythontech | 0:5868e8752d44 | 517 | asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32)); |
| pythontech | 0:5868e8752d44 | 518 | asm_x64_write_byte_1(as, imm); |
| pythontech | 0:5868e8752d44 | 519 | } |
| pythontech | 0:5868e8752d44 | 520 | |
| pythontech | 0:5868e8752d44 | 521 | void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) { |
| pythontech | 0:5868e8752d44 | 522 | asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32)); |
| pythontech | 0:5868e8752d44 | 523 | asm_x64_write_byte_1(as, imm); |
| pythontech | 0:5868e8752d44 | 524 | } |
| pythontech | 0:5868e8752d44 | 525 | */ |
| pythontech | 0:5868e8752d44 | 526 | |
| pythontech | 0:5868e8752d44 | 527 | void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) { |
| pythontech | 0:5868e8752d44 | 528 | asm_x64_generic_r64_r64(as, src_r64_b, src_r64_a, OPCODE_CMP_R64_WITH_RM64); |
| pythontech | 0:5868e8752d44 | 529 | } |
| pythontech | 0:5868e8752d44 | 530 | |
| pythontech | 0:5868e8752d44 | 531 | /* |
| pythontech | 0:5868e8752d44 | 532 | void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) { |
| pythontech | 0:5868e8752d44 | 533 | if (SIGNED_FIT8(src_i32)) { |
| pythontech | 0:5868e8752d44 | 534 | asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32)); |
| pythontech | 0:5868e8752d44 | 535 | asm_x64_write_byte_1(as, src_i32 & 0xff); |
| pythontech | 0:5868e8752d44 | 536 | } else { |
| pythontech | 0:5868e8752d44 | 537 | asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32)); |
| pythontech | 0:5868e8752d44 | 538 | asm_x64_write_word32(as, src_i32); |
| pythontech | 0:5868e8752d44 | 539 | } |
| pythontech | 0:5868e8752d44 | 540 | } |
| pythontech | 0:5868e8752d44 | 541 | */ |
| pythontech | 0:5868e8752d44 | 542 | |
| pythontech | 0:5868e8752d44 | 543 | void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) { |
| pythontech | 0:5868e8752d44 | 544 | // TODO implement for other registers |
| pythontech | 0:5868e8752d44 | 545 | assert(src_r64_a == ASM_X64_REG_RAX); |
| pythontech | 0:5868e8752d44 | 546 | assert(src_r64_b == ASM_X64_REG_RAX); |
| pythontech | 0:5868e8752d44 | 547 | 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)); |
| pythontech | 0:5868e8752d44 | 548 | } |
| pythontech | 0:5868e8752d44 | 549 | |
| pythontech | 0:5868e8752d44 | 550 | void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) { |
| pythontech | 0:5868e8752d44 | 551 | assert(dest_r8 < 8); |
| pythontech | 0:5868e8752d44 | 552 | 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)); |
| pythontech | 0:5868e8752d44 | 553 | } |
| pythontech | 0:5868e8752d44 | 554 | |
| pythontech | 0:5868e8752d44 | 555 | void asm_x64_label_assign(asm_x64_t *as, mp_uint_t label) { |
| pythontech | 0:5868e8752d44 | 556 | assert(label < as->max_num_labels); |
| pythontech | 0:5868e8752d44 | 557 | if (as->pass < ASM_X64_PASS_EMIT) { |
| pythontech | 0:5868e8752d44 | 558 | // assign label offset |
| pythontech | 0:5868e8752d44 | 559 | assert(as->label_offsets[label] == (mp_uint_t)-1); |
| pythontech | 0:5868e8752d44 | 560 | as->label_offsets[label] = as->code_offset; |
| pythontech | 0:5868e8752d44 | 561 | } else { |
| pythontech | 0:5868e8752d44 | 562 | // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT |
| pythontech | 0:5868e8752d44 | 563 | //printf("l%d: (at %ld=%ld)\n", label, as->label_offsets[label], as->code_offset); |
| pythontech | 0:5868e8752d44 | 564 | assert(as->label_offsets[label] == as->code_offset); |
| pythontech | 0:5868e8752d44 | 565 | } |
| pythontech | 0:5868e8752d44 | 566 | } |
| pythontech | 0:5868e8752d44 | 567 | |
| pythontech | 0:5868e8752d44 | 568 | STATIC mp_uint_t get_label_dest(asm_x64_t *as, mp_uint_t label) { |
| pythontech | 0:5868e8752d44 | 569 | assert(label < as->max_num_labels); |
| pythontech | 0:5868e8752d44 | 570 | return as->label_offsets[label]; |
| pythontech | 0:5868e8752d44 | 571 | } |
| pythontech | 0:5868e8752d44 | 572 | |
| pythontech | 0:5868e8752d44 | 573 | void asm_x64_jmp_label(asm_x64_t *as, mp_uint_t label) { |
| pythontech | 0:5868e8752d44 | 574 | mp_uint_t dest = get_label_dest(as, label); |
| pythontech | 0:5868e8752d44 | 575 | mp_int_t rel = dest - as->code_offset; |
| pythontech | 0:5868e8752d44 | 576 | if (dest != (mp_uint_t)-1 && rel < 0) { |
| pythontech | 0:5868e8752d44 | 577 | // is a backwards jump, so we know the size of the jump on the first pass |
| pythontech | 0:5868e8752d44 | 578 | // calculate rel assuming 8 bit relative jump |
| pythontech | 0:5868e8752d44 | 579 | rel -= 2; |
| pythontech | 0:5868e8752d44 | 580 | if (SIGNED_FIT8(rel)) { |
| pythontech | 0:5868e8752d44 | 581 | asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff); |
| pythontech | 0:5868e8752d44 | 582 | } else { |
| pythontech | 0:5868e8752d44 | 583 | rel += 2; |
| pythontech | 0:5868e8752d44 | 584 | goto large_jump; |
| pythontech | 0:5868e8752d44 | 585 | } |
| pythontech | 0:5868e8752d44 | 586 | } else { |
| pythontech | 0:5868e8752d44 | 587 | // is a forwards jump, so need to assume it's large |
| pythontech | 0:5868e8752d44 | 588 | large_jump: |
| pythontech | 0:5868e8752d44 | 589 | rel -= 5; |
| pythontech | 0:5868e8752d44 | 590 | asm_x64_write_byte_1(as, OPCODE_JMP_REL32); |
| pythontech | 0:5868e8752d44 | 591 | asm_x64_write_word32(as, rel); |
| pythontech | 0:5868e8752d44 | 592 | } |
| pythontech | 0:5868e8752d44 | 593 | } |
| pythontech | 0:5868e8752d44 | 594 | |
| pythontech | 0:5868e8752d44 | 595 | void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, mp_uint_t label) { |
| pythontech | 0:5868e8752d44 | 596 | mp_uint_t dest = get_label_dest(as, label); |
| pythontech | 0:5868e8752d44 | 597 | mp_int_t rel = dest - as->code_offset; |
| pythontech | 0:5868e8752d44 | 598 | if (dest != (mp_uint_t)-1 && rel < 0) { |
| pythontech | 0:5868e8752d44 | 599 | // is a backwards jump, so we know the size of the jump on the first pass |
| pythontech | 0:5868e8752d44 | 600 | // calculate rel assuming 8 bit relative jump |
| pythontech | 0:5868e8752d44 | 601 | rel -= 2; |
| pythontech | 0:5868e8752d44 | 602 | if (SIGNED_FIT8(rel)) { |
| pythontech | 0:5868e8752d44 | 603 | asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff); |
| pythontech | 0:5868e8752d44 | 604 | } else { |
| pythontech | 0:5868e8752d44 | 605 | rel += 2; |
| pythontech | 0:5868e8752d44 | 606 | goto large_jump; |
| pythontech | 0:5868e8752d44 | 607 | } |
| pythontech | 0:5868e8752d44 | 608 | } else { |
| pythontech | 0:5868e8752d44 | 609 | // is a forwards jump, so need to assume it's large |
| pythontech | 0:5868e8752d44 | 610 | large_jump: |
| pythontech | 0:5868e8752d44 | 611 | rel -= 6; |
| pythontech | 0:5868e8752d44 | 612 | asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type); |
| pythontech | 0:5868e8752d44 | 613 | asm_x64_write_word32(as, rel); |
| pythontech | 0:5868e8752d44 | 614 | } |
| pythontech | 0:5868e8752d44 | 615 | } |
| pythontech | 0:5868e8752d44 | 616 | |
| pythontech | 0:5868e8752d44 | 617 | void asm_x64_entry(asm_x64_t *as, int num_locals) { |
| pythontech | 0:5868e8752d44 | 618 | asm_x64_push_r64(as, ASM_X64_REG_RBP); |
| pythontech | 0:5868e8752d44 | 619 | asm_x64_mov_r64_r64(as, ASM_X64_REG_RBP, ASM_X64_REG_RSP); |
| pythontech | 0:5868e8752d44 | 620 | if (num_locals < 0) { |
| pythontech | 0:5868e8752d44 | 621 | num_locals = 0; |
| pythontech | 0:5868e8752d44 | 622 | } |
| pythontech | 0:5868e8752d44 | 623 | num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary |
| pythontech | 0:5868e8752d44 | 624 | asm_x64_sub_r64_i32(as, ASM_X64_REG_RSP, num_locals * WORD_SIZE); |
| pythontech | 0:5868e8752d44 | 625 | asm_x64_push_r64(as, ASM_X64_REG_RBX); |
| pythontech | 0:5868e8752d44 | 626 | asm_x64_push_r64(as, ASM_X64_REG_R12); |
| pythontech | 0:5868e8752d44 | 627 | asm_x64_push_r64(as, ASM_X64_REG_R13); |
| pythontech | 0:5868e8752d44 | 628 | as->num_locals = num_locals; |
| pythontech | 0:5868e8752d44 | 629 | } |
| pythontech | 0:5868e8752d44 | 630 | |
| pythontech | 0:5868e8752d44 | 631 | void asm_x64_exit(asm_x64_t *as) { |
| pythontech | 0:5868e8752d44 | 632 | asm_x64_pop_r64(as, ASM_X64_REG_R13); |
| pythontech | 0:5868e8752d44 | 633 | asm_x64_pop_r64(as, ASM_X64_REG_R12); |
| pythontech | 0:5868e8752d44 | 634 | asm_x64_pop_r64(as, ASM_X64_REG_RBX); |
| pythontech | 0:5868e8752d44 | 635 | asm_x64_write_byte_1(as, OPCODE_LEAVE); |
| pythontech | 0:5868e8752d44 | 636 | asm_x64_ret(as); |
| pythontech | 0:5868e8752d44 | 637 | } |
| pythontech | 0:5868e8752d44 | 638 | |
| pythontech | 0:5868e8752d44 | 639 | // locals: |
| pythontech | 0:5868e8752d44 | 640 | // - stored on the stack in ascending order |
| pythontech | 0:5868e8752d44 | 641 | // - numbered 0 through as->num_locals-1 |
| pythontech | 0:5868e8752d44 | 642 | // - RBP points above the last local |
| pythontech | 0:5868e8752d44 | 643 | // |
| pythontech | 0:5868e8752d44 | 644 | // | RBP |
| pythontech | 0:5868e8752d44 | 645 | // v |
| pythontech | 0:5868e8752d44 | 646 | // l0 l1 l2 ... l(n-1) |
| pythontech | 0:5868e8752d44 | 647 | // ^ ^ |
| pythontech | 0:5868e8752d44 | 648 | // | low address | high address in RAM |
| pythontech | 0:5868e8752d44 | 649 | // |
| pythontech | 0:5868e8752d44 | 650 | STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) { |
| pythontech | 0:5868e8752d44 | 651 | return (-as->num_locals + local_num) * WORD_SIZE; |
| pythontech | 0:5868e8752d44 | 652 | } |
| pythontech | 0:5868e8752d44 | 653 | |
| pythontech | 0:5868e8752d44 | 654 | void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 655 | asm_x64_mov_mem64_to_r64(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, src_local_num), dest_r64); |
| pythontech | 0:5868e8752d44 | 656 | } |
| pythontech | 0:5868e8752d44 | 657 | |
| pythontech | 0:5868e8752d44 | 658 | void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) { |
| pythontech | 0:5868e8752d44 | 659 | asm_x64_mov_r64_to_mem64(as, src_r64, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, dest_local_num)); |
| pythontech | 0:5868e8752d44 | 660 | } |
| pythontech | 0:5868e8752d44 | 661 | |
| pythontech | 0:5868e8752d44 | 662 | void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) { |
| pythontech | 0:5868e8752d44 | 663 | int offset = asm_x64_local_offset_from_ebp(as, local_num); |
| pythontech | 0:5868e8752d44 | 664 | if (offset == 0) { |
| pythontech | 0:5868e8752d44 | 665 | asm_x64_mov_r64_r64(as, dest_r64, ASM_X64_REG_RBP); |
| pythontech | 0:5868e8752d44 | 666 | } else { |
| pythontech | 0:5868e8752d44 | 667 | asm_x64_lea_disp_to_r64(as, ASM_X64_REG_RBP, offset, dest_r64); |
| pythontech | 0:5868e8752d44 | 668 | } |
| pythontech | 0:5868e8752d44 | 669 | } |
| pythontech | 0:5868e8752d44 | 670 | |
| pythontech | 0:5868e8752d44 | 671 | /* |
| pythontech | 0:5868e8752d44 | 672 | void asm_x64_push_local(asm_x64_t *as, int local_num) { |
| pythontech | 0:5868e8752d44 | 673 | asm_x64_push_disp(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, local_num)); |
| pythontech | 0:5868e8752d44 | 674 | } |
| pythontech | 0:5868e8752d44 | 675 | |
| pythontech | 0:5868e8752d44 | 676 | void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64) { |
| pythontech | 0:5868e8752d44 | 677 | asm_x64_mov_r64_r64(as, temp_r64, ASM_X64_REG_RBP); |
| pythontech | 0:5868e8752d44 | 678 | asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64); |
| pythontech | 0:5868e8752d44 | 679 | asm_x64_push_r64(as, temp_r64); |
| pythontech | 0:5868e8752d44 | 680 | } |
| pythontech | 0:5868e8752d44 | 681 | */ |
| pythontech | 0:5868e8752d44 | 682 | |
| pythontech | 0:5868e8752d44 | 683 | /* |
| pythontech | 0:5868e8752d44 | 684 | can't use these because code might be relocated when resized |
| pythontech | 0:5868e8752d44 | 685 | |
| pythontech | 0:5868e8752d44 | 686 | void asm_x64_call(asm_x64_t *as, void* func) { |
| pythontech | 0:5868e8752d44 | 687 | asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP); |
| pythontech | 0:5868e8752d44 | 688 | asm_x64_write_byte_1(as, OPCODE_CALL_REL32); |
| pythontech | 0:5868e8752d44 | 689 | asm_x64_write_word32(as, func - (void*)(as->code_cur + 4)); |
| pythontech | 0:5868e8752d44 | 690 | asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP); |
| pythontech | 0:5868e8752d44 | 691 | } |
| pythontech | 0:5868e8752d44 | 692 | |
| pythontech | 0:5868e8752d44 | 693 | void asm_x64_call_i1(asm_x64_t *as, void* func, int i1) { |
| pythontech | 0:5868e8752d44 | 694 | asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP); |
| pythontech | 0:5868e8752d44 | 695 | asm_x64_sub_i32_from_r32(as, 12, ASM_X64_REG_RSP); |
| pythontech | 0:5868e8752d44 | 696 | asm_x64_push_i32(as, i1); |
| pythontech | 0:5868e8752d44 | 697 | asm_x64_write_byte_1(as, OPCODE_CALL_REL32); |
| pythontech | 0:5868e8752d44 | 698 | asm_x64_write_word32(as, func - (void*)(as->code_cur + 4)); |
| pythontech | 0:5868e8752d44 | 699 | asm_x64_add_i32_to_r32(as, 16, ASM_X64_REG_RSP); |
| pythontech | 0:5868e8752d44 | 700 | asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP); |
| pythontech | 0:5868e8752d44 | 701 | } |
| pythontech | 0:5868e8752d44 | 702 | */ |
| pythontech | 0:5868e8752d44 | 703 | |
| pythontech | 0:5868e8752d44 | 704 | void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) { |
| pythontech | 0:5868e8752d44 | 705 | assert(temp_r64 < 8); |
| pythontech | 0:5868e8752d44 | 706 | #ifdef __LP64__ |
| pythontech | 0:5868e8752d44 | 707 | asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64); |
| pythontech | 0:5868e8752d44 | 708 | #else |
| pythontech | 0:5868e8752d44 | 709 | // If we get here, sizeof(int) == sizeof(void*). |
| pythontech | 0:5868e8752d44 | 710 | asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64); |
| pythontech | 0:5868e8752d44 | 711 | #endif |
| pythontech | 0:5868e8752d44 | 712 | asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64)); |
| pythontech | 0:5868e8752d44 | 713 | // this reduces code size by 2 bytes per call, but doesn't seem to speed it up at all |
| pythontech | 0:5868e8752d44 | 714 | // doesn't work anymore because calls are 64 bits away |
| pythontech | 0:5868e8752d44 | 715 | /* |
| pythontech | 0:5868e8752d44 | 716 | asm_x64_write_byte_1(as, OPCODE_CALL_REL32); |
| pythontech | 0:5868e8752d44 | 717 | asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4)); |
| pythontech | 0:5868e8752d44 | 718 | */ |
| pythontech | 0:5868e8752d44 | 719 | } |
| pythontech | 0:5868e8752d44 | 720 | |
| pythontech | 0:5868e8752d44 | 721 | #endif // MICROPY_EMIT_X64 |