blob: 59c8113bc4969946c9b581bb53a296d48657f36f [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) {
116 m_free(as->code_base);
117 }
118 /*
119 if (as->label != NULL) {
120 int i;
121 for (i = 0; i < as->label->len; ++i)
122 {
123 Label* lab = &g_array_index(as->label, Label, i);
124 if (lab->unresolved != NULL)
125 g_array_free(lab->unresolved, true);
126 }
127 g_array_free(as->label, true);
128 }
129 */
130 m_free(as);
131}
132
133void asm_x64_start_pass(asm_x64_t *as, int pass) {
134 as->pass = pass;
135 as->code_offset = 0;
136 as->next_label = 1;
137 if (pass == ASM_X64_PASS_1) {
138 as->max_num_labels = 0;
139 } else {
140 if (pass == ASM_X64_PASS_2) {
141 memset(as->label_offsets, -1, as->max_num_labels * sizeof(int));
142 }
143 }
144}
145
146void asm_x64_end_pass(asm_x64_t *as) {
147 if (as->pass == ASM_X64_PASS_1) {
148 // calculate number of labels need
149 if (as->next_label > as->max_num_labels) {
150 as->max_num_labels = as->next_label;
151 }
152 as->label_offsets = m_new(int, as->max_num_labels);
153 } else if (as->pass == ASM_X64_PASS_2) {
154 // calculate size of code in bytes
155 as->code_size = as->code_offset;
156 as->code_base = m_new(byte, as->code_size);
157 printf("code_size: %u\n", as->code_size);
158 }
159
160 /*
161 // check labels are resolved
162 if (as->label != NULL)
163 {
164 int i;
165 for (i = 0; i < as->label->len; ++i)
166 if (g_array_index(as->label, Label, i).unresolved != NULL)
167 return false;
168 }
169 */
170}
171
172// all functions must go through this one to emit bytes
173static byte* asm_x64_get_cur_to_write_bytes(asm_x64_t* as, int num_bytes_to_write) {
174 //printf("emit %d\n", num_bytes_to_write);
175 if (as->pass < ASM_X64_PASS_3) {
176 as->code_offset += num_bytes_to_write;
177 return as->dummy_data;
178 } else {
179 assert(as->code_offset + num_bytes_to_write <= as->code_size);
180 byte *c = as->code_base + as->code_offset;
181 as->code_offset += num_bytes_to_write;
182 return c;
183 }
184}
185
186uint asm_x64_get_code_size(asm_x64_t* as) {
187 return as->code_size;
188}
189
190void* asm_x64_get_code(asm_x64_t* as) {
191 return as->code_base;
192}
193
194static void asm_x64_write_byte_1(asm_x64_t* as, byte b1) {
195 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
196 c[0] = b1;
197}
198
199static void asm_x64_write_byte_2(asm_x64_t* as, byte b1, byte b2) {
200 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
201 c[0] = b1;
202 c[1] = b2;
203}
204
205static void asm_x64_write_byte_3(asm_x64_t* as, byte b1, byte b2, byte b3) {
206 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
207 c[0] = b1;
208 c[1] = b2;
209 c[2] = b3;
210}
211
212static void asm_x64_write_word32(asm_x64_t* as, int w32) {
213 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
214 c[0] = IMM32_L0(w32);
215 c[1] = IMM32_L1(w32);
216 c[2] = IMM32_L2(w32);
217 c[3] = IMM32_L3(w32);
218}
219
220static void asm_x64_write_word64(asm_x64_t* as, int64_t w64) {
221 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
222 c[0] = IMM32_L0(w64);
223 c[1] = IMM32_L1(w64);
224 c[2] = IMM32_L2(w64);
225 c[3] = IMM32_L3(w64);
226 c[4] = IMM64_L4(w64);
227 c[5] = IMM64_L5(w64);
228 c[6] = IMM64_L6(w64);
229 c[7] = IMM64_L7(w64);
230}
231
232/* unused
233static void asm_x64_write_word32_to(asm_x64_t* as, int offset, int w32) {
234 byte* c;
235 assert(offset + 4 <= as->code_size);
236 c = as->code_base + offset;
237 c[0] = IMM32_L0(w32);
238 c[1] = IMM32_L1(w32);
239 c[2] = IMM32_L2(w32);
240 c[3] = IMM32_L3(w32);
241}
242*/
243
244static void asm_x64_write_r64_disp(asm_x64_t* as, int r64, int disp_r64, int disp_offset) {
245 assert(disp_r64 != REG_RSP);
246
247 if (disp_offset == 0 && disp_r64 != REG_RBP) {
248 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
249 } else if (SIGNED_FIT8(disp_offset)) {
250 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
251 } else {
252 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
253 asm_x64_write_word32(as, disp_offset);
254 }
255}
256
257void asm_x64_nop(asm_x64_t* as)
258{
259 asm_x64_write_byte_1(as, OPCODE_NOP);
260}
261
262void asm_x64_push_r64(asm_x64_t* as, int src_r64)
263{
264 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
265}
266
267void asm_x64_push_i32(asm_x64_t* as, int src_i32)
268{
269 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
270 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
271}
272
273void asm_x64_push_disp(asm_x64_t* as, int src_r64, int src_offset) {
274 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
275 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
276}
277
278void asm_x64_pop_r64(asm_x64_t* as, int dest_r64)
279{
280 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
281}
282
283static void asm_x64_ret(asm_x64_t* as)
284{
285 asm_x64_write_byte_1(as, OPCODE_RET);
286}
287
288void asm_x64_mov_r32_to_r32(asm_x64_t* as, int src_r32, int dest_r32) {
289 // defaults to 32 bit operation
290 asm_x64_write_byte_2(as, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
291}
292
293void asm_x64_mov_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64) {
294 // use REX prefix for 64 bit operation
295 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));
296}
297
298void asm_x64_mov_r64_to_disp(asm_x64_t* as, int src_r64, int dest_r64, int dest_disp) {
299 // use REX prefix for 64 bit operation
300 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_R64_TO_RM64);
301 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
302}
303
304void asm_x64_mov_disp_to_r64(asm_x64_t* as, int src_r64, int src_disp, int dest_r64) {
305 // use REX prefix for 64 bit operation
306 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_RM64_TO_R64);
307 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
308}
309
310void asm_x64_lea_disp_to_r64(asm_x64_t* as, int src_r64, int src_disp, int dest_r64) {
311 // use REX prefix for 64 bit operation
312 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
313 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
314}
315
316void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
317 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
318}
319
320void asm_x64_mov_i32_to_r64(asm_x64_t* as, int src_i32, int dest_r64) {
321 // cpu defaults to i32 to r64, with zero extension
322 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
323 asm_x64_write_word32(as, src_i32);
324}
325
326void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64) {
327 // cpu defaults to i32 to r64
328 // to mov i64 to r64 need to use REX prefix
329 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
330 asm_x64_write_word64(as, src_i64);
331}
332
333void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
334 if (UNSIGNED_FIT32(src_i64)) {
335 // 5 bytes
336 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
337 } else {
338 // 10 bytes
339 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
340 }
341}
342
343void asm_x64_mov_i32_to_disp(asm_x64_t* as, int src_i32, int dest_r32, int dest_disp)
344{
345 assert(0);
346 asm_x64_write_byte_1(as, OPCODE_MOV_I32_TO_RM32);
347 //asm_x64_write_r32_disp(as, 0, dest_r32, dest_disp);
348 asm_x64_write_word32(as, src_i32);
349}
350
351void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
352 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));
353}
354
355void asm_x64_add_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_ADD_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
357}
358
359void asm_x64_add_i32_to_r32(asm_x64_t* as, int src_i32, int dest_r32)
360{
361 assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
362 if (SIGNED_FIT8(src_i32))
363 {
364 asm_x64_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
365 asm_x64_write_byte_1(as, src_i32 & 0xff);
366 }
367 else
368 {
369 asm_x64_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
370 asm_x64_write_word32(as, src_i32);
371 }
372}
373
374void asm_x64_sub_r32_from_r32(asm_x64_t* as, int src_r32, int dest_r32) {
375 // defaults to 32 bit operation
376 asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
377}
378
379void asm_x64_sub_r64_from_r64(asm_x64_t* as, int src_r64, int dest_r64) {
380 // use REX prefix for 64 bit operation
381 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));
382}
383
384void asm_x64_sub_i32_from_r32(asm_x64_t* as, int src_i32, int dest_r32) {
385 if (SIGNED_FIT8(src_i32)) {
386 // defaults to 32 bit operation
387 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
388 asm_x64_write_byte_1(as, src_i32 & 0xff);
389 } else {
390 // defaults to 32 bit operation
391 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
392 asm_x64_write_word32(as, src_i32);
393 }
394}
395
396void asm_x64_sub_i32_from_r64(asm_x64_t* as, int src_i32, int dest_r64) {
397 if (SIGNED_FIT8(src_i32)) {
398 // use REX prefix for 64 bit operation
399 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));
400 asm_x64_write_byte_1(as, src_i32 & 0xff);
401 } else {
402 // use REX prefix for 64 bit operation
403 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));
404 asm_x64_write_word32(as, src_i32);
405 }
406}
407
408/* shifts not tested */
409void asm_x64_shl_r32_by_imm(asm_x64_t* as, int r32, int imm) {
410 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
411 asm_x64_write_byte_1(as, imm);
412}
413
414void asm_x64_shr_r32_by_imm(asm_x64_t* as, int r32, int imm) {
415 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
416 asm_x64_write_byte_1(as, imm);
417}
418
419void asm_x64_sar_r32_by_imm(asm_x64_t* as, int r32, int imm) {
420 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
421 asm_x64_write_byte_1(as, imm);
422}
423
424void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b) {
425 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));
426}
427
428void asm_x64_cmp_r32_with_disp(asm_x64_t* as, int src_r32_a, int src_r32_b, int src_disp_b) {
429 assert(0);
430 asm_x64_write_byte_1(as, OPCODE_CMP_R64_WITH_RM64);
431 //asm_x64_write_r32_disp(as, src_r32_a, src_r32_b, src_disp_b);
432}
433
434void asm_x64_cmp_disp_with_r32(asm_x64_t* as, int src_r32_a, int src_disp_a, int src_r32_b) {
435 assert(0);
436 asm_x64_write_byte_1(as, OPCODE_CMP_RM32_WITH_R32);
437 //asm_x64_write_r32_disp(as, src_r32_b, src_r32_a, src_disp_a);
438}
439
440void asm_x64_cmp_i32_with_r32(asm_x64_t* as, int src_i32, int src_r32) {
441 if (SIGNED_FIT8(src_i32)) {
442 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
443 asm_x64_write_byte_1(as, src_i32 & 0xff);
444 } else {
445 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
446 asm_x64_write_word32(as, src_i32);
447 }
448}
449
450void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b) {
451 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));
452}
453
454void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8) {
455 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));
456}
457
458int asm_x64_label_new(asm_x64_t* as) {
459 return as->next_label++;
460}
461
462void asm_x64_label_assign(asm_x64_t* as, int label) {
463 if (as->pass > ASM_X64_PASS_1) {
464 assert(label < as->max_num_labels);
465 if (as->pass == ASM_X64_PASS_2) {
466 // assign label offset
467 assert(as->label_offsets[label] == -1);
468 as->label_offsets[label] = as->code_offset;
469 } else if (as->pass == ASM_X64_PASS_3) {
470 // ensure label offset has not changed from PASS_2 to PASS_3
471 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
472 assert(as->label_offsets[label] == as->code_offset);
473 }
474 }
475}
476
477void asm_x64_jmp_label(asm_x64_t* as, int label) {
478 if (as->pass > ASM_X64_PASS_1) {
479 int dest = as->label_offsets[label];
480 int rel = dest - as->code_offset;
481 if (dest >= 0 && rel < 0) {
482 // is a backwards jump, so we know the size of the jump on the first pass
483 // calculate rel assuming 8 bit relative jump
484 rel -= 2;
485 if (SIGNED_FIT8(rel)) {
486 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
487 } else {
488 rel += 2;
489 goto large_jump;
490 }
491 } else {
492 // is a forwards jump, so need to assume it's large
493 large_jump:
494 rel -= 5;
495 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
496 asm_x64_write_word32(as, rel);
497 }
498 }
499}
500
501void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, int label) {
502 if (as->pass > ASM_X64_PASS_1) {
503 int dest = as->label_offsets[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);
511 } else {
512 rel += 2;
513 goto large_jump;
514 }
515 } 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);
521 }
522 }
523}
524
525void asm_x64_entry(asm_x64_t* as, int num_locals) {
526 asm_x64_push_r64(as, REG_RBP);
527 asm_x64_mov_r64_to_r64(as, REG_RSP, REG_RBP);
528 if (num_locals < 0) {
529 num_locals = 0;
530 }
531 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
532 asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
533 asm_x64_push_r64(as, REG_RBX);
534}
535
536void asm_x64_exit(asm_x64_t* as) {
537 asm_x64_pop_r64(as, REG_RBX);
538 asm_x64_write_byte_1(as, OPCODE_LEAVE);
539 asm_x64_ret(as);
540}
541
542void asm_x64_push_arg(asm_x64_t* as, int src_arg_num) {
543 assert(0);
544 asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
545}
546
547void asm_x64_mov_arg_to_r32(asm_x64_t* as, int src_arg_num, int dest_r32) {
548 assert(0);
549 //asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
550}
551
552void asm_x64_mov_r32_to_arg(asm_x64_t* as, int src_r32, int dest_arg_num) {
553 assert(0);
554 //asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
555}
556
557static int asm_x64_local_offset_from_ebp(int local_num)
558{
559 return -(local_num + 1) * WORD_SIZE;
560}
561
562void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64) {
563 asm_x64_mov_disp_to_r64(as, REG_RBP, asm_x64_local_offset_from_ebp(src_local_num), dest_r64);
564}
565
566void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num) {
567 asm_x64_mov_r64_to_disp(as, src_r64, REG_RBP, asm_x64_local_offset_from_ebp(dest_local_num));
568}
569
570void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64) {
571 int offset = asm_x64_local_offset_from_ebp(local_num);
572 if (offset == 0) {
573 asm_x64_mov_r64_to_r64(as, REG_RBP, dest_r64);
574 } else {
575 asm_x64_lea_disp_to_r64(as, REG_RBP, offset, dest_r64);
576 }
577}
578
579void asm_x64_push_local(asm_x64_t* as, int local_num) {
580 asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(local_num));
581}
582
583void asm_x64_push_local_addr(asm_x64_t* as, int local_num, int temp_r64)
584{
585 asm_x64_mov_r64_to_r64(as, REG_RBP, temp_r64);
586 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(local_num), temp_r64);
587 asm_x64_push_r64(as, temp_r64);
588}
589
590/*
591 can't use these because code might be relocated when resized
592
593void asm_x64_call(asm_x64_t* as, void* func)
594{
595 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
596 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
597 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
598 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
599}
600
601void asm_x64_call_i1(asm_x64_t* as, void* func, int i1)
602{
603 asm_x64_sub_i32_from_r32(as, 8, REG_RSP);
604 asm_x64_sub_i32_from_r32(as, 12, REG_RSP);
605 asm_x64_push_i32(as, i1);
606 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
607 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
608 asm_x64_add_i32_to_r32(as, 16, REG_RSP);
609 asm_x64_mov_r64_to_r64(as, REG_RBP, REG_RSP);
610}
611*/
612
613void asm_x64_call_ind(asm_x64_t* as, void *ptr, int temp_r64) {
614 /*
615 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
616 asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64));
617 */
618 // this reduces code size by 2 bytes per call, but doesn't seem to speed it up at all
619 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
620 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
621}