tccelf.c: write section headers before sections
[tinycc.git] / i386-gen.c
bloba892dca00e37db33db1c2f4ccc67f60ea52eaf04
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_EDX 0x0008
35 #define RC_ECX 0x0010
36 #define RC_EBX 0x0020
37 #define RC_ST0 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 ST_DATA const char * const target_machine_defs =
85 "__i386__\0"
86 "__i386\0"
89 /* define to 1/0 to [not] have EBX as 4th register */
90 #define USE_EBX 0
92 ST_DATA const int reg_classes[NB_REGS] = {
93 /* eax */ RC_INT | RC_EAX,
94 /* ecx */ RC_INT | RC_ECX,
95 /* edx */ RC_INT | RC_EDX,
96 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
97 /* st0 */ RC_FLOAT | RC_ST0,
100 static unsigned long func_sub_sp_offset;
101 static int func_ret_sub;
102 #ifdef CONFIG_TCC_BCHECK
103 static addr_t func_bound_offset;
104 static unsigned long func_bound_ind;
105 ST_DATA int func_bound_add_epilog;
106 static void gen_bounds_prolog(void);
107 static void gen_bounds_epilog(void);
108 #endif
110 /* XXX: make it faster ? */
111 ST_FUNC void g(int c)
113 int ind1;
114 if (nocode_wanted)
115 return;
116 ind1 = ind + 1;
117 if (ind1 > cur_text_section->data_allocated)
118 section_realloc(cur_text_section, ind1);
119 cur_text_section->data[ind] = c;
120 ind = ind1;
123 ST_FUNC void o(unsigned int c)
125 while (c) {
126 g(c);
127 c = c >> 8;
131 ST_FUNC void gen_le16(int v)
133 g(v);
134 g(v >> 8);
137 ST_FUNC void gen_le32(int c)
139 g(c);
140 g(c >> 8);
141 g(c >> 16);
142 g(c >> 24);
145 /* output a symbol and patch all calls to it */
146 ST_FUNC void gsym_addr(int t, int a)
148 while (t) {
149 unsigned char *ptr = cur_text_section->data + t;
150 uint32_t n = read32le(ptr); /* next value */
151 write32le(ptr, a - t - 4);
152 t = n;
156 /* instruction + 4 bytes data. Return the address of the data */
157 static int oad(int c, int s)
159 int t;
160 if (nocode_wanted)
161 return s;
162 o(c);
163 t = ind;
164 gen_le32(s);
165 return t;
168 ST_FUNC void gen_fill_nops(int bytes)
170 while (bytes--)
171 g(0x90);
174 /* generate jmp to a label */
175 #define gjmp2(instr,lbl) oad(instr,lbl)
177 /* output constant with relocation if 'r & VT_SYM' is true */
178 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
180 if (r & VT_SYM)
181 greloc(cur_text_section, sym, ind, R_386_32);
182 gen_le32(c);
185 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
187 if (r & VT_SYM)
188 greloc(cur_text_section, sym, ind, R_386_PC32);
189 gen_le32(c - 4);
192 /* generate a modrm reference. 'op_reg' contains the additional 3
193 opcode bits */
194 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
196 op_reg = op_reg << 3;
197 if ((r & VT_VALMASK) == VT_CONST) {
198 /* constant memory reference */
199 o(0x05 | op_reg);
200 gen_addr32(r, sym, c);
201 } else if ((r & VT_VALMASK) == VT_LOCAL) {
202 /* currently, we use only ebp as base */
203 if (c == (char)c) {
204 /* short reference */
205 o(0x45 | op_reg);
206 g(c);
207 } else {
208 oad(0x85 | op_reg, c);
210 } else {
211 g(0x00 | op_reg | (r & VT_VALMASK));
215 /* load 'r' from value 'sv' */
216 ST_FUNC void load(int r, SValue *sv)
218 int v, t, ft, fc, fr;
219 SValue v1;
221 #ifdef TCC_TARGET_PE
222 SValue v2;
223 sv = pe_getimport(sv, &v2);
224 #endif
226 fr = sv->r;
227 ft = sv->type.t & ~VT_DEFSIGN;
228 fc = sv->c.i;
230 ft &= ~(VT_VOLATILE | VT_CONSTANT);
232 v = fr & VT_VALMASK;
233 if (fr & VT_LVAL) {
234 if (v == VT_LLOCAL) {
235 v1.type.t = VT_INT;
236 v1.r = VT_LOCAL | VT_LVAL;
237 v1.c.i = fc;
238 v1.sym = NULL;
239 fr = r;
240 if (!(reg_classes[fr] & RC_INT))
241 fr = get_reg(RC_INT);
242 load(fr, &v1);
244 if ((ft & VT_BTYPE) == VT_FLOAT) {
245 o(0xd9); /* flds */
246 r = 0;
247 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
248 o(0xdd); /* fldl */
249 r = 0;
250 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
251 o(0xdb); /* fldt */
252 r = 5;
253 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
254 o(0xbe0f); /* movsbl */
255 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
256 o(0xb60f); /* movzbl */
257 } else if ((ft & VT_TYPE) == VT_SHORT) {
258 o(0xbf0f); /* movswl */
259 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
260 o(0xb70f); /* movzwl */
261 } else {
262 o(0x8b); /* movl */
264 gen_modrm(r, fr, sv->sym, fc);
265 } else {
266 if (v == VT_CONST) {
267 o(0xb8 + r); /* mov $xx, r */
268 gen_addr32(fr, sv->sym, fc);
269 } else if (v == VT_LOCAL) {
270 if (fc) {
271 o(0x8d); /* lea xxx(%ebp), r */
272 gen_modrm(r, VT_LOCAL, sv->sym, fc);
273 } else {
274 o(0x89);
275 o(0xe8 + r); /* mov %ebp, r */
277 } else if (v == VT_CMP) {
278 o(0x0f); /* setxx %br */
279 o(fc);
280 o(0xc0 + r);
281 o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
282 } else if (v == VT_JMP || v == VT_JMPI) {
283 t = v & 1;
284 oad(0xb8 + r, t); /* mov $1, r */
285 o(0x05eb); /* jmp after */
286 gsym(fc);
287 oad(0xb8 + r, t ^ 1); /* mov $0, r */
288 } else if (v != r) {
289 o(0x89);
290 o(0xc0 + r + v * 8); /* mov v, r */
295 /* store register 'r' in lvalue 'v' */
296 ST_FUNC void store(int r, SValue *v)
298 int fr, bt, ft, fc;
300 #ifdef TCC_TARGET_PE
301 SValue v2;
302 v = pe_getimport(v, &v2);
303 #endif
305 ft = v->type.t;
306 fc = v->c.i;
307 fr = v->r & VT_VALMASK;
308 ft &= ~(VT_VOLATILE | VT_CONSTANT);
309 bt = ft & VT_BTYPE;
310 /* XXX: incorrect if float reg to reg */
311 if (bt == VT_FLOAT) {
312 o(0xd9); /* fsts */
313 r = 2;
314 } else if (bt == VT_DOUBLE) {
315 o(0xdd); /* fstpl */
316 r = 2;
317 } else if (bt == VT_LDOUBLE) {
318 o(0xc0d9); /* fld %st(0) */
319 o(0xdb); /* fstpt */
320 r = 7;
321 } else {
322 if (bt == VT_SHORT)
323 o(0x66);
324 if (bt == VT_BYTE || bt == VT_BOOL)
325 o(0x88);
326 else
327 o(0x89);
329 if (fr == VT_CONST ||
330 fr == VT_LOCAL ||
331 (v->r & VT_LVAL)) {
332 gen_modrm(r, v->r, v->sym, fc);
333 } else if (fr != r) {
334 o(0xc0 + fr + r * 8); /* mov r, fr */
338 static void gadd_sp(int val)
340 if (val == (char)val) {
341 o(0xc483);
342 g(val);
343 } else {
344 oad(0xc481, val); /* add $xxx, %esp */
348 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
349 static void gen_static_call(int v)
351 Sym *sym;
353 sym = external_helper_sym(v);
354 oad(0xe8, -4);
355 greloc(cur_text_section, sym, ind-4, R_386_PC32);
357 #endif
359 /* 'is_jmp' is '1' if it is a jump */
360 static void gcall_or_jmp(int is_jmp)
362 int r;
363 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
364 /* constant and relocation case */
365 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
366 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
367 } else {
368 /* otherwise, indirect call */
369 r = gv(RC_INT);
370 o(0xff); /* call/jmp *r */
371 o(0xd0 + r + (is_jmp << 4));
375 static const uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
376 static const uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
378 /* Return the number of registers needed to return the struct, or 0 if
379 returning via struct pointer. */
380 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
382 #if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
383 int size, align, nregs;
384 *ret_align = 1; // Never have to re-align return values for x86
385 *regsize = 4;
386 size = type_size(vt, &align);
387 if (size > 8 || (size & (size - 1)))
388 return 0;
389 nregs = 1;
390 if (size == 8)
391 ret->t = VT_INT, nregs = 2;
392 else if (size == 4)
393 ret->t = VT_INT;
394 else if (size == 2)
395 ret->t = VT_SHORT;
396 else
397 ret->t = VT_BYTE;
398 ret->ref = NULL;
399 return nregs;
400 #else
401 *ret_align = 1; // Never have to re-align return values for x86
402 return 0;
403 #endif
406 /* Generate function call. The function address is pushed first, then
407 all the parameters in call order. This functions pops all the
408 parameters and the function address. */
409 ST_FUNC void gfunc_call(int nb_args)
411 int size, align, r, args_size, i, func_call;
412 Sym *func_sym;
414 #ifdef CONFIG_TCC_BCHECK
415 if (tcc_state->do_bounds_check)
416 gbound_args(nb_args);
417 #endif
419 args_size = 0;
420 for(i = 0;i < nb_args; i++) {
421 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
422 size = type_size(&vtop->type, &align);
423 /* align to stack align size */
424 size = (size + 3) & ~3;
425 /* allocate the necessary size on stack */
426 #ifdef TCC_TARGET_PE
427 if (size >= 4096) {
428 r = get_reg(RC_EAX);
429 oad(0x68, size); // push size
430 /* cannot call normal 'alloca' with bound checking */
431 gen_static_call(tok_alloc_const("__alloca"));
432 gadd_sp(4);
433 } else
434 #endif
436 oad(0xec81, size); /* sub $xxx, %esp */
437 /* generate structure store */
438 r = get_reg(RC_INT);
439 o(0xe089 + (r << 8)); /* mov %esp, 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 || func_call == FUNC_THISCALL) {
482 int fastcall_nb_regs;
483 const uint8_t *fastcall_regs_ptr;
484 if (func_call == FUNC_FASTCALLW) {
485 fastcall_regs_ptr = fastcallw_regs;
486 fastcall_nb_regs = 2;
487 } else if (func_call == FUNC_THISCALL) {
488 fastcall_regs_ptr = fastcallw_regs;
489 fastcall_nb_regs = 1;
490 } else {
491 fastcall_regs_ptr = fastcall_regs;
492 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
494 for(i = 0;i < fastcall_nb_regs; i++) {
495 if (args_size <= 0)
496 break;
497 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
498 /* XXX: incorrect for struct/floats */
499 args_size -= 4;
502 #if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
503 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
504 args_size -= 4;
505 #endif
507 gcall_or_jmp(0);
509 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_THISCALL && func_call != FUNC_FASTCALLW)
510 gadd_sp(args_size);
511 vtop--;
514 #ifdef TCC_TARGET_PE
515 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
516 #else
517 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
518 #endif
520 /* generate function prolog of type 't' */
521 ST_FUNC void gfunc_prolog(Sym *func_sym)
523 CType *func_type = &func_sym->type;
524 int addr, align, size, func_call, fastcall_nb_regs;
525 int param_index, param_addr;
526 const uint8_t *fastcall_regs_ptr;
527 Sym *sym;
528 CType *type;
530 sym = func_type->ref;
531 func_call = sym->f.func_call;
532 addr = 8;
533 loc = 0;
534 func_vc = 0;
536 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
537 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
538 fastcall_regs_ptr = fastcall_regs;
539 } else if (func_call == FUNC_FASTCALLW) {
540 fastcall_nb_regs = 2;
541 fastcall_regs_ptr = fastcallw_regs;
542 } else if (func_call == FUNC_THISCALL) {
543 fastcall_nb_regs = 1;
544 fastcall_regs_ptr = fastcallw_regs;
545 } else {
546 fastcall_nb_regs = 0;
547 fastcall_regs_ptr = NULL;
549 param_index = 0;
551 ind += FUNC_PROLOG_SIZE;
552 func_sub_sp_offset = ind;
553 /* if the function returns a structure, then add an
554 implicit pointer parameter */
555 #if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
556 size = type_size(&func_vt,&align);
557 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
558 && (size > 8 || (size & (size - 1)))) {
559 #else
560 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
561 #endif
562 /* XXX: fastcall case ? */
563 func_vc = addr;
564 addr += 4;
565 param_index++;
567 /* define parameters */
568 while ((sym = sym->next) != NULL) {
569 type = &sym->type;
570 size = type_size(type, &align);
571 size = (size + 3) & ~3;
572 #ifdef FUNC_STRUCT_PARAM_AS_PTR
573 /* structs are passed as pointer */
574 if ((type->t & VT_BTYPE) == VT_STRUCT) {
575 size = 4;
577 #endif
578 if (param_index < fastcall_nb_regs) {
579 /* save FASTCALL register */
580 loc -= 4;
581 o(0x89); /* movl */
582 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
583 param_addr = loc;
584 } else {
585 param_addr = addr;
586 addr += size;
588 sym_push(sym->v & ~SYM_FIELD, type,
589 VT_LOCAL | VT_LVAL, param_addr);
590 param_index++;
592 func_ret_sub = 0;
593 /* pascal type call or fastcall ? */
594 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW || func_call == FUNC_THISCALL)
595 func_ret_sub = addr - 8;
596 #if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
597 else if (func_vc)
598 func_ret_sub = 4;
599 #endif
601 #ifdef CONFIG_TCC_BCHECK
602 if (tcc_state->do_bounds_check)
603 gen_bounds_prolog();
604 #endif
607 /* generate function epilog */
608 ST_FUNC void gfunc_epilog(void)
610 addr_t v, saved_ind;
612 #ifdef CONFIG_TCC_BCHECK
613 if (tcc_state->do_bounds_check)
614 gen_bounds_epilog();
615 #endif
617 /* align local size to word & save local variables */
618 v = (-loc + 3) & -4;
620 #if USE_EBX
621 o(0x8b);
622 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
623 #endif
625 o(0xc9); /* leave */
626 if (func_ret_sub == 0) {
627 o(0xc3); /* ret */
628 } else {
629 o(0xc2); /* ret n */
630 g(func_ret_sub);
631 g(func_ret_sub >> 8);
633 saved_ind = ind;
634 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
635 #ifdef TCC_TARGET_PE
636 if (v >= 4096) {
637 oad(0xb8, v); /* mov stacksize, %eax */
638 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
639 } else
640 #endif
642 o(0xe58955); /* push %ebp, mov %esp, %ebp */
643 o(0xec81); /* sub esp, stacksize */
644 gen_le32(v);
645 #ifdef TCC_TARGET_PE
646 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
647 #endif
649 o(0x53 * USE_EBX); /* push ebx */
650 ind = saved_ind;
653 /* generate a jump to a label */
654 ST_FUNC int gjmp(int t)
656 return gjmp2(0xe9, t);
659 /* generate a jump to a fixed address */
660 ST_FUNC void gjmp_addr(int a)
662 int r;
663 r = a - ind - 2;
664 if (r == (char)r) {
665 g(0xeb);
666 g(r);
667 } else {
668 oad(0xe9, a - ind - 5);
672 #if 0
673 /* generate a jump to a fixed address */
674 ST_FUNC void gjmp_cond_addr(int a, int op)
676 int r = a - ind - 2;
677 if (r == (char)r)
678 g(op - 32), g(r);
679 else
680 g(0x0f), gjmp2(op - 16, r - 4);
682 #endif
684 ST_FUNC int gjmp_append(int n, int t)
686 void *p;
687 /* insert vtop->c jump list in t */
688 if (n) {
689 uint32_t n1 = n, n2;
690 while ((n2 = read32le(p = cur_text_section->data + n1)))
691 n1 = n2;
692 write32le(p, t);
693 t = n;
695 return t;
698 ST_FUNC int gjmp_cond(int op, int t)
700 g(0x0f);
701 t = gjmp2(op - 16, t);
702 return t;
705 ST_FUNC void gen_opi(int op)
707 int r, fr, opc, c;
709 switch(op) {
710 case '+':
711 case TOK_ADDC1: /* add with carry generation */
712 opc = 0;
713 gen_op8:
714 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
715 /* constant case */
716 vswap();
717 r = gv(RC_INT);
718 vswap();
719 c = vtop->c.i;
720 if (c == (char)c) {
721 /* generate inc and dec for smaller code */
722 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
723 opc = (c == 1) ^ (op == '+');
724 o (0x40 | (opc << 3) | r); // inc,dec
725 } else {
726 o(0x83);
727 o(0xc0 | (opc << 3) | r);
728 g(c);
730 } else {
731 o(0x81);
732 oad(0xc0 | (opc << 3) | r, c);
734 } else {
735 gv2(RC_INT, RC_INT);
736 r = vtop[-1].r;
737 fr = vtop[0].r;
738 o((opc << 3) | 0x01);
739 o(0xc0 + r + fr * 8);
741 vtop--;
742 if (op >= TOK_ULT && op <= TOK_GT)
743 vset_VT_CMP(op);
744 break;
745 case '-':
746 case TOK_SUBC1: /* sub with carry generation */
747 opc = 5;
748 goto gen_op8;
749 case TOK_ADDC2: /* add with carry use */
750 opc = 2;
751 goto gen_op8;
752 case TOK_SUBC2: /* sub with carry use */
753 opc = 3;
754 goto gen_op8;
755 case '&':
756 opc = 4;
757 goto gen_op8;
758 case '^':
759 opc = 6;
760 goto gen_op8;
761 case '|':
762 opc = 1;
763 goto gen_op8;
764 case '*':
765 gv2(RC_INT, RC_INT);
766 r = vtop[-1].r;
767 fr = vtop[0].r;
768 vtop--;
769 o(0xaf0f); /* imul fr, r */
770 o(0xc0 + fr + r * 8);
771 break;
772 case TOK_SHL:
773 opc = 4;
774 goto gen_shift;
775 case TOK_SHR:
776 opc = 5;
777 goto gen_shift;
778 case TOK_SAR:
779 opc = 7;
780 gen_shift:
781 opc = 0xc0 | (opc << 3);
782 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
783 /* constant case */
784 vswap();
785 r = gv(RC_INT);
786 vswap();
787 c = vtop->c.i & 0x1f;
788 o(0xc1); /* shl/shr/sar $xxx, r */
789 o(opc | r);
790 g(c);
791 } else {
792 /* we generate the shift in ecx */
793 gv2(RC_INT, RC_ECX);
794 r = vtop[-1].r;
795 o(0xd3); /* shl/shr/sar %cl, r */
796 o(opc | r);
798 vtop--;
799 break;
800 case '/':
801 case TOK_UDIV:
802 case TOK_PDIV:
803 case '%':
804 case TOK_UMOD:
805 case TOK_UMULL:
806 /* first operand must be in eax */
807 /* XXX: need better constraint for second operand */
808 gv2(RC_EAX, RC_ECX);
809 r = vtop[-1].r;
810 fr = vtop[0].r;
811 vtop--;
812 save_reg(TREG_EDX);
813 /* save EAX too if used otherwise */
814 save_reg_upstack(TREG_EAX, 1);
815 if (op == TOK_UMULL) {
816 o(0xf7); /* mul fr */
817 o(0xe0 + fr);
818 vtop->r2 = TREG_EDX;
819 r = TREG_EAX;
820 } else {
821 if (op == TOK_UDIV || op == TOK_UMOD) {
822 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
823 o(0xf0 + fr);
824 } else {
825 o(0xf799); /* cltd, idiv fr, %eax */
826 o(0xf8 + fr);
828 if (op == '%' || op == TOK_UMOD)
829 r = TREG_EDX;
830 else
831 r = TREG_EAX;
833 vtop->r = r;
834 break;
835 default:
836 opc = 7;
837 goto gen_op8;
841 /* generate a floating point operation 'v = t1 op t2' instruction. The
842 two operands are guaranteed to have the same floating point type */
843 /* XXX: need to use ST1 too */
844 ST_FUNC void gen_opf(int op)
846 int a, ft, fc, swapped, r;
848 if (op == TOK_NEG) { /* unary minus */
849 gv(RC_FLOAT);
850 o(0xe0d9); /* fchs */
851 return;
854 /* convert constants to memory references */
855 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
856 vswap();
857 gv(RC_FLOAT);
858 vswap();
860 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
861 gv(RC_FLOAT);
863 /* must put at least one value in the floating point register */
864 if ((vtop[-1].r & VT_LVAL) &&
865 (vtop[0].r & VT_LVAL)) {
866 vswap();
867 gv(RC_FLOAT);
868 vswap();
870 swapped = 0;
871 /* swap the stack if needed so that t1 is the register and t2 is
872 the memory reference */
873 if (vtop[-1].r & VT_LVAL) {
874 vswap();
875 swapped = 1;
877 if (op >= TOK_ULT && op <= TOK_GT) {
878 /* load on stack second operand */
879 load(TREG_ST0, vtop);
880 save_reg(TREG_EAX); /* eax is used by FP comparison code */
881 if (op == TOK_GE || op == TOK_GT)
882 swapped = !swapped;
883 else if (op == TOK_EQ || op == TOK_NE)
884 swapped = 0;
885 if (swapped)
886 o(0xc9d9); /* fxch %st(1) */
887 if (op == TOK_EQ || op == TOK_NE)
888 o(0xe9da); /* fucompp */
889 else
890 o(0xd9de); /* fcompp */
891 o(0xe0df); /* fnstsw %ax */
892 if (op == TOK_EQ) {
893 o(0x45e480); /* and $0x45, %ah */
894 o(0x40fC80); /* cmp $0x40, %ah */
895 } else if (op == TOK_NE) {
896 o(0x45e480); /* and $0x45, %ah */
897 o(0x40f480); /* xor $0x40, %ah */
898 op = TOK_NE;
899 } else if (op == TOK_GE || op == TOK_LE) {
900 o(0x05c4f6); /* test $0x05, %ah */
901 op = TOK_EQ;
902 } else {
903 o(0x45c4f6); /* test $0x45, %ah */
904 op = TOK_EQ;
906 vtop--;
907 vset_VT_CMP(op);
908 } else {
909 /* no memory reference possible for long double operations */
910 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
911 load(TREG_ST0, vtop);
912 swapped = !swapped;
915 switch(op) {
916 default:
917 case '+':
918 a = 0;
919 break;
920 case '-':
921 a = 4;
922 if (swapped)
923 a++;
924 break;
925 case '*':
926 a = 1;
927 break;
928 case '/':
929 a = 6;
930 if (swapped)
931 a++;
932 break;
934 ft = vtop->type.t;
935 fc = vtop->c.i;
936 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
937 o(0xde); /* fxxxp %st, %st(1) */
938 o(0xc1 + (a << 3));
939 } else {
940 /* if saved lvalue, then we must reload it */
941 r = vtop->r;
942 if ((r & VT_VALMASK) == VT_LLOCAL) {
943 SValue v1;
944 r = get_reg(RC_INT);
945 v1.type.t = VT_INT;
946 v1.r = VT_LOCAL | VT_LVAL;
947 v1.c.i = fc;
948 v1.sym = NULL;
949 load(r, &v1);
950 fc = 0;
953 if ((ft & VT_BTYPE) == VT_DOUBLE)
954 o(0xdc);
955 else
956 o(0xd8);
957 gen_modrm(a, r, vtop->sym, fc);
959 vtop--;
963 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
964 and 'long long' cases. */
965 ST_FUNC void gen_cvt_itof(int t)
967 save_reg(TREG_ST0);
968 gv(RC_INT);
969 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
970 /* signed long long to float/double/long double (unsigned case
971 is handled generically) */
972 o(0x50 + vtop->r2); /* push r2 */
973 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
974 o(0x242cdf); /* fildll (%esp) */
975 o(0x08c483); /* add $8, %esp */
976 vtop->r2 = VT_CONST;
977 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
978 (VT_INT | VT_UNSIGNED)) {
979 /* unsigned int to float/double/long double */
980 o(0x6a); /* push $0 */
981 g(0x00);
982 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
983 o(0x242cdf); /* fildll (%esp) */
984 o(0x08c483); /* add $8, %esp */
985 } else {
986 /* int to float/double/long double */
987 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
988 o(0x2404db); /* fildl (%esp) */
989 o(0x04c483); /* add $4, %esp */
991 vtop->r2 = VT_CONST;
992 vtop->r = TREG_ST0;
995 /* convert fp to int 't' type */
996 ST_FUNC void gen_cvt_ftoi(int t)
998 int bt = vtop->type.t & VT_BTYPE;
999 if (bt == VT_FLOAT)
1000 vpush_helper_func(TOK___fixsfdi);
1001 else if (bt == VT_LDOUBLE)
1002 vpush_helper_func(TOK___fixxfdi);
1003 else
1004 vpush_helper_func(TOK___fixdfdi);
1005 vswap();
1006 gfunc_call(1);
1007 vpushi(0);
1008 vtop->r = REG_IRET;
1009 if ((t & VT_BTYPE) == VT_LLONG)
1010 vtop->r2 = REG_IRE2;
1013 /* convert from one floating point type to another */
1014 ST_FUNC void gen_cvt_ftof(int t)
1016 /* all we have to do on i386 is to put the float in a register */
1017 gv(RC_FLOAT);
1020 /* char/short to int conversion */
1021 ST_FUNC void gen_cvt_csti(int t)
1023 int r, sz, xl;
1024 r = gv(RC_INT);
1025 sz = !(t & VT_UNSIGNED);
1026 xl = (t & VT_BTYPE) == VT_SHORT;
1027 o(0xc0b60f /* mov[sz] %a[xl], %eax */
1028 | (sz << 3 | xl) << 8
1029 | (r << 3 | r) << 16
1033 /* increment tcov counter */
1034 ST_FUNC void gen_increment_tcov (SValue *sv)
1036 o(0x0583); /* addl $1, xxx */
1037 greloc(cur_text_section, sv->sym, ind, R_386_32);
1038 gen_le32(0);
1039 o(1);
1040 o(0x1583); /* addcl $0, xxx */
1041 greloc(cur_text_section, sv->sym, ind, R_386_32);
1042 gen_le32(4);
1043 g(0);
1046 /* computed goto support */
1047 ST_FUNC void ggoto(void)
1049 gcall_or_jmp(1);
1050 vtop--;
1053 /* bound check support functions */
1054 #ifdef CONFIG_TCC_BCHECK
1056 static void gen_bounds_prolog(void)
1058 /* leave some room for bound checking code */
1059 func_bound_offset = lbounds_section->data_offset;
1060 func_bound_ind = ind;
1061 func_bound_add_epilog = 0;
1062 oad(0xb8, 0); /* lbound section pointer */
1063 oad(0xb8, 0); /* call to function */
1066 static void gen_bounds_epilog(void)
1068 addr_t saved_ind;
1069 addr_t *bounds_ptr;
1070 Sym *sym_data;
1071 int offset_modified = func_bound_offset != lbounds_section->data_offset;
1073 if (!offset_modified && !func_bound_add_epilog)
1074 return;
1076 /* add end of table info */
1077 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
1078 *bounds_ptr = 0;
1080 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
1081 func_bound_offset, PTR_SIZE);
1083 /* generate bound local allocation */
1084 if (offset_modified) {
1085 saved_ind = ind;
1086 ind = func_bound_ind;
1087 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1088 ind = ind + 5;
1089 gen_static_call(TOK___bound_local_new);
1090 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_helper_func(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 /*************************************************************/