1cf3bbca00c5f9a7bb752cdd63d56660d9f6c8a2
[tinycc.git] / x86_64-gen.c
blob1cf3bbca00c5f9a7bb752cdd63d56660d9f6c8a2
1 /*
2 * x86-64 code generator for TCC
4 * Copyright (c) 2008 Shinichiro Hamaji
6 * Based on i386-gen.c by Fabrice Bellard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef TARGET_DEFS_ONLY
25 /* number of available registers */
26 #define NB_REGS 22
27 #define NB_ASM_REGS 8
29 /* a register can belong to several classes. The classes must be
30 sorted from more general to more precise (see gv2() code which does
31 assumptions on it). */
32 #define RC_INT 0x0001 /* generic integer register */
33 #define RC_FLOAT 0x0002 /* generic float register */
34 #define RC_RAX 0x0004
35 #define RC_RCX 0x0008
36 #define RC_RDX 0x0010
37 #define RC_ST0 0x0080 /* only for long double */
38 #define RC_R8 0x0100
39 #define RC_R9 0x0200
40 #define RC_R10 0x0400
41 #define RC_R11 0x0800
42 #define RC_XMM0 0x1000
43 #define RC_XMM1 0x2000
44 #define RC_XMM2 0x4000
45 #define RC_XMM3 0x8000
46 #define RC_XMM4 0x10000
47 #define RC_XMM5 0x20000
48 #define RC_IRET RC_RAX /* function return: integer register */
49 #define RC_LRET RC_RDX /* function return: second integer register */
50 #define RC_FRET RC_XMM0 /* function return: float register */
51 #define RC_QRET RC_XMM1 /* function return: second float register */
53 /* pretty names for the registers */
54 enum {
55 TREG_RAX = 0,
56 TREG_RCX = 1,
57 TREG_RDX = 2,
58 TREG_RSI = 6,
59 TREG_RDI = 7,
61 TREG_R8 = 8,
62 TREG_R9 = 9,
63 TREG_R10 = 10,
64 TREG_R11 = 11,
66 TREG_XMM0 = 16,
67 TREG_XMM1 = 17,
68 TREG_XMM2 = 18,
69 TREG_XMM3 = 19,
70 TREG_XMM4 = 20,
71 TREG_XMM5 = 21,
72 TREG_XMM6 = 22,
73 TREG_XMM7 = 23,
75 TREG_ST0 = 4, // SP slot won't be used
77 TREG_MEM = 0x20,
80 #define REX_BASE(reg) (((reg) >> 3) & 1)
81 #define REG_VALUE(reg) ((reg) & 7)
83 /* return registers for function */
84 #define REG_IRET TREG_RAX /* single word int return register */
85 #define REG_LRET TREG_RDX /* second word return register (for long long) */
86 #define REG_FRET TREG_XMM0 /* float return register */
87 #define REG_QRET TREG_XMM1 /* second float return register */
89 /* defined if function parameters must be evaluated in reverse order */
90 #define INVERT_FUNC_PARAMS
92 /* pointer size, in bytes */
93 #define PTR_SIZE 8
95 /* long double size and alignment, in bytes */
96 #define LDOUBLE_SIZE 16
97 #define LDOUBLE_ALIGN 8
98 /* maximum alignment (for aligned attribute support) */
99 #define MAX_ALIGN 8
101 /******************************************************/
102 /* ELF defines */
104 #define EM_TCC_TARGET EM_X86_64
106 /* relocation type for 32 bit data relocation */
107 #define R_DATA_32 R_X86_64_32
108 #define R_DATA_PTR R_X86_64_64
109 #define R_JMP_SLOT R_X86_64_JUMP_SLOT
110 #define R_COPY R_X86_64_COPY
112 #define ELF_START_ADDR 0x08048000
113 #define ELF_PAGE_SIZE 0x1000
115 /******************************************************/
116 #else /* ! TARGET_DEFS_ONLY */
117 /******************************************************/
118 #include "tcc.h"
119 #include <assert.h>
121 ST_DATA const int reg_classes[NB_REGS] = {
122 /* eax */ RC_INT | RC_RAX,
123 /* ecx */ RC_INT | RC_RCX,
124 /* edx */ RC_INT | RC_RDX,
126 /* st0 */ RC_ST0,
130 RC_R8,
131 RC_R9,
132 RC_R10,
133 RC_R11,
138 /* xmm0 */ RC_FLOAT | RC_XMM0,
139 /* xmm1 */ RC_FLOAT | RC_XMM1,
140 /* xmm2 */ RC_FLOAT | RC_XMM2,
141 /* xmm3 */ RC_FLOAT | RC_XMM3,
142 /* xmm4 */ RC_FLOAT | RC_XMM4,
143 /* xmm5 */ RC_FLOAT | RC_XMM5 /* only up to xmm5: xmm6-15 must be callee saved on Win64 */
146 static unsigned long func_sub_sp_offset;
147 static int func_ret_sub;
149 /* XXX: make it faster ? */
150 void g(int c)
152 int ind1;
153 ind1 = ind + 1;
154 if (ind1 > cur_text_section->data_allocated)
155 section_realloc(cur_text_section, ind1);
156 cur_text_section->data[ind] = c;
157 ind = ind1;
160 void o(unsigned int c)
162 while (c) {
163 g(c);
164 c = c >> 8;
168 void gen_le16(int v)
170 g(v);
171 g(v >> 8);
174 void gen_le32(int c)
176 g(c);
177 g(c >> 8);
178 g(c >> 16);
179 g(c >> 24);
182 void gen_le64(int64_t c)
184 g(c);
185 g(c >> 8);
186 g(c >> 16);
187 g(c >> 24);
188 g(c >> 32);
189 g(c >> 40);
190 g(c >> 48);
191 g(c >> 56);
194 void orex(int ll, int r, int r2, int b)
196 if ((r & VT_VALMASK) >= VT_CONST)
197 r = 0;
198 if ((r2 & VT_VALMASK) >= VT_CONST)
199 r2 = 0;
200 if (ll || REX_BASE(r) || REX_BASE(r2))
201 o(0x40 | REX_BASE(r) | (REX_BASE(r2) << 2) | (ll << 3));
202 o(b);
205 /* output a symbol and patch all calls to it */
206 void gsym_addr(int t, int a)
208 int n, *ptr;
209 while (t) {
210 ptr = (int *)(cur_text_section->data + t);
211 n = *ptr; /* next value */
212 *ptr = a - t - 4;
213 t = n;
217 void gsym(int t)
219 gsym_addr(t, ind);
222 /* psym is used to put an instruction with a data field which is a
223 reference to a symbol. It is in fact the same as oad ! */
224 #define psym oad
226 static int is64_type(int t)
228 return ((t & VT_BTYPE) == VT_PTR ||
229 (t & VT_BTYPE) == VT_FUNC ||
230 (t & VT_BTYPE) == VT_LLONG);
233 static int is_sse_float(int t) {
234 int bt;
235 bt = t & VT_BTYPE;
236 return bt == VT_DOUBLE || bt == VT_FLOAT;
240 /* instruction + 4 bytes data. Return the address of the data */
241 ST_FUNC int oad(int c, int s)
243 int ind1;
245 o(c);
246 ind1 = ind + 4;
247 if (ind1 > cur_text_section->data_allocated)
248 section_realloc(cur_text_section, ind1);
249 *(int *)(cur_text_section->data + ind) = s;
250 s = ind;
251 ind = ind1;
252 return s;
255 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
257 if (r & VT_SYM)
258 greloc(cur_text_section, sym, ind, R_X86_64_32);
259 gen_le32(c);
262 /* output constant with relocation if 'r & VT_SYM' is true */
263 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c)
265 if (r & VT_SYM)
266 greloc(cur_text_section, sym, ind, R_X86_64_64);
267 gen_le64(c);
270 /* output constant with relocation if 'r & VT_SYM' is true */
271 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
273 if (r & VT_SYM)
274 greloc(cur_text_section, sym, ind, R_X86_64_PC32);
275 gen_le32(c-4);
278 /* output got address with relocation */
279 static void gen_gotpcrel(int r, Sym *sym, int c)
281 #ifndef TCC_TARGET_PE
282 Section *sr;
283 ElfW(Rela) *rel;
284 greloc(cur_text_section, sym, ind, R_X86_64_GOTPCREL);
285 sr = cur_text_section->reloc;
286 rel = (ElfW(Rela) *)(sr->data + sr->data_offset - sizeof(ElfW(Rela)));
287 rel->r_addend = -4;
288 #else
289 printf("picpic: %s %x %x | %02x %02x %02x\n", get_tok_str(sym->v, NULL), c, r,
290 cur_text_section->data[ind-3],
291 cur_text_section->data[ind-2],
292 cur_text_section->data[ind-1]
294 greloc(cur_text_section, sym, ind, R_X86_64_PC32);
295 #endif
296 gen_le32(0);
297 if (c) {
298 /* we use add c, %xxx for displacement */
299 orex(1, r, 0, 0x81);
300 o(0xc0 + REG_VALUE(r));
301 gen_le32(c);
305 static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got)
307 op_reg = REG_VALUE(op_reg) << 3;
308 if ((r & VT_VALMASK) == VT_CONST) {
309 /* constant memory reference */
310 o(0x05 | op_reg);
311 if (is_got) {
312 gen_gotpcrel(r, sym, c);
313 } else {
314 gen_addrpc32(r, sym, c);
316 } else if ((r & VT_VALMASK) == VT_LOCAL) {
317 /* currently, we use only ebp as base */
318 if (c == (char)c) {
319 /* short reference */
320 o(0x45 | op_reg);
321 g(c);
322 } else {
323 oad(0x85 | op_reg, c);
325 } else if ((r & VT_VALMASK) >= TREG_MEM) {
326 if (c) {
327 g(0x80 | op_reg | REG_VALUE(r));
328 gen_le32(c);
329 } else {
330 g(0x00 | op_reg | REG_VALUE(r));
332 } else {
333 g(0x00 | op_reg | REG_VALUE(r));
337 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
338 opcode bits */
339 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
341 gen_modrm_impl(op_reg, r, sym, c, 0);
344 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
345 opcode bits */
346 static void gen_modrm64(int opcode, int op_reg, int r, Sym *sym, int c)
348 int is_got;
349 is_got = (op_reg & TREG_MEM) && !(sym->type.t & VT_STATIC);
350 orex(1, r, op_reg, opcode);
351 gen_modrm_impl(op_reg, r, sym, c, is_got);
355 /* load 'r' from value 'sv' */
356 void load(int r, SValue *sv)
358 int v, t, ft, fc, fr;
359 SValue v1;
361 #ifdef TCC_TARGET_PE
362 SValue v2;
363 sv = pe_getimport(sv, &v2);
364 #endif
366 fr = sv->r;
367 ft = sv->type.t;
368 fc = sv->c.ul;
370 #ifndef TCC_TARGET_PE
371 /* we use indirect access via got */
372 if ((fr & VT_VALMASK) == VT_CONST && (fr & VT_SYM) &&
373 (fr & VT_LVAL) && !(sv->sym->type.t & VT_STATIC)) {
374 /* use the result register as a temporal register */
375 int tr = r | TREG_MEM;
376 if (is_float(ft)) {
377 /* we cannot use float registers as a temporal register */
378 tr = get_reg(RC_INT) | TREG_MEM;
380 gen_modrm64(0x8b, tr, fr, sv->sym, 0);
382 /* load from the temporal register */
383 fr = tr | VT_LVAL;
385 #endif
387 v = fr & VT_VALMASK;
388 if (fr & VT_LVAL) {
389 int b, ll;
390 if (v == VT_LLOCAL) {
391 v1.type.t = VT_PTR;
392 v1.r = VT_LOCAL | VT_LVAL;
393 v1.c.ul = fc;
394 fr = r;
395 if (!(reg_classes[fr] & RC_INT))
396 fr = get_reg(RC_INT);
397 load(fr, &v1);
399 ll = 0;
400 if ((ft & VT_BTYPE) == VT_FLOAT) {
401 b = 0x6e0f66;
402 r = REG_VALUE(r); /* movd */
403 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
404 b = 0x7e0ff3; /* movq */
405 r = REG_VALUE(r);
406 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
407 b = 0xdb, r = 5; /* fldt */
408 } else if ((ft & VT_TYPE) == VT_BYTE) {
409 b = 0xbe0f; /* movsbl */
410 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
411 b = 0xb60f; /* movzbl */
412 } else if ((ft & VT_TYPE) == VT_SHORT) {
413 b = 0xbf0f; /* movswl */
414 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
415 b = 0xb70f; /* movzwl */
416 } else {
417 assert(((ft & VT_BTYPE) == VT_INT) || ((ft & VT_BTYPE) == VT_LLONG)
418 || ((ft & VT_BTYPE) == VT_PTR) || ((ft & VT_BTYPE) == VT_ENUM)
419 || ((ft & VT_BTYPE) == VT_FUNC));
420 ll = is64_type(ft);
421 b = 0x8b;
423 if (ll) {
424 gen_modrm64(b, r, fr, sv->sym, fc);
425 } else {
426 orex(ll, fr, r, b);
427 gen_modrm(r, fr, sv->sym, fc);
429 } else {
430 if (v == VT_CONST) {
431 if (fr & VT_SYM) {
432 #ifdef TCC_TARGET_PE
433 orex(1,0,r,0x8d);
434 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
435 gen_addrpc32(fr, sv->sym, fc);
436 #else
437 if (sv->sym->type.t & VT_STATIC) {
438 orex(1,0,r,0x8d);
439 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
440 gen_addrpc32(fr, sv->sym, fc);
441 } else {
442 orex(1,0,r,0x8b);
443 o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */
444 gen_gotpcrel(r, sv->sym, fc);
446 #endif
447 } else if (is64_type(ft)) {
448 orex(1,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
449 gen_le64(sv->c.ull);
450 } else {
451 orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
452 gen_le32(fc);
454 } else if (v == VT_LOCAL) {
455 orex(1,0,r,0x8d); /* lea xxx(%ebp), r */
456 gen_modrm(r, VT_LOCAL, sv->sym, fc);
457 } else if (v == VT_CMP) {
458 orex(0,r,0,0);
459 if ((fc & ~0x100) != TOK_NE)
460 oad(0xb8 + REG_VALUE(r), 0); /* mov $0, r */
461 else
462 oad(0xb8 + REG_VALUE(r), 1); /* mov $1, r */
463 if (fc & 0x100)
465 /* This was a float compare. If the parity bit is
466 set the result was unordered, meaning false for everything
467 except TOK_NE, and true for TOK_NE. */
468 fc &= ~0x100;
469 o(0x037a + (REX_BASE(r) << 8));
471 orex(0,r,0, 0x0f); /* setxx %br */
472 o(fc);
473 o(0xc0 + REG_VALUE(r));
474 } else if (v == VT_JMP || v == VT_JMPI) {
475 t = v & 1;
476 orex(0,r,0,0);
477 oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */
478 o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */
479 gsym(fc);
480 orex(0,r,0,0);
481 oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */
482 } else if (v != r) {
483 if ((r >= TREG_XMM0) && (r <= TREG_XMM7)) {
484 if (v == TREG_ST0) {
485 /* gen_cvt_ftof(VT_DOUBLE); */
486 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
487 /* movsd -0x10(%rsp),%xmmN */
488 o(0x100ff2);
489 o(0x44 + REG_VALUE(r)*8); /* %xmmN */
490 o(0xf024);
491 } else {
492 assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
493 if ((ft & VT_BTYPE) == VT_FLOAT) {
494 o(0x100ff3);
495 } else {
496 assert((ft & VT_BTYPE) == VT_DOUBLE);
497 o(0x100ff2);
499 o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8);
501 } else if (r == TREG_ST0) {
502 assert((v == TREG_XMM0) || (v == TREG_XMM1));
503 /* gen_cvt_ftof(VT_LDOUBLE); */
504 /* movsd %xmmN,-0x10(%rsp) */
505 o(0x110ff2);
506 o(0x44 + REG_VALUE(r)*8); /* %xmmN */
507 o(0xf024);
508 o(0xf02444dd); /* fldl -0x10(%rsp) */
509 } else {
510 orex(1,r,v, 0x89);
511 o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */
517 /* store register 'r' in lvalue 'v' */
518 void store(int r, SValue *v)
520 int fr, bt, ft, fc;
521 int op64 = 0;
522 /* store the REX prefix in this variable when PIC is enabled */
523 int pic = 0;
525 #ifdef TCC_TARGET_PE
526 SValue v2;
527 v = pe_getimport(v, &v2);
528 #endif
530 ft = v->type.t;
531 fc = v->c.ul;
532 fr = v->r & VT_VALMASK;
533 bt = ft & VT_BTYPE;
535 #ifndef TCC_TARGET_PE
536 /* we need to access the variable via got */
537 if (fr == VT_CONST && (v->r & VT_SYM)) {
538 /* mov xx(%rip), %r11 */
539 o(0x1d8b4c);
540 gen_gotpcrel(TREG_R11, v->sym, v->c.ul);
541 pic = is64_type(bt) ? 0x49 : 0x41;
543 #endif
545 /* XXX: incorrect if float reg to reg */
546 if (bt == VT_FLOAT) {
547 o(0x66);
548 o(pic);
549 o(0x7e0f); /* movd */
550 r = REG_VALUE(r);
551 } else if (bt == VT_DOUBLE) {
552 o(0x66);
553 o(pic);
554 o(0xd60f); /* movq */
555 r = REG_VALUE(r);
556 } else if (bt == VT_LDOUBLE) {
557 o(0xc0d9); /* fld %st(0) */
558 o(pic);
559 o(0xdb); /* fstpt */
560 r = 7;
561 } else {
562 if (bt == VT_SHORT)
563 o(0x66);
564 o(pic);
565 if (bt == VT_BYTE || bt == VT_BOOL)
566 orex(0, 0, r, 0x88);
567 else if (is64_type(bt))
568 op64 = 0x89;
569 else
570 orex(0, 0, r, 0x89);
572 if (pic) {
573 /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
574 if (op64)
575 o(op64);
576 o(3 + (r << 3));
577 } else if (op64) {
578 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
579 gen_modrm64(op64, r, v->r, v->sym, fc);
580 } else if (fr != r) {
581 /* XXX: don't we really come here? */
582 abort();
583 o(0xc0 + fr + r * 8); /* mov r, fr */
585 } else {
586 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
587 gen_modrm(r, v->r, v->sym, fc);
588 } else if (fr != r) {
589 /* XXX: don't we really come here? */
590 abort();
591 o(0xc0 + fr + r * 8); /* mov r, fr */
596 /* 'is_jmp' is '1' if it is a jump */
597 static void gcall_or_jmp(int is_jmp)
599 int r;
600 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
601 /* constant case */
602 if (vtop->r & VT_SYM) {
603 /* relocation case */
604 greloc(cur_text_section, vtop->sym,
605 ind + 1, R_X86_64_PC32);
606 } else {
607 /* put an empty PC32 relocation */
608 put_elf_reloc(symtab_section, cur_text_section,
609 ind + 1, R_X86_64_PC32, 0);
611 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
612 } else {
613 /* otherwise, indirect call */
614 r = TREG_R11;
615 load(r, vtop);
616 o(0x41); /* REX */
617 o(0xff); /* call/jmp *r */
618 o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
622 #ifdef TCC_TARGET_PE
624 #define REGN 4
625 static const uint8_t arg_regs[REGN] = {
626 TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
629 /* Prepare arguments in R10 and R11 rather than RCX and RDX
630 because gv() will not ever use these */
631 static int arg_prepare_reg(int idx) {
632 if (idx == 0 || idx == 1)
633 /* idx=0: r10, idx=1: r11 */
634 return idx + 10;
635 else
636 return arg_regs[idx];
639 static int func_scratch;
641 /* Generate function call. The function address is pushed first, then
642 all the parameters in call order. This functions pops all the
643 parameters and the function address. */
645 void gen_offs_sp(int b, int r, int d)
647 orex(1,0,r & 0x100 ? 0 : r, b);
648 if (d == (char)d) {
649 o(0x2444 | (REG_VALUE(r) << 3));
650 g(d);
651 } else {
652 o(0x2484 | (REG_VALUE(r) << 3));
653 gen_le32(d);
657 /* Return 1 if this function returns via an sret pointer, 0 otherwise */
658 ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) {
659 *ret_align = 1; // Never have to re-align return values for x86-64
660 int size, align;
661 size = type_size(vt, &align);
662 ret->ref = NULL;
663 if (size > 8) {
664 return 1;
665 } else if (size > 4) {
666 ret->t = VT_LLONG;
667 return 0;
668 } else if (size > 2) {
669 ret->t = VT_INT;
670 return 0;
671 } else if (size > 1) {
672 ret->t = VT_SHORT;
673 return 0;
674 } else {
675 ret->t = VT_BYTE;
676 return 0;
680 int gfunc_arg_size(CType *type) {
681 if (type->t & (VT_ARRAY|VT_BITFIELD))
682 return 8;
683 int align;
684 return type_size(type, &align);
687 void gfunc_call(int nb_args)
689 int size, r, args_size, i, d, bt, struct_size;
690 int arg;
692 args_size = (nb_args < REGN ? REGN : nb_args) * PTR_SIZE;
693 arg = nb_args;
695 /* for struct arguments, we need to call memcpy and the function
696 call breaks register passing arguments we are preparing.
697 So, we process arguments which will be passed by stack first. */
698 struct_size = args_size;
699 for(i = 0; i < nb_args; i++) {
700 --arg;
702 SValue *sv = &vtop[-i];
703 bt = (sv->type.t & VT_BTYPE);
704 size = gfunc_arg_size(&sv->type);
706 if (size <= 8)
707 continue; /* arguments smaller than 8 bytes passed in registers or on stack */
709 if (bt == VT_STRUCT) {
710 /* align to stack align size */
711 size = (size + 15) & ~15;
712 /* generate structure store */
713 r = get_reg(RC_INT);
714 gen_offs_sp(0x8d, r, struct_size);
715 struct_size += size;
717 /* generate memcpy call */
718 vset(&sv->type, r | VT_LVAL, 0);
719 vpushv(sv);
720 vstore();
721 --vtop;
722 } else if (bt == VT_LDOUBLE) {
723 gv(RC_ST0);
724 gen_offs_sp(0xdb, 0x107, struct_size);
725 struct_size += 16;
729 if (func_scratch < struct_size)
730 func_scratch = struct_size;
732 arg = nb_args;
733 struct_size = args_size;
735 for(i = 0; i < nb_args; i++) {
736 --arg;
737 bt = (vtop->type.t & VT_BTYPE);
739 size = gfunc_arg_size(&vtop->type);
740 if (size > 8) {
741 /* align to stack align size */
742 size = (size + 15) & ~15;
743 if (arg >= REGN) {
744 d = get_reg(RC_INT);
745 gen_offs_sp(0x8d, d, struct_size);
746 gen_offs_sp(0x89, d, arg*8);
747 } else {
748 d = arg_prepare_reg(arg);
749 gen_offs_sp(0x8d, d, struct_size);
751 struct_size += size;
752 } else {
753 if (is_sse_float(vtop->type.t)) {
754 gv(RC_XMM0); /* only use one float register */
755 if (arg >= REGN) {
756 /* movq %xmm0, j*8(%rsp) */
757 gen_offs_sp(0xd60f66, 0x100, arg*8);
758 } else {
759 /* movaps %xmm0, %xmmN */
760 o(0x280f);
761 o(0xc0 + (arg << 3));
762 d = arg_prepare_reg(arg);
763 /* mov %xmm0, %rxx */
764 o(0x66);
765 orex(1,d,0, 0x7e0f);
766 o(0xc0 + REG_VALUE(d));
768 } else {
769 if (bt == VT_STRUCT) {
770 vtop->type.ref = NULL;
771 vtop->type.t = size > 4 ? VT_LLONG : size > 2 ? VT_INT
772 : size > 1 ? VT_SHORT : VT_BYTE;
775 r = gv(RC_INT);
776 if (arg >= REGN) {
777 gen_offs_sp(0x89, r, arg*8);
778 } else {
779 d = arg_prepare_reg(arg);
780 orex(1,d,r,0x89); /* mov */
781 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
785 vtop--;
787 save_regs(0);
789 /* Copy R10 and R11 into RCX and RDX, respectively */
790 if (nb_args > 0) {
791 o(0xd1894c); /* mov %r10, %rcx */
792 if (nb_args > 1) {
793 o(0xda894c); /* mov %r11, %rdx */
797 gcall_or_jmp(0);
798 vtop--;
802 #define FUNC_PROLOG_SIZE 11
804 /* generate function prolog of type 't' */
805 void gfunc_prolog(CType *func_type)
807 int addr, reg_param_index, bt, size;
808 Sym *sym;
809 CType *type;
811 func_ret_sub = 0;
812 func_scratch = 0;
813 loc = 0;
815 addr = PTR_SIZE * 2;
816 ind += FUNC_PROLOG_SIZE;
817 func_sub_sp_offset = ind;
818 reg_param_index = 0;
820 sym = func_type->ref;
822 /* if the function returns a structure, then add an
823 implicit pointer parameter */
824 func_vt = sym->type;
825 size = gfunc_arg_size(&func_vt);
826 if (size > 8) {
827 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
828 func_vc = addr;
829 reg_param_index++;
830 addr += 8;
833 /* define parameters */
834 while ((sym = sym->next) != NULL) {
835 type = &sym->type;
836 bt = type->t & VT_BTYPE;
837 size = gfunc_arg_size(type);
838 if (size > 8) {
839 if (reg_param_index < REGN) {
840 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
842 sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL | VT_REF, addr);
843 } else {
844 if (reg_param_index < REGN) {
845 /* save arguments passed by register */
846 if ((bt == VT_FLOAT) || (bt == VT_DOUBLE)) {
847 o(0xd60f66); /* movq */
848 gen_modrm(reg_param_index, VT_LOCAL, NULL, addr);
849 } else {
850 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
853 sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, addr);
855 addr += 8;
856 reg_param_index++;
859 while (reg_param_index < REGN) {
860 if (func_type->ref->c == FUNC_ELLIPSIS) {
861 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
862 addr += 8;
864 reg_param_index++;
868 /* generate function epilog */
869 void gfunc_epilog(void)
871 int v, saved_ind;
873 o(0xc9); /* leave */
874 if (func_ret_sub == 0) {
875 o(0xc3); /* ret */
876 } else {
877 o(0xc2); /* ret n */
878 g(func_ret_sub);
879 g(func_ret_sub >> 8);
882 saved_ind = ind;
883 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
884 /* align local size to word & save local variables */
885 v = (func_scratch + -loc + 15) & -16;
887 if (v >= 4096) {
888 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
889 oad(0xb8, v); /* mov stacksize, %eax */
890 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
891 greloc(cur_text_section, sym, ind-4, R_X86_64_PC32);
892 o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
893 } else {
894 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
895 o(0xec8148); /* sub rsp, stacksize */
896 gen_le32(v);
899 cur_text_section->data_offset = saved_ind;
900 pe_add_unwind_data(ind, saved_ind, v);
901 ind = cur_text_section->data_offset;
904 #else
906 static void gadd_sp(int val)
908 if (val == (char)val) {
909 o(0xc48348);
910 g(val);
911 } else {
912 oad(0xc48148, val); /* add $xxx, %rsp */
916 typedef enum X86_64_Mode {
917 x86_64_mode_none,
918 x86_64_mode_memory,
919 x86_64_mode_integer,
920 x86_64_mode_sse,
921 x86_64_mode_x87
922 } X86_64_Mode;
924 static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) {
925 if (a == b)
926 return a;
927 else if (a == x86_64_mode_none)
928 return b;
929 else if (b == x86_64_mode_none)
930 return a;
931 else if ((a == x86_64_mode_memory) || (b == x86_64_mode_memory))
932 return x86_64_mode_memory;
933 else if ((a == x86_64_mode_integer) || (b == x86_64_mode_integer))
934 return x86_64_mode_integer;
935 else if ((a == x86_64_mode_x87) || (b == x86_64_mode_x87))
936 return x86_64_mode_memory;
937 else
938 return x86_64_mode_sse;
941 static X86_64_Mode classify_x86_64_inner(CType *ty) {
942 X86_64_Mode mode;
943 Sym *f;
945 switch (ty->t & VT_BTYPE) {
946 case VT_VOID: return x86_64_mode_none;
948 case VT_INT:
949 case VT_BYTE:
950 case VT_SHORT:
951 case VT_LLONG:
952 case VT_BOOL:
953 case VT_PTR:
954 case VT_FUNC:
955 case VT_ENUM: return x86_64_mode_integer;
957 case VT_FLOAT:
958 case VT_DOUBLE: return x86_64_mode_sse;
960 case VT_LDOUBLE: return x86_64_mode_x87;
962 case VT_STRUCT:
963 f = ty->ref;
965 // Detect union
966 if (f->next && (f->c == f->next->c))
967 return x86_64_mode_memory;
969 mode = x86_64_mode_none;
970 for (; f; f = f->next)
971 mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type));
973 return mode;
976 assert(0);
979 static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *reg_count) {
980 X86_64_Mode mode;
981 int size, align, ret_t;
983 if (ty->t & (VT_BITFIELD|VT_ARRAY)) {
984 *psize = 8;
985 *reg_count = 1;
986 ret_t = ty->t;
987 mode = x86_64_mode_integer;
988 } else {
989 size = type_size(ty, &align);
990 *psize = (size + 7) & ~7;
992 if (size > 16) {
993 mode = x86_64_mode_memory;
994 } else {
995 mode = classify_x86_64_inner(ty);
996 switch (mode) {
997 case x86_64_mode_integer:
998 if (size > 8) {
999 *reg_count = 2;
1000 ret_t = VT_QLONG;
1001 } else {
1002 *reg_count = 1;
1003 ret_t = (size > 4) ? VT_LLONG : VT_INT;
1005 break;
1007 case x86_64_mode_x87:
1008 *reg_count = 1;
1009 ret_t = VT_LDOUBLE;
1010 break;
1012 case x86_64_mode_sse:
1013 if (size > 8) {
1014 *reg_count = 2;
1015 ret_t = VT_QFLOAT;
1016 } else {
1017 *reg_count = 1;
1018 ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT;
1020 break;
1025 if (ret) {
1026 ret->ref = NULL;
1027 ret->t = ret_t;
1030 return mode;
1033 ST_FUNC int classify_x86_64_va_arg(CType *ty) {
1034 /* This definition must be synced with stdarg.h */
1035 enum __va_arg_type {
1036 __va_gen_reg, __va_float_reg, __va_stack
1038 int size, reg_count;
1039 X86_64_Mode mode = classify_x86_64_arg(ty, NULL, &size, &reg_count);
1040 switch (mode) {
1041 default: return __va_stack;
1042 case x86_64_mode_integer: return __va_gen_reg;
1043 case x86_64_mode_sse: return __va_float_reg;
1047 /* Return 1 if this function returns via an sret pointer, 0 otherwise */
1048 int gfunc_sret(CType *vt, CType *ret, int *ret_align) {
1049 int size, reg_count;
1050 *ret_align = 1; // Never have to re-align return values for x86-64
1051 return (classify_x86_64_arg(vt, ret, &size, &reg_count) == x86_64_mode_memory);
1054 #define REGN 6
1055 static const uint8_t arg_regs[REGN] = {
1056 TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
1059 static int arg_prepare_reg(int idx) {
1060 if (idx == 2 || idx == 3)
1061 /* idx=2: r10, idx=3: r11 */
1062 return idx + 8;
1063 else
1064 return arg_regs[idx];
1067 /* Generate function call. The function address is pushed first, then
1068 all the parameters in call order. This functions pops all the
1069 parameters and the function address. */
1070 void gfunc_call(int nb_args)
1072 X86_64_Mode mode;
1073 CType type;
1074 int size, align, r, args_size, i, j, reg_count;
1075 int nb_reg_args = 0;
1076 int nb_sse_args = 0;
1077 int sse_reg, gen_reg;
1079 /* calculate the number of integer/float arguments */
1080 args_size = 0;
1081 for(i = 0; i < nb_args; i++) {
1082 mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &reg_count);
1083 switch (mode) {
1084 case x86_64_mode_memory:
1085 case x86_64_mode_x87:
1086 args_size += size;
1087 break;
1089 case x86_64_mode_sse:
1090 nb_sse_args += reg_count;
1091 if (nb_sse_args > 8) args_size += size;
1092 break;
1094 case x86_64_mode_integer:
1095 nb_reg_args += reg_count;
1096 if (nb_reg_args > REGN) args_size += size;
1097 break;
1101 /* for struct arguments, we need to call memcpy and the function
1102 call breaks register passing arguments we are preparing.
1103 So, we process arguments which will be passed by stack first. */
1104 gen_reg = nb_reg_args;
1105 sse_reg = nb_sse_args;
1107 /* adjust stack to align SSE boundary */
1108 if (args_size &= 15) {
1109 /* fetch cpu flag before the following sub will change the value */
1110 if (vtop >= vstack && (vtop->r & VT_VALMASK) == VT_CMP)
1111 gv(RC_INT);
1113 args_size = 16 - args_size;
1114 o(0x48);
1115 oad(0xec81, args_size); /* sub $xxx, %rsp */
1118 for(i = 0; i < nb_args;) {
1119 /* Swap argument to top, it will possibly be changed here,
1120 and might use more temps. At the end of the loop we keep
1121 in on the stack and swap it back to its original position
1122 if it is a register. */
1123 SValue tmp = vtop[0];
1124 vtop[0] = vtop[-i];
1125 vtop[-i] = tmp;
1127 mode = classify_x86_64_arg(&vtop->type, NULL, &size, &reg_count);
1129 int arg_stored = 1;
1130 switch (vtop->type.t & VT_BTYPE) {
1131 case VT_STRUCT:
1132 if (mode == x86_64_mode_sse) {
1133 if (sse_reg > 8)
1134 sse_reg -= reg_count;
1135 else
1136 arg_stored = 0;
1137 } else if (mode == x86_64_mode_integer) {
1138 if (gen_reg > REGN)
1139 gen_reg -= reg_count;
1140 else
1141 arg_stored = 0;
1144 if (arg_stored) {
1145 /* allocate the necessary size on stack */
1146 o(0x48);
1147 oad(0xec81, size); /* sub $xxx, %rsp */
1148 /* generate structure store */
1149 r = get_reg(RC_INT);
1150 orex(1, r, 0, 0x89); /* mov %rsp, r */
1151 o(0xe0 + REG_VALUE(r));
1152 vset(&vtop->type, r | VT_LVAL, 0);
1153 vswap();
1154 vstore();
1155 args_size += size;
1157 break;
1159 case VT_LDOUBLE:
1160 gv(RC_ST0);
1161 size = LDOUBLE_SIZE;
1162 oad(0xec8148, size); /* sub $xxx, %rsp */
1163 o(0x7cdb); /* fstpt 0(%rsp) */
1164 g(0x24);
1165 g(0x00);
1166 args_size += size;
1167 break;
1169 case VT_FLOAT:
1170 case VT_DOUBLE:
1171 assert(mode == x86_64_mode_sse);
1172 if (sse_reg > 8) {
1173 --sse_reg;
1174 r = gv(RC_FLOAT);
1175 o(0x50); /* push $rax */
1176 /* movq %xmm0, (%rsp) */
1177 o(0xd60f66);
1178 o(0x04 + REG_VALUE(r)*8);
1179 o(0x24);
1180 args_size += size;
1181 } else {
1182 arg_stored = 0;
1184 break;
1186 default:
1187 assert(mode == x86_64_mode_integer);
1188 /* simple type */
1189 /* XXX: implicit cast ? */
1190 if (gen_reg > REGN) {
1191 --gen_reg;
1192 r = gv(RC_INT);
1193 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */
1194 args_size += size;
1195 } else {
1196 arg_stored = 0;
1198 break;
1201 /* And swap the argument back to it's original position. */
1202 tmp = vtop[0];
1203 vtop[0] = vtop[-i];
1204 vtop[-i] = tmp;
1206 if (arg_stored) {
1207 vrotb(i+1);
1208 assert(vtop->type.t == tmp.type.t);
1209 vpop();
1210 --nb_args;
1211 } else {
1212 ++i;
1216 /* XXX This should be superfluous. */
1217 save_regs(0); /* save used temporary registers */
1219 /* then, we prepare register passing arguments.
1220 Note that we cannot set RDX and RCX in this loop because gv()
1221 may break these temporary registers. Let's use R10 and R11
1222 instead of them */
1223 assert(gen_reg <= REGN);
1224 assert(sse_reg <= 8);
1225 for(i = 0; i < nb_args; i++) {
1226 mode = classify_x86_64_arg(&vtop->type, &type, &size, &reg_count);
1227 /* Alter stack entry type so that gv() knows how to treat it */
1228 vtop->type = type;
1229 if (mode == x86_64_mode_sse) {
1230 if (reg_count == 2) {
1231 sse_reg -= 2;
1232 gv(RC_FRET); /* Use pair load into xmm0 & xmm1 */
1233 if (sse_reg) { /* avoid redundant movaps %xmm0, %xmm0 */
1234 /* movaps %xmm0, %xmmN */
1235 o(0x280f);
1236 o(0xc0 + (sse_reg << 3));
1237 /* movaps %xmm1, %xmmN */
1238 o(0x280f);
1239 o(0xc1 + ((sse_reg+1) << 3));
1241 } else {
1242 assert(reg_count == 1);
1243 --sse_reg;
1244 /* Load directly to register */
1245 gv(RC_XMM0 << sse_reg);
1247 } else if (mode == x86_64_mode_integer) {
1248 /* simple type */
1249 /* XXX: implicit cast ? */
1250 gen_reg -= reg_count;
1251 r = gv(RC_INT);
1252 int d = arg_prepare_reg(gen_reg);
1253 orex(1,d,r,0x89); /* mov */
1254 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
1255 if (reg_count == 2) {
1256 d = arg_prepare_reg(gen_reg+1);
1257 orex(1,d,vtop->r2,0x89); /* mov */
1258 o(0xc0 + REG_VALUE(vtop->r2) * 8 + REG_VALUE(d));
1261 vtop--;
1263 assert(gen_reg == 0);
1264 assert(sse_reg == 0);
1266 /* We shouldn't have many operands on the stack anymore, but the
1267 call address itself is still there, and it might be in %eax
1268 (or edx/ecx) currently, which the below writes would clobber.
1269 So evict all remaining operands here. */
1270 save_regs(0);
1272 /* Copy R10 and R11 into RDX and RCX, respectively */
1273 if (nb_reg_args > 2) {
1274 o(0xd2894c); /* mov %r10, %rdx */
1275 if (nb_reg_args > 3) {
1276 o(0xd9894c); /* mov %r11, %rcx */
1280 oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
1281 gcall_or_jmp(0);
1282 if (args_size)
1283 gadd_sp(args_size);
1284 vtop--;
1288 #define FUNC_PROLOG_SIZE 11
1290 static void push_arg_reg(int i) {
1291 loc -= 8;
1292 gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
1295 /* generate function prolog of type 't' */
1296 void gfunc_prolog(CType *func_type)
1298 X86_64_Mode mode;
1299 int i, addr, align, size, reg_count;
1300 int param_addr, reg_param_index, sse_param_index;
1301 Sym *sym;
1302 CType *type;
1304 sym = func_type->ref;
1305 addr = PTR_SIZE * 2;
1306 loc = 0;
1307 ind += FUNC_PROLOG_SIZE;
1308 func_sub_sp_offset = ind;
1309 func_ret_sub = 0;
1311 if (func_type->ref->c == FUNC_ELLIPSIS) {
1312 int seen_reg_num, seen_sse_num, seen_stack_size;
1313 seen_reg_num = seen_sse_num = 0;
1314 /* frame pointer and return address */
1315 seen_stack_size = PTR_SIZE * 2;
1316 /* count the number of seen parameters */
1317 sym = func_type->ref;
1318 while ((sym = sym->next) != NULL) {
1319 type = &sym->type;
1320 mode = classify_x86_64_arg(type, NULL, &size, &reg_count);
1321 switch (mode) {
1322 default:
1323 seen_stack_size += size;
1324 break;
1326 case x86_64_mode_integer:
1327 if (seen_reg_num + reg_count <= 8) {
1328 seen_reg_num += reg_count;
1329 } else {
1330 seen_reg_num = 8;
1331 seen_stack_size += size;
1333 break;
1335 case x86_64_mode_sse:
1336 if (seen_sse_num + reg_count <= 8) {
1337 seen_sse_num += reg_count;
1338 } else {
1339 seen_sse_num = 8;
1340 seen_stack_size += size;
1342 break;
1346 loc -= 16;
1347 /* movl $0x????????, -0x10(%rbp) */
1348 o(0xf045c7);
1349 gen_le32(seen_reg_num * 8);
1350 /* movl $0x????????, -0xc(%rbp) */
1351 o(0xf445c7);
1352 gen_le32(seen_sse_num * 16 + 48);
1353 /* movl $0x????????, -0x8(%rbp) */
1354 o(0xf845c7);
1355 gen_le32(seen_stack_size);
1357 /* save all register passing arguments */
1358 for (i = 0; i < 8; i++) {
1359 loc -= 16;
1360 o(0xd60f66); /* movq */
1361 gen_modrm(7 - i, VT_LOCAL, NULL, loc);
1362 /* movq $0, loc+8(%rbp) */
1363 o(0x85c748);
1364 gen_le32(loc + 8);
1365 gen_le32(0);
1367 for (i = 0; i < REGN; i++) {
1368 push_arg_reg(REGN-1-i);
1372 sym = func_type->ref;
1373 reg_param_index = 0;
1374 sse_param_index = 0;
1376 /* if the function returns a structure, then add an
1377 implicit pointer parameter */
1378 func_vt = sym->type;
1379 mode = classify_x86_64_arg(&func_vt, NULL, &size, &reg_count);
1380 if (mode == x86_64_mode_memory) {
1381 push_arg_reg(reg_param_index);
1382 func_vc = loc;
1383 reg_param_index++;
1385 /* define parameters */
1386 while ((sym = sym->next) != NULL) {
1387 type = &sym->type;
1388 mode = classify_x86_64_arg(type, NULL, &size, &reg_count);
1389 switch (mode) {
1390 case x86_64_mode_sse:
1391 if (sse_param_index + reg_count <= 8) {
1392 /* save arguments passed by register */
1393 loc -= reg_count * 8;
1394 param_addr = loc;
1395 for (i = 0; i < reg_count; ++i) {
1396 o(0xd60f66); /* movq */
1397 gen_modrm(sse_param_index, VT_LOCAL, NULL, param_addr + i*8);
1398 ++sse_param_index;
1400 } else {
1401 param_addr = addr;
1402 addr += size;
1403 sse_param_index += reg_count;
1405 break;
1407 case x86_64_mode_memory:
1408 case x86_64_mode_x87:
1409 param_addr = addr;
1410 addr += size;
1411 break;
1413 case x86_64_mode_integer: {
1414 if (reg_param_index + reg_count <= REGN) {
1415 /* save arguments passed by register */
1416 loc -= reg_count * 8;
1417 param_addr = loc;
1418 for (i = 0; i < reg_count; ++i) {
1419 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, param_addr + i*8);
1420 ++reg_param_index;
1422 } else {
1423 param_addr = addr;
1424 addr += size;
1425 reg_param_index += reg_count;
1427 break;
1430 sym_push(sym->v & ~SYM_FIELD, type,
1431 VT_LOCAL | VT_LVAL, param_addr);
1435 /* generate function epilog */
1436 void gfunc_epilog(void)
1438 int v, saved_ind;
1440 o(0xc9); /* leave */
1441 if (func_ret_sub == 0) {
1442 o(0xc3); /* ret */
1443 } else {
1444 o(0xc2); /* ret n */
1445 g(func_ret_sub);
1446 g(func_ret_sub >> 8);
1448 /* align local size to word & save local variables */
1449 v = (-loc + 15) & -16;
1450 saved_ind = ind;
1451 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1452 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1453 o(0xec8148); /* sub rsp, stacksize */
1454 gen_le32(v);
1455 ind = saved_ind;
1458 #endif /* not PE */
1460 /* generate a jump to a label */
1461 int gjmp(int t)
1463 return psym(0xe9, t);
1466 /* generate a jump to a fixed address */
1467 void gjmp_addr(int a)
1469 int r;
1470 r = a - ind - 2;
1471 if (r == (char)r) {
1472 g(0xeb);
1473 g(r);
1474 } else {
1475 oad(0xe9, a - ind - 5);
1479 /* generate a test. set 'inv' to invert test. Stack entry is popped */
1480 int gtst(int inv, int t)
1482 int v, *p;
1484 v = vtop->r & VT_VALMASK;
1485 if (v == VT_CMP) {
1486 /* fast case : can jump directly since flags are set */
1487 if (vtop->c.i & 0x100)
1489 /* This was a float compare. If the parity flag is set
1490 the result was unordered. For anything except != this
1491 means false and we don't jump (anding both conditions).
1492 For != this means true (oring both).
1493 Take care about inverting the test. We need to jump
1494 to our target if the result was unordered and test wasn't NE,
1495 otherwise if unordered we don't want to jump. */
1496 vtop->c.i &= ~0x100;
1497 if (!inv == (vtop->c.i != TOK_NE))
1498 o(0x067a); /* jp +6 */
1499 else
1501 g(0x0f);
1502 t = psym(0x8a, t); /* jp t */
1505 g(0x0f);
1506 t = psym((vtop->c.i - 16) ^ inv, t);
1507 } else if (v == VT_JMP || v == VT_JMPI) {
1508 /* && or || optimization */
1509 if ((v & 1) == inv) {
1510 /* insert vtop->c jump list in t */
1511 p = &vtop->c.i;
1512 while (*p != 0)
1513 p = (int *)(cur_text_section->data + *p);
1514 *p = t;
1515 t = vtop->c.i;
1516 } else {
1517 t = gjmp(t);
1518 gsym(vtop->c.i);
1520 } else {
1521 if (is_float(vtop->type.t) ||
1522 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
1523 vpushi(0);
1524 gen_op(TOK_NE);
1526 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1527 /* constant jmp optimization */
1528 if ((vtop->c.i != 0) != inv)
1529 t = gjmp(t);
1530 } else {
1531 v = gv(RC_INT);
1532 orex(0,v,v,0x85);
1533 o(0xc0 + REG_VALUE(v) * 9);
1534 g(0x0f);
1535 t = psym(0x85 ^ inv, t);
1538 vtop--;
1539 return t;
1542 /* generate an integer binary operation */
1543 void gen_opi(int op)
1545 int r, fr, opc, c;
1546 int ll, uu, cc;
1548 ll = is64_type(vtop[-1].type.t);
1549 uu = (vtop[-1].type.t & VT_UNSIGNED) != 0;
1550 cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1552 switch(op) {
1553 case '+':
1554 case TOK_ADDC1: /* add with carry generation */
1555 opc = 0;
1556 gen_op8:
1557 if (cc && (!ll || (int)vtop->c.ll == vtop->c.ll)) {
1558 /* constant case */
1559 vswap();
1560 r = gv(RC_INT);
1561 vswap();
1562 c = vtop->c.i;
1563 if (c == (char)c) {
1564 /* XXX: generate inc and dec for smaller code ? */
1565 orex(ll, r, 0, 0x83);
1566 o(0xc0 | (opc << 3) | REG_VALUE(r));
1567 g(c);
1568 } else {
1569 orex(ll, r, 0, 0x81);
1570 oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
1572 } else {
1573 gv2(RC_INT, RC_INT);
1574 r = vtop[-1].r;
1575 fr = vtop[0].r;
1576 orex(ll, r, fr, (opc << 3) | 0x01);
1577 o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
1579 vtop--;
1580 if (op >= TOK_ULT && op <= TOK_GT) {
1581 vtop->r = VT_CMP;
1582 vtop->c.i = op;
1584 break;
1585 case '-':
1586 case TOK_SUBC1: /* sub with carry generation */
1587 opc = 5;
1588 goto gen_op8;
1589 case TOK_ADDC2: /* add with carry use */
1590 opc = 2;
1591 goto gen_op8;
1592 case TOK_SUBC2: /* sub with carry use */
1593 opc = 3;
1594 goto gen_op8;
1595 case '&':
1596 opc = 4;
1597 goto gen_op8;
1598 case '^':
1599 opc = 6;
1600 goto gen_op8;
1601 case '|':
1602 opc = 1;
1603 goto gen_op8;
1604 case '*':
1605 gv2(RC_INT, RC_INT);
1606 r = vtop[-1].r;
1607 fr = vtop[0].r;
1608 orex(ll, fr, r, 0xaf0f); /* imul fr, r */
1609 o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8);
1610 vtop--;
1611 break;
1612 case TOK_SHL:
1613 opc = 4;
1614 goto gen_shift;
1615 case TOK_SHR:
1616 opc = 5;
1617 goto gen_shift;
1618 case TOK_SAR:
1619 opc = 7;
1620 gen_shift:
1621 opc = 0xc0 | (opc << 3);
1622 if (cc) {
1623 /* constant case */
1624 vswap();
1625 r = gv(RC_INT);
1626 vswap();
1627 orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */
1628 o(opc | REG_VALUE(r));
1629 g(vtop->c.i & (ll ? 63 : 31));
1630 } else {
1631 /* we generate the shift in ecx */
1632 gv2(RC_INT, RC_RCX);
1633 r = vtop[-1].r;
1634 orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */
1635 o(opc | REG_VALUE(r));
1637 vtop--;
1638 break;
1639 case TOK_UDIV:
1640 case TOK_UMOD:
1641 uu = 1;
1642 goto divmod;
1643 case '/':
1644 case '%':
1645 case TOK_PDIV:
1646 uu = 0;
1647 divmod:
1648 /* first operand must be in eax */
1649 /* XXX: need better constraint for second operand */
1650 gv2(RC_RAX, RC_RCX);
1651 r = vtop[-1].r;
1652 fr = vtop[0].r;
1653 vtop--;
1654 save_reg(TREG_RDX);
1655 orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */
1656 orex(ll, fr, 0, 0xf7); /* div fr, %eax */
1657 o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr));
1658 if (op == '%' || op == TOK_UMOD)
1659 r = TREG_RDX;
1660 else
1661 r = TREG_RAX;
1662 vtop->r = r;
1663 break;
1664 default:
1665 opc = 7;
1666 goto gen_op8;
1670 void gen_opl(int op)
1672 gen_opi(op);
1675 /* generate a floating point operation 'v = t1 op t2' instruction. The
1676 two operands are guaranted to have the same floating point type */
1677 /* XXX: need to use ST1 too */
1678 void gen_opf(int op)
1680 int a, ft, fc, swapped, r;
1681 int float_type =
1682 (vtop->type.t & VT_BTYPE) == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
1684 /* convert constants to memory references */
1685 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1686 vswap();
1687 gv(float_type);
1688 vswap();
1690 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
1691 gv(float_type);
1693 /* must put at least one value in the floating point register */
1694 if ((vtop[-1].r & VT_LVAL) &&
1695 (vtop[0].r & VT_LVAL)) {
1696 vswap();
1697 gv(float_type);
1698 vswap();
1700 swapped = 0;
1701 /* swap the stack if needed so that t1 is the register and t2 is
1702 the memory reference */
1703 if (vtop[-1].r & VT_LVAL) {
1704 vswap();
1705 swapped = 1;
1707 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1708 if (op >= TOK_ULT && op <= TOK_GT) {
1709 /* load on stack second operand */
1710 load(TREG_ST0, vtop);
1711 save_reg(TREG_RAX); /* eax is used by FP comparison code */
1712 if (op == TOK_GE || op == TOK_GT)
1713 swapped = !swapped;
1714 else if (op == TOK_EQ || op == TOK_NE)
1715 swapped = 0;
1716 if (swapped)
1717 o(0xc9d9); /* fxch %st(1) */
1718 o(0xe9da); /* fucompp */
1719 o(0xe0df); /* fnstsw %ax */
1720 if (op == TOK_EQ) {
1721 o(0x45e480); /* and $0x45, %ah */
1722 o(0x40fC80); /* cmp $0x40, %ah */
1723 } else if (op == TOK_NE) {
1724 o(0x45e480); /* and $0x45, %ah */
1725 o(0x40f480); /* xor $0x40, %ah */
1726 op = TOK_NE;
1727 } else if (op == TOK_GE || op == TOK_LE) {
1728 o(0x05c4f6); /* test $0x05, %ah */
1729 op = TOK_EQ;
1730 } else {
1731 o(0x45c4f6); /* test $0x45, %ah */
1732 op = TOK_EQ;
1734 vtop--;
1735 vtop->r = VT_CMP;
1736 vtop->c.i = op;
1737 } else {
1738 /* no memory reference possible for long double operations */
1739 load(TREG_ST0, vtop);
1740 swapped = !swapped;
1742 switch(op) {
1743 default:
1744 case '+':
1745 a = 0;
1746 break;
1747 case '-':
1748 a = 4;
1749 if (swapped)
1750 a++;
1751 break;
1752 case '*':
1753 a = 1;
1754 break;
1755 case '/':
1756 a = 6;
1757 if (swapped)
1758 a++;
1759 break;
1761 ft = vtop->type.t;
1762 fc = vtop->c.ul;
1763 o(0xde); /* fxxxp %st, %st(1) */
1764 o(0xc1 + (a << 3));
1765 vtop--;
1767 } else {
1768 if (op >= TOK_ULT && op <= TOK_GT) {
1769 /* if saved lvalue, then we must reload it */
1770 r = vtop->r;
1771 fc = vtop->c.ul;
1772 if ((r & VT_VALMASK) == VT_LLOCAL) {
1773 SValue v1;
1774 r = get_reg(RC_INT);
1775 v1.type.t = VT_PTR;
1776 v1.r = VT_LOCAL | VT_LVAL;
1777 v1.c.ul = fc;
1778 load(r, &v1);
1779 fc = 0;
1782 if (op == TOK_EQ || op == TOK_NE) {
1783 swapped = 0;
1784 } else {
1785 if (op == TOK_LE || op == TOK_LT)
1786 swapped = !swapped;
1787 if (op == TOK_LE || op == TOK_GE) {
1788 op = 0x93; /* setae */
1789 } else {
1790 op = 0x97; /* seta */
1794 if (swapped) {
1795 gv(RC_FLOAT);
1796 vswap();
1798 assert(!(vtop[-1].r & VT_LVAL));
1800 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1801 o(0x66);
1802 o(0x2e0f); /* ucomisd */
1804 if (vtop->r & VT_LVAL) {
1805 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
1806 } else {
1807 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
1810 vtop--;
1811 vtop->r = VT_CMP;
1812 vtop->c.i = op | 0x100;
1813 } else {
1814 assert((vtop->type.t & VT_BTYPE) != VT_LDOUBLE);
1815 switch(op) {
1816 default:
1817 case '+':
1818 a = 0;
1819 break;
1820 case '-':
1821 a = 4;
1822 break;
1823 case '*':
1824 a = 1;
1825 break;
1826 case '/':
1827 a = 6;
1828 break;
1830 ft = vtop->type.t;
1831 fc = vtop->c.ul;
1832 assert((ft & VT_BTYPE) != VT_LDOUBLE);
1834 r = vtop->r;
1835 /* if saved lvalue, then we must reload it */
1836 if ((vtop->r & VT_VALMASK) == VT_LLOCAL) {
1837 SValue v1;
1838 r = get_reg(RC_INT);
1839 v1.type.t = VT_PTR;
1840 v1.r = VT_LOCAL | VT_LVAL;
1841 v1.c.ul = fc;
1842 load(r, &v1);
1843 fc = 0;
1846 assert(!(vtop[-1].r & VT_LVAL));
1847 if (swapped) {
1848 assert(vtop->r & VT_LVAL);
1849 gv(RC_FLOAT);
1850 vswap();
1853 if ((ft & VT_BTYPE) == VT_DOUBLE) {
1854 o(0xf2);
1855 } else {
1856 o(0xf3);
1858 o(0x0f);
1859 o(0x58 + a);
1861 if (vtop->r & VT_LVAL) {
1862 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
1863 } else {
1864 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
1867 vtop--;
1872 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1873 and 'long long' cases. */
1874 void gen_cvt_itof(int t)
1876 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1877 save_reg(TREG_ST0);
1878 gv(RC_INT);
1879 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1880 /* signed long long to float/double/long double (unsigned case
1881 is handled generically) */
1882 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1883 o(0x242cdf); /* fildll (%rsp) */
1884 o(0x08c48348); /* add $8, %rsp */
1885 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1886 (VT_INT | VT_UNSIGNED)) {
1887 /* unsigned int to float/double/long double */
1888 o(0x6a); /* push $0 */
1889 g(0x00);
1890 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1891 o(0x242cdf); /* fildll (%rsp) */
1892 o(0x10c48348); /* add $16, %rsp */
1893 } else {
1894 /* int to float/double/long double */
1895 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1896 o(0x2404db); /* fildl (%rsp) */
1897 o(0x08c48348); /* add $8, %rsp */
1899 vtop->r = TREG_ST0;
1900 } else {
1901 int r = get_reg(RC_FLOAT);
1902 gv(RC_INT);
1903 o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT?1:0));
1904 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1905 (VT_INT | VT_UNSIGNED) ||
1906 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
1907 o(0x48); /* REX */
1909 o(0x2a0f);
1910 o(0xc0 + (vtop->r & VT_VALMASK) + REG_VALUE(r)*8); /* cvtsi2sd */
1911 vtop->r = r;
1915 /* convert from one floating point type to another */
1916 void gen_cvt_ftof(int t)
1918 int ft, bt, tbt;
1920 ft = vtop->type.t;
1921 bt = ft & VT_BTYPE;
1922 tbt = t & VT_BTYPE;
1924 if (bt == VT_FLOAT) {
1925 gv(RC_FLOAT);
1926 if (tbt == VT_DOUBLE) {
1927 o(0x140f); /* unpcklps */
1928 o(0xc0 + REG_VALUE(vtop->r)*9);
1929 o(0x5a0f); /* cvtps2pd */
1930 o(0xc0 + REG_VALUE(vtop->r)*9);
1931 } else if (tbt == VT_LDOUBLE) {
1932 save_reg(RC_ST0);
1933 /* movss %xmm0,-0x10(%rsp) */
1934 o(0x110ff3);
1935 o(0x44 + REG_VALUE(vtop->r)*8);
1936 o(0xf024);
1937 o(0xf02444d9); /* flds -0x10(%rsp) */
1938 vtop->r = TREG_ST0;
1940 } else if (bt == VT_DOUBLE) {
1941 gv(RC_FLOAT);
1942 if (tbt == VT_FLOAT) {
1943 o(0x140f66); /* unpcklpd */
1944 o(0xc0 + REG_VALUE(vtop->r)*9);
1945 o(0x5a0f66); /* cvtpd2ps */
1946 o(0xc0 + REG_VALUE(vtop->r)*9);
1947 } else if (tbt == VT_LDOUBLE) {
1948 save_reg(RC_ST0);
1949 /* movsd %xmm0,-0x10(%rsp) */
1950 o(0x110ff2);
1951 o(0x44 + REG_VALUE(vtop->r)*8);
1952 o(0xf024);
1953 o(0xf02444dd); /* fldl -0x10(%rsp) */
1954 vtop->r = TREG_ST0;
1956 } else {
1957 gv(RC_ST0);
1958 int r = get_reg(RC_FLOAT);
1959 if (tbt == VT_DOUBLE) {
1960 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
1961 /* movsd -0x10(%rsp),%xmm0 */
1962 o(0x100ff2);
1963 o(0x44 + REG_VALUE(r)*8);
1964 o(0xf024);
1965 vtop->r = r;
1966 } else if (tbt == VT_FLOAT) {
1967 o(0xf0245cd9); /* fstps -0x10(%rsp) */
1968 /* movss -0x10(%rsp),%xmm0 */
1969 o(0x100ff3);
1970 o(0x44 + REG_VALUE(r)*8);
1971 o(0xf024);
1972 vtop->r = r;
1977 /* convert fp to int 't' type */
1978 void gen_cvt_ftoi(int t)
1980 int ft, bt, size, r;
1981 ft = vtop->type.t;
1982 bt = ft & VT_BTYPE;
1983 if (bt == VT_LDOUBLE) {
1984 gen_cvt_ftof(VT_DOUBLE);
1985 bt = VT_DOUBLE;
1988 gv(RC_FLOAT);
1989 if (t != VT_INT)
1990 size = 8;
1991 else
1992 size = 4;
1994 r = get_reg(RC_INT);
1995 if (bt == VT_FLOAT) {
1996 o(0xf3);
1997 } else if (bt == VT_DOUBLE) {
1998 o(0xf2);
1999 } else {
2000 assert(0);
2002 orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */
2003 o(0xc0 + REG_VALUE(vtop->r) + REG_VALUE(r)*8);
2004 vtop->r = r;
2007 /* computed goto support */
2008 void ggoto(void)
2010 gcall_or_jmp(1);
2011 vtop--;
2014 /* end of x86-64 code generator */
2015 /*************************************************************/
2016 #endif /* ! TARGET_DEFS_ONLY */
2017 /******************************************************/