blob: f2ad16d15ef88f9593884072709f16e66e1200be [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 */
Damien429d7192013-10-04 19:53:11 +010054#define OPCODE_MOV_RM64_TO_R64 (0x8b)
55#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
56#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
57#define OPCODE_ADD_R64_TO_RM64 (0x01)
58#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
59#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
60#define OPCODE_SUB_R64_FROM_RM64 (0x29)
61#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
62#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
63#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
64#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
65#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
66#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
67#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
68#define OPCODE_CMP_R64_WITH_RM64 (0x39)
69#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
70#define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
71#define OPCODE_JMP_REL8 (0xeb)
72#define OPCODE_JMP_REL32 (0xe9)
73#define OPCODE_JCC_REL8 (0x70) /* | jcc type */
74#define OPCODE_JCC_REL32_A (0x0f)
75#define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
76#define OPCODE_SETCC_RM8_A (0x0f)
77#define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
78#define OPCODE_CALL_REL32 (0xe8)
79#define OPCODE_CALL_RM32 (0xff) /* /2 */
80#define OPCODE_LEAVE (0xc9)
81
Damien George81057362014-09-07 01:06:19 +010082#define MODRM_R64(x) (((x) & 0x7) << 3)
Damien429d7192013-10-04 19:53:11 +010083#define MODRM_RM_DISP0 (0x00)
84#define MODRM_RM_DISP8 (0x40)
85#define MODRM_RM_DISP32 (0x80)
86#define MODRM_RM_REG (0xc0)
Damien George81057362014-09-07 01:06:19 +010087#define MODRM_RM_R64(x) ((x) & 0x7)
Damien429d7192013-10-04 19:53:11 +010088
Damien Georged66e4862014-09-29 10:13:49 +010089#define OP_SIZE_PREFIX (0x66)
90
Damien429d7192013-10-04 19:53:11 +010091#define REX_PREFIX (0x40)
92#define REX_W (0x08) // width
93#define REX_R (0x04) // register
94#define REX_X (0x02) // index
95#define REX_B (0x01) // base
96
97#define IMM32_L0(x) ((x) & 0xff)
98#define IMM32_L1(x) (((x) >> 8) & 0xff)
99#define IMM32_L2(x) (((x) >> 16) & 0xff)
100#define IMM32_L3(x) (((x) >> 24) & 0xff)
101#define IMM64_L4(x) (((x) >> 32) & 0xff)
102#define IMM64_L5(x) (((x) >> 40) & 0xff)
103#define IMM64_L6(x) (((x) >> 48) & 0xff)
104#define IMM64_L7(x) (((x) >> 56) & 0xff)
105
106#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
107#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
108#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
109
110struct _asm_x64_t {
Damien George36db6bc2014-05-07 17:24:22 +0100111 uint pass;
Fabian Vogtb7235b82014-09-03 16:59:33 +0200112 mp_uint_t code_offset;
113 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +0100114 byte *code_base;
115 byte dummy_data[8];
116
Damien054848a2013-10-05 13:44:41 +0100117 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +0100118 int *label_offsets;
Damien Georgecd82e022014-02-02 13:11:48 +0000119 int num_locals;
Damien429d7192013-10-04 19:53:11 +0100120};
121
Damien Georgecd82e022014-02-02 13:11:48 +0000122asm_x64_t *asm_x64_new(uint max_num_labels) {
123 asm_x64_t *as;
Damien429d7192013-10-04 19:53:11 +0100124
Damien George36db6bc2014-05-07 17:24:22 +0100125 as = m_new0(asm_x64_t, 1);
Damien054848a2013-10-05 13:44:41 +0100126 as->max_num_labels = max_num_labels;
127 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100128
129 return as;
130}
131
Damien Georgecd82e022014-02-02 13:11:48 +0000132void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100133 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200134 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +0100135 }
136 /*
137 if (as->label != NULL) {
138 int i;
139 for (i = 0; i < as->label->len; ++i)
140 {
141 Label* lab = &g_array_index(as->label, Label, i);
142 if (lab->unresolved != NULL)
143 g_array_free(lab->unresolved, true);
144 }
145 g_array_free(as->label, true);
146 }
147 */
Damien732407f2013-12-29 19:33:23 +0000148 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100149}
150
Damien George36db6bc2014-05-07 17:24:22 +0100151void asm_x64_start_pass(asm_x64_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +0100152 as->pass = pass;
153 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100154 if (pass == ASM_X64_PASS_COMPUTE) {
Damien054848a2013-10-05 13:44:41 +0100155 // reset all labels
156 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +0100157 }
158}
159
160void asm_x64_end_pass(asm_x64_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +0100161 if (as->pass == ASM_X64_PASS_COMPUTE) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200162 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
Damien Georgedda46462014-09-03 22:47:23 +0100163 if(as->code_base == NULL) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200164 assert(0);
165 }
Damien Georgee2e3d112014-01-06 22:13:00 +0000166 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100167 }
168
169 /*
170 // check labels are resolved
171 if (as->label != NULL)
172 {
173 int i;
174 for (i = 0; i < as->label->len; ++i)
175 if (g_array_index(as->label, Label, i).unresolved != NULL)
176 return false;
177 }
178 */
179}
180
181// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200182STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100183 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100184 if (as->pass < ASM_X64_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100185 as->code_offset += num_bytes_to_write;
186 return as->dummy_data;
187 } else {
188 assert(as->code_offset + num_bytes_to_write <= as->code_size);
189 byte *c = as->code_base + as->code_offset;
190 as->code_offset += num_bytes_to_write;
191 return c;
192 }
193}
194
Damien Georgecd82e022014-02-02 13:11:48 +0000195uint asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100196 return as->code_size;
197}
198
Damien Georgecd82e022014-02-02 13:11:48 +0000199void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100200 return as->code_base;
201}
202
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200203STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100204 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
205 c[0] = b1;
206}
207
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200208STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100209 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
210 c[0] = b1;
211 c[1] = b2;
212}
213
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200214STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100215 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
216 c[0] = b1;
217 c[1] = b2;
218 c[2] = b3;
219}
220
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200221STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100222 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
223 c[0] = IMM32_L0(w32);
224 c[1] = IMM32_L1(w32);
225 c[2] = IMM32_L2(w32);
226 c[3] = IMM32_L3(w32);
227}
228
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200229STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100230 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
231 c[0] = IMM32_L0(w64);
232 c[1] = IMM32_L1(w64);
233 c[2] = IMM32_L2(w64);
234 c[3] = IMM32_L3(w64);
235 c[4] = IMM64_L4(w64);
236 c[5] = IMM64_L5(w64);
237 c[6] = IMM64_L6(w64);
238 c[7] = IMM64_L7(w64);
239}
240
241/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200242STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100243 byte* c;
244 assert(offset + 4 <= as->code_size);
245 c = as->code_base + offset;
246 c[0] = IMM32_L0(w32);
247 c[1] = IMM32_L1(w32);
248 c[2] = IMM32_L2(w32);
249 c[3] = IMM32_L3(w32);
250}
251*/
252
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200253STATIC 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 +0100254 assert(disp_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100255 assert(disp_r64 != REG_RSP);
256
257 if (disp_offset == 0 && disp_r64 != REG_RBP) {
258 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
259 } else if (SIGNED_FIT8(disp_offset)) {
260 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
261 } else {
262 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
263 asm_x64_write_word32(as, disp_offset);
264 }
265}
266
Damien Georgecd82e022014-02-02 13:11:48 +0000267void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100268 asm_x64_write_byte_1(as, OPCODE_NOP);
269}
270
Damien Georgecd82e022014-02-02 13:11:48 +0000271void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien George81057362014-09-07 01:06:19 +0100272 if (src_r64 < 8) {
273 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
274 } else {
275 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_PUSH_R64 | (src_r64 & 7));
276 }
Damien429d7192013-10-04 19:53:11 +0100277}
278
Damien George81057362014-09-07 01:06:19 +0100279/*
Damien Georgecd82e022014-02-02 13:11:48 +0000280void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100281 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
282 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
283}
Damien George81057362014-09-07 01:06:19 +0100284*/
Damien429d7192013-10-04 19:53:11 +0100285
Damien Georgecd82e022014-02-02 13:11:48 +0000286void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien George81057362014-09-07 01:06:19 +0100287 assert(src_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100288 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
289 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
290}
291
Damien Georgecd82e022014-02-02 13:11:48 +0000292void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100293 if (dest_r64 < 8) {
294 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
295 } else {
296 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_POP_R64 | (dest_r64 & 7));
297 }
Damien429d7192013-10-04 19:53:11 +0100298}
299
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200300STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100301 asm_x64_write_byte_1(as, OPCODE_RET);
302}
303
Damien Georgecd82e022014-02-02 13:11:48 +0000304void asm_x64_mov_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100305 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100306 asm_x64_write_byte_3(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R) | (dest_r64 < 8 ? 0 : REX_B), OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
Damien429d7192013-10-04 19:53:11 +0100307}
308
Damien Georged66e4862014-09-29 10:13:49 +0100309void asm_x64_mov_r8_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
310 assert(dest_r64 < 8);
311 if (src_r64 < 8) {
312 asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
313 } else {
314 asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_R8_TO_RM8);
315 }
316 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
317}
318
319void asm_x64_mov_r16_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
320 assert(src_r64 < 8);
321 assert(dest_r64 < 8);
322 asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64);
323 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
324}
325
Damien Georgecd82e022014-02-02 13:11:48 +0000326void asm_x64_mov_r64_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
Damien429d7192013-10-04 19:53:11 +0100327 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100328 assert(dest_r64 < 8);
329 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 +0100330 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
331}
332
Damien Georgecd82e022014-02-02 13:11:48 +0000333void asm_x64_mov_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100334 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100335 assert(src_r64 < 8);
336 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 +0100337 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
338}
339
Damien Georgecd82e022014-02-02 13:11:48 +0000340void 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 +0100341 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100342 assert(src_r64 < 8);
343 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100344 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
345 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
346}
347
348void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100349 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100350 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
351}
352
Damien George81057362014-09-07 01:06:19 +0100353STATIC void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100354 // cpu defaults to i32 to r64, with zero extension
Damien George81057362014-09-07 01:06:19 +0100355 if (dest_r64 < 8) {
356 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
357 } else {
358 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7));
359 }
Damien429d7192013-10-04 19:53:11 +0100360 asm_x64_write_word32(as, src_i32);
361}
362
Damien Georgecd82e022014-02-02 13:11:48 +0000363void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100364 // cpu defaults to i32 to r64
365 // to mov i64 to r64 need to use REX prefix
Damien George81057362014-09-07 01:06:19 +0100366 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100367 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
368 asm_x64_write_word64(as, src_i64);
369}
370
371void 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 +0100372 // TODO use movzx, movsx if possible
Damien429d7192013-10-04 19:53:11 +0100373 if (UNSIGNED_FIT32(src_i64)) {
374 // 5 bytes
375 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
376 } else {
377 // 10 bytes
378 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
379 }
380}
381
Damien George36db6bc2014-05-07 17:24:22 +0100382// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
383void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
384 // mov instruction uses 2 bytes for the instruction, before the i64
385 while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
386 asm_x64_nop(as);
387 }
388 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
389}
390
Damien429d7192013-10-04 19:53:11 +0100391void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100392 assert(src_r64 < 8);
393 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100394 asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_XOR_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
395}
396
Damien Georgecd82e022014-02-02 13:11:48 +0000397void asm_x64_add_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100398 assert(src_r64 < 8);
399 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100400 asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_ADD_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
401}
402
Damien George81057362014-09-07 01:06:19 +0100403/*
Damien Georgecd82e022014-02-02 13:11:48 +0000404void asm_x64_add_i32_to_r32(asm_x64_t *as, int src_i32, int dest_r32)
Damien429d7192013-10-04 19:53:11 +0100405{
406 assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
407 if (SIGNED_FIT8(src_i32))
408 {
409 asm_x64_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
410 asm_x64_write_byte_1(as, src_i32 & 0xff);
411 }
412 else
413 {
414 asm_x64_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
415 asm_x64_write_word32(as, src_i32);
416 }
417}
Damien George81057362014-09-07 01:06:19 +0100418*/
Damien429d7192013-10-04 19:53:11 +0100419
Damien George81057362014-09-07 01:06:19 +0100420/*
Damien Georgecd82e022014-02-02 13:11:48 +0000421void asm_x64_sub_r32_from_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100422 // defaults to 32 bit operation
423 asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
424}
Damien George81057362014-09-07 01:06:19 +0100425*/
Damien429d7192013-10-04 19:53:11 +0100426
Damien Georgecd82e022014-02-02 13:11:48 +0000427void asm_x64_sub_r64_from_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100428 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100429 assert(src_r64 < 8);
430 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100431 asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
432}
433
Damien George81057362014-09-07 01:06:19 +0100434/*
Damien Georgecd82e022014-02-02 13:11:48 +0000435void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100436 if (SIGNED_FIT8(src_i32)) {
437 // defaults to 32 bit operation
438 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
439 asm_x64_write_byte_1(as, src_i32 & 0xff);
440 } else {
441 // defaults to 32 bit operation
442 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
443 asm_x64_write_word32(as, src_i32);
444 }
445}
Damien George81057362014-09-07 01:06:19 +0100446*/
Damien429d7192013-10-04 19:53:11 +0100447
Damien Georgecd82e022014-02-02 13:11:48 +0000448void asm_x64_sub_i32_from_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100449 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100450 if (SIGNED_FIT8(src_i32)) {
451 // use REX prefix for 64 bit operation
452 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));
453 asm_x64_write_byte_1(as, src_i32 & 0xff);
454 } else {
455 // use REX prefix for 64 bit operation
456 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));
457 asm_x64_write_word32(as, src_i32);
458 }
459}
460
Damien George81057362014-09-07 01:06:19 +0100461/*
Damien Georgecd82e022014-02-02 13:11:48 +0000462void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100463 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
464 asm_x64_write_byte_1(as, imm);
465}
466
Damien Georgecd82e022014-02-02 13:11:48 +0000467void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100468 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
469 asm_x64_write_byte_1(as, imm);
470}
471
Damien Georgecd82e022014-02-02 13:11:48 +0000472void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100473 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
474 asm_x64_write_byte_1(as, imm);
475}
Damien George81057362014-09-07 01:06:19 +0100476*/
Damien429d7192013-10-04 19:53:11 +0100477
Damien Georgecd82e022014-02-02 13:11:48 +0000478void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien George81057362014-09-07 01:06:19 +0100479 assert(src_r64_a < 8);
480 assert(src_r64_b < 8);
Damien429d7192013-10-04 19:53:11 +0100481 asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_CMP_R64_WITH_RM64, MODRM_R64(src_r64_a) | MODRM_RM_REG | MODRM_RM_R64(src_r64_b));
482}
483
Damien George81057362014-09-07 01:06:19 +0100484/*
Damien Georgecd82e022014-02-02 13:11:48 +0000485void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100486 if (SIGNED_FIT8(src_i32)) {
487 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
488 asm_x64_write_byte_1(as, src_i32 & 0xff);
489 } else {
490 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
491 asm_x64_write_word32(as, src_i32);
492 }
493}
Damien George81057362014-09-07 01:06:19 +0100494*/
Damien429d7192013-10-04 19:53:11 +0100495
Damien Georgecd82e022014-02-02 13:11:48 +0000496void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100497 // TODO implement for other registers
498 assert(src_r64_a == REG_RAX);
499 assert(src_r64_b == REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100500 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));
501}
502
Damien Georgecd82e022014-02-02 13:11:48 +0000503void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien George81057362014-09-07 01:06:19 +0100504 assert(dest_r8 < 8);
Damien429d7192013-10-04 19:53:11 +0100505 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));
506}
507
Damien Georgecd82e022014-02-02 13:11:48 +0000508void asm_x64_label_assign(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100509 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100510 if (as->pass < ASM_X64_PASS_EMIT) {
Damien054848a2013-10-05 13:44:41 +0100511 // assign label offset
512 assert(as->label_offsets[label] == -1);
513 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100514 } else {
515 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien054848a2013-10-05 13:44:41 +0100516 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
517 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100518 }
519}
520
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200521STATIC int get_label_dest(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100522 assert(label < as->max_num_labels);
523 return as->label_offsets[label];
524}
525
526void asm_x64_jmp_label(asm_x64_t *as, int label) {
527 int dest = get_label_dest(as, label);
528 int rel = dest - as->code_offset;
529 if (dest >= 0 && rel < 0) {
530 // is a backwards jump, so we know the size of the jump on the first pass
531 // calculate rel assuming 8 bit relative jump
532 rel -= 2;
533 if (SIGNED_FIT8(rel)) {
534 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100535 } else {
Damien054848a2013-10-05 13:44:41 +0100536 rel += 2;
537 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100538 }
Damien054848a2013-10-05 13:44:41 +0100539 } else {
540 // is a forwards jump, so need to assume it's large
541 large_jump:
542 rel -= 5;
543 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
544 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100545 }
546}
547
Damien054848a2013-10-05 13:44:41 +0100548void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
549 int dest = get_label_dest(as, label);
550 int rel = dest - as->code_offset;
551 if (dest >= 0 && rel < 0) {
552 // is a backwards jump, so we know the size of the jump on the first pass
553 // calculate rel assuming 8 bit relative jump
554 rel -= 2;
555 if (SIGNED_FIT8(rel)) {
556 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100557 } else {
Damien054848a2013-10-05 13:44:41 +0100558 rel += 2;
559 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100560 }
Damien054848a2013-10-05 13:44:41 +0100561 } else {
562 // is a forwards jump, so need to assume it's large
563 large_jump:
564 rel -= 6;
565 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
566 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100567 }
568}
569
Damien Georgecd82e022014-02-02 13:11:48 +0000570void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien429d7192013-10-04 19:53:11 +0100571 asm_x64_push_r64(as, REG_RBP);
572 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
573 if (num_locals < 0) {
574 num_locals = 0;
575 }
576 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
577 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
578 asm_x64_push_r64(as, REG_RBX);
Damien George81057362014-09-07 01:06:19 +0100579 asm_x64_push_r64(as, REG_R12);
580 asm_x64_push_r64(as, REG_R13);
Damien Georgecd82e022014-02-02 13:11:48 +0000581 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100582}
583
Damien Georgecd82e022014-02-02 13:11:48 +0000584void asm_x64_exit(asm_x64_t *as) {
Damien George81057362014-09-07 01:06:19 +0100585 asm_x64_pop_r64(as, REG_R13);
586 asm_x64_pop_r64(as, REG_R12);
Damien429d7192013-10-04 19:53:11 +0100587 asm_x64_pop_r64(as, REG_RBX);
588 asm_x64_write_byte_1(as, OPCODE_LEAVE);
589 asm_x64_ret(as);
590}
591
Damien Georgecd82e022014-02-02 13:11:48 +0000592// locals:
593// - stored on the stack in ascending order
594// - numbered 0 through as->num_locals-1
595// - RBP points above the last local
596//
597// | RPB
598// v
599// l0 l1 l2 ... l(n-1)
600// ^ ^
601// | low address | high address in RAM
602//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200603STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000604 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100605}
606
Damien Georgecd82e022014-02-02 13:11:48 +0000607void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
608 asm_x64_mov_disp_to_r64(as, REG_RBP, asm_x64_local_offset_from_ebp(as, src_local_num), dest_r64);
Damien429d7192013-10-04 19:53:11 +0100609}
610
Damien Georgecd82e022014-02-02 13:11:48 +0000611void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
612 asm_x64_mov_r64_to_disp(as, src_r64, REG_RBP, asm_x64_local_offset_from_ebp(as, dest_local_num));
Damien429d7192013-10-04 19:53:11 +0100613}
614
Damien Georgecd82e022014-02-02 13:11:48 +0000615void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
616 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100617 if (offset == 0) {
618 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
619 } else {
620 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
621 }
622}
623
Damien George81057362014-09-07 01:06:19 +0100624/*
Damien Georgecd82e022014-02-02 13:11:48 +0000625void asm_x64_push_local(asm_x64_t *as, int local_num) {
626 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100627}
628
Damien Georgecd82e022014-02-02 13:11:48 +0000629void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
Damien429d7192013-10-04 19:53:11 +0100630{
631 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
Damien Georgecd82e022014-02-02 13:11:48 +0000632 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100633 asm_x64_push_r64(as, temp_r64);
634}
Damien George81057362014-09-07 01:06:19 +0100635*/
Damien429d7192013-10-04 19:53:11 +0100636
637/*
638 can't use these because code might be relocated when resized
639
Damien Georgecd82e022014-02-02 13:11:48 +0000640void asm_x64_call(asm_x64_t *as, void* func)
Damien429d7192013-10-04 19:53:11 +0100641{
642 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
643 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
644 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
645 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
646}
647
Damien Georgecd82e022014-02-02 13:11:48 +0000648void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
Damien429d7192013-10-04 19:53:11 +0100649{
650 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
651 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
652 asm_x64_push_i32(as, i1);
653 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
654 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
655 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
656 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
657}
658*/
659
Damien Georgecd82e022014-02-02 13:11:48 +0000660void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George81057362014-09-07 01:06:19 +0100661 assert(temp_r64 < 8);
Damien George212c2962013-12-30 12:52:32 +0000662#ifdef __LP64__
663 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
664#else
665 // If we get here, sizeof(int) == sizeof(void*).
666 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
667#endif
Damien429d7192013-10-04 19:53:11 +0100668 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 +0100669 // 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 +0100670 // doesn't work anymore because calls are 64 bits away
671 /*
Damien429d7192013-10-04 19:53:11 +0100672 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
673 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100674 */
Damien429d7192013-10-04 19:53:11 +0100675}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000676
677#endif // MICROPY_EMIT_X64