tccgen.c: cleanup debug support
[tinycc.git] / i386-gen.c
blobb1dc1303b35e7711900c064e3da2195a50747a63
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 5
25 #define NB_ASM_REGS 8
26 #define CONFIG_TCC_ASM
28 /* a register can belong to several classes. The classes must be
29 sorted from more general to more precise (see gv2() code which does
30 assumptions on it). */
31 #define RC_INT 0x0001 /* generic integer register */
32 #define RC_FLOAT 0x0002 /* generic float register */
33 #define RC_EAX 0x0004
34 #define RC_ST0 0x0008
35 #define RC_ECX 0x0010
36 #define RC_EDX 0x0020
37 #define RC_EBX 0x0040
39 #define RC_IRET RC_EAX /* function return: integer register */
40 #define RC_IRE2 RC_EDX /* function return: second integer register */
41 #define RC_FRET RC_ST0 /* function return: float register */
43 /* pretty names for the registers */
44 enum {
45 TREG_EAX = 0,
46 TREG_ECX,
47 TREG_EDX,
48 TREG_EBX,
49 TREG_ST0,
50 TREG_ESP = 4
53 /* return registers for function */
54 #define REG_IRET TREG_EAX /* single word int return register */
55 #define REG_IRE2 TREG_EDX /* second word return register (for long long) */
56 #define REG_FRET TREG_ST0 /* float return register */
58 /* defined if function parameters must be evaluated in reverse order */
59 #define INVERT_FUNC_PARAMS
61 /* defined if structures are passed as pointers. Otherwise structures
62 are directly pushed on stack. */
63 /* #define FUNC_STRUCT_PARAM_AS_PTR */
65 /* pointer size, in bytes */
66 #define PTR_SIZE 4
68 /* long double size and alignment, in bytes */
69 #define LDOUBLE_SIZE 12
70 #define LDOUBLE_ALIGN 4
71 /* maximum alignment (for aligned attribute support) */
72 #define MAX_ALIGN 8
74 /* define if return values need to be extended explicitely
75 at caller side (for interfacing with non-TCC compilers) */
76 #define PROMOTE_RET
78 /******************************************************/
79 #else /* ! TARGET_DEFS_ONLY */
80 /******************************************************/
81 #define USING_GLOBALS
82 #include "tcc.h"
84 /* define to 1/0 to [not] have EBX as 4th register */
85 #define USE_EBX 0
87 ST_DATA const int reg_classes[NB_REGS] = {
88 /* eax */ RC_INT | RC_EAX,
89 /* ecx */ RC_INT | RC_ECX,
90 /* edx */ RC_INT | RC_EDX,
91 /* ebx */ (RC_INT | RC_EBX) * USE_EBX,
92 /* st0 */ RC_FLOAT | RC_ST0,
95 static unsigned long func_sub_sp_offset;
96 static int func_ret_sub;
97 #ifdef CONFIG_TCC_BCHECK
98 static addr_t func_bound_offset;
99 static unsigned long func_bound_ind;
100 static int func_bound_alloca_used;
101 static void gen_bounds_prolog(void);
102 static void gen_bounds_epilog(void);
103 #endif
105 /* XXX: make it faster ? */
106 ST_FUNC void g(int c)
108 int ind1;
109 if (nocode_wanted)
110 return;
111 ind1 = ind + 1;
112 if (ind1 > cur_text_section->data_allocated)
113 section_realloc(cur_text_section, ind1);
114 cur_text_section->data[ind] = c;
115 ind = ind1;
118 ST_FUNC void o(unsigned int c)
120 while (c) {
121 g(c);
122 c = c >> 8;
126 ST_FUNC void gen_le16(int v)
128 g(v);
129 g(v >> 8);
132 ST_FUNC void gen_le32(int c)
134 g(c);
135 g(c >> 8);
136 g(c >> 16);
137 g(c >> 24);
140 /* output a symbol and patch all calls to it */
141 ST_FUNC void gsym_addr(int t, int a)
143 while (t) {
144 unsigned char *ptr = cur_text_section->data + t;
145 uint32_t n = read32le(ptr); /* next value */
146 write32le(ptr, a - t - 4);
147 t = n;
151 /* instruction + 4 bytes data. Return the address of the data */
152 static int oad(int c, int s)
154 int t;
155 if (nocode_wanted)
156 return s;
157 o(c);
158 t = ind;
159 gen_le32(s);
160 return t;
163 ST_FUNC void gen_fill_nops(int bytes)
165 while (bytes--)
166 g(0x90);
169 /* generate jmp to a label */
170 #define gjmp2(instr,lbl) oad(instr,lbl)
172 /* output constant with relocation if 'r & VT_SYM' is true */
173 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
175 if (r & VT_SYM)
176 greloc(cur_text_section, sym, ind, R_386_32);
177 gen_le32(c);
180 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
182 if (r & VT_SYM)
183 greloc(cur_text_section, sym, ind, R_386_PC32);
184 gen_le32(c - 4);
187 /* generate a modrm reference. 'op_reg' contains the additional 3
188 opcode bits */
189 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
191 op_reg = op_reg << 3;
192 if ((r & VT_VALMASK) == VT_CONST) {
193 /* constant memory reference */
194 o(0x05 | op_reg);
195 gen_addr32(r, sym, c);
196 } else if ((r & VT_VALMASK) == VT_LOCAL) {
197 /* currently, we use only ebp as base */
198 if (c == (char)c) {
199 /* short reference */
200 o(0x45 | op_reg);
201 g(c);
202 } else {
203 oad(0x85 | op_reg, c);
205 } else {
206 g(0x00 | op_reg | (r & VT_VALMASK));
210 /* load 'r' from value 'sv' */
211 ST_FUNC void load(int r, SValue *sv)
213 int v, t, ft, fc, fr;
214 SValue v1;
216 #ifdef TCC_TARGET_PE
217 SValue v2;
218 sv = pe_getimport(sv, &v2);
219 #endif
221 fr = sv->r;
222 ft = sv->type.t & ~VT_DEFSIGN;
223 fc = sv->c.i;
225 ft &= ~(VT_VOLATILE | VT_CONSTANT);
227 v = fr & VT_VALMASK;
228 if (fr & VT_LVAL) {
229 if (v == VT_LLOCAL) {
230 v1.type.t = VT_INT;
231 v1.r = VT_LOCAL | VT_LVAL;
232 v1.c.i = fc;
233 fr = r;
234 if (!(reg_classes[fr] & RC_INT))
235 fr = get_reg(RC_INT);
236 load(fr, &v1);
238 if ((ft & VT_BTYPE) == VT_FLOAT) {
239 o(0xd9); /* flds */
240 r = 0;
241 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
242 o(0xdd); /* fldl */
243 r = 0;
244 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
245 o(0xdb); /* fldt */
246 r = 5;
247 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
248 o(0xbe0f); /* movsbl */
249 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
250 o(0xb60f); /* movzbl */
251 } else if ((ft & VT_TYPE) == VT_SHORT) {
252 o(0xbf0f); /* movswl */
253 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
254 o(0xb70f); /* movzwl */
255 } else {
256 o(0x8b); /* movl */
258 gen_modrm(r, fr, sv->sym, fc);
259 } else {
260 if (v == VT_CONST) {
261 o(0xb8 + r); /* mov $xx, r */
262 gen_addr32(fr, sv->sym, fc);
263 } else if (v == VT_LOCAL) {
264 if (fc) {
265 o(0x8d); /* lea xxx(%ebp), r */
266 gen_modrm(r, VT_LOCAL, sv->sym, fc);
267 } else {
268 o(0x89);
269 o(0xe8 + r); /* mov %ebp, r */
271 } else if (v == VT_CMP) {
272 o(0x0f); /* setxx %br */
273 o(fc);
274 o(0xc0 + r);
275 o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
276 } else if (v == VT_JMP || v == VT_JMPI) {
277 t = v & 1;
278 oad(0xb8 + r, t); /* mov $1, r */
279 o(0x05eb); /* jmp after */
280 gsym(fc);
281 oad(0xb8 + r, t ^ 1); /* mov $0, r */
282 } else if (v != r) {
283 o(0x89);
284 o(0xc0 + r + v * 8); /* mov v, r */
289 /* store register 'r' in lvalue 'v' */
290 ST_FUNC void store(int r, SValue *v)
292 int fr, bt, ft, fc;
294 #ifdef TCC_TARGET_PE
295 SValue v2;
296 v = pe_getimport(v, &v2);
297 #endif
299 ft = v->type.t;
300 fc = v->c.i;
301 fr = v->r & VT_VALMASK;
302 ft &= ~(VT_VOLATILE | VT_CONSTANT);
303 bt = ft & VT_BTYPE;
304 /* XXX: incorrect if float reg to reg */
305 if (bt == VT_FLOAT) {
306 o(0xd9); /* fsts */
307 r = 2;
308 } else if (bt == VT_DOUBLE) {
309 o(0xdd); /* fstpl */
310 r = 2;
311 } else if (bt == VT_LDOUBLE) {
312 o(0xc0d9); /* fld %st(0) */
313 o(0xdb); /* fstpt */
314 r = 7;
315 } else {
316 if (bt == VT_SHORT)
317 o(0x66);
318 if (bt == VT_BYTE || bt == VT_BOOL)
319 o(0x88);
320 else
321 o(0x89);
323 if (fr == VT_CONST ||
324 fr == VT_LOCAL ||
325 (v->r & VT_LVAL)) {
326 gen_modrm(r, v->r, v->sym, fc);
327 } else if (fr != r) {
328 o(0xc0 + fr + r * 8); /* mov r, fr */
332 static void gadd_sp(int val)
334 if (val == (char)val) {
335 o(0xc483);
336 g(val);
337 } else {
338 oad(0xc481, val); /* add $xxx, %esp */
342 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
343 static void gen_static_call(int v)
345 Sym *sym;
347 sym = external_global_sym(v, &func_old_type);
348 oad(0xe8, -4);
349 greloc(cur_text_section, sym, ind-4, R_386_PC32);
351 #endif
353 /* 'is_jmp' is '1' if it is a jump */
354 static void gcall_or_jmp(int is_jmp)
356 int r;
357 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
358 /* constant and relocation case */
359 greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
360 oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
361 #ifdef CONFIG_TCC_BCHECK
362 if (tcc_state->do_bounds_check && vtop->sym->v == TOK_alloca)
363 func_bound_alloca_used = 1;
364 #endif
365 } else {
366 /* otherwise, indirect call */
367 r = gv(RC_INT);
368 o(0xff); /* call/jmp *r */
369 o(0xd0 + r + (is_jmp << 4));
373 static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
374 static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
376 /* Return the number of registers needed to return the struct, or 0 if
377 returning via struct pointer. */
378 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
380 #ifdef TCC_TARGET_PE
381 int size, align;
382 *ret_align = 1; // Never have to re-align return values for x86
383 *regsize = 4;
384 size = type_size(vt, &align);
385 if (size > 8 || (size & (size - 1)))
386 return 0;
387 if (size == 8)
388 ret->t = VT_LLONG;
389 else if (size == 4)
390 ret->t = VT_INT;
391 else if (size == 2)
392 ret->t = VT_SHORT;
393 else
394 ret->t = VT_BYTE;
395 ret->ref = NULL;
396 return 1;
397 #else
398 *ret_align = 1; // Never have to re-align return values for x86
399 return 0;
400 #endif
403 /* Generate function call. The function address is pushed first, then
404 all the parameters in call order. This functions pops all the
405 parameters and the function address. */
406 ST_FUNC void gfunc_call(int nb_args)
408 int size, align, r, args_size, i, func_call;
409 Sym *func_sym;
411 #ifdef CONFIG_TCC_BCHECK
412 if (tcc_state->do_bounds_check)
413 gbound_args(nb_args);
414 #endif
416 args_size = 0;
417 for(i = 0;i < nb_args; i++) {
418 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
419 size = type_size(&vtop->type, &align);
420 /* align to stack align size */
421 size = (size + 3) & ~3;
422 /* allocate the necessary size on stack */
423 oad(0xec81, size); /* sub $xxx, %esp */
424 /* generate structure store */
425 r = get_reg(RC_INT);
426 o(0x89); /* mov %esp, r */
427 o(0xe0 + r);
428 vset(&vtop->type, r | VT_LVAL, 0);
429 vswap();
430 vstore();
431 args_size += size;
432 } else if (is_float(vtop->type.t)) {
433 gv(RC_FLOAT); /* only one float register */
434 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
435 size = 4;
436 else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
437 size = 8;
438 else
439 size = 12;
440 oad(0xec81, size); /* sub $xxx, %esp */
441 if (size == 12)
442 o(0x7cdb);
443 else
444 o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
445 g(0x24);
446 g(0x00);
447 args_size += size;
448 } else {
449 /* simple type (currently always same size) */
450 /* XXX: implicit cast ? */
451 r = gv(RC_INT);
452 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
453 size = 8;
454 o(0x50 + vtop->r2); /* push r */
455 } else {
456 size = 4;
458 o(0x50 + r); /* push r */
459 args_size += size;
461 vtop--;
463 save_regs(0); /* save used temporary registers */
464 func_sym = vtop->type.ref;
465 func_call = func_sym->f.func_call;
466 /* fast call case */
467 if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
468 func_call == FUNC_FASTCALLW) {
469 int fastcall_nb_regs;
470 uint8_t *fastcall_regs_ptr;
471 if (func_call == FUNC_FASTCALLW) {
472 fastcall_regs_ptr = fastcallw_regs;
473 fastcall_nb_regs = 2;
474 } else {
475 fastcall_regs_ptr = fastcall_regs;
476 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
478 for(i = 0;i < fastcall_nb_regs; i++) {
479 if (args_size <= 0)
480 break;
481 o(0x58 + fastcall_regs_ptr[i]); /* pop r */
482 /* XXX: incorrect for struct/floats */
483 args_size -= 4;
486 #ifndef TCC_TARGET_PE
487 else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
488 args_size -= 4;
489 #endif
491 gcall_or_jmp(0);
493 if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
494 gadd_sp(args_size);
495 vtop--;
498 #ifdef TCC_TARGET_PE
499 #define FUNC_PROLOG_SIZE (10 + USE_EBX)
500 #else
501 #define FUNC_PROLOG_SIZE (9 + USE_EBX)
502 #endif
504 /* generate function prolog of type 't' */
505 ST_FUNC void gfunc_prolog(Sym *func_sym)
507 CType *func_type = &func_sym->type;
508 int addr, align, size, func_call, fastcall_nb_regs;
509 int param_index, param_addr;
510 uint8_t *fastcall_regs_ptr;
511 Sym *sym;
512 CType *type;
514 sym = func_type->ref;
515 func_call = sym->f.func_call;
516 addr = 8;
517 loc = 0;
518 func_vc = 0;
520 if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
521 fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
522 fastcall_regs_ptr = fastcall_regs;
523 } else if (func_call == FUNC_FASTCALLW) {
524 fastcall_nb_regs = 2;
525 fastcall_regs_ptr = fastcallw_regs;
526 } else {
527 fastcall_nb_regs = 0;
528 fastcall_regs_ptr = NULL;
530 param_index = 0;
532 ind += FUNC_PROLOG_SIZE;
533 func_sub_sp_offset = ind;
534 /* if the function returns a structure, then add an
535 implicit pointer parameter */
536 #ifdef TCC_TARGET_PE
537 size = type_size(&func_vt,&align);
538 if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
539 && (size > 8 || (size & (size - 1)))) {
540 #else
541 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
542 #endif
543 /* XXX: fastcall case ? */
544 func_vc = addr;
545 addr += 4;
546 param_index++;
548 /* define parameters */
549 while ((sym = sym->next) != NULL) {
550 type = &sym->type;
551 size = type_size(type, &align);
552 size = (size + 3) & ~3;
553 #ifdef FUNC_STRUCT_PARAM_AS_PTR
554 /* structs are passed as pointer */
555 if ((type->t & VT_BTYPE) == VT_STRUCT) {
556 size = 4;
558 #endif
559 if (param_index < fastcall_nb_regs) {
560 /* save FASTCALL register */
561 loc -= 4;
562 o(0x89); /* movl */
563 gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
564 param_addr = loc;
565 } else {
566 param_addr = addr;
567 addr += size;
569 sym_push(sym->v & ~SYM_FIELD, type,
570 VT_LOCAL | VT_LVAL, param_addr);
571 param_index++;
573 func_ret_sub = 0;
574 /* pascal type call or fastcall ? */
575 if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
576 func_ret_sub = addr - 8;
577 #ifndef TCC_TARGET_PE
578 else if (func_vc)
579 func_ret_sub = 4;
580 #endif
582 #ifdef CONFIG_TCC_BCHECK
583 if (tcc_state->do_bounds_check)
584 gen_bounds_prolog();
585 #endif
588 /* generate function epilog */
589 ST_FUNC void gfunc_epilog(void)
591 addr_t v, saved_ind;
593 #ifdef CONFIG_TCC_BCHECK
594 if (tcc_state->do_bounds_check)
595 gen_bounds_epilog();
596 #endif
598 /* align local size to word & save local variables */
599 v = (-loc + 3) & -4;
601 #if USE_EBX
602 o(0x8b);
603 gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
604 #endif
606 o(0xc9); /* leave */
607 if (func_ret_sub == 0) {
608 o(0xc3); /* ret */
609 } else {
610 o(0xc2); /* ret n */
611 g(func_ret_sub);
612 g(func_ret_sub >> 8);
614 saved_ind = ind;
615 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
616 #ifdef TCC_TARGET_PE
617 if (v >= 4096) {
618 oad(0xb8, v); /* mov stacksize, %eax */
619 gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
620 } else
621 #endif
623 o(0xe58955); /* push %ebp, mov %esp, %ebp */
624 o(0xec81); /* sub esp, stacksize */
625 gen_le32(v);
626 #ifdef TCC_TARGET_PE
627 o(0x90); /* adjust to FUNC_PROLOG_SIZE */
628 #endif
630 o(0x53 * USE_EBX); /* push ebx */
631 ind = saved_ind;
634 /* generate a jump to a label */
635 ST_FUNC int gjmp(int t)
637 return gjmp2(0xe9, t);
640 /* generate a jump to a fixed address */
641 ST_FUNC void gjmp_addr(int a)
643 int r;
644 r = a - ind - 2;
645 if (r == (char)r) {
646 g(0xeb);
647 g(r);
648 } else {
649 oad(0xe9, a - ind - 5);
653 #if 0
654 /* generate a jump to a fixed address */
655 ST_FUNC void gjmp_cond_addr(int a, int op)
657 int r = a - ind - 2;
658 if (r == (char)r)
659 g(op - 32), g(r);
660 else
661 g(0x0f), gjmp2(op - 16, r - 4);
663 #endif
665 ST_FUNC int gjmp_append(int n, int t)
667 void *p;
668 /* insert vtop->c jump list in t */
669 if (n) {
670 uint32_t n1 = n, n2;
671 while ((n2 = read32le(p = cur_text_section->data + n1)))
672 n1 = n2;
673 write32le(p, t);
674 t = n;
676 return t;
679 ST_FUNC int gjmp_cond(int op, int t)
681 g(0x0f);
682 t = gjmp2(op - 16, t);
683 return t;
686 ST_FUNC void gen_opi(int op)
688 int r, fr, opc, c;
690 switch(op) {
691 case '+':
692 case TOK_ADDC1: /* add with carry generation */
693 opc = 0;
694 gen_op8:
695 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
696 /* constant case */
697 vswap();
698 r = gv(RC_INT);
699 vswap();
700 c = vtop->c.i;
701 if (c == (char)c) {
702 /* generate inc and dec for smaller code */
703 if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
704 opc = (c == 1) ^ (op == '+');
705 o (0x40 | (opc << 3) | r); // inc,dec
706 } else {
707 o(0x83);
708 o(0xc0 | (opc << 3) | r);
709 g(c);
711 } else {
712 o(0x81);
713 oad(0xc0 | (opc << 3) | r, c);
715 } else {
716 gv2(RC_INT, RC_INT);
717 r = vtop[-1].r;
718 fr = vtop[0].r;
719 o((opc << 3) | 0x01);
720 o(0xc0 + r + fr * 8);
722 vtop--;
723 if (op >= TOK_ULT && op <= TOK_GT)
724 vset_VT_CMP(op);
725 break;
726 case '-':
727 case TOK_SUBC1: /* sub with carry generation */
728 opc = 5;
729 goto gen_op8;
730 case TOK_ADDC2: /* add with carry use */
731 opc = 2;
732 goto gen_op8;
733 case TOK_SUBC2: /* sub with carry use */
734 opc = 3;
735 goto gen_op8;
736 case '&':
737 opc = 4;
738 goto gen_op8;
739 case '^':
740 opc = 6;
741 goto gen_op8;
742 case '|':
743 opc = 1;
744 goto gen_op8;
745 case '*':
746 gv2(RC_INT, RC_INT);
747 r = vtop[-1].r;
748 fr = vtop[0].r;
749 vtop--;
750 o(0xaf0f); /* imul fr, r */
751 o(0xc0 + fr + r * 8);
752 break;
753 case TOK_SHL:
754 opc = 4;
755 goto gen_shift;
756 case TOK_SHR:
757 opc = 5;
758 goto gen_shift;
759 case TOK_SAR:
760 opc = 7;
761 gen_shift:
762 opc = 0xc0 | (opc << 3);
763 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
764 /* constant case */
765 vswap();
766 r = gv(RC_INT);
767 vswap();
768 c = vtop->c.i & 0x1f;
769 o(0xc1); /* shl/shr/sar $xxx, r */
770 o(opc | r);
771 g(c);
772 } else {
773 /* we generate the shift in ecx */
774 gv2(RC_INT, RC_ECX);
775 r = vtop[-1].r;
776 o(0xd3); /* shl/shr/sar %cl, r */
777 o(opc | r);
779 vtop--;
780 break;
781 case '/':
782 case TOK_UDIV:
783 case TOK_PDIV:
784 case '%':
785 case TOK_UMOD:
786 case TOK_UMULL:
787 /* first operand must be in eax */
788 /* XXX: need better constraint for second operand */
789 gv2(RC_EAX, RC_ECX);
790 r = vtop[-1].r;
791 fr = vtop[0].r;
792 vtop--;
793 save_reg(TREG_EDX);
794 /* save EAX too if used otherwise */
795 save_reg_upstack(TREG_EAX, 1);
796 if (op == TOK_UMULL) {
797 o(0xf7); /* mul fr */
798 o(0xe0 + fr);
799 vtop->r2 = TREG_EDX;
800 r = TREG_EAX;
801 } else {
802 if (op == TOK_UDIV || op == TOK_UMOD) {
803 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
804 o(0xf0 + fr);
805 } else {
806 o(0xf799); /* cltd, idiv fr, %eax */
807 o(0xf8 + fr);
809 if (op == '%' || op == TOK_UMOD)
810 r = TREG_EDX;
811 else
812 r = TREG_EAX;
814 vtop->r = r;
815 break;
816 default:
817 opc = 7;
818 goto gen_op8;
822 /* generate a floating point operation 'v = t1 op t2' instruction. The
823 two operands are guaranteed to have the same floating point type */
824 /* XXX: need to use ST1 too */
825 ST_FUNC void gen_opf(int op)
827 int a, ft, fc, swapped, r;
829 /* convert constants to memory references */
830 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
831 vswap();
832 gv(RC_FLOAT);
833 vswap();
835 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
836 gv(RC_FLOAT);
838 /* must put at least one value in the floating point register */
839 if ((vtop[-1].r & VT_LVAL) &&
840 (vtop[0].r & VT_LVAL)) {
841 vswap();
842 gv(RC_FLOAT);
843 vswap();
845 swapped = 0;
846 /* swap the stack if needed so that t1 is the register and t2 is
847 the memory reference */
848 if (vtop[-1].r & VT_LVAL) {
849 vswap();
850 swapped = 1;
852 if (op >= TOK_ULT && op <= TOK_GT) {
853 /* load on stack second operand */
854 load(TREG_ST0, vtop);
855 save_reg(TREG_EAX); /* eax is used by FP comparison code */
856 if (op == TOK_GE || op == TOK_GT)
857 swapped = !swapped;
858 else if (op == TOK_EQ || op == TOK_NE)
859 swapped = 0;
860 if (swapped)
861 o(0xc9d9); /* fxch %st(1) */
862 if (op == TOK_EQ || op == TOK_NE)
863 o(0xe9da); /* fucompp */
864 else
865 o(0xd9de); /* fcompp */
866 o(0xe0df); /* fnstsw %ax */
867 if (op == TOK_EQ) {
868 o(0x45e480); /* and $0x45, %ah */
869 o(0x40fC80); /* cmp $0x40, %ah */
870 } else if (op == TOK_NE) {
871 o(0x45e480); /* and $0x45, %ah */
872 o(0x40f480); /* xor $0x40, %ah */
873 op = TOK_NE;
874 } else if (op == TOK_GE || op == TOK_LE) {
875 o(0x05c4f6); /* test $0x05, %ah */
876 op = TOK_EQ;
877 } else {
878 o(0x45c4f6); /* test $0x45, %ah */
879 op = TOK_EQ;
881 vtop--;
882 vset_VT_CMP(op);
883 } else {
884 /* no memory reference possible for long double operations */
885 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
886 load(TREG_ST0, vtop);
887 swapped = !swapped;
890 switch(op) {
891 default:
892 case '+':
893 a = 0;
894 break;
895 case '-':
896 a = 4;
897 if (swapped)
898 a++;
899 break;
900 case '*':
901 a = 1;
902 break;
903 case '/':
904 a = 6;
905 if (swapped)
906 a++;
907 break;
909 ft = vtop->type.t;
910 fc = vtop->c.i;
911 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
912 o(0xde); /* fxxxp %st, %st(1) */
913 o(0xc1 + (a << 3));
914 } else {
915 /* if saved lvalue, then we must reload it */
916 r = vtop->r;
917 if ((r & VT_VALMASK) == VT_LLOCAL) {
918 SValue v1;
919 r = get_reg(RC_INT);
920 v1.type.t = VT_INT;
921 v1.r = VT_LOCAL | VT_LVAL;
922 v1.c.i = fc;
923 load(r, &v1);
924 fc = 0;
927 if ((ft & VT_BTYPE) == VT_DOUBLE)
928 o(0xdc);
929 else
930 o(0xd8);
931 gen_modrm(a, r, vtop->sym, fc);
933 vtop--;
937 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
938 and 'long long' cases. */
939 ST_FUNC void gen_cvt_itof(int t)
941 save_reg(TREG_ST0);
942 gv(RC_INT);
943 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
944 /* signed long long to float/double/long double (unsigned case
945 is handled generically) */
946 o(0x50 + vtop->r2); /* push r2 */
947 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
948 o(0x242cdf); /* fildll (%esp) */
949 o(0x08c483); /* add $8, %esp */
950 vtop->r2 = VT_CONST;
951 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
952 (VT_INT | VT_UNSIGNED)) {
953 /* unsigned int to float/double/long double */
954 o(0x6a); /* push $0 */
955 g(0x00);
956 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
957 o(0x242cdf); /* fildll (%esp) */
958 o(0x08c483); /* add $8, %esp */
959 } else {
960 /* int to float/double/long double */
961 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
962 o(0x2404db); /* fildl (%esp) */
963 o(0x04c483); /* add $4, %esp */
965 vtop->r2 = VT_CONST;
966 vtop->r = TREG_ST0;
969 /* convert fp to int 't' type */
970 ST_FUNC void gen_cvt_ftoi(int t)
972 int bt = vtop->type.t & VT_BTYPE;
973 if (bt == VT_FLOAT)
974 vpush_global_sym(&func_old_type, TOK___fixsfdi);
975 else if (bt == VT_LDOUBLE)
976 vpush_global_sym(&func_old_type, TOK___fixxfdi);
977 else
978 vpush_global_sym(&func_old_type, TOK___fixdfdi);
979 vswap();
980 gfunc_call(1);
981 vpushi(0);
982 vtop->r = REG_IRET;
983 if ((t & VT_BTYPE) == VT_LLONG)
984 vtop->r2 = REG_IRE2;
987 /* convert from one floating point type to another */
988 ST_FUNC void gen_cvt_ftof(int t)
990 /* all we have to do on i386 is to put the float in a register */
991 gv(RC_FLOAT);
994 /* char/short to int conversion */
995 ST_FUNC void gen_cvt_csti(int t)
997 int r, sz, xl;
998 r = gv(RC_INT);
999 sz = !(t & VT_UNSIGNED);
1000 xl = (t & VT_BTYPE) == VT_SHORT;
1001 o(0xc0b60f /* mov[sz] %a[xl], %eax */
1002 | (sz << 3 | xl) << 8
1003 | (r << 3 | r) << 16
1007 /* computed goto support */
1008 ST_FUNC void ggoto(void)
1010 gcall_or_jmp(1);
1011 vtop--;
1014 /* bound check support functions */
1015 #ifdef CONFIG_TCC_BCHECK
1016 /* generate a bounded pointer addition */
1017 ST_FUNC void gen_bounded_ptr_add(void)
1019 vpush_global_sym(&func_old_type, TOK___bound_ptr_add);
1020 vrott(3);
1021 gfunc_call(2);
1022 vpushi(0);
1023 /* returned pointer is in eax */
1024 vtop->r = TREG_EAX | VT_BOUNDED;
1025 if (nocode_wanted)
1026 return;
1027 /* relocation offset of the bounding function call point */
1028 vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1031 /* patch pointer addition in vtop so that pointer dereferencing is
1032 also tested */
1033 ST_FUNC void gen_bounded_ptr_deref(void)
1035 addr_t func;
1036 int size, align;
1037 Elf32_Rel *rel;
1038 Sym *sym;
1040 if (nocode_wanted)
1041 return;
1043 size = type_size(&vtop->type, &align);
1044 switch(size) {
1045 case 1: func = TOK___bound_ptr_indir1; break;
1046 case 2: func = TOK___bound_ptr_indir2; break;
1047 case 4: func = TOK___bound_ptr_indir4; break;
1048 case 8: func = TOK___bound_ptr_indir8; break;
1049 case 12: func = TOK___bound_ptr_indir12; break;
1050 case 16: func = TOK___bound_ptr_indir16; break;
1051 default:
1052 /* may happen with struct member access */
1053 return;
1054 //tcc_error("unhandled size when dereferencing bounded pointer");
1055 //func = 0;
1056 //break;
1058 sym = external_global_sym(func, &func_old_type);
1059 if (!sym->c)
1060 put_extern_sym(sym, NULL, 0, 0);
1061 /* patch relocation */
1062 /* XXX: find a better solution ? */
1063 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.i);
1064 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1067 static void gen_bounds_prolog(void)
1069 /* leave some room for bound checking code */
1070 func_bound_offset = lbounds_section->data_offset;
1071 func_bound_ind = ind;
1072 func_bound_alloca_used = 0;
1073 oad(0xb8, 0); /* lbound section pointer */
1074 oad(0xb8, 0); /* call to function */
1077 static void gen_bounds_epilog(void)
1079 addr_t saved_ind;
1080 addr_t *bounds_ptr;
1081 Sym *sym_data;
1083 if (func_bound_offset == lbounds_section->data_offset && !func_bound_alloca_used)
1084 return;
1086 /* add end of table info */
1087 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
1088 *bounds_ptr = 0;
1090 /* generate bound local allocation */
1091 saved_ind = ind;
1092 ind = func_bound_ind;
1093 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
1094 func_bound_offset, lbounds_section->data_offset);
1095 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1096 ind = ind + 5;
1097 gen_static_call(TOK___bound_local_new);
1098 ind = saved_ind;
1100 /* generate bound check local freeing */
1101 o(0x5250); /* save returned value, if any */
1102 greloc(cur_text_section, sym_data, ind + 1, R_386_32);
1103 oad(0xb8, 0); /* mov %eax, xxx */
1104 gen_static_call(TOK___bound_local_delete);
1105 o(0x585a); /* restore returned value, if any */
1107 #endif
1109 /* Save the stack pointer onto the stack */
1110 ST_FUNC void gen_vla_sp_save(int addr) {
1111 /* mov %esp,addr(%ebp)*/
1112 o(0x89);
1113 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1116 /* Restore the SP from a location on the stack */
1117 ST_FUNC void gen_vla_sp_restore(int addr) {
1118 o(0x8b);
1119 gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
1122 /* Subtract from the stack pointer, and push the resulting value onto the stack */
1123 ST_FUNC void gen_vla_alloc(CType *type, int align) {
1124 int use_call = 0;
1126 #if defined(CONFIG_TCC_BCHECK)
1127 use_call = tcc_state->do_bounds_check;
1128 #endif
1129 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
1130 use_call = 1;
1131 #endif
1132 if (use_call)
1134 vpush_global_sym(&func_old_type, TOK_alloca);
1135 vswap(); /* Move alloca ref past allocation size */
1136 gfunc_call(1);
1138 else {
1139 int r;
1140 r = gv(RC_INT); /* allocation size */
1141 /* sub r,%rsp */
1142 o(0x2b);
1143 o(0xe0 | r);
1144 /* We align to 16 bytes rather than align */
1145 /* and ~15, %esp */
1146 o(0xf0e483);
1147 vpop();
1151 /* end of X86 code generator */
1152 /*************************************************************/
1153 #endif
1154 /*************************************************************/