Windows test Makefile target also test pp tests.
[tinycc.git] / i386-gen.c
blob829bcda1c9b30156f4126e24d0cf6a5a16919eca
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_LRET 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_LRET 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 /******************************************************/
75 #else /* ! TARGET_DEFS_ONLY */
76 /******************************************************/
77 #include "tcc.h"
79 /* define to 1/0 to [not] have EBX as 4th register */
80 #define USE_EBX 0
82 ST_DATA const int reg_classes[NB_REGS] = {
83 /* eax */ RC_INT | RC_EAX,
84 /* ecx */ RC_INT | RC_ECX,
85 /* edx */ RC_INT | RC_EDX,
86 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
87 /* st0 */ RC_FLOAT | RC_ST0,
90 static unsigned long func_sub_sp_offset;
91 static int func_ret_sub;
92 #ifdef CONFIG_TCC_BCHECK
93 static addr_t func_bound_offset;
94 static unsigned long func_bound_ind;
95 #endif
97 /* XXX: make it faster ? */
98 ST_FUNC void g(int c)
100 int ind1;
101 if (nocode_wanted)
102 return;
103 ind1 = ind + 1;
104 if (ind1 > cur_text_section->data_allocated)
105 section_realloc(cur_text_section, ind1);
106 cur_text_section->data[ind] = c;
107 ind = ind1;
110 ST_FUNC void o(unsigned int c)
112 while (c) {
113 g(c);
114 c = c >> 8;
118 ST_FUNC void gen_le16(int v)
120 g(v);
121 g(v >> 8);
124 ST_FUNC void gen_le32(int c)
126 g(c);
127 g(c >> 8);
128 g(c >> 16);
129 g(c >> 24);
132 /* output a symbol and patch all calls to it */
133 ST_FUNC void gsym_addr(int t, int a)
135 while (t) {
136 unsigned char *ptr = cur_text_section->data + t;
137 uint32_t n = read32le(ptr); /* next value */
138 write32le(ptr, a - t - 4);
139 t = n;
143 ST_FUNC void gsym(int t)
145 gsym_addr(t, ind);
148 /* instruction + 4 bytes data. Return the address of the data */
149 static int oad(int c, int s)
151 int t;
152 if (nocode_wanted)
153 return s;
154 o(c);
155 t = ind;
156 gen_le32(s);
157 return t;
160 /* generate jmp to a label */
161 #define gjmp2(instr,lbl) oad(instr,lbl)
163 /* output constant with relocation if 'r & VT_SYM' is true */
164 ST_FUNC void gen_addr32(int r, Sym *sym, long c)
166 if (r & VT_SYM)
167 greloc(cur_text_section, sym, ind, R_386_32);
168 gen_le32(c);
171 ST_FUNC void gen_addrpc32(int r, Sym *sym, long c)
173 if (r & VT_SYM)
174 greloc(cur_text_section, sym, ind, R_386_PC32);
175 gen_le32(c - 4);
178 /* generate a modrm reference. 'op_reg' contains the addtional 3
179 opcode bits */
180 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
182 op_reg = op_reg << 3;
183 if ((r & VT_VALMASK) == VT_CONST) {
184 /* constant memory reference */
185 o(0x05 | op_reg);
186 gen_addr32(r, sym, c);
187 } else if ((r & VT_VALMASK) == VT_LOCAL) {
188 /* currently, we use only ebp as base */
189 if (c == (char)c) {
190 /* short reference */
191 o(0x45 | op_reg);
192 g(c);
193 } else {
194 oad(0x85 | op_reg, c);
196 } else {
197 g(0x00 | op_reg | (r & VT_VALMASK));
201 /* load 'r' from value 'sv' */
202 ST_FUNC void load(int r, SValue *sv)
204 int v, t, ft, fc, fr;
205 SValue v1;
207 #ifdef TCC_TARGET_PE
208 SValue v2;
209 sv = pe_getimport(sv, &v2);
210 #endif
212 fr = sv->r;
213 ft = sv->type.t;
214 fc = sv->c.i;
216 ft &= ~(VT_VOLATILE | VT_CONSTANT);
218 v = fr & VT_VALMASK;
219 if (fr & VT_LVAL) {
220 if (v == VT_LLOCAL) {
221 v1.type.t = VT_INT;
222 v1.r = VT_LOCAL | VT_LVAL;
223 v1.c.i = fc;
224 fr = r;
225 if (!(reg_classes[fr] & RC_INT))
226 fr = get_reg(RC_INT);
227 load(fr, &v1);
229 if ((ft & VT_BTYPE) == VT_FLOAT) {
230 o(0xd9); /* flds */
231 r = 0;
232 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
233 o(0xdd); /* fldl */
234 r = 0;
235 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
236 o(0xdb); /* fldt */
237 r = 5;
238 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
239 o(0xbe0f); /* movsbl */
240 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
241 o(0xb60f); /* movzbl */
242 } else if ((ft & VT_TYPE) == VT_SHORT) {
243 o(0xbf0f); /* movswl */
244 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
245 o(0xb70f); /* movzwl */
246 } else {
247 o(0x8b); /* movl */
249 gen_modrm(r, fr, sv->sym, fc);
250 } else {
251 if (v == VT_CONST) {
252 o(0xb8 + r); /* mov $xx, r */
253 gen_addr32(fr, sv->sym, fc);
254 } else if (v == VT_LOCAL) {
255 if (fc) {
256 o(0x8d); /* lea xxx(%ebp), r */
257 gen_modrm(r, VT_LOCAL, sv->sym, fc);
258 } else {
259 o(0x89);
260 o(0xe8 + r); /* mov %ebp, r */
262 } else if (v == VT_CMP) {
263 oad(0xb8 + r, 0); /* mov $0, r */
264 o(0x0f); /* setxx %br */
265 o(fc);
266 o(0xc0 + r);
267 } else if (v == VT_JMP || v == VT_JMPI) {
268 t = v & 1;
269 oad(0xb8 + r, t); /* mov $1, r */
270 o(0x05eb); /* jmp after */
271 gsym(fc);
272 oad(0xb8 + r, t ^ 1); /* mov $0, r */
273 } else if (v != r) {
274 o(0x89);
275 o(0xc0 + r + v * 8); /* mov v, r */
280 /* store register 'r' in lvalue 'v' */
281 ST_FUNC void store(int r, SValue *v)
283 int fr, bt, ft, fc;
285 #ifdef TCC_TARGET_PE
286 SValue v2;
287 v = pe_getimport(v, &v2);
288 #endif
290 ft = v->type.t;
291 fc = v->c.i;
292 fr = v->r & VT_VALMASK;
293 ft &= ~(VT_VOLATILE | VT_CONSTANT);
294 bt = ft & VT_BTYPE;
295 /* XXX: incorrect if float reg to reg */
296 if (bt == VT_FLOAT) {
297 o(0xd9); /* fsts */
298 r = 2;
299 } else if (bt == VT_DOUBLE) {
300 o(0xdd); /* fstpl */
301 r = 2;
302 } else if (bt == VT_LDOUBLE) {
303 o(0xc0d9); /* fld %st(0) */
304 o(0xdb); /* fstpt */
305 r = 7;
306 } else {
307 if (bt == VT_SHORT)
308 o(0x66);
309 if (bt == VT_BYTE || bt == VT_BOOL)
310 o(0x88);
311 else
312 o(0x89);
314 if (fr == VT_CONST ||
315 fr == VT_LOCAL ||
316 (v->r & VT_LVAL)) {
317 gen_modrm(r, v->r, v->sym, fc);
318 } else if (fr != r) {
319 o(0xc0 + fr + r * 8); /* mov r, fr */
323 static void gadd_sp(int val)
325 if (val == (char)val) {
326 o(0xc483);
327 g(val);
328 } else {
329 oad(0xc481, val); /* add $xxx, %esp */
333 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
334 static void gen_static_call(int v)
336 Sym *sym;
338 sym = external_global_sym(v, &func_old_type, 0);
339 oad(0xe8, -4);
340 greloc(cur_text_section, sym, ind-4, R_386_PC32);
342 #endif
344 /* 'is_jmp' is '1' if it is a jump */
345 static void gcall_or_jmp(int is_jmp)
347 int r;
348 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
349 int rt;
350 /* constant case */
351 if (vtop->r & VT_SYM) {
352 /* relocation case */
353 greloc(cur_text_section, vtop->sym,
354 ind + 1, R_386_PC32);
355 } else {
356 /* put an empty PC32 relocation */
357 put_elf_reloc(symtab_section, cur_text_section,
358 ind + 1, R_386_PC32, 0);
360 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
361 /* extend the return value to the whole register if necessary
362 visual studio and gcc do not always set the whole eax register
363 when assigning the return value of a function */
364 rt = vtop->type.ref->type.t;
365 switch (rt & VT_BTYPE) {
366 case VT_BYTE:
367 if (rt & VT_UNSIGNED) {
368 o(0xc0b60f); /* movzx %al, %eax */
370 else {
371 o(0xc0be0f); /* movsx %al, %eax */
373 break;
374 case VT_SHORT:
375 if (rt & VT_UNSIGNED) {
376 o(0xc0b70f); /* movzx %ax, %eax */
378 else {
379 o(0xc0bf0f); /* movsx %ax, %eax */
381 break;
382 default:
383 break;
385 } else {
386 /* otherwise, indirect call */
387 r = gv(RC_INT);
388 o(0xff); /* call/jmp *r */
389 o(0xd0 + r + (is_jmp << 4));
393 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
394 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
396 /* Return the number of registers needed to return the struct, or 0 if
397 returning via struct pointer. */
398 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
400 #ifdef TCC_TARGET_PE
401 int size, align;
402 *ret_align = 1; // Never have to re-align return values for x86
403 *regsize = 4;
404 size = type_size(vt, &align);
405 if (size > 8 || (size & (size - 1)))
406 return 0;
407 if (size == 8)
408 ret->t = VT_LLONG;
409 else if (size == 4)
410 ret->t = VT_INT;
411 else if (size == 2)
412 ret->t = VT_SHORT;
413 else
414 ret->t = VT_BYTE;
415 ret->ref = NULL;
416 return 1;
417 #else
418 *ret_align = 1; // Never have to re-align return values for x86
419 return 0;
420 #endif
423 /* Generate function call. The function address is pushed first, then
424 all the parameters in call order. This functions pops all the
425 parameters and the function address. */
426 ST_FUNC void gfunc_call(int nb_args)
428 int size, align, r, args_size, i, func_call;
429 Sym *func_sym;
431 args_size = 0;
432 for(i = 0;i < nb_args; i++) {
433 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
434 size = type_size(&vtop->type, &align);
435 /* align to stack align size */
436 size = (size + 3) & ~3;
437 /* allocate the necessary size on stack */
438 oad(0xec81, size); /* sub $xxx, %esp */
439 /* generate structure store */
440 r = get_reg(RC_INT);
441 o(0x89); /* mov %esp, r */
442 o(0xe0 + r);
443 vset(&vtop->type, r | VT_LVAL, 0);
444 vswap();
445 vstore();
446 args_size += size;
447 } else if (is_float(vtop->type.t)) {
448 gv(RC_FLOAT); /* only one float register */
449 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
450 size = 4;
451 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
452 size = 8;
453 else
454 size = 12;
455 oad(0xec81, size); /* sub $xxx, %esp */
456 if (size == 12)
457 o(0x7cdb);
458 else
459 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
460 g(0x24);
461 g(0x00);
462 args_size += size;
463 } else {
464 /* simple type (currently always same size) */
465 /* XXX: implicit cast ? */
466 r = gv(RC_INT);
467 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
468 size = 8;
469 o(0x50 + vtop->r2); /* push r */
470 } else {
471 size = 4;
473 o(0x50 + r); /* push r */
474 args_size += size;
476 vtop--;
478 save_regs(0); /* save used temporary registers */
479 func_sym = vtop->type.ref;
480 func_call = func_sym->a.func_call;
481 /* fast call case */
482 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
483 func_call == FUNC_FASTCALLW) {
484 int fastcall_nb_regs;
485 uint8_t *fastcall_regs_ptr;
486 if (func_call == FUNC_FASTCALLW) {
487 fastcall_regs_ptr = fastcallw_regs;
488 fastcall_nb_regs = 2;
489 } else {
490 fastcall_regs_ptr = fastcall_regs;
491 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
493 for(i = 0;i < fastcall_nb_regs; i++) {
494 if (args_size <= 0)
495 break;
496 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
497 /* XXX: incorrect for struct/floats */
498 args_size -= 4;
501 #ifndef TCC_TARGET_PE
502 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
503 args_size -= 4;
504 #endif
505 gcall_or_jmp(0);
507 if (args_size && func_call != FUNC_STDCALL)
508 gadd_sp(args_size);
509 vtop--;
512 #ifdef TCC_TARGET_PE
513 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
514 #else
515 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
516 #endif
518 /* generate function prolog of type 't' */
519 ST_FUNC void gfunc_prolog(CType *func_type)
521 int addr, align, size, func_call, fastcall_nb_regs;
522 int param_index, param_addr;
523 uint8_t *fastcall_regs_ptr;
524 Sym *sym;
525 CType *type;
527 sym = func_type->ref;
528 func_call = sym->a.func_call;
529 addr = 8;
530 loc = 0;
531 func_vc = 0;
533 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
534 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
535 fastcall_regs_ptr = fastcall_regs;
536 } else if (func_call == FUNC_FASTCALLW) {
537 fastcall_nb_regs = 2;
538 fastcall_regs_ptr = fastcallw_regs;
539 } else {
540 fastcall_nb_regs = 0;
541 fastcall_regs_ptr = NULL;
543 param_index = 0;
545 ind += FUNC_PROLOG_SIZE;
546 func_sub_sp_offset = ind;
547 /* if the function returns a structure, then add an
548 implicit pointer parameter */
549 func_vt = sym->type;
550 func_var = (sym->c == FUNC_ELLIPSIS);
551 #ifdef TCC_TARGET_PE
552 size = type_size(&func_vt,&align);
553 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
554 && (size > 8 || (size & (size - 1)))) {
555 #else
556 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
557 #endif
558 /* XXX: fastcall case ? */
559 func_vc = addr;
560 addr += 4;
561 param_index++;
563 /* define parameters */
564 while ((sym = sym->next) != NULL) {
565 type = &sym->type;
566 size = type_size(type, &align);
567 size = (size + 3) & ~3;
568 #ifdef FUNC_STRUCT_PARAM_AS_PTR
569 /* structs are passed as pointer */
570 if ((type->t & VT_BTYPE) == VT_STRUCT) {
571 size = 4;
573 #endif
574 if (param_index < fastcall_nb_regs) {
575 /* save FASTCALL register */
576 loc -= 4;
577 o(0x89); /* movl */
578 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
579 param_addr = loc;
580 } else {
581 param_addr = addr;
582 addr += size;
584 sym_push(sym->v & ~SYM_FIELD, type,
585 VT_LOCAL | lvalue_type(type->t), param_addr);
586 param_index++;
588 func_ret_sub = 0;
589 /* pascal type call ? */
590 if (func_call == FUNC_STDCALL)
591 func_ret_sub = addr - 8;
592 #ifndef TCC_TARGET_PE
593 else if (func_vc)
594 func_ret_sub = 4;
595 #endif
597 #ifdef CONFIG_TCC_BCHECK
598 /* leave some room for bound checking code */
599 if (tcc_state->do_bounds_check) {
600 func_bound_offset = lbounds_section->data_offset;
601 func_bound_ind = ind;
602 oad(0xb8, 0); /* lbound section pointer */
603 oad(0xb8, 0); /* call to function */
605 #endif
608 /* generate function epilog */
609 ST_FUNC void gfunc_epilog(void)
611 addr_t v, saved_ind;
613 #ifdef CONFIG_TCC_BCHECK
614 if (tcc_state->do_bounds_check
615 && func_bound_offset != lbounds_section->data_offset) {
616 addr_t saved_ind;
617 addr_t *bounds_ptr;
618 Sym *sym_data;
620 /* add end of table info */
621 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
622 *bounds_ptr = 0;
624 /* generate bound local allocation */
625 saved_ind = ind;
626 ind = func_bound_ind;
627 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
628 func_bound_offset, lbounds_section->data_offset);
629 greloc(cur_text_section, sym_data,
630 ind + 1, R_386_32);
631 oad(0xb8, 0); /* mov %eax, xxx */
632 gen_static_call(TOK___bound_local_new);
633 ind = saved_ind;
635 /* generate bound check local freeing */
636 o(0x5250); /* save returned value, if any */
637 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
638 oad(0xb8, 0); /* mov %eax, xxx */
639 gen_static_call(TOK___bound_local_delete);
640 o(0x585a); /* restore returned value, if any */
642 #endif
644 /* align local size to word & save local variables */
645 v = (-loc + 3) & -4;
647 #if USE_EBX
648 o(0x8b);
649 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
650 #endif
652 o(0xc9); /* leave */
653 if (func_ret_sub == 0) {
654 o(0xc3); /* ret */
655 } else {
656 o(0xc2); /* ret n */
657 g(func_ret_sub);
658 g(func_ret_sub >> 8);
660 saved_ind = ind;
661 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
662 #ifdef TCC_TARGET_PE
663 if (v >= 4096) {
664 oad(0xb8, v); /* mov stacksize, %eax */
665 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
666 } else
667 #endif
669 o(0xe58955); /* push %ebp, mov %esp, %ebp */
670 o(0xec81); /* sub esp, stacksize */
671 gen_le32(v);
672 #ifdef TCC_TARGET_PE
673 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
674 #endif
676 o(0x53 * USE_EBX); /* push ebx */
677 ind = saved_ind;
680 /* generate a jump to a label */
681 ST_FUNC int gjmp(int t)
683 return gjmp2(0xe9, t);
686 /* generate a jump to a fixed address */
687 ST_FUNC void gjmp_addr(int a)
689 int r;
690 r = a - ind - 2;
691 if (r == (char)r) {
692 g(0xeb);
693 g(r);
694 } else {
695 oad(0xe9, a - ind - 5);
699 ST_FUNC void gtst_addr(int inv, int a)
701 int v = vtop->r & VT_VALMASK;
702 if (v == VT_CMP) {
703 inv ^= (vtop--)->c.i;
704 a -= ind + 2;
705 if (a == (char)a) {
706 g(inv - 32);
707 g(a);
708 } else {
709 g(0x0f);
710 oad(inv - 16, a - 4);
712 } else if ((v & ~1) == VT_JMP) {
713 if ((v & 1) != inv) {
714 gjmp_addr(a);
715 gsym(vtop->c.i);
716 } else {
717 gsym(vtop->c.i);
718 o(0x05eb);
719 gjmp_addr(a);
721 vtop--;
725 /* generate a test. set 'inv' to invert test. Stack entry is popped */
726 ST_FUNC int gtst(int inv, int t)
728 int v = vtop->r & VT_VALMASK;
729 if (nocode_wanted) {
731 } else if (v == VT_CMP) {
732 /* fast case : can jump directly since flags are set */
733 g(0x0f);
734 t = gjmp2((vtop->c.i - 16) ^ inv, t);
735 } else if (v == VT_JMP || v == VT_JMPI) {
736 /* && or || optimization */
737 if ((v & 1) == inv) {
738 /* insert vtop->c jump list in t */
739 uint32_t n1, n = vtop->c.i;
740 if (n) {
741 while ((n1 = read32le(cur_text_section->data + n)))
742 n = n1;
743 write32le(cur_text_section->data + n, t);
744 t = vtop->c.i;
746 } else {
747 t = gjmp(t);
748 gsym(vtop->c.i);
751 vtop--;
752 return t;
755 /* generate an integer binary operation */
756 ST_FUNC void gen_opi(int op)
758 int r, fr, opc, c;
760 switch(op) {
761 case '+':
762 case TOK_ADDC1: /* add with carry generation */
763 opc = 0;
764 gen_op8:
765 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
766 /* constant case */
767 vswap();
768 r = gv(RC_INT);
769 vswap();
770 c = vtop->c.i;
771 if (c == (char)c) {
772 /* generate inc and dec for smaller code */
773 if (c==1 && opc==0 && op != TOK_ADDC1) {
774 o (0x40 | r); // inc
775 } else if (c==1 && opc==5 && op != TOK_SUBC1) {
776 o (0x48 | r); // dec
777 } else {
778 o(0x83);
779 o(0xc0 | (opc << 3) | r);
780 g(c);
782 } else {
783 o(0x81);
784 oad(0xc0 | (opc << 3) | r, c);
786 } else {
787 gv2(RC_INT, RC_INT);
788 r = vtop[-1].r;
789 fr = vtop[0].r;
790 o((opc << 3) | 0x01);
791 o(0xc0 + r + fr * 8);
793 vtop--;
794 if (op >= TOK_ULT && op <= TOK_GT) {
795 vtop->r = VT_CMP;
796 vtop->c.i = op;
798 break;
799 case '-':
800 case TOK_SUBC1: /* sub with carry generation */
801 opc = 5;
802 goto gen_op8;
803 case TOK_ADDC2: /* add with carry use */
804 opc = 2;
805 goto gen_op8;
806 case TOK_SUBC2: /* sub with carry use */
807 opc = 3;
808 goto gen_op8;
809 case '&':
810 opc = 4;
811 goto gen_op8;
812 case '^':
813 opc = 6;
814 goto gen_op8;
815 case '|':
816 opc = 1;
817 goto gen_op8;
818 case '*':
819 gv2(RC_INT, RC_INT);
820 r = vtop[-1].r;
821 fr = vtop[0].r;
822 vtop--;
823 o(0xaf0f); /* imul fr, r */
824 o(0xc0 + fr + r * 8);
825 break;
826 case TOK_SHL:
827 opc = 4;
828 goto gen_shift;
829 case TOK_SHR:
830 opc = 5;
831 goto gen_shift;
832 case TOK_SAR:
833 opc = 7;
834 gen_shift:
835 opc = 0xc0 | (opc << 3);
836 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
837 /* constant case */
838 vswap();
839 r = gv(RC_INT);
840 vswap();
841 c = vtop->c.i & 0x1f;
842 o(0xc1); /* shl/shr/sar $xxx, r */
843 o(opc | r);
844 g(c);
845 } else {
846 /* we generate the shift in ecx */
847 gv2(RC_INT, RC_ECX);
848 r = vtop[-1].r;
849 o(0xd3); /* shl/shr/sar %cl, r */
850 o(opc | r);
852 vtop--;
853 break;
854 case '/':
855 case TOK_UDIV:
856 case TOK_PDIV:
857 case '%':
858 case TOK_UMOD:
859 case TOK_UMULL:
860 /* first operand must be in eax */
861 /* XXX: need better constraint for second operand */
862 gv2(RC_EAX, RC_ECX);
863 r = vtop[-1].r;
864 fr = vtop[0].r;
865 vtop--;
866 save_reg(TREG_EDX);
867 /* save EAX too if used otherwise */
868 save_reg_upstack(TREG_EAX, 1);
869 if (op == TOK_UMULL) {
870 o(0xf7); /* mul fr */
871 o(0xe0 + fr);
872 vtop->r2 = TREG_EDX;
873 r = TREG_EAX;
874 } else {
875 if (op == TOK_UDIV || op == TOK_UMOD) {
876 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
877 o(0xf0 + fr);
878 } else {
879 o(0xf799); /* cltd, idiv fr, %eax */
880 o(0xf8 + fr);
882 if (op == '%' || op == TOK_UMOD)
883 r = TREG_EDX;
884 else
885 r = TREG_EAX;
887 vtop->r = r;
888 break;
889 default:
890 opc = 7;
891 goto gen_op8;
895 /* generate a floating point operation 'v = t1 op t2' instruction. The
896 two operands are guaranteed to have the same floating point type */
897 /* XXX: need to use ST1 too */
898 ST_FUNC void gen_opf(int op)
900 int a, ft, fc, swapped, r;
902 /* convert constants to memory references */
903 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
904 vswap();
905 gv(RC_FLOAT);
906 vswap();
908 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
909 gv(RC_FLOAT);
911 /* must put at least one value in the floating point register */
912 if ((vtop[-1].r & VT_LVAL) &&
913 (vtop[0].r & VT_LVAL)) {
914 vswap();
915 gv(RC_FLOAT);
916 vswap();
918 swapped = 0;
919 /* swap the stack if needed so that t1 is the register and t2 is
920 the memory reference */
921 if (vtop[-1].r & VT_LVAL) {
922 vswap();
923 swapped = 1;
925 if (op >= TOK_ULT && op <= TOK_GT) {
926 /* load on stack second operand */
927 load(TREG_ST0, vtop);
928 save_reg(TREG_EAX); /* eax is used by FP comparison code */
929 if (op == TOK_GE || op == TOK_GT)
930 swapped = !swapped;
931 else if (op == TOK_EQ || op == TOK_NE)
932 swapped = 0;
933 if (swapped)
934 o(0xc9d9); /* fxch %st(1) */
935 if (op == TOK_EQ || op == TOK_NE)
936 o(0xe9da); /* fucompp */
937 else
938 o(0xd9de); /* fcompp */
939 o(0xe0df); /* fnstsw %ax */
940 if (op == TOK_EQ) {
941 o(0x45e480); /* and $0x45, %ah */
942 o(0x40fC80); /* cmp $0x40, %ah */
943 } else if (op == TOK_NE) {
944 o(0x45e480); /* and $0x45, %ah */
945 o(0x40f480); /* xor $0x40, %ah */
946 op = TOK_NE;
947 } else if (op == TOK_GE || op == TOK_LE) {
948 o(0x05c4f6); /* test $0x05, %ah */
949 op = TOK_EQ;
950 } else {
951 o(0x45c4f6); /* test $0x45, %ah */
952 op = TOK_EQ;
954 vtop--;
955 vtop->r = VT_CMP;
956 vtop->c.i = op;
957 } else {
958 /* no memory reference possible for long double operations */
959 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
960 load(TREG_ST0, vtop);
961 swapped = !swapped;
964 switch(op) {
965 default:
966 case '+':
967 a = 0;
968 break;
969 case '-':
970 a = 4;
971 if (swapped)
972 a++;
973 break;
974 case '*':
975 a = 1;
976 break;
977 case '/':
978 a = 6;
979 if (swapped)
980 a++;
981 break;
983 ft = vtop->type.t;
984 fc = vtop->c.i;
985 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
986 o(0xde); /* fxxxp %st, %st(1) */
987 o(0xc1 + (a << 3));
988 } else {
989 /* if saved lvalue, then we must reload it */
990 r = vtop->r;
991 if ((r & VT_VALMASK) == VT_LLOCAL) {
992 SValue v1;
993 r = get_reg(RC_INT);
994 v1.type.t = VT_INT;
995 v1.r = VT_LOCAL | VT_LVAL;
996 v1.c.i = fc;
997 load(r, &v1);
998 fc = 0;
1001 if ((ft & VT_BTYPE) == VT_DOUBLE)
1002 o(0xdc);
1003 else
1004 o(0xd8);
1005 gen_modrm(a, r, vtop->sym, fc);
1007 vtop--;
1011 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1012 and 'long long' cases. */
1013 ST_FUNC void gen_cvt_itof(int t)
1015 save_reg(TREG_ST0);
1016 gv(RC_INT);
1017 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1018 /* signed long long to float/double/long double (unsigned case
1019 is handled generically) */
1020 o(0x50 + vtop->r2); /* push r2 */
1021 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1022 o(0x242cdf); /* fildll (%esp) */
1023 o(0x08c483); /* add $8, %esp */
1024 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1025 (VT_INT | VT_UNSIGNED)) {
1026 /* unsigned int to float/double/long double */
1027 o(0x6a); /* push $0 */
1028 g(0x00);
1029 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1030 o(0x242cdf); /* fildll (%esp) */
1031 o(0x08c483); /* add $8, %esp */
1032 } else {
1033 /* int to float/double/long double */
1034 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1035 o(0x2404db); /* fildl (%esp) */
1036 o(0x04c483); /* add $4, %esp */
1038 vtop->r = TREG_ST0;
1041 /* convert fp to int 't' type */
1042 ST_FUNC void gen_cvt_ftoi(int t)
1044 int bt = vtop->type.t & VT_BTYPE;
1045 if (bt == VT_FLOAT)
1046 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1047 else if (bt == VT_LDOUBLE)
1048 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1049 else
1050 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1051 vswap();
1052 gfunc_call(1);
1053 vpushi(0);
1054 vtop->r = REG_IRET;
1055 vtop->r2 = REG_LRET;
1058 /* convert from one floating point type to another */
1059 ST_FUNC void gen_cvt_ftof(int t)
1061 /* all we have to do on i386 is to put the float in a register */
1062 gv(RC_FLOAT);
1065 /* computed goto support */
1066 ST_FUNC void ggoto(void)
1068 gcall_or_jmp(1);
1069 vtop--;
1072 /* bound check support functions */
1073 #ifdef CONFIG_TCC_BCHECK
1075 /* generate a bounded pointer addition */
1076 ST_FUNC void gen_bounded_ptr_add(void)
1078 /* prepare fast i386 function call (args in eax and edx) */
1079 gv2(RC_EAX, RC_EDX);
1080 /* save all temporary registers */
1081 vtop -= 2;
1082 save_regs(0);
1083 /* do a fast function call */
1084 gen_static_call(TOK___bound_ptr_add);
1085 /* returned pointer is in eax */
1086 vtop++;
1087 vtop->r = TREG_EAX | VT_BOUNDED;
1088 /* address of bounding function call point */
1089 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1092 /* patch pointer addition in vtop so that pointer dereferencing is
1093 also tested */
1094 ST_FUNC void gen_bounded_ptr_deref(void)
1096 addr_t func;
1097 int size, align;
1098 Elf32_Rel *rel;
1099 Sym *sym;
1101 size = 0;
1102 /* XXX: put that code in generic part of tcc */
1103 if (!is_float(vtop->type.t)) {
1104 if (vtop->r & VT_LVAL_BYTE)
1105 size = 1;
1106 else if (vtop->r & VT_LVAL_SHORT)
1107 size = 2;
1109 if (!size)
1110 size = type_size(&vtop->type, &align);
1111 switch(size) {
1112 case 1: func = TOK___bound_ptr_indir1; break;
1113 case 2: func = TOK___bound_ptr_indir2; break;
1114 case 4: func = TOK___bound_ptr_indir4; break;
1115 case 8: func = TOK___bound_ptr_indir8; break;
1116 case 12: func = TOK___bound_ptr_indir12; break;
1117 case 16: func = TOK___bound_ptr_indir16; break;
1118 default:
1119 tcc_error("unhandled size when dereferencing bounded pointer");
1120 func = 0;
1121 break;
1124 /* patch relocation */
1125 /* XXX: find a better solution ? */
1126 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1127 sym = external_global_sym(func, &func_old_type, 0);
1128 if (!sym->c)
1129 put_extern_sym(sym, NULL, 0, 0);
1130 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1132 #endif
1134 /* Save the stack pointer onto the stack */
1135 ST_FUNC void gen_vla_sp_save(int addr) {
1136 /* mov %esp,addr(%ebp)*/
1137 o(0x89);
1138 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1141 /* Restore the SP from a location on the stack */
1142 ST_FUNC void gen_vla_sp_restore(int addr) {
1143 o(0x8b);
1144 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1147 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1148 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1149 #ifdef TCC_TARGET_PE
1150 /* alloca does more than just adjust %rsp on Windows */
1151 vpush_global_sym(&func_old_type, TOK_alloca);
1152 vswap(); /* Move alloca ref past allocation size */
1153 gfunc_call(1);
1154 #else
1155 int r;
1156 r = gv(RC_INT); /* allocation size */
1157 /* sub r,%rsp */
1158 o(0x2b);
1159 o(0xe0 | r);
1160 /* We align to 16 bytes rather than align */
1161 /* and ~15, %esp */
1162 o(0xf0e483);
1163 vpop();
1164 #endif
1167 /* end of X86 code generator */
1168 /*************************************************************/
1169 #endif
1170 /*************************************************************/