blob: 6c22ea25de5723fefa08bb1d73a9842f6a32a183 [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
32#include "misc.h"
Damien Georgee67ed5d2014-01-04 13:55:24 +000033#include "mpconfig.h"
34
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;
Damien429d7192013-10-04 19:53:11 +0100116 uint code_offset;
117 uint code_size;
118 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
126// for allocating memory, see src/v8/src/platform-linux.cc
127void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
128 req_size = (req_size + 0xfff) & (~0xfff);
129 int prot = PROT_READ | PROT_WRITE | (is_exec ? PROT_EXEC : 0);
130 void *ptr = mmap(NULL, req_size, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
131 if (ptr == MAP_FAILED) {
132 assert(0);
133 }
134 *alloc_size = req_size;
135 return ptr;
136}
137
Damien Georgecd82e022014-02-02 13:11:48 +0000138asm_x64_t *asm_x64_new(uint max_num_labels) {
139 asm_x64_t *as;
Damien429d7192013-10-04 19:53:11 +0100140
Damien George36db6bc2014-05-07 17:24:22 +0100141 as = m_new0(asm_x64_t, 1);
Damien054848a2013-10-05 13:44:41 +0100142 as->max_num_labels = max_num_labels;
143 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100144
145 return as;
146}
147
Damien Georgecd82e022014-02-02 13:11:48 +0000148void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100149 if (free_code) {
Damien415eb6f2013-10-05 12:19:06 +0100150 // need to un-mmap
151 //m_free(as->code_base);
Damien429d7192013-10-04 19:53:11 +0100152 }
153 /*
154 if (as->label != NULL) {
155 int i;
156 for (i = 0; i < as->label->len; ++i)
157 {
158 Label* lab = &g_array_index(as->label, Label, i);
159 if (lab->unresolved != NULL)
160 g_array_free(lab->unresolved, true);
161 }
162 g_array_free(as->label, true);
163 }
164 */
Damien732407f2013-12-29 19:33:23 +0000165 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100166}
167
Damien George36db6bc2014-05-07 17:24:22 +0100168void asm_x64_start_pass(asm_x64_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +0100169 as->pass = pass;
170 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100171 if (pass == ASM_X64_PASS_COMPUTE) {
Damien054848a2013-10-05 13:44:41 +0100172 // reset all labels
173 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +0100174 }
175}
176
177void asm_x64_end_pass(asm_x64_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +0100178 if (as->pass == ASM_X64_PASS_COMPUTE) {
Damien429d7192013-10-04 19:53:11 +0100179 // calculate size of code in bytes
180 as->code_size = as->code_offset;
Damien054848a2013-10-05 13:44:41 +0100181 //as->code_base = m_new(byte, as->code_size); need to allocale executable memory
182 uint actual_alloc;
183 as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
Damien Georgee2e3d112014-01-06 22:13:00 +0000184 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100185 }
186
187 /*
188 // check labels are resolved
189 if (as->label != NULL)
190 {
191 int i;
192 for (i = 0; i < as->label->len; ++i)
193 if (g_array_index(as->label, Label, i).unresolved != NULL)
194 return false;
195 }
196 */
197}
198
199// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200200STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100201 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100202 if (as->pass < ASM_X64_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100203 as->code_offset += num_bytes_to_write;
204 return as->dummy_data;
205 } else {
206 assert(as->code_offset + num_bytes_to_write <= as->code_size);
207 byte *c = as->code_base + as->code_offset;
208 as->code_offset += num_bytes_to_write;
209 return c;
210 }
211}
212
Damien Georgecd82e022014-02-02 13:11:48 +0000213uint asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100214 return as->code_size;
215}
216
Damien Georgecd82e022014-02-02 13:11:48 +0000217void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100218 return as->code_base;
219}
220
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200221STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100222 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
223 c[0] = b1;
224}
225
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200226STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100227 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
228 c[0] = b1;
229 c[1] = b2;
230}
231
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200232STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100233 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
234 c[0] = b1;
235 c[1] = b2;
236 c[2] = b3;
237}
238
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200239STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100240 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
241 c[0] = IMM32_L0(w32);
242 c[1] = IMM32_L1(w32);
243 c[2] = IMM32_L2(w32);
244 c[3] = IMM32_L3(w32);
245}
246
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200247STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100248 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
249 c[0] = IMM32_L0(w64);
250 c[1] = IMM32_L1(w64);
251 c[2] = IMM32_L2(w64);
252 c[3] = IMM32_L3(w64);
253 c[4] = IMM64_L4(w64);
254 c[5] = IMM64_L5(w64);
255 c[6] = IMM64_L6(w64);
256 c[7] = IMM64_L7(w64);
257}
258
259/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200260STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100261 byte* c;
262 assert(offset + 4 <= as->code_size);
263 c = as->code_base + offset;
264 c[0] = IMM32_L0(w32);
265 c[1] = IMM32_L1(w32);
266 c[2] = IMM32_L2(w32);
267 c[3] = IMM32_L3(w32);
268}
269*/
270
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200271STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
Damien429d7192013-10-04 19:53:11 +0100272 assert(disp_r64 != REG_RSP);
273
274 if (disp_offset == 0 && disp_r64 != REG_RBP) {
275 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
276 } else if (SIGNED_FIT8(disp_offset)) {
277 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
278 } else {
279 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
280 asm_x64_write_word32(as, disp_offset);
281 }
282}
283
Damien Georgecd82e022014-02-02 13:11:48 +0000284void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100285 asm_x64_write_byte_1(as, OPCODE_NOP);
286}
287
Damien Georgecd82e022014-02-02 13:11:48 +0000288void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien429d7192013-10-04 19:53:11 +0100289 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
290}
291
Damien Georgecd82e022014-02-02 13:11:48 +0000292void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100293 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
294 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
295}
296
Damien Georgecd82e022014-02-02 13:11:48 +0000297void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien429d7192013-10-04 19:53:11 +0100298 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
299 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
300}
301
Damien Georgecd82e022014-02-02 13:11:48 +0000302void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100303 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
304}
305
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200306STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100307 asm_x64_write_byte_1(as, OPCODE_RET);
308}
309
Damien Georgecd82e022014-02-02 13:11:48 +0000310void asm_x64_mov_r32_to_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100311 // defaults to 32 bit operation
312 asm_x64_write_byte_2(as, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
313}
314
Damien Georgecd82e022014-02-02 13:11:48 +0000315void asm_x64_mov_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100316 // use REX prefix for 64 bit operation
317 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));
318}
319
Damien Georgecd82e022014-02-02 13:11:48 +0000320void 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 +0100321 // use REX prefix for 64 bit operation
322 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, 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_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100327 // use REX prefix for 64 bit operation
328 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_RM64_TO_R64);
329 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
330}
331
Damien Georgecd82e022014-02-02 13:11:48 +0000332void 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 +0100333 // use REX prefix for 64 bit operation
334 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
335 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
336}
337
338void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
339 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
340}
341
Damien Georgecd82e022014-02-02 13:11:48 +0000342void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100343 // cpu defaults to i32 to r64, with zero extension
344 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
345 asm_x64_write_word32(as, src_i32);
346}
347
Damien Georgecd82e022014-02-02 13:11:48 +0000348void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100349 // cpu defaults to i32 to r64
350 // to mov i64 to r64 need to use REX prefix
351 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
352 asm_x64_write_word64(as, src_i64);
353}
354
355void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
356 if (UNSIGNED_FIT32(src_i64)) {
357 // 5 bytes
358 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
359 } else {
360 // 10 bytes
361 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
362 }
363}
364
Damien George36db6bc2014-05-07 17:24:22 +0100365// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
366void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
367 // mov instruction uses 2 bytes for the instruction, before the i64
368 while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
369 asm_x64_nop(as);
370 }
371 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
372}
373
Damien Georgecd82e022014-02-02 13:11:48 +0000374void 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 +0100375{
376 assert(0);
377 asm_x64_write_byte_1(as, OPCODE_MOV_I32_TO_RM32);
378 //asm_x64_write_r32_disp(as, 0, dest_r32, dest_disp);
379 asm_x64_write_word32(as, src_i32);
380}
381
382void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
383 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));
384}
385
Damien Georgecd82e022014-02-02 13:11:48 +0000386void asm_x64_add_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100387 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));
388}
389
Damien Georgecd82e022014-02-02 13:11:48 +0000390void asm_x64_add_i32_to_r32(asm_x64_t *as, int src_i32, int dest_r32)
Damien429d7192013-10-04 19:53:11 +0100391{
392 assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
393 if (SIGNED_FIT8(src_i32))
394 {
395 asm_x64_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
396 asm_x64_write_byte_1(as, src_i32 & 0xff);
397 }
398 else
399 {
400 asm_x64_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
401 asm_x64_write_word32(as, src_i32);
402 }
403}
404
Damien Georgecd82e022014-02-02 13:11:48 +0000405void asm_x64_sub_r32_from_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100406 // defaults to 32 bit operation
407 asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
408}
409
Damien Georgecd82e022014-02-02 13:11:48 +0000410void asm_x64_sub_r64_from_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100411 // use REX prefix for 64 bit operation
412 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));
413}
414
Damien Georgecd82e022014-02-02 13:11:48 +0000415void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100416 if (SIGNED_FIT8(src_i32)) {
417 // defaults to 32 bit operation
418 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
419 asm_x64_write_byte_1(as, src_i32 & 0xff);
420 } else {
421 // defaults to 32 bit operation
422 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
423 asm_x64_write_word32(as, src_i32);
424 }
425}
426
Damien Georgecd82e022014-02-02 13:11:48 +0000427void asm_x64_sub_i32_from_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100428 if (SIGNED_FIT8(src_i32)) {
429 // use REX prefix for 64 bit operation
430 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));
431 asm_x64_write_byte_1(as, src_i32 & 0xff);
432 } else {
433 // use REX prefix for 64 bit operation
434 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));
435 asm_x64_write_word32(as, src_i32);
436 }
437}
438
439/* shifts not tested */
Damien Georgecd82e022014-02-02 13:11:48 +0000440void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100441 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
442 asm_x64_write_byte_1(as, imm);
443}
444
Damien Georgecd82e022014-02-02 13:11:48 +0000445void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100446 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
447 asm_x64_write_byte_1(as, imm);
448}
449
Damien Georgecd82e022014-02-02 13:11:48 +0000450void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100451 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
452 asm_x64_write_byte_1(as, imm);
453}
454
Damien Georgecd82e022014-02-02 13:11:48 +0000455void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien429d7192013-10-04 19:53:11 +0100456 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));
457}
458
Damien Georgecd82e022014-02-02 13:11:48 +0000459void 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 +0100460 assert(0);
461 asm_x64_write_byte_1(as, OPCODE_CMP_R64_WITH_RM64);
462 //asm_x64_write_r32_disp(as, src_r32_a, src_r32_b, src_disp_b);
463}
464
Damien Georgecd82e022014-02-02 13:11:48 +0000465void 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 +0100466 assert(0);
467 asm_x64_write_byte_1(as, OPCODE_CMP_RM32_WITH_R32);
468 //asm_x64_write_r32_disp(as, src_r32_b, src_r32_a, src_disp_a);
469}
470
Damien Georgecd82e022014-02-02 13:11:48 +0000471void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100472 if (SIGNED_FIT8(src_i32)) {
473 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
474 asm_x64_write_byte_1(as, src_i32 & 0xff);
475 } else {
476 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
477 asm_x64_write_word32(as, src_i32);
478 }
479}
480
Damien Georgecd82e022014-02-02 13:11:48 +0000481void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100482 // TODO implement for other registers
483 assert(src_r64_a == REG_RAX);
484 assert(src_r64_b == REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100485 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));
486}
487
Damien Georgecd82e022014-02-02 13:11:48 +0000488void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien429d7192013-10-04 19:53:11 +0100489 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));
490}
491
Damien Georgecd82e022014-02-02 13:11:48 +0000492void asm_x64_label_assign(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100493 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100494 if (as->pass < ASM_X64_PASS_EMIT) {
Damien054848a2013-10-05 13:44:41 +0100495 // assign label offset
496 assert(as->label_offsets[label] == -1);
497 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100498 } else {
499 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien054848a2013-10-05 13:44:41 +0100500 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
501 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100502 }
503}
504
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200505STATIC int get_label_dest(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100506 assert(label < as->max_num_labels);
507 return as->label_offsets[label];
508}
509
510void asm_x64_jmp_label(asm_x64_t *as, int label) {
511 int dest = get_label_dest(as, label);
512 int rel = dest - as->code_offset;
513 if (dest >= 0 && rel < 0) {
514 // is a backwards jump, so we know the size of the jump on the first pass
515 // calculate rel assuming 8 bit relative jump
516 rel -= 2;
517 if (SIGNED_FIT8(rel)) {
518 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100519 } else {
Damien054848a2013-10-05 13:44:41 +0100520 rel += 2;
521 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100522 }
Damien054848a2013-10-05 13:44:41 +0100523 } else {
524 // is a forwards jump, so need to assume it's large
525 large_jump:
526 rel -= 5;
527 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
528 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100529 }
530}
531
Damien054848a2013-10-05 13:44:41 +0100532void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
533 int dest = get_label_dest(as, label);
534 int rel = dest - as->code_offset;
535 if (dest >= 0 && rel < 0) {
536 // is a backwards jump, so we know the size of the jump on the first pass
537 // calculate rel assuming 8 bit relative jump
538 rel -= 2;
539 if (SIGNED_FIT8(rel)) {
540 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100541 } else {
Damien054848a2013-10-05 13:44:41 +0100542 rel += 2;
543 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100544 }
Damien054848a2013-10-05 13:44:41 +0100545 } else {
546 // is a forwards jump, so need to assume it's large
547 large_jump:
548 rel -= 6;
549 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
550 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100551 }
552}
553
Damien Georgecd82e022014-02-02 13:11:48 +0000554void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien429d7192013-10-04 19:53:11 +0100555 asm_x64_push_r64(as, REG_RBP);
556 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
557 if (num_locals < 0) {
558 num_locals = 0;
559 }
560 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
561 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
562 asm_x64_push_r64(as, REG_RBX);
Damien Georgecd82e022014-02-02 13:11:48 +0000563 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100564}
565
Damien Georgecd82e022014-02-02 13:11:48 +0000566void asm_x64_exit(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100567 asm_x64_pop_r64(as, REG_RBX);
568 asm_x64_write_byte_1(as, OPCODE_LEAVE);
569 asm_x64_ret(as);
570}
571
Damien Georgecd82e022014-02-02 13:11:48 +0000572void asm_x64_push_arg(asm_x64_t *as, int src_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100573 assert(0);
574 asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
575}
576
Damien Georgecd82e022014-02-02 13:11:48 +0000577void asm_x64_mov_arg_to_r32(asm_x64_t *as, int src_arg_num, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100578 assert(0);
579 //asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
580}
581
Damien Georgecd82e022014-02-02 13:11:48 +0000582void asm_x64_mov_r32_to_arg(asm_x64_t *as, int src_r32, int dest_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100583 assert(0);
584 //asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
585}
586
Damien Georgecd82e022014-02-02 13:11:48 +0000587// locals:
588// - stored on the stack in ascending order
589// - numbered 0 through as->num_locals-1
590// - RBP points above the last local
591//
592// | RPB
593// v
594// l0 l1 l2 ... l(n-1)
595// ^ ^
596// | low address | high address in RAM
597//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200598STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000599 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100600}
601
Damien Georgecd82e022014-02-02 13:11:48 +0000602void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
603 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 +0100604}
605
Damien Georgecd82e022014-02-02 13:11:48 +0000606void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
607 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 +0100608}
609
Damien Georgecd82e022014-02-02 13:11:48 +0000610void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
611 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100612 if (offset == 0) {
613 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
614 } else {
615 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
616 }
617}
618
Damien Georgecd82e022014-02-02 13:11:48 +0000619void asm_x64_push_local(asm_x64_t *as, int local_num) {
620 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100621}
622
Damien Georgecd82e022014-02-02 13:11:48 +0000623void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
Damien429d7192013-10-04 19:53:11 +0100624{
625 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
Damien Georgecd82e022014-02-02 13:11:48 +0000626 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100627 asm_x64_push_r64(as, temp_r64);
628}
629
630/*
631 can't use these because code might be relocated when resized
632
Damien Georgecd82e022014-02-02 13:11:48 +0000633void asm_x64_call(asm_x64_t *as, void* func)
Damien429d7192013-10-04 19:53:11 +0100634{
635 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
636 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
637 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
638 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
639}
640
Damien Georgecd82e022014-02-02 13:11:48 +0000641void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
Damien429d7192013-10-04 19:53:11 +0100642{
643 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
644 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
645 asm_x64_push_i32(as, i1);
646 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
647 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
648 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
649 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
650}
651*/
652
Damien Georgecd82e022014-02-02 13:11:48 +0000653void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George212c2962013-12-30 12:52:32 +0000654#ifdef __LP64__
655 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
656#else
657 // If we get here, sizeof(int) == sizeof(void*).
658 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
659#endif
Damien429d7192013-10-04 19:53:11 +0100660 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 +0100661 // 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 +0100662 // doesn't work anymore because calls are 64 bits away
663 /*
Damien429d7192013-10-04 19:53:11 +0100664 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
665 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100666 */
Damien429d7192013-10-04 19:53:11 +0100667}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000668
669#endif // MICROPY_EMIT_X64