new_region() fix - added bounded strlen and strcpy
[tinycc/miki.git] / i386-gen.c
blobc845a25c31562260d0be9f4f86be466039fe5a26
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 */
220 gen_modrm(r, fr, fc);
221 } else {
222 if (v == VT_CONST) {
223 o(0xb8 + r); /* mov $xx, r */
224 gen_addr32(fr, fc);
225 } else if (v == VT_LOCAL) {
226 o(0x8d); /* lea xxx(%ebp), r */
227 gen_modrm(r, VT_LOCAL, fc);
228 } else if (v == VT_CMP) {
229 oad(0xb8 + r, 0); /* mov $0, r */
230 o(0x0f); /* setxx %br */
231 o(fc);
232 o(0xc0 + r);
233 } else if (v == VT_JMP || v == VT_JMPI) {
234 t = v & 1;
235 oad(0xb8 + r, t); /* mov $1, r */
236 oad(0xe9, 5); /* jmp after */
237 gsym(fc);
238 oad(0xb8 + r, t ^ 1); /* mov $0, r */
239 } else if (v != r) {
240 o(0x89);
241 o(0xc0 + r + v * 8); /* mov v, r */
246 /* store register 'r' in lvalue 'v' */
247 void store(int r, SValue *v)
249 int fr, bt, ft, fc;
251 ft = v->t;
252 fc = v->c.ul;
253 fr = v->r & VT_VALMASK;
254 bt = ft & VT_BTYPE;
255 /* XXX: incorrect if float reg to reg */
256 if (bt == VT_FLOAT) {
257 o(0xd9); /* fsts */
258 r = 2;
259 } else if (bt == VT_DOUBLE) {
260 o(0xdd); /* fstpl */
261 r = 2;
262 } else if (bt == VT_LDOUBLE) {
263 o(0xc0d9); /* fld %st(0) */
264 o(0xdb); /* fstpt */
265 r = 7;
266 } else {
267 if (bt == VT_SHORT)
268 o(0x66);
269 if (bt == VT_BYTE)
270 o(0x88);
271 else
272 o(0x89);
274 if (fr == VT_CONST ||
275 fr == VT_LOCAL ||
276 (v->r & VT_LVAL)) {
277 gen_modrm(r, v->r, fc);
278 } else if (fr != r) {
279 o(0xc0 + fr + r * 8); /* mov r, fr */
283 /* start function call and return function call context */
284 void gfunc_start(GFuncContext *c, int func_call)
286 c->args_size = 0;
287 c->func_call = func_call;
290 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
291 is then popped. */
292 void gfunc_param(GFuncContext *c)
294 int size, align, r;
296 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
297 size = type_size(vtop->t, &align);
298 /* align to stack align size */
299 size = (size + 3) & ~3;
300 /* allocate the necessary size on stack */
301 oad(0xec81, size); /* sub $xxx, %esp */
302 /* generate structure store */
303 r = get_reg(RC_INT);
304 o(0x89); /* mov %esp, r */
305 o(0xe0 + r);
306 vset(VT_INT, r, 0);
307 vswap();
308 vstore();
309 c->args_size += size;
310 } else if (is_float(vtop->t)) {
311 gv(RC_FLOAT); /* only one float register */
312 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
313 size = 4;
314 else if ((vtop->t & VT_BTYPE) == VT_DOUBLE)
315 size = 8;
316 else
317 size = 12;
318 oad(0xec81, size); /* sub $xxx, %esp */
319 if (size == 12)
320 o(0x7cdb);
321 else
322 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
323 g(0x24);
324 g(0x00);
325 c->args_size += size;
326 } else {
327 /* simple type (currently always same size) */
328 /* XXX: implicit cast ? */
329 r = gv(RC_INT);
330 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
331 size = 8;
332 o(0x50 + vtop->r2); /* push r */
333 } else {
334 size = 4;
336 o(0x50 + r); /* push r */
337 c->args_size += size;
339 vtop--;
342 /* generate function call with address in (vtop->t, vtop->c) and free function
343 context. Stack entry is popped */
344 void gfunc_call(GFuncContext *c)
346 int r;
347 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
348 /* constant case */
349 /* forward reference */
350 if (vtop->r & VT_FORWARD) {
351 greloc(vtop->c.sym, ind + 1, RELOC_REL32);
352 oad(0xe8, 0);
353 } else {
354 oad(0xe8, vtop->c.ul - ind - 5);
356 } else {
357 /* otherwise, indirect call */
358 r = gv(RC_INT);
359 o(0xff); /* call *r */
360 o(0xd0 + r);
362 if (c->args_size && c->func_call == FUNC_CDECL)
363 oad(0xc481, c->args_size); /* add $xxx, %esp */
364 vtop--;
367 /* generate function prolog of type 't' */
368 void gfunc_prolog(int t)
370 int addr, align, size, u, func_call;
371 Sym *sym;
373 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
374 func_call = sym->r;
375 addr = 8;
376 /* if the function returns a structure, then add an
377 implicit pointer parameter */
378 func_vt = sym->t;
379 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
380 func_vc = addr;
381 addr += 4;
383 /* define parameters */
384 while ((sym = sym->next) != NULL) {
385 u = sym->t;
386 sym_push(sym->v & ~SYM_FIELD, u,
387 VT_LOCAL | VT_LVAL, addr);
388 size = type_size(u, &align);
389 size = (size + 3) & ~3;
390 #ifdef FUNC_STRUCT_PARAM_AS_PTR
391 /* structs are passed as pointer */
392 if ((u & VT_BTYPE) == VT_STRUCT) {
393 size = 4;
395 #endif
396 addr += size;
398 func_ret_sub = 0;
399 /* pascal type call ? */
400 if (func_call == FUNC_STDCALL)
401 func_ret_sub = addr - 8;
402 o(0xe58955); /* push %ebp, mov %esp, %ebp */
403 func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
404 /* leave some room for bound checking code */
405 if (do_bounds_check) {
406 oad(0xb8, 0); /* lbound section pointer */
407 oad(0xb8, 0); /* call to function */
408 func_bound_ptr = lbounds_section->data_ptr;
412 /* generate function epilog */
413 void gfunc_epilog(void)
415 if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {
416 int saved_ind;
417 int *bounds_ptr;
418 /* add end of table info */
419 bounds_ptr = (int *)lbounds_section->data_ptr;
420 *bounds_ptr++ = 0;
421 lbounds_section->data_ptr = (unsigned char *)bounds_ptr;
422 /* generate bound local allocation */
423 saved_ind = ind;
424 ind = (int)func_sub_sp_ptr + 4;
425 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
426 oad(0xe8, (int)__bound_local_new - ind - 5);
427 ind = saved_ind;
428 /* generate bound check local freeing */
429 o(0x5250); /* save returned value, if any */
430 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
431 oad(0xe8, (int)__bound_local_delete - ind - 5);
432 o(0x585a); /* restore returned value, if any */
434 o(0xc9); /* leave */
435 if (func_ret_sub == 0) {
436 o(0xc3); /* ret */
437 } else {
438 o(0xc2); /* ret n */
439 g(func_ret_sub);
440 g(func_ret_sub >> 8);
442 /* align local size to word & save local variables */
443 *func_sub_sp_ptr = (-loc + 3) & -4;
446 int gjmp(int t)
448 return psym(0xe9, t);
451 /* generate a test. set 'inv' to invert test. Stack entry is popped */
452 int gtst(int inv, int t)
454 int v, *p;
455 v = vtop->r & VT_VALMASK;
456 if (v == VT_CMP) {
457 /* fast case : can jump directly since flags are set */
458 g(0x0f);
459 t = psym((vtop->c.i - 16) ^ inv, t);
460 } else if (v == VT_JMP || v == VT_JMPI) {
461 /* && or || optimization */
462 if ((v & 1) == inv) {
463 /* insert vtop->c jump list in t */
464 p = &vtop->c.i;
465 while (*p != 0)
466 p = (int *)*p;
467 *p = t;
468 t = vtop->c.i;
469 } else {
470 t = gjmp(t);
471 gsym(vtop->c.i);
473 } else {
474 if (is_float(vtop->t)) {
475 vpushi(0);
476 gen_op(TOK_NE);
478 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
479 /* constant jmp optimization */
480 if ((vtop->c.i != 0) != inv)
481 t = gjmp(t);
482 } else {
483 v = gv(RC_INT);
484 o(0x85);
485 o(0xc0 + v * 9);
486 g(0x0f);
487 t = psym(0x85 ^ inv, t);
490 vtop--;
491 return t;
494 /* generate an integer binary operation */
495 void gen_opi(int op)
497 int r, fr, opc, c;
499 switch(op) {
500 case '+':
501 case TOK_ADDC1: /* add with carry generation */
502 opc = 0;
503 gen_op8:
504 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
505 /* constant case */
506 vswap();
507 r = gv(RC_INT);
508 vswap();
509 c = vtop->c.i;
510 if (c == (char)c) {
511 /* XXX: generate inc and dec for smaller code ? */
512 o(0x83);
513 o(0xc0 | (opc << 3) | r);
514 g(c);
515 } else {
516 o(0x81);
517 oad(0xc0 | (opc << 3) | r, c);
519 } else {
520 gv2(RC_INT, RC_INT);
521 r = vtop[-1].r;
522 fr = vtop[0].r;
523 o((opc << 3) | 0x01);
524 o(0xc0 + r + fr * 8);
526 vtop--;
527 if (op >= TOK_ULT && op <= TOK_GT) {
528 vtop--;
529 vset(VT_INT, VT_CMP, op);
531 break;
532 case '-':
533 case TOK_SUBC1: /* sub with carry generation */
534 opc = 5;
535 goto gen_op8;
536 case TOK_ADDC2: /* add with carry use */
537 opc = 2;
538 goto gen_op8;
539 case TOK_SUBC2: /* sub with carry use */
540 opc = 3;
541 goto gen_op8;
542 case '&':
543 opc = 4;
544 goto gen_op8;
545 case '^':
546 opc = 6;
547 goto gen_op8;
548 case '|':
549 opc = 1;
550 goto gen_op8;
551 case '*':
552 gv2(RC_INT, RC_INT);
553 r = vtop[-1].r;
554 fr = vtop[0].r;
555 vtop--;
556 o(0xaf0f); /* imul fr, r */
557 o(0xc0 + fr + r * 8);
558 break;
559 case TOK_SHL:
560 opc = 4;
561 goto gen_shift;
562 case TOK_SHR:
563 opc = 5;
564 goto gen_shift;
565 case TOK_SAR:
566 opc = 7;
567 gen_shift:
568 opc = 0xc0 | (opc << 3);
569 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
570 /* constant case */
571 vswap();
572 r = gv(RC_INT);
573 vswap();
574 c = vtop->c.i & 0x1f;
575 o(0xc1); /* shl/shr/sar $xxx, r */
576 o(opc | r);
577 g(c);
578 } else {
579 /* we generate the shift in ecx */
580 gv2(RC_INT, RC_ECX);
581 r = vtop[-1].r;
582 o(0xd3); /* shl/shr/sar %cl, r */
583 o(opc | r);
585 vtop--;
586 break;
587 case '/':
588 case TOK_UDIV:
589 case TOK_PDIV:
590 case '%':
591 case TOK_UMOD:
592 case TOK_UMULL:
593 /* first operand must be in eax */
594 /* XXX: need better constraint for second operand */
595 gv2(RC_EAX, RC_ECX);
596 r = vtop[-1].r;
597 fr = vtop[0].r;
598 vtop--;
599 save_reg(REG_EDX);
600 if (op == TOK_UMULL) {
601 o(0xf7); /* mul fr */
602 o(0xe0 + fr);
603 vtop->r2 = REG_EDX;
604 r = REG_EAX;
605 } else {
606 if (op == TOK_UDIV || op == TOK_UMOD) {
607 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
608 o(0xf0 + fr);
609 } else {
610 o(0xf799); /* cltd, idiv fr, %eax */
611 o(0xf8 + fr);
613 if (op == '%' || op == TOK_UMOD)
614 r = REG_EDX;
615 else
616 r = REG_EAX;
618 vtop->r = r;
619 break;
620 default:
621 opc = 7;
622 goto gen_op8;
626 /* generate a floating point operation 'v = t1 op t2' instruction. The
627 two operands are guaranted to have the same floating point type */
628 /* XXX: need to use ST1 too */
629 void gen_opf(int op)
631 int a, ft, fc, swapped, r;
633 /* convert constants to memory references */
634 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
635 vswap();
636 gv(RC_FLOAT);
637 vswap();
639 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
640 gv(RC_FLOAT);
642 /* must put at least one value in the floating point register */
643 if ((vtop[-1].r & VT_LVAL) &&
644 (vtop[0].r & VT_LVAL)) {
645 vswap();
646 gv(RC_FLOAT);
647 vswap();
649 if (op >= TOK_ULT && op <= TOK_GT) {
650 /* load on stack second operand */
651 load(REG_ST0, vtop);
652 save_reg(REG_EAX); /* eax is used by FP comparison code */
653 if (op == TOK_GE || op == TOK_GT)
654 o(0xc9d9); /* fxch %st(1) */
655 o(0xe9da); /* fucompp */
656 o(0xe0df); /* fnstsw %ax */
657 if (op == TOK_EQ) {
658 o(0x45e480); /* and $0x45, %ah */
659 o(0x40fC80); /* cmp $0x40, %ah */
660 } else if (op == TOK_NE) {
661 o(0x45e480); /* and $0x45, %ah */
662 o(0x40f480); /* xor $0x40, %ah */
663 op = TOK_NE;
664 } else if (op == TOK_GE || op == TOK_LE) {
665 o(0x05c4f6); /* test $0x05, %ah */
666 op = TOK_EQ;
667 } else {
668 o(0x45c4f6); /* test $0x45, %ah */
669 op = TOK_EQ;
671 vtop--;
672 vtop->r = VT_CMP;
673 vtop->c.i = op;
674 } else {
675 swapped = 0;
676 /* swap the stack if needed so that t1 is the register and t2 is
677 the memory reference */
678 if (vtop[-1].r & VT_LVAL) {
679 vswap();
680 swapped = 1;
682 /* no memory reference possible for long double operations */
683 if ((vtop->t & VT_BTYPE) == VT_LDOUBLE) {
684 load(REG_ST0, vtop);
685 swapped = !swapped;
688 switch(op) {
689 default:
690 case '+':
691 a = 0;
692 break;
693 case '-':
694 a = 4;
695 if (swapped)
696 a++;
697 break;
698 case '*':
699 a = 1;
700 break;
701 case '/':
702 a = 6;
703 if (swapped)
704 a++;
705 break;
707 ft = vtop->t;
708 fc = vtop->c.ul;
709 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
710 o(0xde); /* fxxxp %st, %st(1) */
711 o(0xc1 + (a << 3));
712 } else {
713 /* if saved lvalue, then we must reload it */
714 r = vtop->r;
715 if ((r & VT_VALMASK) == VT_LLOCAL) {
716 SValue v1;
717 r = get_reg(RC_INT);
718 v1.t = VT_INT;
719 v1.r = VT_LOCAL | VT_LVAL;
720 v1.c.ul = fc;
721 load(r, &v1);
722 fc = 0;
725 if ((ft & VT_BTYPE) == VT_DOUBLE)
726 o(0xdc);
727 else
728 o(0xd8);
729 gen_modrm(a, r, fc);
731 vtop--;
735 /* FPU control word for rounding to nearest mode */
736 /* XXX: should move that into tcc lib support code ! */
737 static unsigned short __tcc_fpu_control = 0x137f;
738 /* FPU control word for round to zero mode for int convertion */
739 static unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00;
741 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
742 and 'long long' cases. */
743 void gen_cvt_itof(int t)
745 save_reg(REG_ST0);
746 gv(RC_INT);
747 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
748 /* signed long long to float/double/long double (unsigned case
749 is handled generically) */
750 o(0x50 + vtop->r2); /* push r2 */
751 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
752 o(0x242cdf); /* fildll (%esp) */
753 o(0x08c483); /* add $8, %esp */
754 } else if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
755 (VT_INT | VT_UNSIGNED)) {
756 /* unsigned int to float/double/long double */
757 o(0x6a); /* push $0 */
758 g(0x00);
759 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
760 o(0x242cdf); /* fildll (%esp) */
761 o(0x08c483); /* add $8, %esp */
762 } else {
763 /* int to float/double/long double */
764 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
765 o(0x2404db); /* fildl (%esp) */
766 o(0x04c483); /* add $4, %esp */
768 vtop->r = REG_ST0;
771 /* convert fp to int 't' type */
772 /* XXX: handle long long case */
773 void gen_cvt_ftoi(int t)
775 int r, r2, size;
777 gv(RC_FLOAT);
778 if (t != VT_INT)
779 size = 8;
780 else
781 size = 4;
783 oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */
784 oad(0xec81, size); /* sub $xxx, %esp */
785 if (size == 4)
786 o(0x1cdb); /* fistpl */
787 else
788 o(0x3cdf); /* fistpll */
789 o(0x24);
790 oad(0x2dd9, (int)&__tcc_fpu_control); /* ldcw xxx */
791 r = get_reg(RC_INT);
792 o(0x58 + r); /* pop r */
793 if (size == 8) {
794 if (t == VT_LLONG) {
795 vtop->r = r; /* mark reg as used */
796 r2 = get_reg(RC_INT);
797 o(0x58 + r2); /* pop r2 */
798 vtop->r2 = r2;
799 } else {
800 o(0x04c483); /* add $4, %esp */
803 vtop->r = r;
806 /* convert from one floating point type to another */
807 void gen_cvt_ftof(int t)
809 /* all we have to do on i386 is to put the float in a register */
810 gv(RC_FLOAT);
813 /* bound check support functions */
815 /* generate first part of bounded pointer addition */
816 void gen_bounded_ptr_add1(void)
818 /* prepare fast i386 function call (args in eax and edx) */
819 gv2(RC_EAX, RC_EDX);
820 /* save all temporary registers */
821 vtop--;
822 vtop->r = VT_CONST;
823 save_regs();
826 /* if deref is true, then also test dereferencing */
827 void gen_bounded_ptr_add2(int deref)
829 void *func;
830 int size, align;
832 if (deref) {
833 size = type_size(vtop->t, &align);
834 switch(size) {
835 case 1: func = __bound_ptr_indir1; break;
836 case 2: func = __bound_ptr_indir2; break;
837 case 4: func = __bound_ptr_indir4; break;
838 case 8: func = __bound_ptr_indir8; break;
839 case 12: func = __bound_ptr_indir12; break;
840 case 16: func = __bound_ptr_indir16; break;
841 default:
842 error("unhandled size when derefencing bounded pointer");
843 func = NULL;
844 break;
846 } else {
847 func = __bound_ptr_add;
850 /* do a fast function call */
851 oad(0xe8, (int)func - ind - 5);
852 /* return pointer is there */
853 vtop->r = REG_EAX;
856 /* end of X86 code generator */
857 /*************************************************************/