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 /* 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 TREG_EAX /* single word int return register */
54 #define REG_LRET TREG_EDX /* second word return register (for long long) */
55 #define REG_FRET TREG_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
70 /* maximum alignment (for aligned attribute support) */
73 /******************************************************/
76 #define EM_TCC_TARGET EM_386
78 /* relocation type for 32 bit data relocation */
79 #define R_DATA_32 R_386_32
80 #define R_JMP_SLOT R_386_JMP_SLOT
81 #define R_COPY R_386_COPY
83 #define ELF_START_ADDR 0x08048000
84 #define ELF_PAGE_SIZE 0x1000
86 /******************************************************/
88 static unsigned long func_sub_sp_offset
;
89 static unsigned long func_bound_offset
;
90 static int func_ret_sub
;
92 /* XXX: make it faster ? */
97 if (ind1
> cur_text_section
->data_allocated
)
98 section_realloc(cur_text_section
, ind1
);
99 cur_text_section
->data
[ind
] = c
;
103 void o(unsigned int c
)
119 /* output a symbol and patch all calls to it */
120 void gsym_addr(int t
, int a
)
124 ptr
= (int *)(cur_text_section
->data
+ t
);
125 n
= *ptr
; /* next value */
136 /* psym is used to put an instruction with a data field which is a
137 reference to a symbol. It is in fact the same as oad ! */
140 /* instruction + 4 bytes data. Return the address of the data */
141 static int oad(int c
, int s
)
147 if (ind1
> cur_text_section
->data_allocated
)
148 section_realloc(cur_text_section
, ind1
);
149 *(int *)(cur_text_section
->data
+ ind
) = s
;
155 /* output constant with relocation if 'r & VT_SYM' is true */
156 static void gen_addr32(int r
, Sym
*sym
, int c
)
159 greloc(cur_text_section
, sym
, ind
, R_386_32
);
163 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
165 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
167 op_reg
= op_reg
<< 3;
168 if ((r
& VT_VALMASK
) == VT_CONST
) {
169 /* constant memory reference */
171 gen_addr32(r
, sym
, c
);
172 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
173 /* currently, we use only ebp as base */
175 /* short reference */
179 oad(0x85 | op_reg
, c
);
182 g(0x00 | op_reg
| (r
& VT_VALMASK
));
187 /* load 'r' from value 'sv' */
188 void load(int r
, SValue
*sv
)
190 int v
, t
, ft
, fc
, fr
;
199 if (v
== VT_LLOCAL
) {
201 v1
.r
= VT_LOCAL
| VT_LVAL
;
206 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
209 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
212 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
215 } else if ((ft
& VT_TYPE
) == VT_BYTE
) {
216 o(0xbe0f); /* movsbl */
217 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
218 o(0xb60f); /* movzbl */
219 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
220 o(0xbf0f); /* movswl */
221 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
222 o(0xb70f); /* movzwl */
226 gen_modrm(r
, fr
, sv
->sym
, fc
);
229 o(0xb8 + r
); /* mov $xx, r */
230 gen_addr32(fr
, sv
->sym
, fc
);
231 } else if (v
== VT_LOCAL
) {
232 o(0x8d); /* lea xxx(%ebp), r */
233 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
234 } else if (v
== VT_CMP
) {
235 oad(0xb8 + r
, 0); /* mov $0, r */
236 o(0x0f); /* setxx %br */
239 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
241 oad(0xb8 + r
, t
); /* mov $1, r */
242 o(0x05eb); /* jmp after */
244 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
247 o(0xc0 + r
+ v
* 8); /* mov v, r */
252 /* store register 'r' in lvalue 'v' */
253 void store(int r
, SValue
*v
)
259 fr
= v
->r
& VT_VALMASK
;
261 /* XXX: incorrect if float reg to reg */
262 if (bt
== VT_FLOAT
) {
265 } else if (bt
== VT_DOUBLE
) {
268 } else if (bt
== VT_LDOUBLE
) {
269 o(0xc0d9); /* fld %st(0) */
275 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
280 if (fr
== VT_CONST
||
283 gen_modrm(r
, v
->r
, v
->sym
, fc
);
284 } else if (fr
!= r
) {
285 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
289 static void gadd_sp(int val
)
291 if (val
== (char)val
) {
295 oad(0xc481, val
); /* add $xxx, %esp */
299 /* 'is_jmp' is '1' if it is a jump */
300 static void gcall_or_jmp(int is_jmp
)
303 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
305 if (vtop
->r
& VT_SYM
) {
306 /* relocation case */
307 greloc(cur_text_section
, vtop
->sym
,
308 ind
+ 1, R_386_PC32
);
310 /* put an empty PC32 relocation */
311 put_elf_reloc(symtab_section
, cur_text_section
,
312 ind
+ 1, R_386_PC32
, 0);
314 oad(0xe8 + is_jmp
, vtop
->c
.ul
- 4); /* call/jmp im */
316 /* otherwise, indirect call */
318 o(0xff); /* call/jmp *r */
319 o(0xd0 + r
+ (is_jmp
<< 4));
323 static uint8_t fastcall_regs
[3] = { TREG_EAX
, TREG_EDX
, TREG_ECX
};
325 /* Generate function call. The function address is pushed first, then
326 all the parameters in call order. This functions pops all the
327 parameters and the function address. */
328 void gfunc_call(int nb_args
)
330 int size
, align
, r
, args_size
, i
, func_call
;
334 for(i
= 0;i
< nb_args
; i
++) {
335 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
336 size
= type_size(&vtop
->type
, &align
);
337 /* align to stack align size */
338 size
= (size
+ 3) & ~3;
339 /* allocate the necessary size on stack */
340 oad(0xec81, size
); /* sub $xxx, %esp */
341 /* generate structure store */
343 o(0x89); /* mov %esp, r */
345 vset(&vtop
->type
, r
| VT_LVAL
, 0);
349 } else if (is_float(vtop
->type
.t
)) {
350 gv(RC_FLOAT
); /* only one float register */
351 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
353 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
357 oad(0xec81, size
); /* sub $xxx, %esp */
361 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
366 /* simple type (currently always same size) */
367 /* XXX: implicit cast ? */
369 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
371 o(0x50 + vtop
->r2
); /* push r */
375 o(0x50 + r
); /* push r */
380 save_regs(0); /* save used temporary registers */
381 func_sym
= vtop
->type
.ref
;
382 func_call
= func_sym
->r
;
384 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
385 int fastcall_nb_regs
;
386 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
387 for(i
= 0;i
< fastcall_nb_regs
; i
++) {
390 o(0x58 + fastcall_regs
[i
]); /* pop r */
391 /* XXX: incorrect for struct/floats */
396 if (args_size
&& func_sym
->r
!= FUNC_STDCALL
)
402 #define FUNC_PROLOG_SIZE 10
404 #define FUNC_PROLOG_SIZE 9
407 /* generate function prolog of type 't' */
408 void gfunc_prolog(CType
*func_type
)
410 int addr
, align
, size
, func_call
, fastcall_nb_regs
;
411 int param_index
, param_addr
;
415 sym
= func_type
->ref
;
419 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
420 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
422 fastcall_nb_regs
= 0;
426 ind
+= FUNC_PROLOG_SIZE
;
427 func_sub_sp_offset
= ind
;
428 /* if the function returns a structure, then add an
429 implicit pointer parameter */
431 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
432 /* XXX: fastcall case ? */
437 /* define parameters */
438 while ((sym
= sym
->next
) != NULL
) {
440 size
= type_size(type
, &align
);
441 size
= (size
+ 3) & ~3;
442 #ifdef FUNC_STRUCT_PARAM_AS_PTR
443 /* structs are passed as pointer */
444 if ((type
->t
& VT_BTYPE
) == VT_STRUCT
) {
448 if (param_index
< fastcall_nb_regs
) {
449 /* save FASTCALL register */
452 gen_modrm(fastcall_regs
[param_index
], VT_LOCAL
, NULL
, loc
);
458 sym_push(sym
->v
& ~SYM_FIELD
, type
,
459 VT_LOCAL
| VT_LVAL
, param_addr
);
463 /* pascal type call ? */
464 if (func_call
== FUNC_STDCALL
)
465 func_ret_sub
= addr
- 8;
467 /* leave some room for bound checking code */
468 if (do_bounds_check
) {
469 oad(0xb8, 0); /* lbound section pointer */
470 oad(0xb8, 0); /* call to function */
471 func_bound_offset
= lbounds_section
->data_offset
;
475 /* generate function epilog */
476 void gfunc_epilog(void)
480 #ifdef CONFIG_TCC_BCHECK
481 if (do_bounds_check
&& func_bound_offset
!= lbounds_section
->data_offset
) {
485 /* add end of table info */
486 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(int));
488 /* generate bound local allocation */
490 ind
= func_sub_sp_offset
;
491 sym_data
= get_sym_ref(&char_pointer_type
, lbounds_section
,
492 func_bound_offset
, lbounds_section
->data_offset
);
493 greloc(cur_text_section
, sym_data
,
495 oad(0xb8, 0); /* mov %eax, xxx */
496 sym
= external_global_sym(TOK___bound_local_new
, &func_old_type
, 0);
497 greloc(cur_text_section
, sym
,
498 ind
+ 1, R_386_PC32
);
501 /* generate bound check local freeing */
502 o(0x5250); /* save returned value, if any */
503 greloc(cur_text_section
, sym_data
,
505 oad(0xb8, 0); /* mov %eax, xxx */
506 sym
= external_global_sym(TOK___bound_local_delete
, &func_old_type
, 0);
507 greloc(cur_text_section
, sym
,
508 ind
+ 1, R_386_PC32
);
510 o(0x585a); /* restore returned value, if any */
514 if (func_ret_sub
== 0) {
519 g(func_ret_sub
>> 8);
521 /* align local size to word & save local variables */
525 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
528 Sym
*sym
= external_global_sym(TOK___chkstk
, &func_old_type
, 0);
529 oad(0xb8, v
); /* mov stacksize, %eax */
530 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
531 greloc(cur_text_section
, sym
, ind
-4, R_386_PC32
);
535 o(0xe58955); /* push %ebp, mov %esp, %ebp */
536 o(0xec81); /* sub esp, stacksize */
538 #if FUNC_PROLOG_SIZE == 10
539 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
545 /* generate a jump to a label */
548 return psym(0xe9, t
);
551 /* generate a jump to a fixed address */
552 void gjmp_addr(int a
)
560 oad(0xe9, a
- ind
- 5);
564 /* generate a test. set 'inv' to invert test. Stack entry is popped */
565 int gtst(int inv
, int t
)
569 v
= vtop
->r
& VT_VALMASK
;
571 /* fast case : can jump directly since flags are set */
573 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
574 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
575 /* && or || optimization */
576 if ((v
& 1) == inv
) {
577 /* insert vtop->c jump list in t */
580 p
= (int *)(cur_text_section
->data
+ *p
);
588 if (is_float(vtop
->type
.t
)) {
592 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
593 /* constant jmp optimization */
594 if ((vtop
->c
.i
!= 0) != inv
)
601 t
= psym(0x85 ^ inv
, t
);
608 /* generate an integer binary operation */
615 case TOK_ADDC1
: /* add with carry generation */
618 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
625 /* XXX: generate inc and dec for smaller code ? */
627 o(0xc0 | (opc
<< 3) | r
);
631 oad(0xc0 | (opc
<< 3) | r
, c
);
637 o((opc
<< 3) | 0x01);
638 o(0xc0 + r
+ fr
* 8);
641 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
647 case TOK_SUBC1
: /* sub with carry generation */
650 case TOK_ADDC2
: /* add with carry use */
653 case TOK_SUBC2
: /* sub with carry use */
670 o(0xaf0f); /* imul fr, r */
671 o(0xc0 + fr
+ r
* 8);
682 opc
= 0xc0 | (opc
<< 3);
683 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
688 c
= vtop
->c
.i
& 0x1f;
689 o(0xc1); /* shl/shr/sar $xxx, r */
693 /* we generate the shift in ecx */
696 o(0xd3); /* shl/shr/sar %cl, r */
707 /* first operand must be in eax */
708 /* XXX: need better constraint for second operand */
714 if (op
== TOK_UMULL
) {
715 o(0xf7); /* mul fr */
720 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
721 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
724 o(0xf799); /* cltd, idiv fr, %eax */
727 if (op
== '%' || op
== TOK_UMOD
)
740 /* generate a floating point operation 'v = t1 op t2' instruction. The
741 two operands are guaranted to have the same floating point type */
742 /* XXX: need to use ST1 too */
745 int a
, ft
, fc
, swapped
, r
;
747 /* convert constants to memory references */
748 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
753 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
756 /* must put at least one value in the floating point register */
757 if ((vtop
[-1].r
& VT_LVAL
) &&
758 (vtop
[0].r
& VT_LVAL
)) {
764 /* swap the stack if needed so that t1 is the register and t2 is
765 the memory reference */
766 if (vtop
[-1].r
& VT_LVAL
) {
770 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
771 /* load on stack second operand */
772 load(TREG_ST0
, vtop
);
773 save_reg(TREG_EAX
); /* eax is used by FP comparison code */
774 if (op
== TOK_GE
|| op
== TOK_GT
)
776 else if (op
== TOK_EQ
|| op
== TOK_NE
)
779 o(0xc9d9); /* fxch %st(1) */
780 o(0xe9da); /* fucompp */
781 o(0xe0df); /* fnstsw %ax */
783 o(0x45e480); /* and $0x45, %ah */
784 o(0x40fC80); /* cmp $0x40, %ah */
785 } else if (op
== TOK_NE
) {
786 o(0x45e480); /* and $0x45, %ah */
787 o(0x40f480); /* xor $0x40, %ah */
789 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
790 o(0x05c4f6); /* test $0x05, %ah */
793 o(0x45c4f6); /* test $0x45, %ah */
800 /* no memory reference possible for long double operations */
801 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
802 load(TREG_ST0
, vtop
);
827 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
828 o(0xde); /* fxxxp %st, %st(1) */
831 /* if saved lvalue, then we must reload it */
833 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
837 v1
.r
= VT_LOCAL
| VT_LVAL
;
843 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
847 gen_modrm(a
, r
, vtop
->sym
, fc
);
853 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
854 and 'long long' cases. */
855 void gen_cvt_itof(int t
)
859 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
860 /* signed long long to float/double/long double (unsigned case
861 is handled generically) */
862 o(0x50 + vtop
->r2
); /* push r2 */
863 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
864 o(0x242cdf); /* fildll (%esp) */
865 o(0x08c483); /* add $8, %esp */
866 } else if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
867 (VT_INT
| VT_UNSIGNED
)) {
868 /* unsigned int to float/double/long double */
869 o(0x6a); /* push $0 */
871 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
872 o(0x242cdf); /* fildll (%esp) */
873 o(0x08c483); /* add $8, %esp */
875 /* int to float/double/long double */
876 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
877 o(0x2404db); /* fildl (%esp) */
878 o(0x04c483); /* add $4, %esp */
883 /* convert fp to int 't' type */
884 /* XXX: handle long long case */
885 void gen_cvt_ftoi(int t
)
891 ushort_type
.t
= VT_SHORT
| VT_UNSIGNED
;
899 o(0x2dd9); /* ldcw xxx */
900 sym
= external_global_sym(TOK___tcc_int_fpu_control
,
901 &ushort_type
, VT_LVAL
);
902 greloc(cur_text_section
, sym
,
906 oad(0xec81, size
); /* sub $xxx, %esp */
908 o(0x1cdb); /* fistpl */
910 o(0x3cdf); /* fistpll */
912 o(0x2dd9); /* ldcw xxx */
913 sym
= external_global_sym(TOK___tcc_fpu_control
,
914 &ushort_type
, VT_LVAL
);
915 greloc(cur_text_section
, sym
,
920 o(0x58 + r
); /* pop r */
923 vtop
->r
= r
; /* mark reg as used */
924 r2
= get_reg(RC_INT
);
925 o(0x58 + r2
); /* pop r2 */
928 o(0x04c483); /* add $4, %esp */
934 /* convert from one floating point type to another */
935 void gen_cvt_ftof(int t
)
937 /* all we have to do on i386 is to put the float in a register */
941 /* computed goto support */
948 /* bound check support functions */
949 #ifdef CONFIG_TCC_BCHECK
951 /* generate a bounded pointer addition */
952 void gen_bounded_ptr_add(void)
956 /* prepare fast i386 function call (args in eax and edx) */
958 /* save all temporary registers */
961 /* do a fast function call */
962 sym
= external_global_sym(TOK___bound_ptr_add
, &func_old_type
, 0);
963 greloc(cur_text_section
, sym
,
964 ind
+ 1, R_386_PC32
);
966 /* returned pointer is in eax */
968 vtop
->r
= TREG_EAX
| VT_BOUNDED
;
969 /* address of bounding function call point */
970 vtop
->c
.ul
= (cur_text_section
->reloc
->data_offset
- sizeof(Elf32_Rel
));
973 /* patch pointer addition in vtop so that pointer dereferencing is
975 void gen_bounded_ptr_deref(void)
983 /* XXX: put that code in generic part of tcc */
984 if (!is_float(vtop
->type
.t
)) {
985 if (vtop
->r
& VT_LVAL_BYTE
)
987 else if (vtop
->r
& VT_LVAL_SHORT
)
991 size
= type_size(&vtop
->type
, &align
);
993 case 1: func
= TOK___bound_ptr_indir1
; break;
994 case 2: func
= TOK___bound_ptr_indir2
; break;
995 case 4: func
= TOK___bound_ptr_indir4
; break;
996 case 8: func
= TOK___bound_ptr_indir8
; break;
997 case 12: func
= TOK___bound_ptr_indir12
; break;
998 case 16: func
= TOK___bound_ptr_indir16
; break;
1000 error("unhandled size when derefencing bounded pointer");
1005 /* patch relocation */
1006 /* XXX: find a better solution ? */
1007 rel
= (Elf32_Rel
*)(cur_text_section
->reloc
->data
+ vtop
->c
.ul
);
1008 sym
= external_global_sym(func
, &func_old_type
, 0);
1010 put_extern_sym(sym
, NULL
, 0, 0);
1011 rel
->r_info
= ELF32_R_INFO(sym
->c
, ELF32_R_TYPE(rel
->r_info
));
1015 /* end of X86 code generator */
1016 /*************************************************************/