fixed floating poing compare - use LVAL type - optimized bound checking
[tinycc/miki.git] / i386-gen.c
blobd6c86ac379a64e5534ef08ab8b78734f0a5543cf
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 /* a register can belong to several classes. The classes must be
25 sorted from more general to more precise (see gv2() code which does
26 assumptions on it). */
27 #define RC_INT 0x0001 /* generic integer register */
28 #define RC_FLOAT 0x0002 /* generic float register */
29 #define RC_EAX 0x0004
30 #define RC_ST0 0x0008
31 #define RC_ECX 0x0010
32 #define RC_EDX 0x0020
33 #define RC_IRET RC_EAX /* function return: integer register */
34 #define RC_LRET RC_EDX /* function return: second integer register */
35 #define RC_FRET RC_ST0 /* function return: float register */
37 /* pretty names for the registers */
38 enum {
39 REG_EAX = 0,
40 REG_ECX,
41 REG_EDX,
42 REG_ST0,
45 int reg_classes[NB_REGS] = {
46 /* eax */ RC_INT | RC_EAX,
47 /* ecx */ RC_INT | RC_ECX,
48 /* edx */ RC_INT | RC_EDX,
49 /* st0 */ RC_FLOAT | RC_ST0,
52 /* return registers for function */
53 #define REG_IRET REG_EAX /* single word int return register */
54 #define REG_LRET REG_EDX /* second word return register (for long long) */
55 #define REG_FRET REG_ST0 /* float return register */
57 /* defined if function parameters must be evaluated in reverse order */
58 #define INVERT_FUNC_PARAMS
60 /* defined if structures are passed as pointers. Otherwise structures
61 are directly pushed on stack. */
62 //#define FUNC_STRUCT_PARAM_AS_PTR
64 /* pointer size, in bytes */
65 #define PTR_SIZE 4
67 /* long double size and alignment, in bytes */
68 #define LDOUBLE_SIZE 12
69 #define LDOUBLE_ALIGN 4
71 /* function call context */
72 typedef struct GFuncContext {
73 int args_size;
74 int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
75 } GFuncContext;
77 /******************************************************/
79 static int *func_sub_sp_ptr;
80 static unsigned char *func_bound_ptr;
81 static int func_ret_sub;
83 void g(int c)
85 *(char *)ind++ = c;
88 void o(int c)
90 while (c) {
91 g(c);
92 c = c / 256;
96 void gen_le32(int c)
98 g(c);
99 g(c >> 8);
100 g(c >> 16);
101 g(c >> 24);
104 /* patch relocation entry with value 'val' */
105 void greloc_patch1(Reloc *p, int val)
107 switch(p->type) {
108 case RELOC_ADDR32:
109 *(int *)p->addr = val;
110 break;
111 case RELOC_REL32:
112 *(int *)p->addr = val - p->addr - 4;
113 break;
117 /* output a symbol and patch all calls to it */
118 void gsym_addr(t, a)
120 int n;
121 while (t) {
122 n = *(int *)t; /* next value */
123 *(int *)t = a - t - 4;
124 t = n;
128 void gsym(t)
130 gsym_addr(t, ind);
133 /* psym is used to put an instruction with a data field which is a
134 reference to a symbol. It is in fact the same as oad ! */
135 #define psym oad
137 /* instruction + 4 bytes data. Return the address of the data */
138 int oad(int c, int s)
140 o(c);
141 *(int *)ind = s;
142 s = ind;
143 ind = ind + 4;
144 return s;
147 /* output constant with relocation if 'r & VT_FORWARD' is true */
148 void gen_addr32(int r, int c)
150 if (!(r & VT_FORWARD)) {
151 gen_le32(c);
152 } else {
153 greloc((Sym *)c, ind, RELOC_ADDR32);
154 gen_le32(0);
158 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
159 opcode bits */
160 void gen_modrm(int op_reg, int r, int c)
162 op_reg = op_reg << 3;
163 if ((r & VT_VALMASK) == VT_CONST) {
164 /* constant memory reference */
165 o(0x05 | op_reg);
166 gen_addr32(r, c);
167 } else if ((r & VT_VALMASK) == VT_LOCAL) {
168 /* currently, we use only ebp as base */
169 if (c == (char)c) {
170 /* short reference */
171 o(0x45 | op_reg);
172 g(c);
173 } else {
174 oad(0x85 | op_reg, c);
176 } else {
177 g(0x00 | op_reg | (r & VT_VALMASK));
182 /* load 'r' from value 'sv' */
183 void load(int r, SValue *sv)
185 int v, t, ft, fc, fr;
186 SValue v1;
188 fr = sv->r;
189 ft = sv->t;
190 fc = sv->c.ul;
192 v = fr & VT_VALMASK;
193 if (fr & VT_LVAL) {
194 if (v == VT_LLOCAL) {
195 v1.t = VT_INT;
196 v1.r = VT_LOCAL | VT_LVAL;
197 v1.c.ul = fc;
198 load(r, &v1);
199 fr = r;
201 if ((ft & VT_BTYPE) == VT_FLOAT) {
202 o(0xd9); /* flds */
203 r = 0;
204 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
205 o(0xdd); /* fldl */
206 r = 0;
207 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
208 o(0xdb); /* fldt */
209 r = 5;
210 } else if ((ft & VT_TYPE) == VT_BYTE) {
211 o(0xbe0f); /* movsbl */
212 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
213 o(0xb60f); /* movzbl */
214 } else if ((ft & VT_TYPE) == VT_SHORT) {
215 o(0xbf0f); /* movswl */
216 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
217 o(0xb70f); /* movzwl */
218 } else {
219 o(0x8b); /* movl */
221 gen_modrm(r, fr, fc);
222 } else {
223 if (v == VT_CONST) {
224 o(0xb8 + r); /* mov $xx, r */
225 gen_addr32(fr, fc);
226 } else if (v == VT_LOCAL) {
227 o(0x8d); /* lea xxx(%ebp), r */
228 gen_modrm(r, VT_LOCAL, fc);
229 } else if (v == VT_CMP) {
230 oad(0xb8 + r, 0); /* mov $0, r */
231 o(0x0f); /* setxx %br */
232 o(fc);
233 o(0xc0 + r);
234 } else if (v == VT_JMP || v == VT_JMPI) {
235 t = v & 1;
236 oad(0xb8 + r, t); /* mov $1, r */
237 oad(0xe9, 5); /* jmp after */
238 gsym(fc);
239 oad(0xb8 + r, t ^ 1); /* mov $0, r */
240 } else if (v != r) {
241 o(0x89);
242 o(0xc0 + r + v * 8); /* mov v, r */
247 /* store register 'r' in lvalue 'v' */
248 void store(int r, SValue *v)
250 int fr, bt, ft, fc;
252 ft = v->t;
253 fc = v->c.ul;
254 fr = v->r & VT_VALMASK;
255 bt = ft & VT_BTYPE;
256 /* XXX: incorrect if float reg to reg */
257 if (bt == VT_FLOAT) {
258 o(0xd9); /* fsts */
259 r = 2;
260 } else if (bt == VT_DOUBLE) {
261 o(0xdd); /* fstpl */
262 r = 2;
263 } else if (bt == VT_LDOUBLE) {
264 o(0xc0d9); /* fld %st(0) */
265 o(0xdb); /* fstpt */
266 r = 7;
267 } else {
268 if (bt == VT_SHORT)
269 o(0x66);
270 if (bt == VT_BYTE)
271 o(0x88);
272 else
273 o(0x89);
275 if (fr == VT_CONST ||
276 fr == VT_LOCAL ||
277 (v->r & VT_LVAL)) {
278 gen_modrm(r, v->r, fc);
279 } else if (fr != r) {
280 o(0xc0 + fr + r * 8); /* mov r, fr */
284 /* start function call and return function call context */
285 void gfunc_start(GFuncContext *c, int func_call)
287 c->args_size = 0;
288 c->func_call = func_call;
291 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
292 is then popped. */
293 void gfunc_param(GFuncContext *c)
295 int size, align, r;
297 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
298 size = type_size(vtop->t, &align);
299 /* align to stack align size */
300 size = (size + 3) & ~3;
301 /* allocate the necessary size on stack */
302 oad(0xec81, size); /* sub $xxx, %esp */
303 /* generate structure store */
304 r = get_reg(RC_INT);
305 o(0x89); /* mov %esp, r */
306 o(0xe0 + r);
307 vset(vtop->t, r | VT_LVAL, 0);
308 vswap();
309 vstore();
310 c->args_size += size;
311 } else if (is_float(vtop->t)) {
312 gv(RC_FLOAT); /* only one float register */
313 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
314 size = 4;
315 else if ((vtop->t & VT_BTYPE) == VT_DOUBLE)
316 size = 8;
317 else
318 size = 12;
319 oad(0xec81, size); /* sub $xxx, %esp */
320 if (size == 12)
321 o(0x7cdb);
322 else
323 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
324 g(0x24);
325 g(0x00);
326 c->args_size += size;
327 } else {
328 /* simple type (currently always same size) */
329 /* XXX: implicit cast ? */
330 r = gv(RC_INT);
331 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
332 size = 8;
333 o(0x50 + vtop->r2); /* push r */
334 } else {
335 size = 4;
337 o(0x50 + r); /* push r */
338 c->args_size += size;
340 vtop--;
343 /* generate function call with address in (vtop->t, vtop->c) and free function
344 context. Stack entry is popped */
345 void gfunc_call(GFuncContext *c)
347 int r;
348 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
349 /* constant case */
350 /* forward reference */
351 if (vtop->r & VT_FORWARD) {
352 greloc(vtop->c.sym, ind + 1, RELOC_REL32);
353 oad(0xe8, 0);
354 } else {
355 oad(0xe8, vtop->c.ul - ind - 5);
357 } else {
358 /* otherwise, indirect call */
359 r = gv(RC_INT);
360 o(0xff); /* call *r */
361 o(0xd0 + r);
363 if (c->args_size && c->func_call == FUNC_CDECL)
364 oad(0xc481, c->args_size); /* add $xxx, %esp */
365 vtop--;
368 /* generate function prolog of type 't' */
369 void gfunc_prolog(int t)
371 int addr, align, size, u, func_call;
372 Sym *sym;
374 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
375 func_call = sym->r;
376 addr = 8;
377 /* if the function returns a structure, then add an
378 implicit pointer parameter */
379 func_vt = sym->t;
380 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
381 func_vc = addr;
382 addr += 4;
384 /* define parameters */
385 while ((sym = sym->next) != NULL) {
386 u = sym->t;
387 sym_push(sym->v & ~SYM_FIELD, u,
388 VT_LOCAL | VT_LVAL, addr);
389 size = type_size(u, &align);
390 size = (size + 3) & ~3;
391 #ifdef FUNC_STRUCT_PARAM_AS_PTR
392 /* structs are passed as pointer */
393 if ((u & VT_BTYPE) == VT_STRUCT) {
394 size = 4;
396 #endif
397 addr += size;
399 func_ret_sub = 0;
400 /* pascal type call ? */
401 if (func_call == FUNC_STDCALL)
402 func_ret_sub = addr - 8;
403 o(0xe58955); /* push %ebp, mov %esp, %ebp */
404 func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
405 /* leave some room for bound checking code */
406 if (do_bounds_check) {
407 oad(0xb8, 0); /* lbound section pointer */
408 oad(0xb8, 0); /* call to function */
409 func_bound_ptr = lbounds_section->data_ptr;
413 /* generate function epilog */
414 void gfunc_epilog(void)
416 #ifdef CONFIG_TCC_BCHECK
417 if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {
418 int saved_ind;
419 int *bounds_ptr;
420 /* add end of table info */
421 bounds_ptr = (int *)lbounds_section->data_ptr;
422 *bounds_ptr++ = 0;
423 lbounds_section->data_ptr = (unsigned char *)bounds_ptr;
424 /* generate bound local allocation */
425 saved_ind = ind;
426 ind = (int)func_sub_sp_ptr + 4;
427 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
428 oad(0xe8, (int)__bound_local_new - ind - 5);
429 ind = saved_ind;
430 /* generate bound check local freeing */
431 o(0x5250); /* save returned value, if any */
432 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
433 oad(0xe8, (int)__bound_local_delete - ind - 5);
434 o(0x585a); /* restore returned value, if any */
436 #endif
437 o(0xc9); /* leave */
438 if (func_ret_sub == 0) {
439 o(0xc3); /* ret */
440 } else {
441 o(0xc2); /* ret n */
442 g(func_ret_sub);
443 g(func_ret_sub >> 8);
445 /* align local size to word & save local variables */
446 *func_sub_sp_ptr = (-loc + 3) & -4;
449 /* generate a jump to a label */
450 int gjmp(int t)
452 return psym(0xe9, t);
455 /* generate a jump to a fixed address */
456 void gjmp_addr(int a)
458 oad(0xe9, a - ind - 5);
461 /* generate a test. set 'inv' to invert test. Stack entry is popped */
462 int gtst(int inv, int t)
464 int v, *p;
465 v = vtop->r & VT_VALMASK;
466 if (v == VT_CMP) {
467 /* fast case : can jump directly since flags are set */
468 g(0x0f);
469 t = psym((vtop->c.i - 16) ^ inv, t);
470 } else if (v == VT_JMP || v == VT_JMPI) {
471 /* && or || optimization */
472 if ((v & 1) == inv) {
473 /* insert vtop->c jump list in t */
474 p = &vtop->c.i;
475 while (*p != 0)
476 p = (int *)*p;
477 *p = t;
478 t = vtop->c.i;
479 } else {
480 t = gjmp(t);
481 gsym(vtop->c.i);
483 } else {
484 if (is_float(vtop->t)) {
485 vpushi(0);
486 gen_op(TOK_NE);
488 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
489 /* constant jmp optimization */
490 if ((vtop->c.i != 0) != inv)
491 t = gjmp(t);
492 } else {
493 v = gv(RC_INT);
494 o(0x85);
495 o(0xc0 + v * 9);
496 g(0x0f);
497 t = psym(0x85 ^ inv, t);
500 vtop--;
501 return t;
504 /* generate an integer binary operation */
505 void gen_opi(int op)
507 int r, fr, opc, c;
509 switch(op) {
510 case '+':
511 case TOK_ADDC1: /* add with carry generation */
512 opc = 0;
513 gen_op8:
514 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
515 /* constant case */
516 vswap();
517 r = gv(RC_INT);
518 vswap();
519 c = vtop->c.i;
520 if (c == (char)c) {
521 /* XXX: generate inc and dec for smaller code ? */
522 o(0x83);
523 o(0xc0 | (opc << 3) | r);
524 g(c);
525 } else {
526 o(0x81);
527 oad(0xc0 | (opc << 3) | r, c);
529 } else {
530 gv2(RC_INT, RC_INT);
531 r = vtop[-1].r;
532 fr = vtop[0].r;
533 o((opc << 3) | 0x01);
534 o(0xc0 + r + fr * 8);
536 vtop--;
537 if (op >= TOK_ULT && op <= TOK_GT) {
538 vtop--;
539 vset(VT_INT, VT_CMP, op);
541 break;
542 case '-':
543 case TOK_SUBC1: /* sub with carry generation */
544 opc = 5;
545 goto gen_op8;
546 case TOK_ADDC2: /* add with carry use */
547 opc = 2;
548 goto gen_op8;
549 case TOK_SUBC2: /* sub with carry use */
550 opc = 3;
551 goto gen_op8;
552 case '&':
553 opc = 4;
554 goto gen_op8;
555 case '^':
556 opc = 6;
557 goto gen_op8;
558 case '|':
559 opc = 1;
560 goto gen_op8;
561 case '*':
562 gv2(RC_INT, RC_INT);
563 r = vtop[-1].r;
564 fr = vtop[0].r;
565 vtop--;
566 o(0xaf0f); /* imul fr, r */
567 o(0xc0 + fr + r * 8);
568 break;
569 case TOK_SHL:
570 opc = 4;
571 goto gen_shift;
572 case TOK_SHR:
573 opc = 5;
574 goto gen_shift;
575 case TOK_SAR:
576 opc = 7;
577 gen_shift:
578 opc = 0xc0 | (opc << 3);
579 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
580 /* constant case */
581 vswap();
582 r = gv(RC_INT);
583 vswap();
584 c = vtop->c.i & 0x1f;
585 o(0xc1); /* shl/shr/sar $xxx, r */
586 o(opc | r);
587 g(c);
588 } else {
589 /* we generate the shift in ecx */
590 gv2(RC_INT, RC_ECX);
591 r = vtop[-1].r;
592 o(0xd3); /* shl/shr/sar %cl, r */
593 o(opc | r);
595 vtop--;
596 break;
597 case '/':
598 case TOK_UDIV:
599 case TOK_PDIV:
600 case '%':
601 case TOK_UMOD:
602 case TOK_UMULL:
603 /* first operand must be in eax */
604 /* XXX: need better constraint for second operand */
605 gv2(RC_EAX, RC_ECX);
606 r = vtop[-1].r;
607 fr = vtop[0].r;
608 vtop--;
609 save_reg(REG_EDX);
610 if (op == TOK_UMULL) {
611 o(0xf7); /* mul fr */
612 o(0xe0 + fr);
613 vtop->r2 = REG_EDX;
614 r = REG_EAX;
615 } else {
616 if (op == TOK_UDIV || op == TOK_UMOD) {
617 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
618 o(0xf0 + fr);
619 } else {
620 o(0xf799); /* cltd, idiv fr, %eax */
621 o(0xf8 + fr);
623 if (op == '%' || op == TOK_UMOD)
624 r = REG_EDX;
625 else
626 r = REG_EAX;
628 vtop->r = r;
629 break;
630 default:
631 opc = 7;
632 goto gen_op8;
636 /* generate a floating point operation 'v = t1 op t2' instruction. The
637 two operands are guaranted to have the same floating point type */
638 /* XXX: need to use ST1 too */
639 void gen_opf(int op)
641 int a, ft, fc, swapped, r;
643 /* convert constants to memory references */
644 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
645 vswap();
646 gv(RC_FLOAT);
647 vswap();
649 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
650 gv(RC_FLOAT);
652 /* must put at least one value in the floating point register */
653 if ((vtop[-1].r & VT_LVAL) &&
654 (vtop[0].r & VT_LVAL)) {
655 vswap();
656 gv(RC_FLOAT);
657 vswap();
659 swapped = 0;
660 /* swap the stack if needed so that t1 is the register and t2 is
661 the memory reference */
662 if (vtop[-1].r & VT_LVAL) {
663 vswap();
664 swapped = 1;
666 if (op >= TOK_ULT && op <= TOK_GT) {
667 /* load on stack second operand */
668 load(REG_ST0, vtop);
669 save_reg(REG_EAX); /* eax is used by FP comparison code */
670 if (op == TOK_GE || op == TOK_GT)
671 swapped = !swapped;
672 else if (op == TOK_EQ || op == TOK_NE)
673 swapped = 0;
674 if (swapped)
675 o(0xc9d9); /* fxch %st(1) */
676 o(0xe9da); /* fucompp */
677 o(0xe0df); /* fnstsw %ax */
678 if (op == TOK_EQ) {
679 o(0x45e480); /* and $0x45, %ah */
680 o(0x40fC80); /* cmp $0x40, %ah */
681 } else if (op == TOK_NE) {
682 o(0x45e480); /* and $0x45, %ah */
683 o(0x40f480); /* xor $0x40, %ah */
684 op = TOK_NE;
685 } else if (op == TOK_GE || op == TOK_LE) {
686 o(0x05c4f6); /* test $0x05, %ah */
687 op = TOK_EQ;
688 } else {
689 o(0x45c4f6); /* test $0x45, %ah */
690 op = TOK_EQ;
692 vtop--;
693 vtop->r = VT_CMP;
694 vtop->c.i = op;
695 } else {
696 /* no memory reference possible for long double operations */
697 if ((vtop->t & VT_BTYPE) == VT_LDOUBLE) {
698 load(REG_ST0, vtop);
699 swapped = !swapped;
702 switch(op) {
703 default:
704 case '+':
705 a = 0;
706 break;
707 case '-':
708 a = 4;
709 if (swapped)
710 a++;
711 break;
712 case '*':
713 a = 1;
714 break;
715 case '/':
716 a = 6;
717 if (swapped)
718 a++;
719 break;
721 ft = vtop->t;
722 fc = vtop->c.ul;
723 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
724 o(0xde); /* fxxxp %st, %st(1) */
725 o(0xc1 + (a << 3));
726 } else {
727 /* if saved lvalue, then we must reload it */
728 r = vtop->r;
729 if ((r & VT_VALMASK) == VT_LLOCAL) {
730 SValue v1;
731 r = get_reg(RC_INT);
732 v1.t = VT_INT;
733 v1.r = VT_LOCAL | VT_LVAL;
734 v1.c.ul = fc;
735 load(r, &v1);
736 fc = 0;
739 if ((ft & VT_BTYPE) == VT_DOUBLE)
740 o(0xdc);
741 else
742 o(0xd8);
743 gen_modrm(a, r, fc);
745 vtop--;
749 /* FPU control word for rounding to nearest mode */
750 /* XXX: should move that into tcc lib support code ! */
751 static unsigned short __tcc_fpu_control = 0x137f;
752 /* FPU control word for round to zero mode for int convertion */
753 static unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00;
755 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
756 and 'long long' cases. */
757 void gen_cvt_itof(int t)
759 save_reg(REG_ST0);
760 gv(RC_INT);
761 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
762 /* signed long long to float/double/long double (unsigned case
763 is handled generically) */
764 o(0x50 + vtop->r2); /* push r2 */
765 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
766 o(0x242cdf); /* fildll (%esp) */
767 o(0x08c483); /* add $8, %esp */
768 } else if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
769 (VT_INT | VT_UNSIGNED)) {
770 /* unsigned int to float/double/long double */
771 o(0x6a); /* push $0 */
772 g(0x00);
773 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
774 o(0x242cdf); /* fildll (%esp) */
775 o(0x08c483); /* add $8, %esp */
776 } else {
777 /* int to float/double/long double */
778 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
779 o(0x2404db); /* fildl (%esp) */
780 o(0x04c483); /* add $4, %esp */
782 vtop->r = REG_ST0;
785 /* convert fp to int 't' type */
786 /* XXX: handle long long case */
787 void gen_cvt_ftoi(int t)
789 int r, r2, size;
791 gv(RC_FLOAT);
792 if (t != VT_INT)
793 size = 8;
794 else
795 size = 4;
797 oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */
798 oad(0xec81, size); /* sub $xxx, %esp */
799 if (size == 4)
800 o(0x1cdb); /* fistpl */
801 else
802 o(0x3cdf); /* fistpll */
803 o(0x24);
804 oad(0x2dd9, (int)&__tcc_fpu_control); /* ldcw xxx */
805 r = get_reg(RC_INT);
806 o(0x58 + r); /* pop r */
807 if (size == 8) {
808 if (t == VT_LLONG) {
809 vtop->r = r; /* mark reg as used */
810 r2 = get_reg(RC_INT);
811 o(0x58 + r2); /* pop r2 */
812 vtop->r2 = r2;
813 } else {
814 o(0x04c483); /* add $4, %esp */
817 vtop->r = r;
820 /* convert from one floating point type to another */
821 void gen_cvt_ftof(int t)
823 /* all we have to do on i386 is to put the float in a register */
824 gv(RC_FLOAT);
827 /* bound check support functions */
828 #ifdef CONFIG_TCC_BCHECK
830 /* generate a bounded pointer addition */
831 void gen_bounded_ptr_add(void)
833 int addr;
834 /* prepare fast i386 function call (args in eax and edx) */
835 gv2(RC_EAX, RC_EDX);
836 /* save all temporary registers */
837 vtop -= 2;
838 save_regs(0);
839 /* do a fast function call */
840 addr = ind;
841 oad(0xe8, (int)__bound_ptr_add - ind - 5);
842 /* returned pointer is in eax */
843 vtop++;
844 vtop->r = REG_EAX | VT_BOUNDED;
845 vtop->c.ul = addr; /* address of bounding function call point */
848 /* patch pointer addition in vtop so that pointer dereferencing is
849 also tested */
850 void gen_bounded_ptr_deref(void)
852 void *func;
853 int size, align, addr;
855 size = 0;
856 /* XXX: put that code in generic part of tcc */
857 if (!is_float(vtop->t)) {
858 if (vtop->r & VT_LVAL_BYTE)
859 size = 1;
860 else if (vtop->r & VT_LVAL_SHORT)
861 size = 2;
863 if (!size)
864 size = type_size(vtop->t, &align);
865 switch(size) {
866 case 1: func = __bound_ptr_indir1; break;
867 case 2: func = __bound_ptr_indir2; break;
868 case 4: func = __bound_ptr_indir4; break;
869 case 8: func = __bound_ptr_indir8; break;
870 case 12: func = __bound_ptr_indir12; break;
871 case 16: func = __bound_ptr_indir16; break;
872 default:
873 error("unhandled size when derefencing bounded pointer");
874 func = NULL;
875 break;
878 addr = vtop->c.ul;
879 *(int *)(addr + 1) = (int)func - addr - 5;
881 #endif
883 /* end of X86 code generator */
884 /*************************************************************/