tccpe: improve dllimport/export and use for tcc_add_symbol
[tinycc.git] / i386-gen.c
blob5ea70dfe978af48358c977cb54ff5b6b39ac2355
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,
48 /* return registers for function */
49 #define REG_IRET TREG_EAX /* single word int return register */
50 #define REG_LRET TREG_EDX /* second word return register (for long long) */
51 #define REG_FRET TREG_ST0 /* float return register */
53 /* defined if function parameters must be evaluated in reverse order */
54 #define INVERT_FUNC_PARAMS
56 /* defined if structures are passed as pointers. Otherwise structures
57 are directly pushed on stack. */
58 //#define FUNC_STRUCT_PARAM_AS_PTR
60 /* pointer size, in bytes */
61 #define PTR_SIZE 4
63 /* long double size and alignment, in bytes */
64 #define LDOUBLE_SIZE 12
65 #define LDOUBLE_ALIGN 4
66 /* maximum alignment (for aligned attribute support) */
67 #define MAX_ALIGN 8
70 #define psym oad
72 /******************************************************/
73 /* ELF defines */
75 #define EM_TCC_TARGET EM_386
77 /* relocation type for 32 bit data relocation */
78 #define R_DATA_32 R_386_32
79 #define R_DATA_PTR R_386_32
80 #define R_JMP_SLOT R_386_JMP_SLOT
81 #define R_COPY R_386_COPY
83 #define ELF_START_ADDR 0x08048000
84 #define ELF_PAGE_SIZE 0x1000
86 /******************************************************/
87 #else /* ! TARGET_DEFS_ONLY */
88 /******************************************************/
89 #include "tcc.h"
91 ST_DATA const int reg_classes[NB_REGS] = {
92 /* eax */ RC_INT | RC_EAX,
93 /* ecx */ RC_INT | RC_ECX,
94 /* edx */ RC_INT | RC_EDX,
95 /* st0 */ RC_FLOAT | RC_ST0,
98 static unsigned long func_sub_sp_offset;
99 static int func_ret_sub;
100 #ifdef CONFIG_TCC_BCHECK
101 static unsigned long func_bound_offset;
102 #endif
104 /* XXX: make it faster ? */
105 ST_FUNC void g(int c)
107 int ind1;
108 ind1 = ind + 1;
109 if (ind1 > cur_text_section->data_allocated)
110 section_realloc(cur_text_section, ind1);
111 cur_text_section->data[ind] = c;
112 ind = ind1;
115 ST_FUNC void o(unsigned int c)
117 while (c) {
118 g(c);
119 c = c >> 8;
123 ST_FUNC void gen_le16(int v)
125 g(v);
126 g(v >> 8);
129 ST_FUNC void gen_le32(int c)
131 g(c);
132 g(c >> 8);
133 g(c >> 16);
134 g(c >> 24);
137 /* output a symbol and patch all calls to it */
138 ST_FUNC void gsym_addr(int t, int a)
140 int n, *ptr;
141 while (t) {
142 ptr = (int *)(cur_text_section->data + t);
143 n = *ptr; /* next value */
144 *ptr = a - t - 4;
145 t = n;
149 ST_FUNC void gsym(int t)
151 gsym_addr(t, ind);
154 /* psym is used to put an instruction with a data field which is a
155 reference to a symbol. It is in fact the same as oad ! */
156 #define psym oad
158 /* instruction + 4 bytes data. Return the address of the data */
159 ST_FUNC int oad(int c, int s)
161 int ind1;
163 o(c);
164 ind1 = ind + 4;
165 if (ind1 > cur_text_section->data_allocated)
166 section_realloc(cur_text_section, ind1);
167 *(int *)(cur_text_section->data + ind) = s;
168 s = ind;
169 ind = ind1;
170 return s;
173 /* output constant with relocation if 'r & VT_SYM' is true */
174 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
176 if (r & VT_SYM)
177 greloc(cur_text_section, sym, ind, R_386_32);
178 gen_le32(c);
181 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
183 if (r & VT_SYM)
184 greloc(cur_text_section, sym, ind, R_386_PC32);
185 gen_le32(c - 4);
188 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
189 opcode bits */
190 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
192 op_reg = op_reg << 3;
193 if ((r & VT_VALMASK) == VT_CONST) {
194 /* constant memory reference */
195 o(0x05 | op_reg);
196 gen_addr32(r, sym, c);
197 } else if ((r & VT_VALMASK) == VT_LOCAL) {
198 /* currently, we use only ebp as base */
199 if (c == (char)c) {
200 /* short reference */
201 o(0x45 | op_reg);
202 g(c);
203 } else {
204 oad(0x85 | op_reg, c);
206 } else {
207 g(0x00 | op_reg | (r & VT_VALMASK));
211 /* load 'r' from value 'sv' */
212 ST_FUNC void load(int r, SValue *sv)
214 int v, t, ft, fc, fr;
215 SValue v1;
217 #ifdef TCC_TARGET_PE
218 SValue v2;
219 sv = pe_getimport(sv, &v2);
220 #endif
222 fr = sv->r;
223 ft = sv->type.t;
224 fc = sv->c.ul;
226 v = fr & VT_VALMASK;
227 if (fr & VT_LVAL) {
228 if (v == VT_LLOCAL) {
229 v1.type.t = VT_INT;
230 v1.r = VT_LOCAL | VT_LVAL;
231 v1.c.ul = fc;
232 load(r, &v1);
233 fr = r;
235 if ((ft & VT_BTYPE) == VT_FLOAT) {
236 o(0xd9); /* flds */
237 r = 0;
238 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
239 o(0xdd); /* fldl */
240 r = 0;
241 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
242 o(0xdb); /* fldt */
243 r = 5;
244 } else if ((ft & VT_TYPE) == VT_BYTE) {
245 o(0xbe0f); /* movsbl */
246 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
247 o(0xb60f); /* movzbl */
248 } else if ((ft & VT_TYPE) == VT_SHORT) {
249 o(0xbf0f); /* movswl */
250 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
251 o(0xb70f); /* movzwl */
252 } else {
253 o(0x8b); /* movl */
255 gen_modrm(r, fr, sv->sym, fc);
256 } else {
257 if (v == VT_CONST) {
258 o(0xb8 + r); /* mov $xx, r */
259 gen_addr32(fr, sv->sym, fc);
260 } else if (v == VT_LOCAL) {
261 o(0x8d); /* lea xxx(%ebp), r */
262 gen_modrm(r, VT_LOCAL, sv->sym, fc);
263 } else if (v == VT_CMP) {
264 oad(0xb8 + r, 0); /* mov $0, r */
265 o(0x0f); /* setxx %br */
266 o(fc);
267 o(0xc0 + r);
268 } else if (v == VT_JMP || v == VT_JMPI) {
269 t = v & 1;
270 oad(0xb8 + r, t); /* mov $1, r */
271 o(0x05eb); /* jmp after */
272 gsym(fc);
273 oad(0xb8 + r, t ^ 1); /* mov $0, r */
274 } else if (v != r) {
275 o(0x89);
276 o(0xc0 + r + v * 8); /* mov v, r */
281 /* store register 'r' in lvalue 'v' */
282 ST_FUNC void store(int r, SValue *v)
284 int fr, bt, ft, fc;
286 #ifdef TCC_TARGET_PE
287 SValue v2;
288 v = pe_getimport(v, &v2);
289 #endif
291 ft = v->type.t;
292 fc = v->c.ul;
293 fr = v->r & VT_VALMASK;
294 bt = ft & VT_BTYPE;
295 /* XXX: incorrect if float reg to reg */
296 if (bt == VT_FLOAT) {
297 o(0xd9); /* fsts */
298 r = 2;
299 } else if (bt == VT_DOUBLE) {
300 o(0xdd); /* fstpl */
301 r = 2;
302 } else if (bt == VT_LDOUBLE) {
303 o(0xc0d9); /* fld %st(0) */
304 o(0xdb); /* fstpt */
305 r = 7;
306 } else {
307 if (bt == VT_SHORT)
308 o(0x66);
309 if (bt == VT_BYTE || bt == VT_BOOL)
310 o(0x88);
311 else
312 o(0x89);
314 if (fr == VT_CONST ||
315 fr == VT_LOCAL ||
316 (v->r & VT_LVAL)) {
317 gen_modrm(r, v->r, v->sym, fc);
318 } else if (fr != r) {
319 o(0xc0 + fr + r * 8); /* mov r, fr */
323 static void gadd_sp(int val)
325 if (val == (char)val) {
326 o(0xc483);
327 g(val);
328 } else {
329 oad(0xc481, val); /* add $xxx, %esp */
333 /* 'is_jmp' is '1' if it is a jump */
334 static void gcall_or_jmp(int is_jmp)
336 int r;
337 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
338 /* constant case */
339 if (vtop->r & VT_SYM) {
340 /* relocation case */
341 greloc(cur_text_section, vtop->sym,
342 ind + 1, R_386_PC32);
343 } else {
344 /* put an empty PC32 relocation */
345 put_elf_reloc(symtab_section, cur_text_section,
346 ind + 1, R_386_PC32, 0);
348 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
349 } else {
350 /* otherwise, indirect call */
351 r = gv(RC_INT);
352 o(0xff); /* call/jmp *r */
353 o(0xd0 + r + (is_jmp << 4));
357 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
358 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
360 /* Generate function call. The function address is pushed first, then
361 all the parameters in call order. This functions pops all the
362 parameters and the function address. */
363 ST_FUNC void gfunc_call(int nb_args)
365 int size, align, r, args_size, i, func_call;
366 Sym *func_sym;
368 args_size = 0;
369 for(i = 0;i < nb_args; i++) {
370 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
371 size = type_size(&vtop->type, &align);
372 /* align to stack align size */
373 size = (size + 3) & ~3;
374 /* allocate the necessary size on stack */
375 oad(0xec81, size); /* sub $xxx, %esp */
376 /* generate structure store */
377 r = get_reg(RC_INT);
378 o(0x89); /* mov %esp, r */
379 o(0xe0 + r);
380 vset(&vtop->type, r | VT_LVAL, 0);
381 vswap();
382 vstore();
383 args_size += size;
384 } else if (is_float(vtop->type.t)) {
385 gv(RC_FLOAT); /* only one float register */
386 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
387 size = 4;
388 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
389 size = 8;
390 else
391 size = 12;
392 oad(0xec81, size); /* sub $xxx, %esp */
393 if (size == 12)
394 o(0x7cdb);
395 else
396 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
397 g(0x24);
398 g(0x00);
399 args_size += size;
400 } else {
401 /* simple type (currently always same size) */
402 /* XXX: implicit cast ? */
403 r = gv(RC_INT);
404 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
405 size = 8;
406 o(0x50 + vtop->r2); /* push r */
407 } else {
408 size = 4;
410 o(0x50 + r); /* push r */
411 args_size += size;
413 vtop--;
415 save_regs(0); /* save used temporary registers */
416 func_sym = vtop->type.ref;
417 func_call = FUNC_CALL(func_sym->r);
418 /* fast call case */
419 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
420 func_call == FUNC_FASTCALLW) {
421 int fastcall_nb_regs;
422 uint8_t *fastcall_regs_ptr;
423 if (func_call == FUNC_FASTCALLW) {
424 fastcall_regs_ptr = fastcallw_regs;
425 fastcall_nb_regs = 2;
426 } else {
427 fastcall_regs_ptr = fastcall_regs;
428 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
430 for(i = 0;i < fastcall_nb_regs; i++) {
431 if (args_size <= 0)
432 break;
433 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
434 /* XXX: incorrect for struct/floats */
435 args_size -= 4;
438 gcall_or_jmp(0);
440 #ifdef TCC_TARGET_PE
441 if ((func_sym->type.t & VT_BTYPE) == VT_STRUCT)
442 args_size -= 4;
443 #endif
444 if (args_size && func_call != FUNC_STDCALL)
445 gadd_sp(args_size);
446 vtop--;
449 #ifdef TCC_TARGET_PE
450 #define FUNC_PROLOG_SIZE 10
451 #else
452 #define FUNC_PROLOG_SIZE 9
453 #endif
455 /* generate function prolog of type 't' */
456 ST_FUNC void gfunc_prolog(CType *func_type)
458 int addr, align, size, func_call, fastcall_nb_regs;
459 int param_index, param_addr;
460 uint8_t *fastcall_regs_ptr;
461 Sym *sym;
462 CType *type;
464 sym = func_type->ref;
465 func_call = FUNC_CALL(sym->r);
466 addr = 8;
467 loc = 0;
468 func_vc = 0;
470 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
471 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
472 fastcall_regs_ptr = fastcall_regs;
473 } else if (func_call == FUNC_FASTCALLW) {
474 fastcall_nb_regs = 2;
475 fastcall_regs_ptr = fastcallw_regs;
476 } else {
477 fastcall_nb_regs = 0;
478 fastcall_regs_ptr = NULL;
480 param_index = 0;
482 ind += FUNC_PROLOG_SIZE;
483 func_sub_sp_offset = ind;
484 /* if the function returns a structure, then add an
485 implicit pointer parameter */
486 func_vt = sym->type;
487 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
488 /* XXX: fastcall case ? */
489 func_vc = addr;
490 addr += 4;
491 param_index++;
493 /* define parameters */
494 while ((sym = sym->next) != NULL) {
495 type = &sym->type;
496 size = type_size(type, &align);
497 size = (size + 3) & ~3;
498 #ifdef FUNC_STRUCT_PARAM_AS_PTR
499 /* structs are passed as pointer */
500 if ((type->t & VT_BTYPE) == VT_STRUCT) {
501 size = 4;
503 #endif
504 if (param_index < fastcall_nb_regs) {
505 /* save FASTCALL register */
506 loc -= 4;
507 o(0x89); /* movl */
508 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
509 param_addr = loc;
510 } else {
511 param_addr = addr;
512 addr += size;
514 sym_push(sym->v & ~SYM_FIELD, type,
515 VT_LOCAL | lvalue_type(type->t), param_addr);
516 param_index++;
518 func_ret_sub = 0;
519 /* pascal type call ? */
520 if (func_call == FUNC_STDCALL)
521 func_ret_sub = addr - 8;
522 #ifdef TCC_TARGET_PE
523 else if (func_vc)
524 func_ret_sub = 4;
525 #endif
527 #ifdef CONFIG_TCC_BCHECK
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;
534 #endif
537 /* generate function epilog */
538 ST_FUNC void gfunc_epilog(void)
540 int v, saved_ind;
542 #ifdef CONFIG_TCC_BCHECK
543 if (tcc_state->do_bounds_check
544 && func_bound_offset != lbounds_section->data_offset) {
545 int saved_ind;
546 int *bounds_ptr;
547 Sym *sym, *sym_data;
548 /* add end of table info */
549 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
550 *bounds_ptr = 0;
551 /* generate bound local allocation */
552 saved_ind = ind;
553 ind = func_sub_sp_offset;
554 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
555 func_bound_offset, lbounds_section->data_offset);
556 greloc(cur_text_section, sym_data,
557 ind + 1, R_386_32);
558 oad(0xb8, 0); /* mov %eax, xxx */
559 sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0);
560 greloc(cur_text_section, sym,
561 ind + 1, R_386_PC32);
562 oad(0xe8, -4);
563 ind = saved_ind;
564 /* generate bound check local freeing */
565 o(0x5250); /* save returned value, if any */
566 greloc(cur_text_section, sym_data,
567 ind + 1, R_386_32);
568 oad(0xb8, 0); /* mov %eax, xxx */
569 sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0);
570 greloc(cur_text_section, sym,
571 ind + 1, R_386_PC32);
572 oad(0xe8, -4);
573 o(0x585a); /* restore returned value, if any */
575 #endif
576 o(0xc9); /* leave */
577 if (func_ret_sub == 0) {
578 o(0xc3); /* ret */
579 } else {
580 o(0xc2); /* ret n */
581 g(func_ret_sub);
582 g(func_ret_sub >> 8);
584 /* align local size to word & save local variables */
586 v = (-loc + 3) & -4;
587 saved_ind = ind;
588 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
589 #ifdef TCC_TARGET_PE
590 if (v >= 4096) {
591 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
592 oad(0xb8, v); /* mov stacksize, %eax */
593 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
594 greloc(cur_text_section, sym, ind-4, R_386_PC32);
595 } else
596 #endif
598 o(0xe58955); /* push %ebp, mov %esp, %ebp */
599 o(0xec81); /* sub esp, stacksize */
600 gen_le32(v);
601 #if FUNC_PROLOG_SIZE == 10
602 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
603 #endif
605 ind = saved_ind;
608 /* generate a jump to a label */
609 ST_FUNC int gjmp(int t)
611 return psym(0xe9, t);
614 /* generate a jump to a fixed address */
615 ST_FUNC void gjmp_addr(int a)
617 int r;
618 r = a - ind - 2;
619 if (r == (char)r) {
620 g(0xeb);
621 g(r);
622 } else {
623 oad(0xe9, a - ind - 5);
627 /* generate a test. set 'inv' to invert test. Stack entry is popped */
628 ST_FUNC int gtst(int inv, int t)
630 int v, *p;
632 v = vtop->r & VT_VALMASK;
633 if (v == VT_CMP) {
634 /* fast case : can jump directly since flags are set */
635 g(0x0f);
636 t = psym((vtop->c.i - 16) ^ inv, t);
637 } else if (v == VT_JMP || v == VT_JMPI) {
638 /* && or || optimization */
639 if ((v & 1) == inv) {
640 /* insert vtop->c jump list in t */
641 p = &vtop->c.i;
642 while (*p != 0)
643 p = (int *)(cur_text_section->data + *p);
644 *p = t;
645 t = vtop->c.i;
646 } else {
647 t = gjmp(t);
648 gsym(vtop->c.i);
650 } else {
651 if (is_float(vtop->type.t) ||
652 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
653 vpushi(0);
654 gen_op(TOK_NE);
656 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
657 /* constant jmp optimization */
658 if ((vtop->c.i != 0) != inv)
659 t = gjmp(t);
660 } else {
661 v = gv(RC_INT);
662 o(0x85);
663 o(0xc0 + v * 9);
664 g(0x0f);
665 t = psym(0x85 ^ inv, t);
668 vtop--;
669 return t;
672 /* generate an integer binary operation */
673 ST_FUNC void gen_opi(int op)
675 int r, fr, opc, c;
677 switch(op) {
678 case '+':
679 case TOK_ADDC1: /* add with carry generation */
680 opc = 0;
681 gen_op8:
682 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
683 /* constant case */
684 vswap();
685 r = gv(RC_INT);
686 vswap();
687 c = vtop->c.i;
688 if (c == (char)c) {
689 /* XXX: generate inc and dec for smaller code ? */
690 o(0x83);
691 o(0xc0 | (opc << 3) | r);
692 g(c);
693 } else {
694 o(0x81);
695 oad(0xc0 | (opc << 3) | r, c);
697 } else {
698 gv2(RC_INT, RC_INT);
699 r = vtop[-1].r;
700 fr = vtop[0].r;
701 o((opc << 3) | 0x01);
702 o(0xc0 + r + fr * 8);
704 vtop--;
705 if (op >= TOK_ULT && op <= TOK_GT) {
706 vtop->r = VT_CMP;
707 vtop->c.i = op;
709 break;
710 case '-':
711 case TOK_SUBC1: /* sub with carry generation */
712 opc = 5;
713 goto gen_op8;
714 case TOK_ADDC2: /* add with carry use */
715 opc = 2;
716 goto gen_op8;
717 case TOK_SUBC2: /* sub with carry use */
718 opc = 3;
719 goto gen_op8;
720 case '&':
721 opc = 4;
722 goto gen_op8;
723 case '^':
724 opc = 6;
725 goto gen_op8;
726 case '|':
727 opc = 1;
728 goto gen_op8;
729 case '*':
730 gv2(RC_INT, RC_INT);
731 r = vtop[-1].r;
732 fr = vtop[0].r;
733 vtop--;
734 o(0xaf0f); /* imul fr, r */
735 o(0xc0 + fr + r * 8);
736 break;
737 case TOK_SHL:
738 opc = 4;
739 goto gen_shift;
740 case TOK_SHR:
741 opc = 5;
742 goto gen_shift;
743 case TOK_SAR:
744 opc = 7;
745 gen_shift:
746 opc = 0xc0 | (opc << 3);
747 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
748 /* constant case */
749 vswap();
750 r = gv(RC_INT);
751 vswap();
752 c = vtop->c.i & 0x1f;
753 o(0xc1); /* shl/shr/sar $xxx, r */
754 o(opc | r);
755 g(c);
756 } else {
757 /* we generate the shift in ecx */
758 gv2(RC_INT, RC_ECX);
759 r = vtop[-1].r;
760 o(0xd3); /* shl/shr/sar %cl, r */
761 o(opc | r);
763 vtop--;
764 break;
765 case '/':
766 case TOK_UDIV:
767 case TOK_PDIV:
768 case '%':
769 case TOK_UMOD:
770 case TOK_UMULL:
771 /* first operand must be in eax */
772 /* XXX: need better constraint for second operand */
773 gv2(RC_EAX, RC_ECX);
774 r = vtop[-1].r;
775 fr = vtop[0].r;
776 vtop--;
777 save_reg(TREG_EDX);
778 if (op == TOK_UMULL) {
779 o(0xf7); /* mul fr */
780 o(0xe0 + fr);
781 vtop->r2 = TREG_EDX;
782 r = TREG_EAX;
783 } else {
784 if (op == TOK_UDIV || op == TOK_UMOD) {
785 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
786 o(0xf0 + fr);
787 } else {
788 o(0xf799); /* cltd, idiv fr, %eax */
789 o(0xf8 + fr);
791 if (op == '%' || op == TOK_UMOD)
792 r = TREG_EDX;
793 else
794 r = TREG_EAX;
796 vtop->r = r;
797 break;
798 default:
799 opc = 7;
800 goto gen_op8;
804 /* generate a floating point operation 'v = t1 op t2' instruction. The
805 two operands are guaranted to have the same floating point type */
806 /* XXX: need to use ST1 too */
807 ST_FUNC void gen_opf(int op)
809 int a, ft, fc, swapped, r;
811 /* convert constants to memory references */
812 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
813 vswap();
814 gv(RC_FLOAT);
815 vswap();
817 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
818 gv(RC_FLOAT);
820 /* must put at least one value in the floating point register */
821 if ((vtop[-1].r & VT_LVAL) &&
822 (vtop[0].r & VT_LVAL)) {
823 vswap();
824 gv(RC_FLOAT);
825 vswap();
827 swapped = 0;
828 /* swap the stack if needed so that t1 is the register and t2 is
829 the memory reference */
830 if (vtop[-1].r & VT_LVAL) {
831 vswap();
832 swapped = 1;
834 if (op >= TOK_ULT && op <= TOK_GT) {
835 /* load on stack second operand */
836 load(TREG_ST0, vtop);
837 save_reg(TREG_EAX); /* eax is used by FP comparison code */
838 if (op == TOK_GE || op == TOK_GT)
839 swapped = !swapped;
840 else if (op == TOK_EQ || op == TOK_NE)
841 swapped = 0;
842 if (swapped)
843 o(0xc9d9); /* fxch %st(1) */
844 o(0xe9da); /* fucompp */
845 o(0xe0df); /* fnstsw %ax */
846 if (op == TOK_EQ) {
847 o(0x45e480); /* and $0x45, %ah */
848 o(0x40fC80); /* cmp $0x40, %ah */
849 } else if (op == TOK_NE) {
850 o(0x45e480); /* and $0x45, %ah */
851 o(0x40f480); /* xor $0x40, %ah */
852 op = TOK_NE;
853 } else if (op == TOK_GE || op == TOK_LE) {
854 o(0x05c4f6); /* test $0x05, %ah */
855 op = TOK_EQ;
856 } else {
857 o(0x45c4f6); /* test $0x45, %ah */
858 op = TOK_EQ;
860 vtop--;
861 vtop->r = VT_CMP;
862 vtop->c.i = op;
863 } else {
864 /* no memory reference possible for long double operations */
865 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
866 load(TREG_ST0, vtop);
867 swapped = !swapped;
870 switch(op) {
871 default:
872 case '+':
873 a = 0;
874 break;
875 case '-':
876 a = 4;
877 if (swapped)
878 a++;
879 break;
880 case '*':
881 a = 1;
882 break;
883 case '/':
884 a = 6;
885 if (swapped)
886 a++;
887 break;
889 ft = vtop->type.t;
890 fc = vtop->c.ul;
891 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
892 o(0xde); /* fxxxp %st, %st(1) */
893 o(0xc1 + (a << 3));
894 } else {
895 /* if saved lvalue, then we must reload it */
896 r = vtop->r;
897 if ((r & VT_VALMASK) == VT_LLOCAL) {
898 SValue v1;
899 r = get_reg(RC_INT);
900 v1.type.t = VT_INT;
901 v1.r = VT_LOCAL | VT_LVAL;
902 v1.c.ul = fc;
903 load(r, &v1);
904 fc = 0;
907 if ((ft & VT_BTYPE) == VT_DOUBLE)
908 o(0xdc);
909 else
910 o(0xd8);
911 gen_modrm(a, r, vtop->sym, fc);
913 vtop--;
917 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
918 and 'long long' cases. */
919 ST_FUNC void gen_cvt_itof(int t)
921 save_reg(TREG_ST0);
922 gv(RC_INT);
923 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
924 /* signed long long to float/double/long double (unsigned case
925 is handled generically) */
926 o(0x50 + vtop->r2); /* push r2 */
927 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
928 o(0x242cdf); /* fildll (%esp) */
929 o(0x08c483); /* add $8, %esp */
930 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
931 (VT_INT | VT_UNSIGNED)) {
932 /* unsigned int to float/double/long double */
933 o(0x6a); /* push $0 */
934 g(0x00);
935 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
936 o(0x242cdf); /* fildll (%esp) */
937 o(0x08c483); /* add $8, %esp */
938 } else {
939 /* int to float/double/long double */
940 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
941 o(0x2404db); /* fildl (%esp) */
942 o(0x04c483); /* add $4, %esp */
944 vtop->r = TREG_ST0;
947 /* convert fp to int 't' type */
948 /* XXX: handle long long case */
949 ST_FUNC void gen_cvt_ftoi(int t)
951 int r, r2, size;
952 Sym *sym;
953 CType ushort_type;
955 ushort_type.t = VT_SHORT | VT_UNSIGNED;
956 ushort_type.ref = 0;
958 gv(RC_FLOAT);
959 if (t != VT_INT)
960 size = 8;
961 else
962 size = 4;
964 o(0x2dd9); /* ldcw xxx */
965 sym = external_global_sym(TOK___tcc_int_fpu_control,
966 &ushort_type, VT_LVAL);
967 greloc(cur_text_section, sym,
968 ind, R_386_32);
969 gen_le32(0);
971 oad(0xec81, size); /* sub $xxx, %esp */
972 if (size == 4)
973 o(0x1cdb); /* fistpl */
974 else
975 o(0x3cdf); /* fistpll */
976 o(0x24);
977 o(0x2dd9); /* ldcw xxx */
978 sym = external_global_sym(TOK___tcc_fpu_control,
979 &ushort_type, VT_LVAL);
980 greloc(cur_text_section, sym,
981 ind, R_386_32);
982 gen_le32(0);
984 r = get_reg(RC_INT);
985 o(0x58 + r); /* pop r */
986 if (size == 8) {
987 if (t == VT_LLONG) {
988 vtop->r = r; /* mark reg as used */
989 r2 = get_reg(RC_INT);
990 o(0x58 + r2); /* pop r2 */
991 vtop->r2 = r2;
992 } else {
993 o(0x04c483); /* add $4, %esp */
996 vtop->r = r;
999 /* convert from one floating point type to another */
1000 ST_FUNC void gen_cvt_ftof(int t)
1002 /* all we have to do on i386 is to put the float in a register */
1003 gv(RC_FLOAT);
1006 /* computed goto support */
1007 ST_FUNC void ggoto(void)
1009 gcall_or_jmp(1);
1010 vtop--;
1013 /* bound check support functions */
1014 #ifdef CONFIG_TCC_BCHECK
1016 /* generate a bounded pointer addition */
1017 ST_FUNC void gen_bounded_ptr_add(void)
1019 Sym *sym;
1021 /* prepare fast i386 function call (args in eax and edx) */
1022 gv2(RC_EAX, RC_EDX);
1023 /* save all temporary registers */
1024 vtop -= 2;
1025 save_regs(0);
1026 /* do a fast function call */
1027 sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
1028 greloc(cur_text_section, sym,
1029 ind + 1, R_386_PC32);
1030 oad(0xe8, -4);
1031 /* returned pointer is in eax */
1032 vtop++;
1033 vtop->r = TREG_EAX | VT_BOUNDED;
1034 /* address of bounding function call point */
1035 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1038 /* patch pointer addition in vtop so that pointer dereferencing is
1039 also tested */
1040 ST_FUNC void gen_bounded_ptr_deref(void)
1042 int func;
1043 int size, align;
1044 Elf32_Rel *rel;
1045 Sym *sym;
1047 size = 0;
1048 /* XXX: put that code in generic part of tcc */
1049 if (!is_float(vtop->type.t)) {
1050 if (vtop->r & VT_LVAL_BYTE)
1051 size = 1;
1052 else if (vtop->r & VT_LVAL_SHORT)
1053 size = 2;
1055 if (!size)
1056 size = type_size(&vtop->type, &align);
1057 switch(size) {
1058 case 1: func = TOK___bound_ptr_indir1; break;
1059 case 2: func = TOK___bound_ptr_indir2; break;
1060 case 4: func = TOK___bound_ptr_indir4; break;
1061 case 8: func = TOK___bound_ptr_indir8; break;
1062 case 12: func = TOK___bound_ptr_indir12; break;
1063 case 16: func = TOK___bound_ptr_indir16; break;
1064 default:
1065 error("unhandled size when derefencing bounded pointer");
1066 func = 0;
1067 break;
1070 /* patch relocation */
1071 /* XXX: find a better solution ? */
1072 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1073 sym = external_global_sym(func, &func_old_type, 0);
1074 if (!sym->c)
1075 put_extern_sym(sym, NULL, 0, 0);
1076 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1078 #endif
1080 /* end of X86 code generator */
1081 /*************************************************************/
1082 #endif
1083 /*************************************************************/