2 * X86 code generator for TCC
4 * Copyright (c) 2001 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* number of available registers */
24 /* a register can belong to several classes. The classes must be
25 sorted from more general to more precise (see gv2() code which does
26 assumptions on it). */
27 #define RC_INT 0x0001 /* generic integer register */
28 #define RC_FLOAT 0x0002 /* generic float register */
33 #define RC_IRET RC_EAX /* function return: integer register */
34 #define RC_LRET RC_EDX /* function return: second integer register */
35 #define RC_FRET RC_ST0 /* function return: float register */
37 /* pretty names for the registers */
45 int reg_classes
[NB_REGS
] = {
46 /* eax */ RC_INT
| RC_EAX
,
47 /* ecx */ RC_INT
| RC_ECX
,
48 /* edx */ RC_INT
| RC_EDX
,
49 /* st0 */ RC_FLOAT
| RC_ST0
,
52 /* return registers for function */
53 #define REG_IRET REG_EAX /* single word int return register */
54 #define REG_LRET REG_EDX /* second word return register (for long long) */
55 #define REG_FRET REG_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
71 /* function call context */
72 typedef struct GFuncContext
{
74 int func_call
; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
77 /******************************************************/
79 static int *func_sub_sp_ptr
;
80 static unsigned char *func_bound_ptr
;
81 static int func_ret_sub
;
104 /* patch relocation entry with value 'val' */
105 void greloc_patch1(Reloc
*p
, int val
)
109 *(int *)p
->addr
= val
;
112 *(int *)p
->addr
= val
- p
->addr
- 4;
117 /* output a symbol and patch all calls to it */
122 n
= *(int *)t
; /* next value */
123 *(int *)t
= a
- t
- 4;
133 /* psym is used to put an instruction with a data field which is a
134 reference to a symbol. It is in fact the same as oad ! */
137 /* instruction + 4 bytes data. Return the address of the data */
138 int oad(int c
, int s
)
147 /* output constant with relocation if 'r & VT_FORWARD' is true */
148 void gen_addr32(int r
, int c
)
150 if (!(r
& VT_FORWARD
)) {
153 greloc((Sym
*)c
, ind
, RELOC_ADDR32
);
158 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
160 void gen_modrm(int op_reg
, int r
, int c
)
162 op_reg
= op_reg
<< 3;
163 if ((r
& VT_VALMASK
) == VT_CONST
) {
164 /* constant memory reference */
167 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
168 /* currently, we use only ebp as base */
170 /* short reference */
174 oad(0x85 | op_reg
, c
);
177 g(0x00 | op_reg
| (r
& VT_VALMASK
));
182 /* load 'r' from value 'sv' */
183 void load(int r
, SValue
*sv
)
185 int v
, t
, ft
, fc
, fr
;
194 if (v
== VT_LLOCAL
) {
196 v1
.r
= VT_LOCAL
| VT_LVAL
;
201 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
204 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
207 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
210 } else if ((ft
& VT_TYPE
) == VT_BYTE
)
211 o(0xbe0f); /* movsbl */
212 else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
))
213 o(0xb60f); /* movzbl */
214 else if ((ft
& VT_TYPE
) == VT_SHORT
)
215 o(0xbf0f); /* movswl */
216 else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
))
217 o(0xb70f); /* movzwl */
220 gen_modrm(r
, fr
, fc
);
223 o(0xb8 + r
); /* mov $xx, r */
225 } else if (v
== VT_LOCAL
) {
226 o(0x8d); /* lea xxx(%ebp), r */
227 gen_modrm(r
, VT_LOCAL
, fc
);
228 } else if (v
== VT_CMP
) {
229 oad(0xb8 + r
, 0); /* mov $0, r */
230 o(0x0f); /* setxx %br */
233 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
235 oad(0xb8 + r
, t
); /* mov $1, r */
236 oad(0xe9, 5); /* jmp after */
238 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
241 o(0xc0 + r
+ v
* 8); /* mov v, r */
246 /* store register 'r' in lvalue 'v' */
247 void store(int r
, SValue
*v
)
253 fr
= v
->r
& VT_VALMASK
;
255 /* XXX: incorrect if float reg to reg */
256 if (bt
== VT_FLOAT
) {
259 } else if (bt
== VT_DOUBLE
) {
262 } else if (bt
== VT_LDOUBLE
) {
263 o(0xc0d9); /* fld %st(0) */
274 if (fr
== VT_CONST
||
277 gen_modrm(r
, v
->r
, fc
);
278 } else if (fr
!= r
) {
279 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
283 /* start function call and return function call context */
284 void gfunc_start(GFuncContext
*c
, int func_call
)
287 c
->func_call
= func_call
;
290 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
292 void gfunc_param(GFuncContext
*c
)
296 if ((vtop
->t
& VT_BTYPE
) == VT_STRUCT
) {
297 size
= type_size(vtop
->t
, &align
);
298 /* align to stack align size */
299 size
= (size
+ 3) & ~3;
300 /* allocate the necessary size on stack */
301 oad(0xec81, size
); /* sub $xxx, %esp */
302 /* generate structure store */
304 o(0x89); /* mov %esp, r */
309 c
->args_size
+= size
;
310 } else if (is_float(vtop
->t
)) {
311 gv(RC_FLOAT
); /* only one float register */
312 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
314 else if ((vtop
->t
& VT_BTYPE
) == VT_DOUBLE
)
318 oad(0xec81, size
); /* sub $xxx, %esp */
322 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
325 c
->args_size
+= size
;
327 /* simple type (currently always same size) */
328 /* XXX: implicit cast ? */
330 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
332 o(0x50 + vtop
->r2
); /* push r */
336 o(0x50 + r
); /* push r */
337 c
->args_size
+= size
;
342 /* generate function call with address in (vtop->t, vtop->c) and free function
343 context. Stack entry is popped */
344 void gfunc_call(GFuncContext
*c
)
347 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
349 /* forward reference */
350 if (vtop
->r
& VT_FORWARD
) {
351 greloc(vtop
->c
.sym
, ind
+ 1, RELOC_REL32
);
354 oad(0xe8, vtop
->c
.ul
- ind
- 5);
357 /* otherwise, indirect call */
359 o(0xff); /* call *r */
362 if (c
->args_size
&& c
->func_call
== FUNC_CDECL
)
363 oad(0xc481, c
->args_size
); /* add $xxx, %esp */
367 /* generate function prolog of type 't' */
368 void gfunc_prolog(int t
)
370 int addr
, align
, size
, u
, func_call
;
373 sym
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
376 /* if the function returns a structure, then add an
377 implicit pointer parameter */
379 if ((func_vt
& VT_BTYPE
) == VT_STRUCT
) {
383 /* define parameters */
384 while ((sym
= sym
->next
) != NULL
) {
386 sym_push(sym
->v
& ~SYM_FIELD
, u
,
387 VT_LOCAL
| VT_LVAL
, addr
);
388 size
= type_size(u
, &align
);
389 size
= (size
+ 3) & ~3;
390 #ifdef FUNC_STRUCT_PARAM_AS_PTR
391 /* structs are passed as pointer */
392 if ((u
& VT_BTYPE
) == VT_STRUCT
) {
399 /* pascal type call ? */
400 if (func_call
== FUNC_STDCALL
)
401 func_ret_sub
= addr
- 8;
402 o(0xe58955); /* push %ebp, mov %esp, %ebp */
403 func_sub_sp_ptr
= (int *)oad(0xec81, 0); /* sub $xxx, %esp */
404 /* leave some room for bound checking code */
405 if (do_bounds_check
) {
406 oad(0xb8, 0); /* lbound section pointer */
407 oad(0xb8, 0); /* call to function */
408 func_bound_ptr
= lbounds_section
->data_ptr
;
412 /* generate function epilog */
413 void gfunc_epilog(void)
415 #ifdef CONFIG_TCC_BCHECK
416 if (do_bounds_check
&& func_bound_ptr
!= lbounds_section
->data_ptr
) {
419 /* add end of table info */
420 bounds_ptr
= (int *)lbounds_section
->data_ptr
;
422 lbounds_section
->data_ptr
= (unsigned char *)bounds_ptr
;
423 /* generate bound local allocation */
425 ind
= (int)func_sub_sp_ptr
+ 4;
426 oad(0xb8, (int)func_bound_ptr
); /* mov %eax, xxx */
427 oad(0xe8, (int)__bound_local_new
- ind
- 5);
429 /* generate bound check local freeing */
430 o(0x5250); /* save returned value, if any */
431 oad(0xb8, (int)func_bound_ptr
); /* mov %eax, xxx */
432 oad(0xe8, (int)__bound_local_delete
- ind
- 5);
433 o(0x585a); /* restore returned value, if any */
437 if (func_ret_sub
== 0) {
442 g(func_ret_sub
>> 8);
444 /* align local size to word & save local variables */
445 *func_sub_sp_ptr
= (-loc
+ 3) & -4;
448 /* generate a jump to a label */
451 return psym(0xe9, t
);
454 /* generate a jump to a fixed address */
455 void gjmp_addr(int a
)
457 oad(0xe9, a
- ind
- 5);
460 /* generate a test. set 'inv' to invert test. Stack entry is popped */
461 int gtst(int inv
, int t
)
464 v
= vtop
->r
& VT_VALMASK
;
466 /* fast case : can jump directly since flags are set */
468 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
469 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
470 /* && or || optimization */
471 if ((v
& 1) == inv
) {
472 /* insert vtop->c jump list in t */
483 if (is_float(vtop
->t
)) {
487 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
488 /* constant jmp optimization */
489 if ((vtop
->c
.i
!= 0) != inv
)
496 t
= psym(0x85 ^ inv
, t
);
503 /* generate an integer binary operation */
510 case TOK_ADDC1
: /* add with carry generation */
513 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
520 /* XXX: generate inc and dec for smaller code ? */
522 o(0xc0 | (opc
<< 3) | r
);
526 oad(0xc0 | (opc
<< 3) | r
, c
);
532 o((opc
<< 3) | 0x01);
533 o(0xc0 + r
+ fr
* 8);
536 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
538 vset(VT_INT
, VT_CMP
, op
);
542 case TOK_SUBC1
: /* sub with carry generation */
545 case TOK_ADDC2
: /* add with carry use */
548 case TOK_SUBC2
: /* sub with carry use */
565 o(0xaf0f); /* imul fr, r */
566 o(0xc0 + fr
+ r
* 8);
577 opc
= 0xc0 | (opc
<< 3);
578 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
583 c
= vtop
->c
.i
& 0x1f;
584 o(0xc1); /* shl/shr/sar $xxx, r */
588 /* we generate the shift in ecx */
591 o(0xd3); /* shl/shr/sar %cl, r */
602 /* first operand must be in eax */
603 /* XXX: need better constraint for second operand */
609 if (op
== TOK_UMULL
) {
610 o(0xf7); /* mul fr */
615 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
616 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
619 o(0xf799); /* cltd, idiv fr, %eax */
622 if (op
== '%' || op
== TOK_UMOD
)
635 /* generate a floating point operation 'v = t1 op t2' instruction. The
636 two operands are guaranted to have the same floating point type */
637 /* XXX: need to use ST1 too */
640 int a
, ft
, fc
, swapped
, r
;
642 /* convert constants to memory references */
643 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
648 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
651 /* must put at least one value in the floating point register */
652 if ((vtop
[-1].r
& VT_LVAL
) &&
653 (vtop
[0].r
& VT_LVAL
)) {
658 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
659 /* load on stack second operand */
661 save_reg(REG_EAX
); /* eax is used by FP comparison code */
662 if (op
== TOK_GE
|| op
== TOK_GT
)
663 o(0xc9d9); /* fxch %st(1) */
664 o(0xe9da); /* fucompp */
665 o(0xe0df); /* fnstsw %ax */
667 o(0x45e480); /* and $0x45, %ah */
668 o(0x40fC80); /* cmp $0x40, %ah */
669 } else if (op
== TOK_NE
) {
670 o(0x45e480); /* and $0x45, %ah */
671 o(0x40f480); /* xor $0x40, %ah */
673 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
674 o(0x05c4f6); /* test $0x05, %ah */
677 o(0x45c4f6); /* test $0x45, %ah */
685 /* swap the stack if needed so that t1 is the register and t2 is
686 the memory reference */
687 if (vtop
[-1].r
& VT_LVAL
) {
691 /* no memory reference possible for long double operations */
692 if ((vtop
->t
& VT_BTYPE
) == VT_LDOUBLE
) {
718 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
719 o(0xde); /* fxxxp %st, %st(1) */
722 /* if saved lvalue, then we must reload it */
724 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
728 v1
.r
= VT_LOCAL
| VT_LVAL
;
734 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
744 /* FPU control word for rounding to nearest mode */
745 /* XXX: should move that into tcc lib support code ! */
746 static unsigned short __tcc_fpu_control
= 0x137f;
747 /* FPU control word for round to zero mode for int convertion */
748 static unsigned short __tcc_int_fpu_control
= 0x137f | 0x0c00;
750 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
751 and 'long long' cases. */
752 void gen_cvt_itof(int t
)
756 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
757 /* signed long long to float/double/long double (unsigned case
758 is handled generically) */
759 o(0x50 + vtop
->r2
); /* push r2 */
760 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
761 o(0x242cdf); /* fildll (%esp) */
762 o(0x08c483); /* add $8, %esp */
763 } else if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
764 (VT_INT
| VT_UNSIGNED
)) {
765 /* unsigned int to float/double/long double */
766 o(0x6a); /* push $0 */
768 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
769 o(0x242cdf); /* fildll (%esp) */
770 o(0x08c483); /* add $8, %esp */
772 /* int to float/double/long double */
773 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
774 o(0x2404db); /* fildl (%esp) */
775 o(0x04c483); /* add $4, %esp */
780 /* convert fp to int 't' type */
781 /* XXX: handle long long case */
782 void gen_cvt_ftoi(int t
)
792 oad(0x2dd9, (int)&__tcc_int_fpu_control
); /* ldcw xxx */
793 oad(0xec81, size
); /* sub $xxx, %esp */
795 o(0x1cdb); /* fistpl */
797 o(0x3cdf); /* fistpll */
799 oad(0x2dd9, (int)&__tcc_fpu_control
); /* ldcw xxx */
801 o(0x58 + r
); /* pop r */
804 vtop
->r
= r
; /* mark reg as used */
805 r2
= get_reg(RC_INT
);
806 o(0x58 + r2
); /* pop r2 */
809 o(0x04c483); /* add $4, %esp */
815 /* convert from one floating point type to another */
816 void gen_cvt_ftof(int t
)
818 /* all we have to do on i386 is to put the float in a register */
822 /* bound check support functions */
823 #ifdef CONFIG_TCC_BCHECK
824 /* generate first part of bounded pointer addition */
825 void gen_bounded_ptr_add1(void)
827 /* prepare fast i386 function call (args in eax and edx) */
829 /* save all temporary registers */
835 /* if deref is true, then also test dereferencing */
836 void gen_bounded_ptr_add2(int deref
)
842 size
= type_size(vtop
->t
, &align
);
844 case 1: func
= __bound_ptr_indir1
; break;
845 case 2: func
= __bound_ptr_indir2
; break;
846 case 4: func
= __bound_ptr_indir4
; break;
847 case 8: func
= __bound_ptr_indir8
; break;
848 case 12: func
= __bound_ptr_indir12
; break;
849 case 16: func
= __bound_ptr_indir16
; break;
851 error("unhandled size when derefencing bounded pointer");
856 func
= __bound_ptr_add
;
859 /* do a fast function call */
860 oad(0xe8, (int)func
- ind
- 5);
861 /* return pointer is there */
866 /* end of X86 code generator */
867 /*************************************************************/