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 */
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 */
37 #define RC_ST0 0x0020 /* only for long double */
40 #define RC_XMM0 0x0100
41 #define RC_XMM1 0x0200
42 #define RC_XMM2 0x0400
43 #define RC_XMM3 0x0800
44 #define RC_XMM4 0x1000
45 #define RC_XMM5 0x2000
46 #define RC_XMM6 0x4000
47 #define RC_XMM7 0x8000
48 #define RC_RSI 0x10000
49 #define RC_RDI 0x20000
50 #define RC_INT1 0x40000 /* function_pointer */
51 #define RC_INT2 0x80000
52 #define RC_RBX 0x100000
53 #define RC_R10 0x200000
54 #define RC_R11 0x400000
55 #define RC_R12 0x800000
56 #define RC_R13 0x1000000
57 #define RC_R14 0x2000000
58 #define RC_R15 0x4000000
59 #define RC_IRET RC_RAX /* function return: integer register */
60 #define RC_LRET RC_RDX /* function return: second integer register */
61 #define RC_FRET RC_XMM0 /* function return: float register */
62 #define RC_QRET RC_XMM1 /* function return: second float register */
63 #define RC_MASK (RC_INT|RC_INT1|RC_INT2|RC_FLOAT)
65 /* pretty names for the registers */
91 #define REX_BASE(reg) (((reg) >> 3) & 1)
92 #define REG_VALUE(reg) ((reg) & 7)
95 /* return registers for function */
96 #define REG_IRET TREG_RAX /* single word int return register */
97 #define REG_LRET TREG_RDX /* second word return register (for long long) */
98 #define REG_FRET TREG_XMM0 /* float return register */
99 #define REG_QRET TREG_XMM1 /* second float return register */
101 /* defined if function parameters must be evaluated in reverse order */
102 #define INVERT_FUNC_PARAMS
104 /* pointer size, in bytes */
107 /* long double size and alignment, in bytes */
108 #define LDOUBLE_SIZE 16
109 #define LDOUBLE_ALIGN 16
110 /* maximum alignment (for aligned attribute support) */
113 /******************************************************/
116 #define EM_TCC_TARGET EM_X86_64
118 /* relocation type for 32 bit data relocation */
119 #define R_DATA_32 R_X86_64_32
120 #define R_DATA_PTR R_X86_64_64
121 #define R_JMP_SLOT R_X86_64_JUMP_SLOT
122 #define R_COPY R_X86_64_COPY
124 #define ELF_START_ADDR 0x400000
125 #define ELF_PAGE_SIZE 0x200000
127 /******************************************************/
128 #else /* ! TARGET_DEFS_ONLY */
129 /******************************************************/
133 ST_DATA
const int reg_classes
[NB_REGS
] = {
134 /* eax */ RC_INT
|RC_RAX
|RC_INT2
,
135 /* ecx */ RC_INT
|RC_RCX
|RC_INT2
,
136 /* edx */ RC_INT
|RC_RDX
,
137 RC_INT
|RC_INT1
|RC_INT2
|RC_RBX
,
142 RC_INT
|RC_R8
|RC_INT2
,
143 RC_INT
|RC_R9
|RC_INT2
,
144 RC_INT
|RC_INT1
|RC_INT2
|RC_R10
,
145 RC_INT
|RC_INT1
|RC_INT2
|RC_R11
,
146 RC_INT
|RC_INT1
|RC_INT2
|RC_R12
,
147 RC_INT
|RC_INT1
|RC_INT2
|RC_R13
,
148 RC_INT
|RC_INT1
|RC_INT2
|RC_R14
,
149 RC_INT
|RC_INT1
|RC_INT2
|RC_R15
,
150 /* xmm0 */ RC_FLOAT
| RC_XMM0
,
160 static unsigned long func_sub_sp_offset
;
161 static int func_ret_sub
;
163 /* XXX: make it faster ? */
168 if (ind1
> cur_text_section
->data_allocated
)
169 section_realloc(cur_text_section
, ind1
);
170 cur_text_section
->data
[ind
] = c
;
174 void o(unsigned int c
)
196 void gen_le64(int64_t c
)
208 void orex(int ll
, int r
, int r2
, int b
)
210 if ((r
& VT_VALMASK
) >= VT_CONST
)
212 if ((r2
& VT_VALMASK
) >= VT_CONST
)
214 if (ll
|| REX_BASE(r
) || REX_BASE(r2
))
215 o(0x40 | REX_BASE(r
) | (REX_BASE(r2
) << 2) | (ll
<< 3));
219 /* output a symbol and patch all calls to it */
220 void gsym_addr(int t
, int a
)
224 ptr
= (int *)(cur_text_section
->data
+ t
);
225 n
= *ptr
; /* next value */
236 /* psym is used to put an instruction with a data field which is a
237 reference to a symbol. It is in fact the same as oad ! */
240 static int is64_type(int t
)
242 return ((t
& VT_BTYPE
) == VT_PTR
||
243 (t
& VT_BTYPE
) == VT_FUNC
||
244 (t
& VT_BTYPE
) == VT_LLONG
);
247 /* instruction + 4 bytes data. Return the address of the data */
248 ST_FUNC
int oad(int c
, int s
)
254 if (ind1
> cur_text_section
->data_allocated
)
255 section_realloc(cur_text_section
, ind1
);
256 *(int *)(cur_text_section
->data
+ ind
) = s
;
262 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
)
265 greloc(cur_text_section
, sym
, ind
, R_X86_64_32
);
269 /* output constant with relocation if 'r & VT_SYM' is true */
270 ST_FUNC
void gen_addr64(int r
, Sym
*sym
, int64_t c
)
273 greloc(cur_text_section
, sym
, ind
, R_X86_64_64
);
277 /* output constant with relocation if 'r & VT_SYM' is true */
278 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
)
281 greloc(cur_text_section
, sym
, ind
, R_X86_64_PC32
);
285 /* output got address with relocation */
286 static void gen_gotpcrel(int r
, Sym
*sym
, int c
)
288 #ifndef TCC_TARGET_PE
291 greloc(cur_text_section
, sym
, ind
, R_X86_64_GOTPCREL
);
292 sr
= cur_text_section
->reloc
;
293 rel
= (ElfW(Rela
) *)(sr
->data
+ sr
->data_offset
- sizeof(ElfW(Rela
)));
296 printf("picpic: %s %x %x | %02x %02x %02x\n", get_tok_str(sym
->v
, NULL
), c
, r
,
297 cur_text_section
->data
[ind
-3],
298 cur_text_section
->data
[ind
-2],
299 cur_text_section
->data
[ind
-1]
301 greloc(cur_text_section
, sym
, ind
, R_X86_64_PC32
);
305 /* we use add c, %xxx for displacement */
307 o(0xc0 + REG_VALUE(r
));
312 static void gen_modrm_impl(int op_reg
, int r
, Sym
*sym
, int c
, int is_got
)
314 op_reg
= REG_VALUE(op_reg
) << 3;
315 if ((r
& VT_VALMASK
) == VT_CONST
) {
316 /* constant memory reference */
319 gen_gotpcrel(r
, sym
, c
);
321 gen_addrpc32(r
, sym
, c
);
323 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
324 /* currently, we use only ebp as base */
326 /* short reference */
330 oad(0x85 | op_reg
, c
);
332 } else if (r
& TREG_MEM
) {
334 g(0x80 | op_reg
| REG_VALUE(r
));
337 g(0x00 | op_reg
| REG_VALUE(r
));
340 g(0x00 | op_reg
| REG_VALUE(r
));
344 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
346 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
348 gen_modrm_impl(op_reg
, r
, sym
, c
, 0);
351 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
353 static void gen_modrm64(int opcode
, int op_reg
, int r
, Sym
*sym
, int c
)
356 is_got
= (op_reg
& TREG_MEM
) && !(sym
->type
.t
& VT_STATIC
);
357 orex(1, r
, op_reg
, opcode
);
358 gen_modrm_impl(op_reg
, r
, sym
, c
, is_got
);
362 /* load 'r' from value 'sv' */
363 void load(int r
, SValue
*sv
)
365 int v
, t
, ft
, fc
, fr
;
370 sv
= pe_getimport(sv
, &v2
);
374 ft
= sv
->type
.t
& ~VT_DEFSIGN
;
377 #ifndef TCC_TARGET_PE
378 /* we use indirect access via got */
379 if ((fr
& VT_VALMASK
) == VT_CONST
&& (fr
& VT_SYM
) &&
380 (fr
& VT_LVAL
) && !(sv
->sym
->type
.t
& VT_STATIC
)) {
381 /* use the result register as a temporal register */
382 int tr
= r
| TREG_MEM
;
384 /* we cannot use float registers as a temporal register */
385 tr
= get_reg(RC_INT
) | TREG_MEM
;
387 gen_modrm64(0x8b, tr
, fr
, sv
->sym
, 0);
389 /* load from the temporal register */
397 if (v
== VT_LLOCAL
) {
399 v1
.r
= VT_LOCAL
| VT_LVAL
;
402 if (!(reg_classes
[fr
] & RC_INT
))
403 fr
= get_reg(RC_INT
);
407 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
409 r
= REG_VALUE(r
); /* movd */
410 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
411 b
= 0x7e0ff3; /* movq */
413 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
414 b
= 0xdb, r
= 5; /* fldt */
415 } else if ((ft
& VT_TYPE
) == VT_BYTE
|| (ft
& VT_TYPE
) == VT_BOOL
) {
416 b
= 0xbe0f; /* movsbl */
417 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
418 b
= 0xb60f; /* movzbl */
419 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
420 b
= 0xbf0f; /* movswl */
421 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
422 b
= 0xb70f; /* movzwl */
424 assert(((ft
& VT_BTYPE
) == VT_INT
) || ((ft
& VT_BTYPE
) == VT_LLONG
)
425 || ((ft
& VT_BTYPE
) == VT_PTR
) || ((ft
& VT_BTYPE
) == VT_ENUM
)
426 || ((ft
& VT_BTYPE
) == VT_FUNC
));
431 gen_modrm64(b
, r
, fr
, sv
->sym
, fc
);
434 gen_modrm(r
, fr
, sv
->sym
, fc
);
441 o(0x05 + REG_VALUE(r
) * 8); /* lea xx(%rip), r */
442 gen_addrpc32(fr
, sv
->sym
, fc
);
444 if (sv
->sym
->type
.t
& VT_STATIC
) {
446 o(0x05 + REG_VALUE(r
) * 8); /* lea xx(%rip), r */
447 gen_addrpc32(fr
, sv
->sym
, fc
);
450 o(0x05 + REG_VALUE(r
) * 8); /* mov xx(%rip), r */
451 gen_gotpcrel(r
, sv
->sym
, fc
);
454 } else if (is64_type(ft
)) {
455 orex(1,r
,0, 0xb8 + REG_VALUE(r
)); /* mov $xx, r */
458 orex(0,r
,0, 0xb8 + REG_VALUE(r
)); /* mov $xx, r */
461 } else if (v
== VT_LOCAL
) {
462 orex(1,0,r
,0x8d); /* lea xxx(%ebp), r */
463 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
464 } else if (v
== VT_CMP
) {
466 if ((fc
& ~0x100) != TOK_NE
)
467 oad(0xb8 + REG_VALUE(r
), 0); /* mov $0, r */
469 oad(0xb8 + REG_VALUE(r
), 1); /* mov $1, r */
472 /* This was a float compare. If the parity bit is
473 set the result was unordered, meaning false for everything
474 except TOK_NE, and true for TOK_NE. */
476 o(0x037a + (REX_BASE(r
) << 8));
478 orex(0,r
,0, 0x0f); /* setxx %br */
480 o(0xc0 + REG_VALUE(r
));
481 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
484 oad(0xb8 + REG_VALUE(r
), t
); /* mov $1, r */
485 o(0x05eb + (REX_BASE(r
) << 8)); /* jmp after */
488 oad(0xb8 + REG_VALUE(r
), t
^ 1); /* mov $0, r */
490 if ((r
>= TREG_XMM0
) && (r
<= TREG_XMM7
)) {
492 /* gen_cvt_ftof(VT_DOUBLE); */
493 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
494 /* movsd -0x10(%rsp),%xmmN */
496 o(0x44 + REG_VALUE(r
)*8); /* %xmmN */
499 assert((v
>= TREG_XMM0
) && (v
<= TREG_XMM7
));
500 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
503 assert((ft
& VT_BTYPE
) == VT_DOUBLE
);
506 o(0xc0 + REG_VALUE(v
) + REG_VALUE(r
)*8);
508 } else if (r
== TREG_ST0
) {
509 assert((v
>= TREG_XMM0
) && (v
<= TREG_XMM7
));
510 /* gen_cvt_ftof(VT_LDOUBLE); */
511 /* movsd %xmmN,-0x10(%rsp) */
513 o(0x44 + REG_VALUE(r
)*8); /* %xmmN */
515 o(0xf02444dd); /* fldl -0x10(%rsp) */
518 o(0xc0 + REG_VALUE(r
) + REG_VALUE(v
) * 8); /* mov v, r */
524 /* store register 'r' in lvalue 'v' */
525 void store(int r
, SValue
*v
)
529 /* store the REX prefix in this variable when PIC is enabled */
534 v
= pe_getimport(v
, &v2
);
539 fr
= v
->r
& VT_VALMASK
;
542 #ifndef TCC_TARGET_PE
543 /* we need to access the variable via got */
544 if (fr
== VT_CONST
&& (v
->r
& VT_SYM
)) {
545 /* mov xx(%rip), %r11 */
547 gen_gotpcrel(TREG_R11
, v
->sym
, v
->c
.ul
);
548 pic
= is64_type(bt
) ? 0x49 : 0x41;
552 /* XXX: incorrect if float reg to reg */
553 if (bt
== VT_FLOAT
) {
556 o(0x7e0f); /* movd */
558 } else if (bt
== VT_DOUBLE
) {
561 o(0xd60f); /* movq */
563 } else if (bt
== VT_LDOUBLE
) {
564 o(0xc0d9); /* fld %st(0) */
572 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
574 else if (is64_type(bt
))
580 /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
585 if (fr
== VT_CONST
|| fr
== VT_LOCAL
|| (v
->r
& VT_LVAL
)) {
586 gen_modrm64(op64
, r
, v
->r
, v
->sym
, fc
);
587 } else if (fr
!= r
) {
588 /* XXX: don't we really come here? */
590 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
593 if (fr
== VT_CONST
|| fr
== VT_LOCAL
|| (v
->r
& VT_LVAL
)) {
594 gen_modrm(r
, v
->r
, v
->sym
, fc
);
595 } else if (fr
!= r
) {
596 /* XXX: don't we really come here? */
598 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
603 /* 'is_jmp' is '1' if it is a jump */
604 static void gcall_or_jmp(int is_jmp
)
607 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
609 if (vtop
->r
& VT_SYM
) {
610 /* relocation case */
611 greloc(cur_text_section
, vtop
->sym
,
612 ind
+ 1, R_X86_64_PLT32
);
614 /* put an empty PC32 relocation */
615 put_elf_reloc(symtab_section
, cur_text_section
,
616 ind
+ 1, R_X86_64_PC32
, 0);
618 oad(0xe8 + is_jmp
, vtop
->c
.ul
- 4); /* call/jmp im */
620 /* otherwise, indirect call */
624 o(0xff); /* call/jmp *r */
625 o(0xd0 + REG_VALUE(r
) + (is_jmp
<< 4));
632 static const uint8_t arg_regs
[REGN
] = {
633 TREG_RCX
, TREG_RDX
, TREG_R8
, TREG_R9
636 /* Prepare arguments in R10 and R11 rather than RCX and RDX
637 because gv() will not ever use these */
638 static int arg_prepare_reg(int idx
) {
639 if (idx
== 0 || idx
== 1)
640 /* idx=0: r10, idx=1: r11 */
643 return arg_regs
[idx
];
646 static int func_scratch
;
648 /* Generate function call. The function address is pushed first, then
649 all the parameters in call order. This functions pops all the
650 parameters and the function address. */
652 void gen_offs_sp(int b
, int r
, int d
)
654 orex(1,0,r
& 0x100 ? 0 : r
, b
);
656 o(0x2444 | (REG_VALUE(r
) << 3));
659 o(0x2484 | (REG_VALUE(r
) << 3));
664 /* Return the number of registers needed to return the struct, or 0 if
665 returning via struct pointer. */
666 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *ret_align
)
669 *ret_align
= 1; // Never have to re-align return values for x86-64
670 size
= type_size(vt
, &align
);
674 } else if (size
> 4) {
677 } else if (size
> 2) {
680 } else if (size
> 1) {
689 static int is_sse_float(int t
) {
692 return bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
695 int gfunc_arg_size(CType
*type
) {
697 if (type
->t
& (VT_ARRAY
|VT_BITFIELD
))
699 return type_size(type
, &align
);
702 void gfunc_call(int nb_args
)
704 int size
, r
, args_size
, i
, d
, bt
, struct_size
;
707 args_size
= (nb_args
< REGN
? REGN
: nb_args
) * PTR_SIZE
;
710 /* for struct arguments, we need to call memcpy and the function
711 call breaks register passing arguments we are preparing.
712 So, we process arguments which will be passed by stack first. */
713 struct_size
= args_size
;
714 for(i
= 0; i
< nb_args
; i
++) {
719 bt
= (sv
->type
.t
& VT_BTYPE
);
720 size
= gfunc_arg_size(&sv
->type
);
723 continue; /* arguments smaller than 8 bytes passed in registers or on stack */
725 if (bt
== VT_STRUCT
) {
726 /* align to stack align size */
727 size
= (size
+ 15) & ~15;
728 /* generate structure store */
730 gen_offs_sp(0x8d, r
, struct_size
);
733 /* generate memcpy call */
734 vset(&sv
->type
, r
| VT_LVAL
, 0);
738 } else if (bt
== VT_LDOUBLE
) {
740 gen_offs_sp(0xdb, 0x107, struct_size
);
745 if (func_scratch
< struct_size
)
746 func_scratch
= struct_size
;
749 struct_size
= args_size
;
751 for(i
= 0; i
< nb_args
; i
++) {
753 bt
= (vtop
->type
.t
& VT_BTYPE
);
755 size
= gfunc_arg_size(&vtop
->type
);
757 /* align to stack align size */
758 size
= (size
+ 15) & ~15;
761 gen_offs_sp(0x8d, d
, struct_size
);
762 gen_offs_sp(0x89, d
, arg
*8);
764 d
= arg_prepare_reg(arg
);
765 gen_offs_sp(0x8d, d
, struct_size
);
769 if (is_sse_float(vtop
->type
.t
)) {
770 gv(RC_XMM0
); /* only use one float register */
772 /* movq %xmm0, j*8(%rsp) */
773 gen_offs_sp(0xd60f66, 0x100, arg
*8);
775 /* movaps %xmm0, %xmmN */
777 o(0xc0 + (arg
<< 3));
778 d
= arg_prepare_reg(arg
);
779 /* mov %xmm0, %rxx */
782 o(0xc0 + REG_VALUE(d
));
785 if (bt
== VT_STRUCT
) {
786 vtop
->type
.ref
= NULL
;
787 vtop
->type
.t
= size
> 4 ? VT_LLONG
: size
> 2 ? VT_INT
788 : size
> 1 ? VT_SHORT
: VT_BYTE
;
793 gen_offs_sp(0x89, r
, arg
*8);
795 d
= arg_prepare_reg(arg
);
796 orex(1,d
,r
,0x89); /* mov */
797 o(0xc0 + REG_VALUE(r
) * 8 + REG_VALUE(d
));
805 /* Copy R10 and R11 into RCX and RDX, respectively */
807 o(0xd1894c); /* mov %r10, %rcx */
809 o(0xda894c); /* mov %r11, %rdx */
818 #define FUNC_PROLOG_SIZE 11
820 /* generate function prolog of type 't' */
821 void gfunc_prolog(CType
*func_type
)
823 int addr
, reg_param_index
, bt
, size
;
832 ind
+= FUNC_PROLOG_SIZE
;
833 func_sub_sp_offset
= ind
;
836 sym
= func_type
->ref
;
838 /* if the function returns a structure, then add an
839 implicit pointer parameter */
841 func_var
= (sym
->c
== FUNC_ELLIPSIS
);
842 size
= gfunc_arg_size(&func_vt
);
844 gen_modrm64(0x89, arg_regs
[reg_param_index
], VT_LOCAL
, NULL
, addr
);
850 /* define parameters */
851 while ((sym
= sym
->next
) != NULL
) {
853 bt
= type
->t
& VT_BTYPE
;
854 size
= gfunc_arg_size(type
);
856 if (reg_param_index
< REGN
) {
857 gen_modrm64(0x89, arg_regs
[reg_param_index
], VT_LOCAL
, NULL
, addr
);
859 sym_push(sym
->v
& ~SYM_FIELD
, type
, VT_LOCAL
| VT_LVAL
| VT_REF
, addr
);
861 if (reg_param_index
< REGN
) {
862 /* save arguments passed by register */
863 if ((bt
== VT_FLOAT
) || (bt
== VT_DOUBLE
)) {
864 o(0xd60f66); /* movq */
865 gen_modrm(reg_param_index
, VT_LOCAL
, NULL
, addr
);
867 gen_modrm64(0x89, arg_regs
[reg_param_index
], VT_LOCAL
, NULL
, addr
);
870 sym_push(sym
->v
& ~SYM_FIELD
, type
, VT_LOCAL
| VT_LVAL
, addr
);
876 while (reg_param_index
< REGN
) {
877 if (func_type
->ref
->c
== FUNC_ELLIPSIS
) {
878 gen_modrm64(0x89, arg_regs
[reg_param_index
], VT_LOCAL
, NULL
, addr
);
885 /* generate function epilog */
886 void gfunc_epilog(void)
891 if (func_ret_sub
== 0) {
896 g(func_ret_sub
>> 8);
900 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
901 /* align local size to word & save local variables */
902 v
= (func_scratch
+ -loc
+ 15) & -16;
905 Sym
*sym
= external_global_sym(TOK___chkstk
, &func_old_type
, 0);
906 oad(0xb8, v
); /* mov stacksize, %eax */
907 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
908 greloc(cur_text_section
, sym
, ind
-4, R_X86_64_PC32
);
909 o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
911 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
912 o(0xec8148); /* sub rsp, stacksize */
916 cur_text_section
->data_offset
= saved_ind
;
917 pe_add_unwind_data(ind
, saved_ind
, v
);
918 ind
= cur_text_section
->data_offset
;
923 static void gadd_sp(int val
)
925 if (val
== (char)val
) {
929 oad(0xc48148, val
); /* add $xxx, %rsp */
933 typedef enum X86_64_Mode
{
941 static X86_64_Mode
classify_x86_64_merge(X86_64_Mode a
, X86_64_Mode b
)
945 else if (a
== x86_64_mode_none
)
947 else if (b
== x86_64_mode_none
)
949 else if ((a
== x86_64_mode_memory
) || (b
== x86_64_mode_memory
))
950 return x86_64_mode_memory
;
951 else if ((a
== x86_64_mode_integer
) || (b
== x86_64_mode_integer
))
952 return x86_64_mode_integer
;
953 else if ((a
== x86_64_mode_x87
) || (b
== x86_64_mode_x87
))
954 return x86_64_mode_memory
;
956 return x86_64_mode_sse
;
959 static X86_64_Mode
classify_x86_64_inner(CType
*ty
)
964 switch (ty
->t
& VT_BTYPE
) {
965 case VT_VOID
: return x86_64_mode_none
;
974 case VT_ENUM
: return x86_64_mode_integer
;
977 case VT_DOUBLE
: return x86_64_mode_sse
;
979 case VT_LDOUBLE
: return x86_64_mode_x87
;
985 if (f
->next
&& (f
->c
== f
->next
->c
))
986 return x86_64_mode_memory
;
988 mode
= x86_64_mode_none
;
989 for (f
= f
->next
; f
; f
= f
->next
)
990 mode
= classify_x86_64_merge(mode
, classify_x86_64_inner(&f
->type
));
998 static X86_64_Mode
classify_x86_64_arg(CType
*ty
, CType
*ret
, int *psize
, int *palign
, int *reg_count
)
1001 int size
, align
, ret_t
= 0;
1003 if (ty
->t
& (VT_BITFIELD
|VT_ARRAY
)) {
1008 mode
= x86_64_mode_integer
;
1010 size
= type_size(ty
, &align
);
1011 *psize
= (size
+ 7) & ~7;
1012 *palign
= (align
+ 7) & ~7;
1015 mode
= x86_64_mode_memory
;
1017 mode
= classify_x86_64_inner(ty
);
1019 case x86_64_mode_integer
:
1025 ret_t
= (size
> 4) ? VT_LLONG
: VT_INT
;
1029 case x86_64_mode_x87
:
1034 case x86_64_mode_sse
:
1040 ret_t
= (size
> 4) ? VT_DOUBLE
: VT_FLOAT
;
1043 default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/
1056 ST_FUNC
int classify_x86_64_va_arg(CType
*ty
)
1058 /* This definition must be synced with stdarg.h */
1059 enum __va_arg_type
{
1060 __va_gen_reg
, __va_float_reg
, __va_stack
1062 int size
, align
, reg_count
;
1063 X86_64_Mode mode
= classify_x86_64_arg(ty
, NULL
, &size
, &align
, ®_count
);
1065 default: return __va_stack
;
1066 case x86_64_mode_integer
: return __va_gen_reg
;
1067 case x86_64_mode_sse
: return __va_float_reg
;
1071 /* Return the number of registers needed to return the struct, or 0 if
1072 returning via struct pointer. */
1073 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *ret_align
)
1075 int size
, align
, reg_count
;
1076 *ret_align
= 1; // Never have to re-align return values for x86-64
1077 return (classify_x86_64_arg(vt
, ret
, &size
, &align
, ®_count
) != x86_64_mode_memory
);
1081 static const uint8_t arg_regs
[REGN
] = {
1082 TREG_RDI
, TREG_RSI
, TREG_RDX
, TREG_RCX
, TREG_R8
, TREG_R9
1085 static int arg_prepare_reg(int idx
) {
1086 if (idx
== 2 || idx
== 3)
1087 /* idx=2: r10, idx=3: r11 */
1090 return arg_regs
[idx
];
1093 /* Generate function call. The function address is pushed first, then
1094 all the parameters in call order. This functions pops all the
1095 parameters and the function address. */
1096 void gfunc_call(int nb_args
)
1100 int size
, align
, r
, args_size
, stack_adjust
, run_start
, run_end
, i
, reg_count
;
1101 int nb_reg_args
= 0;
1102 int nb_sse_args
= 0;
1103 int sse_reg
, gen_reg
;
1105 /* calculate the number of integer/float register arguments */
1106 for(i
= 0; i
< nb_args
; i
++) {
1107 mode
= classify_x86_64_arg(&vtop
[-i
].type
, NULL
, &size
, &align
, ®_count
);
1108 if (mode
== x86_64_mode_sse
)
1109 nb_sse_args
+= reg_count
;
1110 else if (mode
== x86_64_mode_integer
)
1111 nb_reg_args
+= reg_count
;
1114 /* arguments are collected in runs. Each run is a collection of 8-byte aligned arguments
1115 and ended by a 16-byte aligned argument. This is because, from the point of view of
1116 the callee, argument alignment is computed from the bottom up. */
1117 /* for struct arguments, we need to call memcpy and the function
1118 call breaks register passing arguments we are preparing.
1119 So, we process arguments which will be passed by stack first. */
1120 gen_reg
= nb_reg_args
;
1121 sse_reg
= nb_sse_args
;
1124 while (run_start
!= nb_args
) {
1125 int run_gen_reg
= gen_reg
, run_sse_reg
= sse_reg
;
1129 for(i
= run_start
; (i
< nb_args
) && (run_end
== nb_args
); i
++) {
1130 mode
= classify_x86_64_arg(&vtop
[-i
].type
, NULL
, &size
, &align
, ®_count
);
1132 case x86_64_mode_memory
:
1133 case x86_64_mode_x87
:
1138 stack_adjust
+= size
;
1141 case x86_64_mode_sse
:
1142 sse_reg
-= reg_count
;
1143 if (sse_reg
+ reg_count
> 8) goto stack_arg
;
1146 case x86_64_mode_integer
:
1147 gen_reg
-= reg_count
;
1148 if (gen_reg
+ reg_count
> REGN
) goto stack_arg
;
1150 default: break; /* nothing to be done for x86_64_mode_none */
1154 gen_reg
= run_gen_reg
;
1155 sse_reg
= run_sse_reg
;
1157 /* adjust stack to align SSE boundary */
1158 if (stack_adjust
&= 15) {
1159 /* fetch cpu flag before the following sub will change the value */
1160 if (vtop
>= vstack
&& (vtop
->r
& VT_VALMASK
) == VT_CMP
)
1163 stack_adjust
= 16 - stack_adjust
;
1165 oad(0xec81, stack_adjust
); /* sub $xxx, %rsp */
1166 args_size
+= stack_adjust
;
1169 for(i
= run_start
; i
< run_end
;) {
1170 /* Swap argument to top, it will possibly be changed here,
1171 and might use more temps. At the end of the loop we keep
1172 in on the stack and swap it back to its original position
1173 if it is a register. */
1174 SValue tmp
= vtop
[0];
1178 mode
= classify_x86_64_arg(&vtop
->type
, NULL
, &size
, &align
, ®_count
);
1181 switch (vtop
->type
.t
& VT_BTYPE
) {
1183 if (mode
== x86_64_mode_sse
) {
1185 sse_reg
-= reg_count
;
1188 } else if (mode
== x86_64_mode_integer
) {
1190 gen_reg
-= reg_count
;
1196 /* allocate the necessary size on stack */
1198 oad(0xec81, size
); /* sub $xxx, %rsp */
1199 /* generate structure store */
1200 r
= get_reg(RC_INT
);
1201 orex(1, r
, 0, 0x89); /* mov %rsp, r */
1202 o(0xe0 + REG_VALUE(r
));
1203 vset(&vtop
->type
, r
| VT_LVAL
, 0);
1216 assert(mode
== x86_64_mode_sse
);
1220 o(0x50); /* push $rax */
1221 /* movq %xmmN, (%rsp) */
1223 o(0x04 + REG_VALUE(r
)*8);
1232 assert(mode
== x86_64_mode_integer
);
1234 /* XXX: implicit cast ? */
1235 if (gen_reg
> REGN
) {
1238 orex(0,r
,0,0x50 + REG_VALUE(r
)); /* push r */
1246 /* And swap the argument back to it's original position. */
1253 assert((vtop
->type
.t
== tmp
.type
.t
) && (vtop
->r
== tmp
.r
));
1262 /* handle 16 byte aligned arguments at end of run */
1263 run_start
= i
= run_end
;
1264 while (i
< nb_args
) {
1265 /* Rotate argument to top since it will always be popped */
1266 mode
= classify_x86_64_arg(&vtop
[-i
].type
, NULL
, &size
, &align
, ®_count
);
1272 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
1274 oad(0xec8148, size
); /* sub $xxx, %rsp */
1275 o(0x7cdb); /* fstpt 0(%rsp) */
1280 //assert(mode == x86_64_mode_memory);
1282 /* allocate the necessary size on stack */
1284 oad(0xec81, size
); /* sub $xxx, %rsp */
1285 /* generate structure store */
1286 r
= get_reg(RC_INT
);
1287 orex(1, r
, 0, 0x89); /* mov %rsp, r */
1288 o(0xe0 + REG_VALUE(r
));
1289 vset(&vtop
->type
, r
| VT_LVAL
, 0);
1300 /* XXX This should be superfluous. */
1301 save_regs(0); /* save used temporary registers */
1303 /* then, we prepare register passing arguments.
1304 Note that we cannot set RDX and RCX in this loop because gv()
1305 may break these temporary registers. Let's use R10 and R11
1307 assert(gen_reg
<= REGN
);
1308 assert(sse_reg
<= 8);
1309 for(i
= 0; i
< nb_args
; i
++) {
1310 mode
= classify_x86_64_arg(&vtop
->type
, &type
, &size
, &align
, ®_count
);
1311 /* Alter stack entry type so that gv() knows how to treat it */
1313 if (mode
== x86_64_mode_sse
) {
1314 if (reg_count
== 2) {
1316 gv(RC_FRET
); /* Use pair load into xmm0 & xmm1 */
1317 if (sse_reg
) { /* avoid redundant movaps %xmm0, %xmm0 */
1318 /* movaps %xmm0, %xmmN */
1320 o(0xc0 + (sse_reg
<< 3));
1321 /* movaps %xmm1, %xmmN */
1323 o(0xc1 + ((sse_reg
+1) << 3));
1326 assert(reg_count
== 1);
1328 /* Load directly to register */
1329 gv(RC_XMM0
<< sse_reg
);
1331 } else if (mode
== x86_64_mode_integer
) {
1333 /* XXX: implicit cast ? */
1334 gen_reg
-= reg_count
;
1336 int d
= arg_prepare_reg(gen_reg
);
1337 orex(1,d
,r
,0x89); /* mov */
1338 o(0xc0 + REG_VALUE(r
) * 8 + REG_VALUE(d
));
1339 if (reg_count
== 2) {
1340 d
= arg_prepare_reg(gen_reg
+1);
1341 orex(1,d
,vtop
->r2
,0x89); /* mov */
1342 o(0xc0 + REG_VALUE(vtop
->r2
) * 8 + REG_VALUE(d
));
1347 assert(gen_reg
== 0);
1348 assert(sse_reg
== 0);
1350 /* We shouldn't have many operands on the stack anymore, but the
1351 call address itself is still there, and it might be in %eax
1352 (or edx/ecx) currently, which the below writes would clobber.
1353 So evict all remaining operands here. */
1356 /* Copy R10 and R11 into RDX and RCX, respectively */
1357 if (nb_reg_args
> 2) {
1358 o(0xd2894c); /* mov %r10, %rdx */
1359 if (nb_reg_args
> 3) {
1360 o(0xd9894c); /* mov %r11, %rcx */
1364 oad(0xb8, nb_sse_args
< 8 ? nb_sse_args
: 8); /* mov nb_sse_args, %eax */
1372 #define FUNC_PROLOG_SIZE 11
1374 static void push_arg_reg(int i
) {
1376 gen_modrm64(0x89, arg_regs
[i
], VT_LOCAL
, NULL
, loc
);
1379 /* generate function prolog of type 't' */
1380 void gfunc_prolog(CType
*func_type
)
1383 int i
, addr
, align
, size
, reg_count
;
1384 int param_addr
= 0, reg_param_index
, sse_param_index
;
1388 sym
= func_type
->ref
;
1389 addr
= PTR_SIZE
* 2;
1391 ind
+= FUNC_PROLOG_SIZE
;
1392 func_sub_sp_offset
= ind
;
1395 if (func_type
->ref
->c
== FUNC_ELLIPSIS
) {
1396 int seen_reg_num
, seen_sse_num
, seen_stack_size
;
1397 seen_reg_num
= seen_sse_num
= 0;
1398 /* frame pointer and return address */
1399 seen_stack_size
= PTR_SIZE
* 2;
1400 /* count the number of seen parameters */
1401 sym
= func_type
->ref
;
1402 while ((sym
= sym
->next
) != NULL
) {
1404 mode
= classify_x86_64_arg(type
, NULL
, &size
, &align
, ®_count
);
1408 seen_stack_size
= ((seen_stack_size
+ align
- 1) & -align
) + size
;
1411 case x86_64_mode_integer
:
1412 if (seen_reg_num
+ reg_count
<= 8) {
1413 seen_reg_num
+= reg_count
;
1420 case x86_64_mode_sse
:
1421 if (seen_sse_num
+ reg_count
<= 8) {
1422 seen_sse_num
+= reg_count
;
1432 /* movl $0x????????, -0x10(%rbp) */
1434 gen_le32(seen_reg_num
* 8);
1435 /* movl $0x????????, -0xc(%rbp) */
1437 gen_le32(seen_sse_num
* 16 + 48);
1438 /* movl $0x????????, -0x8(%rbp) */
1440 gen_le32(seen_stack_size
);
1442 /* save all register passing arguments */
1443 for (i
= 0; i
< 8; i
++) {
1445 o(0xd60f66); /* movq */
1446 gen_modrm(7 - i
, VT_LOCAL
, NULL
, loc
);
1447 /* movq $0, loc+8(%rbp) */
1452 for (i
= 0; i
< REGN
; i
++) {
1453 push_arg_reg(REGN
-1-i
);
1457 sym
= func_type
->ref
;
1458 reg_param_index
= 0;
1459 sse_param_index
= 0;
1461 /* if the function returns a structure, then add an
1462 implicit pointer parameter */
1463 func_vt
= sym
->type
;
1464 mode
= classify_x86_64_arg(&func_vt
, NULL
, &size
, &align
, ®_count
);
1465 if (mode
== x86_64_mode_memory
) {
1466 push_arg_reg(reg_param_index
);
1470 /* define parameters */
1471 while ((sym
= sym
->next
) != NULL
) {
1473 mode
= classify_x86_64_arg(type
, NULL
, &size
, &align
, ®_count
);
1475 case x86_64_mode_sse
:
1476 if (sse_param_index
+ reg_count
<= 8) {
1477 /* save arguments passed by register */
1478 loc
-= reg_count
* 8;
1480 for (i
= 0; i
< reg_count
; ++i
) {
1481 o(0xd60f66); /* movq */
1482 gen_modrm(sse_param_index
, VT_LOCAL
, NULL
, param_addr
+ i
*8);
1486 addr
= (addr
+ align
- 1) & -align
;
1489 sse_param_index
+= reg_count
;
1493 case x86_64_mode_memory
:
1494 case x86_64_mode_x87
:
1495 addr
= (addr
+ align
- 1) & -align
;
1500 case x86_64_mode_integer
: {
1501 if (reg_param_index
+ reg_count
<= REGN
) {
1502 /* save arguments passed by register */
1503 loc
-= reg_count
* 8;
1505 for (i
= 0; i
< reg_count
; ++i
) {
1506 gen_modrm64(0x89, arg_regs
[reg_param_index
], VT_LOCAL
, NULL
, param_addr
+ i
*8);
1510 addr
= (addr
+ align
- 1) & -align
;
1513 reg_param_index
+= reg_count
;
1517 default: break; /* nothing to be done for x86_64_mode_none */
1519 sym_push(sym
->v
& ~SYM_FIELD
, type
,
1520 VT_LOCAL
| VT_LVAL
, param_addr
);
1524 /* generate function epilog */
1525 void gfunc_epilog(void)
1529 o(0xc9); /* leave */
1530 if (func_ret_sub
== 0) {
1533 o(0xc2); /* ret n */
1535 g(func_ret_sub
>> 8);
1537 /* align local size to word & save local variables */
1538 v
= (-loc
+ 15) & -16;
1540 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
1541 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1542 o(0xec8148); /* sub rsp, stacksize */
1549 /* generate a jump to a label */
1552 return psym(0xe9, t
);
1555 /* generate a jump to a fixed address */
1556 void gjmp_addr(int a
)
1564 oad(0xe9, a
- ind
- 5);
1568 /* generate a test. set 'inv' to invert test. Stack entry is popped */
1569 int gtst(int inv
, int t
)
1573 v
= vtop
->r
& VT_VALMASK
;
1575 /* fast case : can jump directly since flags are set */
1576 if (vtop
->c
.i
& 0x100)
1578 /* This was a float compare. If the parity flag is set
1579 the result was unordered. For anything except != this
1580 means false and we don't jump (anding both conditions).
1581 For != this means true (oring both).
1582 Take care about inverting the test. We need to jump
1583 to our target if the result was unordered and test wasn't NE,
1584 otherwise if unordered we don't want to jump. */
1585 vtop
->c
.i
&= ~0x100;
1586 if (!inv
== (vtop
->c
.i
!= TOK_NE
))
1587 o(0x067a); /* jp +6 */
1591 t
= psym(0x8a, t
); /* jp t */
1595 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
1596 } else { /* VT_JMP || VT_JMPI */
1597 /* && or || optimization */
1598 if ((v
& 1) == inv
) {
1599 /* insert vtop->c jump list in t */
1602 p
= (int *)(cur_text_section
->data
+ *p
);
1614 /* generate an integer binary operation */
1615 void gen_opi(int op
)
1617 int r
, fr
, opc
, fc
, c
, ll
, uu
, cc
, tt2
;
1621 ll
= is64_type(vtop
[-1].type
.t
);
1622 cc
= (fr
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
1623 tt2
= (fr
& (VT_LVAL
| VT_LVAL_TYPE
)) == VT_LVAL
;
1627 case TOK_ADDC1
: /* add with carry generation */
1633 if (cc
&& (!ll
|| (int)vtop
->c
.ll
== vtop
->c
.ll
)) {
1637 /* XXX: generate inc and dec for smaller code ? */
1638 orex(ll
, r
, 0, 0x83);
1639 o(0xc0 + REG_VALUE(r
) + opc
*8);
1642 orex(ll
, r
, 0, 0x81);
1643 oad(0xc0 + REG_VALUE(r
) + opc
*8, c
);
1648 orex(ll
, fr
, r
, 0x03 + opc
*8);
1650 gen_modrm(r
, fr
, vtop
->sym
, fc
);
1652 o(0xc0 + REG_VALUE(fr
) + REG_VALUE(r
)*8);
1655 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1661 case TOK_SUBC1
: /* sub with carry generation */
1664 case TOK_ADDC2
: /* add with carry use */
1667 case TOK_SUBC2
: /* sub with carry use */
1689 orex(ll
, fr
, r
, 0xf7);
1691 gen_modrm(opc
, fr
, vtop
->sym
, fc
);
1693 o(0xc0 + REG_VALUE(fr
) + opc
*8);
1695 orex(ll
, fr
, r
, 0xaf0f); /* imul fr, r */
1697 gen_modrm(r
, fr
, vtop
->sym
, fc
);
1699 o(0xc0 + REG_VALUE(fr
) + REG_VALUE(r
)*8);
1719 orex(ll
, r
, 0, 0xd1);
1720 o(0xc0 + REG_VALUE(r
) + opc
*8);
1722 orex(ll
, r
, 0, 0xc1); /* shl/shr/sar $xxx, r */
1723 o(0xc0 + REG_VALUE(r
) + opc
*8);
1724 g(c
& (ll
? 0x3f : 0x1f));
1727 /* we generate the shift in ecx */
1728 gv2(RC_INT
, RC_RCX
);
1730 orex(ll
, r
, 0, 0xd3); /* shl/shr/sar %cl, r */
1731 o(0xc0 + REG_VALUE(r
) + opc
*8);
1746 /* first operand must be in eax */
1747 /* XXX: need better constraint for second operand */
1749 gv2(RC_RAX
, RC_INT2
);
1757 orex(ll
, 0, 0, uu
? 0xd231 : 0x99); /* xor %edx,%edx : cdq RDX:RAX <- sign-extend of RAX. */
1758 orex(ll
, fr
, 0, 0xf7); /* div fr, %eax */
1760 gen_modrm(opc
, fr
, vtop
->sym
, fc
);
1762 o(0xc0 + REG_VALUE(fr
) + opc
*8);
1763 if (op
== '%' || op
== TOK_UMOD
)
1776 void gen_opl(int op
)
1781 /* generate a floating point operation 'v = t1 op t2' instruction. The
1782 two operands are guaranted to have the same floating point type */
1783 /* XXX: need to use ST1 too */
1784 void gen_opf(int op
)
1786 int a
, ft
, fc
, swapped
, fr
, r
;
1787 int float_type
= (vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
? RC_ST0
: RC_FLOAT
;
1789 /* convert constants to memory references */
1790 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
1795 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
1802 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
1803 /* swap the stack if needed so that t1 is the register and t2 is
1804 the memory reference */
1805 /* must put at least one value in the floating point register */
1806 if ((vtop
[-1].r
& VT_LVAL
) && (vtop
[0].r
& VT_LVAL
)) {
1811 if (vtop
[-1].r
& VT_LVAL
) {
1815 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1816 /* load on stack second operand */
1817 load(TREG_ST0
, vtop
);
1818 save_reg(TREG_RAX
); /* eax is used by FP comparison code */
1819 if (op
== TOK_GE
|| op
== TOK_GT
)
1821 else if (op
== TOK_EQ
|| op
== TOK_NE
)
1824 o(0xc9d9); /* fxch %st(1) */
1825 if (op
== TOK_EQ
|| op
== TOK_NE
)
1826 o(0xe9da); /* fucompp */
1828 o(0xd9de); /* fcompp */
1829 o(0xe0df); /* fnstsw %ax */
1831 o(0x45e480); /* and $0x45, %ah */
1832 o(0x40fC80); /* cmp $0x40, %ah */
1833 } else if (op
== TOK_NE
) {
1834 o(0x45e480); /* and $0x45, %ah */
1835 o(0x40f480); /* xor $0x40, %ah */
1837 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
1838 o(0x05c4f6); /* test $0x05, %ah */
1841 o(0x45c4f6); /* test $0x45, %ah */
1848 /* no memory reference possible for long double operations */
1849 load(TREG_ST0
, vtop
);
1870 o(0xde); /* fxxxp %st, %st(1) */
1880 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
1883 op
= TOK_ULE
; /* setae */
1892 op
= TOK_UGT
; /* seta */
1895 assert(!(vtop
[-1].r
& VT_LVAL
));
1896 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
1898 o(0x2e0f); /* ucomisd */
1900 gen_modrm(r
, fr
, vtop
->sym
, fc
);
1902 o(0xc0 + REG_VALUE(fr
) + REG_VALUE(r
)*8);
1905 vtop
->c
.i
= op
| 0x100;
1907 assert((vtop
->type
.t
& VT_BTYPE
) != VT_LDOUBLE
);
1908 /* no memory reference possible for long double operations */
1924 assert((ft
& VT_BTYPE
) != VT_LDOUBLE
);
1925 assert(!(vtop
[-1].r
& VT_LVAL
));
1926 if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
1934 gen_modrm(r
, fr
, vtop
->sym
, fc
);
1936 o(0xc0 + REG_VALUE(fr
) + REG_VALUE(r
)*8);
1942 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1943 and 'long long' cases. */
1944 void gen_cvt_itof(int t
)
1953 if (tbt
== VT_LDOUBLE
) {
1955 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
1956 /* signed long long to float/double/long double (unsigned case
1957 is handled generically) */
1958 o(0x50 + REG_VALUE(r
)); /* push r */
1959 o(0x242cdf); /* fildll (%rsp) */
1960 o(0x08c48348); /* add $8, %rsp */
1961 } else if ((ft
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
)) {
1962 /* unsigned int to float/double/long double */
1963 o(0x6a); /* push $0 */
1965 o(0x50 + REG_VALUE(r
)); /* push r */
1966 o(0x242cdf); /* fildll (%rsp) */
1967 o(0x10c48348); /* add $16, %rsp */
1969 /* int to float/double/long double */
1970 o(0x50 + REG_VALUE(r
)); /* push r */
1971 o(0x2404db); /* fildl (%rsp) */
1972 o(0x08c48348); /* add $8, %rsp */
1977 r_xmm
= get_reg(RC_FLOAT
);
1978 o(0xf2 + (tbt
== VT_FLOAT
));
1979 if ((ft
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) || bt
== VT_LLONG
) {
1983 o(0xc0 + REG_VALUE(r
) + REG_VALUE(r_xmm
)*8); /* cvtsi2sd or cvtsi2ss */
1988 /* convert from one floating point type to another */
1989 void gen_cvt_ftof(int t
)
1997 if(bt
== VT_LDOUBLE
)
1998 r
= get_reg(RC_FLOAT
);
2001 if (bt
== VT_FLOAT
) {
2002 if (tbt
== VT_DOUBLE
) {
2003 o(0x5a0f); /* cvtps2pd */
2004 o(0xc0 + REG_VALUE(r
) + REG_VALUE(r
) * 8);
2005 } else if (tbt
== VT_LDOUBLE
) {
2006 /* movss %xmm0-7,-0x10(%rsp) */
2008 o(0xf02444 + REG_VALUE(r
)*8);
2009 o(0xf02444d9); /* flds -0x10(%rsp) */
2012 } else if (bt
== VT_DOUBLE
) {
2013 if (tbt
== VT_FLOAT
) {
2014 o(0x5a0f66); /* cvtpd2ps */
2015 o(0xc0 + REG_VALUE(r
) + REG_VALUE(r
) * 8);
2016 } else if (tbt
== VT_LDOUBLE
) {
2017 /* movsd %xmm0-7,-0x10(%rsp) */
2019 o(0xf02444 + REG_VALUE(r
)*8);
2020 o(0xf02444dd); /* fldl -0x10(%rsp) */
2025 if (tbt
== VT_DOUBLE
) {
2026 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
2027 /* movsd -0x10(%rsp),%xmm0-7 */
2029 o(0xf02444 + REG_VALUE(r
)*8);
2031 } else if (tbt
== VT_FLOAT
) {
2032 o(0xf0245cd9); /* fstps -0x10(%rsp) */
2033 /* movss -0x10(%rsp),%xmm0-7 */
2035 o(0xf02444 + REG_VALUE(r
)*8);
2041 /* convert fp to int 't' type */
2042 void gen_cvt_ftoi(int t
)
2044 int ft
, bt
, ll
, r
, r_xmm
;
2049 if (bt
== VT_LDOUBLE
) {
2050 gen_cvt_ftof(VT_DOUBLE
);
2053 r_xmm
= gv(RC_FLOAT
);
2054 if ((t
& VT_BTYPE
) == VT_INT
)
2058 r
= get_reg(RC_INT
);
2059 if (bt
== VT_FLOAT
) {
2061 } else if (bt
== VT_DOUBLE
) {
2066 orex(ll
, r
, r_xmm
, 0x2c0f); /* cvttss2si or cvttsd2si */
2067 o(0xc0 + REG_VALUE(r_xmm
) + (REG_VALUE(r
) << 3));
2071 /* computed goto support */
2078 /* Save the stack pointer onto the stack and return the location of its address */
2079 ST_FUNC
void gen_vla_sp_save(int addr
) {
2080 /* mov %rsp,addr(%rbp)*/
2081 gen_modrm64(0x89, TREG_RSP
, VT_LOCAL
, NULL
, addr
);
2084 /* Restore the SP from a location on the stack */
2085 ST_FUNC
void gen_vla_sp_restore(int addr
) {
2086 gen_modrm64(0x8b, TREG_RSP
, VT_LOCAL
, NULL
, addr
);
2089 /* Subtract from the stack pointer, and push the resulting value onto the stack */
2090 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
) {
2091 #ifdef TCC_TARGET_PE
2092 /* alloca does more than just adjust %rsp on Windows */
2093 vpush_global_sym(&func_old_type
, TOK_alloca
);
2094 vswap(); /* Move alloca ref past allocation size */
2096 vset(type
, REG_IRET
, 0);
2099 r
= gv(RC_INT
); /* allocation size */
2102 o(0xe0 | REG_VALUE(r
));
2103 /* We align to 16 bytes rather than align */
2108 o(0xe0 | REG_VALUE(r
));
2115 /* end of x86-64 code generator */
2116 /*************************************************************/
2117 #endif /* ! TARGET_DEFS_ONLY */
2118 /******************************************************/