tccasm: accept bracketed offset expressions
[tinycc.git] / i386-gen.c
blobf8795c19370a795f59984919a476765b0c5d8ad5
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 /* generate inc and dec for smaller code */
690 if (c==1 && opc==0) {
691 o (0x40 | r); // inc
692 } else if (c==1 && opc==5) {
693 o (0x48 | r); // dec
694 } else {
695 o(0x83);
696 o(0xc0 | (opc << 3) | r);
697 g(c);
699 } else {
700 o(0x81);
701 oad(0xc0 | (opc << 3) | r, c);
703 } else {
704 gv2(RC_INT, RC_INT);
705 r = vtop[-1].r;
706 fr = vtop[0].r;
707 o((opc << 3) | 0x01);
708 o(0xc0 + r + fr * 8);
710 vtop--;
711 if (op >= TOK_ULT && op <= TOK_GT) {
712 vtop->r = VT_CMP;
713 vtop->c.i = op;
715 break;
716 case '-':
717 case TOK_SUBC1: /* sub with carry generation */
718 opc = 5;
719 goto gen_op8;
720 case TOK_ADDC2: /* add with carry use */
721 opc = 2;
722 goto gen_op8;
723 case TOK_SUBC2: /* sub with carry use */
724 opc = 3;
725 goto gen_op8;
726 case '&':
727 opc = 4;
728 goto gen_op8;
729 case '^':
730 opc = 6;
731 goto gen_op8;
732 case '|':
733 opc = 1;
734 goto gen_op8;
735 case '*':
736 gv2(RC_INT, RC_INT);
737 r = vtop[-1].r;
738 fr = vtop[0].r;
739 vtop--;
740 o(0xaf0f); /* imul fr, r */
741 o(0xc0 + fr + r * 8);
742 break;
743 case TOK_SHL:
744 opc = 4;
745 goto gen_shift;
746 case TOK_SHR:
747 opc = 5;
748 goto gen_shift;
749 case TOK_SAR:
750 opc = 7;
751 gen_shift:
752 opc = 0xc0 | (opc << 3);
753 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
754 /* constant case */
755 vswap();
756 r = gv(RC_INT);
757 vswap();
758 c = vtop->c.i & 0x1f;
759 o(0xc1); /* shl/shr/sar $xxx, r */
760 o(opc | r);
761 g(c);
762 } else {
763 /* we generate the shift in ecx */
764 gv2(RC_INT, RC_ECX);
765 r = vtop[-1].r;
766 o(0xd3); /* shl/shr/sar %cl, r */
767 o(opc | r);
769 vtop--;
770 break;
771 case '/':
772 case TOK_UDIV:
773 case TOK_PDIV:
774 case '%':
775 case TOK_UMOD:
776 case TOK_UMULL:
777 /* first operand must be in eax */
778 /* XXX: need better constraint for second operand */
779 gv2(RC_EAX, RC_ECX);
780 r = vtop[-1].r;
781 fr = vtop[0].r;
782 vtop--;
783 save_reg(TREG_EDX);
784 if (op == TOK_UMULL) {
785 o(0xf7); /* mul fr */
786 o(0xe0 + fr);
787 vtop->r2 = TREG_EDX;
788 r = TREG_EAX;
789 } else {
790 if (op == TOK_UDIV || op == TOK_UMOD) {
791 o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
792 o(0xf0 + fr);
793 } else {
794 o(0xf799); /* cltd, idiv fr, %eax */
795 o(0xf8 + fr);
797 if (op == '%' || op == TOK_UMOD)
798 r = TREG_EDX;
799 else
800 r = TREG_EAX;
802 vtop->r = r;
803 break;
804 default:
805 opc = 7;
806 goto gen_op8;
810 /* generate a floating point operation 'v = t1 op t2' instruction. The
811 two operands are guaranted to have the same floating point type */
812 /* XXX: need to use ST1 too */
813 ST_FUNC void gen_opf(int op)
815 int a, ft, fc, swapped, r;
817 /* convert constants to memory references */
818 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
819 vswap();
820 gv(RC_FLOAT);
821 vswap();
823 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
824 gv(RC_FLOAT);
826 /* must put at least one value in the floating point register */
827 if ((vtop[-1].r & VT_LVAL) &&
828 (vtop[0].r & VT_LVAL)) {
829 vswap();
830 gv(RC_FLOAT);
831 vswap();
833 swapped = 0;
834 /* swap the stack if needed so that t1 is the register and t2 is
835 the memory reference */
836 if (vtop[-1].r & VT_LVAL) {
837 vswap();
838 swapped = 1;
840 if (op >= TOK_ULT && op <= TOK_GT) {
841 /* load on stack second operand */
842 load(TREG_ST0, vtop);
843 save_reg(TREG_EAX); /* eax is used by FP comparison code */
844 if (op == TOK_GE || op == TOK_GT)
845 swapped = !swapped;
846 else if (op == TOK_EQ || op == TOK_NE)
847 swapped = 0;
848 if (swapped)
849 o(0xc9d9); /* fxch %st(1) */
850 o(0xe9da); /* fucompp */
851 o(0xe0df); /* fnstsw %ax */
852 if (op == TOK_EQ) {
853 o(0x45e480); /* and $0x45, %ah */
854 o(0x40fC80); /* cmp $0x40, %ah */
855 } else if (op == TOK_NE) {
856 o(0x45e480); /* and $0x45, %ah */
857 o(0x40f480); /* xor $0x40, %ah */
858 op = TOK_NE;
859 } else if (op == TOK_GE || op == TOK_LE) {
860 o(0x05c4f6); /* test $0x05, %ah */
861 op = TOK_EQ;
862 } else {
863 o(0x45c4f6); /* test $0x45, %ah */
864 op = TOK_EQ;
866 vtop--;
867 vtop->r = VT_CMP;
868 vtop->c.i = op;
869 } else {
870 /* no memory reference possible for long double operations */
871 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
872 load(TREG_ST0, vtop);
873 swapped = !swapped;
876 switch(op) {
877 default:
878 case '+':
879 a = 0;
880 break;
881 case '-':
882 a = 4;
883 if (swapped)
884 a++;
885 break;
886 case '*':
887 a = 1;
888 break;
889 case '/':
890 a = 6;
891 if (swapped)
892 a++;
893 break;
895 ft = vtop->type.t;
896 fc = vtop->c.ul;
897 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
898 o(0xde); /* fxxxp %st, %st(1) */
899 o(0xc1 + (a << 3));
900 } else {
901 /* if saved lvalue, then we must reload it */
902 r = vtop->r;
903 if ((r & VT_VALMASK) == VT_LLOCAL) {
904 SValue v1;
905 r = get_reg(RC_INT);
906 v1.type.t = VT_INT;
907 v1.r = VT_LOCAL | VT_LVAL;
908 v1.c.ul = fc;
909 load(r, &v1);
910 fc = 0;
913 if ((ft & VT_BTYPE) == VT_DOUBLE)
914 o(0xdc);
915 else
916 o(0xd8);
917 gen_modrm(a, r, vtop->sym, fc);
919 vtop--;
923 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
924 and 'long long' cases. */
925 ST_FUNC void gen_cvt_itof(int t)
927 save_reg(TREG_ST0);
928 gv(RC_INT);
929 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
930 /* signed long long to float/double/long double (unsigned case
931 is handled generically) */
932 o(0x50 + vtop->r2); /* push r2 */
933 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
934 o(0x242cdf); /* fildll (%esp) */
935 o(0x08c483); /* add $8, %esp */
936 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
937 (VT_INT | VT_UNSIGNED)) {
938 /* unsigned int to float/double/long double */
939 o(0x6a); /* push $0 */
940 g(0x00);
941 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
942 o(0x242cdf); /* fildll (%esp) */
943 o(0x08c483); /* add $8, %esp */
944 } else {
945 /* int to float/double/long double */
946 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
947 o(0x2404db); /* fildl (%esp) */
948 o(0x04c483); /* add $4, %esp */
950 vtop->r = TREG_ST0;
953 /* convert fp to int 't' type */
954 /* XXX: handle long long case */
955 ST_FUNC void gen_cvt_ftoi(int t)
957 int r, r2, size;
958 Sym *sym;
959 CType ushort_type;
961 ushort_type.t = VT_SHORT | VT_UNSIGNED;
962 ushort_type.ref = 0;
964 gv(RC_FLOAT);
965 if (t != VT_INT)
966 size = 8;
967 else
968 size = 4;
970 o(0x2dd9); /* ldcw xxx */
971 sym = external_global_sym(TOK___tcc_int_fpu_control,
972 &ushort_type, VT_LVAL);
973 greloc(cur_text_section, sym,
974 ind, R_386_32);
975 gen_le32(0);
977 oad(0xec81, size); /* sub $xxx, %esp */
978 if (size == 4)
979 o(0x1cdb); /* fistpl */
980 else
981 o(0x3cdf); /* fistpll */
982 o(0x24);
983 o(0x2dd9); /* ldcw xxx */
984 sym = external_global_sym(TOK___tcc_fpu_control,
985 &ushort_type, VT_LVAL);
986 greloc(cur_text_section, sym,
987 ind, R_386_32);
988 gen_le32(0);
990 r = get_reg(RC_INT);
991 o(0x58 + r); /* pop r */
992 if (size == 8) {
993 if (t == VT_LLONG) {
994 vtop->r = r; /* mark reg as used */
995 r2 = get_reg(RC_INT);
996 o(0x58 + r2); /* pop r2 */
997 vtop->r2 = r2;
998 } else {
999 o(0x04c483); /* add $4, %esp */
1002 vtop->r = r;
1005 /* convert from one floating point type to another */
1006 ST_FUNC void gen_cvt_ftof(int t)
1008 /* all we have to do on i386 is to put the float in a register */
1009 gv(RC_FLOAT);
1012 /* computed goto support */
1013 ST_FUNC void ggoto(void)
1015 gcall_or_jmp(1);
1016 vtop--;
1019 /* bound check support functions */
1020 #ifdef CONFIG_TCC_BCHECK
1022 /* generate a bounded pointer addition */
1023 ST_FUNC void gen_bounded_ptr_add(void)
1025 Sym *sym;
1027 /* prepare fast i386 function call (args in eax and edx) */
1028 gv2(RC_EAX, RC_EDX);
1029 /* save all temporary registers */
1030 vtop -= 2;
1031 save_regs(0);
1032 /* do a fast function call */
1033 sym = external_global_sym(TOK___bound_ptr_add, &func_old_type, 0);
1034 greloc(cur_text_section, sym,
1035 ind + 1, R_386_PC32);
1036 oad(0xe8, -4);
1037 /* returned pointer is in eax */
1038 vtop++;
1039 vtop->r = TREG_EAX | VT_BOUNDED;
1040 /* address of bounding function call point */
1041 vtop->c.ul = (cur_text_section->reloc->data_offset - sizeof(Elf32_Rel));
1044 /* patch pointer addition in vtop so that pointer dereferencing is
1045 also tested */
1046 ST_FUNC void gen_bounded_ptr_deref(void)
1048 int func;
1049 int size, align;
1050 Elf32_Rel *rel;
1051 Sym *sym;
1053 size = 0;
1054 /* XXX: put that code in generic part of tcc */
1055 if (!is_float(vtop->type.t)) {
1056 if (vtop->r & VT_LVAL_BYTE)
1057 size = 1;
1058 else if (vtop->r & VT_LVAL_SHORT)
1059 size = 2;
1061 if (!size)
1062 size = type_size(&vtop->type, &align);
1063 switch(size) {
1064 case 1: func = TOK___bound_ptr_indir1; break;
1065 case 2: func = TOK___bound_ptr_indir2; break;
1066 case 4: func = TOK___bound_ptr_indir4; break;
1067 case 8: func = TOK___bound_ptr_indir8; break;
1068 case 12: func = TOK___bound_ptr_indir12; break;
1069 case 16: func = TOK___bound_ptr_indir16; break;
1070 default:
1071 error("unhandled size when derefencing bounded pointer");
1072 func = 0;
1073 break;
1076 /* patch relocation */
1077 /* XXX: find a better solution ? */
1078 rel = (Elf32_Rel *)(cur_text_section->reloc->data + vtop->c.ul);
1079 sym = external_global_sym(func, &func_old_type, 0);
1080 if (!sym->c)
1081 put_extern_sym(sym, NULL, 0, 0);
1082 rel->r_info = ELF32_R_INFO(sym->c, ELF32_R_TYPE(rel->r_info));
1084 #endif
1086 /* end of X86 code generator */
1087 /*************************************************************/
1088 #endif
1089 /*************************************************************/