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
23 const int reg_classes
[NB_REGS
] = {
24 /* eax */ RC_INT
| RC_EAX
,
25 /* ecx */ RC_INT
| RC_ECX
,
26 /* edx */ RC_INT
| RC_EDX
,
27 /* st0 */ RC_FLOAT
| RC_ST0
,
31 /******************************************************/
33 static unsigned long func_sub_sp_offset
;
34 static unsigned long func_bound_offset
;
35 static int func_ret_sub
;
37 /* XXX: make it faster ? */
42 if (ind1
> cur_text_section
->data_allocated
)
43 section_realloc(cur_text_section
, ind1
);
44 cur_text_section
->data
[ind
] = c
;
48 void o(unsigned int c
)
64 /* output a symbol and patch all calls to it */
65 void gsym_addr(int t
, int a
)
69 ptr
= (int *)(cur_text_section
->data
+ t
);
70 n
= *ptr
; /* next value */
81 /* psym is used to put an instruction with a data field which is a
82 reference to a symbol. It is in fact the same as oad ! */
85 /* instruction + 4 bytes data. Return the address of the data */
92 if (ind1
> cur_text_section
->data_allocated
)
93 section_realloc(cur_text_section
, ind1
);
94 *(int *)(cur_text_section
->data
+ ind
) = s
;
100 /* output constant with relocation if 'r & VT_SYM' is true */
101 static void gen_addr32(int r
, Sym
*sym
, int c
)
104 greloc(cur_text_section
, sym
, ind
, R_386_32
);
108 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
110 static void gen_modrm(int op_reg
, int r
, Sym
*sym
, int c
)
112 op_reg
= op_reg
<< 3;
113 if ((r
& VT_VALMASK
) == VT_CONST
) {
114 /* constant memory reference */
116 gen_addr32(r
, sym
, c
);
117 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
118 /* currently, we use only ebp as base */
120 /* short reference */
124 oad(0x85 | op_reg
, c
);
127 g(0x00 | op_reg
| (r
& VT_VALMASK
));
132 static void indir(void);
134 int handle_dllimport(int r
, SValue
*sv
, void (*fn
)(int r
, SValue
*sv
))
136 if ((sv
->r
& (VT_VALMASK
|VT_SYM
|VT_CONST
)) != (VT_SYM
|VT_CONST
))
138 if (0 == (sv
->sym
->type
.t
& VT_IMPORT
))
141 printf("import %d %04x %s\n", r
, ind
, get_tok_str(sv
->sym
->v
, NULL
));
143 sv
->sym
->type
.t
&= ~VT_IMPORT
;
147 mk_pointer(&vtop
->type
);
152 sv
->sym
->type
.t
|= VT_IMPORT
;
157 /* load 'r' from value 'sv' */
158 void load(int r
, SValue
*sv
)
160 int v
, t
, ft
, fc
, fr
;
164 if (handle_dllimport(r
, sv
, load
))
173 if (v
== VT_LLOCAL
) {
175 v1
.r
= VT_LOCAL
| VT_LVAL
;
180 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
183 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
186 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
189 } else if ((ft
& VT_TYPE
) == VT_BYTE
) {
190 o(0xbe0f); /* movsbl */
191 } else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
)) {
192 o(0xb60f); /* movzbl */
193 } else if ((ft
& VT_TYPE
) == VT_SHORT
) {
194 o(0xbf0f); /* movswl */
195 } else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
)) {
196 o(0xb70f); /* movzwl */
200 gen_modrm(r
, fr
, sv
->sym
, fc
);
203 o(0xb8 + r
); /* mov $xx, r */
204 gen_addr32(fr
, sv
->sym
, fc
);
205 } else if (v
== VT_LOCAL
) {
206 o(0x8d); /* lea xxx(%ebp), r */
207 gen_modrm(r
, VT_LOCAL
, sv
->sym
, fc
);
208 } else if (v
== VT_CMP
) {
209 oad(0xb8 + r
, 0); /* mov $0, r */
210 o(0x0f); /* setxx %br */
213 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
215 oad(0xb8 + r
, t
); /* mov $1, r */
216 o(0x05eb); /* jmp after */
218 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
221 o(0xc0 + r
+ v
* 8); /* mov v, r */
226 /* store register 'r' in lvalue 'v' */
227 void store(int r
, SValue
*v
)
232 if (handle_dllimport(r
, v
, store
))
237 fr
= v
->r
& VT_VALMASK
;
239 /* XXX: incorrect if float reg to reg */
240 if (bt
== VT_FLOAT
) {
243 } else if (bt
== VT_DOUBLE
) {
246 } else if (bt
== VT_LDOUBLE
) {
247 o(0xc0d9); /* fld %st(0) */
253 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
258 if (fr
== VT_CONST
||
261 gen_modrm(r
, v
->r
, v
->sym
, fc
);
262 } else if (fr
!= r
) {
263 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
267 static void gadd_sp(int val
)
269 if (val
== (char)val
) {
273 oad(0xc481, val
); /* add $xxx, %esp */
277 /* 'is_jmp' is '1' if it is a jump */
278 static void gcall_or_jmp(int is_jmp
)
281 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
283 if (vtop
->r
& VT_SYM
) {
284 /* relocation case */
285 greloc(cur_text_section
, vtop
->sym
,
286 ind
+ 1, R_386_PC32
);
288 /* put an empty PC32 relocation */
289 put_elf_reloc(symtab_section
, cur_text_section
,
290 ind
+ 1, R_386_PC32
, 0);
292 oad(0xe8 + is_jmp
, vtop
->c
.ul
- 4); /* call/jmp im */
294 /* otherwise, indirect call */
296 o(0xff); /* call/jmp *r */
297 o(0xd0 + r
+ (is_jmp
<< 4));
301 static uint8_t fastcall_regs
[3] = { TREG_EAX
, TREG_EDX
, TREG_ECX
};
302 static uint8_t fastcallw_regs
[2] = { TREG_ECX
, TREG_EDX
};
304 /* Generate function call. The function address is pushed first, then
305 all the parameters in call order. This functions pops all the
306 parameters and the function address. */
307 void gfunc_call(int nb_args
)
309 int size
, align
, r
, args_size
, i
, func_call
;
313 for(i
= 0;i
< nb_args
; i
++) {
314 if ((vtop
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
315 size
= type_size(&vtop
->type
, &align
);
316 /* align to stack align size */
317 size
= (size
+ 3) & ~3;
318 /* allocate the necessary size on stack */
319 oad(0xec81, size
); /* sub $xxx, %esp */
320 /* generate structure store */
322 o(0x89); /* mov %esp, r */
324 vset(&vtop
->type
, r
| VT_LVAL
, 0);
328 } else if (is_float(vtop
->type
.t
)) {
329 gv(RC_FLOAT
); /* only one float register */
330 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
)
332 else if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
336 oad(0xec81, size
); /* sub $xxx, %esp */
340 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
345 /* simple type (currently always same size) */
346 /* XXX: implicit cast ? */
348 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
350 o(0x50 + vtop
->r2
); /* push r */
354 o(0x50 + r
); /* push r */
359 save_regs(0); /* save used temporary registers */
360 func_sym
= vtop
->type
.ref
;
361 func_call
= FUNC_CALL(func_sym
->r
);
363 if ((func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) ||
364 func_call
== FUNC_FASTCALLW
) {
365 int fastcall_nb_regs
;
366 uint8_t *fastcall_regs_ptr
;
367 if (func_call
== FUNC_FASTCALLW
) {
368 fastcall_regs_ptr
= fastcallw_regs
;
369 fastcall_nb_regs
= 2;
371 fastcall_regs_ptr
= fastcall_regs
;
372 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
374 for(i
= 0;i
< fastcall_nb_regs
; i
++) {
377 o(0x58 + fastcall_regs_ptr
[i
]); /* pop r */
378 /* XXX: incorrect for struct/floats */
385 if ((func_sym
->type
.t
& VT_BTYPE
) == VT_STRUCT
)
388 if (args_size
&& func_call
!= FUNC_STDCALL
)
394 #define FUNC_PROLOG_SIZE 10
396 #define FUNC_PROLOG_SIZE 9
399 /* generate function prolog of type 't' */
400 void gfunc_prolog(CType
*func_type
)
402 int addr
, align
, size
, func_call
, fastcall_nb_regs
;
403 int param_index
, param_addr
;
404 uint8_t *fastcall_regs_ptr
;
408 sym
= func_type
->ref
;
409 func_call
= FUNC_CALL(sym
->r
);
414 if (func_call
>= FUNC_FASTCALL1
&& func_call
<= FUNC_FASTCALL3
) {
415 fastcall_nb_regs
= func_call
- FUNC_FASTCALL1
+ 1;
416 fastcall_regs_ptr
= fastcall_regs
;
417 } else if (func_call
== FUNC_FASTCALLW
) {
418 fastcall_nb_regs
= 2;
419 fastcall_regs_ptr
= fastcallw_regs
;
421 fastcall_nb_regs
= 0;
422 fastcall_regs_ptr
= NULL
;
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_ptr
[param_index
], VT_LOCAL
, NULL
, loc
);
458 sym_push(sym
->v
& ~SYM_FIELD
, type
,
459 VT_LOCAL
| lvalue_type(type
->t
), param_addr
);
463 /* pascal type call ? */
464 if (func_call
== FUNC_STDCALL
)
465 func_ret_sub
= addr
- 8;
471 /* leave some room for bound checking code */
472 if (tcc_state
->do_bounds_check
) {
473 oad(0xb8, 0); /* lbound section pointer */
474 oad(0xb8, 0); /* call to function */
475 func_bound_offset
= lbounds_section
->data_offset
;
479 /* generate function epilog */
480 void gfunc_epilog(void)
484 #ifdef CONFIG_TCC_BCHECK
485 if (tcc_state
->do_bounds_check
486 && func_bound_offset
!= lbounds_section
->data_offset
) {
490 /* add end of table info */
491 bounds_ptr
= section_ptr_add(lbounds_section
, sizeof(int));
493 /* generate bound local allocation */
495 ind
= func_sub_sp_offset
;
496 sym_data
= get_sym_ref(&char_pointer_type
, lbounds_section
,
497 func_bound_offset
, lbounds_section
->data_offset
);
498 greloc(cur_text_section
, sym_data
,
500 oad(0xb8, 0); /* mov %eax, xxx */
501 sym
= external_global_sym(TOK___bound_local_new
, &func_old_type
, 0);
502 greloc(cur_text_section
, sym
,
503 ind
+ 1, R_386_PC32
);
506 /* generate bound check local freeing */
507 o(0x5250); /* save returned value, if any */
508 greloc(cur_text_section
, sym_data
,
510 oad(0xb8, 0); /* mov %eax, xxx */
511 sym
= external_global_sym(TOK___bound_local_delete
, &func_old_type
, 0);
512 greloc(cur_text_section
, sym
,
513 ind
+ 1, R_386_PC32
);
515 o(0x585a); /* restore returned value, if any */
519 if (func_ret_sub
== 0) {
524 g(func_ret_sub
>> 8);
526 /* align local size to word & save local variables */
530 ind
= func_sub_sp_offset
- FUNC_PROLOG_SIZE
;
533 Sym
*sym
= external_global_sym(TOK___chkstk
, &func_old_type
, 0);
534 oad(0xb8, v
); /* mov stacksize, %eax */
535 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
536 greloc(cur_text_section
, sym
, ind
-4, R_386_PC32
);
540 o(0xe58955); /* push %ebp, mov %esp, %ebp */
541 o(0xec81); /* sub esp, stacksize */
543 #if FUNC_PROLOG_SIZE == 10
544 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
550 /* generate a jump to a label */
553 return psym(0xe9, t
);
556 /* generate a jump to a fixed address */
557 void gjmp_addr(int a
)
565 oad(0xe9, a
- ind
- 5);
569 /* generate a test. set 'inv' to invert test. Stack entry is popped */
570 int gtst(int inv
, int t
)
574 v
= vtop
->r
& VT_VALMASK
;
576 /* fast case : can jump directly since flags are set */
578 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
579 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
580 /* && or || optimization */
581 if ((v
& 1) == inv
) {
582 /* insert vtop->c jump list in t */
585 p
= (int *)(cur_text_section
->data
+ *p
);
593 if (is_float(vtop
->type
.t
) ||
594 (vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
598 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
599 /* constant jmp optimization */
600 if ((vtop
->c
.i
!= 0) != inv
)
607 t
= psym(0x85 ^ inv
, t
);
614 /* generate an integer binary operation */
621 case TOK_ADDC1
: /* add with carry generation */
624 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
631 /* XXX: generate inc and dec for smaller code ? */
633 o(0xc0 | (opc
<< 3) | r
);
637 oad(0xc0 | (opc
<< 3) | r
, c
);
643 o((opc
<< 3) | 0x01);
644 o(0xc0 + r
+ fr
* 8);
647 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
653 case TOK_SUBC1
: /* sub with carry generation */
656 case TOK_ADDC2
: /* add with carry use */
659 case TOK_SUBC2
: /* sub with carry use */
676 o(0xaf0f); /* imul fr, r */
677 o(0xc0 + fr
+ r
* 8);
688 opc
= 0xc0 | (opc
<< 3);
689 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
694 c
= vtop
->c
.i
& 0x1f;
695 o(0xc1); /* shl/shr/sar $xxx, r */
699 /* we generate the shift in ecx */
702 o(0xd3); /* shl/shr/sar %cl, r */
713 /* first operand must be in eax */
714 /* XXX: need better constraint for second operand */
720 if (op
== TOK_UMULL
) {
721 o(0xf7); /* mul fr */
726 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
727 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
730 o(0xf799); /* cltd, idiv fr, %eax */
733 if (op
== '%' || op
== TOK_UMOD
)
746 /* generate a floating point operation 'v = t1 op t2' instruction. The
747 two operands are guaranted to have the same floating point type */
748 /* XXX: need to use ST1 too */
751 int a
, ft
, fc
, swapped
, r
;
753 /* convert constants to memory references */
754 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
759 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
762 /* must put at least one value in the floating point register */
763 if ((vtop
[-1].r
& VT_LVAL
) &&
764 (vtop
[0].r
& VT_LVAL
)) {
770 /* swap the stack if needed so that t1 is the register and t2 is
771 the memory reference */
772 if (vtop
[-1].r
& VT_LVAL
) {
776 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
777 /* load on stack second operand */
778 load(TREG_ST0
, vtop
);
779 save_reg(TREG_EAX
); /* eax is used by FP comparison code */
780 if (op
== TOK_GE
|| op
== TOK_GT
)
782 else if (op
== TOK_EQ
|| op
== TOK_NE
)
785 o(0xc9d9); /* fxch %st(1) */
786 o(0xe9da); /* fucompp */
787 o(0xe0df); /* fnstsw %ax */
789 o(0x45e480); /* and $0x45, %ah */
790 o(0x40fC80); /* cmp $0x40, %ah */
791 } else if (op
== TOK_NE
) {
792 o(0x45e480); /* and $0x45, %ah */
793 o(0x40f480); /* xor $0x40, %ah */
795 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
796 o(0x05c4f6); /* test $0x05, %ah */
799 o(0x45c4f6); /* test $0x45, %ah */
806 /* no memory reference possible for long double operations */
807 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
808 load(TREG_ST0
, vtop
);
833 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
834 o(0xde); /* fxxxp %st, %st(1) */
837 /* if saved lvalue, then we must reload it */
839 if ((r
& VT_VALMASK
) == VT_LLOCAL
) {
843 v1
.r
= VT_LOCAL
| VT_LVAL
;
849 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
853 gen_modrm(a
, r
, vtop
->sym
, fc
);
859 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
860 and 'long long' cases. */
861 void gen_cvt_itof(int t
)
865 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
866 /* signed long long to float/double/long double (unsigned case
867 is handled generically) */
868 o(0x50 + vtop
->r2
); /* push r2 */
869 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
870 o(0x242cdf); /* fildll (%esp) */
871 o(0x08c483); /* add $8, %esp */
872 } else if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
873 (VT_INT
| VT_UNSIGNED
)) {
874 /* unsigned int to float/double/long double */
875 o(0x6a); /* push $0 */
877 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
878 o(0x242cdf); /* fildll (%esp) */
879 o(0x08c483); /* add $8, %esp */
881 /* int to float/double/long double */
882 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
883 o(0x2404db); /* fildl (%esp) */
884 o(0x04c483); /* add $4, %esp */
889 /* convert fp to int 't' type */
890 /* XXX: handle long long case */
891 void gen_cvt_ftoi(int t
)
897 ushort_type
.t
= VT_SHORT
| VT_UNSIGNED
;
905 o(0x2dd9); /* ldcw xxx */
906 sym
= external_global_sym(TOK___tcc_int_fpu_control
,
907 &ushort_type
, VT_LVAL
);
908 greloc(cur_text_section
, sym
,
912 oad(0xec81, size
); /* sub $xxx, %esp */
914 o(0x1cdb); /* fistpl */
916 o(0x3cdf); /* fistpll */
918 o(0x2dd9); /* ldcw xxx */
919 sym
= external_global_sym(TOK___tcc_fpu_control
,
920 &ushort_type
, VT_LVAL
);
921 greloc(cur_text_section
, sym
,
926 o(0x58 + r
); /* pop r */
929 vtop
->r
= r
; /* mark reg as used */
930 r2
= get_reg(RC_INT
);
931 o(0x58 + r2
); /* pop r2 */
934 o(0x04c483); /* add $4, %esp */
940 /* convert from one floating point type to another */
941 void gen_cvt_ftof(int t
)
943 /* all we have to do on i386 is to put the float in a register */
947 /* computed goto support */
954 /* bound check support functions */
955 #ifdef CONFIG_TCC_BCHECK
957 /* generate a bounded pointer addition */
958 void gen_bounded_ptr_add(void)
962 /* prepare fast i386 function call (args in eax and edx) */
964 /* save all temporary registers */
967 /* do a fast function call */
968 sym
= external_global_sym(TOK___bound_ptr_add
, &func_old_type
, 0);
969 greloc(cur_text_section
, sym
,
970 ind
+ 1, R_386_PC32
);
972 /* returned pointer is in eax */
974 vtop
->r
= TREG_EAX
| VT_BOUNDED
;
975 /* address of bounding function call point */
976 vtop
->c
.ul
= (cur_text_section
->reloc
->data_offset
- sizeof(Elf32_Rel
));
979 /* patch pointer addition in vtop so that pointer dereferencing is
981 void gen_bounded_ptr_deref(void)
989 /* XXX: put that code in generic part of tcc */
990 if (!is_float(vtop
->type
.t
)) {
991 if (vtop
->r
& VT_LVAL_BYTE
)
993 else if (vtop
->r
& VT_LVAL_SHORT
)
997 size
= type_size(&vtop
->type
, &align
);
999 case 1: func
= TOK___bound_ptr_indir1
; break;
1000 case 2: func
= TOK___bound_ptr_indir2
; break;
1001 case 4: func
= TOK___bound_ptr_indir4
; break;
1002 case 8: func
= TOK___bound_ptr_indir8
; break;
1003 case 12: func
= TOK___bound_ptr_indir12
; break;
1004 case 16: func
= TOK___bound_ptr_indir16
; break;
1006 error("unhandled size when derefencing bounded pointer");
1011 /* patch relocation */
1012 /* XXX: find a better solution ? */
1013 rel
= (Elf32_Rel
*)(cur_text_section
->reloc
->data
+ vtop
->c
.ul
);
1014 sym
= external_global_sym(func
, &func_old_type
, 0);
1016 put_extern_sym(sym
, NULL
, 0, 0);
1017 rel
->r_info
= ELF32_R_INFO(sym
->c
, ELF32_R_TYPE(rel
->r_info
));
1021 /* end of X86 code generator */
1022 /*************************************************************/