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) */
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
)
104 if (ind1
> cur_text_section
->data_allocated
)
105 section_realloc(cur_text_section
, ind1
);
106 cur_text_section
->data
[ind
] = c
;
110 ST_FUNC
void o(unsigned int c
)
118 ST_FUNC
void gen_le16(int v
)
124 ST_FUNC
void gen_le32(int c
)
132 /* output a symbol and patch all calls to it */
133 ST_FUNC
void gsym_addr(int t
, int a
)
136 unsigned char *ptr
= cur_text_section
->data
+ t
;
137 uint32_t n
= read32le(ptr
); /* next value */
138 write32le(ptr
, a
- t
- 4);
143 ST_FUNC
void gsym(int t
)
148 /* psym is used to put an instruction with a data field which is a
149 reference to a symbol. It is in fact the same as oad ! */
152 /* instruction + 4 bytes data. Return the address of the data */
153 ST_FUNC
int oad(int c
, int s
)
159 if (ind1
> cur_text_section
->data_allocated
)
160 section_realloc(cur_text_section
, ind1
);
161 write32le(cur_text_section
->data
+ ind
, s
);
167 /* output constant with relocation if 'r & VT_SYM' is true */
168 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
)
171 greloc(cur_text_section
, sym
, ind
, R_386_32
);
175 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
)
178 greloc(cur_text_section
, sym
, ind
, R_386_PC32
);
182 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
184 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
186 op_reg
= op_reg
<< 3;
187 if ((r
& VT_VALMASK
) == VT_CONST
) {
188 /* constant memory reference */
190 gen_addr32(r
, sym
, c
);
191 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
192 /* currently, we use only ebp as base */
194 /* short reference */
198 oad(0x85 | op_reg
, c
);
201 g(0x00 | op_reg
| (r
& VT_VALMASK
));
205 /* load 'r' from value 'sv' */
206 ST_FUNC
void load(int r
, SValue
*sv
)
208 int v
, t
, ft
, fc
, fr
;
213 sv
= pe_getimport(sv
, &v2
);
220 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
224 if (v
== VT_LLOCAL
) {
226 v1
.r
= VT_LOCAL
| VT_LVAL
;
229 if (!(reg_classes
[fr
] & RC_INT
))
230 fr
= get_reg(RC_INT
);
233 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
236 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
239 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
242 } else if ((ft
& VT_TYPE
) == VT_BYTE
|| (ft
& VT_TYPE
) == VT_BOOL
) {
243 o(0xbe0f); /* movsbl */
244 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
245 o(0xb60f); /* movzbl */
246 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
247 o(0xbf0f); /* movswl */
248 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
249 o(0xb70f); /* movzwl */
253 gen_modrm(r
, fr
, sv
->sym
, fc
);
256 o(0xb8 + r
); /* mov $xx, r */
257 gen_addr32(fr
, sv
->sym
, fc
);
258 } else if (v
== VT_LOCAL
) {
260 o(0x8d); /* lea xxx(%ebp), r */
261 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
264 o(0xe8 + r
); /* mov %ebp, r */
266 } else if (v
== VT_CMP
) {
267 oad(0xb8 + r
, 0); /* mov $0, r */
268 o(0x0f); /* setxx %br */
271 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
273 oad(0xb8 + r
, t
); /* mov $1, r */
274 o(0x05eb); /* jmp after */
276 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
279 o(0xc0 + r
+ v
* 8); /* mov v, r */
284 /* store register 'r' in lvalue 'v' */
285 ST_FUNC
void store(int r
, SValue
*v
)
291 v
= pe_getimport(v
, &v2
);
296 fr
= v
->r
& VT_VALMASK
;
297 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
299 /* XXX: incorrect if float reg to reg */
300 if (bt
== VT_FLOAT
) {
303 } else if (bt
== VT_DOUBLE
) {
306 } else if (bt
== VT_LDOUBLE
) {
307 o(0xc0d9); /* fld %st(0) */
313 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
318 if (fr
== VT_CONST
||
321 gen_modrm(r
, v
->r
, v
->sym
, fc
);
322 } else if (fr
!= r
) {
323 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
327 static void gadd_sp(int val
)
329 if (val
== (char)val
) {
333 oad(0xc481, val
); /* add $xxx, %esp */
337 static void gen_static_call(int v
)
341 sym
= external_global_sym(v
, &func_old_type
, 0);
343 greloc(cur_text_section
, sym
, ind
-4, R_386_PC32
);
346 /* 'is_jmp' is '1' if it is a jump */
347 static void gcall_or_jmp(int is_jmp
)
350 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
353 if (vtop
->r
& VT_SYM
) {
354 /* relocation case */
355 greloc(cur_text_section
, vtop
->sym
,
356 ind
+ 1, R_386_PC32
);
358 /* put an empty PC32 relocation */
359 put_elf_reloc(symtab_section
, cur_text_section
,
360 ind
+ 1, R_386_PC32
, 0);
362 oad(0xe8 + is_jmp
, vtop
->c
.i
- 4); /* call/jmp im */
363 /* extend the return value to the whole register if necessary
364 visual studio and gcc do not always set the whole eax register
365 when assigning the return value of a function */
366 rt
= vtop
->type
.ref
->type
.t
;
367 switch (rt
& VT_BTYPE
) {
369 if (rt
& VT_UNSIGNED
) {
370 o(0xc0b60f); /* movzx %al, %eax */
373 o(0xc0be0f); /* movsx %al, %eax */
377 if (rt
& VT_UNSIGNED
) {
378 o(0xc0b70f); /* movzx %ax, %eax */
381 o(0xc0bf0f); /* movsx %ax, %eax */
388 /* otherwise, indirect call */
390 o(0xff); /* call/jmp *r */
391 o(0xd0 + r
+ (is_jmp
<< 4));
395 static uint8_t fastcall_regs
[3] = { TREG_EAX
, TREG_EDX
, TREG_ECX
};
396 static uint8_t fastcallw_regs
[2] = { TREG_ECX
, TREG_EDX
};
398 /* Return the number of registers needed to return the struct, or 0 if
399 returning via struct pointer. */
400 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *ret_align
, int *regsize
)
405 *ret_align
= 1; // Never have to re-align return values for x86
407 size
= type_size(vt
, &align
);
410 } else if (size
> 4) {
420 *ret_align
= 1; // Never have to re-align return values for x86
425 /* Generate function call. The function address is pushed first, then
426 all the parameters in call order. This functions pops all the
427 parameters and the function address. */
428 ST_FUNC
void gfunc_call(int nb_args
)
430 int size
, align
, r
, args_size
, i
, func_call
;
434 for(i
= 0;i
< nb_args
; i
++) {
435 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
436 size
= type_size(&vtop
->type
, &align
);
437 /* align to stack align size */
438 size
= (size
+ 3) & ~3;
439 /* allocate the necessary size on stack */
440 oad(0xec81, size
); /* sub $xxx, %esp */
441 /* generate structure store */
443 o(0x89); /* mov %esp, r */
445 vset(&vtop
->type
, r
| VT_LVAL
, 0);
449 } else if (is_float(vtop
->type
.t
)) {
450 gv(RC_FLOAT
); /* only one float register */
451 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
453 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
457 oad(0xec81, size
); /* sub $xxx, %esp */
461 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
466 /* simple type (currently always same size) */
467 /* XXX: implicit cast ? */
469 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
471 o(0x50 + vtop
->r2
); /* push r */
475 o(0x50 + r
); /* push r */
480 save_regs(0); /* save used temporary registers */
481 func_sym
= vtop
->type
.ref
;
482 func_call
= func_sym
->a
.func_call
;
484 if ((func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) ||
485 func_call
== FUNC_FASTCALLW
) {
486 int fastcall_nb_regs
;
487 uint8_t *fastcall_regs_ptr
;
488 if (func_call
== FUNC_FASTCALLW
) {
489 fastcall_regs_ptr
= fastcallw_regs
;
490 fastcall_nb_regs
= 2;
492 fastcall_regs_ptr
= fastcall_regs
;
493 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
495 for(i
= 0;i
< fastcall_nb_regs
; i
++) {
498 o(0x58 + fastcall_regs_ptr
[i
]); /* pop r */
499 /* XXX: incorrect for struct/floats */
503 #ifndef TCC_TARGET_PE
504 else if ((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
)
509 if (args_size
&& func_call
!= FUNC_STDCALL
)
515 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
517 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
520 /* generate function prolog of type 't' */
521 ST_FUNC
void gfunc_prolog(CType
*func_type
)
523 int addr
, align
, size
, func_call
, fastcall_nb_regs
;
524 int param_index
, param_addr
;
525 uint8_t *fastcall_regs_ptr
;
529 sym
= func_type
->ref
;
530 func_call
= sym
->a
.func_call
;
535 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
536 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
537 fastcall_regs_ptr
= fastcall_regs
;
538 } else if (func_call
== FUNC_FASTCALLW
) {
539 fastcall_nb_regs
= 2;
540 fastcall_regs_ptr
= fastcallw_regs
;
542 fastcall_nb_regs
= 0;
543 fastcall_regs_ptr
= NULL
;
547 ind
+= FUNC_PROLOG_SIZE
;
548 func_sub_sp_offset
= ind
;
549 /* if the function returns a structure, then add an
550 implicit pointer parameter */
552 func_var
= (sym
->c
== FUNC_ELLIPSIS
);
554 size
= type_size(&func_vt
,&align
);
555 if (((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) && (size
> 8)) {
557 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
559 /* XXX: fastcall case ? */
564 /* define parameters */
565 while ((sym
= sym
->next
) != NULL
) {
567 size
= type_size(type
, &align
);
568 size
= (size
+ 3) & ~3;
569 #ifdef FUNC_STRUCT_PARAM_AS_PTR
570 /* structs are passed as pointer */
571 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
575 if (param_index
< fastcall_nb_regs
) {
576 /* save FASTCALL register */
579 gen_modrm(fastcall_regs_ptr
[param_index
], VT_LOCAL
, NULL
, loc
);
585 sym_push(sym
->v
& ~SYM_FIELD
, type
,
586 VT_LOCAL
| lvalue_type(type
->t
), param_addr
);
590 /* pascal type call ? */
591 if (func_call
== FUNC_STDCALL
)
592 func_ret_sub
= addr
- 8;
593 #ifndef TCC_TARGET_PE
598 #ifdef CONFIG_TCC_BCHECK
599 /* leave some room for bound checking code */
600 if (tcc_state
->do_bounds_check
) {
601 func_bound_offset
= lbounds_section
->data_offset
;
602 func_bound_ind
= ind
;
603 oad(0xb8, 0); /* lbound section pointer */
604 oad(0xb8, 0); /* call to function */
609 /* generate function epilog */
610 ST_FUNC
void gfunc_epilog(void)
614 #ifdef CONFIG_TCC_BCHECK
615 if (tcc_state
->do_bounds_check
616 && func_bound_offset
!= lbounds_section
->data_offset
) {
621 /* add end of table info */
622 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(addr_t
));
625 /* generate bound local allocation */
627 ind
= func_bound_ind
;
628 sym_data
= get_sym_ref(&char_pointer_type
, lbounds_section
,
629 func_bound_offset
, lbounds_section
->data_offset
);
630 greloc(cur_text_section
, sym_data
,
632 oad(0xb8, 0); /* mov %eax, xxx */
633 gen_static_call(TOK___bound_local_new
);
636 /* generate bound check local freeing */
637 o(0x5250); /* save returned value, if any */
638 greloc(cur_text_section
, sym_data
, ind
+ 1, R_386_32
);
639 oad(0xb8, 0); /* mov %eax, xxx */
640 gen_static_call(TOK___bound_local_delete
);
641 o(0x585a); /* restore returned value, if any */
644 o(0x5b * USE_EBX
); /* pop ebx */
646 if (func_ret_sub
== 0) {
651 g(func_ret_sub
>> 8);
653 /* align local size to word & save local variables */
657 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
660 oad(0xb8, v
); /* mov stacksize, %eax */
661 gen_static_call(TOK___chkstk
); /* call __chkstk, (does the stackframe too) */
665 o(0xe58955); /* push %ebp, mov %esp, %ebp */
666 o(0xec81); /* sub esp, stacksize */
669 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
672 o(0x53 * USE_EBX
); /* push ebx */
676 /* generate a jump to a label */
677 ST_FUNC
int gjmp(int t
)
679 return psym(0xe9, t
);
682 /* generate a jump to a fixed address */
683 ST_FUNC
void gjmp_addr(int a
)
691 oad(0xe9, a
- ind
- 5);
695 ST_FUNC
void gtst_addr(int inv
, int a
)
697 inv
^= (vtop
--)->c
.i
;
704 oad(inv
- 16, a
- 4);
708 /* generate a test. set 'inv' to invert test. Stack entry is popped */
709 ST_FUNC
int gtst(int inv
, int t
)
711 int v
= vtop
->r
& VT_VALMASK
;
713 /* fast case : can jump directly since flags are set */
715 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
716 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
717 /* && or || optimization */
718 if ((v
& 1) == inv
) {
719 /* insert vtop->c jump list in t */
720 uint32_t n1
, n
= vtop
->c
.i
;
722 while ((n1
= read32le(cur_text_section
->data
+ n
)))
724 write32le(cur_text_section
->data
+ n
, t
);
736 /* generate an integer binary operation */
737 ST_FUNC
void gen_opi(int op
)
743 case TOK_ADDC1
: /* add with carry generation */
746 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
753 /* generate inc and dec for smaller code */
754 if (c
==1 && opc
==0 && op
!= TOK_ADDC1
) {
756 } else if (c
==1 && opc
==5 && op
!= TOK_SUBC1
) {
760 o(0xc0 | (opc
<< 3) | r
);
765 oad(0xc0 | (opc
<< 3) | r
, c
);
771 o((opc
<< 3) | 0x01);
772 o(0xc0 + r
+ fr
* 8);
775 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
781 case TOK_SUBC1
: /* sub with carry generation */
784 case TOK_ADDC2
: /* add with carry use */
787 case TOK_SUBC2
: /* sub with carry use */
804 o(0xaf0f); /* imul fr, r */
805 o(0xc0 + fr
+ r
* 8);
816 opc
= 0xc0 | (opc
<< 3);
817 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
822 c
= vtop
->c
.i
& 0x1f;
823 o(0xc1); /* shl/shr/sar $xxx, r */
827 /* we generate the shift in ecx */
830 o(0xd3); /* shl/shr/sar %cl, r */
841 /* first operand must be in eax */
842 /* XXX: need better constraint for second operand */
848 /* save EAX too if used otherwise */
849 save_reg_upstack(TREG_EAX
, 1);
850 if (op
== TOK_UMULL
) {
851 o(0xf7); /* mul fr */
856 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
857 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
860 o(0xf799); /* cltd, idiv fr, %eax */
863 if (op
== '%' || op
== TOK_UMOD
)
876 /* generate a floating point operation 'v = t1 op t2' instruction. The
877 two operands are guaranted to have the same floating point type */
878 /* XXX: need to use ST1 too */
879 ST_FUNC
void gen_opf(int op
)
881 int a
, ft
, fc
, swapped
, r
;
883 /* convert constants to memory references */
884 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
889 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
892 /* must put at least one value in the floating point register */
893 if ((vtop
[-1].r
& VT_LVAL
) &&
894 (vtop
[0].r
& VT_LVAL
)) {
900 /* swap the stack if needed so that t1 is the register and t2 is
901 the memory reference */
902 if (vtop
[-1].r
& VT_LVAL
) {
906 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
907 /* load on stack second operand */
908 load(TREG_ST0
, vtop
);
909 save_reg(TREG_EAX
); /* eax is used by FP comparison code */
910 if (op
== TOK_GE
|| op
== TOK_GT
)
912 else if (op
== TOK_EQ
|| op
== TOK_NE
)
915 o(0xc9d9); /* fxch %st(1) */
916 if (op
== TOK_EQ
|| op
== TOK_NE
)
917 o(0xe9da); /* fucompp */
919 o(0xd9de); /* fcompp */
920 o(0xe0df); /* fnstsw %ax */
922 o(0x45e480); /* and $0x45, %ah */
923 o(0x40fC80); /* cmp $0x40, %ah */
924 } else if (op
== TOK_NE
) {
925 o(0x45e480); /* and $0x45, %ah */
926 o(0x40f480); /* xor $0x40, %ah */
928 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
929 o(0x05c4f6); /* test $0x05, %ah */
932 o(0x45c4f6); /* test $0x45, %ah */
939 /* no memory reference possible for long double operations */
940 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
941 load(TREG_ST0
, vtop
);
966 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
967 o(0xde); /* fxxxp %st, %st(1) */
970 /* if saved lvalue, then we must reload it */
972 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
976 v1
.r
= VT_LOCAL
| VT_LVAL
;
982 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
986 gen_modrm(a
, r
, vtop
->sym
, fc
);
992 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
993 and 'long long' cases. */
994 ST_FUNC
void gen_cvt_itof(int t
)
998 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
999 /* signed long long to float/double/long double (unsigned case
1000 is handled generically) */
1001 o(0x50 + vtop
->r2
); /* push r2 */
1002 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1003 o(0x242cdf); /* fildll (%esp) */
1004 o(0x08c483); /* add $8, %esp */
1005 } else if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1006 (VT_INT
| VT_UNSIGNED
)) {
1007 /* unsigned int to float/double/long double */
1008 o(0x6a); /* push $0 */
1010 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1011 o(0x242cdf); /* fildll (%esp) */
1012 o(0x08c483); /* add $8, %esp */
1014 /* int to float/double/long double */
1015 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1016 o(0x2404db); /* fildl (%esp) */
1017 o(0x04c483); /* add $4, %esp */
1022 /* convert fp to int 't' type */
1023 ST_FUNC
void gen_cvt_ftoi(int t
)
1029 gen_static_call(TOK___tcc_cvt_ftol
);
1030 vtop
->r
= TREG_EAX
; /* mark reg as used */
1032 vtop
->r2
= TREG_EDX
;
1034 int bt
= vtop
->type
.t
& VT_BTYPE
;
1036 vpush_global_sym(&func_old_type
, TOK___fixsfdi
);
1037 else if (bt
== VT_LDOUBLE
)
1038 vpush_global_sym(&func_old_type
, TOK___fixxfdi
);
1040 vpush_global_sym(&func_old_type
, TOK___fixdfdi
);
1045 vtop
->r2
= REG_LRET
;
1049 /* convert from one floating point type to another */
1050 ST_FUNC
void gen_cvt_ftof(int t
)
1052 /* all we have to do on i386 is to put the float in a register */
1056 /* computed goto support */
1057 ST_FUNC
void ggoto(void)
1063 /* bound check support functions */
1064 #ifdef CONFIG_TCC_BCHECK
1066 /* generate a bounded pointer addition */
1067 ST_FUNC
void gen_bounded_ptr_add(void)
1069 /* prepare fast i386 function call (args in eax and edx) */
1070 gv2(RC_EAX
, RC_EDX
);
1071 /* save all temporary registers */
1074 /* do a fast function call */
1075 gen_static_call(TOK___bound_ptr_add
);
1076 /* returned pointer is in eax */
1078 vtop
->r
= TREG_EAX
| VT_BOUNDED
;
1079 /* address of bounding function call point */
1080 vtop
->c
.i
= (cur_text_section
->reloc
->data_offset
- sizeof(Elf32_Rel
));
1083 /* patch pointer addition in vtop so that pointer dereferencing is
1085 ST_FUNC
void gen_bounded_ptr_deref(void)
1093 /* XXX: put that code in generic part of tcc */
1094 if (!is_float(vtop
->type
.t
)) {
1095 if (vtop
->r
& VT_LVAL_BYTE
)
1097 else if (vtop
->r
& VT_LVAL_SHORT
)
1101 size
= type_size(&vtop
->type
, &align
);
1103 case 1: func
= TOK___bound_ptr_indir1
; break;
1104 case 2: func
= TOK___bound_ptr_indir2
; break;
1105 case 4: func
= TOK___bound_ptr_indir4
; break;
1106 case 8: func
= TOK___bound_ptr_indir8
; break;
1107 case 12: func
= TOK___bound_ptr_indir12
; break;
1108 case 16: func
= TOK___bound_ptr_indir16
; break;
1110 tcc_error("unhandled size when dereferencing bounded pointer");
1115 /* patch relocation */
1116 /* XXX: find a better solution ? */
1117 rel
= (Elf32_Rel
*)(cur_text_section
->reloc
->data
+ vtop
->c
.i
);
1118 sym
= external_global_sym(func
, &func_old_type
, 0);
1120 put_extern_sym(sym
, NULL
, 0, 0);
1121 rel
->r_info
= ELF32_R_INFO(sym
->c
, ELF32_R_TYPE(rel
->r_info
));
1125 /* Save the stack pointer onto the stack */
1126 ST_FUNC
void gen_vla_sp_save(int addr
) {
1127 /* mov %esp,addr(%ebp)*/
1129 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1132 /* Restore the SP from a location on the stack */
1133 ST_FUNC
void gen_vla_sp_restore(int addr
) {
1135 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1138 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1139 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
) {
1140 #ifdef TCC_TARGET_PE
1141 /* alloca does more than just adjust %rsp on Windows */
1142 vpush_global_sym(&func_old_type
, TOK_alloca
);
1143 vswap(); /* Move alloca ref past allocation size */
1147 r
= gv(RC_INT
); /* allocation size */
1151 /* We align to 16 bytes rather than align */
1158 /* end of X86 code generator */
1159 /*************************************************************/
1161 /*************************************************************/