tccpe: with -l try also with "lib" prefix
[tinycc/miki.git] / i386-gen.c
blob0bc41aa46543591ce583d6dc49ee9c2078622583
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_le16(int v)
114 g(v);
115 g(v >> 8);
118 void gen_le32(int c)
120 g(c);
121 g(c >> 8);
122 g(c >> 16);
123 g(c >> 24);
126 /* output a symbol and patch all calls to it */
127 void gsym_addr(int t, int a)
129 int n, *ptr;
130 while (t) {
131 ptr = (int *)(cur_text_section->data + t);
132 n = *ptr; /* next value */
133 *ptr = a - t - 4;
134 t = n;
138 void gsym(int t)
140 gsym_addr(t, ind);
143 /* psym is used to put an instruction with a data field which is a
144 reference to a symbol. It is in fact the same as oad ! */
145 #define psym oad
147 /* instruction + 4 bytes data. Return the address of the data */
148 static int oad(int c, int s)
150 int ind1;
152 o(c);
153 ind1 = ind + 4;
154 if (ind1 > cur_text_section->data_allocated)
155 section_realloc(cur_text_section, ind1);
156 *(int *)(cur_text_section->data + ind) = s;
157 s = ind;
158 ind = ind1;
159 return s;
162 /* output constant with relocation if 'r & VT_SYM' is true */
163 static void gen_addr32(int r, Sym *sym, int c)
165 if (r & VT_SYM)
166 greloc(cur_text_section, sym, ind, R_386_32);
167 gen_le32(c);
170 static void gen_addrpc32(int r, Sym *sym, int c)
172 if (r & VT_SYM)
173 greloc(cur_text_section, sym, ind, R_386_PC32);
174 gen_le32(c - 4);
177 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
178 opcode bits */
179 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
181 op_reg = op_reg << 3;
182 if ((r & VT_VALMASK) == VT_CONST) {
183 /* constant memory reference */
184 o(0x05 | op_reg);
185 gen_addr32(r, sym, c);
186 } else if ((r & VT_VALMASK) == VT_LOCAL) {
187 /* currently, we use only ebp as base */
188 if (c == (char)c) {
189 /* short reference */
190 o(0x45 | op_reg);
191 g(c);
192 } else {
193 oad(0x85 | op_reg, c);
195 } else {
196 g(0x00 | op_reg | (r & VT_VALMASK));
200 /* load 'r' from value 'sv' */
201 void load(int r, SValue *sv)
203 int v, t, ft, fc, fr;
204 SValue v1;
206 #ifdef TCC_TARGET_PE
207 if (pe_dllimport(r, sv, load))
208 return;
209 #endif
210 fr = sv->r;
211 ft = sv->type.t;
212 fc = sv->c.ul;
214 v = fr & VT_VALMASK;
215 if (fr & VT_LVAL) {
216 if (v == VT_LLOCAL) {
217 v1.type.t = VT_INT;
218 v1.r = VT_LOCAL | VT_LVAL;
219 v1.c.ul = fc;
220 load(r, &v1);
221 fr = r;
223 if ((ft & VT_BTYPE) == VT_FLOAT) {
224 o(0xd9); /* flds */
225 r = 0;
226 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
227 o(0xdd); /* fldl */
228 r = 0;
229 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
230 o(0xdb); /* fldt */
231 r = 5;
232 } else if ((ft & VT_TYPE) == VT_BYTE) {
233 o(0xbe0f); /* movsbl */
234 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
235 o(0xb60f); /* movzbl */
236 } else if ((ft & VT_TYPE) == VT_SHORT) {
237 o(0xbf0f); /* movswl */
238 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
239 o(0xb70f); /* movzwl */
240 } else {
241 o(0x8b); /* movl */
243 gen_modrm(r, fr, sv->sym, fc);
244 } else {
245 if (v == VT_CONST) {
246 o(0xb8 + r); /* mov $xx, r */
247 gen_addr32(fr, sv->sym, fc);
248 } else if (v == VT_LOCAL) {
249 o(0x8d); /* lea xxx(%ebp), r */
250 gen_modrm(r, VT_LOCAL, sv->sym, fc);
251 } else if (v == VT_CMP) {
252 oad(0xb8 + r, 0); /* mov $0, r */
253 o(0x0f); /* setxx %br */
254 o(fc);
255 o(0xc0 + r);
256 } else if (v == VT_JMP || v == VT_JMPI) {
257 t = v & 1;
258 oad(0xb8 + r, t); /* mov $1, r */
259 o(0x05eb); /* jmp after */
260 gsym(fc);
261 oad(0xb8 + r, t ^ 1); /* mov $0, r */
262 } else if (v != r) {
263 o(0x89);
264 o(0xc0 + r + v * 8); /* mov v, r */
269 /* store register 'r' in lvalue 'v' */
270 void store(int r, SValue *v)
272 int fr, bt, ft, fc;
274 #ifdef TCC_TARGET_PE
275 if (pe_dllimport(r, v, store))
276 return;
277 #endif
278 ft = v->type.t;
279 fc = v->c.ul;
280 fr = v->r & VT_VALMASK;
281 bt = ft & VT_BTYPE;
282 /* XXX: incorrect if float reg to reg */
283 if (bt == VT_FLOAT) {
284 o(0xd9); /* fsts */
285 r = 2;
286 } else if (bt == VT_DOUBLE) {
287 o(0xdd); /* fstpl */
288 r = 2;
289 } else if (bt == VT_LDOUBLE) {
290 o(0xc0d9); /* fld %st(0) */
291 o(0xdb); /* fstpt */
292 r = 7;
293 } else {
294 if (bt == VT_SHORT)
295 o(0x66);
296 if (bt == VT_BYTE || bt == VT_BOOL)
297 o(0x88);
298 else
299 o(0x89);
301 if (fr == VT_CONST ||
302 fr == VT_LOCAL ||
303 (v->r & VT_LVAL)) {
304 gen_modrm(r, v->r, v->sym, fc);
305 } else if (fr != r) {
306 o(0xc0 + fr + r * 8); /* mov r, fr */
310 static void gadd_sp(int val)
312 if (val == (char)val) {
313 o(0xc483);
314 g(val);
315 } else {
316 oad(0xc481, val); /* add $xxx, %esp */
320 /* 'is_jmp' is '1' if it is a jump */
321 static void gcall_or_jmp(int is_jmp)
323 int r;
324 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
325 /* constant case */
326 if (vtop->r & VT_SYM) {
327 /* relocation case */
328 greloc(cur_text_section, vtop->sym,
329 ind + 1, R_386_PC32);
330 } else {
331 /* put an empty PC32 relocation */
332 put_elf_reloc(symtab_section, cur_text_section,
333 ind + 1, R_386_PC32, 0);
335 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
336 } else {
337 /* otherwise, indirect call */
338 r = gv(RC_INT);
339 o(0xff); /* call/jmp *r */
340 o(0xd0 + r + (is_jmp << 4));
344 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
345 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
347 /* Generate function call. The function address is pushed first, then
348 all the parameters in call order. This functions pops all the
349 parameters and the function address. */
350 void gfunc_call(int nb_args)
352 int size, align, r, args_size, i, func_call;
353 Sym *func_sym;
355 args_size = 0;
356 for(i = 0;i < nb_args; i++) {
357 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
358 size = type_size(&vtop->type, &align);
359 /* align to stack align size */
360 size = (size + 3) & ~3;
361 /* allocate the necessary size on stack */
362 oad(0xec81, size); /* sub $xxx, %esp */
363 /* generate structure store */
364 r = get_reg(RC_INT);
365 o(0x89); /* mov %esp, r */
366 o(0xe0 + r);
367 vset(&vtop->type, r | VT_LVAL, 0);
368 vswap();
369 vstore();
370 args_size += size;
371 } else if (is_float(vtop->type.t)) {
372 gv(RC_FLOAT); /* only one float register */
373 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
374 size = 4;
375 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
376 size = 8;
377 else
378 size = 12;
379 oad(0xec81, size); /* sub $xxx, %esp */
380 if (size == 12)
381 o(0x7cdb);
382 else
383 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
384 g(0x24);
385 g(0x00);
386 args_size += size;
387 } else {
388 /* simple type (currently always same size) */
389 /* XXX: implicit cast ? */
390 r = gv(RC_INT);
391 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
392 size = 8;
393 o(0x50 + vtop->r2); /* push r */
394 } else {
395 size = 4;
397 o(0x50 + r); /* push r */
398 args_size += size;
400 vtop--;
402 save_regs(0); /* save used temporary registers */
403 func_sym = vtop->type.ref;
404 func_call = FUNC_CALL(func_sym->r);
405 /* fast call case */
406 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
407 func_call == FUNC_FASTCALLW) {
408 int fastcall_nb_regs;
409 uint8_t *fastcall_regs_ptr;
410 if (func_call == FUNC_FASTCALLW) {
411 fastcall_regs_ptr = fastcallw_regs;
412 fastcall_nb_regs = 2;
413 } else {
414 fastcall_regs_ptr = fastcall_regs;
415 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
417 for(i = 0;i < fastcall_nb_regs; i++) {
418 if (args_size <= 0)
419 break;
420 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
421 /* XXX: incorrect for struct/floats */
422 args_size -= 4;
425 gcall_or_jmp(0);
427 #ifdef TCC_TARGET_PE
428 if ((func_sym->type.t & VT_BTYPE) == VT_STRUCT)
429 args_size -= 4;
430 #endif
431 if (args_size && func_call != FUNC_STDCALL)
432 gadd_sp(args_size);
433 vtop--;
436 #ifdef TCC_TARGET_PE
437 #define FUNC_PROLOG_SIZE 10
438 #else
439 #define FUNC_PROLOG_SIZE 9
440 #endif
442 /* generate function prolog of type 't' */
443 void gfunc_prolog(CType *func_type)
445 int addr, align, size, func_call, fastcall_nb_regs;
446 int param_index, param_addr;
447 uint8_t *fastcall_regs_ptr;
448 Sym *sym;
449 CType *type;
451 sym = func_type->ref;
452 func_call = FUNC_CALL(sym->r);
453 addr = 8;
454 loc = 0;
455 func_vc = 0;
457 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
458 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
459 fastcall_regs_ptr = fastcall_regs;
460 } else if (func_call == FUNC_FASTCALLW) {
461 fastcall_nb_regs = 2;
462 fastcall_regs_ptr = fastcallw_regs;
463 } else {
464 fastcall_nb_regs = 0;
465 fastcall_regs_ptr = NULL;
467 param_index = 0;
469 ind += FUNC_PROLOG_SIZE;
470 func_sub_sp_offset = ind;
471 /* if the function returns a structure, then add an
472 implicit pointer parameter */
473 func_vt = sym->type;
474 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
475 /* XXX: fastcall case ? */
476 func_vc = addr;
477 addr += 4;
478 param_index++;
480 /* define parameters */
481 while ((sym = sym->next) != NULL) {
482 type = &sym->type;
483 size = type_size(type, &align);
484 size = (size + 3) & ~3;
485 #ifdef FUNC_STRUCT_PARAM_AS_PTR
486 /* structs are passed as pointer */
487 if ((type->t & VT_BTYPE) == VT_STRUCT) {
488 size = 4;
490 #endif
491 if (param_index < fastcall_nb_regs) {
492 /* save FASTCALL register */
493 loc -= 4;
494 o(0x89); /* movl */
495 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
496 param_addr = loc;
497 } else {
498 param_addr = addr;
499 addr += size;
501 sym_push(sym->v & ~SYM_FIELD, type,
502 VT_LOCAL | lvalue_type(type->t), param_addr);
503 param_index++;
505 func_ret_sub = 0;
506 /* pascal type call ? */
507 if (func_call == FUNC_STDCALL)
508 func_ret_sub = addr - 8;
509 #ifdef TCC_TARGET_PE
510 else if (func_vc)
511 func_ret_sub = 4;
512 #endif
514 /* leave some room for bound checking code */
515 if (tcc_state->do_bounds_check) {
516 oad(0xb8, 0); /* lbound section pointer */
517 oad(0xb8, 0); /* call to function */
518 func_bound_offset = lbounds_section->data_offset;
522 /* generate function epilog */
523 void gfunc_epilog(void)
525 int v, saved_ind;
527 #ifdef CONFIG_TCC_BCHECK
528 if (tcc_state->do_bounds_check
529 && func_bound_offset != lbounds_section->data_offset) {
530 int saved_ind;
531 int *bounds_ptr;
532 Sym *sym, *sym_data;
533 /* add end of table info */
534 bounds_ptr = section_ptr_add(lbounds_section, sizeof(int));
535 *bounds_ptr = 0;
536 /* generate bound local allocation */
537 saved_ind = ind;
538 ind = func_sub_sp_offset;
539 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
540 func_bound_offset, lbounds_section->data_offset);
541 greloc(cur_text_section, sym_data,
542 ind + 1, R_386_32);
543 oad(0xb8, 0); /* mov %eax, xxx */
544 sym = external_global_sym(TOK___bound_local_new, &func_old_type, 0);
545 greloc(cur_text_section, sym,
546 ind + 1, R_386_PC32);
547 oad(0xe8, -4);
548 ind = saved_ind;
549 /* generate bound check local freeing */
550 o(0x5250); /* save returned value, if any */
551 greloc(cur_text_section, sym_data,
552 ind + 1, R_386_32);
553 oad(0xb8, 0); /* mov %eax, xxx */
554 sym = external_global_sym(TOK___bound_local_delete, &func_old_type, 0);
555 greloc(cur_text_section, sym,
556 ind + 1, R_386_PC32);
557 oad(0xe8, -4);
558 o(0x585a); /* restore returned value, if any */
560 #endif
561 o(0xc9); /* leave */
562 if (func_ret_sub == 0) {
563 o(0xc3); /* ret */
564 } else {
565 o(0xc2); /* ret n */
566 g(func_ret_sub);
567 g(func_ret_sub >> 8);
569 /* align local size to word & save local variables */
571 v = (-loc + 3) & -4;
572 saved_ind = ind;
573 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
574 #ifdef TCC_TARGET_PE
575 if (v >= 4096) {
576 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
577 oad(0xb8, v); /* mov stacksize, %eax */
578 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
579 greloc(cur_text_section, sym, ind-4, R_386_PC32);
580 } else
581 #endif
583 o(0xe58955); /* push %ebp, mov %esp, %ebp */
584 o(0xec81); /* sub esp, stacksize */
585 gen_le32(v);
586 #if FUNC_PROLOG_SIZE == 10
587 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
588 #endif
590 ind = saved_ind;
593 /* generate a jump to a label */
594 int gjmp(int t)
596 return psym(0xe9, t);
599 /* generate a jump to a fixed address */
600 void gjmp_addr(int a)
602 int r;
603 r = a - ind - 2;
604 if (r == (char)r) {
605 g(0xeb);
606 g(r);
607 } else {
608 oad(0xe9, a - ind - 5);
612 /* generate a test. set 'inv' to invert test. Stack entry is popped */
613 int gtst(int inv, int t)
615 int v, *p;
617 v = vtop->r & VT_VALMASK;
618 if (v == VT_CMP) {
619 /* fast case : can jump directly since flags are set */
620 g(0x0f);
621 t = psym((vtop->c.i - 16) ^ inv, t);
622 } else if (v == VT_JMP || v == VT_JMPI) {
623 /* && or || optimization */
624 if ((v & 1) == inv) {
625 /* insert vtop->c jump list in t */
626 p = &vtop->c.i;
627 while (*p != 0)
628 p = (int *)(cur_text_section->data + *p);
629 *p = t;
630 t = vtop->c.i;
631 } else {
632 t = gjmp(t);
633 gsym(vtop->c.i);
635 } else {
636 if (is_float(vtop->type.t) ||
637 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
638 vpushi(0);
639 gen_op(TOK_NE);
641 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
642 /* constant jmp optimization */
643 if ((vtop->c.i != 0) != inv)
644 t = gjmp(t);
645 } else {
646 v = gv(RC_INT);
647 o(0x85);
648 o(0xc0 + v * 9);
649 g(0x0f);
650 t = psym(0x85 ^ inv, t);
653 vtop--;
654 return t;
657 /* generate an integer binary operation */
658 void gen_opi(int op)
660 int r, fr, opc, c;
662 switch(op) {
663 case '+':
664 case TOK_ADDC1: /* add with carry generation */
665 opc = 0;
666 gen_op8:
667 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
668 /* constant case */
669 vswap();
670 r = gv(RC_INT);
671 vswap();
672 c = vtop->c.i;
673 if (c == (char)c) {
674 /* XXX: generate inc and dec for smaller code ? */
675 o(0x83);
676 o(0xc0 | (opc << 3) | r);
677 g(c);
678 } else {
679 o(0x81);
680 oad(0xc0 | (opc << 3) | r, c);
682 } else {
683 gv2(RC_INT, RC_INT);
684 r = vtop[-1].r;
685 fr = vtop[0].r;
686 o((opc << 3) | 0x01);
687 o(0xc0 + r + fr * 8);
689 vtop--;
690 if (op >= TOK_ULT && op <= TOK_GT) {
691 vtop->r = VT_CMP;
692 vtop->c.i = op;
694 break;
695 case '-':
696 case TOK_SUBC1: /* sub with carry generation */
697 opc = 5;
698 goto gen_op8;
699 case TOK_ADDC2: /* add with carry use */
700 opc = 2;
701 goto gen_op8;
702 case TOK_SUBC2: /* sub with carry use */
703 opc = 3;
704 goto gen_op8;
705 case '&':
706 opc = 4;
707 goto gen_op8;
708 case '^':
709 opc = 6;
710 goto gen_op8;
711 case '|':
712 opc = 1;
713 goto gen_op8;
714 case '*':
715 gv2(RC_INT, RC_INT);
716 r = vtop[-1].r;
717 fr = vtop[0].r;
718 vtop--;
719 o(0xaf0f); /* imul fr, r */
720 o(0xc0 + fr + r * 8);
721 break;
722 case TOK_SHL:
723 opc = 4;
724 goto gen_shift;
725 case TOK_SHR:
726 opc = 5;
727 goto gen_shift;
728 case TOK_SAR:
729 opc = 7;
730 gen_shift:
731 opc = 0xc0 | (opc << 3);
732 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
733 /* constant case */
734 vswap();
735 r = gv(RC_INT);
736 vswap();
737 c = vtop->c.i & 0x1f;
738 o(0xc1); /* shl/shr/sar $xxx, r */
739 o(opc | r);
740 g(c);
741 } else {
742 /* we generate the shift in ecx */
743 gv2(RC_INT, RC_ECX);
744 r = vtop[-1].r;
745 o(0xd3); /* shl/shr/sar %cl, r */
746 o(opc | r);
748 vtop--;
749 break;
750 case '/':
751 case TOK_UDIV:
752 case TOK_PDIV:
753 case '%':
754 case TOK_UMOD:
755 case TOK_UMULL:
756 /* first operand must be in eax */
757 /* XXX: need better constraint for second operand */
758 gv2(RC_EAX, RC_ECX);
759 r = vtop[-1].r;
760 fr = vtop[0].r;
761 vtop--;
762 save_reg(TREG_EDX);
763 if (op == TOK_UMULL) {
764 o(0xf7); /* mul fr */
765 o(0xe0 + fr);
766 vtop->r2 = TREG_EDX;
767 r = TREG_EAX;
768 } else {
769 if (op == TOK_UDIV || op == TOK_UMOD) {
770 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
771 o(0xf0 + fr);
772 } else {
773 o(0xf799); /* cltd, idiv fr, %eax */
774 o(0xf8 + fr);
776 if (op == '%' || op == TOK_UMOD)
777 r = TREG_EDX;
778 else
779 r = TREG_EAX;
781 vtop->r = r;
782 break;
783 default:
784 opc = 7;
785 goto gen_op8;
789 /* generate a floating point operation 'v = t1 op t2' instruction. The
790 two operands are guaranted to have the same floating point type */
791 /* XXX: need to use ST1 too */
792 void gen_opf(int op)
794 int a, ft, fc, swapped, r;
796 /* convert constants to memory references */
797 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
798 vswap();
799 gv(RC_FLOAT);
800 vswap();
802 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
803 gv(RC_FLOAT);
805 /* must put at least one value in the floating point register */
806 if ((vtop[-1].r & VT_LVAL) &&
807 (vtop[0].r & VT_LVAL)) {
808 vswap();
809 gv(RC_FLOAT);
810 vswap();
812 swapped = 0;
813 /* swap the stack if needed so that t1 is the register and t2 is
814 the memory reference */
815 if (vtop[-1].r & VT_LVAL) {
816 vswap();
817 swapped = 1;
819 if (op >= TOK_ULT && op <= TOK_GT) {
820 /* load on stack second operand */
821 load(TREG_ST0, vtop);
822 save_reg(TREG_EAX); /* eax is used by FP comparison code */
823 if (op == TOK_GE || op == TOK_GT)
824 swapped = !swapped;
825 else if (op == TOK_EQ || op == TOK_NE)
826 swapped = 0;
827 if (swapped)
828 o(0xc9d9); /* fxch %st(1) */
829 o(0xe9da); /* fucompp */
830 o(0xe0df); /* fnstsw %ax */
831 if (op == TOK_EQ) {
832 o(0x45e480); /* and $0x45, %ah */
833 o(0x40fC80); /* cmp $0x40, %ah */
834 } else if (op == TOK_NE) {
835 o(0x45e480); /* and $0x45, %ah */
836 o(0x40f480); /* xor $0x40, %ah */
837 op = TOK_NE;
838 } else if (op == TOK_GE || op == TOK_LE) {
839 o(0x05c4f6); /* test $0x05, %ah */
840 op = TOK_EQ;
841 } else {
842 o(0x45c4f6); /* test $0x45, %ah */
843 op = TOK_EQ;
845 vtop--;
846 vtop->r = VT_CMP;
847 vtop->c.i = op;
848 } else {
849 /* no memory reference possible for long double operations */
850 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
851 load(TREG_ST0, vtop);
852 swapped = !swapped;
855 switch(op) {
856 default:
857 case '+':
858 a = 0;
859 break;
860 case '-':
861 a = 4;
862 if (swapped)
863 a++;
864 break;
865 case '*':
866 a = 1;
867 break;
868 case '/':
869 a = 6;
870 if (swapped)
871 a++;
872 break;
874 ft = vtop->type.t;
875 fc = vtop->c.ul;
876 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
877 o(0xde); /* fxxxp %st, %st(1) */
878 o(0xc1 + (a << 3));
879 } else {
880 /* if saved lvalue, then we must reload it */
881 r = vtop->r;
882 if ((r & VT_VALMASK) == VT_LLOCAL) {
883 SValue v1;
884 r = get_reg(RC_INT);
885 v1.type.t = VT_INT;
886 v1.r = VT_LOCAL | VT_LVAL;
887 v1.c.ul = fc;
888 load(r, &v1);
889 fc = 0;
892 if ((ft & VT_BTYPE) == VT_DOUBLE)
893 o(0xdc);
894 else
895 o(0xd8);
896 gen_modrm(a, r, vtop->sym, fc);
898 vtop--;
902 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
903 and 'long long' cases. */
904 void gen_cvt_itof(int t)
906 save_reg(TREG_ST0);
907 gv(RC_INT);
908 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
909 /* signed long long to float/double/long double (unsigned case
910 is handled generically) */
911 o(0x50 + vtop->r2); /* push r2 */
912 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
913 o(0x242cdf); /* fildll (%esp) */
914 o(0x08c483); /* add $8, %esp */
915 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
916 (VT_INT | VT_UNSIGNED)) {
917 /* unsigned int to float/double/long double */
918 o(0x6a); /* push $0 */
919 g(0x00);
920 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
921 o(0x242cdf); /* fildll (%esp) */
922 o(0x08c483); /* add $8, %esp */
923 } else {
924 /* int to float/double/long double */
925 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
926 o(0x2404db); /* fildl (%esp) */
927 o(0x04c483); /* add $4, %esp */
929 vtop->r = TREG_ST0;
932 /* convert fp to int 't' type */
933 /* XXX: handle long long case */
934 void gen_cvt_ftoi(int t)
936 int r, r2, size;
937 Sym *sym;
938 CType ushort_type;
940 ushort_type.t = VT_SHORT | VT_UNSIGNED;
942 gv(RC_FLOAT);
943 if (t != VT_INT)
944 size = 8;
945 else
946 size = 4;
948 o(0x2dd9); /* ldcw xxx */
949 sym = external_global_sym(TOK___tcc_int_fpu_control,
950 &ushort_type, VT_LVAL);
951 greloc(cur_text_section, sym,
952 ind, R_386_32);
953 gen_le32(0);
955 oad(0xec81, size); /* sub $xxx, %esp */
956 if (size == 4)
957 o(0x1cdb); /* fistpl */
958 else
959 o(0x3cdf); /* fistpll */
960 o(0x24);
961 o(0x2dd9); /* ldcw xxx */
962 sym = external_global_sym(TOK___tcc_fpu_control,
963 &ushort_type, VT_LVAL);
964 greloc(cur_text_section, sym,
965 ind, R_386_32);
966 gen_le32(0);
968 r = get_reg(RC_INT);
969 o(0x58 + r); /* pop r */
970 if (size == 8) {
971 if (t == VT_LLONG) {
972 vtop->r = r; /* mark reg as used */
973 r2 = get_reg(RC_INT);
974 o(0x58 + r2); /* pop r2 */
975 vtop->r2 = r2;
976 } else {
977 o(0x04c483); /* add $4, %esp */
980 vtop->r = r;
983 /* convert from one floating point type to another */
984 void gen_cvt_ftof(int t)
986 /* all we have to do on i386 is to put the float in a register */
987 gv(RC_FLOAT);
990 /* computed goto support */
991 void ggoto(void)
993 gcall_or_jmp(1);
994 vtop--;
997 /* bound check support functions */
998 #ifdef CONFIG_TCC_BCHECK
1000 /* generate a bounded pointer addition */
1001 void gen_bounded_ptr_add(void)
1003 Sym *sym;
1005 /* prepare fast i386 function call (args in eax and edx) */
1006 gv2(RC_EAX, RC_EDX);
1007 /* save all temporary registers */
1008 vtop -= 2;
1009 save_regs(0);
1010 /* do a fast function call */
1011 sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
1012 greloc(cur_text_section, sym,
1013 ind + 1, R_386_PC32);
1014 oad(0xe8, -4);
1015 /* returned pointer is in eax */
1016 vtop++;
1017 vtop->r = TREG_EAX | VT_BOUNDED;
1018 /* address of bounding function call point */
1019 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1022 /* patch pointer addition in vtop so that pointer dereferencing is
1023 also tested */
1024 void gen_bounded_ptr_deref(void)
1026 int func;
1027 int size, align;
1028 Elf32_Rel *rel;
1029 Sym *sym;
1031 size = 0;
1032 /* XXX: put that code in generic part of tcc */
1033 if (!is_float(vtop->type.t)) {
1034 if (vtop->r & VT_LVAL_BYTE)
1035 size = 1;
1036 else if (vtop->r & VT_LVAL_SHORT)
1037 size = 2;
1039 if (!size)
1040 size = type_size(&vtop->type, &align);
1041 switch(size) {
1042 case 1: func = TOK___bound_ptr_indir1; break;
1043 case 2: func = TOK___bound_ptr_indir2; break;
1044 case 4: func = TOK___bound_ptr_indir4; break;
1045 case 8: func = TOK___bound_ptr_indir8; break;
1046 case 12: func = TOK___bound_ptr_indir12; break;
1047 case 16: func = TOK___bound_ptr_indir16; break;
1048 default:
1049 error("unhandled size when derefencing bounded pointer");
1050 func = 0;
1051 break;
1054 /* patch relocation */
1055 /* XXX: find a better solution ? */
1056 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1057 sym = external_global_sym(func, &func_old_type, 0);
1058 if (!sym->c)
1059 put_extern_sym(sym, NULL, 0, 0);
1060 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1062 #endif
1064 /* end of X86 code generator */
1065 /*************************************************************/