i386-gen: use EBX as 4th register
[tinycc.git] / i386-gen.c
blobfaac055c718510912462608bd19ad791440153a3
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_COPY R_386_COPY
87 #define ELF_START_ADDR 0x08048000
88 #define ELF_PAGE_SIZE 0x1000
90 /******************************************************/
91 #else /* ! TARGET_DEFS_ONLY */
92 /******************************************************/
93 #include "tcc.h"
95 /* define to 1/0 to [not] have EBX as 4th register */
96 #define USE_EBX 1
98 ST_DATA const int reg_classes[NB_REGS] = {
99 /* eax */ RC_INT | RC_EAX,
100 /* ecx */ RC_INT | RC_ECX,
101 /* edx */ RC_INT | RC_EDX,
102 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
103 /* st0 */ RC_FLOAT | RC_ST0,
106 static unsigned long func_sub_sp_offset;
107 static int func_ret_sub;
108 #ifdef CONFIG_TCC_BCHECK
109 static addr_t func_bound_offset;
110 #endif
112 /* XXX: make it faster ? */
113 ST_FUNC void g(int c)
115 int ind1;
116 ind1 = ind + 1;
117 if (ind1 > cur_text_section->data_allocated)
118 section_realloc(cur_text_section, ind1);
119 cur_text_section->data[ind] = c;
120 ind = ind1;
123 ST_FUNC void o(unsigned int c)
125 while (c) {
126 g(c);
127 c = c >> 8;
131 ST_FUNC void gen_le16(int v)
133 g(v);
134 g(v >> 8);
137 ST_FUNC void gen_le32(int c)
139 g(c);
140 g(c >> 8);
141 g(c >> 16);
142 g(c >> 24);
145 /* output a symbol and patch all calls to it */
146 ST_FUNC void gsym_addr(int t, int a)
148 while (t) {
149 unsigned char *ptr = cur_text_section->data + t;
150 uint32_t n = read32le(ptr); /* next value */
151 write32le(ptr, a - t - 4);
152 t = n;
156 ST_FUNC void gsym(int t)
158 gsym_addr(t, ind);
161 /* psym is used to put an instruction with a data field which is a
162 reference to a symbol. It is in fact the same as oad ! */
163 #define psym oad
165 /* instruction + 4 bytes data. Return the address of the data */
166 ST_FUNC int oad(int c, int s)
168 int ind1;
170 o(c);
171 ind1 = ind + 4;
172 if (ind1 > cur_text_section->data_allocated)
173 section_realloc(cur_text_section, ind1);
174 write32le(cur_text_section->data + ind, s);
175 s = ind;
176 ind = ind1;
177 return s;
180 /* output constant with relocation if 'r & VT_SYM' is true */
181 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
183 if (r & VT_SYM)
184 greloc(cur_text_section, sym, ind, R_386_32);
185 gen_le32(c);
188 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
190 if (r & VT_SYM)
191 greloc(cur_text_section, sym, ind, R_386_PC32);
192 gen_le32(c - 4);
195 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
196 opcode bits */
197 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
199 op_reg = op_reg << 3;
200 if ((r & VT_VALMASK) == VT_CONST) {
201 /* constant memory reference */
202 o(0x05 | op_reg);
203 gen_addr32(r, sym, c);
204 } else if ((r & VT_VALMASK) == VT_LOCAL) {
205 /* currently, we use only ebp as base */
206 if (c == (char)c) {
207 /* short reference */
208 o(0x45 | op_reg);
209 g(c);
210 } else {
211 oad(0x85 | op_reg, c);
213 } else {
214 g(0x00 | op_reg | (r & VT_VALMASK));
218 /* load 'r' from value 'sv' */
219 ST_FUNC void load(int r, SValue *sv)
221 int v, t, ft, fc, fr;
222 SValue v1;
224 #ifdef TCC_TARGET_PE
225 SValue v2;
226 sv = pe_getimport(sv, &v2);
227 #endif
229 fr = sv->r;
230 ft = sv->type.t;
231 fc = sv->c.i;
233 ft &= ~(VT_VOLATILE | VT_CONSTANT);
235 v = fr & VT_VALMASK;
236 if (fr & VT_LVAL) {
237 if (v == VT_LLOCAL) {
238 v1.type.t = VT_INT;
239 v1.r = VT_LOCAL | VT_LVAL;
240 v1.c.i = fc;
241 fr = r;
242 if (!(reg_classes[fr] & RC_INT))
243 fr = get_reg(RC_INT);
244 load(fr, &v1);
246 if ((ft & VT_BTYPE) == VT_FLOAT) {
247 o(0xd9); /* flds */
248 r = 0;
249 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
250 o(0xdd); /* fldl */
251 r = 0;
252 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
253 o(0xdb); /* fldt */
254 r = 5;
255 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
256 o(0xbe0f); /* movsbl */
257 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
258 o(0xb60f); /* movzbl */
259 } else if ((ft & VT_TYPE) == VT_SHORT) {
260 o(0xbf0f); /* movswl */
261 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
262 o(0xb70f); /* movzwl */
263 } else {
264 o(0x8b); /* movl */
266 gen_modrm(r, fr, sv->sym, fc);
267 } else {
268 if (v == VT_CONST) {
269 o(0xb8 + r); /* mov $xx, r */
270 gen_addr32(fr, sv->sym, fc);
271 } else if (v == VT_LOCAL) {
272 if (fc) {
273 o(0x8d); /* lea xxx(%ebp), r */
274 gen_modrm(r, VT_LOCAL, sv->sym, fc);
275 } else {
276 o(0x89);
277 o(0xe8 + r); /* mov %ebp, r */
279 } else if (v == VT_CMP) {
280 oad(0xb8 + r, 0); /* mov $0, r */
281 o(0x0f); /* setxx %br */
282 o(fc);
283 o(0xc0 + r);
284 } else if (v == VT_JMP || v == VT_JMPI) {
285 t = v & 1;
286 oad(0xb8 + r, t); /* mov $1, r */
287 o(0x05eb); /* jmp after */
288 gsym(fc);
289 oad(0xb8 + r, t ^ 1); /* mov $0, r */
290 } else if (v != r) {
291 o(0x89);
292 o(0xc0 + r + v * 8); /* mov v, r */
297 /* store register 'r' in lvalue 'v' */
298 ST_FUNC void store(int r, SValue *v)
300 int fr, bt, ft, fc;
302 #ifdef TCC_TARGET_PE
303 SValue v2;
304 v = pe_getimport(v, &v2);
305 #endif
307 ft = v->type.t;
308 fc = v->c.i;
309 fr = v->r & VT_VALMASK;
310 ft &= ~(VT_VOLATILE | VT_CONSTANT);
311 bt = ft & VT_BTYPE;
312 /* XXX: incorrect if float reg to reg */
313 if (bt == VT_FLOAT) {
314 o(0xd9); /* fsts */
315 r = 2;
316 } else if (bt == VT_DOUBLE) {
317 o(0xdd); /* fstpl */
318 r = 2;
319 } else if (bt == VT_LDOUBLE) {
320 o(0xc0d9); /* fld %st(0) */
321 o(0xdb); /* fstpt */
322 r = 7;
323 } else {
324 if (bt == VT_SHORT)
325 o(0x66);
326 if (bt == VT_BYTE || bt == VT_BOOL)
327 o(0x88);
328 else
329 o(0x89);
331 if (fr == VT_CONST ||
332 fr == VT_LOCAL ||
333 (v->r & VT_LVAL)) {
334 gen_modrm(r, v->r, v->sym, fc);
335 } else if (fr != r) {
336 o(0xc0 + fr + r * 8); /* mov r, fr */
340 static void gadd_sp(int val)
342 if (val == (char)val) {
343 o(0xc483);
344 g(val);
345 } else {
346 oad(0xc481, val); /* add $xxx, %esp */
350 static void gen_static_call(int v)
352 Sym *sym;
354 sym = external_global_sym(v, &func_old_type, 0);
355 oad(0xe8, -4);
356 greloc(cur_text_section, sym, ind-4, R_386_PC32);
359 /* 'is_jmp' is '1' if it is a jump */
360 static void gcall_or_jmp(int is_jmp)
362 int r;
363 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
364 int rt;
365 /* constant case */
366 if (vtop->r & VT_SYM) {
367 /* relocation case */
368 greloc(cur_text_section, vtop->sym,
369 ind + 1, R_386_PC32);
370 } else {
371 /* put an empty PC32 relocation */
372 put_elf_reloc(symtab_section, cur_text_section,
373 ind + 1, R_386_PC32, 0);
375 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
376 /* extend the return value to the whole register if necessary
377 visual studio and gcc do not always set the whole eax register
378 when assigning the return value of a function */
379 rt = vtop->type.ref->type.t;
380 switch (rt & VT_BTYPE) {
381 case VT_BYTE:
382 if (rt & VT_UNSIGNED) {
383 o(0xc0b60f); /* movzx %al, %eax */
385 else {
386 o(0xc0be0f); /* movsx %al, %eax */
388 break;
389 case VT_SHORT:
390 if (rt & VT_UNSIGNED) {
391 o(0xc0b70f); /* movzx %ax, %eax */
393 else {
394 o(0xc0bf0f); /* movsx %ax, %eax */
396 break;
397 default:
398 break;
400 } else {
401 /* otherwise, indirect call */
402 r = gv(RC_INT);
403 o(0xff); /* call/jmp *r */
404 o(0xd0 + r + (is_jmp << 4));
408 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
409 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
411 /* Return the number of registers needed to return the struct, or 0 if
412 returning via struct pointer. */
413 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
415 #ifdef TCC_TARGET_PE
416 int size, align;
418 *ret_align = 1; // Never have to re-align return values for x86
419 *regsize = 4;
420 size = type_size(vt, &align);
421 if (size > 8) {
422 return 0;
423 } else if (size > 4) {
424 ret->ref = NULL;
425 ret->t = VT_LLONG;
426 return 1;
427 } else {
428 ret->ref = NULL;
429 ret->t = VT_INT;
430 return 1;
432 #else
433 *ret_align = 1; // Never have to re-align return values for x86
434 return 0;
435 #endif
438 /* Generate function call. The function address is pushed first, then
439 all the parameters in call order. This functions pops all the
440 parameters and the function address. */
441 ST_FUNC void gfunc_call(int nb_args)
443 int size, align, r, args_size, i, func_call;
444 Sym *func_sym;
446 args_size = 0;
447 for(i = 0;i < nb_args; i++) {
448 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
449 size = type_size(&vtop->type, &align);
450 /* align to stack align size */
451 size = (size + 3) & ~3;
452 /* allocate the necessary size on stack */
453 oad(0xec81, size); /* sub $xxx, %esp */
454 /* generate structure store */
455 r = get_reg(RC_INT);
456 o(0x89); /* mov %esp, r */
457 o(0xe0 + r);
458 vset(&vtop->type, r | VT_LVAL, 0);
459 vswap();
460 vstore();
461 args_size += size;
462 } else if (is_float(vtop->type.t)) {
463 gv(RC_FLOAT); /* only one float register */
464 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
465 size = 4;
466 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
467 size = 8;
468 else
469 size = 12;
470 oad(0xec81, size); /* sub $xxx, %esp */
471 if (size == 12)
472 o(0x7cdb);
473 else
474 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
475 g(0x24);
476 g(0x00);
477 args_size += size;
478 } else {
479 /* simple type (currently always same size) */
480 /* XXX: implicit cast ? */
481 r = gv(RC_INT);
482 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
483 size = 8;
484 o(0x50 + vtop->r2); /* push r */
485 } else {
486 size = 4;
488 o(0x50 + r); /* push r */
489 args_size += size;
491 vtop--;
493 save_regs(0); /* save used temporary registers */
494 func_sym = vtop->type.ref;
495 func_call = func_sym->a.func_call;
496 /* fast call case */
497 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
498 func_call == FUNC_FASTCALLW) {
499 int fastcall_nb_regs;
500 uint8_t *fastcall_regs_ptr;
501 if (func_call == FUNC_FASTCALLW) {
502 fastcall_regs_ptr = fastcallw_regs;
503 fastcall_nb_regs = 2;
504 } else {
505 fastcall_regs_ptr = fastcall_regs;
506 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
508 for(i = 0;i < fastcall_nb_regs; i++) {
509 if (args_size <= 0)
510 break;
511 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
512 /* XXX: incorrect for struct/floats */
513 args_size -= 4;
516 #ifndef TCC_TARGET_PE
517 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
518 args_size -= 4;
519 #endif
520 gcall_or_jmp(0);
522 if (args_size && func_call != FUNC_STDCALL)
523 gadd_sp(args_size);
524 vtop--;
527 #ifdef TCC_TARGET_PE
528 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
529 #else
530 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
531 #endif
533 /* generate function prolog of type 't' */
534 ST_FUNC void gfunc_prolog(CType *func_type)
536 int addr, align, size, func_call, fastcall_nb_regs;
537 int param_index, param_addr;
538 uint8_t *fastcall_regs_ptr;
539 Sym *sym;
540 CType *type;
542 sym = func_type->ref;
543 func_call = sym->a.func_call;
544 addr = 8;
545 loc = 0;
546 func_vc = 0;
548 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
549 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
550 fastcall_regs_ptr = fastcall_regs;
551 } else if (func_call == FUNC_FASTCALLW) {
552 fastcall_nb_regs = 2;
553 fastcall_regs_ptr = fastcallw_regs;
554 } else {
555 fastcall_nb_regs = 0;
556 fastcall_regs_ptr = NULL;
558 param_index = 0;
560 ind += FUNC_PROLOG_SIZE;
561 func_sub_sp_offset = ind;
562 /* if the function returns a structure, then add an
563 implicit pointer parameter */
564 func_vt = sym->type;
565 func_var = (sym->c == FUNC_ELLIPSIS);
566 #ifdef TCC_TARGET_PE
567 size = type_size(&func_vt,&align);
568 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
569 #else
570 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
571 #endif
572 /* XXX: fastcall case ? */
573 func_vc = addr;
574 addr += 4;
575 param_index++;
577 /* define parameters */
578 while ((sym = sym->next) != NULL) {
579 type = &sym->type;
580 size = type_size(type, &align);
581 size = (size + 3) & ~3;
582 #ifdef FUNC_STRUCT_PARAM_AS_PTR
583 /* structs are passed as pointer */
584 if ((type->t & VT_BTYPE) == VT_STRUCT) {
585 size = 4;
587 #endif
588 if (param_index < fastcall_nb_regs) {
589 /* save FASTCALL register */
590 loc -= 4;
591 o(0x89); /* movl */
592 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
593 param_addr = loc;
594 } else {
595 param_addr = addr;
596 addr += size;
598 sym_push(sym->v & ~SYM_FIELD, type,
599 VT_LOCAL | lvalue_type(type->t), param_addr);
600 param_index++;
602 func_ret_sub = 0;
603 /* pascal type call ? */
604 if (func_call == FUNC_STDCALL)
605 func_ret_sub = addr - 8;
606 #ifndef TCC_TARGET_PE
607 else if (func_vc)
608 func_ret_sub = 4;
609 #endif
611 #ifdef CONFIG_TCC_BCHECK
612 /* leave some room for bound checking code */
613 if (tcc_state->do_bounds_check) {
614 oad(0xb8, 0); /* lbound section pointer */
615 oad(0xb8, 0); /* call to function */
616 func_bound_offset = lbounds_section->data_offset;
618 #endif
621 /* generate function epilog */
622 ST_FUNC void gfunc_epilog(void)
624 addr_t v, saved_ind;
626 #ifdef CONFIG_TCC_BCHECK
627 if (tcc_state->do_bounds_check
628 && func_bound_offset != lbounds_section->data_offset) {
629 addr_t saved_ind;
630 addr_t *bounds_ptr;
631 Sym *sym_data;
632 /* add end of table info */
633 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
634 *bounds_ptr = 0;
635 /* generate bound local allocation */
636 saved_ind = ind;
637 ind = func_sub_sp_offset;
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);
645 ind = saved_ind;
646 /* generate bound check local freeing */
647 o(0x5250); /* save returned value, if any */
648 greloc(cur_text_section, sym_data,
649 ind + 1, R_386_32);
650 oad(0xb8, 0); /* mov %eax, xxx */
651 gen_static_call(TOK___bound_local_delete);
653 o(0x585a); /* restore returned value, if any */
655 #endif
656 o(0x5b * USE_EBX); /* pop ebx */
657 o(0xc9); /* leave */
658 if (func_ret_sub == 0) {
659 o(0xc3); /* ret */
660 } else {
661 o(0xc2); /* ret n */
662 g(func_ret_sub);
663 g(func_ret_sub >> 8);
665 /* align local size to word & save local variables */
667 v = (-loc + 3) & -4;
668 saved_ind = ind;
669 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
670 #ifdef TCC_TARGET_PE
671 if (v >= 4096) {
672 oad(0xb8, v); /* mov stacksize, %eax */
673 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
674 } else
675 #endif
677 o(0xe58955); /* push %ebp, mov %esp, %ebp */
678 o(0xec81); /* sub esp, stacksize */
679 gen_le32(v);
680 #ifdef TCC_TARGET_PE
681 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
682 #endif
684 o(0x53 * USE_EBX); /* push ebx */
685 ind = saved_ind;
688 /* generate a jump to a label */
689 ST_FUNC int gjmp(int t)
691 return psym(0xe9, t);
694 /* generate a jump to a fixed address */
695 ST_FUNC void gjmp_addr(int a)
697 int r;
698 r = a - ind - 2;
699 if (r == (char)r) {
700 g(0xeb);
701 g(r);
702 } else {
703 oad(0xe9, a - ind - 5);
707 ST_FUNC void gtst_addr(int inv, int a)
709 inv ^= (vtop--)->c.i;
710 a -= ind + 2;
711 if (a == (char)a) {
712 g(inv - 32);
713 g(a);
714 } else {
715 g(0x0f);
716 oad(inv - 16, a - 4);
720 /* generate a test. set 'inv' to invert test. Stack entry is popped */
721 ST_FUNC int gtst(int inv, int t)
723 int v = vtop->r & VT_VALMASK;
724 if (v == VT_CMP) {
725 /* fast case : can jump directly since flags are set */
726 g(0x0f);
727 t = psym((vtop->c.i - 16) ^ inv, t);
728 } else if (v == VT_JMP || v == VT_JMPI) {
729 /* && or || optimization */
730 if ((v & 1) == inv) {
731 /* insert vtop->c jump list in t */
732 uint32_t n1, n = vtop->c.i;
733 if (n) {
734 while ((n1 = read32le(cur_text_section->data + n)))
735 n = n1;
736 write32le(cur_text_section->data + n, t);
737 t = vtop->c.i;
739 } else {
740 t = gjmp(t);
741 gsym(vtop->c.i);
744 vtop--;
745 return t;
748 /* generate an integer binary operation */
749 ST_FUNC void gen_opi(int op)
751 int r, fr, opc, c;
753 switch(op) {
754 case '+':
755 case TOK_ADDC1: /* add with carry generation */
756 opc = 0;
757 gen_op8:
758 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
759 /* constant case */
760 vswap();
761 r = gv(RC_INT);
762 vswap();
763 c = vtop->c.i;
764 if (c == (char)c) {
765 /* generate inc and dec for smaller code */
766 if (c==1 && opc==0 && op != TOK_ADDC1) {
767 o (0x40 | r); // inc
768 } else if (c==1 && opc==5 && op != TOK_SUBC1) {
769 o (0x48 | r); // dec
770 } else {
771 o(0x83);
772 o(0xc0 | (opc << 3) | r);
773 g(c);
775 } else {
776 o(0x81);
777 oad(0xc0 | (opc << 3) | r, c);
779 } else {
780 gv2(RC_INT, RC_INT);
781 r = vtop[-1].r;
782 fr = vtop[0].r;
783 o((opc << 3) | 0x01);
784 o(0xc0 + r + fr * 8);
786 vtop--;
787 if (op >= TOK_ULT && op <= TOK_GT) {
788 vtop->r = VT_CMP;
789 vtop->c.i = op;
791 break;
792 case '-':
793 case TOK_SUBC1: /* sub with carry generation */
794 opc = 5;
795 goto gen_op8;
796 case TOK_ADDC2: /* add with carry use */
797 opc = 2;
798 goto gen_op8;
799 case TOK_SUBC2: /* sub with carry use */
800 opc = 3;
801 goto gen_op8;
802 case '&':
803 opc = 4;
804 goto gen_op8;
805 case '^':
806 opc = 6;
807 goto gen_op8;
808 case '|':
809 opc = 1;
810 goto gen_op8;
811 case '*':
812 gv2(RC_INT, RC_INT);
813 r = vtop[-1].r;
814 fr = vtop[0].r;
815 vtop--;
816 o(0xaf0f); /* imul fr, r */
817 o(0xc0 + fr + r * 8);
818 break;
819 case TOK_SHL:
820 opc = 4;
821 goto gen_shift;
822 case TOK_SHR:
823 opc = 5;
824 goto gen_shift;
825 case TOK_SAR:
826 opc = 7;
827 gen_shift:
828 opc = 0xc0 | (opc << 3);
829 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
830 /* constant case */
831 vswap();
832 r = gv(RC_INT);
833 vswap();
834 c = vtop->c.i & 0x1f;
835 o(0xc1); /* shl/shr/sar $xxx, r */
836 o(opc | r);
837 g(c);
838 } else {
839 /* we generate the shift in ecx */
840 gv2(RC_INT, RC_ECX);
841 r = vtop[-1].r;
842 o(0xd3); /* shl/shr/sar %cl, r */
843 o(opc | r);
845 vtop--;
846 break;
847 case '/':
848 case TOK_UDIV:
849 case TOK_PDIV:
850 case '%':
851 case TOK_UMOD:
852 case TOK_UMULL:
853 /* first operand must be in eax */
854 /* XXX: need better constraint for second operand */
855 gv2(RC_EAX, RC_ECX);
856 r = vtop[-1].r;
857 fr = vtop[0].r;
858 vtop--;
859 save_reg(TREG_EDX);
860 /* save EAX too if used otherwise */
861 save_reg_upstack(TREG_EAX, 1);
862 if (op == TOK_UMULL) {
863 o(0xf7); /* mul fr */
864 o(0xe0 + fr);
865 vtop->r2 = TREG_EDX;
866 r = TREG_EAX;
867 } else {
868 if (op == TOK_UDIV || op == TOK_UMOD) {
869 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
870 o(0xf0 + fr);
871 } else {
872 o(0xf799); /* cltd, idiv fr, %eax */
873 o(0xf8 + fr);
875 if (op == '%' || op == TOK_UMOD)
876 r = TREG_EDX;
877 else
878 r = TREG_EAX;
880 vtop->r = r;
881 break;
882 default:
883 opc = 7;
884 goto gen_op8;
888 /* generate a floating point operation 'v = t1 op t2' instruction. The
889 two operands are guaranted to have the same floating point type */
890 /* XXX: need to use ST1 too */
891 ST_FUNC void gen_opf(int op)
893 int a, ft, fc, swapped, r;
895 /* convert constants to memory references */
896 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
897 vswap();
898 gv(RC_FLOAT);
899 vswap();
901 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
902 gv(RC_FLOAT);
904 /* must put at least one value in the floating point register */
905 if ((vtop[-1].r & VT_LVAL) &&
906 (vtop[0].r & VT_LVAL)) {
907 vswap();
908 gv(RC_FLOAT);
909 vswap();
911 swapped = 0;
912 /* swap the stack if needed so that t1 is the register and t2 is
913 the memory reference */
914 if (vtop[-1].r & VT_LVAL) {
915 vswap();
916 swapped = 1;
918 if (op >= TOK_ULT && op <= TOK_GT) {
919 /* load on stack second operand */
920 load(TREG_ST0, vtop);
921 save_reg(TREG_EAX); /* eax is used by FP comparison code */
922 if (op == TOK_GE || op == TOK_GT)
923 swapped = !swapped;
924 else if (op == TOK_EQ || op == TOK_NE)
925 swapped = 0;
926 if (swapped)
927 o(0xc9d9); /* fxch %st(1) */
928 if (op == TOK_EQ || op == TOK_NE)
929 o(0xe9da); /* fucompp */
930 else
931 o(0xd9de); /* fcompp */
932 o(0xe0df); /* fnstsw %ax */
933 if (op == TOK_EQ) {
934 o(0x45e480); /* and $0x45, %ah */
935 o(0x40fC80); /* cmp $0x40, %ah */
936 } else if (op == TOK_NE) {
937 o(0x45e480); /* and $0x45, %ah */
938 o(0x40f480); /* xor $0x40, %ah */
939 op = TOK_NE;
940 } else if (op == TOK_GE || op == TOK_LE) {
941 o(0x05c4f6); /* test $0x05, %ah */
942 op = TOK_EQ;
943 } else {
944 o(0x45c4f6); /* test $0x45, %ah */
945 op = TOK_EQ;
947 vtop--;
948 vtop->r = VT_CMP;
949 vtop->c.i = op;
950 } else {
951 /* no memory reference possible for long double operations */
952 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
953 load(TREG_ST0, vtop);
954 swapped = !swapped;
957 switch(op) {
958 default:
959 case '+':
960 a = 0;
961 break;
962 case '-':
963 a = 4;
964 if (swapped)
965 a++;
966 break;
967 case '*':
968 a = 1;
969 break;
970 case '/':
971 a = 6;
972 if (swapped)
973 a++;
974 break;
976 ft = vtop->type.t;
977 fc = vtop->c.i;
978 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
979 o(0xde); /* fxxxp %st, %st(1) */
980 o(0xc1 + (a << 3));
981 } else {
982 /* if saved lvalue, then we must reload it */
983 r = vtop->r;
984 if ((r & VT_VALMASK) == VT_LLOCAL) {
985 SValue v1;
986 r = get_reg(RC_INT);
987 v1.type.t = VT_INT;
988 v1.r = VT_LOCAL | VT_LVAL;
989 v1.c.i = fc;
990 load(r, &v1);
991 fc = 0;
994 if ((ft & VT_BTYPE) == VT_DOUBLE)
995 o(0xdc);
996 else
997 o(0xd8);
998 gen_modrm(a, r, vtop->sym, fc);
1000 vtop--;
1004 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1005 and 'long long' cases. */
1006 ST_FUNC void gen_cvt_itof(int t)
1008 save_reg(TREG_ST0);
1009 gv(RC_INT);
1010 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1011 /* signed long long to float/double/long double (unsigned case
1012 is handled generically) */
1013 o(0x50 + vtop->r2); /* push r2 */
1014 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1015 o(0x242cdf); /* fildll (%esp) */
1016 o(0x08c483); /* add $8, %esp */
1017 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1018 (VT_INT | VT_UNSIGNED)) {
1019 /* unsigned int to float/double/long double */
1020 o(0x6a); /* push $0 */
1021 g(0x00);
1022 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1023 o(0x242cdf); /* fildll (%esp) */
1024 o(0x08c483); /* add $8, %esp */
1025 } else {
1026 /* int to float/double/long double */
1027 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1028 o(0x2404db); /* fildl (%esp) */
1029 o(0x04c483); /* add $4, %esp */
1031 vtop->r = TREG_ST0;
1034 /* convert fp to int 't' type */
1035 ST_FUNC void gen_cvt_ftoi(int t)
1037 #if 1
1038 gv(RC_FLOAT);
1039 save_reg(TREG_EAX);
1040 save_reg(TREG_EDX);
1041 gen_static_call(TOK___tcc_cvt_ftol);
1042 vtop->r = TREG_EAX; /* mark reg as used */
1043 if (t == VT_LLONG)
1044 vtop->r2 = TREG_EDX;
1045 #else
1046 int bt = vtop->type.t & VT_BTYPE;
1047 if (bt == VT_FLOAT)
1048 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1049 else if (bt == VT_LDOUBLE)
1050 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1051 else
1052 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1053 vswap();
1054 gfunc_call(1);
1055 vpushi(0);
1056 vtop->r = REG_IRET;
1057 vtop->r2 = REG_LRET;
1058 #endif
1061 /* convert from one floating point type to another */
1062 ST_FUNC void gen_cvt_ftof(int t)
1064 /* all we have to do on i386 is to put the float in a register */
1065 gv(RC_FLOAT);
1068 /* computed goto support */
1069 ST_FUNC void ggoto(void)
1071 gcall_or_jmp(1);
1072 vtop--;
1075 /* bound check support functions */
1076 #ifdef CONFIG_TCC_BCHECK
1078 /* generate a bounded pointer addition */
1079 ST_FUNC void gen_bounded_ptr_add(void)
1081 /* prepare fast i386 function call (args in eax and edx) */
1082 gv2(RC_EAX, RC_EDX);
1083 /* save all temporary registers */
1084 vtop -= 2;
1085 save_regs(0);
1086 /* do a fast function call */
1087 gen_static_call(TOK___bound_ptr_add);
1088 /* returned pointer is in eax */
1089 vtop++;
1090 vtop->r = TREG_EAX | VT_BOUNDED;
1091 /* address of bounding function call point */
1092 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1095 /* patch pointer addition in vtop so that pointer dereferencing is
1096 also tested */
1097 ST_FUNC void gen_bounded_ptr_deref(void)
1099 addr_t func;
1100 int size, align;
1101 Elf32_Rel *rel;
1102 Sym *sym;
1104 size = 0;
1105 /* XXX: put that code in generic part of tcc */
1106 if (!is_float(vtop->type.t)) {
1107 if (vtop->r & VT_LVAL_BYTE)
1108 size = 1;
1109 else if (vtop->r & VT_LVAL_SHORT)
1110 size = 2;
1112 if (!size)
1113 size = type_size(&vtop->type, &align);
1114 switch(size) {
1115 case 1: func = TOK___bound_ptr_indir1; break;
1116 case 2: func = TOK___bound_ptr_indir2; break;
1117 case 4: func = TOK___bound_ptr_indir4; break;
1118 case 8: func = TOK___bound_ptr_indir8; break;
1119 case 12: func = TOK___bound_ptr_indir12; break;
1120 case 16: func = TOK___bound_ptr_indir16; break;
1121 default:
1122 tcc_error("unhandled size when dereferencing bounded pointer");
1123 func = 0;
1124 break;
1127 /* patch relocation */
1128 /* XXX: find a better solution ? */
1129 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1130 sym = external_global_sym(func, &func_old_type, 0);
1131 if (!sym->c)
1132 put_extern_sym(sym, NULL, 0, 0);
1133 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1135 #endif
1137 /* Save the stack pointer onto the stack */
1138 ST_FUNC void gen_vla_sp_save(int addr) {
1139 /* mov %esp,addr(%ebp)*/
1140 o(0x89);
1141 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1144 /* Restore the SP from a location on the stack */
1145 ST_FUNC void gen_vla_sp_restore(int addr) {
1146 o(0x8b);
1147 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1150 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1151 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1152 #ifdef TCC_TARGET_PE
1153 /* alloca does more than just adjust %rsp on Windows */
1154 vpush_global_sym(&func_old_type, TOK_alloca);
1155 vswap(); /* Move alloca ref past allocation size */
1156 gfunc_call(1);
1157 #else
1158 int r;
1159 r = gv(RC_INT); /* allocation size */
1160 /* sub r,%rsp */
1161 o(0x2b);
1162 o(0xe0 | r);
1163 /* We align to 16 bytes rather than align */
1164 /* and ~15, %esp */
1165 o(0xf0e483);
1166 vpop();
1167 #endif
1170 /* end of X86 code generator */
1171 /*************************************************************/
1172 #endif
1173 /*************************************************************/