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 */
26 #define CONFIG_TCC_ASM
28 /* a register can belong to several classes. The classes must be
29 sorted from more general to more precise (see gv2() code which does
30 assumptions on it). */
31 #define RC_INT 0x0001 /* generic integer register */
32 #define RC_FLOAT 0x0002 /* generic float register */
39 #define RC_IRET RC_EAX /* function return: integer register */
40 #define RC_IRE2 RC_EDX /* function return: second integer register */
41 #define RC_FRET RC_ST0 /* function return: float register */
43 /* pretty names for the registers */
53 /* return registers for function */
54 #define REG_IRET TREG_EAX /* single word int return register */
55 #define REG_IRE2 TREG_EDX /* second word return register (for long long) */
56 #define REG_FRET TREG_ST0 /* float return register */
58 /* defined if function parameters must be evaluated in reverse order */
59 #define INVERT_FUNC_PARAMS
61 /* defined if structures are passed as pointers. Otherwise structures
62 are directly pushed on stack. */
63 /* #define FUNC_STRUCT_PARAM_AS_PTR */
65 /* pointer size, in bytes */
68 /* long double size and alignment, in bytes */
69 #define LDOUBLE_SIZE 12
70 #define LDOUBLE_ALIGN 4
71 /* maximum alignment (for aligned attribute support) */
74 /* define if return values need to be extended explicitely
75 at caller side (for interfacing with non-TCC compilers) */
78 /******************************************************/
79 #else /* ! TARGET_DEFS_ONLY */
80 /******************************************************/
84 ST_DATA
const char * const target_machine_defs
=
89 /* define to 1/0 to [not] have EBX as 4th register */
92 ST_DATA
const int reg_classes
[NB_REGS
] = {
93 /* eax */ RC_INT
| RC_EAX
,
94 /* ecx */ RC_INT
| RC_ECX
,
95 /* edx */ RC_INT
| RC_EDX
,
96 /* ebx */ (RC_INT
| RC_EBX
) * USE_EBX
,
97 /* st0 */ RC_FLOAT
| RC_ST0
,
100 static unsigned long func_sub_sp_offset
;
101 static int func_ret_sub
;
102 #ifdef CONFIG_TCC_BCHECK
103 static addr_t func_bound_offset
;
104 static unsigned long func_bound_ind
;
105 ST_DATA
int func_bound_add_epilog
;
106 static void gen_bounds_prolog(void);
107 static void gen_bounds_epilog(void);
110 /* XXX: make it faster ? */
111 ST_FUNC
void g(int c
)
117 if (ind1
> cur_text_section
->data_allocated
)
118 section_realloc(cur_text_section
, ind1
);
119 cur_text_section
->data
[ind
] = c
;
123 ST_FUNC
void o(unsigned int c
)
131 ST_FUNC
void gen_le16(int v
)
137 ST_FUNC
void gen_le32(int c
)
145 /* output a symbol and patch all calls to it */
146 ST_FUNC
void gsym_addr(int t
, int a
)
149 unsigned char *ptr
= cur_text_section
->data
+ t
;
150 uint32_t n
= read32le(ptr
); /* next value */
151 write32le(ptr
, a
- t
- 4);
156 /* instruction + 4 bytes data. Return the address of the data */
157 static int oad(int c
, int s
)
168 ST_FUNC
void gen_fill_nops(int bytes
)
174 /* generate jmp to a label */
175 #define gjmp2(instr,lbl) oad(instr,lbl)
177 /* output constant with relocation if 'r & VT_SYM' is true */
178 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
)
181 greloc(cur_text_section
, sym
, ind
, R_386_32
);
185 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
)
188 greloc(cur_text_section
, sym
, ind
, R_386_PC32
);
192 /* generate a modrm reference. 'op_reg' contains the additional 3
194 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
196 op_reg
= op_reg
<< 3;
197 if ((r
& VT_VALMASK
) == VT_CONST
) {
198 /* constant memory reference */
200 gen_addr32(r
, sym
, c
);
201 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
202 /* currently, we use only ebp as base */
204 /* short reference */
208 oad(0x85 | op_reg
, c
);
211 g(0x00 | op_reg
| (r
& VT_VALMASK
));
215 /* load 'r' from value 'sv' */
216 ST_FUNC
void load(int r
, SValue
*sv
)
218 int v
, t
, ft
, fc
, fr
;
223 sv
= pe_getimport(sv
, &v2
);
227 ft
= sv
->type
.t
& ~VT_DEFSIGN
;
230 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
234 if (v
== VT_LLOCAL
) {
236 v1
.r
= VT_LOCAL
| VT_LVAL
;
240 if (!(reg_classes
[fr
] & RC_INT
))
241 fr
= get_reg(RC_INT
);
244 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
247 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
250 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
253 } else if ((ft
& VT_TYPE
) == VT_BYTE
|| (ft
& VT_TYPE
) == VT_BOOL
) {
254 o(0xbe0f); /* movsbl */
255 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
256 o(0xb60f); /* movzbl */
257 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
258 o(0xbf0f); /* movswl */
259 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
260 o(0xb70f); /* movzwl */
264 gen_modrm(r
, fr
, sv
->sym
, fc
);
267 o(0xb8 + r
); /* mov $xx, r */
268 gen_addr32(fr
, sv
->sym
, fc
);
269 } else if (v
== VT_LOCAL
) {
271 o(0x8d); /* lea xxx(%ebp), r */
272 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
275 o(0xe8 + r
); /* mov %ebp, r */
277 } else if (v
== VT_CMP
) {
278 o(0x0f); /* setxx %br */
281 o(0xc0b60f + r
* 0x90000); /* movzbl %al, %eax */
282 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
284 oad(0xb8 + r
, t
); /* mov $1, r */
285 o(0x05eb); /* jmp after */
287 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
290 o(0xc0 + r
+ v
* 8); /* mov v, r */
295 /* store register 'r' in lvalue 'v' */
296 ST_FUNC
void store(int r
, SValue
*v
)
302 v
= pe_getimport(v
, &v2
);
307 fr
= v
->r
& VT_VALMASK
;
308 ft
&= ~(VT_VOLATILE
| VT_CONSTANT
);
310 /* XXX: incorrect if float reg to reg */
311 if (bt
== VT_FLOAT
) {
314 } else if (bt
== VT_DOUBLE
) {
317 } else if (bt
== VT_LDOUBLE
) {
318 o(0xc0d9); /* fld %st(0) */
324 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
329 if (fr
== VT_CONST
||
332 gen_modrm(r
, v
->r
, v
->sym
, fc
);
333 } else if (fr
!= r
) {
334 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
338 static void gadd_sp(int val
)
340 if (val
== (char)val
) {
344 oad(0xc481, val
); /* add $xxx, %esp */
348 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
349 static void gen_static_call(int v
)
353 sym
= external_helper_sym(v
);
355 greloc(cur_text_section
, sym
, ind
-4, R_386_PC32
);
359 /* 'is_jmp' is '1' if it is a jump */
360 static void gcall_or_jmp(int is_jmp
)
363 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
&& (vtop
->r
& VT_SYM
)) {
364 /* constant and relocation case */
365 greloc(cur_text_section
, vtop
->sym
, ind
+ 1, R_386_PC32
);
366 oad(0xe8 + is_jmp
, vtop
->c
.i
- 4); /* call/jmp im */
368 /* otherwise, indirect call */
370 o(0xff); /* call/jmp *r */
371 o(0xd0 + r
+ (is_jmp
<< 4));
375 static const uint8_t fastcall_regs
[3] = { TREG_EAX
, TREG_EDX
, TREG_ECX
};
376 static const uint8_t fastcallw_regs
[2] = { TREG_ECX
, TREG_EDX
};
378 /* Return the number of registers needed to return the struct, or 0 if
379 returning via struct pointer. */
380 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *ret_align
, int *regsize
)
382 #if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
383 int size
, align
, nregs
;
384 *ret_align
= 1; // Never have to re-align return values for x86
386 size
= type_size(vt
, &align
);
387 if (size
> 8 || (size
& (size
- 1)))
391 ret
->t
= VT_INT
, nregs
= 2;
401 *ret_align
= 1; // Never have to re-align return values for x86
406 /* Generate function call. The function address is pushed first, then
407 all the parameters in call order. This functions pops all the
408 parameters and the function address. */
409 ST_FUNC
void gfunc_call(int nb_args
)
411 int size
, align
, r
, args_size
, i
, func_call
;
413 // Look ahead to the function on the stack to get the function call type
414 int func_call2
= ((vtop
- nb_args
)->type
.ref
)->f
.func_call
;
416 #ifdef CONFIG_TCC_BCHECK
417 if (tcc_state
->do_bounds_check
)
418 gbound_args(nb_args
);
422 for(i
= 0;i
< nb_args
; i
++) {
423 if (func_call2
== FUNC_THISCALL
&& i
== (nb_args
- 1)) {
424 // If thiscall, zap the last push, as it is `this`. Instead, mov into ecx
426 load(get_reg(RC_ECX
), vtop
);
429 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
430 size
= type_size(&vtop
->type
, &align
);
431 /* align to stack align size */
432 size
= (size
+ 3) & ~3;
433 /* allocate the necessary size on stack */
437 oad(0x68, size
); // push size
438 /* cannot call normal 'alloca' with bound checking */
439 gen_static_call(tok_alloc_const("__alloca"));
444 oad(0xec81, size
); /* sub $xxx, %esp */
445 /* generate structure store */
447 o(0xe089 + (r
<< 8)); /* mov %esp, r */
449 vset(&vtop
->type
, r
| VT_LVAL
, 0);
453 } else if (is_float(vtop
->type
.t
)) {
454 gv(RC_FLOAT
); /* only one float register */
455 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
457 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
461 oad(0xec81, size
); /* sub $xxx, %esp */
465 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
470 /* simple type (currently always same size) */
471 /* XXX: implicit cast ? */
473 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
475 o(0x50 + vtop
->r2
); /* push r */
479 o(0x50 + r
); /* push r */
484 save_regs(0); /* save used temporary registers */
485 func_sym
= vtop
->type
.ref
;
486 func_call
= func_sym
->f
.func_call
;
488 if ((func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) ||
489 func_call
== FUNC_FASTCALLW
) {
490 int fastcall_nb_regs
;
491 const uint8_t *fastcall_regs_ptr
;
492 if (func_call
== FUNC_FASTCALLW
) {
493 fastcall_regs_ptr
= fastcallw_regs
;
494 fastcall_nb_regs
= 2;
496 fastcall_regs_ptr
= fastcall_regs
;
497 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
499 for(i
= 0;i
< fastcall_nb_regs
; i
++) {
502 o(0x58 + fastcall_regs_ptr
[i
]); /* pop r */
503 /* XXX: incorrect for struct/floats */
507 #if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
508 else if ((vtop
->type
.ref
->type
.t
& VT_BTYPE
) == VT_STRUCT
)
514 if (args_size
&& func_call
!= FUNC_STDCALL
&& func_call
!= FUNC_THISCALL
&& func_call
!= FUNC_FASTCALLW
)
520 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
522 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
525 /* generate function prolog of type 't' */
526 ST_FUNC
void gfunc_prolog(Sym
*func_sym
)
528 CType
*func_type
= &func_sym
->type
;
529 int addr
, align
, size
, func_call
, fastcall_nb_regs
;
530 int param_index
, param_addr
;
531 const uint8_t *fastcall_regs_ptr
;
534 int thiscall_nb_regs
;
536 sym
= func_type
->ref
;
537 func_call
= sym
->f
.func_call
;
542 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
543 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
544 fastcall_regs_ptr
= fastcall_regs
;
545 } else if (func_call
== FUNC_FASTCALLW
) {
546 fastcall_nb_regs
= 2;
547 fastcall_regs_ptr
= fastcallw_regs
;
549 fastcall_nb_regs
= 0;
550 fastcall_regs_ptr
= NULL
;
553 if (func_call
== FUNC_THISCALL
) {
554 thiscall_nb_regs
= 1;
559 ind
+= FUNC_PROLOG_SIZE
;
560 func_sub_sp_offset
= ind
;
561 /* if the function returns a structure, then add an
562 implicit pointer parameter */
563 #if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
564 size
= type_size(&func_vt
,&align
);
565 if (((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
)
566 && (size
> 8 || (size
& (size
- 1)))) {
568 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
570 /* XXX: fastcall case ? */
575 /* define parameters */
576 while ((sym
= sym
->next
) != NULL
) {
578 size
= type_size(type
, &align
);
579 size
= (size
+ 3) & ~3;
580 #ifdef FUNC_STRUCT_PARAM_AS_PTR
581 /* structs are passed as pointer */
582 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
586 if (param_index
< fastcall_nb_regs
) {
587 /* save FASTCALL register */
590 gen_modrm(fastcall_regs_ptr
[param_index
], VT_LOCAL
, NULL
, loc
);
593 else if(param_index
< thiscall_nb_regs
) {
595 /* save THISCALL register; ECX */
598 gen_modrm(TREG_ECX
, VT_LOCAL
, NULL
, loc
);
604 sym_push(sym
->v
& ~SYM_FIELD
, type
,
605 VT_LOCAL
| VT_LVAL
, param_addr
);
609 /* pascal type call or fastcall ? */
610 if (func_call
== FUNC_STDCALL
|| func_call
== FUNC_FASTCALLW
|| func_call
== FUNC_THISCALL
)
611 func_ret_sub
= addr
- 8;
612 #if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
617 #ifdef CONFIG_TCC_BCHECK
618 if (tcc_state
->do_bounds_check
)
623 /* generate function epilog */
624 ST_FUNC
void gfunc_epilog(void)
628 #ifdef CONFIG_TCC_BCHECK
629 if (tcc_state
->do_bounds_check
)
633 /* align local size to word & save local variables */
638 gen_modrm(TREG_EBX
, VT_LOCAL
, NULL
, -(v
+4));
642 if (func_ret_sub
== 0) {
647 g(func_ret_sub
>> 8);
650 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
653 oad(0xb8, v
); /* mov stacksize, %eax */
654 gen_static_call(TOK___chkstk
); /* call __chkstk, (does the stackframe too) */
658 o(0xe58955); /* push %ebp, mov %esp, %ebp */
659 o(0xec81); /* sub esp, stacksize */
662 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
665 o(0x53 * USE_EBX
); /* push ebx */
669 /* generate a jump to a label */
670 ST_FUNC
int gjmp(int t
)
672 return gjmp2(0xe9, t
);
675 /* generate a jump to a fixed address */
676 ST_FUNC
void gjmp_addr(int a
)
684 oad(0xe9, a
- ind
- 5);
689 /* generate a jump to a fixed address */
690 ST_FUNC
void gjmp_cond_addr(int a
, int op
)
696 g(0x0f), gjmp2(op
- 16, r
- 4);
700 ST_FUNC
int gjmp_append(int n
, int t
)
703 /* insert vtop->c jump list in t */
706 while ((n2
= read32le(p
= cur_text_section
->data
+ n1
)))
714 ST_FUNC
int gjmp_cond(int op
, int t
)
717 t
= gjmp2(op
- 16, t
);
721 ST_FUNC
void gen_opi(int op
)
727 case TOK_ADDC1
: /* add with carry generation */
730 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
737 /* generate inc and dec for smaller code */
738 if ((c
== 1 || c
== -1) && (op
== '+' || op
== '-')) {
739 opc
= (c
== 1) ^ (op
== '+');
740 o (0x40 | (opc
<< 3) | r
); // inc,dec
743 o(0xc0 | (opc
<< 3) | r
);
748 oad(0xc0 | (opc
<< 3) | r
, c
);
754 o((opc
<< 3) | 0x01);
755 o(0xc0 + r
+ fr
* 8);
758 if (op
>= TOK_ULT
&& op
<= TOK_GT
)
762 case TOK_SUBC1
: /* sub with carry generation */
765 case TOK_ADDC2
: /* add with carry use */
768 case TOK_SUBC2
: /* sub with carry use */
785 o(0xaf0f); /* imul fr, r */
786 o(0xc0 + fr
+ r
* 8);
797 opc
= 0xc0 | (opc
<< 3);
798 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
803 c
= vtop
->c
.i
& 0x1f;
804 o(0xc1); /* shl/shr/sar $xxx, r */
808 /* we generate the shift in ecx */
811 o(0xd3); /* shl/shr/sar %cl, r */
822 /* first operand must be in eax */
823 /* XXX: need better constraint for second operand */
829 /* save EAX too if used otherwise */
830 save_reg_upstack(TREG_EAX
, 1);
831 if (op
== TOK_UMULL
) {
832 o(0xf7); /* mul fr */
837 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
838 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
841 o(0xf799); /* cltd, idiv fr, %eax */
844 if (op
== '%' || op
== TOK_UMOD
)
857 /* generate a floating point operation 'v = t1 op t2' instruction. The
858 two operands are guaranteed to have the same floating point type */
859 /* XXX: need to use ST1 too */
860 ST_FUNC
void gen_opf(int op
)
862 int a
, ft
, fc
, swapped
, r
;
864 if (op
== TOK_NEG
) { /* unary minus */
866 o(0xe0d9); /* fchs */
870 /* convert constants to memory references */
871 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
876 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
879 /* must put at least one value in the floating point register */
880 if ((vtop
[-1].r
& VT_LVAL
) &&
881 (vtop
[0].r
& VT_LVAL
)) {
887 /* swap the stack if needed so that t1 is the register and t2 is
888 the memory reference */
889 if (vtop
[-1].r
& VT_LVAL
) {
893 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
894 /* load on stack second operand */
895 load(TREG_ST0
, vtop
);
896 save_reg(TREG_EAX
); /* eax is used by FP comparison code */
897 if (op
== TOK_GE
|| op
== TOK_GT
)
899 else if (op
== TOK_EQ
|| op
== TOK_NE
)
902 o(0xc9d9); /* fxch %st(1) */
903 if (op
== TOK_EQ
|| op
== TOK_NE
)
904 o(0xe9da); /* fucompp */
906 o(0xd9de); /* fcompp */
907 o(0xe0df); /* fnstsw %ax */
909 o(0x45e480); /* and $0x45, %ah */
910 o(0x40fC80); /* cmp $0x40, %ah */
911 } else if (op
== TOK_NE
) {
912 o(0x45e480); /* and $0x45, %ah */
913 o(0x40f480); /* xor $0x40, %ah */
915 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
916 o(0x05c4f6); /* test $0x05, %ah */
919 o(0x45c4f6); /* test $0x45, %ah */
925 /* no memory reference possible for long double operations */
926 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
927 load(TREG_ST0
, vtop
);
952 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
953 o(0xde); /* fxxxp %st, %st(1) */
956 /* if saved lvalue, then we must reload it */
958 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
962 v1
.r
= VT_LOCAL
| VT_LVAL
;
969 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
973 gen_modrm(a
, r
, vtop
->sym
, fc
);
979 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
980 and 'long long' cases. */
981 ST_FUNC
void gen_cvt_itof(int t
)
985 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
986 /* signed long long to float/double/long double (unsigned case
987 is handled generically) */
988 o(0x50 + vtop
->r2
); /* push r2 */
989 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
990 o(0x242cdf); /* fildll (%esp) */
991 o(0x08c483); /* add $8, %esp */
993 } else if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
994 (VT_INT
| VT_UNSIGNED
)) {
995 /* unsigned int to float/double/long double */
996 o(0x6a); /* push $0 */
998 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
999 o(0x242cdf); /* fildll (%esp) */
1000 o(0x08c483); /* add $8, %esp */
1002 /* int to float/double/long double */
1003 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
1004 o(0x2404db); /* fildl (%esp) */
1005 o(0x04c483); /* add $4, %esp */
1007 vtop
->r2
= VT_CONST
;
1011 /* convert fp to int 't' type */
1012 ST_FUNC
void gen_cvt_ftoi(int t
)
1014 int bt
= vtop
->type
.t
& VT_BTYPE
;
1016 vpush_helper_func(TOK___fixsfdi
);
1017 else if (bt
== VT_LDOUBLE
)
1018 vpush_helper_func(TOK___fixxfdi
);
1020 vpush_helper_func(TOK___fixdfdi
);
1025 if ((t
& VT_BTYPE
) == VT_LLONG
)
1026 vtop
->r2
= REG_IRE2
;
1029 /* convert from one floating point type to another */
1030 ST_FUNC
void gen_cvt_ftof(int t
)
1032 /* all we have to do on i386 is to put the float in a register */
1036 /* char/short to int conversion */
1037 ST_FUNC
void gen_cvt_csti(int t
)
1041 sz
= !(t
& VT_UNSIGNED
);
1042 xl
= (t
& VT_BTYPE
) == VT_SHORT
;
1043 o(0xc0b60f /* mov[sz] %a[xl], %eax */
1044 | (sz
<< 3 | xl
) << 8
1045 | (r
<< 3 | r
) << 16
1049 /* increment tcov counter */
1050 ST_FUNC
void gen_increment_tcov (SValue
*sv
)
1052 o(0x0583); /* addl $1, xxx */
1053 greloc(cur_text_section
, sv
->sym
, ind
, R_386_32
);
1056 o(0x1583); /* addcl $0, xxx */
1057 greloc(cur_text_section
, sv
->sym
, ind
, R_386_32
);
1062 /* computed goto support */
1063 ST_FUNC
void ggoto(void)
1069 /* bound check support functions */
1070 #ifdef CONFIG_TCC_BCHECK
1072 static void gen_bounds_prolog(void)
1074 /* leave some room for bound checking code */
1075 func_bound_offset
= lbounds_section
->data_offset
;
1076 func_bound_ind
= ind
;
1077 func_bound_add_epilog
= 0;
1078 oad(0xb8, 0); /* lbound section pointer */
1079 oad(0xb8, 0); /* call to function */
1082 static void gen_bounds_epilog(void)
1087 int offset_modified
= func_bound_offset
!= lbounds_section
->data_offset
;
1089 if (!offset_modified
&& !func_bound_add_epilog
)
1092 /* add end of table info */
1093 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(addr_t
));
1096 sym_data
= get_sym_ref(&char_pointer_type
, lbounds_section
,
1097 func_bound_offset
, PTR_SIZE
);
1099 /* generate bound local allocation */
1100 if (offset_modified
) {
1102 ind
= func_bound_ind
;
1103 greloc(cur_text_section
, sym_data
, ind
+ 1, R_386_32
);
1105 gen_static_call(TOK___bound_local_new
);
1109 /* generate bound check local freeing */
1110 o(0x5250); /* save returned value, if any */
1111 greloc(cur_text_section
, sym_data
, ind
+ 1, R_386_32
);
1112 oad(0xb8, 0); /* mov %eax, xxx */
1113 gen_static_call(TOK___bound_local_delete
);
1114 o(0x585a); /* restore returned value, if any */
1118 /* Save the stack pointer onto the stack */
1119 ST_FUNC
void gen_vla_sp_save(int addr
) {
1120 /* mov %esp,addr(%ebp)*/
1122 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1125 /* Restore the SP from a location on the stack */
1126 ST_FUNC
void gen_vla_sp_restore(int addr
) {
1128 gen_modrm(TREG_ESP
, VT_LOCAL
, NULL
, addr
);
1131 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1132 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
) {
1135 #if defined(CONFIG_TCC_BCHECK)
1136 use_call
= tcc_state
->do_bounds_check
;
1138 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
1143 vpush_helper_func(TOK_alloca
);
1144 vswap(); /* Move alloca ref past allocation size */
1149 r
= gv(RC_INT
); /* allocation size */
1153 /* We align to 16 bytes rather than align */
1160 /* end of X86 code generator */
1161 /*************************************************************/
1163 /*************************************************************/