Consolidate all relocations in relocate_section
[tinycc.git] / i386-gen.c
blob1d8ff19780a4922b790b79cf8325b352cedcc7c5
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
74 #define psym oad
76 /******************************************************/
77 /* ELF defines */
79 #define EM_TCC_TARGET EM_386
81 /* relocation type for 32 bit data relocation */
82 #define R_DATA_32 R_386_32
83 #define R_DATA_PTR R_386_32
84 #define R_JMP_SLOT R_386_JMP_SLOT
85 #define R_GLOB_DAT R_386_GLOB_DAT
86 #define R_COPY R_386_COPY
88 #define ELF_START_ADDR 0x08048000
89 #define ELF_PAGE_SIZE 0x1000
91 /******************************************************/
92 #else /* ! TARGET_DEFS_ONLY */
93 /******************************************************/
94 #include "tcc.h"
96 /* define to 1/0 to [not] have EBX as 4th register */
97 #define USE_EBX 1
99 ST_DATA const int reg_classes[NB_REGS] = {
100 /* eax */ RC_INT | RC_EAX,
101 /* ecx */ RC_INT | RC_ECX,
102 /* edx */ RC_INT | RC_EDX,
103 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
104 /* st0 */ RC_FLOAT | RC_ST0,
107 static unsigned long func_sub_sp_offset;
108 static int func_ret_sub;
109 #ifdef CONFIG_TCC_BCHECK
110 static addr_t func_bound_offset;
111 static unsigned long func_bound_ind;
112 #endif
114 /* XXX: make it faster ? */
115 ST_FUNC void g(int c)
117 int ind1;
118 ind1 = ind + 1;
119 if (ind1 > cur_text_section->data_allocated)
120 section_realloc(cur_text_section, ind1);
121 cur_text_section->data[ind] = c;
122 ind = ind1;
125 ST_FUNC void o(unsigned int c)
127 while (c) {
128 g(c);
129 c = c >> 8;
133 ST_FUNC void gen_le16(int v)
135 g(v);
136 g(v >> 8);
139 ST_FUNC void gen_le32(int c)
141 g(c);
142 g(c >> 8);
143 g(c >> 16);
144 g(c >> 24);
147 /* output a symbol and patch all calls to it */
148 ST_FUNC void gsym_addr(int t, int a)
150 while (t) {
151 unsigned char *ptr = cur_text_section->data + t;
152 uint32_t n = read32le(ptr); /* next value */
153 write32le(ptr, a - t - 4);
154 t = n;
158 ST_FUNC void gsym(int t)
160 gsym_addr(t, ind);
163 /* psym is used to put an instruction with a data field which is a
164 reference to a symbol. It is in fact the same as oad ! */
165 #define psym oad
167 /* instruction + 4 bytes data. Return the address of the data */
168 ST_FUNC int oad(int c, int s)
170 int ind1;
172 o(c);
173 ind1 = ind + 4;
174 if (ind1 > cur_text_section->data_allocated)
175 section_realloc(cur_text_section, ind1);
176 write32le(cur_text_section->data + ind, s);
177 s = ind;
178 ind = ind1;
179 return s;
182 /* output constant with relocation if 'r & VT_SYM' is true */
183 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
185 if (r & VT_SYM)
186 greloc(cur_text_section, sym, ind, R_386_32);
187 gen_le32(c);
190 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
192 if (r & VT_SYM)
193 greloc(cur_text_section, sym, ind, R_386_PC32);
194 gen_le32(c - 4);
197 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
198 opcode bits */
199 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
201 op_reg = op_reg << 3;
202 if ((r & VT_VALMASK) == VT_CONST) {
203 /* constant memory reference */
204 o(0x05 | op_reg);
205 gen_addr32(r, sym, c);
206 } else if ((r & VT_VALMASK) == VT_LOCAL) {
207 /* currently, we use only ebp as base */
208 if (c == (char)c) {
209 /* short reference */
210 o(0x45 | op_reg);
211 g(c);
212 } else {
213 oad(0x85 | op_reg, c);
215 } else {
216 g(0x00 | op_reg | (r & VT_VALMASK));
220 /* load 'r' from value 'sv' */
221 ST_FUNC void load(int r, SValue *sv)
223 int v, t, ft, fc, fr;
224 SValue v1;
226 #ifdef TCC_TARGET_PE
227 SValue v2;
228 sv = pe_getimport(sv, &v2);
229 #endif
231 fr = sv->r;
232 ft = sv->type.t;
233 fc = sv->c.i;
235 ft &= ~(VT_VOLATILE | VT_CONSTANT);
237 v = fr & VT_VALMASK;
238 if (fr & VT_LVAL) {
239 if (v == VT_LLOCAL) {
240 v1.type.t = VT_INT;
241 v1.r = VT_LOCAL | VT_LVAL;
242 v1.c.i = fc;
243 fr = r;
244 if (!(reg_classes[fr] & RC_INT))
245 fr = get_reg(RC_INT);
246 load(fr, &v1);
248 if ((ft & VT_BTYPE) == VT_FLOAT) {
249 o(0xd9); /* flds */
250 r = 0;
251 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
252 o(0xdd); /* fldl */
253 r = 0;
254 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
255 o(0xdb); /* fldt */
256 r = 5;
257 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
258 o(0xbe0f); /* movsbl */
259 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
260 o(0xb60f); /* movzbl */
261 } else if ((ft & VT_TYPE) == VT_SHORT) {
262 o(0xbf0f); /* movswl */
263 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
264 o(0xb70f); /* movzwl */
265 } else {
266 o(0x8b); /* movl */
268 gen_modrm(r, fr, sv->sym, fc);
269 } else {
270 if (v == VT_CONST) {
271 o(0xb8 + r); /* mov $xx, r */
272 gen_addr32(fr, sv->sym, fc);
273 } else if (v == VT_LOCAL) {
274 if (fc) {
275 o(0x8d); /* lea xxx(%ebp), r */
276 gen_modrm(r, VT_LOCAL, sv->sym, fc);
277 } else {
278 o(0x89);
279 o(0xe8 + r); /* mov %ebp, r */
281 } else if (v == VT_CMP) {
282 oad(0xb8 + r, 0); /* mov $0, r */
283 o(0x0f); /* setxx %br */
284 o(fc);
285 o(0xc0 + r);
286 } else if (v == VT_JMP || v == VT_JMPI) {
287 t = v & 1;
288 oad(0xb8 + r, t); /* mov $1, r */
289 o(0x05eb); /* jmp after */
290 gsym(fc);
291 oad(0xb8 + r, t ^ 1); /* mov $0, r */
292 } else if (v != r) {
293 o(0x89);
294 o(0xc0 + r + v * 8); /* mov v, r */
299 /* store register 'r' in lvalue 'v' */
300 ST_FUNC void store(int r, SValue *v)
302 int fr, bt, ft, fc;
304 #ifdef TCC_TARGET_PE
305 SValue v2;
306 v = pe_getimport(v, &v2);
307 #endif
309 ft = v->type.t;
310 fc = v->c.i;
311 fr = v->r & VT_VALMASK;
312 ft &= ~(VT_VOLATILE | VT_CONSTANT);
313 bt = ft & VT_BTYPE;
314 /* XXX: incorrect if float reg to reg */
315 if (bt == VT_FLOAT) {
316 o(0xd9); /* fsts */
317 r = 2;
318 } else if (bt == VT_DOUBLE) {
319 o(0xdd); /* fstpl */
320 r = 2;
321 } else if (bt == VT_LDOUBLE) {
322 o(0xc0d9); /* fld %st(0) */
323 o(0xdb); /* fstpt */
324 r = 7;
325 } else {
326 if (bt == VT_SHORT)
327 o(0x66);
328 if (bt == VT_BYTE || bt == VT_BOOL)
329 o(0x88);
330 else
331 o(0x89);
333 if (fr == VT_CONST ||
334 fr == VT_LOCAL ||
335 (v->r & VT_LVAL)) {
336 gen_modrm(r, v->r, v->sym, fc);
337 } else if (fr != r) {
338 o(0xc0 + fr + r * 8); /* mov r, fr */
342 static void gadd_sp(int val)
344 if (val == (char)val) {
345 o(0xc483);
346 g(val);
347 } else {
348 oad(0xc481, val); /* add $xxx, %esp */
352 static void gen_static_call(int v)
354 Sym *sym;
356 sym = external_global_sym(v, &func_old_type, 0);
357 oad(0xe8, -4);
358 greloc(cur_text_section, sym, ind-4, R_386_PC32);
361 /* 'is_jmp' is '1' if it is a jump */
362 static void gcall_or_jmp(int is_jmp)
364 int r;
365 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
366 int rt;
367 /* constant case */
368 if (vtop->r & VT_SYM) {
369 /* relocation case */
370 greloc(cur_text_section, vtop->sym,
371 ind + 1, R_386_PC32);
372 } else {
373 /* put an empty PC32 relocation */
374 put_elf_reloc(symtab_section, cur_text_section,
375 ind + 1, R_386_PC32, 0);
377 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
378 /* extend the return value to the whole register if necessary
379 visual studio and gcc do not always set the whole eax register
380 when assigning the return value of a function */
381 rt = vtop->type.ref->type.t;
382 switch (rt & VT_BTYPE) {
383 case VT_BYTE:
384 if (rt & VT_UNSIGNED) {
385 o(0xc0b60f); /* movzx %al, %eax */
387 else {
388 o(0xc0be0f); /* movsx %al, %eax */
390 break;
391 case VT_SHORT:
392 if (rt & VT_UNSIGNED) {
393 o(0xc0b70f); /* movzx %ax, %eax */
395 else {
396 o(0xc0bf0f); /* movsx %ax, %eax */
398 break;
399 default:
400 break;
402 } else {
403 /* otherwise, indirect call */
404 r = gv(RC_INT);
405 o(0xff); /* call/jmp *r */
406 o(0xd0 + r + (is_jmp << 4));
410 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
411 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
413 /* Return the number of registers needed to return the struct, or 0 if
414 returning via struct pointer. */
415 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
417 #ifdef TCC_TARGET_PE
418 int size, align;
420 *ret_align = 1; // Never have to re-align return values for x86
421 *regsize = 4;
422 size = type_size(vt, &align);
423 if (size > 8) {
424 return 0;
425 } else if (size > 4) {
426 ret->ref = NULL;
427 ret->t = VT_LLONG;
428 return 1;
429 } else {
430 ret->ref = NULL;
431 ret->t = VT_INT;
432 return 1;
434 #else
435 *ret_align = 1; // Never have to re-align return values for x86
436 return 0;
437 #endif
440 /* Generate function call. The function address is pushed first, then
441 all the parameters in call order. This functions pops all the
442 parameters and the function address. */
443 ST_FUNC void gfunc_call(int nb_args)
445 int size, align, r, args_size, i, func_call;
446 Sym *func_sym;
448 args_size = 0;
449 for(i = 0;i < nb_args; i++) {
450 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
451 size = type_size(&vtop->type, &align);
452 /* align to stack align size */
453 size = (size + 3) & ~3;
454 /* allocate the necessary size on stack */
455 oad(0xec81, size); /* sub $xxx, %esp */
456 /* generate structure store */
457 r = get_reg(RC_INT);
458 o(0x89); /* mov %esp, r */
459 o(0xe0 + r);
460 vset(&vtop->type, r | VT_LVAL, 0);
461 vswap();
462 vstore();
463 args_size += size;
464 } else if (is_float(vtop->type.t)) {
465 gv(RC_FLOAT); /* only one float register */
466 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
467 size = 4;
468 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
469 size = 8;
470 else
471 size = 12;
472 oad(0xec81, size); /* sub $xxx, %esp */
473 if (size == 12)
474 o(0x7cdb);
475 else
476 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
477 g(0x24);
478 g(0x00);
479 args_size += size;
480 } else {
481 /* simple type (currently always same size) */
482 /* XXX: implicit cast ? */
483 r = gv(RC_INT);
484 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
485 size = 8;
486 o(0x50 + vtop->r2); /* push r */
487 } else {
488 size = 4;
490 o(0x50 + r); /* push r */
491 args_size += size;
493 vtop--;
495 save_regs(0); /* save used temporary registers */
496 func_sym = vtop->type.ref;
497 func_call = func_sym->a.func_call;
498 /* fast call case */
499 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
500 func_call == FUNC_FASTCALLW) {
501 int fastcall_nb_regs;
502 uint8_t *fastcall_regs_ptr;
503 if (func_call == FUNC_FASTCALLW) {
504 fastcall_regs_ptr = fastcallw_regs;
505 fastcall_nb_regs = 2;
506 } else {
507 fastcall_regs_ptr = fastcall_regs;
508 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
510 for(i = 0;i < fastcall_nb_regs; i++) {
511 if (args_size <= 0)
512 break;
513 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
514 /* XXX: incorrect for struct/floats */
515 args_size -= 4;
518 #ifndef TCC_TARGET_PE
519 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
520 args_size -= 4;
521 #endif
522 gcall_or_jmp(0);
524 if (args_size && func_call != FUNC_STDCALL)
525 gadd_sp(args_size);
526 vtop--;
529 #ifdef TCC_TARGET_PE
530 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
531 #else
532 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
533 #endif
535 /* generate function prolog of type 't' */
536 ST_FUNC void gfunc_prolog(CType *func_type)
538 int addr, align, size, func_call, fastcall_nb_regs;
539 int param_index, param_addr;
540 uint8_t *fastcall_regs_ptr;
541 Sym *sym;
542 CType *type;
544 sym = func_type->ref;
545 func_call = sym->a.func_call;
546 addr = 8;
547 loc = 0;
548 func_vc = 0;
550 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
551 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
552 fastcall_regs_ptr = fastcall_regs;
553 } else if (func_call == FUNC_FASTCALLW) {
554 fastcall_nb_regs = 2;
555 fastcall_regs_ptr = fastcallw_regs;
556 } else {
557 fastcall_nb_regs = 0;
558 fastcall_regs_ptr = NULL;
560 param_index = 0;
562 ind += FUNC_PROLOG_SIZE;
563 func_sub_sp_offset = ind;
564 /* if the function returns a structure, then add an
565 implicit pointer parameter */
566 func_vt = sym->type;
567 func_var = (sym->c == FUNC_ELLIPSIS);
568 #ifdef TCC_TARGET_PE
569 size = type_size(&func_vt,&align);
570 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
571 #else
572 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
573 #endif
574 /* XXX: fastcall case ? */
575 func_vc = addr;
576 addr += 4;
577 param_index++;
579 /* define parameters */
580 while ((sym = sym->next) != NULL) {
581 type = &sym->type;
582 size = type_size(type, &align);
583 size = (size + 3) & ~3;
584 #ifdef FUNC_STRUCT_PARAM_AS_PTR
585 /* structs are passed as pointer */
586 if ((type->t & VT_BTYPE) == VT_STRUCT) {
587 size = 4;
589 #endif
590 if (param_index < fastcall_nb_regs) {
591 /* save FASTCALL register */
592 loc -= 4;
593 o(0x89); /* movl */
594 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
595 param_addr = loc;
596 } else {
597 param_addr = addr;
598 addr += size;
600 sym_push(sym->v & ~SYM_FIELD, type,
601 VT_LOCAL | lvalue_type(type->t), param_addr);
602 param_index++;
604 func_ret_sub = 0;
605 /* pascal type call ? */
606 if (func_call == FUNC_STDCALL)
607 func_ret_sub = addr - 8;
608 #ifndef TCC_TARGET_PE
609 else if (func_vc)
610 func_ret_sub = 4;
611 #endif
613 #ifdef CONFIG_TCC_BCHECK
614 /* leave some room for bound checking code */
615 if (tcc_state->do_bounds_check) {
616 func_bound_offset = lbounds_section->data_offset;
617 func_bound_ind = ind;
618 oad(0xb8, 0); /* lbound section pointer */
619 oad(0xb8, 0); /* call to function */
621 #endif
624 /* generate function epilog */
625 ST_FUNC void gfunc_epilog(void)
627 addr_t v, saved_ind;
629 #ifdef CONFIG_TCC_BCHECK
630 if (tcc_state->do_bounds_check
631 && func_bound_offset != lbounds_section->data_offset) {
632 addr_t saved_ind;
633 addr_t *bounds_ptr;
634 Sym *sym_data;
636 /* add end of table info */
637 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
638 *bounds_ptr = 0;
640 /* generate bound local allocation */
641 saved_ind = ind;
642 ind = func_bound_ind;
643 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
644 func_bound_offset, lbounds_section->data_offset);
645 greloc(cur_text_section, sym_data,
646 ind + 1, R_386_32);
647 oad(0xb8, 0); /* mov %eax, xxx */
648 gen_static_call(TOK___bound_local_new);
649 ind = saved_ind;
651 /* generate bound check local freeing */
652 o(0x5250); /* save returned value, if any */
653 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
654 oad(0xb8, 0); /* mov %eax, xxx */
655 gen_static_call(TOK___bound_local_delete);
656 o(0x585a); /* restore returned value, if any */
658 #endif
659 o(0x5b * USE_EBX); /* pop ebx */
660 o(0xc9); /* leave */
661 if (func_ret_sub == 0) {
662 o(0xc3); /* ret */
663 } else {
664 o(0xc2); /* ret n */
665 g(func_ret_sub);
666 g(func_ret_sub >> 8);
668 /* align local size to word & save local variables */
670 v = (-loc + 3) & -4;
671 saved_ind = ind;
672 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
673 #ifdef TCC_TARGET_PE
674 if (v >= 4096) {
675 oad(0xb8, v); /* mov stacksize, %eax */
676 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
677 } else
678 #endif
680 o(0xe58955); /* push %ebp, mov %esp, %ebp */
681 o(0xec81); /* sub esp, stacksize */
682 gen_le32(v);
683 #ifdef TCC_TARGET_PE
684 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
685 #endif
687 o(0x53 * USE_EBX); /* push ebx */
688 ind = saved_ind;
691 /* generate a jump to a label */
692 ST_FUNC int gjmp(int t)
694 return psym(0xe9, t);
697 /* generate a jump to a fixed address */
698 ST_FUNC void gjmp_addr(int a)
700 int r;
701 r = a - ind - 2;
702 if (r == (char)r) {
703 g(0xeb);
704 g(r);
705 } else {
706 oad(0xe9, a - ind - 5);
710 ST_FUNC void gtst_addr(int inv, int a)
712 inv ^= (vtop--)->c.i;
713 a -= ind + 2;
714 if (a == (char)a) {
715 g(inv - 32);
716 g(a);
717 } else {
718 g(0x0f);
719 oad(inv - 16, a - 4);
723 /* generate a test. set 'inv' to invert test. Stack entry is popped */
724 ST_FUNC int gtst(int inv, int t)
726 int v = vtop->r & VT_VALMASK;
727 if (v == VT_CMP) {
728 /* fast case : can jump directly since flags are set */
729 g(0x0f);
730 t = psym((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 /*************************************************************/