tcc on i386 are still having problems at work.Thank Roy report again. Struck on sever...
[tinycc.git] / i386-gen.c
blobcfa9cc6fe5501cbac8ba0c708e54a8e3037f821b
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 8
25 #define NB_ASM_REGS 8
27 /* a register can belong to several classes. The classes must be
28 sorted from more general to more precise (see gv2() code which does
29 assumptions on it). */
30 #define RC_INT 0x0001 /* generic integer register */
31 #define RC_FLOAT 0x0002 /* generic float register */
32 #define RC_EAX 0x0004
33 #define RC_ST0 0x0008
34 #define RC_ECX 0x0010
35 #define RC_EDX 0x0020
36 #define RC_EBX 0x0040
37 #define RC_ESI 0x0080
38 #define RC_EDI 0x0100
39 #define RC_INT2 0x0200
40 #define RC_IRET RC_EAX /* function return: integer register */
41 #define RC_LRET RC_EDX /* function return: second integer register */
42 #define RC_FRET RC_ST0 /* function return: float register */
43 #define RC_MASK (RC_INT|RC_INT2|RC_FLOAT)
44 /* pretty names for the registers */
45 enum {
46 TREG_EAX = 0,
47 TREG_ECX,
48 TREG_EDX,
49 TREG_EBX,
50 TREG_ESP,
51 TREG_ST0,
52 TREG_ESI,
53 TREG_EDI,
56 /* return registers for function */
57 #define REG_IRET TREG_EAX /* single word int return register */
58 #define REG_LRET TREG_EDX /* second word return register (for long long) */
59 #define REG_FRET TREG_ST0 /* float return register */
61 /* defined if function parameters must be evaluated in reverse order */
62 #define INVERT_FUNC_PARAMS
64 /* defined if structures are passed as pointers. Otherwise structures
65 are directly pushed on stack. */
66 /* #define FUNC_STRUCT_PARAM_AS_PTR */
68 /* pointer size, in bytes */
69 #define PTR_SIZE 4
71 /* long double size and alignment, in bytes */
72 #define LDOUBLE_SIZE 12
73 #define LDOUBLE_ALIGN 4
74 /* maximum alignment (for aligned attribute support) */
75 #define MAX_ALIGN 8
78 #define psym oad
80 /******************************************************/
81 /* ELF defines */
83 #define EM_TCC_TARGET EM_386
85 /* relocation type for 32 bit data relocation */
86 #define R_DATA_32 R_386_32
87 #define R_DATA_PTR R_386_32
88 #define R_JMP_SLOT R_386_JMP_SLOT
89 #define R_COPY R_386_COPY
91 #define ELF_START_ADDR 0x08048000
92 #define ELF_PAGE_SIZE 0x1000
94 /******************************************************/
95 #else /* ! TARGET_DEFS_ONLY */
96 /******************************************************/
97 #include "tcc.h"
99 ST_DATA const int reg_classes[NB_REGS] = {
100 /* eax */ RC_INT | RC_EAX | RC_INT2,
101 /* ecx */ RC_INT | RC_ECX | RC_INT2,
102 /* edx */ RC_INT | RC_EDX,
103 RC_INT|RC_INT2|RC_EBX,
105 /* st0 */ RC_FLOAT | RC_ST0,
106 RC_ESI|RC_INT2,
107 RC_EDI|RC_INT2,
110 static unsigned long func_sub_sp_offset;
111 static int func_ret_sub;
112 #ifdef CONFIG_TCC_BCHECK
113 static unsigned long func_bound_offset;
114 #endif
116 /* XXX: make it faster ? */
117 ST_FUNC void g(int c)
119 int ind1;
120 ind1 = ind + 1;
121 if (ind1 > cur_text_section->data_allocated)
122 section_realloc(cur_text_section, ind1);
123 cur_text_section->data[ind] = c;
124 ind = ind1;
127 ST_FUNC void o(unsigned int c)
129 while (c) {
130 g(c);
131 c = c >> 8;
135 ST_FUNC void gen_le16(int v)
137 g(v);
138 g(v >> 8);
141 ST_FUNC void gen_le32(int c)
143 g(c);
144 g(c >> 8);
145 g(c >> 16);
146 g(c >> 24);
149 /* output a symbol and patch all calls to it */
150 ST_FUNC void gsym_addr(int t, int a)
152 int n, *ptr;
153 while (t) {
154 ptr = (int *)(cur_text_section->data + t);
155 n = *ptr; /* next value */
156 *ptr = a - t - 4;
157 t = n;
161 ST_FUNC void gsym(int t)
163 gsym_addr(t, ind);
166 /* psym is used to put an instruction with a data field which is a
167 reference to a symbol. It is in fact the same as oad ! */
168 #define psym oad
170 /* instruction + 4 bytes data. Return the address of the data */
171 ST_FUNC int oad(int c, int s)
173 int ind1;
175 o(c);
176 ind1 = ind + 4;
177 if (ind1 > cur_text_section->data_allocated)
178 section_realloc(cur_text_section, ind1);
179 *(int *)(cur_text_section->data + ind) = s;
180 s = ind;
181 ind = ind1;
182 return s;
185 /* output constant with relocation if 'r & VT_SYM' is true */
186 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
188 if (r & VT_SYM)
189 greloc(cur_text_section, sym, ind, R_386_32);
190 gen_le32(c);
193 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
195 if (r & VT_SYM)
196 greloc(cur_text_section, sym, ind, R_386_PC32);
197 gen_le32(c - 4);
200 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
201 opcode bits */
202 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
204 op_reg = op_reg << 3;
205 if ((r & VT_VALMASK) == VT_CONST) {
206 /* constant memory reference */
207 o(0x05 | op_reg);
208 gen_addr32(r, sym, c);
209 } else if ((r & VT_VALMASK) == VT_LOCAL) {
210 /* currently, we use only ebp as base */
211 if (c == (char)c) {
212 /* short reference */
213 o(0x45 | op_reg);
214 g(c);
215 } else {
216 oad(0x85 | op_reg, c);
218 } else {
219 g(0x00 | op_reg | (r & VT_VALMASK));
223 /* load 'r' from value 'sv' */
224 ST_FUNC void load(int r, SValue *sv)
226 int v, t, ft, fc, fr;
227 SValue v1;
229 #ifdef TCC_TARGET_PE
230 SValue v2;
231 sv = pe_getimport(sv, &v2);
232 #endif
234 fr = sv->r;
235 ft = sv->type.t;
236 fc = sv->c.ul;
238 v = fr & VT_VALMASK;
239 if (fr & VT_LVAL) {
240 if(fr & VT_TMP){
241 int size, align;
242 if((ft & VT_BTYPE) == VT_FUNC)
243 size = PTR_SIZE;
244 else
245 size = type_size(&sv->type, &align);
246 loc_stack(size, 0);
248 if (v == VT_LLOCAL) {
249 v1.type.t = VT_INT;
250 v1.r = VT_LOCAL | VT_LVAL;
251 v1.c.ul = fc;
252 fr = r;
253 if (!(reg_classes[fr] & RC_INT))
254 fr = get_reg(RC_INT);
255 load(fr, &v1);
256 fc = 0;
258 if ((ft & VT_BTYPE) == VT_FLOAT) {
259 o(0xd9); /* flds */
260 r = 0;
261 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
262 o(0xdd); /* fldl */
263 r = 0;
264 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
265 o(0xdb); /* fldt */
266 r = 5;
267 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
268 o(0xbe0f); /* movsbl */
269 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
270 o(0xb60f); /* movzbl */
271 } else if ((ft & VT_TYPE) == VT_SHORT) {
272 o(0xbf0f); /* movswl */
273 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
274 o(0xb70f); /* movzwl */
275 } else {
276 o(0x8b); /* movl */
278 gen_modrm(r, fr, sv->sym, fc);
279 } else {
280 if (v == VT_CONST) {
281 o(0xb8 + r); /* mov $xx, r */
282 gen_addr32(fr, sv->sym, fc);
283 } else if (v == VT_LOCAL) {
284 if (fc) {
285 o(0x8d); /* lea xxx(%ebp), r */
286 gen_modrm(r, VT_LOCAL, sv->sym, fc);
287 } else {
288 o(0x89);
289 o(0xe8 + r); /* mov %ebp, r */
291 } else if (v == VT_CMP) {
292 oad(0xb8 + r, 0); /* mov $0, r */
293 o(0x0f); /* setxx %br */
294 o(fc);
295 o(0xc0 + r);
296 } else if (v == VT_JMP || v == VT_JMPI) {
297 t = v & 1;
298 oad(0xb8 + r, t); /* mov $1, r */
299 o(0x05eb); /* jmp after */
300 gsym(fc);
301 oad(0xb8 + r, t ^ 1); /* mov $0, r */
302 } else if (v != r) {
303 o(0x89);
304 o(0xc0 + r + v * 8); /* mov v, r */
309 /* store register 'r' in lvalue 'v' */
310 ST_FUNC void store(int r, SValue *v)
312 int fr, bt, ft, fc;
314 #ifdef TCC_TARGET_PE
315 SValue v2;
316 v = pe_getimport(v, &v2);
317 #endif
319 ft = v->type.t;
320 fc = v->c.ul;
321 fr = v->r & VT_VALMASK;
322 bt = ft & VT_BTYPE;
323 /* XXX: incorrect if float reg to reg */
324 if (bt == VT_FLOAT) {
325 o(0xd9); /* fsts */
326 r = 2;
327 } else if (bt == VT_DOUBLE) {
328 o(0xdd); /* fstpl */
329 r = 2;
330 } else if (bt == VT_LDOUBLE) {
331 o(0xc0d9); /* fld %st(0) */
332 o(0xdb); /* fstpt */
333 r = 7;
334 } else {
335 if (bt == VT_SHORT)
336 o(0x66);
337 if (bt == VT_BYTE || bt == VT_BOOL)
338 o(0x88);
339 else
340 o(0x89);
342 if (fr == VT_CONST ||
343 fr == VT_LOCAL ||
344 (v->r & VT_LVAL)) {
345 gen_modrm(r, v->r, v->sym, fc);
346 } else if (fr != r) {
347 o(0xc0 + fr + r * 8); /* mov r, fr */
351 static void gadd_sp(int val)
353 if (val == (char)val) {
354 o(0xc483);
355 g(val);
356 } else {
357 oad(0xc481, val); /* add $xxx, %esp */
361 static void gen_static_call(int v)
363 Sym *sym;
365 sym = external_global_sym(v, &func_old_type, 0);
366 oad(0xe8, -4);
367 greloc(cur_text_section, sym, ind-4, R_386_PC32);
370 /* 'is_jmp' is '1' if it is a jump */
371 static void gcall_or_jmp(int is_jmp)
373 int r;
374 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
375 /* constant case */
376 if (vtop->r & VT_SYM) {
377 /* relocation case */
378 greloc(cur_text_section, vtop->sym,
379 ind + 1, R_386_PC32);
380 } else {
381 /* put an empty PC32 relocation */
382 put_elf_reloc(symtab_section, cur_text_section,
383 ind + 1, R_386_PC32, 0);
385 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
386 } else {
387 /* otherwise, indirect call */
388 r = gv(RC_INT);
389 o(0xff); /* call/jmp *r */
390 o(0xd0 + r + (is_jmp << 4));
394 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
395 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
397 /* Return the number of registers needed to return the struct, or 0 if
398 returning via struct pointer. */
399 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align)
401 #ifdef TCC_TARGET_PE
402 int size, align;
404 *ret_align = 1; // Never have to re-align return values for x86
405 size = type_size(vt, &align);
406 if (size > 8) {
407 return 0;
408 } else if (size > 4) {
409 ret->ref = NULL;
410 ret->t = VT_LLONG;
411 return 1;
412 } else {
413 ret->ref = NULL;
414 ret->t = VT_INT;
415 return 1;
417 #else
418 *ret_align = 1; // Never have to re-align return values for x86
419 return 0;
420 #endif
423 /* Generate function call. The function address is pushed first, then
424 all the parameters in call order. This functions pops all the
425 parameters and the function address. */
426 ST_FUNC void gfunc_call(int nb_args)
428 int size, align, r, args_size, i, func_call;
429 Sym *func_sym;
431 args_size = 0;
432 for(i = 0;i < nb_args; i++) {
433 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
434 size = type_size(&vtop->type, &align);
435 /* align to stack align size */
436 size = (size + 3) & ~3;
437 /* allocate the necessary size on stack */
438 oad(0xec81, size); /* sub $xxx, %esp */
439 /* generate structure store */
440 r = get_reg(RC_INT);
441 o(0x89); /* mov %esp, r */
442 o(0xe0 + r);
443 vset(&vtop->type, r | VT_LVAL, 0);
444 vswap();
445 vstore();
446 args_size += size;
447 } else if (is_float(vtop->type.t)) {
448 gv(RC_FLOAT); /* only one float register */
449 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
450 size = 4;
451 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
452 size = 8;
453 else
454 size = 12;
455 oad(0xec81, size); /* sub $xxx, %esp */
456 if (size == 12)
457 o(0x7cdb);
458 else
459 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
460 g(0x24);
461 g(0x00);
462 args_size += size;
463 } else {
464 /* simple type (currently always same size) */
465 /* XXX: implicit cast ? */
466 r = gv(RC_INT);
467 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
468 size = 8;
469 o(0x50 + vtop->r2); /* push r */
470 } else {
471 size = 4;
473 o(0x50 + r); /* push r */
474 args_size += size;
476 vtop--;
478 save_regs(0); /* save used temporary registers */
479 func_sym = vtop->type.ref;
480 func_call = func_sym->a.func_call;
481 /* fast call case */
482 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
483 func_call == FUNC_FASTCALLW) {
484 int fastcall_nb_regs;
485 uint8_t *fastcall_regs_ptr;
486 if (func_call == FUNC_FASTCALLW) {
487 fastcall_regs_ptr = fastcallw_regs;
488 fastcall_nb_regs = 2;
489 } else {
490 fastcall_regs_ptr = fastcall_regs;
491 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
493 for(i = 0;i < fastcall_nb_regs; i++) {
494 if (args_size <= 0)
495 break;
496 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
497 /* XXX: incorrect for struct/floats */
498 args_size -= 4;
501 #ifndef TCC_TARGET_PE
502 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
503 args_size -= 4;
504 #endif
505 gcall_or_jmp(0);
507 if (args_size && func_call != FUNC_STDCALL)
508 gadd_sp(args_size);
509 vtop--;
512 #ifdef TCC_TARGET_PE
513 #define FUNC_PROLOG_SIZE 10
514 #else
515 #define FUNC_PROLOG_SIZE 9
516 #endif
518 /* generate function prolog of type 't' */
519 ST_FUNC void gfunc_prolog(CType *func_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->a.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 func_vt = sym->type;
550 func_var = (sym->c == FUNC_ELLIPSIS);
551 #ifdef TCC_TARGET_PE
552 size = type_size(&func_vt,&align);
553 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
554 #else
555 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
556 #endif
557 /* XXX: fastcall case ? */
558 func_vc = addr;
559 addr += 4;
560 param_index++;
562 /* define parameters */
563 while ((sym = sym->next) != NULL) {
564 type = &sym->type;
565 size = type_size(type, &align);
566 size = (size + 3) & ~3;
567 #ifdef FUNC_STRUCT_PARAM_AS_PTR
568 /* structs are passed as pointer */
569 if ((type->t & VT_BTYPE) == VT_STRUCT) {
570 size = 4;
572 #endif
573 if (param_index < fastcall_nb_regs) {
574 /* save FASTCALL register */
575 loc -= 4;
576 o(0x89); /* movl */
577 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
578 param_addr = loc;
579 } else {
580 param_addr = addr;
581 addr += size;
583 sym_push(sym->v & ~SYM_FIELD, type,
584 VT_LOCAL | lvalue_type(type->t), param_addr);
585 param_index++;
587 func_ret_sub = 0;
588 /* pascal type call ? */
589 if (func_call == FUNC_STDCALL)
590 func_ret_sub = addr - 8;
591 #ifndef TCC_TARGET_PE
592 else if (func_vc)
593 func_ret_sub = 4;
594 #endif
596 #ifdef CONFIG_TCC_BCHECK
597 /* leave some room for bound checking code */
598 if (tcc_state->do_bounds_check) {
599 oad(0xb8, 0); /* lbound section pointer */
600 oad(0xb8, 0); /* call to function */
601 func_bound_offset = lbounds_section->data_offset;
603 #endif
606 /* generate function epilog */
607 ST_FUNC void gfunc_epilog(void)
609 int v, saved_ind;
611 #ifdef CONFIG_TCC_BCHECK
612 if (tcc_state->do_bounds_check
613 && func_bound_offset != lbounds_section->data_offset) {
614 int saved_ind;
615 int *bounds_ptr;
616 Sym *sym_data;
617 /* add end of table info */
618 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
619 *bounds_ptr = 0;
620 /* generate bound local allocation */
621 saved_ind = ind;
622 ind = func_sub_sp_offset;
623 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
624 func_bound_offset, lbounds_section->data_offset);
625 greloc(cur_text_section, sym_data,
626 ind + 1, R_386_32);
627 oad(0xb8, 0); /* mov %eax, xxx */
628 gen_static_call(TOK___bound_local_new);
630 ind = saved_ind;
631 /* generate bound check local freeing */
632 o(0x5250); /* save returned value, if any */
633 greloc(cur_text_section, sym_data,
634 ind + 1, R_386_32);
635 oad(0xb8, 0); /* mov %eax, xxx */
636 gen_static_call(TOK___bound_local_delete);
638 o(0x585a); /* restore returned value, if any */
640 #endif
641 o(0xc9); /* leave */
642 if (func_ret_sub == 0) {
643 o(0xc3); /* ret */
644 } else {
645 o(0xc2); /* ret n */
646 g(func_ret_sub);
647 g(func_ret_sub >> 8);
649 /* align local size to word & save local variables */
651 v = (-loc + 3) & -4;
652 saved_ind = ind;
653 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
654 #ifdef TCC_TARGET_PE
655 if (v >= 4096) {
656 oad(0xb8, v); /* mov stacksize, %eax */
657 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
658 } else
659 #endif
661 o(0xe58955); /* push %ebp, mov %esp, %ebp */
662 o(0xec81); /* sub esp, stacksize */
663 gen_le32(v);
664 #if FUNC_PROLOG_SIZE == 10
665 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
666 #endif
668 ind = saved_ind;
671 /* generate a jump to a label */
672 ST_FUNC int gjmp(int t)
674 return psym(0xe9, t);
677 /* generate a jump to a fixed address */
678 ST_FUNC void gjmp_addr(int a)
680 int r;
681 r = a - ind - 2;
682 if (r == (char)r) {
683 g(0xeb);
684 g(r);
685 } else {
686 oad(0xe9, a - ind - 5);
690 /* generate a test. set 'inv' to invert test. Stack entry is popped */
691 ST_FUNC int gtst(int inv, int t)
693 int v, *p;
695 v = vtop->r & VT_VALMASK;
696 if (v == VT_CMP) {
697 /* fast case : can jump directly since flags are set */
698 g(0x0f);
699 t = psym((vtop->c.i - 16) ^ inv, t);
700 } else if (v == VT_JMP || v == VT_JMPI) {
701 /* && or || optimization */
702 if ((v & 1) == inv) {
703 /* insert vtop->c jump list in t */
704 p = &vtop->c.i;
705 while (*p != 0)
706 p = (int *)(cur_text_section->data + *p);
707 *p = t;
708 t = vtop->c.i;
709 } else {
710 t = gjmp(t);
711 gsym(vtop->c.i);
713 } else {
714 if (is_float(vtop->type.t) ||
715 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
716 vpushi(0);
717 gen_op(TOK_NE);
719 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
720 /* constant jmp optimization */
721 if ((vtop->c.i != 0) != inv)
722 t = gjmp(t);
723 } else {
724 v = gv(RC_INT);
725 o(0x85);
726 o(0xc0 + v * 9);
727 g(0x0f);
728 t = psym(0x85 ^ inv, t);
731 vtop--;
732 return t;
735 /* generate an integer binary operation */
736 ST_FUNC void gen_opi(int op)
738 int r, fr, opc, fc, c;
739 int cc, uu, tt2;
741 fr = vtop[0].r;
742 fc = vtop->c.ul;
743 cc = (fr & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
744 tt2 = (fr & (VT_LVAL | VT_LVAL_TYPE)) == VT_LVAL;
746 switch(op) {
747 case '+':
748 case TOK_ADDC1: /* add with carry generation */
749 opc = 0;
750 gen_op8:
751 vswap();
752 r = gv(RC_INT);
753 vswap();
754 if (cc) {
755 /* constant case */
756 c = vtop->c.i;
757 if (c == (char)c) {
758 /* generate inc and dec for smaller code */
759 if (c == 1 && opc == 0) {
760 o (0x40 | r); // inc
761 } else if (c == 1 && opc == 5) {
762 o (0x48 | r); // dec
763 } else {
764 o(0x83);
765 o(0xc0 + r + opc*8);
766 g(c);
768 } else {
769 o(0x81);
770 oad(0xc0 + r+ opc*8, c);
772 } else {
773 if(!tt2)
774 fr = gv(RC_INT);
775 o(0x03 + opc*8);
776 if(fr >= VT_CONST)
777 gen_modrm(r, fr, vtop->sym, fc);
778 else
779 o(0xc0 + fr + r*8);
781 vtop--;
782 if (op >= TOK_ULT && op <= TOK_GT) {
783 vtop->r = VT_CMP;
784 vtop->c.i = op;
786 break;
787 case '-':
788 case TOK_SUBC1: /* sub with carry generation */
789 opc = 5;
790 goto gen_op8;
791 case TOK_ADDC2: /* add with carry use */
792 opc = 2;
793 goto gen_op8;
794 case TOK_SUBC2: /* sub with carry use */
795 opc = 3;
796 goto gen_op8;
797 case '&':
798 opc = 4;
799 goto gen_op8;
800 case '^':
801 opc = 6;
802 goto gen_op8;
803 case '|':
804 opc = 1;
805 goto gen_op8;
806 case '*':
807 opc = 5;
808 vswap();
809 r = gv(RC_INT);
810 vswap();
811 if(!tt2)
812 fr = gv(RC_INT);
813 if(r == TREG_EAX){
814 if(fr != TREG_EDX)
815 save_reg(TREG_EDX);
816 o(0xf7);
817 if(fr >= VT_CONST)
818 gen_modrm(opc, fr, vtop->sym, fc);
819 else
820 o(0xc0 + fr + opc*8);
821 }else{
822 o(0xaf0f); /* imul fr, r */
823 if(fr >= VT_CONST)
824 gen_modrm(r, fr, vtop->sym, fc);
825 else
826 o(0xc0 + fr + r*8);
828 vtop--;
829 break;
830 case TOK_SHL:
831 opc = 4;
832 goto gen_shift;
833 case TOK_SHR:
834 opc = 5;
835 goto gen_shift;
836 case TOK_SAR:
837 opc = 7;
838 gen_shift:
839 if (cc) {
840 /* constant case */
841 vswap();
842 r = gv(RC_INT);
843 vswap();
844 c = vtop->c.i;
845 if(c == 1){
846 o(0xd1);
847 o(0xc0 + r + opc*8);
848 }else{
849 o(0xc1); /* shl/shr/sar $xxx, r */
850 o(0xc0 + r + opc*8);
851 g(c & 0x1f);
853 } else {
854 /* we generate the shift in ecx */
855 gv2(RC_INT, RC_ECX);
856 r = vtop[-1].r;
857 o(0xd3); /* shl/shr/sar %cl, r */
858 o(0xc0 + r + opc*8);
860 vtop--;
861 break;
862 case TOK_UMOD:
863 opc = 4;
864 uu = 1;
865 goto divmod;
866 case TOK_UDIV:
867 case TOK_UMULL:
868 opc = 6;
869 uu = 1;
870 goto divmod;
871 case '/':
872 case '%':
873 case TOK_PDIV:
874 opc = 7;
875 uu = 0;
876 divmod:
877 /* first operand must be in eax */
878 /* XXX: need better constraint for second operand */
879 if(!tt2){
880 gv2(RC_EAX, RC_INT2);
881 fr = vtop[0].r;
882 }else{
883 vswap();
884 gv(RC_EAX);
885 vswap();
887 save_reg(TREG_EDX);
888 if (op == TOK_UMULL) {
889 o(0xf7); /* mul fr */
890 vtop->r2 = TREG_EDX;
891 }else{
892 o(uu ? 0xd231 : 0x99); /* xor %edx,%edx : cdq RDX:RAX <- sign-extend of RAX. */
893 o(0xf7); /* div fr, %eax */
895 if(fr >= VT_CONST)
896 gen_modrm(opc, fr, vtop->sym, fc);
897 else
898 o(0xc0 + fr + opc*8);
899 if (op == '%' || op == TOK_UMOD)
900 r = TREG_EDX;
901 else
902 r = TREG_EAX;
903 vtop--;
904 vtop->r = r;
905 break;
906 default:
907 opc = 7;
908 goto gen_op8;
912 /* generate a floating point operation 'v = t1 op t2' instruction. The
913 two operands are guaranted to have the same floating point type */
914 /* XXX: need to use ST1 too */
915 ST_FUNC void gen_opf(int op)
917 int a, ft, fc, swapped, r;
919 /* convert constants to memory references */
920 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
921 vswap();
922 gv(RC_FLOAT);
923 vswap();
925 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
926 gv(RC_FLOAT);
928 /* must put at least one value in the floating point register */
929 if ((vtop[-1].r & VT_LVAL) &&
930 (vtop[0].r & VT_LVAL)) {
931 vswap();
932 gv(RC_FLOAT);
933 vswap();
935 swapped = 0;
936 /* swap the stack if needed so that t1 is the register and t2 is
937 the memory reference */
938 if (vtop[-1].r & VT_LVAL) {
939 vswap();
940 swapped = 1;
942 if (op >= TOK_ULT && op <= TOK_GT) {
943 /* load on stack second operand */
944 load(TREG_ST0, vtop);
945 save_reg(TREG_EAX); /* eax is used by FP comparison code */
946 if (op == TOK_GE || op == TOK_GT)
947 swapped = !swapped;
948 else if (op == TOK_EQ || op == TOK_NE)
949 swapped = 0;
950 if (swapped)
951 o(0xc9d9); /* fxch %st(1) */
952 if (op == TOK_EQ || op == TOK_NE)
953 o(0xe9da); /* fucompp */
954 else
955 o(0xd9de); /* fcompp */
956 o(0xe0df); /* fnstsw %ax */
957 if (op == TOK_EQ) {
958 o(0x45e480); /* and $0x45, %ah */
959 o(0x40fC80); /* cmp $0x40, %ah */
960 } else if (op == TOK_NE) {
961 o(0x45e480); /* and $0x45, %ah */
962 o(0x40f480); /* xor $0x40, %ah */
963 op = TOK_NE;
964 } else if (op == TOK_GE || op == TOK_LE) {
965 o(0x05c4f6); /* test $0x05, %ah */
966 op = TOK_EQ;
967 } else {
968 o(0x45c4f6); /* test $0x45, %ah */
969 op = TOK_EQ;
971 vtop--;
972 vtop->r = VT_CMP;
973 vtop->c.i = op;
974 } else {
975 /* no memory reference possible for long double operations */
976 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
977 load(TREG_ST0, vtop);
978 swapped = !swapped;
981 switch(op) {
982 default:
983 case '+':
984 a = 0;
985 break;
986 case '-':
987 a = 4;
988 if (swapped)
989 a++;
990 break;
991 case '*':
992 a = 1;
993 break;
994 case '/':
995 a = 6;
996 if (swapped)
997 a++;
998 break;
1000 ft = vtop->type.t;
1001 fc = vtop->c.ul;
1002 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
1003 o(0xde); /* fxxxp %st, %st(1) */
1004 o(0xc1 + (a << 3));
1005 } else {
1006 /* if saved lvalue, then we must reload it */
1007 r = vtop->r;
1008 if ((r & VT_VALMASK) == VT_LLOCAL) {
1009 SValue v1;
1010 r = get_reg(RC_INT);
1011 v1.type.t = VT_INT;
1012 v1.r = VT_LOCAL | VT_LVAL;
1013 v1.c.ul = fc;
1014 load(r, &v1);
1015 fc = 0;
1018 if ((ft & VT_BTYPE) == VT_DOUBLE)
1019 o(0xdc);
1020 else
1021 o(0xd8);
1022 gen_modrm(a, r, vtop->sym, fc);
1024 vtop--;
1028 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1029 and 'long long' cases. */
1030 ST_FUNC void gen_cvt_itof(int t)
1032 save_reg(TREG_ST0);
1033 gv(RC_INT);
1034 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1035 /* signed long long to float/double/long double (unsigned case
1036 is handled generically) */
1037 o(0x50 + vtop->r2); /* push r2 */
1038 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1039 o(0x242cdf); /* fildll (%esp) */
1040 o(0x08c483); /* add $8, %esp */
1041 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1042 (VT_INT | VT_UNSIGNED)) {
1043 /* unsigned int to float/double/long double */
1044 o(0x6a); /* push $0 */
1045 g(0x00);
1046 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1047 o(0x242cdf); /* fildll (%esp) */
1048 o(0x08c483); /* add $8, %esp */
1049 } else {
1050 /* int to float/double/long double */
1051 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1052 o(0x2404db); /* fildl (%esp) */
1053 o(0x04c483); /* add $4, %esp */
1055 vtop->r = TREG_ST0;
1058 /* convert fp to int 't' type */
1059 ST_FUNC void gen_cvt_ftoi(int t)
1061 int bt = vtop->type.t & VT_BTYPE;
1062 if (bt == VT_FLOAT)
1063 vpush_global_sym(&func_old_type, TOK___fixsfdi);
1064 else if (bt == VT_LDOUBLE)
1065 vpush_global_sym(&func_old_type, TOK___fixxfdi);
1066 else
1067 vpush_global_sym(&func_old_type, TOK___fixdfdi);
1068 vswap();
1069 gfunc_call(1);
1070 vpushi(0);
1071 vtop->r = REG_IRET;
1072 vtop->r2 = REG_LRET;
1075 /* convert from one floating point type to another */
1076 ST_FUNC void gen_cvt_ftof(int t)
1078 /* all we have to do on i386 is to put the float in a register */
1079 gv(RC_FLOAT);
1082 /* computed goto support */
1083 ST_FUNC void ggoto(void)
1085 gcall_or_jmp(1);
1086 vtop--;
1089 /* bound check support functions */
1090 #ifdef CONFIG_TCC_BCHECK
1092 /* generate a bounded pointer addition */
1093 ST_FUNC void gen_bounded_ptr_add(void)
1095 /* prepare fast i386 function call (args in eax and edx) */
1096 gv2(RC_EAX, RC_EDX);
1097 /* save all temporary registers */
1098 vtop -= 2;
1099 save_regs(0);
1100 /* do a fast function call */
1101 gen_static_call(TOK___bound_ptr_add);
1102 /* returned pointer is in eax */
1103 vtop++;
1104 vtop->r = TREG_EAX | VT_BOUNDED;
1105 /* address of bounding function call point */
1106 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1109 /* patch pointer addition in vtop so that pointer dereferencing is
1110 also tested */
1111 ST_FUNC void gen_bounded_ptr_deref(void)
1113 int func;
1114 int size, align;
1115 Elf32_Rel *rel;
1116 Sym *sym;
1118 size = 0;
1119 /* XXX: put that code in generic part of tcc */
1120 if (!is_float(vtop->type.t)) {
1121 if (vtop->r & VT_LVAL_BYTE)
1122 size = 1;
1123 else if (vtop->r & VT_LVAL_SHORT)
1124 size = 2;
1126 if (!size)
1127 size = type_size(&vtop->type, &align);
1128 switch(size) {
1129 case 1: func = TOK___bound_ptr_indir1; break;
1130 case 2: func = TOK___bound_ptr_indir2; break;
1131 case 4: func = TOK___bound_ptr_indir4; break;
1132 case 8: func = TOK___bound_ptr_indir8; break;
1133 case 12: func = TOK___bound_ptr_indir12; break;
1134 case 16: func = TOK___bound_ptr_indir16; break;
1135 default:
1136 tcc_error("unhandled size when dereferencing bounded pointer");
1137 func = 0;
1138 break;
1141 /* patch relocation */
1142 /* XXX: find a better solution ? */
1143 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1144 sym = external_global_sym(func, &func_old_type, 0);
1145 if (!sym->c)
1146 put_extern_sym(sym, NULL, 0, 0);
1147 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1149 #endif
1151 /* Save the stack pointer onto the stack */
1152 ST_FUNC void gen_vla_sp_save(int addr) {
1153 /* mov %esp,addr(%ebp)*/
1154 o(0x89);
1155 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1158 /* Restore the SP from a location on the stack */
1159 ST_FUNC void gen_vla_sp_restore(int addr) {
1160 o(0x8b);
1161 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1164 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1165 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1166 #ifdef TCC_TARGET_PE
1167 /* alloca does more than just adjust %rsp on Windows */
1168 vpush_global_sym(&func_old_type, TOK_alloca);
1169 vswap(); /* Move alloca ref past allocation size */
1170 gfunc_call(1);
1171 vset(type, REG_IRET, 0);
1172 #else
1173 int r;
1174 r = gv(RC_INT); /* allocation size */
1175 /* sub r,%rsp */
1176 o(0x2b);
1177 o(0xe0 | r);
1178 /* We align to 16 bytes rather than align */
1179 /* and ~15, %esp */
1180 o(0xf0e483);
1181 /* mov %esp, r */
1182 o(0x89);
1183 o(0xe0 | r);
1184 vpop();
1185 vset(type, r, 0);
1186 #endif
1189 /* end of X86 code generator */
1190 /*************************************************************/
1191 #endif
1192 /*************************************************************/