added better relocation support (needed for file output)
[tinycc.git] / i386-gen.c
blob6631d07de701d0a5a34027dec03646b2ce6dc015
1 /*
2 * X86 code generator for TCC
3 *
4 * Copyright (c) 2001, 2002 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 /* relocation type for 32 bit data relocation */
72 #define R_DATA_32 R_386_32
74 /* function call context */
75 typedef struct GFuncContext {
76 int args_size;
77 int func_call; /* func call type (FUNC_STDCALL or FUNC_CDECL) */
78 } GFuncContext;
80 /******************************************************/
82 static int *func_sub_sp_ptr;
83 static unsigned char *func_bound_ptr;
84 static int func_ret_sub;
86 void g(int c)
88 *(char *)ind++ = c;
91 void o(int c)
93 while (c) {
94 g(c);
95 c = c / 256;
99 void gen_le32(int c)
101 g(c);
102 g(c >> 8);
103 g(c >> 16);
104 g(c >> 24);
107 void greloc_patch(unsigned char *ptr,
108 unsigned long addr, unsigned long val, int type)
110 switch(type) {
111 case R_386_32:
112 *(int *)ptr += val;
113 break;
114 case R_386_PC32:
115 *(int *)ptr += val - addr - 4;
116 break;
120 /* output a symbol and patch all calls to it */
121 void gsym_addr(int t, int a)
123 int n;
124 while (t) {
125 n = *(int *)t; /* next value */
126 *(int *)t = a - t - 4;
127 t = n;
131 void gsym(int t)
133 gsym_addr(t, ind);
136 /* psym is used to put an instruction with a data field which is a
137 reference to a symbol. It is in fact the same as oad ! */
138 #define psym oad
140 /* instruction + 4 bytes data. Return the address of the data */
141 int oad(int c, int s)
143 o(c);
144 *(int *)ind = s;
145 s = ind;
146 ind = ind + 4;
147 return s;
150 /* output constant with relocation if 'r & VT_SYM' is true */
151 void gen_addr32(int r, int c)
153 if (!(r & VT_SYM)) {
154 gen_le32(c);
155 } else {
156 greloc(cur_text_section,
157 (Sym *)c, ind - (int)cur_text_section->data, R_386_32);
158 gen_le32(0);
162 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
163 opcode bits */
164 void gen_modrm(int op_reg, int r, int c)
166 op_reg = op_reg << 3;
167 if ((r & VT_VALMASK) == VT_CONST) {
168 /* constant memory reference */
169 o(0x05 | op_reg);
170 gen_addr32(r, c);
171 } else if ((r & VT_VALMASK) == VT_LOCAL) {
172 /* currently, we use only ebp as base */
173 if (c == (char)c) {
174 /* short reference */
175 o(0x45 | op_reg);
176 g(c);
177 } else {
178 oad(0x85 | op_reg, c);
180 } else {
181 g(0x00 | op_reg | (r & VT_VALMASK));
186 /* load 'r' from value 'sv' */
187 void load(int r, SValue *sv)
189 int v, t, ft, fc, fr;
190 SValue v1;
192 fr = sv->r;
193 ft = sv->t;
194 fc = sv->c.ul;
196 v = fr & VT_VALMASK;
197 if (fr & VT_LVAL) {
198 if (v == VT_LLOCAL) {
199 v1.t = VT_INT;
200 v1.r = VT_LOCAL | VT_LVAL;
201 v1.c.ul = fc;
202 load(r, &v1);
203 fr = r;
205 if ((ft & VT_BTYPE) == VT_FLOAT) {
206 o(0xd9); /* flds */
207 r = 0;
208 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
209 o(0xdd); /* fldl */
210 r = 0;
211 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
212 o(0xdb); /* fldt */
213 r = 5;
214 } else if ((ft & VT_TYPE) == VT_BYTE) {
215 o(0xbe0f); /* movsbl */
216 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
217 o(0xb60f); /* movzbl */
218 } else if ((ft & VT_TYPE) == VT_SHORT) {
219 o(0xbf0f); /* movswl */
220 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
221 o(0xb70f); /* movzwl */
222 } else {
223 o(0x8b); /* movl */
225 gen_modrm(r, fr, fc);
226 } else {
227 if (v == VT_CONST) {
228 o(0xb8 + r); /* mov $xx, r */
229 gen_addr32(fr, fc);
230 } else if (v == VT_LOCAL) {
231 o(0x8d); /* lea xxx(%ebp), r */
232 gen_modrm(r, VT_LOCAL, fc);
233 } else if (v == VT_CMP) {
234 oad(0xb8 + r, 0); /* mov $0, r */
235 o(0x0f); /* setxx %br */
236 o(fc);
237 o(0xc0 + r);
238 } else if (v == VT_JMP || v == VT_JMPI) {
239 t = v & 1;
240 oad(0xb8 + r, t); /* mov $1, r */
241 oad(0xe9, 5); /* jmp after */
242 gsym(fc);
243 oad(0xb8 + r, t ^ 1); /* mov $0, r */
244 } else if (v != r) {
245 o(0x89);
246 o(0xc0 + r + v * 8); /* mov v, r */
251 /* store register 'r' in lvalue 'v' */
252 void store(int r, SValue *v)
254 int fr, bt, ft, fc;
256 ft = v->t;
257 fc = v->c.ul;
258 fr = v->r & VT_VALMASK;
259 bt = ft & VT_BTYPE;
260 /* XXX: incorrect if float reg to reg */
261 if (bt == VT_FLOAT) {
262 o(0xd9); /* fsts */
263 r = 2;
264 } else if (bt == VT_DOUBLE) {
265 o(0xdd); /* fstpl */
266 r = 2;
267 } else if (bt == VT_LDOUBLE) {
268 o(0xc0d9); /* fld %st(0) */
269 o(0xdb); /* fstpt */
270 r = 7;
271 } else {
272 if (bt == VT_SHORT)
273 o(0x66);
274 if (bt == VT_BYTE)
275 o(0x88);
276 else
277 o(0x89);
279 if (fr == VT_CONST ||
280 fr == VT_LOCAL ||
281 (v->r & VT_LVAL)) {
282 gen_modrm(r, v->r, fc);
283 } else if (fr != r) {
284 o(0xc0 + fr + r * 8); /* mov r, fr */
288 /* start function call and return function call context */
289 void gfunc_start(GFuncContext *c, int func_call)
291 c->args_size = 0;
292 c->func_call = func_call;
295 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
296 is then popped. */
297 void gfunc_param(GFuncContext *c)
299 int size, align, r;
301 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
302 size = type_size(vtop->t, &align);
303 /* align to stack align size */
304 size = (size + 3) & ~3;
305 /* allocate the necessary size on stack */
306 oad(0xec81, size); /* sub $xxx, %esp */
307 /* generate structure store */
308 r = get_reg(RC_INT);
309 o(0x89); /* mov %esp, r */
310 o(0xe0 + r);
311 vset(vtop->t, r | VT_LVAL, 0);
312 vswap();
313 vstore();
314 c->args_size += size;
315 } else if (is_float(vtop->t)) {
316 gv(RC_FLOAT); /* only one float register */
317 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
318 size = 4;
319 else if ((vtop->t & VT_BTYPE) == VT_DOUBLE)
320 size = 8;
321 else
322 size = 12;
323 oad(0xec81, size); /* sub $xxx, %esp */
324 if (size == 12)
325 o(0x7cdb);
326 else
327 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
328 g(0x24);
329 g(0x00);
330 c->args_size += size;
331 } else {
332 /* simple type (currently always same size) */
333 /* XXX: implicit cast ? */
334 r = gv(RC_INT);
335 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
336 size = 8;
337 o(0x50 + vtop->r2); /* push r */
338 } else {
339 size = 4;
341 o(0x50 + r); /* push r */
342 c->args_size += size;
344 vtop--;
347 /* generate function call with address in (vtop->t, vtop->c) and free function
348 context. Stack entry is popped */
349 void gfunc_call(GFuncContext *c)
351 int r;
352 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
353 /* constant case */
354 if (vtop->r & VT_SYM) {
355 /* relocation case */
356 greloc(cur_text_section, vtop->c.sym,
357 ind + 1 - (int)cur_text_section->data, R_386_PC32);
358 oad(0xe8, 0);
359 } else {
360 oad(0xe8, vtop->c.ul - ind - 5);
362 } else {
363 /* otherwise, indirect call */
364 r = gv(RC_INT);
365 o(0xff); /* call *r */
366 o(0xd0 + r);
368 if (c->args_size && c->func_call == FUNC_CDECL)
369 oad(0xc481, c->args_size); /* add $xxx, %esp */
370 vtop--;
373 /* generate function prolog of type 't' */
374 void gfunc_prolog(int t)
376 int addr, align, size, u, func_call;
377 Sym *sym;
379 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
380 func_call = sym->r;
381 addr = 8;
382 /* if the function returns a structure, then add an
383 implicit pointer parameter */
384 func_vt = sym->t;
385 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
386 func_vc = addr;
387 addr += 4;
389 /* define parameters */
390 while ((sym = sym->next) != NULL) {
391 u = sym->t;
392 sym_push(sym->v & ~SYM_FIELD, u,
393 VT_LOCAL | VT_LVAL, addr);
394 size = type_size(u, &align);
395 size = (size + 3) & ~3;
396 #ifdef FUNC_STRUCT_PARAM_AS_PTR
397 /* structs are passed as pointer */
398 if ((u & VT_BTYPE) == VT_STRUCT) {
399 size = 4;
401 #endif
402 addr += size;
404 func_ret_sub = 0;
405 /* pascal type call ? */
406 if (func_call == FUNC_STDCALL)
407 func_ret_sub = addr - 8;
408 o(0xe58955); /* push %ebp, mov %esp, %ebp */
409 func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
410 /* leave some room for bound checking code */
411 if (do_bounds_check) {
412 oad(0xb8, 0); /* lbound section pointer */
413 oad(0xb8, 0); /* call to function */
414 func_bound_ptr = lbounds_section->data_ptr;
418 /* generate function epilog */
419 void gfunc_epilog(void)
421 #ifdef CONFIG_TCC_BCHECK
422 if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {
423 int saved_ind;
424 int *bounds_ptr;
425 /* add end of table info */
426 bounds_ptr = (int *)lbounds_section->data_ptr;
427 *bounds_ptr++ = 0;
428 lbounds_section->data_ptr = (unsigned char *)bounds_ptr;
429 /* generate bound local allocation */
430 saved_ind = ind;
431 ind = (int)func_sub_sp_ptr + 4;
432 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
433 oad(0xe8, (int)__bound_local_new - ind - 5);
434 ind = saved_ind;
435 /* generate bound check local freeing */
436 o(0x5250); /* save returned value, if any */
437 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
438 oad(0xe8, (int)__bound_local_delete - ind - 5);
439 o(0x585a); /* restore returned value, if any */
441 #endif
442 o(0xc9); /* leave */
443 if (func_ret_sub == 0) {
444 o(0xc3); /* ret */
445 } else {
446 o(0xc2); /* ret n */
447 g(func_ret_sub);
448 g(func_ret_sub >> 8);
450 /* align local size to word & save local variables */
451 *func_sub_sp_ptr = (-loc + 3) & -4;
454 /* generate a jump to a label */
455 int gjmp(int t)
457 return psym(0xe9, t);
460 /* generate a jump to a fixed address */
461 void gjmp_addr(int a)
463 oad(0xe9, a - ind - 5);
466 /* generate a test. set 'inv' to invert test. Stack entry is popped */
467 int gtst(int inv, int t)
469 int v, *p;
470 v = vtop->r & VT_VALMASK;
471 if (v == VT_CMP) {
472 /* fast case : can jump directly since flags are set */
473 g(0x0f);
474 t = psym((vtop->c.i - 16) ^ inv, t);
475 } else if (v == VT_JMP || v == VT_JMPI) {
476 /* && or || optimization */
477 if ((v & 1) == inv) {
478 /* insert vtop->c jump list in t */
479 p = &vtop->c.i;
480 while (*p != 0)
481 p = (int *)*p;
482 *p = t;
483 t = vtop->c.i;
484 } else {
485 t = gjmp(t);
486 gsym(vtop->c.i);
488 } else {
489 if (is_float(vtop->t)) {
490 vpushi(0);
491 gen_op(TOK_NE);
493 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
494 /* constant jmp optimization */
495 if ((vtop->c.i != 0) != inv)
496 t = gjmp(t);
497 } else {
498 v = gv(RC_INT);
499 o(0x85);
500 o(0xc0 + v * 9);
501 g(0x0f);
502 t = psym(0x85 ^ inv, t);
505 vtop--;
506 return t;
509 /* generate an integer binary operation */
510 void gen_opi(int op)
512 int r, fr, opc, c;
514 switch(op) {
515 case '+':
516 case TOK_ADDC1: /* add with carry generation */
517 opc = 0;
518 gen_op8:
519 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
520 /* constant case */
521 vswap();
522 r = gv(RC_INT);
523 vswap();
524 c = vtop->c.i;
525 if (c == (char)c) {
526 /* XXX: generate inc and dec for smaller code ? */
527 o(0x83);
528 o(0xc0 | (opc << 3) | r);
529 g(c);
530 } else {
531 o(0x81);
532 oad(0xc0 | (opc << 3) | r, c);
534 } else {
535 gv2(RC_INT, RC_INT);
536 r = vtop[-1].r;
537 fr = vtop[0].r;
538 o((opc << 3) | 0x01);
539 o(0xc0 + r + fr * 8);
541 vtop--;
542 if (op >= TOK_ULT && op <= TOK_GT) {
543 vtop--;
544 vset(VT_INT, VT_CMP, op);
546 break;
547 case '-':
548 case TOK_SUBC1: /* sub with carry generation */
549 opc = 5;
550 goto gen_op8;
551 case TOK_ADDC2: /* add with carry use */
552 opc = 2;
553 goto gen_op8;
554 case TOK_SUBC2: /* sub with carry use */
555 opc = 3;
556 goto gen_op8;
557 case '&':
558 opc = 4;
559 goto gen_op8;
560 case '^':
561 opc = 6;
562 goto gen_op8;
563 case '|':
564 opc = 1;
565 goto gen_op8;
566 case '*':
567 gv2(RC_INT, RC_INT);
568 r = vtop[-1].r;
569 fr = vtop[0].r;
570 vtop--;
571 o(0xaf0f); /* imul fr, r */
572 o(0xc0 + fr + r * 8);
573 break;
574 case TOK_SHL:
575 opc = 4;
576 goto gen_shift;
577 case TOK_SHR:
578 opc = 5;
579 goto gen_shift;
580 case TOK_SAR:
581 opc = 7;
582 gen_shift:
583 opc = 0xc0 | (opc << 3);
584 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
585 /* constant case */
586 vswap();
587 r = gv(RC_INT);
588 vswap();
589 c = vtop->c.i & 0x1f;
590 o(0xc1); /* shl/shr/sar $xxx, r */
591 o(opc | r);
592 g(c);
593 } else {
594 /* we generate the shift in ecx */
595 gv2(RC_INT, RC_ECX);
596 r = vtop[-1].r;
597 o(0xd3); /* shl/shr/sar %cl, r */
598 o(opc | r);
600 vtop--;
601 break;
602 case '/':
603 case TOK_UDIV:
604 case TOK_PDIV:
605 case '%':
606 case TOK_UMOD:
607 case TOK_UMULL:
608 /* first operand must be in eax */
609 /* XXX: need better constraint for second operand */
610 gv2(RC_EAX, RC_ECX);
611 r = vtop[-1].r;
612 fr = vtop[0].r;
613 vtop--;
614 save_reg(REG_EDX);
615 if (op == TOK_UMULL) {
616 o(0xf7); /* mul fr */
617 o(0xe0 + fr);
618 vtop->r2 = REG_EDX;
619 r = REG_EAX;
620 } else {
621 if (op == TOK_UDIV || op == TOK_UMOD) {
622 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
623 o(0xf0 + fr);
624 } else {
625 o(0xf799); /* cltd, idiv fr, %eax */
626 o(0xf8 + fr);
628 if (op == '%' || op == TOK_UMOD)
629 r = REG_EDX;
630 else
631 r = REG_EAX;
633 vtop->r = r;
634 break;
635 default:
636 opc = 7;
637 goto gen_op8;
641 /* generate a floating point operation 'v = t1 op t2' instruction. The
642 two operands are guaranted to have the same floating point type */
643 /* XXX: need to use ST1 too */
644 void gen_opf(int op)
646 int a, ft, fc, swapped, r;
648 /* convert constants to memory references */
649 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
650 vswap();
651 gv(RC_FLOAT);
652 vswap();
654 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
655 gv(RC_FLOAT);
657 /* must put at least one value in the floating point register */
658 if ((vtop[-1].r & VT_LVAL) &&
659 (vtop[0].r & VT_LVAL)) {
660 vswap();
661 gv(RC_FLOAT);
662 vswap();
664 swapped = 0;
665 /* swap the stack if needed so that t1 is the register and t2 is
666 the memory reference */
667 if (vtop[-1].r & VT_LVAL) {
668 vswap();
669 swapped = 1;
671 if (op >= TOK_ULT && op <= TOK_GT) {
672 /* load on stack second operand */
673 load(REG_ST0, vtop);
674 save_reg(REG_EAX); /* eax is used by FP comparison code */
675 if (op == TOK_GE || op == TOK_GT)
676 swapped = !swapped;
677 else if (op == TOK_EQ || op == TOK_NE)
678 swapped = 0;
679 if (swapped)
680 o(0xc9d9); /* fxch %st(1) */
681 o(0xe9da); /* fucompp */
682 o(0xe0df); /* fnstsw %ax */
683 if (op == TOK_EQ) {
684 o(0x45e480); /* and $0x45, %ah */
685 o(0x40fC80); /* cmp $0x40, %ah */
686 } else if (op == TOK_NE) {
687 o(0x45e480); /* and $0x45, %ah */
688 o(0x40f480); /* xor $0x40, %ah */
689 op = TOK_NE;
690 } else if (op == TOK_GE || op == TOK_LE) {
691 o(0x05c4f6); /* test $0x05, %ah */
692 op = TOK_EQ;
693 } else {
694 o(0x45c4f6); /* test $0x45, %ah */
695 op = TOK_EQ;
697 vtop--;
698 vtop->r = VT_CMP;
699 vtop->c.i = op;
700 } else {
701 /* no memory reference possible for long double operations */
702 if ((vtop->t & VT_BTYPE) == VT_LDOUBLE) {
703 load(REG_ST0, vtop);
704 swapped = !swapped;
707 switch(op) {
708 default:
709 case '+':
710 a = 0;
711 break;
712 case '-':
713 a = 4;
714 if (swapped)
715 a++;
716 break;
717 case '*':
718 a = 1;
719 break;
720 case '/':
721 a = 6;
722 if (swapped)
723 a++;
724 break;
726 ft = vtop->t;
727 fc = vtop->c.ul;
728 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
729 o(0xde); /* fxxxp %st, %st(1) */
730 o(0xc1 + (a << 3));
731 } else {
732 /* if saved lvalue, then we must reload it */
733 r = vtop->r;
734 if ((r & VT_VALMASK) == VT_LLOCAL) {
735 SValue v1;
736 r = get_reg(RC_INT);
737 v1.t = VT_INT;
738 v1.r = VT_LOCAL | VT_LVAL;
739 v1.c.ul = fc;
740 load(r, &v1);
741 fc = 0;
744 if ((ft & VT_BTYPE) == VT_DOUBLE)
745 o(0xdc);
746 else
747 o(0xd8);
748 gen_modrm(a, r, fc);
750 vtop--;
754 /* FPU control word for rounding to nearest mode */
755 /* XXX: should move that into tcc lib support code ! */
756 static unsigned short __tcc_fpu_control = 0x137f;
757 /* FPU control word for round to zero mode for int convertion */
758 static unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00;
760 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
761 and 'long long' cases. */
762 void gen_cvt_itof(int t)
764 save_reg(REG_ST0);
765 gv(RC_INT);
766 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
767 /* signed long long to float/double/long double (unsigned case
768 is handled generically) */
769 o(0x50 + vtop->r2); /* push r2 */
770 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
771 o(0x242cdf); /* fildll (%esp) */
772 o(0x08c483); /* add $8, %esp */
773 } else if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
774 (VT_INT | VT_UNSIGNED)) {
775 /* unsigned int to float/double/long double */
776 o(0x6a); /* push $0 */
777 g(0x00);
778 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
779 o(0x242cdf); /* fildll (%esp) */
780 o(0x08c483); /* add $8, %esp */
781 } else {
782 /* int to float/double/long double */
783 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
784 o(0x2404db); /* fildl (%esp) */
785 o(0x04c483); /* add $4, %esp */
787 vtop->r = REG_ST0;
790 /* convert fp to int 't' type */
791 /* XXX: handle long long case */
792 void gen_cvt_ftoi(int t)
794 int r, r2, size;
796 gv(RC_FLOAT);
797 if (t != VT_INT)
798 size = 8;
799 else
800 size = 4;
802 oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */
803 oad(0xec81, size); /* sub $xxx, %esp */
804 if (size == 4)
805 o(0x1cdb); /* fistpl */
806 else
807 o(0x3cdf); /* fistpll */
808 o(0x24);
809 oad(0x2dd9, (int)&__tcc_fpu_control); /* ldcw xxx */
810 r = get_reg(RC_INT);
811 o(0x58 + r); /* pop r */
812 if (size == 8) {
813 if (t == VT_LLONG) {
814 vtop->r = r; /* mark reg as used */
815 r2 = get_reg(RC_INT);
816 o(0x58 + r2); /* pop r2 */
817 vtop->r2 = r2;
818 } else {
819 o(0x04c483); /* add $4, %esp */
822 vtop->r = r;
825 /* convert from one floating point type to another */
826 void gen_cvt_ftof(int t)
828 /* all we have to do on i386 is to put the float in a register */
829 gv(RC_FLOAT);
832 /* bound check support functions */
833 #ifdef CONFIG_TCC_BCHECK
835 /* generate a bounded pointer addition */
836 void gen_bounded_ptr_add(void)
838 int addr;
839 /* prepare fast i386 function call (args in eax and edx) */
840 gv2(RC_EAX, RC_EDX);
841 /* save all temporary registers */
842 vtop -= 2;
843 save_regs(0);
844 /* do a fast function call */
845 addr = ind;
846 oad(0xe8, (int)__bound_ptr_add - ind - 5);
847 /* returned pointer is in eax */
848 vtop++;
849 vtop->r = REG_EAX | VT_BOUNDED;
850 vtop->c.ul = addr; /* address of bounding function call point */
853 /* patch pointer addition in vtop so that pointer dereferencing is
854 also tested */
855 void gen_bounded_ptr_deref(void)
857 void *func;
858 int size, align, addr;
860 size = 0;
861 /* XXX: put that code in generic part of tcc */
862 if (!is_float(vtop->t)) {
863 if (vtop->r & VT_LVAL_BYTE)
864 size = 1;
865 else if (vtop->r & VT_LVAL_SHORT)
866 size = 2;
868 if (!size)
869 size = type_size(vtop->t, &align);
870 switch(size) {
871 case 1: func = __bound_ptr_indir1; break;
872 case 2: func = __bound_ptr_indir2; break;
873 case 4: func = __bound_ptr_indir4; break;
874 case 8: func = __bound_ptr_indir8; break;
875 case 12: func = __bound_ptr_indir12; break;
876 case 16: func = __bound_ptr_indir16; break;
877 default:
878 error("unhandled size when derefencing bounded pointer");
879 func = NULL;
880 break;
883 addr = vtop->c.ul;
884 *(int *)(addr + 1) = (int)func - addr - 5;
886 #endif
888 /* end of X86 code generator */
889 /*************************************************************/