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 /******************************************************/
96 /* patch relocation entry with value 'val' */
97 void greloc_patch1(Reloc
*p
, int val
)
101 *(int *)p
->addr
= val
;
104 *(int *)p
->addr
= val
- p
->addr
- 4;
109 /* output a symbol and patch all calls to it */
114 n
= *(int *)t
; /* next value */
115 *(int *)t
= a
- t
- 4;
125 /* psym is used to put an instruction with a data field which is a
126 reference to a symbol. It is in fact the same as oad ! */
129 /* instruction + 4 bytes data. Return the address of the data */
130 int oad(int c
, int s
)
139 /* output constant with relocation if 'r & VT_FORWARD' is true */
140 void gen_addr32(int r
, int c
)
142 if (!(r
& VT_FORWARD
)) {
145 greloc((Sym
*)c
, ind
, RELOC_ADDR32
);
150 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
152 void gen_modrm(int op_reg
, int r
, int c
)
154 op_reg
= op_reg
<< 3;
155 if ((r
& VT_VALMASK
) == VT_CONST
) {
156 /* constant memory reference */
159 } else if ((r
& VT_VALMASK
) == VT_LOCAL
) {
160 /* currently, we use only ebp as base */
162 /* short reference */
166 oad(0x85 | op_reg
, c
);
169 g(0x00 | op_reg
| (r
& VT_VALMASK
));
174 /* load 'r' from value 'sv' */
175 void load(int r
, SValue
*sv
)
177 int v
, t
, ft
, fc
, fr
;
186 if (v
== VT_LLOCAL
) {
188 v1
.r
= VT_LOCAL
| VT_LVAL
;
193 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
196 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
199 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
202 } else if ((ft
& VT_TYPE
) == VT_BYTE
)
203 o(0xbe0f); /* movsbl */
204 else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
))
205 o(0xb60f); /* movzbl */
206 else if ((ft
& VT_TYPE
) == VT_SHORT
)
207 o(0xbf0f); /* movswl */
208 else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
))
209 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
)
281 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
283 void gfunc_param(GFuncContext
*c
)
287 if ((vtop
->t
& VT_BTYPE
) == VT_STRUCT
) {
288 size
= type_size(vtop
->t
, &align
);
289 /* align to stack align size */
290 size
= (size
+ 3) & ~3;
291 /* allocate the necessary size on stack */
292 oad(0xec81, size
); /* sub $xxx, %esp */
293 /* generate structure store */
295 o(0x89); /* mov %esp, r */
300 c
->args_size
+= size
;
301 } else if (is_float(vtop
->t
)) {
302 gv(RC_FLOAT
); /* only one float register */
303 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
305 else if ((vtop
->t
& VT_BTYPE
) == VT_DOUBLE
)
309 oad(0xec81, size
); /* sub $xxx, %esp */
313 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
316 c
->args_size
+= size
;
318 /* simple type (currently always same size) */
319 /* XXX: implicit cast ? */
321 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
323 o(0x50 + vtop
->r2
); /* push r */
327 o(0x50 + r
); /* push r */
328 c
->args_size
+= size
;
333 /* generate function call with address in (vtop->t, vtop->c) and free function
334 context. Stack entry is popped */
335 void gfunc_call(GFuncContext
*c
)
338 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
340 /* forward reference */
341 if (vtop
->r
& VT_FORWARD
) {
342 greloc(vtop
->c
.sym
, ind
+ 1, RELOC_REL32
);
345 oad(0xe8, vtop
->c
.ul
- ind
- 5);
348 /* otherwise, indirect call */
350 o(0xff); /* call *r */
354 oad(0xc481, c
->args_size
); /* add $xxx, %esp */
360 return psym(0xe9, t
);
363 /* generate a test. set 'inv' to invert test. Stack entry is popped */
364 int gtst(int inv
, int t
)
367 v
= vtop
->r
& VT_VALMASK
;
369 /* fast case : can jump directly since flags are set */
371 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
372 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
373 /* && or || optimization */
374 if ((v
& 1) == inv
) {
375 /* insert vtop->c jump list in t */
386 if (is_float(vtop
->t
)) {
390 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
391 /* constant jmp optimization */
392 if ((vtop
->c
.i
!= 0) != inv
)
399 t
= psym(0x85 ^ inv
, t
);
406 /* generate an integer binary operation */
413 case TOK_ADDC1
: /* add with carry generation */
419 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
423 /* XXX: generate inc and dec for smaller code ? */
425 o(0xc0 | (opc
<< 3) | r
);
429 oad(0xc0 | (opc
<< 3) | r
, c
);
433 o((opc
<< 3) | 0x01);
434 o(0xc0 + r
+ fr
* 8);
437 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
439 vset(VT_INT
, VT_CMP
, op
);
443 case TOK_SUBC1
: /* sub with carry generation */
446 case TOK_ADDC2
: /* add with carry use */
449 case TOK_SUBC2
: /* sub with carry use */
467 o(0xaf0f); /* imul fr, r */
468 o(0xc0 + fr
+ r
* 8);
482 opc
= 0xc0 | (opc
<< 3);
483 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_FORWARD
)) == VT_CONST
) {
485 c
= vtop
->c
.i
& 0x1f;
486 o(0xc1); /* shl/shr/sar $xxx, r */
490 /* we generate the shift in ecx */
492 /* the first op may have been spilled, so we reload it if
497 o(0xd3); /* shl/shr/sar %cl, r */
510 r
= gv(RC_EAX
); /* first operand must be in eax */
512 /* XXX: need better constraint */
513 fr
= gv(RC_ECX
); /* second operand in ecx */
515 r
= gv(RC_EAX
); /* reload first operand if flushed */
519 if (op
== TOK_UMULL
) {
520 o(0xf7); /* mul fr */
525 if (op
== TOK_UDIV
|| op
== TOK_UMOD
) {
526 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
529 o(0xf799); /* cltd, idiv fr, %eax */
532 if (op
== '%' || op
== TOK_UMOD
)
545 /* generate a floating point operation 'v = t1 op t2' instruction. The
546 two operands are guaranted to have the same floating point type */
547 /* NOTE: currently floats can only be lvalues */
550 int a
, ft
, fc
, swapped
;
552 /* convert constants to memory references */
553 if ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
558 if ((vtop
[0].r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
)
561 /* must put at least one value in the floating point register */
562 if ((vtop
[-1].r
& VT_LVAL
) &&
563 (vtop
[0].r
& VT_LVAL
)) {
568 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
569 /* load on stack second operand */
571 if (op
== TOK_GE
|| op
== TOK_GT
)
572 o(0xc9d9); /* fxch %st(1) */
573 o(0xe9da); /* fucompp */
574 o(0xe0df); /* fnstsw %ax */
576 o(0x45e480); /* and $0x45, %ah */
577 o(0x40fC80); /* cmp $0x40, %ah */
578 } else if (op
== TOK_NE
) {
579 o(0x45e480); /* and $0x45, %ah */
580 o(0x40f480); /* xor $0x40, %ah */
582 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
583 o(0x05c4f6); /* test $0x05, %ah */
586 o(0x45c4f6); /* test $0x45, %ah */
594 /* swap the stack if needed so that t1 is the register and t2 is
595 the memory reference */
596 if (vtop
[-1].r
& VT_LVAL
) {
600 /* no memory reference possible for long double operations */
601 if ((vtop
->t
& VT_BTYPE
) == VT_LDOUBLE
) {
627 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
628 o(0xde); /* fxxxp %st, %st(1) */
631 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
635 gen_modrm(a
, vtop
->r
, fc
);
641 /* FPU control word for rounding to nearest mode */
642 /* XXX: should move that into tcc lib support code ! */
643 static unsigned short __tcc_fpu_control
= 0x137f;
644 /* FPU control word for round to zero mode for int convertion */
645 static unsigned short __tcc_int_fpu_control
= 0x137f | 0x0c00;
647 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
648 and 'long long' cases. */
649 void gen_cvt_itof(int t
)
652 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
653 /* signed long long to float/double/long double (unsigned case
654 is handled generically) */
655 o(0x50 + vtop
->r2
); /* push r2 */
656 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
657 o(0x242cdf); /* fildll (%esp) */
658 o(0x08c483); /* add $8, %esp */
659 } else if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
660 (VT_INT
| VT_UNSIGNED
)) {
661 /* unsigned int to float/double/long double */
662 o(0x6a); /* push $0 */
664 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
665 o(0x242cdf); /* fildll (%esp) */
666 o(0x08c483); /* add $8, %esp */
668 /* int to float/double/long double */
669 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
670 o(0x2404db); /* fildl (%esp) */
671 o(0x04c483); /* add $4, %esp */
676 /* convert fp to int 't' type */
677 /* XXX: handle long long case */
678 void gen_cvt_ftoi(int t
)
688 oad(0x2dd9, (int)&__tcc_int_fpu_control
); /* ldcw xxx */
689 oad(0xec81, size
); /* sub $xxx, %esp */
691 o(0x1cdb); /* fistpl */
693 o(0x3cdf); /* fistpll */
695 oad(0x2dd9, (int)&__tcc_fpu_control
); /* ldcw xxx */
697 o(0x58 + r
); /* pop r */
700 vtop
->r
= r
; /* mark reg as used */
701 r2
= get_reg(RC_INT
);
702 o(0x58 + r2
); /* pop r2 */
705 o(0x04c483); /* add $4, %esp */
711 /* convert from one floating point type to another */
712 void gen_cvt_ftof(int t
)
714 /* all we have to do on i386 is to put the float in a register */
718 /* end of X86 code generator */
719 /*************************************************************/