stdarg: always have the __builtin_va_* available
[tinycc.git] / i386-gen.c
blob8016af94ce6c9f7a5055fbdcaa7f93ec09c487aa
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 void gen_bounds_prolog(void);
101 static void gen_bounds_epilog(void);
102 #endif
104 /* XXX: make it faster ? */
105 ST_FUNC void g(int c)
107 int ind1;
108 if (nocode_wanted)
109 return;
110 ind1 = ind + 1;
111 if (ind1 > cur_text_section->data_allocated)
112 section_realloc(cur_text_section, ind1);
113 cur_text_section->data[ind] = c;
114 ind = ind1;
117 ST_FUNC void o(unsigned int c)
119 while (c) {
120 g(c);
121 c = c >> 8;
125 ST_FUNC void gen_le16(int v)
127 g(v);
128 g(v >> 8);
131 ST_FUNC void gen_le32(int c)
133 g(c);
134 g(c >> 8);
135 g(c >> 16);
136 g(c >> 24);
139 /* output a symbol and patch all calls to it */
140 ST_FUNC void gsym_addr(int t, int a)
142 while (t) {
143 unsigned char *ptr = cur_text_section->data + t;
144 uint32_t n = read32le(ptr); /* next value */
145 write32le(ptr, a - t - 4);
146 t = n;
150 /* instruction + 4 bytes data. Return the address of the data */
151 static int oad(int c, int s)
153 int t;
154 if (nocode_wanted)
155 return s;
156 o(c);
157 t = ind;
158 gen_le32(s);
159 return t;
162 ST_FUNC void gen_fill_nops(int bytes)
164 while (bytes--)
165 g(0x90);
168 /* generate jmp to a label */
169 #define gjmp2(instr,lbl) oad(instr,lbl)
171 /* output constant with relocation if 'r & VT_SYM' is true */
172 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
174 if (r & VT_SYM)
175 greloc(cur_text_section, sym, ind, R_386_32);
176 gen_le32(c);
179 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
181 if (r & VT_SYM)
182 greloc(cur_text_section, sym, ind, R_386_PC32);
183 gen_le32(c - 4);
186 /* generate a modrm reference. 'op_reg' contains the additional 3
187 opcode bits */
188 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
190 op_reg = op_reg << 3;
191 if ((r & VT_VALMASK) == VT_CONST) {
192 /* constant memory reference */
193 o(0x05 | op_reg);
194 gen_addr32(r, sym, c);
195 } else if ((r & VT_VALMASK) == VT_LOCAL) {
196 /* currently, we use only ebp as base */
197 if (c == (char)c) {
198 /* short reference */
199 o(0x45 | op_reg);
200 g(c);
201 } else {
202 oad(0x85 | op_reg, c);
204 } else {
205 g(0x00 | op_reg | (r & VT_VALMASK));
209 /* load 'r' from value 'sv' */
210 ST_FUNC void load(int r, SValue *sv)
212 int v, t, ft, fc, fr;
213 SValue v1;
215 #ifdef TCC_TARGET_PE
216 SValue v2;
217 sv = pe_getimport(sv, &v2);
218 #endif
220 fr = sv->r;
221 ft = sv->type.t & ~VT_DEFSIGN;
222 fc = sv->c.i;
224 ft &= ~(VT_VOLATILE | VT_CONSTANT);
226 v = fr & VT_VALMASK;
227 if (fr & VT_LVAL) {
228 if (v == VT_LLOCAL) {
229 v1.type.t = VT_INT;
230 v1.r = VT_LOCAL | VT_LVAL;
231 v1.c.i = fc;
232 fr = r;
233 if (!(reg_classes[fr] & RC_INT))
234 fr = get_reg(RC_INT);
235 load(fr, &v1);
237 if ((ft & VT_BTYPE) == VT_FLOAT) {
238 o(0xd9); /* flds */
239 r = 0;
240 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
241 o(0xdd); /* fldl */
242 r = 0;
243 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
244 o(0xdb); /* fldt */
245 r = 5;
246 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
247 o(0xbe0f); /* movsbl */
248 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
249 o(0xb60f); /* movzbl */
250 } else if ((ft & VT_TYPE) == VT_SHORT) {
251 o(0xbf0f); /* movswl */
252 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
253 o(0xb70f); /* movzwl */
254 } else {
255 o(0x8b); /* movl */
257 gen_modrm(r, fr, sv->sym, fc);
258 } else {
259 if (v == VT_CONST) {
260 o(0xb8 + r); /* mov $xx, r */
261 gen_addr32(fr, sv->sym, fc);
262 } else if (v == VT_LOCAL) {
263 if (fc) {
264 o(0x8d); /* lea xxx(%ebp), r */
265 gen_modrm(r, VT_LOCAL, sv->sym, fc);
266 } else {
267 o(0x89);
268 o(0xe8 + r); /* mov %ebp, r */
270 } else if (v == VT_CMP) {
271 o(0x0f); /* setxx %br */
272 o(fc);
273 o(0xc0 + r);
274 o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
275 } else if (v == VT_JMP || v == VT_JMPI) {
276 t = v & 1;
277 oad(0xb8 + r, t); /* mov $1, r */
278 o(0x05eb); /* jmp after */
279 gsym(fc);
280 oad(0xb8 + r, t ^ 1); /* mov $0, r */
281 } else if (v != r) {
282 o(0x89);
283 o(0xc0 + r + v * 8); /* mov v, r */
288 /* store register 'r' in lvalue 'v' */
289 ST_FUNC void store(int r, SValue *v)
291 int fr, bt, ft, fc;
293 #ifdef TCC_TARGET_PE
294 SValue v2;
295 v = pe_getimport(v, &v2);
296 #endif
298 ft = v->type.t;
299 fc = v->c.i;
300 fr = v->r & VT_VALMASK;
301 ft &= ~(VT_VOLATILE | VT_CONSTANT);
302 bt = ft & VT_BTYPE;
303 /* XXX: incorrect if float reg to reg */
304 if (bt == VT_FLOAT) {
305 o(0xd9); /* fsts */
306 r = 2;
307 } else if (bt == VT_DOUBLE) {
308 o(0xdd); /* fstpl */
309 r = 2;
310 } else if (bt == VT_LDOUBLE) {
311 o(0xc0d9); /* fld %st(0) */
312 o(0xdb); /* fstpt */
313 r = 7;
314 } else {
315 if (bt == VT_SHORT)
316 o(0x66);
317 if (bt == VT_BYTE || bt == VT_BOOL)
318 o(0x88);
319 else
320 o(0x89);
322 if (fr == VT_CONST ||
323 fr == VT_LOCAL ||
324 (v->r & VT_LVAL)) {
325 gen_modrm(r, v->r, v->sym, fc);
326 } else if (fr != r) {
327 o(0xc0 + fr + r * 8); /* mov r, fr */
331 static void gadd_sp(int val)
333 if (val == (char)val) {
334 o(0xc483);
335 g(val);
336 } else {
337 oad(0xc481, val); /* add $xxx, %esp */
341 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
342 static void gen_static_call(int v)
344 Sym *sym;
346 sym = external_global_sym(v, &func_old_type);
347 oad(0xe8, -4);
348 greloc(cur_text_section, sym, ind-4, R_386_PC32);
350 #endif
352 /* 'is_jmp' is '1' if it is a jump */
353 static void gcall_or_jmp(int is_jmp)
355 int r;
356 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
357 /* constant and relocation case */
358 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
359 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
360 } else {
361 /* otherwise, indirect call */
362 r = gv(RC_INT);
363 o(0xff); /* call/jmp *r */
364 o(0xd0 + r + (is_jmp << 4));
368 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
369 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
371 /* Return the number of registers needed to return the struct, or 0 if
372 returning via struct pointer. */
373 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
375 #ifdef TCC_TARGET_PE
376 int size, align;
377 *ret_align = 1; // Never have to re-align return values for x86
378 *regsize = 4;
379 size = type_size(vt, &align);
380 if (size > 8 || (size & (size - 1)))
381 return 0;
382 if (size == 8)
383 ret->t = VT_LLONG;
384 else if (size == 4)
385 ret->t = VT_INT;
386 else if (size == 2)
387 ret->t = VT_SHORT;
388 else
389 ret->t = VT_BYTE;
390 ret->ref = NULL;
391 return 1;
392 #else
393 *ret_align = 1; // Never have to re-align return values for x86
394 return 0;
395 #endif
398 /* Generate function call. The function address is pushed first, then
399 all the parameters in call order. This functions pops all the
400 parameters and the function address. */
401 ST_FUNC void gfunc_call(int nb_args)
403 int size, align, r, args_size, i, func_call;
404 Sym *func_sym;
406 #ifdef CONFIG_TCC_BCHECK
407 if (tcc_state->do_bounds_check)
408 gbound_args(nb_args);
409 #endif
411 args_size = 0;
412 for(i = 0;i < nb_args; i++) {
413 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
414 size = type_size(&vtop->type, &align);
415 /* align to stack align size */
416 size = (size + 3) & ~3;
417 /* allocate the necessary size on stack */
418 oad(0xec81, size); /* sub $xxx, %esp */
419 /* generate structure store */
420 r = get_reg(RC_INT);
421 o(0x89); /* mov %esp, r */
422 o(0xe0 + r);
423 vset(&vtop->type, r | VT_LVAL, 0);
424 vswap();
425 vstore();
426 args_size += size;
427 } else if (is_float(vtop->type.t)) {
428 gv(RC_FLOAT); /* only one float register */
429 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
430 size = 4;
431 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
432 size = 8;
433 else
434 size = 12;
435 oad(0xec81, size); /* sub $xxx, %esp */
436 if (size == 12)
437 o(0x7cdb);
438 else
439 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
440 g(0x24);
441 g(0x00);
442 args_size += size;
443 } else {
444 /* simple type (currently always same size) */
445 /* XXX: implicit cast ? */
446 r = gv(RC_INT);
447 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
448 size = 8;
449 o(0x50 + vtop->r2); /* push r */
450 } else {
451 size = 4;
453 o(0x50 + r); /* push r */
454 args_size += size;
456 vtop--;
458 save_regs(0); /* save used temporary registers */
459 func_sym = vtop->type.ref;
460 func_call = func_sym->f.func_call;
461 /* fast call case */
462 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
463 func_call == FUNC_FASTCALLW) {
464 int fastcall_nb_regs;
465 uint8_t *fastcall_regs_ptr;
466 if (func_call == FUNC_FASTCALLW) {
467 fastcall_regs_ptr = fastcallw_regs;
468 fastcall_nb_regs = 2;
469 } else {
470 fastcall_regs_ptr = fastcall_regs;
471 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
473 for(i = 0;i < fastcall_nb_regs; i++) {
474 if (args_size <= 0)
475 break;
476 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
477 /* XXX: incorrect for struct/floats */
478 args_size -= 4;
481 #ifndef TCC_TARGET_PE
482 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
483 args_size -= 4;
484 #endif
486 gcall_or_jmp(0);
488 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
489 gadd_sp(args_size);
490 vtop--;
493 #ifdef TCC_TARGET_PE
494 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
495 #else
496 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
497 #endif
499 /* generate function prolog of type 't' */
500 ST_FUNC void gfunc_prolog(Sym *func_sym)
502 CType *func_type = &func_sym->type;
503 int addr, align, size, func_call, fastcall_nb_regs;
504 int param_index, param_addr;
505 uint8_t *fastcall_regs_ptr;
506 Sym *sym;
507 CType *type;
509 sym = func_type->ref;
510 func_call = sym->f.func_call;
511 addr = 8;
512 loc = 0;
513 func_vc = 0;
515 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
516 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
517 fastcall_regs_ptr = fastcall_regs;
518 } else if (func_call == FUNC_FASTCALLW) {
519 fastcall_nb_regs = 2;
520 fastcall_regs_ptr = fastcallw_regs;
521 } else {
522 fastcall_nb_regs = 0;
523 fastcall_regs_ptr = NULL;
525 param_index = 0;
527 ind += FUNC_PROLOG_SIZE;
528 func_sub_sp_offset = ind;
529 /* if the function returns a structure, then add an
530 implicit pointer parameter */
531 func_vt = sym->type;
532 func_var = (sym->f.func_type == FUNC_ELLIPSIS);
533 #ifdef TCC_TARGET_PE
534 size = type_size(&func_vt,&align);
535 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
536 && (size > 8 || (size & (size - 1)))) {
537 #else
538 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
539 #endif
540 /* XXX: fastcall case ? */
541 func_vc = addr;
542 addr += 4;
543 param_index++;
545 /* define parameters */
546 while ((sym = sym->next) != NULL) {
547 type = &sym->type;
548 size = type_size(type, &align);
549 size = (size + 3) & ~3;
550 #ifdef FUNC_STRUCT_PARAM_AS_PTR
551 /* structs are passed as pointer */
552 if ((type->t & VT_BTYPE) == VT_STRUCT) {
553 size = 4;
555 #endif
556 if (param_index < fastcall_nb_regs) {
557 /* save FASTCALL register */
558 loc -= 4;
559 o(0x89); /* movl */
560 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
561 param_addr = loc;
562 } else {
563 param_addr = addr;
564 addr += size;
566 sym_push(sym->v & ~SYM_FIELD, type,
567 VT_LOCAL | VT_LVAL, param_addr);
568 param_index++;
570 func_ret_sub = 0;
571 /* pascal type call or fastcall ? */
572 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
573 func_ret_sub = addr - 8;
574 #ifndef TCC_TARGET_PE
575 else if (func_vc)
576 func_ret_sub = 4;
577 #endif
579 #ifdef CONFIG_TCC_BCHECK
580 if (tcc_state->do_bounds_check)
581 gen_bounds_prolog();
582 #endif
585 /* generate function epilog */
586 ST_FUNC void gfunc_epilog(void)
588 addr_t v, saved_ind;
590 #ifdef CONFIG_TCC_BCHECK
591 if (tcc_state->do_bounds_check)
592 gen_bounds_epilog();
593 #endif
595 /* align local size to word & save local variables */
596 v = (-loc + 3) & -4;
598 #if USE_EBX
599 o(0x8b);
600 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
601 #endif
603 o(0xc9); /* leave */
604 if (func_ret_sub == 0) {
605 o(0xc3); /* ret */
606 } else {
607 o(0xc2); /* ret n */
608 g(func_ret_sub);
609 g(func_ret_sub >> 8);
611 saved_ind = ind;
612 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
613 #ifdef TCC_TARGET_PE
614 if (v >= 4096) {
615 oad(0xb8, v); /* mov stacksize, %eax */
616 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
617 } else
618 #endif
620 o(0xe58955); /* push %ebp, mov %esp, %ebp */
621 o(0xec81); /* sub esp, stacksize */
622 gen_le32(v);
623 #ifdef TCC_TARGET_PE
624 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
625 #endif
627 o(0x53 * USE_EBX); /* push ebx */
628 ind = saved_ind;
631 /* generate a jump to a label */
632 ST_FUNC int gjmp(int t)
634 return gjmp2(0xe9, t);
637 /* generate a jump to a fixed address */
638 ST_FUNC void gjmp_addr(int a)
640 int r;
641 r = a - ind - 2;
642 if (r == (char)r) {
643 g(0xeb);
644 g(r);
645 } else {
646 oad(0xe9, a - ind - 5);
650 #if 0
651 /* generate a jump to a fixed address */
652 ST_FUNC void gjmp_cond_addr(int a, int op)
654 int r = a - ind - 2;
655 if (r == (char)r)
656 g(op - 32), g(r);
657 else
658 g(0x0f), gjmp2(op - 16, r - 4);
660 #endif
662 ST_FUNC int gjmp_append(int n, int t)
664 void *p;
665 /* insert vtop->c jump list in t */
666 if (n) {
667 uint32_t n1 = n, n2;
668 while ((n2 = read32le(p = cur_text_section->data + n1)))
669 n1 = n2;
670 write32le(p, t);
671 t = n;
673 return t;
676 ST_FUNC int gjmp_cond(int op, int t)
678 g(0x0f);
679 t = gjmp2(op - 16, t);
680 return t;
683 ST_FUNC void gen_opi(int op)
685 int r, fr, opc, c;
687 switch(op) {
688 case '+':
689 case TOK_ADDC1: /* add with carry generation */
690 opc = 0;
691 gen_op8:
692 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
693 /* constant case */
694 vswap();
695 r = gv(RC_INT);
696 vswap();
697 c = vtop->c.i;
698 if (c == (char)c) {
699 /* generate inc and dec for smaller code */
700 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
701 opc = (c == 1) ^ (op == '+');
702 o (0x40 | (opc << 3) | r); // inc,dec
703 } else {
704 o(0x83);
705 o(0xc0 | (opc << 3) | r);
706 g(c);
708 } else {
709 o(0x81);
710 oad(0xc0 | (opc << 3) | r, c);
712 } else {
713 gv2(RC_INT, RC_INT);
714 r = vtop[-1].r;
715 fr = vtop[0].r;
716 o((opc << 3) | 0x01);
717 o(0xc0 + r + fr * 8);
719 vtop--;
720 if (op >= TOK_ULT && op <= TOK_GT)
721 vset_VT_CMP(op);
722 break;
723 case '-':
724 case TOK_SUBC1: /* sub with carry generation */
725 opc = 5;
726 goto gen_op8;
727 case TOK_ADDC2: /* add with carry use */
728 opc = 2;
729 goto gen_op8;
730 case TOK_SUBC2: /* sub with carry use */
731 opc = 3;
732 goto gen_op8;
733 case '&':
734 opc = 4;
735 goto gen_op8;
736 case '^':
737 opc = 6;
738 goto gen_op8;
739 case '|':
740 opc = 1;
741 goto gen_op8;
742 case '*':
743 gv2(RC_INT, RC_INT);
744 r = vtop[-1].r;
745 fr = vtop[0].r;
746 vtop--;
747 o(0xaf0f); /* imul fr, r */
748 o(0xc0 + fr + r * 8);
749 break;
750 case TOK_SHL:
751 opc = 4;
752 goto gen_shift;
753 case TOK_SHR:
754 opc = 5;
755 goto gen_shift;
756 case TOK_SAR:
757 opc = 7;
758 gen_shift:
759 opc = 0xc0 | (opc << 3);
760 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
761 /* constant case */
762 vswap();
763 r = gv(RC_INT);
764 vswap();
765 c = vtop->c.i & 0x1f;
766 o(0xc1); /* shl/shr/sar $xxx, r */
767 o(opc | r);
768 g(c);
769 } else {
770 /* we generate the shift in ecx */
771 gv2(RC_INT, RC_ECX);
772 r = vtop[-1].r;
773 o(0xd3); /* shl/shr/sar %cl, r */
774 o(opc | r);
776 vtop--;
777 break;
778 case '/':
779 case TOK_UDIV:
780 case TOK_PDIV:
781 case '%':
782 case TOK_UMOD:
783 case TOK_UMULL:
784 /* first operand must be in eax */
785 /* XXX: need better constraint for second operand */
786 gv2(RC_EAX, RC_ECX);
787 r = vtop[-1].r;
788 fr = vtop[0].r;
789 vtop--;
790 save_reg(TREG_EDX);
791 /* save EAX too if used otherwise */
792 save_reg_upstack(TREG_EAX, 1);
793 if (op == TOK_UMULL) {
794 o(0xf7); /* mul fr */
795 o(0xe0 + fr);
796 vtop->r2 = TREG_EDX;
797 r = TREG_EAX;
798 } else {
799 if (op == TOK_UDIV || op == TOK_UMOD) {
800 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
801 o(0xf0 + fr);
802 } else {
803 o(0xf799); /* cltd, idiv fr, %eax */
804 o(0xf8 + fr);
806 if (op == '%' || op == TOK_UMOD)
807 r = TREG_EDX;
808 else
809 r = TREG_EAX;
811 vtop->r = r;
812 break;
813 default:
814 opc = 7;
815 goto gen_op8;
819 /* generate a floating point operation 'v = t1 op t2' instruction. The
820 two operands are guaranteed to have the same floating point type */
821 /* XXX: need to use ST1 too */
822 ST_FUNC void gen_opf(int op)
824 int a, ft, fc, swapped, r;
826 /* convert constants to memory references */
827 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
828 vswap();
829 gv(RC_FLOAT);
830 vswap();
832 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
833 gv(RC_FLOAT);
835 /* must put at least one value in the floating point register */
836 if ((vtop[-1].r & VT_LVAL) &&
837 (vtop[0].r & VT_LVAL)) {
838 vswap();
839 gv(RC_FLOAT);
840 vswap();
842 swapped = 0;
843 /* swap the stack if needed so that t1 is the register and t2 is
844 the memory reference */
845 if (vtop[-1].r & VT_LVAL) {
846 vswap();
847 swapped = 1;
849 if (op >= TOK_ULT && op <= TOK_GT) {
850 /* load on stack second operand */
851 load(TREG_ST0, vtop);
852 save_reg(TREG_EAX); /* eax is used by FP comparison code */
853 if (op == TOK_GE || op == TOK_GT)
854 swapped = !swapped;
855 else if (op == TOK_EQ || op == TOK_NE)
856 swapped = 0;
857 if (swapped)
858 o(0xc9d9); /* fxch %st(1) */
859 if (op == TOK_EQ || op == TOK_NE)
860 o(0xe9da); /* fucompp */
861 else
862 o(0xd9de); /* fcompp */
863 o(0xe0df); /* fnstsw %ax */
864 if (op == TOK_EQ) {
865 o(0x45e480); /* and $0x45, %ah */
866 o(0x40fC80); /* cmp $0x40, %ah */
867 } else if (op == TOK_NE) {
868 o(0x45e480); /* and $0x45, %ah */
869 o(0x40f480); /* xor $0x40, %ah */
870 op = TOK_NE;
871 } else if (op == TOK_GE || op == TOK_LE) {
872 o(0x05c4f6); /* test $0x05, %ah */
873 op = TOK_EQ;
874 } else {
875 o(0x45c4f6); /* test $0x45, %ah */
876 op = TOK_EQ;
878 vtop--;
879 vset_VT_CMP(op);
880 } else {
881 /* no memory reference possible for long double operations */
882 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
883 load(TREG_ST0, vtop);
884 swapped = !swapped;
887 switch(op) {
888 default:
889 case '+':
890 a = 0;
891 break;
892 case '-':
893 a = 4;
894 if (swapped)
895 a++;
896 break;
897 case '*':
898 a = 1;
899 break;
900 case '/':
901 a = 6;
902 if (swapped)
903 a++;
904 break;
906 ft = vtop->type.t;
907 fc = vtop->c.i;
908 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
909 o(0xde); /* fxxxp %st, %st(1) */
910 o(0xc1 + (a << 3));
911 } else {
912 /* if saved lvalue, then we must reload it */
913 r = vtop->r;
914 if ((r & VT_VALMASK) == VT_LLOCAL) {
915 SValue v1;
916 r = get_reg(RC_INT);
917 v1.type.t = VT_INT;
918 v1.r = VT_LOCAL | VT_LVAL;
919 v1.c.i = fc;
920 load(r, &v1);
921 fc = 0;
924 if ((ft & VT_BTYPE) == VT_DOUBLE)
925 o(0xdc);
926 else
927 o(0xd8);
928 gen_modrm(a, r, vtop->sym, fc);
930 vtop--;
934 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
935 and 'long long' cases. */
936 ST_FUNC void gen_cvt_itof(int t)
938 save_reg(TREG_ST0);
939 gv(RC_INT);
940 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
941 /* signed long long to float/double/long double (unsigned case
942 is handled generically) */
943 o(0x50 + vtop->r2); /* push r2 */
944 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
945 o(0x242cdf); /* fildll (%esp) */
946 o(0x08c483); /* add $8, %esp */
947 vtop->r2 = VT_CONST;
948 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
949 (VT_INT | VT_UNSIGNED)) {
950 /* unsigned int to float/double/long double */
951 o(0x6a); /* push $0 */
952 g(0x00);
953 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
954 o(0x242cdf); /* fildll (%esp) */
955 o(0x08c483); /* add $8, %esp */
956 } else {
957 /* int to float/double/long double */
958 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
959 o(0x2404db); /* fildl (%esp) */
960 o(0x04c483); /* add $4, %esp */
962 vtop->r2 = VT_CONST;
963 vtop->r = TREG_ST0;
966 /* convert fp to int 't' type */
967 ST_FUNC void gen_cvt_ftoi(int t)
969 int bt = vtop->type.t & VT_BTYPE;
970 if (bt == VT_FLOAT)
971 vpush_global_sym(&func_old_type, TOK___fixsfdi);
972 else if (bt == VT_LDOUBLE)
973 vpush_global_sym(&func_old_type, TOK___fixxfdi);
974 else
975 vpush_global_sym(&func_old_type, TOK___fixdfdi);
976 vswap();
977 gfunc_call(1);
978 vpushi(0);
979 vtop->r = REG_IRET;
980 if ((t & VT_BTYPE) == VT_LLONG)
981 vtop->r2 = REG_IRE2;
984 /* convert from one floating point type to another */
985 ST_FUNC void gen_cvt_ftof(int t)
987 /* all we have to do on i386 is to put the float in a register */
988 gv(RC_FLOAT);
991 /* char/short to int conversion */
992 ST_FUNC void gen_cvt_csti(int t)
994 int r, sz, xl;
995 r = gv(RC_INT);
996 sz = !(t & VT_UNSIGNED);
997 xl = (t & VT_BTYPE) == VT_SHORT;
998 o(0xc0b60f /* mov[sz] %a[xl], %eax */
999 | (sz << 3 | xl) << 8
1000 | (r << 3 | r) << 16
1004 /* computed goto support */
1005 ST_FUNC void ggoto(void)
1007 gcall_or_jmp(1);
1008 vtop--;
1011 /* bound check support functions */
1012 #ifdef CONFIG_TCC_BCHECK
1013 /* generate a bounded pointer addition */
1014 ST_FUNC void gen_bounded_ptr_add(void)
1016 vpush_global_sym(&func_old_type, TOK___bound_ptr_add);
1017 vrott(3);
1018 gfunc_call(2);
1019 vpushi(0);
1020 /* returned pointer is in eax */
1021 vtop->r = TREG_EAX | VT_BOUNDED;
1022 if (nocode_wanted)
1023 return;
1024 /* relocation offset of the bounding function call point */
1025 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1028 /* patch pointer addition in vtop so that pointer dereferencing is
1029 also tested */
1030 ST_FUNC void gen_bounded_ptr_deref(void)
1032 addr_t func;
1033 int size, align;
1034 Elf32_Rel *rel;
1035 Sym *sym;
1037 if (nocode_wanted)
1038 return;
1040 size = type_size(&vtop->type, &align);
1041 switch(size) {
1042 case 1: func = TOK___bound_ptr_indir1; break;
1043 case 2: func = TOK___bound_ptr_indir2; break;
1044 case 4: func = TOK___bound_ptr_indir4; break;
1045 case 8: func = TOK___bound_ptr_indir8; break;
1046 case 12: func = TOK___bound_ptr_indir12; break;
1047 case 16: func = TOK___bound_ptr_indir16; break;
1048 default:
1049 /* may happen with struct member access */
1050 return;
1051 //tcc_error("unhandled size when dereferencing bounded pointer");
1052 //func = 0;
1053 //break;
1055 sym = external_global_sym(func, &func_old_type);
1056 if (!sym->c)
1057 put_extern_sym(sym, NULL, 0, 0);
1058 /* patch relocation */
1059 /* XXX: find a better solution ? */
1060 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1061 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1064 static void gen_bounds_prolog(void)
1066 /* leave some room for bound checking code */
1067 func_bound_offset = lbounds_section->data_offset;
1068 func_bound_ind = ind;
1069 oad(0xb8, 0); /* lbound section pointer */
1070 oad(0xb8, 0); /* call to function */
1073 static void gen_bounds_epilog(void)
1075 addr_t saved_ind;
1076 addr_t *bounds_ptr;
1077 Sym *sym_data;
1079 /* add end of table info */
1080 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
1081 *bounds_ptr = 0;
1083 /* generate bound local allocation */
1084 saved_ind = ind;
1085 ind = func_bound_ind;
1086 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
1087 func_bound_offset, lbounds_section->data_offset);
1088 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1089 ind = ind + 5;
1090 gen_static_call(TOK___bound_local_new);
1091 ind = saved_ind;
1093 /* generate bound check local freeing */
1094 o(0x5250); /* save returned value, if any */
1095 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1096 oad(0xb8, 0); /* mov %eax, xxx */
1097 gen_static_call(TOK___bound_local_delete);
1098 o(0x585a); /* restore returned value, if any */
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 int use_call = 0;
1119 #if defined(CONFIG_TCC_BCHECK)
1120 use_call = tcc_state->do_bounds_check;
1121 #endif
1122 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
1123 use_call = 1;
1124 #endif
1125 if (use_call)
1127 vpush_global_sym(&func_old_type, TOK_alloca);
1128 vswap(); /* Move alloca ref past allocation size */
1129 gfunc_call(1);
1131 else {
1132 int r;
1133 r = gv(RC_INT); /* allocation size */
1134 /* sub r,%rsp */
1135 o(0x2b);
1136 o(0xe0 | r);
1137 /* We align to 16 bytes rather than align */
1138 /* and ~15, %esp */
1139 o(0xf0e483);
1140 vpop();
1144 /* end of X86 code generator */
1145 /*************************************************************/
1146 #endif
1147 /*************************************************************/