riscv: fp parameters
[tinycc.git] / i386-gen.c
blob51fbf073671b7f3781ba071c238734b0ac7e2b8d
1 /*
2 * X86 code generator for TCC
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifdef TARGET_DEFS_ONLY
23 /* number of available registers */
24 #define NB_REGS 5
25 #define NB_ASM_REGS 8
26 #define CONFIG_TCC_ASM
28 /* a register can belong to several classes. The classes must be
29 sorted from more general to more precise (see gv2() code which does
30 assumptions on it). */
31 #define RC_INT 0x0001 /* generic integer register */
32 #define RC_FLOAT 0x0002 /* generic float register */
33 #define RC_EAX 0x0004
34 #define RC_ST0 0x0008
35 #define RC_ECX 0x0010
36 #define RC_EDX 0x0020
37 #define RC_EBX 0x0040
39 #define RC_IRET RC_EAX /* function return: integer register */
40 #define RC_LRET RC_EDX /* function return: second integer register */
41 #define RC_FRET RC_ST0 /* function return: float register */
43 /* pretty names for the registers */
44 enum {
45 TREG_EAX = 0,
46 TREG_ECX,
47 TREG_EDX,
48 TREG_EBX,
49 TREG_ST0,
50 TREG_ESP = 4
53 /* return registers for function */
54 #define REG_IRET TREG_EAX /* single word int return register */
55 #define REG_LRET TREG_EDX /* second word return register (for long long) */
56 #define REG_FRET TREG_ST0 /* float return register */
58 /* defined if function parameters must be evaluated in reverse order */
59 #define INVERT_FUNC_PARAMS
61 /* defined if structures are passed as pointers. Otherwise structures
62 are directly pushed on stack. */
63 /* #define FUNC_STRUCT_PARAM_AS_PTR */
65 /* pointer size, in bytes */
66 #define PTR_SIZE 4
68 /* long double size and alignment, in bytes */
69 #define LDOUBLE_SIZE 12
70 #define LDOUBLE_ALIGN 4
71 /* maximum alignment (for aligned attribute support) */
72 #define MAX_ALIGN 8
74 /******************************************************/
75 #else /* ! TARGET_DEFS_ONLY */
76 /******************************************************/
77 #include "tcc.h"
79 /* define to 1/0 to [not] have EBX as 4th register */
80 #define USE_EBX 0
82 ST_DATA const int reg_classes[NB_REGS] = {
83 /* eax */ RC_INT | RC_EAX,
84 /* ecx */ RC_INT | RC_ECX,
85 /* edx */ RC_INT | RC_EDX,
86 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
87 /* st0 */ RC_FLOAT | RC_ST0,
90 static unsigned long func_sub_sp_offset;
91 static int func_ret_sub;
92 #ifdef CONFIG_TCC_BCHECK
93 static addr_t func_bound_offset;
94 static unsigned long func_bound_ind;
95 #endif
97 /* XXX: make it faster ? */
98 ST_FUNC void g(int c)
100 int ind1;
101 if (nocode_wanted)
102 return;
103 ind1 = ind + 1;
104 if (ind1 > cur_text_section->data_allocated)
105 section_realloc(cur_text_section, ind1);
106 cur_text_section->data[ind] = c;
107 ind = ind1;
110 ST_FUNC void o(unsigned int c)
112 while (c) {
113 g(c);
114 c = c >> 8;
118 ST_FUNC void gen_le16(int v)
120 g(v);
121 g(v >> 8);
124 ST_FUNC void gen_le32(int c)
126 g(c);
127 g(c >> 8);
128 g(c >> 16);
129 g(c >> 24);
132 /* output a symbol and patch all calls to it */
133 ST_FUNC void gsym_addr(int t, int a)
135 while (t) {
136 unsigned char *ptr = cur_text_section->data + t;
137 uint32_t n = read32le(ptr); /* next value */
138 write32le(ptr, a - t - 4);
139 t = n;
143 /* instruction + 4 bytes data. Return the address of the data */
144 static int oad(int c, int s)
146 int t;
147 if (nocode_wanted)
148 return s;
149 o(c);
150 t = ind;
151 gen_le32(s);
152 return t;
155 ST_FUNC void gen_fill_nops(int bytes)
157 while (bytes--)
158 g(0x90);
161 /* generate jmp to a label */
162 #define gjmp2(instr,lbl) oad(instr,lbl)
164 /* output constant with relocation if 'r & VT_SYM' is true */
165 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
167 if (r & VT_SYM)
168 greloc(cur_text_section, sym, ind, R_386_32);
169 gen_le32(c);
172 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
174 if (r & VT_SYM)
175 greloc(cur_text_section, sym, ind, R_386_PC32);
176 gen_le32(c - 4);
179 /* generate a modrm reference. 'op_reg' contains the additional 3
180 opcode bits */
181 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
183 op_reg = op_reg << 3;
184 if ((r & VT_VALMASK) == VT_CONST) {
185 /* constant memory reference */
186 o(0x05 | op_reg);
187 gen_addr32(r, sym, c);
188 } else if ((r & VT_VALMASK) == VT_LOCAL) {
189 /* currently, we use only ebp as base */
190 if (c == (char)c) {
191 /* short reference */
192 o(0x45 | op_reg);
193 g(c);
194 } else {
195 oad(0x85 | op_reg, c);
197 } else {
198 g(0x00 | op_reg | (r & VT_VALMASK));
202 /* load 'r' from value 'sv' */
203 ST_FUNC void load(int r, SValue *sv)
205 int v, t, ft, fc, fr;
206 SValue v1;
208 #ifdef TCC_TARGET_PE
209 SValue v2;
210 sv = pe_getimport(sv, &v2);
211 #endif
213 fr = sv->r;
214 ft = sv->type.t & ~VT_DEFSIGN;
215 fc = sv->c.i;
217 ft &= ~(VT_VOLATILE | VT_CONSTANT);
219 v = fr & VT_VALMASK;
220 if (fr & VT_LVAL) {
221 if (v == VT_LLOCAL) {
222 v1.type.t = VT_INT;
223 v1.r = VT_LOCAL | VT_LVAL;
224 v1.c.i = fc;
225 fr = r;
226 if (!(reg_classes[fr] & RC_INT))
227 fr = get_reg(RC_INT);
228 load(fr, &v1);
230 if ((ft & VT_BTYPE) == VT_FLOAT) {
231 o(0xd9); /* flds */
232 r = 0;
233 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
234 o(0xdd); /* fldl */
235 r = 0;
236 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
237 o(0xdb); /* fldt */
238 r = 5;
239 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
240 o(0xbe0f); /* movsbl */
241 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
242 o(0xb60f); /* movzbl */
243 } else if ((ft & VT_TYPE) == VT_SHORT) {
244 o(0xbf0f); /* movswl */
245 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
246 o(0xb70f); /* movzwl */
247 } else {
248 o(0x8b); /* movl */
250 gen_modrm(r, fr, sv->sym, fc);
251 } else {
252 if (v == VT_CONST) {
253 o(0xb8 + r); /* mov $xx, r */
254 gen_addr32(fr, sv->sym, fc);
255 } else if (v == VT_LOCAL) {
256 if (fc) {
257 o(0x8d); /* lea xxx(%ebp), r */
258 gen_modrm(r, VT_LOCAL, sv->sym, fc);
259 } else {
260 o(0x89);
261 o(0xe8 + r); /* mov %ebp, r */
263 } else if (v == VT_CMP) {
264 o(0x0f); /* setxx %br */
265 o(fc);
266 o(0xc0 + r);
267 o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
268 } else if (v == VT_JMP || v == VT_JMPI) {
269 t = v & 1;
270 oad(0xb8 + r, t); /* mov $1, r */
271 o(0x05eb); /* jmp after */
272 gsym(fc);
273 oad(0xb8 + r, t ^ 1); /* mov $0, r */
274 } else if (v != r) {
275 o(0x89);
276 o(0xc0 + r + v * 8); /* mov v, r */
281 /* store register 'r' in lvalue 'v' */
282 ST_FUNC void store(int r, SValue *v)
284 int fr, bt, ft, fc;
286 #ifdef TCC_TARGET_PE
287 SValue v2;
288 v = pe_getimport(v, &v2);
289 #endif
291 ft = v->type.t;
292 fc = v->c.i;
293 fr = v->r & VT_VALMASK;
294 ft &= ~(VT_VOLATILE | VT_CONSTANT);
295 bt = ft & VT_BTYPE;
296 /* XXX: incorrect if float reg to reg */
297 if (bt == VT_FLOAT) {
298 o(0xd9); /* fsts */
299 r = 2;
300 } else if (bt == VT_DOUBLE) {
301 o(0xdd); /* fstpl */
302 r = 2;
303 } else if (bt == VT_LDOUBLE) {
304 o(0xc0d9); /* fld %st(0) */
305 o(0xdb); /* fstpt */
306 r = 7;
307 } else {
308 if (bt == VT_SHORT)
309 o(0x66);
310 if (bt == VT_BYTE || bt == VT_BOOL)
311 o(0x88);
312 else
313 o(0x89);
315 if (fr == VT_CONST ||
316 fr == VT_LOCAL ||
317 (v->r & VT_LVAL)) {
318 gen_modrm(r, v->r, v->sym, fc);
319 } else if (fr != r) {
320 o(0xc0 + fr + r * 8); /* mov r, fr */
324 static void gadd_sp(int val)
326 if (val == (char)val) {
327 o(0xc483);
328 g(val);
329 } else {
330 oad(0xc481, val); /* add $xxx, %esp */
334 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
335 static void gen_static_call(int v)
337 Sym *sym;
339 sym = external_global_sym(v, &func_old_type);
340 oad(0xe8, -4);
341 greloc(cur_text_section, sym, ind-4, R_386_PC32);
343 #endif
345 /* 'is_jmp' is '1' if it is a jump */
346 static void gcall_or_jmp(int is_jmp)
348 int r;
349 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
350 /* constant and relocation case */
351 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
352 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
353 } else {
354 /* otherwise, indirect call */
355 r = gv(RC_INT);
356 o(0xff); /* call/jmp *r */
357 o(0xd0 + r + (is_jmp << 4));
359 if (!is_jmp) {
360 int rt;
361 /* extend the return value to the whole register if necessary
362 visual studio and gcc do not always set the whole eax register
363 when assigning the return value of a function */
364 rt = vtop->type.ref->type.t;
365 switch (rt & VT_BTYPE) {
366 case VT_BYTE:
367 case VT_BOOL:
368 if (rt & VT_UNSIGNED) {
369 o(0xc0b60f); /* movzx %al, %eax */
371 else {
372 o(0xc0be0f); /* movsx %al, %eax */
374 break;
375 case VT_SHORT:
376 if (rt & VT_UNSIGNED) {
377 o(0xc0b70f); /* movzx %ax, %eax */
379 else {
380 o(0xc0bf0f); /* movsx %ax, %eax */
382 break;
383 default:
384 break;
389 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
390 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
392 /* Return the number of registers needed to return the struct, or 0 if
393 returning via struct pointer. */
394 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
396 #ifdef TCC_TARGET_PE
397 int size, align;
398 *ret_align = 1; // Never have to re-align return values for x86
399 *regsize = 4;
400 size = type_size(vt, &align);
401 if (size > 8 || (size & (size - 1)))
402 return 0;
403 if (size == 8)
404 ret->t = VT_LLONG;
405 else if (size == 4)
406 ret->t = VT_INT;
407 else if (size == 2)
408 ret->t = VT_SHORT;
409 else
410 ret->t = VT_BYTE;
411 ret->ref = NULL;
412 return 1;
413 #else
414 *ret_align = 1; // Never have to re-align return values for x86
415 return 0;
416 #endif
419 /* Generate function call. The function address is pushed first, then
420 all the parameters in call order. This functions pops all the
421 parameters and the function address. */
422 ST_FUNC void gfunc_call(int nb_args)
424 int size, align, r, args_size, i, func_call;
425 Sym *func_sym;
427 args_size = 0;
428 for(i = 0;i < nb_args; i++) {
429 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
430 size = type_size(&vtop->type, &align);
431 /* align to stack align size */
432 size = (size + 3) & ~3;
433 /* allocate the necessary size on stack */
434 oad(0xec81, size); /* sub $xxx, %esp */
435 /* generate structure store */
436 r = get_reg(RC_INT);
437 o(0x89); /* mov %esp, r */
438 o(0xe0 + r);
439 vset(&vtop->type, r | VT_LVAL, 0);
440 vswap();
441 vstore();
442 args_size += size;
443 } else if (is_float(vtop->type.t)) {
444 gv(RC_FLOAT); /* only one float register */
445 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
446 size = 4;
447 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
448 size = 8;
449 else
450 size = 12;
451 oad(0xec81, size); /* sub $xxx, %esp */
452 if (size == 12)
453 o(0x7cdb);
454 else
455 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
456 g(0x24);
457 g(0x00);
458 args_size += size;
459 } else {
460 /* simple type (currently always same size) */
461 /* XXX: implicit cast ? */
462 r = gv(RC_INT);
463 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
464 size = 8;
465 o(0x50 + vtop->r2); /* push r */
466 } else {
467 size = 4;
469 o(0x50 + r); /* push r */
470 args_size += size;
472 vtop--;
474 save_regs(0); /* save used temporary registers */
475 func_sym = vtop->type.ref;
476 func_call = func_sym->f.func_call;
477 /* fast call case */
478 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
479 func_call == FUNC_FASTCALLW) {
480 int fastcall_nb_regs;
481 uint8_t *fastcall_regs_ptr;
482 if (func_call == FUNC_FASTCALLW) {
483 fastcall_regs_ptr = fastcallw_regs;
484 fastcall_nb_regs = 2;
485 } else {
486 fastcall_regs_ptr = fastcall_regs;
487 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
489 for(i = 0;i < fastcall_nb_regs; i++) {
490 if (args_size <= 0)
491 break;
492 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
493 /* XXX: incorrect for struct/floats */
494 args_size -= 4;
497 #ifndef TCC_TARGET_PE
498 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
499 args_size -= 4;
500 #endif
501 gcall_or_jmp(0);
503 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
504 gadd_sp(args_size);
505 vtop--;
508 #ifdef TCC_TARGET_PE
509 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
510 #else
511 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
512 #endif
514 /* generate function prolog of type 't' */
515 ST_FUNC void gfunc_prolog(CType *func_type)
517 int addr, align, size, func_call, fastcall_nb_regs;
518 int param_index, param_addr;
519 uint8_t *fastcall_regs_ptr;
520 Sym *sym;
521 CType *type;
523 sym = func_type->ref;
524 func_call = sym->f.func_call;
525 addr = 8;
526 loc = 0;
527 func_vc = 0;
529 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
530 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
531 fastcall_regs_ptr = fastcall_regs;
532 } else if (func_call == FUNC_FASTCALLW) {
533 fastcall_nb_regs = 2;
534 fastcall_regs_ptr = fastcallw_regs;
535 } else {
536 fastcall_nb_regs = 0;
537 fastcall_regs_ptr = NULL;
539 param_index = 0;
541 ind += FUNC_PROLOG_SIZE;
542 func_sub_sp_offset = ind;
543 /* if the function returns a structure, then add an
544 implicit pointer parameter */
545 func_vt = sym->type;
546 func_var = (sym->f.func_type == FUNC_ELLIPSIS);
547 #ifdef TCC_TARGET_PE
548 size = type_size(&func_vt,&align);
549 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
550 && (size > 8 || (size & (size - 1)))) {
551 #else
552 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
553 #endif
554 /* XXX: fastcall case ? */
555 func_vc = addr;
556 addr += 4;
557 param_index++;
559 /* define parameters */
560 while ((sym = sym->next) != NULL) {
561 type = &sym->type;
562 size = type_size(type, &align);
563 size = (size + 3) & ~3;
564 #ifdef FUNC_STRUCT_PARAM_AS_PTR
565 /* structs are passed as pointer */
566 if ((type->t & VT_BTYPE) == VT_STRUCT) {
567 size = 4;
569 #endif
570 if (param_index < fastcall_nb_regs) {
571 /* save FASTCALL register */
572 loc -= 4;
573 o(0x89); /* movl */
574 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
575 param_addr = loc;
576 } else {
577 param_addr = addr;
578 addr += size;
580 sym_push(sym->v & ~SYM_FIELD, type,
581 VT_LOCAL | lvalue_type(type->t), param_addr);
582 param_index++;
584 func_ret_sub = 0;
585 /* pascal type call or fastcall ? */
586 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
587 func_ret_sub = addr - 8;
588 #ifndef TCC_TARGET_PE
589 else if (func_vc)
590 func_ret_sub = 4;
591 #endif
593 #ifdef CONFIG_TCC_BCHECK
594 /* leave some room for bound checking code */
595 if (tcc_state->do_bounds_check) {
596 func_bound_offset = lbounds_section->data_offset;
597 func_bound_ind = ind;
598 oad(0xb8, 0); /* lbound section pointer */
599 oad(0xb8, 0); /* call to function */
601 #endif
604 /* generate function epilog */
605 ST_FUNC void gfunc_epilog(void)
607 addr_t v, saved_ind;
609 #ifdef CONFIG_TCC_BCHECK
610 if (tcc_state->do_bounds_check
611 && func_bound_offset != lbounds_section->data_offset) {
612 addr_t saved_ind;
613 addr_t *bounds_ptr;
614 Sym *sym_data;
616 /* add end of table info */
617 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
618 *bounds_ptr = 0;
620 /* generate bound local allocation */
621 saved_ind = ind;
622 ind = func_bound_ind;
623 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
624 func_bound_offset, lbounds_section->data_offset);
625 greloc(cur_text_section, sym_data,
626 ind + 1, R_386_32);
627 oad(0xb8, 0); /* mov %eax, xxx */
628 gen_static_call(TOK___bound_local_new);
629 ind = saved_ind;
631 /* generate bound check local freeing */
632 o(0x5250); /* save returned value, if any */
633 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
634 oad(0xb8, 0); /* mov %eax, xxx */
635 gen_static_call(TOK___bound_local_delete);
636 o(0x585a); /* restore returned value, if any */
638 #endif
640 /* align local size to word & save local variables */
641 v = (-loc + 3) & -4;
643 #if USE_EBX
644 o(0x8b);
645 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
646 #endif
648 o(0xc9); /* leave */
649 if (func_ret_sub == 0) {
650 o(0xc3); /* ret */
651 } else {
652 o(0xc2); /* ret n */
653 g(func_ret_sub);
654 g(func_ret_sub >> 8);
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 gjmp2(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 #if 0
696 /* generate a jump to a fixed address */
697 ST_FUNC void gjmp_cond_addr(int a, int op)
699 int r = a - ind - 2;
700 if (r == (char)r)
701 g(op - 32), g(r);
702 else
703 g(0x0f), gjmp2(op - 16, r - 4);
705 #endif
707 ST_FUNC int gjmp_append(int n, int t)
709 void *p;
710 /* insert vtop->c jump list in t */
711 if (n) {
712 uint32_t n1 = n, n2;
713 while ((n2 = read32le(p = cur_text_section->data + n1)))
714 n1 = n2;
715 write32le(p, t);
716 t = n;
718 return t;
721 ST_FUNC int gjmp_cond(int op, int t)
723 g(0x0f);
724 t = gjmp2(op - 16, t);
725 return t;
728 ST_FUNC void gen_opi(int op)
730 int r, fr, opc, c;
732 switch(op) {
733 case '+':
734 case TOK_ADDC1: /* add with carry generation */
735 opc = 0;
736 gen_op8:
737 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
738 /* constant case */
739 vswap();
740 r = gv(RC_INT);
741 vswap();
742 c = vtop->c.i;
743 if (c == (char)c) {
744 /* generate inc and dec for smaller code */
745 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
746 opc = (c == 1) ^ (op == '+');
747 o (0x40 | (opc << 3) | r); // inc,dec
748 } else {
749 o(0x83);
750 o(0xc0 | (opc << 3) | r);
751 g(c);
753 } else {
754 o(0x81);
755 oad(0xc0 | (opc << 3) | r, c);
757 } else {
758 gv2(RC_INT, RC_INT);
759 r = vtop[-1].r;
760 fr = vtop[0].r;
761 o((opc << 3) | 0x01);
762 o(0xc0 + r + fr * 8);
764 vtop--;
765 if (op >= TOK_ULT && op <= TOK_GT)
766 vset_VT_CMP(op);
767 break;
768 case '-':
769 case TOK_SUBC1: /* sub with carry generation */
770 opc = 5;
771 goto gen_op8;
772 case TOK_ADDC2: /* add with carry use */
773 opc = 2;
774 goto gen_op8;
775 case TOK_SUBC2: /* sub with carry use */
776 opc = 3;
777 goto gen_op8;
778 case '&':
779 opc = 4;
780 goto gen_op8;
781 case '^':
782 opc = 6;
783 goto gen_op8;
784 case '|':
785 opc = 1;
786 goto gen_op8;
787 case '*':
788 gv2(RC_INT, RC_INT);
789 r = vtop[-1].r;
790 fr = vtop[0].r;
791 vtop--;
792 o(0xaf0f); /* imul fr, r */
793 o(0xc0 + fr + r * 8);
794 break;
795 case TOK_SHL:
796 opc = 4;
797 goto gen_shift;
798 case TOK_SHR:
799 opc = 5;
800 goto gen_shift;
801 case TOK_SAR:
802 opc = 7;
803 gen_shift:
804 opc = 0xc0 | (opc << 3);
805 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
806 /* constant case */
807 vswap();
808 r = gv(RC_INT);
809 vswap();
810 c = vtop->c.i & 0x1f;
811 o(0xc1); /* shl/shr/sar $xxx, r */
812 o(opc | r);
813 g(c);
814 } else {
815 /* we generate the shift in ecx */
816 gv2(RC_INT, RC_ECX);
817 r = vtop[-1].r;
818 o(0xd3); /* shl/shr/sar %cl, r */
819 o(opc | r);
821 vtop--;
822 break;
823 case '/':
824 case TOK_UDIV:
825 case TOK_PDIV:
826 case '%':
827 case TOK_UMOD:
828 case TOK_UMULL:
829 /* first operand must be in eax */
830 /* XXX: need better constraint for second operand */
831 gv2(RC_EAX, RC_ECX);
832 r = vtop[-1].r;
833 fr = vtop[0].r;
834 vtop--;
835 save_reg(TREG_EDX);
836 /* save EAX too if used otherwise */
837 save_reg_upstack(TREG_EAX, 1);
838 if (op == TOK_UMULL) {
839 o(0xf7); /* mul fr */
840 o(0xe0 + fr);
841 vtop->r2 = TREG_EDX;
842 r = TREG_EAX;
843 } else {
844 if (op == TOK_UDIV || op == TOK_UMOD) {
845 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
846 o(0xf0 + fr);
847 } else {
848 o(0xf799); /* cltd, idiv fr, %eax */
849 o(0xf8 + fr);
851 if (op == '%' || op == TOK_UMOD)
852 r = TREG_EDX;
853 else
854 r = TREG_EAX;
856 vtop->r = r;
857 break;
858 default:
859 opc = 7;
860 goto gen_op8;
864 /* generate a floating point operation 'v = t1 op t2' instruction. The
865 two operands are guaranteed to have the same floating point type */
866 /* XXX: need to use ST1 too */
867 ST_FUNC void gen_opf(int op)
869 int a, ft, fc, swapped, r;
871 /* convert constants to memory references */
872 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
873 vswap();
874 gv(RC_FLOAT);
875 vswap();
877 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
878 gv(RC_FLOAT);
880 /* must put at least one value in the floating point register */
881 if ((vtop[-1].r & VT_LVAL) &&
882 (vtop[0].r & VT_LVAL)) {
883 vswap();
884 gv(RC_FLOAT);
885 vswap();
887 swapped = 0;
888 /* swap the stack if needed so that t1 is the register and t2 is
889 the memory reference */
890 if (vtop[-1].r & VT_LVAL) {
891 vswap();
892 swapped = 1;
894 if (op >= TOK_ULT && op <= TOK_GT) {
895 /* load on stack second operand */
896 load(TREG_ST0, vtop);
897 save_reg(TREG_EAX); /* eax is used by FP comparison code */
898 if (op == TOK_GE || op == TOK_GT)
899 swapped = !swapped;
900 else if (op == TOK_EQ || op == TOK_NE)
901 swapped = 0;
902 if (swapped)
903 o(0xc9d9); /* fxch %st(1) */
904 if (op == TOK_EQ || op == TOK_NE)
905 o(0xe9da); /* fucompp */
906 else
907 o(0xd9de); /* fcompp */
908 o(0xe0df); /* fnstsw %ax */
909 if (op == TOK_EQ) {
910 o(0x45e480); /* and $0x45, %ah */
911 o(0x40fC80); /* cmp $0x40, %ah */
912 } else if (op == TOK_NE) {
913 o(0x45e480); /* and $0x45, %ah */
914 o(0x40f480); /* xor $0x40, %ah */
915 op = TOK_NE;
916 } else if (op == TOK_GE || op == TOK_LE) {
917 o(0x05c4f6); /* test $0x05, %ah */
918 op = TOK_EQ;
919 } else {
920 o(0x45c4f6); /* test $0x45, %ah */
921 op = TOK_EQ;
923 vtop--;
924 vset_VT_CMP(op);
925 } else {
926 /* no memory reference possible for long double operations */
927 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
928 load(TREG_ST0, vtop);
929 swapped = !swapped;
932 switch(op) {
933 default:
934 case '+':
935 a = 0;
936 break;
937 case '-':
938 a = 4;
939 if (swapped)
940 a++;
941 break;
942 case '*':
943 a = 1;
944 break;
945 case '/':
946 a = 6;
947 if (swapped)
948 a++;
949 break;
951 ft = vtop->type.t;
952 fc = vtop->c.i;
953 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
954 o(0xde); /* fxxxp %st, %st(1) */
955 o(0xc1 + (a << 3));
956 } else {
957 /* if saved lvalue, then we must reload it */
958 r = vtop->r;
959 if ((r & VT_VALMASK) == VT_LLOCAL) {
960 SValue v1;
961 r = get_reg(RC_INT);
962 v1.type.t = VT_INT;
963 v1.r = VT_LOCAL | VT_LVAL;
964 v1.c.i = fc;
965 load(r, &v1);
966 fc = 0;
969 if ((ft & VT_BTYPE) == VT_DOUBLE)
970 o(0xdc);
971 else
972 o(0xd8);
973 gen_modrm(a, r, vtop->sym, fc);
975 vtop--;
979 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
980 and 'long long' cases. */
981 ST_FUNC void gen_cvt_itof(int t)
983 save_reg(TREG_ST0);
984 gv(RC_INT);
985 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
986 /* signed long long to float/double/long double (unsigned case
987 is handled generically) */
988 o(0x50 + vtop->r2); /* push r2 */
989 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
990 o(0x242cdf); /* fildll (%esp) */
991 o(0x08c483); /* add $8, %esp */
992 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
993 (VT_INT | VT_UNSIGNED)) {
994 /* unsigned int to float/double/long double */
995 o(0x6a); /* push $0 */
996 g(0x00);
997 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
998 o(0x242cdf); /* fildll (%esp) */
999 o(0x08c483); /* add $8, %esp */
1000 } else {
1001 /* int to float/double/long double */
1002 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1003 o(0x2404db); /* fildl (%esp) */
1004 o(0x04c483); /* add $4, %esp */
1006 vtop->r = TREG_ST0;
1009 /* convert fp to int 't' type */
1010 ST_FUNC void gen_cvt_ftoi(int t)
1012 int bt = vtop->type.t & VT_BTYPE;
1013 if (bt == VT_FLOAT)
1014 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1015 else if (bt == VT_LDOUBLE)
1016 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1017 else
1018 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1019 vswap();
1020 gfunc_call(1);
1021 vpushi(0);
1022 vtop->r = REG_IRET;
1023 vtop->r2 = REG_LRET;
1026 /* convert from one floating point type to another */
1027 ST_FUNC void gen_cvt_ftof(int t)
1029 /* all we have to do on i386 is to put the float in a register */
1030 gv(RC_FLOAT);
1033 /* computed goto support */
1034 ST_FUNC void ggoto(void)
1036 gcall_or_jmp(1);
1037 vtop--;
1040 /* bound check support functions */
1041 #ifdef CONFIG_TCC_BCHECK
1043 /* generate a bounded pointer addition */
1044 ST_FUNC void gen_bounded_ptr_add(void)
1046 /* prepare fast i386 function call (args in eax and edx) */
1047 gv2(RC_EAX, RC_EDX);
1048 /* save all temporary registers */
1049 vtop -= 2;
1050 save_regs(0);
1051 /* do a fast function call */
1052 gen_static_call(TOK___bound_ptr_add);
1053 /* returned pointer is in eax */
1054 vtop++;
1055 vtop->r = TREG_EAX | VT_BOUNDED;
1056 /* address of bounding function call point */
1057 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1060 /* patch pointer addition in vtop so that pointer dereferencing is
1061 also tested */
1062 ST_FUNC void gen_bounded_ptr_deref(void)
1064 addr_t func;
1065 int size, align;
1066 Elf32_Rel *rel;
1067 Sym *sym;
1069 size = 0;
1070 /* XXX: put that code in generic part of tcc */
1071 if (!is_float(vtop->type.t)) {
1072 if (vtop->r & VT_LVAL_BYTE)
1073 size = 1;
1074 else if (vtop->r & VT_LVAL_SHORT)
1075 size = 2;
1077 if (!size)
1078 size = type_size(&vtop->type, &align);
1079 switch(size) {
1080 case 1: func = TOK___bound_ptr_indir1; break;
1081 case 2: func = TOK___bound_ptr_indir2; break;
1082 case 4: func = TOK___bound_ptr_indir4; break;
1083 case 8: func = TOK___bound_ptr_indir8; break;
1084 case 12: func = TOK___bound_ptr_indir12; break;
1085 case 16: func = TOK___bound_ptr_indir16; break;
1086 default:
1087 tcc_error("unhandled size when dereferencing bounded pointer");
1088 func = 0;
1089 break;
1092 /* patch relocation */
1093 /* XXX: find a better solution ? */
1094 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1095 sym = external_global_sym(func, &func_old_type);
1096 if (!sym->c)
1097 put_extern_sym(sym, NULL, 0, 0);
1098 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1100 #endif
1102 /* Save the stack pointer onto the stack */
1103 ST_FUNC void gen_vla_sp_save(int addr) {
1104 /* mov %esp,addr(%ebp)*/
1105 o(0x89);
1106 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1109 /* Restore the SP from a location on the stack */
1110 ST_FUNC void gen_vla_sp_restore(int addr) {
1111 o(0x8b);
1112 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1115 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1116 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1117 #ifdef TCC_TARGET_PE
1118 /* alloca does more than just adjust %rsp on Windows */
1119 vpush_global_sym(&func_old_type, TOK_alloca);
1120 vswap(); /* Move alloca ref past allocation size */
1121 gfunc_call(1);
1122 #else
1123 int r;
1124 r = gv(RC_INT); /* allocation size */
1125 /* sub r,%rsp */
1126 o(0x2b);
1127 o(0xe0 | r);
1128 /* We align to 16 bytes rather than align */
1129 /* and ~15, %esp */
1130 o(0xf0e483);
1131 vpop();
1132 #endif
1135 /* end of X86 code generator */
1136 /*************************************************************/
1137 #endif
1138 /*************************************************************/