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