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 'r & VT_FORWARD' is true */
159 void gen_addr32(int r
, int c
)
161 if (!(r
& VT_FORWARD
)) {
164 greloc((Sym
*)c
, ind
, RELOC_ADDR32
);
169 /* load 'r' from value 'sv' */
170 void load(int r
, SValue
*sv
)
172 int v
, t
, ft
, fc
, fr
;
181 if (v
== VT_LLOCAL
) {
183 v1
.r
= VT_LOCAL
| VT_LVAL
;
188 if ((ft
& VT_BTYPE
) == VT_FLOAT
) {
191 } else if ((ft
& VT_BTYPE
) == VT_DOUBLE
) {
194 } else if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
197 } else if ((ft
& VT_TYPE
) == VT_BYTE
)
198 o(0xbe0f); /* movsbl */
199 else if ((ft
& VT_TYPE
) == (VT_BYTE
| VT_UNSIGNED
))
200 o(0xb60f); /* movzbl */
201 else if ((ft
& VT_TYPE
) == VT_SHORT
)
202 o(0xbf0f); /* movswl */
203 else if ((ft
& VT_TYPE
) == (VT_SHORT
| VT_UNSIGNED
))
204 o(0xb70f); /* movzwl */
209 o(0x05 + r
* 8); /* 0xXX, r */
211 } else if (v
== VT_LOCAL
) {
212 oad(0x85 + r
* 8, fc
); /* xx(%ebp), r */
214 g(0x00 + r
* 8 + v
); /* (v), r */
218 o(0xb8 + r
); /* mov $xx, r */
220 } else if (v
== VT_LOCAL
) {
222 oad(0x85 + r
* 8, fc
); /* lea xxx(%ebp), r */
223 } else if (v
== VT_CMP
) {
224 oad(0xb8 + r
, 0); /* mov $0, r */
225 o(0x0f); /* setxx %br */
228 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
230 oad(0xb8 + r
, t
); /* mov $1, r */
231 oad(0xe9, 5); /* jmp after */
233 oad(0xb8 + r
, t
^ 1); /* mov $0, r */
236 o(0xc0 + r
+ v
* 8); /* mov v, r */
241 /* store register 'r' in lvalue 'v' */
242 void store(int r
, SValue
*v
)
248 fr
= v
->r
& VT_VALMASK
;
250 /* XXX: incorrect if reg to reg */
251 /* XXX: should not flush float stack */
252 if (bt
== VT_FLOAT
) {
255 } else if (bt
== VT_DOUBLE
) {
258 } else if (bt
== VT_LDOUBLE
) {
259 o(0xc0d9); /* fld %st(0) */
270 if (fr
== VT_CONST
) {
271 o(0x05 + r
* 8); /* mov r,xxx */
272 gen_addr32(v
->r
, fc
);
273 } else if (fr
== VT_LOCAL
) {
274 oad(0x85 + r
* 8, fc
); /* mov r,xxx(%ebp) */
275 } else if (v
->r
& VT_LVAL
) {
276 g(fr
+ r
* 8); /* mov r, (fr) */
277 } else if (fr
!= r
) {
278 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
282 /* start function call and return function call context */
283 void gfunc_start(GFuncContext
*c
)
288 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
290 void gfunc_param(GFuncContext
*c
)
294 if ((vtop
->t
& VT_BTYPE
) == VT_STRUCT
) {
295 size
= type_size(vtop
->t
, &align
);
296 /* align to stack align size */
297 size
= (size
+ 3) & ~3;
298 /* allocate the necessary size on stack */
299 oad(0xec81, size
); /* sub $xxx, %esp */
300 /* generate structure store */
301 r
= get_reg(REG_CLASS_INT
);
302 o(0x89); /* mov %esp, r */
307 c
->args_size
+= size
;
308 } else if (is_float(vtop
->t
)) {
309 gv(); /* only one float register */
310 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
312 else if ((vtop
->t
& VT_BTYPE
) == VT_DOUBLE
)
316 oad(0xec81, size
); /* sub $xxx, %esp */
320 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
323 c
->args_size
+= size
;
325 /* simple type (currently always same size) */
326 /* XXX: implicit cast ? */
328 o(0x50 + r
); /* push r */
334 /* generate function call with address in (vtop->t, vtop->c) and free function
335 context. Stack entry is popped */
336 void gfunc_call(GFuncContext
*c
)
339 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
341 /* forward reference */
342 if (vtop
->r
& VT_FORWARD
) {
343 greloc(vtop
->c
.sym
, ind
+ 1, RELOC_REL32
);
346 oad(0xe8, vtop
->c
.ul
- ind
- 5);
349 /* otherwise, indirect call */
351 o(0xff); /* call *r */
355 oad(0xc481, c
->args_size
); /* add $xxx, %esp */
361 return psym(0xe9, t
);
364 /* generate a test. set 'inv' to invert test. Stack entry is popped */
365 int gtst(int inv
, int t
)
368 v
= vtop
->r
& VT_VALMASK
;
370 /* fast case : can jump directly since flags are set */
372 t
= psym((vtop
->c
.i
- 16) ^ inv
, t
);
373 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
374 /* && or || optimization */
375 if ((v
& 1) == inv
) {
376 /* insert vtop->c jump list in t */
386 } else if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
387 /* constant jmp optimization */
388 if ((vtop
->c
.i
!= 0) != inv
)
396 t
= psym(0x85 ^ inv
, t
);
402 /* generate an integer binary operation */
415 o(0xc0 + r
+ fr
* 8);
416 } else if (op
== '-') {
418 o(0xc0 + r
+ fr
* 8);
419 } else if (op
== '&') {
421 o(0xc0 + r
+ fr
* 8);
422 } else if (op
== '^') {
424 o(0xc0 + r
+ fr
* 8);
425 } else if (op
== '|') {
427 o(0xc0 + r
+ fr
* 8);
428 } else if (op
== '*') {
429 o(0xaf0f); /* imul fr, r */
430 o(0xc0 + fr
+ r
* 8);
431 } else if (op
== TOK_SHL
| op
== TOK_SHR
| op
== TOK_SAR
) {
437 o(0x87); /* xchg r, %ecx */
442 o(0xd3); /* shl/shr/sar %cl, r */
445 else if (op
== TOK_SHR
)
450 } else if (op
== '/' | op
== TOK_UDIV
| op
== TOK_PDIV
|
451 op
== '%' | op
== TOK_UMOD
) {
452 save_reg(2); /* save edx */
453 t
= save_reg_forced(fr
); /* save fr and get op2 location */
454 move_reg(0, r
); /* op1 is %eax */
455 if (op
== TOK_UDIV
| op
== TOK_UMOD
) {
456 o(0xf7d231); /* xor %edx, %edx, div t(%ebp), %eax */
459 o(0xf799); /* cltd, idiv t(%ebp), %eax */
462 if (op
== '%' | op
== TOK_UMOD
)
470 o(0xc0 + r
+ fr
* 8); /* cmp fr, r */
471 vset(VT_INT
, VT_CMP
, op
);
475 /* generate a floating point operation 'v = t1 op t2' instruction. The
476 two operands are guaranted to have the same floating point type */
477 /* NOTE: currently floats can only be lvalues */
480 int a
, ft
, fc
, swapped
, r
;
482 /* convert constants to memory references */
483 if ((vtop
[-1].r
& (VT_CONST
| VT_LVAL
)) == VT_CONST
) {
488 if ((vtop
[0].r
& (VT_CONST
| VT_LVAL
)) == VT_CONST
)
491 /* must put at least one value in the floating point register */
492 if ((vtop
[-1].r
& VT_LVAL
) &&
493 (vtop
[0].r
& VT_LVAL
)) {
498 if (op
>= TOK_EQ
&& op
<= TOK_GT
) {
499 /* load on stack second operand */
501 if (op
== TOK_GE
|| op
== TOK_GT
)
502 o(0xc9d9); /* fxch %st(1) */
503 o(0xe9da); /* fucompp */
504 o(0xe0df); /* fnstsw %ax */
506 o(0x45e480); /* and $0x45, %ah */
507 o(0x40fC80); /* cmp $0x40, %ah */
508 } else if (op
== TOK_NE
) {
509 o(0x45e480); /* and $0x45, %ah */
510 o(0x40f480); /* xor $0x40, %ah */
512 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
513 o(0x05c4f6); /* test $0x05, %ah */
516 o(0x45c4f6); /* test $0x45, %ah */
523 /* swap the stack if needed so that t1 is the register and t2 is
524 the memory reference */
526 if (vtop
[-1].r
& VT_LVAL
) {
552 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
557 r
= vtop
->r
& VT_VALMASK
;
560 gen_addr32(vtop
->r
, fc
);
561 } else if (r
== VT_LOCAL
) {
570 /* convert integers to fp 't' type */
571 void gen_cvt_itof(int t
)
574 if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
)) {
575 /* unsigned int to float/double/long double */
576 o(0x6a); /* push $0 */
578 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
579 o(0x242cdf); /* fildll (%esp) */
580 o(0x08c483); /* add $8, %esp */
582 /* int to float/double/long double */
583 o(0x50 + (vtop
->r
& VT_VALMASK
)); /* push r */
584 o(0x2404db); /* fildl (%esp) */
585 o(0x04c483); /* add $4, %esp */
590 /* FPU control word for rounding to nearest mode */
591 /* XXX: should move that into tcc lib support code ! */
592 static unsigned short __tcc_fpu_control
= 0x137f;
593 /* FPU control word for round to zero mode for int convertion */
594 static unsigned short __tcc_int_fpu_control
= 0x137f | 0x0c00;
596 /* convert fp to int 't' type */
597 /* XXX: handle long long case */
598 void gen_cvt_ftoi(int t
)
603 if (t
== VT_INT
| VT_UNSIGNED
&&
604 t
== VT_LLONG
| VT_UNSIGNED
&&
610 r
= get_reg(REG_CLASS_INT
);
611 oad(0x2dd9, (int)&__tcc_int_fpu_control
); /* ldcw xxx */
612 oad(0xec81, size
); /* sub $xxx, %esp */
614 o(0x1cdb); /* fistpl */
616 o(0x3cdb); /* fistpll */
618 oad(0x2dd9, (int)&__tcc_fpu_control
); /* ldcw xxx */
619 o(0x58 + r
); /* pop r */
621 o(0x04c483); /* add $4, %esp */
625 /* convert from one floating point type to another */
626 void gen_cvt_ftof(int t
)
628 /* all we have to do on i386 is to put the float in a register */
632 /* pop stack value */
635 /* for x86, we need to pop the FP stack */
636 if ((vtop
->r
& VT_VALMASK
) == REG_ST0
) {
637 o(0xd9dd); /* fstp %st(1) */
644 /* end of X86 code generator */
645 /*************************************************************/