2008-12-01 21:19:25 +03:00
|
|
|
/*
|
|
|
|
* x86-64 code generator for TCC
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Shinichiro Hamaji
|
|
|
|
*
|
|
|
|
* Based on i386-gen.c by Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
#ifdef TARGET_DEFS_ONLY
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
/* number of available registers */
|
2013-04-27 23:39:34 +04:00
|
|
|
#define NB_REGS 25
|
2009-12-20 03:53:49 +03:00
|
|
|
#define NB_ASM_REGS 8
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
/* a register can belong to several classes. The classes must be
|
|
|
|
sorted from more general to more precise (see gv2() code which does
|
|
|
|
assumptions on it). */
|
|
|
|
#define RC_INT 0x0001 /* generic integer register */
|
|
|
|
#define RC_FLOAT 0x0002 /* generic float register */
|
|
|
|
#define RC_RAX 0x0004
|
|
|
|
#define RC_RCX 0x0008
|
|
|
|
#define RC_RDX 0x0010
|
2013-04-20 01:05:49 +04:00
|
|
|
#define RC_ST0 0x0080 /* only for long double */
|
2009-12-19 02:06:41 +03:00
|
|
|
#define RC_R8 0x0100
|
|
|
|
#define RC_R9 0x0200
|
2012-03-05 23:19:28 +04:00
|
|
|
#define RC_R10 0x0400
|
|
|
|
#define RC_R11 0x0800
|
2013-04-20 01:05:49 +04:00
|
|
|
#define RC_XMM0 0x1000
|
|
|
|
#define RC_XMM1 0x2000
|
|
|
|
#define RC_XMM2 0x4000
|
|
|
|
#define RC_XMM3 0x8000
|
|
|
|
#define RC_XMM4 0x10000
|
|
|
|
#define RC_XMM5 0x20000
|
2013-04-26 01:30:53 +04:00
|
|
|
#define RC_XMM6 0x40000
|
|
|
|
#define RC_XMM7 0x80000
|
2008-12-01 21:19:25 +03:00
|
|
|
#define RC_IRET RC_RAX /* function return: integer register */
|
|
|
|
#define RC_LRET RC_RDX /* function return: second integer register */
|
|
|
|
#define RC_FRET RC_XMM0 /* function return: float register */
|
2013-04-19 18:33:16 +04:00
|
|
|
#define RC_QRET RC_XMM1 /* function return: second float register */
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
/* pretty names for the registers */
|
|
|
|
enum {
|
|
|
|
TREG_RAX = 0,
|
|
|
|
TREG_RCX = 1,
|
|
|
|
TREG_RDX = 2,
|
2013-04-27 23:39:34 +04:00
|
|
|
TREG_RSP = 4,
|
2008-12-01 21:19:25 +03:00
|
|
|
TREG_RSI = 6,
|
|
|
|
TREG_RDI = 7,
|
2013-04-19 21:31:24 +04:00
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
TREG_R8 = 8,
|
|
|
|
TREG_R9 = 9,
|
|
|
|
TREG_R10 = 10,
|
|
|
|
TREG_R11 = 11,
|
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
TREG_XMM0 = 16,
|
|
|
|
TREG_XMM1 = 17,
|
2013-04-20 01:05:49 +04:00
|
|
|
TREG_XMM2 = 18,
|
|
|
|
TREG_XMM3 = 19,
|
|
|
|
TREG_XMM4 = 20,
|
|
|
|
TREG_XMM5 = 21,
|
|
|
|
TREG_XMM6 = 22,
|
|
|
|
TREG_XMM7 = 23,
|
2013-04-19 21:31:24 +04:00
|
|
|
|
2013-04-27 23:39:34 +04:00
|
|
|
TREG_ST0 = 24,
|
2013-04-19 21:31:24 +04:00
|
|
|
|
2015-11-19 21:26:47 +03:00
|
|
|
TREG_MEM = 0x20
|
2008-12-01 21:19:25 +03:00
|
|
|
};
|
|
|
|
|
2009-04-12 19:27:04 +04:00
|
|
|
#define REX_BASE(reg) (((reg) >> 3) & 1)
|
2008-12-01 21:19:25 +03:00
|
|
|
#define REG_VALUE(reg) ((reg) & 7)
|
|
|
|
|
|
|
|
/* return registers for function */
|
|
|
|
#define REG_IRET TREG_RAX /* single word int return register */
|
|
|
|
#define REG_LRET TREG_RDX /* second word return register (for long long) */
|
|
|
|
#define REG_FRET TREG_XMM0 /* float return register */
|
2013-04-19 18:33:16 +04:00
|
|
|
#define REG_QRET TREG_XMM1 /* second float return register */
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
/* defined if function parameters must be evaluated in reverse order */
|
|
|
|
#define INVERT_FUNC_PARAMS
|
|
|
|
|
|
|
|
/* pointer size, in bytes */
|
|
|
|
#define PTR_SIZE 8
|
|
|
|
|
|
|
|
/* long double size and alignment, in bytes */
|
|
|
|
#define LDOUBLE_SIZE 16
|
2013-04-26 19:42:12 +04:00
|
|
|
#define LDOUBLE_ALIGN 16
|
2008-12-01 21:19:25 +03:00
|
|
|
/* maximum alignment (for aligned attribute support) */
|
2013-04-26 19:42:12 +04:00
|
|
|
#define MAX_ALIGN 16
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
/******************************************************/
|
2009-12-20 03:53:49 +03:00
|
|
|
#else /* ! TARGET_DEFS_ONLY */
|
|
|
|
/******************************************************/
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "tcc.h"
|
2009-12-20 03:53:49 +03:00
|
|
|
#include <assert.h>
|
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
ST_DATA const int reg_classes[NB_REGS] = {
|
2009-12-20 03:53:49 +03:00
|
|
|
/* eax */ RC_INT | RC_RAX,
|
|
|
|
/* ecx */ RC_INT | RC_RCX,
|
|
|
|
/* edx */ RC_INT | RC_RDX,
|
2013-04-19 21:31:24 +04:00
|
|
|
0,
|
2013-04-27 23:39:34 +04:00
|
|
|
0,
|
2009-12-19 02:06:41 +03:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2013-04-20 01:05:49 +04:00
|
|
|
RC_R8,
|
|
|
|
RC_R9,
|
|
|
|
RC_R10,
|
|
|
|
RC_R11,
|
2013-04-19 21:31:24 +04:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
/* xmm0 */ RC_FLOAT | RC_XMM0,
|
|
|
|
/* xmm1 */ RC_FLOAT | RC_XMM1,
|
2013-04-20 01:05:49 +04:00
|
|
|
/* xmm2 */ RC_FLOAT | RC_XMM2,
|
|
|
|
/* xmm3 */ RC_FLOAT | RC_XMM3,
|
|
|
|
/* xmm4 */ RC_FLOAT | RC_XMM4,
|
2013-04-26 01:30:53 +04:00
|
|
|
/* xmm5 */ RC_FLOAT | RC_XMM5,
|
|
|
|
/* xmm6 an xmm7 are included so gv() can be used on them,
|
|
|
|
but they are not tagged with RC_FLOAT because they are
|
|
|
|
callee saved on Windows */
|
|
|
|
RC_XMM6,
|
2013-04-27 23:39:34 +04:00
|
|
|
RC_XMM7,
|
|
|
|
/* st0 */ RC_ST0
|
2009-12-20 03:53:49 +03:00
|
|
|
};
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
static unsigned long func_sub_sp_offset;
|
|
|
|
static int func_ret_sub;
|
|
|
|
|
|
|
|
/* XXX: make it faster ? */
|
2016-09-26 05:32:41 +03:00
|
|
|
ST_FUNC void g(int c)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
int ind1;
|
|
|
|
ind1 = ind + 1;
|
|
|
|
if (ind1 > cur_text_section->data_allocated)
|
|
|
|
section_realloc(cur_text_section, ind1);
|
|
|
|
cur_text_section->data[ind] = c;
|
|
|
|
ind = ind1;
|
|
|
|
}
|
|
|
|
|
2016-09-26 05:32:41 +03:00
|
|
|
ST_FUNC void o(unsigned int c)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
while (c) {
|
|
|
|
g(c);
|
|
|
|
c = c >> 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 05:32:41 +03:00
|
|
|
ST_FUNC void gen_le16(int v)
|
2009-12-20 00:08:37 +03:00
|
|
|
{
|
|
|
|
g(v);
|
|
|
|
g(v >> 8);
|
|
|
|
}
|
|
|
|
|
2016-09-26 05:32:41 +03:00
|
|
|
ST_FUNC void gen_le32(int c)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
g(c);
|
|
|
|
g(c >> 8);
|
|
|
|
g(c >> 16);
|
|
|
|
g(c >> 24);
|
|
|
|
}
|
|
|
|
|
2016-09-26 05:32:41 +03:00
|
|
|
ST_FUNC void gen_le64(int64_t c)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
g(c);
|
|
|
|
g(c >> 8);
|
|
|
|
g(c >> 16);
|
|
|
|
g(c >> 24);
|
|
|
|
g(c >> 32);
|
|
|
|
g(c >> 40);
|
|
|
|
g(c >> 48);
|
|
|
|
g(c >> 56);
|
|
|
|
}
|
|
|
|
|
2016-09-26 05:32:41 +03:00
|
|
|
static void orex(int ll, int r, int r2, int b)
|
2009-12-20 04:19:51 +03:00
|
|
|
{
|
|
|
|
if ((r & VT_VALMASK) >= VT_CONST)
|
|
|
|
r = 0;
|
|
|
|
if ((r2 & VT_VALMASK) >= VT_CONST)
|
|
|
|
r2 = 0;
|
|
|
|
if (ll || REX_BASE(r) || REX_BASE(r2))
|
|
|
|
o(0x40 | REX_BASE(r) | (REX_BASE(r2) << 2) | (ll << 3));
|
|
|
|
o(b);
|
|
|
|
}
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* output a symbol and patch all calls to it */
|
2016-09-26 05:32:41 +03:00
|
|
|
ST_FUNC void gsym_addr(int t, int a)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
while (t) {
|
2015-11-19 21:21:14 +03:00
|
|
|
unsigned char *ptr = cur_text_section->data + t;
|
|
|
|
uint32_t n = read32le(ptr); /* next value */
|
|
|
|
write32le(ptr, a - t - 4);
|
2008-12-01 21:19:25 +03:00
|
|
|
t = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gsym(int t)
|
|
|
|
{
|
|
|
|
gsym_addr(t, ind);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* psym is used to put an instruction with a data field which is a
|
|
|
|
reference to a symbol. It is in fact the same as oad ! */
|
|
|
|
#define psym oad
|
|
|
|
|
|
|
|
static int is64_type(int t)
|
|
|
|
{
|
|
|
|
return ((t & VT_BTYPE) == VT_PTR ||
|
|
|
|
(t & VT_BTYPE) == VT_FUNC ||
|
|
|
|
(t & VT_BTYPE) == VT_LLONG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* instruction + 4 bytes data. Return the address of the data */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC int oad(int c, int s)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
int ind1;
|
|
|
|
|
|
|
|
o(c);
|
|
|
|
ind1 = ind + 4;
|
|
|
|
if (ind1 > cur_text_section->data_allocated)
|
|
|
|
section_realloc(cur_text_section, ind1);
|
2015-11-19 21:21:14 +03:00
|
|
|
write32le(cur_text_section->data + ind, s);
|
2008-12-01 21:19:25 +03:00
|
|
|
s = ind;
|
|
|
|
ind = ind1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-09-29 17:57:30 +03:00
|
|
|
ST_FUNC void gen_addr32(int r, Sym *sym, long c)
|
2009-12-20 00:08:37 +03:00
|
|
|
{
|
|
|
|
if (r & VT_SYM)
|
2016-09-27 18:04:39 +03:00
|
|
|
greloca(cur_text_section, sym, ind, R_X86_64_32S, c), c=0;
|
2009-12-20 00:08:37 +03:00
|
|
|
gen_le32(c);
|
|
|
|
}
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* output constant with relocation if 'r & VT_SYM' is true */
|
2009-12-20 03:53:49 +03:00
|
|
|
ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
if (r & VT_SYM)
|
2016-05-10 00:09:55 +03:00
|
|
|
greloca(cur_text_section, sym, ind, R_X86_64_64, c), c=0;
|
2008-12-01 21:19:25 +03:00
|
|
|
gen_le64(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output constant with relocation if 'r & VT_SYM' is true */
|
2016-09-29 17:57:30 +03:00
|
|
|
ST_FUNC void gen_addrpc32(int r, Sym *sym, long c)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
|
|
|
if (r & VT_SYM)
|
2016-05-10 00:09:55 +03:00
|
|
|
greloca(cur_text_section, sym, ind, R_X86_64_PC32, c-4), c=4;
|
2008-12-01 21:19:25 +03:00
|
|
|
gen_le32(c-4);
|
|
|
|
}
|
|
|
|
|
2009-04-12 19:27:04 +04:00
|
|
|
/* output got address with relocation */
|
|
|
|
static void gen_gotpcrel(int r, Sym *sym, int c)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
2009-07-19 00:05:49 +04:00
|
|
|
#ifndef TCC_TARGET_PE
|
2016-05-10 00:09:55 +03:00
|
|
|
greloca(cur_text_section, sym, ind, R_X86_64_GOTPCREL, -4);
|
2009-07-19 00:05:49 +04:00
|
|
|
#else
|
2014-05-08 19:32:29 +04:00
|
|
|
tcc_error("internal error: no GOT on PE: %s %x %x | %02x %02x %02x\n",
|
|
|
|
get_tok_str(sym->v, NULL), c, r,
|
2009-07-19 00:05:49 +04:00
|
|
|
cur_text_section->data[ind-3],
|
|
|
|
cur_text_section->data[ind-2],
|
|
|
|
cur_text_section->data[ind-1]
|
|
|
|
);
|
|
|
|
greloc(cur_text_section, sym, ind, R_X86_64_PC32);
|
|
|
|
#endif
|
2009-04-12 19:27:04 +04:00
|
|
|
gen_le32(0);
|
|
|
|
if (c) {
|
|
|
|
/* we use add c, %xxx for displacement */
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1, r, 0, 0x81);
|
2009-04-12 19:27:04 +04:00
|
|
|
o(0xc0 + REG_VALUE(r));
|
|
|
|
gen_le32(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gen_modrm_impl(int op_reg, int r, Sym *sym, int c, int is_got)
|
|
|
|
{
|
|
|
|
op_reg = REG_VALUE(op_reg) << 3;
|
2008-12-01 21:19:25 +03:00
|
|
|
if ((r & VT_VALMASK) == VT_CONST) {
|
|
|
|
/* constant memory reference */
|
|
|
|
o(0x05 | op_reg);
|
2009-04-12 19:27:04 +04:00
|
|
|
if (is_got) {
|
|
|
|
gen_gotpcrel(r, sym, c);
|
|
|
|
} else {
|
|
|
|
gen_addrpc32(r, sym, c);
|
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if ((r & VT_VALMASK) == VT_LOCAL) {
|
|
|
|
/* currently, we use only ebp as base */
|
|
|
|
if (c == (char)c) {
|
|
|
|
/* short reference */
|
|
|
|
o(0x45 | op_reg);
|
|
|
|
g(c);
|
|
|
|
} else {
|
|
|
|
oad(0x85 | op_reg, c);
|
|
|
|
}
|
2009-04-12 19:27:04 +04:00
|
|
|
} else if ((r & VT_VALMASK) >= TREG_MEM) {
|
|
|
|
if (c) {
|
|
|
|
g(0x80 | op_reg | REG_VALUE(r));
|
|
|
|
gen_le32(c);
|
|
|
|
} else {
|
|
|
|
g(0x00 | op_reg | REG_VALUE(r));
|
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
2009-12-19 02:06:41 +03:00
|
|
|
g(0x00 | op_reg | REG_VALUE(r));
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-12 19:27:04 +04:00
|
|
|
/* generate a modrm reference. 'op_reg' contains the addtionnal 3
|
|
|
|
opcode bits */
|
|
|
|
static void gen_modrm(int op_reg, int r, Sym *sym, int c)
|
|
|
|
{
|
|
|
|
gen_modrm_impl(op_reg, r, sym, c, 0);
|
|
|
|
}
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* generate a modrm reference. 'op_reg' contains the addtionnal 3
|
|
|
|
opcode bits */
|
|
|
|
static void gen_modrm64(int opcode, int op_reg, int r, Sym *sym, int c)
|
|
|
|
{
|
2009-04-12 19:27:04 +04:00
|
|
|
int is_got;
|
|
|
|
is_got = (op_reg & TREG_MEM) && !(sym->type.t & VT_STATIC);
|
2009-12-20 22:33:21 +03:00
|
|
|
orex(1, r, op_reg, opcode);
|
2009-04-12 19:27:04 +04:00
|
|
|
gen_modrm_impl(op_reg, r, sym, c, is_got);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* load 'r' from value 'sv' */
|
|
|
|
void load(int r, SValue *sv)
|
|
|
|
{
|
|
|
|
int v, t, ft, fc, fr;
|
|
|
|
SValue v1;
|
|
|
|
|
2009-12-20 00:08:52 +03:00
|
|
|
#ifdef TCC_TARGET_PE
|
2010-01-14 22:55:51 +03:00
|
|
|
SValue v2;
|
|
|
|
sv = pe_getimport(sv, &v2);
|
2009-12-20 00:08:52 +03:00
|
|
|
#endif
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
fr = sv->r;
|
2014-02-07 18:31:44 +04:00
|
|
|
ft = sv->type.t & ~VT_DEFSIGN;
|
2015-11-17 22:09:35 +03:00
|
|
|
fc = sv->c.i;
|
2008-12-01 21:19:25 +03:00
|
|
|
|
2016-03-26 19:57:22 +03:00
|
|
|
ft &= ~(VT_VOLATILE | VT_CONSTANT);
|
|
|
|
|
2009-07-19 00:05:49 +04:00
|
|
|
#ifndef TCC_TARGET_PE
|
2009-04-12 19:27:04 +04:00
|
|
|
/* we use indirect access via got */
|
|
|
|
if ((fr & VT_VALMASK) == VT_CONST && (fr & VT_SYM) &&
|
|
|
|
(fr & VT_LVAL) && !(sv->sym->type.t & VT_STATIC)) {
|
|
|
|
/* use the result register as a temporal register */
|
|
|
|
int tr = r | TREG_MEM;
|
|
|
|
if (is_float(ft)) {
|
|
|
|
/* we cannot use float registers as a temporal register */
|
|
|
|
tr = get_reg(RC_INT) | TREG_MEM;
|
|
|
|
}
|
|
|
|
gen_modrm64(0x8b, tr, fr, sv->sym, 0);
|
|
|
|
|
|
|
|
/* load from the temporal register */
|
|
|
|
fr = tr | VT_LVAL;
|
|
|
|
}
|
2009-07-19 00:05:49 +04:00
|
|
|
#endif
|
2009-04-12 19:27:04 +04:00
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
v = fr & VT_VALMASK;
|
|
|
|
if (fr & VT_LVAL) {
|
2009-12-20 04:19:51 +03:00
|
|
|
int b, ll;
|
2008-12-01 21:19:25 +03:00
|
|
|
if (v == VT_LLOCAL) {
|
|
|
|
v1.type.t = VT_PTR;
|
|
|
|
v1.r = VT_LOCAL | VT_LVAL;
|
2015-11-17 22:09:35 +03:00
|
|
|
v1.c.i = fc;
|
2008-12-01 21:19:25 +03:00
|
|
|
fr = r;
|
fix a subtle x86-64 calling bug
I ran into an issue playing with tinycc, and tracked it down to a rather
weird assumption in the function calling code. This breaks only when
varargs and float/double arguments are combined, I think, and only when
calling GCC-generated (or non-TinyCC, at least) code. The problem is we
sometimes generate code like this:
804a468: 4c 89 d9 mov %r11,%rcx
804a46b: b8 01 00 00 00 mov $0x1,%eax
804a470: 48 8b 45 c0 mov -0x40(%rbp),%rax
804a474: 4c 8b 18 mov (%rax),%r11
804a477: 41 ff d3 callq *%r11
for a function call. Note how $eax is first set to the correct value,
then clobbered when we try to load the function pointer into R11. With
the patch, the code generated is:
804a468: 4c 89 d9 mov %r11,%rcx
804a46b: b8 01 00 00 00 mov $0x1,%eax
804a470: 4c 8b 5d c0 mov -0x40(%rbp),%r11
804a474: 4d 8b 1b mov (%r11),%r11
804a477: 41 ff d3 callq *%r11
which is correct.
This becomes an issue when get_reg(RC_INT) is modified not always to
return %rax after a save_regs(0), because then another register (%ecx,
say) is clobbered, and the function passed an invalid argument.
A rather convoluted test case that generates the above code is
included. Please note that the test will not cause a failure because
TinyCC code ignores the %rax argument, but it will cause incorrect
behavior when combined with GCC code, which might wrongly fail to save
XMM registers and cause data corruption.
2015-04-23 21:08:28 +03:00
|
|
|
if (!(reg_classes[fr] & (RC_INT|RC_R11)))
|
2012-03-05 23:19:28 +04:00
|
|
|
fr = get_reg(RC_INT);
|
|
|
|
load(fr, &v1);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
2009-12-20 04:19:51 +03:00
|
|
|
ll = 0;
|
2016-08-08 21:18:11 +03:00
|
|
|
/* Like GCC we can load from small enough properly sized
|
|
|
|
structs and unions as well.
|
|
|
|
XXX maybe move to generic operand handling, but should
|
|
|
|
occur only with asm, so tccasm.c might also be a better place */
|
|
|
|
if ((ft & VT_BTYPE) == VT_STRUCT) {
|
|
|
|
int align;
|
|
|
|
switch (type_size(&sv->type, &align)) {
|
|
|
|
case 1: ft = VT_BYTE; break;
|
|
|
|
case 2: ft = VT_SHORT; break;
|
|
|
|
case 4: ft = VT_INT; break;
|
|
|
|
case 8: ft = VT_LLONG; break;
|
|
|
|
default:
|
|
|
|
tcc_error("invalid aggregate type for register load");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
if ((ft & VT_BTYPE) == VT_FLOAT) {
|
2013-04-19 21:31:24 +04:00
|
|
|
b = 0x6e0f66;
|
|
|
|
r = REG_VALUE(r); /* movd */
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if ((ft & VT_BTYPE) == VT_DOUBLE) {
|
2013-04-19 18:33:16 +04:00
|
|
|
b = 0x7e0ff3; /* movq */
|
2013-04-19 21:31:24 +04:00
|
|
|
r = REG_VALUE(r);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
|
2009-12-20 04:19:51 +03:00
|
|
|
b = 0xdb, r = 5; /* fldt */
|
2013-06-14 18:18:16 +04:00
|
|
|
} else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
|
2009-12-20 04:19:51 +03:00
|
|
|
b = 0xbe0f; /* movsbl */
|
|
|
|
} else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
|
|
|
|
b = 0xb60f; /* movzbl */
|
|
|
|
} else if ((ft & VT_TYPE) == VT_SHORT) {
|
|
|
|
b = 0xbf0f; /* movswl */
|
|
|
|
} else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
|
|
|
|
b = 0xb70f; /* movzwl */
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
2013-04-19 18:33:16 +04:00
|
|
|
assert(((ft & VT_BTYPE) == VT_INT) || ((ft & VT_BTYPE) == VT_LLONG)
|
|
|
|
|| ((ft & VT_BTYPE) == VT_PTR) || ((ft & VT_BTYPE) == VT_ENUM)
|
|
|
|
|| ((ft & VT_BTYPE) == VT_FUNC));
|
2009-12-20 04:19:51 +03:00
|
|
|
ll = is64_type(ft);
|
|
|
|
b = 0x8b;
|
|
|
|
}
|
|
|
|
if (ll) {
|
|
|
|
gen_modrm64(b, r, fr, sv->sym, fc);
|
|
|
|
} else {
|
|
|
|
orex(ll, fr, r, b);
|
|
|
|
gen_modrm(r, fr, sv->sym, fc);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (v == VT_CONST) {
|
2009-07-19 00:07:03 +04:00
|
|
|
if (fr & VT_SYM) {
|
|
|
|
#ifdef TCC_TARGET_PE
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1,0,r,0x8d);
|
2009-07-19 00:07:03 +04:00
|
|
|
o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
|
|
|
|
gen_addrpc32(fr, sv->sym, fc);
|
|
|
|
#else
|
|
|
|
if (sv->sym->type.t & VT_STATIC) {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1,0,r,0x8d);
|
2009-07-19 00:07:03 +04:00
|
|
|
o(0x05 + REG_VALUE(r) * 8); /* lea xx(%rip), r */
|
|
|
|
gen_addrpc32(fr, sv->sym, fc);
|
|
|
|
} else {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1,0,r,0x8b);
|
2009-07-19 00:07:03 +04:00
|
|
|
o(0x05 + REG_VALUE(r) * 8); /* mov xx(%rip), r */
|
2012-04-15 21:29:45 +04:00
|
|
|
gen_gotpcrel(r, sv->sym, fc);
|
2009-07-19 00:07:03 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else if (is64_type(ft)) {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
|
2015-11-17 22:09:35 +03:00
|
|
|
gen_le64(sv->c.i);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(0,r,0, 0xb8 + REG_VALUE(r)); /* mov $xx, r */
|
2009-07-19 00:07:03 +04:00
|
|
|
gen_le32(fc);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
} else if (v == VT_LOCAL) {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1,0,r,0x8d); /* lea xxx(%ebp), r */
|
2008-12-01 21:19:25 +03:00
|
|
|
gen_modrm(r, VT_LOCAL, sv->sym, fc);
|
|
|
|
} else if (v == VT_CMP) {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(0,r,0,0);
|
2015-07-29 23:53:57 +03:00
|
|
|
if ((fc & ~0x100) != TOK_NE)
|
|
|
|
oad(0xb8 + REG_VALUE(r), 0); /* mov $0, r */
|
|
|
|
else
|
|
|
|
oad(0xb8 + REG_VALUE(r), 1); /* mov $1, r */
|
|
|
|
if (fc & 0x100)
|
|
|
|
{
|
|
|
|
/* This was a float compare. If the parity bit is
|
|
|
|
set the result was unordered, meaning false for everything
|
|
|
|
except TOK_NE, and true for TOK_NE. */
|
|
|
|
fc &= ~0x100;
|
|
|
|
o(0x037a + (REX_BASE(r) << 8));
|
|
|
|
}
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(0,r,0, 0x0f); /* setxx %br */
|
2008-12-01 21:19:25 +03:00
|
|
|
o(fc);
|
2009-12-19 02:06:41 +03:00
|
|
|
o(0xc0 + REG_VALUE(r));
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (v == VT_JMP || v == VT_JMPI) {
|
|
|
|
t = v & 1;
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(0,r,0,0);
|
2009-12-19 02:06:41 +03:00
|
|
|
oad(0xb8 + REG_VALUE(r), t); /* mov $1, r */
|
|
|
|
o(0x05eb + (REX_BASE(r) << 8)); /* jmp after */
|
2008-12-01 21:19:25 +03:00
|
|
|
gsym(fc);
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(0,r,0,0);
|
2009-12-19 02:06:41 +03:00
|
|
|
oad(0xb8 + REG_VALUE(r), t ^ 1); /* mov $0, r */
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (v != r) {
|
2013-04-24 05:19:15 +04:00
|
|
|
if ((r >= TREG_XMM0) && (r <= TREG_XMM7)) {
|
2013-04-19 18:33:16 +04:00
|
|
|
if (v == TREG_ST0) {
|
|
|
|
/* gen_cvt_ftof(VT_DOUBLE); */
|
|
|
|
o(0xf0245cdd); /* fstpl -0x10(%rsp) */
|
|
|
|
/* movsd -0x10(%rsp),%xmmN */
|
|
|
|
o(0x100ff2);
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x44 + REG_VALUE(r)*8); /* %xmmN */
|
2013-04-19 18:33:16 +04:00
|
|
|
o(0xf024);
|
|
|
|
} else {
|
2013-04-24 05:19:15 +04:00
|
|
|
assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
|
2013-04-19 18:33:16 +04:00
|
|
|
if ((ft & VT_BTYPE) == VT_FLOAT) {
|
|
|
|
o(0x100ff3);
|
|
|
|
} else {
|
|
|
|
assert((ft & VT_BTYPE) == VT_DOUBLE);
|
|
|
|
o(0x100ff2);
|
|
|
|
}
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8);
|
2013-04-19 18:33:16 +04:00
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (r == TREG_ST0) {
|
2014-03-08 14:36:02 +04:00
|
|
|
assert((v >= TREG_XMM0) && (v <= TREG_XMM7));
|
2008-12-01 21:19:25 +03:00
|
|
|
/* gen_cvt_ftof(VT_LDOUBLE); */
|
2013-04-19 21:31:24 +04:00
|
|
|
/* movsd %xmmN,-0x10(%rsp) */
|
2013-04-19 18:33:16 +04:00
|
|
|
o(0x110ff2);
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x44 + REG_VALUE(r)*8); /* %xmmN */
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xf024);
|
|
|
|
o(0xf02444dd); /* fldl -0x10(%rsp) */
|
|
|
|
} else {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1,r,v, 0x89);
|
2009-12-19 02:06:41 +03:00
|
|
|
o(0xc0 + REG_VALUE(r) + REG_VALUE(v) * 8); /* mov v, r */
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store register 'r' in lvalue 'v' */
|
|
|
|
void store(int r, SValue *v)
|
|
|
|
{
|
|
|
|
int fr, bt, ft, fc;
|
|
|
|
int op64 = 0;
|
2009-04-12 19:27:04 +04:00
|
|
|
/* store the REX prefix in this variable when PIC is enabled */
|
|
|
|
int pic = 0;
|
2008-12-01 21:19:25 +03:00
|
|
|
|
2009-12-20 00:08:52 +03:00
|
|
|
#ifdef TCC_TARGET_PE
|
2010-01-14 22:55:51 +03:00
|
|
|
SValue v2;
|
|
|
|
v = pe_getimport(v, &v2);
|
2009-12-20 00:08:52 +03:00
|
|
|
#endif
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
ft = v->type.t;
|
2015-11-17 22:09:35 +03:00
|
|
|
fc = v->c.i;
|
2008-12-01 21:19:25 +03:00
|
|
|
fr = v->r & VT_VALMASK;
|
2016-03-26 19:57:22 +03:00
|
|
|
ft &= ~(VT_VOLATILE | VT_CONSTANT);
|
2008-12-01 21:19:25 +03:00
|
|
|
bt = ft & VT_BTYPE;
|
2009-04-12 19:27:04 +04:00
|
|
|
|
2009-07-19 00:05:49 +04:00
|
|
|
#ifndef TCC_TARGET_PE
|
2009-04-12 19:27:04 +04:00
|
|
|
/* we need to access the variable via got */
|
|
|
|
if (fr == VT_CONST && (v->r & VT_SYM)) {
|
|
|
|
/* mov xx(%rip), %r11 */
|
|
|
|
o(0x1d8b4c);
|
2015-11-17 22:09:35 +03:00
|
|
|
gen_gotpcrel(TREG_R11, v->sym, v->c.i);
|
2009-04-12 19:27:04 +04:00
|
|
|
pic = is64_type(bt) ? 0x49 : 0x41;
|
|
|
|
}
|
2009-07-19 00:05:49 +04:00
|
|
|
#endif
|
2009-04-12 19:27:04 +04:00
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* XXX: incorrect if float reg to reg */
|
|
|
|
if (bt == VT_FLOAT) {
|
2009-04-12 19:27:04 +04:00
|
|
|
o(0x66);
|
|
|
|
o(pic);
|
|
|
|
o(0x7e0f); /* movd */
|
2013-04-19 21:31:24 +04:00
|
|
|
r = REG_VALUE(r);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (bt == VT_DOUBLE) {
|
2009-04-12 19:27:04 +04:00
|
|
|
o(0x66);
|
|
|
|
o(pic);
|
|
|
|
o(0xd60f); /* movq */
|
2013-04-19 21:31:24 +04:00
|
|
|
r = REG_VALUE(r);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (bt == VT_LDOUBLE) {
|
|
|
|
o(0xc0d9); /* fld %st(0) */
|
2009-04-12 19:27:04 +04:00
|
|
|
o(pic);
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xdb); /* fstpt */
|
|
|
|
r = 7;
|
|
|
|
} else {
|
|
|
|
if (bt == VT_SHORT)
|
|
|
|
o(0x66);
|
2009-04-12 19:27:04 +04:00
|
|
|
o(pic);
|
2008-12-01 21:19:25 +03:00
|
|
|
if (bt == VT_BYTE || bt == VT_BOOL)
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(0, 0, r, 0x88);
|
2008-12-01 21:19:25 +03:00
|
|
|
else if (is64_type(bt))
|
|
|
|
op64 = 0x89;
|
|
|
|
else
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(0, 0, r, 0x89);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
2009-04-12 19:27:04 +04:00
|
|
|
if (pic) {
|
|
|
|
/* xxx r, (%r11) where xxx is mov, movq, fld, or etc */
|
|
|
|
if (op64)
|
|
|
|
o(op64);
|
|
|
|
o(3 + (r << 3));
|
|
|
|
} else if (op64) {
|
2009-12-19 02:06:41 +03:00
|
|
|
if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
|
2008-12-01 21:19:25 +03:00
|
|
|
gen_modrm64(op64, r, v->r, v->sym, fc);
|
|
|
|
} else if (fr != r) {
|
|
|
|
/* XXX: don't we really come here? */
|
|
|
|
abort();
|
|
|
|
o(0xc0 + fr + r * 8); /* mov r, fr */
|
|
|
|
}
|
|
|
|
} else {
|
2009-12-19 02:06:41 +03:00
|
|
|
if (fr == VT_CONST || fr == VT_LOCAL || (v->r & VT_LVAL)) {
|
2008-12-01 21:19:25 +03:00
|
|
|
gen_modrm(r, v->r, v->sym, fc);
|
|
|
|
} else if (fr != r) {
|
|
|
|
/* XXX: don't we really come here? */
|
|
|
|
abort();
|
|
|
|
o(0xc0 + fr + r * 8); /* mov r, fr */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 'is_jmp' is '1' if it is a jump */
|
|
|
|
static void gcall_or_jmp(int is_jmp)
|
|
|
|
{
|
|
|
|
int r;
|
2015-04-23 20:30:16 +03:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
|
2015-11-17 22:09:35 +03:00
|
|
|
((vtop->r & VT_SYM) || (vtop->c.i-4) == (int)(vtop->c.i-4))) {
|
2008-12-01 21:19:25 +03:00
|
|
|
/* constant case */
|
|
|
|
if (vtop->r & VT_SYM) {
|
|
|
|
/* relocation case */
|
2014-05-08 19:32:29 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
2016-05-10 00:09:55 +03:00
|
|
|
greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PC32, (int)(vtop->c.i-4));
|
2014-05-08 19:32:29 +04:00
|
|
|
#else
|
2016-05-10 00:09:55 +03:00
|
|
|
greloca(cur_text_section, vtop->sym, ind + 1, R_X86_64_PLT32, (int)(vtop->c.i-4));
|
2014-05-08 19:32:29 +04:00
|
|
|
#endif
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
|
|
|
/* put an empty PC32 relocation */
|
2016-05-10 00:09:55 +03:00
|
|
|
put_elf_reloca(symtab_section, cur_text_section,
|
|
|
|
ind + 1, R_X86_64_PC32, 0, (int)(vtop->c.i-4));
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
2016-05-10 00:09:55 +03:00
|
|
|
oad(0xe8 + is_jmp, 0); /* call/jmp im */
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
|
|
|
/* otherwise, indirect call */
|
|
|
|
r = TREG_R11;
|
|
|
|
load(r, vtop);
|
|
|
|
o(0x41); /* REX */
|
|
|
|
o(0xff); /* call/jmp *r */
|
|
|
|
o(0xd0 + REG_VALUE(r) + (is_jmp << 4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 15:17:22 +03:00
|
|
|
#if defined(CONFIG_TCC_BCHECK)
|
|
|
|
#ifndef TCC_TARGET_PE
|
|
|
|
static addr_t func_bound_offset;
|
|
|
|
static unsigned long func_bound_ind;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void gen_static_call(int v)
|
|
|
|
{
|
|
|
|
Sym *sym = external_global_sym(v, &func_old_type, 0);
|
2016-05-10 00:09:55 +03:00
|
|
|
oad(0xe8, 0);
|
|
|
|
greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
|
2015-04-10 15:17:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a bounded pointer addition */
|
|
|
|
ST_FUNC void gen_bounded_ptr_add(void)
|
|
|
|
{
|
|
|
|
/* save all temporary registers */
|
|
|
|
save_regs(0);
|
|
|
|
|
|
|
|
/* prepare fast x86_64 function call */
|
|
|
|
gv(RC_RAX);
|
|
|
|
o(0xc68948); // mov %rax,%rsi ## second arg in %rsi, this must be size
|
|
|
|
vtop--;
|
|
|
|
|
|
|
|
gv(RC_RAX);
|
|
|
|
o(0xc78948); // mov %rax,%rdi ## first arg in %rdi, this must be ptr
|
|
|
|
vtop--;
|
|
|
|
|
|
|
|
/* do a fast function call */
|
|
|
|
gen_static_call(TOK___bound_ptr_add);
|
|
|
|
|
|
|
|
/* returned pointer is in rax */
|
|
|
|
vtop++;
|
|
|
|
vtop->r = TREG_RAX | VT_BOUNDED;
|
|
|
|
|
|
|
|
|
|
|
|
/* relocation offset of the bounding function call point */
|
2015-11-17 22:09:35 +03:00
|
|
|
vtop->c.i = (cur_text_section->reloc->data_offset - sizeof(ElfW(Rela)));
|
2015-04-10 15:17:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* patch pointer addition in vtop so that pointer dereferencing is
|
|
|
|
also tested */
|
|
|
|
ST_FUNC void gen_bounded_ptr_deref(void)
|
|
|
|
{
|
|
|
|
addr_t func;
|
|
|
|
int size, align;
|
|
|
|
ElfW(Rela) *rel;
|
|
|
|
Sym *sym;
|
|
|
|
|
|
|
|
size = 0;
|
|
|
|
/* XXX: put that code in generic part of tcc */
|
|
|
|
if (!is_float(vtop->type.t)) {
|
|
|
|
if (vtop->r & VT_LVAL_BYTE)
|
|
|
|
size = 1;
|
|
|
|
else if (vtop->r & VT_LVAL_SHORT)
|
|
|
|
size = 2;
|
|
|
|
}
|
|
|
|
if (!size)
|
|
|
|
size = type_size(&vtop->type, &align);
|
|
|
|
switch(size) {
|
|
|
|
case 1: func = TOK___bound_ptr_indir1; break;
|
|
|
|
case 2: func = TOK___bound_ptr_indir2; break;
|
|
|
|
case 4: func = TOK___bound_ptr_indir4; break;
|
|
|
|
case 8: func = TOK___bound_ptr_indir8; break;
|
|
|
|
case 12: func = TOK___bound_ptr_indir12; break;
|
|
|
|
case 16: func = TOK___bound_ptr_indir16; break;
|
|
|
|
default:
|
|
|
|
tcc_error("unhandled size when dereferencing bounded pointer");
|
|
|
|
func = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sym = external_global_sym(func, &func_old_type, 0);
|
|
|
|
if (!sym->c)
|
|
|
|
put_extern_sym(sym, NULL, 0, 0);
|
|
|
|
|
|
|
|
/* patch relocation */
|
|
|
|
/* XXX: find a better solution ? */
|
|
|
|
|
2015-11-17 22:09:35 +03:00
|
|
|
rel = (ElfW(Rela) *)(cur_text_section->reloc->data + vtop->c.i);
|
2015-04-10 15:17:22 +03:00
|
|
|
rel->r_info = ELF64_R_INFO(sym->c, ELF64_R_TYPE(rel->r_info));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-19 00:05:49 +04:00
|
|
|
#ifdef TCC_TARGET_PE
|
2009-12-20 00:40:28 +03:00
|
|
|
|
2009-07-19 00:05:49 +04:00
|
|
|
#define REGN 4
|
2013-04-24 05:19:15 +04:00
|
|
|
static const uint8_t arg_regs[REGN] = {
|
2009-07-19 00:05:49 +04:00
|
|
|
TREG_RCX, TREG_RDX, TREG_R8, TREG_R9
|
|
|
|
};
|
2009-12-20 00:40:28 +03:00
|
|
|
|
2013-04-24 05:19:15 +04:00
|
|
|
/* Prepare arguments in R10 and R11 rather than RCX and RDX
|
|
|
|
because gv() will not ever use these */
|
|
|
|
static int arg_prepare_reg(int idx) {
|
|
|
|
if (idx == 0 || idx == 1)
|
|
|
|
/* idx=0: r10, idx=1: r11 */
|
|
|
|
return idx + 10;
|
|
|
|
else
|
|
|
|
return arg_regs[idx];
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:40:28 +03:00
|
|
|
static int func_scratch;
|
|
|
|
|
|
|
|
/* Generate function call. The function address is pushed first, then
|
|
|
|
all the parameters in call order. This functions pops all the
|
|
|
|
parameters and the function address. */
|
|
|
|
|
|
|
|
void gen_offs_sp(int b, int r, int d)
|
|
|
|
{
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(1,0,r & 0x100 ? 0 : r, b);
|
2009-12-20 00:40:28 +03:00
|
|
|
if (d == (char)d) {
|
2009-12-20 04:19:51 +03:00
|
|
|
o(0x2444 | (REG_VALUE(r) << 3));
|
2009-12-20 00:40:28 +03:00
|
|
|
g(d);
|
|
|
|
} else {
|
2009-12-20 04:19:51 +03:00
|
|
|
o(0x2484 | (REG_VALUE(r) << 3));
|
2009-12-20 00:40:28 +03:00
|
|
|
gen_le32(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-22 05:27:15 +04:00
|
|
|
/* Return the number of registers needed to return the struct, or 0 if
|
|
|
|
returning via struct pointer. */
|
2015-07-29 23:57:41 +03:00
|
|
|
ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
|
2013-04-30 02:33:34 +04:00
|
|
|
{
|
2013-04-24 05:19:15 +04:00
|
|
|
int size, align;
|
2015-03-09 02:19:59 +03:00
|
|
|
*regsize = 8;
|
2013-04-30 02:33:34 +04:00
|
|
|
*ret_align = 1; // Never have to re-align return values for x86-64
|
2013-04-24 05:19:15 +04:00
|
|
|
size = type_size(vt, &align);
|
|
|
|
ret->ref = NULL;
|
|
|
|
if (size > 8) {
|
2015-07-29 23:57:41 +03:00
|
|
|
return 0;
|
2013-04-24 05:19:15 +04:00
|
|
|
} else if (size > 4) {
|
|
|
|
ret->t = VT_LLONG;
|
2015-07-29 23:57:41 +03:00
|
|
|
return 1;
|
2013-04-24 05:19:15 +04:00
|
|
|
} else if (size > 2) {
|
|
|
|
ret->t = VT_INT;
|
2015-07-29 23:57:41 +03:00
|
|
|
return 1;
|
2013-04-24 05:19:15 +04:00
|
|
|
} else if (size > 1) {
|
|
|
|
ret->t = VT_SHORT;
|
2015-07-29 23:57:41 +03:00
|
|
|
return 1;
|
2013-04-24 05:19:15 +04:00
|
|
|
} else {
|
|
|
|
ret->t = VT_BYTE;
|
2015-07-29 23:57:41 +03:00
|
|
|
return 1;
|
2013-04-24 05:19:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-24 17:36:04 +04:00
|
|
|
static int is_sse_float(int t) {
|
|
|
|
int bt;
|
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
return bt == VT_DOUBLE || bt == VT_FLOAT;
|
|
|
|
}
|
|
|
|
|
2013-04-24 05:19:15 +04:00
|
|
|
int gfunc_arg_size(CType *type) {
|
2013-04-30 02:33:34 +04:00
|
|
|
int align;
|
2013-04-24 05:19:15 +04:00
|
|
|
if (type->t & (VT_ARRAY|VT_BITFIELD))
|
|
|
|
return 8;
|
|
|
|
return type_size(type, &align);
|
2013-04-19 03:40:48 +04:00
|
|
|
}
|
|
|
|
|
2009-12-20 00:40:28 +03:00
|
|
|
void gfunc_call(int nb_args)
|
|
|
|
{
|
2013-04-24 05:19:15 +04:00
|
|
|
int size, r, args_size, i, d, bt, struct_size;
|
|
|
|
int arg;
|
2009-12-20 00:40:28 +03:00
|
|
|
|
2013-04-24 05:19:15 +04:00
|
|
|
args_size = (nb_args < REGN ? REGN : nb_args) * PTR_SIZE;
|
|
|
|
arg = nb_args;
|
2009-12-20 00:40:28 +03:00
|
|
|
|
|
|
|
/* for struct arguments, we need to call memcpy and the function
|
|
|
|
call breaks register passing arguments we are preparing.
|
|
|
|
So, we process arguments which will be passed by stack first. */
|
2010-12-17 15:15:01 +03:00
|
|
|
struct_size = args_size;
|
2009-12-20 00:40:28 +03:00
|
|
|
for(i = 0; i < nb_args; i++) {
|
2013-04-30 02:33:34 +04:00
|
|
|
SValue *sv;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-30 02:33:34 +04:00
|
|
|
--arg;
|
|
|
|
sv = &vtop[-i];
|
2009-12-20 00:40:28 +03:00
|
|
|
bt = (sv->type.t & VT_BTYPE);
|
2013-04-24 05:19:15 +04:00
|
|
|
size = gfunc_arg_size(&sv->type);
|
|
|
|
|
|
|
|
if (size <= 8)
|
|
|
|
continue; /* arguments smaller than 8 bytes passed in registers or on stack */
|
|
|
|
|
2009-12-20 00:40:28 +03:00
|
|
|
if (bt == VT_STRUCT) {
|
|
|
|
/* align to stack align size */
|
2010-08-26 21:49:09 +04:00
|
|
|
size = (size + 15) & ~15;
|
2009-12-20 00:40:28 +03:00
|
|
|
/* generate structure store */
|
|
|
|
r = get_reg(RC_INT);
|
2010-12-17 15:15:01 +03:00
|
|
|
gen_offs_sp(0x8d, r, struct_size);
|
|
|
|
struct_size += size;
|
2009-12-20 00:40:28 +03:00
|
|
|
|
|
|
|
/* generate memcpy call */
|
|
|
|
vset(&sv->type, r | VT_LVAL, 0);
|
2009-12-20 00:41:26 +03:00
|
|
|
vpushv(sv);
|
2009-12-20 00:40:28 +03:00
|
|
|
vstore();
|
|
|
|
--vtop;
|
|
|
|
} else if (bt == VT_LDOUBLE) {
|
|
|
|
gv(RC_ST0);
|
2010-12-17 15:15:01 +03:00
|
|
|
gen_offs_sp(0xdb, 0x107, struct_size);
|
|
|
|
struct_size += 16;
|
2009-12-20 00:40:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 15:15:01 +03:00
|
|
|
if (func_scratch < struct_size)
|
|
|
|
func_scratch = struct_size;
|
2013-04-24 05:19:15 +04:00
|
|
|
|
|
|
|
arg = nb_args;
|
2010-12-17 15:15:01 +03:00
|
|
|
struct_size = args_size;
|
|
|
|
|
2009-12-20 00:40:28 +03:00
|
|
|
for(i = 0; i < nb_args; i++) {
|
2013-04-24 05:19:15 +04:00
|
|
|
--arg;
|
2009-12-20 00:40:28 +03:00
|
|
|
bt = (vtop->type.t & VT_BTYPE);
|
2010-12-17 15:15:01 +03:00
|
|
|
|
2013-04-24 05:19:15 +04:00
|
|
|
size = gfunc_arg_size(&vtop->type);
|
|
|
|
if (size > 8) {
|
2010-12-17 15:15:01 +03:00
|
|
|
/* align to stack align size */
|
|
|
|
size = (size + 15) & ~15;
|
2013-04-24 05:19:15 +04:00
|
|
|
if (arg >= REGN) {
|
|
|
|
d = get_reg(RC_INT);
|
2010-12-17 15:15:01 +03:00
|
|
|
gen_offs_sp(0x8d, d, struct_size);
|
2013-04-24 05:19:15 +04:00
|
|
|
gen_offs_sp(0x89, d, arg*8);
|
2010-12-17 15:15:01 +03:00
|
|
|
} else {
|
2013-04-24 05:19:15 +04:00
|
|
|
d = arg_prepare_reg(arg);
|
2010-12-17 15:15:01 +03:00
|
|
|
gen_offs_sp(0x8d, d, struct_size);
|
|
|
|
}
|
|
|
|
struct_size += size;
|
2009-12-20 00:40:28 +03:00
|
|
|
} else {
|
2013-04-24 05:19:15 +04:00
|
|
|
if (is_sse_float(vtop->type.t)) {
|
2016-10-03 06:20:21 +03:00
|
|
|
if (tcc_state->nosse)
|
|
|
|
tcc_error("SSE disabled");
|
2013-04-24 05:19:15 +04:00
|
|
|
gv(RC_XMM0); /* only use one float register */
|
|
|
|
if (arg >= REGN) {
|
|
|
|
/* movq %xmm0, j*8(%rsp) */
|
|
|
|
gen_offs_sp(0xd60f66, 0x100, arg*8);
|
|
|
|
} else {
|
|
|
|
/* movaps %xmm0, %xmmN */
|
|
|
|
o(0x280f);
|
|
|
|
o(0xc0 + (arg << 3));
|
|
|
|
d = arg_prepare_reg(arg);
|
|
|
|
/* mov %xmm0, %rxx */
|
|
|
|
o(0x66);
|
|
|
|
orex(1,d,0, 0x7e0f);
|
|
|
|
o(0xc0 + REG_VALUE(d));
|
|
|
|
}
|
2009-12-20 00:40:28 +03:00
|
|
|
} else {
|
2013-04-24 05:19:15 +04:00
|
|
|
if (bt == VT_STRUCT) {
|
|
|
|
vtop->type.ref = NULL;
|
|
|
|
vtop->type.t = size > 4 ? VT_LLONG : size > 2 ? VT_INT
|
|
|
|
: size > 1 ? VT_SHORT : VT_BYTE;
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-24 05:19:15 +04:00
|
|
|
r = gv(RC_INT);
|
|
|
|
if (arg >= REGN) {
|
|
|
|
gen_offs_sp(0x89, r, arg*8);
|
2009-12-19 02:06:41 +03:00
|
|
|
} else {
|
2013-04-24 05:19:15 +04:00
|
|
|
d = arg_prepare_reg(arg);
|
|
|
|
orex(1,d,r,0x89); /* mov */
|
|
|
|
o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
|
2009-12-20 00:40:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
}
|
2009-12-19 02:06:41 +03:00
|
|
|
save_regs(0);
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-24 05:19:15 +04:00
|
|
|
/* Copy R10 and R11 into RCX and RDX, respectively */
|
|
|
|
if (nb_args > 0) {
|
|
|
|
o(0xd1894c); /* mov %r10, %rcx */
|
|
|
|
if (nb_args > 1) {
|
|
|
|
o(0xda894c); /* mov %r11, %rdx */
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2009-12-20 00:40:28 +03:00
|
|
|
gcall_or_jmp(0);
|
2016-10-19 20:21:27 +03:00
|
|
|
/* other compilers don't clear the upper bits when returning char/short */
|
|
|
|
bt = vtop->type.ref->type.t & (VT_BTYPE | VT_UNSIGNED);
|
|
|
|
if (bt == (VT_BYTE | VT_UNSIGNED))
|
|
|
|
o(0xc0b60f); /* movzbl %al, %eax */
|
|
|
|
else if (bt == VT_BYTE)
|
|
|
|
o(0xc0be0f); /* movsbl %al, %eax */
|
|
|
|
else if (bt == VT_SHORT)
|
|
|
|
o(0x98); /* cwtl */
|
|
|
|
else if (bt == (VT_SHORT | VT_UNSIGNED))
|
|
|
|
o(0xc0b70f); /* movzbl %al, %eax */
|
|
|
|
#if 0 /* handled in gen_cast() */
|
|
|
|
else if (bt == VT_INT)
|
|
|
|
o(0x9848); /* cltq */
|
|
|
|
else if (bt == (VT_INT | VT_UNSIGNED))
|
|
|
|
o(0xc089); /* mov %eax,%eax */
|
|
|
|
#endif
|
2009-12-20 00:40:28 +03:00
|
|
|
vtop--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define FUNC_PROLOG_SIZE 11
|
|
|
|
|
|
|
|
/* generate function prolog of type 't' */
|
|
|
|
void gfunc_prolog(CType *func_type)
|
|
|
|
{
|
2013-04-24 05:19:15 +04:00
|
|
|
int addr, reg_param_index, bt, size;
|
2009-12-20 00:40:28 +03:00
|
|
|
Sym *sym;
|
|
|
|
CType *type;
|
|
|
|
|
|
|
|
func_ret_sub = 0;
|
|
|
|
func_scratch = 0;
|
|
|
|
loc = 0;
|
|
|
|
|
|
|
|
addr = PTR_SIZE * 2;
|
|
|
|
ind += FUNC_PROLOG_SIZE;
|
|
|
|
func_sub_sp_offset = ind;
|
|
|
|
reg_param_index = 0;
|
|
|
|
|
|
|
|
sym = func_type->ref;
|
|
|
|
|
|
|
|
/* if the function returns a structure, then add an
|
|
|
|
implicit pointer parameter */
|
|
|
|
func_vt = sym->type;
|
2014-01-06 18:27:39 +04:00
|
|
|
func_var = (sym->c == FUNC_ELLIPSIS);
|
2013-04-24 05:19:15 +04:00
|
|
|
size = gfunc_arg_size(&func_vt);
|
|
|
|
if (size > 8) {
|
2009-12-20 00:40:28 +03:00
|
|
|
gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
|
2013-04-24 05:19:15 +04:00
|
|
|
func_vc = addr;
|
2009-12-20 00:40:28 +03:00
|
|
|
reg_param_index++;
|
2013-04-24 05:19:15 +04:00
|
|
|
addr += 8;
|
2009-12-20 00:40:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* define parameters */
|
|
|
|
while ((sym = sym->next) != NULL) {
|
|
|
|
type = &sym->type;
|
|
|
|
bt = type->t & VT_BTYPE;
|
2013-04-24 05:19:15 +04:00
|
|
|
size = gfunc_arg_size(type);
|
|
|
|
if (size > 8) {
|
|
|
|
if (reg_param_index < REGN) {
|
|
|
|
gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
|
|
|
|
}
|
2010-12-17 15:15:01 +03:00
|
|
|
sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL | VT_REF, addr);
|
|
|
|
} else {
|
2013-04-24 05:19:15 +04:00
|
|
|
if (reg_param_index < REGN) {
|
|
|
|
/* save arguments passed by register */
|
|
|
|
if ((bt == VT_FLOAT) || (bt == VT_DOUBLE)) {
|
2016-10-03 06:20:21 +03:00
|
|
|
if (tcc_state->nosse)
|
|
|
|
tcc_error("SSE disabled");
|
2013-04-24 05:19:15 +04:00
|
|
|
o(0xd60f66); /* movq */
|
|
|
|
gen_modrm(reg_param_index, VT_LOCAL, NULL, addr);
|
|
|
|
} else {
|
|
|
|
gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
|
|
|
|
}
|
|
|
|
}
|
2010-12-17 15:15:01 +03:00
|
|
|
sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, addr);
|
|
|
|
}
|
2013-04-24 05:19:15 +04:00
|
|
|
addr += 8;
|
2009-12-20 00:40:28 +03:00
|
|
|
reg_param_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (reg_param_index < REGN) {
|
2013-04-24 05:19:15 +04:00
|
|
|
if (func_type->ref->c == FUNC_ELLIPSIS) {
|
2009-12-20 00:40:28 +03:00
|
|
|
gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr);
|
2013-04-24 05:19:15 +04:00
|
|
|
addr += 8;
|
|
|
|
}
|
2009-12-20 00:40:28 +03:00
|
|
|
reg_param_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate function epilog */
|
|
|
|
void gfunc_epilog(void)
|
|
|
|
{
|
|
|
|
int v, saved_ind;
|
|
|
|
|
|
|
|
o(0xc9); /* leave */
|
|
|
|
if (func_ret_sub == 0) {
|
|
|
|
o(0xc3); /* ret */
|
|
|
|
} else {
|
|
|
|
o(0xc2); /* ret n */
|
|
|
|
g(func_ret_sub);
|
|
|
|
g(func_ret_sub >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
saved_ind = ind;
|
|
|
|
ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
|
|
|
|
/* align local size to word & save local variables */
|
|
|
|
v = (func_scratch + -loc + 15) & -16;
|
|
|
|
|
|
|
|
if (v >= 4096) {
|
|
|
|
Sym *sym = external_global_sym(TOK___chkstk, &func_old_type, 0);
|
|
|
|
oad(0xb8, v); /* mov stacksize, %eax */
|
2016-05-10 00:09:55 +03:00
|
|
|
oad(0xe8, 0); /* call __chkstk, (does the stackframe too) */
|
|
|
|
greloca(cur_text_section, sym, ind-4, R_X86_64_PC32, -4);
|
2009-12-20 00:40:28 +03:00
|
|
|
o(0x90); /* fill for FUNC_PROLOG_SIZE = 11 bytes */
|
|
|
|
} else {
|
|
|
|
o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
|
|
|
|
o(0xec8148); /* sub rsp, stacksize */
|
|
|
|
gen_le32(v);
|
|
|
|
}
|
2011-07-14 21:09:49 +04:00
|
|
|
|
|
|
|
cur_text_section->data_offset = saved_ind;
|
|
|
|
pe_add_unwind_data(ind, saved_ind, v);
|
|
|
|
ind = cur_text_section->data_offset;
|
2009-12-20 00:40:28 +03:00
|
|
|
}
|
|
|
|
|
2009-07-19 00:05:49 +04:00
|
|
|
#else
|
2009-12-20 00:40:28 +03:00
|
|
|
|
|
|
|
static void gadd_sp(int val)
|
|
|
|
{
|
|
|
|
if (val == (char)val) {
|
|
|
|
o(0xc48348);
|
|
|
|
g(val);
|
|
|
|
} else {
|
|
|
|
oad(0xc48148, val); /* add $xxx, %rsp */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
typedef enum X86_64_Mode {
|
|
|
|
x86_64_mode_none,
|
|
|
|
x86_64_mode_memory,
|
|
|
|
x86_64_mode_integer,
|
|
|
|
x86_64_mode_sse,
|
|
|
|
x86_64_mode_x87
|
|
|
|
} X86_64_Mode;
|
|
|
|
|
2014-01-07 17:57:07 +04:00
|
|
|
static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b)
|
|
|
|
{
|
2013-04-19 03:40:48 +04:00
|
|
|
if (a == b)
|
|
|
|
return a;
|
|
|
|
else if (a == x86_64_mode_none)
|
|
|
|
return b;
|
|
|
|
else if (b == x86_64_mode_none)
|
|
|
|
return a;
|
|
|
|
else if ((a == x86_64_mode_memory) || (b == x86_64_mode_memory))
|
|
|
|
return x86_64_mode_memory;
|
|
|
|
else if ((a == x86_64_mode_integer) || (b == x86_64_mode_integer))
|
|
|
|
return x86_64_mode_integer;
|
|
|
|
else if ((a == x86_64_mode_x87) || (b == x86_64_mode_x87))
|
|
|
|
return x86_64_mode_memory;
|
|
|
|
else
|
|
|
|
return x86_64_mode_sse;
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:57:41 +03:00
|
|
|
static X86_64_Mode classify_x86_64_inner(CType *ty)
|
2014-01-07 17:57:07 +04:00
|
|
|
{
|
2013-04-19 03:40:48 +04:00
|
|
|
X86_64_Mode mode;
|
|
|
|
Sym *f;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
switch (ty->t & VT_BTYPE) {
|
|
|
|
case VT_VOID: return x86_64_mode_none;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
case VT_INT:
|
|
|
|
case VT_BYTE:
|
|
|
|
case VT_SHORT:
|
|
|
|
case VT_LLONG:
|
|
|
|
case VT_BOOL:
|
|
|
|
case VT_PTR:
|
2013-04-19 18:33:16 +04:00
|
|
|
case VT_FUNC:
|
2013-04-19 03:40:48 +04:00
|
|
|
case VT_ENUM: return x86_64_mode_integer;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
case VT_FLOAT:
|
|
|
|
case VT_DOUBLE: return x86_64_mode_sse;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
case VT_LDOUBLE: return x86_64_mode_x87;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
case VT_STRUCT:
|
|
|
|
f = ty->ref;
|
|
|
|
|
|
|
|
mode = x86_64_mode_none;
|
2015-07-29 23:57:41 +03:00
|
|
|
for (f = f->next; f; f = f->next)
|
|
|
|
mode = classify_x86_64_merge(mode, classify_x86_64_inner(&f->type));
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
return mode;
|
|
|
|
}
|
2013-04-19 18:33:16 +04:00
|
|
|
assert(0);
|
2016-10-03 13:29:38 +03:00
|
|
|
return 0;
|
2013-04-19 03:40:48 +04:00
|
|
|
}
|
|
|
|
|
2015-07-29 23:57:41 +03:00
|
|
|
static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count)
|
2014-01-07 17:57:07 +04:00
|
|
|
{
|
2013-04-19 03:40:48 +04:00
|
|
|
X86_64_Mode mode;
|
2013-09-24 17:36:04 +04:00
|
|
|
int size, align, ret_t = 0;
|
2015-07-29 23:57:41 +03:00
|
|
|
|
2013-04-19 14:08:12 +04:00
|
|
|
if (ty->t & (VT_BITFIELD|VT_ARRAY)) {
|
2013-04-19 03:40:48 +04:00
|
|
|
*psize = 8;
|
2014-01-03 04:41:34 +04:00
|
|
|
*palign = 8;
|
2015-07-29 23:57:41 +03:00
|
|
|
*reg_count = 1;
|
2013-04-19 14:08:12 +04:00
|
|
|
ret_t = ty->t;
|
|
|
|
mode = x86_64_mode_integer;
|
|
|
|
} else {
|
|
|
|
size = type_size(ty, &align);
|
|
|
|
*psize = (size + 7) & ~7;
|
2013-04-26 19:42:12 +04:00
|
|
|
*palign = (align + 7) & ~7;
|
2015-07-29 23:57:41 +03:00
|
|
|
|
2013-04-19 14:08:12 +04:00
|
|
|
if (size > 16) {
|
|
|
|
mode = x86_64_mode_memory;
|
|
|
|
} else {
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_inner(ty);
|
|
|
|
switch (mode) {
|
|
|
|
case x86_64_mode_integer:
|
|
|
|
if (size > 8) {
|
|
|
|
*reg_count = 2;
|
|
|
|
ret_t = VT_QLONG;
|
2013-04-19 14:08:12 +04:00
|
|
|
} else {
|
2015-07-29 23:57:41 +03:00
|
|
|
*reg_count = 1;
|
|
|
|
ret_t = (size > 4) ? VT_LLONG : VT_INT;
|
2013-04-19 14:08:12 +04:00
|
|
|
}
|
2015-07-29 23:57:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case x86_64_mode_x87:
|
|
|
|
*reg_count = 1;
|
|
|
|
ret_t = VT_LDOUBLE;
|
|
|
|
break;
|
2013-04-19 14:08:12 +04:00
|
|
|
|
2015-07-29 23:57:41 +03:00
|
|
|
case x86_64_mode_sse:
|
|
|
|
if (size > 8) {
|
|
|
|
*reg_count = 2;
|
|
|
|
ret_t = VT_QFLOAT;
|
fix-mixed-struct (patch by Pip Cet)
Jsut for testing. It works for me (don't break anything)
Small fixes for x86_64-gen.c in "tccpp: fix issues, add tests"
are dropped in flavor of this patch.
Pip Cet:
Okay, here's a first patch that fixes the problem (but I've found
another bug, yet unfixed, in the process), though it's not
particularly pretty code (I tried hard to keep the changes to the
minimum necessary). If we decide to actually get rid of VT_QLONG and
VT_QFLOAT (please, can we?), there are some further simplifications in
tccgen.c that might offset some of the cost of this patch.
The idea is that an integer is no longer enough to describe how an
argument is stored in registers. There are a number of possibilities
(none, integer register, two integer registers, float register, two
float registers, integer register plus float register, float register
plus integer register), and instead of enumerating them I've
introduced a RegArgs type that stores the offsets for each of our
registers (for the other architectures, it's simply an int specifying
the number of registers). If someone strongly prefers an enum, we
could do that instead, but I believe this is a place where keeping
things general is worth it, because this way it should be doable to
add SSE or AVX support.
There is one line in the patch that looks suspicious:
} else {
addr = (addr + align - 1) & -align;
param_addr = addr;
addr += size;
- sse_param_index += reg_count;
}
break;
However, this actually fixes one half of a bug we have when calling a
function with eight double arguments "interrupted" by a two-double
structure after the seventh double argument:
f(double,double,double,double,double,double,double,struct { double
x,y; },double);
In this case, the last argument should be passed in %xmm7. This patch
fixes the problem in gfunc_prolog, but not the corresponding problem
in gfunc_call, which I'll try tackling next.
2015-05-14 07:32:24 +03:00
|
|
|
} else {
|
2015-07-29 23:57:41 +03:00
|
|
|
*reg_count = 1;
|
|
|
|
ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT;
|
2013-04-19 14:08:12 +04:00
|
|
|
}
|
2015-07-29 23:57:41 +03:00
|
|
|
break;
|
|
|
|
default: break; /* nothing to be done for x86_64_mode_memory and x86_64_mode_none*/
|
2013-04-19 14:08:12 +04:00
|
|
|
}
|
|
|
|
}
|
2013-04-19 03:40:48 +04:00
|
|
|
}
|
2015-07-29 23:57:41 +03:00
|
|
|
|
2013-04-19 14:08:12 +04:00
|
|
|
if (ret) {
|
|
|
|
ret->ref = NULL;
|
|
|
|
ret->t = ret_t;
|
2013-04-19 03:40:48 +04:00
|
|
|
}
|
2015-07-29 23:57:41 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2014-01-07 17:57:07 +04:00
|
|
|
ST_FUNC int classify_x86_64_va_arg(CType *ty)
|
|
|
|
{
|
2013-04-19 14:08:12 +04:00
|
|
|
/* This definition must be synced with stdarg.h */
|
|
|
|
enum __va_arg_type {
|
|
|
|
__va_gen_reg, __va_float_reg, __va_stack
|
|
|
|
};
|
2015-07-29 23:57:41 +03:00
|
|
|
int size, align, reg_count;
|
|
|
|
X86_64_Mode mode = classify_x86_64_arg(ty, NULL, &size, &align, ®_count);
|
2013-04-19 03:40:48 +04:00
|
|
|
switch (mode) {
|
2013-04-19 14:08:12 +04:00
|
|
|
default: return __va_stack;
|
|
|
|
case x86_64_mode_integer: return __va_gen_reg;
|
|
|
|
case x86_64_mode_sse: return __va_float_reg;
|
2013-04-19 03:40:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:57:41 +03:00
|
|
|
/* Return the number of registers needed to return the struct, or 0 if
|
|
|
|
returning via struct pointer. */
|
|
|
|
ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
|
fix-mixed-struct (patch by Pip Cet)
Jsut for testing. It works for me (don't break anything)
Small fixes for x86_64-gen.c in "tccpp: fix issues, add tests"
are dropped in flavor of this patch.
Pip Cet:
Okay, here's a first patch that fixes the problem (but I've found
another bug, yet unfixed, in the process), though it's not
particularly pretty code (I tried hard to keep the changes to the
minimum necessary). If we decide to actually get rid of VT_QLONG and
VT_QFLOAT (please, can we?), there are some further simplifications in
tccgen.c that might offset some of the cost of this patch.
The idea is that an integer is no longer enough to describe how an
argument is stored in registers. There are a number of possibilities
(none, integer register, two integer registers, float register, two
float registers, integer register plus float register, float register
plus integer register), and instead of enumerating them I've
introduced a RegArgs type that stores the offsets for each of our
registers (for the other architectures, it's simply an int specifying
the number of registers). If someone strongly prefers an enum, we
could do that instead, but I believe this is a place where keeping
things general is worth it, because this way it should be doable to
add SSE or AVX support.
There is one line in the patch that looks suspicious:
} else {
addr = (addr + align - 1) & -align;
param_addr = addr;
addr += size;
- sse_param_index += reg_count;
}
break;
However, this actually fixes one half of a bug we have when calling a
function with eight double arguments "interrupted" by a two-double
structure after the seventh double argument:
f(double,double,double,double,double,double,double,struct { double
x,y; },double);
In this case, the last argument should be passed in %xmm7. This patch
fixes the problem in gfunc_prolog, but not the corresponding problem
in gfunc_call, which I'll try tackling next.
2015-05-14 07:32:24 +03:00
|
|
|
{
|
2015-07-29 23:57:41 +03:00
|
|
|
int size, align, reg_count;
|
2013-04-19 03:40:48 +04:00
|
|
|
*ret_align = 1; // Never have to re-align return values for x86-64
|
2015-03-09 02:19:59 +03:00
|
|
|
*regsize = 8;
|
2015-07-29 23:57:41 +03:00
|
|
|
return (classify_x86_64_arg(vt, ret, &size, &align, ®_count) != x86_64_mode_memory);
|
2013-04-19 03:40:48 +04:00
|
|
|
}
|
|
|
|
|
2009-07-19 00:05:49 +04:00
|
|
|
#define REGN 6
|
2009-07-19 00:07:42 +04:00
|
|
|
static const uint8_t arg_regs[REGN] = {
|
2008-12-01 21:19:25 +03:00
|
|
|
TREG_RDI, TREG_RSI, TREG_RDX, TREG_RCX, TREG_R8, TREG_R9
|
|
|
|
};
|
2009-07-19 00:05:49 +04:00
|
|
|
|
2013-04-19 18:33:16 +04:00
|
|
|
static int arg_prepare_reg(int idx) {
|
|
|
|
if (idx == 2 || idx == 3)
|
|
|
|
/* idx=2: r10, idx=3: r11 */
|
|
|
|
return idx + 8;
|
|
|
|
else
|
|
|
|
return arg_regs[idx];
|
|
|
|
}
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* Generate function call. The function address is pushed first, then
|
|
|
|
all the parameters in call order. This functions pops all the
|
|
|
|
parameters and the function address. */
|
|
|
|
void gfunc_call(int nb_args)
|
|
|
|
{
|
2013-04-19 03:40:48 +04:00
|
|
|
X86_64_Mode mode;
|
|
|
|
CType type;
|
2015-07-29 23:57:41 +03:00
|
|
|
int size, align, r, args_size, stack_adjust, run_start, run_end, i, reg_count;
|
2008-12-01 21:19:25 +03:00
|
|
|
int nb_reg_args = 0;
|
|
|
|
int nb_sse_args = 0;
|
2015-07-29 23:57:41 +03:00
|
|
|
int sse_reg, gen_reg;
|
2008-12-01 21:19:25 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
/* calculate the number of integer/float register arguments */
|
2015-07-29 23:57:41 +03:00
|
|
|
for(i = 0; i < nb_args; i++) {
|
|
|
|
mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, ®_count);
|
|
|
|
if (mode == x86_64_mode_sse)
|
|
|
|
nb_sse_args += reg_count;
|
|
|
|
else if (mode == x86_64_mode_integer)
|
|
|
|
nb_reg_args += reg_count;
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
|
2016-10-03 06:20:21 +03:00
|
|
|
if (nb_sse_args && tcc_state->nosse)
|
|
|
|
tcc_error("SSE disabled but floating point arguments passed");
|
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
/* arguments are collected in runs. Each run is a collection of 8-byte aligned arguments
|
|
|
|
and ended by a 16-byte aligned argument. This is because, from the point of view of
|
|
|
|
the callee, argument alignment is computed from the bottom up. */
|
2008-12-01 21:19:25 +03:00
|
|
|
/* for struct arguments, we need to call memcpy and the function
|
|
|
|
call breaks register passing arguments we are preparing.
|
|
|
|
So, we process arguments which will be passed by stack first. */
|
|
|
|
gen_reg = nb_reg_args;
|
|
|
|
sse_reg = nb_sse_args;
|
2013-04-26 19:42:12 +04:00
|
|
|
run_start = 0;
|
|
|
|
args_size = 0;
|
|
|
|
while (run_start != nb_args) {
|
|
|
|
int run_gen_reg = gen_reg, run_sse_reg = sse_reg;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
run_end = nb_args;
|
|
|
|
stack_adjust = 0;
|
|
|
|
for(i = run_start; (i < nb_args) && (run_end == nb_args); i++) {
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, ®_count);
|
|
|
|
switch (mode) {
|
|
|
|
case x86_64_mode_memory:
|
|
|
|
case x86_64_mode_x87:
|
|
|
|
stack_arg:
|
2013-04-26 19:42:12 +04:00
|
|
|
if (align == 16)
|
|
|
|
run_end = i;
|
2013-04-20 01:05:49 +04:00
|
|
|
else
|
2013-04-26 19:42:12 +04:00
|
|
|
stack_adjust += size;
|
2015-07-29 23:57:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case x86_64_mode_sse:
|
|
|
|
sse_reg -= reg_count;
|
|
|
|
if (sse_reg + reg_count > 8) goto stack_arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case x86_64_mode_integer:
|
|
|
|
gen_reg -= reg_count;
|
|
|
|
if (gen_reg + reg_count > REGN) goto stack_arg;
|
|
|
|
break;
|
|
|
|
default: break; /* nothing to be done for x86_64_mode_none */
|
2013-04-26 19:42:12 +04:00
|
|
|
}
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
gen_reg = run_gen_reg;
|
|
|
|
sse_reg = run_sse_reg;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
/* adjust stack to align SSE boundary */
|
|
|
|
if (stack_adjust &= 15) {
|
|
|
|
/* fetch cpu flag before the following sub will change the value */
|
|
|
|
if (vtop >= vstack && (vtop->r & VT_VALMASK) == VT_CMP)
|
|
|
|
gv(RC_INT);
|
|
|
|
|
|
|
|
stack_adjust = 16 - stack_adjust;
|
|
|
|
o(0x48);
|
|
|
|
oad(0xec81, stack_adjust); /* sub $xxx, %rsp */
|
|
|
|
args_size += stack_adjust;
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
for(i = run_start; i < run_end;) {
|
|
|
|
/* Swap argument to top, it will possibly be changed here,
|
|
|
|
and might use more temps. At the end of the loop we keep
|
|
|
|
in on the stack and swap it back to its original position
|
|
|
|
if it is a register. */
|
2015-07-29 23:57:41 +03:00
|
|
|
SValue tmp = vtop[0];
|
|
|
|
int arg_stored = 1;
|
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
vtop[0] = vtop[-i];
|
|
|
|
vtop[-i] = tmp;
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_arg(&vtop->type, NULL, &size, &align, ®_count);
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
switch (vtop->type.t & VT_BTYPE) {
|
|
|
|
case VT_STRUCT:
|
2015-07-29 23:57:41 +03:00
|
|
|
if (mode == x86_64_mode_sse) {
|
|
|
|
if (sse_reg > 8)
|
|
|
|
sse_reg -= reg_count;
|
|
|
|
else
|
|
|
|
arg_stored = 0;
|
|
|
|
} else if (mode == x86_64_mode_integer) {
|
|
|
|
if (gen_reg > REGN)
|
|
|
|
gen_reg -= reg_count;
|
|
|
|
else
|
|
|
|
arg_stored = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_stored) {
|
|
|
|
/* allocate the necessary size on stack */
|
|
|
|
o(0x48);
|
|
|
|
oad(0xec81, size); /* sub $xxx, %rsp */
|
|
|
|
/* generate structure store */
|
|
|
|
r = get_reg(RC_INT);
|
|
|
|
orex(1, r, 0, 0x89); /* mov %rsp, r */
|
|
|
|
o(0xe0 + REG_VALUE(r));
|
|
|
|
vset(&vtop->type, r | VT_LVAL, 0);
|
|
|
|
vswap();
|
|
|
|
vstore();
|
|
|
|
args_size += size;
|
|
|
|
}
|
2013-04-26 19:42:12 +04:00
|
|
|
break;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
case VT_LDOUBLE:
|
|
|
|
assert(0);
|
|
|
|
break;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
case VT_FLOAT:
|
|
|
|
case VT_DOUBLE:
|
2015-07-29 23:57:41 +03:00
|
|
|
assert(mode == x86_64_mode_sse);
|
|
|
|
if (sse_reg > 8) {
|
|
|
|
--sse_reg;
|
|
|
|
r = gv(RC_FLOAT);
|
|
|
|
o(0x50); /* push $rax */
|
|
|
|
/* movq %xmmN, (%rsp) */
|
|
|
|
o(0xd60f66);
|
|
|
|
o(0x04 + REG_VALUE(r)*8);
|
|
|
|
o(0x24);
|
|
|
|
args_size += size;
|
|
|
|
} else {
|
|
|
|
arg_stored = 0;
|
|
|
|
}
|
2013-04-26 19:42:12 +04:00
|
|
|
break;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
default:
|
2015-07-29 23:57:41 +03:00
|
|
|
assert(mode == x86_64_mode_integer);
|
2013-04-26 19:42:12 +04:00
|
|
|
/* simple type */
|
|
|
|
/* XXX: implicit cast ? */
|
2015-07-29 23:57:41 +03:00
|
|
|
if (gen_reg > REGN) {
|
|
|
|
--gen_reg;
|
|
|
|
r = gv(RC_INT);
|
|
|
|
orex(0,r,0,0x50 + REG_VALUE(r)); /* push r */
|
|
|
|
args_size += size;
|
|
|
|
} else {
|
|
|
|
arg_stored = 0;
|
|
|
|
}
|
2013-04-26 19:42:12 +04:00
|
|
|
break;
|
2013-04-20 01:05:49 +04:00
|
|
|
}
|
2015-07-29 23:57:41 +03:00
|
|
|
|
|
|
|
/* And swap the argument back to it's original position. */
|
2013-04-26 19:42:12 +04:00
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-i];
|
|
|
|
vtop[-i] = tmp;
|
|
|
|
|
2015-07-29 23:57:41 +03:00
|
|
|
if (arg_stored) {
|
|
|
|
vrotb(i+1);
|
|
|
|
assert((vtop->type.t == tmp.type.t) && (vtop->r == tmp.r));
|
|
|
|
vpop();
|
|
|
|
--nb_args;
|
|
|
|
--run_end;
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
2013-04-26 19:42:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* handle 16 byte aligned arguments at end of run */
|
|
|
|
run_start = i = run_end;
|
|
|
|
while (i < nb_args) {
|
|
|
|
/* Rotate argument to top since it will always be popped */
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_arg(&vtop[-i].type, NULL, &size, &align, ®_count);
|
2013-04-26 19:42:12 +04:00
|
|
|
if (align != 16)
|
|
|
|
break;
|
|
|
|
|
|
|
|
vrotb(i+1);
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
gv(RC_ST0);
|
|
|
|
oad(0xec8148, size); /* sub $xxx, %rsp */
|
|
|
|
o(0x7cdb); /* fstpt 0(%rsp) */
|
|
|
|
g(0x24);
|
|
|
|
g(0x00);
|
|
|
|
args_size += size;
|
|
|
|
} else {
|
|
|
|
assert(mode == x86_64_mode_memory);
|
|
|
|
|
2013-04-20 01:05:49 +04:00
|
|
|
/* allocate the necessary size on stack */
|
|
|
|
o(0x48);
|
|
|
|
oad(0xec81, size); /* sub $xxx, %rsp */
|
|
|
|
/* generate structure store */
|
|
|
|
r = get_reg(RC_INT);
|
|
|
|
orex(1, r, 0, 0x89); /* mov %rsp, r */
|
|
|
|
o(0xe0 + REG_VALUE(r));
|
|
|
|
vset(&vtop->type, r | VT_LVAL, 0);
|
|
|
|
vswap();
|
|
|
|
vstore();
|
|
|
|
args_size += size;
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-26 19:42:12 +04:00
|
|
|
vpop();
|
|
|
|
--nb_args;
|
2013-04-20 01:05:49 +04:00
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
2015-07-29 23:57:41 +03:00
|
|
|
|
2012-04-15 07:12:43 +04:00
|
|
|
/* XXX This should be superfluous. */
|
2009-12-20 22:33:21 +03:00
|
|
|
save_regs(0); /* save used temporary registers */
|
2009-12-20 04:19:51 +03:00
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* then, we prepare register passing arguments.
|
|
|
|
Note that we cannot set RDX and RCX in this loop because gv()
|
|
|
|
may break these temporary registers. Let's use R10 and R11
|
|
|
|
instead of them */
|
2013-04-20 01:05:49 +04:00
|
|
|
assert(gen_reg <= REGN);
|
|
|
|
assert(sse_reg <= 8);
|
2008-12-01 21:19:25 +03:00
|
|
|
for(i = 0; i < nb_args; i++) {
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_arg(&vtop->type, &type, &size, &align, ®_count);
|
2013-04-19 03:40:48 +04:00
|
|
|
/* Alter stack entry type so that gv() knows how to treat it */
|
2015-07-29 23:57:41 +03:00
|
|
|
vtop->type = type;
|
|
|
|
if (mode == x86_64_mode_sse) {
|
|
|
|
if (reg_count == 2) {
|
|
|
|
sse_reg -= 2;
|
|
|
|
gv(RC_FRET); /* Use pair load into xmm0 & xmm1 */
|
|
|
|
if (sse_reg) { /* avoid redundant movaps %xmm0, %xmm0 */
|
|
|
|
/* movaps %xmm0, %xmmN */
|
|
|
|
o(0x280f);
|
|
|
|
o(0xc0 + (sse_reg << 3));
|
|
|
|
/* movaps %xmm1, %xmmN */
|
|
|
|
o(0x280f);
|
|
|
|
o(0xc1 + ((sse_reg+1) << 3));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(reg_count == 1);
|
2013-04-24 05:19:15 +04:00
|
|
|
--sse_reg;
|
|
|
|
/* Load directly to register */
|
|
|
|
gv(RC_XMM0 << sse_reg);
|
2015-07-29 23:57:41 +03:00
|
|
|
}
|
|
|
|
} else if (mode == x86_64_mode_integer) {
|
|
|
|
/* simple type */
|
|
|
|
/* XXX: implicit cast ? */
|
|
|
|
int d;
|
|
|
|
gen_reg -= reg_count;
|
|
|
|
r = gv(RC_INT);
|
|
|
|
d = arg_prepare_reg(gen_reg);
|
|
|
|
orex(1,d,r,0x89); /* mov */
|
|
|
|
o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
|
|
|
|
if (reg_count == 2) {
|
|
|
|
d = arg_prepare_reg(gen_reg+1);
|
|
|
|
orex(1,d,vtop->r2,0x89); /* mov */
|
|
|
|
o(0xc0 + REG_VALUE(vtop->r2) * 8 + REG_VALUE(d));
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
}
|
2013-04-20 01:05:49 +04:00
|
|
|
assert(gen_reg == 0);
|
|
|
|
assert(sse_reg == 0);
|
2008-12-01 21:19:25 +03:00
|
|
|
|
2012-04-15 07:12:43 +04:00
|
|
|
/* We shouldn't have many operands on the stack anymore, but the
|
|
|
|
call address itself is still there, and it might be in %eax
|
|
|
|
(or edx/ecx) currently, which the below writes would clobber.
|
|
|
|
So evict all remaining operands here. */
|
|
|
|
save_regs(0);
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* Copy R10 and R11 into RDX and RCX, respectively */
|
|
|
|
if (nb_reg_args > 2) {
|
|
|
|
o(0xd2894c); /* mov %r10, %rdx */
|
|
|
|
if (nb_reg_args > 3) {
|
|
|
|
o(0xd9894c); /* mov %r11, %rcx */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 16:12:11 +03:00
|
|
|
if (vtop->type.ref->c != FUNC_NEW) /* implies FUNC_OLD or FUNC_ELLIPSIS */
|
|
|
|
oad(0xb8, nb_sse_args < 8 ? nb_sse_args : 8); /* mov nb_sse_args, %eax */
|
2008-12-01 21:19:25 +03:00
|
|
|
gcall_or_jmp(0);
|
|
|
|
if (args_size)
|
|
|
|
gadd_sp(args_size);
|
|
|
|
vtop--;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:40:28 +03:00
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
#define FUNC_PROLOG_SIZE 11
|
|
|
|
|
|
|
|
static void push_arg_reg(int i) {
|
|
|
|
loc -= 8;
|
|
|
|
gen_modrm64(0x89, arg_regs[i], VT_LOCAL, NULL, loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate function prolog of type 't' */
|
|
|
|
void gfunc_prolog(CType *func_type)
|
|
|
|
{
|
2013-04-19 03:40:48 +04:00
|
|
|
X86_64_Mode mode;
|
2015-07-29 23:57:41 +03:00
|
|
|
int i, addr, align, size, reg_count;
|
2013-09-24 17:36:04 +04:00
|
|
|
int param_addr = 0, reg_param_index, sse_param_index;
|
2008-12-01 21:19:25 +03:00
|
|
|
Sym *sym;
|
|
|
|
CType *type;
|
|
|
|
|
|
|
|
sym = func_type->ref;
|
|
|
|
addr = PTR_SIZE * 2;
|
|
|
|
loc = 0;
|
|
|
|
ind += FUNC_PROLOG_SIZE;
|
|
|
|
func_sub_sp_offset = ind;
|
2009-12-20 00:40:28 +03:00
|
|
|
func_ret_sub = 0;
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
if (func_type->ref->c == FUNC_ELLIPSIS) {
|
|
|
|
int seen_reg_num, seen_sse_num, seen_stack_size;
|
|
|
|
seen_reg_num = seen_sse_num = 0;
|
|
|
|
/* frame pointer and return address */
|
|
|
|
seen_stack_size = PTR_SIZE * 2;
|
|
|
|
/* count the number of seen parameters */
|
|
|
|
sym = func_type->ref;
|
|
|
|
while ((sym = sym->next) != NULL) {
|
|
|
|
type = &sym->type;
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_arg(type, NULL, &size, &align, ®_count);
|
2013-04-19 14:08:12 +04:00
|
|
|
switch (mode) {
|
|
|
|
default:
|
2013-04-26 19:42:12 +04:00
|
|
|
stack_arg:
|
|
|
|
seen_stack_size = ((seen_stack_size + align - 1) & -align) + size;
|
2013-04-19 14:08:12 +04:00
|
|
|
break;
|
2015-07-29 23:57:41 +03:00
|
|
|
|
2013-04-19 14:08:12 +04:00
|
|
|
case x86_64_mode_integer:
|
2015-07-29 23:57:41 +03:00
|
|
|
if (seen_reg_num + reg_count <= 8) {
|
|
|
|
seen_reg_num += reg_count;
|
|
|
|
} else {
|
2013-04-19 14:08:12 +04:00
|
|
|
seen_reg_num = 8;
|
2015-07-29 23:57:41 +03:00
|
|
|
goto stack_arg;
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
2015-07-29 23:57:41 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case x86_64_mode_sse:
|
|
|
|
if (seen_sse_num + reg_count <= 8) {
|
|
|
|
seen_sse_num += reg_count;
|
|
|
|
} else {
|
2013-04-19 14:08:12 +04:00
|
|
|
seen_sse_num = 8;
|
fix-mixed-struct (patch by Pip Cet)
Jsut for testing. It works for me (don't break anything)
Small fixes for x86_64-gen.c in "tccpp: fix issues, add tests"
are dropped in flavor of this patch.
Pip Cet:
Okay, here's a first patch that fixes the problem (but I've found
another bug, yet unfixed, in the process), though it's not
particularly pretty code (I tried hard to keep the changes to the
minimum necessary). If we decide to actually get rid of VT_QLONG and
VT_QFLOAT (please, can we?), there are some further simplifications in
tccgen.c that might offset some of the cost of this patch.
The idea is that an integer is no longer enough to describe how an
argument is stored in registers. There are a number of possibilities
(none, integer register, two integer registers, float register, two
float registers, integer register plus float register, float register
plus integer register), and instead of enumerating them I've
introduced a RegArgs type that stores the offsets for each of our
registers (for the other architectures, it's simply an int specifying
the number of registers). If someone strongly prefers an enum, we
could do that instead, but I believe this is a place where keeping
things general is worth it, because this way it should be doable to
add SSE or AVX support.
There is one line in the patch that looks suspicious:
} else {
addr = (addr + align - 1) & -align;
param_addr = addr;
addr += size;
- sse_param_index += reg_count;
}
break;
However, this actually fixes one half of a bug we have when calling a
function with eight double arguments "interrupted" by a two-double
structure after the seventh double argument:
f(double,double,double,double,double,double,double,struct { double
x,y; },double);
In this case, the last argument should be passed in %xmm7. This patch
fixes the problem in gfunc_prolog, but not the corresponding problem
in gfunc_call, which I'll try tackling next.
2015-05-14 07:32:24 +03:00
|
|
|
goto stack_arg;
|
2015-07-29 23:57:41 +03:00
|
|
|
}
|
2013-04-19 14:08:12 +04:00
|
|
|
break;
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loc -= 16;
|
|
|
|
/* movl $0x????????, -0x10(%rbp) */
|
|
|
|
o(0xf045c7);
|
|
|
|
gen_le32(seen_reg_num * 8);
|
|
|
|
/* movl $0x????????, -0xc(%rbp) */
|
|
|
|
o(0xf445c7);
|
|
|
|
gen_le32(seen_sse_num * 16 + 48);
|
|
|
|
/* movl $0x????????, -0x8(%rbp) */
|
|
|
|
o(0xf845c7);
|
|
|
|
gen_le32(seen_stack_size);
|
|
|
|
|
|
|
|
/* save all register passing arguments */
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
loc -= 16;
|
2016-10-03 06:20:21 +03:00
|
|
|
if (!tcc_state->nosse) {
|
|
|
|
o(0xd60f66); /* movq */
|
|
|
|
gen_modrm(7 - i, VT_LOCAL, NULL, loc);
|
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
/* movq $0, loc+8(%rbp) */
|
|
|
|
o(0x85c748);
|
|
|
|
gen_le32(loc + 8);
|
|
|
|
gen_le32(0);
|
|
|
|
}
|
2009-07-19 00:05:49 +04:00
|
|
|
for (i = 0; i < REGN; i++) {
|
|
|
|
push_arg_reg(REGN-1-i);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sym = func_type->ref;
|
|
|
|
reg_param_index = 0;
|
|
|
|
sse_param_index = 0;
|
|
|
|
|
|
|
|
/* if the function returns a structure, then add an
|
|
|
|
implicit pointer parameter */
|
|
|
|
func_vt = sym->type;
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_arg(&func_vt, NULL, &size, &align, ®_count);
|
2013-04-19 03:40:48 +04:00
|
|
|
if (mode == x86_64_mode_memory) {
|
2008-12-01 21:19:25 +03:00
|
|
|
push_arg_reg(reg_param_index);
|
|
|
|
func_vc = loc;
|
|
|
|
reg_param_index++;
|
|
|
|
}
|
|
|
|
/* define parameters */
|
|
|
|
while ((sym = sym->next) != NULL) {
|
|
|
|
type = &sym->type;
|
2015-07-29 23:57:41 +03:00
|
|
|
mode = classify_x86_64_arg(type, NULL, &size, &align, ®_count);
|
2013-04-19 03:40:48 +04:00
|
|
|
switch (mode) {
|
|
|
|
case x86_64_mode_sse:
|
2016-10-03 06:20:21 +03:00
|
|
|
if (tcc_state->nosse)
|
|
|
|
tcc_error("SSE disabled but floating point arguments used");
|
2015-07-29 23:57:41 +03:00
|
|
|
if (sse_param_index + reg_count <= 8) {
|
2008-12-01 21:19:25 +03:00
|
|
|
/* save arguments passed by register */
|
2015-07-29 23:57:41 +03:00
|
|
|
loc -= reg_count * 8;
|
2013-04-19 18:33:16 +04:00
|
|
|
param_addr = loc;
|
2015-07-29 23:57:41 +03:00
|
|
|
for (i = 0; i < reg_count; ++i) {
|
2013-04-19 03:40:48 +04:00
|
|
|
o(0xd60f66); /* movq */
|
2015-07-29 23:57:41 +03:00
|
|
|
gen_modrm(sse_param_index, VT_LOCAL, NULL, param_addr + i*8);
|
2013-04-19 03:40:48 +04:00
|
|
|
++sse_param_index;
|
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
2013-04-26 19:42:12 +04:00
|
|
|
addr = (addr + align - 1) & -align;
|
2008-12-01 21:19:25 +03:00
|
|
|
param_addr = addr;
|
|
|
|
addr += size;
|
|
|
|
}
|
2013-04-19 03:40:48 +04:00
|
|
|
break;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 03:40:48 +04:00
|
|
|
case x86_64_mode_memory:
|
|
|
|
case x86_64_mode_x87:
|
2013-04-26 19:42:12 +04:00
|
|
|
addr = (addr + align - 1) & -align;
|
2008-12-01 21:19:25 +03:00
|
|
|
param_addr = addr;
|
|
|
|
addr += size;
|
2013-04-19 03:40:48 +04:00
|
|
|
break;
|
2015-07-29 23:57:41 +03:00
|
|
|
|
|
|
|
case x86_64_mode_integer: {
|
|
|
|
if (reg_param_index + reg_count <= REGN) {
|
|
|
|
/* save arguments passed by register */
|
|
|
|
loc -= reg_count * 8;
|
|
|
|
param_addr = loc;
|
|
|
|
for (i = 0; i < reg_count; ++i) {
|
|
|
|
gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, param_addr + i*8);
|
|
|
|
++reg_param_index;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addr = (addr + align - 1) & -align;
|
|
|
|
param_addr = addr;
|
|
|
|
addr += size;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
default: break; /* nothing to be done for x86_64_mode_none */
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
sym_push(sym->v & ~SYM_FIELD, type,
|
|
|
|
VT_LOCAL | VT_LVAL, param_addr);
|
|
|
|
}
|
2015-04-10 15:17:22 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
/* leave some room for bound checking code */
|
|
|
|
if (tcc_state->do_bounds_check) {
|
|
|
|
func_bound_offset = lbounds_section->data_offset;
|
|
|
|
func_bound_ind = ind;
|
|
|
|
oad(0xb8, 0); /* lbound section pointer */
|
2015-07-29 23:53:57 +03:00
|
|
|
o(0xc78948); /* mov %rax,%rdi ## first arg in %rdi, this must be ptr */
|
|
|
|
oad(0xb8, 0); /* call to function */
|
2015-04-10 15:17:22 +03:00
|
|
|
}
|
|
|
|
#endif
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* generate function epilog */
|
|
|
|
void gfunc_epilog(void)
|
|
|
|
{
|
|
|
|
int v, saved_ind;
|
|
|
|
|
2015-04-10 15:17:22 +03:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (tcc_state->do_bounds_check
|
2015-07-29 23:53:57 +03:00
|
|
|
&& func_bound_offset != lbounds_section->data_offset)
|
2015-04-10 15:17:22 +03:00
|
|
|
{
|
|
|
|
addr_t saved_ind;
|
|
|
|
addr_t *bounds_ptr;
|
|
|
|
Sym *sym_data;
|
|
|
|
|
|
|
|
/* add end of table info */
|
|
|
|
bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
|
|
|
|
*bounds_ptr = 0;
|
|
|
|
|
|
|
|
/* generate bound local allocation */
|
2015-07-29 23:57:41 +03:00
|
|
|
sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
|
2015-04-10 15:17:22 +03:00
|
|
|
func_bound_offset, lbounds_section->data_offset);
|
|
|
|
saved_ind = ind;
|
|
|
|
ind = func_bound_ind;
|
|
|
|
greloc(cur_text_section, sym_data, ind + 1, R_386_32);
|
|
|
|
ind = ind + 5 + 3;
|
|
|
|
gen_static_call(TOK___bound_local_new);
|
|
|
|
ind = saved_ind;
|
|
|
|
|
|
|
|
/* generate bound check local freeing */
|
|
|
|
o(0x5250); /* save returned value, if any */
|
|
|
|
greloc(cur_text_section, sym_data, ind + 1, R_386_32);
|
|
|
|
oad(0xb8, 0); /* mov xxx, %rax */
|
2016-11-09 11:22:28 +03:00
|
|
|
o(0xc78948); /* mov %rax,%rdi # first arg in %rdi, this must be ptr */
|
2015-04-10 15:17:22 +03:00
|
|
|
gen_static_call(TOK___bound_local_delete);
|
|
|
|
o(0x585a); /* restore returned value, if any */
|
|
|
|
}
|
|
|
|
#endif
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xc9); /* leave */
|
|
|
|
if (func_ret_sub == 0) {
|
|
|
|
o(0xc3); /* ret */
|
|
|
|
} else {
|
|
|
|
o(0xc2); /* ret n */
|
|
|
|
g(func_ret_sub);
|
|
|
|
g(func_ret_sub >> 8);
|
|
|
|
}
|
|
|
|
/* align local size to word & save local variables */
|
|
|
|
v = (-loc + 15) & -16;
|
|
|
|
saved_ind = ind;
|
|
|
|
ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
|
2009-12-20 00:40:28 +03:00
|
|
|
o(0xe5894855); /* push %rbp, mov %rsp, %rbp */
|
|
|
|
o(0xec8148); /* sub rsp, stacksize */
|
|
|
|
gen_le32(v);
|
2008-12-01 21:19:25 +03:00
|
|
|
ind = saved_ind;
|
|
|
|
}
|
|
|
|
|
2009-12-20 00:40:28 +03:00
|
|
|
#endif /* not PE */
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* generate a jump to a label */
|
|
|
|
int gjmp(int t)
|
|
|
|
{
|
|
|
|
return psym(0xe9, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a jump to a fixed address */
|
|
|
|
void gjmp_addr(int a)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
r = a - ind - 2;
|
|
|
|
if (r == (char)r) {
|
|
|
|
g(0xeb);
|
|
|
|
g(r);
|
|
|
|
} else {
|
|
|
|
oad(0xe9, a - ind - 5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 17:29:38 +03:00
|
|
|
ST_FUNC void gtst_addr(int inv, int a)
|
|
|
|
{
|
|
|
|
inv ^= (vtop--)->c.i;
|
|
|
|
a -= ind + 2;
|
|
|
|
if (a == (char)a) {
|
|
|
|
g(inv - 32);
|
|
|
|
g(a);
|
|
|
|
} else {
|
|
|
|
g(0x0f);
|
|
|
|
oad(inv - 16, a - 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* generate a test. set 'inv' to invert test. Stack entry is popped */
|
2016-09-30 17:29:38 +03:00
|
|
|
ST_FUNC int gtst(int inv, int t)
|
2008-12-01 21:19:25 +03:00
|
|
|
{
|
2015-11-19 21:21:14 +03:00
|
|
|
int v = vtop->r & VT_VALMASK;
|
2008-12-01 21:19:25 +03:00
|
|
|
if (v == VT_CMP) {
|
|
|
|
/* fast case : can jump directly since flags are set */
|
2015-07-29 23:53:57 +03:00
|
|
|
if (vtop->c.i & 0x100)
|
|
|
|
{
|
|
|
|
/* This was a float compare. If the parity flag is set
|
|
|
|
the result was unordered. For anything except != this
|
|
|
|
means false and we don't jump (anding both conditions).
|
|
|
|
For != this means true (oring both).
|
|
|
|
Take care about inverting the test. We need to jump
|
|
|
|
to our target if the result was unordered and test wasn't NE,
|
|
|
|
otherwise if unordered we don't want to jump. */
|
|
|
|
vtop->c.i &= ~0x100;
|
2015-11-19 21:26:47 +03:00
|
|
|
if (inv == (vtop->c.i == TOK_NE))
|
2015-07-29 23:53:57 +03:00
|
|
|
o(0x067a); /* jp +6 */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g(0x0f);
|
|
|
|
t = psym(0x8a, t); /* jp t */
|
|
|
|
}
|
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
g(0x0f);
|
|
|
|
t = psym((vtop->c.i - 16) ^ inv, t);
|
2015-03-03 15:51:09 +03:00
|
|
|
} else if (v == VT_JMP || v == VT_JMPI) {
|
2008-12-01 21:19:25 +03:00
|
|
|
/* && or || optimization */
|
|
|
|
if ((v & 1) == inv) {
|
|
|
|
/* insert vtop->c jump list in t */
|
2015-11-21 02:17:24 +03:00
|
|
|
uint32_t n1, n = vtop->c.i;
|
|
|
|
if (n) {
|
|
|
|
while ((n1 = read32le(cur_text_section->data + n)))
|
|
|
|
n = n1;
|
|
|
|
write32le(cur_text_section->data + n, t);
|
|
|
|
t = vtop->c.i;
|
|
|
|
}
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
|
|
|
t = gjmp(t);
|
|
|
|
gsym(vtop->c.i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate an integer binary operation */
|
|
|
|
void gen_opi(int op)
|
|
|
|
{
|
|
|
|
int r, fr, opc, c;
|
2009-12-20 04:19:51 +03:00
|
|
|
int ll, uu, cc;
|
|
|
|
|
|
|
|
ll = is64_type(vtop[-1].type.t);
|
|
|
|
uu = (vtop[-1].type.t & VT_UNSIGNED) != 0;
|
|
|
|
cc = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
case '+':
|
|
|
|
case TOK_ADDC1: /* add with carry generation */
|
|
|
|
opc = 0;
|
|
|
|
gen_op8:
|
2015-11-17 22:09:35 +03:00
|
|
|
if (cc && (!ll || (int)vtop->c.i == vtop->c.i)) {
|
2008-12-01 21:19:25 +03:00
|
|
|
/* constant case */
|
|
|
|
vswap();
|
|
|
|
r = gv(RC_INT);
|
|
|
|
vswap();
|
|
|
|
c = vtop->c.i;
|
|
|
|
if (c == (char)c) {
|
|
|
|
/* XXX: generate inc and dec for smaller code ? */
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(ll, r, 0, 0x83);
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xc0 | (opc << 3) | REG_VALUE(r));
|
|
|
|
g(c);
|
|
|
|
} else {
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(ll, r, 0, 0x81);
|
2008-12-01 21:19:25 +03:00
|
|
|
oad(0xc0 | (opc << 3) | REG_VALUE(r), c);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gv2(RC_INT, RC_INT);
|
|
|
|
r = vtop[-1].r;
|
|
|
|
fr = vtop[0].r;
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(ll, r, fr, (opc << 3) | 0x01);
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xc0 + REG_VALUE(r) + REG_VALUE(fr) * 8);
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
|
|
|
vtop->r = VT_CMP;
|
|
|
|
vtop->c.i = op;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
case TOK_SUBC1: /* sub with carry generation */
|
|
|
|
opc = 5;
|
|
|
|
goto gen_op8;
|
|
|
|
case TOK_ADDC2: /* add with carry use */
|
|
|
|
opc = 2;
|
|
|
|
goto gen_op8;
|
|
|
|
case TOK_SUBC2: /* sub with carry use */
|
|
|
|
opc = 3;
|
|
|
|
goto gen_op8;
|
|
|
|
case '&':
|
|
|
|
opc = 4;
|
|
|
|
goto gen_op8;
|
|
|
|
case '^':
|
|
|
|
opc = 6;
|
|
|
|
goto gen_op8;
|
|
|
|
case '|':
|
|
|
|
opc = 1;
|
|
|
|
goto gen_op8;
|
|
|
|
case '*':
|
|
|
|
gv2(RC_INT, RC_INT);
|
|
|
|
r = vtop[-1].r;
|
|
|
|
fr = vtop[0].r;
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(ll, fr, r, 0xaf0f); /* imul fr, r */
|
|
|
|
o(0xc0 + REG_VALUE(fr) + REG_VALUE(r) * 8);
|
2008-12-01 21:19:25 +03:00
|
|
|
vtop--;
|
|
|
|
break;
|
|
|
|
case TOK_SHL:
|
|
|
|
opc = 4;
|
|
|
|
goto gen_shift;
|
|
|
|
case TOK_SHR:
|
|
|
|
opc = 5;
|
|
|
|
goto gen_shift;
|
|
|
|
case TOK_SAR:
|
|
|
|
opc = 7;
|
|
|
|
gen_shift:
|
|
|
|
opc = 0xc0 | (opc << 3);
|
2009-12-20 04:19:51 +03:00
|
|
|
if (cc) {
|
2008-12-01 21:19:25 +03:00
|
|
|
/* constant case */
|
|
|
|
vswap();
|
|
|
|
r = gv(RC_INT);
|
|
|
|
vswap();
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(ll, r, 0, 0xc1); /* shl/shr/sar $xxx, r */
|
|
|
|
o(opc | REG_VALUE(r));
|
|
|
|
g(vtop->c.i & (ll ? 63 : 31));
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
|
|
|
/* we generate the shift in ecx */
|
|
|
|
gv2(RC_INT, RC_RCX);
|
|
|
|
r = vtop[-1].r;
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(ll, r, 0, 0xd3); /* shl/shr/sar %cl, r */
|
|
|
|
o(opc | REG_VALUE(r));
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
break;
|
|
|
|
case TOK_UDIV:
|
|
|
|
case TOK_UMOD:
|
2009-12-20 04:19:51 +03:00
|
|
|
uu = 1;
|
|
|
|
goto divmod;
|
|
|
|
case '/':
|
|
|
|
case '%':
|
|
|
|
case TOK_PDIV:
|
|
|
|
uu = 0;
|
|
|
|
divmod:
|
2008-12-01 21:19:25 +03:00
|
|
|
/* first operand must be in eax */
|
|
|
|
/* XXX: need better constraint for second operand */
|
|
|
|
gv2(RC_RAX, RC_RCX);
|
|
|
|
r = vtop[-1].r;
|
|
|
|
fr = vtop[0].r;
|
|
|
|
vtop--;
|
|
|
|
save_reg(TREG_RDX);
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(ll, 0, 0, uu ? 0xd231 : 0x99); /* xor %edx,%edx : cqto */
|
|
|
|
orex(ll, fr, 0, 0xf7); /* div fr, %eax */
|
|
|
|
o((uu ? 0xf0 : 0xf8) + REG_VALUE(fr));
|
|
|
|
if (op == '%' || op == TOK_UMOD)
|
|
|
|
r = TREG_RDX;
|
|
|
|
else
|
2008-12-01 21:19:25 +03:00
|
|
|
r = TREG_RAX;
|
|
|
|
vtop->r = r;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
opc = 7;
|
|
|
|
goto gen_op8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gen_opl(int op)
|
|
|
|
{
|
|
|
|
gen_opi(op);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a floating point operation 'v = t1 op t2' instruction. The
|
|
|
|
two operands are guaranted to have the same floating point type */
|
2013-04-19 21:31:24 +04:00
|
|
|
/* XXX: need to use ST1 too */
|
2008-12-01 21:19:25 +03:00
|
|
|
void gen_opf(int op)
|
|
|
|
{
|
|
|
|
int a, ft, fc, swapped, r;
|
|
|
|
int float_type =
|
2013-04-19 21:31:24 +04:00
|
|
|
(vtop->type.t & VT_BTYPE) == VT_LDOUBLE ? RC_ST0 : RC_FLOAT;
|
2008-12-01 21:19:25 +03:00
|
|
|
|
|
|
|
/* convert constants to memory references */
|
|
|
|
if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
|
|
|
vswap();
|
|
|
|
gv(float_type);
|
|
|
|
vswap();
|
|
|
|
}
|
|
|
|
if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
|
|
|
|
gv(float_type);
|
|
|
|
|
|
|
|
/* must put at least one value in the floating point register */
|
|
|
|
if ((vtop[-1].r & VT_LVAL) &&
|
|
|
|
(vtop[0].r & VT_LVAL)) {
|
|
|
|
vswap();
|
|
|
|
gv(float_type);
|
|
|
|
vswap();
|
|
|
|
}
|
|
|
|
swapped = 0;
|
|
|
|
/* swap the stack if needed so that t1 is the register and t2 is
|
|
|
|
the memory reference */
|
|
|
|
if (vtop[-1].r & VT_LVAL) {
|
|
|
|
vswap();
|
|
|
|
swapped = 1;
|
|
|
|
}
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
|
|
|
/* load on stack second operand */
|
|
|
|
load(TREG_ST0, vtop);
|
|
|
|
save_reg(TREG_RAX); /* eax is used by FP comparison code */
|
|
|
|
if (op == TOK_GE || op == TOK_GT)
|
|
|
|
swapped = !swapped;
|
|
|
|
else if (op == TOK_EQ || op == TOK_NE)
|
|
|
|
swapped = 0;
|
|
|
|
if (swapped)
|
|
|
|
o(0xc9d9); /* fxch %st(1) */
|
2013-12-31 19:40:21 +04:00
|
|
|
if (op == TOK_EQ || op == TOK_NE)
|
|
|
|
o(0xe9da); /* fucompp */
|
|
|
|
else
|
|
|
|
o(0xd9de); /* fcompp */
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xe0df); /* fnstsw %ax */
|
|
|
|
if (op == TOK_EQ) {
|
|
|
|
o(0x45e480); /* and $0x45, %ah */
|
|
|
|
o(0x40fC80); /* cmp $0x40, %ah */
|
|
|
|
} else if (op == TOK_NE) {
|
|
|
|
o(0x45e480); /* and $0x45, %ah */
|
|
|
|
o(0x40f480); /* xor $0x40, %ah */
|
|
|
|
op = TOK_NE;
|
|
|
|
} else if (op == TOK_GE || op == TOK_LE) {
|
|
|
|
o(0x05c4f6); /* test $0x05, %ah */
|
|
|
|
op = TOK_EQ;
|
|
|
|
} else {
|
|
|
|
o(0x45c4f6); /* test $0x45, %ah */
|
|
|
|
op = TOK_EQ;
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
vtop->r = VT_CMP;
|
|
|
|
vtop->c.i = op;
|
|
|
|
} else {
|
|
|
|
/* no memory reference possible for long double operations */
|
|
|
|
load(TREG_ST0, vtop);
|
|
|
|
swapped = !swapped;
|
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
default:
|
|
|
|
case '+':
|
|
|
|
a = 0;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
a = 4;
|
|
|
|
if (swapped)
|
|
|
|
a++;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
a = 1;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
a = 6;
|
|
|
|
if (swapped)
|
|
|
|
a++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ft = vtop->type.t;
|
2015-11-17 22:09:35 +03:00
|
|
|
fc = vtop->c.i;
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xde); /* fxxxp %st, %st(1) */
|
|
|
|
o(0xc1 + (a << 3));
|
|
|
|
vtop--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
|
|
|
/* if saved lvalue, then we must reload it */
|
|
|
|
r = vtop->r;
|
2015-11-17 22:09:35 +03:00
|
|
|
fc = vtop->c.i;
|
2008-12-01 21:19:25 +03:00
|
|
|
if ((r & VT_VALMASK) == VT_LLOCAL) {
|
|
|
|
SValue v1;
|
|
|
|
r = get_reg(RC_INT);
|
2012-06-10 11:01:13 +04:00
|
|
|
v1.type.t = VT_PTR;
|
2008-12-01 21:19:25 +03:00
|
|
|
v1.r = VT_LOCAL | VT_LVAL;
|
2015-11-17 22:09:35 +03:00
|
|
|
v1.c.i = fc;
|
2008-12-01 21:19:25 +03:00
|
|
|
load(r, &v1);
|
|
|
|
fc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (op == TOK_EQ || op == TOK_NE) {
|
|
|
|
swapped = 0;
|
|
|
|
} else {
|
|
|
|
if (op == TOK_LE || op == TOK_LT)
|
|
|
|
swapped = !swapped;
|
|
|
|
if (op == TOK_LE || op == TOK_GE) {
|
|
|
|
op = 0x93; /* setae */
|
|
|
|
} else {
|
|
|
|
op = 0x97; /* seta */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swapped) {
|
2013-04-19 21:31:24 +04:00
|
|
|
gv(RC_FLOAT);
|
|
|
|
vswap();
|
|
|
|
}
|
|
|
|
assert(!(vtop[-1].r & VT_LVAL));
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
|
|
|
|
o(0x66);
|
2013-12-31 19:40:21 +04:00
|
|
|
if (op == TOK_EQ || op == TOK_NE)
|
|
|
|
o(0x2e0f); /* ucomisd */
|
|
|
|
else
|
|
|
|
o(0x2f0f); /* comisd */
|
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
if (vtop->r & VT_LVAL) {
|
|
|
|
gen_modrm(vtop[-1].r, r, vtop->sym, fc);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vtop--;
|
|
|
|
vtop->r = VT_CMP;
|
2012-05-13 04:21:51 +04:00
|
|
|
vtop->c.i = op | 0x100;
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
2013-04-19 21:31:24 +04:00
|
|
|
assert((vtop->type.t & VT_BTYPE) != VT_LDOUBLE);
|
2008-12-01 21:19:25 +03:00
|
|
|
switch(op) {
|
|
|
|
default:
|
|
|
|
case '+':
|
|
|
|
a = 0;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
a = 4;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
a = 1;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
a = 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ft = vtop->type.t;
|
2015-11-17 22:09:35 +03:00
|
|
|
fc = vtop->c.i;
|
2013-04-19 21:31:24 +04:00
|
|
|
assert((ft & VT_BTYPE) != VT_LDOUBLE);
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
r = vtop->r;
|
|
|
|
/* if saved lvalue, then we must reload it */
|
|
|
|
if ((vtop->r & VT_VALMASK) == VT_LLOCAL) {
|
|
|
|
SValue v1;
|
|
|
|
r = get_reg(RC_INT);
|
|
|
|
v1.type.t = VT_PTR;
|
|
|
|
v1.r = VT_LOCAL | VT_LVAL;
|
2015-11-17 22:09:35 +03:00
|
|
|
v1.c.i = fc;
|
2013-04-19 21:31:24 +04:00
|
|
|
load(r, &v1);
|
|
|
|
fc = 0;
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
assert(!(vtop[-1].r & VT_LVAL));
|
|
|
|
if (swapped) {
|
|
|
|
assert(vtop->r & VT_LVAL);
|
|
|
|
gv(RC_FLOAT);
|
|
|
|
vswap();
|
|
|
|
}
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
if ((ft & VT_BTYPE) == VT_DOUBLE) {
|
|
|
|
o(0xf2);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else {
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0xf3);
|
|
|
|
}
|
|
|
|
o(0x0f);
|
|
|
|
o(0x58 + a);
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
if (vtop->r & VT_LVAL) {
|
|
|
|
gen_modrm(vtop[-1].r, r, vtop->sym, fc);
|
|
|
|
} else {
|
|
|
|
o(0xc0 + REG_VALUE(vtop[0].r) + REG_VALUE(vtop[-1].r)*8);
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
2013-04-19 21:31:24 +04:00
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
vtop--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
|
|
|
|
and 'long long' cases. */
|
|
|
|
void gen_cvt_itof(int t)
|
|
|
|
{
|
|
|
|
if ((t & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
save_reg(TREG_ST0);
|
|
|
|
gv(RC_INT);
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
|
|
|
/* signed long long to float/double/long double (unsigned case
|
|
|
|
is handled generically) */
|
|
|
|
o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
|
|
|
o(0x242cdf); /* fildll (%rsp) */
|
|
|
|
o(0x08c48348); /* add $8, %rsp */
|
|
|
|
} else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
|
|
|
|
(VT_INT | VT_UNSIGNED)) {
|
|
|
|
/* unsigned int to float/double/long double */
|
|
|
|
o(0x6a); /* push $0 */
|
|
|
|
g(0x00);
|
|
|
|
o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
|
|
|
o(0x242cdf); /* fildll (%rsp) */
|
|
|
|
o(0x10c48348); /* add $16, %rsp */
|
|
|
|
} else {
|
|
|
|
/* int to float/double/long double */
|
|
|
|
o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
|
|
|
o(0x2404db); /* fildl (%rsp) */
|
|
|
|
o(0x08c48348); /* add $8, %rsp */
|
|
|
|
}
|
|
|
|
vtop->r = TREG_ST0;
|
|
|
|
} else {
|
2013-04-19 21:31:24 +04:00
|
|
|
int r = get_reg(RC_FLOAT);
|
2008-12-01 21:19:25 +03:00
|
|
|
gv(RC_INT);
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0xf2 + ((t & VT_BTYPE) == VT_FLOAT?1:0));
|
2008-12-01 21:19:25 +03:00
|
|
|
if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
|
|
|
|
(VT_INT | VT_UNSIGNED) ||
|
|
|
|
(vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
|
|
|
o(0x48); /* REX */
|
|
|
|
}
|
|
|
|
o(0x2a0f);
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0xc0 + (vtop->r & VT_VALMASK) + REG_VALUE(r)*8); /* cvtsi2sd */
|
|
|
|
vtop->r = r;
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert from one floating point type to another */
|
|
|
|
void gen_cvt_ftof(int t)
|
|
|
|
{
|
|
|
|
int ft, bt, tbt;
|
|
|
|
|
|
|
|
ft = vtop->type.t;
|
|
|
|
bt = ft & VT_BTYPE;
|
|
|
|
tbt = t & VT_BTYPE;
|
2015-07-29 23:53:57 +03:00
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
if (bt == VT_FLOAT) {
|
2013-04-19 21:31:24 +04:00
|
|
|
gv(RC_FLOAT);
|
2008-12-01 21:19:25 +03:00
|
|
|
if (tbt == VT_DOUBLE) {
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x140f); /* unpcklps */
|
|
|
|
o(0xc0 + REG_VALUE(vtop->r)*9);
|
|
|
|
o(0x5a0f); /* cvtps2pd */
|
|
|
|
o(0xc0 + REG_VALUE(vtop->r)*9);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (tbt == VT_LDOUBLE) {
|
2013-04-19 21:31:24 +04:00
|
|
|
save_reg(RC_ST0);
|
2008-12-01 21:19:25 +03:00
|
|
|
/* movss %xmm0,-0x10(%rsp) */
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x110ff3);
|
|
|
|
o(0x44 + REG_VALUE(vtop->r)*8);
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xf024);
|
|
|
|
o(0xf02444d9); /* flds -0x10(%rsp) */
|
|
|
|
vtop->r = TREG_ST0;
|
|
|
|
}
|
|
|
|
} else if (bt == VT_DOUBLE) {
|
2013-04-19 21:31:24 +04:00
|
|
|
gv(RC_FLOAT);
|
2008-12-01 21:19:25 +03:00
|
|
|
if (tbt == VT_FLOAT) {
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x140f66); /* unpcklpd */
|
|
|
|
o(0xc0 + REG_VALUE(vtop->r)*9);
|
|
|
|
o(0x5a0f66); /* cvtpd2ps */
|
|
|
|
o(0xc0 + REG_VALUE(vtop->r)*9);
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (tbt == VT_LDOUBLE) {
|
2013-04-19 21:31:24 +04:00
|
|
|
save_reg(RC_ST0);
|
2008-12-01 21:19:25 +03:00
|
|
|
/* movsd %xmm0,-0x10(%rsp) */
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x110ff2);
|
|
|
|
o(0x44 + REG_VALUE(vtop->r)*8);
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xf024);
|
|
|
|
o(0xf02444dd); /* fldl -0x10(%rsp) */
|
|
|
|
vtop->r = TREG_ST0;
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-30 02:33:34 +04:00
|
|
|
int r;
|
2008-12-01 21:19:25 +03:00
|
|
|
gv(RC_ST0);
|
2013-04-30 02:33:34 +04:00
|
|
|
r = get_reg(RC_FLOAT);
|
2008-12-01 21:19:25 +03:00
|
|
|
if (tbt == VT_DOUBLE) {
|
|
|
|
o(0xf0245cdd); /* fstpl -0x10(%rsp) */
|
|
|
|
/* movsd -0x10(%rsp),%xmm0 */
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x100ff2);
|
|
|
|
o(0x44 + REG_VALUE(r)*8);
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xf024);
|
2013-04-19 21:31:24 +04:00
|
|
|
vtop->r = r;
|
2008-12-01 21:19:25 +03:00
|
|
|
} else if (tbt == VT_FLOAT) {
|
|
|
|
o(0xf0245cd9); /* fstps -0x10(%rsp) */
|
|
|
|
/* movss -0x10(%rsp),%xmm0 */
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0x100ff3);
|
|
|
|
o(0x44 + REG_VALUE(r)*8);
|
2008-12-01 21:19:25 +03:00
|
|
|
o(0xf024);
|
2013-04-19 21:31:24 +04:00
|
|
|
vtop->r = r;
|
2008-12-01 21:19:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert fp to int 't' type */
|
|
|
|
void gen_cvt_ftoi(int t)
|
|
|
|
{
|
|
|
|
int ft, bt, size, r;
|
|
|
|
ft = vtop->type.t;
|
|
|
|
bt = ft & VT_BTYPE;
|
|
|
|
if (bt == VT_LDOUBLE) {
|
|
|
|
gen_cvt_ftof(VT_DOUBLE);
|
|
|
|
bt = VT_DOUBLE;
|
|
|
|
}
|
|
|
|
|
2013-04-19 21:31:24 +04:00
|
|
|
gv(RC_FLOAT);
|
2008-12-01 21:19:25 +03:00
|
|
|
if (t != VT_INT)
|
|
|
|
size = 8;
|
|
|
|
else
|
|
|
|
size = 4;
|
|
|
|
|
|
|
|
r = get_reg(RC_INT);
|
|
|
|
if (bt == VT_FLOAT) {
|
|
|
|
o(0xf3);
|
|
|
|
} else if (bt == VT_DOUBLE) {
|
|
|
|
o(0xf2);
|
|
|
|
} else {
|
|
|
|
assert(0);
|
|
|
|
}
|
2009-12-20 04:19:51 +03:00
|
|
|
orex(size == 8, r, 0, 0x2c0f); /* cvttss2si or cvttsd2si */
|
2013-04-19 21:31:24 +04:00
|
|
|
o(0xc0 + REG_VALUE(vtop->r) + REG_VALUE(r)*8);
|
2008-12-01 21:19:25 +03:00
|
|
|
vtop->r = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computed goto support */
|
|
|
|
void ggoto(void)
|
|
|
|
{
|
|
|
|
gcall_or_jmp(1);
|
|
|
|
vtop--;
|
|
|
|
}
|
|
|
|
|
2013-04-27 23:39:34 +04:00
|
|
|
/* Save the stack pointer onto the stack and return the location of its address */
|
|
|
|
ST_FUNC void gen_vla_sp_save(int addr) {
|
|
|
|
/* mov %rsp,addr(%rbp)*/
|
|
|
|
gen_modrm64(0x89, TREG_RSP, VT_LOCAL, NULL, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the SP from a location on the stack */
|
|
|
|
ST_FUNC void gen_vla_sp_restore(int addr) {
|
|
|
|
gen_modrm64(0x8b, TREG_RSP, VT_LOCAL, NULL, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Subtract from the stack pointer, and push the resulting value onto the stack */
|
|
|
|
ST_FUNC void gen_vla_alloc(CType *type, int align) {
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
/* alloca does more than just adjust %rsp on Windows */
|
|
|
|
vpush_global_sym(&func_old_type, TOK_alloca);
|
|
|
|
vswap(); /* Move alloca ref past allocation size */
|
|
|
|
gfunc_call(1);
|
|
|
|
#else
|
2015-07-29 23:57:41 +03:00
|
|
|
int r;
|
|
|
|
r = gv(RC_INT); /* allocation size */
|
2013-04-27 23:39:34 +04:00
|
|
|
/* sub r,%rsp */
|
|
|
|
o(0x2b48);
|
|
|
|
o(0xe0 | REG_VALUE(r));
|
|
|
|
/* We align to 16 bytes rather than align */
|
|
|
|
/* and ~15, %rsp */
|
|
|
|
o(0xf0e48348);
|
|
|
|
vpop();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-01 21:19:25 +03:00
|
|
|
/* end of x86-64 code generator */
|
|
|
|
/*************************************************************/
|
2009-12-20 03:53:49 +03:00
|
|
|
#endif /* ! TARGET_DEFS_ONLY */
|
|
|
|
/******************************************************/
|