2 * X86 code generator for TCC
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifdef TARGET_DEFS_ONLY
23 /* number of available registers */
27 /* a register can belong to several classes. The classes must be
28 sorted from more general to more precise (see gv2() code which does
29 assumptions on it). */
30 #define RC_INT 0x0001 /* generic integer register */
31 #define RC_FLOAT 0x0002 /* generic float register */
38 #define RC_IRET RC_EAX /* function return: integer register */
39 #define RC_LRET RC_EDX /* function return: second integer register */
40 #define RC_FRET RC_ST0 /* function return: float register */
42 /* pretty names for the registers */
52 /* return registers for function */
53 #define REG_IRET TREG_EAX /* single word int return register */
54 #define REG_LRET TREG_EDX /* second word return register (for long long) */
55 #define REG_FRET TREG_ST0 /* float return register */
57 /* defined if function parameters must be evaluated in reverse order */
58 #define INVERT_FUNC_PARAMS
60 /* defined if structures are passed as pointers. Otherwise structures
61 are directly pushed on stack. */
62 /* #define FUNC_STRUCT_PARAM_AS_PTR */
64 /* pointer size, in bytes */
67 /* long double size and alignment, in bytes */
68 #define LDOUBLE_SIZE 12
69 #define LDOUBLE_ALIGN 4
70 /* maximum alignment (for aligned attribute support) */
73 /* generate jmp to a label */
74 #define gjmp2(instr,lbl) oad(instr,lbl)
76 /******************************************************/
77 #else /* ! TARGET_DEFS_ONLY */
78 /******************************************************/
81 /* define to 1/0 to [not] have EBX as 4th register */
84 ST_DATA
const int reg_classes
[NB_REGS
] = {
85 /* eax */ RC_INT
| RC_EAX
,
86 /* ecx */ RC_INT
| RC_ECX
,
87 /* edx */ RC_INT
| RC_EDX
,
88 /* ebx */ (RC_INT
| RC_EBX
) * USE_EBX
,
89 /* st0 */ RC_FLOAT
| RC_ST0
,
92 static unsigned long func_sub_sp_offset
;
93 static int func_ret_sub
;
94 #ifdef CONFIG_TCC_BCHECK
95 static addr_t func_bound_offset
;
96 static unsigned long func_bound_ind
;
99 /* XXX: make it faster ? */
100 ST_FUNC
void g(int c
)
106 if (ind1
> cur_text_section
->data_allocated
)
107 section_realloc(cur_text_section
, ind1
);
108 cur_text_section
->data
[ind
] = c
;
112 ST_FUNC
void o(unsigned int c
)
120 ST_FUNC
void gen_le16(int v
)
126 ST_FUNC
void gen_le32(int c
)
134 /* output a symbol and patch all calls to it */
135 ST_FUNC
void gsym_addr(int t
, int a
)
138 unsigned char *ptr
= cur_text_section
->data
+ t
;
139 uint32_t n
= read32le(ptr
); /* next value */
140 write32le(ptr
, a
- t
- 4);
145 ST_FUNC
void gsym(int t
)
150 /* instruction + 4 bytes data. Return the address of the data */
151 ST_FUNC
int oad(int c
, int s
)
162 /* output constant with relocation if 'r & VT_SYM' is true */
163 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, long c
)
166 greloc(cur_text_section
, sym
, ind
, R_386_32
);
170 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, long c
)
173 greloc(cur_text_section
, sym
, ind
, R_386_PC32
);
177 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
179 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
181 op_reg
= op_reg
<< 3;
182 if ((r
& VT_VALMASK
) == VT_CONST
) {
183 /* constant memory reference */
185 gen_addr32(r
, sym
, c
);
186 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
187 /* currently, we use only ebp as base */
189 /* short reference */
193 oad(0x85 | op_reg
, c
);
196 g(0x00 | op_reg
| (r
& VT_VALMASK
));
200 /* load 'r' from value 'sv' */
201 ST_FUNC
void load(int r
, SValue
*sv
)
203 int v
, t
, ft
, fc
, fr
;
208 sv
= pe_getimport(sv
, &v2
);
215 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
219 if (v
== VT_LLOCAL
) {
221 v1
.r
= VT_LOCAL
| VT_LVAL
;
224 if (!(reg_classes
[fr
] & RC_INT
))
225 fr
= get_reg(RC_INT
);
228 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
231 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
234 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
237 } else if ((ft
& VT_TYPE
) == VT_BYTE
|| (ft
& VT_TYPE
) == VT_BOOL
) {
238 o(0xbe0f); /* movsbl */
239 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
240 o(0xb60f); /* movzbl */
241 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
242 o(0xbf0f); /* movswl */
243 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
244 o(0xb70f); /* movzwl */
248 gen_modrm(r
, fr
, sv
->sym
, fc
);
251 o(0xb8 + r
); /* mov $xx, r */
252 gen_addr32(fr
, sv
->sym
, fc
);
253 } else if (v
== VT_LOCAL
) {
255 o(0x8d); /* lea xxx(%ebp), r */
256 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
259 o(0xe8 + r
); /* mov %ebp, r */
261 } else if (v
== VT_CMP
) {
262 oad(0xb8 + r
, 0); /* mov $0, r */
263 o(0x0f); /* setxx %br */
266 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
268 oad(0xb8 + r
, t
); /* mov $1, r */
269 o(0x05eb); /* jmp after */
271 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
274 o(0xc0 + r
+ v
* 8); /* mov v, r */
279 /* store register 'r' in lvalue 'v' */
280 ST_FUNC
void store(int r
, SValue
*v
)
286 v
= pe_getimport(v
, &v2
);
291 fr
= v
->r
& VT_VALMASK
;
292 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
294 /* XXX: incorrect if float reg to reg */
295 if (bt
== VT_FLOAT
) {
298 } else if (bt
== VT_DOUBLE
) {
301 } else if (bt
== VT_LDOUBLE
) {
302 o(0xc0d9); /* fld %st(0) */
308 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
313 if (fr
== VT_CONST
||
316 gen_modrm(r
, v
->r
, v
->sym
, fc
);
317 } else if (fr
!= r
) {
318 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
322 static void gadd_sp(int val
)
324 if (val
== (char)val
) {
328 oad(0xc481, val
); /* add $xxx, %esp */
332 static void gen_static_call(int v
)
336 sym
= external_global_sym(v
, &func_old_type
, 0);
338 greloc(cur_text_section
, sym
, ind
-4, R_386_PC32
);
341 /* 'is_jmp' is '1' if it is a jump */
342 static void gcall_or_jmp(int is_jmp
)
345 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
348 if (vtop
->r
& VT_SYM
) {
349 /* relocation case */
350 greloc(cur_text_section
, vtop
->sym
,
351 ind
+ 1, R_386_PC32
);
353 /* put an empty PC32 relocation */
354 put_elf_reloc(symtab_section
, cur_text_section
,
355 ind
+ 1, R_386_PC32
, 0);
357 oad(0xe8 + is_jmp
, vtop
->c
.i
- 4); /* call/jmp im */
358 /* extend the return value to the whole register if necessary
359 visual studio and gcc do not always set the whole eax register
360 when assigning the return value of a function */
361 rt
= vtop
->type
.ref
->type
.t
;
362 switch (rt
& VT_BTYPE
) {
364 if (rt
& VT_UNSIGNED
) {
365 o(0xc0b60f); /* movzx %al, %eax */
368 o(0xc0be0f); /* movsx %al, %eax */
372 if (rt
& VT_UNSIGNED
) {
373 o(0xc0b70f); /* movzx %ax, %eax */
376 o(0xc0bf0f); /* movsx %ax, %eax */
383 /* otherwise, indirect call */
385 o(0xff); /* call/jmp *r */
386 o(0xd0 + r
+ (is_jmp
<< 4));
390 static uint8_t fastcall_regs
[3] = { TREG_EAX
, TREG_EDX
, TREG_ECX
};
391 static uint8_t fastcallw_regs
[2] = { TREG_ECX
, TREG_EDX
};
393 /* Return the number of registers needed to return the struct, or 0 if
394 returning via struct pointer. */
395 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *ret_align
, int *regsize
)
399 *ret_align
= 1; // Never have to re-align return values for x86
401 size
= type_size(vt
, &align
);
402 if (size
> 8 || (size
& (size
- 1)))
415 *ret_align
= 1; // Never have to re-align return values for x86
420 /* Generate function call. The function address is pushed first, then
421 all the parameters in call order. This functions pops all the
422 parameters and the function address. */
423 ST_FUNC
void gfunc_call(int nb_args
)
425 int size
, align
, r
, args_size
, i
, func_call
;
429 for(i
= 0;i
< nb_args
; i
++) {
430 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
431 size
= type_size(&vtop
->type
, &align
);
432 /* align to stack align size */
433 size
= (size
+ 3) & ~3;
434 /* allocate the necessary size on stack */
435 oad(0xec81, size
); /* sub $xxx, %esp */
436 /* generate structure store */
438 o(0x89); /* mov %esp, r */
440 vset(&vtop
->type
, r
| VT_LVAL
, 0);
444 } else if (is_float(vtop
->type
.t
)) {
445 gv(RC_FLOAT
); /* only one float register */
446 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
448 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
452 oad(0xec81, size
); /* sub $xxx, %esp */
456 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
461 /* simple type (currently always same size) */
462 /* XXX: implicit cast ? */
464 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
466 o(0x50 + vtop
->r2
); /* push r */
470 o(0x50 + r
); /* push r */
475 save_regs(0); /* save used temporary registers */
476 func_sym
= vtop
->type
.ref
;
477 func_call
= func_sym
->a
.func_call
;
479 if ((func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) ||
480 func_call
== FUNC_FASTCALLW
) {
481 int fastcall_nb_regs
;
482 uint8_t *fastcall_regs_ptr
;
483 if (func_call
== FUNC_FASTCALLW
) {
484 fastcall_regs_ptr
= fastcallw_regs
;
485 fastcall_nb_regs
= 2;
487 fastcall_regs_ptr
= fastcall_regs
;
488 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
490 for(i
= 0;i
< fastcall_nb_regs
; i
++) {
493 o(0x58 + fastcall_regs_ptr
[i
]); /* pop r */
494 /* XXX: incorrect for struct/floats */
498 #ifndef TCC_TARGET_PE
499 else if ((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
)
504 if (args_size
&& func_call
!= FUNC_STDCALL
)
510 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
512 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
515 /* generate function prolog of type 't' */
516 ST_FUNC
void gfunc_prolog(CType
*func_type
)
518 int addr
, align
, size
, func_call
, fastcall_nb_regs
;
519 int param_index
, param_addr
;
520 uint8_t *fastcall_regs_ptr
;
524 sym
= func_type
->ref
;
525 func_call
= sym
->a
.func_call
;
530 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
531 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
532 fastcall_regs_ptr
= fastcall_regs
;
533 } else if (func_call
== FUNC_FASTCALLW
) {
534 fastcall_nb_regs
= 2;
535 fastcall_regs_ptr
= fastcallw_regs
;
537 fastcall_nb_regs
= 0;
538 fastcall_regs_ptr
= NULL
;
542 ind
+= FUNC_PROLOG_SIZE
;
543 func_sub_sp_offset
= ind
;
544 /* if the function returns a structure, then add an
545 implicit pointer parameter */
547 func_var
= (sym
->c
== FUNC_ELLIPSIS
);
549 size
= type_size(&func_vt
,&align
);
550 if (((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
)
551 && (size
> 8 || (size
& (size
- 1)))) {
553 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
555 /* XXX: fastcall case ? */
560 /* define parameters */
561 while ((sym
= sym
->next
) != NULL
) {
563 size
= type_size(type
, &align
);
564 size
= (size
+ 3) & ~3;
565 #ifdef FUNC_STRUCT_PARAM_AS_PTR
566 /* structs are passed as pointer */
567 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
571 if (param_index
< fastcall_nb_regs
) {
572 /* save FASTCALL register */
575 gen_modrm(fastcall_regs_ptr
[param_index
], VT_LOCAL
, NULL
, loc
);
581 sym_push(sym
->v
& ~SYM_FIELD
, type
,
582 VT_LOCAL
| lvalue_type(type
->t
), param_addr
);
586 /* pascal type call ? */
587 if (func_call
== FUNC_STDCALL
)
588 func_ret_sub
= addr
- 8;
589 #ifndef TCC_TARGET_PE
594 #ifdef CONFIG_TCC_BCHECK
595 /* leave some room for bound checking code */
596 if (tcc_state
->do_bounds_check
) {
597 func_bound_offset
= lbounds_section
->data_offset
;
598 func_bound_ind
= ind
;
599 oad(0xb8, 0); /* lbound section pointer */
600 oad(0xb8, 0); /* call to function */
605 /* generate function epilog */
606 ST_FUNC
void gfunc_epilog(void)
610 #ifdef CONFIG_TCC_BCHECK
611 if (tcc_state
->do_bounds_check
612 && func_bound_offset
!= lbounds_section
->data_offset
) {
617 /* add end of table info */
618 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(addr_t
));
621 /* generate bound local allocation */
623 ind
= func_bound_ind
;
624 sym_data
= get_sym_ref(&char_pointer_type
, lbounds_section
,
625 func_bound_offset
, lbounds_section
->data_offset
);
626 greloc(cur_text_section
, sym_data
,
628 oad(0xb8, 0); /* mov %eax, xxx */
629 gen_static_call(TOK___bound_local_new
);
632 /* generate bound check local freeing */
633 o(0x5250); /* save returned value, if any */
634 greloc(cur_text_section
, sym_data
, ind
+ 1, R_386_32
);
635 oad(0xb8, 0); /* mov %eax, xxx */
636 gen_static_call(TOK___bound_local_delete
);
637 o(0x585a); /* restore returned value, if any */
641 /* align local size to word & save local variables */
646 gen_modrm(TREG_EBX
, VT_LOCAL
, NULL
, -(v
+4));
650 if (func_ret_sub
== 0) {
655 g(func_ret_sub
>> 8);
658 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
661 oad(0xb8, v
); /* mov stacksize, %eax */
662 gen_static_call(TOK___chkstk
); /* call __chkstk, (does the stackframe too) */
666 o(0xe58955); /* push %ebp, mov %esp, %ebp */
667 o(0xec81); /* sub esp, stacksize */
670 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
673 o(0x53 * USE_EBX
); /* push ebx */
677 /* generate a jump to a label */
678 ST_FUNC
int gjmp(int t
)
680 return gjmp2(0xe9, t
);
683 /* generate a jump to a fixed address */
684 ST_FUNC
void gjmp_addr(int a
)
692 oad(0xe9, a
- ind
- 5);
696 ST_FUNC
void gtst_addr(int inv
, int a
)
698 int v
= vtop
->r
& VT_VALMASK
;
700 inv
^= (vtop
--)->c
.i
;
707 oad(inv
- 16, a
- 4);
709 } else if ((v
& ~1) == VT_JMP
) {
710 if ((v
& 1) != inv
) {
722 /* generate a test. set 'inv' to invert test. Stack entry is popped */
723 ST_FUNC
int gtst(int inv
, int t
)
725 int v
= vtop
->r
& VT_VALMASK
;
728 } else if (v
== VT_CMP
) {
729 /* fast case : can jump directly since flags are set */
731 t
= gjmp2((vtop
->c
.i
- 16) ^ inv
, t
);
732 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
733 /* && or || optimization */
734 if ((v
& 1) == inv
) {
735 /* insert vtop->c jump list in t */
736 uint32_t n1
, n
= vtop
->c
.i
;
738 while ((n1
= read32le(cur_text_section
->data
+ n
)))
740 write32le(cur_text_section
->data
+ n
, t
);
752 /* generate an integer binary operation */
753 ST_FUNC
void gen_opi(int op
)
759 case TOK_ADDC1
: /* add with carry generation */
762 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
769 /* generate inc and dec for smaller code */
770 if (c
==1 && opc
==0 && op
!= TOK_ADDC1
) {
772 } else if (c
==1 && opc
==5 && op
!= TOK_SUBC1
) {
776 o(0xc0 | (opc
<< 3) | r
);
781 oad(0xc0 | (opc
<< 3) | r
, c
);
787 o((opc
<< 3) | 0x01);
788 o(0xc0 + r
+ fr
* 8);
791 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
797 case TOK_SUBC1
: /* sub with carry generation */
800 case TOK_ADDC2
: /* add with carry use */
803 case TOK_SUBC2
: /* sub with carry use */
820 o(0xaf0f); /* imul fr, r */
821 o(0xc0 + fr
+ r
* 8);
832 opc
= 0xc0 | (opc
<< 3);
833 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
838 c
= vtop
->c
.i
& 0x1f;
839 o(0xc1); /* shl/shr/sar $xxx, r */
843 /* we generate the shift in ecx */
846 o(0xd3); /* shl/shr/sar %cl, r */
857 /* first operand must be in eax */
858 /* XXX: need better constraint for second operand */
864 /* save EAX too if used otherwise */
865 save_reg_upstack(TREG_EAX
, 1);
866 if (op
== TOK_UMULL
) {
867 o(0xf7); /* mul fr */
872 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
873 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
876 o(0xf799); /* cltd, idiv fr, %eax */
879 if (op
== '%' || op
== TOK_UMOD
)
892 /* generate a floating point operation 'v = t1 op t2' instruction. The
893 two operands are guaranted to have the same floating point type */
894 /* XXX: need to use ST1 too */
895 ST_FUNC
void gen_opf(int op
)
897 int a
, ft
, fc
, swapped
, r
;
899 /* convert constants to memory references */
900 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
905 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
908 /* must put at least one value in the floating point register */
909 if ((vtop
[-1].r
& VT_LVAL
) &&
910 (vtop
[0].r
& VT_LVAL
)) {
916 /* swap the stack if needed so that t1 is the register and t2 is
917 the memory reference */
918 if (vtop
[-1].r
& VT_LVAL
) {
922 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
923 /* load on stack second operand */
924 load(TREG_ST0
, vtop
);
925 save_reg(TREG_EAX
); /* eax is used by FP comparison code */
926 if (op
== TOK_GE
|| op
== TOK_GT
)
928 else if (op
== TOK_EQ
|| op
== TOK_NE
)
931 o(0xc9d9); /* fxch %st(1) */
932 if (op
== TOK_EQ
|| op
== TOK_NE
)
933 o(0xe9da); /* fucompp */
935 o(0xd9de); /* fcompp */
936 o(0xe0df); /* fnstsw %ax */
938 o(0x45e480); /* and $0x45, %ah */
939 o(0x40fC80); /* cmp $0x40, %ah */
940 } else if (op
== TOK_NE
) {
941 o(0x45e480); /* and $0x45, %ah */
942 o(0x40f480); /* xor $0x40, %ah */
944 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
945 o(0x05c4f6); /* test $0x05, %ah */
948 o(0x45c4f6); /* test $0x45, %ah */
955 /* no memory reference possible for long double operations */
956 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
957 load(TREG_ST0
, vtop
);
982 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
983 o(0xde); /* fxxxp %st, %st(1) */
986 /* if saved lvalue, then we must reload it */
988 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
992 v1
.r
= VT_LOCAL
| VT_LVAL
;
998 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
1002 gen_modrm(a
, r
, vtop
->sym
, fc
);
1008 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1009 and 'long long' cases. */
1010 ST_FUNC
void gen_cvt_itof(int t
)
1014 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1015 /* signed long long to float/double/long double (unsigned case
1016 is handled generically) */
1017 o(0x50 + vtop
->r2
); /* push r2 */
1018 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1019 o(0x242cdf); /* fildll (%esp) */
1020 o(0x08c483); /* add $8, %esp */
1021 } else if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1022 (VT_INT
| VT_UNSIGNED
)) {
1023 /* unsigned int to float/double/long double */
1024 o(0x6a); /* push $0 */
1026 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1027 o(0x242cdf); /* fildll (%esp) */
1028 o(0x08c483); /* add $8, %esp */
1030 /* int to float/double/long double */
1031 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1032 o(0x2404db); /* fildl (%esp) */
1033 o(0x04c483); /* add $4, %esp */
1038 /* convert fp to int 't' type */
1039 ST_FUNC
void gen_cvt_ftoi(int t
)
1041 int bt
= vtop
->type
.t
& VT_BTYPE
;
1043 vpush_global_sym(&func_old_type
, TOK___fixsfdi
);
1044 else if (bt
== VT_LDOUBLE
)
1045 vpush_global_sym(&func_old_type
, TOK___fixxfdi
);
1047 vpush_global_sym(&func_old_type
, TOK___fixdfdi
);
1052 vtop
->r2
= REG_LRET
;
1055 /* convert from one floating point type to another */
1056 ST_FUNC
void gen_cvt_ftof(int t
)
1058 /* all we have to do on i386 is to put the float in a register */
1062 /* computed goto support */
1063 ST_FUNC
void ggoto(void)
1069 /* bound check support functions */
1070 #ifdef CONFIG_TCC_BCHECK
1072 /* generate a bounded pointer addition */
1073 ST_FUNC
void gen_bounded_ptr_add(void)
1075 /* prepare fast i386 function call (args in eax and edx) */
1076 gv2(RC_EAX
, RC_EDX
);
1077 /* save all temporary registers */
1080 /* do a fast function call */
1081 gen_static_call(TOK___bound_ptr_add
);
1082 /* returned pointer is in eax */
1084 vtop
->r
= TREG_EAX
| VT_BOUNDED
;
1085 /* address of bounding function call point */
1086 vtop
->c
.i
= (cur_text_section
->reloc
->data_offset
- sizeof(Elf32_Rel
));
1089 /* patch pointer addition in vtop so that pointer dereferencing is
1091 ST_FUNC
void gen_bounded_ptr_deref(void)
1099 /* XXX: put that code in generic part of tcc */
1100 if (!is_float(vtop
->type
.t
)) {
1101 if (vtop
->r
& VT_LVAL_BYTE
)
1103 else if (vtop
->r
& VT_LVAL_SHORT
)
1107 size
= type_size(&vtop
->type
, &align
);
1109 case 1: func
= TOK___bound_ptr_indir1
; break;
1110 case 2: func
= TOK___bound_ptr_indir2
; break;
1111 case 4: func
= TOK___bound_ptr_indir4
; break;
1112 case 8: func
= TOK___bound_ptr_indir8
; break;
1113 case 12: func
= TOK___bound_ptr_indir12
; break;
1114 case 16: func
= TOK___bound_ptr_indir16
; break;
1116 tcc_error("unhandled size when dereferencing bounded pointer");
1121 /* patch relocation */
1122 /* XXX: find a better solution ? */
1123 rel
= (Elf32_Rel
*)(cur_text_section
->reloc
->data
+ vtop
->c
.i
);
1124 sym
= external_global_sym(func
, &func_old_type
, 0);
1126 put_extern_sym(sym
, NULL
, 0, 0);
1127 rel
->r_info
= ELF32_R_INFO(sym
->c
, ELF32_R_TYPE(rel
->r_info
));
1131 /* Save the stack pointer onto the stack */
1132 ST_FUNC
void gen_vla_sp_save(int addr
) {
1133 /* mov %esp,addr(%ebp)*/
1135 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1138 /* Restore the SP from a location on the stack */
1139 ST_FUNC
void gen_vla_sp_restore(int addr
) {
1141 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1144 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1145 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
) {
1146 #ifdef TCC_TARGET_PE
1147 /* alloca does more than just adjust %rsp on Windows */
1148 vpush_global_sym(&func_old_type
, TOK_alloca
);
1149 vswap(); /* Move alloca ref past allocation size */
1153 r
= gv(RC_INT
); /* allocation size */
1157 /* We align to 16 bytes rather than align */
1164 /* end of X86 code generator */
1165 /*************************************************************/
1167 /*************************************************************/