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 /******************************************************/
79 #define EM_TCC_TARGET EM_386
81 /* relocation type for 32 bit data relocation */
82 #define R_DATA_32 R_386_32
83 #define R_DATA_PTR R_386_32
84 #define R_JMP_SLOT R_386_JMP_SLOT
85 #define R_GLOB_DAT R_386_GLOB_DAT
86 #define R_COPY R_386_COPY
88 #define ELF_START_ADDR 0x08048000
89 #define ELF_PAGE_SIZE 0x1000
91 /******************************************************/
92 #else /* ! TARGET_DEFS_ONLY */
93 /******************************************************/
96 /* define to 1/0 to [not] have EBX as 4th register */
99 ST_DATA
const int reg_classes
[NB_REGS
] = {
100 /* eax */ RC_INT
| RC_EAX
,
101 /* ecx */ RC_INT
| RC_ECX
,
102 /* edx */ RC_INT
| RC_EDX
,
103 /* ebx */ (RC_INT
| RC_EBX
) * USE_EBX
,
104 /* st0 */ RC_FLOAT
| RC_ST0
,
107 static unsigned long func_sub_sp_offset
;
108 static int func_ret_sub
;
109 #ifdef CONFIG_TCC_BCHECK
110 static addr_t func_bound_offset
;
111 static unsigned long func_bound_ind
;
114 /* XXX: make it faster ? */
115 ST_FUNC
void g(int c
)
119 if (ind1
> cur_text_section
->data_allocated
)
120 section_realloc(cur_text_section
, ind1
);
121 cur_text_section
->data
[ind
] = c
;
125 ST_FUNC
void o(unsigned int c
)
133 ST_FUNC
void gen_le16(int v
)
139 ST_FUNC
void gen_le32(int c
)
147 /* output a symbol and patch all calls to it */
148 ST_FUNC
void gsym_addr(int t
, int a
)
151 unsigned char *ptr
= cur_text_section
->data
+ t
;
152 uint32_t n
= read32le(ptr
); /* next value */
153 write32le(ptr
, a
- t
- 4);
158 ST_FUNC
void gsym(int t
)
163 /* psym is used to put an instruction with a data field which is a
164 reference to a symbol. It is in fact the same as oad ! */
167 /* instruction + 4 bytes data. Return the address of the data */
168 ST_FUNC
int oad(int c
, int s
)
174 if (ind1
> cur_text_section
->data_allocated
)
175 section_realloc(cur_text_section
, ind1
);
176 write32le(cur_text_section
->data
+ ind
, s
);
182 /* output constant with relocation if 'r & VT_SYM' is true */
183 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
)
186 greloc(cur_text_section
, sym
, ind
, R_386_32
);
190 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
)
193 greloc(cur_text_section
, sym
, ind
, R_386_PC32
);
197 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
199 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
201 op_reg
= op_reg
<< 3;
202 if ((r
& VT_VALMASK
) == VT_CONST
) {
203 /* constant memory reference */
205 gen_addr32(r
, sym
, c
);
206 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
207 /* currently, we use only ebp as base */
209 /* short reference */
213 oad(0x85 | op_reg
, c
);
216 g(0x00 | op_reg
| (r
& VT_VALMASK
));
220 /* load 'r' from value 'sv' */
221 ST_FUNC
void load(int r
, SValue
*sv
)
223 int v
, t
, ft
, fc
, fr
;
228 sv
= pe_getimport(sv
, &v2
);
235 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
239 if (v
== VT_LLOCAL
) {
241 v1
.r
= VT_LOCAL
| VT_LVAL
;
244 if (!(reg_classes
[fr
] & RC_INT
))
245 fr
= get_reg(RC_INT
);
248 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
251 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
254 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
257 } else if ((ft
& VT_TYPE
) == VT_BYTE
|| (ft
& VT_TYPE
) == VT_BOOL
) {
258 o(0xbe0f); /* movsbl */
259 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
260 o(0xb60f); /* movzbl */
261 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
262 o(0xbf0f); /* movswl */
263 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
264 o(0xb70f); /* movzwl */
268 gen_modrm(r
, fr
, sv
->sym
, fc
);
271 o(0xb8 + r
); /* mov $xx, r */
272 gen_addr32(fr
, sv
->sym
, fc
);
273 } else if (v
== VT_LOCAL
) {
275 o(0x8d); /* lea xxx(%ebp), r */
276 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
279 o(0xe8 + r
); /* mov %ebp, r */
281 } else if (v
== VT_CMP
) {
282 oad(0xb8 + r
, 0); /* mov $0, r */
283 o(0x0f); /* setxx %br */
286 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
288 oad(0xb8 + r
, t
); /* mov $1, r */
289 o(0x05eb); /* jmp after */
291 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
294 o(0xc0 + r
+ v
* 8); /* mov v, r */
299 /* store register 'r' in lvalue 'v' */
300 ST_FUNC
void store(int r
, SValue
*v
)
306 v
= pe_getimport(v
, &v2
);
311 fr
= v
->r
& VT_VALMASK
;
312 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
314 /* XXX: incorrect if float reg to reg */
315 if (bt
== VT_FLOAT
) {
318 } else if (bt
== VT_DOUBLE
) {
321 } else if (bt
== VT_LDOUBLE
) {
322 o(0xc0d9); /* fld %st(0) */
328 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
333 if (fr
== VT_CONST
||
336 gen_modrm(r
, v
->r
, v
->sym
, fc
);
337 } else if (fr
!= r
) {
338 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
342 static void gadd_sp(int val
)
344 if (val
== (char)val
) {
348 oad(0xc481, val
); /* add $xxx, %esp */
352 static void gen_static_call(int v
)
356 sym
= external_global_sym(v
, &func_old_type
, 0);
358 greloc(cur_text_section
, sym
, ind
-4, R_386_PC32
);
361 /* 'is_jmp' is '1' if it is a jump */
362 static void gcall_or_jmp(int is_jmp
)
365 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
368 if (vtop
->r
& VT_SYM
) {
369 /* relocation case */
370 greloc(cur_text_section
, vtop
->sym
,
371 ind
+ 1, R_386_PC32
);
373 /* put an empty PC32 relocation */
374 put_elf_reloc(symtab_section
, cur_text_section
,
375 ind
+ 1, R_386_PC32
, 0);
377 oad(0xe8 + is_jmp
, vtop
->c
.i
- 4); /* call/jmp im */
378 /* extend the return value to the whole register if necessary
379 visual studio and gcc do not always set the whole eax register
380 when assigning the return value of a function */
381 rt
= vtop
->type
.ref
->type
.t
;
382 switch (rt
& VT_BTYPE
) {
384 if (rt
& VT_UNSIGNED
) {
385 o(0xc0b60f); /* movzx %al, %eax */
388 o(0xc0be0f); /* movsx %al, %eax */
392 if (rt
& VT_UNSIGNED
) {
393 o(0xc0b70f); /* movzx %ax, %eax */
396 o(0xc0bf0f); /* movsx %ax, %eax */
403 /* otherwise, indirect call */
405 o(0xff); /* call/jmp *r */
406 o(0xd0 + r
+ (is_jmp
<< 4));
410 static uint8_t fastcall_regs
[3] = { TREG_EAX
, TREG_EDX
, TREG_ECX
};
411 static uint8_t fastcallw_regs
[2] = { TREG_ECX
, TREG_EDX
};
413 /* Return the number of registers needed to return the struct, or 0 if
414 returning via struct pointer. */
415 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *ret_align
, int *regsize
)
420 *ret_align
= 1; // Never have to re-align return values for x86
422 size
= type_size(vt
, &align
);
425 } else if (size
> 4) {
435 *ret_align
= 1; // Never have to re-align return values for x86
440 /* Generate function call. The function address is pushed first, then
441 all the parameters in call order. This functions pops all the
442 parameters and the function address. */
443 ST_FUNC
void gfunc_call(int nb_args
)
445 int size
, align
, r
, args_size
, i
, func_call
;
449 for(i
= 0;i
< nb_args
; i
++) {
450 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
451 size
= type_size(&vtop
->type
, &align
);
452 /* align to stack align size */
453 size
= (size
+ 3) & ~3;
454 /* allocate the necessary size on stack */
455 oad(0xec81, size
); /* sub $xxx, %esp */
456 /* generate structure store */
458 o(0x89); /* mov %esp, r */
460 vset(&vtop
->type
, r
| VT_LVAL
, 0);
464 } else if (is_float(vtop
->type
.t
)) {
465 gv(RC_FLOAT
); /* only one float register */
466 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
468 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
472 oad(0xec81, size
); /* sub $xxx, %esp */
476 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
481 /* simple type (currently always same size) */
482 /* XXX: implicit cast ? */
484 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
486 o(0x50 + vtop
->r2
); /* push r */
490 o(0x50 + r
); /* push r */
495 save_regs(0); /* save used temporary registers */
496 func_sym
= vtop
->type
.ref
;
497 func_call
= func_sym
->a
.func_call
;
499 if ((func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) ||
500 func_call
== FUNC_FASTCALLW
) {
501 int fastcall_nb_regs
;
502 uint8_t *fastcall_regs_ptr
;
503 if (func_call
== FUNC_FASTCALLW
) {
504 fastcall_regs_ptr
= fastcallw_regs
;
505 fastcall_nb_regs
= 2;
507 fastcall_regs_ptr
= fastcall_regs
;
508 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
510 for(i
= 0;i
< fastcall_nb_regs
; i
++) {
513 o(0x58 + fastcall_regs_ptr
[i
]); /* pop r */
514 /* XXX: incorrect for struct/floats */
518 #ifndef TCC_TARGET_PE
519 else if ((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
)
524 if (args_size
&& func_call
!= FUNC_STDCALL
)
530 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
532 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
535 /* generate function prolog of type 't' */
536 ST_FUNC
void gfunc_prolog(CType
*func_type
)
538 int addr
, align
, size
, func_call
, fastcall_nb_regs
;
539 int param_index
, param_addr
;
540 uint8_t *fastcall_regs_ptr
;
544 sym
= func_type
->ref
;
545 func_call
= sym
->a
.func_call
;
550 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
551 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
552 fastcall_regs_ptr
= fastcall_regs
;
553 } else if (func_call
== FUNC_FASTCALLW
) {
554 fastcall_nb_regs
= 2;
555 fastcall_regs_ptr
= fastcallw_regs
;
557 fastcall_nb_regs
= 0;
558 fastcall_regs_ptr
= NULL
;
562 ind
+= FUNC_PROLOG_SIZE
;
563 func_sub_sp_offset
= ind
;
564 /* if the function returns a structure, then add an
565 implicit pointer parameter */
567 func_var
= (sym
->c
== FUNC_ELLIPSIS
);
569 size
= type_size(&func_vt
,&align
);
570 if (((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) && (size
> 8)) {
572 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
574 /* XXX: fastcall case ? */
579 /* define parameters */
580 while ((sym
= sym
->next
) != NULL
) {
582 size
= type_size(type
, &align
);
583 size
= (size
+ 3) & ~3;
584 #ifdef FUNC_STRUCT_PARAM_AS_PTR
585 /* structs are passed as pointer */
586 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
590 if (param_index
< fastcall_nb_regs
) {
591 /* save FASTCALL register */
594 gen_modrm(fastcall_regs_ptr
[param_index
], VT_LOCAL
, NULL
, loc
);
600 sym_push(sym
->v
& ~SYM_FIELD
, type
,
601 VT_LOCAL
| lvalue_type(type
->t
), param_addr
);
605 /* pascal type call ? */
606 if (func_call
== FUNC_STDCALL
)
607 func_ret_sub
= addr
- 8;
608 #ifndef TCC_TARGET_PE
613 #ifdef CONFIG_TCC_BCHECK
614 /* leave some room for bound checking code */
615 if (tcc_state
->do_bounds_check
) {
616 func_bound_offset
= lbounds_section
->data_offset
;
617 func_bound_ind
= ind
;
618 oad(0xb8, 0); /* lbound section pointer */
619 oad(0xb8, 0); /* call to function */
624 /* generate function epilog */
625 ST_FUNC
void gfunc_epilog(void)
629 #ifdef CONFIG_TCC_BCHECK
630 if (tcc_state
->do_bounds_check
631 && func_bound_offset
!= lbounds_section
->data_offset
) {
636 /* add end of table info */
637 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(addr_t
));
640 /* generate bound local allocation */
642 ind
= func_bound_ind
;
643 sym_data
= get_sym_ref(&char_pointer_type
, lbounds_section
,
644 func_bound_offset
, lbounds_section
->data_offset
);
645 greloc(cur_text_section
, sym_data
,
647 oad(0xb8, 0); /* mov %eax, xxx */
648 gen_static_call(TOK___bound_local_new
);
651 /* generate bound check local freeing */
652 o(0x5250); /* save returned value, if any */
653 greloc(cur_text_section
, sym_data
, ind
+ 1, R_386_32
);
654 oad(0xb8, 0); /* mov %eax, xxx */
655 gen_static_call(TOK___bound_local_delete
);
656 o(0x585a); /* restore returned value, if any */
659 o(0x5b * USE_EBX
); /* pop ebx */
661 if (func_ret_sub
== 0) {
666 g(func_ret_sub
>> 8);
668 /* align local size to word & save local variables */
672 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
675 oad(0xb8, v
); /* mov stacksize, %eax */
676 gen_static_call(TOK___chkstk
); /* call __chkstk, (does the stackframe too) */
680 o(0xe58955); /* push %ebp, mov %esp, %ebp */
681 o(0xec81); /* sub esp, stacksize */
684 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
687 o(0x53 * USE_EBX
); /* push ebx */
691 /* generate a jump to a label */
692 ST_FUNC
int gjmp(int t
)
694 return psym(0xe9, t
);
697 /* generate a jump to a fixed address */
698 ST_FUNC
void gjmp_addr(int a
)
706 oad(0xe9, a
- ind
- 5);
710 ST_FUNC
void gtst_addr(int inv
, int a
)
712 inv
^= (vtop
--)->c
.i
;
719 oad(inv
- 16, a
- 4);
723 /* generate a test. set 'inv' to invert test. Stack entry is popped */
724 ST_FUNC
int gtst(int inv
, int t
)
726 int v
= vtop
->r
& VT_VALMASK
;
728 /* fast case : can jump directly since flags are set */
730 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
731 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
732 /* && or || optimization */
733 if ((v
& 1) == inv
) {
734 /* insert vtop->c jump list in t */
735 uint32_t n1
, n
= vtop
->c
.i
;
737 while ((n1
= read32le(cur_text_section
->data
+ n
)))
739 write32le(cur_text_section
->data
+ n
, t
);
751 /* generate an integer binary operation */
752 ST_FUNC
void gen_opi(int op
)
758 case TOK_ADDC1
: /* add with carry generation */
761 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
768 /* generate inc and dec for smaller code */
769 if (c
==1 && opc
==0 && op
!= TOK_ADDC1
) {
771 } else if (c
==1 && opc
==5 && op
!= TOK_SUBC1
) {
775 o(0xc0 | (opc
<< 3) | r
);
780 oad(0xc0 | (opc
<< 3) | r
, c
);
786 o((opc
<< 3) | 0x01);
787 o(0xc0 + r
+ fr
* 8);
790 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
796 case TOK_SUBC1
: /* sub with carry generation */
799 case TOK_ADDC2
: /* add with carry use */
802 case TOK_SUBC2
: /* sub with carry use */
819 o(0xaf0f); /* imul fr, r */
820 o(0xc0 + fr
+ r
* 8);
831 opc
= 0xc0 | (opc
<< 3);
832 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
837 c
= vtop
->c
.i
& 0x1f;
838 o(0xc1); /* shl/shr/sar $xxx, r */
842 /* we generate the shift in ecx */
845 o(0xd3); /* shl/shr/sar %cl, r */
856 /* first operand must be in eax */
857 /* XXX: need better constraint for second operand */
863 /* save EAX too if used otherwise */
864 save_reg_upstack(TREG_EAX
, 1);
865 if (op
== TOK_UMULL
) {
866 o(0xf7); /* mul fr */
871 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
872 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
875 o(0xf799); /* cltd, idiv fr, %eax */
878 if (op
== '%' || op
== TOK_UMOD
)
891 /* generate a floating point operation 'v = t1 op t2' instruction. The
892 two operands are guaranted to have the same floating point type */
893 /* XXX: need to use ST1 too */
894 ST_FUNC
void gen_opf(int op
)
896 int a
, ft
, fc
, swapped
, r
;
898 /* convert constants to memory references */
899 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
904 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
907 /* must put at least one value in the floating point register */
908 if ((vtop
[-1].r
& VT_LVAL
) &&
909 (vtop
[0].r
& VT_LVAL
)) {
915 /* swap the stack if needed so that t1 is the register and t2 is
916 the memory reference */
917 if (vtop
[-1].r
& VT_LVAL
) {
921 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
922 /* load on stack second operand */
923 load(TREG_ST0
, vtop
);
924 save_reg(TREG_EAX
); /* eax is used by FP comparison code */
925 if (op
== TOK_GE
|| op
== TOK_GT
)
927 else if (op
== TOK_EQ
|| op
== TOK_NE
)
930 o(0xc9d9); /* fxch %st(1) */
931 if (op
== TOK_EQ
|| op
== TOK_NE
)
932 o(0xe9da); /* fucompp */
934 o(0xd9de); /* fcompp */
935 o(0xe0df); /* fnstsw %ax */
937 o(0x45e480); /* and $0x45, %ah */
938 o(0x40fC80); /* cmp $0x40, %ah */
939 } else if (op
== TOK_NE
) {
940 o(0x45e480); /* and $0x45, %ah */
941 o(0x40f480); /* xor $0x40, %ah */
943 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
944 o(0x05c4f6); /* test $0x05, %ah */
947 o(0x45c4f6); /* test $0x45, %ah */
954 /* no memory reference possible for long double operations */
955 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
956 load(TREG_ST0
, vtop
);
981 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
982 o(0xde); /* fxxxp %st, %st(1) */
985 /* if saved lvalue, then we must reload it */
987 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
991 v1
.r
= VT_LOCAL
| VT_LVAL
;
997 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
1001 gen_modrm(a
, r
, vtop
->sym
, fc
);
1007 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1008 and 'long long' cases. */
1009 ST_FUNC
void gen_cvt_itof(int t
)
1013 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1014 /* signed long long to float/double/long double (unsigned case
1015 is handled generically) */
1016 o(0x50 + vtop
->r2
); /* push r2 */
1017 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1018 o(0x242cdf); /* fildll (%esp) */
1019 o(0x08c483); /* add $8, %esp */
1020 } else if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1021 (VT_INT
| VT_UNSIGNED
)) {
1022 /* unsigned int to float/double/long double */
1023 o(0x6a); /* push $0 */
1025 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1026 o(0x242cdf); /* fildll (%esp) */
1027 o(0x08c483); /* add $8, %esp */
1029 /* int to float/double/long double */
1030 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1031 o(0x2404db); /* fildl (%esp) */
1032 o(0x04c483); /* add $4, %esp */
1037 /* convert fp to int 't' type */
1038 ST_FUNC
void gen_cvt_ftoi(int t
)
1044 gen_static_call(TOK___tcc_cvt_ftol
);
1045 vtop
->r
= TREG_EAX
; /* mark reg as used */
1047 vtop
->r2
= TREG_EDX
;
1049 int bt
= vtop
->type
.t
& VT_BTYPE
;
1051 vpush_global_sym(&func_old_type
, TOK___fixsfdi
);
1052 else if (bt
== VT_LDOUBLE
)
1053 vpush_global_sym(&func_old_type
, TOK___fixxfdi
);
1055 vpush_global_sym(&func_old_type
, TOK___fixdfdi
);
1060 vtop
->r2
= REG_LRET
;
1064 /* convert from one floating point type to another */
1065 ST_FUNC
void gen_cvt_ftof(int t
)
1067 /* all we have to do on i386 is to put the float in a register */
1071 /* computed goto support */
1072 ST_FUNC
void ggoto(void)
1078 /* bound check support functions */
1079 #ifdef CONFIG_TCC_BCHECK
1081 /* generate a bounded pointer addition */
1082 ST_FUNC
void gen_bounded_ptr_add(void)
1084 /* prepare fast i386 function call (args in eax and edx) */
1085 gv2(RC_EAX
, RC_EDX
);
1086 /* save all temporary registers */
1089 /* do a fast function call */
1090 gen_static_call(TOK___bound_ptr_add
);
1091 /* returned pointer is in eax */
1093 vtop
->r
= TREG_EAX
| VT_BOUNDED
;
1094 /* address of bounding function call point */
1095 vtop
->c
.i
= (cur_text_section
->reloc
->data_offset
- sizeof(Elf32_Rel
));
1098 /* patch pointer addition in vtop so that pointer dereferencing is
1100 ST_FUNC
void gen_bounded_ptr_deref(void)
1108 /* XXX: put that code in generic part of tcc */
1109 if (!is_float(vtop
->type
.t
)) {
1110 if (vtop
->r
& VT_LVAL_BYTE
)
1112 else if (vtop
->r
& VT_LVAL_SHORT
)
1116 size
= type_size(&vtop
->type
, &align
);
1118 case 1: func
= TOK___bound_ptr_indir1
; break;
1119 case 2: func
= TOK___bound_ptr_indir2
; break;
1120 case 4: func
= TOK___bound_ptr_indir4
; break;
1121 case 8: func
= TOK___bound_ptr_indir8
; break;
1122 case 12: func
= TOK___bound_ptr_indir12
; break;
1123 case 16: func
= TOK___bound_ptr_indir16
; break;
1125 tcc_error("unhandled size when dereferencing bounded pointer");
1130 /* patch relocation */
1131 /* XXX: find a better solution ? */
1132 rel
= (Elf32_Rel
*)(cur_text_section
->reloc
->data
+ vtop
->c
.i
);
1133 sym
= external_global_sym(func
, &func_old_type
, 0);
1135 put_extern_sym(sym
, NULL
, 0, 0);
1136 rel
->r_info
= ELF32_R_INFO(sym
->c
, ELF32_R_TYPE(rel
->r_info
));
1140 /* Save the stack pointer onto the stack */
1141 ST_FUNC
void gen_vla_sp_save(int addr
) {
1142 /* mov %esp,addr(%ebp)*/
1144 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1147 /* Restore the SP from a location on the stack */
1148 ST_FUNC
void gen_vla_sp_restore(int addr
) {
1150 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1153 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1154 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
) {
1155 #ifdef TCC_TARGET_PE
1156 /* alloca does more than just adjust %rsp on Windows */
1157 vpush_global_sym(&func_old_type
, TOK_alloca
);
1158 vswap(); /* Move alloca ref past allocation size */
1162 r
= gv(RC_INT
); /* allocation size */
1166 /* We align to 16 bytes rather than align */
1173 /* end of X86 code generator */
1174 /*************************************************************/
1176 /*************************************************************/