comments
[tinycc.git] / i386-gen.c
blobb7db8de5424ef37d829059343904f10ecb22e163
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 } GFuncContext;
76 /******************************************************/
78 static int *func_sub_sp_ptr;
79 static unsigned char *func_bound_ptr;
81 void g(int c)
83 *(char *)ind++ = c;
86 void o(int c)
88 while (c) {
89 g(c);
90 c = c / 256;
94 void gen_le32(int c)
96 g(c);
97 g(c >> 8);
98 g(c >> 16);
99 g(c >> 24);
102 /* patch relocation entry with value 'val' */
103 void greloc_patch1(Reloc *p, int val)
105 switch(p->type) {
106 case RELOC_ADDR32:
107 *(int *)p->addr = val;
108 break;
109 case RELOC_REL32:
110 *(int *)p->addr = val - p->addr - 4;
111 break;
115 /* output a symbol and patch all calls to it */
116 void gsym_addr(t, a)
118 int n;
119 while (t) {
120 n = *(int *)t; /* next value */
121 *(int *)t = a - t - 4;
122 t = n;
126 void gsym(t)
128 gsym_addr(t, ind);
131 /* psym is used to put an instruction with a data field which is a
132 reference to a symbol. It is in fact the same as oad ! */
133 #define psym oad
135 /* instruction + 4 bytes data. Return the address of the data */
136 int oad(int c, int s)
138 o(c);
139 *(int *)ind = s;
140 s = ind;
141 ind = ind + 4;
142 return s;
145 /* output constant with relocation if 'r & VT_FORWARD' is true */
146 void gen_addr32(int r, int c)
148 if (!(r & VT_FORWARD)) {
149 gen_le32(c);
150 } else {
151 greloc((Sym *)c, ind, RELOC_ADDR32);
152 gen_le32(0);
156 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
157 opcode bits */
158 void gen_modrm(int op_reg, int r, int c)
160 op_reg = op_reg << 3;
161 if ((r & VT_VALMASK) == VT_CONST) {
162 /* constant memory reference */
163 o(0x05 | op_reg);
164 gen_addr32(r, c);
165 } else if ((r & VT_VALMASK) == VT_LOCAL) {
166 /* currently, we use only ebp as base */
167 if (c == (char)c) {
168 /* short reference */
169 o(0x45 | op_reg);
170 g(c);
171 } else {
172 oad(0x85 | op_reg, c);
174 } else {
175 g(0x00 | op_reg | (r & VT_VALMASK));
180 /* load 'r' from value 'sv' */
181 void load(int r, SValue *sv)
183 int v, t, ft, fc, fr;
184 SValue v1;
186 fr = sv->r;
187 ft = sv->t;
188 fc = sv->c.ul;
190 v = fr & VT_VALMASK;
191 if (fr & VT_LVAL) {
192 if (v == VT_LLOCAL) {
193 v1.t = VT_INT;
194 v1.r = VT_LOCAL | VT_LVAL;
195 v1.c.ul = fc;
196 load(r, &v1);
197 fr = r;
199 if ((ft & VT_BTYPE) == VT_FLOAT) {
200 o(0xd9); /* flds */
201 r = 0;
202 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
203 o(0xdd); /* fldl */
204 r = 0;
205 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
206 o(0xdb); /* fldt */
207 r = 5;
208 } else if ((ft & VT_TYPE) == VT_BYTE)
209 o(0xbe0f); /* movsbl */
210 else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED))
211 o(0xb60f); /* movzbl */
212 else if ((ft & VT_TYPE) == VT_SHORT)
213 o(0xbf0f); /* movswl */
214 else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED))
215 o(0xb70f); /* movzwl */
216 else
217 o(0x8b); /* movl */
218 gen_modrm(r, fr, fc);
219 } else {
220 if (v == VT_CONST) {
221 o(0xb8 + r); /* mov $xx, r */
222 gen_addr32(fr, fc);
223 } else if (v == VT_LOCAL) {
224 o(0x8d); /* lea xxx(%ebp), r */
225 gen_modrm(r, VT_LOCAL, fc);
226 } else if (v == VT_CMP) {
227 oad(0xb8 + r, 0); /* mov $0, r */
228 o(0x0f); /* setxx %br */
229 o(fc);
230 o(0xc0 + r);
231 } else if (v == VT_JMP || v == VT_JMPI) {
232 t = v & 1;
233 oad(0xb8 + r, t); /* mov $1, r */
234 oad(0xe9, 5); /* jmp after */
235 gsym(fc);
236 oad(0xb8 + r, t ^ 1); /* mov $0, r */
237 } else if (v != r) {
238 o(0x89);
239 o(0xc0 + r + v * 8); /* mov v, r */
244 /* store register 'r' in lvalue 'v' */
245 void store(int r, SValue *v)
247 int fr, bt, ft, fc;
249 ft = v->t;
250 fc = v->c.ul;
251 fr = v->r & VT_VALMASK;
252 bt = ft & VT_BTYPE;
253 /* XXX: incorrect if float reg to reg */
254 if (bt == VT_FLOAT) {
255 o(0xd9); /* fsts */
256 r = 2;
257 } else if (bt == VT_DOUBLE) {
258 o(0xdd); /* fstpl */
259 r = 2;
260 } else if (bt == VT_LDOUBLE) {
261 o(0xc0d9); /* fld %st(0) */
262 o(0xdb); /* fstpt */
263 r = 7;
264 } else {
265 if (bt == VT_SHORT)
266 o(0x66);
267 if (bt == VT_BYTE)
268 o(0x88);
269 else
270 o(0x89);
272 if (fr == VT_CONST ||
273 fr == VT_LOCAL ||
274 (v->r & VT_LVAL)) {
275 gen_modrm(r, v->r, fc);
276 } else if (fr != r) {
277 o(0xc0 + fr + r * 8); /* mov r, fr */
281 /* start function call and return function call context */
282 void gfunc_start(GFuncContext *c)
284 c->args_size = 0;
287 /* push function parameter which is in (vtop->t, vtop->c). Stack entry
288 is then popped. */
289 void gfunc_param(GFuncContext *c)
291 int size, align, r;
293 if ((vtop->t & VT_BTYPE) == VT_STRUCT) {
294 size = type_size(vtop->t, &align);
295 /* align to stack align size */
296 size = (size + 3) & ~3;
297 /* allocate the necessary size on stack */
298 oad(0xec81, size); /* sub $xxx, %esp */
299 /* generate structure store */
300 r = get_reg(RC_INT);
301 o(0x89); /* mov %esp, r */
302 o(0xe0 + r);
303 vset(VT_INT, r, 0);
304 vswap();
305 vstore();
306 c->args_size += size;
307 } else if (is_float(vtop->t)) {
308 gv(RC_FLOAT); /* only one float register */
309 if ((vtop->t & VT_BTYPE) == VT_FLOAT)
310 size = 4;
311 else if ((vtop->t & VT_BTYPE) == VT_DOUBLE)
312 size = 8;
313 else
314 size = 12;
315 oad(0xec81, size); /* sub $xxx, %esp */
316 if (size == 12)
317 o(0x7cdb);
318 else
319 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
320 g(0x24);
321 g(0x00);
322 c->args_size += size;
323 } else {
324 /* simple type (currently always same size) */
325 /* XXX: implicit cast ? */
326 r = gv(RC_INT);
327 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
328 size = 8;
329 o(0x50 + vtop->r2); /* push r */
330 } else {
331 size = 4;
333 o(0x50 + r); /* push r */
334 c->args_size += size;
336 vtop--;
339 /* generate function call with address in (vtop->t, vtop->c) and free function
340 context. Stack entry is popped */
341 void gfunc_call(GFuncContext *c)
343 int r;
344 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
345 /* constant case */
346 /* forward reference */
347 if (vtop->r & VT_FORWARD) {
348 greloc(vtop->c.sym, ind + 1, RELOC_REL32);
349 oad(0xe8, 0);
350 } else {
351 oad(0xe8, vtop->c.ul - ind - 5);
353 } else {
354 /* otherwise, indirect call */
355 r = gv(RC_INT);
356 o(0xff); /* call *r */
357 o(0xd0 + r);
359 if (c->args_size)
360 oad(0xc481, c->args_size); /* add $xxx, %esp */
361 vtop--;
364 /* generate function prolog of type 't' */
365 void gfunc_prolog(int t)
367 int addr, align, size, u;
368 Sym *sym;
370 sym = sym_find((unsigned)t >> VT_STRUCT_SHIFT);
371 addr = 8;
372 /* if the function returns a structure, then add an
373 implicit pointer parameter */
374 func_vt = sym->t;
375 if ((func_vt & VT_BTYPE) == VT_STRUCT) {
376 func_vc = addr;
377 addr += 4;
379 /* define parameters */
380 while ((sym = sym->next) != NULL) {
381 u = sym->t;
382 sym_push(sym->v & ~SYM_FIELD, u,
383 VT_LOCAL | VT_LVAL, addr);
384 size = type_size(u, &align);
385 size = (size + 3) & ~3;
386 #ifdef FUNC_STRUCT_PARAM_AS_PTR
387 /* structs are passed as pointer */
388 if ((u & VT_BTYPE) == VT_STRUCT) {
389 size = 4;
391 #endif
392 addr += size;
394 o(0xe58955); /* push %ebp, mov %esp, %ebp */
395 func_sub_sp_ptr = (int *)oad(0xec81, 0); /* sub $xxx, %esp */
396 /* leave some room for bound checking code */
397 if (do_bounds_check) {
398 oad(0xb8, 0); /* lbound section pointer */
399 oad(0xb8, 0); /* call to function */
400 func_bound_ptr = lbounds_section->data_ptr;
404 /* generate function epilog */
405 void gfunc_epilog(void)
407 if (do_bounds_check && func_bound_ptr != lbounds_section->data_ptr) {
408 int saved_ind;
409 int *bounds_ptr;
410 /* add end of table info */
411 bounds_ptr = (int *)lbounds_section->data_ptr;
412 *bounds_ptr++ = 0;
413 lbounds_section->data_ptr = (unsigned char *)bounds_ptr;
414 /* generate bound local allocation */
415 saved_ind = ind;
416 ind = (int)func_sub_sp_ptr + 4;
417 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
418 oad(0xe8, (int)__bound_local_new - ind - 5);
419 ind = saved_ind;
420 /* generate bound check local freeing */
421 o(0x5250); /* save returned value, if any */
422 oad(0xb8, (int)func_bound_ptr); /* mov %eax, xxx */
423 oad(0xe8, (int)__bound_local_delete - ind - 5);
424 o(0x585a); /* restore returned value, if any */
426 o(0xc3c9); /* leave, ret */
427 /* align local size to word & save local variables */
428 *func_sub_sp_ptr = (-loc + 3) & -4;
431 int gjmp(int t)
433 return psym(0xe9, t);
436 /* generate a test. set 'inv' to invert test. Stack entry is popped */
437 int gtst(int inv, int t)
439 int v, *p;
440 v = vtop->r & VT_VALMASK;
441 if (v == VT_CMP) {
442 /* fast case : can jump directly since flags are set */
443 g(0x0f);
444 t = psym((vtop->c.i - 16) ^ inv, t);
445 } else if (v == VT_JMP || v == VT_JMPI) {
446 /* && or || optimization */
447 if ((v & 1) == inv) {
448 /* insert vtop->c jump list in t */
449 p = &vtop->c.i;
450 while (*p != 0)
451 p = (int *)*p;
452 *p = t;
453 t = vtop->c.i;
454 } else {
455 t = gjmp(t);
456 gsym(vtop->c.i);
458 } else {
459 if (is_float(vtop->t)) {
460 vpushi(0);
461 gen_op(TOK_NE);
463 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
464 /* constant jmp optimization */
465 if ((vtop->c.i != 0) != inv)
466 t = gjmp(t);
467 } else {
468 v = gv(RC_INT);
469 o(0x85);
470 o(0xc0 + v * 9);
471 g(0x0f);
472 t = psym(0x85 ^ inv, t);
475 vtop--;
476 return t;
479 /* generate an integer binary operation */
480 void gen_opi(int op)
482 int r, fr, opc, c;
484 switch(op) {
485 case '+':
486 case TOK_ADDC1: /* add with carry generation */
487 opc = 0;
488 gen_op8:
489 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
490 /* constant case */
491 vswap();
492 r = gv(RC_INT);
493 vswap();
494 c = vtop->c.i;
495 if (c == (char)c) {
496 /* XXX: generate inc and dec for smaller code ? */
497 o(0x83);
498 o(0xc0 | (opc << 3) | r);
499 g(c);
500 } else {
501 o(0x81);
502 oad(0xc0 | (opc << 3) | r, c);
504 } else {
505 gv2(RC_INT, RC_INT);
506 r = vtop[-1].r;
507 fr = vtop[0].r;
508 o((opc << 3) | 0x01);
509 o(0xc0 + r + fr * 8);
511 vtop--;
512 if (op >= TOK_ULT && op <= TOK_GT) {
513 vtop--;
514 vset(VT_INT, VT_CMP, op);
516 break;
517 case '-':
518 case TOK_SUBC1: /* sub with carry generation */
519 opc = 5;
520 goto gen_op8;
521 case TOK_ADDC2: /* add with carry use */
522 opc = 2;
523 goto gen_op8;
524 case TOK_SUBC2: /* sub with carry use */
525 opc = 3;
526 goto gen_op8;
527 case '&':
528 opc = 4;
529 goto gen_op8;
530 case '^':
531 opc = 6;
532 goto gen_op8;
533 case '|':
534 opc = 1;
535 goto gen_op8;
536 case '*':
537 gv2(RC_INT, RC_INT);
538 r = vtop[-1].r;
539 fr = vtop[0].r;
540 vtop--;
541 o(0xaf0f); /* imul fr, r */
542 o(0xc0 + fr + r * 8);
543 break;
544 case TOK_SHL:
545 opc = 4;
546 goto gen_shift;
547 case TOK_SHR:
548 opc = 5;
549 goto gen_shift;
550 case TOK_SAR:
551 opc = 7;
552 gen_shift:
553 opc = 0xc0 | (opc << 3);
554 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) {
555 /* constant case */
556 vswap();
557 r = gv(RC_INT);
558 vswap();
559 c = vtop->c.i & 0x1f;
560 o(0xc1); /* shl/shr/sar $xxx, r */
561 o(opc | r);
562 g(c);
563 } else {
564 /* we generate the shift in ecx */
565 gv2(RC_INT, RC_ECX);
566 r = vtop[-1].r;
567 o(0xd3); /* shl/shr/sar %cl, r */
568 o(opc | r);
570 vtop--;
571 break;
572 case '/':
573 case TOK_UDIV:
574 case TOK_PDIV:
575 case '%':
576 case TOK_UMOD:
577 case TOK_UMULL:
578 /* first operand must be in eax */
579 /* XXX: need better constraint for second operand */
580 gv2(RC_EAX, RC_ECX);
581 r = vtop[-1].r;
582 fr = vtop[0].r;
583 vtop--;
584 save_reg(REG_EDX);
585 if (op == TOK_UMULL) {
586 o(0xf7); /* mul fr */
587 o(0xe0 + fr);
588 vtop->r2 = REG_EDX;
589 r = REG_EAX;
590 } else {
591 if (op == TOK_UDIV || op == TOK_UMOD) {
592 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
593 o(0xf0 + fr);
594 } else {
595 o(0xf799); /* cltd, idiv fr, %eax */
596 o(0xf8 + fr);
598 if (op == '%' || op == TOK_UMOD)
599 r = REG_EDX;
600 else
601 r = REG_EAX;
603 vtop->r = r;
604 break;
605 default:
606 opc = 7;
607 goto gen_op8;
611 /* generate a floating point operation 'v = t1 op t2' instruction. The
612 two operands are guaranted to have the same floating point type */
613 /* XXX: need to use ST1 too */
614 void gen_opf(int op)
616 int a, ft, fc, swapped;
618 /* convert constants to memory references */
619 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
620 vswap();
621 gv(RC_FLOAT);
622 vswap();
624 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
625 gv(RC_FLOAT);
627 /* must put at least one value in the floating point register */
628 if ((vtop[-1].r & VT_LVAL) &&
629 (vtop[0].r & VT_LVAL)) {
630 vswap();
631 gv(RC_FLOAT);
632 vswap();
634 if (op >= TOK_ULT && op <= TOK_GT) {
635 /* load on stack second operand */
636 load(REG_ST0, vtop);
637 if (op == TOK_GE || op == TOK_GT)
638 o(0xc9d9); /* fxch %st(1) */
639 o(0xe9da); /* fucompp */
640 o(0xe0df); /* fnstsw %ax */
641 if (op == TOK_EQ) {
642 o(0x45e480); /* and $0x45, %ah */
643 o(0x40fC80); /* cmp $0x40, %ah */
644 } else if (op == TOK_NE) {
645 o(0x45e480); /* and $0x45, %ah */
646 o(0x40f480); /* xor $0x40, %ah */
647 op = TOK_NE;
648 } else if (op == TOK_GE || op == TOK_LE) {
649 o(0x05c4f6); /* test $0x05, %ah */
650 op = TOK_EQ;
651 } else {
652 o(0x45c4f6); /* test $0x45, %ah */
653 op = TOK_EQ;
655 vtop--;
656 vtop->r = VT_CMP;
657 vtop->c.i = op;
658 } else {
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 /* no memory reference possible for long double operations */
667 if ((vtop->t & VT_BTYPE) == VT_LDOUBLE) {
668 load(REG_ST0, vtop);
669 swapped = !swapped;
672 switch(op) {
673 default:
674 case '+':
675 a = 0;
676 break;
677 case '-':
678 a = 4;
679 if (swapped)
680 a++;
681 break;
682 case '*':
683 a = 1;
684 break;
685 case '/':
686 a = 6;
687 if (swapped)
688 a++;
689 break;
691 ft = vtop->t;
692 fc = vtop->c.ul;
693 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
694 o(0xde); /* fxxxp %st, %st(1) */
695 o(0xc1 + (a << 3));
696 } else {
697 if ((ft & VT_BTYPE) == VT_DOUBLE)
698 o(0xdc);
699 else
700 o(0xd8);
701 gen_modrm(a, vtop->r, fc);
703 vtop--;
707 /* FPU control word for rounding to nearest mode */
708 /* XXX: should move that into tcc lib support code ! */
709 static unsigned short __tcc_fpu_control = 0x137f;
710 /* FPU control word for round to zero mode for int convertion */
711 static unsigned short __tcc_int_fpu_control = 0x137f | 0x0c00;
713 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
714 and 'long long' cases. */
715 void gen_cvt_itof(int t)
717 gv(RC_INT);
718 if ((vtop->t & VT_BTYPE) == VT_LLONG) {
719 /* signed long long to float/double/long double (unsigned case
720 is handled generically) */
721 o(0x50 + vtop->r2); /* push r2 */
722 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
723 o(0x242cdf); /* fildll (%esp) */
724 o(0x08c483); /* add $8, %esp */
725 } else if ((vtop->t & (VT_BTYPE | VT_UNSIGNED)) ==
726 (VT_INT | VT_UNSIGNED)) {
727 /* unsigned int to float/double/long double */
728 o(0x6a); /* push $0 */
729 g(0x00);
730 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
731 o(0x242cdf); /* fildll (%esp) */
732 o(0x08c483); /* add $8, %esp */
733 } else {
734 /* int to float/double/long double */
735 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
736 o(0x2404db); /* fildl (%esp) */
737 o(0x04c483); /* add $4, %esp */
739 vtop->r = REG_ST0;
742 /* convert fp to int 't' type */
743 /* XXX: handle long long case */
744 void gen_cvt_ftoi(int t)
746 int r, r2, size;
748 gv(RC_FLOAT);
749 if (t != VT_INT)
750 size = 8;
751 else
752 size = 4;
754 oad(0x2dd9, (int)&__tcc_int_fpu_control); /* ldcw xxx */
755 oad(0xec81, size); /* sub $xxx, %esp */
756 if (size == 4)
757 o(0x1cdb); /* fistpl */
758 else
759 o(0x3cdf); /* fistpll */
760 o(0x24);
761 oad(0x2dd9, (int)&__tcc_fpu_control); /* ldcw xxx */
762 r = get_reg(RC_INT);
763 o(0x58 + r); /* pop r */
764 if (size == 8) {
765 if (t == VT_LLONG) {
766 vtop->r = r; /* mark reg as used */
767 r2 = get_reg(RC_INT);
768 o(0x58 + r2); /* pop r2 */
769 vtop->r2 = r2;
770 } else {
771 o(0x04c483); /* add $4, %esp */
774 vtop->r = r;
777 /* convert from one floating point type to another */
778 void gen_cvt_ftof(int t)
780 /* all we have to do on i386 is to put the float in a register */
781 gv(RC_FLOAT);
784 /* bound check support functions */
786 /* generate first part of bounded pointer addition */
787 void gen_bounded_ptr_add1(void)
789 /* prepare fast i386 function call (args in eax and edx) */
790 gv2(RC_EAX, RC_EDX);
791 /* save all temporary registers */
792 vtop--;
793 vtop->r = VT_CONST;
794 save_regs();
797 /* if deref is true, then also test dereferencing */
798 void gen_bounded_ptr_add2(int deref)
800 void *func;
801 int size, align;
803 if (deref) {
804 size = type_size(vtop->t, &align);
805 switch(size) {
806 case 1: func = __bound_ptr_indir1; break;
807 case 2: func = __bound_ptr_indir2; break;
808 case 4: func = __bound_ptr_indir4; break;
809 case 8: func = __bound_ptr_indir8; break;
810 case 12: func = __bound_ptr_indir12; break;
811 case 16: func = __bound_ptr_indir16; break;
812 default:
813 error("unhandled size when derefencing bounded pointer");
814 func = NULL;
815 break;
817 } else {
818 func = __bound_ptr_add;
821 /* do a fast function call */
822 oad(0xe8, (int)func - ind - 5);
823 /* return pointer is there */
824 vtop->r = REG_EAX;
827 /* end of X86 code generator */
828 /*************************************************************/