fixup! riscv: Implement large addend for global address
[tinycc.git] / x86_64-gen.c
blob4f46efc90fd5713937152d5722074c527270498f
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 orex(1,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
493 gen_le64(sv->c.i);
494 } else {
495 orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
496 gen_le32(fc);
498 } else if (v == VT_LOCAL) {
499 orex(1,0,r,0x8d); /* lea xxx(%ebp), r */
500 gen_modrm(r, VT_LOCAL, sv->sym, fc);
501 } else if (v == VT_CMP) {
502 if (fc & 0x100)
504 v = vtop->cmp_r;
505 fc &= ~0x100;
506 /* This was a float compare. If the parity bit is
507 set the result was unordered, meaning false for everything
508 except TOK_NE, and true for TOK_NE. */
509 orex(0, r, 0, 0xb0 + REG_VALUE(r)); /* mov $0/1,%al */
510 g(v ^ fc ^ (v == TOK_NE));
511 o(0x037a + (REX_BASE(r) << 8));
513 orex(0,r,0, 0x0f); /* setxx %br */
514 o(fc);
515 o(0xc0 + REG_VALUE(r));
516 orex(0,r,0, 0x0f);
517 o(0xc0b6 + REG_VALUE(r) * 0x900); /* movzbl %al, %eax */
518 } else if (v == VT_JMP || v == VT_JMPI) {
519 t = v & 1;
520 orex(0,r,0,0);
521 oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */
522 o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */
523 gsym(fc);
524 orex(0,r,0,0);
525 oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */
526 } else if (v != r) {
527 if ((r >= TREG_XMM0) && (r <= TREG_XMM7)) {
528 if (v == TREG_ST0) {
529 /* gen_cvt_ftof(VT_DOUBLE); */
530 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
531 /* movsd -0x10(%rsp),%xmmN */
532 o(0x100ff2);
533 o(0x44 + REG_VALUE(r)*8); /* %xmmN */
534 o(0xf024);
535 } else {
536 assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
537 if ((ft & VT_BTYPE) == VT_FLOAT) {
538 o(0x100ff3);
539 } else {
540 assert((ft & VT_BTYPE) == VT_DOUBLE);
541 o(0x100ff2);
543 o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8);
545 } else if (r == TREG_ST0) {
546 assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
547 /* gen_cvt_ftof(VT_LDOUBLE); */
548 /* movsd %xmmN,-0x10(%rsp) */
549 o(0x110ff2);
550 o(0x44 + REG_VALUE(r)*8); /* %xmmN */
551 o(0xf024);
552 o(0xf02444dd); /* fldl -0x10(%rsp) */
553 } else {
554 orex(is64_type(ft), r, v, 0x89);
555 o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */
561 /* store register 'r' in lvalue 'v' */
562 void store(int r, SValue *v)
564 int fr, bt, ft, fc;
565 int op64 = 0;
566 /* store the REX prefix in this variable when PIC is enabled */
567 int pic = 0;
569 #ifdef TCC_TARGET_PE
570 SValue v2;
571 v = pe_getimport(v, &v2);
572 #endif
574 fr = v->r & VT_VALMASK;
575 ft = v->type.t;
576 fc = v->c.i;
577 if (fc != v->c.i && (fr & VT_SYM))
578 tcc_error("64 bit addend in store");
579 ft &= ~(VT_VOLATILE | VT_CONSTANT);
580 bt = ft & VT_BTYPE;
582 #ifndef TCC_TARGET_PE
583 /* we need to access the variable via got */
584 if (fr == VT_CONST
585 && (v->r & VT_SYM)
586 && !(v->sym->type.t & VT_STATIC)) {
587 /* mov xx(%rip), %r11 */
588 o(0x1d8b4c);
589 gen_gotpcrel(TREG_R11, v->sym, v->c.i);
590 pic = is64_type(bt) ? 0x49 : 0x41;
592 #endif
594 /* XXX: incorrect if float reg to reg */
595 if (bt == VT_FLOAT) {
596 o(0x66);
597 o(pic);
598 o(0x7e0f); /* movd */
599 r = REG_VALUE(r);
600 } else if (bt == VT_DOUBLE) {
601 o(0x66);
602 o(pic);
603 o(0xd60f); /* movq */
604 r = REG_VALUE(r);
605 } else if (bt == VT_LDOUBLE) {
606 o(0xc0d9); /* fld %st(0) */
607 o(pic);
608 o(0xdb); /* fstpt */
609 r = 7;
610 } else {
611 if (bt == VT_SHORT)
612 o(0x66);
613 o(pic);
614 if (bt == VT_BYTE || bt == VT_BOOL)
615 orex(0, 0, r, 0x88);
616 else if (is64_type(bt))
617 op64 = 0x89;
618 else
619 orex(0, 0, r, 0x89);
621 if (pic) {
622 /* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
623 if (op64)
624 o(op64);
625 o(3 + (r << 3));
626 } else if (op64) {
627 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
628 gen_modrm64(op64, r, v->r, v->sym, fc);
629 } else if (fr != r) {
630 orex(1, fr, r, op64);
631 o(0xc0 + fr + r * 8); /* mov r, fr */
633 } else {
634 if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
635 gen_modrm(r, v->r, v->sym, fc);
636 } else if (fr != r) {
637 o(0xc0 + fr + r * 8); /* mov r, fr */
642 /* 'is_jmp' is '1' if it is a jump */
643 static void gcall_or_jmp(int is_jmp)
645 int r;
646 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
647 ((vtop->r & VT_SYM) && (vtop->c.i-4) == (int)(vtop->c.i-4))) {
648 /* constant symbolic case -> simple relocation */
649 greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));
650 oad(0xe8 + is_jmp, 0); /* call/jmp im */
651 } else {
652 /* otherwise, indirect call */
653 r = TREG_R11;
654 load(r, vtop);
655 o(0x41); /* REX */
656 o(0xff); /* call/jmp *r */
657 o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
661 #if defined(CONFIG_TCC_BCHECK)
663 static void gen_bounds_call(int v)
665 Sym *sym = external_helper_sym(v);
666 oad(0xe8, 0);
667 greloca(cur_text_section, sym, ind-4, R_X86_64_PLT32, -4);
670 #ifdef TCC_TARGET_PE
671 # define TREG_FASTCALL_1 TREG_RCX
672 #else
673 # define TREG_FASTCALL_1 TREG_RDI
674 #endif
676 static void gen_bounds_prolog(void)
678 /* leave some room for bound checking code */
679 func_bound_offset = lbounds_section->data_offset;
680 func_bound_ind = ind;
681 func_bound_add_epilog = 0;
682 o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /*lbound section pointer */
683 gen_le32 (0);
684 oad(0xb8, 0); /* call to function */
687 static void gen_bounds_epilog(void)
689 addr_t saved_ind;
690 addr_t *bounds_ptr;
691 Sym *sym_data;
692 int offset_modified = func_bound_offset != lbounds_section->data_offset;
694 if (!offset_modified && !func_bound_add_epilog)
695 return;
697 /* add end of table info */
698 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
699 *bounds_ptr = 0;
701 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
702 func_bound_offset, PTR_SIZE);
704 /* generate bound local allocation */
705 if (offset_modified) {
706 saved_ind = ind;
707 ind = func_bound_ind;
708 greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
709 ind = ind + 7;
710 gen_bounds_call(TOK___bound_local_new);
711 ind = saved_ind;
714 /* generate bound check local freeing */
715 o(0x5250); /* save returned value, if any */
716 o(0x20ec8348); /* sub $32,%rsp */
717 o(0x290f); /* movaps %xmm0,0x10(%rsp) */
718 o(0x102444);
719 o(0x240c290f); /* movaps %xmm1,(%rsp) */
720 greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
721 o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /* lea xxx(%rip), %rcx/rdi */
722 gen_le32 (0);
723 gen_bounds_call(TOK___bound_local_delete);
724 o(0x280f); /* movaps 0x10(%rsp),%xmm0 */
725 o(0x102444);
726 o(0x240c280f); /* movaps (%rsp),%xmm1 */
727 o(0x20c48348); /* add $32,%rsp */
728 o(0x585a); /* restore returned value, if any */
730 #endif
732 #ifdef TCC_TARGET_PE
734 #define REGN 4
735 static const uint8_t arg_regs[REGN] = {
736 TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
739 /* Prepare arguments in R10 and R11 rather than RCX and RDX
740 because gv() will not ever use these */
741 static int arg_prepare_reg(int idx) {
742 if (idx == 0 || idx == 1)
743 /* idx=0: r10, idx=1: r11 */
744 return idx + 10;
745 else
746 return idx >= 0 && idx < REGN ? arg_regs[idx] : 0;
749 /* Generate function call. The function address is pushed first, then
750 all the parameters in call order. This functions pops all the
751 parameters and the function address. */
753 static void gen_offs_sp(int b, int r, int d)
755 orex(1,0,r & 0x100 ? 0 : r, b);
756 if (d == (char)d) {
757 o(0x2444 | (REG_VALUE(r) << 3));
758 g(d);
759 } else {
760 o(0x2484 | (REG_VALUE(r) << 3));
761 gen_le32(d);
765 static int using_regs(int size)
767 return !(size > 8 || (size & (size - 1)));
770 /* Return the number of registers needed to return the struct, or 0 if
771 returning via struct pointer. */
772 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
774 int size, align;
775 *ret_align = 1; // Never have to re-align return values for x86-64
776 *regsize = 8;
777 size = type_size(vt, &align);
778 if (!using_regs(size))
779 return 0;
780 if (size == 8)
781 ret->t = VT_LLONG;
782 else if (size == 4)
783 ret->t = VT_INT;
784 else if (size == 2)
785 ret->t = VT_SHORT;
786 else
787 ret->t = VT_BYTE;
788 ret->ref = NULL;
789 return 1;
792 static int is_sse_float(int t) {
793 int bt;
794 bt = t & VT_BTYPE;
795 return bt == VT_DOUBLE || bt == VT_FLOAT;
798 static int gfunc_arg_size(CType *type) {
799 int align;
800 if (type->t & (VT_ARRAY|VT_BITFIELD))
801 return 8;
802 return type_size(type, &align);
805 void gfunc_call(int nb_args)
807 int size, r, args_size, i, d, bt, struct_size;
808 int arg;
810 #ifdef CONFIG_TCC_BCHECK
811 if (tcc_state->do_bounds_check)
812 gbound_args(nb_args);
813 #endif
815 args_size = (nb_args < REGN ? REGN : nb_args) * PTR_SIZE;
816 arg = nb_args;
818 /* for struct arguments, we need to call memcpy and the function
819 call breaks register passing arguments we are preparing.
820 So, we process arguments which will be passed by stack first. */
821 struct_size = args_size;
822 for(i = 0; i < nb_args; i++) {
823 SValue *sv;
825 --arg;
826 sv = &vtop[-i];
827 bt = (sv->type.t & VT_BTYPE);
828 size = gfunc_arg_size(&sv->type);
830 if (using_regs(size))
831 continue; /* arguments smaller than 8 bytes passed in registers or on stack */
833 if (bt == VT_STRUCT) {
834 /* align to stack align size */
835 size = (size + 15) & ~15;
836 /* generate structure store */
837 r = get_reg(RC_INT);
838 gen_offs_sp(0x8d, r, struct_size);
839 struct_size += size;
841 /* generate memcpy call */
842 vset(&sv->type, r | VT_LVAL, 0);
843 vpushv(sv);
844 vstore();
845 --vtop;
846 } else if (bt == VT_LDOUBLE) {
847 gv(RC_ST0);
848 gen_offs_sp(0xdb, 0x107, struct_size);
849 struct_size += 16;
853 if (func_scratch < struct_size)
854 func_scratch = struct_size;
856 arg = nb_args;
857 struct_size = args_size;
859 for(i = 0; i < nb_args; i++) {
860 --arg;
861 bt = (vtop->type.t & VT_BTYPE);
863 size = gfunc_arg_size(&vtop->type);
864 if (!using_regs(size)) {
865 /* align to stack align size */
866 size = (size + 15) & ~15;
867 if (arg >= REGN) {
868 d = get_reg(RC_INT);
869 gen_offs_sp(0x8d, d, struct_size);
870 gen_offs_sp(0x89, d, arg*8);
871 } else {
872 d = arg_prepare_reg(arg);
873 gen_offs_sp(0x8d, d, struct_size);
875 struct_size += size;
876 } else {
877 if (is_sse_float(vtop->type.t)) {
878 if (tcc_state->nosse)
879 tcc_error("SSE disabled");
880 if (arg >= REGN) {
881 gv(RC_XMM0);
882 /* movq %xmm0, j*8(%rsp) */
883 gen_offs_sp(0xd60f66, 0x100, arg*8);
884 } else {
885 /* Load directly to xmmN register */
886 gv(RC_XMM0 << arg);
887 d = arg_prepare_reg(arg);
888 /* mov %xmmN, %rxx */
889 o(0x66);
890 orex(1,d,0, 0x7e0f);
891 o(0xc0 + arg*8 + REG_VALUE(d));
893 } else {
894 if (bt == VT_STRUCT) {
895 vtop->type.ref = NULL;
896 vtop->type.t = size > 4 ? VT_LLONG : size > 2 ? VT_INT
897 : size > 1 ? VT_SHORT : VT_BYTE;
900 r = gv(RC_INT);
901 if (arg >= REGN) {
902 gen_offs_sp(0x89, r, arg*8);
903 } else {
904 d = arg_prepare_reg(arg);
905 orex(1,d,r,0x89); /* mov */
906 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
910 vtop--;
912 save_regs(0);
913 /* Copy R10 and R11 into RCX and RDX, respectively */
914 if (nb_args > 0) {
915 o(0xd1894c); /* mov %r10, %rcx */
916 if (nb_args > 1) {
917 o(0xda894c); /* mov %r11, %rdx */
921 gcall_or_jmp(0);
923 if ((vtop->r & VT_SYM) && vtop->sym->v == TOK_alloca) {
924 /* need to add the "func_scratch" area after alloca */
925 o(0x48); func_alloca = oad(0x05, func_alloca); /* add $NN, %rax */
926 #ifdef CONFIG_TCC_BCHECK
927 if (tcc_state->do_bounds_check)
928 gen_bounds_call(TOK___bound_alloca_nr); /* new region */
929 #endif
931 vtop--;
935 #define FUNC_PROLOG_SIZE 11
937 /* generate function prolog of type 't' */
938 void gfunc_prolog(Sym *func_sym)
940 CType *func_type = &func_sym->type;
941 int addr, reg_param_index, bt, size;
942 Sym *sym;
943 CType *type;
945 func_ret_sub = 0;
946 func_scratch = 32;
947 func_alloca = 0;
948 loc = 0;
950 addr = PTR_SIZE * 2;
951 ind += FUNC_PROLOG_SIZE;
952 func_sub_sp_offset = ind;
953 reg_param_index = 0;
955 sym = func_type->ref;
957 /* if the function returns a structure, then add an
958 implicit pointer parameter */
959 size = gfunc_arg_size(&func_vt);
960 if (!using_regs(size)) {
961 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
962 func_vc = addr;
963 reg_param_index++;
964 addr += 8;
967 /* define parameters */
968 while ((sym = sym->next) != NULL) {
969 type = &sym->type;
970 bt = type->t & VT_BTYPE;
971 size = gfunc_arg_size(type);
972 if (!using_regs(size)) {
973 if (reg_param_index < REGN) {
974 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
976 sym_push(sym->v & ~SYM_FIELD, type,
977 VT_LLOCAL | VT_LVAL, addr);
978 } else {
979 if (reg_param_index < REGN) {
980 /* save arguments passed by register */
981 if ((bt == VT_FLOAT) || (bt == VT_DOUBLE)) {
982 if (tcc_state->nosse)
983 tcc_error("SSE disabled");
984 o(0xd60f66); /* movq */
985 gen_modrm(reg_param_index, VT_LOCAL, NULL, addr);
986 } else {
987 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
990 sym_push(sym->v & ~SYM_FIELD, type,
991 VT_LOCAL | VT_LVAL, addr);
993 addr += 8;
994 reg_param_index++;
997 while (reg_param_index < REGN) {
998 if (func_var) {
999 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
1000 addr += 8;
1002 reg_param_index++;
1004 #ifdef CONFIG_TCC_BCHECK
1005 if (tcc_state->do_bounds_check)
1006 gen_bounds_prolog();
1007 #endif
1010 /* generate function epilog */
1011 void gfunc_epilog(void)
1013 int v, saved_ind;
1015 /* align local size to word & save local variables */
1016 func_scratch = (func_scratch + 15) & -16;
1017 loc = (loc & -16) - func_scratch;
1019 #ifdef CONFIG_TCC_BCHECK
1020 if (tcc_state->do_bounds_check)
1021 gen_bounds_epilog();
1022 #endif
1024 o(0xc9); /* leave */
1025 if (func_ret_sub == 0) {
1026 o(0xc3); /* ret */
1027 } else {
1028 o(0xc2); /* ret n */
1029 g(func_ret_sub);
1030 g(func_ret_sub >> 8);
1033 saved_ind = ind;
1034 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1035 v = -loc;
1037 if (v >= 4096) {
1038 Sym *sym = external_helper_sym(TOK___chkstk);
1039 oad(0xb8, v); /* mov stacksize, %eax */
1040 oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */
1041 greloca(cur_text_section, sym, ind-4, R_X86_64_PLT32, -4);
1042 o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
1043 } else {
1044 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1045 o(0xec8148); /* sub rsp, stacksize */
1046 gen_le32(v);
1049 /* add the "func_scratch" area after each alloca seen */
1050 gsym_addr(func_alloca, -func_scratch);
1052 cur_text_section->data_offset = saved_ind;
1053 pe_add_unwind_data(ind, saved_ind, v);
1054 ind = cur_text_section->data_offset;
1057 #else
1059 static void gadd_sp(int val)
1061 if (val == (char)val) {
1062 o(0xc48348);
1063 g(val);
1064 } else {
1065 oad(0xc48148, val); /* add $xxx, %rsp */
1069 typedef enum X86_64_Mode {
1070 x86_64_mode_none,
1071 x86_64_mode_memory,
1072 x86_64_mode_integer,
1073 x86_64_mode_sse,
1074 x86_64_mode_x87
1075 } X86_64_Mode;
1077 static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b)
1079 if (a == b)
1080 return a;
1081 else if (a == x86_64_mode_none)
1082 return b;
1083 else if (b == x86_64_mode_none)
1084 return a;
1085 else if ((a == x86_64_mode_memory) || (b == x86_64_mode_memory))
1086 return x86_64_mode_memory;
1087 else if ((a == x86_64_mode_integer) || (b == x86_64_mode_integer))
1088 return x86_64_mode_integer;
1089 else if ((a == x86_64_mode_x87) || (b == x86_64_mode_x87))
1090 return x86_64_mode_memory;
1091 else
1092 return x86_64_mode_sse;
1095 static X86_64_Mode classify_x86_64_inner(CType *ty)
1097 X86_64_Mode mode;
1098 Sym *f;
1100 switch (ty->t & VT_BTYPE) {
1101 case VT_VOID: return x86_64_mode_none;
1103 case VT_INT:
1104 case VT_BYTE:
1105 case VT_SHORT:
1106 case VT_LLONG:
1107 case VT_BOOL:
1108 case VT_PTR:
1109 case VT_FUNC:
1110 return x86_64_mode_integer;
1112 case VT_FLOAT:
1113 case VT_DOUBLE: return x86_64_mode_sse;
1115 case VT_LDOUBLE: return x86_64_mode_x87;
1117 case VT_STRUCT:
1118 f = ty->ref;
1120 mode = x86_64_mode_none;
1121 for (f = f->next; f; f = f->next)
1122 mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type));
1124 return mode;
1126 assert(0);
1127 return 0;
1130 static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count)
1132 X86_64_Mode mode;
1133 int size, align, ret_t = 0;
1135 if (ty->t & (VT_BITFIELD|VT_ARRAY)) {
1136 *psize = 8;
1137 *palign = 8;
1138 *reg_count = 1;
1139 ret_t = ty->t;
1140 mode = x86_64_mode_integer;
1141 } else {
1142 size = type_size(ty, &align);
1143 *psize = (size + 7) & ~7;
1144 *palign = (align + 7) & ~7;
1145 *reg_count = 0; /* avoid compiler warning */
1147 if (size > 16) {
1148 mode = x86_64_mode_memory;
1149 } else {
1150 mode = classify_x86_64_inner(ty);
1151 switch (mode) {
1152 case x86_64_mode_integer:
1153 if (size > 8) {
1154 *reg_count = 2;
1155 ret_t = VT_QLONG;
1156 } else {
1157 *reg_count = 1;
1158 if (size > 4)
1159 ret_t = VT_LLONG;
1160 else if (size > 2)
1161 ret_t = VT_INT;
1162 else if (size > 1)
1163 ret_t = VT_SHORT;
1164 else
1165 ret_t = VT_BYTE;
1166 if ((ty->t & VT_BTYPE) == VT_STRUCT || (ty->t & VT_UNSIGNED))
1167 ret_t |= VT_UNSIGNED;
1169 break;
1171 case x86_64_mode_x87:
1172 *reg_count = 1;
1173 ret_t = VT_LDOUBLE;
1174 break;
1176 case x86_64_mode_sse:
1177 if (size > 8) {
1178 *reg_count = 2;
1179 ret_t = VT_QFLOAT;
1180 } else {
1181 *reg_count = 1;
1182 ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT;
1184 break;
1185 default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/
1190 if (ret) {
1191 ret->ref = NULL;
1192 ret->t = ret_t;
1195 return mode;
1198 ST_FUNC int classify_x86_64_va_arg(CType *ty)
1200 /* This definition must be synced with stdarg.h */
1201 enum __va_arg_type {
1202 __va_gen_reg, __va_float_reg, __va_stack
1204 int size, align, reg_count;
1205 X86_64_Mode mode = classify_x86_64_arg(ty, NULL, &size, &align, &reg_count);
1206 switch (mode) {
1207 default: return __va_stack;
1208 case x86_64_mode_integer: return __va_gen_reg;
1209 case x86_64_mode_sse: return __va_float_reg;
1213 /* Return the number of registers needed to return the struct, or 0 if
1214 returning via struct pointer. */
1215 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
1217 int size, align, reg_count;
1218 if (classify_x86_64_arg(vt, ret, &size, &align, &reg_count) == x86_64_mode_memory)
1219 return 0;
1220 *ret_align = 1; // Never have to re-align return values for x86-64
1221 *regsize = 8 * reg_count; /* the (virtual) regsize is 16 for VT_QLONG/QFLOAT */
1222 return 1;
1225 #define REGN 6
1226 static const uint8_t arg_regs[REGN] = {
1227 TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
1230 static int arg_prepare_reg(int idx) {
1231 if (idx == 2 || idx == 3)
1232 /* idx=2: r10, idx=3: r11 */
1233 return idx + 8;
1234 else
1235 return idx >= 0 && idx < REGN ? arg_regs[idx] : 0;
1238 /* Generate function call. The function address is pushed first, then
1239 all the parameters in call order. This functions pops all the
1240 parameters and the function address. */
1241 void gfunc_call(int nb_args)
1243 X86_64_Mode mode;
1244 CType type;
1245 int size, align, r, args_size, stack_adjust, i, reg_count, k;
1246 int nb_reg_args = 0;
1247 int nb_sse_args = 0;
1248 int sse_reg, gen_reg;
1249 char *onstack = tcc_malloc((nb_args + 1) * sizeof (char));
1251 #ifdef CONFIG_TCC_BCHECK
1252 if (tcc_state->do_bounds_check)
1253 gbound_args(nb_args);
1254 #endif
1256 /* calculate the number of integer/float register arguments, remember
1257 arguments to be passed via stack (in onstack[]), and also remember
1258 if we have to align the stack pointer to 16 (onstack[i] == 2). Needs
1259 to be done in a left-to-right pass over arguments. */
1260 stack_adjust = 0;
1261 for(i = nb_args - 1; i >= 0; i--) {
1262 mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
1263 if (size == 0) continue;
1264 if (mode == x86_64_mode_sse && nb_sse_args + reg_count <= 8) {
1265 nb_sse_args += reg_count;
1266 onstack[i] = 0;
1267 } else if (mode == x86_64_mode_integer && nb_reg_args + reg_count <= REGN) {
1268 nb_reg_args += reg_count;
1269 onstack[i] = 0;
1270 } else if (mode == x86_64_mode_none) {
1271 onstack[i] = 0;
1272 } else {
1273 if (align == 16 && (stack_adjust &= 15)) {
1274 onstack[i] = 2;
1275 stack_adjust = 0;
1276 } else
1277 onstack[i] = 1;
1278 stack_adjust += size;
1282 if (nb_sse_args && tcc_state->nosse)
1283 tcc_error("SSE disabled but floating point arguments passed");
1285 /* fetch cpu flag before generating any code */
1286 if ((vtop->r & VT_VALMASK) == VT_CMP)
1287 gv(RC_INT);
1289 /* for struct arguments, we need to call memcpy and the function
1290 call breaks register passing arguments we are preparing.
1291 So, we process arguments which will be passed by stack first. */
1292 gen_reg = nb_reg_args;
1293 sse_reg = nb_sse_args;
1294 args_size = 0;
1295 stack_adjust &= 15;
1296 for (i = k = 0; i < nb_args;) {
1297 mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
1298 if (size) {
1299 if (!onstack[i + k]) {
1300 ++i;
1301 continue;
1303 /* Possibly adjust stack to align SSE boundary. We're processing
1304 args from right to left while allocating happens left to right
1305 (stack grows down), so the adjustment needs to happen _after_
1306 an argument that requires it. */
1307 if (stack_adjust) {
1308 o(0x50); /* push %rax; aka sub $8,%rsp */
1309 args_size += 8;
1310 stack_adjust = 0;
1312 if (onstack[i + k] == 2)
1313 stack_adjust = 1;
1316 vrotb(i+1);
1318 switch (vtop->type.t & VT_BTYPE) {
1319 case VT_STRUCT:
1320 /* allocate the necessary size on stack */
1321 o(0x48);
1322 oad(0xec81, size); /* sub $xxx, %rsp */
1323 /* generate structure store */
1324 r = get_reg(RC_INT);
1325 orex(1, r, 0, 0x89); /* mov %rsp, r */
1326 o(0xe0 + REG_VALUE(r));
1327 vset(&vtop->type, r | VT_LVAL, 0);
1328 vswap();
1329 /* keep stack aligned for (__bound_)memmove call */
1330 o(0x10ec8348); /* sub $16,%rsp */
1331 o(0xf0e48348); /* and $-16,%rsp */
1332 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r (last %rsp) */
1333 o(0x08ec8348); /* sub $8,%rsp */
1334 vstore();
1335 o(0x08c48348); /* add $8,%rsp */
1336 o(0x5c); /* pop %rsp */
1337 break;
1339 case VT_LDOUBLE:
1340 gv(RC_ST0);
1341 oad(0xec8148, size); /* sub $xxx, %rsp */
1342 o(0x7cdb); /* fstpt 0(%rsp) */
1343 g(0x24);
1344 g(0x00);
1345 break;
1347 case VT_FLOAT:
1348 case VT_DOUBLE:
1349 assert(mode == x86_64_mode_sse);
1350 r = gv(RC_FLOAT);
1351 o(0x50); /* push $rax */
1352 /* movq %xmmN, (%rsp) */
1353 o(0xd60f66);
1354 o(0x04 + REG_VALUE(r)*8);
1355 o(0x24);
1356 break;
1358 default:
1359 assert(mode == x86_64_mode_integer);
1360 /* simple type */
1361 /* XXX: implicit cast ? */
1362 r = gv(RC_INT);
1363 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */
1364 break;
1366 args_size += size;
1368 vpop();
1369 --nb_args;
1370 k++;
1373 tcc_free(onstack);
1375 /* XXX This should be superfluous. */
1376 save_regs(0); /* save used temporary registers */
1378 /* then, we prepare register passing arguments.
1379 Note that we cannot set RDX and RCX in this loop because gv()
1380 may break these temporary registers. Let's use R10 and R11
1381 instead of them */
1382 assert(gen_reg <= REGN);
1383 assert(sse_reg <= 8);
1384 for(i = 0; i < nb_args; i++) {
1385 mode = classify_x86_64_arg(&vtop->type, &type, &size, &align, &reg_count);
1386 if (size == 0) continue;
1387 /* Alter stack entry type so that gv() knows how to treat it */
1388 vtop->type = type;
1389 if (mode == x86_64_mode_sse) {
1390 if (reg_count == 2) {
1391 sse_reg -= 2;
1392 gv(RC_FRET); /* Use pair load into xmm0 & xmm1 */
1393 if (sse_reg) { /* avoid redundant movaps %xmm0, %xmm0 */
1394 /* movaps %xmm1, %xmmN */
1395 o(0x280f);
1396 o(0xc1 + ((sse_reg+1) << 3));
1397 /* movaps %xmm0, %xmmN */
1398 o(0x280f);
1399 o(0xc0 + (sse_reg << 3));
1401 } else {
1402 assert(reg_count == 1);
1403 --sse_reg;
1404 /* Load directly to register */
1405 gv(RC_XMM0 << sse_reg);
1407 } else if (mode == x86_64_mode_integer) {
1408 /* simple type */
1409 /* XXX: implicit cast ? */
1410 int d;
1411 gen_reg -= reg_count;
1412 r = gv(RC_INT);
1413 d = arg_prepare_reg(gen_reg);
1414 orex(1,d,r,0x89); /* mov */
1415 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
1416 if (reg_count == 2) {
1417 d = arg_prepare_reg(gen_reg+1);
1418 orex(1,d,vtop->r2,0x89); /* mov */
1419 o(0xc0 + REG_VALUE(vtop->r2) * 8 + REG_VALUE(d));
1422 vtop--;
1424 assert(gen_reg == 0);
1425 assert(sse_reg == 0);
1427 /* We shouldn't have many operands on the stack anymore, but the
1428 call address itself is still there, and it might be in %eax
1429 (or edx/ecx) currently, which the below writes would clobber.
1430 So evict all remaining operands here. */
1431 save_regs(0);
1433 /* Copy R10 and R11 into RDX and RCX, respectively */
1434 if (nb_reg_args > 2) {
1435 o(0xd2894c); /* mov %r10, %rdx */
1436 if (nb_reg_args > 3) {
1437 o(0xd9894c); /* mov %r11, %rcx */
1441 if (vtop->type.ref->f.func_type != FUNC_NEW) /* implies FUNC_OLD or FUNC_ELLIPSIS */
1442 oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
1443 gcall_or_jmp(0);
1444 if (args_size)
1445 gadd_sp(args_size);
1446 vtop--;
1449 #define FUNC_PROLOG_SIZE 11
1451 static void push_arg_reg(int i) {
1452 loc -= 8;
1453 gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
1456 /* generate function prolog of type 't' */
1457 void gfunc_prolog(Sym *func_sym)
1459 CType *func_type = &func_sym->type;
1460 X86_64_Mode mode, ret_mode;
1461 int i, addr, align, size, reg_count;
1462 int param_addr = 0, reg_param_index, sse_param_index;
1463 Sym *sym;
1464 CType *type;
1466 sym = func_type->ref;
1467 addr = PTR_SIZE * 2;
1468 loc = 0;
1469 ind += FUNC_PROLOG_SIZE;
1470 func_sub_sp_offset = ind;
1471 func_ret_sub = 0;
1472 ret_mode = classify_x86_64_arg(&func_vt, NULL, &size, &align, &reg_count);
1474 if (func_var) {
1475 int seen_reg_num, seen_sse_num, seen_stack_size;
1476 seen_reg_num = ret_mode == x86_64_mode_memory;
1477 seen_sse_num = 0;
1478 /* frame pointer and return address */
1479 seen_stack_size = PTR_SIZE * 2;
1480 /* count the number of seen parameters */
1481 sym = func_type->ref;
1482 while ((sym = sym->next) != NULL) {
1483 type = &sym->type;
1484 mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
1485 switch (mode) {
1486 default:
1487 stack_arg:
1488 seen_stack_size = ((seen_stack_size + align - 1) & -align) + size;
1489 break;
1491 case x86_64_mode_integer:
1492 if (seen_reg_num + reg_count > REGN)
1493 goto stack_arg;
1494 seen_reg_num += reg_count;
1495 break;
1497 case x86_64_mode_sse:
1498 if (seen_sse_num + reg_count > 8)
1499 goto stack_arg;
1500 seen_sse_num += reg_count;
1501 break;
1505 loc -= 24;
1506 /* movl $0x????????, -0x18(%rbp) */
1507 o(0xe845c7);
1508 gen_le32(seen_reg_num * 8);
1509 /* movl $0x????????, -0x14(%rbp) */
1510 o(0xec45c7);
1511 gen_le32(seen_sse_num * 16 + 48);
1512 /* leaq $0x????????, %r11 */
1513 o(0x9d8d4c);
1514 gen_le32(seen_stack_size);
1515 /* movq %r11, -0x10(%rbp) */
1516 o(0xf05d894c);
1517 /* leaq $-192(%rbp), %r11 */
1518 o(0x9d8d4c);
1519 gen_le32(-176 - 24);
1520 /* movq %r11, -0x8(%rbp) */
1521 o(0xf85d894c);
1523 /* save all register passing arguments */
1524 for (i = 0; i < 8; i++) {
1525 loc -= 16;
1526 if (!tcc_state->nosse) {
1527 o(0xd60f66); /* movq */
1528 gen_modrm(7 - i, VT_LOCAL, NULL, loc);
1530 /* movq $0, loc+8(%rbp) */
1531 o(0x85c748);
1532 gen_le32(loc + 8);
1533 gen_le32(0);
1535 for (i = 0; i < REGN; i++) {
1536 push_arg_reg(REGN-1-i);
1540 sym = func_type->ref;
1541 reg_param_index = 0;
1542 sse_param_index = 0;
1544 /* if the function returns a structure, then add an
1545 implicit pointer parameter */
1546 if (ret_mode == x86_64_mode_memory) {
1547 push_arg_reg(reg_param_index);
1548 func_vc = loc;
1549 reg_param_index++;
1551 /* define parameters */
1552 while ((sym = sym->next) != NULL) {
1553 type = &sym->type;
1554 mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
1555 switch (mode) {
1556 case x86_64_mode_sse:
1557 if (tcc_state->nosse)
1558 tcc_error("SSE disabled but floating point arguments used");
1559 if (sse_param_index + reg_count <= 8) {
1560 /* save arguments passed by register */
1561 loc -= reg_count * 8;
1562 param_addr = loc;
1563 for (i = 0; i < reg_count; ++i) {
1564 o(0xd60f66); /* movq */
1565 gen_modrm(sse_param_index, VT_LOCAL, NULL, param_addr + i*8);
1566 ++sse_param_index;
1568 } else {
1569 addr = (addr + align - 1) & -align;
1570 param_addr = addr;
1571 addr += size;
1573 break;
1575 case x86_64_mode_memory:
1576 case x86_64_mode_x87:
1577 addr = (addr + align - 1) & -align;
1578 param_addr = addr;
1579 addr += size;
1580 break;
1582 case x86_64_mode_integer: {
1583 if (reg_param_index + reg_count <= REGN) {
1584 /* save arguments passed by register */
1585 loc -= reg_count * 8;
1586 param_addr = loc;
1587 for (i = 0; i < reg_count; ++i) {
1588 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, param_addr + i*8);
1589 ++reg_param_index;
1591 } else {
1592 addr = (addr + align - 1) & -align;
1593 param_addr = addr;
1594 addr += size;
1596 break;
1598 default: break; /* nothing to be done for x86_64_mode_none */
1600 sym_push(sym->v & ~SYM_FIELD, type,
1601 VT_LOCAL | VT_LVAL, param_addr);
1604 #ifdef CONFIG_TCC_BCHECK
1605 if (tcc_state->do_bounds_check)
1606 gen_bounds_prolog();
1607 #endif
1610 /* generate function epilog */
1611 void gfunc_epilog(void)
1613 int v, saved_ind;
1615 #ifdef CONFIG_TCC_BCHECK
1616 if (tcc_state->do_bounds_check)
1617 gen_bounds_epilog();
1618 #endif
1619 o(0xc9); /* leave */
1620 if (func_ret_sub == 0) {
1621 o(0xc3); /* ret */
1622 } else {
1623 o(0xc2); /* ret n */
1624 g(func_ret_sub);
1625 g(func_ret_sub >> 8);
1627 /* align local size to word & save local variables */
1628 v = (-loc + 15) & -16;
1629 saved_ind = ind;
1630 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1631 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1632 o(0xec8148); /* sub rsp, stacksize */
1633 gen_le32(v);
1634 ind = saved_ind;
1637 #endif /* not PE */
1639 ST_FUNC void gen_fill_nops(int bytes)
1641 while (bytes--)
1642 g(0x90);
1645 /* generate a jump to a label */
1646 int gjmp(int t)
1648 return gjmp2(0xe9, t);
1651 /* generate a jump to a fixed address */
1652 void gjmp_addr(int a)
1654 int r;
1655 r = a - ind - 2;
1656 if (r == (char)r) {
1657 g(0xeb);
1658 g(r);
1659 } else {
1660 oad(0xe9, a - ind - 5);
1664 ST_FUNC int gjmp_append(int n, int t)
1666 void *p;
1667 /* insert vtop->c jump list in t */
1668 if (n) {
1669 uint32_t n1 = n, n2;
1670 while ((n2 = read32le(p = cur_text_section->data + n1)))
1671 n1 = n2;
1672 write32le(p, t);
1673 t = n;
1675 return t;
1678 ST_FUNC int gjmp_cond(int op, int t)
1680 if (op & 0x100)
1682 /* This was a float compare. If the parity flag is set
1683 the result was unordered. For anything except != this
1684 means false and we don't jump (anding both conditions).
1685 For != this means true (oring both).
1686 Take care about inverting the test. We need to jump
1687 to our target if the result was unordered and test wasn't NE,
1688 otherwise if unordered we don't want to jump. */
1689 int v = vtop->cmp_r;
1690 op &= ~0x100;
1691 if (op ^ v ^ (v != TOK_NE))
1692 o(0x067a); /* jp +6 */
1693 else
1695 g(0x0f);
1696 t = gjmp2(0x8a, t); /* jp t */
1699 g(0x0f);
1700 t = gjmp2(op - 16, t);
1701 return t;
1704 /* generate an integer binary operation */
1705 void gen_opi(int op)
1707 int r, fr, opc, c;
1708 int ll, uu, cc;
1710 ll = is64_type(vtop[-1].type.t);
1711 uu = (vtop[-1].type.t & VT_UNSIGNED) != 0;
1712 cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1714 switch(op) {
1715 case '+':
1716 case TOK_ADDC1: /* add with carry generation */
1717 opc = 0;
1718 gen_op8:
1719 if (cc && (!ll || (int)vtop->c.i == vtop->c.i)) {
1720 /* constant case */
1721 vswap();
1722 r = gv(RC_INT);
1723 vswap();
1724 c = vtop->c.i;
1725 if (c == (char)c) {
1726 /* XXX: generate inc and dec for smaller code ? */
1727 orex(ll, r, 0, 0x83);
1728 o(0xc0 | (opc << 3) | REG_VALUE(r));
1729 g(c);
1730 } else {
1731 orex(ll, r, 0, 0x81);
1732 oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
1734 } else {
1735 gv2(RC_INT, RC_INT);
1736 r = vtop[-1].r;
1737 fr = vtop[0].r;
1738 orex(ll, r, fr, (opc << 3) | 0x01);
1739 o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
1741 vtop--;
1742 if (op >= TOK_ULT && op <= TOK_GT)
1743 vset_VT_CMP(op);
1744 break;
1745 case '-':
1746 case TOK_SUBC1: /* sub with carry generation */
1747 opc = 5;
1748 goto gen_op8;
1749 case TOK_ADDC2: /* add with carry use */
1750 opc = 2;
1751 goto gen_op8;
1752 case TOK_SUBC2: /* sub with carry use */
1753 opc = 3;
1754 goto gen_op8;
1755 case '&':
1756 opc = 4;
1757 goto gen_op8;
1758 case '^':
1759 opc = 6;
1760 goto gen_op8;
1761 case '|':
1762 opc = 1;
1763 goto gen_op8;
1764 case '*':
1765 gv2(RC_INT, RC_INT);
1766 r = vtop[-1].r;
1767 fr = vtop[0].r;
1768 orex(ll, fr, r, 0xaf0f); /* imul fr, r */
1769 o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8);
1770 vtop--;
1771 break;
1772 case TOK_SHL:
1773 opc = 4;
1774 goto gen_shift;
1775 case TOK_SHR:
1776 opc = 5;
1777 goto gen_shift;
1778 case TOK_SAR:
1779 opc = 7;
1780 gen_shift:
1781 opc = 0xc0 | (opc << 3);
1782 if (cc) {
1783 /* constant case */
1784 vswap();
1785 r = gv(RC_INT);
1786 vswap();
1787 orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */
1788 o(opc | REG_VALUE(r));
1789 g(vtop->c.i & (ll ? 63 : 31));
1790 } else {
1791 /* we generate the shift in ecx */
1792 gv2(RC_INT, RC_RCX);
1793 r = vtop[-1].r;
1794 orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */
1795 o(opc | REG_VALUE(r));
1797 vtop--;
1798 break;
1799 case TOK_UDIV:
1800 case TOK_UMOD:
1801 uu = 1;
1802 goto divmod;
1803 case '/':
1804 case '%':
1805 case TOK_PDIV:
1806 uu = 0;
1807 divmod:
1808 /* first operand must be in eax */
1809 /* XXX: need better constraint for second operand */
1810 gv2(RC_RAX, RC_RCX);
1811 r = vtop[-1].r;
1812 fr = vtop[0].r;
1813 vtop--;
1814 save_reg(TREG_RDX);
1815 orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */
1816 orex(ll, fr, 0, 0xf7); /* div fr, %eax */
1817 o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr));
1818 if (op == '%' || op == TOK_UMOD)
1819 r = TREG_RDX;
1820 else
1821 r = TREG_RAX;
1822 vtop->r = r;
1823 break;
1824 default:
1825 opc = 7;
1826 goto gen_op8;
1830 void gen_opl(int op)
1832 gen_opi(op);
1835 void vpush_const(int t, int v)
1837 CType ctype = { t | VT_CONSTANT, 0 };
1838 vpushsym(&ctype, external_global_sym(v, &ctype));
1839 vtop->r |= VT_LVAL;
1842 /* generate a floating point operation 'v = t1 op t2' instruction. The
1843 two operands are guaranteed to have the same floating point type */
1844 /* XXX: need to use ST1 too */
1845 void gen_opf(int op)
1847 int a, ft, fc, swapped, r;
1848 int bt = vtop->type.t & VT_BTYPE;
1849 int float_type = bt == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
1851 if (op == TOK_NEG) { /* unary minus */
1852 gv(float_type);
1853 if (float_type == RC_ST0) {
1854 o(0xe0d9); /* fchs */
1855 } else {
1856 /* -0.0, in libtcc1.c */
1857 vpush_const(bt, bt == VT_FLOAT ? TOK___mzerosf : TOK___mzerodf);
1858 gv(RC_FLOAT);
1859 if (bt == VT_DOUBLE)
1860 o(0x66);
1861 /* xorp[sd] %xmm1, %xmm0 */
1862 o(0xc0570f | (REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8) << 16);
1863 vtop--;
1865 return;
1868 /* convert constants to memory references */
1869 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1870 vswap();
1871 gv(float_type);
1872 vswap();
1874 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
1875 gv(float_type);
1877 /* must put at least one value in the floating point register */
1878 if ((vtop[-1].r & VT_LVAL) &&
1879 (vtop[0].r & VT_LVAL)) {
1880 vswap();
1881 gv(float_type);
1882 vswap();
1884 swapped = 0;
1885 /* swap the stack if needed so that t1 is the register and t2 is
1886 the memory reference */
1887 if (vtop[-1].r & VT_LVAL) {
1888 vswap();
1889 swapped = 1;
1891 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1892 if (op >= TOK_ULT && op <= TOK_GT) {
1893 /* load on stack second operand */
1894 load(TREG_ST0, vtop);
1895 save_reg(TREG_RAX); /* eax is used by FP comparison code */
1896 if (op == TOK_GE || op == TOK_GT)
1897 swapped = !swapped;
1898 else if (op == TOK_EQ || op == TOK_NE)
1899 swapped = 0;
1900 if (swapped)
1901 o(0xc9d9); /* fxch %st(1) */
1902 if (op == TOK_EQ || op == TOK_NE)
1903 o(0xe9da); /* fucompp */
1904 else
1905 o(0xd9de); /* fcompp */
1906 o(0xe0df); /* fnstsw %ax */
1907 if (op == TOK_EQ) {
1908 o(0x45e480); /* and $0x45, %ah */
1909 o(0x40fC80); /* cmp $0x40, %ah */
1910 } else if (op == TOK_NE) {
1911 o(0x45e480); /* and $0x45, %ah */
1912 o(0x40f480); /* xor $0x40, %ah */
1913 op = TOK_NE;
1914 } else if (op == TOK_GE || op == TOK_LE) {
1915 o(0x05c4f6); /* test $0x05, %ah */
1916 op = TOK_EQ;
1917 } else {
1918 o(0x45c4f6); /* test $0x45, %ah */
1919 op = TOK_EQ;
1921 vtop--;
1922 vset_VT_CMP(op);
1923 } else {
1924 /* no memory reference possible for long double operations */
1925 load(TREG_ST0, vtop);
1926 swapped = !swapped;
1928 switch(op) {
1929 default:
1930 case '+':
1931 a = 0;
1932 break;
1933 case '-':
1934 a = 4;
1935 if (swapped)
1936 a++;
1937 break;
1938 case '*':
1939 a = 1;
1940 break;
1941 case '/':
1942 a = 6;
1943 if (swapped)
1944 a++;
1945 break;
1947 ft = vtop->type.t;
1948 fc = vtop->c.i;
1949 o(0xde); /* fxxxp %st, %st(1) */
1950 o(0xc1 + (a << 3));
1951 vtop--;
1953 } else {
1954 if (op >= TOK_ULT && op <= TOK_GT) {
1955 /* if saved lvalue, then we must reload it */
1956 r = vtop->r;
1957 fc = vtop->c.i;
1958 if ((r & VT_VALMASK) == VT_LLOCAL) {
1959 SValue v1;
1960 r = get_reg(RC_INT);
1961 v1.type.t = VT_PTR;
1962 v1.r = VT_LOCAL | VT_LVAL;
1963 v1.c.i = fc;
1964 load(r, &v1);
1965 fc = 0;
1966 vtop->r = r = r | VT_LVAL;
1969 if (op == TOK_EQ || op == TOK_NE) {
1970 swapped = 0;
1971 } else {
1972 if (op == TOK_LE || op == TOK_LT)
1973 swapped = !swapped;
1974 if (op == TOK_LE || op == TOK_GE) {
1975 op = 0x93; /* setae */
1976 } else {
1977 op = 0x97; /* seta */
1981 if (swapped) {
1982 gv(RC_FLOAT);
1983 vswap();
1985 assert(!(vtop[-1].r & VT_LVAL));
1987 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1988 o(0x66);
1989 if (op == TOK_EQ || op == TOK_NE)
1990 o(0x2e0f); /* ucomisd */
1991 else
1992 o(0x2f0f); /* comisd */
1994 if (vtop->r & VT_LVAL) {
1995 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
1996 } else {
1997 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
2000 vtop--;
2001 vset_VT_CMP(op | 0x100);
2002 vtop->cmp_r = op;
2003 } else {
2004 assert((vtop->type.t & VT_BTYPE) != VT_LDOUBLE);
2005 switch(op) {
2006 default:
2007 case '+':
2008 a = 0;
2009 break;
2010 case '-':
2011 a = 4;
2012 break;
2013 case '*':
2014 a = 1;
2015 break;
2016 case '/':
2017 a = 6;
2018 break;
2020 ft = vtop->type.t;
2021 fc = vtop->c.i;
2022 assert((ft & VT_BTYPE) != VT_LDOUBLE);
2024 r = vtop->r;
2025 /* if saved lvalue, then we must reload it */
2026 if ((vtop->r & VT_VALMASK) == VT_LLOCAL) {
2027 SValue v1;
2028 r = get_reg(RC_INT);
2029 v1.type.t = VT_PTR;
2030 v1.r = VT_LOCAL | VT_LVAL;
2031 v1.c.i = fc;
2032 load(r, &v1);
2033 fc = 0;
2034 vtop->r = r = r | VT_LVAL;
2037 assert(!(vtop[-1].r & VT_LVAL));
2038 if (swapped) {
2039 assert(vtop->r & VT_LVAL);
2040 gv(RC_FLOAT);
2041 vswap();
2042 fc = vtop->c.i; /* bcheck may have saved previous vtop[-1] */
2045 if ((ft & VT_BTYPE) == VT_DOUBLE) {
2046 o(0xf2);
2047 } else {
2048 o(0xf3);
2050 o(0x0f);
2051 o(0x58 + a);
2053 if (vtop->r & VT_LVAL) {
2054 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
2055 } else {
2056 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
2059 vtop--;
2064 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
2065 and 'long long' cases. */
2066 void gen_cvt_itof(int t)
2068 if ((t & VT_BTYPE) == VT_LDOUBLE) {
2069 save_reg(TREG_ST0);
2070 gv(RC_INT);
2071 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
2072 /* signed long long to float/double/long double (unsigned case
2073 is handled generically) */
2074 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2075 o(0x242cdf); /* fildll (%rsp) */
2076 o(0x08c48348); /* add $8, %rsp */
2077 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2078 (VT_INT | VT_UNSIGNED)) {
2079 /* unsigned int to float/double/long double */
2080 o(0x6a); /* push $0 */
2081 g(0x00);
2082 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2083 o(0x242cdf); /* fildll (%rsp) */
2084 o(0x10c48348); /* add $16, %rsp */
2085 } else {
2086 /* int to float/double/long double */
2087 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2088 o(0x2404db); /* fildl (%rsp) */
2089 o(0x08c48348); /* add $8, %rsp */
2091 vtop->r = TREG_ST0;
2092 } else {
2093 int r = get_reg(RC_FLOAT);
2094 gv(RC_INT);
2095 o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT?1:0));
2096 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2097 (VT_INT | VT_UNSIGNED) ||
2098 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
2099 o(0x48); /* REX */
2101 o(0x2a0f);
2102 o(0xc0 + (vtop->r & VT_VALMASK) + REG_VALUE(r)*8); /* cvtsi2sd */
2103 vtop->r = r;
2107 /* convert from one floating point type to another */
2108 void gen_cvt_ftof(int t)
2110 int ft, bt, tbt;
2112 ft = vtop->type.t;
2113 bt = ft & VT_BTYPE;
2114 tbt = t & VT_BTYPE;
2116 if (bt == VT_FLOAT) {
2117 gv(RC_FLOAT);
2118 if (tbt == VT_DOUBLE) {
2119 o(0x140f); /* unpcklps */
2120 o(0xc0 + REG_VALUE(vtop->r)*9);
2121 o(0x5a0f); /* cvtps2pd */
2122 o(0xc0 + REG_VALUE(vtop->r)*9);
2123 } else if (tbt == VT_LDOUBLE) {
2124 save_reg(RC_ST0);
2125 /* movss %xmm0,-0x10(%rsp) */
2126 o(0x110ff3);
2127 o(0x44 + REG_VALUE(vtop->r)*8);
2128 o(0xf024);
2129 o(0xf02444d9); /* flds -0x10(%rsp) */
2130 vtop->r = TREG_ST0;
2132 } else if (bt == VT_DOUBLE) {
2133 gv(RC_FLOAT);
2134 if (tbt == VT_FLOAT) {
2135 o(0x140f66); /* unpcklpd */
2136 o(0xc0 + REG_VALUE(vtop->r)*9);
2137 o(0x5a0f66); /* cvtpd2ps */
2138 o(0xc0 + REG_VALUE(vtop->r)*9);
2139 } else if (tbt == VT_LDOUBLE) {
2140 save_reg(RC_ST0);
2141 /* movsd %xmm0,-0x10(%rsp) */
2142 o(0x110ff2);
2143 o(0x44 + REG_VALUE(vtop->r)*8);
2144 o(0xf024);
2145 o(0xf02444dd); /* fldl -0x10(%rsp) */
2146 vtop->r = TREG_ST0;
2148 } else {
2149 int r;
2150 gv(RC_ST0);
2151 r = get_reg(RC_FLOAT);
2152 if (tbt == VT_DOUBLE) {
2153 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
2154 /* movsd -0x10(%rsp),%xmm0 */
2155 o(0x100ff2);
2156 o(0x44 + REG_VALUE(r)*8);
2157 o(0xf024);
2158 vtop->r = r;
2159 } else if (tbt == VT_FLOAT) {
2160 o(0xf0245cd9); /* fstps -0x10(%rsp) */
2161 /* movss -0x10(%rsp),%xmm0 */
2162 o(0x100ff3);
2163 o(0x44 + REG_VALUE(r)*8);
2164 o(0xf024);
2165 vtop->r = r;
2170 /* convert fp to int 't' type */
2171 void gen_cvt_ftoi(int t)
2173 int ft, bt, size, r;
2174 ft = vtop->type.t;
2175 bt = ft & VT_BTYPE;
2176 if (bt == VT_LDOUBLE) {
2177 gen_cvt_ftof(VT_DOUBLE);
2178 bt = VT_DOUBLE;
2181 gv(RC_FLOAT);
2182 if (t != VT_INT)
2183 size = 8;
2184 else
2185 size = 4;
2187 r = get_reg(RC_INT);
2188 if (bt == VT_FLOAT) {
2189 o(0xf3);
2190 } else if (bt == VT_DOUBLE) {
2191 o(0xf2);
2192 } else {
2193 assert(0);
2195 orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */
2196 o(0xc0 + REG_VALUE(vtop->r) + REG_VALUE(r)*8);
2197 vtop->r = r;
2200 // Generate sign extension from 32 to 64 bits:
2201 ST_FUNC void gen_cvt_sxtw(void)
2203 int r = gv(RC_INT);
2204 /* x86_64 specific: movslq */
2205 o(0x6348);
2206 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2209 /* char/short to int conversion */
2210 ST_FUNC void gen_cvt_csti(int t)
2212 int r, sz, xl, ll;
2213 r = gv(RC_INT);
2214 sz = !(t & VT_UNSIGNED);
2215 xl = (t & VT_BTYPE) == VT_SHORT;
2216 ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
2217 orex(ll, r, 0, 0xc0b60f /* mov[sz] %a[xl], %eax */
2218 | (sz << 3 | xl) << 8
2219 | (REG_VALUE(r) << 3 | REG_VALUE(r)) << 16
2223 /* increment tcov counter */
2224 ST_FUNC void gen_increment_tcov (SValue *sv)
2226 o(0x058348); /* addq $1, xxx(%rip) */
2227 greloca(cur_text_section, sv->sym, ind, R_X86_64_PC32, -5);
2228 gen_le32(0);
2229 o(1);
2232 /* computed goto support */
2233 ST_FUNC void ggoto(void)
2235 gcall_or_jmp(1);
2236 vtop--;
2239 /* Save the stack pointer onto the stack and return the location of its address */
2240 ST_FUNC void gen_vla_sp_save(int addr) {
2241 /* mov %rsp,addr(%rbp)*/
2242 gen_modrm64(0x89, TREG_RSP, VT_LOCAL, NULL, addr);
2245 /* Restore the SP from a location on the stack */
2246 ST_FUNC void gen_vla_sp_restore(int addr) {
2247 gen_modrm64(0x8b, TREG_RSP, VT_LOCAL, NULL, addr);
2250 #ifdef TCC_TARGET_PE
2251 /* Save result of gen_vla_alloc onto the stack */
2252 ST_FUNC void gen_vla_result(int addr) {
2253 /* mov %rax,addr(%rbp)*/
2254 gen_modrm64(0x89, TREG_RAX, VT_LOCAL, NULL, addr);
2256 #endif
2258 /* Subtract from the stack pointer, and push the resulting value onto the stack */
2259 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2260 int use_call = 0;
2262 #if defined(CONFIG_TCC_BCHECK)
2263 use_call = tcc_state->do_bounds_check;
2264 #endif
2265 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
2266 use_call = 1;
2267 #endif
2268 if (use_call)
2270 vpush_helper_func(TOK_alloca);
2271 vswap(); /* Move alloca ref past allocation size */
2272 gfunc_call(1);
2274 else {
2275 int r;
2276 r = gv(RC_INT); /* allocation size */
2277 /* sub r,%rsp */
2278 o(0x2b48);
2279 o(0xe0 | REG_VALUE(r));
2280 /* We align to 16 bytes rather than align */
2281 /* and ~15, %rsp */
2282 o(0xf0e48348);
2283 vpop();
2288 * Assmuing the top part of the stack looks like below,
2289 * src dest src
2291 ST_FUNC void gen_struct_copy(int size)
2293 int n = size / PTR_SIZE;
2294 #ifdef TCC_TARGET_PE
2295 o(0x5756); /* push rsi, rdi */
2296 #endif
2297 gv2(RC_RDI, RC_RSI);
2298 if (n <= 4) {
2299 while (n)
2300 o(0xa548), --n;
2301 } else {
2302 vpushi(n);
2303 gv(RC_RCX);
2304 o(0xa548f3);
2305 vpop();
2307 if (size & 0x04)
2308 o(0xa5);
2309 if (size & 0x02)
2310 o(0xa566);
2311 if (size & 0x01)
2312 o(0xa4);
2313 #ifdef TCC_TARGET_PE
2314 o(0x5e5f); /* pop rdi, rsi */
2315 #endif
2316 vpop();
2317 vpop();
2320 /* end of x86-64 code generator */
2321 /*************************************************************/
2322 #endif /* ! TARGET_DEFS_ONLY */
2323 /******************************************************/