Fix test90 for 32 bits targets
[tinycc.git] / i386-gen.c
blob62bc2ad52ab76a92bf21680c4346b63334be2750
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 5
25 #define NB_ASM_REGS 8
26 #define CONFIG_TCC_ASM
28 /* a register can belong to several classes. The classes must be
29 sorted from more general to more precise (see gv2() code which does
30 assumptions on it). */
31 #define RC_INT 0x0001 /* generic integer register */
32 #define RC_FLOAT 0x0002 /* generic float register */
33 #define RC_EAX 0x0004
34 #define RC_ST0 0x0008
35 #define RC_ECX 0x0010
36 #define RC_EDX 0x0020
37 #define RC_EBX 0x0040
39 #define RC_IRET RC_EAX /* function return: integer register */
40 #define RC_IRE2 RC_EDX /* function return: second integer register */
41 #define RC_FRET RC_ST0 /* function return: float register */
43 /* pretty names for the registers */
44 enum {
45 TREG_EAX = 0,
46 TREG_ECX,
47 TREG_EDX,
48 TREG_EBX,
49 TREG_ST0,
50 TREG_ESP = 4
53 /* return registers for function */
54 #define REG_IRET TREG_EAX /* single word int return register */
55 #define REG_IRE2 TREG_EDX /* second word return register (for long long) */
56 #define REG_FRET TREG_ST0 /* float return register */
58 /* defined if function parameters must be evaluated in reverse order */
59 #define INVERT_FUNC_PARAMS
61 /* defined if structures are passed as pointers. Otherwise structures
62 are directly pushed on stack. */
63 /* #define FUNC_STRUCT_PARAM_AS_PTR */
65 /* pointer size, in bytes */
66 #define PTR_SIZE 4
68 /* long double size and alignment, in bytes */
69 #define LDOUBLE_SIZE 12
70 #define LDOUBLE_ALIGN 4
71 /* maximum alignment (for aligned attribute support) */
72 #define MAX_ALIGN 8
74 /* define if return values need to be extended explicitely
75 at caller side (for interfacing with non-TCC compilers) */
76 #define PROMOTE_RET
78 /******************************************************/
79 #else /* ! TARGET_DEFS_ONLY */
80 /******************************************************/
81 #define USING_GLOBALS
82 #include "tcc.h"
84 ST_DATA const char * const target_machine_defs =
85 "__i386__\0"
86 "__i386\0"
89 /* define to 1/0 to [not] have EBX as 4th register */
90 #define USE_EBX 0
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 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
97 /* st0 */ RC_FLOAT | RC_ST0,
100 static unsigned long func_sub_sp_offset;
101 static int func_ret_sub;
102 #ifdef CONFIG_TCC_BCHECK
103 static addr_t func_bound_offset;
104 static unsigned long func_bound_ind;
105 ST_DATA int func_bound_add_epilog;
106 static void gen_bounds_prolog(void);
107 static void gen_bounds_epilog(void);
108 #endif
110 /* XXX: make it faster ? */
111 ST_FUNC void g(int c)
113 int ind1;
114 if (nocode_wanted)
115 return;
116 ind1 = ind + 1;
117 if (ind1 > cur_text_section->data_allocated)
118 section_realloc(cur_text_section, ind1);
119 cur_text_section->data[ind] = c;
120 ind = ind1;
123 ST_FUNC void o(unsigned int c)
125 while (c) {
126 g(c);
127 c = c >> 8;
131 ST_FUNC void gen_le16(int v)
133 g(v);
134 g(v >> 8);
137 ST_FUNC void gen_le32(int c)
139 g(c);
140 g(c >> 8);
141 g(c >> 16);
142 g(c >> 24);
145 /* output a symbol and patch all calls to it */
146 ST_FUNC void gsym_addr(int t, int a)
148 while (t) {
149 unsigned char *ptr = cur_text_section->data + t;
150 uint32_t n = read32le(ptr); /* next value */
151 write32le(ptr, a - t - 4);
152 t = n;
156 /* instruction + 4 bytes data. Return the address of the data */
157 static int oad(int c, int s)
159 int t;
160 if (nocode_wanted)
161 return s;
162 o(c);
163 t = ind;
164 gen_le32(s);
165 return t;
168 ST_FUNC void gen_fill_nops(int bytes)
170 while (bytes--)
171 g(0x90);
174 /* generate jmp to a label */
175 #define gjmp2(instr,lbl) oad(instr,lbl)
177 /* output constant with relocation if 'r & VT_SYM' is true */
178 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
180 if (r & VT_SYM)
181 greloc(cur_text_section, sym, ind, R_386_32);
182 gen_le32(c);
185 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
187 if (r & VT_SYM)
188 greloc(cur_text_section, sym, ind, R_386_PC32);
189 gen_le32(c - 4);
192 /* generate a modrm reference. 'op_reg' contains the additional 3
193 opcode bits */
194 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
196 op_reg = op_reg << 3;
197 if ((r & VT_VALMASK) == VT_CONST) {
198 /* constant memory reference */
199 o(0x05 | op_reg);
200 gen_addr32(r, sym, c);
201 } else if ((r & VT_VALMASK) == VT_LOCAL) {
202 /* currently, we use only ebp as base */
203 if (c == (char)c) {
204 /* short reference */
205 o(0x45 | op_reg);
206 g(c);
207 } else {
208 oad(0x85 | op_reg, c);
210 } else {
211 g(0x00 | op_reg | (r & VT_VALMASK));
215 /* load 'r' from value 'sv' */
216 ST_FUNC void load(int r, SValue *sv)
218 int v, t, ft, fc, fr;
219 SValue v1;
221 #ifdef TCC_TARGET_PE
222 SValue v2;
223 sv = pe_getimport(sv, &v2);
224 #endif
226 fr = sv->r;
227 ft = sv->type.t & ~VT_DEFSIGN;
228 fc = sv->c.i;
230 ft &= ~(VT_VOLATILE | VT_CONSTANT);
232 v = fr & VT_VALMASK;
233 if (fr & VT_LVAL) {
234 if (v == VT_LLOCAL) {
235 v1.type.t = VT_INT;
236 v1.r = VT_LOCAL | VT_LVAL;
237 v1.c.i = fc;
238 v1.sym = NULL;
239 fr = r;
240 if (!(reg_classes[fr] & RC_INT))
241 fr = get_reg(RC_INT);
242 load(fr, &v1);
244 if ((ft & VT_BTYPE) == VT_FLOAT) {
245 o(0xd9); /* flds */
246 r = 0;
247 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
248 o(0xdd); /* fldl */
249 r = 0;
250 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
251 o(0xdb); /* fldt */
252 r = 5;
253 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
254 o(0xbe0f); /* movsbl */
255 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
256 o(0xb60f); /* movzbl */
257 } else if ((ft & VT_TYPE) == VT_SHORT) {
258 o(0xbf0f); /* movswl */
259 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
260 o(0xb70f); /* movzwl */
261 } else {
262 o(0x8b); /* movl */
264 gen_modrm(r, fr, sv->sym, fc);
265 } else {
266 if (v == VT_CONST) {
267 o(0xb8 + r); /* mov $xx, r */
268 gen_addr32(fr, sv->sym, fc);
269 } else if (v == VT_LOCAL) {
270 if (fc) {
271 o(0x8d); /* lea xxx(%ebp), r */
272 gen_modrm(r, VT_LOCAL, sv->sym, fc);
273 } else {
274 o(0x89);
275 o(0xe8 + r); /* mov %ebp, r */
277 } else if (v == VT_CMP) {
278 o(0x0f); /* setxx %br */
279 o(fc);
280 o(0xc0 + r);
281 o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
282 } else if (v == VT_JMP || v == VT_JMPI) {
283 t = v & 1;
284 oad(0xb8 + r, t); /* mov $1, r */
285 o(0x05eb); /* jmp after */
286 gsym(fc);
287 oad(0xb8 + r, t ^ 1); /* mov $0, r */
288 } else if (v != r) {
289 o(0x89);
290 o(0xc0 + r + v * 8); /* mov v, r */
295 /* store register 'r' in lvalue 'v' */
296 ST_FUNC void store(int r, SValue *v)
298 int fr, bt, ft, fc;
300 #ifdef TCC_TARGET_PE
301 SValue v2;
302 v = pe_getimport(v, &v2);
303 #endif
305 ft = v->type.t;
306 fc = v->c.i;
307 fr = v->r & VT_VALMASK;
308 ft &= ~(VT_VOLATILE | VT_CONSTANT);
309 bt = ft & VT_BTYPE;
310 /* XXX: incorrect if float reg to reg */
311 if (bt == VT_FLOAT) {
312 o(0xd9); /* fsts */
313 r = 2;
314 } else if (bt == VT_DOUBLE) {
315 o(0xdd); /* fstpl */
316 r = 2;
317 } else if (bt == VT_LDOUBLE) {
318 o(0xc0d9); /* fld %st(0) */
319 o(0xdb); /* fstpt */
320 r = 7;
321 } else {
322 if (bt == VT_SHORT)
323 o(0x66);
324 if (bt == VT_BYTE || bt == VT_BOOL)
325 o(0x88);
326 else
327 o(0x89);
329 if (fr == VT_CONST ||
330 fr == VT_LOCAL ||
331 (v->r & VT_LVAL)) {
332 gen_modrm(r, v->r, v->sym, fc);
333 } else if (fr != r) {
334 o(0xc0 + fr + r * 8); /* mov r, fr */
338 static void gadd_sp(int val)
340 if (val == (char)val) {
341 o(0xc483);
342 g(val);
343 } else {
344 oad(0xc481, val); /* add $xxx, %esp */
348 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
349 static void gen_static_call(int v)
351 Sym *sym;
353 sym = external_helper_sym(v);
354 oad(0xe8, -4);
355 greloc(cur_text_section, sym, ind-4, R_386_PC32);
357 #endif
359 /* 'is_jmp' is '1' if it is a jump */
360 static void gcall_or_jmp(int is_jmp)
362 int r;
363 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
364 /* constant and relocation case */
365 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
366 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
367 } else {
368 /* otherwise, indirect call */
369 r = gv(RC_INT);
370 o(0xff); /* call/jmp *r */
371 o(0xd0 + r + (is_jmp << 4));
375 static const uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
376 static const uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
378 /* Return the number of registers needed to return the struct, or 0 if
379 returning via struct pointer. */
380 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
382 #if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
383 int size, align;
384 *ret_align = 1; // Never have to re-align return values for x86
385 *regsize = 4;
386 size = type_size(vt, &align);
387 if (size > 8 || (size & (size - 1)))
388 return 0;
389 if (size == 8)
390 ret->t = VT_LLONG;
391 else if (size == 4)
392 ret->t = VT_INT;
393 else if (size == 2)
394 ret->t = VT_SHORT;
395 else
396 ret->t = VT_BYTE;
397 ret->ref = NULL;
398 return 1;
399 #else
400 *ret_align = 1; // Never have to re-align return values for x86
401 return 0;
402 #endif
405 /* Generate function call. The function address is pushed first, then
406 all the parameters in call order. This functions pops all the
407 parameters and the function address. */
408 ST_FUNC void gfunc_call(int nb_args)
410 int size, align, r, args_size, i, func_call;
411 Sym *func_sym;
413 #ifdef CONFIG_TCC_BCHECK
414 if (tcc_state->do_bounds_check)
415 gbound_args(nb_args);
416 #endif
418 args_size = 0;
419 for(i = 0;i < nb_args; i++) {
420 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
421 size = type_size(&vtop->type, &align);
422 /* align to stack align size */
423 size = (size + 3) & ~3;
424 /* allocate the necessary size on stack */
425 #ifdef TCC_TARGET_PE
426 if (size >= 4096) {
427 r = get_reg(RC_EAX);
428 oad(0x68, size); // push size
429 /* cannot call normal 'alloca' with bound checking */
430 gen_static_call(tok_alloc_const("__alloca"));
431 gadd_sp(4);
432 } else
433 #endif
435 oad(0xec81, size); /* sub $xxx, %esp */
436 /* generate structure store */
437 r = get_reg(RC_INT);
438 o(0xe089 + (r << 8)); /* mov %esp, r */
440 vset(&vtop->type, r | VT_LVAL, 0);
441 vswap();
442 vstore();
443 args_size += size;
444 } else if (is_float(vtop->type.t)) {
445 gv(RC_FLOAT); /* only one float register */
446 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
447 size = 4;
448 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
449 size = 8;
450 else
451 size = 12;
452 oad(0xec81, size); /* sub $xxx, %esp */
453 if (size == 12)
454 o(0x7cdb);
455 else
456 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
457 g(0x24);
458 g(0x00);
459 args_size += size;
460 } else {
461 /* simple type (currently always same size) */
462 /* XXX: implicit cast ? */
463 r = gv(RC_INT);
464 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
465 size = 8;
466 o(0x50 + vtop->r2); /* push r */
467 } else {
468 size = 4;
470 o(0x50 + r); /* push r */
471 args_size += size;
473 vtop--;
475 save_regs(0); /* save used temporary registers */
476 func_sym = vtop->type.ref;
477 func_call = func_sym->f.func_call;
478 /* fast call case */
479 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
480 func_call == FUNC_FASTCALLW) {
481 int fastcall_nb_regs;
482 const uint8_t *fastcall_regs_ptr;
483 if (func_call == FUNC_FASTCALLW) {
484 fastcall_regs_ptr = fastcallw_regs;
485 fastcall_nb_regs = 2;
486 } else {
487 fastcall_regs_ptr = fastcall_regs;
488 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
490 for(i = 0;i < fastcall_nb_regs; i++) {
491 if (args_size <= 0)
492 break;
493 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
494 /* XXX: incorrect for struct/floats */
495 args_size -= 4;
498 #if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
499 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
500 args_size -= 4;
501 #endif
503 gcall_or_jmp(0);
505 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
506 gadd_sp(args_size);
507 vtop--;
510 #ifdef TCC_TARGET_PE
511 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
512 #else
513 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
514 #endif
516 /* generate function prolog of type 't' */
517 ST_FUNC void gfunc_prolog(Sym *func_sym)
519 CType *func_type = &func_sym->type;
520 int addr, align, size, func_call, fastcall_nb_regs;
521 int param_index, param_addr;
522 const uint8_t *fastcall_regs_ptr;
523 Sym *sym;
524 CType *type;
526 sym = func_type->ref;
527 func_call = sym->f.func_call;
528 addr = 8;
529 loc = 0;
530 func_vc = 0;
532 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
533 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
534 fastcall_regs_ptr = fastcall_regs;
535 } else if (func_call == FUNC_FASTCALLW) {
536 fastcall_nb_regs = 2;
537 fastcall_regs_ptr = fastcallw_regs;
538 } else {
539 fastcall_nb_regs = 0;
540 fastcall_regs_ptr = NULL;
542 param_index = 0;
544 ind += FUNC_PROLOG_SIZE;
545 func_sub_sp_offset = ind;
546 /* if the function returns a structure, then add an
547 implicit pointer parameter */
548 #if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
549 size = type_size(&func_vt,&align);
550 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
551 && (size > 8 || (size & (size - 1)))) {
552 #else
553 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
554 #endif
555 /* XXX: fastcall case ? */
556 func_vc = addr;
557 addr += 4;
558 param_index++;
560 /* define parameters */
561 while ((sym = sym->next) != NULL) {
562 type = &sym->type;
563 size = type_size(type, &align);
564 size = (size + 3) & ~3;
565 #ifdef FUNC_STRUCT_PARAM_AS_PTR
566 /* structs are passed as pointer */
567 if ((type->t & VT_BTYPE) == VT_STRUCT) {
568 size = 4;
570 #endif
571 if (param_index < fastcall_nb_regs) {
572 /* save FASTCALL register */
573 loc -= 4;
574 o(0x89); /* movl */
575 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
576 param_addr = loc;
577 } else {
578 param_addr = addr;
579 addr += size;
581 sym_push(sym->v & ~SYM_FIELD, type,
582 VT_LOCAL | VT_LVAL, param_addr);
583 param_index++;
585 func_ret_sub = 0;
586 /* pascal type call or fastcall ? */
587 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
588 func_ret_sub = addr - 8;
589 #if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
590 else if (func_vc)
591 func_ret_sub = 4;
592 #endif
594 #ifdef CONFIG_TCC_BCHECK
595 if (tcc_state->do_bounds_check)
596 gen_bounds_prolog();
597 #endif
600 /* generate function epilog */
601 ST_FUNC void gfunc_epilog(void)
603 addr_t v, saved_ind;
605 #ifdef CONFIG_TCC_BCHECK
606 if (tcc_state->do_bounds_check)
607 gen_bounds_epilog();
608 #endif
610 /* align local size to word & save local variables */
611 v = (-loc + 3) & -4;
613 #if USE_EBX
614 o(0x8b);
615 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
616 #endif
618 o(0xc9); /* leave */
619 if (func_ret_sub == 0) {
620 o(0xc3); /* ret */
621 } else {
622 o(0xc2); /* ret n */
623 g(func_ret_sub);
624 g(func_ret_sub >> 8);
626 saved_ind = ind;
627 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
628 #ifdef TCC_TARGET_PE
629 if (v >= 4096) {
630 oad(0xb8, v); /* mov stacksize, %eax */
631 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
632 } else
633 #endif
635 o(0xe58955); /* push %ebp, mov %esp, %ebp */
636 o(0xec81); /* sub esp, stacksize */
637 gen_le32(v);
638 #ifdef TCC_TARGET_PE
639 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
640 #endif
642 o(0x53 * USE_EBX); /* push ebx */
643 ind = saved_ind;
646 /* generate a jump to a label */
647 ST_FUNC int gjmp(int t)
649 return gjmp2(0xe9, t);
652 /* generate a jump to a fixed address */
653 ST_FUNC void gjmp_addr(int a)
655 int r;
656 r = a - ind - 2;
657 if (r == (char)r) {
658 g(0xeb);
659 g(r);
660 } else {
661 oad(0xe9, a - ind - 5);
665 #if 0
666 /* generate a jump to a fixed address */
667 ST_FUNC void gjmp_cond_addr(int a, int op)
669 int r = a - ind - 2;
670 if (r == (char)r)
671 g(op - 32), g(r);
672 else
673 g(0x0f), gjmp2(op - 16, r - 4);
675 #endif
677 ST_FUNC int gjmp_append(int n, int t)
679 void *p;
680 /* insert vtop->c jump list in t */
681 if (n) {
682 uint32_t n1 = n, n2;
683 while ((n2 = read32le(p = cur_text_section->data + n1)))
684 n1 = n2;
685 write32le(p, t);
686 t = n;
688 return t;
691 ST_FUNC int gjmp_cond(int op, int t)
693 g(0x0f);
694 t = gjmp2(op - 16, t);
695 return t;
698 ST_FUNC void gen_opi(int op)
700 int r, fr, opc, c;
702 switch(op) {
703 case '+':
704 case TOK_ADDC1: /* add with carry generation */
705 opc = 0;
706 gen_op8:
707 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
708 /* constant case */
709 vswap();
710 r = gv(RC_INT);
711 vswap();
712 c = vtop->c.i;
713 if (c == (char)c) {
714 /* generate inc and dec for smaller code */
715 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
716 opc = (c == 1) ^ (op == '+');
717 o (0x40 | (opc << 3) | r); // inc,dec
718 } else {
719 o(0x83);
720 o(0xc0 | (opc << 3) | r);
721 g(c);
723 } else {
724 o(0x81);
725 oad(0xc0 | (opc << 3) | r, c);
727 } else {
728 gv2(RC_INT, RC_INT);
729 r = vtop[-1].r;
730 fr = vtop[0].r;
731 o((opc << 3) | 0x01);
732 o(0xc0 + r + fr * 8);
734 vtop--;
735 if (op >= TOK_ULT && op <= TOK_GT)
736 vset_VT_CMP(op);
737 break;
738 case '-':
739 case TOK_SUBC1: /* sub with carry generation */
740 opc = 5;
741 goto gen_op8;
742 case TOK_ADDC2: /* add with carry use */
743 opc = 2;
744 goto gen_op8;
745 case TOK_SUBC2: /* sub with carry use */
746 opc = 3;
747 goto gen_op8;
748 case '&':
749 opc = 4;
750 goto gen_op8;
751 case '^':
752 opc = 6;
753 goto gen_op8;
754 case '|':
755 opc = 1;
756 goto gen_op8;
757 case '*':
758 gv2(RC_INT, RC_INT);
759 r = vtop[-1].r;
760 fr = vtop[0].r;
761 vtop--;
762 o(0xaf0f); /* imul fr, r */
763 o(0xc0 + fr + r * 8);
764 break;
765 case TOK_SHL:
766 opc = 4;
767 goto gen_shift;
768 case TOK_SHR:
769 opc = 5;
770 goto gen_shift;
771 case TOK_SAR:
772 opc = 7;
773 gen_shift:
774 opc = 0xc0 | (opc << 3);
775 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
776 /* constant case */
777 vswap();
778 r = gv(RC_INT);
779 vswap();
780 c = vtop->c.i & 0x1f;
781 o(0xc1); /* shl/shr/sar $xxx, r */
782 o(opc | r);
783 g(c);
784 } else {
785 /* we generate the shift in ecx */
786 gv2(RC_INT, RC_ECX);
787 r = vtop[-1].r;
788 o(0xd3); /* shl/shr/sar %cl, r */
789 o(opc | r);
791 vtop--;
792 break;
793 case '/':
794 case TOK_UDIV:
795 case TOK_PDIV:
796 case '%':
797 case TOK_UMOD:
798 case TOK_UMULL:
799 /* first operand must be in eax */
800 /* XXX: need better constraint for second operand */
801 gv2(RC_EAX, RC_ECX);
802 r = vtop[-1].r;
803 fr = vtop[0].r;
804 vtop--;
805 save_reg(TREG_EDX);
806 /* save EAX too if used otherwise */
807 save_reg_upstack(TREG_EAX, 1);
808 if (op == TOK_UMULL) {
809 o(0xf7); /* mul fr */
810 o(0xe0 + fr);
811 vtop->r2 = TREG_EDX;
812 r = TREG_EAX;
813 } else {
814 if (op == TOK_UDIV || op == TOK_UMOD) {
815 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
816 o(0xf0 + fr);
817 } else {
818 o(0xf799); /* cltd, idiv fr, %eax */
819 o(0xf8 + fr);
821 if (op == '%' || op == TOK_UMOD)
822 r = TREG_EDX;
823 else
824 r = TREG_EAX;
826 vtop->r = r;
827 break;
828 default:
829 opc = 7;
830 goto gen_op8;
834 /* generate a floating point operation 'v = t1 op t2' instruction. The
835 two operands are guaranteed to have the same floating point type */
836 /* XXX: need to use ST1 too */
837 ST_FUNC void gen_opf(int op)
839 int a, ft, fc, swapped, r;
841 if (op == TOK_NEG) { /* unary minus */
842 gv(RC_FLOAT);
843 o(0xe0d9); /* fchs */
844 return;
847 /* convert constants to memory references */
848 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
849 vswap();
850 gv(RC_FLOAT);
851 vswap();
853 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
854 gv(RC_FLOAT);
856 /* must put at least one value in the floating point register */
857 if ((vtop[-1].r & VT_LVAL) &&
858 (vtop[0].r & VT_LVAL)) {
859 vswap();
860 gv(RC_FLOAT);
861 vswap();
863 swapped = 0;
864 /* swap the stack if needed so that t1 is the register and t2 is
865 the memory reference */
866 if (vtop[-1].r & VT_LVAL) {
867 vswap();
868 swapped = 1;
870 if (op >= TOK_ULT && op <= TOK_GT) {
871 /* load on stack second operand */
872 load(TREG_ST0, vtop);
873 save_reg(TREG_EAX); /* eax is used by FP comparison code */
874 if (op == TOK_GE || op == TOK_GT)
875 swapped = !swapped;
876 else if (op == TOK_EQ || op == TOK_NE)
877 swapped = 0;
878 if (swapped)
879 o(0xc9d9); /* fxch %st(1) */
880 if (op == TOK_EQ || op == TOK_NE)
881 o(0xe9da); /* fucompp */
882 else
883 o(0xd9de); /* fcompp */
884 o(0xe0df); /* fnstsw %ax */
885 if (op == TOK_EQ) {
886 o(0x45e480); /* and $0x45, %ah */
887 o(0x40fC80); /* cmp $0x40, %ah */
888 } else if (op == TOK_NE) {
889 o(0x45e480); /* and $0x45, %ah */
890 o(0x40f480); /* xor $0x40, %ah */
891 op = TOK_NE;
892 } else if (op == TOK_GE || op == TOK_LE) {
893 o(0x05c4f6); /* test $0x05, %ah */
894 op = TOK_EQ;
895 } else {
896 o(0x45c4f6); /* test $0x45, %ah */
897 op = TOK_EQ;
899 vtop--;
900 vset_VT_CMP(op);
901 } else {
902 /* no memory reference possible for long double operations */
903 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
904 load(TREG_ST0, vtop);
905 swapped = !swapped;
908 switch(op) {
909 default:
910 case '+':
911 a = 0;
912 break;
913 case '-':
914 a = 4;
915 if (swapped)
916 a++;
917 break;
918 case '*':
919 a = 1;
920 break;
921 case '/':
922 a = 6;
923 if (swapped)
924 a++;
925 break;
927 ft = vtop->type.t;
928 fc = vtop->c.i;
929 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
930 o(0xde); /* fxxxp %st, %st(1) */
931 o(0xc1 + (a << 3));
932 } else {
933 /* if saved lvalue, then we must reload it */
934 r = vtop->r;
935 if ((r & VT_VALMASK) == VT_LLOCAL) {
936 SValue v1;
937 r = get_reg(RC_INT);
938 v1.type.t = VT_INT;
939 v1.r = VT_LOCAL | VT_LVAL;
940 v1.c.i = fc;
941 v1.sym = NULL;
942 load(r, &v1);
943 fc = 0;
946 if ((ft & VT_BTYPE) == VT_DOUBLE)
947 o(0xdc);
948 else
949 o(0xd8);
950 gen_modrm(a, r, vtop->sym, fc);
952 vtop--;
956 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
957 and 'long long' cases. */
958 ST_FUNC void gen_cvt_itof(int t)
960 save_reg(TREG_ST0);
961 gv(RC_INT);
962 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
963 /* signed long long to float/double/long double (unsigned case
964 is handled generically) */
965 o(0x50 + vtop->r2); /* push r2 */
966 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
967 o(0x242cdf); /* fildll (%esp) */
968 o(0x08c483); /* add $8, %esp */
969 vtop->r2 = VT_CONST;
970 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
971 (VT_INT | VT_UNSIGNED)) {
972 /* unsigned int to float/double/long double */
973 o(0x6a); /* push $0 */
974 g(0x00);
975 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
976 o(0x242cdf); /* fildll (%esp) */
977 o(0x08c483); /* add $8, %esp */
978 } else {
979 /* int to float/double/long double */
980 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
981 o(0x2404db); /* fildl (%esp) */
982 o(0x04c483); /* add $4, %esp */
984 vtop->r2 = VT_CONST;
985 vtop->r = TREG_ST0;
988 /* convert fp to int 't' type */
989 ST_FUNC void gen_cvt_ftoi(int t)
991 int bt = vtop->type.t & VT_BTYPE;
992 if (bt == VT_FLOAT)
993 vpush_helper_func(TOK___fixsfdi);
994 else if (bt == VT_LDOUBLE)
995 vpush_helper_func(TOK___fixxfdi);
996 else
997 vpush_helper_func(TOK___fixdfdi);
998 vswap();
999 gfunc_call(1);
1000 vpushi(0);
1001 vtop->r = REG_IRET;
1002 if ((t & VT_BTYPE) == VT_LLONG)
1003 vtop->r2 = REG_IRE2;
1006 /* convert from one floating point type to another */
1007 ST_FUNC void gen_cvt_ftof(int t)
1009 /* all we have to do on i386 is to put the float in a register */
1010 gv(RC_FLOAT);
1013 /* char/short to int conversion */
1014 ST_FUNC void gen_cvt_csti(int t)
1016 int r, sz, xl;
1017 r = gv(RC_INT);
1018 sz = !(t & VT_UNSIGNED);
1019 xl = (t & VT_BTYPE) == VT_SHORT;
1020 o(0xc0b60f /* mov[sz] %a[xl], %eax */
1021 | (sz << 3 | xl) << 8
1022 | (r << 3 | r) << 16
1026 /* increment tcov counter */
1027 ST_FUNC void gen_increment_tcov (SValue *sv)
1029 o(0x0583); /* addl $1, xxx */
1030 greloc(cur_text_section, sv->sym, ind, R_386_32);
1031 gen_le32(0);
1032 o(1);
1033 o(0x1583); /* addcl $0, xxx */
1034 greloc(cur_text_section, sv->sym, ind, R_386_32);
1035 gen_le32(4);
1036 g(0);
1039 /* computed goto support */
1040 ST_FUNC void ggoto(void)
1042 gcall_or_jmp(1);
1043 vtop--;
1046 /* bound check support functions */
1047 #ifdef CONFIG_TCC_BCHECK
1049 static void gen_bounds_prolog(void)
1051 /* leave some room for bound checking code */
1052 func_bound_offset = lbounds_section->data_offset;
1053 func_bound_ind = ind;
1054 func_bound_add_epilog = 0;
1055 oad(0xb8, 0); /* lbound section pointer */
1056 oad(0xb8, 0); /* call to function */
1059 static void gen_bounds_epilog(void)
1061 addr_t saved_ind;
1062 addr_t *bounds_ptr;
1063 Sym *sym_data;
1064 int offset_modified = func_bound_offset != lbounds_section->data_offset;
1066 if (!offset_modified && !func_bound_add_epilog)
1067 return;
1069 /* add end of table info */
1070 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
1071 *bounds_ptr = 0;
1073 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
1074 func_bound_offset, PTR_SIZE);
1076 /* generate bound local allocation */
1077 if (offset_modified) {
1078 saved_ind = ind;
1079 ind = func_bound_ind;
1080 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1081 ind = ind + 5;
1082 gen_static_call(TOK___bound_local_new);
1083 ind = saved_ind;
1086 /* generate bound check local freeing */
1087 o(0x5250); /* save returned value, if any */
1088 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1089 oad(0xb8, 0); /* mov %eax, xxx */
1090 gen_static_call(TOK___bound_local_delete);
1091 o(0x585a); /* restore returned value, if any */
1093 #endif
1095 /* Save the stack pointer onto the stack */
1096 ST_FUNC void gen_vla_sp_save(int addr) {
1097 /* mov %esp,addr(%ebp)*/
1098 o(0x89);
1099 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1102 /* Restore the SP from a location on the stack */
1103 ST_FUNC void gen_vla_sp_restore(int addr) {
1104 o(0x8b);
1105 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1108 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1109 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1110 int use_call = 0;
1112 #if defined(CONFIG_TCC_BCHECK)
1113 use_call = tcc_state->do_bounds_check;
1114 #endif
1115 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
1116 use_call = 1;
1117 #endif
1118 if (use_call)
1120 vpush_helper_func(TOK_alloca);
1121 vswap(); /* Move alloca ref past allocation size */
1122 gfunc_call(1);
1124 else {
1125 int r;
1126 r = gv(RC_INT); /* allocation size */
1127 /* sub r,%rsp */
1128 o(0x2b);
1129 o(0xe0 | r);
1130 /* We align to 16 bytes rather than align */
1131 /* and ~15, %esp */
1132 o(0xf0e483);
1133 vpop();
1137 /* end of X86 code generator */
1138 /*************************************************************/
1139 #endif
1140 /*************************************************************/