Improved variable length array support.
[tinycc.git] / i386-gen.c
blob9ff2e180a614c206c445c6451c372be9bba42b41
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 4
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_IRET RC_EAX /* function return: integer register */
37 #define RC_LRET RC_EDX /* function return: second integer register */
38 #define RC_FRET RC_ST0 /* function return: float register */
40 /* pretty names for the registers */
41 enum {
42 TREG_EAX = 0,
43 TREG_ECX,
44 TREG_EDX,
45 TREG_ST0,
46 TREG_ESP = 4
49 /* return registers for function */
50 #define REG_IRET TREG_EAX /* single word int return register */
51 #define REG_LRET TREG_EDX /* second word return register (for long long) */
52 #define REG_FRET TREG_ST0 /* float return register */
54 /* defined if function parameters must be evaluated in reverse order */
55 #define INVERT_FUNC_PARAMS
57 /* defined if structures are passed as pointers. Otherwise structures
58 are directly pushed on stack. */
59 /* #define FUNC_STRUCT_PARAM_AS_PTR */
61 /* pointer size, in bytes */
62 #define PTR_SIZE 4
64 /* long double size and alignment, in bytes */
65 #define LDOUBLE_SIZE 12
66 #define LDOUBLE_ALIGN 4
67 /* maximum alignment (for aligned attribute support) */
68 #define MAX_ALIGN 8
71 #define psym oad
73 /******************************************************/
74 /* ELF defines */
76 #define EM_TCC_TARGET EM_386
78 /* relocation type for 32 bit data relocation */
79 #define R_DATA_32 R_386_32
80 #define R_DATA_PTR R_386_32
81 #define R_JMP_SLOT R_386_JMP_SLOT
82 #define R_COPY R_386_COPY
84 #define ELF_START_ADDR 0x08048000
85 #define ELF_PAGE_SIZE 0x1000
87 /******************************************************/
88 #else /* ! TARGET_DEFS_ONLY */
89 /******************************************************/
90 #include "tcc.h"
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 /* st0 */ RC_FLOAT | RC_ST0,
99 static unsigned long func_sub_sp_offset;
100 static int func_ret_sub;
101 #ifdef CONFIG_TCC_BCHECK
102 static unsigned long func_bound_offset;
103 #endif
105 /* XXX: make it faster ? */
106 ST_FUNC void g(int c)
108 int ind1;
109 ind1 = ind + 1;
110 if (ind1 > cur_text_section->data_allocated)
111 section_realloc(cur_text_section, ind1);
112 cur_text_section->data[ind] = c;
113 ind = ind1;
116 ST_FUNC void o(unsigned int c)
118 while (c) {
119 g(c);
120 c = c >> 8;
124 ST_FUNC void gen_le16(int v)
126 g(v);
127 g(v >> 8);
130 ST_FUNC void gen_le32(int c)
132 g(c);
133 g(c >> 8);
134 g(c >> 16);
135 g(c >> 24);
138 /* output a symbol and patch all calls to it */
139 ST_FUNC void gsym_addr(int t, int a)
141 int n, *ptr;
142 while (t) {
143 ptr = (int *)(cur_text_section->data + t);
144 n = *ptr; /* next value */
145 *ptr = a - t - 4;
146 t = n;
150 ST_FUNC void gsym(int t)
152 gsym_addr(t, ind);
155 /* psym is used to put an instruction with a data field which is a
156 reference to a symbol. It is in fact the same as oad ! */
157 #define psym oad
159 /* instruction + 4 bytes data. Return the address of the data */
160 ST_FUNC int oad(int c, int s)
162 int ind1;
164 o(c);
165 ind1 = ind + 4;
166 if (ind1 > cur_text_section->data_allocated)
167 section_realloc(cur_text_section, ind1);
168 *(int *)(cur_text_section->data + ind) = s;
169 s = ind;
170 ind = ind1;
171 return s;
174 /* output constant with relocation if 'r & VT_SYM' is true */
175 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
177 if (r & VT_SYM)
178 greloc(cur_text_section, sym, ind, R_386_32);
179 gen_le32(c);
182 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
184 if (r & VT_SYM)
185 greloc(cur_text_section, sym, ind, R_386_PC32);
186 gen_le32(c - 4);
189 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
190 opcode bits */
191 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
193 op_reg = op_reg << 3;
194 if ((r & VT_VALMASK) == VT_CONST) {
195 /* constant memory reference */
196 o(0x05 | op_reg);
197 gen_addr32(r, sym, c);
198 } else if ((r & VT_VALMASK) == VT_LOCAL) {
199 /* currently, we use only ebp as base */
200 if (c == (char)c) {
201 /* short reference */
202 o(0x45 | op_reg);
203 g(c);
204 } else {
205 oad(0x85 | op_reg, c);
207 } else {
208 g(0x00 | op_reg | (r & VT_VALMASK));
212 /* load 'r' from value 'sv' */
213 ST_FUNC void load(int r, SValue *sv)
215 int v, t, ft, fc, fr;
216 SValue v1;
218 #ifdef TCC_TARGET_PE
219 SValue v2;
220 sv = pe_getimport(sv, &v2);
221 #endif
223 fr = sv->r;
224 ft = sv->type.t;
225 fc = sv->c.ul;
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.ul = fc;
233 fr = r;
234 if (!(reg_classes[fr] & RC_INT))
235 fr = get_reg(RC_INT);
236 load(fr, &v1);
238 if ((ft & VT_BTYPE) == VT_FLOAT) {
239 o(0xd9); /* flds */
240 r = 0;
241 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
242 o(0xdd); /* fldl */
243 r = 0;
244 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
245 o(0xdb); /* fldt */
246 r = 5;
247 } else if ((ft & VT_TYPE) == VT_BYTE) {
248 o(0xbe0f); /* movsbl */
249 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
250 o(0xb60f); /* movzbl */
251 } else if ((ft & VT_TYPE) == VT_SHORT) {
252 o(0xbf0f); /* movswl */
253 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
254 o(0xb70f); /* movzwl */
255 } else {
256 o(0x8b); /* movl */
258 gen_modrm(r, fr, sv->sym, fc);
259 } else {
260 if (v == VT_CONST) {
261 o(0xb8 + r); /* mov $xx, r */
262 gen_addr32(fr, sv->sym, fc);
263 } else if (v == VT_LOCAL) {
264 if (fc) {
265 o(0x8d); /* lea xxx(%ebp), r */
266 gen_modrm(r, VT_LOCAL, sv->sym, fc);
267 } else {
268 o(0x89);
269 o(0xe8 + r); /* mov %ebp, r */
271 } else if (v == VT_CMP) {
272 oad(0xb8 + r, 0); /* mov $0, r */
273 o(0x0f); /* setxx %br */
274 o(fc);
275 o(0xc0 + r);
276 } else if (v == VT_JMP || v == VT_JMPI) {
277 t = v & 1;
278 oad(0xb8 + r, t); /* mov $1, r */
279 o(0x05eb); /* jmp after */
280 gsym(fc);
281 oad(0xb8 + r, t ^ 1); /* mov $0, r */
282 } else if (v != r) {
283 o(0x89);
284 o(0xc0 + r + v * 8); /* mov v, r */
289 /* store register 'r' in lvalue 'v' */
290 ST_FUNC void store(int r, SValue *v)
292 int fr, bt, ft, fc;
294 #ifdef TCC_TARGET_PE
295 SValue v2;
296 v = pe_getimport(v, &v2);
297 #endif
299 ft = v->type.t;
300 fc = v->c.ul;
301 fr = v->r & VT_VALMASK;
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 /* 'is_jmp' is '1' if it is a jump */
342 static void gcall_or_jmp(int is_jmp)
344 int r;
345 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
346 /* constant case */
347 if (vtop->r & VT_SYM) {
348 /* relocation case */
349 greloc(cur_text_section, vtop->sym,
350 ind + 1, R_386_PC32);
351 } else {
352 /* put an empty PC32 relocation */
353 put_elf_reloc(symtab_section, cur_text_section,
354 ind + 1, R_386_PC32, 0);
356 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
357 } else {
358 /* otherwise, indirect call */
359 r = gv(RC_INT);
360 o(0xff); /* call/jmp *r */
361 o(0xd0 + r + (is_jmp << 4));
365 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
366 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
368 /* Return 1 if this function returns via an sret pointer, 0 otherwise */
369 ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) {
370 *ret_align = 1; // Never have to re-align return values for x86
371 #ifdef TCC_TARGET_PE
372 int size, align;
373 size = type_size(vt, &align);
374 if (size > 8) {
375 return 1;
376 } else if (size > 4) {
377 ret->ref = NULL;
378 ret->t = VT_LLONG;
379 return 0;
380 } else {
381 ret->ref = NULL;
382 ret->t = VT_INT;
383 return 0;
385 #else
386 return 1;
387 #endif
390 /* Generate function call. The function address is pushed first, then
391 all the parameters in call order. This functions pops all the
392 parameters and the function address. */
393 ST_FUNC void gfunc_call(int nb_args)
395 int size, align, r, args_size, i, func_call;
396 Sym *func_sym;
398 args_size = 0;
399 for(i = 0;i < nb_args; i++) {
400 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
401 size = type_size(&vtop->type, &align);
402 /* align to stack align size */
403 size = (size + 3) & ~3;
404 /* allocate the necessary size on stack */
405 oad(0xec81, size); /* sub $xxx, %esp */
406 /* generate structure store */
407 r = get_reg(RC_INT);
408 o(0x89); /* mov %esp, r */
409 o(0xe0 + r);
410 vset(&vtop->type, r | VT_LVAL, 0);
411 vswap();
412 vstore();
413 args_size += size;
414 } else if (is_float(vtop->type.t)) {
415 gv(RC_FLOAT); /* only one float register */
416 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
417 size = 4;
418 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
419 size = 8;
420 else
421 size = 12;
422 oad(0xec81, size); /* sub $xxx, %esp */
423 if (size == 12)
424 o(0x7cdb);
425 else
426 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
427 g(0x24);
428 g(0x00);
429 args_size += size;
430 } else {
431 /* simple type (currently always same size) */
432 /* XXX: implicit cast ? */
433 r = gv(RC_INT);
434 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
435 size = 8;
436 o(0x50 + vtop->r2); /* push r */
437 } else {
438 size = 4;
440 o(0x50 + r); /* push r */
441 args_size += size;
443 vtop--;
445 save_regs(0); /* save used temporary registers */
446 func_sym = vtop->type.ref;
447 func_call = FUNC_CALL(func_sym->r);
448 /* fast call case */
449 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
450 func_call == FUNC_FASTCALLW) {
451 int fastcall_nb_regs;
452 uint8_t *fastcall_regs_ptr;
453 if (func_call == FUNC_FASTCALLW) {
454 fastcall_regs_ptr = fastcallw_regs;
455 fastcall_nb_regs = 2;
456 } else {
457 fastcall_regs_ptr = fastcall_regs;
458 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
460 for(i = 0;i < fastcall_nb_regs; i++) {
461 if (args_size <= 0)
462 break;
463 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
464 /* XXX: incorrect for struct/floats */
465 args_size -= 4;
468 #ifndef TCC_TARGET_PE
469 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
470 args_size -= 4;
471 #endif
472 gcall_or_jmp(0);
474 if (args_size && func_call != FUNC_STDCALL)
475 gadd_sp(args_size);
476 vtop--;
479 #ifdef TCC_TARGET_PE
480 #define FUNC_PROLOG_SIZE 10
481 #else
482 #define FUNC_PROLOG_SIZE 9
483 #endif
485 /* generate function prolog of type 't' */
486 ST_FUNC void gfunc_prolog(CType *func_type)
488 int addr, align, size, func_call, fastcall_nb_regs;
489 int param_index, param_addr;
490 uint8_t *fastcall_regs_ptr;
491 Sym *sym;
492 CType *type;
494 sym = func_type->ref;
495 func_call = FUNC_CALL(sym->r);
496 addr = 8;
497 loc = 0;
498 func_vc = 0;
500 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
501 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
502 fastcall_regs_ptr = fastcall_regs;
503 } else if (func_call == FUNC_FASTCALLW) {
504 fastcall_nb_regs = 2;
505 fastcall_regs_ptr = fastcallw_regs;
506 } else {
507 fastcall_nb_regs = 0;
508 fastcall_regs_ptr = NULL;
510 param_index = 0;
512 ind += FUNC_PROLOG_SIZE;
513 func_sub_sp_offset = ind;
514 /* if the function returns a structure, then add an
515 implicit pointer parameter */
516 func_vt = sym->type;
517 #ifdef TCC_TARGET_PE
518 size = type_size(&func_vt,&align);
519 if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) {
520 #else
521 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
522 #endif
523 /* XXX: fastcall case ? */
524 func_vc = addr;
525 addr += 4;
526 param_index++;
528 /* define parameters */
529 while ((sym = sym->next) != NULL) {
530 type = &sym->type;
531 size = type_size(type, &align);
532 size = (size + 3) & ~3;
533 #ifdef FUNC_STRUCT_PARAM_AS_PTR
534 /* structs are passed as pointer */
535 if ((type->t & VT_BTYPE) == VT_STRUCT) {
536 size = 4;
538 #endif
539 if (param_index < fastcall_nb_regs) {
540 /* save FASTCALL register */
541 loc -= 4;
542 o(0x89); /* movl */
543 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
544 param_addr = loc;
545 } else {
546 param_addr = addr;
547 addr += size;
549 sym_push(sym->v & ~SYM_FIELD, type,
550 VT_LOCAL | lvalue_type(type->t), param_addr);
551 param_index++;
553 func_ret_sub = 0;
554 /* pascal type call ? */
555 if (func_call == FUNC_STDCALL)
556 func_ret_sub = addr - 8;
557 #ifndef TCC_TARGET_PE
558 else if (func_vc)
559 func_ret_sub = 4;
560 #endif
562 #ifdef CONFIG_TCC_BCHECK
563 /* leave some room for bound checking code */
564 if (tcc_state->do_bounds_check) {
565 oad(0xb8, 0); /* lbound section pointer */
566 oad(0xb8, 0); /* call to function */
567 func_bound_offset = lbounds_section->data_offset;
569 #endif
572 /* generate function epilog */
573 ST_FUNC void gfunc_epilog(void)
575 int v, saved_ind;
577 #ifdef CONFIG_TCC_BCHECK
578 if (tcc_state->do_bounds_check
579 && func_bound_offset != lbounds_section->data_offset) {
580 int saved_ind;
581 int *bounds_ptr;
582 Sym *sym, *sym_data;
583 /* add end of table info */
584 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
585 *bounds_ptr = 0;
586 /* generate bound local allocation */
587 saved_ind = ind;
588 ind = func_sub_sp_offset;
589 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
590 func_bound_offset, lbounds_section->data_offset);
591 greloc(cur_text_section, sym_data,
592 ind + 1, R_386_32);
593 oad(0xb8, 0); /* mov %eax, xxx */
594 sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0);
595 greloc(cur_text_section, sym,
596 ind + 1, R_386_PC32);
597 oad(0xe8, -4);
598 ind = saved_ind;
599 /* generate bound check local freeing */
600 o(0x5250); /* save returned value, if any */
601 greloc(cur_text_section, sym_data,
602 ind + 1, R_386_32);
603 oad(0xb8, 0); /* mov %eax, xxx */
604 sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0);
605 greloc(cur_text_section, sym,
606 ind + 1, R_386_PC32);
607 oad(0xe8, -4);
608 o(0x585a); /* restore returned value, if any */
610 #endif
611 o(0xc9); /* leave */
612 if (func_ret_sub == 0) {
613 o(0xc3); /* ret */
614 } else {
615 o(0xc2); /* ret n */
616 g(func_ret_sub);
617 g(func_ret_sub >> 8);
619 /* align local size to word & save local variables */
621 v = (-loc + 3) & -4;
622 saved_ind = ind;
623 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
624 #ifdef TCC_TARGET_PE
625 if (v >= 4096) {
626 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
627 oad(0xb8, v); /* mov stacksize, %eax */
628 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
629 greloc(cur_text_section, sym, ind-4, R_386_PC32);
630 } else
631 #endif
633 o(0xe58955); /* push %ebp, mov %esp, %ebp */
634 o(0xec81); /* sub esp, stacksize */
635 gen_le32(v);
636 #if FUNC_PROLOG_SIZE == 10
637 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
638 #endif
640 ind = saved_ind;
643 /* generate a jump to a label */
644 ST_FUNC int gjmp(int t)
646 return psym(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 /* generate a test. set 'inv' to invert test. Stack entry is popped */
663 ST_FUNC int gtst(int inv, int t)
665 int v, *p;
667 v = vtop->r & VT_VALMASK;
668 if (v == VT_CMP) {
669 /* fast case : can jump directly since flags are set */
670 g(0x0f);
671 t = psym((vtop->c.i - 16) ^ inv, t);
672 } else if (v == VT_JMP || v == VT_JMPI) {
673 /* && or || optimization */
674 if ((v & 1) == inv) {
675 /* insert vtop->c jump list in t */
676 p = &vtop->c.i;
677 while (*p != 0)
678 p = (int *)(cur_text_section->data + *p);
679 *p = t;
680 t = vtop->c.i;
681 } else {
682 t = gjmp(t);
683 gsym(vtop->c.i);
685 } else {
686 if (is_float(vtop->type.t) ||
687 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
688 vpushi(0);
689 gen_op(TOK_NE);
691 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
692 /* constant jmp optimization */
693 if ((vtop->c.i != 0) != inv)
694 t = gjmp(t);
695 } else {
696 v = gv(RC_INT);
697 o(0x85);
698 o(0xc0 + v * 9);
699 g(0x0f);
700 t = psym(0x85 ^ inv, t);
703 vtop--;
704 return t;
707 /* generate an integer binary operation */
708 ST_FUNC void gen_opi(int op)
710 int r, fr, opc, c;
712 switch(op) {
713 case '+':
714 case TOK_ADDC1: /* add with carry generation */
715 opc = 0;
716 gen_op8:
717 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
718 /* constant case */
719 vswap();
720 r = gv(RC_INT);
721 vswap();
722 c = vtop->c.i;
723 if (c == (char)c) {
724 /* generate inc and dec for smaller code */
725 if (c==1 && opc==0) {
726 o (0x40 | r); // inc
727 } else if (c==1 && opc==5) {
728 o (0x48 | r); // dec
729 } else {
730 o(0x83);
731 o(0xc0 | (opc << 3) | r);
732 g(c);
734 } else {
735 o(0x81);
736 oad(0xc0 | (opc << 3) | r, c);
738 } else {
739 gv2(RC_INT, RC_INT);
740 r = vtop[-1].r;
741 fr = vtop[0].r;
742 o((opc << 3) | 0x01);
743 o(0xc0 + r + fr * 8);
745 vtop--;
746 if (op >= TOK_ULT && op <= TOK_GT) {
747 vtop->r = VT_CMP;
748 vtop->c.i = op;
750 break;
751 case '-':
752 case TOK_SUBC1: /* sub with carry generation */
753 opc = 5;
754 goto gen_op8;
755 case TOK_ADDC2: /* add with carry use */
756 opc = 2;
757 goto gen_op8;
758 case TOK_SUBC2: /* sub with carry use */
759 opc = 3;
760 goto gen_op8;
761 case '&':
762 opc = 4;
763 goto gen_op8;
764 case '^':
765 opc = 6;
766 goto gen_op8;
767 case '|':
768 opc = 1;
769 goto gen_op8;
770 case '*':
771 gv2(RC_INT, RC_INT);
772 r = vtop[-1].r;
773 fr = vtop[0].r;
774 vtop--;
775 o(0xaf0f); /* imul fr, r */
776 o(0xc0 + fr + r * 8);
777 break;
778 case TOK_SHL:
779 opc = 4;
780 goto gen_shift;
781 case TOK_SHR:
782 opc = 5;
783 goto gen_shift;
784 case TOK_SAR:
785 opc = 7;
786 gen_shift:
787 opc = 0xc0 | (opc << 3);
788 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
789 /* constant case */
790 vswap();
791 r = gv(RC_INT);
792 vswap();
793 c = vtop->c.i & 0x1f;
794 o(0xc1); /* shl/shr/sar $xxx, r */
795 o(opc | r);
796 g(c);
797 } else {
798 /* we generate the shift in ecx */
799 gv2(RC_INT, RC_ECX);
800 r = vtop[-1].r;
801 o(0xd3); /* shl/shr/sar %cl, r */
802 o(opc | r);
804 vtop--;
805 break;
806 case '/':
807 case TOK_UDIV:
808 case TOK_PDIV:
809 case '%':
810 case TOK_UMOD:
811 case TOK_UMULL:
812 /* first operand must be in eax */
813 /* XXX: need better constraint for second operand */
814 gv2(RC_EAX, RC_ECX);
815 r = vtop[-1].r;
816 fr = vtop[0].r;
817 vtop--;
818 save_reg(TREG_EDX);
819 if (op == TOK_UMULL) {
820 o(0xf7); /* mul fr */
821 o(0xe0 + fr);
822 vtop->r2 = TREG_EDX;
823 r = TREG_EAX;
824 } else {
825 if (op == TOK_UDIV || op == TOK_UMOD) {
826 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
827 o(0xf0 + fr);
828 } else {
829 o(0xf799); /* cltd, idiv fr, %eax */
830 o(0xf8 + fr);
832 if (op == '%' || op == TOK_UMOD)
833 r = TREG_EDX;
834 else
835 r = TREG_EAX;
837 vtop->r = r;
838 break;
839 default:
840 opc = 7;
841 goto gen_op8;
845 /* generate a floating point operation 'v = t1 op t2' instruction. The
846 two operands are guaranted to have the same floating point type */
847 /* XXX: need to use ST1 too */
848 ST_FUNC void gen_opf(int op)
850 int a, ft, fc, swapped, r;
852 /* convert constants to memory references */
853 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
854 vswap();
855 gv(RC_FLOAT);
856 vswap();
858 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
859 gv(RC_FLOAT);
861 /* must put at least one value in the floating point register */
862 if ((vtop[-1].r & VT_LVAL) &&
863 (vtop[0].r & VT_LVAL)) {
864 vswap();
865 gv(RC_FLOAT);
866 vswap();
868 swapped = 0;
869 /* swap the stack if needed so that t1 is the register and t2 is
870 the memory reference */
871 if (vtop[-1].r & VT_LVAL) {
872 vswap();
873 swapped = 1;
875 if (op >= TOK_ULT && op <= TOK_GT) {
876 /* load on stack second operand */
877 load(TREG_ST0, vtop);
878 save_reg(TREG_EAX); /* eax is used by FP comparison code */
879 if (op == TOK_GE || op == TOK_GT)
880 swapped = !swapped;
881 else if (op == TOK_EQ || op == TOK_NE)
882 swapped = 0;
883 if (swapped)
884 o(0xc9d9); /* fxch %st(1) */
885 o(0xe9da); /* fucompp */
886 o(0xe0df); /* fnstsw %ax */
887 if (op == TOK_EQ) {
888 o(0x45e480); /* and $0x45, %ah */
889 o(0x40fC80); /* cmp $0x40, %ah */
890 } else if (op == TOK_NE) {
891 o(0x45e480); /* and $0x45, %ah */
892 o(0x40f480); /* xor $0x40, %ah */
893 op = TOK_NE;
894 } else if (op == TOK_GE || op == TOK_LE) {
895 o(0x05c4f6); /* test $0x05, %ah */
896 op = TOK_EQ;
897 } else {
898 o(0x45c4f6); /* test $0x45, %ah */
899 op = TOK_EQ;
901 vtop--;
902 vtop->r = VT_CMP;
903 vtop->c.i = op;
904 } else {
905 /* no memory reference possible for long double operations */
906 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
907 load(TREG_ST0, vtop);
908 swapped = !swapped;
911 switch(op) {
912 default:
913 case '+':
914 a = 0;
915 break;
916 case '-':
917 a = 4;
918 if (swapped)
919 a++;
920 break;
921 case '*':
922 a = 1;
923 break;
924 case '/':
925 a = 6;
926 if (swapped)
927 a++;
928 break;
930 ft = vtop->type.t;
931 fc = vtop->c.ul;
932 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
933 o(0xde); /* fxxxp %st, %st(1) */
934 o(0xc1 + (a << 3));
935 } else {
936 /* if saved lvalue, then we must reload it */
937 r = vtop->r;
938 if ((r & VT_VALMASK) == VT_LLOCAL) {
939 SValue v1;
940 r = get_reg(RC_INT);
941 v1.type.t = VT_INT;
942 v1.r = VT_LOCAL | VT_LVAL;
943 v1.c.ul = fc;
944 load(r, &v1);
945 fc = 0;
948 if ((ft & VT_BTYPE) == VT_DOUBLE)
949 o(0xdc);
950 else
951 o(0xd8);
952 gen_modrm(a, r, vtop->sym, fc);
954 vtop--;
958 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
959 and 'long long' cases. */
960 ST_FUNC void gen_cvt_itof(int t)
962 save_reg(TREG_ST0);
963 gv(RC_INT);
964 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
965 /* signed long long to float/double/long double (unsigned case
966 is handled generically) */
967 o(0x50 + vtop->r2); /* push r2 */
968 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
969 o(0x242cdf); /* fildll (%esp) */
970 o(0x08c483); /* add $8, %esp */
971 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
972 (VT_INT | VT_UNSIGNED)) {
973 /* unsigned int to float/double/long double */
974 o(0x6a); /* push $0 */
975 g(0x00);
976 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
977 o(0x242cdf); /* fildll (%esp) */
978 o(0x08c483); /* add $8, %esp */
979 } else {
980 /* int to float/double/long double */
981 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
982 o(0x2404db); /* fildl (%esp) */
983 o(0x04c483); /* add $4, %esp */
985 vtop->r = TREG_ST0;
988 /* convert fp to int 't' type */
989 /* XXX: handle long long case */
990 ST_FUNC void gen_cvt_ftoi(int t)
992 int r, r2, size;
993 Sym *sym;
994 CType ushort_type;
996 ushort_type.t = VT_SHORT | VT_UNSIGNED;
997 ushort_type.ref = 0;
999 gv(RC_FLOAT);
1000 if (t != VT_INT)
1001 size = 8;
1002 else
1003 size = 4;
1005 o(0x2dd9); /* ldcw xxx */
1006 sym = external_global_sym(TOK___tcc_int_fpu_control,
1007 &ushort_type, VT_LVAL);
1008 greloc(cur_text_section, sym,
1009 ind, R_386_32);
1010 gen_le32(0);
1012 oad(0xec81, size); /* sub $xxx, %esp */
1013 if (size == 4)
1014 o(0x1cdb); /* fistpl */
1015 else
1016 o(0x3cdf); /* fistpll */
1017 o(0x24);
1018 o(0x2dd9); /* ldcw xxx */
1019 sym = external_global_sym(TOK___tcc_fpu_control,
1020 &ushort_type, VT_LVAL);
1021 greloc(cur_text_section, sym,
1022 ind, R_386_32);
1023 gen_le32(0);
1025 r = get_reg(RC_INT);
1026 o(0x58 + r); /* pop r */
1027 if (size == 8) {
1028 if (t == VT_LLONG) {
1029 vtop->r = r; /* mark reg as used */
1030 r2 = get_reg(RC_INT);
1031 o(0x58 + r2); /* pop r2 */
1032 vtop->r2 = r2;
1033 } else {
1034 o(0x04c483); /* add $4, %esp */
1037 vtop->r = r;
1040 /* convert from one floating point type to another */
1041 ST_FUNC void gen_cvt_ftof(int t)
1043 /* all we have to do on i386 is to put the float in a register */
1044 gv(RC_FLOAT);
1047 /* computed goto support */
1048 ST_FUNC void ggoto(void)
1050 gcall_or_jmp(1);
1051 vtop--;
1054 /* bound check support functions */
1055 #ifdef CONFIG_TCC_BCHECK
1057 /* generate a bounded pointer addition */
1058 ST_FUNC void gen_bounded_ptr_add(void)
1060 Sym *sym;
1062 /* prepare fast i386 function call (args in eax and edx) */
1063 gv2(RC_EAX, RC_EDX);
1064 /* save all temporary registers */
1065 vtop -= 2;
1066 save_regs(0);
1067 /* do a fast function call */
1068 sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
1069 greloc(cur_text_section, sym,
1070 ind + 1, R_386_PC32);
1071 oad(0xe8, -4);
1072 /* returned pointer is in eax */
1073 vtop++;
1074 vtop->r = TREG_EAX | VT_BOUNDED;
1075 /* address of bounding function call point */
1076 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1079 /* patch pointer addition in vtop so that pointer dereferencing is
1080 also tested */
1081 ST_FUNC void gen_bounded_ptr_deref(void)
1083 int func;
1084 int size, align;
1085 Elf32_Rel *rel;
1086 Sym *sym;
1088 size = 0;
1089 /* XXX: put that code in generic part of tcc */
1090 if (!is_float(vtop->type.t)) {
1091 if (vtop->r & VT_LVAL_BYTE)
1092 size = 1;
1093 else if (vtop->r & VT_LVAL_SHORT)
1094 size = 2;
1096 if (!size)
1097 size = type_size(&vtop->type, &align);
1098 switch(size) {
1099 case 1: func = TOK___bound_ptr_indir1; break;
1100 case 2: func = TOK___bound_ptr_indir2; break;
1101 case 4: func = TOK___bound_ptr_indir4; break;
1102 case 8: func = TOK___bound_ptr_indir8; break;
1103 case 12: func = TOK___bound_ptr_indir12; break;
1104 case 16: func = TOK___bound_ptr_indir16; break;
1105 default:
1106 tcc_error("unhandled size when dereferencing bounded pointer");
1107 func = 0;
1108 break;
1111 /* patch relocation */
1112 /* XXX: find a better solution ? */
1113 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1114 sym = external_global_sym(func, &func_old_type, 0);
1115 if (!sym->c)
1116 put_extern_sym(sym, NULL, 0, 0);
1117 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1119 #endif
1121 /* Save the stack pointer onto the stack */
1122 ST_FUNC void gen_vla_sp_save(int addr) {
1123 /* mov %esp,addr(%ebp)*/
1124 o(0x89);
1125 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1128 /* Restore the SP from a location on the stack */
1129 ST_FUNC void gen_vla_sp_restore(int addr) {
1130 o(0x8b);
1131 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1134 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1135 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1136 #ifdef TCC_TARGET_PE
1137 /* alloca does more than just adjust %rsp on Windows */
1138 vpush_global_sym(&func_old_type, TOK_alloca);
1139 vswap(); /* Move alloca ref past allocation size */
1140 gfunc_call(1);
1141 vset(type, REG_IRET, 0);
1142 #else
1143 int r;
1144 r = gv(RC_INT); /* allocation size */
1145 /* sub r,%rsp */
1146 o(0x2b);
1147 o(0xe0 | r);
1148 /* We align to 16 bytes rather than align */
1149 /* and ~15, %esp */
1150 o(0xf0e483);
1151 /* mov %esp, r */
1152 o(0x89);
1153 o(0xe0 | r);
1154 vpop();
1155 vset(type, r, 0);
1156 #endif
1159 /* end of X86 code generator */
1160 /*************************************************************/
1161 #endif
1162 /*************************************************************/