tccelf.c: write section headers before sections
[tinycc.git] / x86_64-gen.c
bloba792ba67674b2b8b884cbbb84b40dd79676fe7db
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 25
27 #define NB_ASM_REGS 16
28 #define CONFIG_TCC_ASM
30 /* a register can belong to several classes. The classes must be
31 sorted from more general to more precise (see gv2() code which does
32 assumptions on it). */
33 #define RC_INT 0x0001 /* generic integer register */
34 #define RC_FLOAT 0x0002 /* generic float register */
35 #define RC_RAX 0x0004
36 #define RC_RDX 0x0008
37 #define RC_RCX 0x0010
38 #define RC_RSI 0x0020
39 #define RC_RDI 0x0040
40 #define RC_ST0 0x0080 /* only for long double */
41 #define RC_R8 0x0100
42 #define RC_R9 0x0200
43 #define RC_R10 0x0400
44 #define RC_R11 0x0800
45 #define RC_XMM0 0x1000
46 #define RC_XMM1 0x2000
47 #define RC_XMM2 0x4000
48 #define RC_XMM3 0x8000
49 #define RC_XMM4 0x10000
50 #define RC_XMM5 0x20000
51 #define RC_XMM6 0x40000
52 #define RC_XMM7 0x80000
53 #define RC_IRET RC_RAX /* function return: integer register */
54 #define RC_IRE2 RC_RDX /* function return: second integer register */
55 #define RC_FRET RC_XMM0 /* function return: float register */
56 #define RC_FRE2 RC_XMM1 /* function return: second float register */
58 /* pretty names for the registers */
59 enum {
60 TREG_RAX = 0,
61 TREG_RCX = 1,
62 TREG_RDX = 2,
63 TREG_RSP = 4,
64 TREG_RSI = 6,
65 TREG_RDI = 7,
67 TREG_R8 = 8,
68 TREG_R9 = 9,
69 TREG_R10 = 10,
70 TREG_R11 = 11,
72 TREG_XMM0 = 16,
73 TREG_XMM1 = 17,
74 TREG_XMM2 = 18,
75 TREG_XMM3 = 19,
76 TREG_XMM4 = 20,
77 TREG_XMM5 = 21,
78 TREG_XMM6 = 22,
79 TREG_XMM7 = 23,
81 TREG_ST0 = 24,
83 TREG_MEM = 0x20
86 #define REX_BASE(reg) (((reg) >> 3) & 1)
87 #define REG_VALUE(reg) ((reg) & 7)
89 /* return registers for function */
90 #define REG_IRET TREG_RAX /* single word int return register */
91 #define REG_IRE2 TREG_RDX /* second word return register (for long long) */
92 #define REG_FRET TREG_XMM0 /* float return register */
93 #define REG_FRE2 TREG_XMM1 /* second float return register */
95 /* defined if function parameters must be evaluated in reverse order */
96 #define INVERT_FUNC_PARAMS
98 /* pointer size, in bytes */
99 #define PTR_SIZE 8
101 /* long double size and alignment, in bytes */
102 #define LDOUBLE_SIZE 16
103 #define LDOUBLE_ALIGN 16
104 /* maximum alignment (for aligned attribute support) */
105 #define MAX_ALIGN 16
107 /* define if return values need to be extended explicitely
108 at caller side (for interfacing with non-TCC compilers) */
109 #define PROMOTE_RET
111 #define TCC_TARGET_NATIVE_STRUCT_COPY
112 ST_FUNC void gen_struct_copy(int size);
114 /******************************************************/
115 #else /* ! TARGET_DEFS_ONLY */
116 /******************************************************/
117 #define USING_GLOBALS
118 #include "tcc.h"
119 #include <assert.h>
121 ST_DATA const char * const target_machine_defs =
122 "__x86_64__\0"
123 "__amd64__\0"
126 ST_DATA const int reg_classes[NB_REGS] = {
127 /* eax */ RC_INT | RC_RAX,
128 /* ecx */ RC_INT | RC_RCX,
129 /* edx */ RC_INT | RC_RDX,
133 RC_RSI,
134 RC_RDI,
135 RC_R8,
136 RC_R9,
137 RC_R10,
138 RC_R11,
143 /* xmm0 */ RC_FLOAT | RC_XMM0,
144 /* xmm1 */ RC_FLOAT | RC_XMM1,
145 /* xmm2 */ RC_FLOAT | RC_XMM2,
146 /* xmm3 */ RC_FLOAT | RC_XMM3,
147 /* xmm4 */ RC_FLOAT | RC_XMM4,
148 /* xmm5 */ RC_FLOAT | RC_XMM5,
149 /* xmm6 an xmm7 are included so gv() can be used on them,
150 but they are not tagged with RC_FLOAT because they are
151 callee saved on Windows */
152 RC_XMM6,
153 RC_XMM7,
154 /* st0 */ RC_ST0
157 static unsigned long func_sub_sp_offset;
158 static int func_ret_sub;
160 #if defined(CONFIG_TCC_BCHECK)
161 static addr_t func_bound_offset;
162 static unsigned long func_bound_ind;
163 ST_DATA int func_bound_add_epilog;
164 #endif
166 #ifdef TCC_TARGET_PE
167 static int func_scratch, func_alloca;
168 #endif
170 /* XXX: make it faster ? */
171 ST_FUNC void g(int c)
173 int ind1;
174 if (nocode_wanted)
175 return;
176 ind1 = ind + 1;
177 if (ind1 > cur_text_section->data_allocated)
178 section_realloc(cur_text_section, ind1);
179 cur_text_section->data[ind] = c;
180 ind = ind1;
183 ST_FUNC void o(unsigned int c)
185 while (c) {
186 g(c);
187 c = c >> 8;
191 ST_FUNC void gen_le16(int v)
193 g(v);
194 g(v >> 8);
197 ST_FUNC void gen_le32(int c)
199 g(c);
200 g(c >> 8);
201 g(c >> 16);
202 g(c >> 24);
205 ST_FUNC void gen_le64(int64_t c)
207 g(c);
208 g(c >> 8);
209 g(c >> 16);
210 g(c >> 24);
211 g(c >> 32);
212 g(c >> 40);
213 g(c >> 48);
214 g(c >> 56);
217 static void orex(int ll, int r, int r2, int b)
219 if ((r & VT_VALMASK) >= VT_CONST)
220 r = 0;
221 if ((r2 & VT_VALMASK) >= VT_CONST)
222 r2 = 0;
223 if (ll || REX_BASE(r) || REX_BASE(r2))
224 o(0x40 | REX_BASE(r) | (REX_BASE(r2) << 2) | (ll << 3));
225 o(b);
228 /* output a symbol and patch all calls to it */
229 ST_FUNC void gsym_addr(int t, int a)
231 while (t) {
232 unsigned char *ptr = cur_text_section->data + t;
233 uint32_t n = read32le(ptr); /* next value */
234 write32le(ptr, a < 0 ? -a : a - t - 4);
235 t = n;
239 static int is64_type(int t)
241 return ((t & VT_BTYPE) == VT_PTR ||
242 (t & VT_BTYPE) == VT_FUNC ||
243 (t & VT_BTYPE) == VT_LLONG);
246 /* instruction + 4 bytes data. Return the address of the data */
247 static int oad(int c, int s)
249 int t;
250 if (nocode_wanted)
251 return s;
252 o(c);
253 t = ind;
254 gen_le32(s);
255 return t;
258 /* generate jmp to a label */
259 #define gjmp2(instr,lbl) oad(instr,lbl)
261 ST_FUNC void gen_addr32(int r, Sym *sym, int c)
263 if (r & VT_SYM)
264 greloca(cur_text_section, sym, ind, R_X86_64_32S, c), c=0;
265 gen_le32(c);
268 /* output constant with relocation if 'r & VT_SYM' is true */
269 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c)
271 if (r & VT_SYM)
272 greloca(cur_text_section, sym, ind, R_X86_64_64, c), c=0;
273 gen_le64(c);
276 /* output constant with relocation if 'r & VT_SYM' is true */
277 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
279 if (r & VT_SYM)
280 greloca(cur_text_section, sym, ind, R_X86_64_PC32, c-4), c=4;
281 gen_le32(c-4);
284 /* output got address with relocation */
285 static void gen_gotpcrel(int r, Sym *sym, int c)
287 #ifdef TCC_TARGET_PE
288 tcc_error("internal error: no GOT on PE: %s %x %x | %02x %02x %02x\n",
289 get_tok_str(sym->v, NULL), c, r,
290 cur_text_section->data[ind-3],
291 cur_text_section->data[ind-2],
292 cur_text_section->data[ind-1]
294 #endif
295 greloca(cur_text_section, sym, ind, R_X86_64_GOTPCREL, -4);
296 gen_le32(0);
297 if (c) {
298 /* we use add c, %xxx for displacement */
299 orex(1, r, 0, 0x81);
300 o(0xc0 + REG_VALUE(r));
301 gen_le32(c);
305 static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got)
307 op_reg = REG_VALUE(op_reg) << 3;
308 if ((r & VT_VALMASK) == VT_CONST) {
309 /* constant memory reference */
310 if (!(r & VT_SYM)) {
311 /* Absolute memory reference */
312 o(0x04 | op_reg); /* [sib] | destreg */
313 oad(0x25, c); /* disp32 */
314 } else {
315 o(0x05 | op_reg); /* (%rip)+disp32 | destreg */
316 if (is_got) {
317 gen_gotpcrel(r, sym, c);
318 } else {
319 gen_addrpc32(r, sym, c);
322 } else if ((r & VT_VALMASK) == VT_LOCAL) {
323 /* currently, we use only ebp as base */
324 if (c == (char)c) {
325 /* short reference */
326 o(0x45 | op_reg);
327 g(c);
328 } else {
329 oad(0x85 | op_reg, c);
331 } else if ((r & VT_VALMASK) >= TREG_MEM) {
332 if (c) {
333 g(0x80 | op_reg | REG_VALUE(r));
334 gen_le32(c);
335 } else {
336 g(0x00 | op_reg | REG_VALUE(r));
338 } else {
339 g(0x00 | op_reg | REG_VALUE(r));
343 /* generate a modrm reference. 'op_reg' contains the additional 3
344 opcode bits */
345 static void gen_modrm(int op_reg, int r, Sym *sym, int c)
347 gen_modrm_impl(op_reg, r, sym, c, 0);
350 /* generate a modrm reference. 'op_reg' contains the additional 3
351 opcode bits */
352 static void gen_modrm64(int opcode, int op_reg, int r, Sym *sym, int c)
354 int is_got;
355 is_got = (op_reg & TREG_MEM) && !(sym->type.t & VT_STATIC);
356 orex(1, r, op_reg, opcode);
357 gen_modrm_impl(op_reg, r, sym, c, is_got);
361 /* load 'r' from value 'sv' */
362 void load(int r, SValue *sv)
364 int v, t, ft, fc, fr;
365 SValue v1;
367 #ifdef TCC_TARGET_PE
368 SValue v2;
369 sv = pe_getimport(sv, &v2);
370 #endif
372 fr = sv->r;
373 ft = sv->type.t & ~VT_DEFSIGN;
374 fc = sv->c.i;
375 if (fc != sv->c.i && (fr & VT_SYM))
376 tcc_error("64 bit addend in load");
378 ft &= ~(VT_VOLATILE | VT_CONSTANT);
380 #ifndef TCC_TARGET_PE
381 /* we use indirect access via got */
382 if ((fr & VT_VALMASK) == VT_CONST && (fr & VT_SYM) &&
383 (fr & VT_LVAL) && !(sv->sym->type.t & VT_STATIC)) {
384 /* use the result register as a temporal register */
385 int tr = r | TREG_MEM;
386 if (is_float(ft)) {
387 /* we cannot use float registers as a temporal register */
388 tr = get_reg(RC_INT) | TREG_MEM;
390 gen_modrm64(0x8b, tr, fr, sv->sym, 0);
392 /* load from the temporal register */
393 fr = tr | VT_LVAL;
395 #endif
397 v = fr & VT_VALMASK;
398 if (fr & VT_LVAL) {
399 int b, ll;
400 if (v == VT_LLOCAL) {
401 v1.type.t = VT_PTR;
402 v1.r = VT_LOCAL | VT_LVAL;
403 v1.c.i = fc;
404 fr = r;
405 if (!(reg_classes[fr] & (RC_INT|RC_R11)))
406 fr = get_reg(RC_INT);
407 load(fr, &v1);
409 if (fc != sv->c.i) {
410 /* If the addends doesn't fit into a 32bit signed
411 we must use a 64bit move. We've checked above
412 that this doesn't have a sym associated. */
413 v1.type.t = VT_LLONG;
414 v1.r = VT_CONST;
415 v1.c.i = sv->c.i;
416 fr = r;
417 if (!(reg_classes[fr] & (RC_INT|RC_R11)))
418 fr = get_reg(RC_INT);
419 load(fr, &v1);
420 fc = 0;
422 ll = 0;
423 /* Like GCC we can load from small enough properly sized
424 structs and unions as well.
425 XXX maybe move to generic operand handling, but should
426 occur only with asm, so tccasm.c might also be a better place */
427 if ((ft & VT_BTYPE) == VT_STRUCT) {
428 int align;
429 switch (type_size(&sv->type, &align)) {
430 case 1: ft = VT_BYTE; break;
431 case 2: ft = VT_SHORT; break;
432 case 4: ft = VT_INT; break;
433 case 8: ft = VT_LLONG; break;
434 default:
435 tcc_error("invalid aggregate type for register load");
436 break;
439 if ((ft & VT_BTYPE) == VT_FLOAT) {
440 b = 0x6e0f66;
441 r = REG_VALUE(r); /* movd */
442 } else if ((ft & VT_BTYPE) == VT_DOUBLE) {
443 b = 0x7e0ff3; /* movq */
444 r = REG_VALUE(r);
445 } else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
446 b = 0xdb, r = 5; /* fldt */
447 } else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
448 b = 0xbe0f; /* movsbl */
449 } else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
450 b = 0xb60f; /* movzbl */
451 } else if ((ft & VT_TYPE) == VT_SHORT) {
452 b = 0xbf0f; /* movswl */
453 } else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
454 b = 0xb70f; /* movzwl */
455 } else if ((ft & VT_TYPE) == (VT_VOID)) {
456 /* Can happen with zero size structs */
457 return;
458 } else {
459 assert(((ft & VT_BTYPE) == VT_INT)
460 || ((ft & VT_BTYPE) == VT_LLONG)
461 || ((ft & VT_BTYPE) == VT_PTR)
462 || ((ft & VT_BTYPE) == VT_FUNC)
464 ll = is64_type(ft);
465 b = 0x8b;
467 if (ll) {
468 gen_modrm64(b, r, fr, sv->sym, fc);
469 } else {
470 orex(ll, fr, r, b);
471 gen_modrm(r, fr, sv->sym, fc);
473 } else {
474 if (v == VT_CONST) {
475 if (fr & VT_SYM) {
476 #ifdef TCC_TARGET_PE
477 orex(1,0,r,0x8d);
478 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
479 gen_addrpc32(fr, sv->sym, fc);
480 #else
481 if (sv->sym->type.t & VT_STATIC) {
482 orex(1,0,r,0x8d);
483 o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
484 gen_addrpc32(fr, sv->sym, fc);
485 } else {
486 orex(1,0,r,0x8b);
487 o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */
488 gen_gotpcrel(r, sv->sym, fc);
490 #endif
491 } else if (is64_type(ft)) {
492 if (sv->c.i >> 32) {
493 orex(1,r,0, 0xb8 + REG_VALUE(r)); /* movabs $xx, r */
494 gen_le64(sv->c.i);
495 } else if (sv->c.i > 0) {
496 orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
497 gen_le32(sv->c.i);
498 } else {
499 o(0xc031 + REG_VALUE(r) * 0x900); /* xor r, r */
501 } else {
502 orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
503 gen_le32(fc);
505 } else if (v == VT_LOCAL) {
506 orex(1,0,r,0x8d); /* lea xxx(%ebp), r */
507 gen_modrm(r, VT_LOCAL, sv->sym, fc);
508 } else if (v == VT_CMP) {
509 if (fc & 0x100)
511 v = vtop->cmp_r;
512 fc &= ~0x100;
513 /* This was a float compare. If the parity bit is
514 set the result was unordered, meaning false for everything
515 except TOK_NE, and true for TOK_NE. */
516 orex(0, r, 0, 0xb0 + REG_VALUE(r)); /* mov $0/1,%al */
517 g(v ^ fc ^ (v == TOK_NE));
518 o(0x037a + (REX_BASE(r) << 8));
520 orex(0,r,0, 0x0f); /* setxx %br */
521 o(fc);
522 o(0xc0 + REG_VALUE(r));
523 orex(0,r,0, 0x0f);
524 o(0xc0b6 + REG_VALUE(r) * 0x900); /* movzbl %al, %eax */
525 } else if (v == VT_JMP || v == VT_JMPI) {
526 t = v & 1;
527 orex(0,r,0,0);
528 oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */
529 o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */
530 gsym(fc);
531 orex(0,r,0,0);
532 oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */
533 } else if (v != r) {
534 if ((r >= TREG_XMM0) && (r <= TREG_XMM7)) {
535 if (v == TREG_ST0) {
536 /* gen_cvt_ftof(VT_DOUBLE); */
537 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
538 /* movsd -0x10(%rsp),%xmmN */
539 o(0x100ff2);
540 o(0x44 + REG_VALUE(r)*8); /* %xmmN */
541 o(0xf024);
542 } else {
543 assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
544 if ((ft & VT_BTYPE) == VT_FLOAT) {
545 o(0x100ff3);
546 } else {
547 assert((ft & VT_BTYPE) == VT_DOUBLE);
548 o(0x100ff2);
550 o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8);
552 } else if (r == TREG_ST0) {
553 assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
554 /* gen_cvt_ftof(VT_LDOUBLE); */
555 /* movsd %xmmN,-0x10(%rsp) */
556 o(0x110ff2);
557 o(0x44 + REG_VALUE(r)*8); /* %xmmN */
558 o(0xf024);
559 o(0xf02444dd); /* fldl -0x10(%rsp) */
560 } else {
561 orex(is64_type(ft), r, v, 0x89);
562 o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */
568 /* store register 'r' in lvalue 'v' */
569 void store(int r, SValue *v)
571 int fr, bt, ft, fc;
572 int op64 = 0;
573 /* store the REX prefix in this variable when PIC is enabled */
574 int pic = 0;
576 #ifdef TCC_TARGET_PE
577 SValue v2;
578 v = pe_getimport(v, &v2);
579 #endif
581 fr = v->r & VT_VALMASK;
582 ft = v->type.t;
583 fc = v->c.i;
584 if (fc != v->c.i && (fr & VT_SYM))
585 tcc_error("64 bit addend in store");
586 ft &= ~(VT_VOLATILE | VT_CONSTANT);
587 bt = ft & VT_BTYPE;
589 #ifndef TCC_TARGET_PE
590 /* we need to access the variable via got */
591 if (fr == VT_CONST
592 && (v->r & VT_SYM)
593 && !(v->sym->type.t & VT_STATIC)) {
594 /* mov xx(%rip), %r11 */
595 o(0x1d8b4c);
596 gen_gotpcrel(TREG_R11, v->sym, v->c.i);
597 pic = is64_type(bt) ? 0x49 : 0x41;
599 #endif
601 /* XXX: incorrect if float reg to reg */
602 if (bt == VT_FLOAT) {
603 o(0x66);
604 o(pic);
605 o(0x7e0f); /* movd */
606 r = REG_VALUE(r);
607 } else if (bt == VT_DOUBLE) {
608 o(0x66);
609 o(pic);
610 o(0xd60f); /* movq */
611 r = REG_VALUE(r);
612 } else if (bt == VT_LDOUBLE) {
613 o(0xc0d9); /* fld %st(0) */
614 o(pic);
615 o(0xdb); /* fstpt */
616 r = 7;
617 } else {
618 if (bt == VT_SHORT)
619 o(0x66);
620 o(pic);
621 if (bt == VT_BYTE || bt == VT_BOOL)
622 orex(0, 0, r, 0x88);
623 else if (is64_type(bt))
624 op64 = 0x89;
625 else
626 orex(0, 0, r, 0x89);
628 if (pic) {
629 /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
630 if (op64)
631 o(op64);
632 o(3 + (r << 3));
633 } else if (op64) {
634 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
635 gen_modrm64(op64, r, v->r, v->sym, fc);
636 } else if (fr != r) {
637 orex(1, fr, r, op64);
638 o(0xc0 + fr + r * 8); /* mov r, fr */
640 } else {
641 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
642 gen_modrm(r, v->r, v->sym, fc);
643 } else if (fr != r) {
644 o(0xc0 + fr + r * 8); /* mov r, fr */
649 /* 'is_jmp' is '1' if it is a jump */
650 static void gcall_or_jmp(int is_jmp)
652 int r;
653 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
654 ((vtop->r & VT_SYM) && (vtop->c.i-4) == (int)(vtop->c.i-4))) {
655 /* constant symbolic case -> simple relocation */
656 greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));
657 oad(0xe8 + is_jmp, 0); /* call/jmp im */
658 } else {
659 /* otherwise, indirect call */
660 r = TREG_R11;
661 load(r, vtop);
662 o(0x41); /* REX */
663 o(0xff); /* call/jmp *r */
664 o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
668 #if defined(CONFIG_TCC_BCHECK)
670 static void gen_bounds_call(int v)
672 Sym *sym = external_helper_sym(v);
673 oad(0xe8, 0);
674 greloca(cur_text_section, sym, ind-4, R_X86_64_PLT32, -4);
677 #ifdef TCC_TARGET_PE
678 # define TREG_FASTCALL_1 TREG_RCX
679 #else
680 # define TREG_FASTCALL_1 TREG_RDI
681 #endif
683 static void gen_bounds_prolog(void)
685 /* leave some room for bound checking code */
686 func_bound_offset = lbounds_section->data_offset;
687 func_bound_ind = ind;
688 func_bound_add_epilog = 0;
689 o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /*lbound section pointer */
690 gen_le32 (0);
691 oad(0xb8, 0); /* call to function */
694 static void gen_bounds_epilog(void)
696 addr_t saved_ind;
697 addr_t *bounds_ptr;
698 Sym *sym_data;
699 int offset_modified = func_bound_offset != lbounds_section->data_offset;
701 if (!offset_modified && !func_bound_add_epilog)
702 return;
704 /* add end of table info */
705 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
706 *bounds_ptr = 0;
708 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
709 func_bound_offset, PTR_SIZE);
711 /* generate bound local allocation */
712 if (offset_modified) {
713 saved_ind = ind;
714 ind = func_bound_ind;
715 greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
716 ind = ind + 7;
717 gen_bounds_call(TOK___bound_local_new);
718 ind = saved_ind;
721 /* generate bound check local freeing */
722 o(0x5250); /* save returned value, if any */
723 o(0x20ec8348); /* sub $32,%rsp */
724 o(0x290f); /* movaps %xmm0,0x10(%rsp) */
725 o(0x102444);
726 o(0x240c290f); /* movaps %xmm1,(%rsp) */
727 greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
728 o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /* lea xxx(%rip), %rcx/rdi */
729 gen_le32 (0);
730 gen_bounds_call(TOK___bound_local_delete);
731 o(0x280f); /* movaps 0x10(%rsp),%xmm0 */
732 o(0x102444);
733 o(0x240c280f); /* movaps (%rsp),%xmm1 */
734 o(0x20c48348); /* add $32,%rsp */
735 o(0x585a); /* restore returned value, if any */
737 #endif
739 #ifdef TCC_TARGET_PE
741 #define REGN 4
742 static const uint8_t arg_regs[REGN] = {
743 TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
746 /* Prepare arguments in R10 and R11 rather than RCX and RDX
747 because gv() will not ever use these */
748 static int arg_prepare_reg(int idx) {
749 if (idx == 0 || idx == 1)
750 /* idx=0: r10, idx=1: r11 */
751 return idx + 10;
752 else
753 return idx >= 0 && idx < REGN ? arg_regs[idx] : 0;
756 /* Generate function call. The function address is pushed first, then
757 all the parameters in call order. This functions pops all the
758 parameters and the function address. */
760 static void gen_offs_sp(int b, int r, int d)
762 orex(1,0,r & 0x100 ? 0 : r, b);
763 if (d == (char)d) {
764 o(0x2444 | (REG_VALUE(r) << 3));
765 g(d);
766 } else {
767 o(0x2484 | (REG_VALUE(r) << 3));
768 gen_le32(d);
772 static int using_regs(int size)
774 return !(size > 8 || (size & (size - 1)));
777 /* Return the number of registers needed to return the struct, or 0 if
778 returning via struct pointer. */
779 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
781 int size, align;
782 *ret_align = 1; // Never have to re-align return values for x86-64
783 *regsize = 8;
784 size = type_size(vt, &align);
785 if (!using_regs(size))
786 return 0;
787 if (size == 8)
788 ret->t = VT_LLONG;
789 else if (size == 4)
790 ret->t = VT_INT;
791 else if (size == 2)
792 ret->t = VT_SHORT;
793 else
794 ret->t = VT_BYTE;
795 ret->ref = NULL;
796 return 1;
799 static int is_sse_float(int t) {
800 int bt;
801 bt = t & VT_BTYPE;
802 return bt == VT_DOUBLE || bt == VT_FLOAT;
805 static int gfunc_arg_size(CType *type) {
806 int align;
807 if (type->t & (VT_ARRAY|VT_BITFIELD))
808 return 8;
809 return type_size(type, &align);
812 void gfunc_call(int nb_args)
814 int size, r, args_size, i, d, bt, struct_size;
815 int arg;
817 #ifdef CONFIG_TCC_BCHECK
818 if (tcc_state->do_bounds_check)
819 gbound_args(nb_args);
820 #endif
822 args_size = (nb_args < REGN ? REGN : nb_args) * PTR_SIZE;
823 arg = nb_args;
825 /* for struct arguments, we need to call memcpy and the function
826 call breaks register passing arguments we are preparing.
827 So, we process arguments which will be passed by stack first. */
828 struct_size = args_size;
829 for(i = 0; i < nb_args; i++) {
830 SValue *sv;
832 --arg;
833 sv = &vtop[-i];
834 bt = (sv->type.t & VT_BTYPE);
835 size = gfunc_arg_size(&sv->type);
837 if (using_regs(size))
838 continue; /* arguments smaller than 8 bytes passed in registers or on stack */
840 if (bt == VT_STRUCT) {
841 /* align to stack align size */
842 size = (size + 15) & ~15;
843 /* generate structure store */
844 r = get_reg(RC_INT);
845 gen_offs_sp(0x8d, r, struct_size);
846 struct_size += size;
848 /* generate memcpy call */
849 vset(&sv->type, r | VT_LVAL, 0);
850 vpushv(sv);
851 vstore();
852 --vtop;
853 } else if (bt == VT_LDOUBLE) {
854 gv(RC_ST0);
855 gen_offs_sp(0xdb, 0x107, struct_size);
856 struct_size += 16;
860 if (func_scratch < struct_size)
861 func_scratch = struct_size;
863 arg = nb_args;
864 struct_size = args_size;
866 for(i = 0; i < nb_args; i++) {
867 --arg;
868 bt = (vtop->type.t & VT_BTYPE);
870 size = gfunc_arg_size(&vtop->type);
871 if (!using_regs(size)) {
872 /* align to stack align size */
873 size = (size + 15) & ~15;
874 if (arg >= REGN) {
875 d = get_reg(RC_INT);
876 gen_offs_sp(0x8d, d, struct_size);
877 gen_offs_sp(0x89, d, arg*8);
878 } else {
879 d = arg_prepare_reg(arg);
880 gen_offs_sp(0x8d, d, struct_size);
882 struct_size += size;
883 } else {
884 if (is_sse_float(vtop->type.t)) {
885 if (tcc_state->nosse)
886 tcc_error("SSE disabled");
887 if (arg >= REGN) {
888 gv(RC_XMM0);
889 /* movq %xmm0, j*8(%rsp) */
890 gen_offs_sp(0xd60f66, 0x100, arg*8);
891 } else {
892 /* Load directly to xmmN register */
893 gv(RC_XMM0 << arg);
894 d = arg_prepare_reg(arg);
895 /* mov %xmmN, %rxx */
896 o(0x66);
897 orex(1,d,0, 0x7e0f);
898 o(0xc0 + arg*8 + REG_VALUE(d));
900 } else {
901 if (bt == VT_STRUCT) {
902 vtop->type.ref = NULL;
903 vtop->type.t = size > 4 ? VT_LLONG : size > 2 ? VT_INT
904 : size > 1 ? VT_SHORT : VT_BYTE;
907 r = gv(RC_INT);
908 if (arg >= REGN) {
909 gen_offs_sp(0x89, r, arg*8);
910 } else {
911 d = arg_prepare_reg(arg);
912 orex(1,d,r,0x89); /* mov */
913 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
917 vtop--;
919 save_regs(0);
920 /* Copy R10 and R11 into RCX and RDX, respectively */
921 if (nb_args > 0) {
922 o(0xd1894c); /* mov %r10, %rcx */
923 if (nb_args > 1) {
924 o(0xda894c); /* mov %r11, %rdx */
928 gcall_or_jmp(0);
930 if ((vtop->r & VT_SYM) && vtop->sym->v == TOK_alloca) {
931 /* need to add the "func_scratch" area after alloca */
932 o(0x48); func_alloca = oad(0x05, func_alloca); /* add $NN, %rax */
933 #ifdef CONFIG_TCC_BCHECK
934 if (tcc_state->do_bounds_check)
935 gen_bounds_call(TOK___bound_alloca_nr); /* new region */
936 #endif
938 vtop--;
942 #define FUNC_PROLOG_SIZE 11
944 /* generate function prolog of type 't' */
945 void gfunc_prolog(Sym *func_sym)
947 CType *func_type = &func_sym->type;
948 int addr, reg_param_index, bt, size;
949 Sym *sym;
950 CType *type;
952 func_ret_sub = 0;
953 func_scratch = 32;
954 func_alloca = 0;
955 loc = 0;
957 addr = PTR_SIZE * 2;
958 ind += FUNC_PROLOG_SIZE;
959 func_sub_sp_offset = ind;
960 reg_param_index = 0;
962 sym = func_type->ref;
964 /* if the function returns a structure, then add an
965 implicit pointer parameter */
966 size = gfunc_arg_size(&func_vt);
967 if (!using_regs(size)) {
968 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
969 func_vc = addr;
970 reg_param_index++;
971 addr += 8;
974 /* define parameters */
975 while ((sym = sym->next) != NULL) {
976 type = &sym->type;
977 bt = type->t & VT_BTYPE;
978 size = gfunc_arg_size(type);
979 if (!using_regs(size)) {
980 if (reg_param_index < REGN) {
981 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
983 sym_push(sym->v & ~SYM_FIELD, type,
984 VT_LLOCAL | VT_LVAL, addr);
985 } else {
986 if (reg_param_index < REGN) {
987 /* save arguments passed by register */
988 if ((bt == VT_FLOAT) || (bt == VT_DOUBLE)) {
989 if (tcc_state->nosse)
990 tcc_error("SSE disabled");
991 o(0xd60f66); /* movq */
992 gen_modrm(reg_param_index, VT_LOCAL, NULL, addr);
993 } else {
994 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
997 sym_push(sym->v & ~SYM_FIELD, type,
998 VT_LOCAL | VT_LVAL, addr);
1000 addr += 8;
1001 reg_param_index++;
1004 while (reg_param_index < REGN) {
1005 if (func_var) {
1006 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
1007 addr += 8;
1009 reg_param_index++;
1011 #ifdef CONFIG_TCC_BCHECK
1012 if (tcc_state->do_bounds_check)
1013 gen_bounds_prolog();
1014 #endif
1017 /* generate function epilog */
1018 void gfunc_epilog(void)
1020 int v, start;
1022 /* align local size to word & save local variables */
1023 func_scratch = (func_scratch + 15) & -16;
1024 loc = (loc & -16) - func_scratch;
1026 #ifdef CONFIG_TCC_BCHECK
1027 if (tcc_state->do_bounds_check)
1028 gen_bounds_epilog();
1029 #endif
1031 o(0xc9); /* leave */
1032 if (func_ret_sub == 0) {
1033 o(0xc3); /* ret */
1034 } else {
1035 o(0xc2); /* ret n */
1036 g(func_ret_sub);
1037 g(func_ret_sub >> 8);
1040 v = -loc;
1041 start = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1042 cur_text_section->data_offset = ind;
1043 pe_add_unwind_data(start, ind, v);
1045 ind = start;
1046 if (v >= 4096) {
1047 Sym *sym = external_helper_sym(TOK___chkstk);
1048 oad(0xb8, v); /* mov stacksize, %eax */
1049 oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */
1050 greloca(cur_text_section, sym, ind-4, R_X86_64_PLT32, -4);
1051 o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
1052 } else {
1053 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1054 o(0xec8148); /* sub rsp, stacksize */
1055 gen_le32(v);
1057 ind = cur_text_section->data_offset;
1059 /* add the "func_scratch" area after each alloca seen */
1060 gsym_addr(func_alloca, -func_scratch);
1063 #else
1065 static void gadd_sp(int val)
1067 if (val == (char)val) {
1068 o(0xc48348);
1069 g(val);
1070 } else {
1071 oad(0xc48148, val); /* add $xxx, %rsp */
1075 typedef enum X86_64_Mode {
1076 x86_64_mode_none,
1077 x86_64_mode_memory,
1078 x86_64_mode_integer,
1079 x86_64_mode_sse,
1080 x86_64_mode_x87
1081 } X86_64_Mode;
1083 static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b)
1085 if (a == b)
1086 return a;
1087 else if (a == x86_64_mode_none)
1088 return b;
1089 else if (b == x86_64_mode_none)
1090 return a;
1091 else if ((a == x86_64_mode_memory) || (b == x86_64_mode_memory))
1092 return x86_64_mode_memory;
1093 else if ((a == x86_64_mode_integer) || (b == x86_64_mode_integer))
1094 return x86_64_mode_integer;
1095 else if ((a == x86_64_mode_x87) || (b == x86_64_mode_x87))
1096 return x86_64_mode_memory;
1097 else
1098 return x86_64_mode_sse;
1101 static X86_64_Mode classify_x86_64_inner(CType *ty)
1103 X86_64_Mode mode;
1104 Sym *f;
1106 switch (ty->t & VT_BTYPE) {
1107 case VT_VOID: return x86_64_mode_none;
1109 case VT_INT:
1110 case VT_BYTE:
1111 case VT_SHORT:
1112 case VT_LLONG:
1113 case VT_BOOL:
1114 case VT_PTR:
1115 case VT_FUNC:
1116 return x86_64_mode_integer;
1118 case VT_FLOAT:
1119 case VT_DOUBLE: return x86_64_mode_sse;
1121 case VT_LDOUBLE: return x86_64_mode_x87;
1123 case VT_STRUCT:
1124 f = ty->ref;
1126 mode = x86_64_mode_none;
1127 for (f = f->next; f; f = f->next)
1128 mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type));
1130 return mode;
1132 assert(0);
1133 return 0;
1136 static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count)
1138 X86_64_Mode mode;
1139 int size, align, ret_t = 0;
1141 if (ty->t & (VT_BITFIELD|VT_ARRAY)) {
1142 *psize = 8;
1143 *palign = 8;
1144 *reg_count = 1;
1145 ret_t = ty->t;
1146 mode = x86_64_mode_integer;
1147 } else {
1148 size = type_size(ty, &align);
1149 *psize = (size + 7) & ~7;
1150 *palign = (align + 7) & ~7;
1151 *reg_count = 0; /* avoid compiler warning */
1153 if (size > 16) {
1154 mode = x86_64_mode_memory;
1155 } else {
1156 mode = classify_x86_64_inner(ty);
1157 switch (mode) {
1158 case x86_64_mode_integer:
1159 if (size > 8) {
1160 *reg_count = 2;
1161 ret_t = VT_QLONG;
1162 } else {
1163 *reg_count = 1;
1164 if (size > 4)
1165 ret_t = VT_LLONG;
1166 else if (size > 2)
1167 ret_t = VT_INT;
1168 else if (size > 1)
1169 ret_t = VT_SHORT;
1170 else
1171 ret_t = VT_BYTE;
1172 if ((ty->t & VT_BTYPE) == VT_STRUCT || (ty->t & VT_UNSIGNED))
1173 ret_t |= VT_UNSIGNED;
1175 break;
1177 case x86_64_mode_x87:
1178 *reg_count = 1;
1179 ret_t = VT_LDOUBLE;
1180 break;
1182 case x86_64_mode_sse:
1183 if (size > 8) {
1184 *reg_count = 2;
1185 ret_t = VT_QFLOAT;
1186 } else {
1187 *reg_count = 1;
1188 ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT;
1190 break;
1191 default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/
1196 if (ret) {
1197 ret->ref = NULL;
1198 ret->t = ret_t;
1201 return mode;
1204 ST_FUNC int classify_x86_64_va_arg(CType *ty)
1206 /* This definition must be synced with stdarg.h */
1207 enum __va_arg_type {
1208 __va_gen_reg, __va_float_reg, __va_stack
1210 int size, align, reg_count;
1211 X86_64_Mode mode = classify_x86_64_arg(ty, NULL, &size, &align, &reg_count);
1212 switch (mode) {
1213 default: return __va_stack;
1214 case x86_64_mode_integer: return __va_gen_reg;
1215 case x86_64_mode_sse: return __va_float_reg;
1219 /* Return the number of registers needed to return the struct, or 0 if
1220 returning via struct pointer. */
1221 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
1223 int size, align, reg_count;
1224 if (classify_x86_64_arg(vt, ret, &size, &align, &reg_count) == x86_64_mode_memory)
1225 return 0;
1226 *ret_align = 1; // Never have to re-align return values for x86-64
1227 *regsize = 8 * reg_count; /* the (virtual) regsize is 16 for VT_QLONG/QFLOAT */
1228 return 1;
1231 #define REGN 6
1232 static const uint8_t arg_regs[REGN] = {
1233 TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
1236 static int arg_prepare_reg(int idx) {
1237 if (idx == 2 || idx == 3)
1238 /* idx=2: r10, idx=3: r11 */
1239 return idx + 8;
1240 else
1241 return idx >= 0 && idx < REGN ? arg_regs[idx] : 0;
1244 /* Generate function call. The function address is pushed first, then
1245 all the parameters in call order. This functions pops all the
1246 parameters and the function address. */
1247 void gfunc_call(int nb_args)
1249 X86_64_Mode mode;
1250 CType type;
1251 int size, align, r, args_size, stack_adjust, i, reg_count, k;
1252 int nb_reg_args = 0;
1253 int nb_sse_args = 0;
1254 int sse_reg, gen_reg;
1255 char *onstack = tcc_malloc((nb_args + 1) * sizeof (char));
1257 #ifdef CONFIG_TCC_BCHECK
1258 if (tcc_state->do_bounds_check)
1259 gbound_args(nb_args);
1260 #endif
1262 /* calculate the number of integer/float register arguments, remember
1263 arguments to be passed via stack (in onstack[]), and also remember
1264 if we have to align the stack pointer to 16 (onstack[i] == 2). Needs
1265 to be done in a left-to-right pass over arguments. */
1266 stack_adjust = 0;
1267 for(i = nb_args - 1; i >= 0; i--) {
1268 mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
1269 if (size == 0) continue;
1270 if (mode == x86_64_mode_sse && nb_sse_args + reg_count <= 8) {
1271 nb_sse_args += reg_count;
1272 onstack[i] = 0;
1273 } else if (mode == x86_64_mode_integer && nb_reg_args + reg_count <= REGN) {
1274 nb_reg_args += reg_count;
1275 onstack[i] = 0;
1276 } else if (mode == x86_64_mode_none) {
1277 onstack[i] = 0;
1278 } else {
1279 if (align == 16 && (stack_adjust &= 15)) {
1280 onstack[i] = 2;
1281 stack_adjust = 0;
1282 } else
1283 onstack[i] = 1;
1284 stack_adjust += size;
1288 if (nb_sse_args && tcc_state->nosse)
1289 tcc_error("SSE disabled but floating point arguments passed");
1291 /* fetch cpu flag before generating any code */
1292 if ((vtop->r & VT_VALMASK) == VT_CMP)
1293 gv(RC_INT);
1295 /* for struct arguments, we need to call memcpy and the function
1296 call breaks register passing arguments we are preparing.
1297 So, we process arguments which will be passed by stack first. */
1298 gen_reg = nb_reg_args;
1299 sse_reg = nb_sse_args;
1300 args_size = 0;
1301 stack_adjust &= 15;
1302 for (i = k = 0; i < nb_args;) {
1303 mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
1304 if (size) {
1305 if (!onstack[i + k]) {
1306 ++i;
1307 continue;
1309 /* Possibly adjust stack to align SSE boundary. We're processing
1310 args from right to left while allocating happens left to right
1311 (stack grows down), so the adjustment needs to happen _after_
1312 an argument that requires it. */
1313 if (stack_adjust) {
1314 o(0x50); /* push %rax; aka sub $8,%rsp */
1315 args_size += 8;
1316 stack_adjust = 0;
1318 if (onstack[i + k] == 2)
1319 stack_adjust = 1;
1322 vrotb(i+1);
1324 switch (vtop->type.t & VT_BTYPE) {
1325 case VT_STRUCT:
1326 /* allocate the necessary size on stack */
1327 o(0x48);
1328 oad(0xec81, size); /* sub $xxx, %rsp */
1329 /* generate structure store */
1330 r = get_reg(RC_INT);
1331 orex(1, r, 0, 0x89); /* mov %rsp, r */
1332 o(0xe0 + REG_VALUE(r));
1333 vset(&vtop->type, r | VT_LVAL, 0);
1334 vswap();
1335 /* keep stack aligned for (__bound_)memmove call */
1336 o(0x10ec8348); /* sub $16,%rsp */
1337 o(0xf0e48348); /* and $-16,%rsp */
1338 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r (last %rsp) */
1339 o(0x08ec8348); /* sub $8,%rsp */
1340 vstore();
1341 o(0x08c48348); /* add $8,%rsp */
1342 o(0x5c); /* pop %rsp */
1343 break;
1345 case VT_LDOUBLE:
1346 gv(RC_ST0);
1347 oad(0xec8148, size); /* sub $xxx, %rsp */
1348 o(0x7cdb); /* fstpt 0(%rsp) */
1349 g(0x24);
1350 g(0x00);
1351 break;
1353 case VT_FLOAT:
1354 case VT_DOUBLE:
1355 assert(mode == x86_64_mode_sse);
1356 r = gv(RC_FLOAT);
1357 o(0x50); /* push $rax */
1358 /* movq %xmmN, (%rsp) */
1359 o(0xd60f66);
1360 o(0x04 + REG_VALUE(r)*8);
1361 o(0x24);
1362 break;
1364 default:
1365 assert(mode == x86_64_mode_integer);
1366 /* simple type */
1367 /* XXX: implicit cast ? */
1368 r = gv(RC_INT);
1369 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */
1370 break;
1372 args_size += size;
1374 vpop();
1375 --nb_args;
1376 k++;
1379 tcc_free(onstack);
1381 /* XXX This should be superfluous. */
1382 save_regs(0); /* save used temporary registers */
1384 /* then, we prepare register passing arguments.
1385 Note that we cannot set RDX and RCX in this loop because gv()
1386 may break these temporary registers. Let's use R10 and R11
1387 instead of them */
1388 assert(gen_reg <= REGN);
1389 assert(sse_reg <= 8);
1390 for(i = 0; i < nb_args; i++) {
1391 mode = classify_x86_64_arg(&vtop->type, &type, &size, &align, &reg_count);
1392 if (size == 0) continue;
1393 /* Alter stack entry type so that gv() knows how to treat it */
1394 vtop->type = type;
1395 if (mode == x86_64_mode_sse) {
1396 if (reg_count == 2) {
1397 sse_reg -= 2;
1398 gv(RC_FRET); /* Use pair load into xmm0 & xmm1 */
1399 if (sse_reg) { /* avoid redundant movaps %xmm0, %xmm0 */
1400 /* movaps %xmm1, %xmmN */
1401 o(0x280f);
1402 o(0xc1 + ((sse_reg+1) << 3));
1403 /* movaps %xmm0, %xmmN */
1404 o(0x280f);
1405 o(0xc0 + (sse_reg << 3));
1407 } else {
1408 assert(reg_count == 1);
1409 --sse_reg;
1410 /* Load directly to register */
1411 gv(RC_XMM0 << sse_reg);
1413 } else if (mode == x86_64_mode_integer) {
1414 /* simple type */
1415 /* XXX: implicit cast ? */
1416 int d;
1417 gen_reg -= reg_count;
1418 r = gv(RC_INT);
1419 d = arg_prepare_reg(gen_reg);
1420 orex(1,d,r,0x89); /* mov */
1421 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
1422 if (reg_count == 2) {
1423 d = arg_prepare_reg(gen_reg+1);
1424 orex(1,d,vtop->r2,0x89); /* mov */
1425 o(0xc0 + REG_VALUE(vtop->r2) * 8 + REG_VALUE(d));
1428 vtop--;
1430 assert(gen_reg == 0);
1431 assert(sse_reg == 0);
1433 /* We shouldn't have many operands on the stack anymore, but the
1434 call address itself is still there, and it might be in %eax
1435 (or edx/ecx) currently, which the below writes would clobber.
1436 So evict all remaining operands here. */
1437 save_regs(0);
1439 /* Copy R10 and R11 into RDX and RCX, respectively */
1440 if (nb_reg_args > 2) {
1441 o(0xd2894c); /* mov %r10, %rdx */
1442 if (nb_reg_args > 3) {
1443 o(0xd9894c); /* mov %r11, %rcx */
1447 if (vtop->type.ref->f.func_type != FUNC_NEW) /* implies FUNC_OLD or FUNC_ELLIPSIS */
1448 oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
1449 gcall_or_jmp(0);
1450 if (args_size)
1451 gadd_sp(args_size);
1452 vtop--;
1455 #define FUNC_PROLOG_SIZE 11
1457 static void push_arg_reg(int i) {
1458 loc -= 8;
1459 gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
1462 /* generate function prolog of type 't' */
1463 void gfunc_prolog(Sym *func_sym)
1465 CType *func_type = &func_sym->type;
1466 X86_64_Mode mode, ret_mode;
1467 int i, addr, align, size, reg_count;
1468 int param_addr = 0, reg_param_index, sse_param_index;
1469 Sym *sym;
1470 CType *type;
1472 sym = func_type->ref;
1473 addr = PTR_SIZE * 2;
1474 loc = 0;
1475 ind += FUNC_PROLOG_SIZE;
1476 func_sub_sp_offset = ind;
1477 func_ret_sub = 0;
1478 ret_mode = classify_x86_64_arg(&func_vt, NULL, &size, &align, &reg_count);
1480 if (func_var) {
1481 int seen_reg_num, seen_sse_num, seen_stack_size;
1482 seen_reg_num = ret_mode == x86_64_mode_memory;
1483 seen_sse_num = 0;
1484 /* frame pointer and return address */
1485 seen_stack_size = PTR_SIZE * 2;
1486 /* count the number of seen parameters */
1487 sym = func_type->ref;
1488 while ((sym = sym->next) != NULL) {
1489 type = &sym->type;
1490 mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
1491 switch (mode) {
1492 default:
1493 stack_arg:
1494 seen_stack_size = ((seen_stack_size + align - 1) & -align) + size;
1495 break;
1497 case x86_64_mode_integer:
1498 if (seen_reg_num + reg_count > REGN)
1499 goto stack_arg;
1500 seen_reg_num += reg_count;
1501 break;
1503 case x86_64_mode_sse:
1504 if (seen_sse_num + reg_count > 8)
1505 goto stack_arg;
1506 seen_sse_num += reg_count;
1507 break;
1511 loc -= 24;
1512 /* movl $0x????????, -0x18(%rbp) */
1513 o(0xe845c7);
1514 gen_le32(seen_reg_num * 8);
1515 /* movl $0x????????, -0x14(%rbp) */
1516 o(0xec45c7);
1517 gen_le32(seen_sse_num * 16 + 48);
1518 /* leaq $0x????????, %r11 */
1519 o(0x9d8d4c);
1520 gen_le32(seen_stack_size);
1521 /* movq %r11, -0x10(%rbp) */
1522 o(0xf05d894c);
1523 /* leaq $-192(%rbp), %r11 */
1524 o(0x9d8d4c);
1525 gen_le32(-176 - 24);
1526 /* movq %r11, -0x8(%rbp) */
1527 o(0xf85d894c);
1529 /* save all register passing arguments */
1530 for (i = 0; i < 8; i++) {
1531 loc -= 16;
1532 if (!tcc_state->nosse) {
1533 o(0xd60f66); /* movq */
1534 gen_modrm(7 - i, VT_LOCAL, NULL, loc);
1536 /* movq $0, loc+8(%rbp) */
1537 o(0x85c748);
1538 gen_le32(loc + 8);
1539 gen_le32(0);
1541 for (i = 0; i < REGN; i++) {
1542 push_arg_reg(REGN-1-i);
1546 sym = func_type->ref;
1547 reg_param_index = 0;
1548 sse_param_index = 0;
1550 /* if the function returns a structure, then add an
1551 implicit pointer parameter */
1552 if (ret_mode == x86_64_mode_memory) {
1553 push_arg_reg(reg_param_index);
1554 func_vc = loc;
1555 reg_param_index++;
1557 /* define parameters */
1558 while ((sym = sym->next) != NULL) {
1559 type = &sym->type;
1560 mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
1561 switch (mode) {
1562 case x86_64_mode_sse:
1563 if (tcc_state->nosse)
1564 tcc_error("SSE disabled but floating point arguments used");
1565 if (sse_param_index + reg_count <= 8) {
1566 /* save arguments passed by register */
1567 loc -= reg_count * 8;
1568 param_addr = loc;
1569 for (i = 0; i < reg_count; ++i) {
1570 o(0xd60f66); /* movq */
1571 gen_modrm(sse_param_index, VT_LOCAL, NULL, param_addr + i*8);
1572 ++sse_param_index;
1574 } else {
1575 addr = (addr + align - 1) & -align;
1576 param_addr = addr;
1577 addr += size;
1579 break;
1581 case x86_64_mode_memory:
1582 case x86_64_mode_x87:
1583 addr = (addr + align - 1) & -align;
1584 param_addr = addr;
1585 addr += size;
1586 break;
1588 case x86_64_mode_integer: {
1589 if (reg_param_index + reg_count <= REGN) {
1590 /* save arguments passed by register */
1591 loc -= reg_count * 8;
1592 param_addr = loc;
1593 for (i = 0; i < reg_count; ++i) {
1594 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, param_addr + i*8);
1595 ++reg_param_index;
1597 } else {
1598 addr = (addr + align - 1) & -align;
1599 param_addr = addr;
1600 addr += size;
1602 break;
1604 default: break; /* nothing to be done for x86_64_mode_none */
1606 sym_push(sym->v & ~SYM_FIELD, type,
1607 VT_LOCAL | VT_LVAL, param_addr);
1610 #ifdef CONFIG_TCC_BCHECK
1611 if (tcc_state->do_bounds_check)
1612 gen_bounds_prolog();
1613 #endif
1616 /* generate function epilog */
1617 void gfunc_epilog(void)
1619 int v, saved_ind;
1621 #ifdef CONFIG_TCC_BCHECK
1622 if (tcc_state->do_bounds_check)
1623 gen_bounds_epilog();
1624 #endif
1625 o(0xc9); /* leave */
1626 if (func_ret_sub == 0) {
1627 o(0xc3); /* ret */
1628 } else {
1629 o(0xc2); /* ret n */
1630 g(func_ret_sub);
1631 g(func_ret_sub >> 8);
1633 /* align local size to word & save local variables */
1634 v = (-loc + 15) & -16;
1635 saved_ind = ind;
1636 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1637 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1638 o(0xec8148); /* sub rsp, stacksize */
1639 gen_le32(v);
1640 ind = saved_ind;
1643 #endif /* not PE */
1645 ST_FUNC void gen_fill_nops(int bytes)
1647 while (bytes--)
1648 g(0x90);
1651 /* generate a jump to a label */
1652 int gjmp(int t)
1654 return gjmp2(0xe9, t);
1657 /* generate a jump to a fixed address */
1658 void gjmp_addr(int a)
1660 int r;
1661 r = a - ind - 2;
1662 if (r == (char)r) {
1663 g(0xeb);
1664 g(r);
1665 } else {
1666 oad(0xe9, a - ind - 5);
1670 ST_FUNC int gjmp_append(int n, int t)
1672 void *p;
1673 /* insert vtop->c jump list in t */
1674 if (n) {
1675 uint32_t n1 = n, n2;
1676 while ((n2 = read32le(p = cur_text_section->data + n1)))
1677 n1 = n2;
1678 write32le(p, t);
1679 t = n;
1681 return t;
1684 ST_FUNC int gjmp_cond(int op, int t)
1686 if (op & 0x100)
1688 /* This was a float compare. If the parity flag is set
1689 the result was unordered. For anything except != this
1690 means false and we don't jump (anding both conditions).
1691 For != this means true (oring both).
1692 Take care about inverting the test. We need to jump
1693 to our target if the result was unordered and test wasn't NE,
1694 otherwise if unordered we don't want to jump. */
1695 int v = vtop->cmp_r;
1696 op &= ~0x100;
1697 if (op ^ v ^ (v != TOK_NE))
1698 o(0x067a); /* jp +6 */
1699 else
1701 g(0x0f);
1702 t = gjmp2(0x8a, t); /* jp t */
1705 g(0x0f);
1706 t = gjmp2(op - 16, t);
1707 return t;
1710 /* generate an integer binary operation */
1711 void gen_opi(int op)
1713 int r, fr, opc, c;
1714 int ll, uu, cc;
1716 ll = is64_type(vtop[-1].type.t);
1717 uu = (vtop[-1].type.t & VT_UNSIGNED) != 0;
1718 cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1720 switch(op) {
1721 case '+':
1722 case TOK_ADDC1: /* add with carry generation */
1723 opc = 0;
1724 gen_op8:
1725 if (cc && (!ll || (int)vtop->c.i == vtop->c.i)) {
1726 /* constant case */
1727 vswap();
1728 r = gv(RC_INT);
1729 vswap();
1730 c = vtop->c.i;
1731 if (c == (char)c) {
1732 /* XXX: generate inc and dec for smaller code ? */
1733 orex(ll, r, 0, 0x83);
1734 o(0xc0 | (opc << 3) | REG_VALUE(r));
1735 g(c);
1736 } else {
1737 orex(ll, r, 0, 0x81);
1738 oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
1740 } else {
1741 gv2(RC_INT, RC_INT);
1742 r = vtop[-1].r;
1743 fr = vtop[0].r;
1744 orex(ll, r, fr, (opc << 3) | 0x01);
1745 o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
1747 vtop--;
1748 if (op >= TOK_ULT && op <= TOK_GT)
1749 vset_VT_CMP(op);
1750 break;
1751 case '-':
1752 case TOK_SUBC1: /* sub with carry generation */
1753 opc = 5;
1754 goto gen_op8;
1755 case TOK_ADDC2: /* add with carry use */
1756 opc = 2;
1757 goto gen_op8;
1758 case TOK_SUBC2: /* sub with carry use */
1759 opc = 3;
1760 goto gen_op8;
1761 case '&':
1762 opc = 4;
1763 goto gen_op8;
1764 case '^':
1765 opc = 6;
1766 goto gen_op8;
1767 case '|':
1768 opc = 1;
1769 goto gen_op8;
1770 case '*':
1771 gv2(RC_INT, RC_INT);
1772 r = vtop[-1].r;
1773 fr = vtop[0].r;
1774 orex(ll, fr, r, 0xaf0f); /* imul fr, r */
1775 o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8);
1776 vtop--;
1777 break;
1778 case TOK_SHL:
1779 opc = 4;
1780 goto gen_shift;
1781 case TOK_SHR:
1782 opc = 5;
1783 goto gen_shift;
1784 case TOK_SAR:
1785 opc = 7;
1786 gen_shift:
1787 opc = 0xc0 | (opc << 3);
1788 if (cc) {
1789 /* constant case */
1790 vswap();
1791 r = gv(RC_INT);
1792 vswap();
1793 orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */
1794 o(opc | REG_VALUE(r));
1795 g(vtop->c.i & (ll ? 63 : 31));
1796 } else {
1797 /* we generate the shift in ecx */
1798 gv2(RC_INT, RC_RCX);
1799 r = vtop[-1].r;
1800 orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */
1801 o(opc | REG_VALUE(r));
1803 vtop--;
1804 break;
1805 case TOK_UDIV:
1806 case TOK_UMOD:
1807 uu = 1;
1808 goto divmod;
1809 case '/':
1810 case '%':
1811 case TOK_PDIV:
1812 uu = 0;
1813 divmod:
1814 /* first operand must be in eax */
1815 /* XXX: need better constraint for second operand */
1816 gv2(RC_RAX, RC_RCX);
1817 r = vtop[-1].r;
1818 fr = vtop[0].r;
1819 vtop--;
1820 save_reg(TREG_RDX);
1821 orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */
1822 orex(ll, fr, 0, 0xf7); /* div fr, %eax */
1823 o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr));
1824 if (op == '%' || op == TOK_UMOD)
1825 r = TREG_RDX;
1826 else
1827 r = TREG_RAX;
1828 vtop->r = r;
1829 break;
1830 default:
1831 opc = 7;
1832 goto gen_op8;
1836 void gen_opl(int op)
1838 gen_opi(op);
1841 void vpush_const(int t, int v)
1843 CType ctype = { t | VT_CONSTANT, 0 };
1844 vpushsym(&ctype, external_global_sym(v, &ctype));
1845 vtop->r |= VT_LVAL;
1848 /* generate a floating point operation 'v = t1 op t2' instruction. The
1849 two operands are guaranteed to have the same floating point type */
1850 /* XXX: need to use ST1 too */
1851 void gen_opf(int op)
1853 int a, ft, fc, swapped, r;
1854 int bt = vtop->type.t & VT_BTYPE;
1855 int float_type = bt == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
1857 if (op == TOK_NEG) { /* unary minus */
1858 gv(float_type);
1859 if (float_type == RC_ST0) {
1860 o(0xe0d9); /* fchs */
1861 } else {
1862 /* -0.0, in libtcc1.c */
1863 vpush_const(bt, bt == VT_FLOAT ? TOK___mzerosf : TOK___mzerodf);
1864 gv(RC_FLOAT);
1865 if (bt == VT_DOUBLE)
1866 o(0x66);
1867 /* xorp[sd] %xmm1, %xmm0 */
1868 o(0xc0570f | (REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8) << 16);
1869 vtop--;
1871 return;
1874 /* convert constants to memory references */
1875 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1876 vswap();
1877 gv(float_type);
1878 vswap();
1880 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
1881 gv(float_type);
1883 /* must put at least one value in the floating point register */
1884 if ((vtop[-1].r & VT_LVAL) &&
1885 (vtop[0].r & VT_LVAL)) {
1886 vswap();
1887 gv(float_type);
1888 vswap();
1890 swapped = 0;
1891 /* swap the stack if needed so that t1 is the register and t2 is
1892 the memory reference */
1893 if (vtop[-1].r & VT_LVAL) {
1894 vswap();
1895 swapped = 1;
1897 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1898 if (op >= TOK_ULT && op <= TOK_GT) {
1899 /* load on stack second operand */
1900 load(TREG_ST0, vtop);
1901 save_reg(TREG_RAX); /* eax is used by FP comparison code */
1902 if (op == TOK_GE || op == TOK_GT)
1903 swapped = !swapped;
1904 else if (op == TOK_EQ || op == TOK_NE)
1905 swapped = 0;
1906 if (swapped)
1907 o(0xc9d9); /* fxch %st(1) */
1908 if (op == TOK_EQ || op == TOK_NE)
1909 o(0xe9da); /* fucompp */
1910 else
1911 o(0xd9de); /* fcompp */
1912 o(0xe0df); /* fnstsw %ax */
1913 if (op == TOK_EQ) {
1914 o(0x45e480); /* and $0x45, %ah */
1915 o(0x40fC80); /* cmp $0x40, %ah */
1916 } else if (op == TOK_NE) {
1917 o(0x45e480); /* and $0x45, %ah */
1918 o(0x40f480); /* xor $0x40, %ah */
1919 op = TOK_NE;
1920 } else if (op == TOK_GE || op == TOK_LE) {
1921 o(0x05c4f6); /* test $0x05, %ah */
1922 op = TOK_EQ;
1923 } else {
1924 o(0x45c4f6); /* test $0x45, %ah */
1925 op = TOK_EQ;
1927 vtop--;
1928 vset_VT_CMP(op);
1929 } else {
1930 /* no memory reference possible for long double operations */
1931 load(TREG_ST0, vtop);
1932 swapped = !swapped;
1934 switch(op) {
1935 default:
1936 case '+':
1937 a = 0;
1938 break;
1939 case '-':
1940 a = 4;
1941 if (swapped)
1942 a++;
1943 break;
1944 case '*':
1945 a = 1;
1946 break;
1947 case '/':
1948 a = 6;
1949 if (swapped)
1950 a++;
1951 break;
1953 ft = vtop->type.t;
1954 fc = vtop->c.i;
1955 o(0xde); /* fxxxp %st, %st(1) */
1956 o(0xc1 + (a << 3));
1957 vtop--;
1959 } else {
1960 if (op >= TOK_ULT && op <= TOK_GT) {
1961 /* if saved lvalue, then we must reload it */
1962 r = vtop->r;
1963 fc = vtop->c.i;
1964 if ((r & VT_VALMASK) == VT_LLOCAL) {
1965 SValue v1;
1966 r = get_reg(RC_INT);
1967 v1.type.t = VT_PTR;
1968 v1.r = VT_LOCAL | VT_LVAL;
1969 v1.c.i = fc;
1970 load(r, &v1);
1971 fc = 0;
1972 vtop->r = r = r | VT_LVAL;
1975 if (op == TOK_EQ || op == TOK_NE) {
1976 swapped = 0;
1977 } else {
1978 if (op == TOK_LE || op == TOK_LT)
1979 swapped = !swapped;
1980 if (op == TOK_LE || op == TOK_GE) {
1981 op = 0x93; /* setae */
1982 } else {
1983 op = 0x97; /* seta */
1987 if (swapped) {
1988 gv(RC_FLOAT);
1989 vswap();
1991 assert(!(vtop[-1].r & VT_LVAL));
1993 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1994 o(0x66);
1995 if (op == TOK_EQ || op == TOK_NE)
1996 o(0x2e0f); /* ucomisd */
1997 else
1998 o(0x2f0f); /* comisd */
2000 if (vtop->r & VT_LVAL) {
2001 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
2002 } else {
2003 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
2006 vtop--;
2007 vset_VT_CMP(op | 0x100);
2008 vtop->cmp_r = op;
2009 } else {
2010 assert((vtop->type.t & VT_BTYPE) != VT_LDOUBLE);
2011 switch(op) {
2012 default:
2013 case '+':
2014 a = 0;
2015 break;
2016 case '-':
2017 a = 4;
2018 break;
2019 case '*':
2020 a = 1;
2021 break;
2022 case '/':
2023 a = 6;
2024 break;
2026 ft = vtop->type.t;
2027 fc = vtop->c.i;
2028 assert((ft & VT_BTYPE) != VT_LDOUBLE);
2030 r = vtop->r;
2031 /* if saved lvalue, then we must reload it */
2032 if ((vtop->r & VT_VALMASK) == VT_LLOCAL) {
2033 SValue v1;
2034 r = get_reg(RC_INT);
2035 v1.type.t = VT_PTR;
2036 v1.r = VT_LOCAL | VT_LVAL;
2037 v1.c.i = fc;
2038 load(r, &v1);
2039 fc = 0;
2040 vtop->r = r = r | VT_LVAL;
2043 assert(!(vtop[-1].r & VT_LVAL));
2044 if (swapped) {
2045 assert(vtop->r & VT_LVAL);
2046 gv(RC_FLOAT);
2047 vswap();
2048 fc = vtop->c.i; /* bcheck may have saved previous vtop[-1] */
2051 if ((ft & VT_BTYPE) == VT_DOUBLE) {
2052 o(0xf2);
2053 } else {
2054 o(0xf3);
2056 o(0x0f);
2057 o(0x58 + a);
2059 if (vtop->r & VT_LVAL) {
2060 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
2061 } else {
2062 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
2065 vtop--;
2070 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
2071 and 'long long' cases. */
2072 void gen_cvt_itof(int t)
2074 if ((t & VT_BTYPE) == VT_LDOUBLE) {
2075 save_reg(TREG_ST0);
2076 gv(RC_INT);
2077 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
2078 /* signed long long to float/double/long double (unsigned case
2079 is handled generically) */
2080 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2081 o(0x242cdf); /* fildll (%rsp) */
2082 o(0x08c48348); /* add $8, %rsp */
2083 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2084 (VT_INT | VT_UNSIGNED)) {
2085 /* unsigned int to float/double/long double */
2086 o(0x6a); /* push $0 */
2087 g(0x00);
2088 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2089 o(0x242cdf); /* fildll (%rsp) */
2090 o(0x10c48348); /* add $16, %rsp */
2091 } else {
2092 /* int to float/double/long double */
2093 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2094 o(0x2404db); /* fildl (%rsp) */
2095 o(0x08c48348); /* add $8, %rsp */
2097 vtop->r = TREG_ST0;
2098 } else {
2099 int r = get_reg(RC_FLOAT);
2100 gv(RC_INT);
2101 o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT?1:0));
2102 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2103 (VT_INT | VT_UNSIGNED) ||
2104 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
2105 o(0x48); /* REX */
2107 o(0x2a0f);
2108 o(0xc0 + (vtop->r & VT_VALMASK) + REG_VALUE(r)*8); /* cvtsi2sd */
2109 vtop->r = r;
2113 /* convert from one floating point type to another */
2114 void gen_cvt_ftof(int t)
2116 int ft, bt, tbt;
2118 ft = vtop->type.t;
2119 bt = ft & VT_BTYPE;
2120 tbt = t & VT_BTYPE;
2122 if (bt == VT_FLOAT) {
2123 gv(RC_FLOAT);
2124 if (tbt == VT_DOUBLE) {
2125 o(0x140f); /* unpcklps */
2126 o(0xc0 + REG_VALUE(vtop->r)*9);
2127 o(0x5a0f); /* cvtps2pd */
2128 o(0xc0 + REG_VALUE(vtop->r)*9);
2129 } else if (tbt == VT_LDOUBLE) {
2130 save_reg(RC_ST0);
2131 /* movss %xmm0,-0x10(%rsp) */
2132 o(0x110ff3);
2133 o(0x44 + REG_VALUE(vtop->r)*8);
2134 o(0xf024);
2135 o(0xf02444d9); /* flds -0x10(%rsp) */
2136 vtop->r = TREG_ST0;
2138 } else if (bt == VT_DOUBLE) {
2139 gv(RC_FLOAT);
2140 if (tbt == VT_FLOAT) {
2141 o(0x140f66); /* unpcklpd */
2142 o(0xc0 + REG_VALUE(vtop->r)*9);
2143 o(0x5a0f66); /* cvtpd2ps */
2144 o(0xc0 + REG_VALUE(vtop->r)*9);
2145 } else if (tbt == VT_LDOUBLE) {
2146 save_reg(RC_ST0);
2147 /* movsd %xmm0,-0x10(%rsp) */
2148 o(0x110ff2);
2149 o(0x44 + REG_VALUE(vtop->r)*8);
2150 o(0xf024);
2151 o(0xf02444dd); /* fldl -0x10(%rsp) */
2152 vtop->r = TREG_ST0;
2154 } else {
2155 int r;
2156 gv(RC_ST0);
2157 r = get_reg(RC_FLOAT);
2158 if (tbt == VT_DOUBLE) {
2159 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
2160 /* movsd -0x10(%rsp),%xmm0 */
2161 o(0x100ff2);
2162 o(0x44 + REG_VALUE(r)*8);
2163 o(0xf024);
2164 vtop->r = r;
2165 } else if (tbt == VT_FLOAT) {
2166 o(0xf0245cd9); /* fstps -0x10(%rsp) */
2167 /* movss -0x10(%rsp),%xmm0 */
2168 o(0x100ff3);
2169 o(0x44 + REG_VALUE(r)*8);
2170 o(0xf024);
2171 vtop->r = r;
2176 /* convert fp to int 't' type */
2177 void gen_cvt_ftoi(int t)
2179 int ft, bt, size, r;
2180 ft = vtop->type.t;
2181 bt = ft & VT_BTYPE;
2182 if (bt == VT_LDOUBLE) {
2183 gen_cvt_ftof(VT_DOUBLE);
2184 bt = VT_DOUBLE;
2187 gv(RC_FLOAT);
2188 if (t != VT_INT)
2189 size = 8;
2190 else
2191 size = 4;
2193 r = get_reg(RC_INT);
2194 if (bt == VT_FLOAT) {
2195 o(0xf3);
2196 } else if (bt == VT_DOUBLE) {
2197 o(0xf2);
2198 } else {
2199 assert(0);
2201 orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */
2202 o(0xc0 + REG_VALUE(vtop->r) + REG_VALUE(r)*8);
2203 vtop->r = r;
2206 // Generate sign extension from 32 to 64 bits:
2207 ST_FUNC void gen_cvt_sxtw(void)
2209 int r = gv(RC_INT);
2210 /* x86_64 specific: movslq */
2211 o(0x6348);
2212 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2215 /* char/short to int conversion */
2216 ST_FUNC void gen_cvt_csti(int t)
2218 int r, sz, xl, ll;
2219 r = gv(RC_INT);
2220 sz = !(t & VT_UNSIGNED);
2221 xl = (t & VT_BTYPE) == VT_SHORT;
2222 ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
2223 orex(ll, r, 0, 0xc0b60f /* mov[sz] %a[xl], %eax */
2224 | (sz << 3 | xl) << 8
2225 | (REG_VALUE(r) << 3 | REG_VALUE(r)) << 16
2229 /* increment tcov counter */
2230 ST_FUNC void gen_increment_tcov (SValue *sv)
2232 o(0x058348); /* addq $1, xxx(%rip) */
2233 greloca(cur_text_section, sv->sym, ind, R_X86_64_PC32, -5);
2234 gen_le32(0);
2235 o(1);
2238 /* computed goto support */
2239 ST_FUNC void ggoto(void)
2241 gcall_or_jmp(1);
2242 vtop--;
2245 /* Save the stack pointer onto the stack and return the location of its address */
2246 ST_FUNC void gen_vla_sp_save(int addr) {
2247 /* mov %rsp,addr(%rbp)*/
2248 gen_modrm64(0x89, TREG_RSP, VT_LOCAL, NULL, addr);
2251 /* Restore the SP from a location on the stack */
2252 ST_FUNC void gen_vla_sp_restore(int addr) {
2253 gen_modrm64(0x8b, TREG_RSP, VT_LOCAL, NULL, addr);
2256 #ifdef TCC_TARGET_PE
2257 /* Save result of gen_vla_alloc onto the stack */
2258 ST_FUNC void gen_vla_result(int addr) {
2259 /* mov %rax,addr(%rbp)*/
2260 gen_modrm64(0x89, TREG_RAX, VT_LOCAL, NULL, addr);
2262 #endif
2264 /* Subtract from the stack pointer, and push the resulting value onto the stack */
2265 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2266 int use_call = 0;
2268 #if defined(CONFIG_TCC_BCHECK)
2269 use_call = tcc_state->do_bounds_check;
2270 #endif
2271 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
2272 use_call = 1;
2273 #endif
2274 if (use_call)
2276 vpush_helper_func(TOK_alloca);
2277 vswap(); /* Move alloca ref past allocation size */
2278 gfunc_call(1);
2280 else {
2281 int r;
2282 r = gv(RC_INT); /* allocation size */
2283 /* sub r,%rsp */
2284 o(0x2b48);
2285 o(0xe0 | REG_VALUE(r));
2286 /* We align to 16 bytes rather than align */
2287 /* and ~15, %rsp */
2288 o(0xf0e48348);
2289 vpop();
2294 * Assmuing the top part of the stack looks like below,
2295 * src dest src
2297 ST_FUNC void gen_struct_copy(int size)
2299 int n = size / PTR_SIZE;
2300 #ifdef TCC_TARGET_PE
2301 o(0x5756); /* push rsi, rdi */
2302 #endif
2303 gv2(RC_RDI, RC_RSI);
2304 if (n <= 4) {
2305 while (n)
2306 o(0xa548), --n;
2307 } else {
2308 vpushi(n);
2309 gv(RC_RCX);
2310 o(0xa548f3);
2311 vpop();
2313 if (size & 0x04)
2314 o(0xa5);
2315 if (size & 0x02)
2316 o(0xa566);
2317 if (size & 0x01)
2318 o(0xa4);
2319 #ifdef TCC_TARGET_PE
2320 o(0x5e5f); /* pop rdi, rsi */
2321 #endif
2322 vpop();
2323 vpop();
2326 /* end of x86-64 code generator */
2327 /*************************************************************/
2328 #endif /* ! TARGET_DEFS_ONLY */
2329 /******************************************************/