2 * X86 code generator for TCC
4 * Copyright (c) 2001, 2002 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 /* relocation type for 32 bit data relocation */
72 #define R_DATA_32 R_386_32
74 /* function call context */
75 typedef struct GFuncContext
{
77 int func_call
; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
80 /******************************************************/
82 static int *func_sub_sp_ptr
;
83 static unsigned char *func_bound_ptr
;
84 static int func_ret_sub
;
107 /* output a symbol and patch all calls to it */
108 void gsym_addr(int t
, int a
)
112 n
= *(int *)t
; /* next value */
113 *(int *)t
= a
- t
- 4;
123 /* psym is used to put an instruction with a data field which is a
124 reference to a symbol. It is in fact the same as oad ! */
127 /* instruction + 4 bytes data. Return the address of the data */
128 int oad(int c
, int s
)
137 /* output constant with relocation if 'r & VT_SYM' is true */
138 void gen_addr32(int r
, int c
)
143 greloc(cur_text_section
,
144 (Sym
*)c
, ind
- (int)cur_text_section
->data
, R_386_32
);
149 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
151 void gen_modrm(int op_reg
, int r
, int c
)
153 op_reg
= op_reg
<< 3;
154 if ((r
& VT_VALMASK
) == VT_CONST
) {
155 /* constant memory reference */
158 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
159 /* currently, we use only ebp as base */
161 /* short reference */
165 oad(0x85 | op_reg
, c
);
168 g(0x00 | op_reg
| (r
& VT_VALMASK
));
173 /* load 'r' from value 'sv' */
174 void load(int r
, SValue
*sv
)
176 int v
, t
, ft
, fc
, fr
;
185 if (v
== VT_LLOCAL
) {
187 v1
.r
= VT_LOCAL
| VT_LVAL
;
192 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
195 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
198 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
201 } else if ((ft
& VT_TYPE
) == VT_BYTE
) {
202 o(0xbe0f); /* movsbl */
203 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
204 o(0xb60f); /* movzbl */
205 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
206 o(0xbf0f); /* movswl */
207 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
208 o(0xb70f); /* movzwl */
212 gen_modrm(r
, fr
, fc
);
215 o(0xb8 + r
); /* mov $xx, r */
217 } else if (v
== VT_LOCAL
) {
218 o(0x8d); /* lea xxx(%ebp), r */
219 gen_modrm(r
, VT_LOCAL
, fc
);
220 } else if (v
== VT_CMP
) {
221 oad(0xb8 + r
, 0); /* mov $0, r */
222 o(0x0f); /* setxx %br */
225 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
227 oad(0xb8 + r
, t
); /* mov $1, r */
228 oad(0xe9, 5); /* jmp after */
230 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
233 o(0xc0 + r
+ v
* 8); /* mov v, r */
238 /* store register 'r' in lvalue 'v' */
239 void store(int r
, SValue
*v
)
245 fr
= v
->r
& VT_VALMASK
;
247 /* XXX: incorrect if float reg to reg */
248 if (bt
== VT_FLOAT
) {
251 } else if (bt
== VT_DOUBLE
) {
254 } else if (bt
== VT_LDOUBLE
) {
255 o(0xc0d9); /* fld %st(0) */
266 if (fr
== VT_CONST
||
269 gen_modrm(r
, v
->r
, fc
);
270 } else if (fr
!= r
) {
271 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
275 /* start function call and return function call context */
276 void gfunc_start(GFuncContext
*c
, int func_call
)
279 c
->func_call
= func_call
;
282 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
284 void gfunc_param(GFuncContext
*c
)
288 if ((vtop
->t
& VT_BTYPE
) == VT_STRUCT
) {
289 size
= type_size(vtop
->t
, &align
);
290 /* align to stack align size */
291 size
= (size
+ 3) & ~3;
292 /* allocate the necessary size on stack */
293 oad(0xec81, size
); /* sub $xxx, %esp */
294 /* generate structure store */
296 o(0x89); /* mov %esp, r */
298 vset(vtop
->t
, r
| VT_LVAL
, 0);
301 c
->args_size
+= size
;
302 } else if (is_float(vtop
->t
)) {
303 gv(RC_FLOAT
); /* only one float register */
304 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
306 else if ((vtop
->t
& VT_BTYPE
) == VT_DOUBLE
)
310 oad(0xec81, size
); /* sub $xxx, %esp */
314 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
317 c
->args_size
+= size
;
319 /* simple type (currently always same size) */
320 /* XXX: implicit cast ? */
322 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
324 o(0x50 + vtop
->r2
); /* push r */
328 o(0x50 + r
); /* push r */
329 c
->args_size
+= size
;
334 /* generate function call with address in (vtop->t, vtop->c) and free function
335 context. Stack entry is popped */
336 void gfunc_call(GFuncContext
*c
)
339 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
341 if (vtop
->r
& VT_SYM
) {
342 /* relocation case */
343 greloc(cur_text_section
, vtop
->c
.sym
,
344 ind
+ 1 - (int)cur_text_section
->data
, R_386_PC32
);
347 oad(0xe8, vtop
->c
.ul
- ind
- 5);
350 /* otherwise, indirect call */
352 o(0xff); /* call *r */
355 if (c
->args_size
&& c
->func_call
== FUNC_CDECL
)
356 oad(0xc481, c
->args_size
); /* add $xxx, %esp */
360 /* generate function prolog of type 't' */
361 void gfunc_prolog(int t
)
363 int addr
, align
, size
, u
, func_call
;
366 sym
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
369 /* if the function returns a structure, then add an
370 implicit pointer parameter */
372 if ((func_vt
& VT_BTYPE
) == VT_STRUCT
) {
376 /* define parameters */
377 while ((sym
= sym
->next
) != NULL
) {
379 sym_push(sym
->v
& ~SYM_FIELD
, u
,
380 VT_LOCAL
| VT_LVAL
, addr
);
381 size
= type_size(u
, &align
);
382 size
= (size
+ 3) & ~3;
383 #ifdef FUNC_STRUCT_PARAM_AS_PTR
384 /* structs are passed as pointer */
385 if ((u
& VT_BTYPE
) == VT_STRUCT
) {
392 /* pascal type call ? */
393 if (func_call
== FUNC_STDCALL
)
394 func_ret_sub
= addr
- 8;
395 o(0xe58955); /* push %ebp, mov %esp, %ebp */
396 func_sub_sp_ptr
= (int *)oad(0xec81, 0); /* sub $xxx, %esp */
397 /* leave some room for bound checking code */
398 if (do_bounds_check
) {
399 oad(0xb8, 0); /* lbound section pointer */
400 oad(0xb8, 0); /* call to function */
401 func_bound_ptr
= lbounds_section
->data_ptr
;
405 /* generate function epilog */
406 void gfunc_epilog(void)
408 #ifdef CONFIG_TCC_BCHECK
409 if (do_bounds_check
&& func_bound_ptr
!= lbounds_section
->data_ptr
) {
412 /* add end of table info */
413 bounds_ptr
= (int *)lbounds_section
->data_ptr
;
415 lbounds_section
->data_ptr
= (unsigned char *)bounds_ptr
;
416 /* generate bound local allocation */
418 ind
= (int)func_sub_sp_ptr
+ 4;
419 oad(0xb8, (int)func_bound_ptr
); /* mov %eax, xxx */
420 oad(0xe8, (int)__bound_local_new
- ind
- 5);
422 /* generate bound check local freeing */
423 o(0x5250); /* save returned value, if any */
424 oad(0xb8, (int)func_bound_ptr
); /* mov %eax, xxx */
425 oad(0xe8, (int)__bound_local_delete
- ind
- 5);
426 o(0x585a); /* restore returned value, if any */
430 if (func_ret_sub
== 0) {
435 g(func_ret_sub
>> 8);
437 /* align local size to word & save local variables */
438 *func_sub_sp_ptr
= (-loc
+ 3) & -4;
441 /* generate a jump to a label */
444 return psym(0xe9, t
);
447 /* generate a jump to a fixed address */
448 void gjmp_addr(int a
)
450 oad(0xe9, a
- ind
- 5);
453 /* generate a test. set 'inv' to invert test. Stack entry is popped */
454 int gtst(int inv
, int t
)
457 v
= vtop
->r
& VT_VALMASK
;
459 /* fast case : can jump directly since flags are set */
461 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
462 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
463 /* && or || optimization */
464 if ((v
& 1) == inv
) {
465 /* insert vtop->c jump list in t */
476 if (is_float(vtop
->t
)) {
480 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
481 /* constant jmp optimization */
482 if ((vtop
->c
.i
!= 0) != inv
)
489 t
= psym(0x85 ^ inv
, t
);
496 /* generate an integer binary operation */
503 case TOK_ADDC1
: /* add with carry generation */
506 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
513 /* XXX: generate inc and dec for smaller code ? */
515 o(0xc0 | (opc
<< 3) | r
);
519 oad(0xc0 | (opc
<< 3) | r
, c
);
525 o((opc
<< 3) | 0x01);
526 o(0xc0 + r
+ fr
* 8);
529 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
531 vset(VT_INT
, VT_CMP
, op
);
535 case TOK_SUBC1
: /* sub with carry generation */
538 case TOK_ADDC2
: /* add with carry use */
541 case TOK_SUBC2
: /* sub with carry use */
558 o(0xaf0f); /* imul fr, r */
559 o(0xc0 + fr
+ r
* 8);
570 opc
= 0xc0 | (opc
<< 3);
571 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
576 c
= vtop
->c
.i
& 0x1f;
577 o(0xc1); /* shl/shr/sar $xxx, r */
581 /* we generate the shift in ecx */
584 o(0xd3); /* shl/shr/sar %cl, r */
595 /* first operand must be in eax */
596 /* XXX: need better constraint for second operand */
602 if (op
== TOK_UMULL
) {
603 o(0xf7); /* mul fr */
608 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
609 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
612 o(0xf799); /* cltd, idiv fr, %eax */
615 if (op
== '%' || op
== TOK_UMOD
)
628 /* generate a floating point operation 'v = t1 op t2' instruction. The
629 two operands are guaranted to have the same floating point type */
630 /* XXX: need to use ST1 too */
633 int a
, ft
, fc
, swapped
, r
;
635 /* convert constants to memory references */
636 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
641 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
644 /* must put at least one value in the floating point register */
645 if ((vtop
[-1].r
& VT_LVAL
) &&
646 (vtop
[0].r
& VT_LVAL
)) {
652 /* swap the stack if needed so that t1 is the register and t2 is
653 the memory reference */
654 if (vtop
[-1].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
)
664 else if (op
== TOK_EQ
|| op
== TOK_NE
)
667 o(0xc9d9); /* fxch %st(1) */
668 o(0xe9da); /* fucompp */
669 o(0xe0df); /* fnstsw %ax */
671 o(0x45e480); /* and $0x45, %ah */
672 o(0x40fC80); /* cmp $0x40, %ah */
673 } else if (op
== TOK_NE
) {
674 o(0x45e480); /* and $0x45, %ah */
675 o(0x40f480); /* xor $0x40, %ah */
677 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
678 o(0x05c4f6); /* test $0x05, %ah */
681 o(0x45c4f6); /* test $0x45, %ah */
688 /* no memory reference possible for long double operations */
689 if ((vtop
->t
& VT_BTYPE
) == VT_LDOUBLE
) {
715 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
716 o(0xde); /* fxxxp %st, %st(1) */
719 /* if saved lvalue, then we must reload it */
721 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
725 v1
.r
= VT_LOCAL
| VT_LVAL
;
731 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
741 /* FPU control word for rounding to nearest mode */
742 /* XXX: should move that into tcc lib support code ! */
743 static unsigned short __tcc_fpu_control
= 0x137f;
744 /* FPU control word for round to zero mode for int convertion */
745 static unsigned short __tcc_int_fpu_control
= 0x137f | 0x0c00;
747 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
748 and 'long long' cases. */
749 void gen_cvt_itof(int t
)
753 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
754 /* signed long long to float/double/long double (unsigned case
755 is handled generically) */
756 o(0x50 + vtop
->r2
); /* push r2 */
757 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
758 o(0x242cdf); /* fildll (%esp) */
759 o(0x08c483); /* add $8, %esp */
760 } else if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
761 (VT_INT
| VT_UNSIGNED
)) {
762 /* unsigned int to float/double/long double */
763 o(0x6a); /* push $0 */
765 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
766 o(0x242cdf); /* fildll (%esp) */
767 o(0x08c483); /* add $8, %esp */
769 /* int to float/double/long double */
770 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
771 o(0x2404db); /* fildl (%esp) */
772 o(0x04c483); /* add $4, %esp */
777 /* convert fp to int 't' type */
778 /* XXX: handle long long case */
779 void gen_cvt_ftoi(int t
)
789 oad(0x2dd9, (int)&__tcc_int_fpu_control
); /* ldcw xxx */
790 oad(0xec81, size
); /* sub $xxx, %esp */
792 o(0x1cdb); /* fistpl */
794 o(0x3cdf); /* fistpll */
796 oad(0x2dd9, (int)&__tcc_fpu_control
); /* ldcw xxx */
798 o(0x58 + r
); /* pop r */
801 vtop
->r
= r
; /* mark reg as used */
802 r2
= get_reg(RC_INT
);
803 o(0x58 + r2
); /* pop r2 */
806 o(0x04c483); /* add $4, %esp */
812 /* convert from one floating point type to another */
813 void gen_cvt_ftof(int t
)
815 /* all we have to do on i386 is to put the float in a register */
819 /* bound check support functions */
820 #ifdef CONFIG_TCC_BCHECK
822 /* generate a bounded pointer addition */
823 void gen_bounded_ptr_add(void)
826 /* prepare fast i386 function call (args in eax and edx) */
828 /* save all temporary registers */
831 /* do a fast function call */
833 oad(0xe8, (int)__bound_ptr_add
- ind
- 5);
834 /* returned pointer is in eax */
836 vtop
->r
= REG_EAX
| VT_BOUNDED
;
837 vtop
->c
.ul
= addr
; /* address of bounding function call point */
840 /* patch pointer addition in vtop so that pointer dereferencing is
842 void gen_bounded_ptr_deref(void)
845 int size
, align
, addr
;
848 /* XXX: put that code in generic part of tcc */
849 if (!is_float(vtop
->t
)) {
850 if (vtop
->r
& VT_LVAL_BYTE
)
852 else if (vtop
->r
& VT_LVAL_SHORT
)
856 size
= type_size(vtop
->t
, &align
);
858 case 1: func
= __bound_ptr_indir1
; break;
859 case 2: func
= __bound_ptr_indir2
; break;
860 case 4: func
= __bound_ptr_indir4
; break;
861 case 8: func
= __bound_ptr_indir8
; break;
862 case 12: func
= __bound_ptr_indir12
; break;
863 case 16: func
= __bound_ptr_indir16
; break;
865 error("unhandled size when derefencing bounded pointer");
871 *(int *)(addr
+ 1) = (int)func
- addr
- 5;
875 /* end of X86 code generator */
876 /*************************************************************/