blob: 940bd9ec4ef17b6cfe43044cbbfa0958feaea997 [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)
44#define OPCODE_PUSH_R64 (0x50)
45#define OPCODE_PUSH_I64 (0x68)
46#define OPCODE_PUSH_M64 (0xff) /* /6 */
47#define OPCODE_POP_R64 (0x58)
48#define OPCODE_RET (0xc3)
49#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
50#define OPCODE_MOV_I64_TO_R64 (0xb8)
51#define OPCODE_MOV_I32_TO_RM32 (0xc7)
52#define OPCODE_MOV_R64_TO_RM64 (0x89)
53#define OPCODE_MOV_RM64_TO_R64 (0x8b)
54#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
55#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
56#define OPCODE_ADD_R64_TO_RM64 (0x01)
57#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
58#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
59#define OPCODE_SUB_R64_FROM_RM64 (0x29)
60#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
61#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
62#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
63#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
64#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
65#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
66#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
67#define OPCODE_CMP_R64_WITH_RM64 (0x39)
68#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
69#define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
70#define OPCODE_JMP_REL8 (0xeb)
71#define OPCODE_JMP_REL32 (0xe9)
72#define OPCODE_JCC_REL8 (0x70) /* | jcc type */
73#define OPCODE_JCC_REL32_A (0x0f)
74#define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
75#define OPCODE_SETCC_RM8_A (0x0f)
76#define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
77#define OPCODE_CALL_REL32 (0xe8)
78#define OPCODE_CALL_RM32 (0xff) /* /2 */
79#define OPCODE_LEAVE (0xc9)
80
81#define MODRM_R64(x) ((x) << 3)
82#define MODRM_RM_DISP0 (0x00)
83#define MODRM_RM_DISP8 (0x40)
84#define MODRM_RM_DISP32 (0x80)
85#define MODRM_RM_REG (0xc0)
86#define MODRM_RM_R64(x) (x)
87
88#define REX_PREFIX (0x40)
89#define REX_W (0x08) // width
90#define REX_R (0x04) // register
91#define REX_X (0x02) // index
92#define REX_B (0x01) // base
93
94#define IMM32_L0(x) ((x) & 0xff)
95#define IMM32_L1(x) (((x) >> 8) & 0xff)
96#define IMM32_L2(x) (((x) >> 16) & 0xff)
97#define IMM32_L3(x) (((x) >> 24) & 0xff)
98#define IMM64_L4(x) (((x) >> 32) & 0xff)
99#define IMM64_L5(x) (((x) >> 40) & 0xff)
100#define IMM64_L6(x) (((x) >> 48) & 0xff)
101#define IMM64_L7(x) (((x) >> 56) & 0xff)
102
103#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
104#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
105#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
106
107struct _asm_x64_t {
Damien George36db6bc2014-05-07 17:24:22 +0100108 uint pass;
Fabian Vogtb7235b82014-09-03 16:59:33 +0200109 mp_uint_t code_offset;
110 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +0100111 byte *code_base;
112 byte dummy_data[8];
113
Damien054848a2013-10-05 13:44:41 +0100114 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +0100115 int *label_offsets;
Damien Georgecd82e022014-02-02 13:11:48 +0000116 int num_locals;
Damien429d7192013-10-04 19:53:11 +0100117};
118
Damien Georgecd82e022014-02-02 13:11:48 +0000119asm_x64_t *asm_x64_new(uint max_num_labels) {
120 asm_x64_t *as;
Damien429d7192013-10-04 19:53:11 +0100121
Damien George36db6bc2014-05-07 17:24:22 +0100122 as = m_new0(asm_x64_t, 1);
Damien054848a2013-10-05 13:44:41 +0100123 as->max_num_labels = max_num_labels;
124 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100125
126 return as;
127}
128
Damien Georgecd82e022014-02-02 13:11:48 +0000129void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100130 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200131 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +0100132 }
133 /*
134 if (as->label != NULL) {
135 int i;
136 for (i = 0; i < as->label->len; ++i)
137 {
138 Label* lab = &g_array_index(as->label, Label, i);
139 if (lab->unresolved != NULL)
140 g_array_free(lab->unresolved, true);
141 }
142 g_array_free(as->label, true);
143 }
144 */
Damien732407f2013-12-29 19:33:23 +0000145 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100146}
147
Damien George36db6bc2014-05-07 17:24:22 +0100148void asm_x64_start_pass(asm_x64_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +0100149 as->pass = pass;
150 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100151 if (pass == ASM_X64_PASS_COMPUTE) {
Damien054848a2013-10-05 13:44:41 +0100152 // reset all labels
153 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +0100154 }
155}
156
157void asm_x64_end_pass(asm_x64_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +0100158 if (as->pass == ASM_X64_PASS_COMPUTE) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200159 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
Damien Georgedda46462014-09-03 22:47:23 +0100160 if(as->code_base == NULL) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200161 assert(0);
162 }
Damien Georgee2e3d112014-01-06 22:13:00 +0000163 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100164 }
165
166 /*
167 // check labels are resolved
168 if (as->label != NULL)
169 {
170 int i;
171 for (i = 0; i < as->label->len; ++i)
172 if (g_array_index(as->label, Label, i).unresolved != NULL)
173 return false;
174 }
175 */
176}
177
178// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200179STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100180 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100181 if (as->pass < ASM_X64_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100182 as->code_offset += num_bytes_to_write;
183 return as->dummy_data;
184 } else {
185 assert(as->code_offset + num_bytes_to_write <= as->code_size);
186 byte *c = as->code_base + as->code_offset;
187 as->code_offset += num_bytes_to_write;
188 return c;
189 }
190}
191
Damien Georgecd82e022014-02-02 13:11:48 +0000192uint asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100193 return as->code_size;
194}
195
Damien Georgecd82e022014-02-02 13:11:48 +0000196void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100197 return as->code_base;
198}
199
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100201 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
202 c[0] = b1;
203}
204
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200205STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100206 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
207 c[0] = b1;
208 c[1] = b2;
209}
210
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200211STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100212 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
213 c[0] = b1;
214 c[1] = b2;
215 c[2] = b3;
216}
217
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200218STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100219 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
220 c[0] = IMM32_L0(w32);
221 c[1] = IMM32_L1(w32);
222 c[2] = IMM32_L2(w32);
223 c[3] = IMM32_L3(w32);
224}
225
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200226STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100227 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
228 c[0] = IMM32_L0(w64);
229 c[1] = IMM32_L1(w64);
230 c[2] = IMM32_L2(w64);
231 c[3] = IMM32_L3(w64);
232 c[4] = IMM64_L4(w64);
233 c[5] = IMM64_L5(w64);
234 c[6] = IMM64_L6(w64);
235 c[7] = IMM64_L7(w64);
236}
237
238/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200239STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100240 byte* c;
241 assert(offset + 4 <= as->code_size);
242 c = as->code_base + offset;
243 c[0] = IMM32_L0(w32);
244 c[1] = IMM32_L1(w32);
245 c[2] = IMM32_L2(w32);
246 c[3] = IMM32_L3(w32);
247}
248*/
249
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200250STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
Damien429d7192013-10-04 19:53:11 +0100251 assert(disp_r64 != REG_RSP);
252
253 if (disp_offset == 0 && disp_r64 != REG_RBP) {
254 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
255 } else if (SIGNED_FIT8(disp_offset)) {
256 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
257 } else {
258 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
259 asm_x64_write_word32(as, disp_offset);
260 }
261}
262
Damien Georgecd82e022014-02-02 13:11:48 +0000263void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100264 asm_x64_write_byte_1(as, OPCODE_NOP);
265}
266
Damien Georgecd82e022014-02-02 13:11:48 +0000267void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien429d7192013-10-04 19:53:11 +0100268 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
269}
270
Damien Georgecd82e022014-02-02 13:11:48 +0000271void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100272 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
273 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
274}
275
Damien Georgecd82e022014-02-02 13:11:48 +0000276void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien429d7192013-10-04 19:53:11 +0100277 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
278 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
279}
280
Damien Georgecd82e022014-02-02 13:11:48 +0000281void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100282 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
283}
284
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200285STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100286 asm_x64_write_byte_1(as, OPCODE_RET);
287}
288
Damien Georgecd82e022014-02-02 13:11:48 +0000289void asm_x64_mov_r32_to_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100290 // defaults to 32 bit operation
291 asm_x64_write_byte_2(as, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
292}
293
Damien Georgecd82e022014-02-02 13:11:48 +0000294void asm_x64_mov_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100295 // use REX prefix for 64 bit operation
296 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));
297}
298
Damien Georgecd82e022014-02-02 13:11:48 +0000299void 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 +0100300 // use REX prefix for 64 bit operation
301 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_R64_TO_RM64);
302 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
303}
304
Damien Georgecd82e022014-02-02 13:11:48 +0000305void 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 +0100306 // use REX prefix for 64 bit operation
307 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_RM64_TO_R64);
308 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
309}
310
Damien Georgecd82e022014-02-02 13:11:48 +0000311void 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 +0100312 // use REX prefix for 64 bit operation
313 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
314 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
315}
316
317void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
318 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
319}
320
Damien Georgecd82e022014-02-02 13:11:48 +0000321void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100322 // cpu defaults to i32 to r64, with zero extension
323 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
324 asm_x64_write_word32(as, src_i32);
325}
326
Damien Georgecd82e022014-02-02 13:11:48 +0000327void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100328 // cpu defaults to i32 to r64
329 // to mov i64 to r64 need to use REX prefix
330 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
331 asm_x64_write_word64(as, src_i64);
332}
333
334void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
335 if (UNSIGNED_FIT32(src_i64)) {
336 // 5 bytes
337 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
338 } else {
339 // 10 bytes
340 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
341 }
342}
343
Damien George36db6bc2014-05-07 17:24:22 +0100344// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
345void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
346 // mov instruction uses 2 bytes for the instruction, before the i64
347 while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
348 asm_x64_nop(as);
349 }
350 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
351}
352
Damien Georgecd82e022014-02-02 13:11:48 +0000353void 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 +0100354{
355 assert(0);
356 asm_x64_write_byte_1(as, OPCODE_MOV_I32_TO_RM32);
357 //asm_x64_write_r32_disp(as, 0, dest_r32, dest_disp);
358 asm_x64_write_word32(as, src_i32);
359}
360
361void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
362 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));
363}
364
Damien Georgecd82e022014-02-02 13:11:48 +0000365void asm_x64_add_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100366 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));
367}
368
Damien Georgecd82e022014-02-02 13:11:48 +0000369void asm_x64_add_i32_to_r32(asm_x64_t *as, int src_i32, int dest_r32)
Damien429d7192013-10-04 19:53:11 +0100370{
371 assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
372 if (SIGNED_FIT8(src_i32))
373 {
374 asm_x64_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
375 asm_x64_write_byte_1(as, src_i32 & 0xff);
376 }
377 else
378 {
379 asm_x64_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
380 asm_x64_write_word32(as, src_i32);
381 }
382}
383
Damien Georgecd82e022014-02-02 13:11:48 +0000384void asm_x64_sub_r32_from_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100385 // defaults to 32 bit operation
386 asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
387}
388
Damien Georgecd82e022014-02-02 13:11:48 +0000389void asm_x64_sub_r64_from_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100390 // use REX prefix for 64 bit operation
391 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));
392}
393
Damien Georgecd82e022014-02-02 13:11:48 +0000394void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100395 if (SIGNED_FIT8(src_i32)) {
396 // defaults to 32 bit operation
397 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
398 asm_x64_write_byte_1(as, src_i32 & 0xff);
399 } else {
400 // defaults to 32 bit operation
401 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
402 asm_x64_write_word32(as, src_i32);
403 }
404}
405
Damien Georgecd82e022014-02-02 13:11:48 +0000406void asm_x64_sub_i32_from_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100407 if (SIGNED_FIT8(src_i32)) {
408 // use REX prefix for 64 bit operation
409 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));
410 asm_x64_write_byte_1(as, src_i32 & 0xff);
411 } else {
412 // use REX prefix for 64 bit operation
413 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));
414 asm_x64_write_word32(as, src_i32);
415 }
416}
417
418/* shifts not tested */
Damien Georgecd82e022014-02-02 13:11:48 +0000419void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100420 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
421 asm_x64_write_byte_1(as, imm);
422}
423
Damien Georgecd82e022014-02-02 13:11:48 +0000424void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100425 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
426 asm_x64_write_byte_1(as, imm);
427}
428
Damien Georgecd82e022014-02-02 13:11:48 +0000429void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100430 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
431 asm_x64_write_byte_1(as, imm);
432}
433
Damien Georgecd82e022014-02-02 13:11:48 +0000434void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien429d7192013-10-04 19:53:11 +0100435 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));
436}
437
Damien Georgecd82e022014-02-02 13:11:48 +0000438void 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 +0100439 assert(0);
440 asm_x64_write_byte_1(as, OPCODE_CMP_R64_WITH_RM64);
441 //asm_x64_write_r32_disp(as, src_r32_a, src_r32_b, src_disp_b);
442}
443
Damien Georgecd82e022014-02-02 13:11:48 +0000444void 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 +0100445 assert(0);
446 asm_x64_write_byte_1(as, OPCODE_CMP_RM32_WITH_R32);
447 //asm_x64_write_r32_disp(as, src_r32_b, src_r32_a, src_disp_a);
448}
449
Damien Georgecd82e022014-02-02 13:11:48 +0000450void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100451 if (SIGNED_FIT8(src_i32)) {
452 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
453 asm_x64_write_byte_1(as, src_i32 & 0xff);
454 } else {
455 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
456 asm_x64_write_word32(as, src_i32);
457 }
458}
459
Damien Georgecd82e022014-02-02 13:11:48 +0000460void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100461 // TODO implement for other registers
462 assert(src_r64_a == REG_RAX);
463 assert(src_r64_b == REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100464 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));
465}
466
Damien Georgecd82e022014-02-02 13:11:48 +0000467void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien429d7192013-10-04 19:53:11 +0100468 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));
469}
470
Damien Georgecd82e022014-02-02 13:11:48 +0000471void asm_x64_label_assign(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100472 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100473 if (as->pass < ASM_X64_PASS_EMIT) {
Damien054848a2013-10-05 13:44:41 +0100474 // assign label offset
475 assert(as->label_offsets[label] == -1);
476 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100477 } else {
478 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien054848a2013-10-05 13:44:41 +0100479 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
480 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100481 }
482}
483
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200484STATIC int get_label_dest(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100485 assert(label < as->max_num_labels);
486 return as->label_offsets[label];
487}
488
489void asm_x64_jmp_label(asm_x64_t *as, int label) {
490 int dest = get_label_dest(as, label);
491 int rel = dest - as->code_offset;
492 if (dest >= 0 && rel < 0) {
493 // is a backwards jump, so we know the size of the jump on the first pass
494 // calculate rel assuming 8 bit relative jump
495 rel -= 2;
496 if (SIGNED_FIT8(rel)) {
497 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100498 } else {
Damien054848a2013-10-05 13:44:41 +0100499 rel += 2;
500 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100501 }
Damien054848a2013-10-05 13:44:41 +0100502 } else {
503 // is a forwards jump, so need to assume it's large
504 large_jump:
505 rel -= 5;
506 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
507 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100508 }
509}
510
Damien054848a2013-10-05 13:44:41 +0100511void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
512 int dest = get_label_dest(as, label);
513 int rel = dest - as->code_offset;
514 if (dest >= 0 && rel < 0) {
515 // is a backwards jump, so we know the size of the jump on the first pass
516 // calculate rel assuming 8 bit relative jump
517 rel -= 2;
518 if (SIGNED_FIT8(rel)) {
519 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100520 } else {
Damien054848a2013-10-05 13:44:41 +0100521 rel += 2;
522 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100523 }
Damien054848a2013-10-05 13:44:41 +0100524 } else {
525 // is a forwards jump, so need to assume it's large
526 large_jump:
527 rel -= 6;
528 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
529 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100530 }
531}
532
Damien Georgecd82e022014-02-02 13:11:48 +0000533void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien429d7192013-10-04 19:53:11 +0100534 asm_x64_push_r64(as, REG_RBP);
535 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
536 if (num_locals < 0) {
537 num_locals = 0;
538 }
539 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
540 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
541 asm_x64_push_r64(as, REG_RBX);
Damien Georgecd82e022014-02-02 13:11:48 +0000542 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100543}
544
Damien Georgecd82e022014-02-02 13:11:48 +0000545void asm_x64_exit(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100546 asm_x64_pop_r64(as, REG_RBX);
547 asm_x64_write_byte_1(as, OPCODE_LEAVE);
548 asm_x64_ret(as);
549}
550
Damien Georgecd82e022014-02-02 13:11:48 +0000551void asm_x64_push_arg(asm_x64_t *as, int src_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100552 assert(0);
553 asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
554}
555
Damien Georgecd82e022014-02-02 13:11:48 +0000556void asm_x64_mov_arg_to_r32(asm_x64_t *as, int src_arg_num, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100557 assert(0);
558 //asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
559}
560
Damien Georgecd82e022014-02-02 13:11:48 +0000561void asm_x64_mov_r32_to_arg(asm_x64_t *as, int src_r32, int dest_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100562 assert(0);
563 //asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
564}
565
Damien Georgecd82e022014-02-02 13:11:48 +0000566// locals:
567// - stored on the stack in ascending order
568// - numbered 0 through as->num_locals-1
569// - RBP points above the last local
570//
571// | RPB
572// v
573// l0 l1 l2 ... l(n-1)
574// ^ ^
575// | low address | high address in RAM
576//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200577STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000578 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100579}
580
Damien Georgecd82e022014-02-02 13:11:48 +0000581void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
582 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 +0100583}
584
Damien Georgecd82e022014-02-02 13:11:48 +0000585void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
586 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 +0100587}
588
Damien Georgecd82e022014-02-02 13:11:48 +0000589void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
590 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100591 if (offset == 0) {
592 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
593 } else {
594 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
595 }
596}
597
Damien Georgecd82e022014-02-02 13:11:48 +0000598void asm_x64_push_local(asm_x64_t *as, int local_num) {
599 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100600}
601
Damien Georgecd82e022014-02-02 13:11:48 +0000602void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
Damien429d7192013-10-04 19:53:11 +0100603{
604 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
Damien Georgecd82e022014-02-02 13:11:48 +0000605 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100606 asm_x64_push_r64(as, temp_r64);
607}
608
609/*
610 can't use these because code might be relocated when resized
611
Damien Georgecd82e022014-02-02 13:11:48 +0000612void asm_x64_call(asm_x64_t *as, void* func)
Damien429d7192013-10-04 19:53:11 +0100613{
614 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
615 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
616 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
617 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
618}
619
Damien Georgecd82e022014-02-02 13:11:48 +0000620void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
Damien429d7192013-10-04 19:53:11 +0100621{
622 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
623 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
624 asm_x64_push_i32(as, i1);
625 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
626 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
627 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
628 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
629}
630*/
631
Damien Georgecd82e022014-02-02 13:11:48 +0000632void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George212c2962013-12-30 12:52:32 +0000633#ifdef __LP64__
634 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
635#else
636 // If we get here, sizeof(int) == sizeof(void*).
637 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
638#endif
Damien429d7192013-10-04 19:53:11 +0100639 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 +0100640 // 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 +0100641 // doesn't work anymore because calls are 64 bits away
642 /*
Damien429d7192013-10-04 19:53:11 +0100643 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
644 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100645 */
Damien429d7192013-10-04 19:53:11 +0100646}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000647
648#endif // MICROPY_EMIT_X64