bcheck cleanup
[tinycc.git] / i386-gen.c
blob6ff096ad34f20c8202f2ad40886787029b7d0842
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 #define USING_GLOBALS
78 #include "tcc.h"
80 /* define to 1/0 to [not] have EBX as 4th register */
81 #define USE_EBX 0
83 ST_DATA const int reg_classes[NB_REGS] = {
84 /* eax */ RC_INT | RC_EAX,
85 /* ecx */ RC_INT | RC_ECX,
86 /* edx */ RC_INT | RC_EDX,
87 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
88 /* st0 */ RC_FLOAT | RC_ST0,
91 static unsigned long func_sub_sp_offset;
92 static int func_ret_sub;
93 #ifdef CONFIG_TCC_BCHECK
94 static addr_t func_bound_offset;
95 static unsigned long func_bound_ind;
96 #endif
98 /* XXX: make it faster ? */
99 ST_FUNC void g(int c)
101 int ind1;
102 if (nocode_wanted)
103 return;
104 ind1 = ind + 1;
105 if (ind1 > cur_text_section->data_allocated)
106 section_realloc(cur_text_section, ind1);
107 cur_text_section->data[ind] = c;
108 ind = ind1;
111 ST_FUNC void o(unsigned int c)
113 while (c) {
114 g(c);
115 c = c >> 8;
119 ST_FUNC void gen_le16(int v)
121 g(v);
122 g(v >> 8);
125 ST_FUNC void gen_le32(int c)
127 g(c);
128 g(c >> 8);
129 g(c >> 16);
130 g(c >> 24);
133 /* output a symbol and patch all calls to it */
134 ST_FUNC void gsym_addr(int t, int a)
136 while (t) {
137 unsigned char *ptr = cur_text_section->data + t;
138 uint32_t n = read32le(ptr); /* next value */
139 write32le(ptr, a - t - 4);
140 t = n;
144 /* instruction + 4 bytes data. Return the address of the data */
145 static int oad(int c, int s)
147 int t;
148 if (nocode_wanted)
149 return s;
150 o(c);
151 t = ind;
152 gen_le32(s);
153 return t;
156 ST_FUNC void gen_fill_nops(int bytes)
158 while (bytes--)
159 g(0x90);
162 /* generate jmp to a label */
163 #define gjmp2(instr,lbl) oad(instr,lbl)
165 /* output constant with relocation if 'r & VT_SYM' is true */
166 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
168 if (r & VT_SYM)
169 greloc(cur_text_section, sym, ind, R_386_32);
170 gen_le32(c);
173 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
175 if (r & VT_SYM)
176 greloc(cur_text_section, sym, ind, R_386_PC32);
177 gen_le32(c - 4);
180 /* generate a modrm reference. 'op_reg' contains the additional 3
181 opcode bits */
182 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
184 op_reg = op_reg << 3;
185 if ((r & VT_VALMASK) == VT_CONST) {
186 /* constant memory reference */
187 o(0x05 | op_reg);
188 gen_addr32(r, sym, c);
189 } else if ((r & VT_VALMASK) == VT_LOCAL) {
190 /* currently, we use only ebp as base */
191 if (c == (char)c) {
192 /* short reference */
193 o(0x45 | op_reg);
194 g(c);
195 } else {
196 oad(0x85 | op_reg, c);
198 } else {
199 g(0x00 | op_reg | (r & VT_VALMASK));
203 /* load 'r' from value 'sv' */
204 ST_FUNC void load(int r, SValue *sv)
206 int v, t, ft, fc, fr;
207 SValue v1;
209 #ifdef TCC_TARGET_PE
210 SValue v2;
211 sv = pe_getimport(sv, &v2);
212 #endif
214 fr = sv->r;
215 ft = sv->type.t & ~VT_DEFSIGN;
216 fc = sv->c.i;
218 ft &= ~(VT_VOLATILE | VT_CONSTANT);
220 v = fr & VT_VALMASK;
221 if (fr & VT_LVAL) {
222 if (v == VT_LLOCAL) {
223 v1.type.t = VT_INT;
224 v1.r = VT_LOCAL | VT_LVAL;
225 v1.c.i = fc;
226 fr = r;
227 if (!(reg_classes[fr] & RC_INT))
228 fr = get_reg(RC_INT);
229 load(fr, &v1);
231 if ((ft & VT_BTYPE) == VT_FLOAT) {
232 o(0xd9); /* flds */
233 r = 0;
234 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
235 o(0xdd); /* fldl */
236 r = 0;
237 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
238 o(0xdb); /* fldt */
239 r = 5;
240 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
241 o(0xbe0f); /* movsbl */
242 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
243 o(0xb60f); /* movzbl */
244 } else if ((ft & VT_TYPE) == VT_SHORT) {
245 o(0xbf0f); /* movswl */
246 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
247 o(0xb70f); /* movzwl */
248 } else {
249 o(0x8b); /* movl */
251 gen_modrm(r, fr, sv->sym, fc);
252 } else {
253 if (v == VT_CONST) {
254 o(0xb8 + r); /* mov $xx, r */
255 gen_addr32(fr, sv->sym, fc);
256 } else if (v == VT_LOCAL) {
257 if (fc) {
258 o(0x8d); /* lea xxx(%ebp), r */
259 gen_modrm(r, VT_LOCAL, sv->sym, fc);
260 } else {
261 o(0x89);
262 o(0xe8 + r); /* mov %ebp, r */
264 } else if (v == VT_CMP) {
265 o(0x0f); /* setxx %br */
266 o(fc);
267 o(0xc0 + r);
268 o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
269 } else if (v == VT_JMP || v == VT_JMPI) {
270 t = v & 1;
271 oad(0xb8 + r, t); /* mov $1, r */
272 o(0x05eb); /* jmp after */
273 gsym(fc);
274 oad(0xb8 + r, t ^ 1); /* mov $0, r */
275 } else if (v != r) {
276 o(0x89);
277 o(0xc0 + r + v * 8); /* mov v, r */
282 /* store register 'r' in lvalue 'v' */
283 ST_FUNC void store(int r, SValue *v)
285 int fr, bt, ft, fc;
287 #ifdef TCC_TARGET_PE
288 SValue v2;
289 v = pe_getimport(v, &v2);
290 #endif
292 ft = v->type.t;
293 fc = v->c.i;
294 fr = v->r & VT_VALMASK;
295 ft &= ~(VT_VOLATILE | VT_CONSTANT);
296 bt = ft & VT_BTYPE;
297 /* XXX: incorrect if float reg to reg */
298 if (bt == VT_FLOAT) {
299 o(0xd9); /* fsts */
300 r = 2;
301 } else if (bt == VT_DOUBLE) {
302 o(0xdd); /* fstpl */
303 r = 2;
304 } else if (bt == VT_LDOUBLE) {
305 o(0xc0d9); /* fld %st(0) */
306 o(0xdb); /* fstpt */
307 r = 7;
308 } else {
309 if (bt == VT_SHORT)
310 o(0x66);
311 if (bt == VT_BYTE || bt == VT_BOOL)
312 o(0x88);
313 else
314 o(0x89);
316 if (fr == VT_CONST ||
317 fr == VT_LOCAL ||
318 (v->r & VT_LVAL)) {
319 gen_modrm(r, v->r, v->sym, fc);
320 } else if (fr != r) {
321 o(0xc0 + fr + r * 8); /* mov r, fr */
325 static void gadd_sp(int val)
327 if (val == (char)val) {
328 o(0xc483);
329 g(val);
330 } else {
331 oad(0xc481, val); /* add $xxx, %esp */
335 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
336 static void gen_static_call(int v)
338 Sym *sym;
340 sym = external_global_sym(v, &func_old_type);
341 oad(0xe8, -4);
342 greloc(cur_text_section, sym, ind-4, R_386_PC32);
344 #endif
346 /* 'is_jmp' is '1' if it is a jump */
347 static void gcall_or_jmp(int is_jmp)
349 int r;
350 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
351 /* constant and relocation case */
352 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
353 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
354 } else {
355 /* otherwise, indirect call */
356 r = gv(RC_INT);
357 o(0xff); /* call/jmp *r */
358 o(0xd0 + r + (is_jmp << 4));
360 if (!is_jmp) {
361 int rt;
362 /* extend the return value to the whole register if necessary
363 visual studio and gcc do not always set the whole eax register
364 when assigning the return value of a function */
365 rt = vtop->type.ref->type.t;
366 switch (rt & VT_BTYPE) {
367 case VT_BYTE:
368 case VT_BOOL:
369 if (rt & VT_UNSIGNED) {
370 o(0xc0b60f); /* movzx %al, %eax */
372 else {
373 o(0xc0be0f); /* movsx %al, %eax */
375 break;
376 case VT_SHORT:
377 if (rt & VT_UNSIGNED) {
378 o(0xc0b70f); /* movzx %ax, %eax */
380 else {
381 o(0xc0bf0f); /* movsx %ax, %eax */
383 break;
384 default:
385 break;
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 #ifdef CONFIG_TCC_BCHECK
429 if (tcc_state->do_bounds_check)
430 gbound_args(nb_args);
431 #endif
433 args_size = 0;
434 for(i = 0;i < nb_args; i++) {
435 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
436 size = type_size(&vtop->type, &align);
437 /* align to stack align size */
438 size = (size + 3) & ~3;
439 /* allocate the necessary size on stack */
440 oad(0xec81, size); /* sub $xxx, %esp */
441 /* generate structure store */
442 r = get_reg(RC_INT);
443 o(0x89); /* mov %esp, r */
444 o(0xe0 + r);
445 vset(&vtop->type, r | VT_LVAL, 0);
446 vswap();
447 vstore();
448 args_size += size;
449 } else if (is_float(vtop->type.t)) {
450 gv(RC_FLOAT); /* only one float register */
451 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
452 size = 4;
453 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
454 size = 8;
455 else
456 size = 12;
457 oad(0xec81, size); /* sub $xxx, %esp */
458 if (size == 12)
459 o(0x7cdb);
460 else
461 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
462 g(0x24);
463 g(0x00);
464 args_size += size;
465 } else {
466 /* simple type (currently always same size) */
467 /* XXX: implicit cast ? */
468 r = gv(RC_INT);
469 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
470 size = 8;
471 o(0x50 + vtop->r2); /* push r */
472 } else {
473 size = 4;
475 o(0x50 + r); /* push r */
476 args_size += size;
478 vtop--;
480 save_regs(0); /* save used temporary registers */
481 func_sym = vtop->type.ref;
482 func_call = func_sym->f.func_call;
483 /* fast call case */
484 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
485 func_call == FUNC_FASTCALLW) {
486 int fastcall_nb_regs;
487 uint8_t *fastcall_regs_ptr;
488 if (func_call == FUNC_FASTCALLW) {
489 fastcall_regs_ptr = fastcallw_regs;
490 fastcall_nb_regs = 2;
491 } else {
492 fastcall_regs_ptr = fastcall_regs;
493 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
495 for(i = 0;i < fastcall_nb_regs; i++) {
496 if (args_size <= 0)
497 break;
498 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
499 /* XXX: incorrect for struct/floats */
500 args_size -= 4;
503 #ifndef TCC_TARGET_PE
504 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
505 args_size -= 4;
506 #endif
507 gcall_or_jmp(0);
509 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
510 gadd_sp(args_size);
511 vtop--;
514 #ifdef TCC_TARGET_PE
515 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
516 #else
517 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
518 #endif
520 /* generate function prolog of type 't' */
521 ST_FUNC void gfunc_prolog(Sym *func_sym)
523 CType *func_type = &func_sym->type;
524 int addr, align, size, func_call, fastcall_nb_regs;
525 int param_index, param_addr;
526 int n_arg = 0;
527 uint8_t *fastcall_regs_ptr;
528 Sym *sym;
529 CType *type;
531 sym = func_type->ref;
532 func_call = sym->f.func_call;
533 addr = 8;
534 loc = 0;
535 func_vc = 0;
537 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
538 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
539 fastcall_regs_ptr = fastcall_regs;
540 } else if (func_call == FUNC_FASTCALLW) {
541 fastcall_nb_regs = 2;
542 fastcall_regs_ptr = fastcallw_regs;
543 } else {
544 fastcall_nb_regs = 0;
545 fastcall_regs_ptr = NULL;
547 param_index = 0;
549 ind += FUNC_PROLOG_SIZE;
550 func_sub_sp_offset = ind;
551 /* if the function returns a structure, then add an
552 implicit pointer parameter */
553 func_vt = sym->type;
554 func_var = (sym->f.func_type == FUNC_ELLIPSIS);
555 #ifdef TCC_TARGET_PE
556 size = type_size(&func_vt,&align);
557 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
558 && (size > 8 || (size & (size - 1)))) {
559 #else
560 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
561 #endif
562 /* XXX: fastcall case ? */
563 func_vc = addr;
564 addr += 4;
565 param_index++;
567 /* define parameters */
568 while ((sym = sym->next) != NULL) {
569 n_arg++;
570 type = &sym->type;
571 size = type_size(type, &align);
572 size = (size + 3) & ~3;
573 #ifdef FUNC_STRUCT_PARAM_AS_PTR
574 /* structs are passed as pointer */
575 if ((type->t & VT_BTYPE) == VT_STRUCT) {
576 size = 4;
578 #endif
579 if (param_index < fastcall_nb_regs) {
580 /* save FASTCALL register */
581 loc -= 4;
582 o(0x89); /* movl */
583 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
584 param_addr = loc;
585 } else {
586 param_addr = addr;
587 addr += size;
589 sym_push(sym->v & ~SYM_FIELD, type,
590 VT_LOCAL | lvalue_type(type->t), param_addr);
591 param_index++;
593 func_ret_sub = 0;
594 /* pascal type call or fastcall ? */
595 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
596 func_ret_sub = addr - 8;
597 #ifndef TCC_TARGET_PE
598 else if (func_vc)
599 func_ret_sub = 4;
600 #endif
602 #ifdef CONFIG_TCC_BCHECK
603 /* leave some room for bound checking code */
604 if (tcc_state->do_bounds_check) {
605 func_bound_offset = lbounds_section->data_offset;
606 func_bound_ind = ind;
607 oad(0xb8, 0); /* lbound section pointer */
608 oad(0xb8, 0); /* call to function */
609 if (n_arg >= 2 && strcmp (get_tok_str(func_sym->v, NULL), "main") == 0) {
610 o(0x0c458b); /* mov 0x12(%ebp),%eax */
611 o(0x50); /* push %eax */
612 gen_static_call(TOK___bound_main_arg);
613 o(0x04c483); /* add $0x4,%esp */
616 #endif
619 /* generate function epilog */
620 ST_FUNC void gfunc_epilog(void)
622 addr_t v, saved_ind;
624 #ifdef CONFIG_TCC_BCHECK
625 if (tcc_state->do_bounds_check
626 && func_bound_offset != lbounds_section->data_offset) {
627 addr_t saved_ind;
628 addr_t *bounds_ptr;
629 Sym *sym_data;
631 /* add end of table info */
632 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
633 *bounds_ptr = 0;
635 /* generate bound local allocation */
636 saved_ind = ind;
637 ind = func_bound_ind;
638 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
639 func_bound_offset, lbounds_section->data_offset);
640 greloc(cur_text_section, sym_data,
641 ind + 1, R_386_32);
642 oad(0xb8, 0); /* mov %eax, xxx */
643 gen_static_call(TOK___bound_local_new);
644 ind = saved_ind;
646 /* generate bound check local freeing */
647 o(0x5250); /* save returned value, if any */
648 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
649 oad(0xb8, 0); /* mov %eax, xxx */
650 gen_static_call(TOK___bound_local_delete);
651 o(0x585a); /* restore returned value, if any */
653 #endif
655 /* align local size to word & save local variables */
656 v = (-loc + 3) & -4;
658 #if USE_EBX
659 o(0x8b);
660 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
661 #endif
663 o(0xc9); /* leave */
664 if (func_ret_sub == 0) {
665 o(0xc3); /* ret */
666 } else {
667 o(0xc2); /* ret n */
668 g(func_ret_sub);
669 g(func_ret_sub >> 8);
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 gjmp2(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 #if 0
711 /* generate a jump to a fixed address */
712 ST_FUNC void gjmp_cond_addr(int a, int op)
714 int r = a - ind - 2;
715 if (r == (char)r)
716 g(op - 32), g(r);
717 else
718 g(0x0f), gjmp2(op - 16, r - 4);
720 #endif
722 ST_FUNC int gjmp_append(int n, int t)
724 void *p;
725 /* insert vtop->c jump list in t */
726 if (n) {
727 uint32_t n1 = n, n2;
728 while ((n2 = read32le(p = cur_text_section->data + n1)))
729 n1 = n2;
730 write32le(p, t);
731 t = n;
733 return t;
736 ST_FUNC int gjmp_cond(int op, int t)
738 g(0x0f);
739 t = gjmp2(op - 16, t);
740 return t;
743 ST_FUNC void gen_opi(int op)
745 int r, fr, opc, c;
747 switch(op) {
748 case '+':
749 case TOK_ADDC1: /* add with carry generation */
750 opc = 0;
751 gen_op8:
752 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
753 /* constant case */
754 vswap();
755 r = gv(RC_INT);
756 vswap();
757 c = vtop->c.i;
758 if (c == (char)c) {
759 /* generate inc and dec for smaller code */
760 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
761 opc = (c == 1) ^ (op == '+');
762 o (0x40 | (opc << 3) | r); // inc,dec
763 } else {
764 o(0x83);
765 o(0xc0 | (opc << 3) | r);
766 g(c);
768 } else {
769 o(0x81);
770 oad(0xc0 | (opc << 3) | r, c);
772 } else {
773 gv2(RC_INT, RC_INT);
774 r = vtop[-1].r;
775 fr = vtop[0].r;
776 o((opc << 3) | 0x01);
777 o(0xc0 + r + fr * 8);
779 vtop--;
780 if (op >= TOK_ULT && op <= TOK_GT)
781 vset_VT_CMP(op);
782 break;
783 case '-':
784 case TOK_SUBC1: /* sub with carry generation */
785 opc = 5;
786 goto gen_op8;
787 case TOK_ADDC2: /* add with carry use */
788 opc = 2;
789 goto gen_op8;
790 case TOK_SUBC2: /* sub with carry use */
791 opc = 3;
792 goto gen_op8;
793 case '&':
794 opc = 4;
795 goto gen_op8;
796 case '^':
797 opc = 6;
798 goto gen_op8;
799 case '|':
800 opc = 1;
801 goto gen_op8;
802 case '*':
803 gv2(RC_INT, RC_INT);
804 r = vtop[-1].r;
805 fr = vtop[0].r;
806 vtop--;
807 o(0xaf0f); /* imul fr, r */
808 o(0xc0 + fr + r * 8);
809 break;
810 case TOK_SHL:
811 opc = 4;
812 goto gen_shift;
813 case TOK_SHR:
814 opc = 5;
815 goto gen_shift;
816 case TOK_SAR:
817 opc = 7;
818 gen_shift:
819 opc = 0xc0 | (opc << 3);
820 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
821 /* constant case */
822 vswap();
823 r = gv(RC_INT);
824 vswap();
825 c = vtop->c.i & 0x1f;
826 o(0xc1); /* shl/shr/sar $xxx, r */
827 o(opc | r);
828 g(c);
829 } else {
830 /* we generate the shift in ecx */
831 gv2(RC_INT, RC_ECX);
832 r = vtop[-1].r;
833 o(0xd3); /* shl/shr/sar %cl, r */
834 o(opc | r);
836 vtop--;
837 break;
838 case '/':
839 case TOK_UDIV:
840 case TOK_PDIV:
841 case '%':
842 case TOK_UMOD:
843 case TOK_UMULL:
844 /* first operand must be in eax */
845 /* XXX: need better constraint for second operand */
846 gv2(RC_EAX, RC_ECX);
847 r = vtop[-1].r;
848 fr = vtop[0].r;
849 vtop--;
850 save_reg(TREG_EDX);
851 /* save EAX too if used otherwise */
852 save_reg_upstack(TREG_EAX, 1);
853 if (op == TOK_UMULL) {
854 o(0xf7); /* mul fr */
855 o(0xe0 + fr);
856 vtop->r2 = TREG_EDX;
857 r = TREG_EAX;
858 } else {
859 if (op == TOK_UDIV || op == TOK_UMOD) {
860 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
861 o(0xf0 + fr);
862 } else {
863 o(0xf799); /* cltd, idiv fr, %eax */
864 o(0xf8 + fr);
866 if (op == '%' || op == TOK_UMOD)
867 r = TREG_EDX;
868 else
869 r = TREG_EAX;
871 vtop->r = r;
872 break;
873 default:
874 opc = 7;
875 goto gen_op8;
879 /* generate a floating point operation 'v = t1 op t2' instruction. The
880 two operands are guaranteed to have the same floating point type */
881 /* XXX: need to use ST1 too */
882 ST_FUNC void gen_opf(int op)
884 int a, ft, fc, swapped, r;
886 /* convert constants to memory references */
887 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
888 vswap();
889 gv(RC_FLOAT);
890 vswap();
892 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
893 gv(RC_FLOAT);
895 /* must put at least one value in the floating point register */
896 if ((vtop[-1].r & VT_LVAL) &&
897 (vtop[0].r & VT_LVAL)) {
898 vswap();
899 gv(RC_FLOAT);
900 vswap();
902 swapped = 0;
903 /* swap the stack if needed so that t1 is the register and t2 is
904 the memory reference */
905 if (vtop[-1].r & VT_LVAL) {
906 vswap();
907 swapped = 1;
909 if (op >= TOK_ULT && op <= TOK_GT) {
910 /* load on stack second operand */
911 load(TREG_ST0, vtop);
912 save_reg(TREG_EAX); /* eax is used by FP comparison code */
913 if (op == TOK_GE || op == TOK_GT)
914 swapped = !swapped;
915 else if (op == TOK_EQ || op == TOK_NE)
916 swapped = 0;
917 if (swapped)
918 o(0xc9d9); /* fxch %st(1) */
919 if (op == TOK_EQ || op == TOK_NE)
920 o(0xe9da); /* fucompp */
921 else
922 o(0xd9de); /* fcompp */
923 o(0xe0df); /* fnstsw %ax */
924 if (op == TOK_EQ) {
925 o(0x45e480); /* and $0x45, %ah */
926 o(0x40fC80); /* cmp $0x40, %ah */
927 } else if (op == TOK_NE) {
928 o(0x45e480); /* and $0x45, %ah */
929 o(0x40f480); /* xor $0x40, %ah */
930 op = TOK_NE;
931 } else if (op == TOK_GE || op == TOK_LE) {
932 o(0x05c4f6); /* test $0x05, %ah */
933 op = TOK_EQ;
934 } else {
935 o(0x45c4f6); /* test $0x45, %ah */
936 op = TOK_EQ;
938 vtop--;
939 vset_VT_CMP(op);
940 } else {
941 /* no memory reference possible for long double operations */
942 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
943 load(TREG_ST0, vtop);
944 swapped = !swapped;
947 switch(op) {
948 default:
949 case '+':
950 a = 0;
951 break;
952 case '-':
953 a = 4;
954 if (swapped)
955 a++;
956 break;
957 case '*':
958 a = 1;
959 break;
960 case '/':
961 a = 6;
962 if (swapped)
963 a++;
964 break;
966 ft = vtop->type.t;
967 fc = vtop->c.i;
968 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
969 o(0xde); /* fxxxp %st, %st(1) */
970 o(0xc1 + (a << 3));
971 } else {
972 /* if saved lvalue, then we must reload it */
973 r = vtop->r;
974 if ((r & VT_VALMASK) == VT_LLOCAL) {
975 SValue v1;
976 r = get_reg(RC_INT);
977 v1.type.t = VT_INT;
978 v1.r = VT_LOCAL | VT_LVAL;
979 v1.c.i = fc;
980 load(r, &v1);
981 fc = 0;
984 if ((ft & VT_BTYPE) == VT_DOUBLE)
985 o(0xdc);
986 else
987 o(0xd8);
988 gen_modrm(a, r, vtop->sym, fc);
990 vtop--;
994 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
995 and 'long long' cases. */
996 ST_FUNC void gen_cvt_itof(int t)
998 save_reg(TREG_ST0);
999 gv(RC_INT);
1000 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1001 /* signed long long to float/double/long double (unsigned case
1002 is handled generically) */
1003 o(0x50 + vtop->r2); /* push r2 */
1004 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1005 o(0x242cdf); /* fildll (%esp) */
1006 o(0x08c483); /* add $8, %esp */
1007 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1008 (VT_INT | VT_UNSIGNED)) {
1009 /* unsigned int to float/double/long double */
1010 o(0x6a); /* push $0 */
1011 g(0x00);
1012 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1013 o(0x242cdf); /* fildll (%esp) */
1014 o(0x08c483); /* add $8, %esp */
1015 } else {
1016 /* int to float/double/long double */
1017 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1018 o(0x2404db); /* fildl (%esp) */
1019 o(0x04c483); /* add $4, %esp */
1021 vtop->r2 = VT_CONST;
1022 vtop->r = TREG_ST0;
1025 /* convert fp to int 't' type */
1026 ST_FUNC void gen_cvt_ftoi(int t)
1028 int bt = vtop->type.t & VT_BTYPE;
1029 if (bt == VT_FLOAT)
1030 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1031 else if (bt == VT_LDOUBLE)
1032 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1033 else
1034 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1035 vswap();
1036 gfunc_call(1);
1037 vpushi(0);
1038 vtop->r = REG_IRET;
1039 if ((t & VT_BTYPE) == VT_LLONG)
1040 vtop->r2 = REG_LRET;
1043 /* convert from one floating point type to another */
1044 ST_FUNC void gen_cvt_ftof(int t)
1046 /* all we have to do on i386 is to put the float in a register */
1047 gv(RC_FLOAT);
1050 /* computed goto support */
1051 ST_FUNC void ggoto(void)
1053 gcall_or_jmp(1);
1054 vtop--;
1057 /* bound check support functions */
1058 #ifdef CONFIG_TCC_BCHECK
1059 /* generate a bounded pointer addition */
1060 ST_FUNC void gen_bounded_ptr_add(void)
1062 vpush_global_sym(&func_old_type, TOK___bound_ptr_add);
1063 vrott(3);
1064 gfunc_call(2);
1065 vpushi(0);
1066 /* returned pointer is in eax */
1067 vtop->r = TREG_EAX | VT_BOUNDED;
1068 if (nocode_wanted)
1069 return;
1070 /* relocation offset of the bounding function call point */
1071 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1074 /* patch pointer addition in vtop so that pointer dereferencing is
1075 also tested */
1076 ST_FUNC void gen_bounded_ptr_deref(void)
1078 addr_t func;
1079 int size, align;
1080 Elf32_Rel *rel;
1081 Sym *sym;
1083 if (nocode_wanted)
1084 return;
1086 size = 0;
1087 /* XXX: put that code in generic part of tcc */
1088 if (!is_float(vtop->type.t)) {
1089 if (vtop->r & VT_LVAL_BYTE)
1090 size = 1;
1091 else if (vtop->r & VT_LVAL_SHORT)
1092 size = 2;
1094 if (!size)
1095 size = type_size(&vtop->type, &align);
1096 switch(size) {
1097 case 1: func = TOK___bound_ptr_indir1; break;
1098 case 2: func = TOK___bound_ptr_indir2; break;
1099 case 4: func = TOK___bound_ptr_indir4; break;
1100 case 8: func = TOK___bound_ptr_indir8; break;
1101 case 12: func = TOK___bound_ptr_indir12; break;
1102 case 16: func = TOK___bound_ptr_indir16; break;
1103 default:
1104 /* may happen with struct member access */
1105 return;
1106 //tcc_error("unhandled size when dereferencing bounded pointer");
1107 //func = 0;
1108 //break;
1110 sym = external_global_sym(func, &func_old_type);
1111 if (!sym->c)
1112 put_extern_sym(sym, NULL, 0, 0);
1113 /* patch relocation */
1114 /* XXX: find a better solution ? */
1115 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1116 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1118 #endif
1120 /* Save the stack pointer onto the stack */
1121 ST_FUNC void gen_vla_sp_save(int addr) {
1122 /* mov %esp,addr(%ebp)*/
1123 o(0x89);
1124 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1127 /* Restore the SP from a location on the stack */
1128 ST_FUNC void gen_vla_sp_restore(int addr) {
1129 o(0x8b);
1130 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1133 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1134 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1135 int use_call = 0;
1137 #if defined(CONFIG_TCC_BCHECK)
1138 use_call = tcc_state->do_bounds_check;
1139 #endif
1140 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
1141 use_call = 1;
1142 #endif
1143 if (use_call)
1145 vpush_global_sym(&func_old_type, TOK_alloca);
1146 vswap(); /* Move alloca ref past allocation size */
1147 gfunc_call(1);
1149 else {
1150 int r;
1151 r = gv(RC_INT); /* allocation size */
1152 /* sub r,%rsp */
1153 o(0x2b);
1154 o(0xe0 | r);
1155 /* We align to 16 bytes rather than align */
1156 /* and ~15, %esp */
1157 o(0xf0e483);
1158 vpop();
1162 /* end of X86 code generator */
1163 /*************************************************************/
1164 #endif
1165 /*************************************************************/