tccpp: faster next()
[tinycc.git] / i386-gen.c
blob21866bb2df1e4e1928d37dad75f5f8251d0ce33d
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 static 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_global_sym(v, &func_old_type);
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 #ifdef CONFIG_TCC_BCHECK
363 if (tcc_state->do_bounds_check &&
364 (vtop->sym->v == TOK_alloca ||
365 vtop->sym->v == TOK_setjmp ||
366 vtop->sym->v == TOK__setjmp
367 #ifndef TCC_TARGET_PE
368 || vtop->sym->v == TOK_sigsetjmp
369 || vtop->sym->v == TOK___sigsetjmp
370 #endif
372 func_bound_add_epilog = 1;
373 #endif
374 } else {
375 /* otherwise, indirect call */
376 r = gv(RC_INT);
377 o(0xff); /* call/jmp *r */
378 o(0xd0 + r + (is_jmp << 4));
382 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
383 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
385 /* Return the number of registers needed to return the struct, or 0 if
386 returning via struct pointer. */
387 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
389 #ifdef TCC_TARGET_PE
390 int size, align;
391 *ret_align = 1; // Never have to re-align return values for x86
392 *regsize = 4;
393 size = type_size(vt, &align);
394 if (size > 8 || (size & (size - 1)))
395 return 0;
396 if (size == 8)
397 ret->t = VT_LLONG;
398 else if (size == 4)
399 ret->t = VT_INT;
400 else if (size == 2)
401 ret->t = VT_SHORT;
402 else
403 ret->t = VT_BYTE;
404 ret->ref = NULL;
405 return 1;
406 #else
407 *ret_align = 1; // Never have to re-align return values for x86
408 return 0;
409 #endif
412 /* Generate function call. The function address is pushed first, then
413 all the parameters in call order. This functions pops all the
414 parameters and the function address. */
415 ST_FUNC void gfunc_call(int nb_args)
417 int size, align, r, args_size, i, func_call;
418 Sym *func_sym;
420 #ifdef CONFIG_TCC_BCHECK
421 if (tcc_state->do_bounds_check)
422 gbound_args(nb_args);
423 #endif
425 args_size = 0;
426 for(i = 0;i < nb_args; i++) {
427 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
428 size = type_size(&vtop->type, &align);
429 /* align to stack align size */
430 size = (size + 3) & ~3;
431 /* allocate the necessary size on stack */
432 oad(0xec81, size); /* sub $xxx, %esp */
433 /* generate structure store */
434 r = get_reg(RC_INT);
435 o(0x89); /* mov %esp, r */
436 o(0xe0 + r);
437 vset(&vtop->type, r | VT_LVAL, 0);
438 vswap();
439 vstore();
440 args_size += size;
441 } else if (is_float(vtop->type.t)) {
442 gv(RC_FLOAT); /* only one float register */
443 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
444 size = 4;
445 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
446 size = 8;
447 else
448 size = 12;
449 oad(0xec81, size); /* sub $xxx, %esp */
450 if (size == 12)
451 o(0x7cdb);
452 else
453 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
454 g(0x24);
455 g(0x00);
456 args_size += size;
457 } else {
458 /* simple type (currently always same size) */
459 /* XXX: implicit cast ? */
460 r = gv(RC_INT);
461 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
462 size = 8;
463 o(0x50 + vtop->r2); /* push r */
464 } else {
465 size = 4;
467 o(0x50 + r); /* push r */
468 args_size += size;
470 vtop--;
472 save_regs(0); /* save used temporary registers */
473 func_sym = vtop->type.ref;
474 func_call = func_sym->f.func_call;
475 /* fast call case */
476 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
477 func_call == FUNC_FASTCALLW) {
478 int fastcall_nb_regs;
479 uint8_t *fastcall_regs_ptr;
480 if (func_call == FUNC_FASTCALLW) {
481 fastcall_regs_ptr = fastcallw_regs;
482 fastcall_nb_regs = 2;
483 } else {
484 fastcall_regs_ptr = fastcall_regs;
485 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
487 for(i = 0;i < fastcall_nb_regs; i++) {
488 if (args_size <= 0)
489 break;
490 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
491 /* XXX: incorrect for struct/floats */
492 args_size -= 4;
495 #ifndef TCC_TARGET_PE
496 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
497 args_size -= 4;
498 #endif
500 gcall_or_jmp(0);
502 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
503 gadd_sp(args_size);
504 vtop--;
507 #ifdef TCC_TARGET_PE
508 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
509 #else
510 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
511 #endif
513 /* generate function prolog of type 't' */
514 ST_FUNC void gfunc_prolog(Sym *func_sym)
516 CType *func_type = &func_sym->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 #ifdef TCC_TARGET_PE
546 size = type_size(&func_vt,&align);
547 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
548 && (size > 8 || (size & (size - 1)))) {
549 #else
550 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
551 #endif
552 /* XXX: fastcall case ? */
553 func_vc = addr;
554 addr += 4;
555 param_index++;
557 /* define parameters */
558 while ((sym = sym->next) != NULL) {
559 type = &sym->type;
560 size = type_size(type, &align);
561 size = (size + 3) & ~3;
562 #ifdef FUNC_STRUCT_PARAM_AS_PTR
563 /* structs are passed as pointer */
564 if ((type->t & VT_BTYPE) == VT_STRUCT) {
565 size = 4;
567 #endif
568 if (param_index < fastcall_nb_regs) {
569 /* save FASTCALL register */
570 loc -= 4;
571 o(0x89); /* movl */
572 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
573 param_addr = loc;
574 } else {
575 param_addr = addr;
576 addr += size;
578 sym_push(sym->v & ~SYM_FIELD, type,
579 VT_LOCAL | VT_LVAL, param_addr);
580 param_index++;
582 func_ret_sub = 0;
583 /* pascal type call or fastcall ? */
584 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
585 func_ret_sub = addr - 8;
586 #ifndef TCC_TARGET_PE
587 else if (func_vc)
588 func_ret_sub = 4;
589 #endif
591 #ifdef CONFIG_TCC_BCHECK
592 if (tcc_state->do_bounds_check)
593 gen_bounds_prolog();
594 #endif
597 /* generate function epilog */
598 ST_FUNC void gfunc_epilog(void)
600 addr_t v, saved_ind;
602 #ifdef CONFIG_TCC_BCHECK
603 if (tcc_state->do_bounds_check)
604 gen_bounds_epilog();
605 #endif
607 /* align local size to word & save local variables */
608 v = (-loc + 3) & -4;
610 #if USE_EBX
611 o(0x8b);
612 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
613 #endif
615 o(0xc9); /* leave */
616 if (func_ret_sub == 0) {
617 o(0xc3); /* ret */
618 } else {
619 o(0xc2); /* ret n */
620 g(func_ret_sub);
621 g(func_ret_sub >> 8);
623 saved_ind = ind;
624 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
625 #ifdef TCC_TARGET_PE
626 if (v >= 4096) {
627 oad(0xb8, v); /* mov stacksize, %eax */
628 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
629 } else
630 #endif
632 o(0xe58955); /* push %ebp, mov %esp, %ebp */
633 o(0xec81); /* sub esp, stacksize */
634 gen_le32(v);
635 #ifdef TCC_TARGET_PE
636 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
637 #endif
639 o(0x53 * USE_EBX); /* push ebx */
640 ind = saved_ind;
643 /* generate a jump to a label */
644 ST_FUNC int gjmp(int t)
646 return gjmp2(0xe9, t);
649 /* generate a jump to a fixed address */
650 ST_FUNC void gjmp_addr(int a)
652 int r;
653 r = a - ind - 2;
654 if (r == (char)r) {
655 g(0xeb);
656 g(r);
657 } else {
658 oad(0xe9, a - ind - 5);
662 #if 0
663 /* generate a jump to a fixed address */
664 ST_FUNC void gjmp_cond_addr(int a, int op)
666 int r = a - ind - 2;
667 if (r == (char)r)
668 g(op - 32), g(r);
669 else
670 g(0x0f), gjmp2(op - 16, r - 4);
672 #endif
674 ST_FUNC int gjmp_append(int n, int t)
676 void *p;
677 /* insert vtop->c jump list in t */
678 if (n) {
679 uint32_t n1 = n, n2;
680 while ((n2 = read32le(p = cur_text_section->data + n1)))
681 n1 = n2;
682 write32le(p, t);
683 t = n;
685 return t;
688 ST_FUNC int gjmp_cond(int op, int t)
690 g(0x0f);
691 t = gjmp2(op - 16, t);
692 return t;
695 ST_FUNC void gen_opi(int op)
697 int r, fr, opc, c;
699 switch(op) {
700 case '+':
701 case TOK_ADDC1: /* add with carry generation */
702 opc = 0;
703 gen_op8:
704 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
705 /* constant case */
706 vswap();
707 r = gv(RC_INT);
708 vswap();
709 c = vtop->c.i;
710 if (c == (char)c) {
711 /* generate inc and dec for smaller code */
712 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
713 opc = (c == 1) ^ (op == '+');
714 o (0x40 | (opc << 3) | r); // inc,dec
715 } else {
716 o(0x83);
717 o(0xc0 | (opc << 3) | r);
718 g(c);
720 } else {
721 o(0x81);
722 oad(0xc0 | (opc << 3) | r, c);
724 } else {
725 gv2(RC_INT, RC_INT);
726 r = vtop[-1].r;
727 fr = vtop[0].r;
728 o((opc << 3) | 0x01);
729 o(0xc0 + r + fr * 8);
731 vtop--;
732 if (op >= TOK_ULT && op <= TOK_GT)
733 vset_VT_CMP(op);
734 break;
735 case '-':
736 case TOK_SUBC1: /* sub with carry generation */
737 opc = 5;
738 goto gen_op8;
739 case TOK_ADDC2: /* add with carry use */
740 opc = 2;
741 goto gen_op8;
742 case TOK_SUBC2: /* sub with carry use */
743 opc = 3;
744 goto gen_op8;
745 case '&':
746 opc = 4;
747 goto gen_op8;
748 case '^':
749 opc = 6;
750 goto gen_op8;
751 case '|':
752 opc = 1;
753 goto gen_op8;
754 case '*':
755 gv2(RC_INT, RC_INT);
756 r = vtop[-1].r;
757 fr = vtop[0].r;
758 vtop--;
759 o(0xaf0f); /* imul fr, r */
760 o(0xc0 + fr + r * 8);
761 break;
762 case TOK_SHL:
763 opc = 4;
764 goto gen_shift;
765 case TOK_SHR:
766 opc = 5;
767 goto gen_shift;
768 case TOK_SAR:
769 opc = 7;
770 gen_shift:
771 opc = 0xc0 | (opc << 3);
772 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
773 /* constant case */
774 vswap();
775 r = gv(RC_INT);
776 vswap();
777 c = vtop->c.i & 0x1f;
778 o(0xc1); /* shl/shr/sar $xxx, r */
779 o(opc | r);
780 g(c);
781 } else {
782 /* we generate the shift in ecx */
783 gv2(RC_INT, RC_ECX);
784 r = vtop[-1].r;
785 o(0xd3); /* shl/shr/sar %cl, r */
786 o(opc | r);
788 vtop--;
789 break;
790 case '/':
791 case TOK_UDIV:
792 case TOK_PDIV:
793 case '%':
794 case TOK_UMOD:
795 case TOK_UMULL:
796 /* first operand must be in eax */
797 /* XXX: need better constraint for second operand */
798 gv2(RC_EAX, RC_ECX);
799 r = vtop[-1].r;
800 fr = vtop[0].r;
801 vtop--;
802 save_reg(TREG_EDX);
803 /* save EAX too if used otherwise */
804 save_reg_upstack(TREG_EAX, 1);
805 if (op == TOK_UMULL) {
806 o(0xf7); /* mul fr */
807 o(0xe0 + fr);
808 vtop->r2 = TREG_EDX;
809 r = TREG_EAX;
810 } else {
811 if (op == TOK_UDIV || op == TOK_UMOD) {
812 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
813 o(0xf0 + fr);
814 } else {
815 o(0xf799); /* cltd, idiv fr, %eax */
816 o(0xf8 + fr);
818 if (op == '%' || op == TOK_UMOD)
819 r = TREG_EDX;
820 else
821 r = TREG_EAX;
823 vtop->r = r;
824 break;
825 default:
826 opc = 7;
827 goto gen_op8;
831 /* generate a floating point operation 'v = t1 op t2' instruction. The
832 two operands are guaranteed to have the same floating point type */
833 /* XXX: need to use ST1 too */
834 ST_FUNC void gen_opf(int op)
836 int a, ft, fc, swapped, r;
838 /* convert constants to memory references */
839 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
840 vswap();
841 gv(RC_FLOAT);
842 vswap();
844 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
845 gv(RC_FLOAT);
847 /* must put at least one value in the floating point register */
848 if ((vtop[-1].r & VT_LVAL) &&
849 (vtop[0].r & VT_LVAL)) {
850 vswap();
851 gv(RC_FLOAT);
852 vswap();
854 swapped = 0;
855 /* swap the stack if needed so that t1 is the register and t2 is
856 the memory reference */
857 if (vtop[-1].r & VT_LVAL) {
858 vswap();
859 swapped = 1;
861 if (op >= TOK_ULT && op <= TOK_GT) {
862 /* load on stack second operand */
863 load(TREG_ST0, vtop);
864 save_reg(TREG_EAX); /* eax is used by FP comparison code */
865 if (op == TOK_GE || op == TOK_GT)
866 swapped = !swapped;
867 else if (op == TOK_EQ || op == TOK_NE)
868 swapped = 0;
869 if (swapped)
870 o(0xc9d9); /* fxch %st(1) */
871 if (op == TOK_EQ || op == TOK_NE)
872 o(0xe9da); /* fucompp */
873 else
874 o(0xd9de); /* fcompp */
875 o(0xe0df); /* fnstsw %ax */
876 if (op == TOK_EQ) {
877 o(0x45e480); /* and $0x45, %ah */
878 o(0x40fC80); /* cmp $0x40, %ah */
879 } else if (op == TOK_NE) {
880 o(0x45e480); /* and $0x45, %ah */
881 o(0x40f480); /* xor $0x40, %ah */
882 op = TOK_NE;
883 } else if (op == TOK_GE || op == TOK_LE) {
884 o(0x05c4f6); /* test $0x05, %ah */
885 op = TOK_EQ;
886 } else {
887 o(0x45c4f6); /* test $0x45, %ah */
888 op = TOK_EQ;
890 vtop--;
891 vset_VT_CMP(op);
892 } else {
893 /* no memory reference possible for long double operations */
894 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
895 load(TREG_ST0, vtop);
896 swapped = !swapped;
899 switch(op) {
900 default:
901 case '+':
902 a = 0;
903 break;
904 case '-':
905 a = 4;
906 if (swapped)
907 a++;
908 break;
909 case '*':
910 a = 1;
911 break;
912 case '/':
913 a = 6;
914 if (swapped)
915 a++;
916 break;
918 ft = vtop->type.t;
919 fc = vtop->c.i;
920 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
921 o(0xde); /* fxxxp %st, %st(1) */
922 o(0xc1 + (a << 3));
923 } else {
924 /* if saved lvalue, then we must reload it */
925 r = vtop->r;
926 if ((r & VT_VALMASK) == VT_LLOCAL) {
927 SValue v1;
928 r = get_reg(RC_INT);
929 v1.type.t = VT_INT;
930 v1.r = VT_LOCAL | VT_LVAL;
931 v1.c.i = fc;
932 v1.sym = NULL;
933 load(r, &v1);
934 fc = 0;
937 if ((ft & VT_BTYPE) == VT_DOUBLE)
938 o(0xdc);
939 else
940 o(0xd8);
941 gen_modrm(a, r, vtop->sym, fc);
943 vtop--;
947 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
948 and 'long long' cases. */
949 ST_FUNC void gen_cvt_itof(int t)
951 save_reg(TREG_ST0);
952 gv(RC_INT);
953 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
954 /* signed long long to float/double/long double (unsigned case
955 is handled generically) */
956 o(0x50 + vtop->r2); /* push r2 */
957 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
958 o(0x242cdf); /* fildll (%esp) */
959 o(0x08c483); /* add $8, %esp */
960 vtop->r2 = VT_CONST;
961 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
962 (VT_INT | VT_UNSIGNED)) {
963 /* unsigned int to float/double/long double */
964 o(0x6a); /* push $0 */
965 g(0x00);
966 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
967 o(0x242cdf); /* fildll (%esp) */
968 o(0x08c483); /* add $8, %esp */
969 } else {
970 /* int to float/double/long double */
971 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
972 o(0x2404db); /* fildl (%esp) */
973 o(0x04c483); /* add $4, %esp */
975 vtop->r2 = VT_CONST;
976 vtop->r = TREG_ST0;
979 /* convert fp to int 't' type */
980 ST_FUNC void gen_cvt_ftoi(int t)
982 int bt = vtop->type.t & VT_BTYPE;
983 if (bt == VT_FLOAT)
984 vpush_global_sym(&func_old_type, TOK___fixsfdi);
985 else if (bt == VT_LDOUBLE)
986 vpush_global_sym(&func_old_type, TOK___fixxfdi);
987 else
988 vpush_global_sym(&func_old_type, TOK___fixdfdi);
989 vswap();
990 gfunc_call(1);
991 vpushi(0);
992 vtop->r = REG_IRET;
993 if ((t & VT_BTYPE) == VT_LLONG)
994 vtop->r2 = REG_IRE2;
997 /* convert from one floating point type to another */
998 ST_FUNC void gen_cvt_ftof(int t)
1000 /* all we have to do on i386 is to put the float in a register */
1001 gv(RC_FLOAT);
1004 /* char/short to int conversion */
1005 ST_FUNC void gen_cvt_csti(int t)
1007 int r, sz, xl;
1008 r = gv(RC_INT);
1009 sz = !(t & VT_UNSIGNED);
1010 xl = (t & VT_BTYPE) == VT_SHORT;
1011 o(0xc0b60f /* mov[sz] %a[xl], %eax */
1012 | (sz << 3 | xl) << 8
1013 | (r << 3 | r) << 16
1017 /* computed goto support */
1018 ST_FUNC void ggoto(void)
1020 gcall_or_jmp(1);
1021 vtop--;
1024 /* bound check support functions */
1025 #ifdef CONFIG_TCC_BCHECK
1026 /* generate a bounded pointer addition */
1027 ST_FUNC void gen_bounded_ptr_add(void)
1029 vpush_global_sym(&func_old_type, TOK___bound_ptr_add);
1030 vrott(3);
1031 gfunc_call(2);
1032 vpushi(0);
1033 /* returned pointer is in eax */
1034 vtop->r = TREG_EAX | VT_BOUNDED;
1035 if (nocode_wanted)
1036 return;
1037 /* relocation offset of the bounding function call point */
1038 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1041 /* patch pointer addition in vtop so that pointer dereferencing is
1042 also tested */
1043 ST_FUNC void gen_bounded_ptr_deref(void)
1045 addr_t func;
1046 int size, align;
1047 Elf32_Rel *rel;
1048 Sym *sym;
1050 if (nocode_wanted)
1051 return;
1053 size = type_size(&vtop->type, &align);
1054 switch(size) {
1055 case 1: func = TOK___bound_ptr_indir1; break;
1056 case 2: func = TOK___bound_ptr_indir2; break;
1057 case 4: func = TOK___bound_ptr_indir4; break;
1058 case 8: func = TOK___bound_ptr_indir8; break;
1059 case 12: func = TOK___bound_ptr_indir12; break;
1060 case 16: func = TOK___bound_ptr_indir16; break;
1061 default:
1062 /* may happen with struct member access */
1063 return;
1064 //tcc_error("unhandled size when dereferencing bounded pointer");
1065 //func = 0;
1066 //break;
1068 sym = external_global_sym(func, &func_old_type);
1069 if (!sym->c)
1070 put_extern_sym(sym, NULL, 0, 0);
1071 /* patch relocation */
1072 /* XXX: find a better solution ? */
1073 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1074 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1077 static void gen_bounds_prolog(void)
1079 /* leave some room for bound checking code */
1080 func_bound_offset = lbounds_section->data_offset;
1081 func_bound_ind = ind;
1082 func_bound_add_epilog = 0;
1083 oad(0xb8, 0); /* lbound section pointer */
1084 oad(0xb8, 0); /* call to function */
1087 static void gen_bounds_epilog(void)
1089 addr_t saved_ind;
1090 addr_t *bounds_ptr;
1091 Sym *sym_data;
1092 int offset_modified = func_bound_offset != lbounds_section->data_offset;
1094 if (!offset_modified && !func_bound_add_epilog)
1095 return;
1097 /* add end of table info */
1098 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
1099 *bounds_ptr = 0;
1101 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
1102 func_bound_offset, lbounds_section->data_offset);
1104 /* generate bound local allocation */
1105 if (offset_modified) {
1106 saved_ind = ind;
1107 ind = func_bound_ind;
1108 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1109 ind = ind + 5;
1110 gen_static_call(TOK___bound_local_new);
1111 ind = saved_ind;
1114 /* generate bound check local freeing */
1115 o(0x5250); /* save returned value, if any */
1116 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1117 oad(0xb8, 0); /* mov %eax, xxx */
1118 gen_static_call(TOK___bound_local_delete);
1119 o(0x585a); /* restore returned value, if any */
1121 #endif
1123 /* Save the stack pointer onto the stack */
1124 ST_FUNC void gen_vla_sp_save(int addr) {
1125 /* mov %esp,addr(%ebp)*/
1126 o(0x89);
1127 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1130 /* Restore the SP from a location on the stack */
1131 ST_FUNC void gen_vla_sp_restore(int addr) {
1132 o(0x8b);
1133 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1136 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1137 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1138 int use_call = 0;
1140 #if defined(CONFIG_TCC_BCHECK)
1141 use_call = tcc_state->do_bounds_check;
1142 #endif
1143 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
1144 use_call = 1;
1145 #endif
1146 if (use_call)
1148 vpush_global_sym(&func_old_type, TOK_alloca);
1149 vswap(); /* Move alloca ref past allocation size */
1150 gfunc_call(1);
1152 else {
1153 int r;
1154 r = gv(RC_INT); /* allocation size */
1155 /* sub r,%rsp */
1156 o(0x2b);
1157 o(0xe0 | r);
1158 /* We align to 16 bytes rather than align */
1159 /* and ~15, %esp */
1160 o(0xf0e483);
1161 vpop();
1165 /* end of X86 code generator */
1166 /*************************************************************/
1167 #endif
1168 /*************************************************************/