turn -fdollars-in-identifiers on by default
[tinycc.git] / i386-gen.c
blobda3c2e4e977161b885c30746784c8c34995e627d
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 ST_FUNC void gen_fill_nops(int bytes)
162 while (bytes--)
163 g(0x90);
166 /* generate jmp to a label */
167 #define gjmp2(instr,lbl) oad(instr,lbl)
169 /* output constant with relocation if 'r & VT_SYM' is true */
170 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
172 if (r & VT_SYM)
173 greloc(cur_text_section, sym, ind, R_386_32);
174 gen_le32(c);
177 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
179 if (r & VT_SYM)
180 greloc(cur_text_section, sym, ind, R_386_PC32);
181 gen_le32(c - 4);
184 /* generate a modrm reference. 'op_reg' contains the additional 3
185 opcode bits */
186 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
188 op_reg = op_reg << 3;
189 if ((r & VT_VALMASK) == VT_CONST) {
190 /* constant memory reference */
191 o(0x05 | op_reg);
192 gen_addr32(r, sym, c);
193 } else if ((r & VT_VALMASK) == VT_LOCAL) {
194 /* currently, we use only ebp as base */
195 if (c == (char)c) {
196 /* short reference */
197 o(0x45 | op_reg);
198 g(c);
199 } else {
200 oad(0x85 | op_reg, c);
202 } else {
203 g(0x00 | op_reg | (r & VT_VALMASK));
207 /* load 'r' from value 'sv' */
208 ST_FUNC void load(int r, SValue *sv)
210 int v, t, ft, fc, fr;
211 SValue v1;
213 #ifdef TCC_TARGET_PE
214 SValue v2;
215 sv = pe_getimport(sv, &v2);
216 #endif
218 fr = sv->r;
219 ft = sv->type.t & ~VT_DEFSIGN;
220 fc = sv->c.i;
222 ft &= ~(VT_VOLATILE | VT_CONSTANT);
224 v = fr & VT_VALMASK;
225 if (fr & VT_LVAL) {
226 if (v == VT_LLOCAL) {
227 v1.type.t = VT_INT;
228 v1.r = VT_LOCAL | VT_LVAL;
229 v1.c.i = fc;
230 fr = r;
231 if (!(reg_classes[fr] & RC_INT))
232 fr = get_reg(RC_INT);
233 load(fr, &v1);
235 if ((ft & VT_BTYPE) == VT_FLOAT) {
236 o(0xd9); /* flds */
237 r = 0;
238 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
239 o(0xdd); /* fldl */
240 r = 0;
241 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
242 o(0xdb); /* fldt */
243 r = 5;
244 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
245 o(0xbe0f); /* movsbl */
246 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
247 o(0xb60f); /* movzbl */
248 } else if ((ft & VT_TYPE) == VT_SHORT) {
249 o(0xbf0f); /* movswl */
250 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
251 o(0xb70f); /* movzwl */
252 } else {
253 o(0x8b); /* movl */
255 gen_modrm(r, fr, sv->sym, fc);
256 } else {
257 if (v == VT_CONST) {
258 o(0xb8 + r); /* mov $xx, r */
259 gen_addr32(fr, sv->sym, fc);
260 } else if (v == VT_LOCAL) {
261 if (fc) {
262 o(0x8d); /* lea xxx(%ebp), r */
263 gen_modrm(r, VT_LOCAL, sv->sym, fc);
264 } else {
265 o(0x89);
266 o(0xe8 + r); /* mov %ebp, r */
268 } else if (v == VT_CMP) {
269 oad(0xb8 + r, 0); /* mov $0, r */
270 o(0x0f); /* setxx %br */
271 o(fc);
272 o(0xc0 + r);
273 } else if (v == VT_JMP || v == VT_JMPI) {
274 t = v & 1;
275 oad(0xb8 + r, t); /* mov $1, r */
276 o(0x05eb); /* jmp after */
277 gsym(fc);
278 oad(0xb8 + r, t ^ 1); /* mov $0, r */
279 } else if (v != r) {
280 o(0x89);
281 o(0xc0 + r + v * 8); /* mov v, r */
286 /* store register 'r' in lvalue 'v' */
287 ST_FUNC void store(int r, SValue *v)
289 int fr, bt, ft, fc;
291 #ifdef TCC_TARGET_PE
292 SValue v2;
293 v = pe_getimport(v, &v2);
294 #endif
296 ft = v->type.t;
297 fc = v->c.i;
298 fr = v->r & VT_VALMASK;
299 ft &= ~(VT_VOLATILE | VT_CONSTANT);
300 bt = ft & VT_BTYPE;
301 /* XXX: incorrect if float reg to reg */
302 if (bt == VT_FLOAT) {
303 o(0xd9); /* fsts */
304 r = 2;
305 } else if (bt == VT_DOUBLE) {
306 o(0xdd); /* fstpl */
307 r = 2;
308 } else if (bt == VT_LDOUBLE) {
309 o(0xc0d9); /* fld %st(0) */
310 o(0xdb); /* fstpt */
311 r = 7;
312 } else {
313 if (bt == VT_SHORT)
314 o(0x66);
315 if (bt == VT_BYTE || bt == VT_BOOL)
316 o(0x88);
317 else
318 o(0x89);
320 if (fr == VT_CONST ||
321 fr == VT_LOCAL ||
322 (v->r & VT_LVAL)) {
323 gen_modrm(r, v->r, v->sym, fc);
324 } else if (fr != r) {
325 o(0xc0 + fr + r * 8); /* mov r, fr */
329 static void gadd_sp(int val)
331 if (val == (char)val) {
332 o(0xc483);
333 g(val);
334 } else {
335 oad(0xc481, val); /* add $xxx, %esp */
339 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
340 static void gen_static_call(int v)
342 Sym *sym;
344 sym = external_global_sym(v, &func_old_type);
345 oad(0xe8, -4);
346 greloc(cur_text_section, sym, ind-4, R_386_PC32);
348 #endif
350 /* 'is_jmp' is '1' if it is a jump */
351 static void gcall_or_jmp(int is_jmp)
353 int r;
354 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
355 /* constant and relocation case */
356 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
357 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
358 } else {
359 /* otherwise, indirect call */
360 r = gv(RC_INT);
361 o(0xff); /* call/jmp *r */
362 o(0xd0 + r + (is_jmp << 4));
364 if (!is_jmp) {
365 int rt;
366 /* extend the return value to the whole register if necessary
367 visual studio and gcc do not always set the whole eax register
368 when assigning the return value of a function */
369 rt = vtop->type.ref->type.t;
370 switch (rt & VT_BTYPE) {
371 case VT_BYTE:
372 case VT_BOOL:
373 if (rt & VT_UNSIGNED) {
374 o(0xc0b60f); /* movzx %al, %eax */
376 else {
377 o(0xc0be0f); /* movsx %al, %eax */
379 break;
380 case VT_SHORT:
381 if (rt & VT_UNSIGNED) {
382 o(0xc0b70f); /* movzx %ax, %eax */
384 else {
385 o(0xc0bf0f); /* movsx %ax, %eax */
387 break;
388 default:
389 break;
394 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
395 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
397 /* Return the number of registers needed to return the struct, or 0 if
398 returning via struct pointer. */
399 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
401 #ifdef TCC_TARGET_PE
402 int size, align;
403 *ret_align = 1; // Never have to re-align return values for x86
404 *regsize = 4;
405 size = type_size(vt, &align);
406 if (size > 8 || (size & (size - 1)))
407 return 0;
408 if (size == 8)
409 ret->t = VT_LLONG;
410 else if (size == 4)
411 ret->t = VT_INT;
412 else if (size == 2)
413 ret->t = VT_SHORT;
414 else
415 ret->t = VT_BYTE;
416 ret->ref = NULL;
417 return 1;
418 #else
419 *ret_align = 1; // Never have to re-align return values for x86
420 return 0;
421 #endif
424 /* Generate function call. The function address is pushed first, then
425 all the parameters in call order. This functions pops all the
426 parameters and the function address. */
427 ST_FUNC void gfunc_call(int nb_args)
429 int size, align, r, args_size, i, func_call;
430 Sym *func_sym;
432 args_size = 0;
433 for(i = 0;i < nb_args; i++) {
434 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
435 size = type_size(&vtop->type, &align);
436 /* align to stack align size */
437 size = (size + 3) & ~3;
438 /* allocate the necessary size on stack */
439 oad(0xec81, size); /* sub $xxx, %esp */
440 /* generate structure store */
441 r = get_reg(RC_INT);
442 o(0x89); /* mov %esp, r */
443 o(0xe0 + r);
444 vset(&vtop->type, r | VT_LVAL, 0);
445 vswap();
446 vstore();
447 args_size += size;
448 } else if (is_float(vtop->type.t)) {
449 gv(RC_FLOAT); /* only one float register */
450 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
451 size = 4;
452 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
453 size = 8;
454 else
455 size = 12;
456 oad(0xec81, size); /* sub $xxx, %esp */
457 if (size == 12)
458 o(0x7cdb);
459 else
460 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
461 g(0x24);
462 g(0x00);
463 args_size += size;
464 } else {
465 /* simple type (currently always same size) */
466 /* XXX: implicit cast ? */
467 r = gv(RC_INT);
468 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
469 size = 8;
470 o(0x50 + vtop->r2); /* push r */
471 } else {
472 size = 4;
474 o(0x50 + r); /* push r */
475 args_size += size;
477 vtop--;
479 save_regs(0); /* save used temporary registers */
480 func_sym = vtop->type.ref;
481 func_call = func_sym->f.func_call;
482 /* fast call case */
483 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
484 func_call == FUNC_FASTCALLW) {
485 int fastcall_nb_regs;
486 uint8_t *fastcall_regs_ptr;
487 if (func_call == FUNC_FASTCALLW) {
488 fastcall_regs_ptr = fastcallw_regs;
489 fastcall_nb_regs = 2;
490 } else {
491 fastcall_regs_ptr = fastcall_regs;
492 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
494 for(i = 0;i < fastcall_nb_regs; i++) {
495 if (args_size <= 0)
496 break;
497 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
498 /* XXX: incorrect for struct/floats */
499 args_size -= 4;
502 #ifndef TCC_TARGET_PE
503 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
504 args_size -= 4;
505 #endif
506 gcall_or_jmp(0);
508 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
509 gadd_sp(args_size);
510 vtop--;
513 #ifdef TCC_TARGET_PE
514 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
515 #else
516 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
517 #endif
519 /* generate function prolog of type 't' */
520 ST_FUNC void gfunc_prolog(CType *func_type)
522 int addr, align, size, func_call, fastcall_nb_regs;
523 int param_index, param_addr;
524 uint8_t *fastcall_regs_ptr;
525 Sym *sym;
526 CType *type;
528 sym = func_type->ref;
529 func_call = sym->f.func_call;
530 addr = 8;
531 loc = 0;
532 func_vc = 0;
534 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
535 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
536 fastcall_regs_ptr = fastcall_regs;
537 } else if (func_call == FUNC_FASTCALLW) {
538 fastcall_nb_regs = 2;
539 fastcall_regs_ptr = fastcallw_regs;
540 } else {
541 fastcall_nb_regs = 0;
542 fastcall_regs_ptr = NULL;
544 param_index = 0;
546 ind += FUNC_PROLOG_SIZE;
547 func_sub_sp_offset = ind;
548 /* if the function returns a structure, then add an
549 implicit pointer parameter */
550 func_vt = sym->type;
551 func_var = (sym->f.func_type == FUNC_ELLIPSIS);
552 #ifdef TCC_TARGET_PE
553 size = type_size(&func_vt,&align);
554 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
555 && (size > 8 || (size & (size - 1)))) {
556 #else
557 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
558 #endif
559 /* XXX: fastcall case ? */
560 func_vc = addr;
561 addr += 4;
562 param_index++;
564 /* define parameters */
565 while ((sym = sym->next) != NULL) {
566 type = &sym->type;
567 size = type_size(type, &align);
568 size = (size + 3) & ~3;
569 #ifdef FUNC_STRUCT_PARAM_AS_PTR
570 /* structs are passed as pointer */
571 if ((type->t & VT_BTYPE) == VT_STRUCT) {
572 size = 4;
574 #endif
575 if (param_index < fastcall_nb_regs) {
576 /* save FASTCALL register */
577 loc -= 4;
578 o(0x89); /* movl */
579 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
580 param_addr = loc;
581 } else {
582 param_addr = addr;
583 addr += size;
585 sym_push(sym->v & ~SYM_FIELD, type,
586 VT_LOCAL | lvalue_type(type->t), param_addr);
587 param_index++;
589 func_ret_sub = 0;
590 /* pascal type call or fastcall ? */
591 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
592 func_ret_sub = addr - 8;
593 #ifndef TCC_TARGET_PE
594 else if (func_vc)
595 func_ret_sub = 4;
596 #endif
598 #ifdef CONFIG_TCC_BCHECK
599 /* leave some room for bound checking code */
600 if (tcc_state->do_bounds_check) {
601 func_bound_offset = lbounds_section->data_offset;
602 func_bound_ind = ind;
603 oad(0xb8, 0); /* lbound section pointer */
604 oad(0xb8, 0); /* call to function */
606 #endif
609 /* generate function epilog */
610 ST_FUNC void gfunc_epilog(void)
612 addr_t v, saved_ind;
614 #ifdef CONFIG_TCC_BCHECK
615 if (tcc_state->do_bounds_check
616 && func_bound_offset != lbounds_section->data_offset) {
617 addr_t saved_ind;
618 addr_t *bounds_ptr;
619 Sym *sym_data;
621 /* add end of table info */
622 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
623 *bounds_ptr = 0;
625 /* generate bound local allocation */
626 saved_ind = ind;
627 ind = func_bound_ind;
628 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
629 func_bound_offset, lbounds_section->data_offset);
630 greloc(cur_text_section, sym_data,
631 ind + 1, R_386_32);
632 oad(0xb8, 0); /* mov %eax, xxx */
633 gen_static_call(TOK___bound_local_new);
634 ind = saved_ind;
636 /* generate bound check local freeing */
637 o(0x5250); /* save returned value, if any */
638 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
639 oad(0xb8, 0); /* mov %eax, xxx */
640 gen_static_call(TOK___bound_local_delete);
641 o(0x585a); /* restore returned value, if any */
643 #endif
645 /* align local size to word & save local variables */
646 v = (-loc + 3) & -4;
648 #if USE_EBX
649 o(0x8b);
650 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
651 #endif
653 o(0xc9); /* leave */
654 if (func_ret_sub == 0) {
655 o(0xc3); /* ret */
656 } else {
657 o(0xc2); /* ret n */
658 g(func_ret_sub);
659 g(func_ret_sub >> 8);
661 saved_ind = ind;
662 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
663 #ifdef TCC_TARGET_PE
664 if (v >= 4096) {
665 oad(0xb8, v); /* mov stacksize, %eax */
666 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
667 } else
668 #endif
670 o(0xe58955); /* push %ebp, mov %esp, %ebp */
671 o(0xec81); /* sub esp, stacksize */
672 gen_le32(v);
673 #ifdef TCC_TARGET_PE
674 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
675 #endif
677 o(0x53 * USE_EBX); /* push ebx */
678 ind = saved_ind;
681 /* generate a jump to a label */
682 ST_FUNC int gjmp(int t)
684 return gjmp2(0xe9, t);
687 /* generate a jump to a fixed address */
688 ST_FUNC void gjmp_addr(int a)
690 int r;
691 r = a - ind - 2;
692 if (r == (char)r) {
693 g(0xeb);
694 g(r);
695 } else {
696 oad(0xe9, a - ind - 5);
700 ST_FUNC void gtst_addr(int inv, int a)
702 int v = vtop->r & VT_VALMASK;
703 if (v == VT_CMP) {
704 inv ^= (vtop--)->c.i;
705 a -= ind + 2;
706 if (a == (char)a) {
707 g(inv - 32);
708 g(a);
709 } else {
710 g(0x0f);
711 oad(inv - 16, a - 4);
713 } else if ((v & ~1) == VT_JMP) {
714 if ((v & 1) != inv) {
715 gjmp_addr(a);
716 gsym(vtop->c.i);
717 } else {
718 gsym(vtop->c.i);
719 o(0x05eb);
720 gjmp_addr(a);
722 vtop--;
726 /* generate a test. set 'inv' to invert test. Stack entry is popped */
727 ST_FUNC int gtst(int inv, int t)
729 int v = vtop->r & VT_VALMASK;
730 if (nocode_wanted) {
732 } else if (v == VT_CMP) {
733 /* fast case : can jump directly since flags are set */
734 g(0x0f);
735 t = gjmp2((vtop->c.i - 16) ^ inv, t);
736 } else if (v == VT_JMP || v == VT_JMPI) {
737 /* && or || optimization */
738 if ((v & 1) == inv) {
739 /* insert vtop->c jump list in t */
740 uint32_t n1, n = vtop->c.i;
741 if (n) {
742 while ((n1 = read32le(cur_text_section->data + n)))
743 n = n1;
744 write32le(cur_text_section->data + n, t);
745 t = vtop->c.i;
747 } else {
748 t = gjmp(t);
749 gsym(vtop->c.i);
752 vtop--;
753 return t;
756 /* generate an integer binary operation */
757 ST_FUNC void gen_opi(int op)
759 int r, fr, opc, c;
761 switch(op) {
762 case '+':
763 case TOK_ADDC1: /* add with carry generation */
764 opc = 0;
765 gen_op8:
766 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
767 /* constant case */
768 vswap();
769 r = gv(RC_INT);
770 vswap();
771 c = vtop->c.i;
772 if (c == (char)c) {
773 /* generate inc and dec for smaller code */
774 if (c==1 && opc==0 && op != TOK_ADDC1) {
775 o (0x40 | r); // inc
776 } else if (c==1 && opc==5 && op != TOK_SUBC1) {
777 o (0x48 | r); // dec
778 } else {
779 o(0x83);
780 o(0xc0 | (opc << 3) | r);
781 g(c);
783 } else {
784 o(0x81);
785 oad(0xc0 | (opc << 3) | r, c);
787 } else {
788 gv2(RC_INT, RC_INT);
789 r = vtop[-1].r;
790 fr = vtop[0].r;
791 o((opc << 3) | 0x01);
792 o(0xc0 + r + fr * 8);
794 vtop--;
795 if (op >= TOK_ULT && op <= TOK_GT) {
796 vtop->r = VT_CMP;
797 vtop->c.i = op;
799 break;
800 case '-':
801 case TOK_SUBC1: /* sub with carry generation */
802 opc = 5;
803 goto gen_op8;
804 case TOK_ADDC2: /* add with carry use */
805 opc = 2;
806 goto gen_op8;
807 case TOK_SUBC2: /* sub with carry use */
808 opc = 3;
809 goto gen_op8;
810 case '&':
811 opc = 4;
812 goto gen_op8;
813 case '^':
814 opc = 6;
815 goto gen_op8;
816 case '|':
817 opc = 1;
818 goto gen_op8;
819 case '*':
820 gv2(RC_INT, RC_INT);
821 r = vtop[-1].r;
822 fr = vtop[0].r;
823 vtop--;
824 o(0xaf0f); /* imul fr, r */
825 o(0xc0 + fr + r * 8);
826 break;
827 case TOK_SHL:
828 opc = 4;
829 goto gen_shift;
830 case TOK_SHR:
831 opc = 5;
832 goto gen_shift;
833 case TOK_SAR:
834 opc = 7;
835 gen_shift:
836 opc = 0xc0 | (opc << 3);
837 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
838 /* constant case */
839 vswap();
840 r = gv(RC_INT);
841 vswap();
842 c = vtop->c.i & 0x1f;
843 o(0xc1); /* shl/shr/sar $xxx, r */
844 o(opc | r);
845 g(c);
846 } else {
847 /* we generate the shift in ecx */
848 gv2(RC_INT, RC_ECX);
849 r = vtop[-1].r;
850 o(0xd3); /* shl/shr/sar %cl, r */
851 o(opc | r);
853 vtop--;
854 break;
855 case '/':
856 case TOK_UDIV:
857 case TOK_PDIV:
858 case '%':
859 case TOK_UMOD:
860 case TOK_UMULL:
861 /* first operand must be in eax */
862 /* XXX: need better constraint for second operand */
863 gv2(RC_EAX, RC_ECX);
864 r = vtop[-1].r;
865 fr = vtop[0].r;
866 vtop--;
867 save_reg(TREG_EDX);
868 /* save EAX too if used otherwise */
869 save_reg_upstack(TREG_EAX, 1);
870 if (op == TOK_UMULL) {
871 o(0xf7); /* mul fr */
872 o(0xe0 + fr);
873 vtop->r2 = TREG_EDX;
874 r = TREG_EAX;
875 } else {
876 if (op == TOK_UDIV || op == TOK_UMOD) {
877 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
878 o(0xf0 + fr);
879 } else {
880 o(0xf799); /* cltd, idiv fr, %eax */
881 o(0xf8 + fr);
883 if (op == '%' || op == TOK_UMOD)
884 r = TREG_EDX;
885 else
886 r = TREG_EAX;
888 vtop->r = r;
889 break;
890 default:
891 opc = 7;
892 goto gen_op8;
896 /* generate a floating point operation 'v = t1 op t2' instruction. The
897 two operands are guaranteed to have the same floating point type */
898 /* XXX: need to use ST1 too */
899 ST_FUNC void gen_opf(int op)
901 int a, ft, fc, swapped, r;
903 /* convert constants to memory references */
904 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
905 vswap();
906 gv(RC_FLOAT);
907 vswap();
909 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
910 gv(RC_FLOAT);
912 /* must put at least one value in the floating point register */
913 if ((vtop[-1].r & VT_LVAL) &&
914 (vtop[0].r & VT_LVAL)) {
915 vswap();
916 gv(RC_FLOAT);
917 vswap();
919 swapped = 0;
920 /* swap the stack if needed so that t1 is the register and t2 is
921 the memory reference */
922 if (vtop[-1].r & VT_LVAL) {
923 vswap();
924 swapped = 1;
926 if (op >= TOK_ULT && op <= TOK_GT) {
927 /* load on stack second operand */
928 load(TREG_ST0, vtop);
929 save_reg(TREG_EAX); /* eax is used by FP comparison code */
930 if (op == TOK_GE || op == TOK_GT)
931 swapped = !swapped;
932 else if (op == TOK_EQ || op == TOK_NE)
933 swapped = 0;
934 if (swapped)
935 o(0xc9d9); /* fxch %st(1) */
936 if (op == TOK_EQ || op == TOK_NE)
937 o(0xe9da); /* fucompp */
938 else
939 o(0xd9de); /* fcompp */
940 o(0xe0df); /* fnstsw %ax */
941 if (op == TOK_EQ) {
942 o(0x45e480); /* and $0x45, %ah */
943 o(0x40fC80); /* cmp $0x40, %ah */
944 } else if (op == TOK_NE) {
945 o(0x45e480); /* and $0x45, %ah */
946 o(0x40f480); /* xor $0x40, %ah */
947 op = TOK_NE;
948 } else if (op == TOK_GE || op == TOK_LE) {
949 o(0x05c4f6); /* test $0x05, %ah */
950 op = TOK_EQ;
951 } else {
952 o(0x45c4f6); /* test $0x45, %ah */
953 op = TOK_EQ;
955 vtop--;
956 vtop->r = VT_CMP;
957 vtop->c.i = op;
958 } else {
959 /* no memory reference possible for long double operations */
960 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
961 load(TREG_ST0, vtop);
962 swapped = !swapped;
965 switch(op) {
966 default:
967 case '+':
968 a = 0;
969 break;
970 case '-':
971 a = 4;
972 if (swapped)
973 a++;
974 break;
975 case '*':
976 a = 1;
977 break;
978 case '/':
979 a = 6;
980 if (swapped)
981 a++;
982 break;
984 ft = vtop->type.t;
985 fc = vtop->c.i;
986 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
987 o(0xde); /* fxxxp %st, %st(1) */
988 o(0xc1 + (a << 3));
989 } else {
990 /* if saved lvalue, then we must reload it */
991 r = vtop->r;
992 if ((r & VT_VALMASK) == VT_LLOCAL) {
993 SValue v1;
994 r = get_reg(RC_INT);
995 v1.type.t = VT_INT;
996 v1.r = VT_LOCAL | VT_LVAL;
997 v1.c.i = fc;
998 load(r, &v1);
999 fc = 0;
1002 if ((ft & VT_BTYPE) == VT_DOUBLE)
1003 o(0xdc);
1004 else
1005 o(0xd8);
1006 gen_modrm(a, r, vtop->sym, fc);
1008 vtop--;
1012 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1013 and 'long long' cases. */
1014 ST_FUNC void gen_cvt_itof(int t)
1016 save_reg(TREG_ST0);
1017 gv(RC_INT);
1018 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1019 /* signed long long to float/double/long double (unsigned case
1020 is handled generically) */
1021 o(0x50 + vtop->r2); /* push r2 */
1022 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1023 o(0x242cdf); /* fildll (%esp) */
1024 o(0x08c483); /* add $8, %esp */
1025 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1026 (VT_INT | VT_UNSIGNED)) {
1027 /* unsigned int to float/double/long double */
1028 o(0x6a); /* push $0 */
1029 g(0x00);
1030 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1031 o(0x242cdf); /* fildll (%esp) */
1032 o(0x08c483); /* add $8, %esp */
1033 } else {
1034 /* int to float/double/long double */
1035 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1036 o(0x2404db); /* fildl (%esp) */
1037 o(0x04c483); /* add $4, %esp */
1039 vtop->r = TREG_ST0;
1042 /* convert fp to int 't' type */
1043 ST_FUNC void gen_cvt_ftoi(int t)
1045 int bt = vtop->type.t & VT_BTYPE;
1046 if (bt == VT_FLOAT)
1047 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1048 else if (bt == VT_LDOUBLE)
1049 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1050 else
1051 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1052 vswap();
1053 gfunc_call(1);
1054 vpushi(0);
1055 vtop->r = REG_IRET;
1056 vtop->r2 = REG_LRET;
1059 /* convert from one floating point type to another */
1060 ST_FUNC void gen_cvt_ftof(int t)
1062 /* all we have to do on i386 is to put the float in a register */
1063 gv(RC_FLOAT);
1066 /* computed goto support */
1067 ST_FUNC void ggoto(void)
1069 gcall_or_jmp(1);
1070 vtop--;
1073 /* bound check support functions */
1074 #ifdef CONFIG_TCC_BCHECK
1076 /* generate a bounded pointer addition */
1077 ST_FUNC void gen_bounded_ptr_add(void)
1079 /* prepare fast i386 function call (args in eax and edx) */
1080 gv2(RC_EAX, RC_EDX);
1081 /* save all temporary registers */
1082 vtop -= 2;
1083 save_regs(0);
1084 /* do a fast function call */
1085 gen_static_call(TOK___bound_ptr_add);
1086 /* returned pointer is in eax */
1087 vtop++;
1088 vtop->r = TREG_EAX | VT_BOUNDED;
1089 /* address of bounding function call point */
1090 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1093 /* patch pointer addition in vtop so that pointer dereferencing is
1094 also tested */
1095 ST_FUNC void gen_bounded_ptr_deref(void)
1097 addr_t func;
1098 int size, align;
1099 Elf32_Rel *rel;
1100 Sym *sym;
1102 size = 0;
1103 /* XXX: put that code in generic part of tcc */
1104 if (!is_float(vtop->type.t)) {
1105 if (vtop->r & VT_LVAL_BYTE)
1106 size = 1;
1107 else if (vtop->r & VT_LVAL_SHORT)
1108 size = 2;
1110 if (!size)
1111 size = type_size(&vtop->type, &align);
1112 switch(size) {
1113 case 1: func = TOK___bound_ptr_indir1; break;
1114 case 2: func = TOK___bound_ptr_indir2; break;
1115 case 4: func = TOK___bound_ptr_indir4; break;
1116 case 8: func = TOK___bound_ptr_indir8; break;
1117 case 12: func = TOK___bound_ptr_indir12; break;
1118 case 16: func = TOK___bound_ptr_indir16; break;
1119 default:
1120 tcc_error("unhandled size when dereferencing bounded pointer");
1121 func = 0;
1122 break;
1125 /* patch relocation */
1126 /* XXX: find a better solution ? */
1127 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1128 sym = external_global_sym(func, &func_old_type);
1129 if (!sym->c)
1130 put_extern_sym(sym, NULL, 0, 0);
1131 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1133 #endif
1135 /* Save the stack pointer onto the stack */
1136 ST_FUNC void gen_vla_sp_save(int addr) {
1137 /* mov %esp,addr(%ebp)*/
1138 o(0x89);
1139 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1142 /* Restore the SP from a location on the stack */
1143 ST_FUNC void gen_vla_sp_restore(int addr) {
1144 o(0x8b);
1145 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1148 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1149 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1150 #ifdef TCC_TARGET_PE
1151 /* alloca does more than just adjust %rsp on Windows */
1152 vpush_global_sym(&func_old_type, TOK_alloca);
1153 vswap(); /* Move alloca ref past allocation size */
1154 gfunc_call(1);
1155 #else
1156 int r;
1157 r = gv(RC_INT); /* allocation size */
1158 /* sub r,%rsp */
1159 o(0x2b);
1160 o(0xe0 | r);
1161 /* We align to 16 bytes rather than align */
1162 /* and ~15, %esp */
1163 o(0xf0e483);
1164 vpop();
1165 #endif
1168 /* end of X86 code generator */
1169 /*************************************************************/
1170 #endif
1171 /*************************************************************/