blob: ed9ca80f5cdf1503493f3eb545fdc7fd9346dc15 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
2#include <assert.h>
3#include <sys/types.h>
4#include <sys/mman.h>
5#include <string.h>
6
7#include "misc.h"
8#include "asmx64.h"
Damien Georgee67ed5d2014-01-04 13:55:24 +00009#include "mpconfig.h"
10
11// wrapper around everything in this file
12#if MICROPY_EMIT_X64
Damien429d7192013-10-04 19:53:11 +010013
Damien Georgeebd2e872014-01-02 20:28:12 +000014#if defined(__OpenBSD__) || defined(__MACH__)
Edd Barrett67ab5ee2014-01-01 23:16:27 +000015#define MAP_ANONYMOUS MAP_ANON
16#endif
17
Damien429d7192013-10-04 19:53:11 +010018/* all offsets are measured in multiples of 8 bytes */
19#define WORD_SIZE (8)
20
21#define OPCODE_NOP (0x90)
22#define OPCODE_PUSH_R64 (0x50)
23#define OPCODE_PUSH_I64 (0x68)
24#define OPCODE_PUSH_M64 (0xff) /* /6 */
25#define OPCODE_POP_R64 (0x58)
26#define OPCODE_RET (0xc3)
27#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
28#define OPCODE_MOV_I64_TO_R64 (0xb8)
29#define OPCODE_MOV_I32_TO_RM32 (0xc7)
30#define OPCODE_MOV_R64_TO_RM64 (0x89)
31#define OPCODE_MOV_RM64_TO_R64 (0x8b)
32#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
33#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
34#define OPCODE_ADD_R64_TO_RM64 (0x01)
35#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
36#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
37#define OPCODE_SUB_R64_FROM_RM64 (0x29)
38#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
39#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
40#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
41#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
42#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
43#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
44#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
45#define OPCODE_CMP_R64_WITH_RM64 (0x39)
46#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
47#define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
48#define OPCODE_JMP_REL8 (0xeb)
49#define OPCODE_JMP_REL32 (0xe9)
50#define OPCODE_JCC_REL8 (0x70) /* | jcc type */
51#define OPCODE_JCC_REL32_A (0x0f)
52#define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
53#define OPCODE_SETCC_RM8_A (0x0f)
54#define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
55#define OPCODE_CALL_REL32 (0xe8)
56#define OPCODE_CALL_RM32 (0xff) /* /2 */
57#define OPCODE_LEAVE (0xc9)
58
59#define MODRM_R64(x) ((x) << 3)
60#define MODRM_RM_DISP0 (0x00)
61#define MODRM_RM_DISP8 (0x40)
62#define MODRM_RM_DISP32 (0x80)
63#define MODRM_RM_REG (0xc0)
64#define MODRM_RM_R64(x) (x)
65
66#define REX_PREFIX (0x40)
67#define REX_W (0x08) // width
68#define REX_R (0x04) // register
69#define REX_X (0x02) // index
70#define REX_B (0x01) // base
71
72#define IMM32_L0(x) ((x) & 0xff)
73#define IMM32_L1(x) (((x) >> 8) & 0xff)
74#define IMM32_L2(x) (((x) >> 16) & 0xff)
75#define IMM32_L3(x) (((x) >> 24) & 0xff)
76#define IMM64_L4(x) (((x) >> 32) & 0xff)
77#define IMM64_L5(x) (((x) >> 40) & 0xff)
78#define IMM64_L6(x) (((x) >> 48) & 0xff)
79#define IMM64_L7(x) (((x) >> 56) & 0xff)
80
81#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
82#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
83#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
84
85struct _asm_x64_t {
86 int pass;
87 uint code_offset;
88 uint code_size;
89 byte *code_base;
90 byte dummy_data[8];
91
Damien054848a2013-10-05 13:44:41 +010092 uint max_num_labels;
Damien429d7192013-10-04 19:53:11 +010093 int *label_offsets;
94};
95
96// for allocating memory, see src/v8/src/platform-linux.cc
97void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
98 req_size = (req_size + 0xfff) & (~0xfff);
99 int prot = PROT_READ | PROT_WRITE | (is_exec ? PROT_EXEC : 0);
100 void *ptr = mmap(NULL, req_size, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
101 if (ptr == MAP_FAILED) {
102 assert(0);
103 }
104 *alloc_size = req_size;
105 return ptr;
106}
107
Damien054848a2013-10-05 13:44:41 +0100108asm_x64_t* asm_x64_new(uint max_num_labels) {
Damien429d7192013-10-04 19:53:11 +0100109 asm_x64_t* as;
110
111 as = m_new(asm_x64_t, 1);
112 as->pass = 0;
113 as->code_offset = 0;
114 as->code_size = 0;
115 as->code_base = NULL;
Damien054848a2013-10-05 13:44:41 +0100116 as->max_num_labels = max_num_labels;
117 as->label_offsets = m_new(int, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100118
119 return as;
120}
121
122void asm_x64_free(asm_x64_t* as, bool free_code) {
123 if (free_code) {
Damien415eb6f2013-10-05 12:19:06 +0100124 // need to un-mmap
125 //m_free(as->code_base);
Damien429d7192013-10-04 19:53:11 +0100126 }
127 /*
128 if (as->label != NULL) {
129 int i;
130 for (i = 0; i < as->label->len; ++i)
131 {
132 Label* lab = &g_array_index(as->label, Label, i);
133 if (lab->unresolved != NULL)
134 g_array_free(lab->unresolved, true);
135 }
136 g_array_free(as->label, true);
137 }
138 */
Damien732407f2013-12-29 19:33:23 +0000139 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100140}
141
142void asm_x64_start_pass(asm_x64_t *as, int pass) {
143 as->pass = pass;
144 as->code_offset = 0;
Damien054848a2013-10-05 13:44:41 +0100145 if (pass == ASM_X64_PASS_2) {
146 // reset all labels
147 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
Damien429d7192013-10-04 19:53:11 +0100148 }
149}
150
151void asm_x64_end_pass(asm_x64_t *as) {
Damien054848a2013-10-05 13:44:41 +0100152 if (as->pass == ASM_X64_PASS_2) {
Damien429d7192013-10-04 19:53:11 +0100153 // calculate size of code in bytes
154 as->code_size = as->code_offset;
Damien054848a2013-10-05 13:44:41 +0100155 //as->code_base = m_new(byte, as->code_size); need to allocale executable memory
156 uint actual_alloc;
157 as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
Damien429d7192013-10-04 19:53:11 +0100158 printf("code_size: %u\n", as->code_size);
159 }
160
161 /*
162 // check labels are resolved
163 if (as->label != NULL)
164 {
165 int i;
166 for (i = 0; i < as->label->len; ++i)
167 if (g_array_index(as->label, Label, i).unresolved != NULL)
168 return false;
169 }
170 */
171}
172
173// all functions must go through this one to emit bytes
174static byte* asm_x64_get_cur_to_write_bytes(asm_x64_t* as, int num_bytes_to_write) {
175 //printf("emit %d\n", num_bytes_to_write);
176 if (as->pass < ASM_X64_PASS_3) {
177 as->code_offset += num_bytes_to_write;
178 return as->dummy_data;
179 } else {
180 assert(as->code_offset + num_bytes_to_write <= as->code_size);
181 byte *c = as->code_base + as->code_offset;
182 as->code_offset += num_bytes_to_write;
183 return c;
184 }
185}
186
187uint asm_x64_get_code_size(asm_x64_t* as) {
188 return as->code_size;
189}
190
191void* asm_x64_get_code(asm_x64_t* as) {
192 return as->code_base;
193}
194
195static void asm_x64_write_byte_1(asm_x64_t* as, byte b1) {
196 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
197 c[0] = b1;
198}
199
200static void asm_x64_write_byte_2(asm_x64_t* as, byte b1, byte b2) {
201 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
202 c[0] = b1;
203 c[1] = b2;
204}
205
206static void asm_x64_write_byte_3(asm_x64_t* as, byte b1, byte b2, byte b3) {
207 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
208 c[0] = b1;
209 c[1] = b2;
210 c[2] = b3;
211}
212
213static void asm_x64_write_word32(asm_x64_t* as, int w32) {
214 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
215 c[0] = IMM32_L0(w32);
216 c[1] = IMM32_L1(w32);
217 c[2] = IMM32_L2(w32);
218 c[3] = IMM32_L3(w32);
219}
220
221static void asm_x64_write_word64(asm_x64_t* as, int64_t w64) {
222 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
223 c[0] = IMM32_L0(w64);
224 c[1] = IMM32_L1(w64);
225 c[2] = IMM32_L2(w64);
226 c[3] = IMM32_L3(w64);
227 c[4] = IMM64_L4(w64);
228 c[5] = IMM64_L5(w64);
229 c[6] = IMM64_L6(w64);
230 c[7] = IMM64_L7(w64);
231}
232
233/* unused
234static void asm_x64_write_word32_to(asm_x64_t* as, int offset, int w32) {
235 byte* c;
236 assert(offset + 4 <= as->code_size);
237 c = as->code_base + offset;
238 c[0] = IMM32_L0(w32);
239 c[1] = IMM32_L1(w32);
240 c[2] = IMM32_L2(w32);
241 c[3] = IMM32_L3(w32);
242}
243*/
244
245static void asm_x64_write_r64_disp(asm_x64_t* as, int r64, int disp_r64, int disp_offset) {
246 assert(disp_r64 != REG_RSP);
247
248 if (disp_offset == 0 && disp_r64 != REG_RBP) {
249 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
250 } else if (SIGNED_FIT8(disp_offset)) {
251 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
252 } else {
253 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
254 asm_x64_write_word32(as, disp_offset);
255 }
256}
257
258void asm_x64_nop(asm_x64_t* as)
259{
260 asm_x64_write_byte_1(as, OPCODE_NOP);
261}
262
263void asm_x64_push_r64(asm_x64_t* as, int src_r64)
264{
265 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
266}
267
268void asm_x64_push_i32(asm_x64_t* as, int src_i32)
269{
270 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
271 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
272}
273
274void asm_x64_push_disp(asm_x64_t* as, int src_r64, int src_offset) {
275 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
276 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
277}
278
279void asm_x64_pop_r64(asm_x64_t* as, int dest_r64)
280{
281 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
282}
283
284static void asm_x64_ret(asm_x64_t* as)
285{
286 asm_x64_write_byte_1(as, OPCODE_RET);
287}
288
289void asm_x64_mov_r32_to_r32(asm_x64_t* as, int src_r32, int dest_r32) {
290 // 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
294void asm_x64_mov_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64) {
295 // 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
299void asm_x64_mov_r64_to_disp(asm_x64_t* as, int src_r64, int dest_r64, int dest_disp) {
300 // 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
305void asm_x64_mov_disp_to_r64(asm_x64_t* as, int src_r64, int src_disp, int dest_r64) {
306 // 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
311void asm_x64_lea_disp_to_r64(asm_x64_t* as, int src_r64, int src_disp, int dest_r64) {
312 // 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
321void asm_x64_mov_i32_to_r64(asm_x64_t* as, int src_i32, int dest_r64) {
322 // 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
327void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64) {
328 // 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
344void asm_x64_mov_i32_to_disp(asm_x64_t* as, int src_i32, int dest_r32, int dest_disp)
345{
346 assert(0);
347 asm_x64_write_byte_1(as, OPCODE_MOV_I32_TO_RM32);
348 //asm_x64_write_r32_disp(as, 0, dest_r32, dest_disp);
349 asm_x64_write_word32(as, src_i32);
350}
351
352void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
353 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));
354}
355
356void asm_x64_add_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64) {
357 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));
358}
359
360void asm_x64_add_i32_to_r32(asm_x64_t* as, int src_i32, int dest_r32)
361{
362 assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
363 if (SIGNED_FIT8(src_i32))
364 {
365 asm_x64_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
366 asm_x64_write_byte_1(as, src_i32 & 0xff);
367 }
368 else
369 {
370 asm_x64_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
371 asm_x64_write_word32(as, src_i32);
372 }
373}
374
375void asm_x64_sub_r32_from_r32(asm_x64_t* as, int src_r32, int dest_r32) {
376 // defaults to 32 bit operation
377 asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
378}
379
380void asm_x64_sub_r64_from_r64(asm_x64_t* as, int src_r64, int dest_r64) {
381 // use REX prefix for 64 bit operation
382 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));
383}
384
385void asm_x64_sub_i32_from_r32(asm_x64_t* as, int src_i32, int dest_r32) {
386 if (SIGNED_FIT8(src_i32)) {
387 // defaults to 32 bit operation
388 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
389 asm_x64_write_byte_1(as, src_i32 & 0xff);
390 } else {
391 // defaults to 32 bit operation
392 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
393 asm_x64_write_word32(as, src_i32);
394 }
395}
396
397void asm_x64_sub_i32_from_r64(asm_x64_t* as, int src_i32, int dest_r64) {
398 if (SIGNED_FIT8(src_i32)) {
399 // use REX prefix for 64 bit operation
400 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));
401 asm_x64_write_byte_1(as, src_i32 & 0xff);
402 } else {
403 // use REX prefix for 64 bit operation
404 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));
405 asm_x64_write_word32(as, src_i32);
406 }
407}
408
409/* shifts not tested */
410void asm_x64_shl_r32_by_imm(asm_x64_t* as, int r32, int imm) {
411 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
412 asm_x64_write_byte_1(as, imm);
413}
414
415void asm_x64_shr_r32_by_imm(asm_x64_t* as, int r32, int imm) {
416 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
417 asm_x64_write_byte_1(as, imm);
418}
419
420void asm_x64_sar_r32_by_imm(asm_x64_t* as, int r32, int imm) {
421 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
422 asm_x64_write_byte_1(as, imm);
423}
424
425void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b) {
426 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));
427}
428
429void asm_x64_cmp_r32_with_disp(asm_x64_t* as, int src_r32_a, int src_r32_b, int src_disp_b) {
430 assert(0);
431 asm_x64_write_byte_1(as, OPCODE_CMP_R64_WITH_RM64);
432 //asm_x64_write_r32_disp(as, src_r32_a, src_r32_b, src_disp_b);
433}
434
435void asm_x64_cmp_disp_with_r32(asm_x64_t* as, int src_r32_a, int src_disp_a, int src_r32_b) {
436 assert(0);
437 asm_x64_write_byte_1(as, OPCODE_CMP_RM32_WITH_R32);
438 //asm_x64_write_r32_disp(as, src_r32_b, src_r32_a, src_disp_a);
439}
440
441void asm_x64_cmp_i32_with_r32(asm_x64_t* as, int src_i32, int src_r32) {
442 if (SIGNED_FIT8(src_i32)) {
443 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
444 asm_x64_write_byte_1(as, src_i32 & 0xff);
445 } else {
446 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
447 asm_x64_write_word32(as, src_i32);
448 }
449}
450
451void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100452 // TODO implement for other registers
453 assert(src_r64_a == REG_RAX);
454 assert(src_r64_b == REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100455 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));
456}
457
458void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8) {
459 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));
460}
461
Damien429d7192013-10-04 19:53:11 +0100462void asm_x64_label_assign(asm_x64_t* as, int label) {
Damien054848a2013-10-05 13:44:41 +0100463 assert(label < as->max_num_labels);
464 if (as->pass == ASM_X64_PASS_2) {
465 // assign label offset
466 assert(as->label_offsets[label] == -1);
467 as->label_offsets[label] = as->code_offset;
468 } else if (as->pass == ASM_X64_PASS_3) {
469 // ensure label offset has not changed from PASS_2 to PASS_3
470 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
471 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100472 }
473}
474
Damien054848a2013-10-05 13:44:41 +0100475static int get_label_dest(asm_x64_t *as, int label) {
476 assert(label < as->max_num_labels);
477 return as->label_offsets[label];
478}
479
480void asm_x64_jmp_label(asm_x64_t *as, int label) {
481 int dest = get_label_dest(as, label);
482 int rel = dest - as->code_offset;
483 if (dest >= 0 && rel < 0) {
484 // is a backwards jump, so we know the size of the jump on the first pass
485 // calculate rel assuming 8 bit relative jump
486 rel -= 2;
487 if (SIGNED_FIT8(rel)) {
488 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100489 } else {
Damien054848a2013-10-05 13:44:41 +0100490 rel += 2;
491 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100492 }
Damien054848a2013-10-05 13:44:41 +0100493 } else {
494 // is a forwards jump, so need to assume it's large
495 large_jump:
496 rel -= 5;
497 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
498 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100499 }
500}
501
Damien054848a2013-10-05 13:44:41 +0100502void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
503 int dest = get_label_dest(as, label);
504 int rel = dest - as->code_offset;
505 if (dest >= 0 && rel < 0) {
506 // is a backwards jump, so we know the size of the jump on the first pass
507 // calculate rel assuming 8 bit relative jump
508 rel -= 2;
509 if (SIGNED_FIT8(rel)) {
510 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100511 } else {
Damien054848a2013-10-05 13:44:41 +0100512 rel += 2;
513 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100514 }
Damien054848a2013-10-05 13:44:41 +0100515 } else {
516 // is a forwards jump, so need to assume it's large
517 large_jump:
518 rel -= 6;
519 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
520 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100521 }
522}
523
524void asm_x64_entry(asm_x64_t* as, int num_locals) {
525 asm_x64_push_r64(as, REG_RBP);
526 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
527 if (num_locals < 0) {
528 num_locals = 0;
529 }
530 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
531 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
532 asm_x64_push_r64(as, REG_RBX);
533}
534
535void asm_x64_exit(asm_x64_t* as) {
536 asm_x64_pop_r64(as, REG_RBX);
537 asm_x64_write_byte_1(as, OPCODE_LEAVE);
538 asm_x64_ret(as);
539}
540
541void asm_x64_push_arg(asm_x64_t* as, int src_arg_num) {
542 assert(0);
543 asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
544}
545
546void asm_x64_mov_arg_to_r32(asm_x64_t* as, int src_arg_num, int dest_r32) {
547 assert(0);
548 //asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
549}
550
551void asm_x64_mov_r32_to_arg(asm_x64_t* as, int src_r32, int dest_arg_num) {
552 assert(0);
553 //asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
554}
555
556static int asm_x64_local_offset_from_ebp(int local_num)
557{
558 return -(local_num + 1) * WORD_SIZE;
559}
560
561void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64) {
562 asm_x64_mov_disp_to_r64(as, REG_RBP, asm_x64_local_offset_from_ebp(src_local_num), dest_r64);
563}
564
565void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num) {
566 asm_x64_mov_r64_to_disp(as, src_r64, REG_RBP, asm_x64_local_offset_from_ebp(dest_local_num));
567}
568
569void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64) {
570 int offset = asm_x64_local_offset_from_ebp(local_num);
571 if (offset == 0) {
572 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
573 } else {
574 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
575 }
576}
577
578void asm_x64_push_local(asm_x64_t* as, int local_num) {
579 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(local_num));
580}
581
582void asm_x64_push_local_addr(asm_x64_t* as, int local_num, int temp_r64)
583{
584 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
585 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(local_num), temp_r64);
586 asm_x64_push_r64(as, temp_r64);
587}
588
589/*
590 can't use these because code might be relocated when resized
591
592void asm_x64_call(asm_x64_t* as, void* func)
593{
594 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
595 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
596 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
597 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
598}
599
600void asm_x64_call_i1(asm_x64_t* as, void* func, int i1)
601{
602 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
603 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
604 asm_x64_push_i32(as, i1);
605 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
606 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
607 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
608 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
609}
610*/
611
612void asm_x64_call_ind(asm_x64_t* as, void *ptr, int temp_r64) {
Damien George212c2962013-12-30 12:52:32 +0000613#ifdef __LP64__
614 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
615#else
616 // If we get here, sizeof(int) == sizeof(void*).
617 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
618#endif
Damien429d7192013-10-04 19:53:11 +0100619 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 +0100620 // 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 +0100621 // doesn't work anymore because calls are 64 bits away
622 /*
Damien429d7192013-10-04 19:53:11 +0100623 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
624 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100625 */
Damien429d7192013-10-04 19:53:11 +0100626}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000627
628#endif // MICROPY_EMIT_X64