x86-64: use r8/r9 as generic integer registers
[tinycc.git] / x86_64-gen.c
blob81aa5bef856c5a2512258e8d08d374e6fe7b2abe
1 /*
2 * x86-64 code generator for TCC
4 * Copyright (c) 2008 Shinichiro Hamaji
6 * Based on i386-gen.c by Fabrice Bellard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef TARGET_DEFS_ONLY
25 /* number of available registers */
26 #define NB_REGS 10
27 #define NB_ASM_REGS 8
29 /* a register can belong to several classes. The classes must be
30 sorted from more general to more precise (see gv2() code which does
31 assumptions on it). */
32 #define RC_INT 0x0001 /* generic integer register */
33 #define RC_FLOAT 0x0002 /* generic float register */
34 #define RC_RAX 0x0004
35 #define RC_RCX 0x0008
36 #define RC_RDX 0x0010
37 #define RC_R8 0x0100
38 #define RC_R9 0x0200
39 #define RC_XMM0 0x0020
40 #define RC_ST0 0x0040 /* only for long double */
41 #define RC_IRET RC_RAX /* function return: integer register */
42 #define RC_LRET RC_RDX /* function return: second integer register */
43 #define RC_FRET RC_XMM0 /* function return: float register */
45 /* pretty names for the registers */
46 enum {
47 TREG_RAX = 0,
48 TREG_RCX = 1,
49 TREG_RDX = 2,
50 TREG_XMM0 = 3,
51 TREG_ST0 = 4,
53 TREG_RSI = 6,
54 TREG_RDI = 7,
55 TREG_R8 = 8,
56 TREG_R9 = 9,
58 TREG_R10 = 10,
59 TREG_R11 = 11,
61 TREG_MEM = 0x10,
64 #define REX_BASE(reg) (((reg) >> 3) & 1)
65 #define REG_VALUE(reg) ((reg) & 7)
67 /* return registers for function */
68 #define REG_IRET TREG_RAX /* single word int return register */
69 #define REG_LRET TREG_RDX /* second word return register (for long long) */
70 #define REG_FRET TREG_XMM0 /* float return register */
72 /* defined if function parameters must be evaluated in reverse order */
73 #define INVERT_FUNC_PARAMS
75 /* pointer size, in bytes */
76 #define PTR_SIZE 8
78 /* long double size and alignment, in bytes */
79 #define LDOUBLE_SIZE 16
80 #define LDOUBLE_ALIGN 8
81 /* maximum alignment (for aligned attribute support) */
82 #define MAX_ALIGN 8
84 ST_FUNC void gen_opl(int op);
85 ST_FUNC void gen_le64(int64_t c);
87 /******************************************************/
88 /* ELF defines */
90 #define EM_TCC_TARGET EM_X86_64
92 /* relocation type for 32 bit data relocation */
93 #define R_DATA_32 R_X86_64_32
94 #define R_DATA_PTR R_X86_64_64
95 #define R_JMP_SLOT R_X86_64_JUMP_SLOT
96 #define R_COPY R_X86_64_COPY
98 #define ELF_START_ADDR 0x08048000
99 #define ELF_PAGE_SIZE 0x1000
101 /******************************************************/
102 #else /* ! TARGET_DEFS_ONLY */
103 /******************************************************/
104 #include "tcc.h"
105 #include <assert.h>
107 ST_DATA const int reg_classes[NB_REGS] = {
108 /* eax */ RC_INT | RC_RAX,
109 /* ecx */ RC_INT | RC_RCX,
110 /* edx */ RC_INT | RC_RDX,
111 /* xmm0 */ RC_FLOAT | RC_XMM0,
112 /* st0 */ RC_ST0,
113 #if NB_REGS == 10
117 RC_INT | RC_R8,
118 RC_INT | RC_R9,
119 #endif
122 static unsigned long func_sub_sp_offset;
123 static int func_ret_sub;
125 /* XXX: make it faster ? */
126 void g(int c)
128 int ind1;
129 ind1 = ind + 1;
130 if (ind1 > cur_text_section->data_allocated)
131 section_realloc(cur_text_section, ind1);
132 cur_text_section->data[ind] = c;
133 ind = ind1;
136 void o(unsigned int c)
138 while (c) {
139 g(c);
140 c = c >> 8;
144 void gen_le16(int v)
146 g(v);
147 g(v >> 8);
150 void gen_le32(int c)
152 g(c);
153 g(c >> 8);
154 g(c >> 16);
155 g(c >> 24);
158 void gen_le64(int64_t c)
160 g(c);
161 g(c >> 8);
162 g(c >> 16);
163 g(c >> 24);
164 g(c >> 32);
165 g(c >> 40);
166 g(c >> 48);
167 g(c >> 56);
170 void orex(int ll, int r, int r2, int b)
172 if ((r & VT_VALMASK) >= VT_CONST)
173 r = 0;
174 if ((r2 & VT_VALMASK) >= VT_CONST)
175 r2 = 0;
176 if (ll || REX_BASE(r) || REX_BASE(r2))
177 o(0x40 | REX_BASE(r) | (REX_BASE(r2) << 2) | (ll << 3));
178 o(b);
181 /* output a symbol and patch all calls to it */
182 void gsym_addr(int t, int a)
184 int n, *ptr;
185 while (t) {
186 ptr = (int *)(cur_text_section->data + t);
187 n = *ptr; /* next value */
188 *ptr = a - t - 4;
189 t = n;
193 void gsym(int t)
195 gsym_addr(t, ind);
198 /* psym is used to put an instruction with a data field which is a
199 reference to a symbol. It is in fact the same as oad ! */
200 #define psym oad
202 static int is64_type(int t)
204 return ((t & VT_BTYPE) == VT_PTR ||
205 (t & VT_BTYPE) == VT_FUNC ||
206 (t & VT_BTYPE) == VT_LLONG);
209 static int is_sse_float(int t) {
210 int bt;
211 bt = t & VT_BTYPE;
212 return bt == VT_DOUBLE || bt == VT_FLOAT;
216 /* instruction + 4 bytes data. Return the address of the data */
217 ST_FUNC int oad(int c, int s)
219 int ind1;
221 o(c);
222 ind1 = ind + 4;
223 if (ind1 > cur_text_section->data_allocated)
224 section_realloc(cur_text_section, ind1);
225 *(int *)(cur_text_section->data + ind) = s;
226 s = ind;
227 ind = ind1;
228 return s;
231 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
233 if (r & VT_SYM)
234 greloc(cur_text_section, sym, ind, R_X86_64_32);
235 gen_le32(c);
238 /* output constant with relocation if 'r & VT_SYM' is true */
239 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c)
241 if (r & VT_SYM)
242 greloc(cur_text_section, sym, ind, R_X86_64_64);
243 gen_le64(c);
246 /* output constant with relocation if 'r & VT_SYM' is true */
247 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
249 if (r & VT_SYM)
250 greloc(cur_text_section, sym, ind, R_X86_64_PC32);
251 gen_le32(c-4);
254 /* output got address with relocation */
255 static void gen_gotpcrel(int r, Sym *sym, int c)
257 #ifndef TCC_TARGET_PE
258 Section *sr;
259 ElfW(Rela) *rel;
260 greloc(cur_text_section, sym, ind, R_X86_64_GOTPCREL);
261 sr = cur_text_section->reloc;
262 rel = (ElfW(Rela) *)(sr->data + sr->data_offset - sizeof(ElfW(Rela)));
263 rel->r_addend = -4;
264 #else
265 printf("picpic: %s %x %x | %02x %02x %02x\n", get_tok_str(sym->v, NULL), c, r,
266 cur_text_section->data[ind-3],
267 cur_text_section->data[ind-2],
268 cur_text_section->data[ind-1]
270 greloc(cur_text_section, sym, ind, R_X86_64_PC32);
271 #endif
272 gen_le32(0);
274 if (c) {
275 /* we use add c, %xxx for displacement */
276 orex(1, r, 0, 0x81);
277 o(0xc0 + REG_VALUE(r));
278 gen_le32(c);
282 static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got)
284 op_reg = REG_VALUE(op_reg) << 3;
285 if ((r & VT_VALMASK) == VT_CONST) {
286 /* constant memory reference */
287 o(0x05 | op_reg);
288 if (is_got) {
289 gen_gotpcrel(r, sym, c);
290 } else {
291 gen_addrpc32(r, sym, c);
293 } else if ((r & VT_VALMASK) == VT_LOCAL) {
294 /* currently, we use only ebp as base */
295 if (c == (char)c) {
296 /* short reference */
297 o(0x45 | op_reg);
298 g(c);
299 } else {
300 oad(0x85 | op_reg, c);
302 } else if ((r & VT_VALMASK) >= TREG_MEM) {
303 if (c) {
304 g(0x80 | op_reg | REG_VALUE(r));
305 gen_le32(c);
306 } else {
307 g(0x00 | op_reg | REG_VALUE(r));
309 } else {
310 g(0x00 | op_reg | REG_VALUE(r));
314 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
315 opcode bits */
316 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
318 gen_modrm_impl(op_reg, r, sym, c, 0);
321 /* generate a modrm reference. 'op_reg' contains the addtionnal 3
322 opcode bits */
323 static void gen_modrm64(int opcode, int op_reg, int r, Sym *sym, int c)
325 int is_got;
326 orex(1, r, op_reg, opcode);
327 is_got = (op_reg & TREG_MEM) && !(sym->type.t & VT_STATIC);
328 gen_modrm_impl(op_reg, r, sym, c, is_got);
332 /* load 'r' from value 'sv' */
333 void load(int r, SValue *sv)
335 int v, t, ft, fc, fr;
336 SValue v1;
338 #ifdef TCC_TARGET_PE
339 if (pe_dllimport(r, sv, load))
340 return;
341 #endif
343 fr = sv->r;
344 ft = sv->type.t;
345 fc = sv->c.ul;
347 #ifndef TCC_TARGET_PE
348 /* we use indirect access via got */
349 if ((fr & VT_VALMASK) == VT_CONST && (fr & VT_SYM) &&
350 (fr & VT_LVAL) && !(sv->sym->type.t & VT_STATIC)) {
351 /* use the result register as a temporal register */
352 int tr = r | TREG_MEM;
353 if (is_float(ft)) {
354 /* we cannot use float registers as a temporal register */
355 tr = get_reg(RC_INT) | TREG_MEM;
357 gen_modrm64(0x8b, tr, fr, sv->sym, 0);
359 /* load from the temporal register */
360 fr = tr | VT_LVAL;
362 #endif
364 v = fr & VT_VALMASK;
365 if (fr & VT_LVAL) {
366 int b, ll;
367 if (v == VT_LLOCAL) {
368 v1.type.t = VT_PTR;
369 v1.r = VT_LOCAL | VT_LVAL;
370 v1.c.ul = fc;
371 load(r, &v1);
372 fr = r;
374 ll = 0;
375 if ((ft & VT_BTYPE) == VT_FLOAT) {
376 b = 0x6e0f66, r = 0; /* movd */
377 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
378 b = 0x7e0ff3, r = 0; /* movq */
379 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
380 b = 0xdb, r = 5; /* fldt */
381 } else if ((ft & VT_TYPE) == VT_BYTE) {
382 b = 0xbe0f; /* movsbl */
383 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
384 b = 0xb60f; /* movzbl */
385 } else if ((ft & VT_TYPE) == VT_SHORT) {
386 b = 0xbf0f; /* movswl */
387 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
388 b = 0xb70f; /* movzwl */
389 } else {
390 ll = is64_type(ft);
391 b = 0x8b;
393 if (ll) {
394 gen_modrm64(b, r, fr, sv->sym, fc);
395 } else {
396 orex(ll, fr, r, b);
397 gen_modrm(r, fr, sv->sym, fc);
399 } else {
400 if (v == VT_CONST) {
401 if (fr & VT_SYM) {
402 #ifdef TCC_TARGET_PE
403 orex(1,0,r,0x8d);
404 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
405 gen_addrpc32(fr, sv->sym, fc);
406 #else
407 if (sv->sym->type.t & VT_STATIC) {
408 orex(1,0,r,0x8d);
409 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
410 gen_addrpc32(fr, sv->sym, fc);
411 } else {
412 orex(1,0,r,0x8b);
413 o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */
414 gen_gotpcrel(fr, sv->sym, fc);
416 #endif
417 } else if (is64_type(ft)) {
418 orex(1,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
419 gen_le64(sv->c.ull);
420 } else {
421 orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
422 gen_le32(fc);
424 } else if (v == VT_LOCAL) {
425 orex(1,0,r,0x8d); /* lea xxx(%ebp), r */
426 gen_modrm(r, VT_LOCAL, sv->sym, fc);
427 } else if (v == VT_CMP) {
428 orex(0,r,0,0);
429 oad(0xb8 + REG_VALUE(r), 0); /* mov $0, r */
430 orex(0,r,0, 0x0f); /* setxx %br */
431 o(fc);
432 o(0xc0 + REG_VALUE(r));
433 } else if (v == VT_JMP || v == VT_JMPI) {
434 t = v & 1;
435 orex(0,r,0,0);
436 oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */
437 o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */
438 gsym(fc);
439 orex(0,r,0,0);
440 oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */
441 } else if (v != r) {
442 if (r == TREG_XMM0) {
443 assert(v == TREG_ST0);
444 /* gen_cvt_ftof(VT_DOUBLE); */
445 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
446 /* movsd -0x10(%rsp),%xmm0 */
447 o(0x44100ff2);
448 o(0xf024);
449 } else if (r == TREG_ST0) {
450 assert(v == TREG_XMM0);
451 /* gen_cvt_ftof(VT_LDOUBLE); */
452 /* movsd %xmm0,-0x10(%rsp) */
453 o(0x44110ff2);
454 o(0xf024);
455 o(0xf02444dd); /* fldl -0x10(%rsp) */
456 } else {
457 orex(1,r,v, 0x89);
458 o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */
464 /* store register 'r' in lvalue 'v' */
465 void store(int r, SValue *v)
467 int fr, bt, ft, fc;
468 int op64 = 0;
469 /* store the REX prefix in this variable when PIC is enabled */
470 int pic = 0;
472 #ifdef TCC_TARGET_PE
473 if (pe_dllimport(r, v, store))
474 return;
475 #endif
477 ft = v->type.t;
478 fc = v->c.ul;
479 fr = v->r & VT_VALMASK;
480 bt = ft & VT_BTYPE;
482 #ifndef TCC_TARGET_PE
483 /* we need to access the variable via got */
484 if (fr == VT_CONST && (v->r & VT_SYM)) {
485 /* mov xx(%rip), %r11 */
486 o(0x1d8b4c);
487 gen_gotpcrel(TREG_R11, v->sym, v->c.ul);
488 pic = is64_type(bt) ? 0x49 : 0x41;
490 #endif
492 /* XXX: incorrect if float reg to reg */
493 if (bt == VT_FLOAT) {
494 o(0x66);
495 o(pic);
496 o(0x7e0f); /* movd */
497 r = 0;
498 } else if (bt == VT_DOUBLE) {
499 o(0x66);
500 o(pic);
501 o(0xd60f); /* movq */
502 r = 0;
503 } else if (bt == VT_LDOUBLE) {
504 o(0xc0d9); /* fld %st(0) */
505 o(pic);
506 o(0xdb); /* fstpt */
507 r = 7;
508 } else {
509 if (bt == VT_SHORT)
510 o(0x66);
511 o(pic);
512 if (bt == VT_BYTE || bt == VT_BOOL)
513 orex(0, 0, r, 0x88);
514 else if (is64_type(bt))
515 op64 = 0x89;
516 else
517 orex(0, 0, r, 0x89);
519 if (pic) {
520 /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
521 if (op64)
522 o(op64);
523 o(3 + (r << 3));
524 } else if (op64) {
525 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
526 gen_modrm64(op64, r, v->r, v->sym, fc);
527 } else if (fr != r) {
528 /* XXX: don't we really come here? */
529 abort();
530 o(0xc0 + fr + r * 8); /* mov r, fr */
532 } else {
533 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
534 gen_modrm(r, v->r, v->sym, fc);
535 } else if (fr != r) {
536 /* XXX: don't we really come here? */
537 abort();
538 o(0xc0 + fr + r * 8); /* mov r, fr */
543 /* 'is_jmp' is '1' if it is a jump */
544 static void gcall_or_jmp(int is_jmp)
546 int r;
547 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
548 /* constant case */
549 if (vtop->r & VT_SYM) {
550 /* relocation case */
551 greloc(cur_text_section, vtop->sym,
552 ind + 1, R_X86_64_PC32);
553 } else {
554 /* put an empty PC32 relocation */
555 put_elf_reloc(symtab_section, cur_text_section,
556 ind + 1, R_X86_64_PC32, 0);
558 oad(0xe8 + is_jmp, vtop->c.ul - 4); /* call/jmp im */
559 } else {
560 /* otherwise, indirect call */
561 r = TREG_R11;
562 load(r, vtop);
563 o(0x41); /* REX */
564 o(0xff); /* call/jmp *r */
565 o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
569 #ifdef TCC_TARGET_PE
571 #define REGN 4
572 static const uint8_t arg_regs[] = {
573 TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
576 static int func_scratch;
578 /* Generate function call. The function address is pushed first, then
579 all the parameters in call order. This functions pops all the
580 parameters and the function address. */
582 void gen_offs_sp(int b, int r, int d)
584 orex(1,0,r & 0x100 ? 0 : r, b);
585 if (d == (char)d) {
586 o(0x2444 | (REG_VALUE(r) << 3));
587 g(d);
588 } else {
589 o(0x2484 | (REG_VALUE(r) << 3));
590 gen_le32(d);
594 void gfunc_call(int nb_args)
596 int size, align, r, args_size, i, d, j, bt;
597 int nb_reg_args, gen_reg;
599 /* calculate the number of integer/float arguments */
600 nb_reg_args = 0;
601 for(i = 0; i < nb_args; i++) {
602 bt = (vtop[-i].type.t & VT_BTYPE);
603 if (bt != VT_STRUCT && bt != VT_LDOUBLE)
604 nb_reg_args++;
607 args_size = (nb_reg_args < REGN ? REGN : nb_reg_args) * PTR_SIZE;
609 /* for struct arguments, we need to call memcpy and the function
610 call breaks register passing arguments we are preparing.
611 So, we process arguments which will be passed by stack first. */
612 for(i = 0; i < nb_args; i++) {
613 SValue *sv = &vtop[-i];
614 bt = (sv->type.t & VT_BTYPE);
615 if (bt == VT_STRUCT) {
616 size = type_size(&sv->type, &align);
617 /* align to stack align size */
618 size = (size + 15) & ~16;
619 /* generate structure store */
620 r = get_reg(RC_INT);
621 gen_offs_sp(0x8d, r, args_size);
622 args_size += size;
624 /* generate memcpy call */
625 vset(&sv->type, r | VT_LVAL, 0);
626 vpushv(sv);
627 vstore();
628 --vtop;
630 } else if (bt == VT_LDOUBLE) {
632 gv(RC_ST0);
633 gen_offs_sp(0xdb, 0x107, args_size);
634 args_size += 16;
639 if (func_scratch < args_size)
640 func_scratch = args_size;
642 for (i = 0; i < REGN; ++i)
643 save_reg(arg_regs[i]);
645 gen_reg = nb_reg_args;
646 for(i = 0; i < nb_args; i++) {
647 bt = (vtop->type.t & VT_BTYPE);
648 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
649 ; /* done */
650 } else if (is_sse_float(vtop->type.t)) {
651 gv(RC_FLOAT); /* only one float register */
652 j = --gen_reg;
653 if (j >= REGN) {
654 /* movq %xmm0, j*8(%rsp) */
655 gen_offs_sp(0xd60f66, 0x100, j*8);
656 } else {
657 /* movaps %xmm0, %xmmN */
658 o(0x280f);
659 o(0xc0 + (j << 3));
660 d = arg_regs[j];
661 /* mov %xmm0, %rxx */
662 o(0x66);
663 orex(1,d,0, 0x7e0f);
664 o(0xc0 + REG_VALUE(d));
666 } else {
667 j = --gen_reg;
668 if (j >= REGN) {
669 r = gv(RC_INT);
670 gen_offs_sp(0x89, r, j*8);
671 } else {
672 d = arg_regs[j];
673 if (d < NB_REGS) {
674 gv(reg_classes[d] & ~RC_INT);
675 } else {
676 r = gv(RC_INT);
677 if (d != r) {
678 orex(1,d,r, 0x89);
679 o(0xc0 + REG_VALUE(d) + REG_VALUE(r) * 8);
685 vtop--;
687 save_regs(0);
688 gcall_or_jmp(0);
689 vtop--;
693 #define FUNC_PROLOG_SIZE 11
695 /* generate function prolog of type 't' */
696 void gfunc_prolog(CType *func_type)
698 int addr, align, size, reg_param_index, bt;
699 Sym *sym;
700 CType *type;
702 func_ret_sub = 0;
703 func_scratch = 0;
704 loc = 0;
706 addr = PTR_SIZE * 2;
707 ind += FUNC_PROLOG_SIZE;
708 func_sub_sp_offset = ind;
709 reg_param_index = 0;
711 sym = func_type->ref;
713 /* if the function returns a structure, then add an
714 implicit pointer parameter */
715 func_vt = sym->type;
716 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
717 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
718 reg_param_index++;
719 addr += PTR_SIZE;
722 /* define parameters */
723 while ((sym = sym->next) != NULL) {
724 type = &sym->type;
725 bt = type->t & VT_BTYPE;
726 if (bt == VT_STRUCT || bt == VT_LDOUBLE)
727 continue;
728 if (reg_param_index < REGN) {
729 /* save arguments passed by register */
730 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
732 sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, addr);
733 reg_param_index++;
734 addr += PTR_SIZE;
737 while (reg_param_index < REGN) {
738 if (func_type->ref->c == FUNC_ELLIPSIS)
739 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
740 reg_param_index++;
741 addr += PTR_SIZE;
744 sym = func_type->ref;
745 while ((sym = sym->next) != NULL) {
746 type = &sym->type;
747 bt = type->t & VT_BTYPE;
748 if (bt == VT_STRUCT || bt == VT_LDOUBLE) {
749 size = type_size(type, &align);
750 size = (size + 15) & -16;
751 sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, addr);
752 addr += size;
757 /* generate function epilog */
758 void gfunc_epilog(void)
760 int v, saved_ind;
762 o(0xc9); /* leave */
763 if (func_ret_sub == 0) {
764 o(0xc3); /* ret */
765 } else {
766 o(0xc2); /* ret n */
767 g(func_ret_sub);
768 g(func_ret_sub >> 8);
771 saved_ind = ind;
772 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
773 /* align local size to word & save local variables */
774 v = (func_scratch + -loc + 15) & -16;
776 pe_add_unwind_data(ind, saved_ind, v);
778 if (v >= 4096) {
779 Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
780 oad(0xb8, v); /* mov stacksize, %eax */
781 oad(0xe8, -4); /* call __chkstk, (does the stackframe too) */
782 greloc(cur_text_section, sym, ind-4, R_X86_64_PC32);
783 o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
784 } else {
785 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
786 o(0xec8148); /* sub rsp, stacksize */
787 gen_le32(v);
789 ind = saved_ind;
792 #else
794 static void gadd_sp(int val)
796 if (val == (char)val) {
797 o(0xc48348);
798 g(val);
799 } else {
800 oad(0xc48148, val); /* add $xxx, %rsp */
804 #define REGN 6
805 static const uint8_t arg_regs[REGN] = {
806 TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
809 /* Generate function call. The function address is pushed first, then
810 all the parameters in call order. This functions pops all the
811 parameters and the function address. */
812 void gfunc_call(int nb_args)
814 int size, align, r, args_size, i;
815 SValue *orig_vtop;
816 int nb_reg_args = 0;
817 int nb_sse_args = 0;
818 int sse_reg, gen_reg;
820 /* calculate the number of integer/float arguments */
821 args_size = 0;
822 for(i = 0; i < nb_args; i++) {
823 if ((vtop[-i].type.t & VT_BTYPE) == VT_STRUCT) {
824 args_size += type_size(&vtop->type, &align);
825 } else if ((vtop[-i].type.t & VT_BTYPE) == VT_LDOUBLE) {
826 args_size += 16;
827 } else if (is_sse_float(vtop[-i].type.t)) {
828 nb_sse_args++;
829 if (nb_sse_args > 8) args_size += 8;
830 } else {
831 nb_reg_args++;
832 if (nb_reg_args > REGN) args_size += 8;
836 save_regs(0); /* save used temporary registers */
838 /* for struct arguments, we need to call memcpy and the function
839 call breaks register passing arguments we are preparing.
840 So, we process arguments which will be passed by stack first. */
841 orig_vtop = vtop;
842 gen_reg = nb_reg_args;
843 sse_reg = nb_sse_args;
845 /* adjust stack to align SSE boundary */
846 if (args_size &= 8) {
847 o(0x50); /* push $rax */
849 for(i = 0; i < nb_args; i++) {
850 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
851 size = type_size(&vtop->type, &align);
852 /* align to stack align size */
853 size = (size + 3) & ~3;
854 /* allocate the necessary size on stack */
855 o(0x48);
856 oad(0xec81, size); /* sub $xxx, %rsp */
857 /* generate structure store */
858 r = get_reg(RC_INT);
859 orex(1, r, 0, 0x89); /* mov %rsp, r */
860 o(0xe0 + REG_VALUE(r));
862 /* following code breaks vtop[1] */
863 SValue tmp = vtop[1];
864 vset(&vtop->type, r | VT_LVAL, 0);
865 vswap();
866 vstore();
867 vtop[1] = tmp;
869 args_size += size;
870 } else if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
871 gv(RC_ST0);
872 size = LDOUBLE_SIZE;
873 oad(0xec8148, size); /* sub $xxx, %rsp */
874 o(0x7cdb); /* fstpt 0(%rsp) */
875 g(0x24);
876 g(0x00);
877 args_size += size;
878 } else if (is_sse_float(vtop->type.t)) {
879 int j = --sse_reg;
880 if (j >= 8) {
881 gv(RC_FLOAT);
882 o(0x50); /* push $rax */
883 /* movq %xmm0, (%rsp) */
884 o(0x04d60f66);
885 o(0x24);
886 args_size += 8;
888 } else {
889 int j = --gen_reg;
890 /* simple type */
891 /* XXX: implicit cast ? */
892 if (j >= REGN) {
893 r = gv(RC_INT);
894 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */
895 args_size += 8;
898 vtop--;
900 vtop = orig_vtop;
903 /* then, we prepare register passing arguments.
904 Note that we cannot set RDX and RCX in this loop because gv()
905 may break these temporary registers. Let's use R10 and R11
906 instead of them */
907 gen_reg = nb_reg_args;
908 sse_reg = nb_sse_args;
909 for(i = 0; i < nb_args; i++) {
910 if ((vtop->type.t & VT_BTYPE) == VT_STRUCT ||
911 (vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
912 } else if (is_sse_float(vtop->type.t)) {
913 int j = --sse_reg;
914 if (j < 8) {
915 gv(RC_FLOAT); /* only one float register */
916 /* movaps %xmm0, %xmmN */
917 o(0x280f);
918 o(0xc0 + (sse_reg << 3));
920 } else {
921 int j = --gen_reg;
922 /* simple type */
923 /* XXX: implicit cast ? */
924 if (j < REGN) {
925 r = gv(RC_INT);
926 if (j < 2) {
927 o(0x8948); /* mov */
928 o(0xc0 + r * 8 + arg_regs[j]);
929 } else if (j < 4) {
930 o(0x8949); /* mov */
931 /* j=2: r10, j=3: r11 */
932 o(0xc0 + r * 8 + j);
933 } else {
934 o(0x8949); /* mov */
935 /* j=4: r8, j=5: r9 */
936 o(0xc0 + r * 8 + j - 4);
940 vtop--;
943 /* Copy R10 and R11 into RDX and RCX, respectively */
944 if (nb_reg_args > 2) {
945 o(0xd2894c); /* mov %r10, %rdx */
946 if (nb_reg_args > 3) {
947 o(0xd9894c); /* mov %r11, %rcx */
951 oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
952 gcall_or_jmp(0);
953 if (args_size)
954 gadd_sp(args_size);
955 vtop--;
959 #define FUNC_PROLOG_SIZE 11
961 static void push_arg_reg(int i) {
962 loc -= 8;
963 gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
966 /* generate function prolog of type 't' */
967 void gfunc_prolog(CType *func_type)
969 int i, addr, align, size;
970 int param_index, param_addr, reg_param_index, sse_param_index;
971 Sym *sym;
972 CType *type;
974 sym = func_type->ref;
975 addr = PTR_SIZE * 2;
976 loc = 0;
977 ind += FUNC_PROLOG_SIZE;
978 func_sub_sp_offset = ind;
979 func_ret_sub = 0;
981 if (func_type->ref->c == FUNC_ELLIPSIS) {
982 int seen_reg_num, seen_sse_num, seen_stack_size;
983 seen_reg_num = seen_sse_num = 0;
984 /* frame pointer and return address */
985 seen_stack_size = PTR_SIZE * 2;
986 /* count the number of seen parameters */
987 sym = func_type->ref;
988 while ((sym = sym->next) != NULL) {
989 type = &sym->type;
990 if (is_sse_float(type->t)) {
991 if (seen_sse_num < 8) {
992 seen_sse_num++;
993 } else {
994 seen_stack_size += 8;
996 } else if ((type->t & VT_BTYPE) == VT_STRUCT) {
997 size = type_size(type, &align);
998 size = (size + 3) & ~3;
999 seen_stack_size += size;
1000 } else if ((type->t & VT_BTYPE) == VT_LDOUBLE) {
1001 seen_stack_size += LDOUBLE_SIZE;
1002 } else {
1003 if (seen_reg_num < REGN) {
1004 seen_reg_num++;
1005 } else {
1006 seen_stack_size += 8;
1011 loc -= 16;
1012 /* movl $0x????????, -0x10(%rbp) */
1013 o(0xf045c7);
1014 gen_le32(seen_reg_num * 8);
1015 /* movl $0x????????, -0xc(%rbp) */
1016 o(0xf445c7);
1017 gen_le32(seen_sse_num * 16 + 48);
1018 /* movl $0x????????, -0x8(%rbp) */
1019 o(0xf845c7);
1020 gen_le32(seen_stack_size);
1022 /* save all register passing arguments */
1023 for (i = 0; i < 8; i++) {
1024 loc -= 16;
1025 o(0xd60f66); /* movq */
1026 gen_modrm(7 - i, VT_LOCAL, NULL, loc);
1027 /* movq $0, loc+8(%rbp) */
1028 o(0x85c748);
1029 gen_le32(loc + 8);
1030 gen_le32(0);
1032 for (i = 0; i < REGN; i++) {
1033 push_arg_reg(REGN-1-i);
1037 sym = func_type->ref;
1038 param_index = 0;
1039 reg_param_index = 0;
1040 sse_param_index = 0;
1042 /* if the function returns a structure, then add an
1043 implicit pointer parameter */
1044 func_vt = sym->type;
1045 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
1046 push_arg_reg(reg_param_index);
1047 param_addr = loc;
1049 func_vc = loc;
1050 param_index++;
1051 reg_param_index++;
1053 /* define parameters */
1054 while ((sym = sym->next) != NULL) {
1055 type = &sym->type;
1056 size = type_size(type, &align);
1057 size = (size + 3) & ~3;
1058 if (is_sse_float(type->t)) {
1059 if (sse_param_index < 8) {
1060 /* save arguments passed by register */
1061 loc -= 8;
1062 o(0xd60f66); /* movq */
1063 gen_modrm(sse_param_index, VT_LOCAL, NULL, loc);
1064 param_addr = loc;
1065 } else {
1066 param_addr = addr;
1067 addr += size;
1069 sse_param_index++;
1071 } else if ((type->t & VT_BTYPE) == VT_STRUCT ||
1072 (type->t & VT_BTYPE) == VT_LDOUBLE) {
1073 param_addr = addr;
1074 addr += size;
1075 } else {
1076 if (reg_param_index < REGN) {
1077 /* save arguments passed by register */
1078 push_arg_reg(reg_param_index);
1079 param_addr = loc;
1080 } else {
1081 param_addr = addr;
1082 addr += 8;
1084 reg_param_index++;
1086 sym_push(sym->v & ~SYM_FIELD, type,
1087 VT_LOCAL | VT_LVAL, param_addr);
1088 param_index++;
1092 /* generate function epilog */
1093 void gfunc_epilog(void)
1095 int v, saved_ind;
1097 o(0xc9); /* leave */
1098 if (func_ret_sub == 0) {
1099 o(0xc3); /* ret */
1100 } else {
1101 o(0xc2); /* ret n */
1102 g(func_ret_sub);
1103 g(func_ret_sub >> 8);
1105 /* align local size to word & save local variables */
1106 v = (-loc + 15) & -16;
1107 saved_ind = ind;
1108 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1109 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1110 o(0xec8148); /* sub rsp, stacksize */
1111 gen_le32(v);
1112 ind = saved_ind;
1115 #endif /* not PE */
1117 /* generate a jump to a label */
1118 int gjmp(int t)
1120 return psym(0xe9, t);
1123 /* generate a jump to a fixed address */
1124 void gjmp_addr(int a)
1126 int r;
1127 r = a - ind - 2;
1128 if (r == (char)r) {
1129 g(0xeb);
1130 g(r);
1131 } else {
1132 oad(0xe9, a - ind - 5);
1136 /* generate a test. set 'inv' to invert test. Stack entry is popped */
1137 int gtst(int inv, int t)
1139 int v, *p;
1141 v = vtop->r & VT_VALMASK;
1142 if (v == VT_CMP) {
1143 /* fast case : can jump directly since flags are set */
1144 g(0x0f);
1145 t = psym((vtop->c.i - 16) ^ inv, t);
1146 } else if (v == VT_JMP || v == VT_JMPI) {
1147 /* && or || optimization */
1148 if ((v & 1) == inv) {
1149 /* insert vtop->c jump list in t */
1150 p = &vtop->c.i;
1151 while (*p != 0)
1152 p = (int *)(cur_text_section->data + *p);
1153 *p = t;
1154 t = vtop->c.i;
1155 } else {
1156 t = gjmp(t);
1157 gsym(vtop->c.i);
1159 } else {
1160 if (is_float(vtop->type.t) ||
1161 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
1162 vpushi(0);
1163 gen_op(TOK_NE);
1165 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1166 /* constant jmp optimization */
1167 if ((vtop->c.i != 0) != inv)
1168 t = gjmp(t);
1169 } else {
1170 v = gv(RC_INT);
1171 o(0x85);
1172 o(0xc0 + v * 9);
1173 g(0x0f);
1174 t = psym(0x85 ^ inv, t);
1177 vtop--;
1178 return t;
1181 /* generate an integer binary operation */
1182 void gen_opi(int op)
1184 int r, fr, opc, c;
1185 int ll, uu, cc;
1187 ll = is64_type(vtop[-1].type.t);
1188 uu = (vtop[-1].type.t & VT_UNSIGNED) != 0;
1189 cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1191 switch(op) {
1192 case '+':
1193 case TOK_ADDC1: /* add with carry generation */
1194 opc = 0;
1195 gen_op8:
1196 if (cc && (!ll || (int)vtop->c.ll == vtop->c.ll)) {
1197 /* constant case */
1198 vswap();
1199 r = gv(RC_INT);
1200 vswap();
1201 c = vtop->c.i;
1202 if (c == (char)c) {
1203 /* XXX: generate inc and dec for smaller code ? */
1204 orex(ll, r, 0, 0x83);
1205 o(0xc0 | (opc << 3) | REG_VALUE(r));
1206 g(c);
1207 } else {
1208 orex(ll, r, 0, 0x81);
1209 oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
1211 } else {
1212 gv2(RC_INT, RC_INT);
1213 r = vtop[-1].r;
1214 fr = vtop[0].r;
1215 orex(ll, r, fr, (opc << 3) | 0x01);
1216 o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
1218 vtop--;
1219 if (op >= TOK_ULT && op <= TOK_GT) {
1220 vtop->r = VT_CMP;
1221 vtop->c.i = op;
1223 break;
1224 case '-':
1225 case TOK_SUBC1: /* sub with carry generation */
1226 opc = 5;
1227 goto gen_op8;
1228 case TOK_ADDC2: /* add with carry use */
1229 opc = 2;
1230 goto gen_op8;
1231 case TOK_SUBC2: /* sub with carry use */
1232 opc = 3;
1233 goto gen_op8;
1234 case '&':
1235 opc = 4;
1236 goto gen_op8;
1237 case '^':
1238 opc = 6;
1239 goto gen_op8;
1240 case '|':
1241 opc = 1;
1242 goto gen_op8;
1243 case '*':
1244 gv2(RC_INT, RC_INT);
1245 r = vtop[-1].r;
1246 fr = vtop[0].r;
1247 orex(ll, fr, r, 0xaf0f); /* imul fr, r */
1248 o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8);
1249 vtop--;
1250 break;
1251 case TOK_SHL:
1252 opc = 4;
1253 goto gen_shift;
1254 case TOK_SHR:
1255 opc = 5;
1256 goto gen_shift;
1257 case TOK_SAR:
1258 opc = 7;
1259 gen_shift:
1260 opc = 0xc0 | (opc << 3);
1261 if (cc) {
1262 /* constant case */
1263 vswap();
1264 r = gv(RC_INT);
1265 vswap();
1266 orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */
1267 o(opc | REG_VALUE(r));
1268 g(vtop->c.i & (ll ? 63 : 31));
1269 } else {
1270 /* we generate the shift in ecx */
1271 gv2(RC_INT, RC_RCX);
1272 r = vtop[-1].r;
1273 orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */
1274 o(opc | REG_VALUE(r));
1276 vtop--;
1277 break;
1278 case TOK_UDIV:
1279 case TOK_UMOD:
1280 uu = 1;
1281 goto divmod;
1282 case '/':
1283 case '%':
1284 case TOK_PDIV:
1285 uu = 0;
1286 divmod:
1287 /* first operand must be in eax */
1288 /* XXX: need better constraint for second operand */
1289 gv2(RC_RAX, RC_RCX);
1290 r = vtop[-1].r;
1291 fr = vtop[0].r;
1292 vtop--;
1293 save_reg(TREG_RDX);
1294 orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */
1295 orex(ll, fr, 0, 0xf7); /* div fr, %eax */
1296 o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr));
1297 if (op == '%' || op == TOK_UMOD)
1298 r = TREG_RDX;
1299 else
1300 r = TREG_RAX;
1301 vtop->r = r;
1302 break;
1303 default:
1304 opc = 7;
1305 goto gen_op8;
1309 void gen_opl(int op)
1311 gen_opi(op);
1314 /* generate a floating point operation 'v = t1 op t2' instruction. The
1315 two operands are guaranted to have the same floating point type */
1316 /* XXX: need to use ST1 too */
1317 void gen_opf(int op)
1319 int a, ft, fc, swapped, r;
1320 int float_type =
1321 (vtop->type.t & VT_BTYPE) == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
1323 /* convert constants to memory references */
1324 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1325 vswap();
1326 gv(float_type);
1327 vswap();
1329 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
1330 gv(float_type);
1332 /* must put at least one value in the floating point register */
1333 if ((vtop[-1].r & VT_LVAL) &&
1334 (vtop[0].r & VT_LVAL)) {
1335 vswap();
1336 gv(float_type);
1337 vswap();
1339 swapped = 0;
1340 /* swap the stack if needed so that t1 is the register and t2 is
1341 the memory reference */
1342 if (vtop[-1].r & VT_LVAL) {
1343 vswap();
1344 swapped = 1;
1346 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1347 if (op >= TOK_ULT && op <= TOK_GT) {
1348 /* load on stack second operand */
1349 load(TREG_ST0, vtop);
1350 save_reg(TREG_RAX); /* eax is used by FP comparison code */
1351 if (op == TOK_GE || op == TOK_GT)
1352 swapped = !swapped;
1353 else if (op == TOK_EQ || op == TOK_NE)
1354 swapped = 0;
1355 if (swapped)
1356 o(0xc9d9); /* fxch %st(1) */
1357 o(0xe9da); /* fucompp */
1358 o(0xe0df); /* fnstsw %ax */
1359 if (op == TOK_EQ) {
1360 o(0x45e480); /* and $0x45, %ah */
1361 o(0x40fC80); /* cmp $0x40, %ah */
1362 } else if (op == TOK_NE) {
1363 o(0x45e480); /* and $0x45, %ah */
1364 o(0x40f480); /* xor $0x40, %ah */
1365 op = TOK_NE;
1366 } else if (op == TOK_GE || op == TOK_LE) {
1367 o(0x05c4f6); /* test $0x05, %ah */
1368 op = TOK_EQ;
1369 } else {
1370 o(0x45c4f6); /* test $0x45, %ah */
1371 op = TOK_EQ;
1373 vtop--;
1374 vtop->r = VT_CMP;
1375 vtop->c.i = op;
1376 } else {
1377 /* no memory reference possible for long double operations */
1378 load(TREG_ST0, vtop);
1379 swapped = !swapped;
1381 switch(op) {
1382 default:
1383 case '+':
1384 a = 0;
1385 break;
1386 case '-':
1387 a = 4;
1388 if (swapped)
1389 a++;
1390 break;
1391 case '*':
1392 a = 1;
1393 break;
1394 case '/':
1395 a = 6;
1396 if (swapped)
1397 a++;
1398 break;
1400 ft = vtop->type.t;
1401 fc = vtop->c.ul;
1402 o(0xde); /* fxxxp %st, %st(1) */
1403 o(0xc1 + (a << 3));
1404 vtop--;
1406 } else {
1407 if (op >= TOK_ULT && op <= TOK_GT) {
1408 /* if saved lvalue, then we must reload it */
1409 r = vtop->r;
1410 fc = vtop->c.ul;
1411 if ((r & VT_VALMASK) == VT_LLOCAL) {
1412 SValue v1;
1413 r = get_reg(RC_INT);
1414 v1.type.t = VT_INT;
1415 v1.r = VT_LOCAL | VT_LVAL;
1416 v1.c.ul = fc;
1417 load(r, &v1);
1418 fc = 0;
1421 if (op == TOK_EQ || op == TOK_NE) {
1422 swapped = 0;
1423 } else {
1424 if (op == TOK_LE || op == TOK_LT)
1425 swapped = !swapped;
1426 if (op == TOK_LE || op == TOK_GE) {
1427 op = 0x93; /* setae */
1428 } else {
1429 op = 0x97; /* seta */
1433 if (swapped) {
1434 o(0x7e0ff3); /* movq */
1435 gen_modrm(1, r, vtop->sym, fc);
1437 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) {
1438 o(0x66);
1440 o(0x2e0f); /* ucomisd %xmm0, %xmm1 */
1441 o(0xc8);
1442 } else {
1443 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE) {
1444 o(0x66);
1446 o(0x2e0f); /* ucomisd */
1447 gen_modrm(0, r, vtop->sym, fc);
1450 vtop--;
1451 vtop->r = VT_CMP;
1452 vtop->c.i = op;
1453 } else {
1454 /* no memory reference possible for long double operations */
1455 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1456 load(TREG_XMM0, vtop);
1457 swapped = !swapped;
1459 switch(op) {
1460 default:
1461 case '+':
1462 a = 0;
1463 break;
1464 case '-':
1465 a = 4;
1466 break;
1467 case '*':
1468 a = 1;
1469 break;
1470 case '/':
1471 a = 6;
1472 break;
1474 ft = vtop->type.t;
1475 fc = vtop->c.ul;
1476 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
1477 o(0xde); /* fxxxp %st, %st(1) */
1478 o(0xc1 + (a << 3));
1479 } else {
1480 /* if saved lvalue, then we must reload it */
1481 r = vtop->r;
1482 if ((r & VT_VALMASK) == VT_LLOCAL) {
1483 SValue v1;
1484 r = get_reg(RC_INT);
1485 v1.type.t = VT_INT;
1486 v1.r = VT_LOCAL | VT_LVAL;
1487 v1.c.ul = fc;
1488 load(r, &v1);
1489 fc = 0;
1491 if (swapped) {
1492 /* movq %xmm0,%xmm1 */
1493 o(0x7e0ff3);
1494 o(0xc8);
1495 load(TREG_XMM0, vtop);
1496 /* subsd %xmm1,%xmm0 (f2 0f 5c c1) */
1497 if ((ft & VT_BTYPE) == VT_DOUBLE) {
1498 o(0xf2);
1499 } else {
1500 o(0xf3);
1502 o(0x0f);
1503 o(0x58 + a);
1504 o(0xc1);
1505 } else {
1506 if ((ft & VT_BTYPE) == VT_DOUBLE) {
1507 o(0xf2);
1508 } else {
1509 o(0xf3);
1511 o(0x0f);
1512 o(0x58 + a);
1513 gen_modrm(0, r, vtop->sym, fc);
1516 vtop--;
1521 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
1522 and 'long long' cases. */
1523 void gen_cvt_itof(int t)
1525 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1526 save_reg(TREG_ST0);
1527 gv(RC_INT);
1528 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1529 /* signed long long to float/double/long double (unsigned case
1530 is handled generically) */
1531 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1532 o(0x242cdf); /* fildll (%rsp) */
1533 o(0x08c48348); /* add $8, %rsp */
1534 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1535 (VT_INT | VT_UNSIGNED)) {
1536 /* unsigned int to float/double/long double */
1537 o(0x6a); /* push $0 */
1538 g(0x00);
1539 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1540 o(0x242cdf); /* fildll (%rsp) */
1541 o(0x10c48348); /* add $16, %rsp */
1542 } else {
1543 /* int to float/double/long double */
1544 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
1545 o(0x2404db); /* fildl (%rsp) */
1546 o(0x08c48348); /* add $8, %rsp */
1548 vtop->r = TREG_ST0;
1549 } else {
1550 save_reg(TREG_XMM0);
1551 gv(RC_INT);
1552 o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT));
1553 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1554 (VT_INT | VT_UNSIGNED) ||
1555 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
1556 o(0x48); /* REX */
1558 o(0x2a0f);
1559 o(0xc0 + (vtop->r & VT_VALMASK)); /* cvtsi2sd */
1560 vtop->r = TREG_XMM0;
1564 /* convert from one floating point type to another */
1565 void gen_cvt_ftof(int t)
1567 int ft, bt, tbt;
1569 ft = vtop->type.t;
1570 bt = ft & VT_BTYPE;
1571 tbt = t & VT_BTYPE;
1573 if (bt == VT_FLOAT) {
1574 gv(RC_FLOAT);
1575 if (tbt == VT_DOUBLE) {
1576 o(0xc0140f); /* unpcklps */
1577 o(0xc05a0f); /* cvtps2pd */
1578 } else if (tbt == VT_LDOUBLE) {
1579 /* movss %xmm0,-0x10(%rsp) */
1580 o(0x44110ff3);
1581 o(0xf024);
1582 o(0xf02444d9); /* flds -0x10(%rsp) */
1583 vtop->r = TREG_ST0;
1585 } else if (bt == VT_DOUBLE) {
1586 gv(RC_FLOAT);
1587 if (tbt == VT_FLOAT) {
1588 o(0xc0140f66); /* unpcklpd */
1589 o(0xc05a0f66); /* cvtpd2ps */
1590 } else if (tbt == VT_LDOUBLE) {
1591 /* movsd %xmm0,-0x10(%rsp) */
1592 o(0x44110ff2);
1593 o(0xf024);
1594 o(0xf02444dd); /* fldl -0x10(%rsp) */
1595 vtop->r = TREG_ST0;
1597 } else {
1598 gv(RC_ST0);
1599 if (tbt == VT_DOUBLE) {
1600 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
1601 /* movsd -0x10(%rsp),%xmm0 */
1602 o(0x44100ff2);
1603 o(0xf024);
1604 vtop->r = TREG_XMM0;
1605 } else if (tbt == VT_FLOAT) {
1606 o(0xf0245cd9); /* fstps -0x10(%rsp) */
1607 /* movss -0x10(%rsp),%xmm0 */
1608 o(0x44100ff3);
1609 o(0xf024);
1610 vtop->r = TREG_XMM0;
1615 /* convert fp to int 't' type */
1616 void gen_cvt_ftoi(int t)
1618 int ft, bt, size, r;
1619 ft = vtop->type.t;
1620 bt = ft & VT_BTYPE;
1621 if (bt == VT_LDOUBLE) {
1622 gen_cvt_ftof(VT_DOUBLE);
1623 bt = VT_DOUBLE;
1626 gv(RC_FLOAT);
1627 if (t != VT_INT)
1628 size = 8;
1629 else
1630 size = 4;
1632 r = get_reg(RC_INT);
1633 if (bt == VT_FLOAT) {
1634 o(0xf3);
1635 } else if (bt == VT_DOUBLE) {
1636 o(0xf2);
1637 } else {
1638 assert(0);
1640 orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */
1641 o(0xc0 + (REG_VALUE(r) << 3));
1642 vtop->r = r;
1645 /* computed goto support */
1646 void ggoto(void)
1648 gcall_or_jmp(1);
1649 vtop--;
1652 /* end of x86-64 code generator */
1653 /*************************************************************/
1654 #endif /* ! TARGET_DEFS_ONLY */
1655 /******************************************************/