blob: cf9d8b0f23a56a16676b3d94730237c5d5088e6a [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 <sys/types.h>
39#include <sys/mman.h>
40
41#include "asmx64.h"
42
Damien Georgeebd2e872014-01-02 20:28:12 +000043#if defined(__OpenBSD__) || defined(__MACH__)
Edd Barrett67ab5ee2014-01-01 23:16:27 +000044#define MAP_ANONYMOUS MAP_ANON
45#endif
46
Damien429d7192013-10-04 19:53:11 +010047/* all offsets are measured in multiples of 8 bytes */
48#define WORD_SIZE (8)
49
50#define OPCODE_NOP (0x90)
51#define OPCODE_PUSH_R64 (0x50)
52#define OPCODE_PUSH_I64 (0x68)
53#define OPCODE_PUSH_M64 (0xff) /* /6 */
54#define OPCODE_POP_R64 (0x58)
55#define OPCODE_RET (0xc3)
56#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
57#define OPCODE_MOV_I64_TO_R64 (0xb8)
58#define OPCODE_MOV_I32_TO_RM32 (0xc7)
59#define OPCODE_MOV_R64_TO_RM64 (0x89)
60#define OPCODE_MOV_RM64_TO_R64 (0x8b)
61#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
62#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
63#define OPCODE_ADD_R64_TO_RM64 (0x01)
64#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
65#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
66#define OPCODE_SUB_R64_FROM_RM64 (0x29)
67#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
68#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
69#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
70#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
71#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /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)
75#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
76#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
88#define MODRM_R64(x) ((x) << 3)
89#define MODRM_RM_DISP0 (0x00)
90#define MODRM_RM_DISP8 (0x40)
91#define MODRM_RM_DISP32 (0x80)
92#define MODRM_RM_REG (0xc0)
93#define MODRM_RM_R64(x) (x)
94
95#define REX_PREFIX (0x40)
96#define REX_W (0x08) // width
97#define REX_R (0x04) // register
98#define REX_X (0x02) // index
99#define REX_B (0x01) // base
100
101#define IMM32_L0(x) ((x) & 0xff)
102#define IMM32_L1(x) (((x) >> 8) & 0xff)
103#define IMM32_L2(x) (((x) >> 16) & 0xff)
104#define IMM32_L3(x) (((x) >> 24) & 0xff)
105#define IMM64_L4(x) (((x) >> 32) & 0xff)
106#define IMM64_L5(x) (((x) >> 40) & 0xff)
107#define IMM64_L6(x) (((x) >> 48) & 0xff)
108#define IMM64_L7(x) (((x) >> 56) & 0xff)
109
110#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
111#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
112#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
113
114struct _asm_x64_t {
Damien George36db6bc2014-05-07 17:24:22 +0100115 uint pass;
Fabian Vogtb7235b82014-09-03 16:59:33 +0200116 mp_uint_t code_offset;
117 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +0100118 byte *code_base;
119 byte dummy_data[8];
120
Damien054848a2013-10-05 13:44:41 +0100121 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +0100122 int *label_offsets;
Damien Georgecd82e022014-02-02 13:11:48 +0000123 int num_locals;
Damien429d7192013-10-04 19:53:11 +0100124};
125
Damien Georgecd82e022014-02-02 13:11:48 +0000126asm_x64_t *asm_x64_new(uint max_num_labels) {
127 asm_x64_t *as;
Damien429d7192013-10-04 19:53:11 +0100128
Damien George36db6bc2014-05-07 17:24:22 +0100129 as = m_new0(asm_x64_t, 1);
Damien054848a2013-10-05 13:44:41 +0100130 as->max_num_labels = max_num_labels;
131 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100132
133 return as;
134}
135
Damien Georgecd82e022014-02-02 13:11:48 +0000136void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100137 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200138 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +0100139 }
140 /*
141 if (as->label != NULL) {
142 int i;
143 for (i = 0; i < as->label->len; ++i)
144 {
145 Label* lab = &g_array_index(as->label, Label, i);
146 if (lab->unresolved != NULL)
147 g_array_free(lab->unresolved, true);
148 }
149 g_array_free(as->label, true);
150 }
151 */
Damien732407f2013-12-29 19:33:23 +0000152 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100153}
154
Damien George36db6bc2014-05-07 17:24:22 +0100155void asm_x64_start_pass(asm_x64_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +0100156 as->pass = pass;
157 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100158 if (pass == ASM_X64_PASS_COMPUTE) {
Damien054848a2013-10-05 13:44:41 +0100159 // reset all labels
160 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +0100161 }
162}
163
164void asm_x64_end_pass(asm_x64_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +0100165 if (as->pass == ASM_X64_PASS_COMPUTE) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200166 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
167 if(as->code_base == NULL) {
168 assert(0);
169 }
Damien Georgee2e3d112014-01-06 22:13:00 +0000170 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100171 }
172
173 /*
174 // check labels are resolved
175 if (as->label != NULL)
176 {
177 int i;
178 for (i = 0; i < as->label->len; ++i)
179 if (g_array_index(as->label, Label, i).unresolved != NULL)
180 return false;
181 }
182 */
183}
184
185// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200186STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100187 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100188 if (as->pass < ASM_X64_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100189 as->code_offset += num_bytes_to_write;
190 return as->dummy_data;
191 } else {
192 assert(as->code_offset + num_bytes_to_write <= as->code_size);
193 byte *c = as->code_base + as->code_offset;
194 as->code_offset += num_bytes_to_write;
195 return c;
196 }
197}
198
Damien Georgecd82e022014-02-02 13:11:48 +0000199uint asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100200 return as->code_size;
201}
202
Damien Georgecd82e022014-02-02 13:11:48 +0000203void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100204 return as->code_base;
205}
206
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200207STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100208 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
209 c[0] = b1;
210}
211
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200212STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100213 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
214 c[0] = b1;
215 c[1] = b2;
216}
217
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200218STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100219 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
220 c[0] = b1;
221 c[1] = b2;
222 c[2] = b3;
223}
224
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200225STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100226 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
227 c[0] = IMM32_L0(w32);
228 c[1] = IMM32_L1(w32);
229 c[2] = IMM32_L2(w32);
230 c[3] = IMM32_L3(w32);
231}
232
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200233STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100234 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
235 c[0] = IMM32_L0(w64);
236 c[1] = IMM32_L1(w64);
237 c[2] = IMM32_L2(w64);
238 c[3] = IMM32_L3(w64);
239 c[4] = IMM64_L4(w64);
240 c[5] = IMM64_L5(w64);
241 c[6] = IMM64_L6(w64);
242 c[7] = IMM64_L7(w64);
243}
244
245/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200246STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100247 byte* c;
248 assert(offset + 4 <= as->code_size);
249 c = as->code_base + offset;
250 c[0] = IMM32_L0(w32);
251 c[1] = IMM32_L1(w32);
252 c[2] = IMM32_L2(w32);
253 c[3] = IMM32_L3(w32);
254}
255*/
256
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200257STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
Damien429d7192013-10-04 19:53:11 +0100258 assert(disp_r64 != REG_RSP);
259
260 if (disp_offset == 0 && disp_r64 != REG_RBP) {
261 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
262 } else if (SIGNED_FIT8(disp_offset)) {
263 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
264 } else {
265 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
266 asm_x64_write_word32(as, disp_offset);
267 }
268}
269
Damien Georgecd82e022014-02-02 13:11:48 +0000270void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100271 asm_x64_write_byte_1(as, OPCODE_NOP);
272}
273
Damien Georgecd82e022014-02-02 13:11:48 +0000274void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien429d7192013-10-04 19:53:11 +0100275 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
276}
277
Damien Georgecd82e022014-02-02 13:11:48 +0000278void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100279 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
280 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
281}
282
Damien Georgecd82e022014-02-02 13:11:48 +0000283void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien429d7192013-10-04 19:53:11 +0100284 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
285 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
286}
287
Damien Georgecd82e022014-02-02 13:11:48 +0000288void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100289 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
290}
291
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200292STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100293 asm_x64_write_byte_1(as, OPCODE_RET);
294}
295
Damien Georgecd82e022014-02-02 13:11:48 +0000296void asm_x64_mov_r32_to_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100297 // defaults to 32 bit operation
298 asm_x64_write_byte_2(as, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
299}
300
Damien Georgecd82e022014-02-02 13:11:48 +0000301void asm_x64_mov_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100302 // use REX prefix for 64 bit operation
303 asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
304}
305
Damien Georgecd82e022014-02-02 13:11:48 +0000306void 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 +0100307 // use REX prefix for 64 bit operation
308 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_R64_TO_RM64);
309 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
310}
311
Damien Georgecd82e022014-02-02 13:11:48 +0000312void 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 +0100313 // use REX prefix for 64 bit operation
314 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_RM64_TO_R64);
315 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
316}
317
Damien Georgecd82e022014-02-02 13:11:48 +0000318void 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 +0100319 // use REX prefix for 64 bit operation
320 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
321 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
322}
323
324void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
325 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
326}
327
Damien Georgecd82e022014-02-02 13:11:48 +0000328void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100329 // cpu defaults to i32 to r64, with zero extension
330 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
331 asm_x64_write_word32(as, src_i32);
332}
333
Damien Georgecd82e022014-02-02 13:11:48 +0000334void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100335 // cpu defaults to i32 to r64
336 // to mov i64 to r64 need to use REX prefix
337 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
338 asm_x64_write_word64(as, src_i64);
339}
340
341void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
342 if (UNSIGNED_FIT32(src_i64)) {
343 // 5 bytes
344 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
345 } else {
346 // 10 bytes
347 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
348 }
349}
350
Damien George36db6bc2014-05-07 17:24:22 +0100351// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
352void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
353 // mov instruction uses 2 bytes for the instruction, before the i64
354 while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
355 asm_x64_nop(as);
356 }
357 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
358}
359
Damien Georgecd82e022014-02-02 13:11:48 +0000360void asm_x64_mov_i32_to_disp(asm_x64_t *as, int src_i32, int dest_r32, int dest_disp)
Damien429d7192013-10-04 19:53:11 +0100361{
362 assert(0);
363 asm_x64_write_byte_1(as, OPCODE_MOV_I32_TO_RM32);
364 //asm_x64_write_r32_disp(as, 0, dest_r32, dest_disp);
365 asm_x64_write_word32(as, src_i32);
366}
367
368void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
369 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));
370}
371
Damien Georgecd82e022014-02-02 13:11:48 +0000372void asm_x64_add_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100373 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));
374}
375
Damien Georgecd82e022014-02-02 13:11:48 +0000376void asm_x64_add_i32_to_r32(asm_x64_t *as, int src_i32, int dest_r32)
Damien429d7192013-10-04 19:53:11 +0100377{
378 assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
379 if (SIGNED_FIT8(src_i32))
380 {
381 asm_x64_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
382 asm_x64_write_byte_1(as, src_i32 & 0xff);
383 }
384 else
385 {
386 asm_x64_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
387 asm_x64_write_word32(as, src_i32);
388 }
389}
390
Damien Georgecd82e022014-02-02 13:11:48 +0000391void asm_x64_sub_r32_from_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100392 // defaults to 32 bit operation
393 asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
394}
395
Damien Georgecd82e022014-02-02 13:11:48 +0000396void asm_x64_sub_r64_from_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100397 // use REX prefix for 64 bit operation
398 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));
399}
400
Damien Georgecd82e022014-02-02 13:11:48 +0000401void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100402 if (SIGNED_FIT8(src_i32)) {
403 // defaults to 32 bit operation
404 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
405 asm_x64_write_byte_1(as, src_i32 & 0xff);
406 } else {
407 // defaults to 32 bit operation
408 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
409 asm_x64_write_word32(as, src_i32);
410 }
411}
412
Damien Georgecd82e022014-02-02 13:11:48 +0000413void asm_x64_sub_i32_from_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100414 if (SIGNED_FIT8(src_i32)) {
415 // use REX prefix for 64 bit operation
416 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));
417 asm_x64_write_byte_1(as, src_i32 & 0xff);
418 } else {
419 // use REX prefix for 64 bit operation
420 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));
421 asm_x64_write_word32(as, src_i32);
422 }
423}
424
425/* shifts not tested */
Damien Georgecd82e022014-02-02 13:11:48 +0000426void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100427 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
428 asm_x64_write_byte_1(as, imm);
429}
430
Damien Georgecd82e022014-02-02 13:11:48 +0000431void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100432 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
433 asm_x64_write_byte_1(as, imm);
434}
435
Damien Georgecd82e022014-02-02 13:11:48 +0000436void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100437 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
438 asm_x64_write_byte_1(as, imm);
439}
440
Damien Georgecd82e022014-02-02 13:11:48 +0000441void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien429d7192013-10-04 19:53:11 +0100442 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));
443}
444
Damien Georgecd82e022014-02-02 13:11:48 +0000445void asm_x64_cmp_r32_with_disp(asm_x64_t *as, int src_r32_a, int src_r32_b, int src_disp_b) {
Damien429d7192013-10-04 19:53:11 +0100446 assert(0);
447 asm_x64_write_byte_1(as, OPCODE_CMP_R64_WITH_RM64);
448 //asm_x64_write_r32_disp(as, src_r32_a, src_r32_b, src_disp_b);
449}
450
Damien Georgecd82e022014-02-02 13:11:48 +0000451void asm_x64_cmp_disp_with_r32(asm_x64_t *as, int src_r32_a, int src_disp_a, int src_r32_b) {
Damien429d7192013-10-04 19:53:11 +0100452 assert(0);
453 asm_x64_write_byte_1(as, OPCODE_CMP_RM32_WITH_R32);
454 //asm_x64_write_r32_disp(as, src_r32_b, src_r32_a, src_disp_a);
455}
456
Damien Georgecd82e022014-02-02 13:11:48 +0000457void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100458 if (SIGNED_FIT8(src_i32)) {
459 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
460 asm_x64_write_byte_1(as, src_i32 & 0xff);
461 } else {
462 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
463 asm_x64_write_word32(as, src_i32);
464 }
465}
466
Damien Georgecd82e022014-02-02 13:11:48 +0000467void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100468 // TODO implement for other registers
469 assert(src_r64_a == REG_RAX);
470 assert(src_r64_b == REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100471 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));
472}
473
Damien Georgecd82e022014-02-02 13:11:48 +0000474void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien429d7192013-10-04 19:53:11 +0100475 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));
476}
477
Damien Georgecd82e022014-02-02 13:11:48 +0000478void asm_x64_label_assign(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100479 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100480 if (as->pass < ASM_X64_PASS_EMIT) {
Damien054848a2013-10-05 13:44:41 +0100481 // assign label offset
482 assert(as->label_offsets[label] == -1);
483 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100484 } else {
485 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien054848a2013-10-05 13:44:41 +0100486 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
487 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100488 }
489}
490
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200491STATIC int get_label_dest(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100492 assert(label < as->max_num_labels);
493 return as->label_offsets[label];
494}
495
496void asm_x64_jmp_label(asm_x64_t *as, int label) {
497 int dest = get_label_dest(as, label);
498 int rel = dest - as->code_offset;
499 if (dest >= 0 && rel < 0) {
500 // is a backwards jump, so we know the size of the jump on the first pass
501 // calculate rel assuming 8 bit relative jump
502 rel -= 2;
503 if (SIGNED_FIT8(rel)) {
504 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100505 } else {
Damien054848a2013-10-05 13:44:41 +0100506 rel += 2;
507 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100508 }
Damien054848a2013-10-05 13:44:41 +0100509 } else {
510 // is a forwards jump, so need to assume it's large
511 large_jump:
512 rel -= 5;
513 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
514 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100515 }
516}
517
Damien054848a2013-10-05 13:44:41 +0100518void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
519 int dest = get_label_dest(as, label);
520 int rel = dest - as->code_offset;
521 if (dest >= 0 && rel < 0) {
522 // is a backwards jump, so we know the size of the jump on the first pass
523 // calculate rel assuming 8 bit relative jump
524 rel -= 2;
525 if (SIGNED_FIT8(rel)) {
526 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100527 } else {
Damien054848a2013-10-05 13:44:41 +0100528 rel += 2;
529 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100530 }
Damien054848a2013-10-05 13:44:41 +0100531 } else {
532 // is a forwards jump, so need to assume it's large
533 large_jump:
534 rel -= 6;
535 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
536 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100537 }
538}
539
Damien Georgecd82e022014-02-02 13:11:48 +0000540void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien429d7192013-10-04 19:53:11 +0100541 asm_x64_push_r64(as, REG_RBP);
542 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
543 if (num_locals < 0) {
544 num_locals = 0;
545 }
546 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
547 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
548 asm_x64_push_r64(as, REG_RBX);
Damien Georgecd82e022014-02-02 13:11:48 +0000549 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100550}
551
Damien Georgecd82e022014-02-02 13:11:48 +0000552void asm_x64_exit(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100553 asm_x64_pop_r64(as, REG_RBX);
554 asm_x64_write_byte_1(as, OPCODE_LEAVE);
555 asm_x64_ret(as);
556}
557
Damien Georgecd82e022014-02-02 13:11:48 +0000558void asm_x64_push_arg(asm_x64_t *as, int src_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100559 assert(0);
560 asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
561}
562
Damien Georgecd82e022014-02-02 13:11:48 +0000563void asm_x64_mov_arg_to_r32(asm_x64_t *as, int src_arg_num, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100564 assert(0);
565 //asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
566}
567
Damien Georgecd82e022014-02-02 13:11:48 +0000568void asm_x64_mov_r32_to_arg(asm_x64_t *as, int src_r32, int dest_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100569 assert(0);
570 //asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
571}
572
Damien Georgecd82e022014-02-02 13:11:48 +0000573// locals:
574// - stored on the stack in ascending order
575// - numbered 0 through as->num_locals-1
576// - RBP points above the last local
577//
578// | RPB
579// v
580// l0 l1 l2 ... l(n-1)
581// ^ ^
582// | low address | high address in RAM
583//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200584STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000585 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100586}
587
Damien Georgecd82e022014-02-02 13:11:48 +0000588void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
589 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 +0100590}
591
Damien Georgecd82e022014-02-02 13:11:48 +0000592void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
593 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 +0100594}
595
Damien Georgecd82e022014-02-02 13:11:48 +0000596void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
597 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100598 if (offset == 0) {
599 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
600 } else {
601 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
602 }
603}
604
Damien Georgecd82e022014-02-02 13:11:48 +0000605void asm_x64_push_local(asm_x64_t *as, int local_num) {
606 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100607}
608
Damien Georgecd82e022014-02-02 13:11:48 +0000609void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
Damien429d7192013-10-04 19:53:11 +0100610{
611 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
Damien Georgecd82e022014-02-02 13:11:48 +0000612 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100613 asm_x64_push_r64(as, temp_r64);
614}
615
616/*
617 can't use these because code might be relocated when resized
618
Damien Georgecd82e022014-02-02 13:11:48 +0000619void asm_x64_call(asm_x64_t *as, void* func)
Damien429d7192013-10-04 19:53:11 +0100620{
621 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
622 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
623 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
624 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
625}
626
Damien Georgecd82e022014-02-02 13:11:48 +0000627void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
Damien429d7192013-10-04 19:53:11 +0100628{
629 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
630 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
631 asm_x64_push_i32(as, i1);
632 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
633 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
634 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
635 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
636}
637*/
638
Damien Georgecd82e022014-02-02 13:11:48 +0000639void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George212c2962013-12-30 12:52:32 +0000640#ifdef __LP64__
641 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
642#else
643 // If we get here, sizeof(int) == sizeof(void*).
644 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
645#endif
Damien429d7192013-10-04 19:53:11 +0100646 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 +0100647 // 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 +0100648 // doesn't work anymore because calls are 64 bits away
649 /*
Damien429d7192013-10-04 19:53:11 +0100650 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
651 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100652 */
Damien429d7192013-10-04 19:53:11 +0100653}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000654
655#endif // MICROPY_EMIT_X64