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
) {
261 if (fr
== VT_CONST
) {
262 o(0x05 + r
* 8); /* mov r,xxx */
264 } else if (fr
== VT_LOCAL
) {
265 oad(0x85 + r
* 8, fc
); /* mov r,xxx(%ebp) */
266 } else if (ft
& VT_LVAL
) {
267 g(fr
+ r
* 8); /* mov r, (fr) */
268 } else if (fr
!= r
) {
269 o(0xc0 + fr
+ r
* 8); /* mov r, fr */
273 /* start function call and return function call context */
274 void gfunc_start(GFuncContext
*c
)
279 /* push function parameter which is in (vt, vc) */
280 void gfunc_param(GFuncContext
*c
)
282 int size
, align
, ft
, fc
, r
;
284 if ((vt
& (VT_BTYPE
| VT_LVAL
)) == (VT_STRUCT
| VT_LVAL
)) {
285 size
= type_size(vt
, &align
);
286 /* align to stack align size */
287 size
= (size
+ 3) & ~3;
288 /* allocate the necessary size on stack */
289 oad(0xec81, size
); /* sub $xxx, %esp */
290 /* generate structure store */
291 r
= get_reg(REG_CLASS_INT
);
292 o(0x89); /* mov %esp, r */
301 c
->args_size
+= size
;
302 } else if ((vt
& VT_BTYPE
) == VT_LDOUBLE
||
303 (vt
& VT_BTYPE
) == VT_DOUBLE
||
304 (vt
& VT_BTYPE
) == VT_FLOAT
) {
305 gv(); /* only one float register */
306 if ((vt
& VT_BTYPE
) == VT_FLOAT
)
308 else if ((vt
& VT_BTYPE
) == VT_DOUBLE
)
312 oad(0xec81, size
); /* sub $xxx, %esp */
316 o(0x5cd9 + size
- 4); /* fstp[s|l] 0(%esp) */
319 c
->args_size
+= size
;
321 /* simple type (currently always same size) */
322 /* XXX: implicit cast ? */
324 o(0x50 + r
); /* push r */
329 /* generate function call with address in (vt, vc) and free function
331 void gfunc_call(GFuncContext
*c
)
334 if ((vt
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
336 /* forward reference */
337 if (vt
& VT_FORWARD
) {
338 greloc((Sym
*)vc
, ind
+ 1, RELOC_REL32
);
341 oad(0xe8, vc
- ind
- 5);
344 /* otherwise, indirect call */
346 o(0xff); /* call *r */
350 oad(0xc481, c
->args_size
); /* add $xxx, %esp */
355 return psym(0xe9, t
);
358 /* generate a test. set 'inv' to invert test */
359 int gtst(int inv
, int t
)
364 /* fast case : can jump directly since flags are set */
366 t
= psym((vc
- 16) ^ inv
, t
);
367 } else if (v
== VT_JMP
|| v
== VT_JMPI
) {
368 /* && or || optimization */
369 if ((v
& 1) == inv
) {
370 /* insert vc jump list in t */
380 } else if ((vt
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
381 /* constant jmp optimization */
382 if ((vc
!= 0) != inv
)
390 t
= psym(0x85 ^ inv
, t
);
395 /* generate an integer binary operation 'v = r op fr' instruction and
396 modifies (vt,vc) if needed */
397 void gen_opi(int op
, int r
, int fr
)
402 o(0xc0 + r
+ fr
* 8);
403 } else if (op
== '-') {
405 o(0xc0 + r
+ fr
* 8);
406 } else if (op
== '&') {
408 o(0xc0 + r
+ fr
* 8);
409 } else if (op
== '^') {
411 o(0xc0 + r
+ fr
* 8);
412 } else if (op
== '|') {
414 o(0xc0 + r
+ fr
* 8);
415 } else if (op
== '*') {
416 o(0xaf0f); /* imul fr, r */
417 o(0xc0 + fr
+ r
* 8);
418 } else if (op
== TOK_SHL
| op
== TOK_SHR
| op
== TOK_SAR
) {
424 o(0x87); /* xchg r, %ecx */
429 o(0xd3); /* shl/shr/sar %cl, r */
432 else if (op
== TOK_SHR
)
436 vt
= (vt
& VT_TYPE
) | r
;
437 } else if (op
== '/' | op
== TOK_UDIV
| op
== TOK_PDIV
|
438 op
== '%' | op
== TOK_UMOD
) {
439 save_reg(2); /* save edx */
440 t
= save_reg_forced(fr
); /* save fr and get op2 location */
441 move_reg(0, r
); /* op1 is %eax */
442 if (op
== TOK_UDIV
| op
== TOK_UMOD
) {
443 o(0xf7d231); /* xor %edx, %edx, div t(%ebp), %eax */
446 o(0xf799); /* cltd, idiv t(%ebp), %eax */
449 if (op
== '%' | op
== TOK_UMOD
)
453 vt
= (vt
& VT_TYPE
) | r
;
456 o(0xc0 + r
+ fr
* 8); /* cmp fr, r */
461 /* generate a floating point operation 'v = t1 op t2' instruction and
462 modifies (vt,vc) if needed */
463 /* NOTE: floats can only be lvalues */
466 int a
, ft
, fc
, swapped
, r
;
468 /* must put at least one value in the floating point register */
469 if ((vstack_ptr
[-4] & VT_LVAL
) &&
470 (vstack_ptr
[-2] & VT_LVAL
)) {
477 if (op
>= TOK_EQ
&& op
<= TOK_GT
) {
478 /* load on stack second operand */
479 load(REG_ST0
, vstack_ptr
[-2], vstack_ptr
[-1]);
480 if (op
== TOK_GE
|| op
== TOK_GT
)
481 o(0xc9d9); /* fxch %st(1) */
482 o(0xe9da); /* fucompp */
483 o(0xe0df); /* fnstsw %ax */
485 o(0x45e480); /* and $0x45, %ah */
486 o(0x40fC80); /* cmp $0x40, %ah */
487 } else if (op
== TOK_NE
) {
488 o(0x45e480); /* and $0x45, %ah */
489 o(0x40f480); /* xor $0x40, %ah */
491 } else if (op
== TOK_GE
|| op
== TOK_LE
) {
492 o(0x05c4f6); /* test $0x05, %ah */
495 o(0x45c4f6); /* test $0x45, %ah */
498 vstack_ptr
[-4] = (vstack_ptr
[-4] & VT_TYPE
) | VT_CMP
;
501 /* swap the stack if needed so that t1 is the register and t2 is
502 the memory reference */
504 if (vstack_ptr
[-4] & VT_LVAL
) {
530 if ((ft
& VT_BTYPE
) == VT_DOUBLE
)
539 } else if (r
== VT_LOCAL
) {
549 /* convert integers to floating point (float or double) */
552 if ((vt
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
)) {
553 /* unsigned int to float/double/long double */
554 o(0x6a); /* push $0 */
556 o(0x50 + (vt
& VT_VALMASK
)); /* push r */
557 o(0x242cdf); /* fildll (%esp) */
558 o(0x08c483); /* add $8, %esp */
560 /* int to float/double/long double */
561 o(0x50 + (vt
& VT_VALMASK
)); /* push r */
562 o(0x2404db); /* fildl (%esp) */
563 o(0x04c483); /* add $4, %esp */
567 /* end of X86 code generator */
568 /*************************************************************/