realloc text section in code generation
[tinycc.git] / i386-gen.c
blob85f101bc6425667e837f8d7927628bd171143c53
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 unsigned long func_sub_sp_offset;
83 static unsigned long func_bound_offset;
84 static int func_ret_sub;
86 /* XXX: make it faster ? */
87 void g(int c)
89 int ind1;
90 ind1 = ind + 1;
91 if (ind1 > cur_text_section->data_allocated)
92 section_realloc(cur_text_section, ind1);
93 cur_text_section->data[ind] = c;
94 ind = ind1;
97 void o(int c)
99 while (c) {
100 g(c);
101 c = c / 256;
105 void gen_le32(int c)
107 g(c);
108 g(c >> 8);
109 g(c >> 16);
110 g(c >> 24);
113 /* output a symbol and patch all calls to it */
114 void gsym_addr(int t, int a)
116 int n, *ptr;
117 while (t) {
118 ptr = (int *)(cur_text_section->data + t);
119 n = *ptr; /* next value */
120 *ptr = a - t - 4;
121 t = n;
125 void gsym(int t)
127 gsym_addr(t, ind);
130 /* psym is used to put an instruction with a data field which is a
131 reference to a symbol. It is in fact the same as oad ! */
132 #define psym oad
134 /* instruction + 4 bytes data. Return the address of the data */
135 int oad(int c, int s)
137 int ind1;
139 o(c);
140 ind1 = ind + 4;
141 if (ind1 > cur_text_section->data_allocated)
142 section_realloc(cur_text_section, ind1);
143 *(int *)(cur_text_section->data + ind) = s;
144 s = ind;
145 ind = ind1;
146 return s;
149 /* output constant with relocation if 'r & VT_SYM' is true */
150 void gen_addr32(int r, int c)
152 if (!(r & VT_SYM)) {
153 gen_le32(c);
154 } else {
155 greloc(cur_text_section,
156 (Sym *)c, ind, R_386_32);
157 gen_le32(0);
161 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
162 opcode bits */
163 void gen_modrm(int op_reg, int r, int c)
165 op_reg = op_reg << 3;
166 if ((r & VT_VALMASK) == VT_CONST) {
167 /* constant memory reference */
168 o(0x05 | op_reg);
169 gen_addr32(r, c);
170 } else if ((r & VT_VALMASK) == VT_LOCAL) {
171 /* currently, we use only ebp as base */
172 if (c == (char)c) {
173 /* short reference */
174 o(0x45 | op_reg);
175 g(c);
176 } else {
177 oad(0x85 | op_reg, c);
179 } else {
180 g(0x00 | op_reg | (r & VT_VALMASK));
185 /* load 'r' from value 'sv' */
186 void load(int r, SValue *sv)
188 int v, t, ft, fc, fr;
189 SValue v1;
191 fr = sv->r;
192 ft = sv->t;
193 fc = sv->c.ul;
195 v = fr & VT_VALMASK;
196 if (fr & VT_LVAL) {
197 if (v == VT_LLOCAL) {
198 v1.t = VT_INT;
199 v1.r = VT_LOCAL | VT_LVAL;
200 v1.c.ul = fc;
201 load(r, &v1);
202 fr = r;
204 if ((ft & VT_BTYPE) == VT_FLOAT) {
205 o(0xd9); /* flds */
206 r = 0;
207 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
208 o(0xdd); /* fldl */
209 r = 0;
210 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
211 o(0xdb); /* fldt */
212 r = 5;
213 } else if ((ft & VT_TYPE) == VT_BYTE) {
214 o(0xbe0f); /* movsbl */
215 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
216 o(0xb60f); /* movzbl */
217 } else if ((ft & VT_TYPE) == VT_SHORT) {
218 o(0xbf0f); /* movswl */
219 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
220 o(0xb70f); /* movzwl */
221 } else {
222 o(0x8b); /* movl */
224 gen_modrm(r, fr, fc);
225 } else {
226 if (v == VT_CONST) {
227 o(0xb8 + r); /* mov $xx, r */
228 gen_addr32(fr, fc);
229 } else if (v == VT_LOCAL) {
230 o(0x8d); /* lea xxx(%ebp), r */
231 gen_modrm(r, VT_LOCAL, fc);
232 } else if (v == VT_CMP) {
233 oad(0xb8 + r, 0); /* mov $0, r */
234 o(0x0f); /* setxx %br */
235 o(fc);
236 o(0xc0 + r);
237 } else if (v == VT_JMP || v == VT_JMPI) {
238 t = v & 1;
239 oad(0xb8 + r, t); /* mov $1, r */
240 oad(0xe9, 5); /* jmp after */
241 gsym(fc);
242 oad(0xb8 + r, t ^ 1); /* mov $0, r */
243 } else if (v != r) {
244 o(0x89);
245 o(0xc0 + r + v * 8); /* mov v, r */
250 /* store register 'r' in lvalue 'v' */
251 void store(int r, SValue *v)
253 int fr, bt, ft, fc;
255 ft = v->t;
256 fc = v->c.ul;
257 fr = v->r & VT_VALMASK;
258 bt = ft & VT_BTYPE;
259 /* XXX: incorrect if float reg to reg */
260 if (bt == VT_FLOAT) {
261 o(0xd9); /* fsts */
262 r = 2;
263 } else if (bt == VT_DOUBLE) {
264 o(0xdd); /* fstpl */
265 r = 2;
266 } else if (bt == VT_LDOUBLE) {
267 o(0xc0d9); /* fld %st(0) */
268 o(0xdb); /* fstpt */
269 r = 7;
270 } else {
271 if (bt == VT_SHORT)
272 o(0x66);
273 if (bt == VT_BYTE)
274 o(0x88);
275 else
276 o(0x89);
278 if (fr == VT_CONST ||
279 fr == VT_LOCAL ||
280 (v->r & VT_LVAL)) {
281 gen_modrm(r, v->r, fc);
282 } else if (fr != r) {
283 o(0xc0 + fr + r * 8); /* mov r, fr */
287 /* start function call and return function call context */
288 void gfunc_start(GFuncContext *c, int func_call)
290 c->args_size = 0;
291 c->func_call = func_call;
294 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
295 is then popped. */
296 void gfunc_param(GFuncContext *c)
298 int size, align, r;
300 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
301 size = type_size(vtop->t, &align);
302 /* align to stack align size */
303 size = (size + 3) & ~3;
304 /* allocate the necessary size on stack */
305 oad(0xec81, size); /* sub $xxx, %esp */
306 /* generate structure store */
307 r = get_reg(RC_INT);
308 o(0x89); /* mov %esp, r */
309 o(0xe0 + r);
310 vset(vtop->t, r | VT_LVAL, 0);
311 vswap();
312 vstore();
313 c->args_size += size;
314 } else if (is_float(vtop->t)) {
315 gv(RC_FLOAT); /* only one float register */
316 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
317 size = 4;
318 else if ((vtop->t & VT_BTYPE) == VT_DOUBLE)
319 size = 8;
320 else
321 size = 12;
322 oad(0xec81, size); /* sub $xxx, %esp */
323 if (size == 12)
324 o(0x7cdb);
325 else
326 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
327 g(0x24);
328 g(0x00);
329 c->args_size += size;
330 } else {
331 /* simple type (currently always same size) */
332 /* XXX: implicit cast ? */
333 r = gv(RC_INT);
334 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
335 size = 8;
336 o(0x50 + vtop->r2); /* push r */
337 } else {
338 size = 4;
340 o(0x50 + r); /* push r */
341 c->args_size += size;
343 vtop--;
346 static void gadd_sp(int val)
348 if (val == (char)val) {
349 o(0xc483);
350 g(val);
351 } else {
352 oad(0xc481, val); /* add $xxx, %esp */
356 /* generate function call with address in (vtop->t, vtop->c) and free function
357 context. Stack entry is popped */
358 void gfunc_call(GFuncContext *c)
360 int r;
361 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
362 /* constant case */
363 if (vtop->r & VT_SYM) {
364 /* relocation case */
365 greloc(cur_text_section, vtop->c.sym,
366 ind + 1, R_386_PC32);
367 oad(0xe8, -4);
368 } else {
369 oad(0xe8, vtop->c.ul - ind - 5);
371 } else {
372 /* otherwise, indirect call */
373 r = gv(RC_INT);
374 o(0xff); /* call *r */
375 o(0xd0 + r);
377 if (c->args_size && c->func_call == FUNC_CDECL)
378 gadd_sp(c->args_size);
379 vtop--;
382 /* generate function prolog of type 't' */
383 void gfunc_prolog(int t)
385 int addr, align, size, u, func_call;
386 Sym *sym;
388 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
389 func_call = sym->r;
390 addr = 8;
391 /* if the function returns a structure, then add an
392 implicit pointer parameter */
393 func_vt = sym->t;
394 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
395 func_vc = addr;
396 addr += 4;
398 /* define parameters */
399 while ((sym = sym->next) != NULL) {
400 u = sym->t;
401 sym_push(sym->v & ~SYM_FIELD, u,
402 VT_LOCAL | VT_LVAL, addr);
403 size = type_size(u, &align);
404 size = (size + 3) & ~3;
405 #ifdef FUNC_STRUCT_PARAM_AS_PTR
406 /* structs are passed as pointer */
407 if ((u & VT_BTYPE) == VT_STRUCT) {
408 size = 4;
410 #endif
411 addr += size;
413 func_ret_sub = 0;
414 /* pascal type call ? */
415 if (func_call == FUNC_STDCALL)
416 func_ret_sub = addr - 8;
417 o(0xe58955); /* push %ebp, mov %esp, %ebp */
418 func_sub_sp_offset = oad(0xec81, 0); /* sub $xxx, %esp */
419 /* leave some room for bound checking code */
420 if (do_bounds_check) {
421 oad(0xb8, 0); /* lbound section pointer */
422 oad(0xb8, 0); /* call to function */
423 func_bound_offset = lbounds_section->data_offset;
427 /* generate function epilog */
428 void gfunc_epilog(void)
430 #ifdef CONFIG_TCC_BCHECK
431 if (do_bounds_check && func_bound_offset != lbounds_section->data_offset) {
432 int saved_ind;
433 int *bounds_ptr;
434 Sym *sym, *sym_data;
435 /* add end of table info */
436 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
437 *bounds_ptr = 0;
438 /* generate bound local allocation */
439 saved_ind = ind;
440 ind = func_sub_sp_offset + 4;
441 sym_data = get_sym_ref(char_pointer_type, lbounds_section,
442 func_bound_offset, lbounds_section->data_offset);
443 greloc(cur_text_section, sym_data,
444 ind + 1, R_386_32);
445 oad(0xb8, 0); /* mov %eax, xxx */
446 sym = external_sym(TOK___bound_local_new, func_old_type, 0);
447 greloc(cur_text_section, sym,
448 ind + 1, R_386_PC32);
449 oad(0xe8, -4);
450 ind = saved_ind;
451 /* generate bound check local freeing */
452 o(0x5250); /* save returned value, if any */
453 greloc(cur_text_section, sym_data,
454 ind + 1, R_386_32);
455 oad(0xb8, 0); /* mov %eax, xxx */
456 sym = external_sym(TOK___bound_local_delete, func_old_type, 0);
457 greloc(cur_text_section, sym,
458 ind + 1, R_386_PC32);
459 oad(0xe8, -4);
460 o(0x585a); /* restore returned value, if any */
462 #endif
463 o(0xc9); /* leave */
464 if (func_ret_sub == 0) {
465 o(0xc3); /* ret */
466 } else {
467 o(0xc2); /* ret n */
468 g(func_ret_sub);
469 g(func_ret_sub >> 8);
471 /* align local size to word & save local variables */
472 *(int *)(cur_text_section->data + func_sub_sp_offset) = (-loc + 3) & -4;
475 /* generate a jump to a label */
476 int gjmp(int t)
478 return psym(0xe9, t);
481 /* generate a jump to a fixed address */
482 void gjmp_addr(int a)
484 oad(0xe9, a - ind - 5);
487 /* generate a test. set 'inv' to invert test. Stack entry is popped */
488 int gtst(int inv, int t)
490 int v, *p;
491 v = vtop->r & VT_VALMASK;
492 if (v == VT_CMP) {
493 /* fast case : can jump directly since flags are set */
494 g(0x0f);
495 t = psym((vtop->c.i - 16) ^ inv, t);
496 } else if (v == VT_JMP || v == VT_JMPI) {
497 /* && or || optimization */
498 if ((v & 1) == inv) {
499 /* insert vtop->c jump list in t */
500 p = &vtop->c.i;
501 while (*p != 0)
502 p = (int *)(cur_text_section->data + *p);
503 *p = t;
504 t = vtop->c.i;
505 } else {
506 t = gjmp(t);
507 gsym(vtop->c.i);
509 } else {
510 if (is_float(vtop->t)) {
511 vpushi(0);
512 gen_op(TOK_NE);
514 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
515 /* constant jmp optimization */
516 if ((vtop->c.i != 0) != inv)
517 t = gjmp(t);
518 } else {
519 v = gv(RC_INT);
520 o(0x85);
521 o(0xc0 + v * 9);
522 g(0x0f);
523 t = psym(0x85 ^ inv, t);
526 vtop--;
527 return t;
530 /* generate an integer binary operation */
531 void gen_opi(int op)
533 int r, fr, opc, c;
535 switch(op) {
536 case '+':
537 case TOK_ADDC1: /* add with carry generation */
538 opc = 0;
539 gen_op8:
540 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
541 /* constant case */
542 vswap();
543 r = gv(RC_INT);
544 vswap();
545 c = vtop->c.i;
546 if (c == (char)c) {
547 /* XXX: generate inc and dec for smaller code ? */
548 o(0x83);
549 o(0xc0 | (opc << 3) | r);
550 g(c);
551 } else {
552 o(0x81);
553 oad(0xc0 | (opc << 3) | r, c);
555 } else {
556 gv2(RC_INT, RC_INT);
557 r = vtop[-1].r;
558 fr = vtop[0].r;
559 o((opc << 3) | 0x01);
560 o(0xc0 + r + fr * 8);
562 vtop--;
563 if (op >= TOK_ULT && op <= TOK_GT) {
564 vtop--;
565 vset(VT_INT, VT_CMP, op);
567 break;
568 case '-':
569 case TOK_SUBC1: /* sub with carry generation */
570 opc = 5;
571 goto gen_op8;
572 case TOK_ADDC2: /* add with carry use */
573 opc = 2;
574 goto gen_op8;
575 case TOK_SUBC2: /* sub with carry use */
576 opc = 3;
577 goto gen_op8;
578 case '&':
579 opc = 4;
580 goto gen_op8;
581 case '^':
582 opc = 6;
583 goto gen_op8;
584 case '|':
585 opc = 1;
586 goto gen_op8;
587 case '*':
588 gv2(RC_INT, RC_INT);
589 r = vtop[-1].r;
590 fr = vtop[0].r;
591 vtop--;
592 o(0xaf0f); /* imul fr, r */
593 o(0xc0 + fr + r * 8);
594 break;
595 case TOK_SHL:
596 opc = 4;
597 goto gen_shift;
598 case TOK_SHR:
599 opc = 5;
600 goto gen_shift;
601 case TOK_SAR:
602 opc = 7;
603 gen_shift:
604 opc = 0xc0 | (opc << 3);
605 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
606 /* constant case */
607 vswap();
608 r = gv(RC_INT);
609 vswap();
610 c = vtop->c.i & 0x1f;
611 o(0xc1); /* shl/shr/sar $xxx, r */
612 o(opc | r);
613 g(c);
614 } else {
615 /* we generate the shift in ecx */
616 gv2(RC_INT, RC_ECX);
617 r = vtop[-1].r;
618 o(0xd3); /* shl/shr/sar %cl, r */
619 o(opc | r);
621 vtop--;
622 break;
623 case '/':
624 case TOK_UDIV:
625 case TOK_PDIV:
626 case '%':
627 case TOK_UMOD:
628 case TOK_UMULL:
629 /* first operand must be in eax */
630 /* XXX: need better constraint for second operand */
631 gv2(RC_EAX, RC_ECX);
632 r = vtop[-1].r;
633 fr = vtop[0].r;
634 vtop--;
635 save_reg(REG_EDX);
636 if (op == TOK_UMULL) {
637 o(0xf7); /* mul fr */
638 o(0xe0 + fr);
639 vtop->r2 = REG_EDX;
640 r = REG_EAX;
641 } else {
642 if (op == TOK_UDIV || op == TOK_UMOD) {
643 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
644 o(0xf0 + fr);
645 } else {
646 o(0xf799); /* cltd, idiv fr, %eax */
647 o(0xf8 + fr);
649 if (op == '%' || op == TOK_UMOD)
650 r = REG_EDX;
651 else
652 r = REG_EAX;
654 vtop->r = r;
655 break;
656 default:
657 opc = 7;
658 goto gen_op8;
662 /* generate a floating point operation 'v = t1 op t2' instruction. The
663 two operands are guaranted to have the same floating point type */
664 /* XXX: need to use ST1 too */
665 void gen_opf(int op)
667 int a, ft, fc, swapped, r;
669 /* convert constants to memory references */
670 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
671 vswap();
672 gv(RC_FLOAT);
673 vswap();
675 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
676 gv(RC_FLOAT);
678 /* must put at least one value in the floating point register */
679 if ((vtop[-1].r & VT_LVAL) &&
680 (vtop[0].r & VT_LVAL)) {
681 vswap();
682 gv(RC_FLOAT);
683 vswap();
685 swapped = 0;
686 /* swap the stack if needed so that t1 is the register and t2 is
687 the memory reference */
688 if (vtop[-1].r & VT_LVAL) {
689 vswap();
690 swapped = 1;
692 if (op >= TOK_ULT && op <= TOK_GT) {
693 /* load on stack second operand */
694 load(REG_ST0, vtop);
695 save_reg(REG_EAX); /* eax is used by FP comparison code */
696 if (op == TOK_GE || op == TOK_GT)
697 swapped = !swapped;
698 else if (op == TOK_EQ || op == TOK_NE)
699 swapped = 0;
700 if (swapped)
701 o(0xc9d9); /* fxch %st(1) */
702 o(0xe9da); /* fucompp */
703 o(0xe0df); /* fnstsw %ax */
704 if (op == TOK_EQ) {
705 o(0x45e480); /* and $0x45, %ah */
706 o(0x40fC80); /* cmp $0x40, %ah */
707 } else if (op == TOK_NE) {
708 o(0x45e480); /* and $0x45, %ah */
709 o(0x40f480); /* xor $0x40, %ah */
710 op = TOK_NE;
711 } else if (op == TOK_GE || op == TOK_LE) {
712 o(0x05c4f6); /* test $0x05, %ah */
713 op = TOK_EQ;
714 } else {
715 o(0x45c4f6); /* test $0x45, %ah */
716 op = TOK_EQ;
718 vtop--;
719 vtop->r = VT_CMP;
720 vtop->c.i = op;
721 } else {
722 /* no memory reference possible for long double operations */
723 if ((vtop->t & VT_BTYPE) == VT_LDOUBLE) {
724 load(REG_ST0, vtop);
725 swapped = !swapped;
728 switch(op) {
729 default:
730 case '+':
731 a = 0;
732 break;
733 case '-':
734 a = 4;
735 if (swapped)
736 a++;
737 break;
738 case '*':
739 a = 1;
740 break;
741 case '/':
742 a = 6;
743 if (swapped)
744 a++;
745 break;
747 ft = vtop->t;
748 fc = vtop->c.ul;
749 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
750 o(0xde); /* fxxxp %st, %st(1) */
751 o(0xc1 + (a << 3));
752 } else {
753 /* if saved lvalue, then we must reload it */
754 r = vtop->r;
755 if ((r & VT_VALMASK) == VT_LLOCAL) {
756 SValue v1;
757 r = get_reg(RC_INT);
758 v1.t = VT_INT;
759 v1.r = VT_LOCAL | VT_LVAL;
760 v1.c.ul = fc;
761 load(r, &v1);
762 fc = 0;
765 if ((ft & VT_BTYPE) == VT_DOUBLE)
766 o(0xdc);
767 else
768 o(0xd8);
769 gen_modrm(a, r, fc);
771 vtop--;
775 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
776 and 'long long' cases. */
777 void gen_cvt_itof(int t)
779 save_reg(REG_ST0);
780 gv(RC_INT);
781 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
782 /* signed long long to float/double/long double (unsigned case
783 is handled generically) */
784 o(0x50 + vtop->r2); /* push r2 */
785 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
786 o(0x242cdf); /* fildll (%esp) */
787 o(0x08c483); /* add $8, %esp */
788 } else if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
789 (VT_INT | VT_UNSIGNED)) {
790 /* unsigned int to float/double/long double */
791 o(0x6a); /* push $0 */
792 g(0x00);
793 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
794 o(0x242cdf); /* fildll (%esp) */
795 o(0x08c483); /* add $8, %esp */
796 } else {
797 /* int to float/double/long double */
798 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
799 o(0x2404db); /* fildl (%esp) */
800 o(0x04c483); /* add $4, %esp */
802 vtop->r = REG_ST0;
805 /* convert fp to int 't' type */
806 /* XXX: handle long long case */
807 void gen_cvt_ftoi(int t)
809 int r, r2, size;
810 Sym *sym;
812 gv(RC_FLOAT);
813 if (t != VT_INT)
814 size = 8;
815 else
816 size = 4;
818 o(0x2dd9); /* ldcw xxx */
819 sym = external_sym(TOK___tcc_int_fpu_control,
820 VT_SHORT | VT_UNSIGNED, VT_LVAL);
821 greloc(cur_text_section, sym,
822 ind, R_386_32);
823 gen_le32(0);
825 oad(0xec81, size); /* sub $xxx, %esp */
826 if (size == 4)
827 o(0x1cdb); /* fistpl */
828 else
829 o(0x3cdf); /* fistpll */
830 o(0x24);
831 o(0x2dd9); /* ldcw xxx */
832 sym = external_sym(TOK___tcc_fpu_control,
833 VT_SHORT | VT_UNSIGNED, VT_LVAL);
834 greloc(cur_text_section, sym,
835 ind, R_386_32);
836 gen_le32(0);
838 r = get_reg(RC_INT);
839 o(0x58 + r); /* pop r */
840 if (size == 8) {
841 if (t == VT_LLONG) {
842 vtop->r = r; /* mark reg as used */
843 r2 = get_reg(RC_INT);
844 o(0x58 + r2); /* pop r2 */
845 vtop->r2 = r2;
846 } else {
847 o(0x04c483); /* add $4, %esp */
850 vtop->r = r;
853 /* convert from one floating point type to another */
854 void gen_cvt_ftof(int t)
856 /* all we have to do on i386 is to put the float in a register */
857 gv(RC_FLOAT);
860 /* bound check support functions */
861 #ifdef CONFIG_TCC_BCHECK
863 /* generate a bounded pointer addition */
864 void gen_bounded_ptr_add(void)
866 Sym *sym;
868 /* prepare fast i386 function call (args in eax and edx) */
869 gv2(RC_EAX, RC_EDX);
870 /* save all temporary registers */
871 vtop -= 2;
872 save_regs(0);
873 /* do a fast function call */
874 sym = external_sym(TOK___bound_ptr_add, func_old_type, 0);
875 greloc(cur_text_section, sym,
876 ind + 1, R_386_PC32);
877 oad(0xe8, -4);
878 /* returned pointer is in eax */
879 vtop++;
880 vtop->r = REG_EAX | VT_BOUNDED;
881 /* address of bounding function call point */
882 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
885 /* patch pointer addition in vtop so that pointer dereferencing is
886 also tested */
887 void gen_bounded_ptr_deref(void)
889 int func;
890 int size, align;
891 Elf32_Rel *rel;
892 Sym *sym;
894 size = 0;
895 /* XXX: put that code in generic part of tcc */
896 if (!is_float(vtop->t)) {
897 if (vtop->r & VT_LVAL_BYTE)
898 size = 1;
899 else if (vtop->r & VT_LVAL_SHORT)
900 size = 2;
902 if (!size)
903 size = type_size(vtop->t, &align);
904 switch(size) {
905 case 1: func = TOK___bound_ptr_indir1; break;
906 case 2: func = TOK___bound_ptr_indir2; break;
907 case 4: func = TOK___bound_ptr_indir4; break;
908 case 8: func = TOK___bound_ptr_indir8; break;
909 case 12: func = TOK___bound_ptr_indir12; break;
910 case 16: func = TOK___bound_ptr_indir16; break;
911 default:
912 error("unhandled size when derefencing bounded pointer");
913 func = 0;
914 break;
917 /* patch relocation */
918 /* XXX: find a better solution ? */
919 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
920 sym = external_sym(func, func_old_type, 0);
921 if (!sym->c)
922 put_extern_sym(sym, NULL, 0, 0);
923 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
925 #endif
927 /* end of X86 code generator */
928 /*************************************************************/