i386-gen: preserve fp control word in gen_cvt_ftoi
[tinycc.git] / i386-gen.c
blob0a6d4d321af6c7326cfab6f71527b23382d35ad4
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 #ifdef TARGET_DEFS_ONLY
23 /* number of available registers */
24 #define NB_REGS 4
25 #define NB_ASM_REGS 8
27 /* a register can belong to several classes. The classes must be
28 sorted from more general to more precise (see gv2() code which does
29 assumptions on it). */
30 #define RC_INT 0x0001 /* generic integer register */
31 #define RC_FLOAT 0x0002 /* generic float register */
32 #define RC_EAX 0x0004
33 #define RC_ST0 0x0008
34 #define RC_ECX 0x0010
35 #define RC_EDX 0x0020
36 #define RC_IRET RC_EAX /* function return: integer register */
37 #define RC_LRET RC_EDX /* function return: second integer register */
38 #define RC_FRET RC_ST0 /* function return: float register */
40 /* pretty names for the registers */
41 enum {
42 TREG_EAX = 0,
43 TREG_ECX,
44 TREG_EDX,
45 TREG_ST0,
46 TREG_ESP = 4
49 /* return registers for function */
50 #define REG_IRET TREG_EAX /* single word int return register */
51 #define REG_LRET TREG_EDX /* second word return register (for long long) */
52 #define REG_FRET TREG_ST0 /* float return register */
54 /* defined if function parameters must be evaluated in reverse order */
55 #define INVERT_FUNC_PARAMS
57 /* defined if structures are passed as pointers. Otherwise structures
58 are directly pushed on stack. */
59 /* #define FUNC_STRUCT_PARAM_AS_PTR */
61 /* pointer size, in bytes */
62 #define PTR_SIZE 4
64 /* long double size and alignment, in bytes */
65 #define LDOUBLE_SIZE 12
66 #define LDOUBLE_ALIGN 4
67 /* maximum alignment (for aligned attribute support) */
68 #define MAX_ALIGN 8
71 #define psym oad
73 /******************************************************/
74 /* ELF defines */
76 #define EM_TCC_TARGET EM_386
78 /* relocation type for 32 bit data relocation */
79 #define R_DATA_32 R_386_32
80 #define R_DATA_PTR R_386_32
81 #define R_JMP_SLOT R_386_JMP_SLOT
82 #define R_COPY R_386_COPY
84 #define ELF_START_ADDR 0x08048000
85 #define ELF_PAGE_SIZE 0x1000
87 /******************************************************/
88 #else /* ! TARGET_DEFS_ONLY */
89 /******************************************************/
90 #include "tcc.h"
92 ST_DATA const int reg_classes[NB_REGS] = {
93 /* eax */ RC_INT | RC_EAX,
94 /* ecx */ RC_INT | RC_ECX,
95 /* edx */ RC_INT | RC_EDX,
96 /* st0 */ RC_FLOAT | RC_ST0,
99 static unsigned long func_sub_sp_offset;
100 static int func_ret_sub;
101 #ifdef CONFIG_TCC_BCHECK
102 static unsigned long func_bound_offset;
103 #endif
105 /* XXX: make it faster ? */
106 ST_FUNC void g(int c)
108 int ind1;
109 ind1 = ind + 1;
110 if (ind1 > cur_text_section->data_allocated)
111 section_realloc(cur_text_section, ind1);
112 cur_text_section->data[ind] = c;
113 ind = ind1;
116 ST_FUNC void o(unsigned int c)
118 while (c) {
119 g(c);
120 c = c >> 8;
124 ST_FUNC void gen_le16(int v)
126 g(v);
127 g(v >> 8);
130 ST_FUNC void gen_le32(int c)
132 g(c);
133 g(c >> 8);
134 g(c >> 16);
135 g(c >> 24);
138 /* output a symbol and patch all calls to it */
139 ST_FUNC void gsym_addr(int t, int a)
141 int n, *ptr;
142 while (t) {
143 ptr = (int *)(cur_text_section->data + t);
144 n = *ptr; /* next value */
145 *ptr = a - t - 4;
146 t = n;
150 ST_FUNC void gsym(int t)
152 gsym_addr(t, ind);
155 /* psym is used to put an instruction with a data field which is a
156 reference to a symbol. It is in fact the same as oad ! */
157 #define psym oad
159 /* instruction + 4 bytes data. Return the address of the data */
160 ST_FUNC int oad(int c, int s)
162 int ind1;
164 o(c);
165 ind1 = ind + 4;
166 if (ind1 > cur_text_section->data_allocated)
167 section_realloc(cur_text_section, ind1);
168 *(int *)(cur_text_section->data + ind) = s;
169 s = ind;
170 ind = ind1;
171 return s;
174 /* output constant with relocation if 'r & VT_SYM' is true */
175 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
177 if (r & VT_SYM)
178 greloc(cur_text_section, sym, ind, R_386_32);
179 gen_le32(c);
182 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
184 if (r & VT_SYM)
185 greloc(cur_text_section, sym, ind, R_386_PC32);
186 gen_le32(c - 4);
189 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
190 opcode bits */
191 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
193 op_reg = op_reg << 3;
194 if ((r & VT_VALMASK) == VT_CONST) {
195 /* constant memory reference */
196 o(0x05 | op_reg);
197 gen_addr32(r, sym, c);
198 } else if ((r & VT_VALMASK) == VT_LOCAL) {
199 /* currently, we use only ebp as base */
200 if (c == (char)c) {
201 /* short reference */
202 o(0x45 | op_reg);
203 g(c);
204 } else {
205 oad(0x85 | op_reg, c);
207 } else {
208 g(0x00 | op_reg | (r & VT_VALMASK));
212 /* load 'r' from value 'sv' */
213 ST_FUNC void load(int r, SValue *sv)
215 int v, t, ft, fc, fr;
216 SValue v1;
218 #ifdef TCC_TARGET_PE
219 SValue v2;
220 sv = pe_getimport(sv, &v2);
221 #endif
223 fr = sv->r;
224 ft = sv->type.t;
225 fc = sv->c.ul;
227 v = fr & VT_VALMASK;
228 if (fr & VT_LVAL) {
229 if (v == VT_LLOCAL) {
230 v1.type.t = VT_INT;
231 v1.r = VT_LOCAL | VT_LVAL;
232 v1.c.ul = fc;
233 fr = r;
234 if (!(reg_classes[fr] & RC_INT))
235 fr = get_reg(RC_INT);
236 load(fr, &v1);
238 if ((ft & VT_BTYPE) == VT_FLOAT) {
239 o(0xd9); /* flds */
240 r = 0;
241 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
242 o(0xdd); /* fldl */
243 r = 0;
244 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
245 o(0xdb); /* fldt */
246 r = 5;
247 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
248 o(0xbe0f); /* movsbl */
249 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
250 o(0xb60f); /* movzbl */
251 } else if ((ft & VT_TYPE) == VT_SHORT) {
252 o(0xbf0f); /* movswl */
253 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
254 o(0xb70f); /* movzwl */
255 } else {
256 o(0x8b); /* movl */
258 gen_modrm(r, fr, sv->sym, fc);
259 } else {
260 if (v == VT_CONST) {
261 o(0xb8 + r); /* mov $xx, r */
262 gen_addr32(fr, sv->sym, fc);
263 } else if (v == VT_LOCAL) {
264 if (fc) {
265 o(0x8d); /* lea xxx(%ebp), r */
266 gen_modrm(r, VT_LOCAL, sv->sym, fc);
267 } else {
268 o(0x89);
269 o(0xe8 + r); /* mov %ebp, r */
271 } else if (v == VT_CMP) {
272 oad(0xb8 + r, 0); /* mov $0, r */
273 o(0x0f); /* setxx %br */
274 o(fc);
275 o(0xc0 + r);
276 } else if (v == VT_JMP || v == VT_JMPI) {
277 t = v & 1;
278 oad(0xb8 + r, t); /* mov $1, r */
279 o(0x05eb); /* jmp after */
280 gsym(fc);
281 oad(0xb8 + r, t ^ 1); /* mov $0, r */
282 } else if (v != r) {
283 o(0x89);
284 o(0xc0 + r + v * 8); /* mov v, r */
289 /* store register 'r' in lvalue 'v' */
290 ST_FUNC void store(int r, SValue *v)
292 int fr, bt, ft, fc;
294 #ifdef TCC_TARGET_PE
295 SValue v2;
296 v = pe_getimport(v, &v2);
297 #endif
299 ft = v->type.t;
300 fc = v->c.ul;
301 fr = v->r & VT_VALMASK;
302 bt = ft & VT_BTYPE;
303 /* XXX: incorrect if float reg to reg */
304 if (bt == VT_FLOAT) {
305 o(0xd9); /* fsts */
306 r = 2;
307 } else if (bt == VT_DOUBLE) {
308 o(0xdd); /* fstpl */
309 r = 2;
310 } else if (bt == VT_LDOUBLE) {
311 o(0xc0d9); /* fld %st(0) */
312 o(0xdb); /* fstpt */
313 r = 7;
314 } else {
315 if (bt == VT_SHORT)
316 o(0x66);
317 if (bt == VT_BYTE || bt == VT_BOOL)
318 o(0x88);
319 else
320 o(0x89);
322 if (fr == VT_CONST ||
323 fr == VT_LOCAL ||
324 (v->r & VT_LVAL)) {
325 gen_modrm(r, v->r, v->sym, fc);
326 } else if (fr != r) {
327 o(0xc0 + fr + r * 8); /* mov r, fr */
331 static void gadd_sp(int val)
333 if (val == (char)val) {
334 o(0xc483);
335 g(val);
336 } else {
337 oad(0xc481, val); /* add $xxx, %esp */
341 static void gen_static_call(int v)
343 Sym *sym;
345 sym = external_global_sym(v, &func_old_type, 0);
346 oad(0xe8, -4);
347 greloc(cur_text_section, sym, ind-4, R_386_PC32);
350 /* 'is_jmp' is '1' if it is a jump */
351 static void gcall_or_jmp(int is_jmp)
353 int r;
354 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
355 /* constant case */
356 if (vtop->r & VT_SYM) {
357 /* relocation case */
358 greloc(cur_text_section, vtop->sym,
359 ind + 1, R_386_PC32);
360 } else {
361 /* put an empty PC32 relocation */
362 put_elf_reloc(symtab_section, cur_text_section,
363 ind + 1, R_386_PC32, 0);
365 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
366 } else {
367 /* otherwise, indirect call */
368 r = gv(RC_INT);
369 o(0xff); /* call/jmp *r */
370 o(0xd0 + r + (is_jmp << 4));
374 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
375 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
377 /* Return 1 if this function returns via an sret pointer, 0 otherwise */
378 ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align)
380 #ifdef TCC_TARGET_PE
381 int size, align;
383 *ret_align = 1; // Never have to re-align return values for x86
384 size = type_size(vt, &align);
385 if (size > 8) {
386 return 1;
387 } else if (size > 4) {
388 ret->ref = NULL;
389 ret->t = VT_LLONG;
390 return 0;
391 } else {
392 ret->ref = NULL;
393 ret->t = VT_INT;
394 return 0;
396 #else
397 *ret_align = 1; // Never have to re-align return values for x86
398 return 1;
399 #endif
402 /* Generate function call. The function address is pushed first, then
403 all the parameters in call order. This functions pops all the
404 parameters and the function address. */
405 ST_FUNC void gfunc_call(int nb_args)
407 int size, align, r, args_size, i, func_call;
408 Sym *func_sym;
410 args_size = 0;
411 for(i = 0;i < nb_args; i++) {
412 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
413 size = type_size(&vtop->type, &align);
414 /* align to stack align size */
415 size = (size + 3) & ~3;
416 /* allocate the necessary size on stack */
417 oad(0xec81, size); /* sub $xxx, %esp */
418 /* generate structure store */
419 r = get_reg(RC_INT);
420 o(0x89); /* mov %esp, r */
421 o(0xe0 + r);
422 vset(&vtop->type, r | VT_LVAL, 0);
423 vswap();
424 vstore();
425 args_size += size;
426 } else if (is_float(vtop->type.t)) {
427 gv(RC_FLOAT); /* only one float register */
428 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
429 size = 4;
430 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
431 size = 8;
432 else
433 size = 12;
434 oad(0xec81, size); /* sub $xxx, %esp */
435 if (size == 12)
436 o(0x7cdb);
437 else
438 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
439 g(0x24);
440 g(0x00);
441 args_size += size;
442 } else {
443 /* simple type (currently always same size) */
444 /* XXX: implicit cast ? */
445 r = gv(RC_INT);
446 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
447 size = 8;
448 o(0x50 + vtop->r2); /* push r */
449 } else {
450 size = 4;
452 o(0x50 + r); /* push r */
453 args_size += size;
455 vtop--;
457 save_regs(0); /* save used temporary registers */
458 func_sym = vtop->type.ref;
459 func_call = FUNC_CALL(func_sym->r);
460 /* fast call case */
461 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
462 func_call == FUNC_FASTCALLW) {
463 int fastcall_nb_regs;
464 uint8_t *fastcall_regs_ptr;
465 if (func_call == FUNC_FASTCALLW) {
466 fastcall_regs_ptr = fastcallw_regs;
467 fastcall_nb_regs = 2;
468 } else {
469 fastcall_regs_ptr = fastcall_regs;
470 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
472 for(i = 0;i < fastcall_nb_regs; i++) {
473 if (args_size <= 0)
474 break;
475 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
476 /* XXX: incorrect for struct/floats */
477 args_size -= 4;
480 #ifndef TCC_TARGET_PE
481 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
482 args_size -= 4;
483 #endif
484 gcall_or_jmp(0);
486 if (args_size && func_call != FUNC_STDCALL)
487 gadd_sp(args_size);
488 vtop--;
491 #ifdef TCC_TARGET_PE
492 #define FUNC_PROLOG_SIZE 10
493 #else
494 #define FUNC_PROLOG_SIZE 9
495 #endif
497 /* generate function prolog of type 't' */
498 ST_FUNC void gfunc_prolog(CType *func_type)
500 int addr, align, size, func_call, fastcall_nb_regs;
501 int param_index, param_addr;
502 uint8_t *fastcall_regs_ptr;
503 Sym *sym;
504 CType *type;
506 sym = func_type->ref;
507 func_call = FUNC_CALL(sym->r);
508 addr = 8;
509 loc = 0;
510 func_vc = 0;
512 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
513 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
514 fastcall_regs_ptr = fastcall_regs;
515 } else if (func_call == FUNC_FASTCALLW) {
516 fastcall_nb_regs = 2;
517 fastcall_regs_ptr = fastcallw_regs;
518 } else {
519 fastcall_nb_regs = 0;
520 fastcall_regs_ptr = NULL;
522 param_index = 0;
524 ind += FUNC_PROLOG_SIZE;
525 func_sub_sp_offset = ind;
526 /* if the function returns a structure, then add an
527 implicit pointer parameter */
528 func_vt = sym->type;
529 #ifdef TCC_TARGET_PE
530 size = type_size(&func_vt,&align);
531 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
532 #else
533 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
534 #endif
535 /* XXX: fastcall case ? */
536 func_vc = addr;
537 addr += 4;
538 param_index++;
540 /* define parameters */
541 while ((sym = sym->next) != NULL) {
542 type = &sym->type;
543 size = type_size(type, &align);
544 size = (size + 3) & ~3;
545 #ifdef FUNC_STRUCT_PARAM_AS_PTR
546 /* structs are passed as pointer */
547 if ((type->t & VT_BTYPE) == VT_STRUCT) {
548 size = 4;
550 #endif
551 if (param_index < fastcall_nb_regs) {
552 /* save FASTCALL register */
553 loc -= 4;
554 o(0x89); /* movl */
555 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
556 param_addr = loc;
557 } else {
558 param_addr = addr;
559 addr += size;
561 sym_push(sym->v & ~SYM_FIELD, type,
562 VT_LOCAL | lvalue_type(type->t), param_addr);
563 param_index++;
565 func_ret_sub = 0;
566 /* pascal type call ? */
567 if (func_call == FUNC_STDCALL)
568 func_ret_sub = addr - 8;
569 #ifndef TCC_TARGET_PE
570 else if (func_vc)
571 func_ret_sub = 4;
572 #endif
574 #ifdef CONFIG_TCC_BCHECK
575 /* leave some room for bound checking code */
576 if (tcc_state->do_bounds_check) {
577 oad(0xb8, 0); /* lbound section pointer */
578 oad(0xb8, 0); /* call to function */
579 func_bound_offset = lbounds_section->data_offset;
581 #endif
583 #ifndef TCC_TARGET_PE
584 if (0 == strcmp(funcname, "main"))
585 gen_static_call(TOK___tcc_fpinit);
586 #endif
590 /* generate function epilog */
591 ST_FUNC void gfunc_epilog(void)
593 int v, saved_ind;
595 #ifdef CONFIG_TCC_BCHECK
596 if (tcc_state->do_bounds_check
597 && func_bound_offset != lbounds_section->data_offset) {
598 int saved_ind;
599 int *bounds_ptr;
600 Sym *sym_data;
601 /* add end of table info */
602 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
603 *bounds_ptr = 0;
604 /* generate bound local allocation */
605 saved_ind = ind;
606 ind = func_sub_sp_offset;
607 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
608 func_bound_offset, lbounds_section->data_offset);
609 greloc(cur_text_section, sym_data,
610 ind + 1, R_386_32);
611 oad(0xb8, 0); /* mov %eax, xxx */
612 gen_static_call(TOK___bound_local_new);
614 ind = saved_ind;
615 /* generate bound check local freeing */
616 o(0x5250); /* save returned value, if any */
617 greloc(cur_text_section, sym_data,
618 ind + 1, R_386_32);
619 oad(0xb8, 0); /* mov %eax, xxx */
620 gen_static_call(TOK___bound_local_delete);
622 o(0x585a); /* restore returned value, if any */
624 #endif
625 o(0xc9); /* leave */
626 if (func_ret_sub == 0) {
627 o(0xc3); /* ret */
628 } else {
629 o(0xc2); /* ret n */
630 g(func_ret_sub);
631 g(func_ret_sub >> 8);
633 /* align local size to word & save local variables */
635 v = (-loc + 3) & -4;
636 saved_ind = ind;
637 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
638 #ifdef TCC_TARGET_PE
639 if (v >= 4096) {
640 oad(0xb8, v); /* mov stacksize, %eax */
641 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
642 } else
643 #endif
645 o(0xe58955); /* push %ebp, mov %esp, %ebp */
646 o(0xec81); /* sub esp, stacksize */
647 gen_le32(v);
648 #if FUNC_PROLOG_SIZE == 10
649 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
650 #endif
652 ind = saved_ind;
655 /* generate a jump to a label */
656 ST_FUNC int gjmp(int t)
658 return psym(0xe9, t);
661 /* generate a jump to a fixed address */
662 ST_FUNC void gjmp_addr(int a)
664 int r;
665 r = a - ind - 2;
666 if (r == (char)r) {
667 g(0xeb);
668 g(r);
669 } else {
670 oad(0xe9, a - ind - 5);
674 /* generate a test. set 'inv' to invert test. Stack entry is popped */
675 ST_FUNC int gtst(int inv, int t)
677 int v, *p;
679 v = vtop->r & VT_VALMASK;
680 if (v == VT_CMP) {
681 /* fast case : can jump directly since flags are set */
682 g(0x0f);
683 t = psym((vtop->c.i - 16) ^ inv, t);
684 } else if (v == VT_JMP || v == VT_JMPI) {
685 /* && or || optimization */
686 if ((v & 1) == inv) {
687 /* insert vtop->c jump list in t */
688 p = &vtop->c.i;
689 while (*p != 0)
690 p = (int *)(cur_text_section->data + *p);
691 *p = t;
692 t = vtop->c.i;
693 } else {
694 t = gjmp(t);
695 gsym(vtop->c.i);
697 } else {
698 if (is_float(vtop->type.t) ||
699 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
700 vpushi(0);
701 gen_op(TOK_NE);
703 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
704 /* constant jmp optimization */
705 if ((vtop->c.i != 0) != inv)
706 t = gjmp(t);
707 } else {
708 v = gv(RC_INT);
709 o(0x85);
710 o(0xc0 + v * 9);
711 g(0x0f);
712 t = psym(0x85 ^ inv, t);
715 vtop--;
716 return t;
719 /* generate an integer binary operation */
720 ST_FUNC void gen_opi(int op)
722 int r, fr, opc, c;
724 switch(op) {
725 case '+':
726 case TOK_ADDC1: /* add with carry generation */
727 opc = 0;
728 gen_op8:
729 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
730 /* constant case */
731 vswap();
732 r = gv(RC_INT);
733 vswap();
734 c = vtop->c.i;
735 if (c == (char)c) {
736 /* generate inc and dec for smaller code */
737 if (c==1 && opc==0) {
738 o (0x40 | r); // inc
739 } else if (c==1 && opc==5) {
740 o (0x48 | r); // dec
741 } else {
742 o(0x83);
743 o(0xc0 | (opc << 3) | r);
744 g(c);
746 } else {
747 o(0x81);
748 oad(0xc0 | (opc << 3) | r, c);
750 } else {
751 gv2(RC_INT, RC_INT);
752 r = vtop[-1].r;
753 fr = vtop[0].r;
754 o((opc << 3) | 0x01);
755 o(0xc0 + r + fr * 8);
757 vtop--;
758 if (op >= TOK_ULT && op <= TOK_GT) {
759 vtop->r = VT_CMP;
760 vtop->c.i = op;
762 break;
763 case '-':
764 case TOK_SUBC1: /* sub with carry generation */
765 opc = 5;
766 goto gen_op8;
767 case TOK_ADDC2: /* add with carry use */
768 opc = 2;
769 goto gen_op8;
770 case TOK_SUBC2: /* sub with carry use */
771 opc = 3;
772 goto gen_op8;
773 case '&':
774 opc = 4;
775 goto gen_op8;
776 case '^':
777 opc = 6;
778 goto gen_op8;
779 case '|':
780 opc = 1;
781 goto gen_op8;
782 case '*':
783 gv2(RC_INT, RC_INT);
784 r = vtop[-1].r;
785 fr = vtop[0].r;
786 vtop--;
787 o(0xaf0f); /* imul fr, r */
788 o(0xc0 + fr + r * 8);
789 break;
790 case TOK_SHL:
791 opc = 4;
792 goto gen_shift;
793 case TOK_SHR:
794 opc = 5;
795 goto gen_shift;
796 case TOK_SAR:
797 opc = 7;
798 gen_shift:
799 opc = 0xc0 | (opc << 3);
800 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
801 /* constant case */
802 vswap();
803 r = gv(RC_INT);
804 vswap();
805 c = vtop->c.i & 0x1f;
806 o(0xc1); /* shl/shr/sar $xxx, r */
807 o(opc | r);
808 g(c);
809 } else {
810 /* we generate the shift in ecx */
811 gv2(RC_INT, RC_ECX);
812 r = vtop[-1].r;
813 o(0xd3); /* shl/shr/sar %cl, r */
814 o(opc | r);
816 vtop--;
817 break;
818 case '/':
819 case TOK_UDIV:
820 case TOK_PDIV:
821 case '%':
822 case TOK_UMOD:
823 case TOK_UMULL:
824 /* first operand must be in eax */
825 /* XXX: need better constraint for second operand */
826 gv2(RC_EAX, RC_ECX);
827 r = vtop[-1].r;
828 fr = vtop[0].r;
829 vtop--;
830 save_reg(TREG_EDX);
831 if (op == TOK_UMULL) {
832 o(0xf7); /* mul fr */
833 o(0xe0 + fr);
834 vtop->r2 = TREG_EDX;
835 r = TREG_EAX;
836 } else {
837 if (op == TOK_UDIV || op == TOK_UMOD) {
838 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
839 o(0xf0 + fr);
840 } else {
841 o(0xf799); /* cltd, idiv fr, %eax */
842 o(0xf8 + fr);
844 if (op == '%' || op == TOK_UMOD)
845 r = TREG_EDX;
846 else
847 r = TREG_EAX;
849 vtop->r = r;
850 break;
851 default:
852 opc = 7;
853 goto gen_op8;
857 /* generate a floating point operation 'v = t1 op t2' instruction. The
858 two operands are guaranted to have the same floating point type */
859 /* XXX: need to use ST1 too */
860 ST_FUNC void gen_opf(int op)
862 int a, ft, fc, swapped, r;
864 /* convert constants to memory references */
865 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
866 vswap();
867 gv(RC_FLOAT);
868 vswap();
870 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
871 gv(RC_FLOAT);
873 /* must put at least one value in the floating point register */
874 if ((vtop[-1].r & VT_LVAL) &&
875 (vtop[0].r & VT_LVAL)) {
876 vswap();
877 gv(RC_FLOAT);
878 vswap();
880 swapped = 0;
881 /* swap the stack if needed so that t1 is the register and t2 is
882 the memory reference */
883 if (vtop[-1].r & VT_LVAL) {
884 vswap();
885 swapped = 1;
887 if (op >= TOK_ULT && op <= TOK_GT) {
888 /* load on stack second operand */
889 load(TREG_ST0, vtop);
890 save_reg(TREG_EAX); /* eax is used by FP comparison code */
891 if (op == TOK_GE || op == TOK_GT)
892 swapped = !swapped;
893 else if (op == TOK_EQ || op == TOK_NE)
894 swapped = 0;
895 if (swapped)
896 o(0xc9d9); /* fxch %st(1) */
897 o(0xe9da); /* fucompp */
898 o(0xe0df); /* fnstsw %ax */
899 if (op == TOK_EQ) {
900 o(0x45e480); /* and $0x45, %ah */
901 o(0x40fC80); /* cmp $0x40, %ah */
902 } else if (op == TOK_NE) {
903 o(0x45e480); /* and $0x45, %ah */
904 o(0x40f480); /* xor $0x40, %ah */
905 op = TOK_NE;
906 } else if (op == TOK_GE || op == TOK_LE) {
907 o(0x05c4f6); /* test $0x05, %ah */
908 op = TOK_EQ;
909 } else {
910 o(0x45c4f6); /* test $0x45, %ah */
911 op = TOK_EQ;
913 vtop--;
914 vtop->r = VT_CMP;
915 vtop->c.i = op;
916 } else {
917 /* no memory reference possible for long double operations */
918 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
919 load(TREG_ST0, vtop);
920 swapped = !swapped;
923 switch(op) {
924 default:
925 case '+':
926 a = 0;
927 break;
928 case '-':
929 a = 4;
930 if (swapped)
931 a++;
932 break;
933 case '*':
934 a = 1;
935 break;
936 case '/':
937 a = 6;
938 if (swapped)
939 a++;
940 break;
942 ft = vtop->type.t;
943 fc = vtop->c.ul;
944 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
945 o(0xde); /* fxxxp %st, %st(1) */
946 o(0xc1 + (a << 3));
947 } else {
948 /* if saved lvalue, then we must reload it */
949 r = vtop->r;
950 if ((r & VT_VALMASK) == VT_LLOCAL) {
951 SValue v1;
952 r = get_reg(RC_INT);
953 v1.type.t = VT_INT;
954 v1.r = VT_LOCAL | VT_LVAL;
955 v1.c.ul = fc;
956 load(r, &v1);
957 fc = 0;
960 if ((ft & VT_BTYPE) == VT_DOUBLE)
961 o(0xdc);
962 else
963 o(0xd8);
964 gen_modrm(a, r, vtop->sym, fc);
966 vtop--;
970 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
971 and 'long long' cases. */
972 ST_FUNC void gen_cvt_itof(int t)
974 save_reg(TREG_ST0);
975 gv(RC_INT);
976 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
977 /* signed long long to float/double/long double (unsigned case
978 is handled generically) */
979 o(0x50 + vtop->r2); /* push r2 */
980 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
981 o(0x242cdf); /* fildll (%esp) */
982 o(0x08c483); /* add $8, %esp */
983 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
984 (VT_INT | VT_UNSIGNED)) {
985 /* unsigned int to float/double/long double */
986 o(0x6a); /* push $0 */
987 g(0x00);
988 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
989 o(0x242cdf); /* fildll (%esp) */
990 o(0x08c483); /* add $8, %esp */
991 } else {
992 /* int to float/double/long double */
993 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
994 o(0x2404db); /* fildl (%esp) */
995 o(0x04c483); /* add $4, %esp */
997 vtop->r = TREG_ST0;
1000 /* convert fp to int 't' type */
1001 /* XXX: handle long long case */
1002 ST_FUNC void gen_cvt_ftoi(int t)
1004 gv(RC_FLOAT);
1005 save_reg(TREG_EAX);
1006 save_reg(TREG_EDX);
1007 gen_static_call(TOK___tcc_cvt_ftol);
1008 vtop->r = TREG_EAX; /* mark reg as used */
1009 if (t == VT_LLONG)
1010 vtop->r2 = TREG_EDX;
1013 /* convert from one floating point type to another */
1014 ST_FUNC void gen_cvt_ftof(int t)
1016 /* all we have to do on i386 is to put the float in a register */
1017 gv(RC_FLOAT);
1020 /* computed goto support */
1021 ST_FUNC void ggoto(void)
1023 gcall_or_jmp(1);
1024 vtop--;
1027 /* bound check support functions */
1028 #ifdef CONFIG_TCC_BCHECK
1030 /* generate a bounded pointer addition */
1031 ST_FUNC void gen_bounded_ptr_add(void)
1033 /* prepare fast i386 function call (args in eax and edx) */
1034 gv2(RC_EAX, RC_EDX);
1035 /* save all temporary registers */
1036 vtop -= 2;
1037 save_regs(0);
1038 /* do a fast function call */
1039 gen_static_call(TOK___bound_ptr_add);
1040 /* returned pointer is in eax */
1041 vtop++;
1042 vtop->r = TREG_EAX | VT_BOUNDED;
1043 /* address of bounding function call point */
1044 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1047 /* patch pointer addition in vtop so that pointer dereferencing is
1048 also tested */
1049 ST_FUNC void gen_bounded_ptr_deref(void)
1051 int func;
1052 int size, align;
1053 Elf32_Rel *rel;
1054 Sym *sym;
1056 size = 0;
1057 /* XXX: put that code in generic part of tcc */
1058 if (!is_float(vtop->type.t)) {
1059 if (vtop->r & VT_LVAL_BYTE)
1060 size = 1;
1061 else if (vtop->r & VT_LVAL_SHORT)
1062 size = 2;
1064 if (!size)
1065 size = type_size(&vtop->type, &align);
1066 switch(size) {
1067 case 1: func = TOK___bound_ptr_indir1; break;
1068 case 2: func = TOK___bound_ptr_indir2; break;
1069 case 4: func = TOK___bound_ptr_indir4; break;
1070 case 8: func = TOK___bound_ptr_indir8; break;
1071 case 12: func = TOK___bound_ptr_indir12; break;
1072 case 16: func = TOK___bound_ptr_indir16; break;
1073 default:
1074 tcc_error("unhandled size when dereferencing bounded pointer");
1075 func = 0;
1076 break;
1079 /* patch relocation */
1080 /* XXX: find a better solution ? */
1081 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1082 sym = external_global_sym(func, &func_old_type, 0);
1083 if (!sym->c)
1084 put_extern_sym(sym, NULL, 0, 0);
1085 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1087 #endif
1089 /* Save the stack pointer onto the stack */
1090 ST_FUNC void gen_vla_sp_save(int addr) {
1091 /* mov %esp,addr(%ebp)*/
1092 o(0x89);
1093 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1096 /* Restore the SP from a location on the stack */
1097 ST_FUNC void gen_vla_sp_restore(int addr) {
1098 o(0x8b);
1099 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1102 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1103 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1104 #ifdef TCC_TARGET_PE
1105 /* alloca does more than just adjust %rsp on Windows */
1106 vpush_global_sym(&func_old_type, TOK_alloca);
1107 vswap(); /* Move alloca ref past allocation size */
1108 gfunc_call(1);
1109 vset(type, REG_IRET, 0);
1110 #else
1111 int r;
1112 r = gv(RC_INT); /* allocation size */
1113 /* sub r,%rsp */
1114 o(0x2b);
1115 o(0xe0 | r);
1116 /* We align to 16 bytes rather than align */
1117 /* and ~15, %esp */
1118 o(0xf0e483);
1119 /* mov %esp, r */
1120 o(0x89);
1121 o(0xe0 | r);
1122 vpop();
1123 vset(type, r, 0);
1124 #endif
1127 /* end of X86 code generator */
1128 /*************************************************************/
1129 #endif
1130 /*************************************************************/