bitfields: Implement MS compatible layout
[tinycc.git] / i386-gen.c
blobcd4cebde2639a3d6e0681b70fb478c993f3046c3
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 #else /* ! TARGET_DEFS_ONLY */
78 /******************************************************/
79 #include "tcc.h"
81 /* define to 1/0 to [not] have EBX as 4th register */
82 #define USE_EBX 1
84 ST_DATA const int reg_classes[NB_REGS] = {
85 /* eax */ RC_INT | RC_EAX,
86 /* ecx */ RC_INT | RC_ECX,
87 /* edx */ RC_INT | RC_EDX,
88 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
89 /* st0 */ RC_FLOAT | RC_ST0,
92 static unsigned long func_sub_sp_offset;
93 static int func_ret_sub;
94 #ifdef CONFIG_TCC_BCHECK
95 static addr_t func_bound_offset;
96 static unsigned long func_bound_ind;
97 #endif
99 /* XXX: make it faster ? */
100 ST_FUNC void g(int c)
102 int ind1;
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 /* psym is used to put an instruction with a data field which is a
149 reference to a symbol. It is in fact the same as oad ! */
150 #define psym oad
152 /* instruction + 4 bytes data. Return the address of the data */
153 ST_FUNC int oad(int c, int s)
155 int ind1;
157 o(c);
158 ind1 = ind + 4;
159 if (ind1 > cur_text_section->data_allocated)
160 section_realloc(cur_text_section, ind1);
161 write32le(cur_text_section->data + ind, s);
162 s = ind;
163 ind = ind1;
164 return s;
167 /* output constant with relocation if 'r & VT_SYM' is true */
168 ST_FUNC void gen_addr32(int r, Sym *sym, long c)
170 if (r & VT_SYM)
171 greloc(cur_text_section, sym, ind, R_386_32);
172 gen_le32(c);
175 ST_FUNC void gen_addrpc32(int r, Sym *sym, long c)
177 if (r & VT_SYM)
178 greloc(cur_text_section, sym, ind, R_386_PC32);
179 gen_le32(c - 4);
182 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
183 opcode bits */
184 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
186 op_reg = op_reg << 3;
187 if ((r & VT_VALMASK) == VT_CONST) {
188 /* constant memory reference */
189 o(0x05 | op_reg);
190 gen_addr32(r, sym, c);
191 } else if ((r & VT_VALMASK) == VT_LOCAL) {
192 /* currently, we use only ebp as base */
193 if (c == (char)c) {
194 /* short reference */
195 o(0x45 | op_reg);
196 g(c);
197 } else {
198 oad(0x85 | op_reg, c);
200 } else {
201 g(0x00 | op_reg | (r & VT_VALMASK));
205 /* load 'r' from value 'sv' */
206 ST_FUNC void load(int r, SValue *sv)
208 int v, t, ft, fc, fr;
209 SValue v1;
211 #ifdef TCC_TARGET_PE
212 SValue v2;
213 sv = pe_getimport(sv, &v2);
214 #endif
216 fr = sv->r;
217 ft = sv->type.t;
218 fc = sv->c.i;
220 ft &= ~(VT_VOLATILE | VT_CONSTANT);
222 v = fr & VT_VALMASK;
223 if (fr & VT_LVAL) {
224 if (v == VT_LLOCAL) {
225 v1.type.t = VT_INT;
226 v1.r = VT_LOCAL | VT_LVAL;
227 v1.c.i = fc;
228 fr = r;
229 if (!(reg_classes[fr] & RC_INT))
230 fr = get_reg(RC_INT);
231 load(fr, &v1);
233 if ((ft & VT_BTYPE) == VT_FLOAT) {
234 o(0xd9); /* flds */
235 r = 0;
236 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
237 o(0xdd); /* fldl */
238 r = 0;
239 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
240 o(0xdb); /* fldt */
241 r = 5;
242 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
243 o(0xbe0f); /* movsbl */
244 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
245 o(0xb60f); /* movzbl */
246 } else if ((ft & VT_TYPE) == VT_SHORT) {
247 o(0xbf0f); /* movswl */
248 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
249 o(0xb70f); /* movzwl */
250 } else {
251 o(0x8b); /* movl */
253 gen_modrm(r, fr, sv->sym, fc);
254 } else {
255 if (v == VT_CONST) {
256 o(0xb8 + r); /* mov $xx, r */
257 gen_addr32(fr, sv->sym, fc);
258 } else if (v == VT_LOCAL) {
259 if (fc) {
260 o(0x8d); /* lea xxx(%ebp), r */
261 gen_modrm(r, VT_LOCAL, sv->sym, fc);
262 } else {
263 o(0x89);
264 o(0xe8 + r); /* mov %ebp, r */
266 } else if (v == VT_CMP) {
267 oad(0xb8 + r, 0); /* mov $0, r */
268 o(0x0f); /* setxx %br */
269 o(fc);
270 o(0xc0 + r);
271 } else if (v == VT_JMP || v == VT_JMPI) {
272 t = v & 1;
273 oad(0xb8 + r, t); /* mov $1, r */
274 o(0x05eb); /* jmp after */
275 gsym(fc);
276 oad(0xb8 + r, t ^ 1); /* mov $0, r */
277 } else if (v != r) {
278 o(0x89);
279 o(0xc0 + r + v * 8); /* mov v, r */
284 /* store register 'r' in lvalue 'v' */
285 ST_FUNC void store(int r, SValue *v)
287 int fr, bt, ft, fc;
289 #ifdef TCC_TARGET_PE
290 SValue v2;
291 v = pe_getimport(v, &v2);
292 #endif
294 ft = v->type.t;
295 fc = v->c.i;
296 fr = v->r & VT_VALMASK;
297 ft &= ~(VT_VOLATILE | VT_CONSTANT);
298 bt = ft & VT_BTYPE;
299 /* XXX: incorrect if float reg to reg */
300 if (bt == VT_FLOAT) {
301 o(0xd9); /* fsts */
302 r = 2;
303 } else if (bt == VT_DOUBLE) {
304 o(0xdd); /* fstpl */
305 r = 2;
306 } else if (bt == VT_LDOUBLE) {
307 o(0xc0d9); /* fld %st(0) */
308 o(0xdb); /* fstpt */
309 r = 7;
310 } else {
311 if (bt == VT_SHORT)
312 o(0x66);
313 if (bt == VT_BYTE || bt == VT_BOOL)
314 o(0x88);
315 else
316 o(0x89);
318 if (fr == VT_CONST ||
319 fr == VT_LOCAL ||
320 (v->r & VT_LVAL)) {
321 gen_modrm(r, v->r, v->sym, fc);
322 } else if (fr != r) {
323 o(0xc0 + fr + r * 8); /* mov r, fr */
327 static void gadd_sp(int val)
329 if (val == (char)val) {
330 o(0xc483);
331 g(val);
332 } else {
333 oad(0xc481, val); /* add $xxx, %esp */
337 static void gen_static_call(int v)
339 Sym *sym;
341 sym = external_global_sym(v, &func_old_type, 0);
342 oad(0xe8, -4);
343 greloc(cur_text_section, sym, ind-4, R_386_PC32);
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) {
351 int rt;
352 /* constant case */
353 if (vtop->r & VT_SYM) {
354 /* relocation case */
355 greloc(cur_text_section, vtop->sym,
356 ind + 1, R_386_PC32);
357 } else {
358 /* put an empty PC32 relocation */
359 put_elf_reloc(symtab_section, cur_text_section,
360 ind + 1, R_386_PC32, 0);
362 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
363 /* extend the return value to the whole register if necessary
364 visual studio and gcc do not always set the whole eax register
365 when assigning the return value of a function */
366 rt = vtop->type.ref->type.t;
367 switch (rt & VT_BTYPE) {
368 case VT_BYTE:
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;
387 } else {
388 /* otherwise, indirect call */
389 r = gv(RC_INT);
390 o(0xff); /* call/jmp *r */
391 o(0xd0 + r + (is_jmp << 4));
395 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
396 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
398 /* Return the number of registers needed to return the struct, or 0 if
399 returning via struct pointer. */
400 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
402 #ifdef TCC_TARGET_PE
403 int size, align;
405 *ret_align = 1; // Never have to re-align return values for x86
406 *regsize = 4;
407 size = type_size(vt, &align);
408 if (size > 8) {
409 return 0;
410 } else if (size > 4) {
411 ret->ref = NULL;
412 ret->t = VT_LLONG;
413 return 1;
414 } else {
415 ret->ref = NULL;
416 ret->t = VT_INT;
417 return 1;
419 #else
420 *ret_align = 1; // Never have to re-align return values for x86
421 return 0;
422 #endif
425 /* Generate function call. The function address is pushed first, then
426 all the parameters in call order. This functions pops all the
427 parameters and the function address. */
428 ST_FUNC void gfunc_call(int nb_args)
430 int size, align, r, args_size, i, func_call;
431 Sym *func_sym;
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->a.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)
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(CType *func_type)
523 int addr, align, size, func_call, fastcall_nb_regs;
524 int param_index, param_addr;
525 uint8_t *fastcall_regs_ptr;
526 Sym *sym;
527 CType *type;
529 sym = func_type->ref;
530 func_call = sym->a.func_call;
531 addr = 8;
532 loc = 0;
533 func_vc = 0;
535 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
536 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
537 fastcall_regs_ptr = fastcall_regs;
538 } else if (func_call == FUNC_FASTCALLW) {
539 fastcall_nb_regs = 2;
540 fastcall_regs_ptr = fastcallw_regs;
541 } else {
542 fastcall_nb_regs = 0;
543 fastcall_regs_ptr = NULL;
545 param_index = 0;
547 ind += FUNC_PROLOG_SIZE;
548 func_sub_sp_offset = ind;
549 /* if the function returns a structure, then add an
550 implicit pointer parameter */
551 func_vt = sym->type;
552 func_var = (sym->c == FUNC_ELLIPSIS);
553 #ifdef TCC_TARGET_PE
554 size = type_size(&func_vt,&align);
555 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
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 ? */
591 if (func_call == FUNC_STDCALL)
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
644 o(0x5b * USE_EBX); /* pop ebx */
645 o(0xc9); /* leave */
646 if (func_ret_sub == 0) {
647 o(0xc3); /* ret */
648 } else {
649 o(0xc2); /* ret n */
650 g(func_ret_sub);
651 g(func_ret_sub >> 8);
653 /* align local size to word & save local variables */
655 v = (-loc + 3) & -4;
656 saved_ind = ind;
657 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
658 #ifdef TCC_TARGET_PE
659 if (v >= 4096) {
660 oad(0xb8, v); /* mov stacksize, %eax */
661 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
662 } else
663 #endif
665 o(0xe58955); /* push %ebp, mov %esp, %ebp */
666 o(0xec81); /* sub esp, stacksize */
667 gen_le32(v);
668 #ifdef TCC_TARGET_PE
669 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
670 #endif
672 o(0x53 * USE_EBX); /* push ebx */
673 ind = saved_ind;
676 /* generate a jump to a label */
677 ST_FUNC int gjmp(int t)
679 return psym(0xe9, t);
682 /* generate a jump to a fixed address */
683 ST_FUNC void gjmp_addr(int a)
685 int r;
686 r = a - ind - 2;
687 if (r == (char)r) {
688 g(0xeb);
689 g(r);
690 } else {
691 oad(0xe9, a - ind - 5);
695 ST_FUNC void gtst_addr(int inv, int a)
697 inv ^= (vtop--)->c.i;
698 a -= ind + 2;
699 if (a == (char)a) {
700 g(inv - 32);
701 g(a);
702 } else {
703 g(0x0f);
704 oad(inv - 16, a - 4);
708 /* generate a test. set 'inv' to invert test. Stack entry is popped */
709 ST_FUNC int gtst(int inv, int t)
711 int v = vtop->r & VT_VALMASK;
712 if (v == VT_CMP) {
713 /* fast case : can jump directly since flags are set */
714 g(0x0f);
715 t = psym((vtop->c.i - 16) ^ inv, t);
716 } else if (v == VT_JMP || v == VT_JMPI) {
717 /* && or || optimization */
718 if ((v & 1) == inv) {
719 /* insert vtop->c jump list in t */
720 uint32_t n1, n = vtop->c.i;
721 if (n) {
722 while ((n1 = read32le(cur_text_section->data + n)))
723 n = n1;
724 write32le(cur_text_section->data + n, t);
725 t = vtop->c.i;
727 } else {
728 t = gjmp(t);
729 gsym(vtop->c.i);
732 vtop--;
733 return t;
736 /* generate an integer binary operation */
737 ST_FUNC void gen_opi(int op)
739 int r, fr, opc, c;
741 switch(op) {
742 case '+':
743 case TOK_ADDC1: /* add with carry generation */
744 opc = 0;
745 gen_op8:
746 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
747 /* constant case */
748 vswap();
749 r = gv(RC_INT);
750 vswap();
751 c = vtop->c.i;
752 if (c == (char)c) {
753 /* generate inc and dec for smaller code */
754 if (c==1 && opc==0 && op != TOK_ADDC1) {
755 o (0x40 | r); // inc
756 } else if (c==1 && opc==5 && op != TOK_SUBC1) {
757 o (0x48 | r); // dec
758 } else {
759 o(0x83);
760 o(0xc0 | (opc << 3) | r);
761 g(c);
763 } else {
764 o(0x81);
765 oad(0xc0 | (opc << 3) | r, c);
767 } else {
768 gv2(RC_INT, RC_INT);
769 r = vtop[-1].r;
770 fr = vtop[0].r;
771 o((opc << 3) | 0x01);
772 o(0xc0 + r + fr * 8);
774 vtop--;
775 if (op >= TOK_ULT && op <= TOK_GT) {
776 vtop->r = VT_CMP;
777 vtop->c.i = op;
779 break;
780 case '-':
781 case TOK_SUBC1: /* sub with carry generation */
782 opc = 5;
783 goto gen_op8;
784 case TOK_ADDC2: /* add with carry use */
785 opc = 2;
786 goto gen_op8;
787 case TOK_SUBC2: /* sub with carry use */
788 opc = 3;
789 goto gen_op8;
790 case '&':
791 opc = 4;
792 goto gen_op8;
793 case '^':
794 opc = 6;
795 goto gen_op8;
796 case '|':
797 opc = 1;
798 goto gen_op8;
799 case '*':
800 gv2(RC_INT, RC_INT);
801 r = vtop[-1].r;
802 fr = vtop[0].r;
803 vtop--;
804 o(0xaf0f); /* imul fr, r */
805 o(0xc0 + fr + r * 8);
806 break;
807 case TOK_SHL:
808 opc = 4;
809 goto gen_shift;
810 case TOK_SHR:
811 opc = 5;
812 goto gen_shift;
813 case TOK_SAR:
814 opc = 7;
815 gen_shift:
816 opc = 0xc0 | (opc << 3);
817 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
818 /* constant case */
819 vswap();
820 r = gv(RC_INT);
821 vswap();
822 c = vtop->c.i & 0x1f;
823 o(0xc1); /* shl/shr/sar $xxx, r */
824 o(opc | r);
825 g(c);
826 } else {
827 /* we generate the shift in ecx */
828 gv2(RC_INT, RC_ECX);
829 r = vtop[-1].r;
830 o(0xd3); /* shl/shr/sar %cl, r */
831 o(opc | r);
833 vtop--;
834 break;
835 case '/':
836 case TOK_UDIV:
837 case TOK_PDIV:
838 case '%':
839 case TOK_UMOD:
840 case TOK_UMULL:
841 /* first operand must be in eax */
842 /* XXX: need better constraint for second operand */
843 gv2(RC_EAX, RC_ECX);
844 r = vtop[-1].r;
845 fr = vtop[0].r;
846 vtop--;
847 save_reg(TREG_EDX);
848 /* save EAX too if used otherwise */
849 save_reg_upstack(TREG_EAX, 1);
850 if (op == TOK_UMULL) {
851 o(0xf7); /* mul fr */
852 o(0xe0 + fr);
853 vtop->r2 = TREG_EDX;
854 r = TREG_EAX;
855 } else {
856 if (op == TOK_UDIV || op == TOK_UMOD) {
857 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
858 o(0xf0 + fr);
859 } else {
860 o(0xf799); /* cltd, idiv fr, %eax */
861 o(0xf8 + fr);
863 if (op == '%' || op == TOK_UMOD)
864 r = TREG_EDX;
865 else
866 r = TREG_EAX;
868 vtop->r = r;
869 break;
870 default:
871 opc = 7;
872 goto gen_op8;
876 /* generate a floating point operation 'v = t1 op t2' instruction. The
877 two operands are guaranted to have the same floating point type */
878 /* XXX: need to use ST1 too */
879 ST_FUNC void gen_opf(int op)
881 int a, ft, fc, swapped, r;
883 /* convert constants to memory references */
884 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
885 vswap();
886 gv(RC_FLOAT);
887 vswap();
889 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
890 gv(RC_FLOAT);
892 /* must put at least one value in the floating point register */
893 if ((vtop[-1].r & VT_LVAL) &&
894 (vtop[0].r & VT_LVAL)) {
895 vswap();
896 gv(RC_FLOAT);
897 vswap();
899 swapped = 0;
900 /* swap the stack if needed so that t1 is the register and t2 is
901 the memory reference */
902 if (vtop[-1].r & VT_LVAL) {
903 vswap();
904 swapped = 1;
906 if (op >= TOK_ULT && op <= TOK_GT) {
907 /* load on stack second operand */
908 load(TREG_ST0, vtop);
909 save_reg(TREG_EAX); /* eax is used by FP comparison code */
910 if (op == TOK_GE || op == TOK_GT)
911 swapped = !swapped;
912 else if (op == TOK_EQ || op == TOK_NE)
913 swapped = 0;
914 if (swapped)
915 o(0xc9d9); /* fxch %st(1) */
916 if (op == TOK_EQ || op == TOK_NE)
917 o(0xe9da); /* fucompp */
918 else
919 o(0xd9de); /* fcompp */
920 o(0xe0df); /* fnstsw %ax */
921 if (op == TOK_EQ) {
922 o(0x45e480); /* and $0x45, %ah */
923 o(0x40fC80); /* cmp $0x40, %ah */
924 } else if (op == TOK_NE) {
925 o(0x45e480); /* and $0x45, %ah */
926 o(0x40f480); /* xor $0x40, %ah */
927 op = TOK_NE;
928 } else if (op == TOK_GE || op == TOK_LE) {
929 o(0x05c4f6); /* test $0x05, %ah */
930 op = TOK_EQ;
931 } else {
932 o(0x45c4f6); /* test $0x45, %ah */
933 op = TOK_EQ;
935 vtop--;
936 vtop->r = VT_CMP;
937 vtop->c.i = op;
938 } else {
939 /* no memory reference possible for long double operations */
940 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
941 load(TREG_ST0, vtop);
942 swapped = !swapped;
945 switch(op) {
946 default:
947 case '+':
948 a = 0;
949 break;
950 case '-':
951 a = 4;
952 if (swapped)
953 a++;
954 break;
955 case '*':
956 a = 1;
957 break;
958 case '/':
959 a = 6;
960 if (swapped)
961 a++;
962 break;
964 ft = vtop->type.t;
965 fc = vtop->c.i;
966 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
967 o(0xde); /* fxxxp %st, %st(1) */
968 o(0xc1 + (a << 3));
969 } else {
970 /* if saved lvalue, then we must reload it */
971 r = vtop->r;
972 if ((r & VT_VALMASK) == VT_LLOCAL) {
973 SValue v1;
974 r = get_reg(RC_INT);
975 v1.type.t = VT_INT;
976 v1.r = VT_LOCAL | VT_LVAL;
977 v1.c.i = fc;
978 load(r, &v1);
979 fc = 0;
982 if ((ft & VT_BTYPE) == VT_DOUBLE)
983 o(0xdc);
984 else
985 o(0xd8);
986 gen_modrm(a, r, vtop->sym, fc);
988 vtop--;
992 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
993 and 'long long' cases. */
994 ST_FUNC void gen_cvt_itof(int t)
996 save_reg(TREG_ST0);
997 gv(RC_INT);
998 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
999 /* signed long long to float/double/long double (unsigned case
1000 is handled generically) */
1001 o(0x50 + vtop->r2); /* push r2 */
1002 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1003 o(0x242cdf); /* fildll (%esp) */
1004 o(0x08c483); /* add $8, %esp */
1005 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1006 (VT_INT | VT_UNSIGNED)) {
1007 /* unsigned int to float/double/long double */
1008 o(0x6a); /* push $0 */
1009 g(0x00);
1010 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1011 o(0x242cdf); /* fildll (%esp) */
1012 o(0x08c483); /* add $8, %esp */
1013 } else {
1014 /* int to float/double/long double */
1015 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1016 o(0x2404db); /* fildl (%esp) */
1017 o(0x04c483); /* add $4, %esp */
1019 vtop->r = TREG_ST0;
1022 /* convert fp to int 't' type */
1023 ST_FUNC void gen_cvt_ftoi(int t)
1025 #if 1
1026 gv(RC_FLOAT);
1027 save_reg(TREG_EAX);
1028 save_reg(TREG_EDX);
1029 gen_static_call(TOK___tcc_cvt_ftol);
1030 vtop->r = TREG_EAX; /* mark reg as used */
1031 if (t == VT_LLONG)
1032 vtop->r2 = TREG_EDX;
1033 #else
1034 int bt = vtop->type.t & VT_BTYPE;
1035 if (bt == VT_FLOAT)
1036 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1037 else if (bt == VT_LDOUBLE)
1038 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1039 else
1040 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1041 vswap();
1042 gfunc_call(1);
1043 vpushi(0);
1044 vtop->r = REG_IRET;
1045 vtop->r2 = REG_LRET;
1046 #endif
1049 /* convert from one floating point type to another */
1050 ST_FUNC void gen_cvt_ftof(int t)
1052 /* all we have to do on i386 is to put the float in a register */
1053 gv(RC_FLOAT);
1056 /* computed goto support */
1057 ST_FUNC void ggoto(void)
1059 gcall_or_jmp(1);
1060 vtop--;
1063 /* bound check support functions */
1064 #ifdef CONFIG_TCC_BCHECK
1066 /* generate a bounded pointer addition */
1067 ST_FUNC void gen_bounded_ptr_add(void)
1069 /* prepare fast i386 function call (args in eax and edx) */
1070 gv2(RC_EAX, RC_EDX);
1071 /* save all temporary registers */
1072 vtop -= 2;
1073 save_regs(0);
1074 /* do a fast function call */
1075 gen_static_call(TOK___bound_ptr_add);
1076 /* returned pointer is in eax */
1077 vtop++;
1078 vtop->r = TREG_EAX | VT_BOUNDED;
1079 /* address of bounding function call point */
1080 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1083 /* patch pointer addition in vtop so that pointer dereferencing is
1084 also tested */
1085 ST_FUNC void gen_bounded_ptr_deref(void)
1087 addr_t func;
1088 int size, align;
1089 Elf32_Rel *rel;
1090 Sym *sym;
1092 size = 0;
1093 /* XXX: put that code in generic part of tcc */
1094 if (!is_float(vtop->type.t)) {
1095 if (vtop->r & VT_LVAL_BYTE)
1096 size = 1;
1097 else if (vtop->r & VT_LVAL_SHORT)
1098 size = 2;
1100 if (!size)
1101 size = type_size(&vtop->type, &align);
1102 switch(size) {
1103 case 1: func = TOK___bound_ptr_indir1; break;
1104 case 2: func = TOK___bound_ptr_indir2; break;
1105 case 4: func = TOK___bound_ptr_indir4; break;
1106 case 8: func = TOK___bound_ptr_indir8; break;
1107 case 12: func = TOK___bound_ptr_indir12; break;
1108 case 16: func = TOK___bound_ptr_indir16; break;
1109 default:
1110 tcc_error("unhandled size when dereferencing bounded pointer");
1111 func = 0;
1112 break;
1115 /* patch relocation */
1116 /* XXX: find a better solution ? */
1117 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1118 sym = external_global_sym(func, &func_old_type, 0);
1119 if (!sym->c)
1120 put_extern_sym(sym, NULL, 0, 0);
1121 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1123 #endif
1125 /* Save the stack pointer onto the stack */
1126 ST_FUNC void gen_vla_sp_save(int addr) {
1127 /* mov %esp,addr(%ebp)*/
1128 o(0x89);
1129 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1132 /* Restore the SP from a location on the stack */
1133 ST_FUNC void gen_vla_sp_restore(int addr) {
1134 o(0x8b);
1135 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1138 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1139 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1140 #ifdef TCC_TARGET_PE
1141 /* alloca does more than just adjust %rsp on Windows */
1142 vpush_global_sym(&func_old_type, TOK_alloca);
1143 vswap(); /* Move alloca ref past allocation size */
1144 gfunc_call(1);
1145 #else
1146 int r;
1147 r = gv(RC_INT); /* allocation size */
1148 /* sub r,%rsp */
1149 o(0x2b);
1150 o(0xe0 | r);
1151 /* We align to 16 bytes rather than align */
1152 /* and ~15, %esp */
1153 o(0xf0e483);
1154 vpop();
1155 #endif
1158 /* end of X86 code generator */
1159 /*************************************************************/
1160 #endif
1161 /*************************************************************/