Fix null pointer constants
[tinycc.git] / x86_64-gen.c
blob26435fa95547bccc4db30166a12dcef7658822d5
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_RCX 0x0008
37 #define RC_RDX 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 #ifdef TCC_TARGET_PE
650 greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PC32, (int)(vtop->c.i-4));
651 #else
652 greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));
653 #endif
654 oad(0xe8 + is_jmp, 0); /* call/jmp im */
655 } else {
656 /* otherwise, indirect call */
657 r = TREG_R11;
658 load(r, vtop);
659 o(0x41); /* REX */
660 o(0xff); /* call/jmp *r */
661 o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
665 #if defined(CONFIG_TCC_BCHECK)
667 static void gen_bounds_call(int v)
669 Sym *sym = external_helper_sym(v);
670 oad(0xe8, 0);
671 #ifdef TCC_TARGET_PE
672 greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
673 #else
674 greloca(cur_text_section, sym, ind-4, R_X86_64_PLT32, -4);
675 #endif
678 #ifdef TCC_TARGET_PE
679 # define TREG_FASTCALL_1 TREG_RCX
680 #else
681 # define TREG_FASTCALL_1 TREG_RDI
682 #endif
684 static void gen_bounds_prolog(void)
686 /* leave some room for bound checking code */
687 func_bound_offset = lbounds_section->data_offset;
688 func_bound_ind = ind;
689 func_bound_add_epilog = 0;
690 o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /*lbound section pointer */
691 gen_le32 (0);
692 oad(0xb8, 0); /* call to function */
695 static void gen_bounds_epilog(void)
697 addr_t saved_ind;
698 addr_t *bounds_ptr;
699 Sym *sym_data;
700 int offset_modified = func_bound_offset != lbounds_section->data_offset;
702 if (!offset_modified && !func_bound_add_epilog)
703 return;
705 /* add end of table info */
706 bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
707 *bounds_ptr = 0;
709 sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
710 func_bound_offset, PTR_SIZE);
712 /* generate bound local allocation */
713 if (offset_modified) {
714 saved_ind = ind;
715 ind = func_bound_ind;
716 greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
717 ind = ind + 7;
718 gen_bounds_call(TOK___bound_local_new);
719 ind = saved_ind;
722 /* generate bound check local freeing */
723 o(0x5250); /* save returned value, if any */
724 o(0x20ec8348); /* sub $32,%rsp */
725 o(0x290f); /* movaps %xmm0,0x10(%rsp) */
726 o(0x102444);
727 o(0x240c290f); /* movaps %xmm1,(%rsp) */
728 greloca(cur_text_section, sym_data, ind + 3, R_X86_64_PC32, -4);
729 o(0x0d8d48 + ((TREG_FASTCALL_1 == TREG_RDI) * 0x300000)); /* lea xxx(%rip), %rcx/rdi */
730 gen_le32 (0);
731 gen_bounds_call(TOK___bound_local_delete);
732 o(0x280f); /* movaps 0x10(%rsp),%xmm0 */
733 o(0x102444);
734 o(0x240c280f); /* movaps (%rsp),%xmm1 */
735 o(0x20c48348); /* add $32,%rsp */
736 o(0x585a); /* restore returned value, if any */
738 #endif
740 #ifdef TCC_TARGET_PE
742 #define REGN 4
743 static const uint8_t arg_regs[REGN] = {
744 TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
747 /* Prepare arguments in R10 and R11 rather than RCX and RDX
748 because gv() will not ever use these */
749 static int arg_prepare_reg(int idx) {
750 if (idx == 0 || idx == 1)
751 /* idx=0: r10, idx=1: r11 */
752 return idx + 10;
753 else
754 return idx >= 0 && idx < REGN ? arg_regs[idx] : 0;
757 /* Generate function call. The function address is pushed first, then
758 all the parameters in call order. This functions pops all the
759 parameters and the function address. */
761 static void gen_offs_sp(int b, int r, int d)
763 orex(1,0,r & 0x100 ? 0 : r, b);
764 if (d == (char)d) {
765 o(0x2444 | (REG_VALUE(r) << 3));
766 g(d);
767 } else {
768 o(0x2484 | (REG_VALUE(r) << 3));
769 gen_le32(d);
773 static int using_regs(int size)
775 return !(size > 8 || (size & (size - 1)));
778 /* Return the number of registers needed to return the struct, or 0 if
779 returning via struct pointer. */
780 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
782 int size, align;
783 *ret_align = 1; // Never have to re-align return values for x86-64
784 *regsize = 8;
785 size = type_size(vt, &align);
786 if (!using_regs(size))
787 return 0;
788 if (size == 8)
789 ret->t = VT_LLONG;
790 else if (size == 4)
791 ret->t = VT_INT;
792 else if (size == 2)
793 ret->t = VT_SHORT;
794 else
795 ret->t = VT_BYTE;
796 ret->ref = NULL;
797 return 1;
800 static int is_sse_float(int t) {
801 int bt;
802 bt = t & VT_BTYPE;
803 return bt == VT_DOUBLE || bt == VT_FLOAT;
806 static int gfunc_arg_size(CType *type) {
807 int align;
808 if (type->t & (VT_ARRAY|VT_BITFIELD))
809 return 8;
810 return type_size(type, &align);
813 void gfunc_call(int nb_args)
815 int size, r, args_size, i, d, bt, struct_size;
816 int arg;
818 #ifdef CONFIG_TCC_BCHECK
819 if (tcc_state->do_bounds_check)
820 gbound_args(nb_args);
821 #endif
823 args_size = (nb_args < REGN ? REGN : nb_args) * PTR_SIZE;
824 arg = nb_args;
826 /* for struct arguments, we need to call memcpy and the function
827 call breaks register passing arguments we are preparing.
828 So, we process arguments which will be passed by stack first. */
829 struct_size = args_size;
830 for(i = 0; i < nb_args; i++) {
831 SValue *sv;
833 --arg;
834 sv = &vtop[-i];
835 bt = (sv->type.t & VT_BTYPE);
836 size = gfunc_arg_size(&sv->type);
838 if (using_regs(size))
839 continue; /* arguments smaller than 8 bytes passed in registers or on stack */
841 if (bt == VT_STRUCT) {
842 /* align to stack align size */
843 size = (size + 15) & ~15;
844 /* generate structure store */
845 r = get_reg(RC_INT);
846 gen_offs_sp(0x8d, r, struct_size);
847 struct_size += size;
849 /* generate memcpy call */
850 vset(&sv->type, r | VT_LVAL, 0);
851 vpushv(sv);
852 vstore();
853 --vtop;
854 } else if (bt == VT_LDOUBLE) {
855 gv(RC_ST0);
856 gen_offs_sp(0xdb, 0x107, struct_size);
857 struct_size += 16;
861 if (func_scratch < struct_size)
862 func_scratch = struct_size;
864 arg = nb_args;
865 struct_size = args_size;
867 for(i = 0; i < nb_args; i++) {
868 --arg;
869 bt = (vtop->type.t & VT_BTYPE);
871 size = gfunc_arg_size(&vtop->type);
872 if (!using_regs(size)) {
873 /* align to stack align size */
874 size = (size + 15) & ~15;
875 if (arg >= REGN) {
876 d = get_reg(RC_INT);
877 gen_offs_sp(0x8d, d, struct_size);
878 gen_offs_sp(0x89, d, arg*8);
879 } else {
880 d = arg_prepare_reg(arg);
881 gen_offs_sp(0x8d, d, struct_size);
883 struct_size += size;
884 } else {
885 if (is_sse_float(vtop->type.t)) {
886 if (tcc_state->nosse)
887 tcc_error("SSE disabled");
888 if (arg >= REGN) {
889 gv(RC_XMM0);
890 /* movq %xmm0, j*8(%rsp) */
891 gen_offs_sp(0xd60f66, 0x100, arg*8);
892 } else {
893 /* Load directly to xmmN register */
894 gv(RC_XMM0 << arg);
895 d = arg_prepare_reg(arg);
896 /* mov %xmmN, %rxx */
897 o(0x66);
898 orex(1,d,0, 0x7e0f);
899 o(0xc0 + arg*8 + REG_VALUE(d));
901 } else {
902 if (bt == VT_STRUCT) {
903 vtop->type.ref = NULL;
904 vtop->type.t = size > 4 ? VT_LLONG : size > 2 ? VT_INT
905 : size > 1 ? VT_SHORT : VT_BYTE;
908 r = gv(RC_INT);
909 if (arg >= REGN) {
910 gen_offs_sp(0x89, r, arg*8);
911 } else {
912 d = arg_prepare_reg(arg);
913 orex(1,d,r,0x89); /* mov */
914 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
918 vtop--;
920 save_regs(0);
921 /* Copy R10 and R11 into RCX and RDX, respectively */
922 if (nb_args > 0) {
923 o(0xd1894c); /* mov %r10, %rcx */
924 if (nb_args > 1) {
925 o(0xda894c); /* mov %r11, %rdx */
929 gcall_or_jmp(0);
931 if ((vtop->r & VT_SYM) && vtop->sym->v == TOK_alloca) {
932 /* need to add the "func_scratch" area after alloca */
933 o(0x48); func_alloca = oad(0x05, func_alloca); /* add $NN, %rax */
934 #ifdef CONFIG_TCC_BCHECK
935 if (tcc_state->do_bounds_check)
936 gen_bounds_call(TOK___bound_alloca_nr); /* new region */
937 #endif
939 vtop--;
943 #define FUNC_PROLOG_SIZE 11
945 /* generate function prolog of type 't' */
946 void gfunc_prolog(Sym *func_sym)
948 CType *func_type = &func_sym->type;
949 int addr, reg_param_index, bt, size;
950 Sym *sym;
951 CType *type;
953 func_ret_sub = 0;
954 func_scratch = 32;
955 func_alloca = 0;
956 loc = 0;
958 addr = PTR_SIZE * 2;
959 ind += FUNC_PROLOG_SIZE;
960 func_sub_sp_offset = ind;
961 reg_param_index = 0;
963 sym = func_type->ref;
965 /* if the function returns a structure, then add an
966 implicit pointer parameter */
967 size = gfunc_arg_size(&func_vt);
968 if (!using_regs(size)) {
969 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
970 func_vc = addr;
971 reg_param_index++;
972 addr += 8;
975 /* define parameters */
976 while ((sym = sym->next) != NULL) {
977 type = &sym->type;
978 bt = type->t & VT_BTYPE;
979 size = gfunc_arg_size(type);
980 if (!using_regs(size)) {
981 if (reg_param_index < REGN) {
982 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
984 sym_push(sym->v & ~SYM_FIELD, type,
985 VT_LLOCAL | VT_LVAL, addr);
986 } else {
987 if (reg_param_index < REGN) {
988 /* save arguments passed by register */
989 if ((bt == VT_FLOAT) || (bt == VT_DOUBLE)) {
990 if (tcc_state->nosse)
991 tcc_error("SSE disabled");
992 o(0xd60f66); /* movq */
993 gen_modrm(reg_param_index, VT_LOCAL, NULL, addr);
994 } else {
995 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
998 sym_push(sym->v & ~SYM_FIELD, type,
999 VT_LOCAL | VT_LVAL, addr);
1001 addr += 8;
1002 reg_param_index++;
1005 while (reg_param_index < REGN) {
1006 if (func_var) {
1007 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
1008 addr += 8;
1010 reg_param_index++;
1012 #ifdef CONFIG_TCC_BCHECK
1013 if (tcc_state->do_bounds_check)
1014 gen_bounds_prolog();
1015 #endif
1018 /* generate function epilog */
1019 void gfunc_epilog(void)
1021 int v, saved_ind;
1023 /* align local size to word & save local variables */
1024 func_scratch = (func_scratch + 15) & -16;
1025 loc = (loc & -16) - func_scratch;
1027 #ifdef CONFIG_TCC_BCHECK
1028 if (tcc_state->do_bounds_check)
1029 gen_bounds_epilog();
1030 #endif
1032 o(0xc9); /* leave */
1033 if (func_ret_sub == 0) {
1034 o(0xc3); /* ret */
1035 } else {
1036 o(0xc2); /* ret n */
1037 g(func_ret_sub);
1038 g(func_ret_sub >> 8);
1041 saved_ind = ind;
1042 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1043 v = -loc;
1045 if (v >= 4096) {
1046 Sym *sym = external_helper_sym(TOK___chkstk);
1047 oad(0xb8, v); /* mov stacksize, %eax */
1048 oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */
1049 greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
1050 o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
1051 } else {
1052 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1053 o(0xec8148); /* sub rsp, stacksize */
1054 gen_le32(v);
1057 /* add the "func_scratch" area after each alloca seen */
1058 gsym_addr(func_alloca, -func_scratch);
1060 cur_text_section->data_offset = saved_ind;
1061 pe_add_unwind_data(ind, saved_ind, v);
1062 ind = cur_text_section->data_offset;
1065 #else
1067 static void gadd_sp(int val)
1069 if (val == (char)val) {
1070 o(0xc48348);
1071 g(val);
1072 } else {
1073 oad(0xc48148, val); /* add $xxx, %rsp */
1077 typedef enum X86_64_Mode {
1078 x86_64_mode_none,
1079 x86_64_mode_memory,
1080 x86_64_mode_integer,
1081 x86_64_mode_sse,
1082 x86_64_mode_x87
1083 } X86_64_Mode;
1085 static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b)
1087 if (a == b)
1088 return a;
1089 else if (a == x86_64_mode_none)
1090 return b;
1091 else if (b == x86_64_mode_none)
1092 return a;
1093 else if ((a == x86_64_mode_memory) || (b == x86_64_mode_memory))
1094 return x86_64_mode_memory;
1095 else if ((a == x86_64_mode_integer) || (b == x86_64_mode_integer))
1096 return x86_64_mode_integer;
1097 else if ((a == x86_64_mode_x87) || (b == x86_64_mode_x87))
1098 return x86_64_mode_memory;
1099 else
1100 return x86_64_mode_sse;
1103 static X86_64_Mode classify_x86_64_inner(CType *ty)
1105 X86_64_Mode mode;
1106 Sym *f;
1108 switch (ty->t & VT_BTYPE) {
1109 case VT_VOID: return x86_64_mode_none;
1111 case VT_INT:
1112 case VT_BYTE:
1113 case VT_SHORT:
1114 case VT_LLONG:
1115 case VT_BOOL:
1116 case VT_PTR:
1117 case VT_FUNC:
1118 return x86_64_mode_integer;
1120 case VT_FLOAT:
1121 case VT_DOUBLE: return x86_64_mode_sse;
1123 case VT_LDOUBLE: return x86_64_mode_x87;
1125 case VT_STRUCT:
1126 f = ty->ref;
1128 mode = x86_64_mode_none;
1129 for (f = f->next; f; f = f->next)
1130 mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type));
1132 return mode;
1134 assert(0);
1135 return 0;
1138 static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count)
1140 X86_64_Mode mode;
1141 int size, align, ret_t = 0;
1143 if (ty->t & (VT_BITFIELD|VT_ARRAY)) {
1144 *psize = 8;
1145 *palign = 8;
1146 *reg_count = 1;
1147 ret_t = ty->t;
1148 mode = x86_64_mode_integer;
1149 } else {
1150 size = type_size(ty, &align);
1151 *psize = (size + 7) & ~7;
1152 *palign = (align + 7) & ~7;
1154 if (size > 16) {
1155 mode = x86_64_mode_memory;
1156 } else {
1157 mode = classify_x86_64_inner(ty);
1158 switch (mode) {
1159 case x86_64_mode_integer:
1160 if (size > 8) {
1161 *reg_count = 2;
1162 ret_t = VT_QLONG;
1163 } else {
1164 *reg_count = 1;
1165 if (size > 4)
1166 ret_t = VT_LLONG;
1167 else if (size > 2)
1168 ret_t = VT_INT;
1169 else if (size > 1)
1170 ret_t = VT_SHORT;
1171 else
1172 ret_t = VT_BYTE;
1173 if ((ty->t & VT_BTYPE) == VT_STRUCT || (ty->t & VT_UNSIGNED))
1174 ret_t |= VT_UNSIGNED;
1176 break;
1178 case x86_64_mode_x87:
1179 *reg_count = 1;
1180 ret_t = VT_LDOUBLE;
1181 break;
1183 case x86_64_mode_sse:
1184 if (size > 8) {
1185 *reg_count = 2;
1186 ret_t = VT_QFLOAT;
1187 } else {
1188 *reg_count = 1;
1189 ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT;
1191 break;
1192 default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/
1197 if (ret) {
1198 ret->ref = NULL;
1199 ret->t = ret_t;
1202 return mode;
1205 ST_FUNC int classify_x86_64_va_arg(CType *ty)
1207 /* This definition must be synced with stdarg.h */
1208 enum __va_arg_type {
1209 __va_gen_reg, __va_float_reg, __va_stack
1211 int size, align, reg_count;
1212 X86_64_Mode mode = classify_x86_64_arg(ty, NULL, &size, &align, &reg_count);
1213 switch (mode) {
1214 default: return __va_stack;
1215 case x86_64_mode_integer: return __va_gen_reg;
1216 case x86_64_mode_sse: return __va_float_reg;
1220 /* Return the number of registers needed to return the struct, or 0 if
1221 returning via struct pointer. */
1222 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
1224 int size, align, reg_count;
1225 *ret_align = 1; // Never have to re-align return values for x86-64
1226 *regsize = 8;
1227 return (classify_x86_64_arg(vt, ret, &size, &align, &reg_count) != x86_64_mode_memory);
1230 #define REGN 6
1231 static const uint8_t arg_regs[REGN] = {
1232 TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
1235 static int arg_prepare_reg(int idx) {
1236 if (idx == 2 || idx == 3)
1237 /* idx=2: r10, idx=3: r11 */
1238 return idx + 8;
1239 else
1240 return idx >= 0 && idx < REGN ? arg_regs[idx] : 0;
1243 /* Generate function call. The function address is pushed first, then
1244 all the parameters in call order. This functions pops all the
1245 parameters and the function address. */
1246 void gfunc_call(int nb_args)
1248 X86_64_Mode mode;
1249 CType type;
1250 int size, align, r, args_size, stack_adjust, i, reg_count, k;
1251 int nb_reg_args = 0;
1252 int nb_sse_args = 0;
1253 int sse_reg, gen_reg;
1254 char *onstack = tcc_malloc((nb_args + 1) * sizeof (char));
1256 #ifdef CONFIG_TCC_BCHECK
1257 if (tcc_state->do_bounds_check)
1258 gbound_args(nb_args);
1259 #endif
1261 /* calculate the number of integer/float register arguments, remember
1262 arguments to be passed via stack (in onstack[]), and also remember
1263 if we have to align the stack pointer to 16 (onstack[i] == 2). Needs
1264 to be done in a left-to-right pass over arguments. */
1265 stack_adjust = 0;
1266 for(i = nb_args - 1; i >= 0; i--) {
1267 mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
1268 if (size == 0) continue;
1269 if (mode == x86_64_mode_sse && nb_sse_args + reg_count <= 8) {
1270 nb_sse_args += reg_count;
1271 onstack[i] = 0;
1272 } else if (mode == x86_64_mode_integer && nb_reg_args + reg_count <= REGN) {
1273 nb_reg_args += reg_count;
1274 onstack[i] = 0;
1275 } else if (mode == x86_64_mode_none) {
1276 onstack[i] = 0;
1277 } else {
1278 if (align == 16 && (stack_adjust &= 15)) {
1279 onstack[i] = 2;
1280 stack_adjust = 0;
1281 } else
1282 onstack[i] = 1;
1283 stack_adjust += size;
1287 if (nb_sse_args && tcc_state->nosse)
1288 tcc_error("SSE disabled but floating point arguments passed");
1290 /* fetch cpu flag before generating any code */
1291 if ((vtop->r & VT_VALMASK) == VT_CMP)
1292 gv(RC_INT);
1294 /* for struct arguments, we need to call memcpy and the function
1295 call breaks register passing arguments we are preparing.
1296 So, we process arguments which will be passed by stack first. */
1297 gen_reg = nb_reg_args;
1298 sse_reg = nb_sse_args;
1299 args_size = 0;
1300 stack_adjust &= 15;
1301 for (i = k = 0; i < nb_args;) {
1302 mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, &reg_count);
1303 if (size) {
1304 if (!onstack[i + k]) {
1305 ++i;
1306 continue;
1308 /* Possibly adjust stack to align SSE boundary. We're processing
1309 args from right to left while allocating happens left to right
1310 (stack grows down), so the adjustment needs to happen _after_
1311 an argument that requires it. */
1312 if (stack_adjust) {
1313 o(0x50); /* push %rax; aka sub $8,%rsp */
1314 args_size += 8;
1315 stack_adjust = 0;
1317 if (onstack[i + k] == 2)
1318 stack_adjust = 1;
1321 vrotb(i+1);
1323 switch (vtop->type.t & VT_BTYPE) {
1324 case VT_STRUCT:
1325 /* allocate the necessary size on stack */
1326 o(0x48);
1327 oad(0xec81, size); /* sub $xxx, %rsp */
1328 /* generate structure store */
1329 r = get_reg(RC_INT);
1330 orex(1, r, 0, 0x89); /* mov %rsp, r */
1331 o(0xe0 + REG_VALUE(r));
1332 vset(&vtop->type, r | VT_LVAL, 0);
1333 vswap();
1334 /* keep stack aligned for (__bound_)memmove call */
1335 o(0x10ec8348); /* sub $16,%rsp */
1336 o(0xf0e48348); /* and $-16,%rsp */
1337 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r (last %rsp) */
1338 o(0x08ec8348); /* sub $8,%rsp */
1339 vstore();
1340 o(0x08c48348); /* add $8,%rsp */
1341 o(0x5c); /* pop %rsp */
1342 break;
1344 case VT_LDOUBLE:
1345 gv(RC_ST0);
1346 oad(0xec8148, size); /* sub $xxx, %rsp */
1347 o(0x7cdb); /* fstpt 0(%rsp) */
1348 g(0x24);
1349 g(0x00);
1350 break;
1352 case VT_FLOAT:
1353 case VT_DOUBLE:
1354 assert(mode == x86_64_mode_sse);
1355 r = gv(RC_FLOAT);
1356 o(0x50); /* push $rax */
1357 /* movq %xmmN, (%rsp) */
1358 o(0xd60f66);
1359 o(0x04 + REG_VALUE(r)*8);
1360 o(0x24);
1361 break;
1363 default:
1364 assert(mode == x86_64_mode_integer);
1365 /* simple type */
1366 /* XXX: implicit cast ? */
1367 r = gv(RC_INT);
1368 orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */
1369 break;
1371 args_size += size;
1373 vpop();
1374 --nb_args;
1375 k++;
1378 tcc_free(onstack);
1380 /* XXX This should be superfluous. */
1381 save_regs(0); /* save used temporary registers */
1383 /* then, we prepare register passing arguments.
1384 Note that we cannot set RDX and RCX in this loop because gv()
1385 may break these temporary registers. Let's use R10 and R11
1386 instead of them */
1387 assert(gen_reg <= REGN);
1388 assert(sse_reg <= 8);
1389 for(i = 0; i < nb_args; i++) {
1390 mode = classify_x86_64_arg(&vtop->type, &type, &size, &align, &reg_count);
1391 if (size == 0) continue;
1392 /* Alter stack entry type so that gv() knows how to treat it */
1393 vtop->type = type;
1394 if (mode == x86_64_mode_sse) {
1395 if (reg_count == 2) {
1396 sse_reg -= 2;
1397 gv(RC_FRET); /* Use pair load into xmm0 & xmm1 */
1398 if (sse_reg) { /* avoid redundant movaps %xmm0, %xmm0 */
1399 /* movaps %xmm1, %xmmN */
1400 o(0x280f);
1401 o(0xc1 + ((sse_reg+1) << 3));
1402 /* movaps %xmm0, %xmmN */
1403 o(0x280f);
1404 o(0xc0 + (sse_reg << 3));
1406 } else {
1407 assert(reg_count == 1);
1408 --sse_reg;
1409 /* Load directly to register */
1410 gv(RC_XMM0 << sse_reg);
1412 } else if (mode == x86_64_mode_integer) {
1413 /* simple type */
1414 /* XXX: implicit cast ? */
1415 int d;
1416 gen_reg -= reg_count;
1417 r = gv(RC_INT);
1418 d = arg_prepare_reg(gen_reg);
1419 orex(1,d,r,0x89); /* mov */
1420 o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
1421 if (reg_count == 2) {
1422 d = arg_prepare_reg(gen_reg+1);
1423 orex(1,d,vtop->r2,0x89); /* mov */
1424 o(0xc0 + REG_VALUE(vtop->r2) * 8 + REG_VALUE(d));
1427 vtop--;
1429 assert(gen_reg == 0);
1430 assert(sse_reg == 0);
1432 /* We shouldn't have many operands on the stack anymore, but the
1433 call address itself is still there, and it might be in %eax
1434 (or edx/ecx) currently, which the below writes would clobber.
1435 So evict all remaining operands here. */
1436 save_regs(0);
1438 /* Copy R10 and R11 into RDX and RCX, respectively */
1439 if (nb_reg_args > 2) {
1440 o(0xd2894c); /* mov %r10, %rdx */
1441 if (nb_reg_args > 3) {
1442 o(0xd9894c); /* mov %r11, %rcx */
1446 if (vtop->type.ref->f.func_type != FUNC_NEW) /* implies FUNC_OLD or FUNC_ELLIPSIS */
1447 oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
1448 gcall_or_jmp(0);
1449 if (args_size)
1450 gadd_sp(args_size);
1451 vtop--;
1454 #define FUNC_PROLOG_SIZE 11
1456 static void push_arg_reg(int i) {
1457 loc -= 8;
1458 gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
1461 /* generate function prolog of type 't' */
1462 void gfunc_prolog(Sym *func_sym)
1464 CType *func_type = &func_sym->type;
1465 X86_64_Mode mode, ret_mode;
1466 int i, addr, align, size, reg_count;
1467 int param_addr = 0, reg_param_index, sse_param_index;
1468 Sym *sym;
1469 CType *type;
1471 sym = func_type->ref;
1472 addr = PTR_SIZE * 2;
1473 loc = 0;
1474 ind += FUNC_PROLOG_SIZE;
1475 func_sub_sp_offset = ind;
1476 func_ret_sub = 0;
1477 ret_mode = classify_x86_64_arg(&func_vt, NULL, &size, &align, &reg_count);
1479 if (func_var) {
1480 int seen_reg_num, seen_sse_num, seen_stack_size;
1481 seen_reg_num = ret_mode == x86_64_mode_memory;
1482 seen_sse_num = 0;
1483 /* frame pointer and return address */
1484 seen_stack_size = PTR_SIZE * 2;
1485 /* count the number of seen parameters */
1486 sym = func_type->ref;
1487 while ((sym = sym->next) != NULL) {
1488 type = &sym->type;
1489 mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
1490 switch (mode) {
1491 default:
1492 stack_arg:
1493 seen_stack_size = ((seen_stack_size + align - 1) & -align) + size;
1494 break;
1496 case x86_64_mode_integer:
1497 if (seen_reg_num + reg_count > REGN)
1498 goto stack_arg;
1499 seen_reg_num += reg_count;
1500 break;
1502 case x86_64_mode_sse:
1503 if (seen_sse_num + reg_count > 8)
1504 goto stack_arg;
1505 seen_sse_num += reg_count;
1506 break;
1510 loc -= 24;
1511 /* movl $0x????????, -0x18(%rbp) */
1512 o(0xe845c7);
1513 gen_le32(seen_reg_num * 8);
1514 /* movl $0x????????, -0x14(%rbp) */
1515 o(0xec45c7);
1516 gen_le32(seen_sse_num * 16 + 48);
1517 /* leaq $0x????????, %r11 */
1518 o(0x9d8d4c);
1519 gen_le32(seen_stack_size);
1520 /* movq %r11, -0x10(%rbp) */
1521 o(0xf05d894c);
1522 /* leaq $-192(%rbp), %r11 */
1523 o(0x9d8d4c);
1524 gen_le32(-176 - 24);
1525 /* movq %r11, -0x8(%rbp) */
1526 o(0xf85d894c);
1528 /* save all register passing arguments */
1529 for (i = 0; i < 8; i++) {
1530 loc -= 16;
1531 if (!tcc_state->nosse) {
1532 o(0xd60f66); /* movq */
1533 gen_modrm(7 - i, VT_LOCAL, NULL, loc);
1535 /* movq $0, loc+8(%rbp) */
1536 o(0x85c748);
1537 gen_le32(loc + 8);
1538 gen_le32(0);
1540 for (i = 0; i < REGN; i++) {
1541 push_arg_reg(REGN-1-i);
1545 sym = func_type->ref;
1546 reg_param_index = 0;
1547 sse_param_index = 0;
1549 /* if the function returns a structure, then add an
1550 implicit pointer parameter */
1551 if (ret_mode == x86_64_mode_memory) {
1552 push_arg_reg(reg_param_index);
1553 func_vc = loc;
1554 reg_param_index++;
1556 /* define parameters */
1557 while ((sym = sym->next) != NULL) {
1558 type = &sym->type;
1559 mode = classify_x86_64_arg(type, NULL, &size, &align, &reg_count);
1560 switch (mode) {
1561 case x86_64_mode_sse:
1562 if (tcc_state->nosse)
1563 tcc_error("SSE disabled but floating point arguments used");
1564 if (sse_param_index + reg_count <= 8) {
1565 /* save arguments passed by register */
1566 loc -= reg_count * 8;
1567 param_addr = loc;
1568 for (i = 0; i < reg_count; ++i) {
1569 o(0xd60f66); /* movq */
1570 gen_modrm(sse_param_index, VT_LOCAL, NULL, param_addr + i*8);
1571 ++sse_param_index;
1573 } else {
1574 addr = (addr + align - 1) & -align;
1575 param_addr = addr;
1576 addr += size;
1578 break;
1580 case x86_64_mode_memory:
1581 case x86_64_mode_x87:
1582 addr = (addr + align - 1) & -align;
1583 param_addr = addr;
1584 addr += size;
1585 break;
1587 case x86_64_mode_integer: {
1588 if (reg_param_index + reg_count <= REGN) {
1589 /* save arguments passed by register */
1590 loc -= reg_count * 8;
1591 param_addr = loc;
1592 for (i = 0; i < reg_count; ++i) {
1593 gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, param_addr + i*8);
1594 ++reg_param_index;
1596 } else {
1597 addr = (addr + align - 1) & -align;
1598 param_addr = addr;
1599 addr += size;
1601 break;
1603 default: break; /* nothing to be done for x86_64_mode_none */
1605 sym_push(sym->v & ~SYM_FIELD, type,
1606 VT_LOCAL | VT_LVAL, param_addr);
1609 #ifdef CONFIG_TCC_BCHECK
1610 if (tcc_state->do_bounds_check)
1611 gen_bounds_prolog();
1612 #endif
1615 /* generate function epilog */
1616 void gfunc_epilog(void)
1618 int v, saved_ind;
1620 #ifdef CONFIG_TCC_BCHECK
1621 if (tcc_state->do_bounds_check)
1622 gen_bounds_epilog();
1623 #endif
1624 o(0xc9); /* leave */
1625 if (func_ret_sub == 0) {
1626 o(0xc3); /* ret */
1627 } else {
1628 o(0xc2); /* ret n */
1629 g(func_ret_sub);
1630 g(func_ret_sub >> 8);
1632 /* align local size to word & save local variables */
1633 v = (-loc + 15) & -16;
1634 saved_ind = ind;
1635 ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
1636 o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
1637 o(0xec8148); /* sub rsp, stacksize */
1638 gen_le32(v);
1639 ind = saved_ind;
1642 #endif /* not PE */
1644 ST_FUNC void gen_fill_nops(int bytes)
1646 while (bytes--)
1647 g(0x90);
1650 /* generate a jump to a label */
1651 int gjmp(int t)
1653 return gjmp2(0xe9, t);
1656 /* generate a jump to a fixed address */
1657 void gjmp_addr(int a)
1659 int r;
1660 r = a - ind - 2;
1661 if (r == (char)r) {
1662 g(0xeb);
1663 g(r);
1664 } else {
1665 oad(0xe9, a - ind - 5);
1669 ST_FUNC int gjmp_append(int n, int t)
1671 void *p;
1672 /* insert vtop->c jump list in t */
1673 if (n) {
1674 uint32_t n1 = n, n2;
1675 while ((n2 = read32le(p = cur_text_section->data + n1)))
1676 n1 = n2;
1677 write32le(p, t);
1678 t = n;
1680 return t;
1683 ST_FUNC int gjmp_cond(int op, int t)
1685 if (op & 0x100)
1687 /* This was a float compare. If the parity flag is set
1688 the result was unordered. For anything except != this
1689 means false and we don't jump (anding both conditions).
1690 For != this means true (oring both).
1691 Take care about inverting the test. We need to jump
1692 to our target if the result was unordered and test wasn't NE,
1693 otherwise if unordered we don't want to jump. */
1694 int v = vtop->cmp_r;
1695 op &= ~0x100;
1696 if (op ^ v ^ (v != TOK_NE))
1697 o(0x067a); /* jp +6 */
1698 else
1700 g(0x0f);
1701 t = gjmp2(0x8a, t); /* jp t */
1704 g(0x0f);
1705 t = gjmp2(op - 16, t);
1706 return t;
1709 /* generate an integer binary operation */
1710 void gen_opi(int op)
1712 int r, fr, opc, c;
1713 int ll, uu, cc;
1715 ll = is64_type(vtop[-1].type.t);
1716 uu = (vtop[-1].type.t & VT_UNSIGNED) != 0;
1717 cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1719 switch(op) {
1720 case '+':
1721 case TOK_ADDC1: /* add with carry generation */
1722 opc = 0;
1723 gen_op8:
1724 if (cc && (!ll || (int)vtop->c.i == vtop->c.i)) {
1725 /* constant case */
1726 vswap();
1727 r = gv(RC_INT);
1728 vswap();
1729 c = vtop->c.i;
1730 if (c == (char)c) {
1731 /* XXX: generate inc and dec for smaller code ? */
1732 orex(ll, r, 0, 0x83);
1733 o(0xc0 | (opc << 3) | REG_VALUE(r));
1734 g(c);
1735 } else {
1736 orex(ll, r, 0, 0x81);
1737 oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
1739 } else {
1740 gv2(RC_INT, RC_INT);
1741 r = vtop[-1].r;
1742 fr = vtop[0].r;
1743 orex(ll, r, fr, (opc << 3) | 0x01);
1744 o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
1746 vtop--;
1747 if (op >= TOK_ULT && op <= TOK_GT)
1748 vset_VT_CMP(op);
1749 break;
1750 case '-':
1751 case TOK_SUBC1: /* sub with carry generation */
1752 opc = 5;
1753 goto gen_op8;
1754 case TOK_ADDC2: /* add with carry use */
1755 opc = 2;
1756 goto gen_op8;
1757 case TOK_SUBC2: /* sub with carry use */
1758 opc = 3;
1759 goto gen_op8;
1760 case '&':
1761 opc = 4;
1762 goto gen_op8;
1763 case '^':
1764 opc = 6;
1765 goto gen_op8;
1766 case '|':
1767 opc = 1;
1768 goto gen_op8;
1769 case '*':
1770 gv2(RC_INT, RC_INT);
1771 r = vtop[-1].r;
1772 fr = vtop[0].r;
1773 orex(ll, fr, r, 0xaf0f); /* imul fr, r */
1774 o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8);
1775 vtop--;
1776 break;
1777 case TOK_SHL:
1778 opc = 4;
1779 goto gen_shift;
1780 case TOK_SHR:
1781 opc = 5;
1782 goto gen_shift;
1783 case TOK_SAR:
1784 opc = 7;
1785 gen_shift:
1786 opc = 0xc0 | (opc << 3);
1787 if (cc) {
1788 /* constant case */
1789 vswap();
1790 r = gv(RC_INT);
1791 vswap();
1792 orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */
1793 o(opc | REG_VALUE(r));
1794 g(vtop->c.i & (ll ? 63 : 31));
1795 } else {
1796 /* we generate the shift in ecx */
1797 gv2(RC_INT, RC_RCX);
1798 r = vtop[-1].r;
1799 orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */
1800 o(opc | REG_VALUE(r));
1802 vtop--;
1803 break;
1804 case TOK_UDIV:
1805 case TOK_UMOD:
1806 uu = 1;
1807 goto divmod;
1808 case '/':
1809 case '%':
1810 case TOK_PDIV:
1811 uu = 0;
1812 divmod:
1813 /* first operand must be in eax */
1814 /* XXX: need better constraint for second operand */
1815 gv2(RC_RAX, RC_RCX);
1816 r = vtop[-1].r;
1817 fr = vtop[0].r;
1818 vtop--;
1819 save_reg(TREG_RDX);
1820 orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */
1821 orex(ll, fr, 0, 0xf7); /* div fr, %eax */
1822 o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr));
1823 if (op == '%' || op == TOK_UMOD)
1824 r = TREG_RDX;
1825 else
1826 r = TREG_RAX;
1827 vtop->r = r;
1828 break;
1829 default:
1830 opc = 7;
1831 goto gen_op8;
1835 void gen_opl(int op)
1837 gen_opi(op);
1840 void vpush_const(int t, int v)
1842 CType ctype = { t | VT_CONSTANT, 0 };
1843 vpushsym(&ctype, external_global_sym(v, &ctype));
1844 vtop->r |= VT_LVAL;
1847 /* generate a floating point operation 'v = t1 op t2' instruction. The
1848 two operands are guaranteed to have the same floating point type */
1849 /* XXX: need to use ST1 too */
1850 void gen_opf(int op)
1852 int a, ft, fc, swapped, r;
1853 int bt = vtop->type.t & VT_BTYPE;
1854 int float_type = bt == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
1856 if (op == TOK_NEG) { /* unary minus */
1857 gv(float_type);
1858 if (float_type == RC_ST0) {
1859 o(0xe0d9); /* fchs */
1860 } else {
1861 /* -0.0, in libtcc1.c */
1862 vpush_const(bt, bt == VT_FLOAT ? TOK___mzerosf : TOK___mzerodf);
1863 gv(RC_FLOAT);
1864 if (bt == VT_DOUBLE)
1865 o(0x66);
1866 /* xorp[sd] %xmm1, %xmm0 */
1867 o(0xc0570f | (REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8) << 16);
1868 vtop--;
1870 return;
1873 /* convert constants to memory references */
1874 if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1875 vswap();
1876 gv(float_type);
1877 vswap();
1879 if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
1880 gv(float_type);
1882 /* must put at least one value in the floating point register */
1883 if ((vtop[-1].r & VT_LVAL) &&
1884 (vtop[0].r & VT_LVAL)) {
1885 vswap();
1886 gv(float_type);
1887 vswap();
1889 swapped = 0;
1890 /* swap the stack if needed so that t1 is the register and t2 is
1891 the memory reference */
1892 if (vtop[-1].r & VT_LVAL) {
1893 vswap();
1894 swapped = 1;
1896 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
1897 if (op >= TOK_ULT && op <= TOK_GT) {
1898 /* load on stack second operand */
1899 load(TREG_ST0, vtop);
1900 save_reg(TREG_RAX); /* eax is used by FP comparison code */
1901 if (op == TOK_GE || op == TOK_GT)
1902 swapped = !swapped;
1903 else if (op == TOK_EQ || op == TOK_NE)
1904 swapped = 0;
1905 if (swapped)
1906 o(0xc9d9); /* fxch %st(1) */
1907 if (op == TOK_EQ || op == TOK_NE)
1908 o(0xe9da); /* fucompp */
1909 else
1910 o(0xd9de); /* fcompp */
1911 o(0xe0df); /* fnstsw %ax */
1912 if (op == TOK_EQ) {
1913 o(0x45e480); /* and $0x45, %ah */
1914 o(0x40fC80); /* cmp $0x40, %ah */
1915 } else if (op == TOK_NE) {
1916 o(0x45e480); /* and $0x45, %ah */
1917 o(0x40f480); /* xor $0x40, %ah */
1918 op = TOK_NE;
1919 } else if (op == TOK_GE || op == TOK_LE) {
1920 o(0x05c4f6); /* test $0x05, %ah */
1921 op = TOK_EQ;
1922 } else {
1923 o(0x45c4f6); /* test $0x45, %ah */
1924 op = TOK_EQ;
1926 vtop--;
1927 vset_VT_CMP(op);
1928 } else {
1929 /* no memory reference possible for long double operations */
1930 load(TREG_ST0, vtop);
1931 swapped = !swapped;
1933 switch(op) {
1934 default:
1935 case '+':
1936 a = 0;
1937 break;
1938 case '-':
1939 a = 4;
1940 if (swapped)
1941 a++;
1942 break;
1943 case '*':
1944 a = 1;
1945 break;
1946 case '/':
1947 a = 6;
1948 if (swapped)
1949 a++;
1950 break;
1952 ft = vtop->type.t;
1953 fc = vtop->c.i;
1954 o(0xde); /* fxxxp %st, %st(1) */
1955 o(0xc1 + (a << 3));
1956 vtop--;
1958 } else {
1959 if (op >= TOK_ULT && op <= TOK_GT) {
1960 /* if saved lvalue, then we must reload it */
1961 r = vtop->r;
1962 fc = vtop->c.i;
1963 if ((r & VT_VALMASK) == VT_LLOCAL) {
1964 SValue v1;
1965 r = get_reg(RC_INT);
1966 v1.type.t = VT_PTR;
1967 v1.r = VT_LOCAL | VT_LVAL;
1968 v1.c.i = fc;
1969 load(r, &v1);
1970 fc = 0;
1971 vtop->r = r = r | VT_LVAL;
1974 if (op == TOK_EQ || op == TOK_NE) {
1975 swapped = 0;
1976 } else {
1977 if (op == TOK_LE || op == TOK_LT)
1978 swapped = !swapped;
1979 if (op == TOK_LE || op == TOK_GE) {
1980 op = 0x93; /* setae */
1981 } else {
1982 op = 0x97; /* seta */
1986 if (swapped) {
1987 gv(RC_FLOAT);
1988 vswap();
1990 assert(!(vtop[-1].r & VT_LVAL));
1992 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1993 o(0x66);
1994 if (op == TOK_EQ || op == TOK_NE)
1995 o(0x2e0f); /* ucomisd */
1996 else
1997 o(0x2f0f); /* comisd */
1999 if (vtop->r & VT_LVAL) {
2000 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
2001 } else {
2002 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
2005 vtop--;
2006 vset_VT_CMP(op | 0x100);
2007 vtop->cmp_r = op;
2008 } else {
2009 assert((vtop->type.t & VT_BTYPE) != VT_LDOUBLE);
2010 switch(op) {
2011 default:
2012 case '+':
2013 a = 0;
2014 break;
2015 case '-':
2016 a = 4;
2017 break;
2018 case '*':
2019 a = 1;
2020 break;
2021 case '/':
2022 a = 6;
2023 break;
2025 ft = vtop->type.t;
2026 fc = vtop->c.i;
2027 assert((ft & VT_BTYPE) != VT_LDOUBLE);
2029 r = vtop->r;
2030 /* if saved lvalue, then we must reload it */
2031 if ((vtop->r & VT_VALMASK) == VT_LLOCAL) {
2032 SValue v1;
2033 r = get_reg(RC_INT);
2034 v1.type.t = VT_PTR;
2035 v1.r = VT_LOCAL | VT_LVAL;
2036 v1.c.i = fc;
2037 load(r, &v1);
2038 fc = 0;
2039 vtop->r = r = r | VT_LVAL;
2042 assert(!(vtop[-1].r & VT_LVAL));
2043 if (swapped) {
2044 assert(vtop->r & VT_LVAL);
2045 gv(RC_FLOAT);
2046 vswap();
2049 if ((ft & VT_BTYPE) == VT_DOUBLE) {
2050 o(0xf2);
2051 } else {
2052 o(0xf3);
2054 o(0x0f);
2055 o(0x58 + a);
2057 if (vtop->r & VT_LVAL) {
2058 gen_modrm(vtop[-1].r, r, vtop->sym, fc);
2059 } else {
2060 o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
2063 vtop--;
2068 /* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
2069 and 'long long' cases. */
2070 void gen_cvt_itof(int t)
2072 if ((t & VT_BTYPE) == VT_LDOUBLE) {
2073 save_reg(TREG_ST0);
2074 gv(RC_INT);
2075 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
2076 /* signed long long to float/double/long double (unsigned case
2077 is handled generically) */
2078 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2079 o(0x242cdf); /* fildll (%rsp) */
2080 o(0x08c48348); /* add $8, %rsp */
2081 } else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2082 (VT_INT | VT_UNSIGNED)) {
2083 /* unsigned int to float/double/long double */
2084 o(0x6a); /* push $0 */
2085 g(0x00);
2086 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2087 o(0x242cdf); /* fildll (%rsp) */
2088 o(0x10c48348); /* add $16, %rsp */
2089 } else {
2090 /* int to float/double/long double */
2091 o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
2092 o(0x2404db); /* fildl (%rsp) */
2093 o(0x08c48348); /* add $8, %rsp */
2095 vtop->r = TREG_ST0;
2096 } else {
2097 int r = get_reg(RC_FLOAT);
2098 gv(RC_INT);
2099 o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT?1:0));
2100 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2101 (VT_INT | VT_UNSIGNED) ||
2102 (vtop->type.t & VT_BTYPE) == VT_LLONG) {
2103 o(0x48); /* REX */
2105 o(0x2a0f);
2106 o(0xc0 + (vtop->r & VT_VALMASK) + REG_VALUE(r)*8); /* cvtsi2sd */
2107 vtop->r = r;
2111 /* convert from one floating point type to another */
2112 void gen_cvt_ftof(int t)
2114 int ft, bt, tbt;
2116 ft = vtop->type.t;
2117 bt = ft & VT_BTYPE;
2118 tbt = t & VT_BTYPE;
2120 if (bt == VT_FLOAT) {
2121 gv(RC_FLOAT);
2122 if (tbt == VT_DOUBLE) {
2123 o(0x140f); /* unpcklps */
2124 o(0xc0 + REG_VALUE(vtop->r)*9);
2125 o(0x5a0f); /* cvtps2pd */
2126 o(0xc0 + REG_VALUE(vtop->r)*9);
2127 } else if (tbt == VT_LDOUBLE) {
2128 save_reg(RC_ST0);
2129 /* movss %xmm0,-0x10(%rsp) */
2130 o(0x110ff3);
2131 o(0x44 + REG_VALUE(vtop->r)*8);
2132 o(0xf024);
2133 o(0xf02444d9); /* flds -0x10(%rsp) */
2134 vtop->r = TREG_ST0;
2136 } else if (bt == VT_DOUBLE) {
2137 gv(RC_FLOAT);
2138 if (tbt == VT_FLOAT) {
2139 o(0x140f66); /* unpcklpd */
2140 o(0xc0 + REG_VALUE(vtop->r)*9);
2141 o(0x5a0f66); /* cvtpd2ps */
2142 o(0xc0 + REG_VALUE(vtop->r)*9);
2143 } else if (tbt == VT_LDOUBLE) {
2144 save_reg(RC_ST0);
2145 /* movsd %xmm0,-0x10(%rsp) */
2146 o(0x110ff2);
2147 o(0x44 + REG_VALUE(vtop->r)*8);
2148 o(0xf024);
2149 o(0xf02444dd); /* fldl -0x10(%rsp) */
2150 vtop->r = TREG_ST0;
2152 } else {
2153 int r;
2154 gv(RC_ST0);
2155 r = get_reg(RC_FLOAT);
2156 if (tbt == VT_DOUBLE) {
2157 o(0xf0245cdd); /* fstpl -0x10(%rsp) */
2158 /* movsd -0x10(%rsp),%xmm0 */
2159 o(0x100ff2);
2160 o(0x44 + REG_VALUE(r)*8);
2161 o(0xf024);
2162 vtop->r = r;
2163 } else if (tbt == VT_FLOAT) {
2164 o(0xf0245cd9); /* fstps -0x10(%rsp) */
2165 /* movss -0x10(%rsp),%xmm0 */
2166 o(0x100ff3);
2167 o(0x44 + REG_VALUE(r)*8);
2168 o(0xf024);
2169 vtop->r = r;
2174 /* convert fp to int 't' type */
2175 void gen_cvt_ftoi(int t)
2177 int ft, bt, size, r;
2178 ft = vtop->type.t;
2179 bt = ft & VT_BTYPE;
2180 if (bt == VT_LDOUBLE) {
2181 gen_cvt_ftof(VT_DOUBLE);
2182 bt = VT_DOUBLE;
2185 gv(RC_FLOAT);
2186 if (t != VT_INT)
2187 size = 8;
2188 else
2189 size = 4;
2191 r = get_reg(RC_INT);
2192 if (bt == VT_FLOAT) {
2193 o(0xf3);
2194 } else if (bt == VT_DOUBLE) {
2195 o(0xf2);
2196 } else {
2197 assert(0);
2199 orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */
2200 o(0xc0 + REG_VALUE(vtop->r) + REG_VALUE(r)*8);
2201 vtop->r = r;
2204 // Generate sign extension from 32 to 64 bits:
2205 ST_FUNC void gen_cvt_sxtw(void)
2207 int r = gv(RC_INT);
2208 /* x86_64 specific: movslq */
2209 o(0x6348);
2210 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2213 /* char/short to int conversion */
2214 ST_FUNC void gen_cvt_csti(int t)
2216 int r, sz, xl, ll;
2217 r = gv(RC_INT);
2218 sz = !(t & VT_UNSIGNED);
2219 xl = (t & VT_BTYPE) == VT_SHORT;
2220 ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
2221 orex(ll, r, 0, 0xc0b60f /* mov[sz] %a[xl], %eax */
2222 | (sz << 3 | xl) << 8
2223 | (REG_VALUE(r) << 3 | REG_VALUE(r)) << 16
2227 /* increment tcov counter */
2228 ST_FUNC void gen_increment_tcov (SValue *sv)
2230 o(0x058348); /* addq $1, xxx(%rip) */
2231 greloca(cur_text_section, sv->sym, ind, R_X86_64_PC32, -5);
2232 gen_le32(0);
2233 o(1);
2236 /* computed goto support */
2237 ST_FUNC void ggoto(void)
2239 gcall_or_jmp(1);
2240 vtop--;
2243 /* Save the stack pointer onto the stack and return the location of its address */
2244 ST_FUNC void gen_vla_sp_save(int addr) {
2245 /* mov %rsp,addr(%rbp)*/
2246 gen_modrm64(0x89, TREG_RSP, VT_LOCAL, NULL, addr);
2249 /* Restore the SP from a location on the stack */
2250 ST_FUNC void gen_vla_sp_restore(int addr) {
2251 gen_modrm64(0x8b, TREG_RSP, VT_LOCAL, NULL, addr);
2254 #ifdef TCC_TARGET_PE
2255 /* Save result of gen_vla_alloc onto the stack */
2256 ST_FUNC void gen_vla_result(int addr) {
2257 /* mov %rax,addr(%rbp)*/
2258 gen_modrm64(0x89, TREG_RAX, VT_LOCAL, NULL, addr);
2260 #endif
2262 /* Subtract from the stack pointer, and push the resulting value onto the stack */
2263 ST_FUNC void gen_vla_alloc(CType *type, int align) {
2264 int use_call = 0;
2266 #if defined(CONFIG_TCC_BCHECK)
2267 use_call = tcc_state->do_bounds_check;
2268 #endif
2269 #ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
2270 use_call = 1;
2271 #endif
2272 if (use_call)
2274 vpush_helper_func(TOK_alloca);
2275 vswap(); /* Move alloca ref past allocation size */
2276 gfunc_call(1);
2278 else {
2279 int r;
2280 r = gv(RC_INT); /* allocation size */
2281 /* sub r,%rsp */
2282 o(0x2b48);
2283 o(0xe0 | REG_VALUE(r));
2284 /* We align to 16 bytes rather than align */
2285 /* and ~15, %rsp */
2286 o(0xf0e48348);
2287 vpop();
2292 * Assmuing the top part of the stack looks like below,
2293 * src dest src
2295 ST_FUNC void gen_struct_copy(int size)
2297 int n = size / PTR_SIZE;
2298 #ifdef TCC_TARGET_PE
2299 o(0x5756); /* push rsi, rdi */
2300 #endif
2301 gv2(RC_RDI, RC_RSI);
2302 if (n <= 4) {
2303 while (n)
2304 o(0xa548), --n;
2305 } else {
2306 vpushi(n);
2307 gv(RC_RCX);
2308 o(0xa548f3);
2309 vpop();
2311 if (size & 0x04)
2312 o(0xa5);
2313 if (size & 0x02)
2314 o(0xa566);
2315 if (size & 0x01)
2316 o(0xa4);
2317 #ifdef TCC_TARGET_PE
2318 o(0x5e5f); /* pop rdi, rsi */
2319 #endif
2320 vpop();
2321 vpop();
2324 /* end of x86-64 code generator */
2325 /*************************************************************/
2326 #endif /* ! TARGET_DEFS_ONLY */
2327 /******************************************************/