blob: 7c279c521b9bbfc81417f1d5c49dbfe4b2cdf5b0 [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 {
115 int pass;
116 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
141 as = m_new(asm_x64_t, 1);
142 as->pass = 0;
143 as->code_offset = 0;
144 as->code_size = 0;
145 as->code_base = NULL;
Damien054848a2013-10-05 13:44:41 +0100146 as->max_num_labels = max_num_labels;
147 as->label_offsets = m_new(int, max_num_labels);
Damien Georgecd82e022014-02-02 13:11:48 +0000148 as->num_locals = 0;
Damien429d7192013-10-04 19:53:11 +0100149
150 return as;
151}
152
Damien Georgecd82e022014-02-02 13:11:48 +0000153void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100154 if (free_code) {
Damien415eb6f2013-10-05 12:19:06 +0100155 // need to un-mmap
156 //m_free(as->code_base);
Damien429d7192013-10-04 19:53:11 +0100157 }
158 /*
159 if (as->label != NULL) {
160 int i;
161 for (i = 0; i < as->label->len; ++i)
162 {
163 Label* lab = &g_array_index(as->label, Label, i);
164 if (lab->unresolved != NULL)
165 g_array_free(lab->unresolved, true);
166 }
167 g_array_free(as->label, true);
168 }
169 */
Damien732407f2013-12-29 19:33:23 +0000170 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100171}
172
173void asm_x64_start_pass(asm_x64_t *as, int pass) {
174 as->pass = pass;
175 as->code_offset = 0;
Damien054848a2013-10-05 13:44:41 +0100176 if (pass == ASM_X64_PASS_2) {
177 // reset all labels
178 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +0100179 }
180}
181
182void asm_x64_end_pass(asm_x64_t *as) {
Damien054848a2013-10-05 13:44:41 +0100183 if (as->pass == ASM_X64_PASS_2) {
Damien429d7192013-10-04 19:53:11 +0100184 // calculate size of code in bytes
185 as->code_size = as->code_offset;
Damien054848a2013-10-05 13:44:41 +0100186 //as->code_base = m_new(byte, as->code_size); need to allocale executable memory
187 uint actual_alloc;
188 as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
Damien Georgee2e3d112014-01-06 22:13:00 +0000189 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100190 }
191
192 /*
193 // check labels are resolved
194 if (as->label != NULL)
195 {
196 int i;
197 for (i = 0; i < as->label->len; ++i)
198 if (g_array_index(as->label, Label, i).unresolved != NULL)
199 return false;
200 }
201 */
202}
203
204// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200205STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100206 //printf("emit %d\n", num_bytes_to_write);
207 if (as->pass < ASM_X64_PASS_3) {
208 as->code_offset += num_bytes_to_write;
209 return as->dummy_data;
210 } else {
211 assert(as->code_offset + num_bytes_to_write <= as->code_size);
212 byte *c = as->code_base + as->code_offset;
213 as->code_offset += num_bytes_to_write;
214 return c;
215 }
216}
217
Damien Georgecd82e022014-02-02 13:11:48 +0000218uint asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100219 return as->code_size;
220}
221
Damien Georgecd82e022014-02-02 13:11:48 +0000222void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100223 return as->code_base;
224}
225
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200226STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100227 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
228 c[0] = b1;
229}
230
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200231STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100232 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
233 c[0] = b1;
234 c[1] = b2;
235}
236
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200237STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100238 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
239 c[0] = b1;
240 c[1] = b2;
241 c[2] = b3;
242}
243
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200244STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100245 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
246 c[0] = IMM32_L0(w32);
247 c[1] = IMM32_L1(w32);
248 c[2] = IMM32_L2(w32);
249 c[3] = IMM32_L3(w32);
250}
251
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200252STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100253 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
254 c[0] = IMM32_L0(w64);
255 c[1] = IMM32_L1(w64);
256 c[2] = IMM32_L2(w64);
257 c[3] = IMM32_L3(w64);
258 c[4] = IMM64_L4(w64);
259 c[5] = IMM64_L5(w64);
260 c[6] = IMM64_L6(w64);
261 c[7] = IMM64_L7(w64);
262}
263
264/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200265STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100266 byte* c;
267 assert(offset + 4 <= as->code_size);
268 c = as->code_base + offset;
269 c[0] = IMM32_L0(w32);
270 c[1] = IMM32_L1(w32);
271 c[2] = IMM32_L2(w32);
272 c[3] = IMM32_L3(w32);
273}
274*/
275
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200276STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
Damien429d7192013-10-04 19:53:11 +0100277 assert(disp_r64 != REG_RSP);
278
279 if (disp_offset == 0 && disp_r64 != REG_RBP) {
280 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
281 } else if (SIGNED_FIT8(disp_offset)) {
282 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
283 } else {
284 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
285 asm_x64_write_word32(as, disp_offset);
286 }
287}
288
Damien Georgecd82e022014-02-02 13:11:48 +0000289void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100290 asm_x64_write_byte_1(as, OPCODE_NOP);
291}
292
Damien Georgecd82e022014-02-02 13:11:48 +0000293void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien429d7192013-10-04 19:53:11 +0100294 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
295}
296
Damien Georgecd82e022014-02-02 13:11:48 +0000297void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100298 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
299 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
300}
301
Damien Georgecd82e022014-02-02 13:11:48 +0000302void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien429d7192013-10-04 19:53:11 +0100303 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
304 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
305}
306
Damien Georgecd82e022014-02-02 13:11:48 +0000307void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100308 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
309}
310
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200311STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100312 asm_x64_write_byte_1(as, OPCODE_RET);
313}
314
Damien Georgecd82e022014-02-02 13:11:48 +0000315void asm_x64_mov_r32_to_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100316 // defaults to 32 bit operation
317 asm_x64_write_byte_2(as, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
318}
319
Damien Georgecd82e022014-02-02 13:11:48 +0000320void asm_x64_mov_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100321 // use REX prefix for 64 bit operation
322 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));
323}
324
Damien Georgecd82e022014-02-02 13:11:48 +0000325void 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 +0100326 // use REX prefix for 64 bit operation
327 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_R64_TO_RM64);
328 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
329}
330
Damien Georgecd82e022014-02-02 13:11:48 +0000331void 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 +0100332 // use REX prefix for 64 bit operation
333 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_RM64_TO_R64);
334 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
335}
336
Damien Georgecd82e022014-02-02 13:11:48 +0000337void 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 +0100338 // use REX prefix for 64 bit operation
339 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
340 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
341}
342
343void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
344 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
345}
346
Damien Georgecd82e022014-02-02 13:11:48 +0000347void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100348 // cpu defaults to i32 to r64, with zero extension
349 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
350 asm_x64_write_word32(as, src_i32);
351}
352
Damien Georgecd82e022014-02-02 13:11:48 +0000353void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100354 // cpu defaults to i32 to r64
355 // to mov i64 to r64 need to use REX prefix
356 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
357 asm_x64_write_word64(as, src_i64);
358}
359
360void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
361 if (UNSIGNED_FIT32(src_i64)) {
362 // 5 bytes
363 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
364 } else {
365 // 10 bytes
366 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
367 }
368}
369
Damien Georgecd82e022014-02-02 13:11:48 +0000370void 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 +0100371{
372 assert(0);
373 asm_x64_write_byte_1(as, OPCODE_MOV_I32_TO_RM32);
374 //asm_x64_write_r32_disp(as, 0, dest_r32, dest_disp);
375 asm_x64_write_word32(as, src_i32);
376}
377
378void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
379 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));
380}
381
Damien Georgecd82e022014-02-02 13:11:48 +0000382void asm_x64_add_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100383 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));
384}
385
Damien Georgecd82e022014-02-02 13:11:48 +0000386void asm_x64_add_i32_to_r32(asm_x64_t *as, int src_i32, int dest_r32)
Damien429d7192013-10-04 19:53:11 +0100387{
388 assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
389 if (SIGNED_FIT8(src_i32))
390 {
391 asm_x64_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
392 asm_x64_write_byte_1(as, src_i32 & 0xff);
393 }
394 else
395 {
396 asm_x64_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
397 asm_x64_write_word32(as, src_i32);
398 }
399}
400
Damien Georgecd82e022014-02-02 13:11:48 +0000401void asm_x64_sub_r32_from_r32(asm_x64_t *as, int src_r32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100402 // defaults to 32 bit operation
403 asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
404}
405
Damien Georgecd82e022014-02-02 13:11:48 +0000406void asm_x64_sub_r64_from_r64(asm_x64_t *as, int src_r64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100407 // use REX prefix for 64 bit operation
408 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));
409}
410
Damien Georgecd82e022014-02-02 13:11:48 +0000411void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100412 if (SIGNED_FIT8(src_i32)) {
413 // defaults to 32 bit operation
414 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
415 asm_x64_write_byte_1(as, src_i32 & 0xff);
416 } else {
417 // defaults to 32 bit operation
418 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
419 asm_x64_write_word32(as, src_i32);
420 }
421}
422
Damien Georgecd82e022014-02-02 13:11:48 +0000423void asm_x64_sub_i32_from_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100424 if (SIGNED_FIT8(src_i32)) {
425 // use REX prefix for 64 bit operation
426 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));
427 asm_x64_write_byte_1(as, src_i32 & 0xff);
428 } else {
429 // use REX prefix for 64 bit operation
430 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));
431 asm_x64_write_word32(as, src_i32);
432 }
433}
434
435/* shifts not tested */
Damien Georgecd82e022014-02-02 13:11:48 +0000436void asm_x64_shl_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_SHL_RM32_BY_I8, MODRM_R64(4) | 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_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100442 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
443 asm_x64_write_byte_1(as, imm);
444}
445
Damien Georgecd82e022014-02-02 13:11:48 +0000446void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100447 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
448 asm_x64_write_byte_1(as, imm);
449}
450
Damien Georgecd82e022014-02-02 13:11:48 +0000451void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien429d7192013-10-04 19:53:11 +0100452 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));
453}
454
Damien Georgecd82e022014-02-02 13:11:48 +0000455void 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 +0100456 assert(0);
457 asm_x64_write_byte_1(as, OPCODE_CMP_R64_WITH_RM64);
458 //asm_x64_write_r32_disp(as, src_r32_a, src_r32_b, src_disp_b);
459}
460
Damien Georgecd82e022014-02-02 13:11:48 +0000461void 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 +0100462 assert(0);
463 asm_x64_write_byte_1(as, OPCODE_CMP_RM32_WITH_R32);
464 //asm_x64_write_r32_disp(as, src_r32_b, src_r32_a, src_disp_a);
465}
466
Damien Georgecd82e022014-02-02 13:11:48 +0000467void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100468 if (SIGNED_FIT8(src_i32)) {
469 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
470 asm_x64_write_byte_1(as, src_i32 & 0xff);
471 } else {
472 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
473 asm_x64_write_word32(as, src_i32);
474 }
475}
476
Damien Georgecd82e022014-02-02 13:11:48 +0000477void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100478 // TODO implement for other registers
479 assert(src_r64_a == REG_RAX);
480 assert(src_r64_b == REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100481 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));
482}
483
Damien Georgecd82e022014-02-02 13:11:48 +0000484void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien429d7192013-10-04 19:53:11 +0100485 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));
486}
487
Damien Georgecd82e022014-02-02 13:11:48 +0000488void asm_x64_label_assign(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100489 assert(label < as->max_num_labels);
490 if (as->pass == ASM_X64_PASS_2) {
491 // assign label offset
492 assert(as->label_offsets[label] == -1);
493 as->label_offsets[label] = as->code_offset;
494 } else if (as->pass == ASM_X64_PASS_3) {
495 // ensure label offset has not changed from PASS_2 to PASS_3
496 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
497 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100498 }
499}
500
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200501STATIC int get_label_dest(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100502 assert(label < as->max_num_labels);
503 return as->label_offsets[label];
504}
505
506void asm_x64_jmp_label(asm_x64_t *as, int label) {
507 int dest = get_label_dest(as, label);
508 int rel = dest - as->code_offset;
509 if (dest >= 0 && rel < 0) {
510 // is a backwards jump, so we know the size of the jump on the first pass
511 // calculate rel assuming 8 bit relative jump
512 rel -= 2;
513 if (SIGNED_FIT8(rel)) {
514 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100515 } else {
Damien054848a2013-10-05 13:44:41 +0100516 rel += 2;
517 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100518 }
Damien054848a2013-10-05 13:44:41 +0100519 } else {
520 // is a forwards jump, so need to assume it's large
521 large_jump:
522 rel -= 5;
523 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
524 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100525 }
526}
527
Damien054848a2013-10-05 13:44:41 +0100528void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
529 int dest = get_label_dest(as, label);
530 int rel = dest - as->code_offset;
531 if (dest >= 0 && rel < 0) {
532 // is a backwards jump, so we know the size of the jump on the first pass
533 // calculate rel assuming 8 bit relative jump
534 rel -= 2;
535 if (SIGNED_FIT8(rel)) {
536 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100537 } else {
Damien054848a2013-10-05 13:44:41 +0100538 rel += 2;
539 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100540 }
Damien054848a2013-10-05 13:44:41 +0100541 } else {
542 // is a forwards jump, so need to assume it's large
543 large_jump:
544 rel -= 6;
545 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
546 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100547 }
548}
549
Damien Georgecd82e022014-02-02 13:11:48 +0000550void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien429d7192013-10-04 19:53:11 +0100551 asm_x64_push_r64(as, REG_RBP);
552 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
553 if (num_locals < 0) {
554 num_locals = 0;
555 }
556 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
557 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
558 asm_x64_push_r64(as, REG_RBX);
Damien Georgecd82e022014-02-02 13:11:48 +0000559 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100560}
561
Damien Georgecd82e022014-02-02 13:11:48 +0000562void asm_x64_exit(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100563 asm_x64_pop_r64(as, REG_RBX);
564 asm_x64_write_byte_1(as, OPCODE_LEAVE);
565 asm_x64_ret(as);
566}
567
Damien Georgecd82e022014-02-02 13:11:48 +0000568void asm_x64_push_arg(asm_x64_t *as, int src_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100569 assert(0);
570 asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
571}
572
Damien Georgecd82e022014-02-02 13:11:48 +0000573void asm_x64_mov_arg_to_r32(asm_x64_t *as, int src_arg_num, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100574 assert(0);
575 //asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
576}
577
Damien Georgecd82e022014-02-02 13:11:48 +0000578void asm_x64_mov_r32_to_arg(asm_x64_t *as, int src_r32, int dest_arg_num) {
Damien429d7192013-10-04 19:53:11 +0100579 assert(0);
580 //asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
581}
582
Damien Georgecd82e022014-02-02 13:11:48 +0000583// locals:
584// - stored on the stack in ascending order
585// - numbered 0 through as->num_locals-1
586// - RBP points above the last local
587//
588// | RPB
589// v
590// l0 l1 l2 ... l(n-1)
591// ^ ^
592// | low address | high address in RAM
593//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200594STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000595 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100596}
597
Damien Georgecd82e022014-02-02 13:11:48 +0000598void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
599 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 +0100600}
601
Damien Georgecd82e022014-02-02 13:11:48 +0000602void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
603 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 +0100604}
605
Damien Georgecd82e022014-02-02 13:11:48 +0000606void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
607 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100608 if (offset == 0) {
609 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
610 } else {
611 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
612 }
613}
614
Damien Georgecd82e022014-02-02 13:11:48 +0000615void asm_x64_push_local(asm_x64_t *as, int local_num) {
616 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100617}
618
Damien Georgecd82e022014-02-02 13:11:48 +0000619void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
Damien429d7192013-10-04 19:53:11 +0100620{
621 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
Damien Georgecd82e022014-02-02 13:11:48 +0000622 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100623 asm_x64_push_r64(as, temp_r64);
624}
625
626/*
627 can't use these because code might be relocated when resized
628
Damien Georgecd82e022014-02-02 13:11:48 +0000629void asm_x64_call(asm_x64_t *as, void* func)
Damien429d7192013-10-04 19:53:11 +0100630{
631 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
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_mov_r64_to_r64(as, REG_RBP, REG_RSP);
635}
636
Damien Georgecd82e022014-02-02 13:11:48 +0000637void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
Damien429d7192013-10-04 19:53:11 +0100638{
639 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
640 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
641 asm_x64_push_i32(as, i1);
642 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
643 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
644 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
645 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
646}
647*/
648
Damien Georgecd82e022014-02-02 13:11:48 +0000649void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George212c2962013-12-30 12:52:32 +0000650#ifdef __LP64__
651 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
652#else
653 // If we get here, sizeof(int) == sizeof(void*).
654 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
655#endif
Damien429d7192013-10-04 19:53:11 +0100656 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 +0100657 // 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 +0100658 // doesn't work anymore because calls are 64 bits away
659 /*
Damien429d7192013-10-04 19:53:11 +0100660 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
661 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100662 */
Damien429d7192013-10-04 19:53:11 +0100663}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000664
665#endif // MICROPY_EMIT_X64