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 #define NB_REG_CLASSES 2
26 /* a register can belong to several classes */
27 #define REG_CLASS_INT 0x0001
28 #define REG_CLASS_FLOAT 0x0002
30 /* pretty names for the registers */
38 int reg_classes
[NB_REGS
] = {
39 REG_CLASS_INT
, /* eax */
40 REG_CLASS_INT
, /* ecx */
41 REG_CLASS_INT
, /* edx */
42 REG_CLASS_FLOAT
, /* st0 */
45 /* integer return register for functions */
46 #define FUNC_RET_REG 0
47 /* float return register for functions */
48 #define FUNC_RET_FREG 3
50 /* defined if function parameters must be evaluated in reverse order */
51 #define INVERT_FUNC_PARAMS
53 /* defined if structures are passed as pointers. Otherwise structures
54 are directly pushed on stack. */
55 //#define FUNC_STRUCT_PARAM_AS_PTR
57 /* pointer size, in bytes */
60 /* long double size and alignment, in bytes */
61 #define LDOUBLE_SIZE 12
62 #define LDOUBLE_ALIGN 4
64 /* function call context */
65 typedef struct GFuncContext
{
69 /******************************************************/
92 /* add a new relocation entry to symbol 's' */
93 void greloc(Sym
*s
, int addr
, int type
)
96 p
= malloc(sizeof(Reloc
));
101 p
->next
= (Reloc
*)s
->c
;
105 /* patch each relocation entry with value 'val' */
106 void greloc_patch(Sym
*s
, int val
)
115 *(int *)p
->addr
= val
;
118 *(int *)p
->addr
= val
- p
->addr
- 4;
128 /* output a symbol and patch all calls to it */
133 n
= *(int *)t
; /* next value */
134 *(int *)t
= a
- t
- 4;
144 /* psym is used to put an instruction with a data field which is a
145 reference to a symbol. It is in fact the same as oad ! */
148 /* instruction + 4 bytes data. Return the address of the data */
149 int oad(int c
, int s
)
158 /* output constant with relocation if 't & VT_FORWARD' is true */
159 void gen_addr32(int c
, int t
)
161 if (!(t
& VT_FORWARD
)) {
164 greloc((Sym
*)c
, ind
, RELOC_ADDR32
);
169 /* XXX: generate correct pointer for forward references to functions */
171 void load(int r
, int ft
, int fc
)
177 if (v
== VT_LLOCAL
) {
178 load(r
, VT_LOCAL
| VT_LVAL
, fc
);
181 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
184 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
187 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
190 } else if ((ft
& VT_TYPE
) == VT_BYTE
)
191 o(0xbe0f); /* movsbl */
192 else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
))
193 o(0xb60f); /* movzbl */
194 else if ((ft
& VT_TYPE
) == VT_SHORT
)
195 o(0xbf0f); /* movswl */
196 else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
))
197 o(0xb70f); /* movzwl */
202 o(0x05 + r
* 8); /* 0xXX, r */
204 } else if (v
== VT_LOCAL
) {
205 oad(0x85 + r
* 8, fc
); /* xx(%ebp), r */
207 g(0x00 + r
* 8 + v
); /* (v), r */
211 o(0xb8 + r
); /* mov $xx, r */
213 } else if (v
== VT_LOCAL
) {
215 oad(0x85 + r
* 8, fc
); /* lea xxx(%ebp), r */
216 } else if (v
== VT_CMP
) {
217 oad(0xb8 + r
, 0); /* mov $0, r */
218 o(0x0f); /* setxx %br */
221 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
223 oad(0xb8 + r
, t
); /* mov $1, r */
224 oad(0xe9, 5); /* jmp after */
226 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
229 o(0xc0 + r
+ v
* 8); /* mov v, r */
235 /* WARNING: r must not be allocated on the stack */
236 void store(r
, ft
, fc
)
240 fr
= ft
& VT_VALMASK
;
242 /* XXX: incorrect if reg to reg */
243 /* XXX: should not flush float stack */
244 if (bt
== VT_FLOAT
) {
247 } else if (bt
== VT_DOUBLE
) {
250 } else if (bt
== VT_LDOUBLE
) {
251 o(0xc0d9); /* fld %st(0) */
262 if (fr
== VT_CONST
) {
263 o(0x05 + r
* 8); /* mov r,xxx */
265 } else if (fr
== VT_LOCAL
) {
266 oad(0x85 + r
* 8, fc
); /* mov r,xxx(%ebp) */
267 } else if (ft
& VT_LVAL
) {
268 g(fr
+ r
* 8); /* mov r, (fr) */
269 } else if (fr
!= r
) {
270 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
274 /* start function call and return function call context */
275 void gfunc_start(GFuncContext
*c
)
280 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
282 void gfunc_param(GFuncContext
*c
)
286 if ((vtop
->t
& (VT_BTYPE
| VT_LVAL
)) == (VT_STRUCT
| VT_LVAL
)) {
287 size
= type_size(vtop
->t
, &align
);
288 /* align to stack align size */
289 size
= (size
+ 3) & ~3;
290 /* allocate the necessary size on stack */
291 oad(0xec81, size
); /* sub $xxx, %esp */
292 /* generate structure store */
293 r
= get_reg(REG_CLASS_INT
);
294 o(0x89); /* mov %esp, r */
299 c
->args_size
+= size
;
300 } else if (is_float(vtop
->t
)) {
301 gv(); /* only one float register */
302 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
304 else if ((vtop
->t
& VT_BTYPE
) == VT_DOUBLE
)
308 oad(0xec81, size
); /* sub $xxx, %esp */
312 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
315 c
->args_size
+= size
;
317 /* simple type (currently always same size) */
318 /* XXX: implicit cast ? */
320 o(0x50 + r
); /* push r */
326 /* generate function call with address in (vtop->t, vtop->c) and free function
327 context. Stack entry is popped */
328 void gfunc_call(GFuncContext
*c
)
331 if ((vtop
->t
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
333 /* forward reference */
334 if (vtop
->t
& VT_FORWARD
) {
335 greloc(vtop
->c
.sym
, ind
+ 1, RELOC_REL32
);
338 oad(0xe8, vtop
->c
.ul
- ind
- 5);
341 /* otherwise, indirect call */
343 o(0xff); /* call *r */
347 oad(0xc481, c
->args_size
); /* add $xxx, %esp */
353 return psym(0xe9, t
);
356 /* generate a test. set 'inv' to invert test. Stack entry is popped */
357 int gtst(int inv
, int t
)
360 v
= vtop
->t
& VT_VALMASK
;
362 /* fast case : can jump directly since flags are set */
364 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
365 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
366 /* && or || optimization */
367 if ((v
& 1) == inv
) {
368 /* insert vtop->c jump list in t */
378 } else if ((vtop
->t
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
379 /* constant jmp optimization */
380 if ((vtop
->c
.i
!= 0) != inv
)
388 t
= psym(0x85 ^ inv
, t
);
394 /* generate an integer binary operation */
407 o(0xc0 + r
+ fr
* 8);
408 } else if (op
== '-') {
410 o(0xc0 + r
+ fr
* 8);
411 } else if (op
== '&') {
413 o(0xc0 + r
+ fr
* 8);
414 } else if (op
== '^') {
416 o(0xc0 + r
+ fr
* 8);
417 } else if (op
== '|') {
419 o(0xc0 + r
+ fr
* 8);
420 } else if (op
== '*') {
421 o(0xaf0f); /* imul fr, r */
422 o(0xc0 + fr
+ r
* 8);
423 } else if (op
== TOK_SHL
| op
== TOK_SHR
| op
== TOK_SAR
) {
429 o(0x87); /* xchg r, %ecx */
434 o(0xd3); /* shl/shr/sar %cl, r */
437 else if (op
== TOK_SHR
)
441 vtop
->t
= (vtop
->t
& VT_TYPE
) | r
;
442 } else if (op
== '/' | op
== TOK_UDIV
| op
== TOK_PDIV
|
443 op
== '%' | op
== TOK_UMOD
) {
444 save_reg(2); /* save edx */
445 t
= save_reg_forced(fr
); /* save fr and get op2 location */
446 move_reg(0, r
); /* op1 is %eax */
447 if (op
== TOK_UDIV
| op
== TOK_UMOD
) {
448 o(0xf7d231); /* xor %edx, %edx, div t(%ebp), %eax */
451 o(0xf799); /* cltd, idiv t(%ebp), %eax */
454 if (op
== '%' | op
== TOK_UMOD
)
458 vtop
->t
= (vtop
->t
& VT_TYPE
) | r
;
462 o(0xc0 + r
+ fr
* 8); /* cmp fr, r */
467 /* generate a floating point operation 'v = t1 op t2' instruction. The
468 two operands are guaranted to have the same floating point type */
469 /* NOTE: currently floats can only be lvalues */
472 int a
, ft
, fc
, swapped
, r
;
474 /* convert constants to memory references */
475 if ((vtop
[-1].t
& (VT_CONST
| VT_LVAL
)) == VT_CONST
) {
480 if ((vtop
[0].t
& (VT_CONST
| VT_LVAL
)) == VT_CONST
)
483 /* must put at least one value in the floating point register */
484 if ((vtop
[-1].t
& VT_LVAL
) &&
485 (vtop
[0].t
& VT_LVAL
)) {
490 if (op
>= TOK_EQ
&& op
<= TOK_GT
) {
491 /* load on stack second operand */
492 load(REG_ST0
, vtop
->t
, vtop
->c
.ul
);
493 if (op
== TOK_GE
|| op
== TOK_GT
)
494 o(0xc9d9); /* fxch %st(1) */
495 o(0xe9da); /* fucompp */
496 o(0xe0df); /* fnstsw %ax */
498 o(0x45e480); /* and $0x45, %ah */
499 o(0x40fC80); /* cmp $0x40, %ah */
500 } else if (op
== TOK_NE
) {
501 o(0x45e480); /* and $0x45, %ah */
502 o(0x40f480); /* xor $0x40, %ah */
504 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
505 o(0x05c4f6); /* test $0x05, %ah */
508 o(0x45c4f6); /* test $0x45, %ah */
512 vtop
->t
= (vtop
->t
& VT_TYPE
) | VT_CMP
;
515 /* swap the stack if needed so that t1 is the register and t2 is
516 the memory reference */
518 if (vtop
[-1].t
& VT_LVAL
) {
544 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
553 } else if (r
== VT_LOCAL
) {
562 /* convert integers to fp 't' type */
563 void gen_cvt_itof(int t
)
566 if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
)) {
567 /* unsigned int to float/double/long double */
568 o(0x6a); /* push $0 */
570 o(0x50 + (vtop
->t
& VT_VALMASK
)); /* push r */
571 o(0x242cdf); /* fildll (%esp) */
572 o(0x08c483); /* add $8, %esp */
574 /* int to float/double/long double */
575 o(0x50 + (vtop
->t
& VT_VALMASK
)); /* push r */
576 o(0x2404db); /* fildl (%esp) */
577 o(0x04c483); /* add $4, %esp */
579 vtop
->t
= t
| REG_ST0
;
582 /* FPU control word for rounding to nearest mode */
583 /* XXX: should move that into tcc lib support code ! */
584 static unsigned short __tcc_fpu_control
= 0x137f;
585 /* FPU control word for round to zero mode for int convertion */
586 static unsigned short __tcc_int_fpu_control
= 0x137f | 0x0c00;
588 /* convert fp to int 't' type */
589 /* XXX: handle long long case */
590 void gen_cvt_ftoi(int t
)
595 if (t
== VT_INT
| VT_UNSIGNED
&&
596 t
== VT_LLONG
| VT_UNSIGNED
&&
602 r
= get_reg(REG_CLASS_INT
);
603 oad(0x2dd9, (int)&__tcc_int_fpu_control
); /* ldcw xxx */
604 oad(0xec81, size
); /* sub $xxx, %esp */
606 o(0x1cdb); /* fistpl */
608 o(0x3cdb); /* fistpll */
610 oad(0x2dd9, (int)&__tcc_fpu_control
); /* ldcw xxx */
611 o(0x58 + r
); /* pop r */
613 o(0x04c483); /* add $4, %esp */
617 /* convert from one floating point type to another */
618 void gen_cvt_ftof(int t
)
620 /* all we have to do on i386 is to put the float in a register */
624 /* pop stack value */
627 /* for x86, we need to pop the FP stack */
628 if ((vtop
->t
& VT_VALMASK
) == REG_ST0
) {
629 o(0xd9dd); /* fstp %st(1) */
636 /* end of X86 code generator */
637 /*************************************************************/