arm: libtcc1.a needs gcc with -fPIC
[tinycc.git] / i386-gen.c
blob759f33c9192bba87e003de7b8582a67eabcae835
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;
399 *ret_align = 1; // Never have to re-align return values for x86
400 *regsize = 4;
401 size = type_size(vt, &align);
402 if (size > 8 || (size & (size - 1)))
403 return 0;
404 if (size == 8)
405 ret->t = VT_LLONG;
406 else if (size == 4)
407 ret->t = VT_INT;
408 else if (size == 2)
409 ret->t = VT_SHORT;
410 else
411 ret->t = VT_BYTE;
412 ret->ref = NULL;
413 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)
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 | lvalue_type(type->t), param_addr);
583 param_index++;
585 func_ret_sub = 0;
586 /* pascal type call ? */
587 if (func_call == FUNC_STDCALL)
588 func_ret_sub = addr - 8;
589 #ifndef TCC_TARGET_PE
590 else if (func_vc)
591 func_ret_sub = 4;
592 #endif
594 #ifdef CONFIG_TCC_BCHECK
595 /* leave some room for bound checking code */
596 if (tcc_state->do_bounds_check) {
597 func_bound_offset = lbounds_section->data_offset;
598 func_bound_ind = ind;
599 oad(0xb8, 0); /* lbound section pointer */
600 oad(0xb8, 0); /* call to function */
602 #endif
605 /* generate function epilog */
606 ST_FUNC void gfunc_epilog(void)
608 addr_t v, saved_ind;
610 #ifdef CONFIG_TCC_BCHECK
611 if (tcc_state->do_bounds_check
612 && func_bound_offset != lbounds_section->data_offset) {
613 addr_t saved_ind;
614 addr_t *bounds_ptr;
615 Sym *sym_data;
617 /* add end of table info */
618 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
619 *bounds_ptr = 0;
621 /* generate bound local allocation */
622 saved_ind = ind;
623 ind = func_bound_ind;
624 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
625 func_bound_offset, lbounds_section->data_offset);
626 greloc(cur_text_section, sym_data,
627 ind + 1, R_386_32);
628 oad(0xb8, 0); /* mov %eax, xxx */
629 gen_static_call(TOK___bound_local_new);
630 ind = saved_ind;
632 /* generate bound check local freeing */
633 o(0x5250); /* save returned value, if any */
634 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
635 oad(0xb8, 0); /* mov %eax, xxx */
636 gen_static_call(TOK___bound_local_delete);
637 o(0x585a); /* restore returned value, if any */
639 #endif
641 /* align local size to word & save local variables */
642 v = (-loc + 3) & -4;
644 #if USE_EBX
645 o(0x8b);
646 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
647 #endif
649 o(0xc9); /* leave */
650 if (func_ret_sub == 0) {
651 o(0xc3); /* ret */
652 } else {
653 o(0xc2); /* ret n */
654 g(func_ret_sub);
655 g(func_ret_sub >> 8);
657 saved_ind = ind;
658 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
659 #ifdef TCC_TARGET_PE
660 if (v >= 4096) {
661 oad(0xb8, v); /* mov stacksize, %eax */
662 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
663 } else
664 #endif
666 o(0xe58955); /* push %ebp, mov %esp, %ebp */
667 o(0xec81); /* sub esp, stacksize */
668 gen_le32(v);
669 #ifdef TCC_TARGET_PE
670 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
671 #endif
673 o(0x53 * USE_EBX); /* push ebx */
674 ind = saved_ind;
677 /* generate a jump to a label */
678 ST_FUNC int gjmp(int t)
680 return gjmp2(0xe9, t);
683 /* generate a jump to a fixed address */
684 ST_FUNC void gjmp_addr(int a)
686 int r;
687 r = a - ind - 2;
688 if (r == (char)r) {
689 g(0xeb);
690 g(r);
691 } else {
692 oad(0xe9, a - ind - 5);
696 ST_FUNC void gtst_addr(int inv, int a)
698 int v = vtop->r & VT_VALMASK;
699 if (v == VT_CMP) {
700 inv ^= (vtop--)->c.i;
701 a -= ind + 2;
702 if (a == (char)a) {
703 g(inv - 32);
704 g(a);
705 } else {
706 g(0x0f);
707 oad(inv - 16, a - 4);
709 } else if ((v & ~1) == VT_JMP) {
710 if ((v & 1) != inv) {
711 gjmp_addr(a);
712 gsym(vtop->c.i);
713 } else {
714 gsym(vtop->c.i);
715 o(0x05eb);
716 gjmp_addr(a);
718 vtop--;
722 /* generate a test. set 'inv' to invert test. Stack entry is popped */
723 ST_FUNC int gtst(int inv, int t)
725 int v = vtop->r & VT_VALMASK;
726 if (nocode_wanted) {
728 } else if (v == VT_CMP) {
729 /* fast case : can jump directly since flags are set */
730 g(0x0f);
731 t = gjmp2((vtop->c.i - 16) ^ inv, t);
732 } else if (v == VT_JMP || v == VT_JMPI) {
733 /* && or || optimization */
734 if ((v & 1) == inv) {
735 /* insert vtop->c jump list in t */
736 uint32_t n1, n = vtop->c.i;
737 if (n) {
738 while ((n1 = read32le(cur_text_section->data + n)))
739 n = n1;
740 write32le(cur_text_section->data + n, t);
741 t = vtop->c.i;
743 } else {
744 t = gjmp(t);
745 gsym(vtop->c.i);
748 vtop--;
749 return t;
752 /* generate an integer binary operation */
753 ST_FUNC void gen_opi(int op)
755 int r, fr, opc, c;
757 switch(op) {
758 case '+':
759 case TOK_ADDC1: /* add with carry generation */
760 opc = 0;
761 gen_op8:
762 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
763 /* constant case */
764 vswap();
765 r = gv(RC_INT);
766 vswap();
767 c = vtop->c.i;
768 if (c == (char)c) {
769 /* generate inc and dec for smaller code */
770 if (c==1 && opc==0 && op != TOK_ADDC1) {
771 o (0x40 | r); // inc
772 } else if (c==1 && opc==5 && op != TOK_SUBC1) {
773 o (0x48 | r); // dec
774 } else {
775 o(0x83);
776 o(0xc0 | (opc << 3) | r);
777 g(c);
779 } else {
780 o(0x81);
781 oad(0xc0 | (opc << 3) | r, c);
783 } else {
784 gv2(RC_INT, RC_INT);
785 r = vtop[-1].r;
786 fr = vtop[0].r;
787 o((opc << 3) | 0x01);
788 o(0xc0 + r + fr * 8);
790 vtop--;
791 if (op >= TOK_ULT && op <= TOK_GT) {
792 vtop->r = VT_CMP;
793 vtop->c.i = op;
795 break;
796 case '-':
797 case TOK_SUBC1: /* sub with carry generation */
798 opc = 5;
799 goto gen_op8;
800 case TOK_ADDC2: /* add with carry use */
801 opc = 2;
802 goto gen_op8;
803 case TOK_SUBC2: /* sub with carry use */
804 opc = 3;
805 goto gen_op8;
806 case '&':
807 opc = 4;
808 goto gen_op8;
809 case '^':
810 opc = 6;
811 goto gen_op8;
812 case '|':
813 opc = 1;
814 goto gen_op8;
815 case '*':
816 gv2(RC_INT, RC_INT);
817 r = vtop[-1].r;
818 fr = vtop[0].r;
819 vtop--;
820 o(0xaf0f); /* imul fr, r */
821 o(0xc0 + fr + r * 8);
822 break;
823 case TOK_SHL:
824 opc = 4;
825 goto gen_shift;
826 case TOK_SHR:
827 opc = 5;
828 goto gen_shift;
829 case TOK_SAR:
830 opc = 7;
831 gen_shift:
832 opc = 0xc0 | (opc << 3);
833 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
834 /* constant case */
835 vswap();
836 r = gv(RC_INT);
837 vswap();
838 c = vtop->c.i & 0x1f;
839 o(0xc1); /* shl/shr/sar $xxx, r */
840 o(opc | r);
841 g(c);
842 } else {
843 /* we generate the shift in ecx */
844 gv2(RC_INT, RC_ECX);
845 r = vtop[-1].r;
846 o(0xd3); /* shl/shr/sar %cl, r */
847 o(opc | r);
849 vtop--;
850 break;
851 case '/':
852 case TOK_UDIV:
853 case TOK_PDIV:
854 case '%':
855 case TOK_UMOD:
856 case TOK_UMULL:
857 /* first operand must be in eax */
858 /* XXX: need better constraint for second operand */
859 gv2(RC_EAX, RC_ECX);
860 r = vtop[-1].r;
861 fr = vtop[0].r;
862 vtop--;
863 save_reg(TREG_EDX);
864 /* save EAX too if used otherwise */
865 save_reg_upstack(TREG_EAX, 1);
866 if (op == TOK_UMULL) {
867 o(0xf7); /* mul fr */
868 o(0xe0 + fr);
869 vtop->r2 = TREG_EDX;
870 r = TREG_EAX;
871 } else {
872 if (op == TOK_UDIV || op == TOK_UMOD) {
873 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
874 o(0xf0 + fr);
875 } else {
876 o(0xf799); /* cltd, idiv fr, %eax */
877 o(0xf8 + fr);
879 if (op == '%' || op == TOK_UMOD)
880 r = TREG_EDX;
881 else
882 r = TREG_EAX;
884 vtop->r = r;
885 break;
886 default:
887 opc = 7;
888 goto gen_op8;
892 /* generate a floating point operation 'v = t1 op t2' instruction. The
893 two operands are guaranted to have the same floating point type */
894 /* XXX: need to use ST1 too */
895 ST_FUNC void gen_opf(int op)
897 int a, ft, fc, swapped, r;
899 /* convert constants to memory references */
900 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
901 vswap();
902 gv(RC_FLOAT);
903 vswap();
905 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
906 gv(RC_FLOAT);
908 /* must put at least one value in the floating point register */
909 if ((vtop[-1].r & VT_LVAL) &&
910 (vtop[0].r & VT_LVAL)) {
911 vswap();
912 gv(RC_FLOAT);
913 vswap();
915 swapped = 0;
916 /* swap the stack if needed so that t1 is the register and t2 is
917 the memory reference */
918 if (vtop[-1].r & VT_LVAL) {
919 vswap();
920 swapped = 1;
922 if (op >= TOK_ULT && op <= TOK_GT) {
923 /* load on stack second operand */
924 load(TREG_ST0, vtop);
925 save_reg(TREG_EAX); /* eax is used by FP comparison code */
926 if (op == TOK_GE || op == TOK_GT)
927 swapped = !swapped;
928 else if (op == TOK_EQ || op == TOK_NE)
929 swapped = 0;
930 if (swapped)
931 o(0xc9d9); /* fxch %st(1) */
932 if (op == TOK_EQ || op == TOK_NE)
933 o(0xe9da); /* fucompp */
934 else
935 o(0xd9de); /* fcompp */
936 o(0xe0df); /* fnstsw %ax */
937 if (op == TOK_EQ) {
938 o(0x45e480); /* and $0x45, %ah */
939 o(0x40fC80); /* cmp $0x40, %ah */
940 } else if (op == TOK_NE) {
941 o(0x45e480); /* and $0x45, %ah */
942 o(0x40f480); /* xor $0x40, %ah */
943 op = TOK_NE;
944 } else if (op == TOK_GE || op == TOK_LE) {
945 o(0x05c4f6); /* test $0x05, %ah */
946 op = TOK_EQ;
947 } else {
948 o(0x45c4f6); /* test $0x45, %ah */
949 op = TOK_EQ;
951 vtop--;
952 vtop->r = VT_CMP;
953 vtop->c.i = op;
954 } else {
955 /* no memory reference possible for long double operations */
956 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
957 load(TREG_ST0, vtop);
958 swapped = !swapped;
961 switch(op) {
962 default:
963 case '+':
964 a = 0;
965 break;
966 case '-':
967 a = 4;
968 if (swapped)
969 a++;
970 break;
971 case '*':
972 a = 1;
973 break;
974 case '/':
975 a = 6;
976 if (swapped)
977 a++;
978 break;
980 ft = vtop->type.t;
981 fc = vtop->c.i;
982 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
983 o(0xde); /* fxxxp %st, %st(1) */
984 o(0xc1 + (a << 3));
985 } else {
986 /* if saved lvalue, then we must reload it */
987 r = vtop->r;
988 if ((r & VT_VALMASK) == VT_LLOCAL) {
989 SValue v1;
990 r = get_reg(RC_INT);
991 v1.type.t = VT_INT;
992 v1.r = VT_LOCAL | VT_LVAL;
993 v1.c.i = fc;
994 load(r, &v1);
995 fc = 0;
998 if ((ft & VT_BTYPE) == VT_DOUBLE)
999 o(0xdc);
1000 else
1001 o(0xd8);
1002 gen_modrm(a, r, vtop->sym, fc);
1004 vtop--;
1008 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1009 and 'long long' cases. */
1010 ST_FUNC void gen_cvt_itof(int t)
1012 save_reg(TREG_ST0);
1013 gv(RC_INT);
1014 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1015 /* signed long long to float/double/long double (unsigned case
1016 is handled generically) */
1017 o(0x50 + vtop->r2); /* push r2 */
1018 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1019 o(0x242cdf); /* fildll (%esp) */
1020 o(0x08c483); /* add $8, %esp */
1021 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1022 (VT_INT | VT_UNSIGNED)) {
1023 /* unsigned int to float/double/long double */
1024 o(0x6a); /* push $0 */
1025 g(0x00);
1026 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1027 o(0x242cdf); /* fildll (%esp) */
1028 o(0x08c483); /* add $8, %esp */
1029 } else {
1030 /* int to float/double/long double */
1031 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1032 o(0x2404db); /* fildl (%esp) */
1033 o(0x04c483); /* add $4, %esp */
1035 vtop->r = TREG_ST0;
1038 /* convert fp to int 't' type */
1039 ST_FUNC void gen_cvt_ftoi(int t)
1041 int bt = vtop->type.t & VT_BTYPE;
1042 if (bt == VT_FLOAT)
1043 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1044 else if (bt == VT_LDOUBLE)
1045 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1046 else
1047 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1048 vswap();
1049 gfunc_call(1);
1050 vpushi(0);
1051 vtop->r = REG_IRET;
1052 vtop->r2 = REG_LRET;
1055 /* convert from one floating point type to another */
1056 ST_FUNC void gen_cvt_ftof(int t)
1058 /* all we have to do on i386 is to put the float in a register */
1059 gv(RC_FLOAT);
1062 /* computed goto support */
1063 ST_FUNC void ggoto(void)
1065 gcall_or_jmp(1);
1066 vtop--;
1069 /* bound check support functions */
1070 #ifdef CONFIG_TCC_BCHECK
1072 /* generate a bounded pointer addition */
1073 ST_FUNC void gen_bounded_ptr_add(void)
1075 /* prepare fast i386 function call (args in eax and edx) */
1076 gv2(RC_EAX, RC_EDX);
1077 /* save all temporary registers */
1078 vtop -= 2;
1079 save_regs(0);
1080 /* do a fast function call */
1081 gen_static_call(TOK___bound_ptr_add);
1082 /* returned pointer is in eax */
1083 vtop++;
1084 vtop->r = TREG_EAX | VT_BOUNDED;
1085 /* address of bounding function call point */
1086 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1089 /* patch pointer addition in vtop so that pointer dereferencing is
1090 also tested */
1091 ST_FUNC void gen_bounded_ptr_deref(void)
1093 addr_t func;
1094 int size, align;
1095 Elf32_Rel *rel;
1096 Sym *sym;
1098 size = 0;
1099 /* XXX: put that code in generic part of tcc */
1100 if (!is_float(vtop->type.t)) {
1101 if (vtop->r & VT_LVAL_BYTE)
1102 size = 1;
1103 else if (vtop->r & VT_LVAL_SHORT)
1104 size = 2;
1106 if (!size)
1107 size = type_size(&vtop->type, &align);
1108 switch(size) {
1109 case 1: func = TOK___bound_ptr_indir1; break;
1110 case 2: func = TOK___bound_ptr_indir2; break;
1111 case 4: func = TOK___bound_ptr_indir4; break;
1112 case 8: func = TOK___bound_ptr_indir8; break;
1113 case 12: func = TOK___bound_ptr_indir12; break;
1114 case 16: func = TOK___bound_ptr_indir16; break;
1115 default:
1116 tcc_error("unhandled size when dereferencing bounded pointer");
1117 func = 0;
1118 break;
1121 /* patch relocation */
1122 /* XXX: find a better solution ? */
1123 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1124 sym = external_global_sym(func, &func_old_type, 0);
1125 if (!sym->c)
1126 put_extern_sym(sym, NULL, 0, 0);
1127 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1129 #endif
1131 /* Save the stack pointer onto the stack */
1132 ST_FUNC void gen_vla_sp_save(int addr) {
1133 /* mov %esp,addr(%ebp)*/
1134 o(0x89);
1135 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1138 /* Restore the SP from a location on the stack */
1139 ST_FUNC void gen_vla_sp_restore(int addr) {
1140 o(0x8b);
1141 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1144 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1145 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1146 #ifdef TCC_TARGET_PE
1147 /* alloca does more than just adjust %rsp on Windows */
1148 vpush_global_sym(&func_old_type, TOK_alloca);
1149 vswap(); /* Move alloca ref past allocation size */
1150 gfunc_call(1);
1151 #else
1152 int r;
1153 r = gv(RC_INT); /* allocation size */
1154 /* sub r,%rsp */
1155 o(0x2b);
1156 o(0xe0 | r);
1157 /* We align to 16 bytes rather than align */
1158 /* and ~15, %esp */
1159 o(0xf0e483);
1160 vpop();
1161 #endif
1164 /* end of X86 code generator */
1165 /*************************************************************/
1166 #endif
1167 /*************************************************************/