Added missing file.
[tinycc/k1w1.git] / i386-gen.c
bloba47c3fb722012e38bb0e482911f9d2053d743019
1 /*
2 * X86 code generator for TCC
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "tcc.h"
23 const int reg_classes[NB_REGS] = {
24 /* eax */ RC_INT | RC_EAX,
25 /* ecx */ RC_INT | RC_ECX,
26 /* edx */ RC_INT | RC_EDX,
27 /* st0 */ RC_FLOAT | RC_ST0,
31 /******************************************************/
33 static unsigned long func_sub_sp_offset;
34 static unsigned long func_bound_offset;
35 static int func_ret_sub;
37 /* XXX: make it faster ? */
38 void g(int c)
40 int ind1;
41 ind1 = ind + 1;
42 if (ind1 > cur_text_section->data_allocated)
43 section_realloc(cur_text_section, ind1);
44 cur_text_section->data[ind] = c;
45 ind = ind1;
48 void o(unsigned int c)
50 while (c) {
51 g(c);
52 c = c >> 8;
56 void gen_le32(int c)
58 g(c);
59 g(c >> 8);
60 g(c >> 16);
61 g(c >> 24);
64 /* output a symbol and patch all calls to it */
65 void gsym_addr(int t, int a)
67 int n, *ptr;
68 while (t) {
69 ptr = (int *)(cur_text_section->data + t);
70 n = *ptr; /* next value */
71 *ptr = a - t - 4;
72 t = n;
76 void gsym(int t)
78 gsym_addr(t, ind);
81 /* psym is used to put an instruction with a data field which is a
82 reference to a symbol. It is in fact the same as oad ! */
83 #define psym oad
85 /* instruction + 4 bytes data. Return the address of the data */
86 int oad(int c, int s)
88 int ind1;
90 o(c);
91 ind1 = ind + 4;
92 if (ind1 > cur_text_section->data_allocated)
93 section_realloc(cur_text_section, ind1);
94 *(int *)(cur_text_section->data + ind) = s;
95 s = ind;
96 ind = ind1;
97 return s;
100 /* output constant with relocation if 'r & VT_SYM' is true */
101 static void gen_addr32(int r, Sym *sym, int c)
103 if (r & VT_SYM)
104 greloc(cur_text_section, sym, ind, R_386_32);
105 gen_le32(c);
108 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
109 opcode bits */
110 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
112 op_reg = op_reg << 3;
113 if ((r & VT_VALMASK) == VT_CONST) {
114 /* constant memory reference */
115 o(0x05 | op_reg);
116 gen_addr32(r, sym, c);
117 } else if ((r & VT_VALMASK) == VT_LOCAL) {
118 /* currently, we use only ebp as base */
119 if (c == (char)c) {
120 /* short reference */
121 o(0x45 | op_reg);
122 g(c);
123 } else {
124 oad(0x85 | op_reg, c);
126 } else {
127 g(0x00 | op_reg | (r & VT_VALMASK));
131 #ifdef TCC_TARGET_PE
132 static void indir(void);
134 int handle_dllimport(int r, SValue *sv, void (*fn)(int r, SValue *sv))
136 if ((sv->r & (VT_VALMASK|VT_SYM|VT_CONST)) != (VT_SYM|VT_CONST))
137 return 0;
138 if (0 == (sv->sym->type.t & VT_IMPORT))
139 return 0;
141 printf("import %d %04x %s\n", r, ind, get_tok_str(sv->sym->v, NULL));
143 sv->sym->type.t &= ~VT_IMPORT;
144 ++vtop;
146 *vtop = *sv;
147 mk_pointer(&vtop->type);
148 indir();
149 fn(r, vtop);
151 --vtop;
152 sv->sym->type.t |= VT_IMPORT;
153 return 1;
155 #endif
157 /* load 'r' from value 'sv' */
158 void load(int r, SValue *sv)
160 int v, t, ft, fc, fr;
161 SValue v1;
163 #ifdef TCC_TARGET_PE
164 if (handle_dllimport(r, sv, load))
165 return;
166 #endif
167 fr = sv->r;
168 ft = sv->type.t;
169 fc = sv->c.ul;
171 v = fr & VT_VALMASK;
172 if (fr & VT_LVAL) {
173 if (v == VT_LLOCAL) {
174 v1.type.t = VT_INT;
175 v1.r = VT_LOCAL | VT_LVAL;
176 v1.c.ul = fc;
177 load(r, &v1);
178 fr = r;
180 if ((ft & VT_BTYPE) == VT_FLOAT) {
181 o(0xd9); /* flds */
182 r = 0;
183 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
184 o(0xdd); /* fldl */
185 r = 0;
186 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
187 o(0xdb); /* fldt */
188 r = 5;
189 } else if ((ft & VT_TYPE) == VT_BYTE) {
190 o(0xbe0f); /* movsbl */
191 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
192 o(0xb60f); /* movzbl */
193 } else if ((ft & VT_TYPE) == VT_SHORT) {
194 o(0xbf0f); /* movswl */
195 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
196 o(0xb70f); /* movzwl */
197 } else {
198 o(0x8b); /* movl */
200 gen_modrm(r, fr, sv->sym, fc);
201 } else {
202 if (v == VT_CONST) {
203 o(0xb8 + r); /* mov $xx, r */
204 gen_addr32(fr, sv->sym, fc);
205 } else if (v == VT_LOCAL) {
206 o(0x8d); /* lea xxx(%ebp), r */
207 gen_modrm(r, VT_LOCAL, sv->sym, fc);
208 } else if (v == VT_CMP) {
209 oad(0xb8 + r, 0); /* mov $0, r */
210 o(0x0f); /* setxx %br */
211 o(fc);
212 o(0xc0 + r);
213 } else if (v == VT_JMP || v == VT_JMPI) {
214 t = v & 1;
215 oad(0xb8 + r, t); /* mov $1, r */
216 o(0x05eb); /* jmp after */
217 gsym(fc);
218 oad(0xb8 + r, t ^ 1); /* mov $0, r */
219 } else if (v != r) {
220 o(0x89);
221 o(0xc0 + r + v * 8); /* mov v, r */
226 /* store register 'r' in lvalue 'v' */
227 void store(int r, SValue *v)
229 int fr, bt, ft, fc;
231 #ifdef TCC_TARGET_PE
232 if (handle_dllimport(r, v, store))
233 return;
234 #endif
235 ft = v->type.t;
236 fc = v->c.ul;
237 fr = v->r & VT_VALMASK;
238 bt = ft & VT_BTYPE;
239 /* XXX: incorrect if float reg to reg */
240 if (bt == VT_FLOAT) {
241 o(0xd9); /* fsts */
242 r = 2;
243 } else if (bt == VT_DOUBLE) {
244 o(0xdd); /* fstpl */
245 r = 2;
246 } else if (bt == VT_LDOUBLE) {
247 o(0xc0d9); /* fld %st(0) */
248 o(0xdb); /* fstpt */
249 r = 7;
250 } else {
251 if (bt == VT_SHORT)
252 o(0x66);
253 if (bt == VT_BYTE || bt == VT_BOOL)
254 o(0x88);
255 else
256 o(0x89);
258 if (fr == VT_CONST ||
259 fr == VT_LOCAL ||
260 (v->r & VT_LVAL)) {
261 gen_modrm(r, v->r, v->sym, fc);
262 } else if (fr != r) {
263 o(0xc0 + fr + r * 8); /* mov r, fr */
267 static void gadd_sp(int val)
269 if (val == (char)val) {
270 o(0xc483);
271 g(val);
272 } else {
273 oad(0xc481, val); /* add $xxx, %esp */
277 /* 'is_jmp' is '1' if it is a jump */
278 static void gcall_or_jmp(int is_jmp)
280 int r;
281 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
282 /* constant case */
283 if (vtop->r & VT_SYM) {
284 /* relocation case */
285 greloc(cur_text_section, vtop->sym,
286 ind + 1, R_386_PC32);
287 } else {
288 /* put an empty PC32 relocation */
289 put_elf_reloc(symtab_section, cur_text_section,
290 ind + 1, R_386_PC32, 0);
292 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
293 } else {
294 /* otherwise, indirect call */
295 r = gv(RC_INT);
296 o(0xff); /* call/jmp *r */
297 o(0xd0 + r + (is_jmp << 4));
301 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
302 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
304 /* Generate function call. The function address is pushed first, then
305 all the parameters in call order. This functions pops all the
306 parameters and the function address. */
307 void gfunc_call(int nb_args)
309 int size, align, r, args_size, i, func_call;
310 Sym *func_sym;
312 args_size = 0;
313 for(i = 0;i < nb_args; i++) {
314 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
315 size = type_size(&vtop->type, &align);
316 /* align to stack align size */
317 size = (size + 3) & ~3;
318 /* allocate the necessary size on stack */
319 oad(0xec81, size); /* sub $xxx, %esp */
320 /* generate structure store */
321 r = get_reg(RC_INT);
322 o(0x89); /* mov %esp, r */
323 o(0xe0 + r);
324 vset(&vtop->type, r | VT_LVAL, 0);
325 vswap();
326 vstore();
327 args_size += size;
328 } else if (is_float(vtop->type.t)) {
329 gv(RC_FLOAT); /* only one float register */
330 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
331 size = 4;
332 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
333 size = 8;
334 else
335 size = 12;
336 oad(0xec81, size); /* sub $xxx, %esp */
337 if (size == 12)
338 o(0x7cdb);
339 else
340 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
341 g(0x24);
342 g(0x00);
343 args_size += size;
344 } else {
345 /* simple type (currently always same size) */
346 /* XXX: implicit cast ? */
347 r = gv(RC_INT);
348 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
349 size = 8;
350 o(0x50 + vtop->r2); /* push r */
351 } else {
352 size = 4;
354 o(0x50 + r); /* push r */
355 args_size += size;
357 vtop--;
359 save_regs(0); /* save used temporary registers */
360 func_sym = vtop->type.ref;
361 func_call = FUNC_CALL(func_sym->r);
362 /* fast call case */
363 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
364 func_call == FUNC_FASTCALLW) {
365 int fastcall_nb_regs;
366 uint8_t *fastcall_regs_ptr;
367 if (func_call == FUNC_FASTCALLW) {
368 fastcall_regs_ptr = fastcallw_regs;
369 fastcall_nb_regs = 2;
370 } else {
371 fastcall_regs_ptr = fastcall_regs;
372 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
374 for(i = 0;i < fastcall_nb_regs; i++) {
375 if (args_size <= 0)
376 break;
377 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
378 /* XXX: incorrect for struct/floats */
379 args_size -= 4;
382 gcall_or_jmp(0);
384 #ifdef TCC_TARGET_PE
385 if ((func_sym->type.t & VT_BTYPE) == VT_STRUCT)
386 args_size -= 4;
387 #endif
388 if (args_size && func_call != FUNC_STDCALL)
389 gadd_sp(args_size);
390 vtop--;
393 #ifdef TCC_TARGET_PE
394 #define FUNC_PROLOG_SIZE 10
395 #else
396 #define FUNC_PROLOG_SIZE 9
397 #endif
399 /* generate function prolog of type 't' */
400 void gfunc_prolog(CType *func_type)
402 int addr, align, size, func_call, fastcall_nb_regs;
403 int param_index, param_addr;
404 uint8_t *fastcall_regs_ptr;
405 Sym *sym;
406 CType *type;
408 sym = func_type->ref;
409 func_call = FUNC_CALL(sym->r);
410 addr = 8;
411 loc = 0;
412 func_vc = 0;
414 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
415 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
416 fastcall_regs_ptr = fastcall_regs;
417 } else if (func_call == FUNC_FASTCALLW) {
418 fastcall_nb_regs = 2;
419 fastcall_regs_ptr = fastcallw_regs;
420 } else {
421 fastcall_nb_regs = 0;
422 fastcall_regs_ptr = NULL;
424 param_index = 0;
426 ind += FUNC_PROLOG_SIZE;
427 func_sub_sp_offset = ind;
428 /* if the function returns a structure, then add an
429 implicit pointer parameter */
430 func_vt = sym->type;
431 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
432 /* XXX: fastcall case ? */
433 func_vc = addr;
434 addr += 4;
435 param_index++;
437 /* define parameters */
438 while ((sym = sym->next) != NULL) {
439 type = &sym->type;
440 size = type_size(type, &align);
441 size = (size + 3) & ~3;
442 #ifdef FUNC_STRUCT_PARAM_AS_PTR
443 /* structs are passed as pointer */
444 if ((type->t & VT_BTYPE) == VT_STRUCT) {
445 size = 4;
447 #endif
448 if (param_index < fastcall_nb_regs) {
449 /* save FASTCALL register */
450 loc -= 4;
451 o(0x89); /* movl */
452 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
453 param_addr = loc;
454 } else {
455 param_addr = addr;
456 addr += size;
458 sym_push(sym->v & ~SYM_FIELD, type,
459 VT_LOCAL | lvalue_type(type->t), param_addr);
460 param_index++;
462 func_ret_sub = 0;
463 /* pascal type call ? */
464 if (func_call == FUNC_STDCALL)
465 func_ret_sub = addr - 8;
466 #ifdef TCC_TARGET_PE
467 else if (func_vc)
468 func_ret_sub = 4;
469 #endif
471 /* leave some room for bound checking code */
472 if (tcc_state->do_bounds_check) {
473 oad(0xb8, 0); /* lbound section pointer */
474 oad(0xb8, 0); /* call to function */
475 func_bound_offset = lbounds_section->data_offset;
479 /* generate function epilog */
480 void gfunc_epilog(void)
482 int v, saved_ind;
484 #ifdef CONFIG_TCC_BCHECK
485 if (tcc_state->do_bounds_check
486 && func_bound_offset != lbounds_section->data_offset) {
487 int saved_ind;
488 int *bounds_ptr;
489 Sym *sym, *sym_data;
490 /* add end of table info */
491 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
492 *bounds_ptr = 0;
493 /* generate bound local allocation */
494 saved_ind = ind;
495 ind = func_sub_sp_offset;
496 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
497 func_bound_offset, lbounds_section->data_offset);
498 greloc(cur_text_section, sym_data,
499 ind + 1, R_386_32);
500 oad(0xb8, 0); /* mov %eax, xxx */
501 sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0);
502 greloc(cur_text_section, sym,
503 ind + 1, R_386_PC32);
504 oad(0xe8, -4);
505 ind = saved_ind;
506 /* generate bound check local freeing */
507 o(0x5250); /* save returned value, if any */
508 greloc(cur_text_section, sym_data,
509 ind + 1, R_386_32);
510 oad(0xb8, 0); /* mov %eax, xxx */
511 sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0);
512 greloc(cur_text_section, sym,
513 ind + 1, R_386_PC32);
514 oad(0xe8, -4);
515 o(0x585a); /* restore returned value, if any */
517 #endif
518 o(0xc9); /* leave */
519 if (func_ret_sub == 0) {
520 o(0xc3); /* ret */
521 } else {
522 o(0xc2); /* ret n */
523 g(func_ret_sub);
524 g(func_ret_sub >> 8);
526 /* align local size to word & save local variables */
528 v = (-loc + 3) & -4;
529 saved_ind = ind;
530 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
531 #ifdef TCC_TARGET_PE
532 if (v >= 4096) {
533 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
534 oad(0xb8, v); /* mov stacksize, %eax */
535 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
536 greloc(cur_text_section, sym, ind-4, R_386_PC32);
537 } else
538 #endif
540 o(0xe58955); /* push %ebp, mov %esp, %ebp */
541 o(0xec81); /* sub esp, stacksize */
542 gen_le32(v);
543 #if FUNC_PROLOG_SIZE == 10
544 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
545 #endif
547 ind = saved_ind;
550 /* generate a jump to a label */
551 int gjmp(int t)
553 return psym(0xe9, t);
556 /* generate a jump to a fixed address */
557 void gjmp_addr(int a)
559 int r;
560 r = a - ind - 2;
561 if (r == (char)r) {
562 g(0xeb);
563 g(r);
564 } else {
565 oad(0xe9, a - ind - 5);
569 /* generate a test. set 'inv' to invert test. Stack entry is popped */
570 int gtst(int inv, int t)
572 int v, *p;
574 v = vtop->r & VT_VALMASK;
575 if (v == VT_CMP) {
576 /* fast case : can jump directly since flags are set */
577 g(0x0f);
578 t = psym((vtop->c.i - 16) ^ inv, t);
579 } else if (v == VT_JMP || v == VT_JMPI) {
580 /* && or || optimization */
581 if ((v & 1) == inv) {
582 /* insert vtop->c jump list in t */
583 p = &vtop->c.i;
584 while (*p != 0)
585 p = (int *)(cur_text_section->data + *p);
586 *p = t;
587 t = vtop->c.i;
588 } else {
589 t = gjmp(t);
590 gsym(vtop->c.i);
592 } else {
593 if (is_float(vtop->type.t) ||
594 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
595 vpushi(0);
596 gen_op(TOK_NE);
598 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
599 /* constant jmp optimization */
600 if ((vtop->c.i != 0) != inv)
601 t = gjmp(t);
602 } else {
603 v = gv(RC_INT);
604 o(0x85);
605 o(0xc0 + v * 9);
606 g(0x0f);
607 t = psym(0x85 ^ inv, t);
610 vtop--;
611 return t;
614 /* generate an integer binary operation */
615 void gen_opi(int op)
617 int r, fr, opc, c;
619 switch(op) {
620 case '+':
621 case TOK_ADDC1: /* add with carry generation */
622 opc = 0;
623 gen_op8:
624 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
625 /* constant case */
626 vswap();
627 r = gv(RC_INT);
628 vswap();
629 c = vtop->c.i;
630 if (c == (char)c) {
631 /* XXX: generate inc and dec for smaller code ? */
632 o(0x83);
633 o(0xc0 | (opc << 3) | r);
634 g(c);
635 } else {
636 o(0x81);
637 oad(0xc0 | (opc << 3) | r, c);
639 } else {
640 gv2(RC_INT, RC_INT);
641 r = vtop[-1].r;
642 fr = vtop[0].r;
643 o((opc << 3) | 0x01);
644 o(0xc0 + r + fr * 8);
646 vtop--;
647 if (op >= TOK_ULT && op <= TOK_GT) {
648 vtop->r = VT_CMP;
649 vtop->c.i = op;
651 break;
652 case '-':
653 case TOK_SUBC1: /* sub with carry generation */
654 opc = 5;
655 goto gen_op8;
656 case TOK_ADDC2: /* add with carry use */
657 opc = 2;
658 goto gen_op8;
659 case TOK_SUBC2: /* sub with carry use */
660 opc = 3;
661 goto gen_op8;
662 case '&':
663 opc = 4;
664 goto gen_op8;
665 case '^':
666 opc = 6;
667 goto gen_op8;
668 case '|':
669 opc = 1;
670 goto gen_op8;
671 case '*':
672 gv2(RC_INT, RC_INT);
673 r = vtop[-1].r;
674 fr = vtop[0].r;
675 vtop--;
676 o(0xaf0f); /* imul fr, r */
677 o(0xc0 + fr + r * 8);
678 break;
679 case TOK_SHL:
680 opc = 4;
681 goto gen_shift;
682 case TOK_SHR:
683 opc = 5;
684 goto gen_shift;
685 case TOK_SAR:
686 opc = 7;
687 gen_shift:
688 opc = 0xc0 | (opc << 3);
689 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
690 /* constant case */
691 vswap();
692 r = gv(RC_INT);
693 vswap();
694 c = vtop->c.i & 0x1f;
695 o(0xc1); /* shl/shr/sar $xxx, r */
696 o(opc | r);
697 g(c);
698 } else {
699 /* we generate the shift in ecx */
700 gv2(RC_INT, RC_ECX);
701 r = vtop[-1].r;
702 o(0xd3); /* shl/shr/sar %cl, r */
703 o(opc | r);
705 vtop--;
706 break;
707 case '/':
708 case TOK_UDIV:
709 case TOK_PDIV:
710 case '%':
711 case TOK_UMOD:
712 case TOK_UMULL:
713 /* first operand must be in eax */
714 /* XXX: need better constraint for second operand */
715 gv2(RC_EAX, RC_ECX);
716 r = vtop[-1].r;
717 fr = vtop[0].r;
718 vtop--;
719 save_reg(TREG_EDX);
720 if (op == TOK_UMULL) {
721 o(0xf7); /* mul fr */
722 o(0xe0 + fr);
723 vtop->r2 = TREG_EDX;
724 r = TREG_EAX;
725 } else {
726 if (op == TOK_UDIV || op == TOK_UMOD) {
727 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
728 o(0xf0 + fr);
729 } else {
730 o(0xf799); /* cltd, idiv fr, %eax */
731 o(0xf8 + fr);
733 if (op == '%' || op == TOK_UMOD)
734 r = TREG_EDX;
735 else
736 r = TREG_EAX;
738 vtop->r = r;
739 break;
740 default:
741 opc = 7;
742 goto gen_op8;
746 /* generate a floating point operation 'v = t1 op t2' instruction. The
747 two operands are guaranted to have the same floating point type */
748 /* XXX: need to use ST1 too */
749 void gen_opf(int op)
751 int a, ft, fc, swapped, r;
753 /* convert constants to memory references */
754 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
755 vswap();
756 gv(RC_FLOAT);
757 vswap();
759 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
760 gv(RC_FLOAT);
762 /* must put at least one value in the floating point register */
763 if ((vtop[-1].r & VT_LVAL) &&
764 (vtop[0].r & VT_LVAL)) {
765 vswap();
766 gv(RC_FLOAT);
767 vswap();
769 swapped = 0;
770 /* swap the stack if needed so that t1 is the register and t2 is
771 the memory reference */
772 if (vtop[-1].r & VT_LVAL) {
773 vswap();
774 swapped = 1;
776 if (op >= TOK_ULT && op <= TOK_GT) {
777 /* load on stack second operand */
778 load(TREG_ST0, vtop);
779 save_reg(TREG_EAX); /* eax is used by FP comparison code */
780 if (op == TOK_GE || op == TOK_GT)
781 swapped = !swapped;
782 else if (op == TOK_EQ || op == TOK_NE)
783 swapped = 0;
784 if (swapped)
785 o(0xc9d9); /* fxch %st(1) */
786 o(0xe9da); /* fucompp */
787 o(0xe0df); /* fnstsw %ax */
788 if (op == TOK_EQ) {
789 o(0x45e480); /* and $0x45, %ah */
790 o(0x40fC80); /* cmp $0x40, %ah */
791 } else if (op == TOK_NE) {
792 o(0x45e480); /* and $0x45, %ah */
793 o(0x40f480); /* xor $0x40, %ah */
794 op = TOK_NE;
795 } else if (op == TOK_GE || op == TOK_LE) {
796 o(0x05c4f6); /* test $0x05, %ah */
797 op = TOK_EQ;
798 } else {
799 o(0x45c4f6); /* test $0x45, %ah */
800 op = TOK_EQ;
802 vtop--;
803 vtop->r = VT_CMP;
804 vtop->c.i = op;
805 } else {
806 /* no memory reference possible for long double operations */
807 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
808 load(TREG_ST0, vtop);
809 swapped = !swapped;
812 switch(op) {
813 default:
814 case '+':
815 a = 0;
816 break;
817 case '-':
818 a = 4;
819 if (swapped)
820 a++;
821 break;
822 case '*':
823 a = 1;
824 break;
825 case '/':
826 a = 6;
827 if (swapped)
828 a++;
829 break;
831 ft = vtop->type.t;
832 fc = vtop->c.ul;
833 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
834 o(0xde); /* fxxxp %st, %st(1) */
835 o(0xc1 + (a << 3));
836 } else {
837 /* if saved lvalue, then we must reload it */
838 r = vtop->r;
839 if ((r & VT_VALMASK) == VT_LLOCAL) {
840 SValue v1;
841 r = get_reg(RC_INT);
842 v1.type.t = VT_INT;
843 v1.r = VT_LOCAL | VT_LVAL;
844 v1.c.ul = fc;
845 load(r, &v1);
846 fc = 0;
849 if ((ft & VT_BTYPE) == VT_DOUBLE)
850 o(0xdc);
851 else
852 o(0xd8);
853 gen_modrm(a, r, vtop->sym, fc);
855 vtop--;
859 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
860 and 'long long' cases. */
861 void gen_cvt_itof(int t)
863 save_reg(TREG_ST0);
864 gv(RC_INT);
865 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
866 /* signed long long to float/double/long double (unsigned case
867 is handled generically) */
868 o(0x50 + vtop->r2); /* push r2 */
869 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
870 o(0x242cdf); /* fildll (%esp) */
871 o(0x08c483); /* add $8, %esp */
872 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
873 (VT_INT | VT_UNSIGNED)) {
874 /* unsigned int to float/double/long double */
875 o(0x6a); /* push $0 */
876 g(0x00);
877 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
878 o(0x242cdf); /* fildll (%esp) */
879 o(0x08c483); /* add $8, %esp */
880 } else {
881 /* int to float/double/long double */
882 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
883 o(0x2404db); /* fildl (%esp) */
884 o(0x04c483); /* add $4, %esp */
886 vtop->r = TREG_ST0;
889 /* convert fp to int 't' type */
890 /* XXX: handle long long case */
891 void gen_cvt_ftoi(int t)
893 int r, r2, size;
894 Sym *sym;
895 CType ushort_type;
897 ushort_type.t = VT_SHORT | VT_UNSIGNED;
899 gv(RC_FLOAT);
900 if (t != VT_INT)
901 size = 8;
902 else
903 size = 4;
905 o(0x2dd9); /* ldcw xxx */
906 sym = external_global_sym(TOK___tcc_int_fpu_control,
907 &ushort_type, VT_LVAL);
908 greloc(cur_text_section, sym,
909 ind, R_386_32);
910 gen_le32(0);
912 oad(0xec81, size); /* sub $xxx, %esp */
913 if (size == 4)
914 o(0x1cdb); /* fistpl */
915 else
916 o(0x3cdf); /* fistpll */
917 o(0x24);
918 o(0x2dd9); /* ldcw xxx */
919 sym = external_global_sym(TOK___tcc_fpu_control,
920 &ushort_type, VT_LVAL);
921 greloc(cur_text_section, sym,
922 ind, R_386_32);
923 gen_le32(0);
925 r = get_reg(RC_INT);
926 o(0x58 + r); /* pop r */
927 if (size == 8) {
928 if (t == VT_LLONG) {
929 vtop->r = r; /* mark reg as used */
930 r2 = get_reg(RC_INT);
931 o(0x58 + r2); /* pop r2 */
932 vtop->r2 = r2;
933 } else {
934 o(0x04c483); /* add $4, %esp */
937 vtop->r = r;
940 /* convert from one floating point type to another */
941 void gen_cvt_ftof(int t)
943 /* all we have to do on i386 is to put the float in a register */
944 gv(RC_FLOAT);
947 /* computed goto support */
948 void ggoto(void)
950 gcall_or_jmp(1);
951 vtop--;
954 /* bound check support functions */
955 #ifdef CONFIG_TCC_BCHECK
957 /* generate a bounded pointer addition */
958 void gen_bounded_ptr_add(void)
960 Sym *sym;
962 /* prepare fast i386 function call (args in eax and edx) */
963 gv2(RC_EAX, RC_EDX);
964 /* save all temporary registers */
965 vtop -= 2;
966 save_regs(0);
967 /* do a fast function call */
968 sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
969 greloc(cur_text_section, sym,
970 ind + 1, R_386_PC32);
971 oad(0xe8, -4);
972 /* returned pointer is in eax */
973 vtop++;
974 vtop->r = TREG_EAX | VT_BOUNDED;
975 /* address of bounding function call point */
976 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
979 /* patch pointer addition in vtop so that pointer dereferencing is
980 also tested */
981 void gen_bounded_ptr_deref(void)
983 int func;
984 int size, align;
985 Elf32_Rel *rel;
986 Sym *sym;
988 size = 0;
989 /* XXX: put that code in generic part of tcc */
990 if (!is_float(vtop->type.t)) {
991 if (vtop->r & VT_LVAL_BYTE)
992 size = 1;
993 else if (vtop->r & VT_LVAL_SHORT)
994 size = 2;
996 if (!size)
997 size = type_size(&vtop->type, &align);
998 switch(size) {
999 case 1: func = TOK___bound_ptr_indir1; break;
1000 case 2: func = TOK___bound_ptr_indir2; break;
1001 case 4: func = TOK___bound_ptr_indir4; break;
1002 case 8: func = TOK___bound_ptr_indir8; break;
1003 case 12: func = TOK___bound_ptr_indir12; break;
1004 case 16: func = TOK___bound_ptr_indir16; break;
1005 default:
1006 error("unhandled size when derefencing bounded pointer");
1007 func = 0;
1008 break;
1011 /* patch relocation */
1012 /* XXX: find a better solution ? */
1013 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1014 sym = external_global_sym(func, &func_old_type, 0);
1015 if (!sym->c)
1016 put_extern_sym(sym, NULL, 0, 0);
1017 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1019 #endif
1021 /* end of X86 code generator */
1022 /*************************************************************/