tccpp: allow "0x1e+1" in asm
[tinycc.git] / i386-gen.c
blob79087d0874fcbb49cf48ca9a6da705592cd2be22
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 4
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_IRET RC_EAX /* function return: integer register */
37 #define RC_LRET RC_EDX /* function return: second integer register */
38 #define RC_FRET RC_ST0 /* function return: float register */
40 /* pretty names for the registers */
41 enum {
42 TREG_EAX = 0,
43 TREG_ECX,
44 TREG_EDX,
45 TREG_ST0,
46 TREG_ESP = 4
49 /* return registers for function */
50 #define REG_IRET TREG_EAX /* single word int return register */
51 #define REG_LRET TREG_EDX /* second word return register (for long long) */
52 #define REG_FRET TREG_ST0 /* float return register */
54 /* defined if function parameters must be evaluated in reverse order */
55 #define INVERT_FUNC_PARAMS
57 /* defined if structures are passed as pointers. Otherwise structures
58 are directly pushed on stack. */
59 /* #define FUNC_STRUCT_PARAM_AS_PTR */
61 /* pointer size, in bytes */
62 #define PTR_SIZE 4
64 /* long double size and alignment, in bytes */
65 #define LDOUBLE_SIZE 12
66 #define LDOUBLE_ALIGN 4
67 /* maximum alignment (for aligned attribute support) */
68 #define MAX_ALIGN 8
71 #define psym oad
73 /******************************************************/
74 /* ELF defines */
76 #define EM_TCC_TARGET EM_386
78 /* relocation type for 32 bit data relocation */
79 #define R_DATA_32 R_386_32
80 #define R_DATA_PTR R_386_32
81 #define R_JMP_SLOT R_386_JMP_SLOT
82 #define R_COPY R_386_COPY
84 #define ELF_START_ADDR 0x08048000
85 #define ELF_PAGE_SIZE 0x1000
87 /******************************************************/
88 #else /* ! TARGET_DEFS_ONLY */
89 /******************************************************/
90 #include "tcc.h"
92 ST_DATA const int reg_classes[NB_REGS] = {
93 /* eax */ RC_INT | RC_EAX,
94 /* ecx */ RC_INT | RC_ECX,
95 /* edx */ RC_INT | RC_EDX,
96 /* st0 */ RC_FLOAT | RC_ST0,
99 static unsigned long func_sub_sp_offset;
100 static int func_ret_sub;
101 #ifdef CONFIG_TCC_BCHECK
102 static addr_t func_bound_offset;
103 #endif
105 /* XXX: make it faster ? */
106 ST_FUNC void g(int c)
108 int ind1;
109 ind1 = ind + 1;
110 if (ind1 > cur_text_section->data_allocated)
111 section_realloc(cur_text_section, ind1);
112 cur_text_section->data[ind] = c;
113 ind = ind1;
116 ST_FUNC void o(unsigned int c)
118 while (c) {
119 g(c);
120 c = c >> 8;
124 ST_FUNC void gen_le16(int v)
126 g(v);
127 g(v >> 8);
130 ST_FUNC void gen_le32(int c)
132 g(c);
133 g(c >> 8);
134 g(c >> 16);
135 g(c >> 24);
138 /* output a symbol and patch all calls to it */
139 ST_FUNC void gsym_addr(int t, int a)
141 while (t) {
142 unsigned char *ptr = cur_text_section->data + t;
143 uint32_t n = read32le(ptr); /* next value */
144 write32le(ptr, a - t - 4);
145 t = n;
149 ST_FUNC void gsym(int t)
151 gsym_addr(t, ind);
154 /* psym is used to put an instruction with a data field which is a
155 reference to a symbol. It is in fact the same as oad ! */
156 #define psym oad
158 /* instruction + 4 bytes data. Return the address of the data */
159 ST_FUNC int oad(int c, int s)
161 int ind1;
163 o(c);
164 ind1 = ind + 4;
165 if (ind1 > cur_text_section->data_allocated)
166 section_realloc(cur_text_section, ind1);
167 write32le(cur_text_section->data + ind, s);
168 s = ind;
169 ind = ind1;
170 return s;
173 /* output constant with relocation if 'r & VT_SYM' is true */
174 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
176 if (r & VT_SYM)
177 greloc(cur_text_section, sym, ind, R_386_32);
178 gen_le32(c);
181 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
183 if (r & VT_SYM)
184 greloc(cur_text_section, sym, ind, R_386_PC32);
185 gen_le32(c - 4);
188 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
189 opcode bits */
190 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
192 op_reg = op_reg << 3;
193 if ((r & VT_VALMASK) == VT_CONST) {
194 /* constant memory reference */
195 o(0x05 | op_reg);
196 gen_addr32(r, sym, c);
197 } else if ((r & VT_VALMASK) == VT_LOCAL) {
198 /* currently, we use only ebp as base */
199 if (c == (char)c) {
200 /* short reference */
201 o(0x45 | op_reg);
202 g(c);
203 } else {
204 oad(0x85 | op_reg, c);
206 } else {
207 g(0x00 | op_reg | (r & VT_VALMASK));
211 /* load 'r' from value 'sv' */
212 ST_FUNC void load(int r, SValue *sv)
214 int v, t, ft, fc, fr;
215 SValue v1;
217 #ifdef TCC_TARGET_PE
218 SValue v2;
219 sv = pe_getimport(sv, &v2);
220 #endif
222 fr = sv->r;
223 ft = sv->type.t;
224 fc = sv->c.i;
226 ft &= ~(VT_VOLATILE | VT_CONSTANT);
228 v = fr & VT_VALMASK;
229 if (fr & VT_LVAL) {
230 if (v == VT_LLOCAL) {
231 v1.type.t = VT_INT;
232 v1.r = VT_LOCAL | VT_LVAL;
233 v1.c.i = fc;
234 fr = r;
235 if (!(reg_classes[fr] & RC_INT))
236 fr = get_reg(RC_INT);
237 load(fr, &v1);
239 if ((ft & VT_BTYPE) == VT_FLOAT) {
240 o(0xd9); /* flds */
241 r = 0;
242 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
243 o(0xdd); /* fldl */
244 r = 0;
245 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
246 o(0xdb); /* fldt */
247 r = 5;
248 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
249 o(0xbe0f); /* movsbl */
250 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
251 o(0xb60f); /* movzbl */
252 } else if ((ft & VT_TYPE) == VT_SHORT) {
253 o(0xbf0f); /* movswl */
254 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
255 o(0xb70f); /* movzwl */
256 } else {
257 o(0x8b); /* movl */
259 gen_modrm(r, fr, sv->sym, fc);
260 } else {
261 if (v == VT_CONST) {
262 o(0xb8 + r); /* mov $xx, r */
263 gen_addr32(fr, sv->sym, fc);
264 } else if (v == VT_LOCAL) {
265 if (fc) {
266 o(0x8d); /* lea xxx(%ebp), r */
267 gen_modrm(r, VT_LOCAL, sv->sym, fc);
268 } else {
269 o(0x89);
270 o(0xe8 + r); /* mov %ebp, r */
272 } else if (v == VT_CMP) {
273 oad(0xb8 + r, 0); /* mov $0, r */
274 o(0x0f); /* setxx %br */
275 o(fc);
276 o(0xc0 + r);
277 } else if (v == VT_JMP || v == VT_JMPI) {
278 t = v & 1;
279 oad(0xb8 + r, t); /* mov $1, r */
280 o(0x05eb); /* jmp after */
281 gsym(fc);
282 oad(0xb8 + r, t ^ 1); /* mov $0, r */
283 } else if (v != r) {
284 o(0x89);
285 o(0xc0 + r + v * 8); /* mov v, r */
290 /* store register 'r' in lvalue 'v' */
291 ST_FUNC void store(int r, SValue *v)
293 int fr, bt, ft, fc;
295 #ifdef TCC_TARGET_PE
296 SValue v2;
297 v = pe_getimport(v, &v2);
298 #endif
300 ft = v->type.t;
301 fc = v->c.i;
302 fr = v->r & VT_VALMASK;
303 ft &= ~(VT_VOLATILE | VT_CONSTANT);
304 bt = ft & VT_BTYPE;
305 /* XXX: incorrect if float reg to reg */
306 if (bt == VT_FLOAT) {
307 o(0xd9); /* fsts */
308 r = 2;
309 } else if (bt == VT_DOUBLE) {
310 o(0xdd); /* fstpl */
311 r = 2;
312 } else if (bt == VT_LDOUBLE) {
313 o(0xc0d9); /* fld %st(0) */
314 o(0xdb); /* fstpt */
315 r = 7;
316 } else {
317 if (bt == VT_SHORT)
318 o(0x66);
319 if (bt == VT_BYTE || bt == VT_BOOL)
320 o(0x88);
321 else
322 o(0x89);
324 if (fr == VT_CONST ||
325 fr == VT_LOCAL ||
326 (v->r & VT_LVAL)) {
327 gen_modrm(r, v->r, v->sym, fc);
328 } else if (fr != r) {
329 o(0xc0 + fr + r * 8); /* mov r, fr */
333 static void gadd_sp(int val)
335 if (val == (char)val) {
336 o(0xc483);
337 g(val);
338 } else {
339 oad(0xc481, val); /* add $xxx, %esp */
343 static void gen_static_call(int v)
345 Sym *sym;
347 sym = external_global_sym(v, &func_old_type, 0);
348 oad(0xe8, -4);
349 greloc(cur_text_section, sym, ind-4, R_386_PC32);
352 /* 'is_jmp' is '1' if it is a jump */
353 static void gcall_or_jmp(int is_jmp)
355 int r;
356 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
357 int rt;
358 /* constant case */
359 if (vtop->r & VT_SYM) {
360 /* relocation case */
361 greloc(cur_text_section, vtop->sym,
362 ind + 1, R_386_PC32);
363 } else {
364 /* put an empty PC32 relocation */
365 put_elf_reloc(symtab_section, cur_text_section,
366 ind + 1, R_386_PC32, 0);
368 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
369 /* extend the return value to the whole register if necessary
370 visual studio and gcc do not always set the whole eax register
371 when assigning the return value of a function */
372 rt = vtop->type.ref->type.t;
373 switch (rt & VT_BTYPE) {
374 case VT_BYTE:
375 if (rt & VT_UNSIGNED) {
376 o(0xc0b60f); /* movzx %al, %eax */
378 else {
379 o(0xc0be0f); /* movsx %al, %eax */
381 break;
382 case VT_SHORT:
383 if (rt & VT_UNSIGNED) {
384 o(0xc0b70f); /* movzx %ax, %eax */
386 else {
387 o(0xc0bf0f); /* movsx %ax, %eax */
389 break;
390 default:
391 break;
393 } else {
394 /* otherwise, indirect call */
395 r = gv(RC_INT);
396 o(0xff); /* call/jmp *r */
397 o(0xd0 + r + (is_jmp << 4));
401 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
402 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
404 /* Return the number of registers needed to return the struct, or 0 if
405 returning via struct pointer. */
406 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
408 #ifdef TCC_TARGET_PE
409 int size, align;
411 *ret_align = 1; // Never have to re-align return values for x86
412 *regsize = 4;
413 size = type_size(vt, &align);
414 if (size > 8) {
415 return 0;
416 } else if (size > 4) {
417 ret->ref = NULL;
418 ret->t = VT_LLONG;
419 return 1;
420 } else {
421 ret->ref = NULL;
422 ret->t = VT_INT;
423 return 1;
425 #else
426 *ret_align = 1; // Never have to re-align return values for x86
427 return 0;
428 #endif
431 /* Generate function call. The function address is pushed first, then
432 all the parameters in call order. This functions pops all the
433 parameters and the function address. */
434 ST_FUNC void gfunc_call(int nb_args)
436 int size, align, r, args_size, i, func_call;
437 Sym *func_sym;
439 args_size = 0;
440 for(i = 0;i < nb_args; i++) {
441 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
442 size = type_size(&vtop->type, &align);
443 /* align to stack align size */
444 size = (size + 3) & ~3;
445 /* allocate the necessary size on stack */
446 oad(0xec81, size); /* sub $xxx, %esp */
447 /* generate structure store */
448 r = get_reg(RC_INT);
449 o(0x89); /* mov %esp, r */
450 o(0xe0 + r);
451 vset(&vtop->type, r | VT_LVAL, 0);
452 vswap();
453 vstore();
454 args_size += size;
455 } else if (is_float(vtop->type.t)) {
456 gv(RC_FLOAT); /* only one float register */
457 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
458 size = 4;
459 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
460 size = 8;
461 else
462 size = 12;
463 oad(0xec81, size); /* sub $xxx, %esp */
464 if (size == 12)
465 o(0x7cdb);
466 else
467 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
468 g(0x24);
469 g(0x00);
470 args_size += size;
471 } else {
472 /* simple type (currently always same size) */
473 /* XXX: implicit cast ? */
474 r = gv(RC_INT);
475 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
476 size = 8;
477 o(0x50 + vtop->r2); /* push r */
478 } else {
479 size = 4;
481 o(0x50 + r); /* push r */
482 args_size += size;
484 vtop--;
486 save_regs(0); /* save used temporary registers */
487 func_sym = vtop->type.ref;
488 func_call = func_sym->a.func_call;
489 /* fast call case */
490 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
491 func_call == FUNC_FASTCALLW) {
492 int fastcall_nb_regs;
493 uint8_t *fastcall_regs_ptr;
494 if (func_call == FUNC_FASTCALLW) {
495 fastcall_regs_ptr = fastcallw_regs;
496 fastcall_nb_regs = 2;
497 } else {
498 fastcall_regs_ptr = fastcall_regs;
499 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
501 for(i = 0;i < fastcall_nb_regs; i++) {
502 if (args_size <= 0)
503 break;
504 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
505 /* XXX: incorrect for struct/floats */
506 args_size -= 4;
509 #ifndef TCC_TARGET_PE
510 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
511 args_size -= 4;
512 #endif
513 gcall_or_jmp(0);
515 if (args_size && func_call != FUNC_STDCALL)
516 gadd_sp(args_size);
517 vtop--;
520 #ifdef TCC_TARGET_PE
521 #define FUNC_PROLOG_SIZE 10
522 #else
523 #define FUNC_PROLOG_SIZE 9
524 #endif
526 /* generate function prolog of type 't' */
527 ST_FUNC void gfunc_prolog(CType *func_type)
529 int addr, align, size, func_call, fastcall_nb_regs;
530 int param_index, param_addr;
531 uint8_t *fastcall_regs_ptr;
532 Sym *sym;
533 CType *type;
535 sym = func_type->ref;
536 func_call = sym->a.func_call;
537 addr = 8;
538 loc = 0;
539 func_vc = 0;
541 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
542 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
543 fastcall_regs_ptr = fastcall_regs;
544 } else if (func_call == FUNC_FASTCALLW) {
545 fastcall_nb_regs = 2;
546 fastcall_regs_ptr = fastcallw_regs;
547 } else {
548 fastcall_nb_regs = 0;
549 fastcall_regs_ptr = NULL;
551 param_index = 0;
553 ind += FUNC_PROLOG_SIZE;
554 func_sub_sp_offset = ind;
555 /* if the function returns a structure, then add an
556 implicit pointer parameter */
557 func_vt = sym->type;
558 func_var = (sym->c == FUNC_ELLIPSIS);
559 #ifdef TCC_TARGET_PE
560 size = type_size(&func_vt,&align);
561 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
562 #else
563 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
564 #endif
565 /* XXX: fastcall case ? */
566 func_vc = addr;
567 addr += 4;
568 param_index++;
570 /* define parameters */
571 while ((sym = sym->next) != NULL) {
572 type = &sym->type;
573 size = type_size(type, &align);
574 size = (size + 3) & ~3;
575 #ifdef FUNC_STRUCT_PARAM_AS_PTR
576 /* structs are passed as pointer */
577 if ((type->t & VT_BTYPE) == VT_STRUCT) {
578 size = 4;
580 #endif
581 if (param_index < fastcall_nb_regs) {
582 /* save FASTCALL register */
583 loc -= 4;
584 o(0x89); /* movl */
585 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
586 param_addr = loc;
587 } else {
588 param_addr = addr;
589 addr += size;
591 sym_push(sym->v & ~SYM_FIELD, type,
592 VT_LOCAL | lvalue_type(type->t), param_addr);
593 param_index++;
595 func_ret_sub = 0;
596 /* pascal type call ? */
597 if (func_call == FUNC_STDCALL)
598 func_ret_sub = addr - 8;
599 #ifndef TCC_TARGET_PE
600 else if (func_vc)
601 func_ret_sub = 4;
602 #endif
604 #ifdef CONFIG_TCC_BCHECK
605 /* leave some room for bound checking code */
606 if (tcc_state->do_bounds_check) {
607 oad(0xb8, 0); /* lbound section pointer */
608 oad(0xb8, 0); /* call to function */
609 func_bound_offset = lbounds_section->data_offset;
611 #endif
614 /* generate function epilog */
615 ST_FUNC void gfunc_epilog(void)
617 addr_t v, saved_ind;
619 #ifdef CONFIG_TCC_BCHECK
620 if (tcc_state->do_bounds_check
621 && func_bound_offset != lbounds_section->data_offset) {
622 addr_t saved_ind;
623 addr_t *bounds_ptr;
624 Sym *sym_data;
625 /* add end of table info */
626 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
627 *bounds_ptr = 0;
628 /* generate bound local allocation */
629 saved_ind = ind;
630 ind = func_sub_sp_offset;
631 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
632 func_bound_offset, lbounds_section->data_offset);
633 greloc(cur_text_section, sym_data,
634 ind + 1, R_386_32);
635 oad(0xb8, 0); /* mov %eax, xxx */
636 gen_static_call(TOK___bound_local_new);
638 ind = saved_ind;
639 /* generate bound check local freeing */
640 o(0x5250); /* save returned value, if any */
641 greloc(cur_text_section, sym_data,
642 ind + 1, R_386_32);
643 oad(0xb8, 0); /* mov %eax, xxx */
644 gen_static_call(TOK___bound_local_delete);
646 o(0x585a); /* restore returned value, if any */
648 #endif
649 o(0xc9); /* leave */
650 if (func_ret_sub == 0) {
651 o(0xc3); /* ret */
652 } else {
653 o(0xc2); /* ret n */
654 g(func_ret_sub);
655 g(func_ret_sub >> 8);
657 /* align local size to word & save local variables */
659 v = (-loc + 3) & -4;
660 saved_ind = ind;
661 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
662 #ifdef TCC_TARGET_PE
663 if (v >= 4096) {
664 oad(0xb8, v); /* mov stacksize, %eax */
665 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
666 } else
667 #endif
669 o(0xe58955); /* push %ebp, mov %esp, %ebp */
670 o(0xec81); /* sub esp, stacksize */
671 gen_le32(v);
672 #if FUNC_PROLOG_SIZE == 10
673 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
674 #endif
676 ind = saved_ind;
679 /* generate a jump to a label */
680 ST_FUNC int gjmp(int t)
682 return psym(0xe9, t);
685 /* generate a jump to a fixed address */
686 ST_FUNC void gjmp_addr(int a)
688 int r;
689 r = a - ind - 2;
690 if (r == (char)r) {
691 g(0xeb);
692 g(r);
693 } else {
694 oad(0xe9, a - ind - 5);
698 ST_FUNC void gtst_addr(int inv, int a)
700 inv ^= (vtop--)->c.i;
701 a -= ind + 2;
702 if (a == (char)a) {
703 g(inv - 32);
704 g(a);
705 } else {
706 g(0x0f);
707 oad(inv - 16, a - 4);
711 /* generate a test. set 'inv' to invert test. Stack entry is popped */
712 ST_FUNC int gtst(int inv, int t)
714 int v = vtop->r & VT_VALMASK;
715 if (v == VT_CMP) {
716 /* fast case : can jump directly since flags are set */
717 g(0x0f);
718 t = psym((vtop->c.i - 16) ^ inv, t);
719 } else if (v == VT_JMP || v == VT_JMPI) {
720 /* && or || optimization */
721 if ((v & 1) == inv) {
722 /* insert vtop->c jump list in t */
723 uint32_t n1, n = vtop->c.i;
724 if (n) {
725 while ((n1 = read32le(cur_text_section->data + n)))
726 n = n1;
727 write32le(cur_text_section->data + n, t);
728 t = vtop->c.i;
730 } else {
731 t = gjmp(t);
732 gsym(vtop->c.i);
735 vtop--;
736 return t;
739 /* generate an integer binary operation */
740 ST_FUNC void gen_opi(int op)
742 int r, fr, opc, c;
744 switch(op) {
745 case '+':
746 case TOK_ADDC1: /* add with carry generation */
747 opc = 0;
748 gen_op8:
749 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
750 /* constant case */
751 vswap();
752 r = gv(RC_INT);
753 vswap();
754 c = vtop->c.i;
755 if (c == (char)c) {
756 /* generate inc and dec for smaller code */
757 if (c==1 && opc==0) {
758 o (0x40 | r); // inc
759 } else if (c==1 && opc==5) {
760 o (0x48 | r); // dec
761 } else {
762 o(0x83);
763 o(0xc0 | (opc << 3) | r);
764 g(c);
766 } else {
767 o(0x81);
768 oad(0xc0 | (opc << 3) | r, c);
770 } else {
771 gv2(RC_INT, RC_INT);
772 r = vtop[-1].r;
773 fr = vtop[0].r;
774 o((opc << 3) | 0x01);
775 o(0xc0 + r + fr * 8);
777 vtop--;
778 if (op >= TOK_ULT && op <= TOK_GT) {
779 vtop->r = VT_CMP;
780 vtop->c.i = 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 if (op == TOK_UMULL) {
852 o(0xf7); /* mul fr */
853 o(0xe0 + fr);
854 vtop->r2 = TREG_EDX;
855 r = TREG_EAX;
856 } else {
857 if (op == TOK_UDIV || op == TOK_UMOD) {
858 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
859 o(0xf0 + fr);
860 } else {
861 o(0xf799); /* cltd, idiv fr, %eax */
862 o(0xf8 + fr);
864 if (op == '%' || op == TOK_UMOD)
865 r = TREG_EDX;
866 else
867 r = TREG_EAX;
869 vtop->r = r;
870 break;
871 default:
872 opc = 7;
873 goto gen_op8;
877 /* generate a floating point operation 'v = t1 op t2' instruction. The
878 two operands are guaranted to have the same floating point type */
879 /* XXX: need to use ST1 too */
880 ST_FUNC void gen_opf(int op)
882 int a, ft, fc, swapped, r;
884 /* convert constants to memory references */
885 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
886 vswap();
887 gv(RC_FLOAT);
888 vswap();
890 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
891 gv(RC_FLOAT);
893 /* must put at least one value in the floating point register */
894 if ((vtop[-1].r & VT_LVAL) &&
895 (vtop[0].r & VT_LVAL)) {
896 vswap();
897 gv(RC_FLOAT);
898 vswap();
900 swapped = 0;
901 /* swap the stack if needed so that t1 is the register and t2 is
902 the memory reference */
903 if (vtop[-1].r & VT_LVAL) {
904 vswap();
905 swapped = 1;
907 if (op >= TOK_ULT && op <= TOK_GT) {
908 /* load on stack second operand */
909 load(TREG_ST0, vtop);
910 save_reg(TREG_EAX); /* eax is used by FP comparison code */
911 if (op == TOK_GE || op == TOK_GT)
912 swapped = !swapped;
913 else if (op == TOK_EQ || op == TOK_NE)
914 swapped = 0;
915 if (swapped)
916 o(0xc9d9); /* fxch %st(1) */
917 if (op == TOK_EQ || op == TOK_NE)
918 o(0xe9da); /* fucompp */
919 else
920 o(0xd9de); /* fcompp */
921 o(0xe0df); /* fnstsw %ax */
922 if (op == TOK_EQ) {
923 o(0x45e480); /* and $0x45, %ah */
924 o(0x40fC80); /* cmp $0x40, %ah */
925 } else if (op == TOK_NE) {
926 o(0x45e480); /* and $0x45, %ah */
927 o(0x40f480); /* xor $0x40, %ah */
928 op = TOK_NE;
929 } else if (op == TOK_GE || op == TOK_LE) {
930 o(0x05c4f6); /* test $0x05, %ah */
931 op = TOK_EQ;
932 } else {
933 o(0x45c4f6); /* test $0x45, %ah */
934 op = TOK_EQ;
936 vtop--;
937 vtop->r = VT_CMP;
938 vtop->c.i = op;
939 } else {
940 /* no memory reference possible for long double operations */
941 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
942 load(TREG_ST0, vtop);
943 swapped = !swapped;
946 switch(op) {
947 default:
948 case '+':
949 a = 0;
950 break;
951 case '-':
952 a = 4;
953 if (swapped)
954 a++;
955 break;
956 case '*':
957 a = 1;
958 break;
959 case '/':
960 a = 6;
961 if (swapped)
962 a++;
963 break;
965 ft = vtop->type.t;
966 fc = vtop->c.i;
967 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
968 o(0xde); /* fxxxp %st, %st(1) */
969 o(0xc1 + (a << 3));
970 } else {
971 /* if saved lvalue, then we must reload it */
972 r = vtop->r;
973 if ((r & VT_VALMASK) == VT_LLOCAL) {
974 SValue v1;
975 r = get_reg(RC_INT);
976 v1.type.t = VT_INT;
977 v1.r = VT_LOCAL | VT_LVAL;
978 v1.c.i = fc;
979 load(r, &v1);
980 fc = 0;
983 if ((ft & VT_BTYPE) == VT_DOUBLE)
984 o(0xdc);
985 else
986 o(0xd8);
987 gen_modrm(a, r, vtop->sym, fc);
989 vtop--;
993 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
994 and 'long long' cases. */
995 ST_FUNC void gen_cvt_itof(int t)
997 save_reg(TREG_ST0);
998 gv(RC_INT);
999 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1000 /* signed long long to float/double/long double (unsigned case
1001 is handled generically) */
1002 o(0x50 + vtop->r2); /* push r2 */
1003 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1004 o(0x242cdf); /* fildll (%esp) */
1005 o(0x08c483); /* add $8, %esp */
1006 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1007 (VT_INT | VT_UNSIGNED)) {
1008 /* unsigned int to float/double/long double */
1009 o(0x6a); /* push $0 */
1010 g(0x00);
1011 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1012 o(0x242cdf); /* fildll (%esp) */
1013 o(0x08c483); /* add $8, %esp */
1014 } else {
1015 /* int to float/double/long double */
1016 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1017 o(0x2404db); /* fildl (%esp) */
1018 o(0x04c483); /* add $4, %esp */
1020 vtop->r = TREG_ST0;
1023 /* convert fp to int 't' type */
1024 ST_FUNC void gen_cvt_ftoi(int t)
1026 #ifndef COMMIT_4ad186c5ef61_IS_FIXED
1027 /* a good version but it takes a more time to execute */
1028 gv(RC_FLOAT);
1029 save_reg(TREG_EAX);
1030 save_reg(TREG_EDX);
1031 gen_static_call(TOK___tcc_cvt_ftol);
1032 vtop->r = TREG_EAX; /* mark reg as used */
1033 if (t == VT_LLONG)
1034 vtop->r2 = TREG_EDX;
1035 #else
1036 /* a new version with a bug: t2a = 44100312 */
1038 #include<stdio.h>
1039 int main() {
1040 int t1 = 176401255;
1041 float f = 0.25f;
1042 int t2a = (int)(t1 * f); // must be 44100313
1043 int t2b = (int)(t1 * (float)0.25f);
1044 printf("t2a=%d t2b=%d \n",t2a,t2b);
1045 return 0;
1048 int bt = vtop->type.t & VT_BTYPE;
1049 if (bt == VT_FLOAT)
1050 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1051 else if (bt == VT_LDOUBLE)
1052 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1053 else
1054 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1055 vswap();
1056 gfunc_call(1);
1057 vpushi(0);
1058 vtop->r = REG_IRET;
1059 vtop->r2 = REG_LRET;
1060 #endif
1063 /* convert from one floating point type to another */
1064 ST_FUNC void gen_cvt_ftof(int t)
1066 /* all we have to do on i386 is to put the float in a register */
1067 gv(RC_FLOAT);
1070 /* computed goto support */
1071 ST_FUNC void ggoto(void)
1073 gcall_or_jmp(1);
1074 vtop--;
1077 /* bound check support functions */
1078 #ifdef CONFIG_TCC_BCHECK
1080 /* generate a bounded pointer addition */
1081 ST_FUNC void gen_bounded_ptr_add(void)
1083 /* prepare fast i386 function call (args in eax and edx) */
1084 gv2(RC_EAX, RC_EDX);
1085 /* save all temporary registers */
1086 vtop -= 2;
1087 save_regs(0);
1088 /* do a fast function call */
1089 gen_static_call(TOK___bound_ptr_add);
1090 /* returned pointer is in eax */
1091 vtop++;
1092 vtop->r = TREG_EAX | VT_BOUNDED;
1093 /* address of bounding function call point */
1094 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1097 /* patch pointer addition in vtop so that pointer dereferencing is
1098 also tested */
1099 ST_FUNC void gen_bounded_ptr_deref(void)
1101 addr_t func;
1102 int size, align;
1103 Elf32_Rel *rel;
1104 Sym *sym;
1106 size = 0;
1107 /* XXX: put that code in generic part of tcc */
1108 if (!is_float(vtop->type.t)) {
1109 if (vtop->r & VT_LVAL_BYTE)
1110 size = 1;
1111 else if (vtop->r & VT_LVAL_SHORT)
1112 size = 2;
1114 if (!size)
1115 size = type_size(&vtop->type, &align);
1116 switch(size) {
1117 case 1: func = TOK___bound_ptr_indir1; break;
1118 case 2: func = TOK___bound_ptr_indir2; break;
1119 case 4: func = TOK___bound_ptr_indir4; break;
1120 case 8: func = TOK___bound_ptr_indir8; break;
1121 case 12: func = TOK___bound_ptr_indir12; break;
1122 case 16: func = TOK___bound_ptr_indir16; break;
1123 default:
1124 tcc_error("unhandled size when dereferencing bounded pointer");
1125 func = 0;
1126 break;
1129 /* patch relocation */
1130 /* XXX: find a better solution ? */
1131 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1132 sym = external_global_sym(func, &func_old_type, 0);
1133 if (!sym->c)
1134 put_extern_sym(sym, NULL, 0, 0);
1135 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1137 #endif
1139 /* Save the stack pointer onto the stack */
1140 ST_FUNC void gen_vla_sp_save(int addr) {
1141 /* mov %esp,addr(%ebp)*/
1142 o(0x89);
1143 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1146 /* Restore the SP from a location on the stack */
1147 ST_FUNC void gen_vla_sp_restore(int addr) {
1148 o(0x8b);
1149 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1152 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1153 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1154 #ifdef TCC_TARGET_PE
1155 /* alloca does more than just adjust %rsp on Windows */
1156 vpush_global_sym(&func_old_type, TOK_alloca);
1157 vswap(); /* Move alloca ref past allocation size */
1158 gfunc_call(1);
1159 #else
1160 int r;
1161 r = gv(RC_INT); /* allocation size */
1162 /* sub r,%rsp */
1163 o(0x2b);
1164 o(0xe0 | r);
1165 /* We align to 16 bytes rather than align */
1166 /* and ~15, %esp */
1167 o(0xf0e483);
1168 vpop();
1169 #endif
1172 /* end of X86 code generator */
1173 /*************************************************************/
1174 #endif
1175 /*************************************************************/