blob: f6515feff64adc2388cca4c52ce9b7748f6e9ffa [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python 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
Markus Siemens19ccc6b2014-01-27 22:53:28 +010027#include <stdint.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdio.h>
29#include <assert.h>
Damien429d7192013-10-04 19:53:11 +010030#include <string.h>
31
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/mpconfig.h"
Damien Georgee67ed5d2014-01-04 13:55:24 +000033
34// wrapper around everything in this file
35#if MICROPY_EMIT_X64
Damien429d7192013-10-04 19:53:11 +010036
Damien George51dfcb42015-01-01 20:27:54 +000037#include "py/asmx64.h"
Damien Georged3ebe482014-01-07 15:20:33 +000038
Damien429d7192013-10-04 19:53:11 +010039/* all offsets are measured in multiples of 8 bytes */
40#define WORD_SIZE (8)
41
42#define OPCODE_NOP (0x90)
Damien George81057362014-09-07 01:06:19 +010043#define OPCODE_PUSH_R64 (0x50) /* +rq */
Damien429d7192013-10-04 19:53:11 +010044#define OPCODE_PUSH_I64 (0x68)
45#define OPCODE_PUSH_M64 (0xff) /* /6 */
Damien George81057362014-09-07 01:06:19 +010046#define OPCODE_POP_R64 (0x58) /* +rq */
Damien429d7192013-10-04 19:53:11 +010047#define OPCODE_RET (0xc3)
48#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
Damien George81057362014-09-07 01:06:19 +010049#define OPCODE_MOV_I64_TO_R64 (0xb8) /* +rq */
Damien429d7192013-10-04 19:53:11 +010050#define OPCODE_MOV_I32_TO_RM32 (0xc7)
Damien Georged66e4862014-09-29 10:13:49 +010051#define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
Damien George81057362014-09-07 01:06:19 +010052#define OPCODE_MOV_R64_TO_RM64 (0x89) /* /r */
Damien George91cfd412014-10-12 16:59:29 +010053#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 */
Damien429d7192013-10-04 19:53:11 +010056#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
Damien George1ef23482014-10-12 14:21:06 +010057#define OPCODE_AND_R64_TO_RM64 (0x21) /* /r */
58#define OPCODE_OR_R64_TO_RM64 (0x09) /* /r */
Damien429d7192013-10-04 19:53:11 +010059#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
Damien George3112cde2014-09-29 18:45:42 +010060#define OPCODE_ADD_R64_TO_RM64 (0x01) /* /r */
Damien429d7192013-10-04 19:53:11 +010061#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
62#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
63#define OPCODE_SUB_R64_FROM_RM64 (0x29)
64#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
65#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
Damien George3112cde2014-09-29 18:45:42 +010066//#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
67//#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
68//#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
69#define OPCODE_SHL_RM64_CL (0xd3) /* /4 */
70#define OPCODE_SAR_RM64_CL (0xd3) /* /7 */
71//#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
72//#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
73#define OPCODE_CMP_R64_WITH_RM64 (0x39) /* /r */
74//#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
Damien429d7192013-10-04 19:53:11 +010075#define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
76#define OPCODE_JMP_REL8 (0xeb)
77#define OPCODE_JMP_REL32 (0xe9)
78#define OPCODE_JCC_REL8 (0x70) /* | jcc type */
79#define OPCODE_JCC_REL32_A (0x0f)
80#define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
81#define OPCODE_SETCC_RM8_A (0x0f)
82#define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
83#define OPCODE_CALL_REL32 (0xe8)
84#define OPCODE_CALL_RM32 (0xff) /* /2 */
85#define OPCODE_LEAVE (0xc9)
86
Damien George81057362014-09-07 01:06:19 +010087#define MODRM_R64(x) (((x) & 0x7) << 3)
Damien429d7192013-10-04 19:53:11 +010088#define MODRM_RM_DISP0 (0x00)
89#define MODRM_RM_DISP8 (0x40)
90#define MODRM_RM_DISP32 (0x80)
91#define MODRM_RM_REG (0xc0)
Damien George81057362014-09-07 01:06:19 +010092#define MODRM_RM_R64(x) ((x) & 0x7)
Damien429d7192013-10-04 19:53:11 +010093
Damien Georged66e4862014-09-29 10:13:49 +010094#define OP_SIZE_PREFIX (0x66)
95
Damien429d7192013-10-04 19:53:11 +010096#define REX_PREFIX (0x40)
97#define REX_W (0x08) // width
98#define REX_R (0x04) // register
99#define REX_X (0x02) // index
100#define REX_B (0x01) // base
Damien George46fc7a32016-02-15 09:02:13 +0000101#define REX_W_FROM_R64(r64) ((r64) >> 0 & 0x08)
102#define REX_R_FROM_R64(r64) ((r64) >> 1 & 0x04)
103#define REX_X_FROM_R64(r64) ((r64) >> 2 & 0x02)
104#define REX_B_FROM_R64(r64) ((r64) >> 3 & 0x01)
Damien429d7192013-10-04 19:53:11 +0100105
106#define IMM32_L0(x) ((x) & 0xff)
107#define IMM32_L1(x) (((x) >> 8) & 0xff)
108#define IMM32_L2(x) (((x) >> 16) & 0xff)
109#define IMM32_L3(x) (((x) >> 24) & 0xff)
110#define IMM64_L4(x) (((x) >> 32) & 0xff)
111#define IMM64_L5(x) (((x) >> 40) & 0xff)
112#define IMM64_L6(x) (((x) >> 48) & 0xff)
113#define IMM64_L7(x) (((x) >> 56) & 0xff)
114
115#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
116#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
117#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
118
119struct _asm_x64_t {
Damien George36db6bc2014-05-07 17:24:22 +0100120 uint pass;
Fabian Vogtb7235b82014-09-03 16:59:33 +0200121 mp_uint_t code_offset;
122 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +0100123 byte *code_base;
124 byte dummy_data[8];
125
Damien George0b610de2014-09-29 16:25:04 +0100126 mp_uint_t max_num_labels;
127 mp_uint_t *label_offsets;
Damien Georgecd82e022014-02-02 13:11:48 +0000128 int num_locals;
Damien429d7192013-10-04 19:53:11 +0100129};
130
Damien George0b610de2014-09-29 16:25:04 +0100131asm_x64_t *asm_x64_new(mp_uint_t max_num_labels) {
Damien Georgecd82e022014-02-02 13:11:48 +0000132 asm_x64_t *as;
Damien429d7192013-10-04 19:53:11 +0100133
Damien George36db6bc2014-05-07 17:24:22 +0100134 as = m_new0(asm_x64_t, 1);
Damien054848a2013-10-05 13:44:41 +0100135 as->max_num_labels = max_num_labels;
Damien George0b610de2014-09-29 16:25:04 +0100136 as->label_offsets = m_new(mp_uint_t, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100137
138 return as;
139}
140
Damien Georgecd82e022014-02-02 13:11:48 +0000141void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100142 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200143 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +0100144 }
Damien George0b610de2014-09-29 16:25:04 +0100145 m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
Damien732407f2013-12-29 19:33:23 +0000146 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100147}
148
Damien George36db6bc2014-05-07 17:24:22 +0100149void asm_x64_start_pass(asm_x64_t *as, uint pass) {
Damien George36db6bc2014-05-07 17:24:22 +0100150 if (pass == ASM_X64_PASS_COMPUTE) {
Damien054848a2013-10-05 13:44:41 +0100151 // reset all labels
Damien George0b610de2014-09-29 16:25:04 +0100152 memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
Damien Georged9dc6ff2015-01-14 00:38:33 +0000153 } if (pass == ASM_X64_PASS_EMIT) {
154 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
155 if (as->code_base == NULL) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200156 assert(0);
157 }
Damien Georgee2e3d112014-01-06 22:13:00 +0000158 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100159 }
Damien Georged9dc6ff2015-01-14 00:38:33 +0000160 as->pass = pass;
161 as->code_offset = 0;
162}
Damien429d7192013-10-04 19:53:11 +0100163
Damien Georged9dc6ff2015-01-14 00:38:33 +0000164void asm_x64_end_pass(asm_x64_t *as) {
165 // could check labels are resolved...
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000166 (void)as;
Damien429d7192013-10-04 19:53:11 +0100167}
168
169// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200170STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100171 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100172 if (as->pass < ASM_X64_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100173 as->code_offset += num_bytes_to_write;
174 return as->dummy_data;
175 } else {
176 assert(as->code_offset + num_bytes_to_write <= as->code_size);
177 byte *c = as->code_base + as->code_offset;
178 as->code_offset += num_bytes_to_write;
179 return c;
180 }
181}
182
Damien George99886182015-04-06 22:38:53 +0100183mp_uint_t asm_x64_get_code_pos(asm_x64_t *as) {
184 return as->code_offset;
185}
186
Damien George0b610de2014-09-29 16:25:04 +0100187mp_uint_t asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100188 return as->code_size;
189}
190
Damien Georgecd82e022014-02-02 13:11:48 +0000191void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100192 return as->code_base;
193}
194
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200195STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100196 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
197 c[0] = b1;
198}
199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100201 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
202 c[0] = b1;
203 c[1] = b2;
204}
205
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200206STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100207 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
208 c[0] = b1;
209 c[1] = b2;
210 c[2] = b3;
211}
212
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200213STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100214 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
215 c[0] = IMM32_L0(w32);
216 c[1] = IMM32_L1(w32);
217 c[2] = IMM32_L2(w32);
218 c[3] = IMM32_L3(w32);
219}
220
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200221STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100222 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
223 c[0] = IMM32_L0(w64);
224 c[1] = IMM32_L1(w64);
225 c[2] = IMM32_L2(w64);
226 c[3] = IMM32_L3(w64);
227 c[4] = IMM64_L4(w64);
228 c[5] = IMM64_L5(w64);
229 c[6] = IMM64_L6(w64);
230 c[7] = IMM64_L7(w64);
231}
232
Damien George99886182015-04-06 22:38:53 +0100233// align must be a multiple of 2
234void asm_x64_align(asm_x64_t* as, mp_uint_t align) {
235 // TODO fill unused data with NOPs?
236 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
237}
238
239void asm_x64_data(asm_x64_t* as, mp_uint_t bytesize, mp_uint_t val) {
240 byte *c = asm_x64_get_cur_to_write_bytes(as, bytesize);
241 // machine is little endian
242 for (uint i = 0; i < bytesize; i++) {
243 *c++ = val;
244 val >>= 8;
245 }
246}
247
Damien429d7192013-10-04 19:53:11 +0100248/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200249STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100250 byte* c;
251 assert(offset + 4 <= as->code_size);
252 c = as->code_base + offset;
253 c[0] = IMM32_L0(w32);
254 c[1] = IMM32_L1(w32);
255 c[2] = IMM32_L2(w32);
256 c[3] = IMM32_L3(w32);
257}
258*/
259
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200260STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
Damien George0b610de2014-09-29 16:25:04 +0100261 assert(disp_r64 != ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100262
Damien George4cd9ced2015-01-15 14:41:41 +0000263 if (disp_r64 == ASM_X64_REG_R12) {
264 // special case for r12; not fully implemented
265 assert(SIGNED_FIT8(disp_offset));
266 asm_x64_write_byte_3(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), 0x24, IMM32_L0(disp_offset));
267 return;
268 }
269
Damien George0b610de2014-09-29 16:25:04 +0100270 if (disp_offset == 0 && disp_r64 != ASM_X64_REG_RBP) {
Damien429d7192013-10-04 19:53:11 +0100271 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
272 } else if (SIGNED_FIT8(disp_offset)) {
273 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
274 } else {
275 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
276 asm_x64_write_word32(as, disp_offset);
277 }
278}
279
Damien George3112cde2014-09-29 18:45:42 +0100280STATIC void asm_x64_generic_r64_r64(asm_x64_t *as, int dest_r64, int src_r64, int op) {
Damien George46fc7a32016-02-15 09:02:13 +0000281 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));
Damien George3112cde2014-09-29 18:45:42 +0100282}
283
Damien Georgecd82e022014-02-02 13:11:48 +0000284void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100285 asm_x64_write_byte_1(as, OPCODE_NOP);
286}
287
Damien Georgecd82e022014-02-02 13:11:48 +0000288void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien George81057362014-09-07 01:06:19 +0100289 if (src_r64 < 8) {
290 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
291 } else {
292 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_PUSH_R64 | (src_r64 & 7));
293 }
Damien429d7192013-10-04 19:53:11 +0100294}
295
Damien George81057362014-09-07 01:06:19 +0100296/*
Damien Georgecd82e022014-02-02 13:11:48 +0000297void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100298 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
299 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
300}
Damien George81057362014-09-07 01:06:19 +0100301*/
Damien429d7192013-10-04 19:53:11 +0100302
Damien George3dd1c0a2015-01-12 22:22:46 +0000303/*
Damien Georgecd82e022014-02-02 13:11:48 +0000304void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien George81057362014-09-07 01:06:19 +0100305 assert(src_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100306 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
307 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
308}
Damien George3dd1c0a2015-01-12 22:22:46 +0000309*/
Damien429d7192013-10-04 19:53:11 +0100310
Damien Georgecd82e022014-02-02 13:11:48 +0000311void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100312 if (dest_r64 < 8) {
313 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
314 } else {
315 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_POP_R64 | (dest_r64 & 7));
316 }
Damien429d7192013-10-04 19:53:11 +0100317}
318
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200319STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100320 asm_x64_write_byte_1(as, OPCODE_RET);
321}
322
Damien George3112cde2014-09-29 18:45:42 +0100323void asm_x64_mov_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
324 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_MOV_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100325}
326
Damien George91cfd412014-10-12 16:59:29 +0100327void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
Damien Georged66e4862014-09-29 10:13:49 +0100328 assert(dest_r64 < 8);
329 if (src_r64 < 8) {
330 asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
331 } else {
332 asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_R8_TO_RM8);
333 }
334 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
335}
336
Damien George91cfd412014-10-12 16:59:29 +0100337void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
Damien Georged66e4862014-09-29 10:13:49 +0100338 assert(dest_r64 < 8);
Damien George1c6a1dc2014-09-29 22:44:18 +0100339 if (src_r64 < 8) {
340 asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64);
341 } else {
342 asm_x64_write_byte_3(as, OP_SIZE_PREFIX, REX_PREFIX | REX_R, OPCODE_MOV_R64_TO_RM64);
343 }
Damien Georged66e4862014-09-29 10:13:49 +0100344 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
345}
346
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100347void asm_x64_mov_r32_to_mem32(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
348 assert(dest_r64 < 8);
349 if (src_r64 < 8) {
350 asm_x64_write_byte_1(as, OPCODE_MOV_R64_TO_RM64);
351 } else {
352 asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_R64_TO_RM64);
353 }
354 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
355}
356
Damien George91cfd412014-10-12 16:59:29 +0100357void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
Damien429d7192013-10-04 19:53:11 +0100358 // use REX prefix for 64 bit operation
Damien George46fc7a32016-02-15 09:02:13 +0000359 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);
Damien429d7192013-10-04 19:53:11 +0100360 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
361}
362
Damien George91cfd412014-10-12 16:59:29 +0100363void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
364 assert(src_r64 < 8);
365 if (dest_r64 < 8) {
366 asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R64);
367 } else {
368 asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM8_TO_R64);
369 }
370 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
371}
372
373void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
374 assert(src_r64 < 8);
375 if (dest_r64 < 8) {
376 asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R64);
377 } else {
378 asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM16_TO_R64);
379 }
380 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
381}
382
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100383void asm_x64_mov_mem32_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
384 assert(src_r64 < 8);
385 if (dest_r64 < 8) {
386 asm_x64_write_byte_1(as, OPCODE_MOV_RM64_TO_R64);
387 } else {
388 asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_RM64_TO_R64);
389 }
390 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
391}
392
Damien George91cfd412014-10-12 16:59:29 +0100393void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100394 // use REX prefix for 64 bit operation
Damien George46fc7a32016-02-15 09:02:13 +0000395 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);
Damien429d7192013-10-04 19:53:11 +0100396 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
397}
398
Damien George3dd1c0a2015-01-12 22:22:46 +0000399STATIC void asm_x64_lea_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100400 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100401 assert(src_r64 < 8);
402 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100403 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
404 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
405}
406
Damien George3dd1c0a2015-01-12 22:22:46 +0000407/*
Damien429d7192013-10-04 19:53:11 +0100408void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100409 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100410 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
411}
Damien George3dd1c0a2015-01-12 22:22:46 +0000412*/
Damien429d7192013-10-04 19:53:11 +0100413
Damien George81057362014-09-07 01:06:19 +0100414STATIC void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100415 // cpu defaults to i32 to r64, with zero extension
Damien George81057362014-09-07 01:06:19 +0100416 if (dest_r64 < 8) {
417 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
418 } else {
419 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7));
420 }
Damien429d7192013-10-04 19:53:11 +0100421 asm_x64_write_word32(as, src_i32);
422}
423
Damien Georgecd82e022014-02-02 13:11:48 +0000424void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100425 // cpu defaults to i32 to r64
426 // to mov i64 to r64 need to use REX prefix
Damien George81057362014-09-07 01:06:19 +0100427 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100428 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
429 asm_x64_write_word64(as, src_i64);
430}
431
432void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien Georged66e4862014-09-29 10:13:49 +0100433 // TODO use movzx, movsx if possible
Damien429d7192013-10-04 19:53:11 +0100434 if (UNSIGNED_FIT32(src_i64)) {
435 // 5 bytes
436 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
437 } else {
438 // 10 bytes
439 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
440 }
441}
442
Damien George36db6bc2014-05-07 17:24:22 +0100443// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
444void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
445 // mov instruction uses 2 bytes for the instruction, before the i64
446 while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
447 asm_x64_nop(as);
448 }
449 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
450}
451
Damien George1ef23482014-10-12 14:21:06 +0100452void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
453 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_AND_R64_TO_RM64);
454}
455
456void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
457 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_OR_R64_TO_RM64);
458}
459
Damien George3112cde2014-09-29 18:45:42 +0100460void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
461 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_XOR_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100462}
463
Damien George3112cde2014-09-29 18:45:42 +0100464void asm_x64_shl_r64_cl(asm_x64_t* as, int dest_r64) {
465 asm_x64_generic_r64_r64(as, dest_r64, 4, OPCODE_SHL_RM64_CL);
Damien429d7192013-10-04 19:53:11 +0100466}
467
Damien George3112cde2014-09-29 18:45:42 +0100468void asm_x64_sar_r64_cl(asm_x64_t* as, int dest_r64) {
469 asm_x64_generic_r64_r64(as, dest_r64, 7, OPCODE_SAR_RM64_CL);
Damien429d7192013-10-04 19:53:11 +0100470}
471
Damien George3112cde2014-09-29 18:45:42 +0100472void asm_x64_add_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
473 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_ADD_R64_TO_RM64);
474}
475
476void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
477 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_SUB_R64_FROM_RM64);
Damien429d7192013-10-04 19:53:11 +0100478}
479
Damien George567b3492015-06-04 14:00:29 +0000480void asm_x64_mul_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
481 // imul reg64, reg/mem64 -- 0x0f 0xaf /r
Damien George46fc7a32016-02-15 09:02:13 +0000482 asm_x64_write_byte_1(as, REX_PREFIX | REX_W | REX_R_FROM_R64(dest_r64) | REX_B_FROM_R64(src_r64));
Damien George567b3492015-06-04 14:00:29 +0000483 asm_x64_write_byte_3(as, 0x0f, 0xaf, MODRM_R64(dest_r64) | MODRM_RM_REG | MODRM_RM_R64(src_r64));
484}
485
Damien George81057362014-09-07 01:06:19 +0100486/*
Damien Georgecd82e022014-02-02 13:11:48 +0000487void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100488 if (SIGNED_FIT8(src_i32)) {
489 // defaults to 32 bit operation
490 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
491 asm_x64_write_byte_1(as, src_i32 & 0xff);
492 } else {
493 // defaults to 32 bit operation
494 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
495 asm_x64_write_word32(as, src_i32);
496 }
497}
Damien George81057362014-09-07 01:06:19 +0100498*/
Damien429d7192013-10-04 19:53:11 +0100499
Damien George3112cde2014-09-29 18:45:42 +0100500STATIC void asm_x64_sub_r64_i32(asm_x64_t *as, int dest_r64, int src_i32) {
Damien George81057362014-09-07 01:06:19 +0100501 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100502 if (SIGNED_FIT8(src_i32)) {
503 // use REX prefix for 64 bit operation
504 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));
505 asm_x64_write_byte_1(as, src_i32 & 0xff);
506 } else {
507 // use REX prefix for 64 bit operation
508 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));
509 asm_x64_write_word32(as, src_i32);
510 }
511}
512
Damien George81057362014-09-07 01:06:19 +0100513/*
Damien Georgecd82e022014-02-02 13:11:48 +0000514void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100515 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
516 asm_x64_write_byte_1(as, imm);
517}
518
Damien Georgecd82e022014-02-02 13:11:48 +0000519void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100520 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
521 asm_x64_write_byte_1(as, imm);
522}
523
Damien Georgecd82e022014-02-02 13:11:48 +0000524void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100525 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
526 asm_x64_write_byte_1(as, imm);
527}
Damien George81057362014-09-07 01:06:19 +0100528*/
Damien429d7192013-10-04 19:53:11 +0100529
Damien Georgecd82e022014-02-02 13:11:48 +0000530void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien George3112cde2014-09-29 18:45:42 +0100531 asm_x64_generic_r64_r64(as, src_r64_b, src_r64_a, OPCODE_CMP_R64_WITH_RM64);
Damien429d7192013-10-04 19:53:11 +0100532}
533
Damien George81057362014-09-07 01:06:19 +0100534/*
Damien Georgecd82e022014-02-02 13:11:48 +0000535void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100536 if (SIGNED_FIT8(src_i32)) {
537 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
538 asm_x64_write_byte_1(as, src_i32 & 0xff);
539 } else {
540 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
541 asm_x64_write_word32(as, src_i32);
542 }
543}
Damien George81057362014-09-07 01:06:19 +0100544*/
Damien429d7192013-10-04 19:53:11 +0100545
Damien Georgecd82e022014-02-02 13:11:48 +0000546void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100547 // TODO implement for other registers
Damien George0b610de2014-09-29 16:25:04 +0100548 assert(src_r64_a == ASM_X64_REG_RAX);
549 assert(src_r64_b == ASM_X64_REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100550 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));
551}
552
Damien Georgecd82e022014-02-02 13:11:48 +0000553void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien George81057362014-09-07 01:06:19 +0100554 assert(dest_r8 < 8);
Damien429d7192013-10-04 19:53:11 +0100555 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));
556}
557
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000558void asm_x64_label_assign(asm_x64_t *as, mp_uint_t label) {
Damien054848a2013-10-05 13:44:41 +0100559 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100560 if (as->pass < ASM_X64_PASS_EMIT) {
Damien054848a2013-10-05 13:44:41 +0100561 // assign label offset
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000562 assert(as->label_offsets[label] == (mp_uint_t)-1);
Damien054848a2013-10-05 13:44:41 +0100563 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100564 } else {
565 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien George0b610de2014-09-29 16:25:04 +0100566 //printf("l%d: (at %ld=%ld)\n", label, as->label_offsets[label], as->code_offset);
Damien054848a2013-10-05 13:44:41 +0100567 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100568 }
569}
570
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000571STATIC mp_uint_t get_label_dest(asm_x64_t *as, mp_uint_t label) {
Damien054848a2013-10-05 13:44:41 +0100572 assert(label < as->max_num_labels);
573 return as->label_offsets[label];
574}
575
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000576void asm_x64_jmp_label(asm_x64_t *as, mp_uint_t label) {
Damien George0b610de2014-09-29 16:25:04 +0100577 mp_uint_t dest = get_label_dest(as, label);
578 mp_int_t rel = dest - as->code_offset;
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000579 if (dest != (mp_uint_t)-1 && rel < 0) {
Damien054848a2013-10-05 13:44:41 +0100580 // is a backwards jump, so we know the size of the jump on the first pass
581 // calculate rel assuming 8 bit relative jump
582 rel -= 2;
583 if (SIGNED_FIT8(rel)) {
584 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100585 } else {
Damien054848a2013-10-05 13:44:41 +0100586 rel += 2;
587 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100588 }
Damien054848a2013-10-05 13:44:41 +0100589 } else {
590 // is a forwards jump, so need to assume it's large
591 large_jump:
592 rel -= 5;
593 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
594 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100595 }
596}
597
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000598void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, mp_uint_t label) {
Damien George0b610de2014-09-29 16:25:04 +0100599 mp_uint_t dest = get_label_dest(as, label);
600 mp_int_t rel = dest - as->code_offset;
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000601 if (dest != (mp_uint_t)-1 && rel < 0) {
Damien054848a2013-10-05 13:44:41 +0100602 // is a backwards jump, so we know the size of the jump on the first pass
603 // calculate rel assuming 8 bit relative jump
604 rel -= 2;
605 if (SIGNED_FIT8(rel)) {
606 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100607 } else {
Damien054848a2013-10-05 13:44:41 +0100608 rel += 2;
609 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100610 }
Damien054848a2013-10-05 13:44:41 +0100611 } else {
612 // is a forwards jump, so need to assume it's large
613 large_jump:
614 rel -= 6;
615 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
616 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100617 }
618}
619
Damien Georgecd82e022014-02-02 13:11:48 +0000620void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien George0b610de2014-09-29 16:25:04 +0100621 asm_x64_push_r64(as, ASM_X64_REG_RBP);
Damien George3112cde2014-09-29 18:45:42 +0100622 asm_x64_mov_r64_r64(as, ASM_X64_REG_RBP, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100623 if (num_locals < 0) {
624 num_locals = 0;
625 }
626 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
Damien George3112cde2014-09-29 18:45:42 +0100627 asm_x64_sub_r64_i32(as, ASM_X64_REG_RSP, num_locals * WORD_SIZE);
Damien George0b610de2014-09-29 16:25:04 +0100628 asm_x64_push_r64(as, ASM_X64_REG_RBX);
629 asm_x64_push_r64(as, ASM_X64_REG_R12);
630 asm_x64_push_r64(as, ASM_X64_REG_R13);
Damien Georgecd82e022014-02-02 13:11:48 +0000631 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100632}
633
Damien Georgecd82e022014-02-02 13:11:48 +0000634void asm_x64_exit(asm_x64_t *as) {
Damien George0b610de2014-09-29 16:25:04 +0100635 asm_x64_pop_r64(as, ASM_X64_REG_R13);
636 asm_x64_pop_r64(as, ASM_X64_REG_R12);
637 asm_x64_pop_r64(as, ASM_X64_REG_RBX);
Damien429d7192013-10-04 19:53:11 +0100638 asm_x64_write_byte_1(as, OPCODE_LEAVE);
639 asm_x64_ret(as);
640}
641
Damien Georgecd82e022014-02-02 13:11:48 +0000642// locals:
643// - stored on the stack in ascending order
644// - numbered 0 through as->num_locals-1
645// - RBP points above the last local
646//
Paul Sokolovsky089c3f32015-02-14 02:20:06 +0800647// | RBP
Damien Georgecd82e022014-02-02 13:11:48 +0000648// v
649// l0 l1 l2 ... l(n-1)
650// ^ ^
651// | low address | high address in RAM
652//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200653STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000654 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100655}
656
Damien Georgecd82e022014-02-02 13:11:48 +0000657void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
Damien George91cfd412014-10-12 16:59:29 +0100658 asm_x64_mov_mem64_to_r64(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, src_local_num), dest_r64);
Damien429d7192013-10-04 19:53:11 +0100659}
660
Damien Georgecd82e022014-02-02 13:11:48 +0000661void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
Damien George91cfd412014-10-12 16:59:29 +0100662 asm_x64_mov_r64_to_mem64(as, src_r64, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, dest_local_num));
Damien429d7192013-10-04 19:53:11 +0100663}
664
Damien Georgecd82e022014-02-02 13:11:48 +0000665void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
666 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100667 if (offset == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100668 asm_x64_mov_r64_r64(as, dest_r64, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100669 } else {
Damien George0b610de2014-09-29 16:25:04 +0100670 asm_x64_lea_disp_to_r64(as, ASM_X64_REG_RBP, offset, dest_r64);
Damien429d7192013-10-04 19:53:11 +0100671 }
672}
673
Damien George81057362014-09-07 01:06:19 +0100674/*
Damien Georgecd82e022014-02-02 13:11:48 +0000675void asm_x64_push_local(asm_x64_t *as, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100676 asm_x64_push_disp(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100677}
678
Damien George4dea9222015-04-09 15:29:54 +0000679void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64) {
Damien George3112cde2014-09-29 18:45:42 +0100680 asm_x64_mov_r64_r64(as, temp_r64, ASM_X64_REG_RBP);
Damien Georgecd82e022014-02-02 13:11:48 +0000681 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100682 asm_x64_push_r64(as, temp_r64);
683}
Damien George81057362014-09-07 01:06:19 +0100684*/
Damien429d7192013-10-04 19:53:11 +0100685
686/*
687 can't use these because code might be relocated when resized
688
Damien George4dea9222015-04-09 15:29:54 +0000689void asm_x64_call(asm_x64_t *as, void* func) {
Damien George0b610de2014-09-29 16:25:04 +0100690 asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100691 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
692 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
Damien George3112cde2014-09-29 18:45:42 +0100693 asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100694}
695
Damien George4dea9222015-04-09 15:29:54 +0000696void asm_x64_call_i1(asm_x64_t *as, void* func, int i1) {
Damien George0b610de2014-09-29 16:25:04 +0100697 asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
698 asm_x64_sub_i32_from_r32(as, 12, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100699 asm_x64_push_i32(as, i1);
700 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
701 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
Damien George0b610de2014-09-29 16:25:04 +0100702 asm_x64_add_i32_to_r32(as, 16, ASM_X64_REG_RSP);
Damien George3112cde2014-09-29 18:45:42 +0100703 asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100704}
705*/
706
Damien Georgecd82e022014-02-02 13:11:48 +0000707void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George81057362014-09-07 01:06:19 +0100708 assert(temp_r64 < 8);
Damien George212c2962013-12-30 12:52:32 +0000709#ifdef __LP64__
710 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
711#else
712 // If we get here, sizeof(int) == sizeof(void*).
713 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
714#endif
Damien429d7192013-10-04 19:53:11 +0100715 asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64));
Damien429d7192013-10-04 19:53:11 +0100716 // this reduces code size by 2 bytes per call, but doesn't seem to speed it up at all
Damien415eb6f2013-10-05 12:19:06 +0100717 // doesn't work anymore because calls are 64 bits away
718 /*
Damien429d7192013-10-04 19:53:11 +0100719 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
720 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100721 */
Damien429d7192013-10-04 19:53:11 +0100722}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000723
724#endif // MICROPY_EMIT_X64