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