-Wno-unused-result now added only on gcc >= 4.4
[tinycc.git] / i386-gen.c
blob66355598cbc0c7d256889ddde85c9bf11e9a5c8a
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 fr = r;
233 if (!(reg_classes[fr] & RC_INT))
234 fr = get_reg(RC_INT);
235 load(fr, &v1);
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 ST_FUNC void store(int r, SValue *v)
286 int fr, bt, ft, fc;
288 #ifdef TCC_TARGET_PE
289 SValue v2;
290 v = pe_getimport(v, &v2);
291 #endif
293 ft = v->type.t;
294 fc = v->c.ul;
295 fr = v->r & VT_VALMASK;
296 bt = ft & VT_BTYPE;
297 /* XXX: incorrect if float reg to reg */
298 if (bt == VT_FLOAT) {
299 o(0xd9); /* fsts */
300 r = 2;
301 } else if (bt == VT_DOUBLE) {
302 o(0xdd); /* fstpl */
303 r = 2;
304 } else if (bt == VT_LDOUBLE) {
305 o(0xc0d9); /* fld %st(0) */
306 o(0xdb); /* fstpt */
307 r = 7;
308 } else {
309 if (bt == VT_SHORT)
310 o(0x66);
311 if (bt == VT_BYTE || bt == VT_BOOL)
312 o(0x88);
313 else
314 o(0x89);
316 if (fr == VT_CONST ||
317 fr == VT_LOCAL ||
318 (v->r & VT_LVAL)) {
319 gen_modrm(r, v->r, v->sym, fc);
320 } else if (fr != r) {
321 o(0xc0 + fr + r * 8); /* mov r, fr */
325 static void gadd_sp(int val)
327 if (val == (char)val) {
328 o(0xc483);
329 g(val);
330 } else {
331 oad(0xc481, val); /* add $xxx, %esp */
335 /* 'is_jmp' is '1' if it is a jump */
336 static void gcall_or_jmp(int is_jmp)
338 int r;
339 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
340 /* constant case */
341 if (vtop->r & VT_SYM) {
342 /* relocation case */
343 greloc(cur_text_section, vtop->sym,
344 ind + 1, R_386_PC32);
345 } else {
346 /* put an empty PC32 relocation */
347 put_elf_reloc(symtab_section, cur_text_section,
348 ind + 1, R_386_PC32, 0);
350 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
351 } else {
352 /* otherwise, indirect call */
353 r = gv(RC_INT);
354 o(0xff); /* call/jmp *r */
355 o(0xd0 + r + (is_jmp << 4));
359 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
360 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
362 /* Generate function call. The function address is pushed first, then
363 all the parameters in call order. This functions pops all the
364 parameters and the function address. */
365 ST_FUNC void gfunc_call(int nb_args)
367 int size, align, r, args_size, i, func_call;
368 Sym *func_sym;
370 args_size = 0;
371 for(i = 0;i < nb_args; i++) {
372 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
373 size = type_size(&vtop->type, &align);
374 /* align to stack align size */
375 size = (size + 3) & ~3;
376 /* allocate the necessary size on stack */
377 oad(0xec81, size); /* sub $xxx, %esp */
378 /* generate structure store */
379 r = get_reg(RC_INT);
380 o(0x89); /* mov %esp, r */
381 o(0xe0 + r);
382 vset(&vtop->type, r | VT_LVAL, 0);
383 vswap();
384 vstore();
385 args_size += size;
386 } else if (is_float(vtop->type.t)) {
387 gv(RC_FLOAT); /* only one float register */
388 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
389 size = 4;
390 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
391 size = 8;
392 else
393 size = 12;
394 oad(0xec81, size); /* sub $xxx, %esp */
395 if (size == 12)
396 o(0x7cdb);
397 else
398 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
399 g(0x24);
400 g(0x00);
401 args_size += size;
402 } else {
403 /* simple type (currently always same size) */
404 /* XXX: implicit cast ? */
405 r = gv(RC_INT);
406 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
407 size = 8;
408 o(0x50 + vtop->r2); /* push r */
409 } else {
410 size = 4;
412 o(0x50 + r); /* push r */
413 args_size += size;
415 vtop--;
417 save_regs(0); /* save used temporary registers */
418 func_sym = vtop->type.ref;
419 func_call = FUNC_CALL(func_sym->r);
420 /* fast call case */
421 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
422 func_call == FUNC_FASTCALLW) {
423 int fastcall_nb_regs;
424 uint8_t *fastcall_regs_ptr;
425 if (func_call == FUNC_FASTCALLW) {
426 fastcall_regs_ptr = fastcallw_regs;
427 fastcall_nb_regs = 2;
428 } else {
429 fastcall_regs_ptr = fastcall_regs;
430 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
432 for(i = 0;i < fastcall_nb_regs; i++) {
433 if (args_size <= 0)
434 break;
435 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
436 /* XXX: incorrect for struct/floats */
437 args_size -= 4;
440 gcall_or_jmp(0);
442 #ifdef TCC_TARGET_PE
443 if ((func_sym->type.t & VT_BTYPE) == VT_STRUCT)
444 args_size -= 4;
445 #endif
446 if (args_size && func_call != FUNC_STDCALL)
447 gadd_sp(args_size);
448 vtop--;
451 #ifdef TCC_TARGET_PE
452 #define FUNC_PROLOG_SIZE 10
453 #else
454 #define FUNC_PROLOG_SIZE 9
455 #endif
457 /* generate function prolog of type 't' */
458 ST_FUNC void gfunc_prolog(CType *func_type)
460 int addr, align, size, func_call, fastcall_nb_regs;
461 int param_index, param_addr;
462 uint8_t *fastcall_regs_ptr;
463 Sym *sym;
464 CType *type;
466 sym = func_type->ref;
467 func_call = FUNC_CALL(sym->r);
468 addr = 8;
469 loc = 0;
470 func_vc = 0;
472 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
473 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
474 fastcall_regs_ptr = fastcall_regs;
475 } else if (func_call == FUNC_FASTCALLW) {
476 fastcall_nb_regs = 2;
477 fastcall_regs_ptr = fastcallw_regs;
478 } else {
479 fastcall_nb_regs = 0;
480 fastcall_regs_ptr = NULL;
482 param_index = 0;
484 ind += FUNC_PROLOG_SIZE;
485 func_sub_sp_offset = ind;
486 /* if the function returns a structure, then add an
487 implicit pointer parameter */
488 func_vt = sym->type;
489 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
490 /* XXX: fastcall case ? */
491 func_vc = addr;
492 addr += 4;
493 param_index++;
495 /* define parameters */
496 while ((sym = sym->next) != NULL) {
497 type = &sym->type;
498 size = type_size(type, &align);
499 size = (size + 3) & ~3;
500 #ifdef FUNC_STRUCT_PARAM_AS_PTR
501 /* structs are passed as pointer */
502 if ((type->t & VT_BTYPE) == VT_STRUCT) {
503 size = 4;
505 #endif
506 if (param_index < fastcall_nb_regs) {
507 /* save FASTCALL register */
508 loc -= 4;
509 o(0x89); /* movl */
510 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
511 param_addr = loc;
512 } else {
513 param_addr = addr;
514 addr += size;
516 sym_push(sym->v & ~SYM_FIELD, type,
517 VT_LOCAL | lvalue_type(type->t), param_addr);
518 param_index++;
520 func_ret_sub = 0;
521 /* pascal type call ? */
522 if (func_call == FUNC_STDCALL)
523 func_ret_sub = addr - 8;
524 #ifdef TCC_TARGET_PE
525 else if (func_vc)
526 func_ret_sub = 4;
527 #endif
529 #ifdef CONFIG_TCC_BCHECK
530 /* leave some room for bound checking code */
531 if (tcc_state->do_bounds_check) {
532 oad(0xb8, 0); /* lbound section pointer */
533 oad(0xb8, 0); /* call to function */
534 func_bound_offset = lbounds_section->data_offset;
536 #endif
539 /* generate function epilog */
540 ST_FUNC void gfunc_epilog(void)
542 int v, saved_ind;
544 #ifdef CONFIG_TCC_BCHECK
545 if (tcc_state->do_bounds_check
546 && func_bound_offset != lbounds_section->data_offset) {
547 int saved_ind;
548 int *bounds_ptr;
549 Sym *sym, *sym_data;
550 /* add end of table info */
551 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
552 *bounds_ptr = 0;
553 /* generate bound local allocation */
554 saved_ind = ind;
555 ind = func_sub_sp_offset;
556 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
557 func_bound_offset, lbounds_section->data_offset);
558 greloc(cur_text_section, sym_data,
559 ind + 1, R_386_32);
560 oad(0xb8, 0); /* mov %eax, xxx */
561 sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0);
562 greloc(cur_text_section, sym,
563 ind + 1, R_386_PC32);
564 oad(0xe8, -4);
565 ind = saved_ind;
566 /* generate bound check local freeing */
567 o(0x5250); /* save returned value, if any */
568 greloc(cur_text_section, sym_data,
569 ind + 1, R_386_32);
570 oad(0xb8, 0); /* mov %eax, xxx */
571 sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0);
572 greloc(cur_text_section, sym,
573 ind + 1, R_386_PC32);
574 oad(0xe8, -4);
575 o(0x585a); /* restore returned value, if any */
577 #endif
578 o(0xc9); /* leave */
579 if (func_ret_sub == 0) {
580 o(0xc3); /* ret */
581 } else {
582 o(0xc2); /* ret n */
583 g(func_ret_sub);
584 g(func_ret_sub >> 8);
586 /* align local size to word & save local variables */
588 v = (-loc + 3) & -4;
589 saved_ind = ind;
590 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
591 #ifdef TCC_TARGET_PE
592 if (v >= 4096) {
593 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
594 oad(0xb8, v); /* mov stacksize, %eax */
595 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
596 greloc(cur_text_section, sym, ind-4, R_386_PC32);
597 } else
598 #endif
600 o(0xe58955); /* push %ebp, mov %esp, %ebp */
601 o(0xec81); /* sub esp, stacksize */
602 gen_le32(v);
603 #if FUNC_PROLOG_SIZE == 10
604 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
605 #endif
607 ind = saved_ind;
610 /* generate a jump to a label */
611 ST_FUNC int gjmp(int t)
613 return psym(0xe9, t);
616 /* generate a jump to a fixed address */
617 ST_FUNC void gjmp_addr(int a)
619 int r;
620 r = a - ind - 2;
621 if (r == (char)r) {
622 g(0xeb);
623 g(r);
624 } else {
625 oad(0xe9, a - ind - 5);
629 /* generate a test. set 'inv' to invert test. Stack entry is popped */
630 ST_FUNC int gtst(int inv, int t)
632 int v, *p;
634 v = vtop->r & VT_VALMASK;
635 if (v == VT_CMP) {
636 /* fast case : can jump directly since flags are set */
637 g(0x0f);
638 t = psym((vtop->c.i - 16) ^ inv, t);
639 } else if (v == VT_JMP || v == VT_JMPI) {
640 /* && or || optimization */
641 if ((v & 1) == inv) {
642 /* insert vtop->c jump list in t */
643 p = &vtop->c.i;
644 while (*p != 0)
645 p = (int *)(cur_text_section->data + *p);
646 *p = t;
647 t = vtop->c.i;
648 } else {
649 t = gjmp(t);
650 gsym(vtop->c.i);
652 } else {
653 if (is_float(vtop->type.t) ||
654 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
655 vpushi(0);
656 gen_op(TOK_NE);
658 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
659 /* constant jmp optimization */
660 if ((vtop->c.i != 0) != inv)
661 t = gjmp(t);
662 } else {
663 v = gv(RC_INT);
664 o(0x85);
665 o(0xc0 + v * 9);
666 g(0x0f);
667 t = psym(0x85 ^ inv, t);
670 vtop--;
671 return t;
674 /* generate an integer binary operation */
675 ST_FUNC void gen_opi(int op)
677 int r, fr, opc, c;
679 switch(op) {
680 case '+':
681 case TOK_ADDC1: /* add with carry generation */
682 opc = 0;
683 gen_op8:
684 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
685 /* constant case */
686 vswap();
687 r = gv(RC_INT);
688 vswap();
689 c = vtop->c.i;
690 if (c == (char)c) {
691 /* generate inc and dec for smaller code */
692 if (c==1 && opc==0) {
693 o (0x40 | r); // inc
694 } else if (c==1 && opc==5) {
695 o (0x48 | r); // dec
696 } else {
697 o(0x83);
698 o(0xc0 | (opc << 3) | r);
699 g(c);
701 } else {
702 o(0x81);
703 oad(0xc0 | (opc << 3) | r, c);
705 } else {
706 gv2(RC_INT, RC_INT);
707 r = vtop[-1].r;
708 fr = vtop[0].r;
709 o((opc << 3) | 0x01);
710 o(0xc0 + r + fr * 8);
712 vtop--;
713 if (op >= TOK_ULT && op <= TOK_GT) {
714 vtop->r = VT_CMP;
715 vtop->c.i = op;
717 break;
718 case '-':
719 case TOK_SUBC1: /* sub with carry generation */
720 opc = 5;
721 goto gen_op8;
722 case TOK_ADDC2: /* add with carry use */
723 opc = 2;
724 goto gen_op8;
725 case TOK_SUBC2: /* sub with carry use */
726 opc = 3;
727 goto gen_op8;
728 case '&':
729 opc = 4;
730 goto gen_op8;
731 case '^':
732 opc = 6;
733 goto gen_op8;
734 case '|':
735 opc = 1;
736 goto gen_op8;
737 case '*':
738 gv2(RC_INT, RC_INT);
739 r = vtop[-1].r;
740 fr = vtop[0].r;
741 vtop--;
742 o(0xaf0f); /* imul fr, r */
743 o(0xc0 + fr + r * 8);
744 break;
745 case TOK_SHL:
746 opc = 4;
747 goto gen_shift;
748 case TOK_SHR:
749 opc = 5;
750 goto gen_shift;
751 case TOK_SAR:
752 opc = 7;
753 gen_shift:
754 opc = 0xc0 | (opc << 3);
755 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
756 /* constant case */
757 vswap();
758 r = gv(RC_INT);
759 vswap();
760 c = vtop->c.i & 0x1f;
761 o(0xc1); /* shl/shr/sar $xxx, r */
762 o(opc | r);
763 g(c);
764 } else {
765 /* we generate the shift in ecx */
766 gv2(RC_INT, RC_ECX);
767 r = vtop[-1].r;
768 o(0xd3); /* shl/shr/sar %cl, r */
769 o(opc | r);
771 vtop--;
772 break;
773 case '/':
774 case TOK_UDIV:
775 case TOK_PDIV:
776 case '%':
777 case TOK_UMOD:
778 case TOK_UMULL:
779 /* first operand must be in eax */
780 /* XXX: need better constraint for second operand */
781 gv2(RC_EAX, RC_ECX);
782 r = vtop[-1].r;
783 fr = vtop[0].r;
784 vtop--;
785 save_reg(TREG_EDX);
786 if (op == TOK_UMULL) {
787 o(0xf7); /* mul fr */
788 o(0xe0 + fr);
789 vtop->r2 = TREG_EDX;
790 r = TREG_EAX;
791 } else {
792 if (op == TOK_UDIV || op == TOK_UMOD) {
793 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
794 o(0xf0 + fr);
795 } else {
796 o(0xf799); /* cltd, idiv fr, %eax */
797 o(0xf8 + fr);
799 if (op == '%' || op == TOK_UMOD)
800 r = TREG_EDX;
801 else
802 r = TREG_EAX;
804 vtop->r = r;
805 break;
806 default:
807 opc = 7;
808 goto gen_op8;
812 /* generate a floating point operation 'v = t1 op t2' instruction. The
813 two operands are guaranted to have the same floating point type */
814 /* XXX: need to use ST1 too */
815 ST_FUNC void gen_opf(int op)
817 int a, ft, fc, swapped, r;
819 /* convert constants to memory references */
820 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
821 vswap();
822 gv(RC_FLOAT);
823 vswap();
825 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
826 gv(RC_FLOAT);
828 /* must put at least one value in the floating point register */
829 if ((vtop[-1].r & VT_LVAL) &&
830 (vtop[0].r & VT_LVAL)) {
831 vswap();
832 gv(RC_FLOAT);
833 vswap();
835 swapped = 0;
836 /* swap the stack if needed so that t1 is the register and t2 is
837 the memory reference */
838 if (vtop[-1].r & VT_LVAL) {
839 vswap();
840 swapped = 1;
842 if (op >= TOK_ULT && op <= TOK_GT) {
843 /* load on stack second operand */
844 load(TREG_ST0, vtop);
845 save_reg(TREG_EAX); /* eax is used by FP comparison code */
846 if (op == TOK_GE || op == TOK_GT)
847 swapped = !swapped;
848 else if (op == TOK_EQ || op == TOK_NE)
849 swapped = 0;
850 if (swapped)
851 o(0xc9d9); /* fxch %st(1) */
852 o(0xe9da); /* fucompp */
853 o(0xe0df); /* fnstsw %ax */
854 if (op == TOK_EQ) {
855 o(0x45e480); /* and $0x45, %ah */
856 o(0x40fC80); /* cmp $0x40, %ah */
857 } else if (op == TOK_NE) {
858 o(0x45e480); /* and $0x45, %ah */
859 o(0x40f480); /* xor $0x40, %ah */
860 op = TOK_NE;
861 } else if (op == TOK_GE || op == TOK_LE) {
862 o(0x05c4f6); /* test $0x05, %ah */
863 op = TOK_EQ;
864 } else {
865 o(0x45c4f6); /* test $0x45, %ah */
866 op = TOK_EQ;
868 vtop--;
869 vtop->r = VT_CMP;
870 vtop->c.i = op;
871 } else {
872 /* no memory reference possible for long double operations */
873 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
874 load(TREG_ST0, vtop);
875 swapped = !swapped;
878 switch(op) {
879 default:
880 case '+':
881 a = 0;
882 break;
883 case '-':
884 a = 4;
885 if (swapped)
886 a++;
887 break;
888 case '*':
889 a = 1;
890 break;
891 case '/':
892 a = 6;
893 if (swapped)
894 a++;
895 break;
897 ft = vtop->type.t;
898 fc = vtop->c.ul;
899 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
900 o(0xde); /* fxxxp %st, %st(1) */
901 o(0xc1 + (a << 3));
902 } else {
903 /* if saved lvalue, then we must reload it */
904 r = vtop->r;
905 if ((r & VT_VALMASK) == VT_LLOCAL) {
906 SValue v1;
907 r = get_reg(RC_INT);
908 v1.type.t = VT_INT;
909 v1.r = VT_LOCAL | VT_LVAL;
910 v1.c.ul = fc;
911 load(r, &v1);
912 fc = 0;
915 if ((ft & VT_BTYPE) == VT_DOUBLE)
916 o(0xdc);
917 else
918 o(0xd8);
919 gen_modrm(a, r, vtop->sym, fc);
921 vtop--;
925 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
926 and 'long long' cases. */
927 ST_FUNC void gen_cvt_itof(int t)
929 save_reg(TREG_ST0);
930 gv(RC_INT);
931 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
932 /* signed long long to float/double/long double (unsigned case
933 is handled generically) */
934 o(0x50 + vtop->r2); /* push r2 */
935 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
936 o(0x242cdf); /* fildll (%esp) */
937 o(0x08c483); /* add $8, %esp */
938 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
939 (VT_INT | VT_UNSIGNED)) {
940 /* unsigned int to float/double/long double */
941 o(0x6a); /* push $0 */
942 g(0x00);
943 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
944 o(0x242cdf); /* fildll (%esp) */
945 o(0x08c483); /* add $8, %esp */
946 } else {
947 /* int to float/double/long double */
948 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
949 o(0x2404db); /* fildl (%esp) */
950 o(0x04c483); /* add $4, %esp */
952 vtop->r = TREG_ST0;
955 /* convert fp to int 't' type */
956 /* XXX: handle long long case */
957 ST_FUNC void gen_cvt_ftoi(int t)
959 int r, r2, size;
960 Sym *sym;
961 CType ushort_type;
963 ushort_type.t = VT_SHORT | VT_UNSIGNED;
964 ushort_type.ref = 0;
966 gv(RC_FLOAT);
967 if (t != VT_INT)
968 size = 8;
969 else
970 size = 4;
972 o(0x2dd9); /* ldcw xxx */
973 sym = external_global_sym(TOK___tcc_int_fpu_control,
974 &ushort_type, VT_LVAL);
975 greloc(cur_text_section, sym,
976 ind, R_386_32);
977 gen_le32(0);
979 oad(0xec81, size); /* sub $xxx, %esp */
980 if (size == 4)
981 o(0x1cdb); /* fistpl */
982 else
983 o(0x3cdf); /* fistpll */
984 o(0x24);
985 o(0x2dd9); /* ldcw xxx */
986 sym = external_global_sym(TOK___tcc_fpu_control,
987 &ushort_type, VT_LVAL);
988 greloc(cur_text_section, sym,
989 ind, R_386_32);
990 gen_le32(0);
992 r = get_reg(RC_INT);
993 o(0x58 + r); /* pop r */
994 if (size == 8) {
995 if (t == VT_LLONG) {
996 vtop->r = r; /* mark reg as used */
997 r2 = get_reg(RC_INT);
998 o(0x58 + r2); /* pop r2 */
999 vtop->r2 = r2;
1000 } else {
1001 o(0x04c483); /* add $4, %esp */
1004 vtop->r = r;
1007 /* convert from one floating point type to another */
1008 ST_FUNC void gen_cvt_ftof(int t)
1010 /* all we have to do on i386 is to put the float in a register */
1011 gv(RC_FLOAT);
1014 /* computed goto support */
1015 ST_FUNC void ggoto(void)
1017 gcall_or_jmp(1);
1018 vtop--;
1021 /* bound check support functions */
1022 #ifdef CONFIG_TCC_BCHECK
1024 /* generate a bounded pointer addition */
1025 ST_FUNC void gen_bounded_ptr_add(void)
1027 Sym *sym;
1029 /* prepare fast i386 function call (args in eax and edx) */
1030 gv2(RC_EAX, RC_EDX);
1031 /* save all temporary registers */
1032 vtop -= 2;
1033 save_regs(0);
1034 /* do a fast function call */
1035 sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
1036 greloc(cur_text_section, sym,
1037 ind + 1, R_386_PC32);
1038 oad(0xe8, -4);
1039 /* returned pointer is in eax */
1040 vtop++;
1041 vtop->r = TREG_EAX | VT_BOUNDED;
1042 /* address of bounding function call point */
1043 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1046 /* patch pointer addition in vtop so that pointer dereferencing is
1047 also tested */
1048 ST_FUNC void gen_bounded_ptr_deref(void)
1050 int func;
1051 int size, align;
1052 Elf32_Rel *rel;
1053 Sym *sym;
1055 size = 0;
1056 /* XXX: put that code in generic part of tcc */
1057 if (!is_float(vtop->type.t)) {
1058 if (vtop->r & VT_LVAL_BYTE)
1059 size = 1;
1060 else if (vtop->r & VT_LVAL_SHORT)
1061 size = 2;
1063 if (!size)
1064 size = type_size(&vtop->type, &align);
1065 switch(size) {
1066 case 1: func = TOK___bound_ptr_indir1; break;
1067 case 2: func = TOK___bound_ptr_indir2; break;
1068 case 4: func = TOK___bound_ptr_indir4; break;
1069 case 8: func = TOK___bound_ptr_indir8; break;
1070 case 12: func = TOK___bound_ptr_indir12; break;
1071 case 16: func = TOK___bound_ptr_indir16; break;
1072 default:
1073 tcc_error("unhandled size when dereferencing bounded pointer");
1074 func = 0;
1075 break;
1078 /* patch relocation */
1079 /* XXX: find a better solution ? */
1080 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1081 sym = external_global_sym(func, &func_old_type, 0);
1082 if (!sym->c)
1083 put_extern_sym(sym, NULL, 0, 0);
1084 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1086 #endif
1088 /* end of X86 code generator */
1089 /*************************************************************/
1090 #endif
1091 /*************************************************************/