float parsing + long double
[tinycc.git] / i386-gen.c
blob40f1c2d0e7e035411055c93d59a01090af3d7c88
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); /* fstps */
246 r = 3;
247 } else if (bt == VT_DOUBLE) {
248 o(0xdd); /* fstpl */
249 r = 3;
250 } else if (bt == VT_LDOUBLE) {
251 o(0xdb); /* fstpt */
252 r = 7;
253 } else {
254 if (bt == VT_SHORT)
255 o(0x66);
256 if (bt == VT_BYTE)
257 o(0x88);
258 else
259 o(0x89);
261 if (fr == VT_CONST) {
262 o(0x05 + r * 8); /* mov r,xxx */
263 gen_addr32(fc, ft);
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)
276 c->args_size = 0;
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 */
293 o(0xe0 + r);
294 ft = vt;
295 fc = vc;
296 vset(VT_INT | r, 0);
297 vpush();
298 vt = ft;
299 vc = fc;
300 vstore();
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)
307 size = 4;
308 else if ((vt & VT_BTYPE) == VT_DOUBLE)
309 size = 8;
310 else
311 size = 12;
312 oad(0xec81, size); /* sub $xxx, %esp */
313 if (size == 12)
314 o(0x7cdb);
315 else
316 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
317 g(0x24);
318 g(0x00);
319 c->args_size += size;
320 } else {
321 /* simple type (currently always same size) */
322 /* XXX: implicit cast ? */
323 r = gv();
324 o(0x50 + r); /* push r */
325 c->args_size += 4;
329 /* generate function call with address in (vt, vc) and free function
330 context */
331 void gfunc_call(GFuncContext *c)
333 int r;
334 if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
335 /* constant case */
336 /* forward reference */
337 if (vt & VT_FORWARD) {
338 greloc((Sym *)vc, ind + 1, RELOC_REL32);
339 oad(0xe8, 0);
340 } else {
341 oad(0xe8, vc - ind - 5);
343 } else {
344 /* otherwise, indirect call */
345 r = gv();
346 o(0xff); /* call *r */
347 o(0xd0 + r);
349 if (c->args_size)
350 oad(0xc481, c->args_size); /* add $xxx, %esp */
353 int gjmp(int t)
355 return psym(0xe9, t);
358 /* generate a test. set 'inv' to invert test */
359 int gtst(int inv, int t)
361 int v, *p;
362 v = vt & VT_VALMASK;
363 if (v == VT_CMP) {
364 /* fast case : can jump directly since flags are set */
365 g(0x0f);
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 */
371 p = &vc;
372 while (*p != 0)
373 p = (int *)*p;
374 *p = t;
375 t = vc;
376 } else {
377 t = gjmp(t);
378 gsym(vc);
380 } else if ((vt & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
381 /* constant jmp optimization */
382 if ((vc != 0) != inv)
383 t = gjmp(t);
384 } else {
385 /* XXX: floats */
386 v = gv();
387 o(0x85);
388 o(0xc0 + v * 9);
389 g(0x0f);
390 t = psym(0x85 ^ inv, t);
392 return 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)
399 int t;
400 if (op == '+') {
401 o(0x01);
402 o(0xc0 + r + fr * 8);
403 } else if (op == '-') {
404 o(0x29);
405 o(0xc0 + r + fr * 8);
406 } else if (op == '&') {
407 o(0x21);
408 o(0xc0 + r + fr * 8);
409 } else if (op == '^') {
410 o(0x31);
411 o(0xc0 + r + fr * 8);
412 } else if (op == '|') {
413 o(0x09);
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) {
419 /* op2 is %ecx */
420 if (fr != 1) {
421 if (r == 1) {
422 r = fr;
423 fr = 1;
424 o(0x87); /* xchg r, %ecx */
425 o(0xc1 + r * 8);
426 } else
427 move_reg(1, fr);
429 o(0xd3); /* shl/shr/sar %cl, r */
430 if (op == TOK_SHL)
431 o(0xe0 + r);
432 else if (op == TOK_SHR)
433 o(0xe8 + r);
434 else
435 o(0xf8 + r);
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 */
444 oad(0xb5, t);
445 } else {
446 o(0xf799); /* cltd, idiv t(%ebp), %eax */
447 oad(0xbd, t);
449 if (op == '%' | op == TOK_UMOD)
450 r = 2;
451 else
452 r = 0;
453 vt = (vt & VT_TYPE) | r;
454 } else {
455 o(0x39);
456 o(0xc0 + r + fr * 8); /* cmp fr, r */
457 vset(VT_CMP, op);
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 */
464 void gen_opf(int op)
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)) {
471 vswap();
472 vpop(&vt, &vc);
473 gv();
474 vpush();
475 vswap();
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 */
484 if (op == TOK_EQ) {
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 */
490 op = TOK_NE;
491 } else if (op == TOK_GE || op == TOK_LE) {
492 o(0x05c4f6); /* test $0x05, %ah */
493 op = TOK_EQ;
494 } else {
495 o(0x45c4f6); /* test $0x45, %ah */
496 op = TOK_EQ;
498 vstack_ptr[-4] = (vstack_ptr[-4] & VT_TYPE) | VT_CMP;
499 vstack_ptr[-3] = op;
500 } else {
501 /* swap the stack if needed so that t1 is the register and t2 is
502 the memory reference */
503 swapped = 0;
504 if (vstack_ptr[-4] & VT_LVAL) {
505 vswap();
506 swapped = 1;
509 switch(op) {
510 default:
511 case '+':
512 a = 0;
513 break;
514 case '-':
515 a = 0x20;
516 if (swapped)
517 a += 8;
518 break;
519 case '*':
520 a = 0x08;
521 break;
522 case '/':
523 a = 0x30;
524 if (swapped)
525 a += 8;
526 break;
528 ft = vstack_ptr[-2];
529 fc = vstack_ptr[-1];
530 if ((ft & VT_BTYPE) == VT_DOUBLE)
531 o(0xdc);
532 else
533 o(0xd8);
535 r = ft & VT_VALMASK;
536 if (r == VT_CONST) {
537 o(0x05 + a);
538 gen_addr32(fc, ft);
539 } else if (r == VT_LOCAL) {
540 oad(0x85 + a, fc);
541 } else {
542 g(0x00 + a + r);
545 vstack_ptr -= 2;
546 vpop(&vt, &vc);
549 /* convert integers to floating point (float or double) */
550 void gen_cvtf(int t)
552 if ((vt & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED)) {
553 /* unsigned int to float/double/long double */
554 o(0x6a); /* push $0 */
555 g(0x00);
556 o(0x50 + (vt & VT_VALMASK)); /* push r */
557 o(0x242cdf); /* fildll (%esp) */
558 o(0x08c483); /* add $8, %esp */
559 } else {
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 /*************************************************************/