blob: ca461e0c2e5ec729235bc73af7cccbedee2388d6 [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"
9
10/* all offsets are measured in multiples of 8 bytes */
11#define WORD_SIZE (8)
12
13#define OPCODE_NOP (0x90)
14#define OPCODE_PUSH_R64 (0x50)
15#define OPCODE_PUSH_I64 (0x68)
16#define OPCODE_PUSH_M64 (0xff) /* /6 */
17#define OPCODE_POP_R64 (0x58)
18#define OPCODE_RET (0xc3)
19#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
20#define OPCODE_MOV_I64_TO_R64 (0xb8)
21#define OPCODE_MOV_I32_TO_RM32 (0xc7)
22#define OPCODE_MOV_R64_TO_RM64 (0x89)
23#define OPCODE_MOV_RM64_TO_R64 (0x8b)
24#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
25#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
26#define OPCODE_ADD_R64_TO_RM64 (0x01)
27#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
28#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
29#define OPCODE_SUB_R64_FROM_RM64 (0x29)
30#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
31#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
32#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
33#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
34#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
35#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
36#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
37#define OPCODE_CMP_R64_WITH_RM64 (0x39)
38#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
39#define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
40#define OPCODE_JMP_REL8 (0xeb)
41#define OPCODE_JMP_REL32 (0xe9)
42#define OPCODE_JCC_REL8 (0x70) /* | jcc type */
43#define OPCODE_JCC_REL32_A (0x0f)
44#define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
45#define OPCODE_SETCC_RM8_A (0x0f)
46#define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
47#define OPCODE_CALL_REL32 (0xe8)
48#define OPCODE_CALL_RM32 (0xff) /* /2 */
49#define OPCODE_LEAVE (0xc9)
50
51#define MODRM_R64(x) ((x) << 3)
52#define MODRM_RM_DISP0 (0x00)
53#define MODRM_RM_DISP8 (0x40)
54#define MODRM_RM_DISP32 (0x80)
55#define MODRM_RM_REG (0xc0)
56#define MODRM_RM_R64(x) (x)
57
58#define REX_PREFIX (0x40)
59#define REX_W (0x08) // width
60#define REX_R (0x04) // register
61#define REX_X (0x02) // index
62#define REX_B (0x01) // base
63
64#define IMM32_L0(x) ((x) & 0xff)
65#define IMM32_L1(x) (((x) >> 8) & 0xff)
66#define IMM32_L2(x) (((x) >> 16) & 0xff)
67#define IMM32_L3(x) (((x) >> 24) & 0xff)
68#define IMM64_L4(x) (((x) >> 32) & 0xff)
69#define IMM64_L5(x) (((x) >> 40) & 0xff)
70#define IMM64_L6(x) (((x) >> 48) & 0xff)
71#define IMM64_L7(x) (((x) >> 56) & 0xff)
72
73#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
74#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
75#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
76
77struct _asm_x64_t {
78 int pass;
79 uint code_offset;
80 uint code_size;
81 byte *code_base;
82 byte dummy_data[8];
83
84 int next_label;
85 int max_num_labels;
86 int *label_offsets;
87};
88
89// for allocating memory, see src/v8/src/platform-linux.cc
90void *alloc_mem(uint req_size, uint *alloc_size, bool is_exec) {
91 req_size = (req_size + 0xfff) & (~0xfff);
92 int prot = PROT_READ | PROT_WRITE | (is_exec ? PROT_EXEC : 0);
93 void *ptr = mmap(NULL, req_size, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
94 if (ptr == MAP_FAILED) {
95 assert(0);
96 }
97 *alloc_size = req_size;
98 return ptr;
99}
100
101asm_x64_t* asm_x64_new() {
102 asm_x64_t* as;
103
104 as = m_new(asm_x64_t, 1);
105 as->pass = 0;
106 as->code_offset = 0;
107 as->code_size = 0;
108 as->code_base = NULL;
109 as->label_offsets = NULL;
110
111 return as;
112}
113
114void asm_x64_free(asm_x64_t* as, bool free_code) {
115 if (free_code) {
Damien415eb6f2013-10-05 12:19:06 +0100116 // need to un-mmap
117 //m_free(as->code_base);
Damien429d7192013-10-04 19:53:11 +0100118 }
119 /*
120 if (as->label != NULL) {
121 int i;
122 for (i = 0; i < as->label->len; ++i)
123 {
124 Label* lab = &g_array_index(as->label, Label, i);
125 if (lab->unresolved != NULL)
126 g_array_free(lab->unresolved, true);
127 }
128 g_array_free(as->label, true);
129 }
130 */
131 m_free(as);
132}
133
134void asm_x64_start_pass(asm_x64_t *as, int pass) {
135 as->pass = pass;
136 as->code_offset = 0;
137 as->next_label = 1;
138 if (pass == ASM_X64_PASS_1) {
139 as->max_num_labels = 0;
140 } else {
141 if (pass == ASM_X64_PASS_2) {
142 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
143 }
144 }
145}
146
147void asm_x64_end_pass(asm_x64_t *as) {
148 if (as->pass == ASM_X64_PASS_1) {
149 // calculate number of labels need
150 if (as->next_label > as->max_num_labels) {
151 as->max_num_labels = as->next_label;
152 }
153 as->label_offsets = m_new(int, as->max_num_labels);
154 } else if (as->pass == ASM_X64_PASS_2) {
155 // calculate size of code in bytes
156 as->code_size = as->code_offset;
157 as->code_base = m_new(byte, as->code_size);
Damien415eb6f2013-10-05 12:19:06 +0100158 //uint actual_alloc;
159 //as->code_base = alloc_mem(as->code_size, &actual_alloc, true);
Damien429d7192013-10-04 19:53:11 +0100160 printf("code_size: %u\n", as->code_size);
161 }
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) {
454 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));
455}
456
457void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8) {
458 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));
459}
460
461int asm_x64_label_new(asm_x64_t* as) {
462 return as->next_label++;
463}
464
465void asm_x64_label_assign(asm_x64_t* as, int label) {
466 if (as->pass > ASM_X64_PASS_1) {
467 assert(label < as->max_num_labels);
468 if (as->pass == ASM_X64_PASS_2) {
469 // assign label offset
470 assert(as->label_offsets[label] == -1);
471 as->label_offsets[label] = as->code_offset;
472 } else if (as->pass == ASM_X64_PASS_3) {
473 // ensure label offset has not changed from PASS_2 to PASS_3
474 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
475 assert(as->label_offsets[label] == as->code_offset);
476 }
477 }
478}
479
480void asm_x64_jmp_label(asm_x64_t* as, int label) {
481 if (as->pass > ASM_X64_PASS_1) {
482 int dest = as->label_offsets[label];
483 int rel = dest - as->code_offset;
484 if (dest >= 0 && rel < 0) {
485 // is a backwards jump, so we know the size of the jump on the first pass
486 // calculate rel assuming 8 bit relative jump
487 rel -= 2;
488 if (SIGNED_FIT8(rel)) {
489 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
490 } else {
491 rel += 2;
492 goto large_jump;
493 }
494 } else {
495 // is a forwards jump, so need to assume it's large
496 large_jump:
497 rel -= 5;
498 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
499 asm_x64_write_word32(as, rel);
500 }
501 }
502}
503
504void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, int label) {
505 if (as->pass > ASM_X64_PASS_1) {
506 int dest = as->label_offsets[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);
514 } else {
515 rel += 2;
516 goto large_jump;
517 }
518 } 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);
524 }
525 }
526}
527
528void asm_x64_entry(asm_x64_t* as, int num_locals) {
529 asm_x64_push_r64(as, REG_RBP);
530 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
531 if (num_locals < 0) {
532 num_locals = 0;
533 }
534 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
535 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
536 asm_x64_push_r64(as, REG_RBX);
537}
538
539void asm_x64_exit(asm_x64_t* as) {
540 asm_x64_pop_r64(as, REG_RBX);
541 asm_x64_write_byte_1(as, OPCODE_LEAVE);
542 asm_x64_ret(as);
543}
544
545void asm_x64_push_arg(asm_x64_t* as, int src_arg_num) {
546 assert(0);
547 asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
548}
549
550void asm_x64_mov_arg_to_r32(asm_x64_t* as, int src_arg_num, int dest_r32) {
551 assert(0);
552 //asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
553}
554
555void asm_x64_mov_r32_to_arg(asm_x64_t* as, int src_r32, int dest_arg_num) {
556 assert(0);
557 //asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
558}
559
560static int asm_x64_local_offset_from_ebp(int local_num)
561{
562 return -(local_num + 1) * WORD_SIZE;
563}
564
565void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64) {
566 asm_x64_mov_disp_to_r64(as, REG_RBP, asm_x64_local_offset_from_ebp(src_local_num), dest_r64);
567}
568
569void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num) {
570 asm_x64_mov_r64_to_disp(as, src_r64, REG_RBP, asm_x64_local_offset_from_ebp(dest_local_num));
571}
572
573void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64) {
574 int offset = asm_x64_local_offset_from_ebp(local_num);
575 if (offset == 0) {
576 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
577 } else {
578 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
579 }
580}
581
582void asm_x64_push_local(asm_x64_t* as, int local_num) {
583 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(local_num));
584}
585
586void asm_x64_push_local_addr(asm_x64_t* as, int local_num, int temp_r64)
587{
588 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
589 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(local_num), temp_r64);
590 asm_x64_push_r64(as, temp_r64);
591}
592
593/*
594 can't use these because code might be relocated when resized
595
596void asm_x64_call(asm_x64_t* as, void* func)
597{
598 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
599 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
600 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
601 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
602}
603
604void asm_x64_call_i1(asm_x64_t* as, void* func, int i1)
605{
606 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
607 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
608 asm_x64_push_i32(as, i1);
609 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
610 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
611 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
612 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
613}
614*/
615
616void asm_x64_call_ind(asm_x64_t* as, void *ptr, int temp_r64) {
Damien429d7192013-10-04 19:53:11 +0100617 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
618 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 +0100619 // 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 +0100620 // doesn't work anymore because calls are 64 bits away
621 /*
Damien429d7192013-10-04 19:53:11 +0100622 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
623 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100624 */
Damien429d7192013-10-04 19:53:11 +0100625}