tccpe: allow linkage to extern symbols from asm
[tinycc.git] / i386-gen.c
blobd44737ac0e4f04484bbfe0f4aee73afe83dd8937
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 /* number of available registers */
22 #define NB_REGS 4
24 /* a register can belong to several classes. The classes must be
25 sorted from more general to more precise (see gv2() code which does
26 assumptions on it). */
27 #define RC_INT 0x0001 /* generic integer register */
28 #define RC_FLOAT 0x0002 /* generic float register */
29 #define RC_EAX 0x0004
30 #define RC_ST0 0x0008
31 #define RC_ECX 0x0010
32 #define RC_EDX 0x0020
33 #define RC_IRET RC_EAX /* function return: integer register */
34 #define RC_LRET RC_EDX /* function return: second integer register */
35 #define RC_FRET RC_ST0 /* function return: float register */
37 /* pretty names for the registers */
38 enum {
39 TREG_EAX = 0,
40 TREG_ECX,
41 TREG_EDX,
42 TREG_ST0,
45 const int reg_classes[NB_REGS] = {
46 /* eax */ RC_INT | RC_EAX,
47 /* ecx */ RC_INT | RC_ECX,
48 /* edx */ RC_INT | RC_EDX,
49 /* st0 */ RC_FLOAT | RC_ST0,
52 /* return registers for function */
53 #define REG_IRET TREG_EAX /* single word int return register */
54 #define REG_LRET TREG_EDX /* second word return register (for long long) */
55 #define REG_FRET TREG_ST0 /* float return register */
57 /* defined if function parameters must be evaluated in reverse order */
58 #define INVERT_FUNC_PARAMS
60 /* defined if structures are passed as pointers. Otherwise structures
61 are directly pushed on stack. */
62 //#define FUNC_STRUCT_PARAM_AS_PTR
64 /* pointer size, in bytes */
65 #define PTR_SIZE 4
67 /* long double size and alignment, in bytes */
68 #define LDOUBLE_SIZE 12
69 #define LDOUBLE_ALIGN 4
70 /* maximum alignment (for aligned attribute support) */
71 #define MAX_ALIGN 8
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 /******************************************************/
89 static unsigned long func_sub_sp_offset;
90 static unsigned long func_bound_offset;
91 static int func_ret_sub;
93 /* XXX: make it faster ? */
94 void g(int c)
96 int ind1;
97 ind1 = ind + 1;
98 if (ind1 > cur_text_section->data_allocated)
99 section_realloc(cur_text_section, ind1);
100 cur_text_section->data[ind] = c;
101 ind = ind1;
104 void o(unsigned int c)
106 while (c) {
107 g(c);
108 c = c >> 8;
112 void gen_le32(int c)
114 g(c);
115 g(c >> 8);
116 g(c >> 16);
117 g(c >> 24);
120 /* output a symbol and patch all calls to it */
121 void gsym_addr(int t, int a)
123 int n, *ptr;
124 while (t) {
125 ptr = (int *)(cur_text_section->data + t);
126 n = *ptr; /* next value */
127 *ptr = a - t - 4;
128 t = n;
132 void gsym(int t)
134 gsym_addr(t, ind);
137 /* psym is used to put an instruction with a data field which is a
138 reference to a symbol. It is in fact the same as oad ! */
139 #define psym oad
141 /* instruction + 4 bytes data. Return the address of the data */
142 static int oad(int c, int s)
144 int ind1;
146 o(c);
147 ind1 = ind + 4;
148 if (ind1 > cur_text_section->data_allocated)
149 section_realloc(cur_text_section, ind1);
150 *(int *)(cur_text_section->data + ind) = s;
151 s = ind;
152 ind = ind1;
153 return s;
156 /* output constant with relocation if 'r & VT_SYM' is true */
157 static void gen_addr32(int r, Sym *sym, int c)
159 if (r & VT_SYM)
160 greloc(cur_text_section, sym, ind, R_386_32);
161 gen_le32(c);
164 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
165 opcode bits */
166 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
168 op_reg = op_reg << 3;
169 if ((r & VT_VALMASK) == VT_CONST) {
170 /* constant memory reference */
171 o(0x05 | op_reg);
172 gen_addr32(r, sym, c);
173 } else if ((r & VT_VALMASK) == VT_LOCAL) {
174 /* currently, we use only ebp as base */
175 if (c == (char)c) {
176 /* short reference */
177 o(0x45 | op_reg);
178 g(c);
179 } else {
180 oad(0x85 | op_reg, c);
182 } else {
183 g(0x00 | op_reg | (r & VT_VALMASK));
187 #ifdef TCC_TARGET_PE
188 static void mk_pointer(CType *type);
189 static void indir(void);
191 int handle_dllimport(int r, SValue *sv, void (*fn)(int r, SValue *sv))
193 if ((sv->r & (VT_VALMASK|VT_SYM|VT_CONST)) != (VT_SYM|VT_CONST))
194 return 0;
195 if (0 == (sv->sym->type.t & VT_IMPORT))
196 return 0;
198 printf("import %d %04x %s\n", r, ind, get_tok_str(sv->sym->v, NULL));
200 sv->sym->type.t &= ~VT_IMPORT;
201 ++vtop;
203 *vtop = *sv;
204 mk_pointer(&vtop->type);
205 indir();
206 fn(r, vtop);
208 --vtop;
209 sv->sym->type.t |= VT_IMPORT;
210 return 1;
212 #endif
214 /* load 'r' from value 'sv' */
215 void load(int r, SValue *sv)
217 int v, t, ft, fc, fr;
218 SValue v1;
220 #ifdef TCC_TARGET_PE
221 if (handle_dllimport(r, sv, load))
222 return;
223 #endif
224 fr = sv->r;
225 ft = sv->type.t;
226 fc = sv->c.ul;
228 v = fr & VT_VALMASK;
229 if (fr & VT_LVAL) {
230 if (v == VT_LLOCAL) {
231 v1.type.t = VT_INT;
232 v1.r = VT_LOCAL | VT_LVAL;
233 v1.c.ul = fc;
234 load(r, &v1);
235 fr = r;
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) {
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 o(0x8d); /* lea xxx(%ebp), r */
264 gen_modrm(r, VT_LOCAL, sv->sym, fc);
265 } else if (v == VT_CMP) {
266 oad(0xb8 + r, 0); /* mov $0, r */
267 o(0x0f); /* setxx %br */
268 o(fc);
269 o(0xc0 + r);
270 } else if (v == VT_JMP || v == VT_JMPI) {
271 t = v & 1;
272 oad(0xb8 + r, t); /* mov $1, r */
273 o(0x05eb); /* jmp after */
274 gsym(fc);
275 oad(0xb8 + r, t ^ 1); /* mov $0, r */
276 } else if (v != r) {
277 o(0x89);
278 o(0xc0 + r + v * 8); /* mov v, r */
283 /* store register 'r' in lvalue 'v' */
284 void store(int r, SValue *v)
286 int fr, bt, ft, fc;
288 #ifdef TCC_TARGET_PE
289 if (handle_dllimport(r, v, store))
290 return;
291 #endif
292 ft = v->type.t;
293 fc = v->c.ul;
294 fr = v->r & VT_VALMASK;
295 bt = ft & VT_BTYPE;
296 /* XXX: incorrect if float reg to reg */
297 if (bt == VT_FLOAT) {
298 o(0xd9); /* fsts */
299 r = 2;
300 } else if (bt == VT_DOUBLE) {
301 o(0xdd); /* fstpl */
302 r = 2;
303 } else if (bt == VT_LDOUBLE) {
304 o(0xc0d9); /* fld %st(0) */
305 o(0xdb); /* fstpt */
306 r = 7;
307 } else {
308 if (bt == VT_SHORT)
309 o(0x66);
310 if (bt == VT_BYTE || bt == VT_BOOL)
311 o(0x88);
312 else
313 o(0x89);
315 if (fr == VT_CONST ||
316 fr == VT_LOCAL ||
317 (v->r & VT_LVAL)) {
318 gen_modrm(r, v->r, v->sym, fc);
319 } else if (fr != r) {
320 o(0xc0 + fr + r * 8); /* mov r, fr */
324 static void gadd_sp(int val)
326 if (val == (char)val) {
327 o(0xc483);
328 g(val);
329 } else {
330 oad(0xc481, val); /* add $xxx, %esp */
334 /* 'is_jmp' is '1' if it is a jump */
335 static void gcall_or_jmp(int is_jmp)
337 int r;
338 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
339 /* constant case */
340 if (vtop->r & VT_SYM) {
341 /* relocation case */
342 greloc(cur_text_section, vtop->sym,
343 ind + 1, R_386_PC32);
344 } else {
345 /* put an empty PC32 relocation */
346 put_elf_reloc(symtab_section, cur_text_section,
347 ind + 1, R_386_PC32, 0);
349 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
350 } else {
351 /* otherwise, indirect call */
352 r = gv(RC_INT);
353 o(0xff); /* call/jmp *r */
354 o(0xd0 + r + (is_jmp << 4));
358 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
359 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
361 /* Generate function call. The function address is pushed first, then
362 all the parameters in call order. This functions pops all the
363 parameters and the function address. */
364 void gfunc_call(int nb_args)
366 int size, align, r, args_size, i, func_call;
367 Sym *func_sym;
369 args_size = 0;
370 for(i = 0;i < nb_args; i++) {
371 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
372 size = type_size(&vtop->type, &align);
373 /* align to stack align size */
374 size = (size + 3) & ~3;
375 /* allocate the necessary size on stack */
376 oad(0xec81, size); /* sub $xxx, %esp */
377 /* generate structure store */
378 r = get_reg(RC_INT);
379 o(0x89); /* mov %esp, r */
380 o(0xe0 + r);
381 vset(&vtop->type, r | VT_LVAL, 0);
382 vswap();
383 vstore();
384 args_size += size;
385 } else if (is_float(vtop->type.t)) {
386 gv(RC_FLOAT); /* only one float register */
387 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
388 size = 4;
389 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
390 size = 8;
391 else
392 size = 12;
393 oad(0xec81, size); /* sub $xxx, %esp */
394 if (size == 12)
395 o(0x7cdb);
396 else
397 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
398 g(0x24);
399 g(0x00);
400 args_size += size;
401 } else {
402 /* simple type (currently always same size) */
403 /* XXX: implicit cast ? */
404 r = gv(RC_INT);
405 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
406 size = 8;
407 o(0x50 + vtop->r2); /* push r */
408 } else {
409 size = 4;
411 o(0x50 + r); /* push r */
412 args_size += size;
414 vtop--;
416 save_regs(0); /* save used temporary registers */
417 func_sym = vtop->type.ref;
418 func_call = FUNC_CALL(func_sym->r);
419 /* fast call case */
420 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
421 func_call == FUNC_FASTCALLW) {
422 int fastcall_nb_regs;
423 uint8_t *fastcall_regs_ptr;
424 if (func_call == FUNC_FASTCALLW) {
425 fastcall_regs_ptr = fastcallw_regs;
426 fastcall_nb_regs = 2;
427 } else {
428 fastcall_regs_ptr = fastcall_regs;
429 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
431 for(i = 0;i < fastcall_nb_regs; i++) {
432 if (args_size <= 0)
433 break;
434 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
435 /* XXX: incorrect for struct/floats */
436 args_size -= 4;
439 gcall_or_jmp(0);
441 #ifdef TCC_TARGET_PE
442 if ((func_sym->type.t & VT_BTYPE) == VT_STRUCT)
443 args_size -= 4;
444 #endif
445 if (args_size && func_call != FUNC_STDCALL)
446 gadd_sp(args_size);
447 vtop--;
450 #ifdef TCC_TARGET_PE
451 #define FUNC_PROLOG_SIZE 10
452 #else
453 #define FUNC_PROLOG_SIZE 9
454 #endif
456 /* generate function prolog of type 't' */
457 void gfunc_prolog(CType *func_type)
459 int addr, align, size, func_call, fastcall_nb_regs;
460 int param_index, param_addr;
461 uint8_t *fastcall_regs_ptr;
462 Sym *sym;
463 CType *type;
465 sym = func_type->ref;
466 func_call = FUNC_CALL(sym->r);
467 addr = 8;
468 loc = 0;
469 func_vc = 0;
471 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
472 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
473 fastcall_regs_ptr = fastcall_regs;
474 } else if (func_call == FUNC_FASTCALLW) {
475 fastcall_nb_regs = 2;
476 fastcall_regs_ptr = fastcallw_regs;
477 } else {
478 fastcall_nb_regs = 0;
479 fastcall_regs_ptr = NULL;
481 param_index = 0;
483 ind += FUNC_PROLOG_SIZE;
484 func_sub_sp_offset = ind;
485 /* if the function returns a structure, then add an
486 implicit pointer parameter */
487 func_vt = sym->type;
488 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
489 /* XXX: fastcall case ? */
490 func_vc = addr;
491 addr += 4;
492 param_index++;
494 /* define parameters */
495 while ((sym = sym->next) != NULL) {
496 type = &sym->type;
497 size = type_size(type, &align);
498 size = (size + 3) & ~3;
499 #ifdef FUNC_STRUCT_PARAM_AS_PTR
500 /* structs are passed as pointer */
501 if ((type->t & VT_BTYPE) == VT_STRUCT) {
502 size = 4;
504 #endif
505 if (param_index < fastcall_nb_regs) {
506 /* save FASTCALL register */
507 loc -= 4;
508 o(0x89); /* movl */
509 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
510 param_addr = loc;
511 } else {
512 param_addr = addr;
513 addr += size;
515 sym_push(sym->v & ~SYM_FIELD, type,
516 VT_LOCAL | lvalue_type(type->t), param_addr);
517 param_index++;
519 func_ret_sub = 0;
520 /* pascal type call ? */
521 if (func_call == FUNC_STDCALL)
522 func_ret_sub = addr - 8;
523 #ifdef TCC_TARGET_PE
524 else if (func_vc)
525 func_ret_sub = 4;
526 #endif
528 /* leave some room for bound checking code */
529 if (tcc_state->do_bounds_check) {
530 oad(0xb8, 0); /* lbound section pointer */
531 oad(0xb8, 0); /* call to function */
532 func_bound_offset = lbounds_section->data_offset;
536 /* generate function epilog */
537 void gfunc_epilog(void)
539 int v, saved_ind;
541 #ifdef CONFIG_TCC_BCHECK
542 if (tcc_state->do_bounds_check
543 && func_bound_offset != lbounds_section->data_offset) {
544 int saved_ind;
545 int *bounds_ptr;
546 Sym *sym, *sym_data;
547 /* add end of table info */
548 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
549 *bounds_ptr = 0;
550 /* generate bound local allocation */
551 saved_ind = ind;
552 ind = func_sub_sp_offset;
553 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
554 func_bound_offset, lbounds_section->data_offset);
555 greloc(cur_text_section, sym_data,
556 ind + 1, R_386_32);
557 oad(0xb8, 0); /* mov %eax, xxx */
558 sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0);
559 greloc(cur_text_section, sym,
560 ind + 1, R_386_PC32);
561 oad(0xe8, -4);
562 ind = saved_ind;
563 /* generate bound check local freeing */
564 o(0x5250); /* save returned value, if any */
565 greloc(cur_text_section, sym_data,
566 ind + 1, R_386_32);
567 oad(0xb8, 0); /* mov %eax, xxx */
568 sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0);
569 greloc(cur_text_section, sym,
570 ind + 1, R_386_PC32);
571 oad(0xe8, -4);
572 o(0x585a); /* restore returned value, if any */
574 #endif
575 o(0xc9); /* leave */
576 if (func_ret_sub == 0) {
577 o(0xc3); /* ret */
578 } else {
579 o(0xc2); /* ret n */
580 g(func_ret_sub);
581 g(func_ret_sub >> 8);
583 /* align local size to word & save local variables */
585 v = (-loc + 3) & -4;
586 saved_ind = ind;
587 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
588 #ifdef TCC_TARGET_PE
589 if (v >= 4096) {
590 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
591 oad(0xb8, v); /* mov stacksize, %eax */
592 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
593 greloc(cur_text_section, sym, ind-4, R_386_PC32);
594 } else
595 #endif
597 o(0xe58955); /* push %ebp, mov %esp, %ebp */
598 o(0xec81); /* sub esp, stacksize */
599 gen_le32(v);
600 #if FUNC_PROLOG_SIZE == 10
601 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
602 #endif
604 ind = saved_ind;
607 /* generate a jump to a label */
608 int gjmp(int t)
610 return psym(0xe9, t);
613 /* generate a jump to a fixed address */
614 void gjmp_addr(int a)
616 int r;
617 r = a - ind - 2;
618 if (r == (char)r) {
619 g(0xeb);
620 g(r);
621 } else {
622 oad(0xe9, a - ind - 5);
626 /* generate a test. set 'inv' to invert test. Stack entry is popped */
627 int gtst(int inv, int t)
629 int v, *p;
631 v = vtop->r & VT_VALMASK;
632 if (v == VT_CMP) {
633 /* fast case : can jump directly since flags are set */
634 g(0x0f);
635 t = psym((vtop->c.i - 16) ^ inv, t);
636 } else if (v == VT_JMP || v == VT_JMPI) {
637 /* && or || optimization */
638 if ((v & 1) == inv) {
639 /* insert vtop->c jump list in t */
640 p = &vtop->c.i;
641 while (*p != 0)
642 p = (int *)(cur_text_section->data + *p);
643 *p = t;
644 t = vtop->c.i;
645 } else {
646 t = gjmp(t);
647 gsym(vtop->c.i);
649 } else {
650 if (is_float(vtop->type.t) ||
651 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
652 vpushi(0);
653 gen_op(TOK_NE);
655 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
656 /* constant jmp optimization */
657 if ((vtop->c.i != 0) != inv)
658 t = gjmp(t);
659 } else {
660 v = gv(RC_INT);
661 o(0x85);
662 o(0xc0 + v * 9);
663 g(0x0f);
664 t = psym(0x85 ^ inv, t);
667 vtop--;
668 return t;
671 /* generate an integer binary operation */
672 void gen_opi(int op)
674 int r, fr, opc, c;
676 switch(op) {
677 case '+':
678 case TOK_ADDC1: /* add with carry generation */
679 opc = 0;
680 gen_op8:
681 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
682 /* constant case */
683 vswap();
684 r = gv(RC_INT);
685 vswap();
686 c = vtop->c.i;
687 if (c == (char)c) {
688 /* XXX: generate inc and dec for smaller code ? */
689 o(0x83);
690 o(0xc0 | (opc << 3) | r);
691 g(c);
692 } else {
693 o(0x81);
694 oad(0xc0 | (opc << 3) | r, c);
696 } else {
697 gv2(RC_INT, RC_INT);
698 r = vtop[-1].r;
699 fr = vtop[0].r;
700 o((opc << 3) | 0x01);
701 o(0xc0 + r + fr * 8);
703 vtop--;
704 if (op >= TOK_ULT && op <= TOK_GT) {
705 vtop->r = VT_CMP;
706 vtop->c.i = op;
708 break;
709 case '-':
710 case TOK_SUBC1: /* sub with carry generation */
711 opc = 5;
712 goto gen_op8;
713 case TOK_ADDC2: /* add with carry use */
714 opc = 2;
715 goto gen_op8;
716 case TOK_SUBC2: /* sub with carry use */
717 opc = 3;
718 goto gen_op8;
719 case '&':
720 opc = 4;
721 goto gen_op8;
722 case '^':
723 opc = 6;
724 goto gen_op8;
725 case '|':
726 opc = 1;
727 goto gen_op8;
728 case '*':
729 gv2(RC_INT, RC_INT);
730 r = vtop[-1].r;
731 fr = vtop[0].r;
732 vtop--;
733 o(0xaf0f); /* imul fr, r */
734 o(0xc0 + fr + r * 8);
735 break;
736 case TOK_SHL:
737 opc = 4;
738 goto gen_shift;
739 case TOK_SHR:
740 opc = 5;
741 goto gen_shift;
742 case TOK_SAR:
743 opc = 7;
744 gen_shift:
745 opc = 0xc0 | (opc << 3);
746 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
747 /* constant case */
748 vswap();
749 r = gv(RC_INT);
750 vswap();
751 c = vtop->c.i & 0x1f;
752 o(0xc1); /* shl/shr/sar $xxx, r */
753 o(opc | r);
754 g(c);
755 } else {
756 /* we generate the shift in ecx */
757 gv2(RC_INT, RC_ECX);
758 r = vtop[-1].r;
759 o(0xd3); /* shl/shr/sar %cl, r */
760 o(opc | r);
762 vtop--;
763 break;
764 case '/':
765 case TOK_UDIV:
766 case TOK_PDIV:
767 case '%':
768 case TOK_UMOD:
769 case TOK_UMULL:
770 /* first operand must be in eax */
771 /* XXX: need better constraint for second operand */
772 gv2(RC_EAX, RC_ECX);
773 r = vtop[-1].r;
774 fr = vtop[0].r;
775 vtop--;
776 save_reg(TREG_EDX);
777 if (op == TOK_UMULL) {
778 o(0xf7); /* mul fr */
779 o(0xe0 + fr);
780 vtop->r2 = TREG_EDX;
781 r = TREG_EAX;
782 } else {
783 if (op == TOK_UDIV || op == TOK_UMOD) {
784 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
785 o(0xf0 + fr);
786 } else {
787 o(0xf799); /* cltd, idiv fr, %eax */
788 o(0xf8 + fr);
790 if (op == '%' || op == TOK_UMOD)
791 r = TREG_EDX;
792 else
793 r = TREG_EAX;
795 vtop->r = r;
796 break;
797 default:
798 opc = 7;
799 goto gen_op8;
803 /* generate a floating point operation 'v = t1 op t2' instruction. The
804 two operands are guaranted to have the same floating point type */
805 /* XXX: need to use ST1 too */
806 void gen_opf(int op)
808 int a, ft, fc, swapped, r;
810 /* convert constants to memory references */
811 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
812 vswap();
813 gv(RC_FLOAT);
814 vswap();
816 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
817 gv(RC_FLOAT);
819 /* must put at least one value in the floating point register */
820 if ((vtop[-1].r & VT_LVAL) &&
821 (vtop[0].r & VT_LVAL)) {
822 vswap();
823 gv(RC_FLOAT);
824 vswap();
826 swapped = 0;
827 /* swap the stack if needed so that t1 is the register and t2 is
828 the memory reference */
829 if (vtop[-1].r & VT_LVAL) {
830 vswap();
831 swapped = 1;
833 if (op >= TOK_ULT && op <= TOK_GT) {
834 /* load on stack second operand */
835 load(TREG_ST0, vtop);
836 save_reg(TREG_EAX); /* eax is used by FP comparison code */
837 if (op == TOK_GE || op == TOK_GT)
838 swapped = !swapped;
839 else if (op == TOK_EQ || op == TOK_NE)
840 swapped = 0;
841 if (swapped)
842 o(0xc9d9); /* fxch %st(1) */
843 o(0xe9da); /* fucompp */
844 o(0xe0df); /* fnstsw %ax */
845 if (op == TOK_EQ) {
846 o(0x45e480); /* and $0x45, %ah */
847 o(0x40fC80); /* cmp $0x40, %ah */
848 } else if (op == TOK_NE) {
849 o(0x45e480); /* and $0x45, %ah */
850 o(0x40f480); /* xor $0x40, %ah */
851 op = TOK_NE;
852 } else if (op == TOK_GE || op == TOK_LE) {
853 o(0x05c4f6); /* test $0x05, %ah */
854 op = TOK_EQ;
855 } else {
856 o(0x45c4f6); /* test $0x45, %ah */
857 op = TOK_EQ;
859 vtop--;
860 vtop->r = VT_CMP;
861 vtop->c.i = op;
862 } else {
863 /* no memory reference possible for long double operations */
864 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
865 load(TREG_ST0, vtop);
866 swapped = !swapped;
869 switch(op) {
870 default:
871 case '+':
872 a = 0;
873 break;
874 case '-':
875 a = 4;
876 if (swapped)
877 a++;
878 break;
879 case '*':
880 a = 1;
881 break;
882 case '/':
883 a = 6;
884 if (swapped)
885 a++;
886 break;
888 ft = vtop->type.t;
889 fc = vtop->c.ul;
890 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
891 o(0xde); /* fxxxp %st, %st(1) */
892 o(0xc1 + (a << 3));
893 } else {
894 /* if saved lvalue, then we must reload it */
895 r = vtop->r;
896 if ((r & VT_VALMASK) == VT_LLOCAL) {
897 SValue v1;
898 r = get_reg(RC_INT);
899 v1.type.t = VT_INT;
900 v1.r = VT_LOCAL | VT_LVAL;
901 v1.c.ul = fc;
902 load(r, &v1);
903 fc = 0;
906 if ((ft & VT_BTYPE) == VT_DOUBLE)
907 o(0xdc);
908 else
909 o(0xd8);
910 gen_modrm(a, r, vtop->sym, fc);
912 vtop--;
916 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
917 and 'long long' cases. */
918 void gen_cvt_itof(int t)
920 save_reg(TREG_ST0);
921 gv(RC_INT);
922 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
923 /* signed long long to float/double/long double (unsigned case
924 is handled generically) */
925 o(0x50 + vtop->r2); /* push r2 */
926 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
927 o(0x242cdf); /* fildll (%esp) */
928 o(0x08c483); /* add $8, %esp */
929 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
930 (VT_INT | VT_UNSIGNED)) {
931 /* unsigned int to float/double/long double */
932 o(0x6a); /* push $0 */
933 g(0x00);
934 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
935 o(0x242cdf); /* fildll (%esp) */
936 o(0x08c483); /* add $8, %esp */
937 } else {
938 /* int to float/double/long double */
939 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
940 o(0x2404db); /* fildl (%esp) */
941 o(0x04c483); /* add $4, %esp */
943 vtop->r = TREG_ST0;
946 /* convert fp to int 't' type */
947 /* XXX: handle long long case */
948 void gen_cvt_ftoi(int t)
950 int r, r2, size;
951 Sym *sym;
952 CType ushort_type;
954 ushort_type.t = VT_SHORT | VT_UNSIGNED;
956 gv(RC_FLOAT);
957 if (t != VT_INT)
958 size = 8;
959 else
960 size = 4;
962 o(0x2dd9); /* ldcw xxx */
963 sym = external_global_sym(TOK___tcc_int_fpu_control,
964 &ushort_type, VT_LVAL);
965 greloc(cur_text_section, sym,
966 ind, R_386_32);
967 gen_le32(0);
969 oad(0xec81, size); /* sub $xxx, %esp */
970 if (size == 4)
971 o(0x1cdb); /* fistpl */
972 else
973 o(0x3cdf); /* fistpll */
974 o(0x24);
975 o(0x2dd9); /* ldcw xxx */
976 sym = external_global_sym(TOK___tcc_fpu_control,
977 &ushort_type, VT_LVAL);
978 greloc(cur_text_section, sym,
979 ind, R_386_32);
980 gen_le32(0);
982 r = get_reg(RC_INT);
983 o(0x58 + r); /* pop r */
984 if (size == 8) {
985 if (t == VT_LLONG) {
986 vtop->r = r; /* mark reg as used */
987 r2 = get_reg(RC_INT);
988 o(0x58 + r2); /* pop r2 */
989 vtop->r2 = r2;
990 } else {
991 o(0x04c483); /* add $4, %esp */
994 vtop->r = r;
997 /* convert from one floating point type to another */
998 void gen_cvt_ftof(int t)
1000 /* all we have to do on i386 is to put the float in a register */
1001 gv(RC_FLOAT);
1004 /* computed goto support */
1005 void ggoto(void)
1007 gcall_or_jmp(1);
1008 vtop--;
1011 /* bound check support functions */
1012 #ifdef CONFIG_TCC_BCHECK
1014 /* generate a bounded pointer addition */
1015 void gen_bounded_ptr_add(void)
1017 Sym *sym;
1019 /* prepare fast i386 function call (args in eax and edx) */
1020 gv2(RC_EAX, RC_EDX);
1021 /* save all temporary registers */
1022 vtop -= 2;
1023 save_regs(0);
1024 /* do a fast function call */
1025 sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
1026 greloc(cur_text_section, sym,
1027 ind + 1, R_386_PC32);
1028 oad(0xe8, -4);
1029 /* returned pointer is in eax */
1030 vtop++;
1031 vtop->r = TREG_EAX | VT_BOUNDED;
1032 /* address of bounding function call point */
1033 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1036 /* patch pointer addition in vtop so that pointer dereferencing is
1037 also tested */
1038 void gen_bounded_ptr_deref(void)
1040 int func;
1041 int size, align;
1042 Elf32_Rel *rel;
1043 Sym *sym;
1045 size = 0;
1046 /* XXX: put that code in generic part of tcc */
1047 if (!is_float(vtop->type.t)) {
1048 if (vtop->r & VT_LVAL_BYTE)
1049 size = 1;
1050 else if (vtop->r & VT_LVAL_SHORT)
1051 size = 2;
1053 if (!size)
1054 size = type_size(&vtop->type, &align);
1055 switch(size) {
1056 case 1: func = TOK___bound_ptr_indir1; break;
1057 case 2: func = TOK___bound_ptr_indir2; break;
1058 case 4: func = TOK___bound_ptr_indir4; break;
1059 case 8: func = TOK___bound_ptr_indir8; break;
1060 case 12: func = TOK___bound_ptr_indir12; break;
1061 case 16: func = TOK___bound_ptr_indir16; break;
1062 default:
1063 error("unhandled size when derefencing bounded pointer");
1064 func = 0;
1065 break;
1068 /* patch relocation */
1069 /* XXX: find a better solution ? */
1070 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1071 sym = external_global_sym(func, &func_old_type, 0);
1072 if (!sym->c)
1073 put_extern_sym(sym, NULL, 0, 0);
1074 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1076 #endif
1078 /* end of X86 code generator */
1079 /*************************************************************/