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 unsigned long func_sub_sp_offset
;
83 static unsigned long func_bound_offset
;
84 static int func_ret_sub
;
86 /* XXX: make it faster ? */
91 if (ind1
> cur_text_section
->data_allocated
)
92 section_realloc(cur_text_section
, ind1
);
93 cur_text_section
->data
[ind
] = c
;
113 /* output a symbol and patch all calls to it */
114 void gsym_addr(int t
, int a
)
118 ptr
= (int *)(cur_text_section
->data
+ t
);
119 n
= *ptr
; /* next value */
130 /* psym is used to put an instruction with a data field which is a
131 reference to a symbol. It is in fact the same as oad ! */
134 /* instruction + 4 bytes data. Return the address of the data */
135 int oad(int c
, int s
)
141 if (ind1
> cur_text_section
->data_allocated
)
142 section_realloc(cur_text_section
, ind1
);
143 *(int *)(cur_text_section
->data
+ ind
) = s
;
149 /* output constant with relocation if 'r & VT_SYM' is true */
150 void gen_addr32(int r
, int c
)
155 greloc(cur_text_section
,
156 (Sym
*)c
, ind
, R_386_32
);
161 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
163 void gen_modrm(int op_reg
, int r
, int c
)
165 op_reg
= op_reg
<< 3;
166 if ((r
& VT_VALMASK
) == VT_CONST
) {
167 /* constant memory reference */
170 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
171 /* currently, we use only ebp as base */
173 /* short reference */
177 oad(0x85 | op_reg
, c
);
180 g(0x00 | op_reg
| (r
& VT_VALMASK
));
185 /* load 'r' from value 'sv' */
186 void load(int r
, SValue
*sv
)
188 int v
, t
, ft
, fc
, fr
;
197 if (v
== VT_LLOCAL
) {
199 v1
.r
= VT_LOCAL
| VT_LVAL
;
204 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
207 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
210 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
213 } else if ((ft
& VT_TYPE
) == VT_BYTE
) {
214 o(0xbe0f); /* movsbl */
215 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
216 o(0xb60f); /* movzbl */
217 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
218 o(0xbf0f); /* movswl */
219 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
220 o(0xb70f); /* movzwl */
224 gen_modrm(r
, fr
, fc
);
227 o(0xb8 + r
); /* mov $xx, r */
229 } else if (v
== VT_LOCAL
) {
230 o(0x8d); /* lea xxx(%ebp), r */
231 gen_modrm(r
, VT_LOCAL
, fc
);
232 } else if (v
== VT_CMP
) {
233 oad(0xb8 + r
, 0); /* mov $0, r */
234 o(0x0f); /* setxx %br */
237 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
239 oad(0xb8 + r
, t
); /* mov $1, r */
240 oad(0xe9, 5); /* jmp after */
242 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
245 o(0xc0 + r
+ v
* 8); /* mov v, r */
250 /* store register 'r' in lvalue 'v' */
251 void store(int r
, SValue
*v
)
257 fr
= v
->r
& VT_VALMASK
;
259 /* XXX: incorrect if float reg to reg */
260 if (bt
== VT_FLOAT
) {
263 } else if (bt
== VT_DOUBLE
) {
266 } else if (bt
== VT_LDOUBLE
) {
267 o(0xc0d9); /* fld %st(0) */
278 if (fr
== VT_CONST
||
281 gen_modrm(r
, v
->r
, fc
);
282 } else if (fr
!= r
) {
283 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
287 /* start function call and return function call context */
288 void gfunc_start(GFuncContext
*c
, int func_call
)
291 c
->func_call
= func_call
;
294 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
296 void gfunc_param(GFuncContext
*c
)
300 if ((vtop
->t
& VT_BTYPE
) == VT_STRUCT
) {
301 size
= type_size(vtop
->t
, &align
);
302 /* align to stack align size */
303 size
= (size
+ 3) & ~3;
304 /* allocate the necessary size on stack */
305 oad(0xec81, size
); /* sub $xxx, %esp */
306 /* generate structure store */
308 o(0x89); /* mov %esp, r */
310 vset(vtop
->t
, r
| VT_LVAL
, 0);
313 c
->args_size
+= size
;
314 } else if (is_float(vtop
->t
)) {
315 gv(RC_FLOAT
); /* only one float register */
316 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
318 else if ((vtop
->t
& VT_BTYPE
) == VT_DOUBLE
)
322 oad(0xec81, size
); /* sub $xxx, %esp */
326 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
329 c
->args_size
+= size
;
331 /* simple type (currently always same size) */
332 /* XXX: implicit cast ? */
334 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
336 o(0x50 + vtop
->r2
); /* push r */
340 o(0x50 + r
); /* push r */
341 c
->args_size
+= size
;
346 static void gadd_sp(int val
)
348 if (val
== (char)val
) {
352 oad(0xc481, val
); /* add $xxx, %esp */
356 /* generate function call with address in (vtop->t, vtop->c) and free function
357 context. Stack entry is popped */
358 void gfunc_call(GFuncContext
*c
)
361 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
363 if (vtop
->r
& VT_SYM
) {
364 /* relocation case */
365 greloc(cur_text_section
, vtop
->c
.sym
,
366 ind
+ 1, R_386_PC32
);
369 oad(0xe8, vtop
->c
.ul
- ind
- 5);
372 /* otherwise, indirect call */
374 o(0xff); /* call *r */
377 if (c
->args_size
&& c
->func_call
== FUNC_CDECL
)
378 gadd_sp(c
->args_size
);
382 /* generate function prolog of type 't' */
383 void gfunc_prolog(int t
)
385 int addr
, align
, size
, u
, func_call
;
388 sym
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
391 /* if the function returns a structure, then add an
392 implicit pointer parameter */
394 if ((func_vt
& VT_BTYPE
) == VT_STRUCT
) {
398 /* define parameters */
399 while ((sym
= sym
->next
) != NULL
) {
401 sym_push(sym
->v
& ~SYM_FIELD
, u
,
402 VT_LOCAL
| VT_LVAL
, addr
);
403 size
= type_size(u
, &align
);
404 size
= (size
+ 3) & ~3;
405 #ifdef FUNC_STRUCT_PARAM_AS_PTR
406 /* structs are passed as pointer */
407 if ((u
& VT_BTYPE
) == VT_STRUCT
) {
414 /* pascal type call ? */
415 if (func_call
== FUNC_STDCALL
)
416 func_ret_sub
= addr
- 8;
417 o(0xe58955); /* push %ebp, mov %esp, %ebp */
418 func_sub_sp_offset
= oad(0xec81, 0); /* sub $xxx, %esp */
419 /* leave some room for bound checking code */
420 if (do_bounds_check
) {
421 oad(0xb8, 0); /* lbound section pointer */
422 oad(0xb8, 0); /* call to function */
423 func_bound_offset
= lbounds_section
->data_offset
;
427 /* generate function epilog */
428 void gfunc_epilog(void)
430 #ifdef CONFIG_TCC_BCHECK
431 if (do_bounds_check
&& func_bound_offset
!= lbounds_section
->data_offset
) {
435 /* add end of table info */
436 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(int));
438 /* generate bound local allocation */
440 ind
= func_sub_sp_offset
+ 4;
441 sym_data
= get_sym_ref(char_pointer_type
, lbounds_section
,
442 func_bound_offset
, lbounds_section
->data_offset
);
443 greloc(cur_text_section
, sym_data
,
445 oad(0xb8, 0); /* mov %eax, xxx */
446 sym
= external_sym(TOK___bound_local_new
, func_old_type
, 0);
447 greloc(cur_text_section
, sym
,
448 ind
+ 1, R_386_PC32
);
451 /* generate bound check local freeing */
452 o(0x5250); /* save returned value, if any */
453 greloc(cur_text_section
, sym_data
,
455 oad(0xb8, 0); /* mov %eax, xxx */
456 sym
= external_sym(TOK___bound_local_delete
, func_old_type
, 0);
457 greloc(cur_text_section
, sym
,
458 ind
+ 1, R_386_PC32
);
460 o(0x585a); /* restore returned value, if any */
464 if (func_ret_sub
== 0) {
469 g(func_ret_sub
>> 8);
471 /* align local size to word & save local variables */
472 *(int *)(cur_text_section
->data
+ func_sub_sp_offset
) = (-loc
+ 3) & -4;
475 /* generate a jump to a label */
478 return psym(0xe9, t
);
481 /* generate a jump to a fixed address */
482 void gjmp_addr(int a
)
484 oad(0xe9, a
- ind
- 5);
487 /* generate a test. set 'inv' to invert test. Stack entry is popped */
488 int gtst(int inv
, int t
)
491 v
= vtop
->r
& VT_VALMASK
;
493 /* fast case : can jump directly since flags are set */
495 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
496 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
497 /* && or || optimization */
498 if ((v
& 1) == inv
) {
499 /* insert vtop->c jump list in t */
502 p
= (int *)(cur_text_section
->data
+ *p
);
510 if (is_float(vtop
->t
)) {
514 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
515 /* constant jmp optimization */
516 if ((vtop
->c
.i
!= 0) != inv
)
523 t
= psym(0x85 ^ inv
, t
);
530 /* generate an integer binary operation */
537 case TOK_ADDC1
: /* add with carry generation */
540 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
547 /* XXX: generate inc and dec for smaller code ? */
549 o(0xc0 | (opc
<< 3) | r
);
553 oad(0xc0 | (opc
<< 3) | r
, c
);
559 o((opc
<< 3) | 0x01);
560 o(0xc0 + r
+ fr
* 8);
563 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
565 vset(VT_INT
, VT_CMP
, op
);
569 case TOK_SUBC1
: /* sub with carry generation */
572 case TOK_ADDC2
: /* add with carry use */
575 case TOK_SUBC2
: /* sub with carry use */
592 o(0xaf0f); /* imul fr, r */
593 o(0xc0 + fr
+ r
* 8);
604 opc
= 0xc0 | (opc
<< 3);
605 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
610 c
= vtop
->c
.i
& 0x1f;
611 o(0xc1); /* shl/shr/sar $xxx, r */
615 /* we generate the shift in ecx */
618 o(0xd3); /* shl/shr/sar %cl, r */
629 /* first operand must be in eax */
630 /* XXX: need better constraint for second operand */
636 if (op
== TOK_UMULL
) {
637 o(0xf7); /* mul fr */
642 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
643 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
646 o(0xf799); /* cltd, idiv fr, %eax */
649 if (op
== '%' || op
== TOK_UMOD
)
662 /* generate a floating point operation 'v = t1 op t2' instruction. The
663 two operands are guaranted to have the same floating point type */
664 /* XXX: need to use ST1 too */
667 int a
, ft
, fc
, swapped
, r
;
669 /* convert constants to memory references */
670 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
675 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
678 /* must put at least one value in the floating point register */
679 if ((vtop
[-1].r
& VT_LVAL
) &&
680 (vtop
[0].r
& VT_LVAL
)) {
686 /* swap the stack if needed so that t1 is the register and t2 is
687 the memory reference */
688 if (vtop
[-1].r
& VT_LVAL
) {
692 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
693 /* load on stack second operand */
695 save_reg(REG_EAX
); /* eax is used by FP comparison code */
696 if (op
== TOK_GE
|| op
== TOK_GT
)
698 else if (op
== TOK_EQ
|| op
== TOK_NE
)
701 o(0xc9d9); /* fxch %st(1) */
702 o(0xe9da); /* fucompp */
703 o(0xe0df); /* fnstsw %ax */
705 o(0x45e480); /* and $0x45, %ah */
706 o(0x40fC80); /* cmp $0x40, %ah */
707 } else if (op
== TOK_NE
) {
708 o(0x45e480); /* and $0x45, %ah */
709 o(0x40f480); /* xor $0x40, %ah */
711 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
712 o(0x05c4f6); /* test $0x05, %ah */
715 o(0x45c4f6); /* test $0x45, %ah */
722 /* no memory reference possible for long double operations */
723 if ((vtop
->t
& VT_BTYPE
) == VT_LDOUBLE
) {
749 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
750 o(0xde); /* fxxxp %st, %st(1) */
753 /* if saved lvalue, then we must reload it */
755 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
759 v1
.r
= VT_LOCAL
| VT_LVAL
;
765 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
775 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
776 and 'long long' cases. */
777 void gen_cvt_itof(int t
)
781 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
782 /* signed long long to float/double/long double (unsigned case
783 is handled generically) */
784 o(0x50 + vtop
->r2
); /* push r2 */
785 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
786 o(0x242cdf); /* fildll (%esp) */
787 o(0x08c483); /* add $8, %esp */
788 } else if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
789 (VT_INT
| VT_UNSIGNED
)) {
790 /* unsigned int to float/double/long double */
791 o(0x6a); /* push $0 */
793 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
794 o(0x242cdf); /* fildll (%esp) */
795 o(0x08c483); /* add $8, %esp */
797 /* int to float/double/long double */
798 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
799 o(0x2404db); /* fildl (%esp) */
800 o(0x04c483); /* add $4, %esp */
805 /* convert fp to int 't' type */
806 /* XXX: handle long long case */
807 void gen_cvt_ftoi(int t
)
818 o(0x2dd9); /* ldcw xxx */
819 sym
= external_sym(TOK___tcc_int_fpu_control
,
820 VT_SHORT
| VT_UNSIGNED
, VT_LVAL
);
821 greloc(cur_text_section
, sym
,
825 oad(0xec81, size
); /* sub $xxx, %esp */
827 o(0x1cdb); /* fistpl */
829 o(0x3cdf); /* fistpll */
831 o(0x2dd9); /* ldcw xxx */
832 sym
= external_sym(TOK___tcc_fpu_control
,
833 VT_SHORT
| VT_UNSIGNED
, VT_LVAL
);
834 greloc(cur_text_section
, sym
,
839 o(0x58 + r
); /* pop r */
842 vtop
->r
= r
; /* mark reg as used */
843 r2
= get_reg(RC_INT
);
844 o(0x58 + r2
); /* pop r2 */
847 o(0x04c483); /* add $4, %esp */
853 /* convert from one floating point type to another */
854 void gen_cvt_ftof(int t
)
856 /* all we have to do on i386 is to put the float in a register */
860 /* bound check support functions */
861 #ifdef CONFIG_TCC_BCHECK
863 /* generate a bounded pointer addition */
864 void gen_bounded_ptr_add(void)
868 /* prepare fast i386 function call (args in eax and edx) */
870 /* save all temporary registers */
873 /* do a fast function call */
874 sym
= external_sym(TOK___bound_ptr_add
, func_old_type
, 0);
875 greloc(cur_text_section
, sym
,
876 ind
+ 1, R_386_PC32
);
878 /* returned pointer is in eax */
880 vtop
->r
= REG_EAX
| VT_BOUNDED
;
881 /* address of bounding function call point */
882 vtop
->c
.ul
= (cur_text_section
->reloc
->data_offset
- sizeof(Elf32_Rel
));
885 /* patch pointer addition in vtop so that pointer dereferencing is
887 void gen_bounded_ptr_deref(void)
895 /* XXX: put that code in generic part of tcc */
896 if (!is_float(vtop
->t
)) {
897 if (vtop
->r
& VT_LVAL_BYTE
)
899 else if (vtop
->r
& VT_LVAL_SHORT
)
903 size
= type_size(vtop
->t
, &align
);
905 case 1: func
= TOK___bound_ptr_indir1
; break;
906 case 2: func
= TOK___bound_ptr_indir2
; break;
907 case 4: func
= TOK___bound_ptr_indir4
; break;
908 case 8: func
= TOK___bound_ptr_indir8
; break;
909 case 12: func
= TOK___bound_ptr_indir12
; break;
910 case 16: func
= TOK___bound_ptr_indir16
; break;
912 error("unhandled size when derefencing bounded pointer");
917 /* patch relocation */
918 /* XXX: find a better solution ? */
919 rel
= (Elf32_Rel
*)(cur_text_section
->reloc
->data
+ vtop
->c
.ul
);
920 sym
= external_sym(func
, func_old_type
, 0);
922 put_extern_sym(sym
, NULL
, 0, 0);
923 rel
->r_info
= ELF32_R_INFO(sym
->c
, ELF32_R_TYPE(rel
->r_info
));
927 /* end of X86 code generator */
928 /*************************************************************/