2001-12-14 06:28:53 +08:00
|
|
|
/*
|
|
|
|
* X86 code generator for TCC
|
2015-07-30 04:53:57 +08:00
|
|
|
*
|
2004-10-28 05:38:03 +08:00
|
|
|
* Copyright (c) 2001-2004 Fabrice Bellard
|
2001-12-14 06:28:53 +08:00
|
|
|
*
|
2003-05-24 22:11:17 +08:00
|
|
|
* 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.
|
2001-12-14 06:28:53 +08:00
|
|
|
*
|
2003-05-24 22:11:17 +08:00
|
|
|
* 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.
|
2001-12-14 06:28:53 +08:00
|
|
|
*
|
2003-05-24 22:11:17 +08:00
|
|
|
* 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
|
2001-12-14 06:28:53 +08:00
|
|
|
*/
|
2001-12-13 05:16:34 +08:00
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
#ifdef TARGET_DEFS_ONLY
|
|
|
|
|
2001-12-13 05:16:34 +08:00
|
|
|
/* number of available registers */
|
2016-10-20 01:22:15 +08:00
|
|
|
#define NB_REGS 5
|
2009-12-20 08:53:49 +08:00
|
|
|
#define NB_ASM_REGS 8
|
2017-05-13 14:59:06 +08:00
|
|
|
#define CONFIG_TCC_ASM
|
2001-12-13 05:16:34 +08:00
|
|
|
|
2002-01-06 00:17:34 +08: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). */
|
2001-12-23 08:34:26 +08:00
|
|
|
#define RC_INT 0x0001 /* generic integer register */
|
|
|
|
#define RC_FLOAT 0x0002 /* generic float register */
|
2002-01-04 05:14:59 +08:00
|
|
|
#define RC_EAX 0x0004
|
2015-07-30 04:53:57 +08:00
|
|
|
#define RC_ST0 0x0008
|
2002-01-04 05:14:59 +08:00
|
|
|
#define RC_ECX 0x0010
|
|
|
|
#define RC_EDX 0x0020
|
2016-10-20 01:22:15 +08:00
|
|
|
#define RC_EBX 0x0040
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
#define RC_IRET RC_EAX /* function return: integer register */
|
2019-12-17 01:44:35 +08:00
|
|
|
#define RC_IRE2 RC_EDX /* function return: second integer register */
|
2002-01-06 00:17:34 +08:00
|
|
|
#define RC_FRET RC_ST0 /* function return: float register */
|
2001-12-13 05:16:34 +08:00
|
|
|
|
|
|
|
/* pretty names for the registers */
|
|
|
|
enum {
|
2002-12-08 22:34:48 +08:00
|
|
|
TREG_EAX = 0,
|
|
|
|
TREG_ECX,
|
|
|
|
TREG_EDX,
|
2016-10-20 01:22:15 +08:00
|
|
|
TREG_EBX,
|
2002-12-08 22:34:48 +08:00
|
|
|
TREG_ST0,
|
2013-04-28 03:39:34 +08:00
|
|
|
TREG_ESP = 4
|
2001-12-13 05:16:34 +08:00
|
|
|
};
|
|
|
|
|
2001-12-23 08:34:26 +08:00
|
|
|
/* return registers for function */
|
2002-12-08 22:34:48 +08:00
|
|
|
#define REG_IRET TREG_EAX /* single word int return register */
|
2019-12-17 01:44:35 +08:00
|
|
|
#define REG_IRE2 TREG_EDX /* second word return register (for long long) */
|
2002-12-08 22:34:48 +08:00
|
|
|
#define REG_FRET TREG_ST0 /* float return register */
|
2001-12-13 05:16:34 +08:00
|
|
|
|
|
|
|
/* defined if function parameters must be evaluated in reverse order */
|
|
|
|
#define INVERT_FUNC_PARAMS
|
|
|
|
|
|
|
|
/* defined if structures are passed as pointers. Otherwise structures
|
|
|
|
are directly pushed on stack. */
|
2013-02-17 07:48:51 +08:00
|
|
|
/* #define FUNC_STRUCT_PARAM_AS_PTR */
|
2001-12-13 05:16:34 +08:00
|
|
|
|
2001-12-14 06:28:53 +08:00
|
|
|
/* pointer size, in bytes */
|
|
|
|
#define PTR_SIZE 4
|
|
|
|
|
|
|
|
/* long double size and alignment, in bytes */
|
|
|
|
#define LDOUBLE_SIZE 12
|
|
|
|
#define LDOUBLE_ALIGN 4
|
2002-11-24 23:58:50 +08:00
|
|
|
/* maximum alignment (for aligned attribute support) */
|
|
|
|
#define MAX_ALIGN 8
|
2001-12-14 06:28:53 +08:00
|
|
|
|
Adjust return value promotion for some archs
this is a bit complicated: for i386 and x86-64 we really need to
extend return values ourself, as the common code now does. For arm64
this at least preserves old behaviour. For riscv64 we don't have to
extend ourself but can expect things to be extended up to int (this
matters for var-args tests, when the sign-extension to int64 needs to
happen explicitely). As the extensions are useless, don't do them.
And for arm32 we actually can't express GCC behaviour: the callee side
expects the return value to be correctly extended to int32, but
remembers the original type. In case the ultimate target type for the
call result is only int, no further extension is done. But in case
the target type is e.g. int64 an extension happens, but not from int32
but from the original type. We don't know the ultimate target type,
so we have to choose a type to put into vtop:
* original type (plus VT_MUSTCAST) - this looses when the ultimate
target is int (GCC: no cast, TCC: a cast)
* int (without MUSTCAST) - this looses when the ultimate target is
int64 (GCC: cast from original type, TCC: cast from int)
This difference can only be seen with undefined sources, like the
testcases, so it doesn't seem worthwhile to try an make it work, just
disable the test on arm and choose the second variant as that generates
less code.
2019-12-17 08:46:06 +08:00
|
|
|
/* define if return values need to be extended explicitely
|
|
|
|
at caller side (for interfacing with non-TCC compilers) */
|
|
|
|
#define PROMOTE_RET
|
|
|
|
|
2001-12-13 05:16:34 +08:00
|
|
|
/******************************************************/
|
2009-12-20 08:53:49 +08:00
|
|
|
#else /* ! TARGET_DEFS_ONLY */
|
|
|
|
/******************************************************/
|
2019-12-11 07:37:18 +08:00
|
|
|
#define USING_GLOBALS
|
2015-07-30 04:53:57 +08:00
|
|
|
#include "tcc.h"
|
2009-12-20 08:53:49 +08:00
|
|
|
|
2021-02-01 22:10:58 +08:00
|
|
|
ST_DATA const char * const target_machine_defs =
|
2020-12-23 04:10:22 +08:00
|
|
|
"__i386__\0"
|
|
|
|
"__i386\0"
|
|
|
|
;
|
|
|
|
|
2016-10-20 01:22:15 +08:00
|
|
|
/* define to 1/0 to [not] have EBX as 4th register */
|
2016-12-19 07:27:08 +08:00
|
|
|
#define USE_EBX 0
|
2016-10-20 01:22:15 +08:00
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_DATA const int reg_classes[NB_REGS] = {
|
|
|
|
/* eax */ RC_INT | RC_EAX,
|
|
|
|
/* ecx */ RC_INT | RC_ECX,
|
|
|
|
/* edx */ RC_INT | RC_EDX,
|
2016-10-20 01:22:15 +08:00
|
|
|
/* ebx */ (RC_INT | RC_EBX) * USE_EBX,
|
2009-12-20 08:53:49 +08:00
|
|
|
/* st0 */ RC_FLOAT | RC_ST0,
|
|
|
|
};
|
2001-12-13 05:16:34 +08:00
|
|
|
|
2002-07-27 22:06:11 +08:00
|
|
|
static unsigned long func_sub_sp_offset;
|
2002-01-27 01:23:44 +08:00
|
|
|
static int func_ret_sub;
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2021-10-22 13:39:54 +08:00
|
|
|
static addr_t func_bound_offset;
|
|
|
|
static unsigned long func_bound_ind;
|
|
|
|
ST_DATA int func_bound_add_epilog;
|
|
|
|
static void gen_bounds_prolog(void);
|
|
|
|
static void gen_bounds_epilog(void);
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2002-01-04 06:43:10 +08:00
|
|
|
|
2002-08-18 21:19:18 +08:00
|
|
|
/* XXX: make it faster ? */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void g(int c)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2002-08-18 21:19:18 +08:00
|
|
|
int ind1;
|
2021-10-22 13:39:54 +08:00
|
|
|
if (nocode_wanted)
|
2016-12-19 00:23:33 +08:00
|
|
|
return;
|
2021-10-22 13:39:54 +08:00
|
|
|
ind1 = ind + 1;
|
2002-08-18 21:19:18 +08:00
|
|
|
if (ind1 > cur_text_section->data_allocated)
|
2021-10-22 13:39:54 +08:00
|
|
|
section_realloc(cur_text_section, ind1);
|
|
|
|
cur_text_section->data[ind] = c;
|
|
|
|
ind = ind1;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void o(unsigned int c)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
|
|
|
while (c) {
|
2021-10-22 13:39:54 +08:00
|
|
|
g(c);
|
2004-10-05 06:06:22 +08:00
|
|
|
c = c >> 8;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_le16(int v)
|
2009-12-20 05:08:37 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
g(v);
|
|
|
|
g(v >> 8);
|
2009-12-20 05:08:37 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_le32(int c)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
g(c);
|
|
|
|
g(c >> 8);
|
|
|
|
g(c >> 16);
|
|
|
|
g(c >> 24);
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* output a symbol and patch all calls to it */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gsym_addr(int t, int a)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
|
|
|
while (t) {
|
2015-11-20 02:21:14 +08:00
|
|
|
unsigned char *ptr = cur_text_section->data + t;
|
|
|
|
uint32_t n = read32le(ptr); /* next value */
|
|
|
|
write32le(ptr, a - t - 4);
|
2001-12-13 05:16:34 +08:00
|
|
|
t = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* instruction + 4 bytes data. Return the address of the data */
|
2021-10-22 13:39:54 +08:00
|
|
|
static int oad(int c, int s)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2016-12-19 00:23:33 +08:00
|
|
|
int t;
|
2021-10-22 13:39:54 +08:00
|
|
|
if (nocode_wanted)
|
2016-12-19 00:23:33 +08:00
|
|
|
return s;
|
2021-10-22 13:39:54 +08:00
|
|
|
o(c);
|
|
|
|
t = ind;
|
|
|
|
gen_le32(s);
|
2016-12-19 00:23:33 +08:00
|
|
|
return t;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_fill_nops(int bytes)
|
2018-04-07 05:01:45 +08:00
|
|
|
{
|
|
|
|
while (bytes--)
|
2021-10-22 13:39:54 +08:00
|
|
|
g(0x90);
|
2018-04-07 05:01:45 +08:00
|
|
|
}
|
|
|
|
|
2017-02-14 01:23:43 +08:00
|
|
|
/* generate jmp to a label */
|
2021-10-22 13:39:54 +08:00
|
|
|
#define gjmp2(instr,lbl) oad(instr,lbl)
|
2017-02-14 01:23:43 +08:00
|
|
|
|
2002-07-14 22:38:33 +08:00
|
|
|
/* output constant with relocation if 'r & VT_SYM' is true */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_addr32(int r, Sym *sym, int c)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2002-08-30 07:05:59 +08:00
|
|
|
if (r & VT_SYM)
|
2021-10-22 13:39:54 +08:00
|
|
|
greloc(cur_text_section, sym, ind, R_386_32);
|
|
|
|
gen_le32(c);
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_addrpc32(int r, Sym *sym, int c)
|
2009-12-20 05:08:37 +08:00
|
|
|
{
|
|
|
|
if (r & VT_SYM)
|
2021-10-22 13:39:54 +08:00
|
|
|
greloc(cur_text_section, sym, ind, R_386_PC32);
|
|
|
|
gen_le32(c - 4);
|
2009-12-20 05:08:37 +08:00
|
|
|
}
|
|
|
|
|
2017-09-25 09:03:26 +08:00
|
|
|
/* generate a modrm reference. 'op_reg' contains the additional 3
|
2001-12-23 08:34:26 +08:00
|
|
|
opcode bits */
|
2021-10-22 13:39:54 +08:00
|
|
|
static void gen_modrm(int op_reg, int r, Sym *sym, int c)
|
2001-12-23 08:34:26 +08:00
|
|
|
{
|
|
|
|
op_reg = op_reg << 3;
|
|
|
|
if ((r & VT_VALMASK) == VT_CONST) {
|
|
|
|
/* constant memory reference */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x05 | op_reg);
|
|
|
|
gen_addr32(r, sym, c);
|
2001-12-23 08:34:26 +08:00
|
|
|
} else if ((r & VT_VALMASK) == VT_LOCAL) {
|
|
|
|
/* currently, we use only ebp as base */
|
|
|
|
if (c == (char)c) {
|
|
|
|
/* short reference */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x45 | op_reg);
|
|
|
|
g(c);
|
2001-12-23 08:34:26 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
oad(0x85 | op_reg, c);
|
2001-12-23 08:34:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
g(0x00 | op_reg | (r & VT_VALMASK));
|
2001-12-23 08:34:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
/* load 'r' from value 'sv' */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void load(int r, SValue *sv)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2001-12-20 09:05:21 +08:00
|
|
|
int v, t, ft, fc, fr;
|
|
|
|
SValue v1;
|
2001-12-13 05:16:34 +08:00
|
|
|
|
2009-11-14 00:14:05 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2010-01-15 03:55:51 +08:00
|
|
|
SValue v2;
|
2021-10-22 13:39:54 +08:00
|
|
|
sv = pe_getimport(sv, &v2);
|
2009-11-14 00:14:05 +08:00
|
|
|
#endif
|
2010-01-15 03:55:51 +08:00
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
fr = sv->r;
|
2017-07-09 18:07:40 +08:00
|
|
|
ft = sv->type.t & ~VT_DEFSIGN;
|
2015-11-18 03:09:35 +08:00
|
|
|
fc = sv->c.i;
|
2001-12-20 09:05:21 +08:00
|
|
|
|
2016-03-27 00:57:22 +08:00
|
|
|
ft &= ~(VT_VOLATILE | VT_CONSTANT);
|
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
v = fr & VT_VALMASK;
|
|
|
|
if (fr & VT_LVAL) {
|
2001-12-13 05:16:34 +08:00
|
|
|
if (v == VT_LLOCAL) {
|
2002-11-19 05:46:44 +08:00
|
|
|
v1.type.t = VT_INT;
|
2001-12-20 09:05:21 +08:00
|
|
|
v1.r = VT_LOCAL | VT_LVAL;
|
2015-11-18 03:09:35 +08:00
|
|
|
v1.c.i = fc;
|
2020-05-24 02:27:43 +08:00
|
|
|
v1.sym = NULL;
|
2001-12-23 08:34:26 +08:00
|
|
|
fr = r;
|
2012-01-23 08:41:36 +08:00
|
|
|
if (!(reg_classes[fr] & RC_INT))
|
2021-10-22 13:39:54 +08:00
|
|
|
fr = get_reg(RC_INT);
|
|
|
|
load(fr, &v1);
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
if ((ft & VT_BTYPE) == VT_FLOAT) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xd9); /* flds */
|
2001-12-13 05:16:34 +08:00
|
|
|
r = 0;
|
|
|
|
} else if ((ft & VT_BTYPE) == VT_DOUBLE) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xdd); /* fldl */
|
2001-12-13 05:16:34 +08:00
|
|
|
r = 0;
|
2001-12-14 06:28:53 +08:00
|
|
|
} else if ((ft & VT_BTYPE) == VT_LDOUBLE) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xdb); /* fldt */
|
2001-12-14 06:28:53 +08:00
|
|
|
r = 5;
|
2013-06-14 22:18:16 +08:00
|
|
|
} else if ((ft & VT_TYPE) == VT_BYTE || (ft & VT_TYPE) == VT_BOOL) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xbe0f); /* movsbl */
|
2002-07-13 23:55:16 +08:00
|
|
|
} else if ((ft & VT_TYPE) == (VT_BYTE | VT_UNSIGNED)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xb60f); /* movzbl */
|
2002-07-13 23:55:16 +08:00
|
|
|
} else if ((ft & VT_TYPE) == VT_SHORT) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xbf0f); /* movswl */
|
2002-07-13 23:55:16 +08:00
|
|
|
} else if ((ft & VT_TYPE) == (VT_SHORT | VT_UNSIGNED)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xb70f); /* movzwl */
|
2002-07-13 23:55:16 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x8b); /* movl */
|
2002-07-13 23:55:16 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
gen_modrm(r, fr, sv->sym, fc);
|
2001-12-13 05:16:34 +08:00
|
|
|
} else {
|
|
|
|
if (v == VT_CONST) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xb8 + r); /* mov $xx, r */
|
|
|
|
gen_addr32(fr, sv->sym, fc);
|
2001-12-13 05:16:34 +08:00
|
|
|
} else if (v == VT_LOCAL) {
|
2012-11-16 04:04:56 +08:00
|
|
|
if (fc) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x8d); /* lea xxx(%ebp), r */
|
|
|
|
gen_modrm(r, VT_LOCAL, sv->sym, fc);
|
2012-11-16 04:04:56 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x89);
|
|
|
|
o(0xe8 + r); /* mov %ebp, r */
|
2012-11-16 04:04:56 +08:00
|
|
|
}
|
2001-12-13 05:16:34 +08:00
|
|
|
} else if (v == VT_CMP) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x0f); /* setxx %br */
|
|
|
|
o(fc);
|
|
|
|
o(0xc0 + r);
|
|
|
|
o(0xc0b60f + r * 0x90000); /* movzbl %al, %eax */
|
2001-12-13 05:16:34 +08:00
|
|
|
} else if (v == VT_JMP || v == VT_JMPI) {
|
|
|
|
t = v & 1;
|
2021-10-22 13:39:54 +08:00
|
|
|
oad(0xb8 + r, t); /* mov $1, r */
|
|
|
|
o(0x05eb); /* jmp after */
|
|
|
|
gsym(fc);
|
|
|
|
oad(0xb8 + r, t ^ 1); /* mov $0, r */
|
2001-12-13 05:16:34 +08:00
|
|
|
} else if (v != r) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x89);
|
|
|
|
o(0xc0 + r + v * 8); /* mov v, r */
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-20 09:05:21 +08:00
|
|
|
/* store register 'r' in lvalue 'v' */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void store(int r, SValue *v)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2001-12-20 09:05:21 +08:00
|
|
|
int fr, bt, ft, fc;
|
2001-12-13 05:16:34 +08:00
|
|
|
|
2009-11-14 00:14:05 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2010-01-15 03:55:51 +08:00
|
|
|
SValue v2;
|
2021-10-22 13:39:54 +08:00
|
|
|
v = pe_getimport(v, &v2);
|
2009-11-14 00:14:05 +08:00
|
|
|
#endif
|
2010-01-15 03:55:51 +08:00
|
|
|
|
2002-11-19 05:46:44 +08:00
|
|
|
ft = v->type.t;
|
2015-11-18 03:09:35 +08:00
|
|
|
fc = v->c.i;
|
2001-12-20 09:05:21 +08:00
|
|
|
fr = v->r & VT_VALMASK;
|
2016-03-27 00:57:22 +08:00
|
|
|
ft &= ~(VT_VOLATILE | VT_CONSTANT);
|
2001-12-13 05:16:34 +08:00
|
|
|
bt = ft & VT_BTYPE;
|
2001-12-23 08:34:26 +08:00
|
|
|
/* XXX: incorrect if float reg to reg */
|
2001-12-13 05:16:34 +08:00
|
|
|
if (bt == VT_FLOAT) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xd9); /* fsts */
|
2001-12-18 05:56:48 +08:00
|
|
|
r = 2;
|
2001-12-13 05:16:34 +08:00
|
|
|
} else if (bt == VT_DOUBLE) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xdd); /* fstpl */
|
2001-12-18 05:56:48 +08:00
|
|
|
r = 2;
|
2001-12-14 06:28:53 +08:00
|
|
|
} else if (bt == VT_LDOUBLE) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc0d9); /* fld %st(0) */
|
|
|
|
o(0xdb); /* fstpt */
|
2001-12-14 06:28:53 +08:00
|
|
|
r = 7;
|
2001-12-13 05:16:34 +08:00
|
|
|
} else {
|
|
|
|
if (bt == VT_SHORT)
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x66);
|
2005-04-17 21:15:31 +08:00
|
|
|
if (bt == VT_BYTE || bt == VT_BOOL)
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x88);
|
2001-12-13 05:16:34 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x89);
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
2001-12-23 08:34:26 +08:00
|
|
|
if (fr == VT_CONST ||
|
|
|
|
fr == VT_LOCAL ||
|
|
|
|
(v->r & VT_LVAL)) {
|
2021-10-22 13:39:54 +08:00
|
|
|
gen_modrm(r, v->r, v->sym, fc);
|
2001-12-13 05:16:34 +08:00
|
|
|
} else if (fr != r) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc0 + fr + r * 8); /* mov r, fr */
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static void gadd_sp(int val)
|
2002-07-25 06:11:30 +08:00
|
|
|
{
|
|
|
|
if (val == (char)val) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc483);
|
|
|
|
g(val);
|
2002-07-25 06:11:30 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
oad(0xc481, val); /* add $xxx, %esp */
|
2002-07-25 06:11:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 14:59:06 +08:00
|
|
|
#if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_PE
|
2021-10-22 13:39:54 +08:00
|
|
|
static void gen_static_call(int v)
|
2013-08-29 04:55:05 +08:00
|
|
|
{
|
|
|
|
Sym *sym;
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
sym = external_helper_sym(v);
|
|
|
|
oad(0xe8, -4);
|
|
|
|
greloc(cur_text_section, sym, ind-4, R_386_PC32);
|
2013-08-29 04:55:05 +08:00
|
|
|
}
|
2017-05-13 14:59:06 +08:00
|
|
|
#endif
|
2013-08-29 04:55:05 +08:00
|
|
|
|
2002-11-03 08:43:01 +08:00
|
|
|
/* 'is_jmp' is '1' if it is a jump */
|
2021-10-22 13:39:54 +08:00
|
|
|
static void gcall_or_jmp(int is_jmp)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
|
|
|
int r;
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST && (vtop->r & VT_SYM)) {
|
2017-09-09 21:11:56 +08:00
|
|
|
/* constant and relocation case */
|
2021-10-22 13:39:54 +08:00
|
|
|
greloc(cur_text_section, vtop->sym, ind + 1, R_386_PC32);
|
|
|
|
oad(0xe8 + is_jmp, vtop->c.i - 4); /* call/jmp im */
|
2017-09-09 21:01:42 +08:00
|
|
|
} else {
|
|
|
|
/* otherwise, indirect call */
|
2021-10-22 13:39:54 +08:00
|
|
|
r = gv(RC_INT);
|
|
|
|
o(0xff); /* call/jmp *r */
|
|
|
|
o(0xd0 + r + (is_jmp << 4));
|
2017-09-09 21:01:42 +08:00
|
|
|
}
|
2002-11-03 08:43:01 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 22:10:58 +08:00
|
|
|
static const uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
|
|
|
|
static const uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
|
2004-10-18 08:14:40 +08:00
|
|
|
|
2013-11-22 09:27:15 +08:00
|
|
|
/* Return the number of registers needed to return the struct, or 0 if
|
|
|
|
returning via struct pointer. */
|
2015-07-30 04:57:41 +08:00
|
|
|
ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align, int *regsize)
|
2013-04-30 06:33:34 +08:00
|
|
|
{
|
2021-01-12 02:26:10 +08:00
|
|
|
#if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
|
2013-04-19 00:27:34 +08:00
|
|
|
int size, align;
|
2013-04-30 06:33:34 +08:00
|
|
|
*ret_align = 1; // Never have to re-align return values for x86
|
2015-03-09 07:19:59 +08:00
|
|
|
*regsize = 4;
|
2013-04-19 00:27:34 +08:00
|
|
|
size = type_size(vt, &align);
|
2017-02-09 02:45:31 +08:00
|
|
|
if (size > 8 || (size & (size - 1)))
|
2015-07-30 04:57:41 +08:00
|
|
|
return 0;
|
2017-02-09 02:45:31 +08:00
|
|
|
if (size == 8)
|
2013-04-19 00:27:34 +08:00
|
|
|
ret->t = VT_LLONG;
|
2017-02-09 02:45:31 +08:00
|
|
|
else if (size == 4)
|
2013-04-19 00:27:34 +08:00
|
|
|
ret->t = VT_INT;
|
2017-02-09 02:45:31 +08:00
|
|
|
else if (size == 2)
|
|
|
|
ret->t = VT_SHORT;
|
|
|
|
else
|
|
|
|
ret->t = VT_BYTE;
|
|
|
|
ret->ref = NULL;
|
|
|
|
return 1;
|
2013-04-19 00:27:34 +08:00
|
|
|
#else
|
2013-04-30 06:33:34 +08:00
|
|
|
*ret_align = 1; // Never have to re-align return values for x86
|
2015-07-30 04:57:41 +08:00
|
|
|
return 0;
|
2013-04-19 00:27:34 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-04-17 05:16:20 +08: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. */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gfunc_call(int nb_args)
|
2002-11-03 08:43:01 +08:00
|
|
|
{
|
2004-10-18 08:14:40 +08:00
|
|
|
int size, align, r, args_size, i, func_call;
|
2003-04-17 05:16:20 +08:00
|
|
|
Sym *func_sym;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
bcheck cleanup
- revert Makefiles to state before last bcheck additions
Instead, just load bcheck.o explicitly if that is
what is wanted.
- move tcc_add_bcheck() to the <target>-link.c files and
remove revently added arguments. This function is to
support tccelf.c with linking, not for tccgen.c to
support compilation.
- remove -ba option: It said:
"-ba Enable better address checking with bounds checker"
Okay, if it is better then to have it is not an option.
- remove va_copy. It is C99 and we try to stay C89 in tinycc
when possible. For example, MS compilers do not have va_copy.
- win64: revert any 'fixes' to alloca
It was correct as it was before, except for bound_checking
where it was not implemented. This should now work too.
- remove parasitic filename:linenum features
Such feature is already present with rt_printline in
tccrun.c. If it doesn't work it can be fixed.
- revert changes to gen_bounded_ptr_add()
gen_bounded_ptr_add() was working as it should before
(mostly). For the sake of simplicity I switched it to
CDECL. Anyway, FASTCALL means SLOWCALL with tinycc.
In exchange you get one addition which is required for
bounds_cnecking function arguments. The important thing
is to check them *BEFORE* they are loaded into registers.
New function gbound_args() does that.
In any case, code instrumentation with the bounds-check
functions as such now seems to work flawlessly again,
which means when they are inserted as NOPs, any code that
tcc can compile, seems to behave just the same as without
them.
What these functions then do when fully enabled, is a
differnt story. I did not touch this.
2019-12-12 22:45:45 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2021-10-22 13:39:54 +08:00
|
|
|
if (tcc_state->do_bounds_check)
|
|
|
|
gbound_args(nb_args);
|
bcheck cleanup
- revert Makefiles to state before last bcheck additions
Instead, just load bcheck.o explicitly if that is
what is wanted.
- move tcc_add_bcheck() to the <target>-link.c files and
remove revently added arguments. This function is to
support tccelf.c with linking, not for tccgen.c to
support compilation.
- remove -ba option: It said:
"-ba Enable better address checking with bounds checker"
Okay, if it is better then to have it is not an option.
- remove va_copy. It is C99 and we try to stay C89 in tinycc
when possible. For example, MS compilers do not have va_copy.
- win64: revert any 'fixes' to alloca
It was correct as it was before, except for bound_checking
where it was not implemented. This should now work too.
- remove parasitic filename:linenum features
Such feature is already present with rt_printline in
tccrun.c. If it doesn't work it can be fixed.
- revert changes to gen_bounded_ptr_add()
gen_bounded_ptr_add() was working as it should before
(mostly). For the sake of simplicity I switched it to
CDECL. Anyway, FASTCALL means SLOWCALL with tinycc.
In exchange you get one addition which is required for
bounds_cnecking function arguments. The important thing
is to check them *BEFORE* they are loaded into registers.
New function gbound_args() does that.
In any case, code instrumentation with the bounds-check
functions as such now seems to work flawlessly again,
which means when they are inserted as NOPs, any code that
tcc can compile, seems to behave just the same as without
them.
What these functions then do when fully enabled, is a
differnt story. I did not touch this.
2019-12-12 22:45:45 +08:00
|
|
|
#endif
|
|
|
|
|
2003-04-17 05:16:20 +08:00
|
|
|
args_size = 0;
|
|
|
|
for(i = 0;i < nb_args; i++) {
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) {
|
|
|
|
size = type_size(&vtop->type, &align);
|
2003-04-17 05:16:20 +08:00
|
|
|
/* align to stack align size */
|
|
|
|
size = (size + 3) & ~3;
|
|
|
|
/* allocate the necessary size on stack */
|
2020-07-06 06:00:42 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2021-01-04 20:16:05 +08:00
|
|
|
if (size >= 4096) {
|
2021-10-22 13:39:54 +08:00
|
|
|
r = get_reg(RC_EAX);
|
|
|
|
oad(0x68, size); // push size
|
2021-01-04 20:16:05 +08:00
|
|
|
/* cannot call normal 'alloca' with bound checking */
|
2021-10-22 13:39:54 +08:00
|
|
|
gen_static_call(tok_alloc_const("__alloca"));
|
|
|
|
gadd_sp(4);
|
2021-01-04 20:16:05 +08:00
|
|
|
} else
|
2020-07-06 06:00:42 +08:00
|
|
|
#endif
|
2021-01-04 20:16:05 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
oad(0xec81, size); /* sub $xxx, %esp */
|
2021-01-04 20:16:05 +08:00
|
|
|
/* generate structure store */
|
2021-10-22 13:39:54 +08:00
|
|
|
r = get_reg(RC_INT);
|
|
|
|
o(0xe089 + (r << 8)); /* mov %esp, r */
|
2021-01-04 20:16:05 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vset(&vtop->type, r | VT_LVAL, 0);
|
|
|
|
vswap();
|
|
|
|
vstore();
|
2003-04-17 05:16:20 +08:00
|
|
|
args_size += size;
|
2021-10-22 13:39:54 +08:00
|
|
|
} else if (is_float(vtop->type.t)) {
|
|
|
|
gv(RC_FLOAT); /* only one float register */
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_FLOAT)
|
2003-04-17 05:16:20 +08:00
|
|
|
size = 4;
|
2021-10-22 13:39:54 +08:00
|
|
|
else if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
|
2003-04-17 05:16:20 +08:00
|
|
|
size = 8;
|
|
|
|
else
|
|
|
|
size = 12;
|
2021-10-22 13:39:54 +08:00
|
|
|
oad(0xec81, size); /* sub $xxx, %esp */
|
2003-04-17 05:16:20 +08:00
|
|
|
if (size == 12)
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x7cdb);
|
2003-04-17 05:16:20 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x5cd9 + size - 4); /* fstp[s|l] 0(%esp) */
|
|
|
|
g(0x24);
|
|
|
|
g(0x00);
|
2003-04-17 05:16:20 +08:00
|
|
|
args_size += size;
|
|
|
|
} else {
|
|
|
|
/* simple type (currently always same size) */
|
|
|
|
/* XXX: implicit cast ? */
|
2021-10-22 13:39:54 +08:00
|
|
|
r = gv(RC_INT);
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
2003-04-17 05:16:20 +08:00
|
|
|
size = 8;
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x50 + vtop->r2); /* push r */
|
2003-04-17 05:16:20 +08:00
|
|
|
} else {
|
|
|
|
size = 4;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x50 + r); /* push r */
|
2003-04-17 05:16:20 +08:00
|
|
|
args_size += size;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop--;
|
2003-04-17 05:16:20 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
save_regs(0); /* save used temporary registers */
|
|
|
|
func_sym = vtop->type.ref;
|
2017-07-09 18:34:11 +08:00
|
|
|
func_call = func_sym->f.func_call;
|
2004-10-18 08:14:40 +08:00
|
|
|
/* fast call case */
|
2005-09-04 06:21:22 +08:00
|
|
|
if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
|
|
|
|
func_call == FUNC_FASTCALLW) {
|
2004-10-18 08:14:40 +08:00
|
|
|
int fastcall_nb_regs;
|
2021-02-01 22:10:58 +08:00
|
|
|
const uint8_t *fastcall_regs_ptr;
|
2005-09-04 06:21:22 +08:00
|
|
|
if (func_call == FUNC_FASTCALLW) {
|
|
|
|
fastcall_regs_ptr = fastcallw_regs;
|
|
|
|
fastcall_nb_regs = 2;
|
|
|
|
} else {
|
|
|
|
fastcall_regs_ptr = fastcall_regs;
|
|
|
|
fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
|
|
|
|
}
|
2004-10-18 08:14:40 +08:00
|
|
|
for(i = 0;i < fastcall_nb_regs; i++) {
|
|
|
|
if (args_size <= 0)
|
|
|
|
break;
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x58 + fastcall_regs_ptr[i]); /* pop r */
|
2004-10-18 08:14:40 +08:00
|
|
|
/* XXX: incorrect for struct/floats */
|
|
|
|
args_size -= 4;
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 02:26:10 +08:00
|
|
|
#if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
|
2021-10-22 13:39:54 +08:00
|
|
|
else if ((vtop->type.ref->type.t & VT_BTYPE) == VT_STRUCT)
|
2013-04-26 07:31:46 +08:00
|
|
|
args_size -= 4;
|
|
|
|
#endif
|
2020-01-19 18:15:12 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
gcall_or_jmp(0);
|
2009-06-17 08:11:13 +08:00
|
|
|
|
2017-08-21 19:38:11 +08:00
|
|
|
if (args_size && func_call != FUNC_STDCALL && func_call != FUNC_FASTCALLW)
|
2021-10-22 13:39:54 +08:00
|
|
|
gadd_sp(args_size);
|
|
|
|
vtop--;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
|
2005-04-11 06:15:08 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2016-10-20 01:22:15 +08:00
|
|
|
#define FUNC_PROLOG_SIZE (10 + USE_EBX)
|
2005-04-11 06:15:08 +08:00
|
|
|
#else
|
2016-10-20 01:22:15 +08:00
|
|
|
#define FUNC_PROLOG_SIZE (9 + USE_EBX)
|
2005-04-11 06:15:08 +08:00
|
|
|
#endif
|
|
|
|
|
2002-01-04 06:43:10 +08:00
|
|
|
/* generate function prolog of type 't' */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gfunc_prolog(Sym *func_sym)
|
2002-01-04 06:43:10 +08:00
|
|
|
{
|
2019-12-10 15:07:25 +08:00
|
|
|
CType *func_type = &func_sym->type;
|
2004-10-18 08:14:40 +08:00
|
|
|
int addr, align, size, func_call, fastcall_nb_regs;
|
|
|
|
int param_index, param_addr;
|
2021-02-01 22:10:58 +08:00
|
|
|
const uint8_t *fastcall_regs_ptr;
|
2002-01-04 06:43:10 +08:00
|
|
|
Sym *sym;
|
2002-11-19 05:46:44 +08:00
|
|
|
CType *type;
|
2002-01-04 06:43:10 +08:00
|
|
|
|
2002-11-19 05:46:44 +08:00
|
|
|
sym = func_type->ref;
|
2017-07-09 18:34:11 +08:00
|
|
|
func_call = sym->f.func_call;
|
2002-01-04 06:43:10 +08:00
|
|
|
addr = 8;
|
2021-10-22 13:39:54 +08:00
|
|
|
loc = 0;
|
|
|
|
func_vc = 0;
|
2009-06-17 08:11:13 +08:00
|
|
|
|
2004-10-18 08:14:40 +08:00
|
|
|
if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
|
|
|
|
fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
|
2005-09-04 06:21:22 +08:00
|
|
|
fastcall_regs_ptr = fastcall_regs;
|
|
|
|
} else if (func_call == FUNC_FASTCALLW) {
|
|
|
|
fastcall_nb_regs = 2;
|
|
|
|
fastcall_regs_ptr = fastcallw_regs;
|
2004-10-18 08:14:40 +08:00
|
|
|
} else {
|
|
|
|
fastcall_nb_regs = 0;
|
2005-09-04 06:21:22 +08:00
|
|
|
fastcall_regs_ptr = NULL;
|
2004-10-18 08:14:40 +08:00
|
|
|
}
|
|
|
|
param_index = 0;
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ind += FUNC_PROLOG_SIZE;
|
|
|
|
func_sub_sp_offset = ind;
|
2002-01-04 06:43:10 +08:00
|
|
|
/* if the function returns a structure, then add an
|
|
|
|
implicit pointer parameter */
|
2021-01-12 02:26:10 +08:00
|
|
|
#if defined(TCC_TARGET_PE) || TARGETOS_FreeBSD || TARGETOS_OpenBSD
|
2021-10-22 13:39:54 +08:00
|
|
|
size = type_size(&func_vt,&align);
|
|
|
|
if (((func_vt.t & VT_BTYPE) == VT_STRUCT)
|
2017-02-09 02:45:31 +08:00
|
|
|
&& (size > 8 || (size & (size - 1)))) {
|
2013-04-19 00:27:34 +08:00
|
|
|
#else
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
|
2013-04-19 00:27:34 +08:00
|
|
|
#endif
|
2004-10-18 08:14:40 +08:00
|
|
|
/* XXX: fastcall case ? */
|
2021-10-22 13:39:54 +08:00
|
|
|
func_vc = addr;
|
2002-01-04 06:43:10 +08:00
|
|
|
addr += 4;
|
2004-10-18 08:14:40 +08:00
|
|
|
param_index++;
|
2002-01-04 06:43:10 +08:00
|
|
|
}
|
|
|
|
/* define parameters */
|
|
|
|
while ((sym = sym->next) != NULL) {
|
2002-11-19 05:46:44 +08:00
|
|
|
type = &sym->type;
|
|
|
|
size = type_size(type, &align);
|
2002-01-04 06:43:10 +08:00
|
|
|
size = (size + 3) & ~3;
|
|
|
|
#ifdef FUNC_STRUCT_PARAM_AS_PTR
|
|
|
|
/* structs are passed as pointer */
|
2002-11-19 05:46:44 +08:00
|
|
|
if ((type->t & VT_BTYPE) == VT_STRUCT) {
|
2002-01-04 06:43:10 +08:00
|
|
|
size = 4;
|
|
|
|
}
|
|
|
|
#endif
|
2004-10-18 08:14:40 +08:00
|
|
|
if (param_index < fastcall_nb_regs) {
|
|
|
|
/* save FASTCALL register */
|
2021-10-22 13:39:54 +08:00
|
|
|
loc -= 4;
|
|
|
|
o(0x89); /* movl */
|
|
|
|
gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
|
|
|
|
param_addr = loc;
|
2004-10-18 08:14:40 +08:00
|
|
|
} else {
|
|
|
|
param_addr = addr;
|
|
|
|
addr += size;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
sym_push(sym->v & ~SYM_FIELD, type,
|
2019-12-17 01:48:31 +08:00
|
|
|
VT_LOCAL | VT_LVAL, param_addr);
|
2004-10-18 08:14:40 +08:00
|
|
|
param_index++;
|
2002-01-04 06:43:10 +08:00
|
|
|
}
|
2002-01-27 01:23:44 +08:00
|
|
|
func_ret_sub = 0;
|
2017-08-21 19:38:11 +08:00
|
|
|
/* pascal type call or fastcall ? */
|
|
|
|
if (func_call == FUNC_STDCALL || func_call == FUNC_FASTCALLW)
|
2002-01-27 01:23:44 +08:00
|
|
|
func_ret_sub = addr - 8;
|
2021-01-12 02:26:10 +08:00
|
|
|
#if !defined(TCC_TARGET_PE) && !TARGETOS_FreeBSD || TARGETOS_OpenBSD
|
2021-10-22 13:39:54 +08:00
|
|
|
else if (func_vc)
|
2013-04-26 07:31:46 +08:00
|
|
|
func_ret_sub = 4;
|
|
|
|
#endif
|
2004-10-18 08:14:40 +08:00
|
|
|
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2021-10-22 13:39:54 +08:00
|
|
|
if (tcc_state->do_bounds_check)
|
|
|
|
gen_bounds_prolog();
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2002-01-04 06:43:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* generate function epilog */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gfunc_epilog(void)
|
2002-01-04 06:43:10 +08:00
|
|
|
{
|
2015-03-26 12:47:45 +08:00
|
|
|
addr_t v, saved_ind;
|
2005-04-11 06:15:08 +08:00
|
|
|
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2021-10-22 13:39:54 +08:00
|
|
|
if (tcc_state->do_bounds_check)
|
|
|
|
gen_bounds_epilog();
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2016-12-19 07:27:08 +08:00
|
|
|
|
|
|
|
/* align local size to word & save local variables */
|
2021-10-22 13:39:54 +08:00
|
|
|
v = (-loc + 3) & -4;
|
2016-12-19 07:27:08 +08:00
|
|
|
|
|
|
|
#if USE_EBX
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x8b);
|
|
|
|
gen_modrm(TREG_EBX, VT_LOCAL, NULL, -(v+4));
|
2016-12-19 07:27:08 +08:00
|
|
|
#endif
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc9); /* leave */
|
2002-01-27 01:23:44 +08:00
|
|
|
if (func_ret_sub == 0) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc3); /* ret */
|
2002-01-27 01:23:44 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc2); /* ret n */
|
|
|
|
g(func_ret_sub);
|
|
|
|
g(func_ret_sub >> 8);
|
2002-01-27 01:23:44 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
saved_ind = ind;
|
|
|
|
ind = func_sub_sp_offset - FUNC_PROLOG_SIZE;
|
2005-04-11 06:15:08 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
if (v >= 4096) {
|
2021-10-22 13:39:54 +08:00
|
|
|
oad(0xb8, v); /* mov stacksize, %eax */
|
|
|
|
gen_static_call(TOK___chkstk); /* call __chkstk, (does the stackframe too) */
|
2005-04-11 06:15:08 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xe58955); /* push %ebp, mov %esp, %ebp */
|
|
|
|
o(0xec81); /* sub esp, stacksize */
|
|
|
|
gen_le32(v);
|
2016-10-20 01:22:15 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x90); /* adjust to FUNC_PROLOG_SIZE */
|
2005-04-11 06:15:08 +08:00
|
|
|
#endif
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x53 * USE_EBX); /* push ebx */
|
|
|
|
ind = saved_ind;
|
2002-01-04 06:43:10 +08:00
|
|
|
}
|
|
|
|
|
2002-02-11 00:13:14 +08:00
|
|
|
/* generate a jump to a label */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC int gjmp(int t)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
return gjmp2(0xe9, t);
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
|
2002-02-11 00:13:14 +08:00
|
|
|
/* generate a jump to a fixed address */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gjmp_addr(int a)
|
2002-02-11 00:13:14 +08:00
|
|
|
{
|
2002-09-09 06:06:11 +08:00
|
|
|
int r;
|
2021-10-22 13:39:54 +08:00
|
|
|
r = a - ind - 2;
|
2002-09-09 06:06:11 +08:00
|
|
|
if (r == (char)r) {
|
2021-10-22 13:39:54 +08:00
|
|
|
g(0xeb);
|
|
|
|
g(r);
|
2002-09-09 06:06:11 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
oad(0xe9, a - ind - 5);
|
2002-09-09 06:06:11 +08:00
|
|
|
}
|
2002-02-11 00:13:14 +08:00
|
|
|
}
|
|
|
|
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
#if 0
|
|
|
|
/* generate a jump to a fixed address */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gjmp_cond_addr(int a, int op)
|
2016-09-30 22:29:38 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
int r = a - ind - 2;
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
if (r == (char)r)
|
2021-10-22 13:39:54 +08:00
|
|
|
g(op - 32), g(r);
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
g(0x0f), gjmp2(op - 16, r - 4);
|
2016-09-30 22:29:38 +08:00
|
|
|
}
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
#endif
|
2016-09-30 22:29:38 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC int gjmp_append(int n, int t)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
void *p;
|
|
|
|
/* insert vtop->c jump list in t */
|
|
|
|
if (n) {
|
|
|
|
uint32_t n1 = n, n2;
|
|
|
|
while ((n2 = read32le(p = cur_text_section->data + n1)))
|
|
|
|
n1 = n2;
|
|
|
|
write32le(p, t);
|
|
|
|
t = n;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC int gjmp_cond(int op, int t)
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
g(0x0f);
|
|
|
|
t = gjmp2(op - 16, t);
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_opi(int op)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2002-01-04 05:14:59 +08:00
|
|
|
int r, fr, opc, c;
|
2001-12-17 01:58:32 +08:00
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
switch(op) {
|
|
|
|
case '+':
|
|
|
|
case TOK_ADDC1: /* add with carry generation */
|
|
|
|
opc = 0;
|
|
|
|
gen_op8:
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
2002-01-04 05:14:59 +08:00
|
|
|
/* constant case */
|
2021-10-22 13:39:54 +08:00
|
|
|
vswap();
|
|
|
|
r = gv(RC_INT);
|
|
|
|
vswap();
|
|
|
|
c = vtop->c.i;
|
2002-01-04 05:14:59 +08:00
|
|
|
if (c == (char)c) {
|
2010-04-04 02:20:34 +08:00
|
|
|
/* generate inc and dec for smaller code */
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
if ((c == 1 || c == -1) && (op == '+' || op == '-')) {
|
|
|
|
opc = (c == 1) ^ (op == '+');
|
2021-10-22 13:39:54 +08:00
|
|
|
o (0x40 | (opc << 3) | r); // inc,dec
|
2010-04-04 02:20:34 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x83);
|
|
|
|
o(0xc0 | (opc << 3) | r);
|
|
|
|
g(c);
|
2010-04-04 02:20:34 +08:00
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x81);
|
|
|
|
oad(0xc0 | (opc << 3) | r, c);
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
gv2(RC_INT, RC_INT);
|
|
|
|
r = vtop[-1].r;
|
|
|
|
fr = vtop[0].r;
|
|
|
|
o((opc << 3) | 0x01);
|
|
|
|
o(0xc0 + r + fr * 8);
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop--;
|
jump optimizations
This unifies VT_CMP with VT_JMP(i) by using mostly VT_CMP
with both a positive and a negative jump target list.
Such we can delay putting the non-inverted or inverted jump
until we can see which one is nore suitable (in most cases).
example:
if (a && b || c && d)
e = 0;
before this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 11 00 00 00 je 27 <main+0x27>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 84 05 00 00 00 je 27 <main+0x27>
22: e9 22 00 00 00 jmp 49 <main+0x49>
27: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
2a: 83 f8 00 cmp $0x0,%eax
2d: 0f 84 11 00 00 00 je 44 <main+0x44>
33: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
36: 83 f8 00 cmp $0x0,%eax
39: 0f 84 05 00 00 00 je 44 <main+0x44>
3f: e9 05 00 00 00 jmp 49 <main+0x49>
44: e9 08 00 00 00 jmp 51 <main+0x51>
49: b8 00 00 00 00 mov $0x0,%eax
4e: 89 45 ec mov %eax,0xffffffec(%ebp)
51: ...
with this patch:
a: 8b 45 fc mov 0xfffffffc(%ebp),%eax
d: 83 f8 00 cmp $0x0,%eax
10: 0f 84 0c 00 00 00 je 22 <main+0x22>
16: 8b 45 f8 mov 0xfffffff8(%ebp),%eax
19: 83 f8 00 cmp $0x0,%eax
1c: 0f 85 18 00 00 00 jne 3a <main+0x3a>
22: 8b 45 f4 mov 0xfffffff4(%ebp),%eax
25: 83 f8 00 cmp $0x0,%eax
28: 0f 84 14 00 00 00 je 42 <main+0x42>
2e: 8b 45 f0 mov 0xfffffff0(%ebp),%eax
31: 83 f8 00 cmp $0x0,%eax
34: 0f 84 08 00 00 00 je 42 <main+0x42>
3a: b8 00 00 00 00 mov $0x0,%eax
3f: 89 45 ec mov %eax,0xffffffec(%ebp)
42: ...
2019-06-22 17:45:35 +08:00
|
|
|
if (op >= TOK_ULT && op <= TOK_GT)
|
2021-10-22 13:39:54 +08:00
|
|
|
vset_VT_CMP(op);
|
2002-01-04 05:14:59 +08:00
|
|
|
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 '*':
|
2021-10-22 13:39:54 +08:00
|
|
|
gv2(RC_INT, RC_INT);
|
|
|
|
r = vtop[-1].r;
|
|
|
|
fr = vtop[0].r;
|
|
|
|
vtop--;
|
|
|
|
o(0xaf0f); /* imul fr, r */
|
|
|
|
o(0xc0 + fr + r * 8);
|
2002-01-04 05:14:59 +08:00
|
|
|
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);
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
2002-01-04 05:14:59 +08:00
|
|
|
/* constant case */
|
2021-10-22 13:39:54 +08:00
|
|
|
vswap();
|
|
|
|
r = gv(RC_INT);
|
|
|
|
vswap();
|
|
|
|
c = vtop->c.i & 0x1f;
|
|
|
|
o(0xc1); /* shl/shr/sar $xxx, r */
|
|
|
|
o(opc | r);
|
|
|
|
g(c);
|
2002-01-04 05:14:59 +08:00
|
|
|
} else {
|
|
|
|
/* we generate the shift in ecx */
|
2021-10-22 13:39:54 +08:00
|
|
|
gv2(RC_INT, RC_ECX);
|
|
|
|
r = vtop[-1].r;
|
|
|
|
o(0xd3); /* shl/shr/sar %cl, r */
|
|
|
|
o(opc | r);
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop--;
|
2002-01-04 05:14:59 +08:00
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
case TOK_UDIV:
|
|
|
|
case TOK_PDIV:
|
|
|
|
case '%':
|
|
|
|
case TOK_UMOD:
|
|
|
|
case TOK_UMULL:
|
2002-01-06 00:17:34 +08:00
|
|
|
/* first operand must be in eax */
|
|
|
|
/* XXX: need better constraint for second operand */
|
2021-10-22 13:39:54 +08:00
|
|
|
gv2(RC_EAX, RC_ECX);
|
|
|
|
r = vtop[-1].r;
|
|
|
|
fr = vtop[0].r;
|
|
|
|
vtop--;
|
|
|
|
save_reg(TREG_EDX);
|
tccgen: arm/i386: save_reg_upstack
tccgen.c:gv() when loading long long from lvalue, before
was saving all registers which caused problems in the arm
function call register parameter preparation, as with
void foo(long long y, int x);
int main(void)
{
unsigned int *xx[1], x;
unsigned long long *yy[1], y;
foo(**yy, **xx);
return 0;
}
Now only the modified register is saved if necessary,
as in this case where it is used to store the result
of the post-inc:
long long *p, v, **pp;
v = 1;
p = &v;
p[0]++;
printf("another long long spill test : %lld\n", *p);
i386-gen.c :
- found a similar problem with TOK_UMULL caused by the
vstack juggle in tccgen:gen_opl()
(bug seen only when using EBX as 4th register)
2016-10-04 23:36:51 +08:00
|
|
|
/* save EAX too if used otherwise */
|
2021-10-22 13:39:54 +08:00
|
|
|
save_reg_upstack(TREG_EAX, 1);
|
2002-01-04 05:14:59 +08:00
|
|
|
if (op == TOK_UMULL) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xf7); /* mul fr */
|
|
|
|
o(0xe0 + fr);
|
|
|
|
vtop->r2 = TREG_EDX;
|
2002-12-08 22:34:48 +08:00
|
|
|
r = TREG_EAX;
|
2001-12-13 05:16:34 +08:00
|
|
|
} else {
|
2002-01-04 05:14:59 +08:00
|
|
|
if (op == TOK_UDIV || op == TOK_UMOD) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xf7d231); /* xor %edx, %edx, div fr, %eax */
|
|
|
|
o(0xf0 + fr);
|
2002-01-04 05:14:59 +08:00
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xf799); /* cltd, idiv fr, %eax */
|
|
|
|
o(0xf8 + fr);
|
2002-01-04 05:14:59 +08:00
|
|
|
}
|
|
|
|
if (op == '%' || op == TOK_UMOD)
|
2002-12-08 22:34:48 +08:00
|
|
|
r = TREG_EDX;
|
2002-01-04 05:14:59 +08:00
|
|
|
else
|
2002-12-08 22:34:48 +08:00
|
|
|
r = TREG_EAX;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop->r = r;
|
2002-01-04 05:14:59 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
opc = 7;
|
|
|
|
goto gen_op8;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-17 01:58:32 +08:00
|
|
|
/* generate a floating point operation 'v = t1 op t2' instruction. The
|
2017-05-08 12:38:09 +08:00
|
|
|
two operands are guaranteed to have the same floating point type */
|
2002-01-06 00:17:34 +08:00
|
|
|
/* XXX: need to use ST1 too */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_opf(int op)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2002-01-27 01:23:44 +08:00
|
|
|
int a, ft, fc, swapped, r;
|
2001-12-13 05:16:34 +08:00
|
|
|
|
2021-01-04 20:16:05 +08:00
|
|
|
if (op == TOK_NEG) { /* unary minus */
|
2021-10-22 13:39:54 +08:00
|
|
|
gv(RC_FLOAT);
|
|
|
|
o(0xe0d9); /* fchs */
|
2021-01-04 20:16:05 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-12-18 05:56:48 +08:00
|
|
|
/* convert constants to memory references */
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
|
|
|
vswap();
|
|
|
|
gv(RC_FLOAT);
|
|
|
|
vswap();
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop[0].r & (VT_VALMASK | VT_LVAL)) == VT_CONST)
|
|
|
|
gv(RC_FLOAT);
|
2001-12-18 05:56:48 +08:00
|
|
|
|
2001-12-13 05:16:34 +08:00
|
|
|
/* must put at least one value in the floating point register */
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop[-1].r & VT_LVAL) &&
|
|
|
|
(vtop[0].r & VT_LVAL)) {
|
|
|
|
vswap();
|
|
|
|
gv(RC_FLOAT);
|
|
|
|
vswap();
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
2002-07-13 23:55:16 +08:00
|
|
|
swapped = 0;
|
|
|
|
/* swap the stack if needed so that t1 is the register and t2 is
|
|
|
|
the memory reference */
|
2021-10-22 13:39:54 +08:00
|
|
|
if (vtop[-1].r & VT_LVAL) {
|
|
|
|
vswap();
|
2002-07-13 23:55:16 +08:00
|
|
|
swapped = 1;
|
|
|
|
}
|
2002-01-04 05:14:59 +08:00
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
2001-12-13 05:16:34 +08:00
|
|
|
/* load on stack second operand */
|
2021-10-22 13:39:54 +08:00
|
|
|
load(TREG_ST0, vtop);
|
|
|
|
save_reg(TREG_EAX); /* eax is used by FP comparison code */
|
2001-12-13 05:16:34 +08:00
|
|
|
if (op == TOK_GE || op == TOK_GT)
|
2002-07-13 23:55:16 +08:00
|
|
|
swapped = !swapped;
|
|
|
|
else if (op == TOK_EQ || op == TOK_NE)
|
|
|
|
swapped = 0;
|
|
|
|
if (swapped)
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc9d9); /* fxch %st(1) */
|
2013-12-31 23:40:21 +08:00
|
|
|
if (op == TOK_EQ || op == TOK_NE)
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xe9da); /* fucompp */
|
2013-12-31 23:40:21 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xd9de); /* fcompp */
|
|
|
|
o(0xe0df); /* fnstsw %ax */
|
2001-12-13 05:16:34 +08:00
|
|
|
if (op == TOK_EQ) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x45e480); /* and $0x45, %ah */
|
|
|
|
o(0x40fC80); /* cmp $0x40, %ah */
|
2001-12-13 05:16:34 +08:00
|
|
|
} else if (op == TOK_NE) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x45e480); /* and $0x45, %ah */
|
|
|
|
o(0x40f480); /* xor $0x40, %ah */
|
2001-12-13 05:16:34 +08:00
|
|
|
op = TOK_NE;
|
|
|
|
} else if (op == TOK_GE || op == TOK_LE) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x05c4f6); /* test $0x05, %ah */
|
2001-12-13 05:16:34 +08:00
|
|
|
op = TOK_EQ;
|
|
|
|
} else {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x45c4f6); /* test $0x45, %ah */
|
2001-12-13 05:16:34 +08:00
|
|
|
op = TOK_EQ;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop--;
|
|
|
|
vset_VT_CMP(op);
|
2001-12-13 05:16:34 +08:00
|
|
|
} else {
|
2001-12-23 08:34:26 +08:00
|
|
|
/* no memory reference possible for long double operations */
|
2021-10-22 13:39:54 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
load(TREG_ST0, vtop);
|
2001-12-23 08:34:26 +08:00
|
|
|
swapped = !swapped;
|
|
|
|
}
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2001-12-13 05:16:34 +08:00
|
|
|
switch(op) {
|
|
|
|
default:
|
|
|
|
case '+':
|
|
|
|
a = 0;
|
|
|
|
break;
|
|
|
|
case '-':
|
2001-12-23 08:34:26 +08:00
|
|
|
a = 4;
|
2001-12-13 05:16:34 +08:00
|
|
|
if (swapped)
|
2001-12-23 08:34:26 +08:00
|
|
|
a++;
|
2001-12-13 05:16:34 +08:00
|
|
|
break;
|
|
|
|
case '*':
|
2001-12-23 08:34:26 +08:00
|
|
|
a = 1;
|
2001-12-13 05:16:34 +08:00
|
|
|
break;
|
|
|
|
case '/':
|
2001-12-23 08:34:26 +08:00
|
|
|
a = 6;
|
2001-12-13 05:16:34 +08:00
|
|
|
if (swapped)
|
2001-12-23 08:34:26 +08:00
|
|
|
a++;
|
2001-12-13 05:16:34 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
ft = vtop->type.t;
|
|
|
|
fc = vtop->c.i;
|
2001-12-23 08:34:26 +08:00
|
|
|
if ((ft & VT_BTYPE) == VT_LDOUBLE) {
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xde); /* fxxxp %st, %st(1) */
|
|
|
|
o(0xc1 + (a << 3));
|
2001-12-13 05:16:34 +08:00
|
|
|
} else {
|
2002-01-27 01:23:44 +08:00
|
|
|
/* if saved lvalue, then we must reload it */
|
2021-10-22 13:39:54 +08:00
|
|
|
r = vtop->r;
|
2002-01-27 01:23:44 +08:00
|
|
|
if ((r & VT_VALMASK) == VT_LLOCAL) {
|
|
|
|
SValue v1;
|
2021-10-22 13:39:54 +08:00
|
|
|
r = get_reg(RC_INT);
|
2002-11-19 05:46:44 +08:00
|
|
|
v1.type.t = VT_INT;
|
2002-01-27 01:23:44 +08:00
|
|
|
v1.r = VT_LOCAL | VT_LVAL;
|
2015-11-18 03:09:35 +08:00
|
|
|
v1.c.i = fc;
|
2020-05-24 02:27:43 +08:00
|
|
|
v1.sym = NULL;
|
2021-10-22 13:39:54 +08:00
|
|
|
load(r, &v1);
|
2002-01-27 01:23:44 +08:00
|
|
|
fc = 0;
|
|
|
|
}
|
|
|
|
|
2001-12-23 08:34:26 +08:00
|
|
|
if ((ft & VT_BTYPE) == VT_DOUBLE)
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xdc);
|
2001-12-23 08:34:26 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xd8);
|
|
|
|
gen_modrm(a, r, vtop->sym, fc);
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop--;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-04 05:14:59 +08:00
|
|
|
/* convert integers to fp 't' type. Must handle 'int', 'unsigned int'
|
|
|
|
and 'long long' cases. */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_cvt_itof(int t)
|
2001-12-13 05:16:34 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
save_reg(TREG_ST0);
|
|
|
|
gv(RC_INT);
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
2002-01-04 05:14:59 +08:00
|
|
|
/* signed long long to float/double/long double (unsigned case
|
|
|
|
is handled generically) */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x50 + vtop->r2); /* push r2 */
|
|
|
|
o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
|
|
|
o(0x242cdf); /* fildll (%esp) */
|
|
|
|
o(0x08c483); /* add $8, %esp */
|
|
|
|
vtop->r2 = VT_CONST;
|
|
|
|
} else if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
|
2002-01-04 05:14:59 +08:00
|
|
|
(VT_INT | VT_UNSIGNED)) {
|
2001-12-14 06:28:53 +08:00
|
|
|
/* unsigned int to float/double/long double */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x6a); /* push $0 */
|
|
|
|
g(0x00);
|
|
|
|
o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
|
|
|
o(0x242cdf); /* fildll (%esp) */
|
|
|
|
o(0x08c483); /* add $8, %esp */
|
2001-12-13 05:16:34 +08:00
|
|
|
} else {
|
2001-12-14 06:28:53 +08:00
|
|
|
/* int to float/double/long double */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x50 + (vtop->r & VT_VALMASK)); /* push r */
|
|
|
|
o(0x2404db); /* fildl (%esp) */
|
|
|
|
o(0x04c483); /* add $4, %esp */
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop->r2 = VT_CONST;
|
|
|
|
vtop->r = TREG_ST0;
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* convert fp to int 't' type */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_cvt_ftoi(int t)
|
2001-12-18 05:56:48 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
int bt = vtop->type.t & VT_BTYPE;
|
2014-01-07 02:07:08 +08:00
|
|
|
if (bt == VT_FLOAT)
|
2021-10-22 13:39:54 +08:00
|
|
|
vpush_helper_func(TOK___fixsfdi);
|
2014-01-07 02:07:08 +08:00
|
|
|
else if (bt == VT_LDOUBLE)
|
2021-10-22 13:39:54 +08:00
|
|
|
vpush_helper_func(TOK___fixxfdi);
|
2014-01-07 02:07:08 +08:00
|
|
|
else
|
2021-10-22 13:39:54 +08:00
|
|
|
vpush_helper_func(TOK___fixdfdi);
|
|
|
|
vswap();
|
|
|
|
gfunc_call(1);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = REG_IRET;
|
2019-12-11 07:37:18 +08:00
|
|
|
if ((t & VT_BTYPE) == VT_LLONG)
|
2021-10-22 13:39:54 +08:00
|
|
|
vtop->r2 = REG_IRE2;
|
2001-12-13 05:16:34 +08:00
|
|
|
}
|
|
|
|
|
2001-12-18 05:56:48 +08:00
|
|
|
/* convert from one floating point type to another */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_cvt_ftof(int t)
|
2001-12-18 05:56:48 +08:00
|
|
|
{
|
|
|
|
/* all we have to do on i386 is to put the float in a register */
|
2021-10-22 13:39:54 +08:00
|
|
|
gv(RC_FLOAT);
|
2001-12-18 05:56:48 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 01:51:28 +08:00
|
|
|
/* char/short to int conversion */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_cvt_csti(int t)
|
2019-12-17 01:51:28 +08:00
|
|
|
{
|
|
|
|
int r, sz, xl;
|
2021-10-22 13:39:54 +08:00
|
|
|
r = gv(RC_INT);
|
2019-12-17 01:51:28 +08:00
|
|
|
sz = !(t & VT_UNSIGNED);
|
|
|
|
xl = (t & VT_BTYPE) == VT_SHORT;
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xc0b60f /* mov[sz] %a[xl], %eax */
|
2019-12-17 01:51:28 +08:00
|
|
|
| (sz << 3 | xl) << 8
|
|
|
|
| (r << 3 | r) << 16
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-24 01:17:38 +08:00
|
|
|
/* increment tcov counter */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_increment_tcov (SValue *sv)
|
2021-01-24 01:17:38 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x0583); /* addl $1, xxx */
|
|
|
|
greloc(cur_text_section, sv->sym, ind, R_386_32);
|
|
|
|
gen_le32(0);
|
|
|
|
o(1);
|
|
|
|
o(0x1583); /* addcl $0, xxx */
|
|
|
|
greloc(cur_text_section, sv->sym, ind, R_386_32);
|
|
|
|
gen_le32(4);
|
|
|
|
g(0);
|
2021-01-24 01:17:38 +08:00
|
|
|
}
|
|
|
|
|
2002-11-03 08:43:01 +08:00
|
|
|
/* computed goto support */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void ggoto(void)
|
2002-11-03 08:43:01 +08:00
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
gcall_or_jmp(1);
|
|
|
|
vtop--;
|
2002-11-03 08:43:01 +08:00
|
|
|
}
|
|
|
|
|
2002-01-06 00:17:34 +08:00
|
|
|
/* bound check support functions */
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2020-01-19 18:15:12 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static void gen_bounds_prolog(void)
|
2020-01-19 18:15:12 +08:00
|
|
|
{
|
|
|
|
/* leave some room for bound checking code */
|
2021-10-22 13:39:54 +08:00
|
|
|
func_bound_offset = lbounds_section->data_offset;
|
|
|
|
func_bound_ind = ind;
|
|
|
|
func_bound_add_epilog = 0;
|
|
|
|
oad(0xb8, 0); /* lbound section pointer */
|
|
|
|
oad(0xb8, 0); /* call to function */
|
2020-01-19 18:15:12 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
static void gen_bounds_epilog(void)
|
2020-01-19 18:15:12 +08:00
|
|
|
{
|
|
|
|
addr_t saved_ind;
|
|
|
|
addr_t *bounds_ptr;
|
|
|
|
Sym *sym_data;
|
2021-10-22 13:39:54 +08:00
|
|
|
int offset_modified = func_bound_offset != lbounds_section->data_offset;
|
2020-01-19 18:15:12 +08:00
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
if (!offset_modified && !func_bound_add_epilog)
|
2020-05-05 20:36:35 +08:00
|
|
|
return;
|
|
|
|
|
2020-01-19 18:15:12 +08:00
|
|
|
/* add end of table info */
|
2021-10-22 13:39:54 +08:00
|
|
|
bounds_ptr = section_ptr_add(lbounds_section, sizeof(addr_t));
|
2020-01-19 18:15:12 +08:00
|
|
|
*bounds_ptr = 0;
|
|
|
|
|
2021-10-22 13:39:54 +08:00
|
|
|
sym_data = get_sym_ref(&char_pointer_type, lbounds_section,
|
Add dwarf support
The new gcc12 release does not support stabs any more.
This was a good reason to add support for dwarf.
The stabs code still works and is used if configure option --dwarf
is not used.
Tested on x86_64, i386, arm, arm64, riscv64 with dwarf-5.
Some debuggers may not support dwarf-5. Try using older dwarf versions
i that case.
The tccmacho.c code probably need some support for dwarf.
arm-gen.c, arm64-gen.c, i386-gen.c, riscv64-gen.c, x86_64-gen.
- fix get_sym_ref symbol size
arm-link.c, arm64-link.c, i386-link.c, riscv64-link.c, x86_64-link.c
- add R_DATA_32U
libtcc.c:
- parse -gdwarf option
tcc.c:
- add dwarf option
tcc.h:
- add dwarf option and sections
tccelf.c:
- init dwarf sections
- avoid adding sh_addr for dwarf sections
- remove dwarf relocs for output dll
- add dwarf sections for tccrun
tccgen.c:
- add dwarf defines + global data
- add dwarf_* functions
- mix dwarf code with stabs code
- a trick is used to emit function name in .debug_line section so
only this section has to be parsed instead of .debug_info and
.debug_abbrev.
- fix init debug_modes
tccrun.c:
- add dwarf sections in rt_context
- init them in tcc_run
- add new dwarf code rt_printline_dwarf to find file/function
dwarf.h:
- New file
tcc-doc.texi:
- document dwarf
configure:
- add dwarf option
lib/Makefile
- change -gstabs into -gdwarf
lib/bt-exe.c, tests/tests2/Makefile, tests/tests2/126_bound_global:
- Add __bound_init call
- Add new testcase to test it
2022-05-05 15:10:37 +08:00
|
|
|
func_bound_offset, PTR_SIZE);
|
2020-05-25 18:26:55 +08:00
|
|
|
|
|
|
|
/* generate bound local allocation */
|
2020-06-16 13:39:48 +08:00
|
|
|
if (offset_modified) {
|
2021-10-22 13:39:54 +08:00
|
|
|
saved_ind = ind;
|
|
|
|
ind = func_bound_ind;
|
|
|
|
greloc(cur_text_section, sym_data, ind + 1, R_386_32);
|
|
|
|
ind = ind + 5;
|
|
|
|
gen_static_call(TOK___bound_local_new);
|
|
|
|
ind = saved_ind;
|
2020-05-25 18:26:55 +08:00
|
|
|
}
|
2020-01-19 18:15:12 +08:00
|
|
|
|
|
|
|
/* generate bound check local freeing */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x5250); /* save returned value, if any */
|
|
|
|
greloc(cur_text_section, sym_data, ind + 1, R_386_32);
|
|
|
|
oad(0xb8, 0); /* mov %eax, xxx */
|
|
|
|
gen_static_call(TOK___bound_local_delete);
|
|
|
|
o(0x585a); /* restore returned value, if any */
|
2020-01-19 18:15:12 +08:00
|
|
|
}
|
2002-03-04 06:45:55 +08:00
|
|
|
#endif
|
2002-01-06 00:17:34 +08:00
|
|
|
|
2013-04-28 03:39:34 +08:00
|
|
|
/* Save the stack pointer onto the stack */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_vla_sp_save(int addr) {
|
2013-04-28 03:39:34 +08:00
|
|
|
/* mov %esp,addr(%ebp)*/
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x89);
|
|
|
|
gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
|
2013-04-28 03:39:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the SP from a location on the stack */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_vla_sp_restore(int addr) {
|
|
|
|
o(0x8b);
|
|
|
|
gen_modrm(TREG_ESP, VT_LOCAL, NULL, addr);
|
2013-04-28 03:39:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Subtract from the stack pointer, and push the resulting value onto the stack */
|
2021-10-22 13:39:54 +08:00
|
|
|
ST_FUNC void gen_vla_alloc(CType *type, int align) {
|
2019-12-12 19:56:06 +08:00
|
|
|
int use_call = 0;
|
|
|
|
|
|
|
|
#if defined(CONFIG_TCC_BCHECK)
|
2021-10-22 13:39:54 +08:00
|
|
|
use_call = tcc_state->do_bounds_check;
|
2019-12-12 19:56:06 +08:00
|
|
|
#endif
|
|
|
|
#ifdef TCC_TARGET_PE /* alloca does more than just adjust %rsp on Windows */
|
|
|
|
use_call = 1;
|
2013-04-28 03:39:34 +08:00
|
|
|
#endif
|
2019-12-12 19:56:06 +08:00
|
|
|
if (use_call)
|
|
|
|
{
|
2021-10-22 13:39:54 +08:00
|
|
|
vpush_helper_func(TOK_alloca);
|
|
|
|
vswap(); /* Move alloca ref past allocation size */
|
|
|
|
gfunc_call(1);
|
2019-12-12 19:56:06 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int r;
|
2021-10-22 13:39:54 +08:00
|
|
|
r = gv(RC_INT); /* allocation size */
|
2019-12-12 19:56:06 +08:00
|
|
|
/* sub r,%rsp */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0x2b);
|
|
|
|
o(0xe0 | r);
|
2019-12-12 19:56:06 +08:00
|
|
|
/* We align to 16 bytes rather than align */
|
|
|
|
/* and ~15, %esp */
|
2021-10-22 13:39:54 +08:00
|
|
|
o(0xf0e483);
|
|
|
|
vpop();
|
2019-12-12 19:56:06 +08:00
|
|
|
}
|
2013-04-28 03:39:34 +08:00
|
|
|
}
|
|
|
|
|
2001-12-13 05:16:34 +08:00
|
|
|
/* end of X86 code generator */
|
|
|
|
/*************************************************************/
|
2009-12-20 08:53:49 +08:00
|
|
|
#endif
|
|
|
|
/*************************************************************/
|