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 */
25 #define RC_INT 0x0001 /* generic integer register */
26 #define RC_FLOAT 0x0002 /* generic float register */
28 #define RC_FRET 0x0008 /* function return: float register */
31 #define RC_IRET RC_EAX /* function return: integer register */
32 #define RC_LRET RC_EDX /* function return: second integer register */
34 /* pretty names for the registers */
42 int reg_classes
[NB_REGS
] = {
43 /* eax */ RC_INT
| RC_IRET
,
44 /* ecx */ RC_INT
| RC_ECX
,
45 /* edx */ RC_INT
| RC_EDX
,
46 /* st0 */ RC_FLOAT
| RC_FRET
,
49 /* return registers for function */
50 #define REG_IRET REG_EAX /* single word int return register */
51 #define REG_LRET REG_EDX /* second word return register (for long long) */
52 #define REG_FRET REG_ST0 /* float return register */
54 /* defined if function parameters must be evaluated in reverse order */
55 #define INVERT_FUNC_PARAMS
57 /* defined if structures are passed as pointers. Otherwise structures
58 are directly pushed on stack. */
59 //#define FUNC_STRUCT_PARAM_AS_PTR
61 /* pointer size, in bytes */
64 /* long double size and alignment, in bytes */
65 #define LDOUBLE_SIZE 12
66 #define LDOUBLE_ALIGN 4
68 /* function call context */
69 typedef struct GFuncContext
{
73 /******************************************************/
75 static int *func_sub_sp_ptr
;
98 /* patch relocation entry with value 'val' */
99 void greloc_patch1(Reloc
*p
, int val
)
103 *(int *)p
->addr
= val
;
106 *(int *)p
->addr
= val
- p
->addr
- 4;
111 /* output a symbol and patch all calls to it */
116 n
= *(int *)t
; /* next value */
117 *(int *)t
= a
- t
- 4;
127 /* psym is used to put an instruction with a data field which is a
128 reference to a symbol. It is in fact the same as oad ! */
131 /* instruction + 4 bytes data. Return the address of the data */
132 int oad(int c
, int s
)
141 /* output constant with relocation if 'r & VT_FORWARD' is true */
142 void gen_addr32(int r
, int c
)
144 if (!(r
& VT_FORWARD
)) {
147 greloc((Sym
*)c
, ind
, RELOC_ADDR32
);
152 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
154 void gen_modrm(int op_reg
, int r
, int c
)
156 op_reg
= op_reg
<< 3;
157 if ((r
& VT_VALMASK
) == VT_CONST
) {
158 /* constant memory reference */
161 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
162 /* currently, we use only ebp as base */
164 /* short reference */
168 oad(0x85 | op_reg
, c
);
171 g(0x00 | op_reg
| (r
& VT_VALMASK
));
176 /* load 'r' from value 'sv' */
177 void load(int r
, SValue
*sv
)
179 int v
, t
, ft
, fc
, fr
;
188 if (v
== VT_LLOCAL
) {
190 v1
.r
= VT_LOCAL
| VT_LVAL
;
195 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
198 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
201 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
204 } else if ((ft
& VT_TYPE
) == VT_BYTE
)
205 o(0xbe0f); /* movsbl */
206 else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
))
207 o(0xb60f); /* movzbl */
208 else if ((ft
& VT_TYPE
) == VT_SHORT
)
209 o(0xbf0f); /* movswl */
210 else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
))
211 o(0xb70f); /* movzwl */
214 gen_modrm(r
, fr
, fc
);
217 o(0xb8 + r
); /* mov $xx, r */
219 } else if (v
== VT_LOCAL
) {
220 o(0x8d); /* lea xxx(%ebp), r */
221 gen_modrm(r
, VT_LOCAL
, fc
);
222 } else if (v
== VT_CMP
) {
223 oad(0xb8 + r
, 0); /* mov $0, r */
224 o(0x0f); /* setxx %br */
227 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
229 oad(0xb8 + r
, t
); /* mov $1, r */
230 oad(0xe9, 5); /* jmp after */
232 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
235 o(0xc0 + r
+ v
* 8); /* mov v, r */
240 /* store register 'r' in lvalue 'v' */
241 void store(int r
, SValue
*v
)
247 fr
= v
->r
& VT_VALMASK
;
249 /* XXX: incorrect if float reg to reg */
250 if (bt
== VT_FLOAT
) {
253 } else if (bt
== VT_DOUBLE
) {
256 } else if (bt
== VT_LDOUBLE
) {
257 o(0xc0d9); /* fld %st(0) */
268 if (fr
== VT_CONST
||
271 gen_modrm(r
, v
->r
, fc
);
272 } else if (fr
!= r
) {
273 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
277 /* start function call and return function call context */
278 void gfunc_start(GFuncContext
*c
)
283 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
285 void gfunc_param(GFuncContext
*c
)
289 if ((vtop
->t
& VT_BTYPE
) == VT_STRUCT
) {
290 size
= type_size(vtop
->t
, &align
);
291 /* align to stack align size */
292 size
= (size
+ 3) & ~3;
293 /* allocate the necessary size on stack */
294 oad(0xec81, size
); /* sub $xxx, %esp */
295 /* generate structure store */
297 o(0x89); /* mov %esp, r */
302 c
->args_size
+= size
;
303 } else if (is_float(vtop
->t
)) {
304 gv(RC_FLOAT
); /* only one float register */
305 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
307 else if ((vtop
->t
& VT_BTYPE
) == VT_DOUBLE
)
311 oad(0xec81, size
); /* sub $xxx, %esp */
315 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
318 c
->args_size
+= size
;
320 /* simple type (currently always same size) */
321 /* XXX: implicit cast ? */
323 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
325 o(0x50 + vtop
->r2
); /* push r */
329 o(0x50 + r
); /* push r */
330 c
->args_size
+= size
;
335 /* generate function call with address in (vtop->t, vtop->c) and free function
336 context. Stack entry is popped */
337 void gfunc_call(GFuncContext
*c
)
340 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
342 /* forward reference */
343 if (vtop
->r
& VT_FORWARD
) {
344 greloc(vtop
->c
.sym
, ind
+ 1, RELOC_REL32
);
347 oad(0xe8, vtop
->c
.ul
- ind
- 5);
350 /* otherwise, indirect call */
352 o(0xff); /* call *r */
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
;
366 sym
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
368 /* if the function returns a structure, then add an
369 implicit pointer parameter */
371 if ((func_vt
& VT_BTYPE
) == VT_STRUCT
) {
375 /* define parameters */
376 while ((sym
= sym
->next
) != NULL
) {
378 sym_push(sym
->v
& ~SYM_FIELD
, u
,
379 VT_LOCAL
| VT_LVAL
, addr
);
380 size
= type_size(u
, &align
);
381 size
= (size
+ 3) & ~3;
382 #ifdef FUNC_STRUCT_PARAM_AS_PTR
383 /* structs are passed as pointer */
384 if ((u
& VT_BTYPE
) == VT_STRUCT
) {
390 o(0xe58955); /* push %ebp, mov %esp, %ebp */
391 func_sub_sp_ptr
= (int *)oad(0xec81, 0); /* sub $xxx, %esp */
394 /* generate function epilog */
395 void gfunc_epilog(void)
397 o(0xc3c9); /* leave, ret */
398 *func_sub_sp_ptr
= (-loc
+ 3) & -4; /* align local size to word &
399 save local variables */
404 return psym(0xe9, t
);
407 /* generate a test. set 'inv' to invert test. Stack entry is popped */
408 int gtst(int inv
, int t
)
411 v
= vtop
->r
& VT_VALMASK
;
413 /* fast case : can jump directly since flags are set */
415 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
416 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
417 /* && or || optimization */
418 if ((v
& 1) == inv
) {
419 /* insert vtop->c jump list in t */
430 if (is_float(vtop
->t
)) {
434 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
435 /* constant jmp optimization */
436 if ((vtop
->c
.i
!= 0) != inv
)
443 t
= psym(0x85 ^ inv
, t
);
450 /* generate an integer binary operation */
457 case TOK_ADDC1
: /* add with carry generation */
463 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
467 /* XXX: generate inc and dec for smaller code ? */
469 o(0xc0 | (opc
<< 3) | r
);
473 oad(0xc0 | (opc
<< 3) | r
, c
);
477 o((opc
<< 3) | 0x01);
478 o(0xc0 + r
+ fr
* 8);
481 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
483 vset(VT_INT
, VT_CMP
, op
);
487 case TOK_SUBC1
: /* sub with carry generation */
490 case TOK_ADDC2
: /* add with carry use */
493 case TOK_SUBC2
: /* sub with carry use */
511 o(0xaf0f); /* imul fr, r */
512 o(0xc0 + fr
+ r
* 8);
526 opc
= 0xc0 | (opc
<< 3);
527 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
529 c
= vtop
->c
.i
& 0x1f;
530 o(0xc1); /* shl/shr/sar $xxx, r */
534 /* we generate the shift in ecx */
536 /* the first op may have been spilled, so we reload it if
541 o(0xd3); /* shl/shr/sar %cl, r */
554 r
= gv(RC_EAX
); /* first operand must be in eax */
556 /* XXX: need better constraint */
557 fr
= gv(RC_ECX
); /* second operand in ecx */
559 r
= gv(RC_EAX
); /* reload first operand if flushed */
563 if (op
== TOK_UMULL
) {
564 o(0xf7); /* mul fr */
569 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
570 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
573 o(0xf799); /* cltd, idiv fr, %eax */
576 if (op
== '%' || op
== TOK_UMOD
)
589 /* generate a floating point operation 'v = t1 op t2' instruction. The
590 two operands are guaranted to have the same floating point type */
591 /* NOTE: currently floats can only be lvalues */
594 int a
, ft
, fc
, swapped
;
596 /* convert constants to memory references */
597 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
602 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
605 /* must put at least one value in the floating point register */
606 if ((vtop
[-1].r
& VT_LVAL
) &&
607 (vtop
[0].r
& VT_LVAL
)) {
612 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
613 /* load on stack second operand */
615 if (op
== TOK_GE
|| op
== TOK_GT
)
616 o(0xc9d9); /* fxch %st(1) */
617 o(0xe9da); /* fucompp */
618 o(0xe0df); /* fnstsw %ax */
620 o(0x45e480); /* and $0x45, %ah */
621 o(0x40fC80); /* cmp $0x40, %ah */
622 } else if (op
== TOK_NE
) {
623 o(0x45e480); /* and $0x45, %ah */
624 o(0x40f480); /* xor $0x40, %ah */
626 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
627 o(0x05c4f6); /* test $0x05, %ah */
630 o(0x45c4f6); /* test $0x45, %ah */
638 /* swap the stack if needed so that t1 is the register and t2 is
639 the memory reference */
640 if (vtop
[-1].r
& VT_LVAL
) {
644 /* no memory reference possible for long double operations */
645 if ((vtop
->t
& VT_BTYPE
) == VT_LDOUBLE
) {
671 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
672 o(0xde); /* fxxxp %st, %st(1) */
675 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
679 gen_modrm(a
, vtop
->r
, fc
);
685 /* FPU control word for rounding to nearest mode */
686 /* XXX: should move that into tcc lib support code ! */
687 static unsigned short __tcc_fpu_control
= 0x137f;
688 /* FPU control word for round to zero mode for int convertion */
689 static unsigned short __tcc_int_fpu_control
= 0x137f | 0x0c00;
691 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
692 and 'long long' cases. */
693 void gen_cvt_itof(int t
)
696 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
697 /* signed long long to float/double/long double (unsigned case
698 is handled generically) */
699 o(0x50 + vtop
->r2
); /* push r2 */
700 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
701 o(0x242cdf); /* fildll (%esp) */
702 o(0x08c483); /* add $8, %esp */
703 } else if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
704 (VT_INT
| VT_UNSIGNED
)) {
705 /* unsigned int to float/double/long double */
706 o(0x6a); /* push $0 */
708 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
709 o(0x242cdf); /* fildll (%esp) */
710 o(0x08c483); /* add $8, %esp */
712 /* int to float/double/long double */
713 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
714 o(0x2404db); /* fildl (%esp) */
715 o(0x04c483); /* add $4, %esp */
720 /* convert fp to int 't' type */
721 /* XXX: handle long long case */
722 void gen_cvt_ftoi(int t
)
732 oad(0x2dd9, (int)&__tcc_int_fpu_control
); /* ldcw xxx */
733 oad(0xec81, size
); /* sub $xxx, %esp */
735 o(0x1cdb); /* fistpl */
737 o(0x3cdf); /* fistpll */
739 oad(0x2dd9, (int)&__tcc_fpu_control
); /* ldcw xxx */
741 o(0x58 + r
); /* pop r */
744 vtop
->r
= r
; /* mark reg as used */
745 r2
= get_reg(RC_INT
);
746 o(0x58 + r2
); /* pop r2 */
749 o(0x04c483); /* add $4, %esp */
755 /* convert from one floating point type to another */
756 void gen_cvt_ftof(int t
)
758 /* all we have to do on i386 is to put the float in a register */
762 /* end of X86 code generator */
763 /*************************************************************/