NetBSD: arm64 prevent use of __asm.
[tinycc.git] / i386-gen.c
blobd27279809b87414e878f8216a2368bbc069c14e7
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_IRE2 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_IRE2 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 /* define if return values need to be extended explicitely
75 at caller side (for interfacing with non-TCC compilers) */
76 #define PROMOTE_RET
78 /******************************************************/
79 #else /* ! TARGET_DEFS_ONLY */
80 /******************************************************/
81 #define USING_GLOBALS
82 #include "tcc.h"
84 /* define to 1/0 to [not] have EBX as 4th register */
85 #define USE_EBX 0
87 ST_DATA const int reg_classes[NB_REGS] = {
88 /* eax */ RC_INT | RC_EAX,
89 /* ecx */ RC_INT | RC_ECX,
90 /* edx */ RC_INT | RC_EDX,
91 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
92 /* st0 */ RC_FLOAT | RC_ST0,
95 static unsigned long func_sub_sp_offset;
96 static int func_ret_sub;
97 #ifdef CONFIG_TCC_BCHECK
98 static addr_t func_bound_offset;
99 static unsigned long func_bound_ind;
100 ST_DATA int func_bound_add_epilog;
101 static void gen_bounds_prolog(void);
102 static void gen_bounds_epilog(void);
103 #endif
105 /* XXX: make it faster ? */
106 ST_FUNC void g(int c)
108 int ind1;
109 if (nocode_wanted)
110 return;
111 ind1 = ind + 1;
112 if (ind1 > cur_text_section->data_allocated)
113 section_realloc(cur_text_section, ind1);
114 cur_text_section->data[ind] = c;
115 ind = ind1;
118 ST_FUNC void o(unsigned int c)
120 while (c) {
121 g(c);
122 c = c >> 8;
126 ST_FUNC void gen_le16(int v)
128 g(v);
129 g(v >> 8);
132 ST_FUNC void gen_le32(int c)
134 g(c);
135 g(c >> 8);
136 g(c >> 16);
137 g(c >> 24);
140 /* output a symbol and patch all calls to it */
141 ST_FUNC void gsym_addr(int t, int a)
143 while (t) {
144 unsigned char *ptr = cur_text_section->data + t;
145 uint32_t n = read32le(ptr); /* next value */
146 write32le(ptr, a - t - 4);
147 t = n;
151 /* instruction + 4 bytes data. Return the address of the data */
152 static int oad(int c, int s)
154 int t;
155 if (nocode_wanted)
156 return s;
157 o(c);
158 t = ind;
159 gen_le32(s);
160 return t;
163 ST_FUNC void gen_fill_nops(int bytes)
165 while (bytes--)
166 g(0x90);
169 /* generate jmp to a label */
170 #define gjmp2(instr,lbl) oad(instr,lbl)
172 /* output constant with relocation if 'r & VT_SYM' is true */
173 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
175 if (r & VT_SYM)
176 greloc(cur_text_section, sym, ind, R_386_32);
177 gen_le32(c);
180 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
182 if (r & VT_SYM)
183 greloc(cur_text_section, sym, ind, R_386_PC32);
184 gen_le32(c - 4);
187 /* generate a modrm reference. 'op_reg' contains the additional 3
188 opcode bits */
189 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
191 op_reg = op_reg << 3;
192 if ((r & VT_VALMASK) == VT_CONST) {
193 /* constant memory reference */
194 o(0x05 | op_reg);
195 gen_addr32(r, sym, c);
196 } else if ((r & VT_VALMASK) == VT_LOCAL) {
197 /* currently, we use only ebp as base */
198 if (c == (char)c) {
199 /* short reference */
200 o(0x45 | op_reg);
201 g(c);
202 } else {
203 oad(0x85 | op_reg, c);
205 } else {
206 g(0x00 | op_reg | (r & VT_VALMASK));
210 /* load 'r' from value 'sv' */
211 ST_FUNC void load(int r, SValue *sv)
213 int v, t, ft, fc, fr;
214 SValue v1;
216 #ifdef TCC_TARGET_PE
217 SValue v2;
218 sv = pe_getimport(sv, &v2);
219 #endif
221 fr = sv->r;
222 ft = sv->type.t & ~VT_DEFSIGN;
223 fc = sv->c.i;
225 ft &= ~(VT_VOLATILE | VT_CONSTANT);
227 v = fr & VT_VALMASK;
228 if (fr & VT_LVAL) {
229 if (v == VT_LLOCAL) {
230 v1.type.t = VT_INT;
231 v1.r = VT_LOCAL | VT_LVAL;
232 v1.c.i = fc;
233 v1.sym = NULL;
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 o(0x0f); /* setxx %br */
274 o(fc);
275 o(0xc0 + r);
276 o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
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 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
344 static void gen_static_call(int v)
346 Sym *sym;
348 sym = external_helper_sym(v);
349 oad(0xe8, -4);
350 greloc(cur_text_section, sym, ind-4, R_386_PC32);
352 #endif
354 /* 'is_jmp' is '1' if it is a jump */
355 static void gcall_or_jmp(int is_jmp)
357 int r;
358 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
359 /* constant and relocation case */
360 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
361 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
362 } else {
363 /* otherwise, indirect call */
364 r = gv(RC_INT);
365 o(0xff); /* call/jmp *r */
366 o(0xd0 + r + (is_jmp << 4));
370 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
371 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
373 /* Return the number of registers needed to return the struct, or 0 if
374 returning via struct pointer. */
375 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
377 #ifdef TCC_TARGET_PE
378 int size, align;
379 *ret_align = 1; // Never have to re-align return values for x86
380 *regsize = 4;
381 size = type_size(vt, &align);
382 if (size > 8 || (size & (size - 1)))
383 return 0;
384 if (size == 8)
385 ret->t = VT_LLONG;
386 else if (size == 4)
387 ret->t = VT_INT;
388 else if (size == 2)
389 ret->t = VT_SHORT;
390 else
391 ret->t = VT_BYTE;
392 ret->ref = NULL;
393 return 1;
394 #else
395 *ret_align = 1; // Never have to re-align return values for x86
396 return 0;
397 #endif
400 /* Generate function call. The function address is pushed first, then
401 all the parameters in call order. This functions pops all the
402 parameters and the function address. */
403 ST_FUNC void gfunc_call(int nb_args)
405 int size, align, r, args_size, i, func_call;
406 Sym *func_sym;
408 #ifdef CONFIG_TCC_BCHECK
409 if (tcc_state->do_bounds_check)
410 gbound_args(nb_args);
411 #endif
413 args_size = 0;
414 for(i = 0;i < nb_args; i++) {
415 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
416 size = type_size(&vtop->type, &align);
417 /* align to stack align size */
418 size = (size + 3) & ~3;
419 /* allocate the necessary size on stack */
420 #ifdef TCC_TARGET_PE
421 if (size >= 0x4096) {
422 /* cannot call alloca with bound checking. Do stack probing. */
423 o(0x50); // push %eax
424 oad(0xb8, size - 4); // mov size-4,%eax
425 oad(0x3d, 4096); // p1: cmp $4096,%eax
426 o(0x1476); // jbe <p2>
427 oad(0x248485,-4096); // test %eax,-4096(%esp)
428 oad(0xec81, 4096); // sub $4096,%esp
429 oad(0x2d, 4096); // sub $4096,%eax
430 o(0xe5eb); // jmp <p1>
431 o(0xc429); // p2: sub %eax,%esp
432 oad(0xc481, size - 4); // add size-4,%esp
433 o(0x58); // pop %eax
435 #endif
436 oad(0xec81, size); /* sub $xxx, %esp */
437 /* generate structure store */
438 r = get_reg(RC_INT);
439 o(0x89); /* mov %esp, r */
440 o(0xe0 + r);
441 vset(&vtop->type, r | VT_LVAL, 0);
442 vswap();
443 vstore();
444 args_size += size;
445 } else if (is_float(vtop->type.t)) {
446 gv(RC_FLOAT); /* only one float register */
447 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
448 size = 4;
449 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
450 size = 8;
451 else
452 size = 12;
453 oad(0xec81, size); /* sub $xxx, %esp */
454 if (size == 12)
455 o(0x7cdb);
456 else
457 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
458 g(0x24);
459 g(0x00);
460 args_size += size;
461 } else {
462 /* simple type (currently always same size) */
463 /* XXX: implicit cast ? */
464 r = gv(RC_INT);
465 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
466 size = 8;
467 o(0x50 + vtop->r2); /* push r */
468 } else {
469 size = 4;
471 o(0x50 + r); /* push r */
472 args_size += size;
474 vtop--;
476 save_regs(0); /* save used temporary registers */
477 func_sym = vtop->type.ref;
478 func_call = func_sym->f.func_call;
479 /* fast call case */
480 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
481 func_call == FUNC_FASTCALLW) {
482 int fastcall_nb_regs;
483 uint8_t *fastcall_regs_ptr;
484 if (func_call == FUNC_FASTCALLW) {
485 fastcall_regs_ptr = fastcallw_regs;
486 fastcall_nb_regs = 2;
487 } else {
488 fastcall_regs_ptr = fastcall_regs;
489 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
491 for(i = 0;i < fastcall_nb_regs; i++) {
492 if (args_size <= 0)
493 break;
494 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
495 /* XXX: incorrect for struct/floats */
496 args_size -= 4;
499 #ifndef TCC_TARGET_PE
500 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
501 args_size -= 4;
502 #endif
504 gcall_or_jmp(0);
506 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
507 gadd_sp(args_size);
508 vtop--;
511 #ifdef TCC_TARGET_PE
512 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
513 #else
514 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
515 #endif
517 /* generate function prolog of type 't' */
518 ST_FUNC void gfunc_prolog(Sym *func_sym)
520 CType *func_type = &func_sym->type;
521 int addr, align, size, func_call, fastcall_nb_regs;
522 int param_index, param_addr;
523 uint8_t *fastcall_regs_ptr;
524 Sym *sym;
525 CType *type;
527 sym = func_type->ref;
528 func_call = sym->f.func_call;
529 addr = 8;
530 loc = 0;
531 func_vc = 0;
533 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
534 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
535 fastcall_regs_ptr = fastcall_regs;
536 } else if (func_call == FUNC_FASTCALLW) {
537 fastcall_nb_regs = 2;
538 fastcall_regs_ptr = fastcallw_regs;
539 } else {
540 fastcall_nb_regs = 0;
541 fastcall_regs_ptr = NULL;
543 param_index = 0;
545 ind += FUNC_PROLOG_SIZE;
546 func_sub_sp_offset = ind;
547 /* if the function returns a structure, then add an
548 implicit pointer parameter */
549 #ifdef TCC_TARGET_PE
550 size = type_size(&func_vt,&align);
551 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
552 && (size > 8 || (size & (size - 1)))) {
553 #else
554 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
555 #endif
556 /* XXX: fastcall case ? */
557 func_vc = addr;
558 addr += 4;
559 param_index++;
561 /* define parameters */
562 while ((sym = sym->next) != NULL) {
563 type = &sym->type;
564 size = type_size(type, &align);
565 size = (size + 3) & ~3;
566 #ifdef FUNC_STRUCT_PARAM_AS_PTR
567 /* structs are passed as pointer */
568 if ((type->t & VT_BTYPE) == VT_STRUCT) {
569 size = 4;
571 #endif
572 if (param_index < fastcall_nb_regs) {
573 /* save FASTCALL register */
574 loc -= 4;
575 o(0x89); /* movl */
576 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
577 param_addr = loc;
578 } else {
579 param_addr = addr;
580 addr += size;
582 sym_push(sym->v & ~SYM_FIELD, type,
583 VT_LOCAL | VT_LVAL, param_addr);
584 param_index++;
586 func_ret_sub = 0;
587 /* pascal type call or fastcall ? */
588 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
589 func_ret_sub = addr - 8;
590 #ifndef TCC_TARGET_PE
591 else if (func_vc)
592 func_ret_sub = 4;
593 #endif
595 #ifdef CONFIG_TCC_BCHECK
596 if (tcc_state->do_bounds_check)
597 gen_bounds_prolog();
598 #endif
601 /* generate function epilog */
602 ST_FUNC void gfunc_epilog(void)
604 addr_t v, saved_ind;
606 #ifdef CONFIG_TCC_BCHECK
607 if (tcc_state->do_bounds_check)
608 gen_bounds_epilog();
609 #endif
611 /* align local size to word & save local variables */
612 v = (-loc + 3) & -4;
614 #if USE_EBX
615 o(0x8b);
616 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
617 #endif
619 o(0xc9); /* leave */
620 if (func_ret_sub == 0) {
621 o(0xc3); /* ret */
622 } else {
623 o(0xc2); /* ret n */
624 g(func_ret_sub);
625 g(func_ret_sub >> 8);
627 saved_ind = ind;
628 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
629 #ifdef TCC_TARGET_PE
630 if (v >= 4096) {
631 oad(0xb8, v); /* mov stacksize, %eax */
632 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
633 } else
634 #endif
636 o(0xe58955); /* push %ebp, mov %esp, %ebp */
637 o(0xec81); /* sub esp, stacksize */
638 gen_le32(v);
639 #ifdef TCC_TARGET_PE
640 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
641 #endif
643 o(0x53 * USE_EBX); /* push ebx */
644 ind = saved_ind;
647 /* generate a jump to a label */
648 ST_FUNC int gjmp(int t)
650 return gjmp2(0xe9, t);
653 /* generate a jump to a fixed address */
654 ST_FUNC void gjmp_addr(int a)
656 int r;
657 r = a - ind - 2;
658 if (r == (char)r) {
659 g(0xeb);
660 g(r);
661 } else {
662 oad(0xe9, a - ind - 5);
666 #if 0
667 /* generate a jump to a fixed address */
668 ST_FUNC void gjmp_cond_addr(int a, int op)
670 int r = a - ind - 2;
671 if (r == (char)r)
672 g(op - 32), g(r);
673 else
674 g(0x0f), gjmp2(op - 16, r - 4);
676 #endif
678 ST_FUNC int gjmp_append(int n, int t)
680 void *p;
681 /* insert vtop->c jump list in t */
682 if (n) {
683 uint32_t n1 = n, n2;
684 while ((n2 = read32le(p = cur_text_section->data + n1)))
685 n1 = n2;
686 write32le(p, t);
687 t = n;
689 return t;
692 ST_FUNC int gjmp_cond(int op, int t)
694 g(0x0f);
695 t = gjmp2(op - 16, t);
696 return t;
699 ST_FUNC void gen_opi(int op)
701 int r, fr, opc, c;
703 switch(op) {
704 case '+':
705 case TOK_ADDC1: /* add with carry generation */
706 opc = 0;
707 gen_op8:
708 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
709 /* constant case */
710 vswap();
711 r = gv(RC_INT);
712 vswap();
713 c = vtop->c.i;
714 if (c == (char)c) {
715 /* generate inc and dec for smaller code */
716 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
717 opc = (c == 1) ^ (op == '+');
718 o (0x40 | (opc << 3) | r); // inc,dec
719 } else {
720 o(0x83);
721 o(0xc0 | (opc << 3) | r);
722 g(c);
724 } else {
725 o(0x81);
726 oad(0xc0 | (opc << 3) | r, c);
728 } else {
729 gv2(RC_INT, RC_INT);
730 r = vtop[-1].r;
731 fr = vtop[0].r;
732 o((opc << 3) | 0x01);
733 o(0xc0 + r + fr * 8);
735 vtop--;
736 if (op >= TOK_ULT && op <= TOK_GT)
737 vset_VT_CMP(op);
738 break;
739 case '-':
740 case TOK_SUBC1: /* sub with carry generation */
741 opc = 5;
742 goto gen_op8;
743 case TOK_ADDC2: /* add with carry use */
744 opc = 2;
745 goto gen_op8;
746 case TOK_SUBC2: /* sub with carry use */
747 opc = 3;
748 goto gen_op8;
749 case '&':
750 opc = 4;
751 goto gen_op8;
752 case '^':
753 opc = 6;
754 goto gen_op8;
755 case '|':
756 opc = 1;
757 goto gen_op8;
758 case '*':
759 gv2(RC_INT, RC_INT);
760 r = vtop[-1].r;
761 fr = vtop[0].r;
762 vtop--;
763 o(0xaf0f); /* imul fr, r */
764 o(0xc0 + fr + r * 8);
765 break;
766 case TOK_SHL:
767 opc = 4;
768 goto gen_shift;
769 case TOK_SHR:
770 opc = 5;
771 goto gen_shift;
772 case TOK_SAR:
773 opc = 7;
774 gen_shift:
775 opc = 0xc0 | (opc << 3);
776 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
777 /* constant case */
778 vswap();
779 r = gv(RC_INT);
780 vswap();
781 c = vtop->c.i & 0x1f;
782 o(0xc1); /* shl/shr/sar $xxx, r */
783 o(opc | r);
784 g(c);
785 } else {
786 /* we generate the shift in ecx */
787 gv2(RC_INT, RC_ECX);
788 r = vtop[-1].r;
789 o(0xd3); /* shl/shr/sar %cl, r */
790 o(opc | r);
792 vtop--;
793 break;
794 case '/':
795 case TOK_UDIV:
796 case TOK_PDIV:
797 case '%':
798 case TOK_UMOD:
799 case TOK_UMULL:
800 /* first operand must be in eax */
801 /* XXX: need better constraint for second operand */
802 gv2(RC_EAX, RC_ECX);
803 r = vtop[-1].r;
804 fr = vtop[0].r;
805 vtop--;
806 save_reg(TREG_EDX);
807 /* save EAX too if used otherwise */
808 save_reg_upstack(TREG_EAX, 1);
809 if (op == TOK_UMULL) {
810 o(0xf7); /* mul fr */
811 o(0xe0 + fr);
812 vtop->r2 = TREG_EDX;
813 r = TREG_EAX;
814 } else {
815 if (op == TOK_UDIV || op == TOK_UMOD) {
816 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
817 o(0xf0 + fr);
818 } else {
819 o(0xf799); /* cltd, idiv fr, %eax */
820 o(0xf8 + fr);
822 if (op == '%' || op == TOK_UMOD)
823 r = TREG_EDX;
824 else
825 r = TREG_EAX;
827 vtop->r = r;
828 break;
829 default:
830 opc = 7;
831 goto gen_op8;
835 /* generate a floating point operation 'v = t1 op t2' instruction. The
836 two operands are guaranteed to have the same floating point type */
837 /* XXX: need to use ST1 too */
838 ST_FUNC void gen_opf(int op)
840 int a, ft, fc, swapped, r;
842 /* convert constants to memory references */
843 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
844 vswap();
845 gv(RC_FLOAT);
846 vswap();
848 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
849 gv(RC_FLOAT);
851 /* must put at least one value in the floating point register */
852 if ((vtop[-1].r & VT_LVAL) &&
853 (vtop[0].r & VT_LVAL)) {
854 vswap();
855 gv(RC_FLOAT);
856 vswap();
858 swapped = 0;
859 /* swap the stack if needed so that t1 is the register and t2 is
860 the memory reference */
861 if (vtop[-1].r & VT_LVAL) {
862 vswap();
863 swapped = 1;
865 if (op >= TOK_ULT && op <= TOK_GT) {
866 /* load on stack second operand */
867 load(TREG_ST0, vtop);
868 save_reg(TREG_EAX); /* eax is used by FP comparison code */
869 if (op == TOK_GE || op == TOK_GT)
870 swapped = !swapped;
871 else if (op == TOK_EQ || op == TOK_NE)
872 swapped = 0;
873 if (swapped)
874 o(0xc9d9); /* fxch %st(1) */
875 if (op == TOK_EQ || op == TOK_NE)
876 o(0xe9da); /* fucompp */
877 else
878 o(0xd9de); /* fcompp */
879 o(0xe0df); /* fnstsw %ax */
880 if (op == TOK_EQ) {
881 o(0x45e480); /* and $0x45, %ah */
882 o(0x40fC80); /* cmp $0x40, %ah */
883 } else if (op == TOK_NE) {
884 o(0x45e480); /* and $0x45, %ah */
885 o(0x40f480); /* xor $0x40, %ah */
886 op = TOK_NE;
887 } else if (op == TOK_GE || op == TOK_LE) {
888 o(0x05c4f6); /* test $0x05, %ah */
889 op = TOK_EQ;
890 } else {
891 o(0x45c4f6); /* test $0x45, %ah */
892 op = TOK_EQ;
894 vtop--;
895 vset_VT_CMP(op);
896 } else {
897 /* no memory reference possible for long double operations */
898 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
899 load(TREG_ST0, vtop);
900 swapped = !swapped;
903 switch(op) {
904 default:
905 case '+':
906 a = 0;
907 break;
908 case '-':
909 a = 4;
910 if (swapped)
911 a++;
912 break;
913 case '*':
914 a = 1;
915 break;
916 case '/':
917 a = 6;
918 if (swapped)
919 a++;
920 break;
922 ft = vtop->type.t;
923 fc = vtop->c.i;
924 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
925 o(0xde); /* fxxxp %st, %st(1) */
926 o(0xc1 + (a << 3));
927 } else {
928 /* if saved lvalue, then we must reload it */
929 r = vtop->r;
930 if ((r & VT_VALMASK) == VT_LLOCAL) {
931 SValue v1;
932 r = get_reg(RC_INT);
933 v1.type.t = VT_INT;
934 v1.r = VT_LOCAL | VT_LVAL;
935 v1.c.i = fc;
936 v1.sym = NULL;
937 load(r, &v1);
938 fc = 0;
941 if ((ft & VT_BTYPE) == VT_DOUBLE)
942 o(0xdc);
943 else
944 o(0xd8);
945 gen_modrm(a, r, vtop->sym, fc);
947 vtop--;
951 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
952 and 'long long' cases. */
953 ST_FUNC void gen_cvt_itof(int t)
955 save_reg(TREG_ST0);
956 gv(RC_INT);
957 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
958 /* signed long long to float/double/long double (unsigned case
959 is handled generically) */
960 o(0x50 + vtop->r2); /* push r2 */
961 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
962 o(0x242cdf); /* fildll (%esp) */
963 o(0x08c483); /* add $8, %esp */
964 vtop->r2 = VT_CONST;
965 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
966 (VT_INT | VT_UNSIGNED)) {
967 /* unsigned int to float/double/long double */
968 o(0x6a); /* push $0 */
969 g(0x00);
970 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
971 o(0x242cdf); /* fildll (%esp) */
972 o(0x08c483); /* add $8, %esp */
973 } else {
974 /* int to float/double/long double */
975 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
976 o(0x2404db); /* fildl (%esp) */
977 o(0x04c483); /* add $4, %esp */
979 vtop->r2 = VT_CONST;
980 vtop->r = TREG_ST0;
983 /* convert fp to int 't' type */
984 ST_FUNC void gen_cvt_ftoi(int t)
986 int bt = vtop->type.t & VT_BTYPE;
987 if (bt == VT_FLOAT)
988 vpush_helper_func(TOK___fixsfdi);
989 else if (bt == VT_LDOUBLE)
990 vpush_helper_func(TOK___fixxfdi);
991 else
992 vpush_helper_func(TOK___fixdfdi);
993 vswap();
994 gfunc_call(1);
995 vpushi(0);
996 vtop->r = REG_IRET;
997 if ((t & VT_BTYPE) == VT_LLONG)
998 vtop->r2 = REG_IRE2;
1001 /* convert from one floating point type to another */
1002 ST_FUNC void gen_cvt_ftof(int t)
1004 /* all we have to do on i386 is to put the float in a register */
1005 gv(RC_FLOAT);
1008 /* char/short to int conversion */
1009 ST_FUNC void gen_cvt_csti(int t)
1011 int r, sz, xl;
1012 r = gv(RC_INT);
1013 sz = !(t & VT_UNSIGNED);
1014 xl = (t & VT_BTYPE) == VT_SHORT;
1015 o(0xc0b60f /* mov[sz] %a[xl], %eax */
1016 | (sz << 3 | xl) << 8
1017 | (r << 3 | r) << 16
1021 /* computed goto support */
1022 ST_FUNC void ggoto(void)
1024 gcall_or_jmp(1);
1025 vtop--;
1028 /* bound check support functions */
1029 #ifdef CONFIG_TCC_BCHECK
1031 static void gen_bounds_prolog(void)
1033 /* leave some room for bound checking code */
1034 func_bound_offset = lbounds_section->data_offset;
1035 func_bound_ind = ind;
1036 func_bound_add_epilog = 0;
1037 oad(0xb8, 0); /* lbound section pointer */
1038 oad(0xb8, 0); /* call to function */
1041 static void gen_bounds_epilog(void)
1043 addr_t saved_ind;
1044 addr_t *bounds_ptr;
1045 Sym *sym_data;
1046 int offset_modified = func_bound_offset != lbounds_section->data_offset;
1048 if (!offset_modified && !func_bound_add_epilog)
1049 return;
1051 /* add end of table info */
1052 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
1053 *bounds_ptr = 0;
1055 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
1056 func_bound_offset, lbounds_section->data_offset);
1058 /* generate bound local allocation */
1059 if (offset_modified) {
1060 saved_ind = ind;
1061 ind = func_bound_ind;
1062 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1063 ind = ind + 5;
1064 gen_static_call(TOK___bound_local_new);
1065 ind = saved_ind;
1068 /* generate bound check local freeing */
1069 o(0x5250); /* save returned value, if any */
1070 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1071 oad(0xb8, 0); /* mov %eax, xxx */
1072 gen_static_call(TOK___bound_local_delete);
1073 o(0x585a); /* restore returned value, if any */
1075 #endif
1077 /* Save the stack pointer onto the stack */
1078 ST_FUNC void gen_vla_sp_save(int addr) {
1079 /* mov %esp,addr(%ebp)*/
1080 o(0x89);
1081 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1084 /* Restore the SP from a location on the stack */
1085 ST_FUNC void gen_vla_sp_restore(int addr) {
1086 o(0x8b);
1087 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1090 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1091 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1092 int use_call = 0;
1094 #if defined(CONFIG_TCC_BCHECK)
1095 use_call = tcc_state->do_bounds_check;
1096 #endif
1097 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
1098 use_call = 1;
1099 #endif
1100 if (use_call)
1102 vpush_helper_func(TOK_alloca);
1103 vswap(); /* Move alloca ref past allocation size */
1104 gfunc_call(1);
1106 else {
1107 int r;
1108 r = gv(RC_INT); /* allocation size */
1109 /* sub r,%rsp */
1110 o(0x2b);
1111 o(0xe0 | r);
1112 /* We align to 16 bytes rather than align */
1113 /* and ~15, %esp */
1114 o(0xf0e483);
1115 vpop();
1119 /* end of X86 code generator */
1120 /*************************************************************/
1121 #endif
1122 /*************************************************************/