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 */
39 #define RC_INT2 0x0200
40 #define RC_IRET RC_EAX /* function return: integer register */
41 #define RC_LRET RC_EDX /* function return: second integer register */
42 #define RC_FRET RC_ST0 /* function return: float register */
43 #define RC_MASK (RC_INT|RC_INT2|RC_FLOAT)
45 /* pretty names for the registers */
57 /* return registers for function */
58 #define REG_IRET TREG_EAX /* single word int return register */
59 #define REG_LRET TREG_EDX /* second word return register (for long long) */
60 #define REG_FRET TREG_ST0 /* float return register */
62 /* defined if function parameters must be evaluated in reverse order */
63 #define INVERT_FUNC_PARAMS
65 /* defined if structures are passed as pointers. Otherwise structures
66 are directly pushed on stack. */
67 /* #define FUNC_STRUCT_PARAM_AS_PTR */
69 /* pointer size, in bytes */
72 /* long double size and alignment, in bytes */
73 #define LDOUBLE_SIZE 12
74 #define LDOUBLE_ALIGN 4
75 /* maximum alignment (for aligned attribute support) */
81 /******************************************************/
84 #define EM_TCC_TARGET EM_386
86 /* relocation type for 32 bit data relocation */
87 #define R_DATA_32 R_386_32
88 #define R_DATA_PTR R_386_32
89 #define R_JMP_SLOT R_386_JMP_SLOT
90 #define R_COPY R_386_COPY
92 #define ELF_START_ADDR 0x08048000
93 #define ELF_PAGE_SIZE 0x1000
95 /******************************************************/
96 #else /* ! TARGET_DEFS_ONLY */
97 /******************************************************/
100 ST_DATA
const int reg_classes
[NB_REGS
] = {
101 /* eax */ RC_INT
| RC_EAX
| RC_INT2
,
102 /* ecx */ RC_INT
| RC_ECX
| RC_INT2
,
103 /* edx */ RC_INT
| RC_EDX
,
104 RC_INT
|RC_INT2
|RC_EBX
,
106 /* st0 */ RC_FLOAT
| RC_ST0
,
111 static unsigned long func_sub_sp_offset
;
112 static int func_ret_sub
;
113 #ifdef CONFIG_TCC_BCHECK
114 static unsigned long func_bound_offset
;
117 /* XXX: make it faster ? */
118 ST_FUNC
void g(int c
)
122 if (ind1
> cur_text_section
->data_allocated
)
123 section_realloc(cur_text_section
, ind1
);
124 cur_text_section
->data
[ind
] = c
;
128 ST_FUNC
void o(unsigned int c
)
136 ST_FUNC
void gen_le16(int v
)
142 ST_FUNC
void gen_le32(int c
)
150 /* output a symbol and patch all calls to it */
151 ST_FUNC
void gsym_addr(int t
, int a
)
155 ptr
= (int *)(cur_text_section
->data
+ t
);
156 n
= *ptr
; /* next value */
162 ST_FUNC
void gsym(int t
)
167 /* psym is used to put an instruction with a data field which is a
168 reference to a symbol. It is in fact the same as oad ! */
171 /* instruction + 4 bytes data. Return the address of the data */
172 ST_FUNC
int oad(int c
, int s
)
178 if (ind1
> cur_text_section
->data_allocated
)
179 section_realloc(cur_text_section
, ind1
);
180 *(int *)(cur_text_section
->data
+ ind
) = s
;
186 /* output constant with relocation if 'r & VT_SYM' is true */
187 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
)
190 greloc(cur_text_section
, sym
, ind
, R_386_32
);
194 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
)
197 greloc(cur_text_section
, sym
, ind
, R_386_PC32
);
201 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
203 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
205 op_reg
= op_reg
<< 3;
206 if ((r
& VT_VALMASK
) == VT_CONST
) {
207 /* constant memory reference */
209 gen_addr32(r
, sym
, c
);
210 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
211 /* currently, we use only ebp as base */
213 /* short reference */
217 oad(0x85 | op_reg
, c
);
220 g(0x00 | op_reg
| (r
& VT_VALMASK
));
224 /* load 'r' from value 'sv' */
225 ST_FUNC
void load(int r
, SValue
*sv
)
227 int v
, t
, ft
, fc
, fr
;
232 sv
= pe_getimport(sv
, &v2
);
243 if((ft
& VT_BTYPE
) == VT_FUNC
)
246 size
= type_size(&sv
->type
, &align
);
249 if (v
== VT_LLOCAL
) {
251 v1
.r
= VT_LOCAL
| VT_LVAL
;
254 if (!(reg_classes
[fr
] & RC_INT
))
255 fr
= get_reg(RC_INT
);
258 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
261 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
264 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
267 } else if ((ft
& VT_TYPE
) == VT_BYTE
|| (ft
& VT_TYPE
) == VT_BOOL
) {
268 o(0xbe0f); /* movsbl */
269 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
270 o(0xb60f); /* movzbl */
271 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
272 o(0xbf0f); /* movswl */
273 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
274 o(0xb70f); /* movzwl */
278 gen_modrm(r
, fr
, sv
->sym
, fc
);
281 o(0xb8 + r
); /* mov $xx, r */
282 gen_addr32(fr
, sv
->sym
, fc
);
283 } else if (v
== VT_LOCAL
) {
285 o(0x8d); /* lea xxx(%ebp), r */
286 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
289 o(0xe8 + r
); /* mov %ebp, r */
291 } else if (v
== VT_CMP
) {
292 oad(0xb8 + r
, 0); /* mov $0, r */
293 o(0x0f); /* setxx %br */
296 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
298 oad(0xb8 + r
, t
); /* mov $1, r */
299 o(0x05eb); /* jmp after */
301 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
304 o(0xc0 + r
+ v
* 8); /* mov v, r */
309 /* store register 'r' in lvalue 'v' */
310 ST_FUNC
void store(int r
, SValue
*v
)
316 v
= pe_getimport(v
, &v2
);
321 fr
= v
->r
& VT_VALMASK
;
323 /* XXX: incorrect if float reg to reg */
324 if (bt
== VT_FLOAT
) {
327 } else if (bt
== VT_DOUBLE
) {
330 } else if (bt
== VT_LDOUBLE
) {
331 o(0xc0d9); /* fld %st(0) */
337 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
342 if (fr
== VT_CONST
||
345 gen_modrm(r
, v
->r
, v
->sym
, fc
);
346 } else if (fr
!= r
) {
347 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
351 static void gadd_sp(int val
)
353 if (val
== (char)val
) {
357 oad(0xc481, val
); /* add $xxx, %esp */
361 static void gen_static_call(int v
)
365 sym
= external_global_sym(v
, &func_old_type
, 0);
367 greloc(cur_text_section
, sym
, ind
-4, R_386_PC32
);
370 /* 'is_jmp' is '1' if it is a jump */
371 static void gcall_or_jmp(int is_jmp
)
374 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
376 if (vtop
->r
& VT_SYM
) {
377 /* relocation case */
378 greloc(cur_text_section
, vtop
->sym
,
379 ind
+ 1, R_386_PC32
);
381 /* put an empty PC32 relocation */
382 put_elf_reloc(symtab_section
, cur_text_section
,
383 ind
+ 1, R_386_PC32
, 0);
385 oad(0xe8 + is_jmp
, vtop
->c
.ul
- 4); /* call/jmp im */
387 /* otherwise, indirect call */
389 o(0xff); /* call/jmp *r */
390 o(0xd0 + r
+ (is_jmp
<< 4));
394 static uint8_t fastcall_regs
[3] = { TREG_EAX
, TREG_EDX
, TREG_ECX
};
395 static uint8_t fastcallw_regs
[2] = { TREG_ECX
, TREG_EDX
};
397 /* Return the number of registers needed to return the struct, or 0 if
398 returning via struct pointer. */
399 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *ret_align
)
404 *ret_align
= 1; // Never have to re-align return values for x86
405 size
= type_size(vt
, &align
);
408 } else if (size
> 4) {
418 *ret_align
= 1; // Never have to re-align return values for x86
423 /* Generate function call. The function address is pushed first, then
424 all the parameters in call order. This functions pops all the
425 parameters and the function address. */
426 ST_FUNC
void gfunc_call(int nb_args
)
428 int size
, align
, r
, args_size
, i
, func_call
;
432 for(i
= 0;i
< nb_args
; i
++) {
433 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
434 size
= type_size(&vtop
->type
, &align
);
435 /* align to stack align size */
436 size
= (size
+ 3) & ~3;
437 /* allocate the necessary size on stack */
438 oad(0xec81, size
); /* sub $xxx, %esp */
439 /* generate structure store */
441 o(0x89); /* mov %esp, r */
443 vset(&vtop
->type
, r
| VT_LVAL
, 0);
447 } else if (is_float(vtop
->type
.t
)) {
448 gv(RC_FLOAT
); /* only one float register */
449 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
451 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
455 oad(0xec81, size
); /* sub $xxx, %esp */
459 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
464 /* simple type (currently always same size) */
465 /* XXX: implicit cast ? */
467 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
469 o(0x50 + vtop
->r2
); /* push r */
473 o(0x50 + r
); /* push r */
478 save_regs(0); /* save used temporary registers */
479 func_sym
= vtop
->type
.ref
;
480 func_call
= func_sym
->a
.func_call
;
482 if ((func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) ||
483 func_call
== FUNC_FASTCALLW
) {
484 int fastcall_nb_regs
;
485 uint8_t *fastcall_regs_ptr
;
486 if (func_call
== FUNC_FASTCALLW
) {
487 fastcall_regs_ptr
= fastcallw_regs
;
488 fastcall_nb_regs
= 2;
490 fastcall_regs_ptr
= fastcall_regs
;
491 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
493 for(i
= 0;i
< fastcall_nb_regs
; i
++) {
496 o(0x58 + fastcall_regs_ptr
[i
]); /* pop r */
497 /* XXX: incorrect for struct/floats */
501 #ifndef TCC_TARGET_PE
502 else if ((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
)
507 if (args_size
&& func_call
!= FUNC_STDCALL
)
513 #define FUNC_PROLOG_SIZE 10
515 #define FUNC_PROLOG_SIZE 9
518 /* generate function prolog of type 't' */
519 ST_FUNC
void gfunc_prolog(CType
*func_type
)
521 int addr
, align
, size
, func_call
, fastcall_nb_regs
;
522 int param_index
, param_addr
;
523 uint8_t *fastcall_regs_ptr
;
527 sym
= func_type
->ref
;
528 func_call
= sym
->a
.func_call
;
533 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
534 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
535 fastcall_regs_ptr
= fastcall_regs
;
536 } else if (func_call
== FUNC_FASTCALLW
) {
537 fastcall_nb_regs
= 2;
538 fastcall_regs_ptr
= fastcallw_regs
;
540 fastcall_nb_regs
= 0;
541 fastcall_regs_ptr
= NULL
;
545 ind
+= FUNC_PROLOG_SIZE
;
546 func_sub_sp_offset
= ind
;
547 /* if the function returns a structure, then add an
548 implicit pointer parameter */
550 func_var
= (sym
->c
== FUNC_ELLIPSIS
);
552 size
= type_size(&func_vt
,&align
);
553 if (((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) && (size
> 8)) {
555 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
557 /* XXX: fastcall case ? */
562 /* define parameters */
563 while ((sym
= sym
->next
) != NULL
) {
565 size
= type_size(type
, &align
);
566 size
= (size
+ 3) & ~3;
567 #ifdef FUNC_STRUCT_PARAM_AS_PTR
568 /* structs are passed as pointer */
569 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
573 if (param_index
< fastcall_nb_regs
) {
574 /* save FASTCALL register */
577 gen_modrm(fastcall_regs_ptr
[param_index
], VT_LOCAL
, NULL
, loc
);
583 sym_push(sym
->v
& ~SYM_FIELD
, type
,
584 VT_LOCAL
| lvalue_type(type
->t
), param_addr
);
588 /* pascal type call ? */
589 if (func_call
== FUNC_STDCALL
)
590 func_ret_sub
= addr
- 8;
591 #ifndef TCC_TARGET_PE
596 #ifdef CONFIG_TCC_BCHECK
597 /* leave some room for bound checking code */
598 if (tcc_state
->do_bounds_check
) {
599 oad(0xb8, 0); /* lbound section pointer */
600 oad(0xb8, 0); /* call to function */
601 func_bound_offset
= lbounds_section
->data_offset
;
606 /* generate function epilog */
607 ST_FUNC
void gfunc_epilog(void)
611 #ifdef CONFIG_TCC_BCHECK
612 if (tcc_state
->do_bounds_check
613 && func_bound_offset
!= lbounds_section
->data_offset
) {
617 /* add end of table info */
618 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(int));
620 /* generate bound local allocation */
622 ind
= func_sub_sp_offset
;
623 sym_data
= get_sym_ref(&char_pointer_type
, lbounds_section
,
624 func_bound_offset
, lbounds_section
->data_offset
);
625 greloc(cur_text_section
, sym_data
,
627 oad(0xb8, 0); /* mov %eax, xxx */
628 gen_static_call(TOK___bound_local_new
);
631 /* generate bound check local freeing */
632 o(0x5250); /* save returned value, if any */
633 greloc(cur_text_section
, sym_data
,
635 oad(0xb8, 0); /* mov %eax, xxx */
636 gen_static_call(TOK___bound_local_delete
);
638 o(0x585a); /* restore returned value, if any */
642 if (func_ret_sub
== 0) {
647 g(func_ret_sub
>> 8);
649 /* align local size to word & save local variables */
653 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
656 oad(0xb8, v
); /* mov stacksize, %eax */
657 gen_static_call(TOK___chkstk
); /* call __chkstk, (does the stackframe too) */
661 o(0xe58955); /* push %ebp, mov %esp, %ebp */
662 o(0xec81); /* sub esp, stacksize */
664 #if FUNC_PROLOG_SIZE == 10
665 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
671 /* generate a jump to a label */
672 ST_FUNC
int gjmp(int t
)
674 return psym(0xe9, t
);
677 /* generate a jump to a fixed address */
678 ST_FUNC
void gjmp_addr(int a
)
686 oad(0xe9, a
- ind
- 5);
690 /* generate a test. set 'inv' to invert test. Stack entry is popped */
691 ST_FUNC
int gtst(int inv
, int t
)
695 v
= vtop
->r
& VT_VALMASK
;
697 /* fast case : can jump directly since flags are set */
699 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
700 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
701 /* && or || optimization */
702 if ((v
& 1) == inv
) {
703 /* insert vtop->c jump list in t */
706 p
= (int *)(cur_text_section
->data
+ *p
);
714 if (is_float(vtop
->type
.t
) ||
715 (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
719 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
720 /* constant jmp optimization */
721 if ((vtop
->c
.i
!= 0) != inv
)
728 t
= psym(0x85 ^ inv
, t
);
735 /* generate an integer binary operation */
736 ST_FUNC
void gen_opi(int op
)
738 int r
, fr
, opc
, fc
, c
;
743 cc
= (fr
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
744 tt2
= (fr
& (VT_LVAL
| VT_LVAL_TYPE
)) == VT_LVAL
;
748 case TOK_ADDC1
: /* add with carry generation */
758 /* generate inc and dec for smaller code */
759 if (c
== 1 && opc
== 0) {
761 } else if (c
== 1 && opc
== 5) {
770 oad(0xc0 + r
+ opc
*8, c
);
777 gen_modrm(r
, fr
, vtop
->sym
, fc
);
782 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
788 case TOK_SUBC1
: /* sub with carry generation */
791 case TOK_ADDC2
: /* add with carry use */
794 case TOK_SUBC2
: /* sub with carry use */
818 gen_modrm(opc
, fr
, vtop
->sym
, fc
);
820 o(0xc0 + fr
+ opc
*8);
822 o(0xaf0f); /* imul fr, r */
824 gen_modrm(r
, fr
, vtop
->sym
, fc
);
849 o(0xc1); /* shl/shr/sar $xxx, r */
854 /* we generate the shift in ecx */
857 o(0xd3); /* shl/shr/sar %cl, r */
877 /* first operand must be in eax */
878 /* XXX: need better constraint for second operand */
880 gv2(RC_EAX
, RC_INT2
);
888 if (op
== TOK_UMULL
) {
889 o(0xf7); /* mul fr */
892 o(uu
? 0xd231 : 0x99); /* xor %edx,%edx : cdq RDX:RAX <- sign-extend of RAX. */
893 o(0xf7); /* div fr, %eax */
896 gen_modrm(opc
, fr
, vtop
->sym
, fc
);
898 o(0xc0 + fr
+ opc
*8);
899 if (op
== '%' || op
== TOK_UMOD
)
912 /* generate a floating point operation 'v = t1 op t2' instruction. The
913 two operands are guaranted to have the same floating point type */
914 /* XXX: need to use ST1 too */
915 ST_FUNC
void gen_opf(int op
)
917 int a
, ft
, fc
, swapped
, r
;
919 /* convert constants to memory references */
920 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
925 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
928 /* must put at least one value in the floating point register */
929 if ((vtop
[-1].r
& VT_LVAL
) &&
930 (vtop
[0].r
& VT_LVAL
)) {
936 /* swap the stack if needed so that t1 is the register and t2 is
937 the memory reference */
938 if (vtop
[-1].r
& VT_LVAL
) {
942 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
943 /* load on stack second operand */
944 load(TREG_ST0
, vtop
);
945 save_reg(TREG_EAX
); /* eax is used by FP comparison code */
946 if (op
== TOK_GE
|| op
== TOK_GT
)
948 else if (op
== TOK_EQ
|| op
== TOK_NE
)
951 o(0xc9d9); /* fxch %st(1) */
952 if (op
== TOK_EQ
|| op
== TOK_NE
)
953 o(0xe9da); /* fucompp */
955 o(0xd9de); /* fcompp */
956 o(0xe0df); /* fnstsw %ax */
958 o(0x45e480); /* and $0x45, %ah */
959 o(0x40fC80); /* cmp $0x40, %ah */
960 } else if (op
== TOK_NE
) {
961 o(0x45e480); /* and $0x45, %ah */
962 o(0x40f480); /* xor $0x40, %ah */
964 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
965 o(0x05c4f6); /* test $0x05, %ah */
968 o(0x45c4f6); /* test $0x45, %ah */
975 /* no memory reference possible for long double operations */
976 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
977 load(TREG_ST0
, vtop
);
1002 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
1003 o(0xde); /* fxxxp %st, %st(1) */
1006 /* if saved lvalue, then we must reload it */
1008 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
1010 r
= get_reg(RC_INT
);
1012 v1
.r
= VT_LOCAL
| VT_LVAL
;
1018 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
1022 gen_modrm(a
, r
, vtop
->sym
, fc
);
1028 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1029 and 'long long' cases. */
1030 ST_FUNC
void gen_cvt_itof(int t
)
1034 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
1035 /* signed long long to float/double/long double (unsigned case
1036 is handled generically) */
1037 o(0x50 + vtop
->r2
); /* push r2 */
1038 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1039 o(0x242cdf); /* fildll (%esp) */
1040 o(0x08c483); /* add $8, %esp */
1041 } else if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
1042 (VT_INT
| VT_UNSIGNED
)) {
1043 /* unsigned int to float/double/long double */
1044 o(0x6a); /* push $0 */
1046 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1047 o(0x242cdf); /* fildll (%esp) */
1048 o(0x08c483); /* add $8, %esp */
1050 /* int to float/double/long double */
1051 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1052 o(0x2404db); /* fildl (%esp) */
1053 o(0x04c483); /* add $4, %esp */
1058 /* convert fp to int 't' type */
1059 ST_FUNC
void gen_cvt_ftoi(int t
)
1061 int bt
= vtop
->type
.t
& VT_BTYPE
;
1063 vpush_global_sym(&func_old_type
, TOK___fixsfdi
);
1064 else if (bt
== VT_LDOUBLE
)
1065 vpush_global_sym(&func_old_type
, TOK___fixxfdi
);
1067 vpush_global_sym(&func_old_type
, TOK___fixdfdi
);
1072 vtop
->r2
= REG_LRET
;
1075 /* convert from one floating point type to another */
1076 ST_FUNC
void gen_cvt_ftof(int t
)
1078 /* all we have to do on i386 is to put the float in a register */
1082 /* computed goto support */
1083 ST_FUNC
void ggoto(void)
1089 /* bound check support functions */
1090 #ifdef CONFIG_TCC_BCHECK
1092 /* generate a bounded pointer addition */
1093 ST_FUNC
void gen_bounded_ptr_add(void)
1095 /* prepare fast i386 function call (args in eax and edx) */
1096 gv2(RC_EAX
, RC_EDX
);
1097 /* save all temporary registers */
1100 /* do a fast function call */
1101 gen_static_call(TOK___bound_ptr_add
);
1102 /* returned pointer is in eax */
1104 vtop
->r
= TREG_EAX
| VT_BOUNDED
;
1105 /* address of bounding function call point */
1106 vtop
->c
.ul
= (cur_text_section
->reloc
->data_offset
- sizeof(Elf32_Rel
));
1109 /* patch pointer addition in vtop so that pointer dereferencing is
1111 ST_FUNC
void gen_bounded_ptr_deref(void)
1119 /* XXX: put that code in generic part of tcc */
1120 if (!is_float(vtop
->type
.t
)) {
1121 if (vtop
->r
& VT_LVAL_BYTE
)
1123 else if (vtop
->r
& VT_LVAL_SHORT
)
1127 size
= type_size(&vtop
->type
, &align
);
1129 case 1: func
= TOK___bound_ptr_indir1
; break;
1130 case 2: func
= TOK___bound_ptr_indir2
; break;
1131 case 4: func
= TOK___bound_ptr_indir4
; break;
1132 case 8: func
= TOK___bound_ptr_indir8
; break;
1133 case 12: func
= TOK___bound_ptr_indir12
; break;
1134 case 16: func
= TOK___bound_ptr_indir16
; break;
1136 tcc_error("unhandled size when dereferencing bounded pointer");
1141 /* patch relocation */
1142 /* XXX: find a better solution ? */
1143 rel
= (Elf32_Rel
*)(cur_text_section
->reloc
->data
+ vtop
->c
.ul
);
1144 sym
= external_global_sym(func
, &func_old_type
, 0);
1146 put_extern_sym(sym
, NULL
, 0, 0);
1147 rel
->r_info
= ELF32_R_INFO(sym
->c
, ELF32_R_TYPE(rel
->r_info
));
1151 /* Save the stack pointer onto the stack */
1152 ST_FUNC
void gen_vla_sp_save(int addr
) {
1153 /* mov %esp,addr(%ebp)*/
1155 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1158 /* Restore the SP from a location on the stack */
1159 ST_FUNC
void gen_vla_sp_restore(int addr
) {
1161 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1164 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1165 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
) {
1166 #ifdef TCC_TARGET_PE
1167 /* alloca does more than just adjust %rsp on Windows */
1168 vpush_global_sym(&func_old_type
, TOK_alloca
);
1169 vswap(); /* Move alloca ref past allocation size */
1171 vset(type
, REG_IRET
, 0);
1174 r
= gv(RC_INT
); /* allocation size */
1178 /* We align to 16 bytes rather than align */
1189 /* end of X86 code generator */
1190 /*************************************************************/
1192 /*************************************************************/