blob: 54f541ffb94f62758e92a842cd8247256d5b59e0 [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 Georgee67ed5d2014-01-04 13:55:24 +000032#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030033#include "misc.h"
Damien Georgee67ed5d2014-01-04 13:55:24 +000034
35// wrapper around everything in this file
36#if MICROPY_EMIT_X64
Damien429d7192013-10-04 19:53:11 +010037
Damien Georged3ebe482014-01-07 15:20:33 +000038#include "asmx64.h"
39
Damien429d7192013-10-04 19:53:11 +010040/* all offsets are measured in multiples of 8 bytes */
41#define WORD_SIZE (8)
42
43#define OPCODE_NOP (0x90)
Damien George81057362014-09-07 01:06:19 +010044#define OPCODE_PUSH_R64 (0x50) /* +rq */
Damien429d7192013-10-04 19:53:11 +010045#define OPCODE_PUSH_I64 (0x68)
46#define OPCODE_PUSH_M64 (0xff) /* /6 */
Damien George81057362014-09-07 01:06:19 +010047#define OPCODE_POP_R64 (0x58) /* +rq */
Damien429d7192013-10-04 19:53:11 +010048#define OPCODE_RET (0xc3)
49#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
Damien George81057362014-09-07 01:06:19 +010050#define OPCODE_MOV_I64_TO_R64 (0xb8) /* +rq */
Damien429d7192013-10-04 19:53:11 +010051#define OPCODE_MOV_I32_TO_RM32 (0xc7)
Damien Georged66e4862014-09-29 10:13:49 +010052#define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
Damien George81057362014-09-07 01:06:19 +010053#define OPCODE_MOV_R64_TO_RM64 (0x89) /* /r */
Damien George91cfd412014-10-12 16:59:29 +010054#define OPCODE_MOV_RM64_TO_R64 (0x8b) /* /r */
55#define OPCODE_MOVZX_RM8_TO_R64 (0xb6) /* 0x0f 0xb6/r */
56#define OPCODE_MOVZX_RM16_TO_R64 (0xb7) /* 0x0f 0xb7/r */
Damien429d7192013-10-04 19:53:11 +010057#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
Damien George1ef23482014-10-12 14:21:06 +010058#define OPCODE_AND_R64_TO_RM64 (0x21) /* /r */
59#define OPCODE_OR_R64_TO_RM64 (0x09) /* /r */
Damien429d7192013-10-04 19:53:11 +010060#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
Damien George3112cde2014-09-29 18:45:42 +010061#define OPCODE_ADD_R64_TO_RM64 (0x01) /* /r */
Damien429d7192013-10-04 19:53:11 +010062#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
63#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
64#define OPCODE_SUB_R64_FROM_RM64 (0x29)
65#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
66#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
Damien George3112cde2014-09-29 18:45:42 +010067//#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
68//#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
69//#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
70#define OPCODE_SHL_RM64_CL (0xd3) /* /4 */
71#define OPCODE_SAR_RM64_CL (0xd3) /* /7 */
72//#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
73//#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
74#define OPCODE_CMP_R64_WITH_RM64 (0x39) /* /r */
75//#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
Damien429d7192013-10-04 19:53:11 +010076#define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
77#define OPCODE_JMP_REL8 (0xeb)
78#define OPCODE_JMP_REL32 (0xe9)
79#define OPCODE_JCC_REL8 (0x70) /* | jcc type */
80#define OPCODE_JCC_REL32_A (0x0f)
81#define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
82#define OPCODE_SETCC_RM8_A (0x0f)
83#define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
84#define OPCODE_CALL_REL32 (0xe8)
85#define OPCODE_CALL_RM32 (0xff) /* /2 */
86#define OPCODE_LEAVE (0xc9)
87
Damien George81057362014-09-07 01:06:19 +010088#define MODRM_R64(x) (((x) & 0x7) << 3)
Damien429d7192013-10-04 19:53:11 +010089#define MODRM_RM_DISP0 (0x00)
90#define MODRM_RM_DISP8 (0x40)
91#define MODRM_RM_DISP32 (0x80)
92#define MODRM_RM_REG (0xc0)
Damien George81057362014-09-07 01:06:19 +010093#define MODRM_RM_R64(x) ((x) & 0x7)
Damien429d7192013-10-04 19:53:11 +010094
Damien Georged66e4862014-09-29 10:13:49 +010095#define OP_SIZE_PREFIX (0x66)
96
Damien429d7192013-10-04 19:53:11 +010097#define REX_PREFIX (0x40)
98#define REX_W (0x08) // width
99#define REX_R (0x04) // register
100#define REX_X (0x02) // index
101#define REX_B (0x01) // base
102
103#define IMM32_L0(x) ((x) & 0xff)
104#define IMM32_L1(x) (((x) >> 8) & 0xff)
105#define IMM32_L2(x) (((x) >> 16) & 0xff)
106#define IMM32_L3(x) (((x) >> 24) & 0xff)
107#define IMM64_L4(x) (((x) >> 32) & 0xff)
108#define IMM64_L5(x) (((x) >> 40) & 0xff)
109#define IMM64_L6(x) (((x) >> 48) & 0xff)
110#define IMM64_L7(x) (((x) >> 56) & 0xff)
111
112#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
113#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
114#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
115
116struct _asm_x64_t {
Damien George36db6bc2014-05-07 17:24:22 +0100117 uint pass;
Fabian Vogtb7235b82014-09-03 16:59:33 +0200118 mp_uint_t code_offset;
119 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +0100120 byte *code_base;
121 byte dummy_data[8];
122
Damien George0b610de2014-09-29 16:25:04 +0100123 mp_uint_t max_num_labels;
124 mp_uint_t *label_offsets;
Damien Georgecd82e022014-02-02 13:11:48 +0000125 int num_locals;
Damien429d7192013-10-04 19:53:11 +0100126};
127
Damien George0b610de2014-09-29 16:25:04 +0100128asm_x64_t *asm_x64_new(mp_uint_t max_num_labels) {
Damien Georgecd82e022014-02-02 13:11:48 +0000129 asm_x64_t *as;
Damien429d7192013-10-04 19:53:11 +0100130
Damien George36db6bc2014-05-07 17:24:22 +0100131 as = m_new0(asm_x64_t, 1);
Damien054848a2013-10-05 13:44:41 +0100132 as->max_num_labels = max_num_labels;
Damien George0b610de2014-09-29 16:25:04 +0100133 as->label_offsets = m_new(mp_uint_t, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100134
135 return as;
136}
137
Damien Georgecd82e022014-02-02 13:11:48 +0000138void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100139 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200140 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +0100141 }
Damien George0b610de2014-09-29 16:25:04 +0100142 m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
Damien732407f2013-12-29 19:33:23 +0000143 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100144}
145
Damien George36db6bc2014-05-07 17:24:22 +0100146void asm_x64_start_pass(asm_x64_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +0100147 as->pass = pass;
148 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100149 if (pass == ASM_X64_PASS_COMPUTE) {
Damien054848a2013-10-05 13:44:41 +0100150 // reset all labels
Damien George0b610de2014-09-29 16:25:04 +0100151 memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
Damien429d7192013-10-04 19:53:11 +0100152 }
153}
154
155void asm_x64_end_pass(asm_x64_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +0100156 if (as->pass == ASM_X64_PASS_COMPUTE) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200157 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
Damien Georgedda46462014-09-03 22:47:23 +0100158 if(as->code_base == NULL) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200159 assert(0);
160 }
Damien Georgee2e3d112014-01-06 22:13:00 +0000161 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100162 }
163
164 /*
165 // check labels are resolved
166 if (as->label != NULL)
167 {
168 int i;
169 for (i = 0; i < as->label->len; ++i)
170 if (g_array_index(as->label, Label, i).unresolved != NULL)
171 return false;
172 }
173 */
174}
175
176// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200177STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100178 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100179 if (as->pass < ASM_X64_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100180 as->code_offset += num_bytes_to_write;
181 return as->dummy_data;
182 } else {
183 assert(as->code_offset + num_bytes_to_write <= as->code_size);
184 byte *c = as->code_base + as->code_offset;
185 as->code_offset += num_bytes_to_write;
186 return c;
187 }
188}
189
Damien George0b610de2014-09-29 16:25:04 +0100190mp_uint_t asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100191 return as->code_size;
192}
193
Damien Georgecd82e022014-02-02 13:11:48 +0000194void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100195 return as->code_base;
196}
197
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200198STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100199 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
200 c[0] = b1;
201}
202
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200203STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100204 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
205 c[0] = b1;
206 c[1] = b2;
207}
208
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200209STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100210 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
211 c[0] = b1;
212 c[1] = b2;
213 c[2] = b3;
214}
215
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200216STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100217 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
218 c[0] = IMM32_L0(w32);
219 c[1] = IMM32_L1(w32);
220 c[2] = IMM32_L2(w32);
221 c[3] = IMM32_L3(w32);
222}
223
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200224STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100225 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
226 c[0] = IMM32_L0(w64);
227 c[1] = IMM32_L1(w64);
228 c[2] = IMM32_L2(w64);
229 c[3] = IMM32_L3(w64);
230 c[4] = IMM64_L4(w64);
231 c[5] = IMM64_L5(w64);
232 c[6] = IMM64_L6(w64);
233 c[7] = IMM64_L7(w64);
234}
235
236/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200237STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100238 byte* c;
239 assert(offset + 4 <= as->code_size);
240 c = as->code_base + offset;
241 c[0] = IMM32_L0(w32);
242 c[1] = IMM32_L1(w32);
243 c[2] = IMM32_L2(w32);
244 c[3] = IMM32_L3(w32);
245}
246*/
247
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200248STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
Damien George81057362014-09-07 01:06:19 +0100249 assert(disp_r64 < 8);
Damien George0b610de2014-09-29 16:25:04 +0100250 assert(disp_r64 != ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100251
Damien George0b610de2014-09-29 16:25:04 +0100252 if (disp_offset == 0 && disp_r64 != ASM_X64_REG_RBP) {
Damien429d7192013-10-04 19:53:11 +0100253 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
254 } else if (SIGNED_FIT8(disp_offset)) {
255 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
256 } else {
257 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
258 asm_x64_write_word32(as, disp_offset);
259 }
260}
261
Damien George3112cde2014-09-29 18:45:42 +0100262STATIC void asm_x64_generic_r64_r64(asm_x64_t *as, int dest_r64, int src_r64, int op) {
263 asm_x64_write_byte_3(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R) | (dest_r64 < 8 ? 0 : REX_B), op, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
264}
265
Damien Georgecd82e022014-02-02 13:11:48 +0000266void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100267 asm_x64_write_byte_1(as, OPCODE_NOP);
268}
269
Damien Georgecd82e022014-02-02 13:11:48 +0000270void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien George81057362014-09-07 01:06:19 +0100271 if (src_r64 < 8) {
272 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
273 } else {
274 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_PUSH_R64 | (src_r64 & 7));
275 }
Damien429d7192013-10-04 19:53:11 +0100276}
277
Damien George81057362014-09-07 01:06:19 +0100278/*
Damien Georgecd82e022014-02-02 13:11:48 +0000279void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100280 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
281 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
282}
Damien George81057362014-09-07 01:06:19 +0100283*/
Damien429d7192013-10-04 19:53:11 +0100284
Damien Georgecd82e022014-02-02 13:11:48 +0000285void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien George81057362014-09-07 01:06:19 +0100286 assert(src_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100287 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
288 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
289}
290
Damien Georgecd82e022014-02-02 13:11:48 +0000291void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100292 if (dest_r64 < 8) {
293 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
294 } else {
295 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_POP_R64 | (dest_r64 & 7));
296 }
Damien429d7192013-10-04 19:53:11 +0100297}
298
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200299STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100300 asm_x64_write_byte_1(as, OPCODE_RET);
301}
302
Damien George3112cde2014-09-29 18:45:42 +0100303void asm_x64_mov_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
304 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_MOV_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100305}
306
Damien George91cfd412014-10-12 16:59:29 +0100307void 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 +0100308 assert(dest_r64 < 8);
309 if (src_r64 < 8) {
310 asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
311 } else {
312 asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_R8_TO_RM8);
313 }
314 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
315}
316
Damien George91cfd412014-10-12 16:59:29 +0100317void 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 +0100318 assert(dest_r64 < 8);
Damien George1c6a1dc2014-09-29 22:44:18 +0100319 if (src_r64 < 8) {
320 asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64);
321 } else {
322 asm_x64_write_byte_3(as, OP_SIZE_PREFIX, REX_PREFIX | REX_R, OPCODE_MOV_R64_TO_RM64);
323 }
Damien Georged66e4862014-09-29 10:13:49 +0100324 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
325}
326
Damien George91cfd412014-10-12 16:59:29 +0100327void 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 +0100328 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100329 assert(dest_r64 < 8);
330 asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R), OPCODE_MOV_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100331 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
332}
333
Damien George91cfd412014-10-12 16:59:29 +0100334void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
335 assert(src_r64 < 8);
336 if (dest_r64 < 8) {
337 asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R64);
338 } else {
339 asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM8_TO_R64);
340 }
341 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
342}
343
344void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
345 assert(src_r64 < 8);
346 if (dest_r64 < 8) {
347 asm_x64_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R64);
348 } else {
349 asm_x64_write_byte_3(as, REX_PREFIX | REX_R, 0x0f, OPCODE_MOVZX_RM16_TO_R64);
350 }
351 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
352}
353
354void 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 +0100355 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100356 assert(src_r64 < 8);
357 asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (dest_r64 < 8 ? 0 : REX_R), OPCODE_MOV_RM64_TO_R64);
Damien429d7192013-10-04 19:53:11 +0100358 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
359}
360
Damien Georgecd82e022014-02-02 13:11:48 +0000361void 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 +0100362 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100363 assert(src_r64 < 8);
364 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100365 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
366 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
367}
368
369void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100370 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100371 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
372}
373
Damien George81057362014-09-07 01:06:19 +0100374STATIC void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100375 // cpu defaults to i32 to r64, with zero extension
Damien George81057362014-09-07 01:06:19 +0100376 if (dest_r64 < 8) {
377 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
378 } else {
379 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7));
380 }
Damien429d7192013-10-04 19:53:11 +0100381 asm_x64_write_word32(as, src_i32);
382}
383
Damien Georgecd82e022014-02-02 13:11:48 +0000384void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100385 // cpu defaults to i32 to r64
386 // to mov i64 to r64 need to use REX prefix
Damien George81057362014-09-07 01:06:19 +0100387 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100388 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
389 asm_x64_write_word64(as, src_i64);
390}
391
392void 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 +0100393 // TODO use movzx, movsx if possible
Damien429d7192013-10-04 19:53:11 +0100394 if (UNSIGNED_FIT32(src_i64)) {
395 // 5 bytes
396 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
397 } else {
398 // 10 bytes
399 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
400 }
401}
402
Damien George36db6bc2014-05-07 17:24:22 +0100403// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
404void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
405 // mov instruction uses 2 bytes for the instruction, before the i64
406 while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
407 asm_x64_nop(as);
408 }
409 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
410}
411
Damien George1ef23482014-10-12 14:21:06 +0100412void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
413 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_AND_R64_TO_RM64);
414}
415
416void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
417 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_OR_R64_TO_RM64);
418}
419
Damien George3112cde2014-09-29 18:45:42 +0100420void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
421 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_XOR_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100422}
423
Damien George3112cde2014-09-29 18:45:42 +0100424void asm_x64_shl_r64_cl(asm_x64_t* as, int dest_r64) {
425 asm_x64_generic_r64_r64(as, dest_r64, 4, OPCODE_SHL_RM64_CL);
Damien429d7192013-10-04 19:53:11 +0100426}
427
Damien George3112cde2014-09-29 18:45:42 +0100428void asm_x64_sar_r64_cl(asm_x64_t* as, int dest_r64) {
429 asm_x64_generic_r64_r64(as, dest_r64, 7, OPCODE_SAR_RM64_CL);
Damien429d7192013-10-04 19:53:11 +0100430}
431
Damien George3112cde2014-09-29 18:45:42 +0100432void asm_x64_add_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
433 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_ADD_R64_TO_RM64);
434}
435
436void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
437 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_SUB_R64_FROM_RM64);
Damien429d7192013-10-04 19:53:11 +0100438}
439
Damien George81057362014-09-07 01:06:19 +0100440/*
Damien Georgecd82e022014-02-02 13:11:48 +0000441void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100442 if (SIGNED_FIT8(src_i32)) {
443 // defaults to 32 bit operation
444 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
445 asm_x64_write_byte_1(as, src_i32 & 0xff);
446 } else {
447 // defaults to 32 bit operation
448 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
449 asm_x64_write_word32(as, src_i32);
450 }
451}
Damien George81057362014-09-07 01:06:19 +0100452*/
Damien429d7192013-10-04 19:53:11 +0100453
Damien George3112cde2014-09-29 18:45:42 +0100454STATIC void asm_x64_sub_r64_i32(asm_x64_t *as, int dest_r64, int src_i32) {
Damien George81057362014-09-07 01:06:19 +0100455 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100456 if (SIGNED_FIT8(src_i32)) {
457 // use REX prefix for 64 bit operation
458 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));
459 asm_x64_write_byte_1(as, src_i32 & 0xff);
460 } else {
461 // use REX prefix for 64 bit operation
462 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));
463 asm_x64_write_word32(as, src_i32);
464 }
465}
466
Damien George81057362014-09-07 01:06:19 +0100467/*
Damien Georgecd82e022014-02-02 13:11:48 +0000468void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100469 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
470 asm_x64_write_byte_1(as, imm);
471}
472
Damien Georgecd82e022014-02-02 13:11:48 +0000473void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100474 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
475 asm_x64_write_byte_1(as, imm);
476}
477
Damien Georgecd82e022014-02-02 13:11:48 +0000478void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100479 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
480 asm_x64_write_byte_1(as, imm);
481}
Damien George81057362014-09-07 01:06:19 +0100482*/
Damien429d7192013-10-04 19:53:11 +0100483
Damien Georgecd82e022014-02-02 13:11:48 +0000484void 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 +0100485 asm_x64_generic_r64_r64(as, src_r64_b, src_r64_a, OPCODE_CMP_R64_WITH_RM64);
Damien429d7192013-10-04 19:53:11 +0100486}
487
Damien George81057362014-09-07 01:06:19 +0100488/*
Damien Georgecd82e022014-02-02 13:11:48 +0000489void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100490 if (SIGNED_FIT8(src_i32)) {
491 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
492 asm_x64_write_byte_1(as, src_i32 & 0xff);
493 } else {
494 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_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 Georgecd82e022014-02-02 13:11:48 +0000500void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100501 // TODO implement for other registers
Damien George0b610de2014-09-29 16:25:04 +0100502 assert(src_r64_a == ASM_X64_REG_RAX);
503 assert(src_r64_b == ASM_X64_REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100504 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));
505}
506
Damien Georgecd82e022014-02-02 13:11:48 +0000507void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien George81057362014-09-07 01:06:19 +0100508 assert(dest_r8 < 8);
Damien429d7192013-10-04 19:53:11 +0100509 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));
510}
511
Damien Georgecd82e022014-02-02 13:11:48 +0000512void asm_x64_label_assign(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100513 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100514 if (as->pass < ASM_X64_PASS_EMIT) {
Damien054848a2013-10-05 13:44:41 +0100515 // assign label offset
516 assert(as->label_offsets[label] == -1);
517 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100518 } else {
519 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien George0b610de2014-09-29 16:25:04 +0100520 //printf("l%d: (at %ld=%ld)\n", label, as->label_offsets[label], as->code_offset);
Damien054848a2013-10-05 13:44:41 +0100521 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100522 }
523}
524
Damien George0b610de2014-09-29 16:25:04 +0100525STATIC mp_uint_t get_label_dest(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100526 assert(label < as->max_num_labels);
527 return as->label_offsets[label];
528}
529
530void asm_x64_jmp_label(asm_x64_t *as, int label) {
Damien George0b610de2014-09-29 16:25:04 +0100531 mp_uint_t dest = get_label_dest(as, label);
532 mp_int_t rel = dest - as->code_offset;
533 if (dest != -1 && rel < 0) {
Damien054848a2013-10-05 13:44:41 +0100534 // is a backwards jump, so we know the size of the jump on the first pass
535 // calculate rel assuming 8 bit relative jump
536 rel -= 2;
537 if (SIGNED_FIT8(rel)) {
538 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100539 } else {
Damien054848a2013-10-05 13:44:41 +0100540 rel += 2;
541 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100542 }
Damien054848a2013-10-05 13:44:41 +0100543 } else {
544 // is a forwards jump, so need to assume it's large
545 large_jump:
546 rel -= 5;
547 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
548 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100549 }
550}
551
Damien054848a2013-10-05 13:44:41 +0100552void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
Damien George0b610de2014-09-29 16:25:04 +0100553 mp_uint_t dest = get_label_dest(as, label);
554 mp_int_t rel = dest - as->code_offset;
555 if (dest != -1 && rel < 0) {
Damien054848a2013-10-05 13:44:41 +0100556 // is a backwards jump, so we know the size of the jump on the first pass
557 // calculate rel assuming 8 bit relative jump
558 rel -= 2;
559 if (SIGNED_FIT8(rel)) {
560 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100561 } else {
Damien054848a2013-10-05 13:44:41 +0100562 rel += 2;
563 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100564 }
Damien054848a2013-10-05 13:44:41 +0100565 } else {
566 // is a forwards jump, so need to assume it's large
567 large_jump:
568 rel -= 6;
569 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
570 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100571 }
572}
573
Damien Georgecd82e022014-02-02 13:11:48 +0000574void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien George0b610de2014-09-29 16:25:04 +0100575 asm_x64_push_r64(as, ASM_X64_REG_RBP);
Damien George3112cde2014-09-29 18:45:42 +0100576 asm_x64_mov_r64_r64(as, ASM_X64_REG_RBP, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100577 if (num_locals < 0) {
578 num_locals = 0;
579 }
580 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
Damien George3112cde2014-09-29 18:45:42 +0100581 asm_x64_sub_r64_i32(as, ASM_X64_REG_RSP, num_locals * WORD_SIZE);
Damien George0b610de2014-09-29 16:25:04 +0100582 asm_x64_push_r64(as, ASM_X64_REG_RBX);
583 asm_x64_push_r64(as, ASM_X64_REG_R12);
584 asm_x64_push_r64(as, ASM_X64_REG_R13);
Damien Georgecd82e022014-02-02 13:11:48 +0000585 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100586}
587
Damien Georgecd82e022014-02-02 13:11:48 +0000588void asm_x64_exit(asm_x64_t *as) {
Damien George0b610de2014-09-29 16:25:04 +0100589 asm_x64_pop_r64(as, ASM_X64_REG_R13);
590 asm_x64_pop_r64(as, ASM_X64_REG_R12);
591 asm_x64_pop_r64(as, ASM_X64_REG_RBX);
Damien429d7192013-10-04 19:53:11 +0100592 asm_x64_write_byte_1(as, OPCODE_LEAVE);
593 asm_x64_ret(as);
594}
595
Damien Georgecd82e022014-02-02 13:11:48 +0000596// locals:
597// - stored on the stack in ascending order
598// - numbered 0 through as->num_locals-1
599// - RBP points above the last local
600//
601// | RPB
602// v
603// l0 l1 l2 ... l(n-1)
604// ^ ^
605// | low address | high address in RAM
606//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200607STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000608 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100609}
610
Damien Georgecd82e022014-02-02 13:11:48 +0000611void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
Damien George91cfd412014-10-12 16:59:29 +0100612 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 +0100613}
614
Damien Georgecd82e022014-02-02 13:11:48 +0000615void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
Damien George91cfd412014-10-12 16:59:29 +0100616 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 +0100617}
618
Damien Georgecd82e022014-02-02 13:11:48 +0000619void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
620 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100621 if (offset == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100622 asm_x64_mov_r64_r64(as, dest_r64, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100623 } else {
Damien George0b610de2014-09-29 16:25:04 +0100624 asm_x64_lea_disp_to_r64(as, ASM_X64_REG_RBP, offset, dest_r64);
Damien429d7192013-10-04 19:53:11 +0100625 }
626}
627
Damien George81057362014-09-07 01:06:19 +0100628/*
Damien Georgecd82e022014-02-02 13:11:48 +0000629void asm_x64_push_local(asm_x64_t *as, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100630 asm_x64_push_disp(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100631}
632
Damien Georgecd82e022014-02-02 13:11:48 +0000633void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
Damien429d7192013-10-04 19:53:11 +0100634{
Damien George3112cde2014-09-29 18:45:42 +0100635 asm_x64_mov_r64_r64(as, temp_r64, ASM_X64_REG_RBP);
Damien Georgecd82e022014-02-02 13:11:48 +0000636 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100637 asm_x64_push_r64(as, temp_r64);
638}
Damien George81057362014-09-07 01:06:19 +0100639*/
Damien429d7192013-10-04 19:53:11 +0100640
641/*
642 can't use these because code might be relocated when resized
643
Damien Georgecd82e022014-02-02 13:11:48 +0000644void asm_x64_call(asm_x64_t *as, void* func)
Damien429d7192013-10-04 19:53:11 +0100645{
Damien George0b610de2014-09-29 16:25:04 +0100646 asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100647 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
648 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
Damien George3112cde2014-09-29 18:45:42 +0100649 asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100650}
651
Damien Georgecd82e022014-02-02 13:11:48 +0000652void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
Damien429d7192013-10-04 19:53:11 +0100653{
Damien George0b610de2014-09-29 16:25:04 +0100654 asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
655 asm_x64_sub_i32_from_r32(as, 12, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100656 asm_x64_push_i32(as, i1);
657 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
658 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
Damien George0b610de2014-09-29 16:25:04 +0100659 asm_x64_add_i32_to_r32(as, 16, ASM_X64_REG_RSP);
Damien George3112cde2014-09-29 18:45:42 +0100660 asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100661}
662*/
663
Damien Georgecd82e022014-02-02 13:11:48 +0000664void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George81057362014-09-07 01:06:19 +0100665 assert(temp_r64 < 8);
Damien George212c2962013-12-30 12:52:32 +0000666#ifdef __LP64__
667 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
668#else
669 // If we get here, sizeof(int) == sizeof(void*).
670 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
671#endif
Damien429d7192013-10-04 19:53:11 +0100672 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 +0100673 // 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 +0100674 // doesn't work anymore because calls are 64 bits away
675 /*
Damien429d7192013-10-04 19:53:11 +0100676 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
677 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100678 */
Damien429d7192013-10-04 19:53:11 +0100679}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000680
681#endif // MICROPY_EMIT_X64