SECTION_ALIGNMENT -> RUN_SECTION_ALIGNMENT, and tweaks
[tinycc.git] / i386-gen.c
blob946e42d3af45972065fe5216461d9a1e8d29e399
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
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_EBX 0x0040
38 #define RC_IRET RC_EAX /* function return: integer register */
39 #define RC_LRET RC_EDX /* function return: second integer register */
40 #define RC_FRET RC_ST0 /* function return: float register */
42 /* pretty names for the registers */
43 enum {
44 TREG_EAX = 0,
45 TREG_ECX,
46 TREG_EDX,
47 TREG_EBX,
48 TREG_ST0,
49 TREG_ESP = 4
52 /* return registers for function */
53 #define REG_IRET TREG_EAX /* single word int return register */
54 #define REG_LRET TREG_EDX /* second word return register (for long long) */
55 #define REG_FRET TREG_ST0 /* float return register */
57 /* defined if function parameters must be evaluated in reverse order */
58 #define INVERT_FUNC_PARAMS
60 /* defined if structures are passed as pointers. Otherwise structures
61 are directly pushed on stack. */
62 /* #define FUNC_STRUCT_PARAM_AS_PTR */
64 /* pointer size, in bytes */
65 #define PTR_SIZE 4
67 /* long double size and alignment, in bytes */
68 #define LDOUBLE_SIZE 12
69 #define LDOUBLE_ALIGN 4
70 /* maximum alignment (for aligned attribute support) */
71 #define MAX_ALIGN 8
73 /* generate jmp to a label */
74 #define gjmp2(instr,lbl) oad(instr,lbl)
76 /******************************************************/
77 #else /* ! TARGET_DEFS_ONLY */
78 /******************************************************/
79 #include "tcc.h"
81 /* define to 1/0 to [not] have EBX as 4th register */
82 #define USE_EBX 0
84 ST_DATA const int reg_classes[NB_REGS] = {
85 /* eax */ RC_INT | RC_EAX,
86 /* ecx */ RC_INT | RC_ECX,
87 /* edx */ RC_INT | RC_EDX,
88 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
89 /* st0 */ RC_FLOAT | RC_ST0,
92 static unsigned long func_sub_sp_offset;
93 static int func_ret_sub;
94 #ifdef CONFIG_TCC_BCHECK
95 static addr_t func_bound_offset;
96 static unsigned long func_bound_ind;
97 #endif
99 /* XXX: make it faster ? */
100 ST_FUNC void g(int c)
102 int ind1;
103 if (nocode_wanted)
104 return;
105 ind1 = ind + 1;
106 if (ind1 > cur_text_section->data_allocated)
107 section_realloc(cur_text_section, ind1);
108 cur_text_section->data[ind] = c;
109 ind = ind1;
112 ST_FUNC void o(unsigned int c)
114 while (c) {
115 g(c);
116 c = c >> 8;
120 ST_FUNC void gen_le16(int v)
122 g(v);
123 g(v >> 8);
126 ST_FUNC void gen_le32(int c)
128 g(c);
129 g(c >> 8);
130 g(c >> 16);
131 g(c >> 24);
134 /* output a symbol and patch all calls to it */
135 ST_FUNC void gsym_addr(int t, int a)
137 while (t) {
138 unsigned char *ptr = cur_text_section->data + t;
139 uint32_t n = read32le(ptr); /* next value */
140 write32le(ptr, a - t - 4);
141 t = n;
145 ST_FUNC void gsym(int t)
147 gsym_addr(t, ind);
150 /* instruction + 4 bytes data. Return the address of the data */
151 ST_FUNC int oad(int c, int s)
153 int t;
154 if (nocode_wanted)
155 return s;
156 o(c);
157 t = ind;
158 gen_le32(s);
159 return t;
162 /* output constant with relocation if 'r & VT_SYM' is true */
163 ST_FUNC void gen_addr32(int r, Sym *sym, long c)
165 if (r & VT_SYM)
166 greloc(cur_text_section, sym, ind, R_386_32);
167 gen_le32(c);
170 ST_FUNC void gen_addrpc32(int r, Sym *sym, long c)
172 if (r & VT_SYM)
173 greloc(cur_text_section, sym, ind, R_386_PC32);
174 gen_le32(c - 4);
177 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
178 opcode bits */
179 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
181 op_reg = op_reg << 3;
182 if ((r & VT_VALMASK) == VT_CONST) {
183 /* constant memory reference */
184 o(0x05 | op_reg);
185 gen_addr32(r, sym, c);
186 } else if ((r & VT_VALMASK) == VT_LOCAL) {
187 /* currently, we use only ebp as base */
188 if (c == (char)c) {
189 /* short reference */
190 o(0x45 | op_reg);
191 g(c);
192 } else {
193 oad(0x85 | op_reg, c);
195 } else {
196 g(0x00 | op_reg | (r & VT_VALMASK));
200 /* load 'r' from value 'sv' */
201 ST_FUNC void load(int r, SValue *sv)
203 int v, t, ft, fc, fr;
204 SValue v1;
206 #ifdef TCC_TARGET_PE
207 SValue v2;
208 sv = pe_getimport(sv, &v2);
209 #endif
211 fr = sv->r;
212 ft = sv->type.t;
213 fc = sv->c.i;
215 ft &= ~(VT_VOLATILE | VT_CONSTANT);
217 v = fr & VT_VALMASK;
218 if (fr & VT_LVAL) {
219 if (v == VT_LLOCAL) {
220 v1.type.t = VT_INT;
221 v1.r = VT_LOCAL | VT_LVAL;
222 v1.c.i = fc;
223 fr = r;
224 if (!(reg_classes[fr] & RC_INT))
225 fr = get_reg(RC_INT);
226 load(fr, &v1);
228 if ((ft & VT_BTYPE) == VT_FLOAT) {
229 o(0xd9); /* flds */
230 r = 0;
231 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
232 o(0xdd); /* fldl */
233 r = 0;
234 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
235 o(0xdb); /* fldt */
236 r = 5;
237 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
238 o(0xbe0f); /* movsbl */
239 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
240 o(0xb60f); /* movzbl */
241 } else if ((ft & VT_TYPE) == VT_SHORT) {
242 o(0xbf0f); /* movswl */
243 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
244 o(0xb70f); /* movzwl */
245 } else {
246 o(0x8b); /* movl */
248 gen_modrm(r, fr, sv->sym, fc);
249 } else {
250 if (v == VT_CONST) {
251 o(0xb8 + r); /* mov $xx, r */
252 gen_addr32(fr, sv->sym, fc);
253 } else if (v == VT_LOCAL) {
254 if (fc) {
255 o(0x8d); /* lea xxx(%ebp), r */
256 gen_modrm(r, VT_LOCAL, sv->sym, fc);
257 } else {
258 o(0x89);
259 o(0xe8 + r); /* mov %ebp, r */
261 } else if (v == VT_CMP) {
262 oad(0xb8 + r, 0); /* mov $0, r */
263 o(0x0f); /* setxx %br */
264 o(fc);
265 o(0xc0 + r);
266 } else if (v == VT_JMP || v == VT_JMPI) {
267 t = v & 1;
268 oad(0xb8 + r, t); /* mov $1, r */
269 o(0x05eb); /* jmp after */
270 gsym(fc);
271 oad(0xb8 + r, t ^ 1); /* mov $0, r */
272 } else if (v != r) {
273 o(0x89);
274 o(0xc0 + r + v * 8); /* mov v, r */
279 /* store register 'r' in lvalue 'v' */
280 ST_FUNC void store(int r, SValue *v)
282 int fr, bt, ft, fc;
284 #ifdef TCC_TARGET_PE
285 SValue v2;
286 v = pe_getimport(v, &v2);
287 #endif
289 ft = v->type.t;
290 fc = v->c.i;
291 fr = v->r & VT_VALMASK;
292 ft &= ~(VT_VOLATILE | VT_CONSTANT);
293 bt = ft & VT_BTYPE;
294 /* XXX: incorrect if float reg to reg */
295 if (bt == VT_FLOAT) {
296 o(0xd9); /* fsts */
297 r = 2;
298 } else if (bt == VT_DOUBLE) {
299 o(0xdd); /* fstpl */
300 r = 2;
301 } else if (bt == VT_LDOUBLE) {
302 o(0xc0d9); /* fld %st(0) */
303 o(0xdb); /* fstpt */
304 r = 7;
305 } else {
306 if (bt == VT_SHORT)
307 o(0x66);
308 if (bt == VT_BYTE || bt == VT_BOOL)
309 o(0x88);
310 else
311 o(0x89);
313 if (fr == VT_CONST ||
314 fr == VT_LOCAL ||
315 (v->r & VT_LVAL)) {
316 gen_modrm(r, v->r, v->sym, fc);
317 } else if (fr != r) {
318 o(0xc0 + fr + r * 8); /* mov r, fr */
322 static void gadd_sp(int val)
324 if (val == (char)val) {
325 o(0xc483);
326 g(val);
327 } else {
328 oad(0xc481, val); /* add $xxx, %esp */
332 static void gen_static_call(int v)
334 Sym *sym;
336 sym = external_global_sym(v, &func_old_type, 0);
337 oad(0xe8, -4);
338 greloc(cur_text_section, sym, ind-4, R_386_PC32);
341 /* 'is_jmp' is '1' if it is a jump */
342 static void gcall_or_jmp(int is_jmp)
344 int r;
345 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
346 int rt;
347 /* constant case */
348 if (vtop->r & VT_SYM) {
349 /* relocation case */
350 greloc(cur_text_section, vtop->sym,
351 ind + 1, R_386_PC32);
352 } else {
353 /* put an empty PC32 relocation */
354 put_elf_reloc(symtab_section, cur_text_section,
355 ind + 1, R_386_PC32, 0);
357 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
358 /* extend the return value to the whole register if necessary
359 visual studio and gcc do not always set the whole eax register
360 when assigning the return value of a function */
361 rt = vtop->type.ref->type.t;
362 switch (rt & VT_BTYPE) {
363 case VT_BYTE:
364 if (rt & VT_UNSIGNED) {
365 o(0xc0b60f); /* movzx %al, %eax */
367 else {
368 o(0xc0be0f); /* movsx %al, %eax */
370 break;
371 case VT_SHORT:
372 if (rt & VT_UNSIGNED) {
373 o(0xc0b70f); /* movzx %ax, %eax */
375 else {
376 o(0xc0bf0f); /* movsx %ax, %eax */
378 break;
379 default:
380 break;
382 } else {
383 /* otherwise, indirect call */
384 r = gv(RC_INT);
385 o(0xff); /* call/jmp *r */
386 o(0xd0 + r + (is_jmp << 4));
390 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
391 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
393 /* Return the number of registers needed to return the struct, or 0 if
394 returning via struct pointer. */
395 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
397 #ifdef TCC_TARGET_PE
398 int size, align;
400 *ret_align = 1; // Never have to re-align return values for x86
401 *regsize = 4;
402 size = type_size(vt, &align);
403 if (size > 8) {
404 return 0;
405 } else if (size > 4) {
406 ret->ref = NULL;
407 ret->t = VT_LLONG;
408 return 1;
409 } else {
410 ret->ref = NULL;
411 ret->t = VT_INT;
412 return 1;
414 #else
415 *ret_align = 1; // Never have to re-align return values for x86
416 return 0;
417 #endif
420 /* Generate function call. The function address is pushed first, then
421 all the parameters in call order. This functions pops all the
422 parameters and the function address. */
423 ST_FUNC void gfunc_call(int nb_args)
425 int size, align, r, args_size, i, func_call;
426 Sym *func_sym;
428 args_size = 0;
429 for(i = 0;i < nb_args; i++) {
430 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
431 size = type_size(&vtop->type, &align);
432 /* align to stack align size */
433 size = (size + 3) & ~3;
434 /* allocate the necessary size on stack */
435 oad(0xec81, size); /* sub $xxx, %esp */
436 /* generate structure store */
437 r = get_reg(RC_INT);
438 o(0x89); /* mov %esp, r */
439 o(0xe0 + 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->a.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 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 #ifndef TCC_TARGET_PE
499 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
500 args_size -= 4;
501 #endif
502 gcall_or_jmp(0);
504 if (args_size && func_call != FUNC_STDCALL)
505 gadd_sp(args_size);
506 vtop--;
509 #ifdef TCC_TARGET_PE
510 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
511 #else
512 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
513 #endif
515 /* generate function prolog of type 't' */
516 ST_FUNC void gfunc_prolog(CType *func_type)
518 int addr, align, size, func_call, fastcall_nb_regs;
519 int param_index, param_addr;
520 uint8_t *fastcall_regs_ptr;
521 Sym *sym;
522 CType *type;
524 sym = func_type->ref;
525 func_call = sym->a.func_call;
526 addr = 8;
527 loc = 0;
528 func_vc = 0;
530 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
531 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
532 fastcall_regs_ptr = fastcall_regs;
533 } else if (func_call == FUNC_FASTCALLW) {
534 fastcall_nb_regs = 2;
535 fastcall_regs_ptr = fastcallw_regs;
536 } else {
537 fastcall_nb_regs = 0;
538 fastcall_regs_ptr = NULL;
540 param_index = 0;
542 ind += FUNC_PROLOG_SIZE;
543 func_sub_sp_offset = ind;
544 /* if the function returns a structure, then add an
545 implicit pointer parameter */
546 func_vt = sym->type;
547 func_var = (sym->c == FUNC_ELLIPSIS);
548 #ifdef TCC_TARGET_PE
549 size = type_size(&func_vt,&align);
550 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
551 #else
552 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
553 #endif
554 /* XXX: fastcall case ? */
555 func_vc = addr;
556 addr += 4;
557 param_index++;
559 /* define parameters */
560 while ((sym = sym->next) != NULL) {
561 type = &sym->type;
562 size = type_size(type, &align);
563 size = (size + 3) & ~3;
564 #ifdef FUNC_STRUCT_PARAM_AS_PTR
565 /* structs are passed as pointer */
566 if ((type->t & VT_BTYPE) == VT_STRUCT) {
567 size = 4;
569 #endif
570 if (param_index < fastcall_nb_regs) {
571 /* save FASTCALL register */
572 loc -= 4;
573 o(0x89); /* movl */
574 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
575 param_addr = loc;
576 } else {
577 param_addr = addr;
578 addr += size;
580 sym_push(sym->v & ~SYM_FIELD, type,
581 VT_LOCAL | lvalue_type(type->t), param_addr);
582 param_index++;
584 func_ret_sub = 0;
585 /* pascal type call ? */
586 if (func_call == FUNC_STDCALL)
587 func_ret_sub = addr - 8;
588 #ifndef TCC_TARGET_PE
589 else if (func_vc)
590 func_ret_sub = 4;
591 #endif
593 #ifdef CONFIG_TCC_BCHECK
594 /* leave some room for bound checking code */
595 if (tcc_state->do_bounds_check) {
596 func_bound_offset = lbounds_section->data_offset;
597 func_bound_ind = ind;
598 oad(0xb8, 0); /* lbound section pointer */
599 oad(0xb8, 0); /* call to function */
601 #endif
604 /* generate function epilog */
605 ST_FUNC void gfunc_epilog(void)
607 addr_t v, saved_ind;
609 #ifdef CONFIG_TCC_BCHECK
610 if (tcc_state->do_bounds_check
611 && func_bound_offset != lbounds_section->data_offset) {
612 addr_t saved_ind;
613 addr_t *bounds_ptr;
614 Sym *sym_data;
616 /* add end of table info */
617 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
618 *bounds_ptr = 0;
620 /* generate bound local allocation */
621 saved_ind = ind;
622 ind = func_bound_ind;
623 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
624 func_bound_offset, lbounds_section->data_offset);
625 greloc(cur_text_section, sym_data,
626 ind + 1, R_386_32);
627 oad(0xb8, 0); /* mov %eax, xxx */
628 gen_static_call(TOK___bound_local_new);
629 ind = saved_ind;
631 /* generate bound check local freeing */
632 o(0x5250); /* save returned value, if any */
633 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
634 oad(0xb8, 0); /* mov %eax, xxx */
635 gen_static_call(TOK___bound_local_delete);
636 o(0x585a); /* restore returned value, if any */
638 #endif
640 /* align local size to word & save local variables */
641 v = (-loc + 3) & -4;
643 #if USE_EBX
644 o(0x8b);
645 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
646 #endif
648 o(0xc9); /* leave */
649 if (func_ret_sub == 0) {
650 o(0xc3); /* ret */
651 } else {
652 o(0xc2); /* ret n */
653 g(func_ret_sub);
654 g(func_ret_sub >> 8);
656 saved_ind = ind;
657 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
658 #ifdef TCC_TARGET_PE
659 if (v >= 4096) {
660 oad(0xb8, v); /* mov stacksize, %eax */
661 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
662 } else
663 #endif
665 o(0xe58955); /* push %ebp, mov %esp, %ebp */
666 o(0xec81); /* sub esp, stacksize */
667 gen_le32(v);
668 #ifdef TCC_TARGET_PE
669 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
670 #endif
672 o(0x53 * USE_EBX); /* push ebx */
673 ind = saved_ind;
676 /* generate a jump to a label */
677 ST_FUNC int gjmp(int t)
679 return gjmp2(0xe9, t);
682 /* generate a jump to a fixed address */
683 ST_FUNC void gjmp_addr(int a)
685 int r;
686 r = a - ind - 2;
687 if (r == (char)r) {
688 g(0xeb);
689 g(r);
690 } else {
691 oad(0xe9, a - ind - 5);
695 ST_FUNC void gtst_addr(int inv, int a)
697 int v = vtop->r & VT_VALMASK;
698 if (v == VT_CMP) {
699 inv ^= (vtop--)->c.i;
700 a -= ind + 2;
701 if (a == (char)a) {
702 g(inv - 32);
703 g(a);
704 } else {
705 g(0x0f);
706 oad(inv - 16, a - 4);
708 } else if ((v & ~1) == VT_JMP) {
709 if ((v & 1) != inv) {
710 gjmp_addr(a);
711 gsym(vtop->c.i);
712 } else {
713 gsym(vtop->c.i);
714 o(0x05eb);
715 gjmp_addr(a);
717 vtop--;
721 /* generate a test. set 'inv' to invert test. Stack entry is popped */
722 ST_FUNC int gtst(int inv, int t)
724 int v = vtop->r & VT_VALMASK;
725 if (nocode_wanted) {
727 } else if (v == VT_CMP) {
728 /* fast case : can jump directly since flags are set */
729 g(0x0f);
730 t = gjmp2((vtop->c.i - 16) ^ inv, t);
731 } else if (v == VT_JMP || v == VT_JMPI) {
732 /* && or || optimization */
733 if ((v & 1) == inv) {
734 /* insert vtop->c jump list in t */
735 uint32_t n1, n = vtop->c.i;
736 if (n) {
737 while ((n1 = read32le(cur_text_section->data + n)))
738 n = n1;
739 write32le(cur_text_section->data + n, t);
740 t = vtop->c.i;
742 } else {
743 t = gjmp(t);
744 gsym(vtop->c.i);
747 vtop--;
748 return t;
751 /* generate an integer binary operation */
752 ST_FUNC void gen_opi(int op)
754 int r, fr, opc, c;
756 switch(op) {
757 case '+':
758 case TOK_ADDC1: /* add with carry generation */
759 opc = 0;
760 gen_op8:
761 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
762 /* constant case */
763 vswap();
764 r = gv(RC_INT);
765 vswap();
766 c = vtop->c.i;
767 if (c == (char)c) {
768 /* generate inc and dec for smaller code */
769 if (c==1 && opc==0 && op != TOK_ADDC1) {
770 o (0x40 | r); // inc
771 } else if (c==1 && opc==5 && op != TOK_SUBC1) {
772 o (0x48 | r); // dec
773 } else {
774 o(0x83);
775 o(0xc0 | (opc << 3) | r);
776 g(c);
778 } else {
779 o(0x81);
780 oad(0xc0 | (opc << 3) | r, c);
782 } else {
783 gv2(RC_INT, RC_INT);
784 r = vtop[-1].r;
785 fr = vtop[0].r;
786 o((opc << 3) | 0x01);
787 o(0xc0 + r + fr * 8);
789 vtop--;
790 if (op >= TOK_ULT && op <= TOK_GT) {
791 vtop->r = VT_CMP;
792 vtop->c.i = op;
794 break;
795 case '-':
796 case TOK_SUBC1: /* sub with carry generation */
797 opc = 5;
798 goto gen_op8;
799 case TOK_ADDC2: /* add with carry use */
800 opc = 2;
801 goto gen_op8;
802 case TOK_SUBC2: /* sub with carry use */
803 opc = 3;
804 goto gen_op8;
805 case '&':
806 opc = 4;
807 goto gen_op8;
808 case '^':
809 opc = 6;
810 goto gen_op8;
811 case '|':
812 opc = 1;
813 goto gen_op8;
814 case '*':
815 gv2(RC_INT, RC_INT);
816 r = vtop[-1].r;
817 fr = vtop[0].r;
818 vtop--;
819 o(0xaf0f); /* imul fr, r */
820 o(0xc0 + fr + r * 8);
821 break;
822 case TOK_SHL:
823 opc = 4;
824 goto gen_shift;
825 case TOK_SHR:
826 opc = 5;
827 goto gen_shift;
828 case TOK_SAR:
829 opc = 7;
830 gen_shift:
831 opc = 0xc0 | (opc << 3);
832 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
833 /* constant case */
834 vswap();
835 r = gv(RC_INT);
836 vswap();
837 c = vtop->c.i & 0x1f;
838 o(0xc1); /* shl/shr/sar $xxx, r */
839 o(opc | r);
840 g(c);
841 } else {
842 /* we generate the shift in ecx */
843 gv2(RC_INT, RC_ECX);
844 r = vtop[-1].r;
845 o(0xd3); /* shl/shr/sar %cl, r */
846 o(opc | r);
848 vtop--;
849 break;
850 case '/':
851 case TOK_UDIV:
852 case TOK_PDIV:
853 case '%':
854 case TOK_UMOD:
855 case TOK_UMULL:
856 /* first operand must be in eax */
857 /* XXX: need better constraint for second operand */
858 gv2(RC_EAX, RC_ECX);
859 r = vtop[-1].r;
860 fr = vtop[0].r;
861 vtop--;
862 save_reg(TREG_EDX);
863 /* save EAX too if used otherwise */
864 save_reg_upstack(TREG_EAX, 1);
865 if (op == TOK_UMULL) {
866 o(0xf7); /* mul fr */
867 o(0xe0 + fr);
868 vtop->r2 = TREG_EDX;
869 r = TREG_EAX;
870 } else {
871 if (op == TOK_UDIV || op == TOK_UMOD) {
872 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
873 o(0xf0 + fr);
874 } else {
875 o(0xf799); /* cltd, idiv fr, %eax */
876 o(0xf8 + fr);
878 if (op == '%' || op == TOK_UMOD)
879 r = TREG_EDX;
880 else
881 r = TREG_EAX;
883 vtop->r = r;
884 break;
885 default:
886 opc = 7;
887 goto gen_op8;
891 /* generate a floating point operation 'v = t1 op t2' instruction. The
892 two operands are guaranted to have the same floating point type */
893 /* XXX: need to use ST1 too */
894 ST_FUNC void gen_opf(int op)
896 int a, ft, fc, swapped, r;
898 /* convert constants to memory references */
899 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
900 vswap();
901 gv(RC_FLOAT);
902 vswap();
904 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
905 gv(RC_FLOAT);
907 /* must put at least one value in the floating point register */
908 if ((vtop[-1].r & VT_LVAL) &&
909 (vtop[0].r & VT_LVAL)) {
910 vswap();
911 gv(RC_FLOAT);
912 vswap();
914 swapped = 0;
915 /* swap the stack if needed so that t1 is the register and t2 is
916 the memory reference */
917 if (vtop[-1].r & VT_LVAL) {
918 vswap();
919 swapped = 1;
921 if (op >= TOK_ULT && op <= TOK_GT) {
922 /* load on stack second operand */
923 load(TREG_ST0, vtop);
924 save_reg(TREG_EAX); /* eax is used by FP comparison code */
925 if (op == TOK_GE || op == TOK_GT)
926 swapped = !swapped;
927 else if (op == TOK_EQ || op == TOK_NE)
928 swapped = 0;
929 if (swapped)
930 o(0xc9d9); /* fxch %st(1) */
931 if (op == TOK_EQ || op == TOK_NE)
932 o(0xe9da); /* fucompp */
933 else
934 o(0xd9de); /* fcompp */
935 o(0xe0df); /* fnstsw %ax */
936 if (op == TOK_EQ) {
937 o(0x45e480); /* and $0x45, %ah */
938 o(0x40fC80); /* cmp $0x40, %ah */
939 } else if (op == TOK_NE) {
940 o(0x45e480); /* and $0x45, %ah */
941 o(0x40f480); /* xor $0x40, %ah */
942 op = TOK_NE;
943 } else if (op == TOK_GE || op == TOK_LE) {
944 o(0x05c4f6); /* test $0x05, %ah */
945 op = TOK_EQ;
946 } else {
947 o(0x45c4f6); /* test $0x45, %ah */
948 op = TOK_EQ;
950 vtop--;
951 vtop->r = VT_CMP;
952 vtop->c.i = op;
953 } else {
954 /* no memory reference possible for long double operations */
955 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
956 load(TREG_ST0, vtop);
957 swapped = !swapped;
960 switch(op) {
961 default:
962 case '+':
963 a = 0;
964 break;
965 case '-':
966 a = 4;
967 if (swapped)
968 a++;
969 break;
970 case '*':
971 a = 1;
972 break;
973 case '/':
974 a = 6;
975 if (swapped)
976 a++;
977 break;
979 ft = vtop->type.t;
980 fc = vtop->c.i;
981 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
982 o(0xde); /* fxxxp %st, %st(1) */
983 o(0xc1 + (a << 3));
984 } else {
985 /* if saved lvalue, then we must reload it */
986 r = vtop->r;
987 if ((r & VT_VALMASK) == VT_LLOCAL) {
988 SValue v1;
989 r = get_reg(RC_INT);
990 v1.type.t = VT_INT;
991 v1.r = VT_LOCAL | VT_LVAL;
992 v1.c.i = fc;
993 load(r, &v1);
994 fc = 0;
997 if ((ft & VT_BTYPE) == VT_DOUBLE)
998 o(0xdc);
999 else
1000 o(0xd8);
1001 gen_modrm(a, r, vtop->sym, fc);
1003 vtop--;
1007 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1008 and 'long long' cases. */
1009 ST_FUNC void gen_cvt_itof(int t)
1011 save_reg(TREG_ST0);
1012 gv(RC_INT);
1013 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1014 /* signed long long to float/double/long double (unsigned case
1015 is handled generically) */
1016 o(0x50 + vtop->r2); /* push r2 */
1017 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1018 o(0x242cdf); /* fildll (%esp) */
1019 o(0x08c483); /* add $8, %esp */
1020 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1021 (VT_INT | VT_UNSIGNED)) {
1022 /* unsigned int to float/double/long double */
1023 o(0x6a); /* push $0 */
1024 g(0x00);
1025 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1026 o(0x242cdf); /* fildll (%esp) */
1027 o(0x08c483); /* add $8, %esp */
1028 } else {
1029 /* int to float/double/long double */
1030 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1031 o(0x2404db); /* fildl (%esp) */
1032 o(0x04c483); /* add $4, %esp */
1034 vtop->r = TREG_ST0;
1037 /* convert fp to int 't' type */
1038 ST_FUNC void gen_cvt_ftoi(int t)
1040 #if 1
1041 gv(RC_FLOAT);
1042 save_reg(TREG_EAX);
1043 save_reg(TREG_EDX);
1044 gen_static_call(TOK___tcc_cvt_ftol);
1045 vtop->r = TREG_EAX; /* mark reg as used */
1046 if (t == VT_LLONG)
1047 vtop->r2 = TREG_EDX;
1048 #else
1049 int bt = vtop->type.t & VT_BTYPE;
1050 if (bt == VT_FLOAT)
1051 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1052 else if (bt == VT_LDOUBLE)
1053 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1054 else
1055 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1056 vswap();
1057 gfunc_call(1);
1058 vpushi(0);
1059 vtop->r = REG_IRET;
1060 vtop->r2 = REG_LRET;
1061 #endif
1064 /* convert from one floating point type to another */
1065 ST_FUNC void gen_cvt_ftof(int t)
1067 /* all we have to do on i386 is to put the float in a register */
1068 gv(RC_FLOAT);
1071 /* computed goto support */
1072 ST_FUNC void ggoto(void)
1074 gcall_or_jmp(1);
1075 vtop--;
1078 /* bound check support functions */
1079 #ifdef CONFIG_TCC_BCHECK
1081 /* generate a bounded pointer addition */
1082 ST_FUNC void gen_bounded_ptr_add(void)
1084 /* prepare fast i386 function call (args in eax and edx) */
1085 gv2(RC_EAX, RC_EDX);
1086 /* save all temporary registers */
1087 vtop -= 2;
1088 save_regs(0);
1089 /* do a fast function call */
1090 gen_static_call(TOK___bound_ptr_add);
1091 /* returned pointer is in eax */
1092 vtop++;
1093 vtop->r = TREG_EAX | VT_BOUNDED;
1094 /* address of bounding function call point */
1095 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1098 /* patch pointer addition in vtop so that pointer dereferencing is
1099 also tested */
1100 ST_FUNC void gen_bounded_ptr_deref(void)
1102 addr_t func;
1103 int size, align;
1104 Elf32_Rel *rel;
1105 Sym *sym;
1107 size = 0;
1108 /* XXX: put that code in generic part of tcc */
1109 if (!is_float(vtop->type.t)) {
1110 if (vtop->r & VT_LVAL_BYTE)
1111 size = 1;
1112 else if (vtop->r & VT_LVAL_SHORT)
1113 size = 2;
1115 if (!size)
1116 size = type_size(&vtop->type, &align);
1117 switch(size) {
1118 case 1: func = TOK___bound_ptr_indir1; break;
1119 case 2: func = TOK___bound_ptr_indir2; break;
1120 case 4: func = TOK___bound_ptr_indir4; break;
1121 case 8: func = TOK___bound_ptr_indir8; break;
1122 case 12: func = TOK___bound_ptr_indir12; break;
1123 case 16: func = TOK___bound_ptr_indir16; break;
1124 default:
1125 tcc_error("unhandled size when dereferencing bounded pointer");
1126 func = 0;
1127 break;
1130 /* patch relocation */
1131 /* XXX: find a better solution ? */
1132 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1133 sym = external_global_sym(func, &func_old_type, 0);
1134 if (!sym->c)
1135 put_extern_sym(sym, NULL, 0, 0);
1136 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1138 #endif
1140 /* Save the stack pointer onto the stack */
1141 ST_FUNC void gen_vla_sp_save(int addr) {
1142 /* mov %esp,addr(%ebp)*/
1143 o(0x89);
1144 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1147 /* Restore the SP from a location on the stack */
1148 ST_FUNC void gen_vla_sp_restore(int addr) {
1149 o(0x8b);
1150 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1153 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1154 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1155 #ifdef TCC_TARGET_PE
1156 /* alloca does more than just adjust %rsp on Windows */
1157 vpush_global_sym(&func_old_type, TOK_alloca);
1158 vswap(); /* Move alloca ref past allocation size */
1159 gfunc_call(1);
1160 #else
1161 int r;
1162 r = gv(RC_INT); /* allocation size */
1163 /* sub r,%rsp */
1164 o(0x2b);
1165 o(0xe0 | r);
1166 /* We align to 16 bytes rather than align */
1167 /* and ~15, %esp */
1168 o(0xf0e483);
1169 vpop();
1170 #endif
1173 /* end of X86 code generator */
1174 /*************************************************************/
1175 #endif
1176 /*************************************************************/