update
[tinycc.git] / i386-gen.c
blob11651d887cb139fc52ba0782f1dc83e3db87b275
1 /*
2 * X86 code generator for TCC
3 *
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 */
22 #define NB_REGS 4
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 */
31 enum {
32 REG_EAX = 0,
33 REG_ECX,
34 REG_EDX,
35 REG_ST0,
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 */
58 #define PTR_SIZE 4
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 {
66 int args_size;
67 } GFuncContext;
69 /******************************************************/
71 void g(int c)
73 *(char *)ind++ = c;
76 void o(int c)
78 while (c) {
79 g(c);
80 c = c / 256;
84 void gen_le32(int c)
86 g(c);
87 g(c >> 8);
88 g(c >> 16);
89 g(c >> 24);
92 /* add a new relocation entry to symbol 's' */
93 void greloc(Sym *s, int addr, int type)
95 Reloc *p;
96 p = malloc(sizeof(Reloc));
97 if (!p)
98 error("memory full");
99 p->type = type;
100 p->addr = addr;
101 p->next = (Reloc *)s->c;
102 s->c = (int)p;
105 /* patch each relocation entry with value 'val' */
106 void greloc_patch(Sym *s, int val)
108 Reloc *p, *p1;
110 p = (Reloc *)s->c;
111 while (p != NULL) {
112 p1 = p->next;
113 switch(p->type) {
114 case RELOC_ADDR32:
115 *(int *)p->addr = val;
116 break;
117 case RELOC_REL32:
118 *(int *)p->addr = val - p->addr - 4;
119 break;
121 free(p);
122 p = p1;
124 s->c = val;
125 s->t &= ~VT_FORWARD;
128 /* output a symbol and patch all calls to it */
129 void gsym_addr(t, a)
131 int n;
132 while (t) {
133 n = *(int *)t; /* next value */
134 *(int *)t = a - t - 4;
135 t = n;
139 void gsym(t)
141 gsym_addr(t, ind);
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 ! */
146 #define psym oad
148 /* instruction + 4 bytes data. Return the address of the data */
149 int oad(int c, int s)
151 o(c);
152 *(int *)ind = s;
153 s = ind;
154 ind = ind + 4;
155 return 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)) {
162 gen_le32(c);
163 } else {
164 greloc((Sym *)c, ind, RELOC_ADDR32);
165 gen_le32(0);
169 /* XXX: generate correct pointer for forward references to functions */
170 /* r = (ft, fc) */
171 void load(int r, int ft, int fc)
173 int v, t;
175 v = ft & VT_VALMASK;
176 if (ft & VT_LVAL) {
177 if (v == VT_LLOCAL) {
178 load(r, VT_LOCAL | VT_LVAL, fc);
179 v = r;
181 if ((ft & VT_BTYPE) == VT_FLOAT) {
182 o(0xd9); /* flds */
183 r = 0;
184 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
185 o(0xdd); /* fldl */
186 r = 0;
187 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
188 o(0xdb); /* fldt */
189 r = 5;
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 */
198 else
199 o(0x8b); /* movl */
201 if (v == VT_CONST) {
202 o(0x05 + r * 8); /* 0xXX, r */
203 gen_addr32(fc, ft);
204 } else if (v == VT_LOCAL) {
205 oad(0x85 + r * 8, fc); /* xx(%ebp), r */
206 } else {
207 g(0x00 + r * 8 + v); /* (v), r */
209 } else {
210 if (v == VT_CONST) {
211 o(0xb8 + r); /* mov $xx, r */
212 gen_addr32(fc, ft);
213 } else if (v == VT_LOCAL) {
214 o(0x8d);
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 */
219 o(fc);
220 o(0xc0 + r);
221 } else if (v == VT_JMP || v == VT_JMPI) {
222 t = v & 1;
223 oad(0xb8 + r, t); /* mov $1, r */
224 oad(0xe9, 5); /* jmp after */
225 gsym(fc);
226 oad(0xb8 + r, t ^ 1); /* mov $0, r */
227 } else if (v != r) {
228 o(0x89);
229 o(0xc0 + r + v * 8); /* mov v, r */
234 /* (ft, fc) = r */
235 /* WARNING: r must not be allocated on the stack */
236 void store(r, ft, fc)
238 int fr, bt;
240 fr = ft & VT_VALMASK;
241 bt = ft & VT_BTYPE;
242 /* XXX: incorrect if reg to reg */
243 /* XXX: should not flush float stack */
244 if (bt == VT_FLOAT) {
245 o(0xd9); /* fsts */
246 r = 2;
247 } else if (bt == VT_DOUBLE) {
248 o(0xdd); /* fstpl */
249 r = 2;
250 } else if (bt == VT_LDOUBLE) {
251 o(0xc0d9); /* fld %st(0) */
252 o(0xdb); /* fstpt */
253 r = 7;
254 } else {
255 if (bt == VT_SHORT)
256 o(0x66);
257 if (bt == VT_BYTE)
258 o(0x88);
259 else
260 o(0x89);
262 if (fr == VT_CONST) {
263 o(0x05 + r * 8); /* mov r,xxx */
264 gen_addr32(fc, ft);
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)
277 c->args_size = 0;
280 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
281 is then popped. */
282 void gfunc_param(GFuncContext *c)
284 int size, align, r;
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 */
295 o(0xe0 + r);
296 vset(VT_INT | r, 0);
297 vswap();
298 vstore();
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)
303 size = 4;
304 else if ((vtop->t & VT_BTYPE) == VT_DOUBLE)
305 size = 8;
306 else
307 size = 12;
308 oad(0xec81, size); /* sub $xxx, %esp */
309 if (size == 12)
310 o(0x7cdb);
311 else
312 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
313 g(0x24);
314 g(0x00);
315 c->args_size += size;
316 } else {
317 /* simple type (currently always same size) */
318 /* XXX: implicit cast ? */
319 r = gv();
320 o(0x50 + r); /* push r */
321 c->args_size += 4;
323 vtop--;
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)
330 int r;
331 if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
332 /* constant case */
333 /* forward reference */
334 if (vtop->t & VT_FORWARD) {
335 greloc(vtop->c.sym, ind + 1, RELOC_REL32);
336 oad(0xe8, 0);
337 } else {
338 oad(0xe8, vtop->c.ul - ind - 5);
340 } else {
341 /* otherwise, indirect call */
342 r = gv();
343 o(0xff); /* call *r */
344 o(0xd0 + r);
346 if (c->args_size)
347 oad(0xc481, c->args_size); /* add $xxx, %esp */
348 vtop--;
351 int gjmp(int t)
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)
359 int v, *p;
360 v = vtop->t & VT_VALMASK;
361 if (v == VT_CMP) {
362 /* fast case : can jump directly since flags are set */
363 g(0x0f);
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 */
369 p = &vtop->c.i;
370 while (*p != 0)
371 p = (int *)*p;
372 *p = t;
373 t = vtop->c.i;
374 } else {
375 t = gjmp(t);
376 gsym(vtop->c.i);
378 } else if ((vtop->t & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
379 /* constant jmp optimization */
380 if ((vtop->c.i != 0) != inv)
381 t = gjmp(t);
382 } else {
383 /* XXX: floats */
384 v = gv();
385 o(0x85);
386 o(0xc0 + v * 9);
387 g(0x0f);
388 t = psym(0x85 ^ inv, t);
390 vtop--;
391 return t;
394 /* generate an integer binary operation */
395 void gen_opi(int op)
397 int t, r, fr;
399 vswap();
400 r = gv();
401 vswap();
402 fr = gv();
403 vtop--;
405 if (op == '+') {
406 o(0x01);
407 o(0xc0 + r + fr * 8);
408 } else if (op == '-') {
409 o(0x29);
410 o(0xc0 + r + fr * 8);
411 } else if (op == '&') {
412 o(0x21);
413 o(0xc0 + r + fr * 8);
414 } else if (op == '^') {
415 o(0x31);
416 o(0xc0 + r + fr * 8);
417 } else if (op == '|') {
418 o(0x09);
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) {
424 /* op2 is %ecx */
425 if (fr != 1) {
426 if (r == 1) {
427 r = fr;
428 fr = 1;
429 o(0x87); /* xchg r, %ecx */
430 o(0xc1 + r * 8);
431 } else
432 move_reg(1, fr);
434 o(0xd3); /* shl/shr/sar %cl, r */
435 if (op == TOK_SHL)
436 o(0xe0 + r);
437 else if (op == TOK_SHR)
438 o(0xe8 + r);
439 else
440 o(0xf8 + r);
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 */
449 oad(0xb5, t);
450 } else {
451 o(0xf799); /* cltd, idiv t(%ebp), %eax */
452 oad(0xbd, t);
454 if (op == '%' | op == TOK_UMOD)
455 r = 2;
456 else
457 r = 0;
458 vtop->t = (vtop->t & VT_TYPE) | r;
459 } else {
460 vtop--;
461 o(0x39);
462 o(0xc0 + r + fr * 8); /* cmp fr, r */
463 vset(VT_CMP, op);
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 */
470 void gen_opf(int op)
472 int a, ft, fc, swapped, r;
474 /* convert constants to memory references */
475 if ((vtop[-1].t & (VT_CONST | VT_LVAL)) == VT_CONST) {
476 vswap();
477 gv();
478 vswap();
480 if ((vtop[0].t & (VT_CONST | VT_LVAL)) == VT_CONST)
481 gv();
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)) {
486 vswap();
487 gv();
488 vswap();
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 */
497 if (op == TOK_EQ) {
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 */
503 op = TOK_NE;
504 } else if (op == TOK_GE || op == TOK_LE) {
505 o(0x05c4f6); /* test $0x05, %ah */
506 op = TOK_EQ;
507 } else {
508 o(0x45c4f6); /* test $0x45, %ah */
509 op = TOK_EQ;
511 vtop--;
512 vtop->t = (vtop->t & VT_TYPE) | VT_CMP;
513 vtop->c.i = op;
514 } else {
515 /* swap the stack if needed so that t1 is the register and t2 is
516 the memory reference */
517 swapped = 0;
518 if (vtop[-1].t & VT_LVAL) {
519 vswap();
520 swapped = 1;
523 switch(op) {
524 default:
525 case '+':
526 a = 0;
527 break;
528 case '-':
529 a = 0x20;
530 if (swapped)
531 a += 8;
532 break;
533 case '*':
534 a = 0x08;
535 break;
536 case '/':
537 a = 0x30;
538 if (swapped)
539 a += 8;
540 break;
542 ft = vtop->t;
543 fc = vtop->c.ul;
544 if ((ft & VT_BTYPE) == VT_DOUBLE)
545 o(0xdc);
546 else
547 o(0xd8);
549 r = ft & VT_VALMASK;
550 if (r == VT_CONST) {
551 o(0x05 + a);
552 gen_addr32(fc, ft);
553 } else if (r == VT_LOCAL) {
554 oad(0x85 + a, fc);
555 } else {
556 g(0x00 + a + r);
558 vtop--;
562 /* convert integers to fp 't' type */
563 void gen_cvt_itof(int t)
565 gv();
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 */
569 g(0x00);
570 o(0x50 + (vtop->t & VT_VALMASK)); /* push r */
571 o(0x242cdf); /* fildll (%esp) */
572 o(0x08c483); /* add $8, %esp */
573 } else {
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)
592 int r, size;
594 gv();
595 if (t == VT_INT | VT_UNSIGNED &&
596 t == VT_LLONG | VT_UNSIGNED &&
597 t == VT_LLONG)
598 size = 8;
599 else
600 size = 4;
602 r = get_reg(REG_CLASS_INT);
603 oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */
604 oad(0xec81, size); /* sub $xxx, %esp */
605 if (size == 4)
606 o(0x1cdb); /* fistpl */
607 else
608 o(0x3cdb); /* fistpll */
609 o(0x24);
610 oad(0x2dd9, (int)&__tcc_fpu_control); /* ldcw xxx */
611 o(0x58 + r); /* pop r */
612 if (size == 8)
613 o(0x04c483); /* add $4, %esp */
614 vtop->t = t | r;
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 */
621 gv();
624 /* pop stack value */
625 void vpop(void)
627 /* for x86, we need to pop the FP stack */
628 if ((vtop->t & VT_VALMASK) == REG_ST0) {
629 o(0xd9dd); /* fstp %st(1) */
631 vtop--;
636 /* end of X86 code generator */
637 /*************************************************************/