2009-05-06 02:17:11 +08:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
2015-07-30 04:53:57 +08:00
|
|
|
*
|
2009-05-06 02:17:11 +08:00
|
|
|
* Copyright (c) 2001-2004 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 08:53:49 +08:00
|
|
|
#include "tcc.h"
|
|
|
|
/********************************************************/
|
|
|
|
/* global variables */
|
|
|
|
|
|
|
|
/* loc : local variable index
|
|
|
|
ind : output code index
|
|
|
|
rsym: return symbol
|
|
|
|
anon_sym: anonymous symbol index
|
|
|
|
*/
|
|
|
|
ST_DATA int rsym, anon_sym, ind, loc;
|
|
|
|
|
|
|
|
ST_DATA Sym *sym_free_first;
|
|
|
|
ST_DATA void **sym_pools;
|
|
|
|
ST_DATA int nb_sym_pools;
|
|
|
|
|
|
|
|
ST_DATA Sym *global_stack;
|
|
|
|
ST_DATA Sym *local_stack;
|
|
|
|
ST_DATA Sym *define_stack;
|
|
|
|
ST_DATA Sym *global_label_stack;
|
|
|
|
ST_DATA Sym *local_label_stack;
|
2018-12-20 17:55:22 +08:00
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
static Sym *all_cleanups, *current_cleanups, *pending_gotos;
|
|
|
|
static int ncleanups;
|
2018-12-20 17:55:22 +08:00
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
static int local_scope;
|
2016-10-06 00:34:17 +08:00
|
|
|
static int in_sizeof;
|
2016-10-15 21:55:31 +08:00
|
|
|
static int section_sym;
|
2009-12-20 08:53:49 +08:00
|
|
|
|
2015-05-04 09:09:05 +08:00
|
|
|
ST_DATA int vlas_in_scope; /* number of VLAs that are currently in scope */
|
2013-04-28 03:39:34 +08:00
|
|
|
ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
|
2015-05-04 09:09:05 +08:00
|
|
|
ST_DATA int vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
|
2013-04-28 03:39:34 +08:00
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop, *pvtop;
|
2009-12-20 08:53:49 +08:00
|
|
|
|
|
|
|
ST_DATA int const_wanted; /* true if constant wanted */
|
2017-07-16 18:10:00 +08:00
|
|
|
ST_DATA int nocode_wanted; /* no code generation wanted */
|
|
|
|
#define NODATA_WANTED (nocode_wanted > 0) /* no static data output wanted either */
|
|
|
|
#define STATIC_DATA_WANTED (nocode_wanted & 0xC0000000) /* only static data output */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
|
|
|
|
ST_DATA CType func_vt; /* current function return type (used by return instruction) */
|
2014-01-06 22:27:39 +08:00
|
|
|
ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_DATA int func_vc;
|
|
|
|
ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
ST_DATA const char *funcname;
|
2017-07-15 01:26:01 +08:00
|
|
|
ST_DATA int g_debug;
|
2009-12-20 08:53:49 +08:00
|
|
|
|
2017-09-25 00:57:48 +08:00
|
|
|
ST_DATA CType char_pointer_type, func_old_type, int_type, size_type, ptrdiff_type;
|
2009-12-20 08:53:49 +08:00
|
|
|
|
2016-09-21 23:35:29 +08:00
|
|
|
ST_DATA struct switch_t {
|
|
|
|
struct case_t {
|
2016-11-06 12:02:11 +08:00
|
|
|
int64_t v1, v2;
|
|
|
|
int sym;
|
2016-09-21 23:35:29 +08:00
|
|
|
} **p; int n; /* list of case ranges */
|
|
|
|
int def_sym; /* default symbol */
|
|
|
|
} *cur_switch; /* current switch */
|
|
|
|
|
2019-01-10 03:32:23 +08:00
|
|
|
#define MAX_TEMP_LOCAL_VARIABLE_NUMBER 0x4
|
|
|
|
/*list of temporary local variables on the stack in current function. */
|
|
|
|
ST_DATA struct temp_local_variable {
|
|
|
|
int location; //offset on stack. Svalue.c.i
|
|
|
|
short size;
|
|
|
|
short align;
|
|
|
|
} arr_temp_local_vars[MAX_TEMP_LOCAL_VARIABLE_NUMBER];
|
|
|
|
short nb_temp_local_vars;
|
2018-12-20 17:55:22 +08:00
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
/* ------------------------------------------------------------------------- */
|
2016-12-19 00:23:33 +08:00
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gen_cast(CType *type);
|
2017-07-09 18:34:11 +08:00
|
|
|
static void gen_cast_s(int t);
|
2009-12-20 08:53:49 +08:00
|
|
|
static inline CType *pointed_type(CType *type);
|
|
|
|
static int is_compatible_types(CType *type1, CType *type2);
|
|
|
|
static int parse_btype(CType *type, AttributeDef *ad);
|
2017-03-06 10:25:33 +08:00
|
|
|
static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td);
|
2009-12-20 08:53:49 +08:00
|
|
|
static void parse_expr_type(CType *type);
|
2017-03-12 12:25:09 +08:00
|
|
|
static void init_putv(CType *type, Section *sec, unsigned long c);
|
2019-03-18 10:26:19 +08:00
|
|
|
static void decl_initializer(CType *type, Section *sec, unsigned long c, int flags);
|
2019-04-29 17:18:57 +08:00
|
|
|
static void block(int *bsym, Sym *bcl, int *csym, Sym *ccl, int is_expr);
|
2015-11-20 18:22:56 +08:00
|
|
|
static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, int scope);
|
2017-07-21 04:21:27 +08:00
|
|
|
static void decl(int l);
|
2017-03-11 09:13:59 +08:00
|
|
|
static int decl0(int l, int is_for_loop_init, Sym *);
|
2009-12-20 08:53:49 +08:00
|
|
|
static void expr_eq(void);
|
2011-04-07 00:17:03 +08:00
|
|
|
static void vla_runtime_type_size(CType *type, int *a);
|
2015-05-04 09:09:05 +08:00
|
|
|
static void vla_sp_restore(void);
|
|
|
|
static void vla_sp_restore_root(void);
|
2017-07-09 18:38:25 +08:00
|
|
|
static int is_compatible_unqualified_types(CType *type1, CType *type2);
|
2016-11-06 12:02:11 +08:00
|
|
|
static inline int64_t expr_const64(void);
|
2017-07-21 04:21:27 +08:00
|
|
|
static void vpush64(int ty, unsigned long long v);
|
|
|
|
static void vpush(CType *type);
|
|
|
|
static int gvtst(int inv, int t);
|
2016-10-18 05:24:01 +08:00
|
|
|
static void gen_inline_functions(TCCState *s);
|
2016-12-09 18:42:41 +08:00
|
|
|
static void skip_or_save_block(TokenString **str);
|
2017-07-09 18:38:59 +08:00
|
|
|
static void gv_dup(void);
|
2019-01-10 03:32:23 +08:00
|
|
|
static int get_temp_local_var(int size,int align);
|
|
|
|
static void clear_temp_local_var_list();
|
2009-12-20 08:53:49 +08:00
|
|
|
|
2018-12-20 17:55:22 +08:00
|
|
|
|
|
|
|
static void reset_local_scope(void)
|
|
|
|
{
|
2019-01-28 08:21:38 +08:00
|
|
|
if (current_cleanups)
|
|
|
|
tcc_error("ICE current_cleanups");
|
|
|
|
sym_pop(&all_cleanups, NULL, 0);
|
2018-12-20 17:55:22 +08:00
|
|
|
local_scope = 0;
|
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_INLN int is_float(int t)
|
|
|
|
{
|
|
|
|
int bt;
|
|
|
|
bt = t & VT_BTYPE;
|
2013-04-19 22:33:16 +08:00
|
|
|
return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
|
2009-12-20 08:53:49 +08:00
|
|
|
}
|
|
|
|
|
2011-08-01 07:10:36 +08:00
|
|
|
/* we use our own 'finite' function to avoid potential problems with
|
|
|
|
non standard math libs */
|
|
|
|
/* XXX: endianness dependent */
|
|
|
|
ST_FUNC int ieee_finite(double d)
|
|
|
|
{
|
2014-01-07 21:57:07 +08:00
|
|
|
int p[4];
|
|
|
|
memcpy(p, &d, sizeof(double));
|
2011-08-01 07:10:36 +08:00
|
|
|
return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
|
|
|
|
}
|
|
|
|
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
/* compiling intel long double natively */
|
|
|
|
#if (defined __i386__ || defined __x86_64__) \
|
|
|
|
&& (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64)
|
|
|
|
# define TCC_IS_NATIVE_387
|
|
|
|
#endif
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void test_lvalue(void)
|
|
|
|
{
|
|
|
|
if (!(vtop->r & VT_LVAL))
|
|
|
|
expect("lvalue");
|
|
|
|
}
|
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
ST_FUNC void check_vstack(void)
|
|
|
|
{
|
|
|
|
if (pvtop != vtop)
|
|
|
|
tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:21:43 +08:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* vstack debugging aid */
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
void pv (const char *lbl, int a, int b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = a; i < a + b; ++i) {
|
|
|
|
SValue *p = &vtop[-i];
|
|
|
|
printf("%s vtop[-%d] : type.t:%04x r:%04x r2:%04x c.i:%d\n",
|
|
|
|
lbl, i, p->type.t, p->r, p->r2, (int)p->c.i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
/* ------------------------------------------------------------------------- */
|
2017-02-21 01:58:08 +08:00
|
|
|
/* start of translation unit info */
|
|
|
|
ST_FUNC void tcc_debug_start(TCCState *s1)
|
2016-10-15 21:55:31 +08:00
|
|
|
{
|
|
|
|
if (s1->do_debug) {
|
|
|
|
char buf[512];
|
|
|
|
|
|
|
|
/* file info: full path + filename */
|
|
|
|
section_sym = put_elf_sym(symtab_section, 0, 0,
|
|
|
|
ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
|
|
|
|
text_section->sh_num, NULL);
|
|
|
|
getcwd(buf, sizeof(buf));
|
|
|
|
#ifdef _WIN32
|
|
|
|
normalize_slashes(buf);
|
|
|
|
#endif
|
|
|
|
pstrcat(buf, sizeof(buf), "/");
|
|
|
|
put_stabs_r(buf, N_SO, 0, 0,
|
|
|
|
text_section->data_offset, text_section, section_sym);
|
|
|
|
put_stabs_r(file->filename, N_SO, 0, 0,
|
|
|
|
text_section->data_offset, text_section, section_sym);
|
2017-02-21 01:58:08 +08:00
|
|
|
last_ind = 0;
|
|
|
|
last_line_num = 0;
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
2017-02-21 01:58:08 +08:00
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
/* an elf symbol of type STT_FILE must be put so that STB_LOCAL
|
|
|
|
symbols can be safely used */
|
|
|
|
put_elf_sym(symtab_section, 0, 0,
|
|
|
|
ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
|
|
|
|
SHN_ABS, file->filename);
|
2017-02-21 01:58:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* put end of translation unit info */
|
|
|
|
ST_FUNC void tcc_debug_end(TCCState *s1)
|
|
|
|
{
|
|
|
|
if (!s1->do_debug)
|
|
|
|
return;
|
|
|
|
put_stabs_r(NULL, N_SO, 0, 0,
|
|
|
|
text_section->data_offset, text_section, section_sym);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate line number info */
|
|
|
|
ST_FUNC void tcc_debug_line(TCCState *s1)
|
|
|
|
{
|
|
|
|
if (!s1->do_debug)
|
|
|
|
return;
|
|
|
|
if ((last_line_num != file->line_num || last_ind != ind)) {
|
|
|
|
put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
|
|
|
|
last_ind = ind;
|
|
|
|
last_line_num = file->line_num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put function symbol */
|
|
|
|
ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
|
|
|
|
if (!s1->do_debug)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* stabs info */
|
|
|
|
/* XXX: we put here a dummy type */
|
|
|
|
snprintf(buf, sizeof(buf), "%s:%c1",
|
|
|
|
funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
|
|
|
|
put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
|
|
|
|
cur_text_section, sym->c);
|
|
|
|
/* //gr gdb wants a line at the function */
|
|
|
|
put_stabn(N_SLINE, 0, file->line_num, 0);
|
|
|
|
|
|
|
|
last_ind = 0;
|
|
|
|
last_line_num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put function size */
|
|
|
|
ST_FUNC void tcc_debug_funcend(TCCState *s1, int size)
|
|
|
|
{
|
|
|
|
if (!s1->do_debug)
|
|
|
|
return;
|
|
|
|
put_stabn(N_FUN, 0, 0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
2017-07-21 04:21:27 +08:00
|
|
|
ST_FUNC int tccgen_compile(TCCState *s1)
|
2017-02-21 01:58:08 +08:00
|
|
|
{
|
|
|
|
cur_text_section = NULL;
|
|
|
|
funcname = "";
|
|
|
|
anon_sym = SYM_FIRST_ANOM;
|
|
|
|
section_sym = 0;
|
|
|
|
const_wanted = 0;
|
2017-07-16 18:10:00 +08:00
|
|
|
nocode_wanted = 0x80000000;
|
2017-02-21 01:58:08 +08:00
|
|
|
|
|
|
|
/* define some often used types */
|
|
|
|
int_type.t = VT_INT;
|
|
|
|
char_pointer_type.t = VT_BYTE;
|
|
|
|
mk_pointer(&char_pointer_type);
|
|
|
|
#if PTR_SIZE == 4
|
2017-09-25 00:57:48 +08:00
|
|
|
size_type.t = VT_INT | VT_UNSIGNED;
|
|
|
|
ptrdiff_type.t = VT_INT;
|
|
|
|
#elif LONG_SIZE == 4
|
|
|
|
size_type.t = VT_LLONG | VT_UNSIGNED;
|
|
|
|
ptrdiff_type.t = VT_LLONG;
|
2017-02-21 01:58:08 +08:00
|
|
|
#else
|
2017-09-25 00:57:48 +08:00
|
|
|
size_type.t = VT_LONG | VT_LLONG | VT_UNSIGNED;
|
|
|
|
ptrdiff_type.t = VT_LONG | VT_LLONG;
|
2017-02-21 01:58:08 +08:00
|
|
|
#endif
|
|
|
|
func_old_type.t = VT_FUNC;
|
2017-07-09 18:34:11 +08:00
|
|
|
func_old_type.ref = sym_push(SYM_FIELD, &int_type, 0, 0);
|
|
|
|
func_old_type.ref->f.func_call = FUNC_CDECL;
|
|
|
|
func_old_type.ref->f.func_type = FUNC_OLD;
|
2017-02-21 01:58:08 +08:00
|
|
|
|
|
|
|
tcc_debug_start(s1);
|
2016-10-15 21:55:31 +08:00
|
|
|
|
|
|
|
#ifdef TCC_TARGET_ARM
|
|
|
|
arm_init(s1);
|
|
|
|
#endif
|
|
|
|
|
2017-07-21 04:21:27 +08:00
|
|
|
#ifdef INC_DEBUG
|
|
|
|
printf("%s: **** new file\n", file->filename);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
|
|
|
|
next();
|
|
|
|
decl(VT_CONST);
|
2016-10-18 05:24:01 +08:00
|
|
|
gen_inline_functions(s1);
|
2016-10-15 21:55:31 +08:00
|
|
|
check_vstack();
|
|
|
|
/* end of translation unit info */
|
2017-02-21 01:58:08 +08:00
|
|
|
tcc_debug_end(s1);
|
2017-07-21 04:21:27 +08:00
|
|
|
return 0;
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 14:34:52 +08:00
|
|
|
/* ------------------------------------------------------------------------- */
|
2017-11-27 11:03:03 +08:00
|
|
|
ST_FUNC ElfSym *elfsym(Sym *s)
|
|
|
|
{
|
|
|
|
if (!s || !s->c)
|
|
|
|
return NULL;
|
2017-12-13 00:57:20 +08:00
|
|
|
return &((ElfSym *)symtab_section->data)[s->c];
|
2017-11-27 11:03:03 +08:00
|
|
|
}
|
|
|
|
|
2017-05-08 12:38:09 +08:00
|
|
|
/* apply storage attributes to Elf symbol */
|
2017-11-30 22:15:22 +08:00
|
|
|
ST_FUNC void update_storage(Sym *sym)
|
2017-04-04 14:34:52 +08:00
|
|
|
{
|
2017-11-30 22:15:22 +08:00
|
|
|
ElfSym *esym;
|
|
|
|
int sym_bind, old_sym_bind;
|
|
|
|
|
|
|
|
esym = elfsym(sym);
|
2017-11-27 11:03:03 +08:00
|
|
|
if (!esym)
|
2017-04-04 14:34:52 +08:00
|
|
|
return;
|
2017-11-30 22:15:22 +08:00
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
if (sym->a.visibility)
|
2017-04-04 14:34:52 +08:00
|
|
|
esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1))
|
2017-07-09 18:34:11 +08:00
|
|
|
| sym->a.visibility;
|
2017-11-30 22:15:22 +08:00
|
|
|
|
2019-06-17 09:34:03 +08:00
|
|
|
if ((sym->type.t & VT_STATIC)
|
|
|
|
|| (sym->type.t & (VT_EXTERN | VT_INLINE)) == VT_INLINE)
|
2017-11-30 22:15:22 +08:00
|
|
|
sym_bind = STB_LOCAL;
|
|
|
|
else if (sym->a.weak)
|
|
|
|
sym_bind = STB_WEAK;
|
|
|
|
else
|
|
|
|
sym_bind = STB_GLOBAL;
|
|
|
|
old_sym_bind = ELFW(ST_BIND)(esym->st_info);
|
|
|
|
if (sym_bind != old_sym_bind) {
|
|
|
|
esym->st_info = ELFW(ST_INFO)(sym_bind, ELFW(ST_TYPE)(esym->st_info));
|
|
|
|
}
|
|
|
|
|
2017-04-04 14:34:52 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2017-07-09 18:34:11 +08:00
|
|
|
if (sym->a.dllimport)
|
|
|
|
esym->st_other |= ST_PE_IMPORT;
|
|
|
|
if (sym->a.dllexport)
|
2017-04-04 14:34:52 +08:00
|
|
|
esym->st_other |= ST_PE_EXPORT;
|
|
|
|
#endif
|
2017-11-30 22:15:22 +08:00
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
#if 0
|
2017-11-30 22:15:22 +08:00
|
|
|
printf("storage %s: bind=%c vis=%d exp=%d imp=%d\n",
|
2017-07-09 18:34:11 +08:00
|
|
|
get_tok_str(sym->v, NULL),
|
2017-11-30 22:15:22 +08:00
|
|
|
sym_bind == STB_WEAK ? 'w' : sym_bind == STB_LOCAL ? 'l' : 'g',
|
2017-07-09 18:34:11 +08:00
|
|
|
sym->a.visibility,
|
|
|
|
sym->a.dllexport,
|
|
|
|
sym->a.dllimport
|
|
|
|
);
|
|
|
|
#endif
|
2017-04-04 14:34:52 +08:00
|
|
|
}
|
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* update sym->c so that it points to an external symbol in section
|
|
|
|
'section' with value 'value' */
|
|
|
|
|
2017-12-13 00:57:20 +08:00
|
|
|
ST_FUNC void put_extern_sym2(Sym *sym, int sh_num,
|
2016-10-15 21:55:31 +08:00
|
|
|
addr_t value, unsigned long size,
|
|
|
|
int can_add_underscore)
|
|
|
|
{
|
2017-12-13 00:57:20 +08:00
|
|
|
int sym_type, sym_bind, info, other, t;
|
2017-11-27 11:03:03 +08:00
|
|
|
ElfSym *esym;
|
2016-10-15 21:55:31 +08:00
|
|
|
const char *name;
|
|
|
|
char buf1[256];
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
char buf[32];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!sym->c) {
|
|
|
|
name = get_tok_str(sym->v, NULL);
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
if (tcc_state->do_bounds_check) {
|
|
|
|
/* XXX: avoid doing that for statics ? */
|
|
|
|
/* if bound checking is activated, we change some function
|
|
|
|
names by adding the "__bound" prefix */
|
|
|
|
switch(sym->v) {
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
/* XXX: we rely only on malloc hooks */
|
|
|
|
case TOK_malloc:
|
|
|
|
case TOK_free:
|
|
|
|
case TOK_realloc:
|
|
|
|
case TOK_memalign:
|
|
|
|
case TOK_calloc:
|
|
|
|
#endif
|
|
|
|
case TOK_memcpy:
|
|
|
|
case TOK_memmove:
|
|
|
|
case TOK_memset:
|
|
|
|
case TOK_strlen:
|
|
|
|
case TOK_strcpy:
|
|
|
|
case TOK_alloca:
|
|
|
|
strcpy(buf, "__bound_");
|
|
|
|
strcat(buf, name);
|
|
|
|
name = buf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-04 14:34:52 +08:00
|
|
|
t = sym->type.t;
|
|
|
|
if ((t & VT_BTYPE) == VT_FUNC) {
|
|
|
|
sym_type = STT_FUNC;
|
|
|
|
} else if ((t & VT_BTYPE) == VT_VOID) {
|
|
|
|
sym_type = STT_NOTYPE;
|
|
|
|
} else {
|
|
|
|
sym_type = STT_OBJECT;
|
|
|
|
}
|
2019-06-17 09:34:03 +08:00
|
|
|
if ((t & VT_STATIC) || (t & (VT_EXTERN | VT_INLINE)) == VT_INLINE)
|
2017-04-04 14:34:52 +08:00
|
|
|
sym_bind = STB_LOCAL;
|
|
|
|
else
|
|
|
|
sym_bind = STB_GLOBAL;
|
2016-10-15 21:55:31 +08:00
|
|
|
other = 0;
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
if (sym_type == STT_FUNC && sym->type.ref) {
|
|
|
|
Sym *ref = sym->type.ref;
|
2018-07-22 07:54:01 +08:00
|
|
|
if (ref->a.nodecorate) {
|
|
|
|
can_add_underscore = 0;
|
|
|
|
}
|
2017-07-09 18:34:11 +08:00
|
|
|
if (ref->f.func_call == FUNC_STDCALL && can_add_underscore) {
|
|
|
|
sprintf(buf1, "_%s@%d", name, ref->f.func_args * PTR_SIZE);
|
2016-10-15 21:55:31 +08:00
|
|
|
name = buf1;
|
|
|
|
other |= ST_PE_STDCALL;
|
|
|
|
can_add_underscore = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (tcc_state->leading_underscore && can_add_underscore) {
|
|
|
|
buf1[0] = '_';
|
|
|
|
pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
|
|
|
|
name = buf1;
|
|
|
|
}
|
2017-04-04 14:34:52 +08:00
|
|
|
if (sym->asm_label)
|
2016-10-15 21:55:31 +08:00
|
|
|
name = get_tok_str(sym->asm_label, NULL);
|
|
|
|
info = ELFW(ST_INFO)(sym_bind, sym_type);
|
2017-12-13 00:33:37 +08:00
|
|
|
sym->c = put_elf_sym(symtab_section, value, size, info, other, sh_num, name);
|
2016-10-15 21:55:31 +08:00
|
|
|
} else {
|
2017-11-27 11:03:03 +08:00
|
|
|
esym = elfsym(sym);
|
2016-10-15 21:55:31 +08:00
|
|
|
esym->st_value = value;
|
|
|
|
esym->st_size = size;
|
|
|
|
esym->st_shndx = sh_num;
|
|
|
|
}
|
2017-04-04 14:34:52 +08:00
|
|
|
update_storage(sym);
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ST_FUNC void put_extern_sym(Sym *sym, Section *section,
|
|
|
|
addr_t value, unsigned long size)
|
|
|
|
{
|
2017-12-13 00:57:20 +08:00
|
|
|
int sh_num = section ? section->sh_num : SHN_UNDEF;
|
|
|
|
put_extern_sym2(sym, sh_num, value, size, 1);
|
2016-10-15 21:55:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add a new relocation entry to symbol 'sym' in section 's' */
|
|
|
|
ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type,
|
|
|
|
addr_t addend)
|
|
|
|
{
|
|
|
|
int c = 0;
|
2016-12-19 00:23:33 +08:00
|
|
|
|
|
|
|
if (nocode_wanted && s == cur_text_section)
|
|
|
|
return;
|
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
if (sym) {
|
|
|
|
if (0 == sym->c)
|
|
|
|
put_extern_sym(sym, NULL, 0, 0);
|
|
|
|
c = sym->c;
|
|
|
|
}
|
2016-12-19 00:23:33 +08:00
|
|
|
|
2016-10-15 21:55:31 +08:00
|
|
|
/* now we can add ELF relocation info */
|
|
|
|
put_elf_reloca(symtab_section, s, offset, type, c, addend);
|
|
|
|
}
|
|
|
|
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2016-10-15 21:55:31 +08:00
|
|
|
ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type)
|
|
|
|
{
|
|
|
|
greloca(s, sym, offset, type, 0);
|
|
|
|
}
|
2017-05-13 14:59:06 +08:00
|
|
|
#endif
|
2016-10-15 21:55:31 +08:00
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
/* symbol allocator */
|
|
|
|
static Sym *__sym_malloc(void)
|
|
|
|
{
|
|
|
|
Sym *sym_pool, *sym, *last_sym;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
|
|
|
|
dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
|
|
|
|
|
|
|
|
last_sym = sym_free_first;
|
|
|
|
sym = sym_pool;
|
|
|
|
for(i = 0; i < SYM_POOL_NB; i++) {
|
|
|
|
sym->next = last_sym;
|
|
|
|
last_sym = sym;
|
|
|
|
sym++;
|
|
|
|
}
|
|
|
|
sym_free_first = last_sym;
|
|
|
|
return last_sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Sym *sym_malloc(void)
|
|
|
|
{
|
|
|
|
Sym *sym;
|
2016-08-15 11:09:31 +08:00
|
|
|
#ifndef SYM_DEBUG
|
2009-12-20 08:53:49 +08:00
|
|
|
sym = sym_free_first;
|
|
|
|
if (!sym)
|
|
|
|
sym = __sym_malloc();
|
|
|
|
sym_free_first = sym->next;
|
|
|
|
return sym;
|
2016-08-15 11:09:31 +08:00
|
|
|
#else
|
|
|
|
sym = tcc_malloc(sizeof(Sym));
|
|
|
|
return sym;
|
|
|
|
#endif
|
2009-12-20 08:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ST_INLN void sym_free(Sym *sym)
|
|
|
|
{
|
2016-08-15 11:09:31 +08:00
|
|
|
#ifndef SYM_DEBUG
|
2009-12-20 08:53:49 +08:00
|
|
|
sym->next = sym_free_first;
|
|
|
|
sym_free_first = sym;
|
2016-08-15 11:09:31 +08:00
|
|
|
#else
|
|
|
|
tcc_free(sym);
|
|
|
|
#endif
|
2009-12-20 08:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* push, without hashing */
|
2017-07-09 18:34:11 +08:00
|
|
|
ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c)
|
2009-12-20 08:53:49 +08:00
|
|
|
{
|
|
|
|
Sym *s;
|
2016-05-05 16:39:09 +08:00
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
s = sym_malloc();
|
2017-07-09 18:34:11 +08:00
|
|
|
memset(s, 0, sizeof *s);
|
2009-12-20 08:53:49 +08:00
|
|
|
s->v = v;
|
|
|
|
s->type.t = t;
|
|
|
|
s->c = c;
|
|
|
|
/* add in stack */
|
|
|
|
s->prev = *ps;
|
|
|
|
*ps = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find a symbol and return its associated structure. 's' is the top
|
|
|
|
of the symbol stack */
|
|
|
|
ST_FUNC Sym *sym_find2(Sym *s, int v)
|
|
|
|
{
|
|
|
|
while (s) {
|
|
|
|
if (s->v == v)
|
|
|
|
return s;
|
2014-04-07 21:12:08 +08:00
|
|
|
else if (s->v == -1)
|
|
|
|
return NULL;
|
2009-12-20 08:53:49 +08:00
|
|
|
s = s->prev;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* structure lookup */
|
|
|
|
ST_INLN Sym *struct_find(int v)
|
|
|
|
{
|
|
|
|
v -= TOK_IDENT;
|
|
|
|
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
|
|
|
return NULL;
|
|
|
|
return table_ident[v]->sym_struct;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find an identifier */
|
|
|
|
ST_INLN Sym *sym_find(int v)
|
|
|
|
{
|
|
|
|
v -= TOK_IDENT;
|
|
|
|
if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
|
|
|
return NULL;
|
|
|
|
return table_ident[v]->sym_identifier;
|
|
|
|
}
|
|
|
|
|
2019-03-09 00:58:25 +08:00
|
|
|
static int sym_scope(Sym *s)
|
|
|
|
{
|
|
|
|
if (IS_ENUM_VAL (s->type.t))
|
|
|
|
return s->type.ref->sym_scope;
|
|
|
|
else
|
|
|
|
return s->sym_scope;
|
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
/* push a given symbol on the symbol stack */
|
2017-07-09 18:34:11 +08:00
|
|
|
ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
|
2009-12-20 08:53:49 +08:00
|
|
|
{
|
|
|
|
Sym *s, **ps;
|
|
|
|
TokenSym *ts;
|
|
|
|
|
|
|
|
if (local_stack)
|
|
|
|
ps = &local_stack;
|
|
|
|
else
|
|
|
|
ps = &global_stack;
|
|
|
|
s = sym_push2(ps, v, type->t, c);
|
|
|
|
s->type.ref = type->ref;
|
|
|
|
s->r = r;
|
|
|
|
/* don't record fields or anonymous symbols */
|
|
|
|
/* XXX: simplify */
|
|
|
|
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
|
|
|
|
/* record symbol in token array */
|
|
|
|
ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
|
|
|
|
if (v & SYM_STRUCT)
|
|
|
|
ps = &ts->sym_struct;
|
|
|
|
else
|
|
|
|
ps = &ts->sym_identifier;
|
|
|
|
s->prev_tok = *ps;
|
|
|
|
*ps = s;
|
2017-07-09 18:34:11 +08:00
|
|
|
s->sym_scope = local_scope;
|
2019-03-09 00:58:25 +08:00
|
|
|
if (s->prev_tok && sym_scope(s->prev_tok) == s->sym_scope)
|
2016-05-06 02:04:00 +08:00
|
|
|
tcc_error("redeclaration of '%s'",
|
|
|
|
get_tok_str(v & ~SYM_STRUCT, NULL));
|
2009-12-20 08:53:49 +08:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push a global identifier */
|
2017-11-30 22:15:22 +08:00
|
|
|
ST_FUNC Sym *global_identifier_push(int v, int t, int c)
|
2009-12-20 08:53:49 +08:00
|
|
|
{
|
|
|
|
Sym *s, **ps;
|
2017-11-30 22:15:22 +08:00
|
|
|
s = sym_push2(&global_stack, v, t, c);
|
2019-04-18 09:36:39 +08:00
|
|
|
s->r = VT_CONST | VT_SYM;
|
2009-12-20 08:53:49 +08:00
|
|
|
/* don't record anonymous symbol */
|
|
|
|
if (v < SYM_FIRST_ANOM) {
|
|
|
|
ps = &table_ident[v - TOK_IDENT]->sym_identifier;
|
2019-04-18 09:36:39 +08:00
|
|
|
/* modify the top most local identifier, so that sym_identifier will
|
|
|
|
point to 's' when popped; happens when called from inline asm */
|
2017-11-27 11:03:03 +08:00
|
|
|
while (*ps != NULL && (*ps)->sym_scope)
|
2009-12-20 08:53:49 +08:00
|
|
|
ps = &(*ps)->prev_tok;
|
2017-11-27 11:03:03 +08:00
|
|
|
s->prev_tok = *ps;
|
2009-12-20 08:53:49 +08:00
|
|
|
*ps = s;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-08-15 11:09:31 +08:00
|
|
|
/* pop symbols until top reaches 'b'. If KEEP is non-zero don't really
|
|
|
|
pop them yet from the list, but do remove them from the token array. */
|
|
|
|
ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep)
|
2009-12-20 08:53:49 +08:00
|
|
|
{
|
|
|
|
Sym *s, *ss, **ps;
|
|
|
|
TokenSym *ts;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
s = *ptop;
|
|
|
|
while(s != b) {
|
|
|
|
ss = s->prev;
|
|
|
|
v = s->v;
|
|
|
|
/* remove symbol in token array */
|
|
|
|
/* XXX: simplify */
|
|
|
|
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
|
|
|
|
ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
|
|
|
|
if (v & SYM_STRUCT)
|
|
|
|
ps = &ts->sym_struct;
|
|
|
|
else
|
|
|
|
ps = &ts->sym_identifier;
|
|
|
|
*ps = s->prev_tok;
|
|
|
|
}
|
2016-08-15 11:09:31 +08:00
|
|
|
if (!keep)
|
|
|
|
sym_free(s);
|
2009-12-20 08:53:49 +08:00
|
|
|
s = ss;
|
|
|
|
}
|
2016-08-15 11:09:31 +08:00
|
|
|
if (!keep)
|
|
|
|
*ptop = b;
|
2009-12-20 08:53:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void vsetc(CType *type, int r, CValue *vc)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
if (vtop >= vstack + (VSTACK_SIZE - 1))
|
2014-01-07 02:56:26 +08:00
|
|
|
tcc_error("memory full (vstack)");
|
2009-05-06 02:18:10 +08:00
|
|
|
/* cannot let cpu flags if other instruction are generated. Also
|
|
|
|
avoid leaving VT_JMP anywhere except on the top of the stack
|
2016-12-20 11:49:22 +08:00
|
|
|
because it would complicate the code generator.
|
|
|
|
|
|
|
|
Don't do this when nocode_wanted. vtop might come from
|
|
|
|
!nocode_wanted regions (see 88_codeopt.c) and transforming
|
|
|
|
it to a register without actually generating code is wrong
|
|
|
|
as their value might still be used for real. All values
|
|
|
|
we push under nocode_wanted will eventually be popped
|
|
|
|
again, so that the VT_CMP/VT_JMP value will be in vtop
|
2017-02-09 02:45:31 +08:00
|
|
|
when code is unsuppressed again.
|
|
|
|
|
|
|
|
Same logic below in vswap(); */
|
2016-12-20 11:49:22 +08:00
|
|
|
if (vtop >= vstack && !nocode_wanted) {
|
2009-05-06 02:18:10 +08:00
|
|
|
v = vtop->r & VT_VALMASK;
|
|
|
|
if (v == VT_CMP || (v & ~1) == VT_JMP)
|
|
|
|
gv(RC_INT);
|
|
|
|
}
|
2017-02-09 02:45:31 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop++;
|
|
|
|
vtop->type = *type;
|
|
|
|
vtop->r = r;
|
|
|
|
vtop->r2 = VT_CONST;
|
|
|
|
vtop->c = *vc;
|
2016-10-06 10:05:30 +08:00
|
|
|
vtop->sym = NULL;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2017-02-09 02:45:31 +08:00
|
|
|
ST_FUNC void vswap(void)
|
|
|
|
{
|
|
|
|
SValue tmp;
|
|
|
|
/* cannot vswap cpu flags. See comment at vsetc() above */
|
|
|
|
if (vtop >= vstack && !nocode_wanted) {
|
|
|
|
int v = vtop->r & VT_VALMASK;
|
|
|
|
if (v == VT_CMP || (v & ~1) == VT_JMP)
|
|
|
|
gv(RC_INT);
|
|
|
|
}
|
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-1];
|
|
|
|
vtop[-1] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pop stack value */
|
|
|
|
ST_FUNC void vpop(void)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
v = vtop->r & VT_VALMASK;
|
|
|
|
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
|
|
|
/* for x86, we need to pop the FP stack */
|
|
|
|
if (v == TREG_ST0) {
|
|
|
|
o(0xd8dd); /* fstp %st(0) */
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (v == VT_JMP || v == VT_JMPI) {
|
|
|
|
/* need to put correct jump if && or || without test */
|
|
|
|
gsym(vtop->c.i);
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
}
|
|
|
|
|
2010-04-11 07:53:40 +08:00
|
|
|
/* push constant of type "type" with useless value */
|
2014-04-10 17:53:54 +08:00
|
|
|
ST_FUNC void vpush(CType *type)
|
2010-04-11 07:53:40 +08:00
|
|
|
{
|
2017-07-09 18:34:11 +08:00
|
|
|
vset(type, VT_CONST, 0);
|
2010-04-11 07:53:40 +08:00
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* push integer constant */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void vpushi(int v)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
CValue cval;
|
|
|
|
cval.i = v;
|
|
|
|
vsetc(&int_type, VT_CONST, &cval);
|
|
|
|
}
|
|
|
|
|
2012-04-16 07:13:25 +08:00
|
|
|
/* push a pointer sized constant */
|
2014-04-05 02:18:39 +08:00
|
|
|
static void vpushs(addr_t v)
|
2012-04-16 07:13:25 +08:00
|
|
|
{
|
|
|
|
CValue cval;
|
2015-11-18 03:09:35 +08:00
|
|
|
cval.i = v;
|
2012-04-16 07:13:25 +08:00
|
|
|
vsetc(&size_type, VT_CONST, &cval);
|
|
|
|
}
|
|
|
|
|
2010-05-06 08:19:00 +08:00
|
|
|
/* push arbitrary 64bit constant */
|
2014-04-10 17:53:54 +08:00
|
|
|
ST_FUNC void vpush64(int ty, unsigned long long v)
|
2010-05-06 08:19:00 +08:00
|
|
|
{
|
After several days searching why my code refactoring to remove globals was crashing,
I found the problem it was because CValue stack variables have rubish as it inital values
and assigning to a member that is smaller than the big union item and trying to
recover it later as a different member gives bak garbage.
ST_FUNC void vset(TCCState* tcc_state, CType *type, int r, int v)
{
CValue cval;
memset(&cval, 0, sizeof(CValue));
cval.i = v; //,<<<<<<<<<<< here is the main bug that mix with garbage
vsetc(tcc_state, type, r, &cval);
}
/* store a value or an expression directly in global data or in local array */
static void init_putv(TCCState* tcc_state, CType *type, Section *sec, unsigned long c,
int v, int expr_type)
{
...
case VT_PTR:
if (tcc_state->tccgen_vtop->r & VT_SYM) {
greloc(tcc_state, sec, tcc_state->tccgen_vtop->sym, c, R_DATA_PTR);
}
//<<< on the next line is where we try to get the assigned value to cvalue.i as cvalue.ull
*(addr_t *)ptr |= (tcc_state->tccgen_vtop->c.ull & bit_mask) << bit_pos;
break;
Also this patch makes vla tests pass on linux 32 bits
2014-03-27 04:14:39 +08:00
|
|
|
CValue cval;
|
2014-04-05 02:18:39 +08:00
|
|
|
CType ctype;
|
2010-05-06 08:19:00 +08:00
|
|
|
ctype.t = ty;
|
2013-02-09 02:07:11 +08:00
|
|
|
ctype.ref = NULL;
|
2015-11-18 03:09:35 +08:00
|
|
|
cval.i = v;
|
2010-05-06 08:19:00 +08:00
|
|
|
vsetc(&ctype, VT_CONST, &cval);
|
|
|
|
}
|
|
|
|
|
2013-02-09 02:07:11 +08:00
|
|
|
/* push long long constant */
|
|
|
|
static inline void vpushll(long long v)
|
|
|
|
{
|
|
|
|
vpush64(VT_LLONG, v);
|
|
|
|
}
|
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
ST_FUNC void vset(CType *type, int r, int v)
|
2017-02-09 02:45:31 +08:00
|
|
|
{
|
|
|
|
CValue cval;
|
|
|
|
|
|
|
|
cval.i = v;
|
|
|
|
vsetc(type, r, &cval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vseti(int r, int v)
|
|
|
|
{
|
|
|
|
CType type;
|
|
|
|
type.t = VT_INT;
|
2017-07-09 18:34:11 +08:00
|
|
|
type.ref = NULL;
|
2017-02-09 02:45:31 +08:00
|
|
|
vset(&type, r, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
ST_FUNC void vpushv(SValue *v)
|
|
|
|
{
|
|
|
|
if (vtop >= vstack + (VSTACK_SIZE - 1))
|
|
|
|
tcc_error("memory full (vstack)");
|
|
|
|
vtop++;
|
|
|
|
*vtop = *v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vdup(void)
|
|
|
|
{
|
|
|
|
vpushv(vtop);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rotate n first stack elements to the bottom
|
|
|
|
I1 ... In -> I2 ... In I1 [top is right]
|
|
|
|
*/
|
|
|
|
ST_FUNC void vrotb(int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
tmp = vtop[-n + 1];
|
|
|
|
for(i=-n+1;i!=0;i++)
|
|
|
|
vtop[i] = vtop[i+1];
|
|
|
|
vtop[0] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rotate the n elements before entry e towards the top
|
|
|
|
I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
|
|
|
|
*/
|
|
|
|
ST_FUNC void vrote(SValue *e, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
tmp = *e;
|
|
|
|
for(i = 0;i < n - 1; i++)
|
|
|
|
e[-i] = e[-i - 1];
|
|
|
|
e[-n + 1] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rotate n first stack elements to the top
|
|
|
|
I1 ... In -> In I1 ... I(n-1) [top is right]
|
|
|
|
*/
|
|
|
|
ST_FUNC void vrott(int n)
|
|
|
|
{
|
|
|
|
vrote(vtop, n);
|
|
|
|
}
|
|
|
|
|
2014-01-12 06:42:58 +08:00
|
|
|
/* push a symbol value of TYPE */
|
|
|
|
static inline void vpushsym(CType *type, Sym *sym)
|
|
|
|
{
|
|
|
|
CValue cval;
|
2015-11-18 03:09:35 +08:00
|
|
|
cval.i = 0;
|
2014-01-12 06:42:58 +08:00
|
|
|
vsetc(type, VT_CONST | VT_SYM, &cval);
|
|
|
|
vtop->sym = sym;
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* Return a static symbol pointing to a section */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
Sym *sym;
|
|
|
|
|
|
|
|
v = anon_sym++;
|
2019-04-18 09:36:39 +08:00
|
|
|
sym = sym_push(v, type, VT_CONST | VT_SYM, 0);
|
|
|
|
sym->type.t |= VT_STATIC;
|
2009-05-06 02:18:10 +08:00
|
|
|
put_extern_sym(sym, sec, offset, size);
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push a reference to a section offset by adding a dummy symbol */
|
|
|
|
static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
|
|
|
|
{
|
2015-07-30 04:53:57 +08:00
|
|
|
vpushsym(type, get_sym_ref(type, sec, offset, size));
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* define a new external reference to a symbol 'v' of type 'u' */
|
2019-04-18 09:36:39 +08:00
|
|
|
ST_FUNC Sym *external_global_sym(int v, CType *type)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
|
|
|
|
s = sym_find(v);
|
|
|
|
if (!s) {
|
|
|
|
/* push forward reference */
|
|
|
|
s = global_identifier_push(v, type->t | VT_EXTERN, 0);
|
|
|
|
s->type.ref = type->ref;
|
2017-11-30 22:15:22 +08:00
|
|
|
} else if (IS_ASM_SYM(s)) {
|
|
|
|
s->type.t = type->t | (s->type.t & VT_EXTERN);
|
|
|
|
s->type.ref = type->ref;
|
2017-12-13 00:57:20 +08:00
|
|
|
update_storage(s);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-01-01 05:00:31 +08:00
|
|
|
/* Merge symbol attributes. */
|
|
|
|
static void merge_symattr(struct SymAttr *sa, struct SymAttr *sa1)
|
|
|
|
{
|
|
|
|
if (sa1->aligned && !sa->aligned)
|
|
|
|
sa->aligned = sa1->aligned;
|
|
|
|
sa->packed |= sa1->packed;
|
|
|
|
sa->weak |= sa1->weak;
|
|
|
|
if (sa1->visibility != STV_DEFAULT) {
|
|
|
|
int vis = sa->visibility;
|
|
|
|
if (vis == STV_DEFAULT
|
|
|
|
|| vis > sa1->visibility)
|
|
|
|
vis = sa1->visibility;
|
|
|
|
sa->visibility = vis;
|
|
|
|
}
|
|
|
|
sa->dllexport |= sa1->dllexport;
|
|
|
|
sa->nodecorate |= sa1->nodecorate;
|
|
|
|
sa->dllimport |= sa1->dllimport;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge function attributes. */
|
|
|
|
static void merge_funcattr(struct FuncAttr *fa, struct FuncAttr *fa1)
|
|
|
|
{
|
|
|
|
if (fa1->func_call && !fa->func_call)
|
|
|
|
fa->func_call = fa1->func_call;
|
|
|
|
if (fa1->func_type && !fa->func_type)
|
|
|
|
fa->func_type = fa1->func_type;
|
|
|
|
if (fa1->func_args && !fa->func_args)
|
|
|
|
fa->func_args = fa1->func_args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge attributes. */
|
|
|
|
static void merge_attr(AttributeDef *ad, AttributeDef *ad1)
|
|
|
|
{
|
|
|
|
merge_symattr(&ad->a, &ad1->a);
|
|
|
|
merge_funcattr(&ad->f, &ad1->f);
|
|
|
|
|
|
|
|
if (ad1->section)
|
|
|
|
ad->section = ad1->section;
|
|
|
|
if (ad1->alias_target)
|
|
|
|
ad->alias_target = ad1->alias_target;
|
|
|
|
if (ad1->asm_label)
|
|
|
|
ad->asm_label = ad1->asm_label;
|
|
|
|
if (ad1->attr_mode)
|
|
|
|
ad->attr_mode = ad1->attr_mode;
|
|
|
|
}
|
|
|
|
|
2017-12-04 03:43:48 +08:00
|
|
|
/* Merge some type attributes. */
|
|
|
|
static void patch_type(Sym *sym, CType *type)
|
|
|
|
{
|
2019-06-17 09:34:03 +08:00
|
|
|
if ((!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t))
|
|
|
|
&& (type->t & VT_BTYPE) != VT_FUNC) {
|
2017-12-04 03:43:48 +08:00
|
|
|
if (!(sym->type.t & VT_EXTERN))
|
|
|
|
tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
|
|
|
|
sym->type.t &= ~VT_EXTERN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ASM_SYM(sym)) {
|
2017-12-13 00:57:20 +08:00
|
|
|
/* stay static if both are static */
|
|
|
|
sym->type.t = type->t & (sym->type.t | ~VT_STATIC);
|
|
|
|
sym->type.ref = type->ref;
|
|
|
|
}
|
2017-12-04 03:43:48 +08:00
|
|
|
|
2017-12-13 00:57:20 +08:00
|
|
|
if (!is_compatible_types(&sym->type, type)) {
|
2017-12-04 03:43:48 +08:00
|
|
|
tcc_error("incompatible types for redefinition of '%s'",
|
|
|
|
get_tok_str(sym->v, NULL));
|
|
|
|
|
|
|
|
} else if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
|
|
|
|
int static_proto = sym->type.t & VT_STATIC;
|
|
|
|
/* warn if static follows non-static function declaration */
|
2019-06-18 00:28:56 +08:00
|
|
|
if ((type->t & VT_STATIC) && !static_proto
|
|
|
|
/* XXX this test for inline shouldn't be here. Until we
|
|
|
|
implement gnu-inline mode again it silences a warning for
|
|
|
|
mingw caused by our workarounds. */
|
|
|
|
&& !((type->t | sym->type.t) & VT_INLINE))
|
2017-12-04 03:43:48 +08:00
|
|
|
tcc_warning("static storage ignored for redefinition of '%s'",
|
|
|
|
get_tok_str(sym->v, NULL));
|
|
|
|
|
2019-06-17 09:34:03 +08:00
|
|
|
/* Force external definition if unequal inline specifier
|
|
|
|
or an explicit extern one. */
|
|
|
|
if ((type->t & VT_INLINE) != (sym->type.t & VT_INLINE)
|
|
|
|
|| (type->t | sym->type.t) & VT_EXTERN) {
|
|
|
|
type->t &= ~VT_INLINE;
|
|
|
|
sym->type.t &= ~VT_INLINE;
|
|
|
|
}
|
2017-12-04 03:43:48 +08:00
|
|
|
if (0 == (type->t & VT_EXTERN)) {
|
2019-06-18 00:28:56 +08:00
|
|
|
/* put complete type, use static from prototype, but don't
|
|
|
|
overwrite type.ref, it might contain parameter names */
|
2017-12-13 00:57:20 +08:00
|
|
|
sym->type.t = (type->t & ~VT_STATIC) | static_proto;
|
2017-12-04 03:43:48 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) {
|
|
|
|
/* set array size if it was omitted in extern declaration */
|
|
|
|
if (sym->type.ref->c < 0)
|
|
|
|
sym->type.ref->c = type->ref->c;
|
|
|
|
else if (sym->type.ref->c != type->ref->c)
|
|
|
|
tcc_error("conflicting type for '%s'", get_tok_str(sym->v, NULL));
|
|
|
|
}
|
|
|
|
if ((type->t ^ sym->type.t) & VT_STATIC)
|
|
|
|
tcc_warning("storage mismatch for redefinition of '%s'",
|
|
|
|
get_tok_str(sym->v, NULL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-04 14:34:52 +08:00
|
|
|
/* Merge some storage attributes. */
|
2017-07-09 18:34:11 +08:00
|
|
|
static void patch_storage(Sym *sym, AttributeDef *ad, CType *type)
|
2017-04-04 14:34:52 +08:00
|
|
|
{
|
2017-12-04 03:43:48 +08:00
|
|
|
if (type)
|
|
|
|
patch_type(sym, type);
|
|
|
|
|
2017-04-04 14:34:52 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2017-07-09 18:34:11 +08:00
|
|
|
if (sym->a.dllimport != ad->a.dllimport)
|
2017-04-04 14:34:52 +08:00
|
|
|
tcc_error("incompatible dll linkage for redefinition of '%s'",
|
|
|
|
get_tok_str(sym->v, NULL));
|
2017-12-04 03:43:48 +08:00
|
|
|
#endif
|
2019-01-01 05:00:31 +08:00
|
|
|
merge_symattr(&sym->a, &ad->a);
|
2017-07-09 18:34:11 +08:00
|
|
|
if (ad->asm_label)
|
|
|
|
sym->asm_label = ad->asm_label;
|
|
|
|
update_storage(sym);
|
2017-04-04 14:34:52 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:22:56 +08:00
|
|
|
/* define a new external reference to a symbol 'v' */
|
2017-07-09 18:34:11 +08:00
|
|
|
static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
s = sym_find(v);
|
2019-04-07 08:44:32 +08:00
|
|
|
if (!s || (!IS_ASM_SYM(s) && !(s->type.t & VT_EXTERN)
|
2019-06-18 00:52:49 +08:00
|
|
|
&& (!(type->t & VT_EXTERN) || s->sym_scope)
|
2019-04-07 08:44:32 +08:00
|
|
|
&& (s->type.t & VT_BTYPE) != VT_FUNC)) {
|
2019-03-18 12:53:03 +08:00
|
|
|
if (s && !is_compatible_types(&s->type, type))
|
|
|
|
tcc_error("conflicting types for '%s'", get_tok_str(s->v, NULL));
|
2009-05-06 02:18:10 +08:00
|
|
|
/* push forward reference */
|
|
|
|
s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
|
2017-07-09 18:34:11 +08:00
|
|
|
s->a = ad->a;
|
2019-05-15 03:34:28 +08:00
|
|
|
s->asm_label = ad->asm_label;
|
2017-07-09 18:34:11 +08:00
|
|
|
s->sym_scope = 0;
|
2017-04-04 14:34:52 +08:00
|
|
|
} else {
|
|
|
|
if (s->type.ref == func_old_type.ref) {
|
|
|
|
s->type.ref = type->ref;
|
|
|
|
s->r = r | VT_CONST | VT_SYM;
|
|
|
|
s->type.t |= VT_EXTERN;
|
|
|
|
}
|
2017-07-09 18:34:11 +08:00
|
|
|
patch_storage(s, ad, type);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push a reference to global symbol v */
|
2009-12-21 03:33:41 +08:00
|
|
|
ST_FUNC void vpush_global_sym(CType *type, int v)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2019-04-18 09:36:39 +08:00
|
|
|
vpushsym(type, external_global_sym(v, type));
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
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 registers up to (vtop - n) stack entry */
|
|
|
|
ST_FUNC void save_regs(int n)
|
|
|
|
{
|
|
|
|
SValue *p, *p1;
|
|
|
|
for(p = vstack, p1 = vtop - n; p <= p1; p++)
|
|
|
|
save_reg(p->r);
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* save r to the memory stack, and mark it as being free */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void save_reg(int r)
|
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_reg_upstack(r, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save r to the memory stack, and mark it as being free,
|
|
|
|
if seen up to (vtop - n) stack entry */
|
|
|
|
ST_FUNC void save_reg_upstack(int r, int n)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int l, saved, size, align;
|
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
|
|
|
SValue *p, *p1, sv;
|
2009-05-06 02:18:10 +08:00
|
|
|
CType *type;
|
|
|
|
|
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
|
|
|
if ((r &= VT_VALMASK) >= VT_CONST)
|
|
|
|
return;
|
2016-12-19 00:23:33 +08:00
|
|
|
if (nocode_wanted)
|
|
|
|
return;
|
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
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* modify all stack values */
|
|
|
|
saved = 0;
|
|
|
|
l = 0;
|
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
|
|
|
for(p = vstack, p1 = vtop - n; p <= p1; p++) {
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((p->r & VT_VALMASK) == r ||
|
|
|
|
((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
|
|
|
|
/* must save value on stack if not already done */
|
|
|
|
if (!saved) {
|
|
|
|
/* NOTE: must reload 'r' because r might be equal to r2 */
|
|
|
|
r = p->r & VT_VALMASK;
|
|
|
|
/* store register in the stack */
|
|
|
|
type = &p->type;
|
|
|
|
if ((p->r & VT_LVAL) ||
|
|
|
|
(!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2009-05-06 02:18:10 +08:00
|
|
|
type = &char_pointer_type;
|
|
|
|
#else
|
|
|
|
type = &int_type;
|
|
|
|
#endif
|
|
|
|
size = type_size(type, &align);
|
2019-01-10 03:32:23 +08:00
|
|
|
l=get_temp_local_var(size,align);
|
2009-05-06 02:18:10 +08:00
|
|
|
sv.type.t = type->t;
|
|
|
|
sv.r = VT_LOCAL | VT_LVAL;
|
2019-01-10 03:32:23 +08:00
|
|
|
sv.c.i = l;
|
2009-05-06 02:18:10 +08:00
|
|
|
store(r, &sv);
|
|
|
|
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
|
|
|
/* x86 specific: need to pop fp register ST0 if saved */
|
|
|
|
if (r == TREG_ST0) {
|
trying to fix the bug of unclean FPU st(0)
Date: Mon, 8 Jun 2009 19:06:56 +0800
From: Soloist Deng <soloist.deng-gmail-com>
Subject: [Tinycc-devel] trying to fix the bug of unclean FPU st(0)
Hi all:
I am using tcc-0.9.25, and the FPU bug brought a big trouble to
me. I read the source and tried to fix it.
Below is my solution.
There are two places where program(`o(0xd9dd)') will generates `fstp
%st(1)': vpop() in tccgen.c:689 and save_reg() in tccgen.c:210.
We should first change both of them to `o(0xd8dd) // fstp %st(0)'.
But these changes are not enough. Let's check the following code.
void foo()
{
double var = 2.7;
var++;
}
Using the changed tcc will generate following machine code:
.text:08000000 public foo
.text:08000000 foo proc near
.text:08000000
.text:08000000 var_18 = qword ptr -18h
.text:08000000 var_10 = qword ptr -10h
.text:08000000 var_8 = qword ptr -8
.text:08000000
.text:08000000 push ebp
.text:08000001 mov ebp, esp
.text:08000003 sub esp, 18h
.text:08000009 nop
.text:0800000A fld L_0
.text:08000010 fst [ebp+var_8]
.text:08000013 fstp st(0)
.text:08000015 fld [ebp+var_8]
.text:08000018 fst [ebp+var_10]
.text:0800001B fstp st(0)
.text:0800001D fst [ebp+var_18]
.text:08000020 fstp st(0)
.text:08000022 fld L_1
.text:08000028 fadd [ebp+var_10]
.text:0800002B fst [ebp+var_8]
.text:0800002E fstp st(0)
.text:08000030 leave
.text:08000031 retn
.text:08000031 foo endp
.text:08000031
.text:08000031 _text ends
--------------------------------------------------
.data:08000040 ; Segment type: Pure data
.data:08000040 ; Segment permissions: Read/Write
.data:08000040 ; Segment alignment '32byte' can not be represented in assembly
.data:08000040 _data segment page public 'DATA' use32
.data:08000040 assume cs:_data
.data:08000040 ;org 8000040h
.data:08000040 L_0 dq 400599999999999Ah
.data:08000048 L_1 dq 3FF0000000000000h
.data:08000048 _data ends
Please notice the code snippet from 0800000A to 08000020
// double var = 2.7; load constant to st(0)
.text:0800000A fld L_0
// double var = 2.7; store st(0) to `var'
.text:08000010 fst [ebp+var_8]
// double var = 2.7; poping st(0) will empty the floating registers stack
.text:08000013 fstp st(0)
After that ,tcc will call `void inc(int post, int c)" in
tccgen.c:2150, and produce 08000015 to 0800001B through the calling
chain (inc ->gv_dup)
// load from `var' to st(0)
.text:08000015 fld [ebp+var_8]
// store st(0) to a temporary location
.text:08000018 fst [ebp+var_10]
// poping st(0) will empty the floating registers stack
.text:0800001B fstp st(0)
And the calling chain
(gen_op('+')->gen_opif('+')->gen_opf('+')->gv(rc=2)->get_reg(rc=2)->save_reg(r=3))
will produce 0800001D to 08000020 .
// store st(0) to a temporary location, but floating stack is empty!
.text:0800001D fst [ebp+var_18]
// poping st(0) will empty the floating registers stack
.text:08000020 fstp st(0)
The `0800001D fst [ebp+var_18]' will store st(0) to a memory
location, but st(0) is empty. That will cause FPU invalid operation
exception(#IE).
Why does tcc do that? Please read `gv_dup' called by `inc' carefully.
Notice these lines:
(1): r = gv(rc);
(2): r1 = get_reg(rc);
(3): sv.r = r;
sv.c.ul = 0;
(4) load(r1, &sv); /* move r to r1 */
(5) vdup();
/* duplicates value */
(6) vtop->r = r1;
(1) let the vtop occupy TREG_ST0, and `r' will be TREG_ST0. (2)
try to get a free floating register,but tcc assume
there is only one, so it wil force vtop goto memory and assign `r1'
with TREG_ST0. When executing (3), it will do nothing
because `r' equals `r1'. (5) duplicates vtop. Then (6) let the new
vtop occupy TREG_ST0, but this will cause problem
because the old vtop has been moved to memory, so the new duplicated
vtop does not reside in TREG_ST0 but also
in memory after that. TREG_ST0 is not occupied but freely availabe
now. `gen_op('+')' need at least one oprand in register,
so it will incorrectly think TREG_ST0 is occupied by vtop and produce
instructions(0800001D and 08000020) to store it to
a temporary memory location.
According program above, if `r' == `r1' it is impossible for the old
vtop to still occupy the `r' register . And `load' will do nothing
too at this condition.
So the `gv_dup' can not promise the semantics that old vtop in one
register and the new duplicated vtop in another register at the same
time.
I changed (6) to
if (r != r1)
{
vtop->r = r1;
}
Then the new generated machine code will be :
.text:08000000 push ebp
.text:08000001 mov ebp, esp
.text:08000003 sub esp, 10h
.text:08000009 nop
.text:0800000A fld L_0
.text:08000010 fst [ebp+var_8]
.text:08000013 fstp st(0)
.text:08000015 fld [ebp+var_8]
.text:08000018 fst [ebp+var_10]
.text:0800001B fstp st(0)
.text:0800001D fld L_1
.text:08000023 fadd [ebp+var_10]
.text:08000026 fst [ebp+var_8]
.text:08000029 fstp st(0)
.text:0800002B leave
.text:0800002C retn
It works well, and will clean the floating registers stack when return.
Finally, I want to know there is any potential problem of this fixing ?
soloist
2009-06-09 01:26:19 +08:00
|
|
|
o(0xd8dd); /* fstp %st(0) */
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
#endif
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2009-05-06 02:18:10 +08:00
|
|
|
/* special long long case */
|
|
|
|
if ((type->t & VT_BTYPE) == VT_LLONG) {
|
2015-11-18 03:09:35 +08:00
|
|
|
sv.c.i += 4;
|
2009-05-06 02:18:10 +08:00
|
|
|
store(p->r2, &sv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
saved = 1;
|
|
|
|
}
|
|
|
|
/* mark that stack entry as being saved on the stack */
|
|
|
|
if (p->r & VT_LVAL) {
|
|
|
|
/* also clear the bounded flag because the
|
|
|
|
relocation address of the function was stored in
|
2015-11-18 03:09:35 +08:00
|
|
|
p->c.i */
|
2009-05-06 02:18:10 +08:00
|
|
|
p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
|
|
|
|
} else {
|
|
|
|
p->r = lvalue_type(p->type.t) | VT_LOCAL;
|
|
|
|
}
|
|
|
|
p->r2 = VT_CONST;
|
2015-11-18 03:09:35 +08:00
|
|
|
p->c.i = l;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
#ifdef TCC_TARGET_ARM
|
2009-05-06 02:18:10 +08:00
|
|
|
/* find a register of class 'rc2' with at most one reference on stack.
|
|
|
|
* If none, call get_reg(rc) */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC int get_reg_ex(int rc, int rc2)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
SValue *p;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
for(r=0;r<NB_REGS;r++) {
|
|
|
|
if (reg_classes[r] & rc2) {
|
|
|
|
int n;
|
|
|
|
n=0;
|
|
|
|
for(p = vstack; p <= vtop; p++) {
|
|
|
|
if ((p->r & VT_VALMASK) == r ||
|
|
|
|
(p->r2 & VT_VALMASK) == r)
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (n <= 1)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get_reg(rc);
|
|
|
|
}
|
2009-12-20 08:53:49 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
/* find a free register of class 'rc'. If none, save one register */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC int get_reg(int rc)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
SValue *p;
|
|
|
|
|
|
|
|
/* find a free register */
|
|
|
|
for(r=0;r<NB_REGS;r++) {
|
|
|
|
if (reg_classes[r] & rc) {
|
2016-12-19 00:23:33 +08:00
|
|
|
if (nocode_wanted)
|
|
|
|
return r;
|
2009-05-06 02:18:10 +08:00
|
|
|
for(p=vstack;p<=vtop;p++) {
|
|
|
|
if ((p->r & VT_VALMASK) == r ||
|
|
|
|
(p->r2 & VT_VALMASK) == r)
|
|
|
|
goto notfound;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
notfound: ;
|
|
|
|
}
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* no register left : free the first one on the stack (VERY
|
|
|
|
IMPORTANT to start from the bottom to ensure that we don't
|
|
|
|
spill registers used in gen_opi()) */
|
|
|
|
for(p=vstack;p<=vtop;p++) {
|
2012-07-12 05:39:05 +08:00
|
|
|
/* look at second register (if long long) */
|
|
|
|
r = p->r2 & VT_VALMASK;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (r < VT_CONST && (reg_classes[r] & rc))
|
|
|
|
goto save_found;
|
2012-07-12 05:39:05 +08:00
|
|
|
r = p->r & VT_VALMASK;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (r < VT_CONST && (reg_classes[r] & rc)) {
|
|
|
|
save_found:
|
|
|
|
save_reg(r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Should never comes here */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-10 03:32:23 +08:00
|
|
|
/* find a free temporary local variable (return the offset on stack) match the size and align. If none, add new temporary stack variable*/
|
|
|
|
static int get_temp_local_var(int size,int align){
|
|
|
|
int i;
|
|
|
|
struct temp_local_variable *temp_var;
|
|
|
|
int found_var;
|
|
|
|
SValue *p;
|
|
|
|
int r;
|
|
|
|
char free;
|
|
|
|
char found;
|
|
|
|
found=0;
|
|
|
|
for(i=0;i<nb_temp_local_vars;i++){
|
|
|
|
temp_var=&arr_temp_local_vars[i];
|
|
|
|
if(temp_var->size<size||align!=temp_var->align){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*check if temp_var is free*/
|
|
|
|
free=1;
|
|
|
|
for(p=vstack;p<=vtop;p++) {
|
|
|
|
r=p->r&VT_VALMASK;
|
|
|
|
if(r==VT_LOCAL||r==VT_LLOCAL){
|
|
|
|
if(p->c.i==temp_var->location){
|
|
|
|
free=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(free){
|
|
|
|
found_var=temp_var->location;
|
|
|
|
found=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!found){
|
|
|
|
loc = (loc - size) & -align;
|
|
|
|
if(nb_temp_local_vars<MAX_TEMP_LOCAL_VARIABLE_NUMBER){
|
|
|
|
temp_var=&arr_temp_local_vars[i];
|
|
|
|
temp_var->location=loc;
|
|
|
|
temp_var->size=size;
|
|
|
|
temp_var->align=align;
|
|
|
|
nb_temp_local_vars++;
|
|
|
|
}
|
|
|
|
found_var=loc;
|
|
|
|
}
|
|
|
|
return found_var;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_temp_local_var_list(){
|
|
|
|
nb_temp_local_vars=0;
|
|
|
|
}
|
|
|
|
|
2013-04-20 01:31:24 +08:00
|
|
|
/* move register 's' (of type 't') to 'r', and flush previous value of r to memory
|
2009-05-06 02:18:10 +08:00
|
|
|
if needed */
|
2013-04-20 01:31:24 +08:00
|
|
|
static void move_reg(int r, int s, int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
SValue sv;
|
|
|
|
|
|
|
|
if (r != s) {
|
|
|
|
save_reg(r);
|
2013-04-20 01:31:24 +08:00
|
|
|
sv.type.t = t;
|
|
|
|
sv.type.ref = NULL;
|
2009-05-06 02:18:10 +08:00
|
|
|
sv.r = s;
|
2015-11-18 03:09:35 +08:00
|
|
|
sv.c.i = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
load(r, &sv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get address of vtop (vtop MUST BE an lvalue) */
|
2015-02-14 02:58:31 +08:00
|
|
|
ST_FUNC void gaddrof(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
vtop->r &= ~VT_LVAL;
|
|
|
|
/* tricky: if saved lvalue, then we can go back to lvalue */
|
|
|
|
if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
|
|
|
|
vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
|
2010-12-17 20:15:01 +08:00
|
|
|
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
/* generate lvalue bound code */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gbound(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int lval_type;
|
|
|
|
CType type1;
|
|
|
|
|
|
|
|
vtop->r &= ~VT_MUSTBOUND;
|
|
|
|
/* if lvalue, then use checking code before dereferencing */
|
|
|
|
if (vtop->r & VT_LVAL) {
|
|
|
|
/* if not VT_BOUNDED value, then make one */
|
|
|
|
if (!(vtop->r & VT_BOUNDED)) {
|
|
|
|
lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
|
|
|
|
/* must save type because we must set it to int to get pointer */
|
|
|
|
type1 = vtop->type;
|
2015-04-10 20:17:22 +08:00
|
|
|
vtop->type.t = VT_PTR;
|
2009-05-06 02:18:10 +08:00
|
|
|
gaddrof();
|
|
|
|
vpushi(0);
|
|
|
|
gen_bounded_ptr_add();
|
|
|
|
vtop->r |= lval_type;
|
|
|
|
vtop->type = type1;
|
|
|
|
}
|
|
|
|
/* then check for dereferencing */
|
|
|
|
gen_bounded_ptr_deref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-07-09 18:38:59 +08:00
|
|
|
static void incr_bf_adr(int o)
|
|
|
|
{
|
|
|
|
vtop->type = char_pointer_type;
|
|
|
|
gaddrof();
|
|
|
|
vpushi(o);
|
|
|
|
gen_op('+');
|
|
|
|
vtop->type.t = (vtop->type.t & ~(VT_BTYPE|VT_DEFSIGN))
|
|
|
|
| (VT_BYTE|VT_UNSIGNED);
|
|
|
|
vtop->r = (vtop->r & ~VT_LVAL_TYPE)
|
|
|
|
| (VT_LVAL_BYTE|VT_LVAL_UNSIGNED|VT_LVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* single-byte load mode for packed or otherwise unaligned bitfields */
|
|
|
|
static void load_packed_bf(CType *type, int bit_pos, int bit_size)
|
|
|
|
{
|
|
|
|
int n, o, bits;
|
|
|
|
save_reg_upstack(vtop->r, 1);
|
2017-07-15 01:26:01 +08:00
|
|
|
vpush64(type->t & VT_BTYPE, 0); // B X
|
2017-07-09 18:38:59 +08:00
|
|
|
bits = 0, o = bit_pos >> 3, bit_pos &= 7;
|
|
|
|
do {
|
|
|
|
vswap(); // X B
|
|
|
|
incr_bf_adr(o);
|
|
|
|
vdup(); // X B B
|
|
|
|
n = 8 - bit_pos;
|
|
|
|
if (n > bit_size)
|
|
|
|
n = bit_size;
|
|
|
|
if (bit_pos)
|
|
|
|
vpushi(bit_pos), gen_op(TOK_SHR), bit_pos = 0; // X B Y
|
|
|
|
if (n < 8)
|
|
|
|
vpushi((1 << n) - 1), gen_op('&');
|
|
|
|
gen_cast(type);
|
|
|
|
if (bits)
|
|
|
|
vpushi(bits), gen_op(TOK_SHL);
|
|
|
|
vrotb(3); // B Y X
|
|
|
|
gen_op('|'); // B X
|
|
|
|
bits += n, bit_size -= n, o = 1;
|
|
|
|
} while (bit_size);
|
|
|
|
vswap(), vpop();
|
|
|
|
if (!(type->t & VT_UNSIGNED)) {
|
|
|
|
n = ((type->t & VT_BTYPE) == VT_LLONG ? 64 : 32) - bits;
|
|
|
|
vpushi(n), gen_op(TOK_SHL);
|
|
|
|
vpushi(n), gen_op(TOK_SAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* single-byte store mode for packed or otherwise unaligned bitfields */
|
2017-07-15 01:26:01 +08:00
|
|
|
static void store_packed_bf(int bit_pos, int bit_size)
|
2017-07-09 18:38:59 +08:00
|
|
|
{
|
|
|
|
int bits, n, o, m, c;
|
|
|
|
|
|
|
|
c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
vswap(); // X B
|
|
|
|
save_reg_upstack(vtop->r, 1);
|
|
|
|
bits = 0, o = bit_pos >> 3, bit_pos &= 7;
|
|
|
|
do {
|
|
|
|
incr_bf_adr(o); // X B
|
|
|
|
vswap(); //B X
|
|
|
|
c ? vdup() : gv_dup(); // B V X
|
|
|
|
vrott(3); // X B V
|
|
|
|
if (bits)
|
|
|
|
vpushi(bits), gen_op(TOK_SHR);
|
|
|
|
if (bit_pos)
|
|
|
|
vpushi(bit_pos), gen_op(TOK_SHL);
|
|
|
|
n = 8 - bit_pos;
|
|
|
|
if (n > bit_size)
|
|
|
|
n = bit_size;
|
|
|
|
if (n < 8) {
|
|
|
|
m = ((1 << n) - 1) << bit_pos;
|
|
|
|
vpushi(m), gen_op('&'); // X B V1
|
|
|
|
vpushv(vtop-1); // X B V1 B
|
|
|
|
vpushi(m & 0x80 ? ~m & 0x7f : ~m);
|
|
|
|
gen_op('&'); // X B V1 B1
|
|
|
|
gen_op('|'); // X B V2
|
|
|
|
}
|
|
|
|
vdup(), vtop[-1] = vtop[-2]; // X B B V2
|
|
|
|
vstore(), vpop(); // X B
|
|
|
|
bits += n, bit_size -= n, bit_pos = 0, o = 1;
|
|
|
|
} while (bit_size);
|
|
|
|
vpop(), vpop();
|
|
|
|
}
|
|
|
|
|
2017-07-15 01:26:01 +08:00
|
|
|
static int adjust_bf(SValue *sv, int bit_pos, int bit_size)
|
2017-07-09 18:38:59 +08:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
if (0 == sv->type.ref)
|
|
|
|
return 0;
|
|
|
|
t = sv->type.ref->auxtype;
|
|
|
|
if (t != -1 && t != VT_STRUCT) {
|
|
|
|
sv->type.t = (sv->type.t & ~VT_BTYPE) | t;
|
|
|
|
sv->r = (sv->r & ~VT_LVAL_TYPE) | lvalue_type(sv->type.t);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* store vtop a register belonging to class 'rc'. lvalues are
|
|
|
|
converted to values. Cannot be used if cannot be converted to
|
|
|
|
register value (such as structures). */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC int gv(int rc)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2017-07-09 18:38:59 +08:00
|
|
|
int r, bit_pos, bit_size, size, align, rc2;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
/* NOTE: get_reg can modify vstack[] */
|
|
|
|
if (vtop->type.t & VT_BITFIELD) {
|
|
|
|
CType type;
|
2017-07-09 18:38:59 +08:00
|
|
|
|
|
|
|
bit_pos = BIT_POS(vtop->type.t);
|
|
|
|
bit_size = BIT_SIZE(vtop->type.t);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* remove bit field info to avoid loops */
|
2017-07-09 18:38:59 +08:00
|
|
|
vtop->type.t &= ~VT_STRUCT_MASK;
|
|
|
|
|
|
|
|
type.ref = NULL;
|
|
|
|
type.t = vtop->type.t & VT_UNSIGNED;
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_BOOL)
|
2009-05-06 02:18:10 +08:00
|
|
|
type.t |= VT_UNSIGNED;
|
2017-07-09 18:38:59 +08:00
|
|
|
|
|
|
|
r = adjust_bf(vtop, bit_pos, bit_size);
|
|
|
|
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
|
|
|
|
type.t |= VT_LLONG;
|
|
|
|
else
|
|
|
|
type.t |= VT_INT;
|
|
|
|
|
|
|
|
if (r == VT_STRUCT) {
|
|
|
|
load_packed_bf(&type, bit_pos, bit_size);
|
|
|
|
} else {
|
|
|
|
int bits = (type.t & VT_BTYPE) == VT_LLONG ? 64 : 32;
|
|
|
|
/* cast to int to propagate signedness in following ops */
|
|
|
|
gen_cast(&type);
|
|
|
|
/* generate shifts */
|
|
|
|
vpushi(bits - (bit_pos + bit_size));
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
vpushi(bits - bit_size);
|
|
|
|
/* NOTE: transformed to SHR if unsigned */
|
|
|
|
gen_op(TOK_SAR);
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
r = gv(rc);
|
|
|
|
} else {
|
2015-07-30 04:53:57 +08:00
|
|
|
if (is_float(vtop->type.t) &&
|
2009-05-06 02:18:10 +08:00
|
|
|
(vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
2017-07-24 03:24:11 +08:00
|
|
|
unsigned long offset;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* CPUs usually cannot use float constants, so we store them
|
|
|
|
generically in data segment */
|
|
|
|
size = type_size(&vtop->type, &align);
|
2017-07-24 03:24:11 +08:00
|
|
|
if (NODATA_WANTED)
|
|
|
|
size = 0, align = 1;
|
|
|
|
offset = section_add(data_section, size, align);
|
|
|
|
vpush_ref(&vtop->type, data_section, offset, size);
|
2017-03-12 12:25:09 +08:00
|
|
|
vswap();
|
2017-07-24 03:24:11 +08:00
|
|
|
init_putv(&vtop->type, data_section, offset);
|
2017-03-12 12:25:09 +08:00
|
|
|
vtop->r |= VT_LVAL;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2015-07-30 04:53:57 +08:00
|
|
|
if (vtop->r & VT_MUSTBOUND)
|
2009-05-06 02:18:10 +08:00
|
|
|
gbound();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
r = vtop->r & VT_VALMASK;
|
2013-04-20 05:05:49 +08:00
|
|
|
rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
|
2015-02-14 02:58:31 +08:00
|
|
|
#ifndef TCC_TARGET_ARM64
|
2009-05-06 02:18:10 +08:00
|
|
|
if (rc == RC_IRET)
|
|
|
|
rc2 = RC_LRET;
|
2013-04-19 22:33:16 +08:00
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
else if (rc == RC_FRET)
|
|
|
|
rc2 = RC_QRET;
|
2015-02-14 02:58:31 +08:00
|
|
|
#endif
|
2011-05-16 20:15:32 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
/* need to reload if:
|
|
|
|
- constant
|
|
|
|
- lvalue (need to dereference pointer)
|
|
|
|
- already a register, but not in the right class */
|
2009-12-18 04:09:53 +08:00
|
|
|
if (r >= VT_CONST
|
|
|
|
|| (vtop->r & VT_LVAL)
|
|
|
|
|| !(reg_classes[r] & rc)
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2013-04-19 22:33:16 +08:00
|
|
|
|| ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
|
|
|
|
|| ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
|
|
|
|
#else
|
2009-12-18 04:09:53 +08:00
|
|
|
|| ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
2009-05-06 02:18:10 +08:00
|
|
|
r = get_reg(rc);
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2013-04-19 22:33:16 +08:00
|
|
|
if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
|
2013-04-20 05:55:09 +08:00
|
|
|
int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
|
2013-04-19 22:33:16 +08:00
|
|
|
#else
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
|
2013-04-20 05:55:09 +08:00
|
|
|
int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
|
2013-09-24 21:36:04 +08:00
|
|
|
unsigned long long ll;
|
2013-04-19 22:33:16 +08:00
|
|
|
#endif
|
2013-04-20 05:55:09 +08:00
|
|
|
int r2, original_type;
|
|
|
|
original_type = vtop->type.t;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* two register type load : expand to two words
|
|
|
|
temporarily */
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
|
|
|
|
/* load constant */
|
2015-11-18 03:09:35 +08:00
|
|
|
ll = vtop->c.i;
|
|
|
|
vtop->c.i = ll; /* first word */
|
2009-05-06 02:18:10 +08:00
|
|
|
load(r, vtop);
|
|
|
|
vtop->r = r; /* save register value */
|
|
|
|
vpushi(ll >> 32); /* second word */
|
2013-04-19 22:33:16 +08:00
|
|
|
} else
|
|
|
|
#endif
|
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
|
|
|
if (vtop->r & VT_LVAL) {
|
2016-10-03 18:27:50 +08:00
|
|
|
/* We do not want to modifier the long long
|
|
|
|
pointer here, so the safest (and less
|
|
|
|
efficient) is to save all the other registers
|
|
|
|
in the stack. XXX: totally inefficient. */
|
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
|
|
|
#if 0
|
2016-10-03 18:27:50 +08:00
|
|
|
save_regs(1);
|
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
|
|
|
#else
|
|
|
|
/* lvalue_save: save only if used further down the stack */
|
|
|
|
save_reg_upstack(vtop->r, 1);
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
/* load from memory */
|
2013-04-19 22:33:16 +08:00
|
|
|
vtop->type.t = load_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
load(r, vtop);
|
|
|
|
vdup();
|
|
|
|
vtop[-1].r = r; /* save register value */
|
|
|
|
/* increment pointer to get second word */
|
2013-04-19 22:33:16 +08:00
|
|
|
vtop->type.t = addr_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
gaddrof();
|
2013-04-19 22:33:16 +08:00
|
|
|
vpushi(load_size);
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op('+');
|
|
|
|
vtop->r |= VT_LVAL;
|
2013-04-19 22:33:16 +08:00
|
|
|
vtop->type.t = load_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
/* move registers */
|
|
|
|
load(r, vtop);
|
|
|
|
vdup();
|
|
|
|
vtop[-1].r = r; /* save register value */
|
|
|
|
vtop->r = vtop[-1].r2;
|
|
|
|
}
|
2012-07-12 05:39:05 +08:00
|
|
|
/* Allocate second register. Here we rely on the fact that
|
|
|
|
get_reg() tries first to free r2 of an SValue. */
|
2009-05-06 02:18:10 +08:00
|
|
|
r2 = get_reg(rc2);
|
|
|
|
load(r2, vtop);
|
|
|
|
vpop();
|
|
|
|
/* write second register */
|
|
|
|
vtop->r2 = r2;
|
2013-04-20 05:55:09 +08:00
|
|
|
vtop->type.t = original_type;
|
2013-04-19 22:33:16 +08:00
|
|
|
} else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
|
2009-05-06 02:18:10 +08:00
|
|
|
int t1, t;
|
|
|
|
/* lvalue of scalar type : need to use lvalue type
|
|
|
|
because of possible cast */
|
|
|
|
t = vtop->type.t;
|
|
|
|
t1 = t;
|
|
|
|
/* compute memory access type */
|
2017-02-20 06:50:13 +08:00
|
|
|
if (vtop->r & VT_LVAL_BYTE)
|
2009-05-06 02:18:10 +08:00
|
|
|
t = VT_BYTE;
|
|
|
|
else if (vtop->r & VT_LVAL_SHORT)
|
|
|
|
t = VT_SHORT;
|
|
|
|
if (vtop->r & VT_LVAL_UNSIGNED)
|
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
vtop->type.t = t;
|
|
|
|
load(r, vtop);
|
|
|
|
/* restore wanted type */
|
|
|
|
vtop->type.t = t1;
|
|
|
|
} else {
|
|
|
|
/* one register type load */
|
|
|
|
load(r, vtop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vtop->r = r;
|
|
|
|
#ifdef TCC_TARGET_C67
|
|
|
|
/* uses register pairs for doubles */
|
2015-07-30 04:53:57 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop->r2 = r+1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void gv2(int rc1, int rc2)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
/* generate more generic register first. But VT_JMP or VT_CMP
|
|
|
|
values must be generated first in all cases to avoid possible
|
|
|
|
reload errors */
|
|
|
|
v = vtop[0].r & VT_VALMASK;
|
|
|
|
if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
|
|
|
|
vswap();
|
|
|
|
gv(rc1);
|
|
|
|
vswap();
|
|
|
|
gv(rc2);
|
|
|
|
/* test if reload is needed for first register */
|
|
|
|
if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
|
|
|
|
vswap();
|
|
|
|
gv(rc1);
|
|
|
|
vswap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gv(rc2);
|
|
|
|
vswap();
|
|
|
|
gv(rc1);
|
|
|
|
vswap();
|
|
|
|
/* test if reload is needed for first register */
|
|
|
|
if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
|
|
|
|
gv(rc2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-14 02:58:31 +08:00
|
|
|
#ifndef TCC_TARGET_ARM64
|
2009-05-06 02:18:10 +08:00
|
|
|
/* wrapper around RC_FRET to return a register by type */
|
2009-12-20 08:53:49 +08:00
|
|
|
static int rc_fret(int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
if (t == VT_LDOUBLE) {
|
|
|
|
return RC_ST0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return RC_FRET;
|
|
|
|
}
|
2015-02-14 02:58:31 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
/* wrapper around REG_FRET to return a register by type */
|
2009-12-20 08:53:49 +08:00
|
|
|
static int reg_fret(int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
if (t == VT_LDOUBLE) {
|
|
|
|
return TREG_ST0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return REG_FRET;
|
|
|
|
}
|
|
|
|
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2016-10-17 06:57:16 +08:00
|
|
|
/* expand 64bit on stack in two ints */
|
2019-01-09 02:06:26 +08:00
|
|
|
ST_FUNC void lexpand(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2016-10-17 01:04:40 +08:00
|
|
|
int u, v;
|
2014-02-06 20:51:47 +08:00
|
|
|
u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
|
2016-10-17 01:04:40 +08:00
|
|
|
v = vtop->r & (VT_VALMASK | VT_LVAL);
|
|
|
|
if (v == VT_CONST) {
|
|
|
|
vdup();
|
|
|
|
vtop[0].c.i >>= 32;
|
|
|
|
} else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
|
|
|
|
vdup();
|
|
|
|
vtop[0].c.i += 4;
|
|
|
|
} else {
|
|
|
|
gv(RC_INT);
|
|
|
|
vdup();
|
|
|
|
vtop[0].r = vtop[-1].r2;
|
|
|
|
vtop[0].r2 = vtop[-1].r2 = VT_CONST;
|
|
|
|
}
|
|
|
|
vtop[0].type.t = vtop[-1].type.t = VT_INT | u;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-12-06 07:29:25 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2009-05-06 02:18:10 +08:00
|
|
|
/* build a long long from two ints */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void lbuild(int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
gv2(RC_INT, RC_INT);
|
|
|
|
vtop[-1].r2 = vtop[0].r;
|
|
|
|
vtop[-1].type.t = t;
|
|
|
|
vpop();
|
|
|
|
}
|
2016-10-17 06:57:16 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
/* convert stack entry to register and duplicate its value in another
|
|
|
|
register */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gv_dup(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int rc, t, r, r1;
|
|
|
|
SValue sv;
|
|
|
|
|
|
|
|
t = vtop->type.t;
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((t & VT_BTYPE) == VT_LLONG) {
|
2017-07-09 18:38:59 +08:00
|
|
|
if (t & VT_BITFIELD) {
|
|
|
|
gv(RC_INT);
|
|
|
|
t = vtop->type.t;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
lexpand();
|
|
|
|
gv_dup();
|
|
|
|
vswap();
|
|
|
|
vrotb(3);
|
|
|
|
gv_dup();
|
|
|
|
vrotb(4);
|
|
|
|
/* stack: H L L1 H1 */
|
|
|
|
lbuild(t);
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
vswap();
|
|
|
|
lbuild(t);
|
|
|
|
vswap();
|
2016-10-17 06:57:16 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2009-05-06 02:18:10 +08:00
|
|
|
/* duplicate value */
|
|
|
|
rc = RC_INT;
|
|
|
|
sv.type.t = VT_INT;
|
|
|
|
if (is_float(t)) {
|
|
|
|
rc = RC_FLOAT;
|
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
if ((t & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
rc = RC_ST0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
sv.type.t = t;
|
|
|
|
}
|
|
|
|
r = gv(rc);
|
|
|
|
r1 = get_reg(rc);
|
|
|
|
sv.r = r;
|
2015-11-18 03:09:35 +08:00
|
|
|
sv.c.i = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
load(r1, &sv); /* move r to r1 */
|
|
|
|
vdup();
|
|
|
|
/* duplicates value */
|
trying to fix the bug of unclean FPU st(0)
Date: Mon, 8 Jun 2009 19:06:56 +0800
From: Soloist Deng <soloist.deng-gmail-com>
Subject: [Tinycc-devel] trying to fix the bug of unclean FPU st(0)
Hi all:
I am using tcc-0.9.25, and the FPU bug brought a big trouble to
me. I read the source and tried to fix it.
Below is my solution.
There are two places where program(`o(0xd9dd)') will generates `fstp
%st(1)': vpop() in tccgen.c:689 and save_reg() in tccgen.c:210.
We should first change both of them to `o(0xd8dd) // fstp %st(0)'.
But these changes are not enough. Let's check the following code.
void foo()
{
double var = 2.7;
var++;
}
Using the changed tcc will generate following machine code:
.text:08000000 public foo
.text:08000000 foo proc near
.text:08000000
.text:08000000 var_18 = qword ptr -18h
.text:08000000 var_10 = qword ptr -10h
.text:08000000 var_8 = qword ptr -8
.text:08000000
.text:08000000 push ebp
.text:08000001 mov ebp, esp
.text:08000003 sub esp, 18h
.text:08000009 nop
.text:0800000A fld L_0
.text:08000010 fst [ebp+var_8]
.text:08000013 fstp st(0)
.text:08000015 fld [ebp+var_8]
.text:08000018 fst [ebp+var_10]
.text:0800001B fstp st(0)
.text:0800001D fst [ebp+var_18]
.text:08000020 fstp st(0)
.text:08000022 fld L_1
.text:08000028 fadd [ebp+var_10]
.text:0800002B fst [ebp+var_8]
.text:0800002E fstp st(0)
.text:08000030 leave
.text:08000031 retn
.text:08000031 foo endp
.text:08000031
.text:08000031 _text ends
--------------------------------------------------
.data:08000040 ; Segment type: Pure data
.data:08000040 ; Segment permissions: Read/Write
.data:08000040 ; Segment alignment '32byte' can not be represented in assembly
.data:08000040 _data segment page public 'DATA' use32
.data:08000040 assume cs:_data
.data:08000040 ;org 8000040h
.data:08000040 L_0 dq 400599999999999Ah
.data:08000048 L_1 dq 3FF0000000000000h
.data:08000048 _data ends
Please notice the code snippet from 0800000A to 08000020
// double var = 2.7; load constant to st(0)
.text:0800000A fld L_0
// double var = 2.7; store st(0) to `var'
.text:08000010 fst [ebp+var_8]
// double var = 2.7; poping st(0) will empty the floating registers stack
.text:08000013 fstp st(0)
After that ,tcc will call `void inc(int post, int c)" in
tccgen.c:2150, and produce 08000015 to 0800001B through the calling
chain (inc ->gv_dup)
// load from `var' to st(0)
.text:08000015 fld [ebp+var_8]
// store st(0) to a temporary location
.text:08000018 fst [ebp+var_10]
// poping st(0) will empty the floating registers stack
.text:0800001B fstp st(0)
And the calling chain
(gen_op('+')->gen_opif('+')->gen_opf('+')->gv(rc=2)->get_reg(rc=2)->save_reg(r=3))
will produce 0800001D to 08000020 .
// store st(0) to a temporary location, but floating stack is empty!
.text:0800001D fst [ebp+var_18]
// poping st(0) will empty the floating registers stack
.text:08000020 fstp st(0)
The `0800001D fst [ebp+var_18]' will store st(0) to a memory
location, but st(0) is empty. That will cause FPU invalid operation
exception(#IE).
Why does tcc do that? Please read `gv_dup' called by `inc' carefully.
Notice these lines:
(1): r = gv(rc);
(2): r1 = get_reg(rc);
(3): sv.r = r;
sv.c.ul = 0;
(4) load(r1, &sv); /* move r to r1 */
(5) vdup();
/* duplicates value */
(6) vtop->r = r1;
(1) let the vtop occupy TREG_ST0, and `r' will be TREG_ST0. (2)
try to get a free floating register,but tcc assume
there is only one, so it wil force vtop goto memory and assign `r1'
with TREG_ST0. When executing (3), it will do nothing
because `r' equals `r1'. (5) duplicates vtop. Then (6) let the new
vtop occupy TREG_ST0, but this will cause problem
because the old vtop has been moved to memory, so the new duplicated
vtop does not reside in TREG_ST0 but also
in memory after that. TREG_ST0 is not occupied but freely availabe
now. `gen_op('+')' need at least one oprand in register,
so it will incorrectly think TREG_ST0 is occupied by vtop and produce
instructions(0800001D and 08000020) to store it to
a temporary memory location.
According program above, if `r' == `r1' it is impossible for the old
vtop to still occupy the `r' register . And `load' will do nothing
too at this condition.
So the `gv_dup' can not promise the semantics that old vtop in one
register and the new duplicated vtop in another register at the same
time.
I changed (6) to
if (r != r1)
{
vtop->r = r1;
}
Then the new generated machine code will be :
.text:08000000 push ebp
.text:08000001 mov ebp, esp
.text:08000003 sub esp, 10h
.text:08000009 nop
.text:0800000A fld L_0
.text:08000010 fst [ebp+var_8]
.text:08000013 fstp st(0)
.text:08000015 fld [ebp+var_8]
.text:08000018 fst [ebp+var_10]
.text:0800001B fstp st(0)
.text:0800001D fld L_1
.text:08000023 fadd [ebp+var_10]
.text:08000026 fst [ebp+var_8]
.text:08000029 fstp st(0)
.text:0800002B leave
.text:0800002C retn
It works well, and will clean the floating registers stack when return.
Finally, I want to know there is any potential problem of this fixing ?
soloist
2009-06-09 01:26:19 +08:00
|
|
|
if (r != r1)
|
|
|
|
vtop->r = r1;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:51:20 +08:00
|
|
|
/* Generate value test
|
|
|
|
*
|
|
|
|
* Generate a test for any value (jump, comparison and integers) */
|
2014-04-10 17:53:54 +08:00
|
|
|
ST_FUNC int gvtst(int inv, int t)
|
2013-12-31 23:51:20 +08:00
|
|
|
{
|
|
|
|
int v = vtop->r & VT_VALMASK;
|
|
|
|
if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
|
|
|
|
vpushi(0);
|
|
|
|
gen_op(TOK_NE);
|
|
|
|
}
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
|
|
|
/* constant jmp optimization */
|
|
|
|
if ((vtop->c.i != 0) != inv)
|
|
|
|
t = gjmp(t);
|
|
|
|
vtop--;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return gtst(inv, t);
|
|
|
|
}
|
|
|
|
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2009-05-06 02:18:10 +08:00
|
|
|
/* generate CPU independent (unsigned) long long operations */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gen_opl(int op)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int t, a, b, op1, c, i;
|
|
|
|
int func;
|
|
|
|
unsigned short reg_iret = REG_IRET;
|
|
|
|
unsigned short reg_lret = REG_LRET;
|
|
|
|
SValue tmp;
|
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
case '/':
|
|
|
|
case TOK_PDIV:
|
|
|
|
func = TOK___divdi3;
|
|
|
|
goto gen_func;
|
|
|
|
case TOK_UDIV:
|
|
|
|
func = TOK___udivdi3;
|
|
|
|
goto gen_func;
|
|
|
|
case '%':
|
|
|
|
func = TOK___moddi3;
|
|
|
|
goto gen_mod_func;
|
|
|
|
case TOK_UMOD:
|
|
|
|
func = TOK___umoddi3;
|
|
|
|
gen_mod_func:
|
|
|
|
#ifdef TCC_ARM_EABI
|
|
|
|
reg_iret = TREG_R2;
|
|
|
|
reg_lret = TREG_R3;
|
|
|
|
#endif
|
|
|
|
gen_func:
|
|
|
|
/* call generic long long function */
|
|
|
|
vpush_global_sym(&func_old_type, func);
|
|
|
|
vrott(3);
|
|
|
|
gfunc_call(2);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = reg_iret;
|
|
|
|
vtop->r2 = reg_lret;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '*':
|
|
|
|
case '+':
|
|
|
|
case '-':
|
2016-10-14 01:21:43 +08:00
|
|
|
//pv("gen_opl A",0,2);
|
2009-05-06 02:18:10 +08:00
|
|
|
t = vtop->type.t;
|
|
|
|
vswap();
|
|
|
|
lexpand();
|
|
|
|
vrotb(3);
|
|
|
|
lexpand();
|
|
|
|
/* stack: L1 H1 L2 H2 */
|
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-3];
|
|
|
|
vtop[-3] = tmp;
|
|
|
|
tmp = vtop[-2];
|
|
|
|
vtop[-2] = vtop[-3];
|
|
|
|
vtop[-3] = tmp;
|
|
|
|
vswap();
|
|
|
|
/* stack: H1 H2 L1 L2 */
|
2016-10-14 01:21:43 +08:00
|
|
|
//pv("gen_opl B",0,4);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (op == '*') {
|
|
|
|
vpushv(vtop - 1);
|
|
|
|
vpushv(vtop - 1);
|
|
|
|
gen_op(TOK_UMULL);
|
|
|
|
lexpand();
|
|
|
|
/* stack: H1 H2 L1 L2 ML MH */
|
|
|
|
for(i=0;i<4;i++)
|
|
|
|
vrotb(6);
|
|
|
|
/* stack: ML MH H1 H2 L1 L2 */
|
|
|
|
tmp = vtop[0];
|
|
|
|
vtop[0] = vtop[-2];
|
|
|
|
vtop[-2] = tmp;
|
|
|
|
/* stack: ML MH H1 L2 H2 L1 */
|
|
|
|
gen_op('*');
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
gen_op('*');
|
|
|
|
/* stack: ML MH M1 M2 */
|
|
|
|
gen_op('+');
|
|
|
|
gen_op('+');
|
|
|
|
} else if (op == '+' || op == '-') {
|
|
|
|
/* XXX: add non carry method too (for MIPS or alpha) */
|
|
|
|
if (op == '+')
|
|
|
|
op1 = TOK_ADDC1;
|
|
|
|
else
|
|
|
|
op1 = TOK_SUBC1;
|
|
|
|
gen_op(op1);
|
|
|
|
/* stack: H1 H2 (L1 op L2) */
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
gen_op(op1 + 1); /* TOK_xxxC2 */
|
|
|
|
} else {
|
|
|
|
gen_op(op);
|
|
|
|
/* stack: H1 H2 (L1 op L2) */
|
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
/* stack: (L1 op L2) H1 H2 */
|
|
|
|
gen_op(op);
|
|
|
|
/* stack: (L1 op L2) (H1 op H2) */
|
|
|
|
}
|
|
|
|
/* stack: L H */
|
|
|
|
lbuild(t);
|
|
|
|
break;
|
|
|
|
case TOK_SAR:
|
|
|
|
case TOK_SHR:
|
|
|
|
case TOK_SHL:
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
|
|
|
t = vtop[-1].type.t;
|
|
|
|
vswap();
|
|
|
|
lexpand();
|
|
|
|
vrotb(3);
|
|
|
|
/* stack: L H shift */
|
|
|
|
c = (int)vtop->c.i;
|
|
|
|
/* constant: simpler */
|
|
|
|
/* NOTE: all comments are for SHL. the other cases are
|
2017-05-08 12:38:09 +08:00
|
|
|
done by swapping words */
|
2009-05-06 02:18:10 +08:00
|
|
|
vpop();
|
|
|
|
if (op != TOK_SHL)
|
|
|
|
vswap();
|
|
|
|
if (c >= 32) {
|
|
|
|
/* stack: L H */
|
|
|
|
vpop();
|
|
|
|
if (c > 32) {
|
|
|
|
vpushi(c - 32);
|
|
|
|
gen_op(op);
|
|
|
|
}
|
|
|
|
if (op != TOK_SAR) {
|
|
|
|
vpushi(0);
|
|
|
|
} else {
|
|
|
|
gv_dup();
|
|
|
|
vpushi(31);
|
|
|
|
gen_op(TOK_SAR);
|
|
|
|
}
|
|
|
|
vswap();
|
|
|
|
} else {
|
|
|
|
vswap();
|
|
|
|
gv_dup();
|
|
|
|
/* stack: H L L */
|
|
|
|
vpushi(c);
|
|
|
|
gen_op(op);
|
|
|
|
vswap();
|
|
|
|
vpushi(32 - c);
|
|
|
|
if (op == TOK_SHL)
|
|
|
|
gen_op(TOK_SHR);
|
|
|
|
else
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
vrotb(3);
|
|
|
|
/* stack: L L H */
|
|
|
|
vpushi(c);
|
|
|
|
if (op == TOK_SHL)
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
else
|
|
|
|
gen_op(TOK_SHR);
|
|
|
|
gen_op('|');
|
|
|
|
}
|
|
|
|
if (op != TOK_SHL)
|
|
|
|
vswap();
|
|
|
|
lbuild(t);
|
|
|
|
} else {
|
|
|
|
/* XXX: should provide a faster fallback on x86 ? */
|
|
|
|
switch(op) {
|
|
|
|
case TOK_SAR:
|
|
|
|
func = TOK___ashrdi3;
|
|
|
|
goto gen_func;
|
|
|
|
case TOK_SHR:
|
|
|
|
func = TOK___lshrdi3;
|
|
|
|
goto gen_func;
|
|
|
|
case TOK_SHL:
|
|
|
|
func = TOK___ashldi3;
|
|
|
|
goto gen_func;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* compare operations */
|
|
|
|
t = vtop->type.t;
|
|
|
|
vswap();
|
|
|
|
lexpand();
|
|
|
|
vrotb(3);
|
|
|
|
lexpand();
|
|
|
|
/* stack: L1 H1 L2 H2 */
|
|
|
|
tmp = vtop[-1];
|
|
|
|
vtop[-1] = vtop[-2];
|
|
|
|
vtop[-2] = tmp;
|
|
|
|
/* stack: L1 L2 H1 H2 */
|
|
|
|
/* compare high */
|
|
|
|
op1 = op;
|
|
|
|
/* when values are equal, we need to compare low words. since
|
|
|
|
the jump is inverted, we invert the test too. */
|
|
|
|
if (op1 == TOK_LT)
|
|
|
|
op1 = TOK_LE;
|
|
|
|
else if (op1 == TOK_GT)
|
|
|
|
op1 = TOK_GE;
|
|
|
|
else if (op1 == TOK_ULT)
|
|
|
|
op1 = TOK_ULE;
|
|
|
|
else if (op1 == TOK_UGT)
|
|
|
|
op1 = TOK_UGE;
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
|
|
|
gen_op(op1);
|
2017-02-14 01:23:43 +08:00
|
|
|
if (op == TOK_NE) {
|
|
|
|
b = gvtst(0, 0);
|
|
|
|
} else {
|
2013-12-31 23:51:20 +08:00
|
|
|
a = gvtst(1, 0);
|
2017-02-14 01:23:43 +08:00
|
|
|
if (op != TOK_EQ) {
|
|
|
|
/* generate non equal test */
|
|
|
|
vpushi(TOK_NE);
|
|
|
|
vtop->r = VT_CMP;
|
2013-12-31 23:51:20 +08:00
|
|
|
b = gvtst(0, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* compare low. Always unsigned */
|
|
|
|
op1 = op;
|
|
|
|
if (op1 == TOK_LT)
|
|
|
|
op1 = TOK_ULT;
|
|
|
|
else if (op1 == TOK_LE)
|
|
|
|
op1 = TOK_ULE;
|
|
|
|
else if (op1 == TOK_GT)
|
|
|
|
op1 = TOK_UGT;
|
|
|
|
else if (op1 == TOK_GE)
|
|
|
|
op1 = TOK_UGE;
|
|
|
|
gen_op(op1);
|
2013-12-31 23:51:20 +08:00
|
|
|
a = gvtst(1, a);
|
2009-05-06 02:18:10 +08:00
|
|
|
gsym(b);
|
|
|
|
vseti(VT_JMPI, a);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-11-18 03:34:31 +08:00
|
|
|
static uint64_t gen_opic_sdiv(uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
uint64_t x = (a >> 63 ? -a : a) / (b >> 63 ? -b : b);
|
|
|
|
return (a ^ b) >> 63 ? -x : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gen_opic_lt(uint64_t a, uint64_t b)
|
|
|
|
{
|
|
|
|
return (a ^ (uint64_t)1 << 63) < (b ^ (uint64_t)1 << 63);
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* handle integer constant optimizations and various machine
|
|
|
|
independent opt */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gen_opic(int op)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2015-11-18 03:34:31 +08:00
|
|
|
SValue *v1 = vtop - 1;
|
|
|
|
SValue *v2 = vtop;
|
|
|
|
int t1 = v1->type.t & VT_BTYPE;
|
|
|
|
int t2 = v2->type.t & VT_BTYPE;
|
|
|
|
int c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
int c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
uint64_t l1 = c1 ? v1->c.i : 0;
|
|
|
|
uint64_t l2 = c2 ? v2->c.i : 0;
|
|
|
|
int shm = (t1 == VT_LLONG) ? 63 : 31;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
2016-10-04 07:20:33 +08:00
|
|
|
if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
|
2015-11-18 03:09:35 +08:00
|
|
|
l1 = ((uint32_t)l1 |
|
|
|
|
(v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
|
2016-10-04 07:20:33 +08:00
|
|
|
if (t2 != VT_LLONG && (PTR_SIZE != 8 || t2 != VT_PTR))
|
2015-11-18 03:09:35 +08:00
|
|
|
l2 = ((uint32_t)l2 |
|
|
|
|
(v2->type.t & VT_UNSIGNED ? 0 : -(l2 & 0x80000000)));
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
if (c1 && c2) {
|
|
|
|
switch(op) {
|
|
|
|
case '+': l1 += l2; break;
|
|
|
|
case '-': l1 -= l2; break;
|
|
|
|
case '&': l1 &= l2; break;
|
|
|
|
case '^': l1 ^= l2; break;
|
|
|
|
case '|': l1 |= l2; break;
|
|
|
|
case '*': l1 *= l2; break;
|
|
|
|
|
|
|
|
case TOK_PDIV:
|
|
|
|
case '/':
|
|
|
|
case '%':
|
|
|
|
case TOK_UDIV:
|
|
|
|
case TOK_UMOD:
|
|
|
|
/* if division by zero, generate explicit division */
|
|
|
|
if (l2 == 0) {
|
|
|
|
if (const_wanted)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("division by zero in constant");
|
2009-05-06 02:18:10 +08:00
|
|
|
goto general_case;
|
|
|
|
}
|
|
|
|
switch(op) {
|
2015-11-18 03:34:31 +08:00
|
|
|
default: l1 = gen_opic_sdiv(l1, l2); break;
|
|
|
|
case '%': l1 = l1 - l2 * gen_opic_sdiv(l1, l2); break;
|
|
|
|
case TOK_UDIV: l1 = l1 / l2; break;
|
|
|
|
case TOK_UMOD: l1 = l1 % l2; break;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-11-18 03:34:31 +08:00
|
|
|
case TOK_SHL: l1 <<= (l2 & shm); break;
|
|
|
|
case TOK_SHR: l1 >>= (l2 & shm); break;
|
|
|
|
case TOK_SAR:
|
|
|
|
l1 = (l1 >> 63) ? ~(~l1 >> (l2 & shm)) : l1 >> (l2 & shm);
|
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* tests */
|
2015-11-18 03:34:31 +08:00
|
|
|
case TOK_ULT: l1 = l1 < l2; break;
|
|
|
|
case TOK_UGE: l1 = l1 >= l2; break;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_EQ: l1 = l1 == l2; break;
|
|
|
|
case TOK_NE: l1 = l1 != l2; break;
|
2015-11-18 03:34:31 +08:00
|
|
|
case TOK_ULE: l1 = l1 <= l2; break;
|
|
|
|
case TOK_UGT: l1 = l1 > l2; break;
|
|
|
|
case TOK_LT: l1 = gen_opic_lt(l1, l2); break;
|
|
|
|
case TOK_GE: l1 = !gen_opic_lt(l1, l2); break;
|
|
|
|
case TOK_LE: l1 = !gen_opic_lt(l2, l1); break;
|
|
|
|
case TOK_GT: l1 = gen_opic_lt(l2, l1); break;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* logical */
|
|
|
|
case TOK_LAND: l1 = l1 && l2; break;
|
|
|
|
case TOK_LOR: l1 = l1 || l2; break;
|
|
|
|
default:
|
|
|
|
goto general_case;
|
|
|
|
}
|
2016-11-06 12:02:11 +08:00
|
|
|
if (t1 != VT_LLONG && (PTR_SIZE != 8 || t1 != VT_PTR))
|
|
|
|
l1 = ((uint32_t)l1 |
|
|
|
|
(v1->type.t & VT_UNSIGNED ? 0 : -(l1 & 0x80000000)));
|
2015-11-18 03:09:35 +08:00
|
|
|
v1->c.i = l1;
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop--;
|
|
|
|
} else {
|
|
|
|
/* if commutative ops, put c2 as constant */
|
2015-07-30 04:53:57 +08:00
|
|
|
if (c1 && (op == '+' || op == '&' || op == '^' ||
|
2009-05-06 02:18:10 +08:00
|
|
|
op == '|' || op == '*')) {
|
|
|
|
vswap();
|
|
|
|
c2 = c1; //c = c1, c1 = c2, c2 = c;
|
|
|
|
l2 = l1; //l = l1, l1 = l2, l2 = l;
|
|
|
|
}
|
tccgen.c: Optimise 0<<x, 0>>x, -1>>x, x&0, x*0, x|-1, x%1.
More precisely, treat (0 << x) and so on as constant expressions, but
not if const_wanted as we do not want to allow "case (x*0):", ...
Do not optimise (0 / x) and (0 % x) here as x might be zero, though
for an architecture that does not generate an exception for division
by zero the back end might choose to optimise those.
2015-03-07 19:25:27 +08:00
|
|
|
if (!const_wanted &&
|
|
|
|
c1 && ((l1 == 0 &&
|
|
|
|
(op == TOK_SHL || op == TOK_SHR || op == TOK_SAR)) ||
|
|
|
|
(l1 == -1 && op == TOK_SAR))) {
|
|
|
|
/* treat (0 << x), (0 >> x) and (-1 >> x) as constant */
|
|
|
|
vtop--;
|
|
|
|
} else if (!const_wanted &&
|
|
|
|
c2 && ((l2 == 0 && (op == '&' || op == '*')) ||
|
2017-07-09 18:07:40 +08:00
|
|
|
(op == '|' &&
|
|
|
|
(l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))) ||
|
tccgen.c: Optimise 0<<x, 0>>x, -1>>x, x&0, x*0, x|-1, x%1.
More precisely, treat (0 << x) and so on as constant expressions, but
not if const_wanted as we do not want to allow "case (x*0):", ...
Do not optimise (0 / x) and (0 % x) here as x might be zero, though
for an architecture that does not generate an exception for division
by zero the back end might choose to optimise those.
2015-03-07 19:25:27 +08:00
|
|
|
(l2 == 1 && (op == '%' || op == TOK_UMOD)))) {
|
|
|
|
/* treat (x & 0), (x * 0), (x | -1) and (x % 1) as constant */
|
|
|
|
if (l2 == 1)
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = 0;
|
tccgen.c: Optimise 0<<x, 0>>x, -1>>x, x&0, x*0, x|-1, x%1.
More precisely, treat (0 << x) and so on as constant expressions, but
not if const_wanted as we do not want to allow "case (x*0):", ...
Do not optimise (0 / x) and (0 % x) here as x might be zero, though
for an architecture that does not generate an exception for division
by zero the back end might choose to optimise those.
2015-03-07 19:25:27 +08:00
|
|
|
vswap();
|
|
|
|
vtop--;
|
|
|
|
} else if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
|
|
|
|
op == TOK_PDIV) &&
|
|
|
|
l2 == 1) ||
|
|
|
|
((op == '+' || op == '-' || op == '|' || op == '^' ||
|
|
|
|
op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
|
|
|
|
l2 == 0) ||
|
|
|
|
(op == '&' &&
|
2017-07-09 18:07:40 +08:00
|
|
|
(l2 == -1 || (l2 == 0xFFFFFFFF && t2 != VT_LLONG))))) {
|
tccgen.c: Optimise 0<<x, 0>>x, -1>>x, x&0, x*0, x|-1, x%1.
More precisely, treat (0 << x) and so on as constant expressions, but
not if const_wanted as we do not want to allow "case (x*0):", ...
Do not optimise (0 / x) and (0 % x) here as x might be zero, though
for an architecture that does not generate an exception for division
by zero the back end might choose to optimise those.
2015-03-07 19:25:27 +08:00
|
|
|
/* filter out NOP operations like x*1, x-0, x&-1... */
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop--;
|
|
|
|
} else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
|
|
|
|
/* try to use shifts instead of muls or divs */
|
|
|
|
if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
|
2015-11-18 03:34:31 +08:00
|
|
|
int n = -1;
|
2009-05-06 02:18:10 +08:00
|
|
|
while (l2) {
|
|
|
|
l2 >>= 1;
|
|
|
|
n++;
|
|
|
|
}
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = n;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (op == '*')
|
|
|
|
op = TOK_SHL;
|
|
|
|
else if (op == TOK_PDIV)
|
|
|
|
op = TOK_SAR;
|
|
|
|
else
|
|
|
|
op = TOK_SHR;
|
|
|
|
}
|
|
|
|
goto general_case;
|
|
|
|
} else if (c2 && (op == '+' || op == '-') &&
|
2010-01-15 03:55:51 +08:00
|
|
|
(((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
|
|
|
|
|| (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* symbol + constant case */
|
|
|
|
if (op == '-')
|
|
|
|
l2 = -l2;
|
2016-10-09 06:44:22 +08:00
|
|
|
l2 += vtop[-1].c.i;
|
|
|
|
/* The backends can't always deal with addends to symbols
|
|
|
|
larger than +-1<<31. Don't construct such. */
|
|
|
|
if ((int)l2 != l2)
|
|
|
|
goto general_case;
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop--;
|
2016-10-09 06:44:22 +08:00
|
|
|
vtop->c.i = l2;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
general_case:
|
|
|
|
/* call low level op generator */
|
2015-02-14 02:58:31 +08:00
|
|
|
if (t1 == VT_LLONG || t2 == VT_LLONG ||
|
|
|
|
(PTR_SIZE == 8 && (t1 == VT_PTR || t2 == VT_PTR)))
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_opl(op);
|
|
|
|
else
|
|
|
|
gen_opi(op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate a floating point operation with constant propagation */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gen_opif(int op)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int c1, c2;
|
|
|
|
SValue *v1, *v2;
|
2017-07-09 18:07:40 +08:00
|
|
|
#if defined _MSC_VER && defined _AMD64_
|
|
|
|
/* avoid bad optimization with f1 -= f2 for f1:-0.0, f2:0.0 */
|
|
|
|
volatile
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
long double f1, f2;
|
|
|
|
|
|
|
|
v1 = vtop - 1;
|
|
|
|
v2 = vtop;
|
|
|
|
/* currently, we cannot do computations with forward symbols */
|
|
|
|
c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
if (c1 && c2) {
|
|
|
|
if (v1->type.t == VT_FLOAT) {
|
|
|
|
f1 = v1->c.f;
|
|
|
|
f2 = v2->c.f;
|
|
|
|
} else if (v1->type.t == VT_DOUBLE) {
|
|
|
|
f1 = v1->c.d;
|
|
|
|
f2 = v2->c.d;
|
|
|
|
} else {
|
|
|
|
f1 = v1->c.ld;
|
|
|
|
f2 = v2->c.ld;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: we only do constant propagation if finite number (not
|
|
|
|
NaN or infinity) (ANSI spec) */
|
|
|
|
if (!ieee_finite(f1) || !ieee_finite(f2))
|
|
|
|
goto general_case;
|
|
|
|
|
|
|
|
switch(op) {
|
|
|
|
case '+': f1 += f2; break;
|
|
|
|
case '-': f1 -= f2; break;
|
|
|
|
case '*': f1 *= f2; break;
|
2015-07-30 04:53:57 +08:00
|
|
|
case '/':
|
2009-05-06 02:18:10 +08:00
|
|
|
if (f2 == 0.0) {
|
2017-12-25 19:44:29 +08:00
|
|
|
/* If not in initializer we need to potentially generate
|
|
|
|
FP exceptions at runtime, otherwise we want to fold. */
|
|
|
|
if (!const_wanted)
|
|
|
|
goto general_case;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2015-07-30 04:53:57 +08:00
|
|
|
f1 /= f2;
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
/* XXX: also handles tests ? */
|
|
|
|
default:
|
|
|
|
goto general_case;
|
|
|
|
}
|
|
|
|
/* XXX: overflow test ? */
|
|
|
|
if (v1->type.t == VT_FLOAT) {
|
|
|
|
v1->c.f = f1;
|
|
|
|
} else if (v1->type.t == VT_DOUBLE) {
|
|
|
|
v1->c.d = f1;
|
|
|
|
} else {
|
|
|
|
v1->c.ld = f1;
|
|
|
|
}
|
|
|
|
vtop--;
|
|
|
|
} else {
|
|
|
|
general_case:
|
2016-12-19 00:23:33 +08:00
|
|
|
gen_opf(op);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pointed_size(CType *type)
|
|
|
|
{
|
|
|
|
int align;
|
|
|
|
return type_size(pointed_type(type), &align);
|
|
|
|
}
|
|
|
|
|
2011-04-07 00:17:03 +08:00
|
|
|
static void vla_runtime_pointed_size(CType *type)
|
|
|
|
{
|
|
|
|
int align;
|
|
|
|
vla_runtime_type_size(pointed_type(type), &align);
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
static inline int is_null_pointer(SValue *p)
|
|
|
|
{
|
|
|
|
if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
|
|
|
|
return 0;
|
2015-11-18 03:09:35 +08:00
|
|
|
return ((p->type.t & VT_BTYPE) == VT_INT && (uint32_t)p->c.i == 0) ||
|
|
|
|
((p->type.t & VT_BTYPE) == VT_LLONG && p->c.i == 0) ||
|
|
|
|
((p->type.t & VT_BTYPE) == VT_PTR &&
|
2018-11-28 17:22:37 +08:00
|
|
|
(PTR_SIZE == 4 ? (uint32_t)p->c.i == 0 : p->c.i == 0) &&
|
2018-12-01 06:43:30 +08:00
|
|
|
((pointed_type(&p->type)->t & VT_BTYPE) == VT_VOID) &&
|
|
|
|
0 == (pointed_type(&p->type)->t & (VT_CONSTANT | VT_VOLATILE))
|
2018-11-28 17:22:37 +08:00
|
|
|
);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
static inline int is_integer_btype(int bt)
|
|
|
|
{
|
2015-07-30 04:53:57 +08:00
|
|
|
return (bt == VT_BYTE || bt == VT_SHORT ||
|
2009-05-06 02:18:10 +08:00
|
|
|
bt == VT_INT || bt == VT_LLONG);
|
|
|
|
}
|
|
|
|
|
2014-04-07 19:31:00 +08:00
|
|
|
/* check types for comparison or subtraction of pointers */
|
2009-05-06 02:18:10 +08:00
|
|
|
static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
|
|
|
|
{
|
|
|
|
CType *type1, *type2, tmp_type1, tmp_type2;
|
|
|
|
int bt1, bt2;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* null pointers are accepted for all comparisons as gcc */
|
|
|
|
if (is_null_pointer(p1) || is_null_pointer(p2))
|
|
|
|
return;
|
|
|
|
type1 = &p1->type;
|
|
|
|
type2 = &p2->type;
|
|
|
|
bt1 = type1->t & VT_BTYPE;
|
|
|
|
bt2 = type2->t & VT_BTYPE;
|
|
|
|
/* accept comparison between pointer and integer with a warning */
|
|
|
|
if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
|
|
|
|
if (op != TOK_LOR && op != TOK_LAND )
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("comparison between pointer and integer");
|
2009-05-06 02:18:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* both must be pointers or implicit function pointers */
|
|
|
|
if (bt1 == VT_PTR) {
|
|
|
|
type1 = pointed_type(type1);
|
2015-07-30 04:53:57 +08:00
|
|
|
} else if (bt1 != VT_FUNC)
|
2009-05-06 02:18:10 +08:00
|
|
|
goto invalid_operands;
|
|
|
|
|
|
|
|
if (bt2 == VT_PTR) {
|
|
|
|
type2 = pointed_type(type2);
|
2015-07-30 04:53:57 +08:00
|
|
|
} else if (bt2 != VT_FUNC) {
|
2009-05-06 02:18:10 +08:00
|
|
|
invalid_operands:
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2015-07-30 04:53:57 +08:00
|
|
|
if ((type1->t & VT_BTYPE) == VT_VOID ||
|
2009-05-06 02:18:10 +08:00
|
|
|
(type2->t & VT_BTYPE) == VT_VOID)
|
|
|
|
return;
|
|
|
|
tmp_type1 = *type1;
|
|
|
|
tmp_type2 = *type2;
|
2014-02-06 20:51:47 +08:00
|
|
|
tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
|
|
|
|
tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
|
|
|
|
/* gcc-like error if '-' is used */
|
|
|
|
if (op == '-')
|
|
|
|
goto invalid_operands;
|
|
|
|
else
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("comparison of distinct pointer types lacks a cast");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generic gen_op: handles types problems */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void gen_op(int op)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int u, t1, t2, bt1, bt2, t;
|
|
|
|
CType type1;
|
|
|
|
|
2016-07-14 10:09:49 +08:00
|
|
|
redo:
|
2009-05-06 02:18:10 +08:00
|
|
|
t1 = vtop[-1].type.t;
|
|
|
|
t2 = vtop[0].type.t;
|
|
|
|
bt1 = t1 & VT_BTYPE;
|
|
|
|
bt2 = t2 & VT_BTYPE;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2016-05-12 07:12:04 +08:00
|
|
|
if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
|
|
|
|
tcc_error("operation on a struct");
|
2016-07-14 10:09:49 +08:00
|
|
|
} else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
|
|
|
|
if (bt2 == VT_FUNC) {
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
gaddrof();
|
|
|
|
}
|
|
|
|
if (bt1 == VT_FUNC) {
|
|
|
|
vswap();
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
gaddrof();
|
|
|
|
vswap();
|
|
|
|
}
|
|
|
|
goto redo;
|
2016-05-12 07:12:04 +08:00
|
|
|
} else if (bt1 == VT_PTR || bt2 == VT_PTR) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* at least one operand is a pointer */
|
2017-05-08 12:38:09 +08:00
|
|
|
/* relational op: must be both pointers */
|
2009-05-06 02:18:10 +08:00
|
|
|
if (op >= TOK_ULT && op <= TOK_LOR) {
|
|
|
|
check_comparison_pointer_types(vtop - 1, vtop, op);
|
|
|
|
/* pointers are handled are unsigned */
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2009-05-06 02:18:10 +08:00
|
|
|
t = VT_LLONG | VT_UNSIGNED;
|
|
|
|
#else
|
|
|
|
t = VT_INT | VT_UNSIGNED;
|
|
|
|
#endif
|
|
|
|
goto std_op;
|
|
|
|
}
|
|
|
|
/* if both pointers, then it must be the '-' op */
|
|
|
|
if (bt1 == VT_PTR && bt2 == VT_PTR) {
|
|
|
|
if (op != '-')
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("cannot use pointers here");
|
2009-05-06 02:18:10 +08:00
|
|
|
check_comparison_pointer_types(vtop - 1, vtop, op);
|
|
|
|
/* XXX: check that types are compatible */
|
2011-04-07 00:17:03 +08:00
|
|
|
if (vtop[-1].type.t & VT_VLA) {
|
|
|
|
vla_runtime_pointed_size(&vtop[-1].type);
|
|
|
|
} else {
|
2011-04-12 14:39:27 +08:00
|
|
|
vpushi(pointed_size(&vtop[-1].type));
|
2011-04-07 00:17:03 +08:00
|
|
|
}
|
2011-04-12 14:39:27 +08:00
|
|
|
vrott(3);
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_opic(op);
|
2017-09-25 00:57:48 +08:00
|
|
|
vtop->type.t = ptrdiff_type.t;
|
2011-04-12 14:39:27 +08:00
|
|
|
vswap();
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op(TOK_PDIV);
|
|
|
|
} else {
|
|
|
|
/* exactly one pointer : must be '+' or '-'. */
|
|
|
|
if (op != '-' && op != '+')
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("cannot use pointers here");
|
2009-05-06 02:18:10 +08:00
|
|
|
/* Put pointer as first operand */
|
|
|
|
if (bt2 == VT_PTR) {
|
|
|
|
vswap();
|
2017-02-09 02:45:31 +08:00
|
|
|
t = t1, t1 = t2, t2 = t;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-10-14 01:21:43 +08:00
|
|
|
#if PTR_SIZE == 4
|
|
|
|
if ((vtop[0].type.t & VT_BTYPE) == VT_LLONG)
|
|
|
|
/* XXX: truncate here because gen_opl can't handle ptr + long long */
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_INT);
|
2016-10-14 01:21:43 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
type1 = vtop[-1].type;
|
2011-04-08 16:09:39 +08:00
|
|
|
type1.t &= ~VT_ARRAY;
|
2011-04-07 00:17:03 +08:00
|
|
|
if (vtop[-1].type.t & VT_VLA)
|
|
|
|
vla_runtime_pointed_size(&vtop[-1].type);
|
|
|
|
else {
|
|
|
|
u = pointed_size(&vtop[-1].type);
|
|
|
|
if (u < 0)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("unknown array element size");
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2011-04-07 00:17:03 +08:00
|
|
|
vpushll(u);
|
2009-05-06 02:18:10 +08:00
|
|
|
#else
|
2011-04-07 00:17:03 +08:00
|
|
|
/* XXX: cast to int ? (long long case) */
|
|
|
|
vpushi(u);
|
2009-05-06 02:18:10 +08:00
|
|
|
#endif
|
2011-04-07 00:17:03 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op('*');
|
2015-04-10 20:17:22 +08:00
|
|
|
#if 0
|
|
|
|
/* #ifdef CONFIG_TCC_BCHECK
|
|
|
|
The main reason to removing this code:
|
2015-07-30 04:53:57 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
int v[10];
|
|
|
|
int i = 10;
|
|
|
|
int j = 9;
|
|
|
|
fprintf(stderr, "v+i-j = %p\n", v+i-j);
|
|
|
|
fprintf(stderr, "v+(i-j) = %p\n", v+(i-j));
|
|
|
|
}
|
|
|
|
When this code is on. then the output looks like
|
|
|
|
v+i-j = 0xfffffffe
|
|
|
|
v+(i-j) = 0xbff84000
|
2015-04-10 20:17:22 +08:00
|
|
|
*/
|
2009-05-06 02:18:10 +08:00
|
|
|
/* if evaluating constant expression, no code should be
|
|
|
|
generated, so no bound check */
|
2009-05-12 00:45:44 +08:00
|
|
|
if (tcc_state->do_bounds_check && !const_wanted) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* if bounded pointers, we generate a special code to
|
|
|
|
test bounds */
|
|
|
|
if (op == '-') {
|
|
|
|
vpushi(0);
|
|
|
|
vswap();
|
|
|
|
gen_op('-');
|
|
|
|
}
|
|
|
|
gen_bounded_ptr_add();
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
gen_opic(op);
|
|
|
|
}
|
|
|
|
/* put again type if gen_opic() swaped operands */
|
|
|
|
vtop->type = type1;
|
|
|
|
}
|
|
|
|
} else if (is_float(bt1) || is_float(bt2)) {
|
|
|
|
/* compute bigger type and do implicit casts */
|
|
|
|
if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
|
|
|
|
t = VT_LDOUBLE;
|
|
|
|
} else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
|
|
|
|
t = VT_DOUBLE;
|
|
|
|
} else {
|
|
|
|
t = VT_FLOAT;
|
|
|
|
}
|
|
|
|
/* floats can only be used for a few operations */
|
|
|
|
if (op != '+' && op != '-' && op != '*' && op != '/' &&
|
|
|
|
(op < TOK_ULT || op > TOK_GT))
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("invalid operands for binary operation");
|
2009-05-06 02:18:10 +08:00
|
|
|
goto std_op;
|
2012-06-27 19:31:44 +08:00
|
|
|
} else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
|
2012-07-06 20:22:37 +08:00
|
|
|
t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
|
bitfields: promote to signed int
For integer promotion with for example arithmetics or
expr_cond (x ? y : z), integral types need to be promoted
to signed if they fit.
According to latest standards, this also applies to bit-field
types taking into account their specific width.
In tcc, VT_BITFIELD set means width < original type width
Field-widths between 33 and 63 are promoted to signed long long
accordingly.
struct { unsigned long long ullb:35; } s = { 1 };
#define X (s.ullb - 2)
int main (void)
{
long long Y = X;
printf("%d %016llx %016llx\n", X < 0, -X, -Y);
return 0;
}
Results:
GCC 4.7 : 0 0000000000000001 FFFFFFF800000001
MSVC : 1 0000000000000001 0000000000000001
TCC : 1 0000000000000001 0000000000000001
Also, gcc would promote long long bitfields of size < 32
to int as well. Example:
struct { unsigned long long x:20; } t = { 123 };
/* with gcc: */ printf("%d %d\n", t.x, 456);
/* with tcc: */ printf("%lld %d\n", t.x, 456);
2017-05-10 00:36:24 +08:00
|
|
|
if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (t | VT_UNSIGNED))
|
2012-07-06 20:22:37 +08:00
|
|
|
t |= VT_UNSIGNED;
|
2017-09-25 00:57:48 +08:00
|
|
|
t |= (VT_LONG & t1);
|
2012-06-27 19:31:44 +08:00
|
|
|
goto std_op;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
|
|
|
|
/* cast to biggest op */
|
2017-09-25 00:57:48 +08:00
|
|
|
t = VT_LLONG | VT_LONG;
|
|
|
|
if (bt1 == VT_LLONG)
|
|
|
|
t &= t1;
|
|
|
|
if (bt2 == VT_LLONG)
|
|
|
|
t &= t2;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* convert to unsigned if it does not fit in a long long */
|
bitfields: promote to signed int
For integer promotion with for example arithmetics or
expr_cond (x ? y : z), integral types need to be promoted
to signed if they fit.
According to latest standards, this also applies to bit-field
types taking into account their specific width.
In tcc, VT_BITFIELD set means width < original type width
Field-widths between 33 and 63 are promoted to signed long long
accordingly.
struct { unsigned long long ullb:35; } s = { 1 };
#define X (s.ullb - 2)
int main (void)
{
long long Y = X;
printf("%d %016llx %016llx\n", X < 0, -X, -Y);
return 0;
}
Results:
GCC 4.7 : 0 0000000000000001 FFFFFFF800000001
MSVC : 1 0000000000000001 0000000000000001
TCC : 1 0000000000000001 0000000000000001
Also, gcc would promote long long bitfields of size < 32
to int as well. Example:
struct { unsigned long long x:20; } t = { 123 };
/* with gcc: */ printf("%d %d\n", t.x, 456);
/* with tcc: */ printf("%lld %d\n", t.x, 456);
2017-05-10 00:36:24 +08:00
|
|
|
if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
|
|
|
|
(t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
|
2009-05-06 02:18:10 +08:00
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
goto std_op;
|
|
|
|
} else {
|
|
|
|
/* integer operations */
|
2017-09-25 00:57:48 +08:00
|
|
|
t = VT_INT | (VT_LONG & (t1 | t2));
|
|
|
|
/* convert to unsigned if it does not fit in an integer */
|
bitfields: promote to signed int
For integer promotion with for example arithmetics or
expr_cond (x ? y : z), integral types need to be promoted
to signed if they fit.
According to latest standards, this also applies to bit-field
types taking into account their specific width.
In tcc, VT_BITFIELD set means width < original type width
Field-widths between 33 and 63 are promoted to signed long long
accordingly.
struct { unsigned long long ullb:35; } s = { 1 };
#define X (s.ullb - 2)
int main (void)
{
long long Y = X;
printf("%d %016llx %016llx\n", X < 0, -X, -Y);
return 0;
}
Results:
GCC 4.7 : 0 0000000000000001 FFFFFFF800000001
MSVC : 1 0000000000000001 0000000000000001
TCC : 1 0000000000000001 0000000000000001
Also, gcc would promote long long bitfields of size < 32
to int as well. Example:
struct { unsigned long long x:20; } t = { 123 };
/* with gcc: */ printf("%d %d\n", t.x, 456);
/* with tcc: */ printf("%lld %d\n", t.x, 456);
2017-05-10 00:36:24 +08:00
|
|
|
if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
|
|
|
|
(t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
|
2009-05-06 02:18:10 +08:00
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
std_op:
|
|
|
|
/* XXX: currently, some unsigned operations are explicit, so
|
|
|
|
we modify them here */
|
|
|
|
if (t & VT_UNSIGNED) {
|
|
|
|
if (op == TOK_SAR)
|
|
|
|
op = TOK_SHR;
|
|
|
|
else if (op == '/')
|
|
|
|
op = TOK_UDIV;
|
|
|
|
else if (op == '%')
|
|
|
|
op = TOK_UMOD;
|
|
|
|
else if (op == TOK_LT)
|
|
|
|
op = TOK_ULT;
|
|
|
|
else if (op == TOK_GT)
|
|
|
|
op = TOK_UGT;
|
|
|
|
else if (op == TOK_LE)
|
|
|
|
op = TOK_ULE;
|
|
|
|
else if (op == TOK_GE)
|
|
|
|
op = TOK_UGE;
|
|
|
|
}
|
|
|
|
vswap();
|
|
|
|
type1.t = t;
|
2017-07-09 18:34:11 +08:00
|
|
|
type1.ref = NULL;
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_cast(&type1);
|
|
|
|
vswap();
|
|
|
|
/* special case for shifts and long long: we keep the shift as
|
|
|
|
an integer */
|
|
|
|
if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
|
|
|
|
type1.t = VT_INT;
|
|
|
|
gen_cast(&type1);
|
|
|
|
if (is_float(t))
|
|
|
|
gen_opif(op);
|
|
|
|
else
|
|
|
|
gen_opic(op);
|
|
|
|
if (op >= TOK_ULT && op <= TOK_GT) {
|
2017-05-08 12:38:09 +08:00
|
|
|
/* relational op: the result is an int */
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop->type.t = VT_INT;
|
|
|
|
} else {
|
|
|
|
vtop->type.t = t;
|
|
|
|
}
|
|
|
|
}
|
2015-02-21 06:18:41 +08:00
|
|
|
// Make sure that we have converted to an rvalue:
|
2016-12-19 00:23:33 +08:00
|
|
|
if (vtop->r & VT_LVAL)
|
2015-02-21 06:18:41 +08:00
|
|
|
gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TCC_TARGET_ARM
|
|
|
|
/* generic itof for unsigned long long case */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gen_cvt_itof1(int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2015-02-14 02:58:31 +08:00
|
|
|
#ifdef TCC_TARGET_ARM64
|
|
|
|
gen_cvt_itof(t);
|
|
|
|
#else
|
2015-07-30 04:53:57 +08:00
|
|
|
if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
|
2009-05-06 02:18:10 +08:00
|
|
|
(VT_LLONG | VT_UNSIGNED)) {
|
|
|
|
|
|
|
|
if (t == VT_FLOAT)
|
|
|
|
vpush_global_sym(&func_old_type, TOK___floatundisf);
|
|
|
|
#if LDOUBLE_SIZE != 8
|
|
|
|
else if (t == VT_LDOUBLE)
|
|
|
|
vpush_global_sym(&func_old_type, TOK___floatundixf);
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
vpush_global_sym(&func_old_type, TOK___floatundidf);
|
|
|
|
vrott(2);
|
|
|
|
gfunc_call(1);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = reg_fret(t);
|
|
|
|
} else {
|
|
|
|
gen_cvt_itof(t);
|
|
|
|
}
|
2015-02-14 02:58:31 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* generic ftoi for unsigned long long case */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void gen_cvt_ftoi1(int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2015-02-14 02:58:31 +08:00
|
|
|
#ifdef TCC_TARGET_ARM64
|
|
|
|
gen_cvt_ftoi(t);
|
|
|
|
#else
|
2009-05-06 02:18:10 +08:00
|
|
|
int st;
|
|
|
|
|
|
|
|
if (t == (VT_LLONG | VT_UNSIGNED)) {
|
|
|
|
/* not handled natively */
|
|
|
|
st = vtop->type.t & VT_BTYPE;
|
|
|
|
if (st == VT_FLOAT)
|
|
|
|
vpush_global_sym(&func_old_type, TOK___fixunssfdi);
|
|
|
|
#if LDOUBLE_SIZE != 8
|
|
|
|
else if (st == VT_LDOUBLE)
|
|
|
|
vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
|
|
|
|
vrott(2);
|
|
|
|
gfunc_call(1);
|
|
|
|
vpushi(0);
|
|
|
|
vtop->r = REG_IRET;
|
|
|
|
vtop->r2 = REG_LRET;
|
|
|
|
} else {
|
|
|
|
gen_cvt_ftoi(t);
|
|
|
|
}
|
2015-02-14 02:58:31 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* force char or short cast */
|
2009-12-20 08:53:49 +08:00
|
|
|
static void force_charshort_cast(int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int bits, dbt;
|
2017-07-16 18:10:00 +08:00
|
|
|
|
|
|
|
/* cannot cast static initializers */
|
|
|
|
if (STATIC_DATA_WANTED)
|
|
|
|
return;
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
dbt = t & VT_BTYPE;
|
|
|
|
/* XXX: add optimization if lvalue : just change type and offset */
|
|
|
|
if (dbt == VT_BYTE)
|
|
|
|
bits = 8;
|
|
|
|
else
|
|
|
|
bits = 16;
|
|
|
|
if (t & VT_UNSIGNED) {
|
|
|
|
vpushi((1 << bits) - 1);
|
|
|
|
gen_op('&');
|
|
|
|
} else {
|
2016-10-02 07:38:22 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
|
|
|
|
bits = 64 - bits;
|
|
|
|
else
|
|
|
|
bits = 32 - bits;
|
2009-05-06 02:18:10 +08:00
|
|
|
vpushi(bits);
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
/* result must be signed or the SAR is converted to an SHL
|
|
|
|
This was not the case when "t" was a signed short
|
|
|
|
and the last value on the stack was an unsigned int */
|
|
|
|
vtop->type.t &= ~VT_UNSIGNED;
|
|
|
|
vpushi(bits);
|
|
|
|
gen_op(TOK_SAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
|
2017-07-09 18:34:11 +08:00
|
|
|
static void gen_cast_s(int t)
|
|
|
|
{
|
|
|
|
CType type;
|
|
|
|
type.t = t;
|
|
|
|
type.ref = NULL;
|
|
|
|
gen_cast(&type);
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
static void gen_cast(CType *type)
|
|
|
|
{
|
|
|
|
int sbt, dbt, sf, df, c, p;
|
|
|
|
|
|
|
|
/* special delayed cast for char/short */
|
|
|
|
/* XXX: in some cases (multiple cascaded casts), it may still
|
|
|
|
be incorrect */
|
|
|
|
if (vtop->r & VT_MUSTCAST) {
|
|
|
|
vtop->r &= ~VT_MUSTCAST;
|
|
|
|
force_charshort_cast(vtop->type.t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bitfields first get cast to ints */
|
2016-12-19 00:23:33 +08:00
|
|
|
if (vtop->type.t & VT_BITFIELD) {
|
2009-05-06 02:18:10 +08:00
|
|
|
gv(RC_INT);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
|
|
|
|
sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
|
|
|
|
|
|
|
|
if (sbt != dbt) {
|
|
|
|
sf = is_float(sbt);
|
|
|
|
df = is_float(dbt);
|
|
|
|
c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
#if !defined TCC_IS_NATIVE && !defined TCC_IS_NATIVE_387
|
|
|
|
c &= dbt != VT_LDOUBLE;
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
if (c) {
|
|
|
|
/* constant case: we can do it now */
|
|
|
|
/* XXX: in ISOC, cannot do it if error in convert */
|
|
|
|
if (sbt == VT_FLOAT)
|
|
|
|
vtop->c.ld = vtop->c.f;
|
|
|
|
else if (sbt == VT_DOUBLE)
|
|
|
|
vtop->c.ld = vtop->c.d;
|
|
|
|
|
|
|
|
if (df) {
|
|
|
|
if ((sbt & VT_BTYPE) == VT_LLONG) {
|
2015-11-18 03:09:35 +08:00
|
|
|
if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 63))
|
|
|
|
vtop->c.ld = vtop->c.i;
|
2009-05-06 02:18:10 +08:00
|
|
|
else
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.ld = -(long double)-vtop->c.i;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if(!sf) {
|
2015-11-18 03:09:35 +08:00
|
|
|
if ((sbt & VT_UNSIGNED) || !(vtop->c.i >> 31))
|
|
|
|
vtop->c.ld = (uint32_t)vtop->c.i;
|
2009-05-06 02:18:10 +08:00
|
|
|
else
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.ld = -(long double)-(uint32_t)vtop->c.i;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dbt == VT_FLOAT)
|
|
|
|
vtop->c.f = (float)vtop->c.ld;
|
|
|
|
else if (dbt == VT_DOUBLE)
|
|
|
|
vtop->c.d = (double)vtop->c.ld;
|
|
|
|
} else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = vtop->c.ld;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (sf && dbt == VT_BOOL) {
|
|
|
|
vtop->c.i = (vtop->c.ld != 0);
|
|
|
|
} else {
|
|
|
|
if(sf)
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = vtop->c.ld;
|
2009-05-06 02:18:10 +08:00
|
|
|
else if (sbt == (VT_LLONG|VT_UNSIGNED))
|
2015-11-18 03:09:35 +08:00
|
|
|
;
|
2009-05-06 02:18:10 +08:00
|
|
|
else if (sbt & VT_UNSIGNED)
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = (uint32_t)vtop->c.i;
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2009-12-16 00:45:15 +08:00
|
|
|
else if (sbt == VT_PTR)
|
|
|
|
;
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
else if (sbt != VT_LLONG)
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = ((uint32_t)vtop->c.i |
|
|
|
|
-(vtop->c.i & 0x80000000));
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
if (dbt == (VT_LLONG|VT_UNSIGNED))
|
2015-11-18 03:09:35 +08:00
|
|
|
;
|
2009-05-06 02:18:10 +08:00
|
|
|
else if (dbt == VT_BOOL)
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = (vtop->c.i != 0);
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2014-01-12 06:42:58 +08:00
|
|
|
else if (dbt == VT_PTR)
|
|
|
|
;
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
else if (dbt != VT_LLONG) {
|
2015-11-18 03:09:35 +08:00
|
|
|
uint32_t m = ((dbt & VT_BTYPE) == VT_BYTE ? 0xff :
|
|
|
|
(dbt & VT_BTYPE) == VT_SHORT ? 0xffff :
|
|
|
|
0xffffffff);
|
|
|
|
vtop->c.i &= m;
|
|
|
|
if (!(dbt & VT_UNSIGNED))
|
|
|
|
vtop->c.i |= -(vtop->c.i & ((m >> 1) + 1));
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (p && dbt == VT_BOOL) {
|
|
|
|
vtop->r = VT_CONST;
|
|
|
|
vtop->c.i = 1;
|
2016-12-19 00:23:33 +08:00
|
|
|
} else {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* non constant case: generate code */
|
|
|
|
if (sf && df) {
|
|
|
|
/* convert from fp to fp */
|
|
|
|
gen_cvt_ftof(dbt);
|
|
|
|
} else if (df) {
|
|
|
|
/* convert int to fp */
|
|
|
|
gen_cvt_itof1(dbt);
|
|
|
|
} else if (sf) {
|
|
|
|
/* convert fp to int */
|
|
|
|
if (dbt == VT_BOOL) {
|
|
|
|
vpushi(0);
|
|
|
|
gen_op(TOK_NE);
|
|
|
|
} else {
|
|
|
|
/* we handle char/short/etc... with generic code */
|
|
|
|
if (dbt != (VT_INT | VT_UNSIGNED) &&
|
|
|
|
dbt != (VT_LLONG | VT_UNSIGNED) &&
|
|
|
|
dbt != VT_LLONG)
|
|
|
|
dbt = VT_INT;
|
|
|
|
gen_cvt_ftoi1(dbt);
|
|
|
|
if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
|
|
|
|
/* additional cast for char/short... */
|
|
|
|
vtop->type.t = dbt;
|
|
|
|
gen_cast(type);
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 4
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if ((dbt & VT_BTYPE) == VT_LLONG) {
|
2016-10-16 17:03:57 +08:00
|
|
|
if ((sbt & VT_BTYPE) != VT_LLONG) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* scalar to long long */
|
|
|
|
/* machine independent conversion */
|
|
|
|
gv(RC_INT);
|
|
|
|
/* generate high word */
|
|
|
|
if (sbt == (VT_INT | VT_UNSIGNED)) {
|
|
|
|
vpushi(0);
|
|
|
|
gv(RC_INT);
|
|
|
|
} else {
|
|
|
|
if (sbt == VT_PTR) {
|
|
|
|
/* cast from pointer to int before we apply
|
|
|
|
shift operation, which pointers don't support*/
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_INT);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
gv_dup();
|
|
|
|
vpushi(31);
|
|
|
|
gen_op(TOK_SAR);
|
|
|
|
}
|
|
|
|
/* patch second register */
|
|
|
|
vtop[-1].r2 = vtop->r;
|
|
|
|
vpop();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
} else if ((dbt & VT_BTYPE) == VT_LLONG ||
|
2010-08-31 07:35:31 +08:00
|
|
|
(dbt & VT_BTYPE) == VT_PTR ||
|
|
|
|
(dbt & VT_BTYPE) == VT_FUNC) {
|
|
|
|
if ((sbt & VT_BTYPE) != VT_LLONG &&
|
|
|
|
(sbt & VT_BTYPE) != VT_PTR &&
|
2016-10-16 17:03:57 +08:00
|
|
|
(sbt & VT_BTYPE) != VT_FUNC) {
|
2010-08-31 07:35:31 +08:00
|
|
|
/* need to convert from 32bit to 64bit */
|
2015-02-14 02:58:31 +08:00
|
|
|
gv(RC_INT);
|
2010-08-31 07:35:31 +08:00
|
|
|
if (sbt != (VT_INT | VT_UNSIGNED)) {
|
2015-02-14 02:58:31 +08:00
|
|
|
#if defined(TCC_TARGET_ARM64)
|
|
|
|
gen_cvt_sxtw();
|
|
|
|
#elif defined(TCC_TARGET_X86_64)
|
|
|
|
int r = gv(RC_INT);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* x86_64 specific: movslq */
|
|
|
|
o(0x6348);
|
|
|
|
o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
|
2015-02-14 02:58:31 +08:00
|
|
|
#else
|
|
|
|
#error
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else if (dbt == VT_BOOL) {
|
|
|
|
/* scalar to bool */
|
|
|
|
vpushi(0);
|
|
|
|
gen_op(TOK_NE);
|
2015-07-30 04:53:57 +08:00
|
|
|
} else if ((dbt & VT_BTYPE) == VT_BYTE ||
|
2009-05-06 02:18:10 +08:00
|
|
|
(dbt & VT_BTYPE) == VT_SHORT) {
|
|
|
|
if (sbt == VT_PTR) {
|
|
|
|
vtop->type.t = VT_INT;
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("nonportable conversion from pointer to char/short");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
force_charshort_cast(dbt);
|
|
|
|
} else if ((dbt & VT_BTYPE) == VT_INT) {
|
|
|
|
/* scalar to int */
|
2016-10-16 17:03:57 +08:00
|
|
|
if ((sbt & VT_BTYPE) == VT_LLONG) {
|
2018-04-01 03:52:20 +08:00
|
|
|
#if PTR_SIZE == 4
|
2009-05-06 02:18:10 +08:00
|
|
|
/* from long long: just take low order word */
|
|
|
|
lexpand();
|
|
|
|
vpop();
|
2018-04-01 03:52:20 +08:00
|
|
|
#else
|
|
|
|
vpushi(0xffffffff);
|
|
|
|
vtop->type.t |= VT_UNSIGNED;
|
|
|
|
gen_op('&');
|
|
|
|
#endif
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
/* if lvalue and single word type, nothing to do because
|
|
|
|
the lvalue already contains the real type size (see
|
|
|
|
VT_LVAL_xxx constants) */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
|
|
|
|
/* if we are casting between pointer types,
|
|
|
|
we must update the VT_LVAL_xxx size */
|
|
|
|
vtop->r = (vtop->r & ~VT_LVAL_TYPE)
|
|
|
|
| (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
|
|
|
|
}
|
|
|
|
vtop->type = *type;
|
2018-11-21 02:24:24 +08:00
|
|
|
vtop->type.t &= ~ ( VT_CONSTANT | VT_VOLATILE | VT_ARRAY );
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2011-04-07 00:17:03 +08:00
|
|
|
/* return type size as known at compile time. Put alignment at 'a' */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC int type_size(CType *type, int *a)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
int bt;
|
|
|
|
|
|
|
|
bt = type->t & VT_BTYPE;
|
|
|
|
if (bt == VT_STRUCT) {
|
|
|
|
/* struct/union */
|
|
|
|
s = type->ref;
|
2011-03-19 08:47:35 +08:00
|
|
|
*a = s->r;
|
2009-05-06 02:18:10 +08:00
|
|
|
return s->c;
|
|
|
|
} else if (bt == VT_PTR) {
|
2011-04-08 16:09:39 +08:00
|
|
|
if (type->t & VT_ARRAY) {
|
2009-05-06 02:18:10 +08:00
|
|
|
int ts;
|
|
|
|
|
|
|
|
s = type->ref;
|
|
|
|
ts = type_size(&s->type, a);
|
|
|
|
|
|
|
|
if (ts < 0 && s->c < 0)
|
|
|
|
ts = -ts;
|
|
|
|
|
|
|
|
return ts * s->c;
|
|
|
|
} else {
|
|
|
|
*a = PTR_SIZE;
|
|
|
|
return PTR_SIZE;
|
|
|
|
}
|
2019-04-07 09:15:05 +08:00
|
|
|
} else if (IS_ENUM(type->t) && type->ref->c < 0) {
|
2017-07-09 18:38:25 +08:00
|
|
|
return -1; /* incomplete enum */
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (bt == VT_LDOUBLE) {
|
|
|
|
*a = LDOUBLE_ALIGN;
|
|
|
|
return LDOUBLE_SIZE;
|
|
|
|
} else if (bt == VT_DOUBLE || bt == VT_LLONG) {
|
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
*a = 8;
|
|
|
|
#else
|
|
|
|
*a = 4;
|
|
|
|
#endif
|
|
|
|
#elif defined(TCC_TARGET_ARM)
|
|
|
|
#ifdef TCC_ARM_EABI
|
2015-07-30 04:53:57 +08:00
|
|
|
*a = 8;
|
2009-05-06 02:18:10 +08:00
|
|
|
#else
|
|
|
|
*a = 4;
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
*a = 8;
|
|
|
|
#endif
|
|
|
|
return 8;
|
2016-03-24 22:44:01 +08:00
|
|
|
} else if (bt == VT_INT || bt == VT_FLOAT) {
|
2009-05-06 02:18:10 +08:00
|
|
|
*a = 4;
|
|
|
|
return 4;
|
|
|
|
} else if (bt == VT_SHORT) {
|
|
|
|
*a = 2;
|
|
|
|
return 2;
|
2013-04-19 18:08:12 +08:00
|
|
|
} else if (bt == VT_QLONG || bt == VT_QFLOAT) {
|
|
|
|
*a = 8;
|
|
|
|
return 16;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
/* char, void, function, _Bool */
|
|
|
|
*a = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-07 00:17:03 +08:00
|
|
|
/* push type size as known at runtime time on top of value stack. Put
|
|
|
|
alignment at 'a' */
|
|
|
|
ST_FUNC void vla_runtime_type_size(CType *type, int *a)
|
|
|
|
{
|
|
|
|
if (type->t & VT_VLA) {
|
2016-10-09 06:13:31 +08:00
|
|
|
type_size(&type->ref->type, a);
|
2011-04-10 13:52:25 +08:00
|
|
|
vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
|
2011-04-07 00:17:03 +08:00
|
|
|
} else {
|
2011-04-10 13:52:25 +08:00
|
|
|
vpushi(type_size(type, a));
|
2011-04-07 00:17:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 09:09:05 +08:00
|
|
|
static void vla_sp_restore(void) {
|
|
|
|
if (vlas_in_scope) {
|
|
|
|
gen_vla_sp_restore(vla_sp_loc);
|
2013-04-28 03:39:34 +08:00
|
|
|
}
|
2015-05-04 09:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vla_sp_restore_root(void) {
|
|
|
|
if (vlas_in_scope) {
|
|
|
|
gen_vla_sp_restore(vla_sp_root_loc);
|
2013-04-28 03:39:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* return the pointed type of t */
|
|
|
|
static inline CType *pointed_type(CType *type)
|
|
|
|
{
|
|
|
|
return &type->ref->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* modify type so that its it is a pointer to type. */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void mk_pointer(CType *type)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
s = sym_push(SYM_FIELD, type, 0, -1);
|
2017-07-09 18:38:25 +08:00
|
|
|
type->t = VT_PTR | (type->t & VT_STORAGE);
|
2009-05-06 02:18:10 +08:00
|
|
|
type->ref = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compare function types. OLD functions match any new functions */
|
|
|
|
static int is_compatible_func(CType *type1, CType *type2)
|
|
|
|
{
|
|
|
|
Sym *s1, *s2;
|
|
|
|
|
|
|
|
s1 = type1->ref;
|
|
|
|
s2 = type2->ref;
|
|
|
|
if (!is_compatible_types(&s1->type, &s2->type))
|
|
|
|
return 0;
|
|
|
|
/* check func_call */
|
2017-07-09 18:34:11 +08:00
|
|
|
if (s1->f.func_call != s2->f.func_call)
|
2009-05-06 02:18:10 +08:00
|
|
|
return 0;
|
|
|
|
/* XXX: not complete */
|
2017-07-09 18:34:11 +08:00
|
|
|
if (s1->f.func_type == FUNC_OLD || s2->f.func_type == FUNC_OLD)
|
2009-05-06 02:18:10 +08:00
|
|
|
return 1;
|
2017-07-09 18:34:11 +08:00
|
|
|
if (s1->f.func_type != s2->f.func_type)
|
2009-05-06 02:18:10 +08:00
|
|
|
return 0;
|
|
|
|
while (s1 != NULL) {
|
|
|
|
if (s2 == NULL)
|
|
|
|
return 0;
|
2017-07-09 18:38:25 +08:00
|
|
|
if (!is_compatible_unqualified_types(&s1->type, &s2->type))
|
2009-05-06 02:18:10 +08:00
|
|
|
return 0;
|
|
|
|
s1 = s1->next;
|
|
|
|
s2 = s2->next;
|
|
|
|
}
|
|
|
|
if (s2)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return true if type1 and type2 are the same. If unqualified is
|
|
|
|
true, qualifiers on the types are ignored.
|
|
|
|
*/
|
|
|
|
static int compare_types(CType *type1, CType *type2, int unqualified)
|
|
|
|
{
|
|
|
|
int bt1, t1, t2;
|
|
|
|
|
|
|
|
t1 = type1->t & VT_TYPE;
|
|
|
|
t2 = type2->t & VT_TYPE;
|
|
|
|
if (unqualified) {
|
|
|
|
/* strip qualifiers before comparing */
|
|
|
|
t1 &= ~(VT_CONSTANT | VT_VOLATILE);
|
|
|
|
t2 &= ~(VT_CONSTANT | VT_VOLATILE);
|
|
|
|
}
|
mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
this case is usefull as with -funsigned-char, 'char *' are unsigned
* change VT_LONG so it's now a qualifier
VT_LONG are never use for code generation, but only durring parsing state,
in _Generic we need to be able to make diference between
'long' and 'long long'
So VT_LONG is now use as a type qualifier, it's old behaviour is still
here, but we can keep trace of what was a long and what wasn't
* add TOK_CLONG and TOK_CULONG
tcc was directly converting value like '7171L' into TOK_CLLONG or
TOK_CINT depending of the machine architecture.
because of that, we was unable to make diference between a long and a
long long, which doesn't work with _Generic.
So now 7171L is a TOK_CLONG, and we can handle _Generic properly
* check that _Generic can make diference between long and long long
* uncomment "type match twice" as it should now pass tests on any platforms
* add inside_generic global
the point of this variable is to use VT_LONG in comparaison only
when we are evaluating a _Generic.
problem is with my lastest patchs tcc can now make the diference between
a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
break when stdint.h and stddef.h are include together.
Another solution woud be to modifie include/stddef.h so it define uint64_t as
unsigned long int when processor is 64 bit, but this could break some
legacy code, so for now, VT_LONG are use only inside generc.
* check that _Generic parse first argument correctly
* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-10 23:44:53 +08:00
|
|
|
|
2014-02-06 20:51:47 +08:00
|
|
|
/* Default Vs explicit signedness only matters for char */
|
|
|
|
if ((t1 & VT_BTYPE) != VT_BYTE) {
|
|
|
|
t1 &= ~VT_DEFSIGN;
|
|
|
|
t2 &= ~VT_DEFSIGN;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
/* XXX: bitfields ? */
|
|
|
|
if (t1 != t2)
|
|
|
|
return 0;
|
|
|
|
/* test more complicated cases */
|
2018-12-01 06:43:30 +08:00
|
|
|
bt1 = t1 & (VT_BTYPE | VT_ARRAY);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (bt1 == VT_PTR) {
|
|
|
|
type1 = pointed_type(type1);
|
|
|
|
type2 = pointed_type(type2);
|
|
|
|
return is_compatible_types(type1, type2);
|
2018-11-13 00:19:15 +08:00
|
|
|
} else if (bt1 & VT_ARRAY) {
|
|
|
|
return type1->ref->c < 0 || type2->ref->c < 0
|
|
|
|
|| type1->ref->c == type2->ref->c;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (bt1 == VT_STRUCT) {
|
|
|
|
return (type1->ref == type2->ref);
|
|
|
|
} else if (bt1 == VT_FUNC) {
|
|
|
|
return is_compatible_func(type1, type2);
|
2018-06-01 05:52:07 +08:00
|
|
|
} else if (IS_ENUM(type1->t) || IS_ENUM(type2->t)) {
|
|
|
|
return type1->ref == type2->ref;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return true if type1 and type2 are exactly the same (including
|
2015-07-30 04:53:57 +08:00
|
|
|
qualifiers).
|
2009-05-06 02:18:10 +08:00
|
|
|
*/
|
|
|
|
static int is_compatible_types(CType *type1, CType *type2)
|
|
|
|
{
|
|
|
|
return compare_types(type1,type2,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return true if type1 and type2 are the same (ignoring qualifiers).
|
|
|
|
*/
|
2017-07-09 18:38:25 +08:00
|
|
|
static int is_compatible_unqualified_types(CType *type1, CType *type2)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
return compare_types(type1,type2,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print a type. If 'varstr' is not NULL, then the variable is also
|
|
|
|
printed in the type */
|
|
|
|
/* XXX: union */
|
|
|
|
/* XXX: add array and function pointers */
|
2015-07-30 04:53:57 +08:00
|
|
|
static void type_to_str(char *buf, int buf_size,
|
2009-05-06 02:18:10 +08:00
|
|
|
CType *type, const char *varstr)
|
|
|
|
{
|
|
|
|
int bt, v, t;
|
|
|
|
Sym *s, *sa;
|
|
|
|
char buf1[256];
|
|
|
|
const char *tstr;
|
|
|
|
|
2017-03-06 10:26:21 +08:00
|
|
|
t = type->t;
|
2009-05-06 02:18:10 +08:00
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
buf[0] = '\0';
|
2017-09-25 00:57:48 +08:00
|
|
|
|
2017-03-06 10:26:21 +08:00
|
|
|
if (t & VT_EXTERN)
|
|
|
|
pstrcat(buf, buf_size, "extern ");
|
|
|
|
if (t & VT_STATIC)
|
|
|
|
pstrcat(buf, buf_size, "static ");
|
|
|
|
if (t & VT_TYPEDEF)
|
|
|
|
pstrcat(buf, buf_size, "typedef ");
|
|
|
|
if (t & VT_INLINE)
|
|
|
|
pstrcat(buf, buf_size, "inline ");
|
2017-09-25 00:57:48 +08:00
|
|
|
if (t & VT_VOLATILE)
|
|
|
|
pstrcat(buf, buf_size, "volatile ");
|
|
|
|
if (t & VT_CONSTANT)
|
|
|
|
pstrcat(buf, buf_size, "const ");
|
|
|
|
|
|
|
|
if (((t & VT_DEFSIGN) && bt == VT_BYTE)
|
|
|
|
|| ((t & VT_UNSIGNED)
|
|
|
|
&& (bt == VT_SHORT || bt == VT_INT || bt == VT_LLONG)
|
|
|
|
&& !IS_ENUM(t)
|
|
|
|
))
|
|
|
|
pstrcat(buf, buf_size, (t & VT_UNSIGNED) ? "unsigned " : "signed ");
|
|
|
|
|
2017-03-06 10:26:21 +08:00
|
|
|
buf_size -= strlen(buf);
|
|
|
|
buf += strlen(buf);
|
mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
this case is usefull as with -funsigned-char, 'char *' are unsigned
* change VT_LONG so it's now a qualifier
VT_LONG are never use for code generation, but only durring parsing state,
in _Generic we need to be able to make diference between
'long' and 'long long'
So VT_LONG is now use as a type qualifier, it's old behaviour is still
here, but we can keep trace of what was a long and what wasn't
* add TOK_CLONG and TOK_CULONG
tcc was directly converting value like '7171L' into TOK_CLLONG or
TOK_CINT depending of the machine architecture.
because of that, we was unable to make diference between a long and a
long long, which doesn't work with _Generic.
So now 7171L is a TOK_CLONG, and we can handle _Generic properly
* check that _Generic can make diference between long and long long
* uncomment "type match twice" as it should now pass tests on any platforms
* add inside_generic global
the point of this variable is to use VT_LONG in comparaison only
when we are evaluating a _Generic.
problem is with my lastest patchs tcc can now make the diference between
a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
break when stdint.h and stddef.h are include together.
Another solution woud be to modifie include/stddef.h so it define uint64_t as
unsigned long int when processor is 64 bit, but this could break some
legacy code, so for now, VT_LONG are use only inside generc.
* check that _Generic parse first argument correctly
* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-10 23:44:53 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
switch(bt) {
|
|
|
|
case VT_VOID:
|
|
|
|
tstr = "void";
|
|
|
|
goto add_tstr;
|
|
|
|
case VT_BOOL:
|
|
|
|
tstr = "_Bool";
|
|
|
|
goto add_tstr;
|
|
|
|
case VT_BYTE:
|
|
|
|
tstr = "char";
|
|
|
|
goto add_tstr;
|
|
|
|
case VT_SHORT:
|
|
|
|
tstr = "short";
|
|
|
|
goto add_tstr;
|
|
|
|
case VT_INT:
|
|
|
|
tstr = "int";
|
2017-09-25 00:57:48 +08:00
|
|
|
goto maybe_long;
|
2009-05-06 02:18:10 +08:00
|
|
|
case VT_LLONG:
|
|
|
|
tstr = "long long";
|
2017-09-25 00:57:48 +08:00
|
|
|
maybe_long:
|
|
|
|
if (t & VT_LONG)
|
|
|
|
tstr = "long";
|
|
|
|
if (!IS_ENUM(t))
|
|
|
|
goto add_tstr;
|
|
|
|
tstr = "enum ";
|
|
|
|
goto tstruct;
|
2009-05-06 02:18:10 +08:00
|
|
|
case VT_FLOAT:
|
|
|
|
tstr = "float";
|
|
|
|
goto add_tstr;
|
|
|
|
case VT_DOUBLE:
|
|
|
|
tstr = "double";
|
|
|
|
goto add_tstr;
|
|
|
|
case VT_LDOUBLE:
|
|
|
|
tstr = "long double";
|
|
|
|
add_tstr:
|
|
|
|
pstrcat(buf, buf_size, tstr);
|
|
|
|
break;
|
|
|
|
case VT_STRUCT:
|
2017-07-09 18:38:25 +08:00
|
|
|
tstr = "struct ";
|
|
|
|
if (IS_UNION(t))
|
|
|
|
tstr = "union ";
|
|
|
|
tstruct:
|
2009-05-06 02:18:10 +08:00
|
|
|
pstrcat(buf, buf_size, tstr);
|
|
|
|
v = type->ref->v & ~SYM_STRUCT;
|
|
|
|
if (v >= SYM_FIRST_ANOM)
|
|
|
|
pstrcat(buf, buf_size, "<anonymous>");
|
|
|
|
else
|
|
|
|
pstrcat(buf, buf_size, get_tok_str(v, NULL));
|
|
|
|
break;
|
|
|
|
case VT_FUNC:
|
|
|
|
s = type->ref;
|
2018-03-16 07:26:16 +08:00
|
|
|
buf1[0]=0;
|
|
|
|
if (varstr && '*' == *varstr) {
|
|
|
|
pstrcat(buf1, sizeof(buf1), "(");
|
|
|
|
pstrcat(buf1, sizeof(buf1), varstr);
|
|
|
|
pstrcat(buf1, sizeof(buf1), ")");
|
|
|
|
}
|
|
|
|
pstrcat(buf1, buf_size, "(");
|
2009-05-06 02:18:10 +08:00
|
|
|
sa = s->next;
|
|
|
|
while (sa != NULL) {
|
2018-03-16 07:26:16 +08:00
|
|
|
char buf2[256];
|
|
|
|
type_to_str(buf2, sizeof(buf2), &sa->type, NULL);
|
|
|
|
pstrcat(buf1, sizeof(buf1), buf2);
|
2009-05-06 02:18:10 +08:00
|
|
|
sa = sa->next;
|
|
|
|
if (sa)
|
2018-03-16 07:26:16 +08:00
|
|
|
pstrcat(buf1, sizeof(buf1), ", ");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2018-03-16 07:26:16 +08:00
|
|
|
if (s->f.func_type == FUNC_ELLIPSIS)
|
|
|
|
pstrcat(buf1, sizeof(buf1), ", ...");
|
|
|
|
pstrcat(buf1, sizeof(buf1), ")");
|
|
|
|
type_to_str(buf, buf_size, &s->type, buf1);
|
2009-05-06 02:18:10 +08:00
|
|
|
goto no_var;
|
|
|
|
case VT_PTR:
|
|
|
|
s = type->ref;
|
2015-11-20 07:35:36 +08:00
|
|
|
if (t & VT_ARRAY) {
|
2018-11-13 00:11:24 +08:00
|
|
|
if (varstr && '*' == *varstr)
|
|
|
|
snprintf(buf1, sizeof(buf1), "(%s)[%d]", varstr, s->c);
|
|
|
|
else
|
|
|
|
snprintf(buf1, sizeof(buf1), "%s[%d]", varstr ? varstr : "", s->c);
|
2015-11-20 07:35:36 +08:00
|
|
|
type_to_str(buf, buf_size, &s->type, buf1);
|
|
|
|
goto no_var;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
pstrcpy(buf1, sizeof(buf1), "*");
|
2015-11-20 07:35:36 +08:00
|
|
|
if (t & VT_CONSTANT)
|
|
|
|
pstrcat(buf1, buf_size, "const ");
|
|
|
|
if (t & VT_VOLATILE)
|
|
|
|
pstrcat(buf1, buf_size, "volatile ");
|
2009-05-06 02:18:10 +08:00
|
|
|
if (varstr)
|
|
|
|
pstrcat(buf1, sizeof(buf1), varstr);
|
|
|
|
type_to_str(buf, buf_size, &s->type, buf1);
|
|
|
|
goto no_var;
|
|
|
|
}
|
|
|
|
if (varstr) {
|
|
|
|
pstrcat(buf, buf_size, " ");
|
|
|
|
pstrcat(buf, buf_size, varstr);
|
|
|
|
}
|
|
|
|
no_var: ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* verify type compatibility to store vtop in 'dt' type, and generate
|
|
|
|
casts if needed. */
|
|
|
|
static void gen_assign_cast(CType *dt)
|
|
|
|
{
|
2017-09-25 00:57:48 +08:00
|
|
|
CType *st, *type1, *type2;
|
2009-05-06 02:18:10 +08:00
|
|
|
char buf1[256], buf2[256];
|
2018-06-01 05:52:07 +08:00
|
|
|
int dbt, sbt, qualwarn, lvl;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
st = &vtop->type; /* source type */
|
|
|
|
dbt = dt->t & VT_BTYPE;
|
|
|
|
sbt = st->t & VT_BTYPE;
|
2015-03-03 20:39:57 +08:00
|
|
|
if (sbt == VT_VOID || dbt == VT_VOID) {
|
2015-07-30 04:53:57 +08:00
|
|
|
if (sbt == VT_VOID && dbt == VT_VOID)
|
2018-06-01 05:52:07 +08:00
|
|
|
; /* It is Ok if both are void */
|
2015-07-30 04:53:57 +08:00
|
|
|
else
|
|
|
|
tcc_error("cannot cast from/to void");
|
2015-03-03 20:39:57 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
if (dt->t & VT_CONSTANT)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("assignment of read-only location");
|
2009-05-06 02:18:10 +08:00
|
|
|
switch(dbt) {
|
|
|
|
case VT_PTR:
|
|
|
|
/* special cases for pointers */
|
|
|
|
/* '0' can also be a pointer */
|
|
|
|
if (is_null_pointer(vtop))
|
2018-06-01 05:52:07 +08:00
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* accept implicit pointer to integer cast with warning */
|
|
|
|
if (is_integer_btype(sbt)) {
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("assignment makes pointer from integer without a cast");
|
2018-06-01 05:52:07 +08:00
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
type1 = pointed_type(dt);
|
2018-06-01 05:52:07 +08:00
|
|
|
if (sbt == VT_PTR)
|
|
|
|
type2 = pointed_type(st);
|
|
|
|
else if (sbt == VT_FUNC)
|
|
|
|
type2 = st; /* a function is implicitly a function pointer */
|
|
|
|
else
|
2009-05-06 02:18:10 +08:00
|
|
|
goto error;
|
2018-06-01 05:52:07 +08:00
|
|
|
if (is_compatible_types(type1, type2))
|
|
|
|
break;
|
|
|
|
for (qualwarn = lvl = 0;; ++lvl) {
|
|
|
|
if (((type2->t & VT_CONSTANT) && !(type1->t & VT_CONSTANT)) ||
|
|
|
|
((type2->t & VT_VOLATILE) && !(type1->t & VT_VOLATILE)))
|
|
|
|
qualwarn = 1;
|
|
|
|
dbt = type1->t & (VT_BTYPE|VT_LONG);
|
|
|
|
sbt = type2->t & (VT_BTYPE|VT_LONG);
|
|
|
|
if (dbt != VT_PTR || sbt != VT_PTR)
|
|
|
|
break;
|
|
|
|
type1 = pointed_type(type1);
|
|
|
|
type2 = pointed_type(type2);
|
|
|
|
}
|
|
|
|
if (!is_compatible_unqualified_types(type1, type2)) {
|
|
|
|
if ((dbt == VT_VOID || sbt == VT_VOID) && lvl == 0) {
|
|
|
|
/* void * can match anything */
|
|
|
|
} else if (dbt == sbt
|
|
|
|
&& is_integer_btype(sbt & VT_BTYPE)
|
|
|
|
&& IS_ENUM(type1->t) + IS_ENUM(type2->t)
|
|
|
|
+ !!((type1->t ^ type2->t) & VT_UNSIGNED) < 2) {
|
2016-08-07 08:15:34 +08:00
|
|
|
/* Like GCC don't warn by default for merely changes
|
|
|
|
in pointer target signedness. Do warn for different
|
|
|
|
base types, though, in particular for unsigned enums
|
|
|
|
and signed int targets. */
|
2018-06-01 05:52:07 +08:00
|
|
|
} else {
|
|
|
|
tcc_warning("assignment from incompatible pointer type");
|
|
|
|
break;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2018-06-01 05:52:07 +08:00
|
|
|
if (qualwarn)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("assignment discards qualifiers from pointer target type");
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case VT_BYTE:
|
|
|
|
case VT_SHORT:
|
|
|
|
case VT_INT:
|
|
|
|
case VT_LLONG:
|
|
|
|
if (sbt == VT_PTR || sbt == VT_FUNC) {
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("assignment makes integer from pointer without a cast");
|
2016-05-26 00:52:08 +08:00
|
|
|
} else if (sbt == VT_STRUCT) {
|
|
|
|
goto case_VT_STRUCT;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
/* XXX: more tests */
|
|
|
|
break;
|
|
|
|
case VT_STRUCT:
|
2016-05-26 00:52:08 +08:00
|
|
|
case_VT_STRUCT:
|
2017-09-25 00:57:48 +08:00
|
|
|
if (!is_compatible_unqualified_types(dt, st)) {
|
2009-05-06 02:18:10 +08:00
|
|
|
error:
|
|
|
|
type_to_str(buf1, sizeof(buf1), st, NULL);
|
|
|
|
type_to_str(buf2, sizeof(buf2), dt, NULL);
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gen_cast(dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store vtop in lvalue pushed on stack */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void vstore(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
|
|
|
|
|
|
|
|
ft = vtop[-1].type.t;
|
|
|
|
sbt = vtop->type.t & VT_BTYPE;
|
|
|
|
dbt = ft & VT_BTYPE;
|
2012-04-15 07:06:46 +08:00
|
|
|
if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
|
|
|
|
(sbt == VT_INT && dbt == VT_SHORT))
|
2015-07-30 04:53:57 +08:00
|
|
|
&& !(vtop->type.t & VT_BITFIELD)) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* optimize char/short casts */
|
|
|
|
delayed_cast = VT_MUSTCAST;
|
2017-07-09 18:34:11 +08:00
|
|
|
vtop->type.t = ft & VT_TYPE;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* XXX: factorize */
|
|
|
|
if (ft & VT_CONSTANT)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("assignment of read-only location");
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
delayed_cast = 0;
|
|
|
|
if (!(ft & VT_BITFIELD))
|
|
|
|
gen_assign_cast(&vtop[-1].type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sbt == VT_STRUCT) {
|
|
|
|
/* if structure, only generate pointer */
|
|
|
|
/* structure assignment : generate memcpy */
|
|
|
|
/* XXX: optimize if small size */
|
|
|
|
size = type_size(&vtop->type, &align);
|
|
|
|
|
2009-12-01 02:33:44 +08:00
|
|
|
/* destination */
|
|
|
|
vswap();
|
|
|
|
vtop->type.t = VT_PTR;
|
|
|
|
gaddrof();
|
|
|
|
|
|
|
|
/* address of memcpy() */
|
2009-05-06 02:18:10 +08:00
|
|
|
#ifdef TCC_ARM_EABI
|
|
|
|
if(!(align & 7))
|
|
|
|
vpush_global_sym(&func_old_type, TOK_memcpy8);
|
|
|
|
else if(!(align & 3))
|
|
|
|
vpush_global_sym(&func_old_type, TOK_memcpy4);
|
|
|
|
else
|
|
|
|
#endif
|
2015-11-05 04:23:17 +08:00
|
|
|
/* Use memmove, rather than memcpy, as dest and src may be same: */
|
|
|
|
vpush_global_sym(&func_old_type, TOK_memmove);
|
2009-05-06 02:18:10 +08:00
|
|
|
|
2009-12-01 02:33:44 +08:00
|
|
|
vswap();
|
2009-05-06 02:18:10 +08:00
|
|
|
/* source */
|
|
|
|
vpushv(vtop - 2);
|
|
|
|
vtop->type.t = VT_PTR;
|
|
|
|
gaddrof();
|
|
|
|
/* type size */
|
|
|
|
vpushi(size);
|
|
|
|
gfunc_call(3);
|
2016-12-19 00:23:33 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* leave source on stack */
|
|
|
|
} else if (ft & VT_BITFIELD) {
|
|
|
|
/* bitfield store handling */
|
2014-09-23 18:30:08 +08:00
|
|
|
|
|
|
|
/* save lvalue as expression result (example: s.b = s.a = n;) */
|
|
|
|
vdup(), vtop[-1] = vtop[-2];
|
|
|
|
|
2017-07-09 18:38:59 +08:00
|
|
|
bit_pos = BIT_POS(ft);
|
|
|
|
bit_size = BIT_SIZE(ft);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* remove bit field info to avoid loops */
|
2017-07-09 18:38:59 +08:00
|
|
|
vtop[-1].type.t = ft & ~VT_STRUCT_MASK;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
2017-07-09 18:38:59 +08:00
|
|
|
if ((ft & VT_BTYPE) == VT_BOOL) {
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_cast(&vtop[-1].type);
|
|
|
|
vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
|
|
|
|
}
|
|
|
|
|
2017-07-09 18:38:59 +08:00
|
|
|
r = adjust_bf(vtop - 1, bit_pos, bit_size);
|
|
|
|
if (r == VT_STRUCT) {
|
|
|
|
gen_cast_s((ft & VT_BTYPE) == VT_LLONG ? VT_LLONG : VT_INT);
|
|
|
|
store_packed_bf(bit_pos, bit_size);
|
|
|
|
} else {
|
|
|
|
unsigned long long mask = (1ULL << bit_size) - 1;
|
|
|
|
if ((ft & VT_BTYPE) != VT_BOOL) {
|
|
|
|
/* mask source */
|
|
|
|
if ((vtop[-1].type.t & VT_BTYPE) == VT_LLONG)
|
|
|
|
vpushll(mask);
|
|
|
|
else
|
|
|
|
vpushi((unsigned)mask);
|
|
|
|
gen_op('&');
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-07-09 18:38:59 +08:00
|
|
|
/* shift source */
|
|
|
|
vpushi(bit_pos);
|
|
|
|
gen_op(TOK_SHL);
|
|
|
|
vswap();
|
|
|
|
/* duplicate destination */
|
|
|
|
vdup();
|
|
|
|
vrott(3);
|
|
|
|
/* load destination, mask and or with source */
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LLONG)
|
|
|
|
vpushll(~(mask << bit_pos));
|
|
|
|
else
|
|
|
|
vpushi(~((unsigned)mask << bit_pos));
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op('&');
|
2017-07-09 18:38:59 +08:00
|
|
|
gen_op('|');
|
|
|
|
/* store result */
|
|
|
|
vstore();
|
|
|
|
/* ... and discard */
|
|
|
|
vpop();
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
} else if (dbt == VT_VOID) {
|
|
|
|
--vtop;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2015-03-10 23:23:00 +08:00
|
|
|
/* bound check case */
|
|
|
|
if (vtop[-1].r & VT_MUSTBOUND) {
|
|
|
|
vswap();
|
|
|
|
gbound();
|
|
|
|
vswap();
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
#endif
|
|
|
|
rc = RC_INT;
|
|
|
|
if (is_float(ft)) {
|
|
|
|
rc = RC_FLOAT;
|
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
if ((ft & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
rc = RC_ST0;
|
2013-04-19 22:33:16 +08:00
|
|
|
} else if ((ft & VT_BTYPE) == VT_QFLOAT) {
|
|
|
|
rc = RC_FRET;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
r = gv(rc); /* generate value */
|
|
|
|
/* if lvalue was saved on stack, must read it */
|
|
|
|
if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
|
|
|
|
SValue sv;
|
|
|
|
t = get_reg(RC_INT);
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2009-05-06 02:18:10 +08:00
|
|
|
sv.type.t = VT_PTR;
|
|
|
|
#else
|
|
|
|
sv.type.t = VT_INT;
|
|
|
|
#endif
|
|
|
|
sv.r = VT_LOCAL | VT_LVAL;
|
2015-11-18 03:09:35 +08:00
|
|
|
sv.c.i = vtop[-1].c.i;
|
2009-05-06 02:18:10 +08:00
|
|
|
load(t, &sv);
|
|
|
|
vtop[-1].r = t | VT_LVAL;
|
|
|
|
}
|
2013-04-19 07:40:48 +08:00
|
|
|
/* two word case handling : store second register at word + 4 (or +8 for x86-64) */
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2013-04-19 22:33:16 +08:00
|
|
|
if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
|
|
|
|
int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
|
2013-04-19 07:40:48 +08:00
|
|
|
#else
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((ft & VT_BTYPE) == VT_LLONG) {
|
2013-04-19 22:33:16 +08:00
|
|
|
int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
|
2013-04-19 07:40:48 +08:00
|
|
|
#endif
|
2013-04-19 22:33:16 +08:00
|
|
|
vtop[-1].type.t = load_type;
|
|
|
|
store(r, vtop - 1);
|
2009-05-06 02:18:10 +08:00
|
|
|
vswap();
|
|
|
|
/* convert to int to increment easily */
|
2013-04-19 22:33:16 +08:00
|
|
|
vtop->type.t = addr_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
gaddrof();
|
2013-04-19 22:33:16 +08:00
|
|
|
vpushi(load_size);
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op('+');
|
|
|
|
vtop->r |= VT_LVAL;
|
|
|
|
vswap();
|
2013-04-19 22:33:16 +08:00
|
|
|
vtop[-1].type.t = load_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* XXX: it works because r2 is spilled last ! */
|
|
|
|
store(vtop->r2, vtop - 1);
|
2013-04-19 22:33:16 +08:00
|
|
|
} else {
|
|
|
|
store(r, vtop - 1);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-12-19 00:23:33 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
vswap();
|
|
|
|
vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
|
|
|
|
vtop->r |= delayed_cast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* post defines POST/PRE add. c is the token ++ or -- */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void inc(int post, int c)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
test_lvalue();
|
|
|
|
vdup(); /* save lvalue */
|
|
|
|
if (post) {
|
2016-12-19 00:23:33 +08:00
|
|
|
gv_dup(); /* duplicate value */
|
2009-05-06 02:18:10 +08:00
|
|
|
vrotb(3);
|
|
|
|
vrotb(3);
|
|
|
|
}
|
|
|
|
/* add constant */
|
2015-07-30 04:53:57 +08:00
|
|
|
vpushi(c - TOK_MID);
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op('+');
|
|
|
|
vstore(); /* store value */
|
|
|
|
if (post)
|
|
|
|
vpop(); /* if post op, return saved value */
|
|
|
|
}
|
|
|
|
|
2016-07-11 22:42:18 +08:00
|
|
|
ST_FUNC void parse_mult_str (CString *astr, const char *msg)
|
2016-06-28 02:10:53 +08:00
|
|
|
{
|
|
|
|
/* read the string */
|
|
|
|
if (tok != TOK_STR)
|
|
|
|
expect(msg);
|
|
|
|
cstr_new(astr);
|
|
|
|
while (tok == TOK_STR) {
|
|
|
|
/* XXX: add \0 handling too ? */
|
|
|
|
cstr_cat(astr, tokc.str.data, -1);
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
cstr_ccat(astr, '\0');
|
|
|
|
}
|
|
|
|
|
2016-10-09 08:41:34 +08:00
|
|
|
/* If I is >= 1 and a power of two, returns log2(i)+1.
|
|
|
|
If I is 0 returns 0. */
|
|
|
|
static int exact_log2p1(int i)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
if (!i)
|
|
|
|
return 0;
|
|
|
|
for (ret = 1; i >= 1 << 8; ret += 8)
|
|
|
|
i >>= 8;
|
|
|
|
if (i >= 1 << 4)
|
|
|
|
ret += 4, i >>= 4;
|
|
|
|
if (i >= 1 << 2)
|
|
|
|
ret += 2, i >>= 2;
|
|
|
|
if (i >= 1 << 1)
|
|
|
|
ret++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
/* Parse __attribute__((...)) GNUC extension. */
|
2009-05-06 02:18:10 +08:00
|
|
|
static void parse_attribute(AttributeDef *ad)
|
|
|
|
{
|
|
|
|
int t, n;
|
2016-06-28 02:10:53 +08:00
|
|
|
CString astr;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
redo:
|
|
|
|
if (tok != TOK_ATTRIBUTE1 && tok != TOK_ATTRIBUTE2)
|
|
|
|
return;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
skip('(');
|
|
|
|
while (tok != ')') {
|
|
|
|
if (tok < TOK_IDENT)
|
|
|
|
expect("attribute name");
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
switch(t) {
|
2018-12-20 17:55:22 +08:00
|
|
|
case TOK_CLEANUP1:
|
|
|
|
case TOK_CLEANUP2:
|
|
|
|
{
|
|
|
|
Sym *s;
|
|
|
|
|
|
|
|
skip('(');
|
|
|
|
s = sym_find(tok);
|
|
|
|
if (!s) {
|
|
|
|
tcc_warning("implicit declaration of function '%s'",
|
|
|
|
get_tok_str(tok, &tokc));
|
2019-04-18 09:36:39 +08:00
|
|
|
s = external_global_sym(tok, &func_old_type);
|
2018-12-20 17:55:22 +08:00
|
|
|
}
|
|
|
|
ad->cleanup_func = s;
|
|
|
|
next();
|
|
|
|
skip(')');
|
|
|
|
break;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_SECTION1:
|
|
|
|
case TOK_SECTION2:
|
|
|
|
skip('(');
|
2016-06-28 02:10:53 +08:00
|
|
|
parse_mult_str(&astr, "section name");
|
|
|
|
ad->section = find_section(tcc_state, (char *)astr.data);
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(')');
|
2016-06-28 02:10:53 +08:00
|
|
|
cstr_free(&astr);
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
2011-03-03 17:58:45 +08:00
|
|
|
case TOK_ALIAS1:
|
|
|
|
case TOK_ALIAS2:
|
|
|
|
skip('(');
|
2016-06-28 02:10:53 +08:00
|
|
|
parse_mult_str(&astr, "alias(\"target\")");
|
2011-03-03 17:58:45 +08:00
|
|
|
ad->alias_target = /* save string as token, for later */
|
2016-06-28 02:10:53 +08:00
|
|
|
tok_alloc((char*)astr.data, astr.size-1)->tok;
|
2011-03-03 17:58:45 +08:00
|
|
|
skip(')');
|
2016-06-28 02:10:53 +08:00
|
|
|
cstr_free(&astr);
|
2011-03-03 17:58:45 +08:00
|
|
|
break;
|
2015-07-30 04:53:57 +08:00
|
|
|
case TOK_VISIBILITY1:
|
|
|
|
case TOK_VISIBILITY2:
|
2014-04-14 08:53:11 +08:00
|
|
|
skip('(');
|
2016-06-28 02:10:53 +08:00
|
|
|
parse_mult_str(&astr,
|
|
|
|
"visibility(\"default|hidden|internal|protected\")");
|
|
|
|
if (!strcmp (astr.data, "default"))
|
2015-07-30 04:53:57 +08:00
|
|
|
ad->a.visibility = STV_DEFAULT;
|
2016-06-28 02:10:53 +08:00
|
|
|
else if (!strcmp (astr.data, "hidden"))
|
2015-07-30 04:53:57 +08:00
|
|
|
ad->a.visibility = STV_HIDDEN;
|
2016-06-28 02:10:53 +08:00
|
|
|
else if (!strcmp (astr.data, "internal"))
|
2015-07-30 04:53:57 +08:00
|
|
|
ad->a.visibility = STV_INTERNAL;
|
2016-06-28 02:10:53 +08:00
|
|
|
else if (!strcmp (astr.data, "protected"))
|
2015-07-30 04:53:57 +08:00
|
|
|
ad->a.visibility = STV_PROTECTED;
|
|
|
|
else
|
2014-04-14 08:53:11 +08:00
|
|
|
expect("visibility(\"default|hidden|internal|protected\")");
|
|
|
|
skip(')');
|
2016-06-28 02:10:53 +08:00
|
|
|
cstr_free(&astr);
|
2014-04-14 08:53:11 +08:00
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_ALIGNED1:
|
|
|
|
case TOK_ALIGNED2:
|
|
|
|
if (tok == '(') {
|
|
|
|
next();
|
|
|
|
n = expr_const();
|
2015-07-30 04:53:57 +08:00
|
|
|
if (n <= 0 || (n & (n - 1)) != 0)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("alignment must be a positive power of two");
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(')');
|
|
|
|
} else {
|
|
|
|
n = MAX_ALIGN;
|
|
|
|
}
|
2016-10-09 08:41:34 +08:00
|
|
|
ad->a.aligned = exact_log2p1(n);
|
|
|
|
if (n != 1 << (ad->a.aligned - 1))
|
|
|
|
tcc_error("alignment of %d is larger than implemented", n);
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case TOK_PACKED1:
|
|
|
|
case TOK_PACKED2:
|
2014-01-07 21:57:07 +08:00
|
|
|
ad->a.packed = 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
2010-02-28 00:37:59 +08:00
|
|
|
case TOK_WEAK1:
|
|
|
|
case TOK_WEAK2:
|
2014-01-07 21:57:07 +08:00
|
|
|
ad->a.weak = 1;
|
2010-02-28 00:37:59 +08:00
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_UNUSED1:
|
|
|
|
case TOK_UNUSED2:
|
|
|
|
/* currently, no need to handle it because tcc does not
|
|
|
|
track unused objects */
|
|
|
|
break;
|
|
|
|
case TOK_NORETURN1:
|
|
|
|
case TOK_NORETURN2:
|
|
|
|
/* currently, no need to handle it because tcc does not
|
|
|
|
track unused objects */
|
|
|
|
break;
|
|
|
|
case TOK_CDECL1:
|
|
|
|
case TOK_CDECL2:
|
|
|
|
case TOK_CDECL3:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->f.func_call = FUNC_CDECL;
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case TOK_STDCALL1:
|
|
|
|
case TOK_STDCALL2:
|
|
|
|
case TOK_STDCALL3:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->f.func_call = FUNC_STDCALL;
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
case TOK_REGPARM1:
|
|
|
|
case TOK_REGPARM2:
|
|
|
|
skip('(');
|
|
|
|
n = expr_const();
|
2015-07-30 04:53:57 +08:00
|
|
|
if (n > 3)
|
2009-05-06 02:18:10 +08:00
|
|
|
n = 3;
|
|
|
|
else if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
if (n > 0)
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->f.func_call = FUNC_FASTCALL1 + n - 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(')');
|
|
|
|
break;
|
|
|
|
case TOK_FASTCALL1:
|
|
|
|
case TOK_FASTCALL2:
|
|
|
|
case TOK_FASTCALL3:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->f.func_call = FUNC_FASTCALLW;
|
2015-07-30 04:53:57 +08:00
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
#endif
|
2010-01-27 05:56:22 +08:00
|
|
|
case TOK_MODE:
|
|
|
|
skip('(');
|
|
|
|
switch(tok) {
|
|
|
|
case TOK_MODE_DI:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->attr_mode = VT_LLONG + 1;
|
2010-01-27 05:56:22 +08:00
|
|
|
break;
|
2016-10-15 22:01:16 +08:00
|
|
|
case TOK_MODE_QI:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->attr_mode = VT_BYTE + 1;
|
2016-10-15 22:01:16 +08:00
|
|
|
break;
|
2010-01-27 05:56:22 +08:00
|
|
|
case TOK_MODE_HI:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->attr_mode = VT_SHORT + 1;
|
2010-01-27 05:56:22 +08:00
|
|
|
break;
|
|
|
|
case TOK_MODE_SI:
|
2016-10-15 22:01:16 +08:00
|
|
|
case TOK_MODE_word:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->attr_mode = VT_INT + 1;
|
2010-01-27 05:56:22 +08:00
|
|
|
break;
|
|
|
|
default:
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
|
2010-01-27 05:56:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
skip(')');
|
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_DLLEXPORT:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->a.dllexport = 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
2018-07-22 07:54:01 +08:00
|
|
|
case TOK_NODECORATE:
|
|
|
|
ad->a.nodecorate = 1;
|
|
|
|
break;
|
2009-11-14 00:14:05 +08:00
|
|
|
case TOK_DLLIMPORT:
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->a.dllimport = 1;
|
2009-11-14 00:14:05 +08:00
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
default:
|
|
|
|
if (tcc_state->warn_unsupported)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
|
2009-05-06 02:18:10 +08:00
|
|
|
/* skip parameters */
|
|
|
|
if (tok == '(') {
|
|
|
|
int parenthesis = 0;
|
|
|
|
do {
|
2015-07-30 04:53:57 +08:00
|
|
|
if (tok == '(')
|
2009-05-06 02:18:10 +08:00
|
|
|
parenthesis++;
|
2015-07-30 04:53:57 +08:00
|
|
|
else if (tok == ')')
|
2009-05-06 02:18:10 +08:00
|
|
|
parenthesis--;
|
|
|
|
next();
|
|
|
|
} while (parenthesis && tok != -1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
skip(')');
|
|
|
|
skip(')');
|
2017-07-09 18:34:11 +08:00
|
|
|
goto redo;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2019-04-11 06:30:41 +08:00
|
|
|
static Sym * find_field (CType *type, int v, int *cumofs)
|
2016-08-02 04:11:49 +08:00
|
|
|
{
|
|
|
|
Sym *s = type->ref;
|
|
|
|
v |= SYM_FIELD;
|
|
|
|
while ((s = s->next) != NULL) {
|
2016-10-09 06:52:57 +08:00
|
|
|
if ((s->v & SYM_FIELD) &&
|
|
|
|
(s->type.t & VT_BTYPE) == VT_STRUCT &&
|
|
|
|
(s->v & ~SYM_FIELD) >= SYM_FIRST_ANOM) {
|
2019-04-11 06:30:41 +08:00
|
|
|
Sym *ret = find_field (&s->type, v, cumofs);
|
|
|
|
if (ret) {
|
|
|
|
*cumofs += s->c;
|
2016-08-02 04:11:49 +08:00
|
|
|
return ret;
|
2019-04-11 06:30:41 +08:00
|
|
|
}
|
2016-08-02 04:11:49 +08:00
|
|
|
}
|
|
|
|
if (s->v == v)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-10-03 03:37:58 +08:00
|
|
|
static void struct_layout(CType *type, AttributeDef *ad)
|
|
|
|
{
|
2017-07-09 18:38:59 +08:00
|
|
|
int size, align, maxalign, offset, c, bit_pos, bit_size;
|
|
|
|
int packed, a, bt, prevbt, prev_bit_size;
|
2016-11-12 00:33:31 +08:00
|
|
|
int pcc = !tcc_state->ms_bitfields;
|
2017-07-09 18:38:59 +08:00
|
|
|
int pragma_pack = *tcc_state->pack_stack_ptr;
|
2016-10-03 03:37:58 +08:00
|
|
|
Sym *f;
|
2017-07-09 18:38:59 +08:00
|
|
|
|
|
|
|
maxalign = 1;
|
2016-10-03 03:37:58 +08:00
|
|
|
offset = 0;
|
|
|
|
c = 0;
|
2016-10-09 06:52:57 +08:00
|
|
|
bit_pos = 0;
|
2016-11-08 02:36:21 +08:00
|
|
|
prevbt = VT_STRUCT; /* make it never match */
|
2016-11-29 03:27:42 +08:00
|
|
|
prev_bit_size = 0;
|
2017-07-09 18:38:59 +08:00
|
|
|
|
|
|
|
//#define BF_DEBUG
|
2017-05-10 00:10:02 +08:00
|
|
|
|
2016-10-03 03:37:58 +08:00
|
|
|
for (f = type->ref->next; f; f = f->next) {
|
2017-07-16 18:10:00 +08:00
|
|
|
if (f->type.t & VT_BITFIELD)
|
2017-07-09 18:38:59 +08:00
|
|
|
bit_size = BIT_SIZE(f->type.t);
|
2017-07-16 18:10:00 +08:00
|
|
|
else
|
2017-07-09 18:38:59 +08:00
|
|
|
bit_size = -1;
|
|
|
|
size = type_size(&f->type, &align);
|
|
|
|
a = f->a.aligned ? 1 << (f->a.aligned - 1) : 0;
|
|
|
|
packed = 0;
|
|
|
|
|
|
|
|
if (pcc && bit_size == 0) {
|
|
|
|
/* in pcc mode, packing does not affect zero-width bitfields */
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* in pcc mode, attribute packed overrides if set. */
|
|
|
|
if (pcc && (f->a.packed || ad->a.packed))
|
|
|
|
align = packed = 1;
|
|
|
|
|
|
|
|
/* pragma pack overrides align if lesser and packs bitfields always */
|
|
|
|
if (pragma_pack) {
|
|
|
|
packed = 1;
|
|
|
|
if (pragma_pack < align)
|
|
|
|
align = pragma_pack;
|
|
|
|
/* in pcc mode pragma pack also overrides individual align */
|
|
|
|
if (pcc && pragma_pack < a)
|
|
|
|
a = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* some individual align was specified */
|
|
|
|
if (a)
|
|
|
|
align = a;
|
|
|
|
|
|
|
|
if (type->ref->type.t == VT_UNION) {
|
2016-11-29 03:54:59 +08:00
|
|
|
if (pcc && bit_size >= 0)
|
2017-07-09 18:38:59 +08:00
|
|
|
size = (bit_size + 7) >> 3;
|
2016-11-29 03:54:59 +08:00
|
|
|
offset = 0;
|
|
|
|
if (size > c)
|
2017-07-09 18:38:59 +08:00
|
|
|
c = size;
|
|
|
|
|
2016-11-29 03:54:59 +08:00
|
|
|
} else if (bit_size < 0) {
|
2017-07-09 18:38:59 +08:00
|
|
|
if (pcc)
|
|
|
|
c += (bit_pos + 7) >> 3;
|
|
|
|
c = (c + align - 1) & -align;
|
2016-11-29 03:54:59 +08:00
|
|
|
offset = c;
|
|
|
|
if (size > 0)
|
2017-07-09 18:38:59 +08:00
|
|
|
c += size;
|
2016-10-09 06:52:57 +08:00
|
|
|
bit_pos = 0;
|
2017-07-09 18:38:59 +08:00
|
|
|
prevbt = VT_STRUCT;
|
|
|
|
prev_bit_size = 0;
|
|
|
|
|
2016-10-09 06:52:57 +08:00
|
|
|
} else {
|
|
|
|
/* A bit-field. Layout is more complicated. There are two
|
2017-07-09 18:38:59 +08:00
|
|
|
options: PCC (GCC) compatible and MS compatible */
|
|
|
|
if (pcc) {
|
|
|
|
/* In PCC layout a bit-field is placed adjacent to the
|
|
|
|
preceding bit-fields, except if:
|
|
|
|
- it has zero-width
|
|
|
|
- an individual alignment was given
|
|
|
|
- it would overflow its base type container and
|
|
|
|
there is no packing */
|
|
|
|
if (bit_size == 0) {
|
|
|
|
new_field:
|
|
|
|
c = (c + ((bit_pos + 7) >> 3) + align - 1) & -align;
|
2016-10-09 06:52:57 +08:00
|
|
|
bit_pos = 0;
|
2017-07-09 18:38:59 +08:00
|
|
|
} else if (f->a.aligned) {
|
|
|
|
goto new_field;
|
|
|
|
} else if (!packed) {
|
|
|
|
int a8 = align * 8;
|
|
|
|
int ofs = ((c * 8 + bit_pos) % a8 + bit_size + a8 - 1) / a8;
|
|
|
|
if (ofs > size / align)
|
|
|
|
goto new_field;
|
|
|
|
}
|
|
|
|
|
2017-07-16 18:10:00 +08:00
|
|
|
/* in pcc mode, long long bitfields have type int if they fit */
|
|
|
|
if (size == 8 && bit_size <= 32)
|
|
|
|
f->type.t = (f->type.t & ~VT_BTYPE) | VT_INT, size = 4;
|
|
|
|
|
2017-07-15 01:26:01 +08:00
|
|
|
while (bit_pos >= align * 8)
|
|
|
|
c += align, bit_pos -= align * 8;
|
2017-07-09 18:38:59 +08:00
|
|
|
offset = c;
|
2017-07-15 01:26:01 +08:00
|
|
|
|
2016-10-09 06:52:57 +08:00
|
|
|
/* In PCC layout named bit-fields influence the alignment
|
|
|
|
of the containing struct using the base types alignment,
|
2017-07-09 18:38:59 +08:00
|
|
|
except for packed fields (which here have correct align). */
|
2017-07-24 03:24:11 +08:00
|
|
|
if (f->v & SYM_FIRST_ANOM
|
|
|
|
// && bit_size // ??? gcc on ARM/rpi does that
|
|
|
|
)
|
2017-07-09 18:38:59 +08:00
|
|
|
align = 1;
|
2017-07-24 03:24:11 +08:00
|
|
|
|
2016-10-09 06:52:57 +08:00
|
|
|
} else {
|
2016-11-08 02:36:21 +08:00
|
|
|
bt = f->type.t & VT_BTYPE;
|
2017-07-09 18:38:59 +08:00
|
|
|
if ((bit_pos + bit_size > size * 8)
|
|
|
|
|| (bit_size > 0) == (bt != prevbt)
|
|
|
|
) {
|
|
|
|
c = (c + align - 1) & -align;
|
2016-11-08 02:36:21 +08:00
|
|
|
offset = c;
|
|
|
|
bit_pos = 0;
|
|
|
|
/* In MS bitfield mode a bit-field run always uses
|
2016-11-29 03:54:59 +08:00
|
|
|
at least as many bits as the underlying type.
|
|
|
|
To start a new run it's also required that this
|
|
|
|
or the last bit-field had non-zero width. */
|
2016-11-29 03:27:42 +08:00
|
|
|
if (bit_size || prev_bit_size)
|
2017-07-09 18:38:59 +08:00
|
|
|
c += size;
|
2016-11-08 02:36:21 +08:00
|
|
|
}
|
2016-11-29 03:54:59 +08:00
|
|
|
/* In MS layout the records alignment is normally
|
|
|
|
influenced by the field, except for a zero-width
|
|
|
|
field at the start of a run (but by further zero-width
|
|
|
|
fields it is again). */
|
|
|
|
if (bit_size == 0 && prevbt != bt)
|
2017-07-09 18:38:59 +08:00
|
|
|
align = 1;
|
2016-11-08 02:36:21 +08:00
|
|
|
prevbt = bt;
|
2017-07-09 18:38:59 +08:00
|
|
|
prev_bit_size = bit_size;
|
2016-10-09 06:52:57 +08:00
|
|
|
}
|
2017-07-09 18:38:59 +08:00
|
|
|
|
2016-10-09 06:52:57 +08:00
|
|
|
f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
|
|
|
|
| (bit_pos << VT_STRUCT_SHIFT);
|
|
|
|
bit_pos += bit_size;
|
2016-10-03 03:37:58 +08:00
|
|
|
}
|
2016-11-29 03:54:59 +08:00
|
|
|
if (align > maxalign)
|
2017-07-09 18:38:59 +08:00
|
|
|
maxalign = align;
|
|
|
|
|
|
|
|
#ifdef BF_DEBUG
|
|
|
|
printf("set field %s offset %-2d size %-2d align %-2d",
|
|
|
|
get_tok_str(f->v & ~SYM_FIELD, NULL), offset, size, align);
|
2016-10-03 03:37:58 +08:00
|
|
|
if (f->type.t & VT_BITFIELD) {
|
2017-07-09 18:38:59 +08:00
|
|
|
printf(" pos %-2d bits %-2d",
|
|
|
|
BIT_POS(f->type.t),
|
|
|
|
BIT_SIZE(f->type.t)
|
|
|
|
);
|
2016-10-03 03:37:58 +08:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
2019-04-11 06:30:41 +08:00
|
|
|
f->c = offset;
|
2016-10-03 03:37:58 +08:00
|
|
|
f->r = 0;
|
|
|
|
}
|
2017-07-09 18:38:59 +08:00
|
|
|
|
|
|
|
if (pcc)
|
|
|
|
c += (bit_pos + 7) >> 3;
|
|
|
|
|
2017-07-15 01:26:01 +08:00
|
|
|
/* store size and alignment */
|
|
|
|
a = bt = ad->a.aligned ? 1 << (ad->a.aligned - 1) : 1;
|
2017-07-09 18:38:59 +08:00
|
|
|
if (a < maxalign)
|
|
|
|
a = maxalign;
|
2017-07-15 01:26:01 +08:00
|
|
|
type->ref->r = a;
|
2017-07-24 03:24:11 +08:00
|
|
|
if (pragma_pack && pragma_pack < maxalign && 0 == pcc) {
|
2017-07-15 01:26:01 +08:00
|
|
|
/* can happen if individual align for some member was given. In
|
|
|
|
this case MSVC ignores maxalign when aligning the size */
|
|
|
|
a = pragma_pack;
|
|
|
|
if (a < bt)
|
|
|
|
a = bt;
|
|
|
|
}
|
2017-07-09 18:38:59 +08:00
|
|
|
c = (c + a - 1) & -a;
|
|
|
|
type->ref->c = c;
|
|
|
|
|
|
|
|
#ifdef BF_DEBUG
|
|
|
|
printf("struct size %-2d align %-2d\n\n", c, a), fflush(stdout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check whether we can access bitfields by their type */
|
|
|
|
for (f = type->ref->next; f; f = f->next) {
|
|
|
|
int s, px, cx, c0;
|
|
|
|
CType t;
|
|
|
|
|
|
|
|
if (0 == (f->type.t & VT_BITFIELD))
|
|
|
|
continue;
|
|
|
|
f->type.ref = f;
|
|
|
|
f->auxtype = -1;
|
|
|
|
bit_size = BIT_SIZE(f->type.t);
|
|
|
|
if (bit_size == 0)
|
|
|
|
continue;
|
|
|
|
bit_pos = BIT_POS(f->type.t);
|
|
|
|
size = type_size(&f->type, &align);
|
|
|
|
if (bit_pos + bit_size <= size * 8 && f->c + size <= c)
|
|
|
|
continue;
|
|
|
|
|
2017-09-25 09:03:26 +08:00
|
|
|
/* try to access the field using a different type */
|
2017-07-09 18:38:59 +08:00
|
|
|
c0 = -1, s = align = 1;
|
|
|
|
for (;;) {
|
|
|
|
px = f->c * 8 + bit_pos;
|
|
|
|
cx = (px >> 3) & -align;
|
|
|
|
px = px - (cx << 3);
|
|
|
|
if (c0 == cx)
|
|
|
|
break;
|
|
|
|
s = (px + bit_size + 7) >> 3;
|
|
|
|
if (s > 4) {
|
|
|
|
t.t = VT_LLONG;
|
|
|
|
} else if (s > 2) {
|
|
|
|
t.t = VT_INT;
|
|
|
|
} else if (s > 1) {
|
|
|
|
t.t = VT_SHORT;
|
|
|
|
} else {
|
|
|
|
t.t = VT_BYTE;
|
|
|
|
}
|
|
|
|
s = type_size(&t, &align);
|
|
|
|
c0 = cx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (px + bit_size <= s * 8 && cx + s <= c) {
|
|
|
|
/* update offset and bit position */
|
|
|
|
f->c = cx;
|
|
|
|
bit_pos = px;
|
|
|
|
f->type.t = (f->type.t & ~(0x3f << VT_STRUCT_SHIFT))
|
|
|
|
| (bit_pos << VT_STRUCT_SHIFT);
|
|
|
|
if (s != size)
|
|
|
|
f->auxtype = t.t;
|
2017-07-15 01:26:01 +08:00
|
|
|
#ifdef BF_DEBUG
|
|
|
|
printf("FIX field %s offset %-2d size %-2d align %-2d "
|
|
|
|
"pos %-2d bits %-2d\n",
|
|
|
|
get_tok_str(f->v & ~SYM_FIELD, NULL),
|
|
|
|
cx, s, align, px, bit_size);
|
|
|
|
#endif
|
2017-07-09 18:38:59 +08:00
|
|
|
} else {
|
|
|
|
/* fall back to load/store single-byte wise */
|
|
|
|
f->auxtype = VT_STRUCT;
|
|
|
|
#ifdef BF_DEBUG
|
2017-07-15 01:26:01 +08:00
|
|
|
printf("FIX field %s : load byte-wise\n",
|
|
|
|
get_tok_str(f->v & ~SYM_FIELD, NULL));
|
2017-07-09 18:38:59 +08:00
|
|
|
#endif
|
2017-07-15 01:26:01 +08:00
|
|
|
}
|
2017-07-09 18:38:59 +08:00
|
|
|
}
|
2016-10-03 03:37:58 +08:00
|
|
|
}
|
|
|
|
|
2017-07-09 18:38:25 +08:00
|
|
|
/* enum/struct/union declaration. u is VT_ENUM/VT_STRUCT/VT_UNION */
|
2017-07-09 18:34:11 +08:00
|
|
|
static void struct_decl(CType *type, int u)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2017-07-09 18:38:59 +08:00
|
|
|
int v, c, size, align, flexible;
|
2016-11-29 03:54:59 +08:00
|
|
|
int bit_size, bsize, bt;
|
2016-10-03 03:37:58 +08:00
|
|
|
Sym *s, *ss, **ps;
|
2017-07-09 18:34:11 +08:00
|
|
|
AttributeDef ad, ad1;
|
2009-05-06 02:18:10 +08:00
|
|
|
CType type1, btype;
|
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
memset(&ad, 0, sizeof ad);
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
2017-07-09 18:34:11 +08:00
|
|
|
parse_attribute(&ad);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok != '{') {
|
|
|
|
v = tok;
|
|
|
|
next();
|
|
|
|
/* struct already defined ? return it */
|
|
|
|
if (v < TOK_IDENT)
|
|
|
|
expect("struct/union/enum name");
|
|
|
|
s = struct_find(v);
|
2017-07-09 18:34:11 +08:00
|
|
|
if (s && (s->sym_scope == local_scope || tok != '{')) {
|
2017-07-09 18:38:25 +08:00
|
|
|
if (u == s->type.t)
|
|
|
|
goto do_decl;
|
|
|
|
if (u == VT_ENUM && IS_ENUM(s->type.t))
|
|
|
|
goto do_decl;
|
|
|
|
tcc_error("redefinition of '%s'", get_tok_str(v, NULL));
|
2016-03-24 22:44:01 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
v = anon_sym++;
|
|
|
|
}
|
2016-07-31 12:18:45 +08:00
|
|
|
/* Record the original enum/struct/union token. */
|
2017-07-11 04:20:34 +08:00
|
|
|
type1.t = u == VT_ENUM ? u | VT_INT | VT_UNSIGNED : u;
|
2013-07-24 23:06:13 +08:00
|
|
|
type1.ref = NULL;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* we put an undefined size for struct/union */
|
|
|
|
s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
|
|
|
|
s->r = 0; /* default alignment is zero as gcc */
|
2017-07-09 18:34:11 +08:00
|
|
|
do_decl:
|
2017-07-09 18:38:25 +08:00
|
|
|
type->t = s->type.t;
|
2009-05-06 02:18:10 +08:00
|
|
|
type->ref = s;
|
2017-07-09 18:34:11 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == '{') {
|
|
|
|
next();
|
|
|
|
if (s->c != -1)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("struct/union/enum already defined");
|
2019-04-07 09:15:05 +08:00
|
|
|
s->c = -2;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* cannot be empty */
|
|
|
|
/* non empty enums are not allowed */
|
2017-07-09 18:38:25 +08:00
|
|
|
ps = &s->next;
|
2017-07-09 18:34:11 +08:00
|
|
|
if (u == VT_ENUM) {
|
2017-07-09 18:38:25 +08:00
|
|
|
long long ll = 0, pl = 0, nl = 0;
|
|
|
|
CType t;
|
|
|
|
t.ref = s;
|
|
|
|
/* enum symbols have static storage */
|
|
|
|
t.t = VT_INT|VT_STATIC|VT_ENUM_VAL;
|
2009-05-06 02:18:10 +08:00
|
|
|
for(;;) {
|
|
|
|
v = tok;
|
|
|
|
if (v < TOK_UIDENT)
|
|
|
|
expect("identifier");
|
2013-09-21 04:49:49 +08:00
|
|
|
ss = sym_find(v);
|
2014-03-31 22:58:17 +08:00
|
|
|
if (ss && !local_stack)
|
2013-09-21 04:49:49 +08:00
|
|
|
tcc_error("redefinition of enumerator '%s'",
|
|
|
|
get_tok_str(v, NULL));
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
if (tok == '=') {
|
|
|
|
next();
|
2017-07-09 18:38:25 +08:00
|
|
|
ll = expr_const64();
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-07-09 18:38:25 +08:00
|
|
|
ss = sym_push(v, &t, VT_CONST, 0);
|
|
|
|
ss->enum_val = ll;
|
|
|
|
*ps = ss, ps = &ss->next;
|
|
|
|
if (ll < nl)
|
|
|
|
nl = ll;
|
|
|
|
if (ll > pl)
|
|
|
|
pl = ll;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
next();
|
2017-07-09 18:38:25 +08:00
|
|
|
ll++;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* NOTE: we accept a trailing comma */
|
|
|
|
if (tok == '}')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
skip('}');
|
2017-07-09 18:38:25 +08:00
|
|
|
/* set integral type of the enum */
|
|
|
|
t.t = VT_INT;
|
2017-07-11 04:20:34 +08:00
|
|
|
if (nl >= 0) {
|
2017-07-09 18:38:25 +08:00
|
|
|
if (pl != (unsigned)pl)
|
2017-09-25 00:57:48 +08:00
|
|
|
t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
|
2017-07-09 18:38:25 +08:00
|
|
|
t.t |= VT_UNSIGNED;
|
|
|
|
} else if (pl != (int)pl || nl != (int)nl)
|
2017-09-25 00:57:48 +08:00
|
|
|
t.t = (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
|
2017-07-09 18:38:25 +08:00
|
|
|
s->type.t = type->t = t.t | VT_ENUM;
|
|
|
|
s->c = 0;
|
|
|
|
/* set type for enum members */
|
|
|
|
for (ss = s->next; ss; ss = ss->next) {
|
|
|
|
ll = ss->enum_val;
|
|
|
|
if (ll == (int)ll) /* default is int if it fits */
|
|
|
|
continue;
|
|
|
|
if (t.t & VT_UNSIGNED) {
|
|
|
|
ss->type.t |= VT_UNSIGNED;
|
|
|
|
if (ll == (unsigned)ll)
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-25 00:57:48 +08:00
|
|
|
ss->type.t = (ss->type.t & ~VT_BTYPE)
|
|
|
|
| (LONG_SIZE==8 ? VT_LLONG|VT_LONG : VT_LLONG);
|
2017-07-09 18:38:25 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2017-07-09 18:38:25 +08:00
|
|
|
c = 0;
|
2013-10-06 20:43:16 +08:00
|
|
|
flexible = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
while (tok != '}') {
|
2016-06-28 21:09:40 +08:00
|
|
|
if (!parse_btype(&btype, &ad1)) {
|
|
|
|
skip(';');
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
while (1) {
|
2015-07-30 04:53:57 +08:00
|
|
|
if (flexible)
|
|
|
|
tcc_error("flexible array member '%s' not at the end of struct",
|
2013-10-06 20:43:16 +08:00
|
|
|
get_tok_str(v, NULL));
|
2009-05-06 02:18:10 +08:00
|
|
|
bit_size = -1;
|
|
|
|
v = 0;
|
|
|
|
type1 = btype;
|
|
|
|
if (tok != ':') {
|
2017-03-06 10:25:33 +08:00
|
|
|
if (tok != ';')
|
|
|
|
type_decl(&type1, &ad1, &v, TYPE_DIRECT);
|
2015-04-10 11:31:58 +08:00
|
|
|
if (v == 0) {
|
2015-07-30 04:53:57 +08:00
|
|
|
if ((type1.t & VT_BTYPE) != VT_STRUCT)
|
|
|
|
expect("identifier");
|
|
|
|
else {
|
|
|
|
int v = btype.ref->v;
|
|
|
|
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
|
|
|
|
if (tcc_state->ms_extensions == 0)
|
|
|
|
expect("identifier");
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 11:31:58 +08:00
|
|
|
}
|
2013-10-06 20:43:16 +08:00
|
|
|
if (type_size(&type1, &align) < 0) {
|
2017-07-09 18:34:11 +08:00
|
|
|
if ((u == VT_STRUCT) && (type1.t & VT_ARRAY) && c)
|
2015-07-30 04:53:57 +08:00
|
|
|
flexible = 1;
|
|
|
|
else
|
|
|
|
tcc_error("field '%s' has incomplete type",
|
|
|
|
get_tok_str(v, NULL));
|
2013-10-06 20:43:16 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((type1.t & VT_BTYPE) == VT_FUNC ||
|
2018-08-04 04:39:00 +08:00
|
|
|
(type1.t & VT_BTYPE) == VT_VOID ||
|
2017-03-11 09:13:59 +08:00
|
|
|
(type1.t & VT_STORAGE))
|
2015-07-30 04:53:57 +08:00
|
|
|
tcc_error("invalid type for '%s'",
|
2009-05-06 02:18:10 +08:00
|
|
|
get_tok_str(v, NULL));
|
|
|
|
}
|
|
|
|
if (tok == ':') {
|
|
|
|
next();
|
|
|
|
bit_size = expr_const();
|
|
|
|
/* XXX: handle v = 0 case for messages */
|
|
|
|
if (bit_size < 0)
|
2015-07-30 04:53:57 +08:00
|
|
|
tcc_error("negative width in bit-field '%s'",
|
2009-05-06 02:18:10 +08:00
|
|
|
get_tok_str(v, NULL));
|
|
|
|
if (v && bit_size == 0)
|
2015-07-30 04:53:57 +08:00
|
|
|
tcc_error("zero width for bit-field '%s'",
|
2009-05-06 02:18:10 +08:00
|
|
|
get_tok_str(v, NULL));
|
2017-07-09 18:34:11 +08:00
|
|
|
parse_attribute(&ad1);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
size = type_size(&type1, &align);
|
|
|
|
if (bit_size >= 0) {
|
|
|
|
bt = type1.t & VT_BTYPE;
|
2015-07-30 04:53:57 +08:00
|
|
|
if (bt != VT_INT &&
|
|
|
|
bt != VT_BYTE &&
|
2009-05-06 02:18:10 +08:00
|
|
|
bt != VT_SHORT &&
|
|
|
|
bt != VT_BOOL &&
|
|
|
|
bt != VT_LLONG)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("bitfields must have scalar type");
|
2009-05-06 02:18:10 +08:00
|
|
|
bsize = size * 8;
|
|
|
|
if (bit_size > bsize) {
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("width of '%s' exceeds its type",
|
2009-05-06 02:18:10 +08:00
|
|
|
get_tok_str(v, NULL));
|
2017-07-09 18:38:59 +08:00
|
|
|
} else if (bit_size == bsize
|
|
|
|
&& !ad.a.packed && !ad1.a.packed) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* no need for bit fields */
|
2016-11-29 03:54:59 +08:00
|
|
|
;
|
2017-07-09 18:38:59 +08:00
|
|
|
} else if (bit_size == 64) {
|
|
|
|
tcc_error("field width 64 not implemented");
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2017-07-09 18:38:25 +08:00
|
|
|
type1.t = (type1.t & ~VT_STRUCT_MASK)
|
|
|
|
| VT_BITFIELD
|
|
|
|
| (bit_size << (VT_STRUCT_SHIFT + 6));
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
|
2016-10-03 03:37:58 +08:00
|
|
|
/* Remember we've seen a real field to check
|
|
|
|
for placement of flexible array member. */
|
|
|
|
c = 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-11-29 03:54:59 +08:00
|
|
|
/* If member is a struct or bit-field, enforce
|
|
|
|
placing into the struct (as anonymous). */
|
|
|
|
if (v == 0 &&
|
|
|
|
((type1.t & VT_BTYPE) == VT_STRUCT ||
|
|
|
|
bit_size >= 0)) {
|
2016-08-02 04:11:49 +08:00
|
|
|
v = anon_sym++;
|
|
|
|
}
|
|
|
|
if (v) {
|
2017-07-09 18:38:59 +08:00
|
|
|
ss = sym_push(v | SYM_FIELD, &type1, 0, 0);
|
|
|
|
ss->a = ad1.a;
|
2009-05-06 02:18:10 +08:00
|
|
|
*ps = ss;
|
|
|
|
ps = &ss->next;
|
|
|
|
}
|
|
|
|
if (tok == ';' || tok == TOK_EOF)
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
|
|
|
skip('}');
|
2017-07-09 18:34:11 +08:00
|
|
|
parse_attribute(&ad);
|
|
|
|
struct_layout(type, &ad);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-14 23:42:48 +08:00
|
|
|
static void sym_to_attr(AttributeDef *ad, Sym *s)
|
|
|
|
{
|
2019-01-01 05:00:31 +08:00
|
|
|
merge_symattr(&ad->a, &s->a);
|
|
|
|
merge_funcattr(&ad->f, &s->f);
|
2017-07-14 23:42:48 +08:00
|
|
|
}
|
|
|
|
|
2016-01-11 15:51:58 +08:00
|
|
|
/* Add type qualifiers to a type. If the type is an array then the qualifiers
|
|
|
|
are added to the element type, copied because it could be a typedef. */
|
|
|
|
static void parse_btype_qualify(CType *type, int qualifiers)
|
|
|
|
{
|
|
|
|
while (type->t & VT_ARRAY) {
|
|
|
|
type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
|
|
|
|
type = &type->ref->type;
|
|
|
|
}
|
|
|
|
type->t |= qualifiers;
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* return 0 if no type declaration. otherwise, return the basic type
|
2015-07-30 04:53:57 +08:00
|
|
|
and skip it.
|
2009-05-06 02:18:10 +08:00
|
|
|
*/
|
|
|
|
static int parse_btype(CType *type, AttributeDef *ad)
|
|
|
|
{
|
2017-07-09 18:34:11 +08:00
|
|
|
int t, u, bt, st, type_found, typespec_found, g;
|
2009-05-06 02:18:10 +08:00
|
|
|
Sym *s;
|
|
|
|
CType type1;
|
|
|
|
|
|
|
|
memset(ad, 0, sizeof(AttributeDef));
|
|
|
|
type_found = 0;
|
|
|
|
typespec_found = 0;
|
2017-07-09 18:34:11 +08:00
|
|
|
t = VT_INT;
|
|
|
|
bt = st = -1;
|
|
|
|
type->ref = NULL;
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
while(1) {
|
|
|
|
switch(tok) {
|
|
|
|
case TOK_EXTENSION:
|
|
|
|
/* currently, we really ignore extension */
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* basic types */
|
|
|
|
case TOK_CHAR:
|
|
|
|
u = VT_BYTE;
|
|
|
|
basic_type:
|
|
|
|
next();
|
|
|
|
basic_type1:
|
2017-07-26 04:04:24 +08:00
|
|
|
if (u == VT_SHORT || u == VT_LONG) {
|
2017-07-09 18:34:11 +08:00
|
|
|
if (st != -1 || (bt != -1 && bt != VT_INT))
|
|
|
|
tmbt: tcc_error("too many basic types");
|
|
|
|
st = u;
|
|
|
|
} else {
|
|
|
|
if (bt != -1 || (st != -1 && u != VT_INT))
|
|
|
|
goto tmbt;
|
|
|
|
bt = u;
|
|
|
|
}
|
|
|
|
if (u != VT_INT)
|
2017-09-25 00:57:48 +08:00
|
|
|
t = (t & ~(VT_BTYPE|VT_LONG)) | u;
|
2009-05-06 02:18:10 +08:00
|
|
|
typespec_found = 1;
|
|
|
|
break;
|
|
|
|
case TOK_VOID:
|
|
|
|
u = VT_VOID;
|
|
|
|
goto basic_type;
|
|
|
|
case TOK_SHORT:
|
|
|
|
u = VT_SHORT;
|
|
|
|
goto basic_type;
|
|
|
|
case TOK_INT:
|
2014-03-24 23:28:56 +08:00
|
|
|
u = VT_INT;
|
|
|
|
goto basic_type;
|
2019-03-21 03:03:27 +08:00
|
|
|
case TOK_ALIGNAS:
|
|
|
|
{ int n;
|
2019-04-09 02:58:49 +08:00
|
|
|
AttributeDef ad1;
|
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
memset(&ad1, 0, sizeof(AttributeDef));
|
|
|
|
if (parse_btype(&type1, &ad1)) {
|
|
|
|
type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
|
|
|
|
if (ad1.a.aligned)
|
|
|
|
n = 1 << (ad1.a.aligned - 1);
|
|
|
|
else
|
|
|
|
type_size(&type1, &n);
|
|
|
|
} else {
|
|
|
|
n = expr_const();
|
|
|
|
if (n <= 0 || (n & (n - 1)) != 0)
|
2019-03-21 03:03:27 +08:00
|
|
|
tcc_error("alignment must be a positive power of two");
|
2019-04-09 02:58:49 +08:00
|
|
|
}
|
|
|
|
skip(')');
|
|
|
|
ad->a.aligned = exact_log2p1(n);
|
2019-03-21 03:03:27 +08:00
|
|
|
}
|
2019-04-09 02:58:49 +08:00
|
|
|
continue;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_LONG:
|
|
|
|
if ((t & VT_BTYPE) == VT_DOUBLE) {
|
2017-09-25 00:57:48 +08:00
|
|
|
t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
|
|
|
|
} else if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
|
|
|
|
t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LLONG;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2017-07-26 04:04:24 +08:00
|
|
|
u = VT_LONG;
|
|
|
|
goto basic_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-07-09 18:34:11 +08:00
|
|
|
next();
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
2015-02-14 02:58:31 +08:00
|
|
|
#ifdef TCC_TARGET_ARM64
|
|
|
|
case TOK_UINT128:
|
|
|
|
/* GCC's __uint128_t appears in some Linux header files. Make it a
|
|
|
|
synonym for long double to get the size and alignment right. */
|
|
|
|
u = VT_LDOUBLE;
|
|
|
|
goto basic_type;
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_BOOL:
|
|
|
|
u = VT_BOOL;
|
|
|
|
goto basic_type;
|
|
|
|
case TOK_FLOAT:
|
|
|
|
u = VT_FLOAT;
|
|
|
|
goto basic_type;
|
|
|
|
case TOK_DOUBLE:
|
2017-09-25 00:57:48 +08:00
|
|
|
if ((t & (VT_BTYPE|VT_LONG)) == VT_LONG) {
|
|
|
|
t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
u = VT_DOUBLE;
|
2017-07-09 18:34:11 +08:00
|
|
|
goto basic_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-07-09 18:34:11 +08:00
|
|
|
next();
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case TOK_ENUM:
|
2017-07-09 18:34:11 +08:00
|
|
|
struct_decl(&type1, VT_ENUM);
|
2009-05-06 02:18:10 +08:00
|
|
|
basic_type2:
|
|
|
|
u = type1.t;
|
|
|
|
type->ref = type1.ref;
|
|
|
|
goto basic_type1;
|
|
|
|
case TOK_STRUCT:
|
2017-07-09 18:34:11 +08:00
|
|
|
struct_decl(&type1, VT_STRUCT);
|
|
|
|
goto basic_type2;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_UNION:
|
2017-07-09 18:34:11 +08:00
|
|
|
struct_decl(&type1, VT_UNION);
|
2009-05-06 02:18:10 +08:00
|
|
|
goto basic_type2;
|
|
|
|
|
|
|
|
/* type modifiers */
|
|
|
|
case TOK_CONST1:
|
|
|
|
case TOK_CONST2:
|
|
|
|
case TOK_CONST3:
|
2016-01-11 15:51:58 +08:00
|
|
|
type->t = t;
|
|
|
|
parse_btype_qualify(type, VT_CONSTANT);
|
|
|
|
t = type->t;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_VOLATILE1:
|
|
|
|
case TOK_VOLATILE2:
|
|
|
|
case TOK_VOLATILE3:
|
2016-01-11 15:51:58 +08:00
|
|
|
type->t = t;
|
|
|
|
parse_btype_qualify(type, VT_VOLATILE);
|
|
|
|
t = type->t;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_SIGNED1:
|
|
|
|
case TOK_SIGNED2:
|
|
|
|
case TOK_SIGNED3:
|
2014-02-06 20:51:47 +08:00
|
|
|
if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
|
|
|
|
tcc_error("signed and unsigned modifier");
|
|
|
|
t |= VT_DEFSIGN;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
2017-07-09 18:34:11 +08:00
|
|
|
typespec_found = 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case TOK_REGISTER:
|
|
|
|
case TOK_AUTO:
|
|
|
|
case TOK_RESTRICT1:
|
|
|
|
case TOK_RESTRICT2:
|
|
|
|
case TOK_RESTRICT3:
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_UNSIGNED:
|
2014-02-06 20:51:47 +08:00
|
|
|
if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
|
|
|
|
tcc_error("signed and unsigned modifier");
|
|
|
|
t |= VT_DEFSIGN | VT_UNSIGNED;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
typespec_found = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* storage */
|
|
|
|
case TOK_EXTERN:
|
2017-04-04 14:34:52 +08:00
|
|
|
g = VT_EXTERN;
|
|
|
|
goto storage;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_STATIC:
|
2017-04-04 14:34:52 +08:00
|
|
|
g = VT_STATIC;
|
|
|
|
goto storage;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_TYPEDEF:
|
2017-04-04 14:34:52 +08:00
|
|
|
g = VT_TYPEDEF;
|
|
|
|
goto storage;
|
|
|
|
storage:
|
|
|
|
if (t & (VT_EXTERN|VT_STATIC|VT_TYPEDEF) & ~g)
|
|
|
|
tcc_error("multiple storage classes");
|
|
|
|
t |= g;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_INLINE1:
|
|
|
|
case TOK_INLINE2:
|
|
|
|
case TOK_INLINE3:
|
|
|
|
t |= VT_INLINE;
|
|
|
|
next();
|
|
|
|
break;
|
2019-03-21 03:03:27 +08:00
|
|
|
case TOK_NORETURN3:
|
|
|
|
/* currently, no need to handle it because tcc does not
|
|
|
|
track unused objects */
|
|
|
|
next();
|
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* GNUC attribute */
|
|
|
|
case TOK_ATTRIBUTE1:
|
|
|
|
case TOK_ATTRIBUTE2:
|
|
|
|
parse_attribute(ad);
|
2017-07-09 18:34:11 +08:00
|
|
|
if (ad->attr_mode) {
|
|
|
|
u = ad->attr_mode -1;
|
2017-09-25 00:57:48 +08:00
|
|
|
t = (t & ~(VT_BTYPE|VT_LONG)) | u;
|
2010-01-27 05:56:22 +08:00
|
|
|
}
|
2018-07-29 00:55:54 +08:00
|
|
|
continue;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* GNUC typeof */
|
|
|
|
case TOK_TYPEOF1:
|
|
|
|
case TOK_TYPEOF2:
|
|
|
|
case TOK_TYPEOF3:
|
|
|
|
next();
|
|
|
|
parse_expr_type(&type1);
|
2011-03-07 14:32:35 +08:00
|
|
|
/* remove all storage modifiers except typedef */
|
|
|
|
type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
|
2017-07-14 23:42:48 +08:00
|
|
|
if (type1.ref)
|
|
|
|
sym_to_attr(ad, type1.ref);
|
2009-05-06 02:18:10 +08:00
|
|
|
goto basic_type2;
|
|
|
|
default:
|
2014-03-24 23:28:56 +08:00
|
|
|
if (typespec_found)
|
2009-05-06 02:18:10 +08:00
|
|
|
goto the_end;
|
|
|
|
s = sym_find(tok);
|
|
|
|
if (!s || !(s->type.t & VT_TYPEDEF))
|
|
|
|
goto the_end;
|
2017-09-25 00:57:48 +08:00
|
|
|
t &= ~(VT_BTYPE|VT_LONG);
|
2017-07-09 18:34:11 +08:00
|
|
|
u = t & ~(VT_CONSTANT | VT_VOLATILE), t ^= u;
|
|
|
|
type->t = (s->type.t & ~VT_TYPEDEF) | u;
|
2016-01-11 15:51:58 +08:00
|
|
|
type->ref = s->type.ref;
|
2017-07-09 18:34:11 +08:00
|
|
|
if (t)
|
|
|
|
parse_btype_qualify(type, t);
|
2016-01-11 15:51:58 +08:00
|
|
|
t = type->t;
|
2017-07-09 18:34:11 +08:00
|
|
|
/* get attributes from typedef */
|
2017-07-14 23:42:48 +08:00
|
|
|
sym_to_attr(ad, s);
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
typespec_found = 1;
|
2017-07-09 18:34:11 +08:00
|
|
|
st = bt = -2;
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
type_found = 1;
|
|
|
|
}
|
|
|
|
the_end:
|
|
|
|
if (tcc_state->char_is_unsigned) {
|
2014-02-06 20:51:47 +08:00
|
|
|
if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
|
2009-05-06 02:18:10 +08:00
|
|
|
t |= VT_UNSIGNED;
|
|
|
|
}
|
2017-09-25 00:57:48 +08:00
|
|
|
/* VT_LONG is used just as a modifier for VT_INT / VT_LLONG */
|
|
|
|
bt = t & (VT_BTYPE|VT_LONG);
|
|
|
|
if (bt == VT_LONG)
|
|
|
|
t |= LONG_SIZE == 8 ? VT_LLONG : VT_INT;
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
if (bt == VT_LDOUBLE)
|
|
|
|
t = (t & ~(VT_BTYPE|VT_LONG)) | VT_DOUBLE;
|
2009-05-06 02:18:10 +08:00
|
|
|
#endif
|
|
|
|
type->t = t;
|
|
|
|
return type_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert a function parameter type (array to pointer and function to
|
|
|
|
function pointer) */
|
|
|
|
static inline void convert_parameter_type(CType *pt)
|
|
|
|
{
|
|
|
|
/* remove const and volatile qualifiers (XXX: const could be used
|
|
|
|
to indicate a const function parameter */
|
|
|
|
pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
|
|
|
|
/* array must be transformed to pointer according to ANSI C */
|
|
|
|
pt->t &= ~VT_ARRAY;
|
|
|
|
if ((pt->t & VT_BTYPE) == VT_FUNC) {
|
|
|
|
mk_pointer(pt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-15 01:17:39 +08:00
|
|
|
ST_FUNC void parse_asm_str(CString *astr)
|
|
|
|
{
|
|
|
|
skip('(');
|
2016-06-28 02:10:53 +08:00
|
|
|
parse_mult_str(astr, "string constant");
|
2010-09-15 01:17:39 +08:00
|
|
|
}
|
|
|
|
|
2015-11-20 18:22:56 +08:00
|
|
|
/* Parse an asm label and return the token */
|
|
|
|
static int asm_label_instr(void)
|
2010-09-15 01:17:39 +08:00
|
|
|
{
|
2015-11-20 18:22:56 +08:00
|
|
|
int v;
|
|
|
|
CString astr;
|
|
|
|
|
2010-09-15 01:17:39 +08:00
|
|
|
next();
|
2015-11-20 18:22:56 +08:00
|
|
|
parse_asm_str(&astr);
|
2010-09-15 01:17:39 +08:00
|
|
|
skip(')');
|
|
|
|
#ifdef ASM_DEBUG
|
2015-11-20 18:22:56 +08:00
|
|
|
printf("asm_alias: \"%s\"\n", (char *)astr.data);
|
2010-09-15 01:17:39 +08:00
|
|
|
#endif
|
2015-11-20 18:22:56 +08:00
|
|
|
v = tok_alloc(astr.data, astr.size - 1)->tok;
|
|
|
|
cstr_free(&astr);
|
|
|
|
return v;
|
2010-09-15 01:17:39 +08:00
|
|
|
}
|
|
|
|
|
2017-03-06 10:25:33 +08:00
|
|
|
static int post_type(CType *type, AttributeDef *ad, int storage, int td)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int n, l, t1, arg_size, align;
|
|
|
|
Sym **plast, *s, *first;
|
|
|
|
AttributeDef ad1;
|
|
|
|
CType pt;
|
|
|
|
|
2011-03-09 04:56:13 +08:00
|
|
|
if (tok == '(') {
|
2017-03-06 10:25:33 +08:00
|
|
|
/* function type, or recursive declarator (return if so) */
|
2011-03-09 04:56:13 +08:00
|
|
|
next();
|
2017-03-06 10:25:33 +08:00
|
|
|
if (td && !(td & TYPE_ABSTRACT))
|
|
|
|
return 0;
|
|
|
|
if (tok == ')')
|
|
|
|
l = 0;
|
|
|
|
else if (parse_btype(&pt, &ad1))
|
|
|
|
l = FUNC_NEW;
|
2019-01-01 05:00:31 +08:00
|
|
|
else if (td) {
|
|
|
|
merge_attr (ad, &ad1);
|
|
|
|
return 0;
|
|
|
|
} else
|
2017-03-06 10:25:33 +08:00
|
|
|
l = FUNC_OLD;
|
2011-03-09 04:56:13 +08:00
|
|
|
first = NULL;
|
|
|
|
plast = &first;
|
|
|
|
arg_size = 0;
|
2017-03-06 10:25:33 +08:00
|
|
|
if (l) {
|
2011-03-09 04:56:13 +08:00
|
|
|
for(;;) {
|
|
|
|
/* read param name and compute offset */
|
|
|
|
if (l != FUNC_OLD) {
|
|
|
|
if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
|
|
|
|
break;
|
|
|
|
type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
|
|
|
|
if ((pt.t & VT_BTYPE) == VT_VOID)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("parameter declared as void");
|
2011-03-09 04:56:13 +08:00
|
|
|
} else {
|
|
|
|
n = tok;
|
|
|
|
if (n < TOK_UIDENT)
|
|
|
|
expect("identifier");
|
2017-03-11 09:13:59 +08:00
|
|
|
pt.t = VT_VOID; /* invalid type */
|
2011-03-09 04:56:13 +08:00
|
|
|
next();
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2011-03-09 04:56:13 +08:00
|
|
|
convert_parameter_type(&pt);
|
2018-06-01 05:51:51 +08:00
|
|
|
arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
|
2011-03-09 04:56:13 +08:00
|
|
|
s = sym_push(n | SYM_FIELD, &pt, 0, 0);
|
|
|
|
*plast = s;
|
|
|
|
plast = &s->next;
|
|
|
|
if (tok == ')')
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
2011-03-09 04:56:13 +08:00
|
|
|
skip(',');
|
|
|
|
if (l == FUNC_NEW && tok == TOK_DOTS) {
|
|
|
|
l = FUNC_ELLIPSIS;
|
|
|
|
next();
|
|
|
|
break;
|
|
|
|
}
|
2017-03-06 10:25:33 +08:00
|
|
|
if (l == FUNC_NEW && !parse_btype(&pt, &ad1))
|
|
|
|
tcc_error("invalid type");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-03-06 10:25:33 +08:00
|
|
|
} else
|
|
|
|
/* if no parameters, then old type prototype */
|
2011-03-09 04:56:13 +08:00
|
|
|
l = FUNC_OLD;
|
|
|
|
skip(')');
|
|
|
|
/* NOTE: const is ignored in returned type as it has a special
|
|
|
|
meaning in gcc / C++ */
|
2015-07-30 04:53:57 +08:00
|
|
|
type->t &= ~VT_CONSTANT;
|
2011-03-09 04:56:13 +08:00
|
|
|
/* some ancient pre-K&R C allows a function to return an array
|
2015-07-30 04:53:57 +08:00
|
|
|
and the array brackets to be put after the arguments, such
|
2011-03-09 07:12:09 +08:00
|
|
|
that "int c()[]" means something like "int[] c()" */
|
|
|
|
if (tok == '[') {
|
|
|
|
next();
|
|
|
|
skip(']'); /* only handle simple "[]" */
|
2017-05-06 04:01:02 +08:00
|
|
|
mk_pointer(type);
|
2011-03-09 07:12:09 +08:00
|
|
|
}
|
2011-03-09 04:56:13 +08:00
|
|
|
/* we push a anonymous symbol which will contain the function prototype */
|
2017-07-09 18:34:11 +08:00
|
|
|
ad->f.func_args = arg_size;
|
|
|
|
ad->f.func_type = l;
|
|
|
|
s = sym_push(SYM_FIELD, type, 0, 0);
|
2014-01-07 21:57:07 +08:00
|
|
|
s->a = ad->a;
|
2017-07-09 18:34:11 +08:00
|
|
|
s->f = ad->f;
|
2011-03-09 04:56:13 +08:00
|
|
|
s->next = first;
|
2011-04-07 03:08:50 +08:00
|
|
|
type->t = VT_FUNC;
|
2011-03-09 04:56:13 +08:00
|
|
|
type->ref = s;
|
|
|
|
} else if (tok == '[') {
|
2016-09-04 05:55:54 +08:00
|
|
|
int saved_nocode_wanted = nocode_wanted;
|
2011-03-09 04:56:13 +08:00
|
|
|
/* array definition */
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
2018-03-23 20:19:58 +08:00
|
|
|
while (1) {
|
|
|
|
/* XXX The optional type-quals and static should only be accepted
|
|
|
|
in parameter decls. The '*' as well, and then even only
|
|
|
|
in prototypes (not function defs). */
|
|
|
|
switch (tok) {
|
|
|
|
case TOK_RESTRICT1: case TOK_RESTRICT2: case TOK_RESTRICT3:
|
|
|
|
case TOK_CONST1:
|
|
|
|
case TOK_VOLATILE1:
|
|
|
|
case TOK_STATIC:
|
|
|
|
case '*':
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-03-09 04:56:13 +08:00
|
|
|
n = -1;
|
2011-04-07 03:08:50 +08:00
|
|
|
t1 = 0;
|
2011-03-09 04:56:13 +08:00
|
|
|
if (tok != ']') {
|
2016-09-04 05:55:54 +08:00
|
|
|
if (!local_stack || (storage & VT_STATIC))
|
|
|
|
vpushi(expr_const());
|
|
|
|
else {
|
|
|
|
/* VLAs (which can only happen with local_stack && !VT_STATIC)
|
|
|
|
length must always be evaluated, even under nocode_wanted,
|
|
|
|
so that its size slot is initialized (e.g. under sizeof
|
|
|
|
or typeof). */
|
|
|
|
nocode_wanted = 0;
|
|
|
|
gexpr();
|
|
|
|
}
|
2011-04-07 00:17:03 +08:00
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
|
|
|
n = vtop->c.i;
|
|
|
|
if (n < 0)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("invalid array size");
|
2011-04-07 00:17:03 +08:00
|
|
|
} else {
|
|
|
|
if (!is_integer_btype(vtop->type.t & VT_BTYPE))
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("size of variable length array should be an integer");
|
2019-04-07 10:09:25 +08:00
|
|
|
n = 0;
|
2011-04-07 03:08:50 +08:00
|
|
|
t1 = VT_VLA;
|
2011-04-07 00:17:03 +08:00
|
|
|
}
|
2011-03-09 04:56:13 +08:00
|
|
|
}
|
|
|
|
skip(']');
|
|
|
|
/* parse next post type */
|
2017-03-06 10:25:33 +08:00
|
|
|
post_type(type, ad, storage, 0);
|
2013-09-20 00:58:46 +08:00
|
|
|
if (type->t == VT_FUNC)
|
|
|
|
tcc_error("declaration of an array of functions");
|
2011-04-07 00:17:03 +08:00
|
|
|
t1 |= type->t & VT_VLA;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2011-04-10 13:52:25 +08:00
|
|
|
if (t1 & VT_VLA) {
|
2019-04-07 10:09:25 +08:00
|
|
|
if (n < 0)
|
|
|
|
tcc_error("need explicit inner array size in VLAs");
|
2011-04-10 13:52:25 +08:00
|
|
|
loc -= type_size(&int_type, &align);
|
|
|
|
loc &= -align;
|
|
|
|
n = loc;
|
|
|
|
|
|
|
|
vla_runtime_type_size(type, &align);
|
|
|
|
gen_op('*');
|
2015-04-28 17:23:29 +08:00
|
|
|
vset(&int_type, VT_LOCAL|VT_LVAL, n);
|
2011-04-10 13:52:25 +08:00
|
|
|
vswap();
|
|
|
|
vstore();
|
|
|
|
}
|
|
|
|
if (n != -1)
|
|
|
|
vpop();
|
2016-09-04 05:55:54 +08:00
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2011-04-07 00:17:03 +08:00
|
|
|
/* we push an anonymous symbol which will contain the array
|
2011-03-09 04:56:13 +08:00
|
|
|
element type */
|
|
|
|
s = sym_push(SYM_FIELD, type, 0, n);
|
2011-04-08 16:09:39 +08:00
|
|
|
type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
|
2011-03-09 04:56:13 +08:00
|
|
|
type->ref = s;
|
|
|
|
}
|
2017-03-06 10:25:33 +08:00
|
|
|
return 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2017-03-06 10:25:33 +08:00
|
|
|
/* Parse a type declarator (except basic type), and return the type
|
2009-05-06 02:18:10 +08:00
|
|
|
in 'type'. 'td' is a bitmask indicating which kind of type decl is
|
|
|
|
expected. 'type' should contain the basic type. 'ad' is the
|
|
|
|
attribute definition of the basic type. It can be modified by
|
2017-03-06 10:25:33 +08:00
|
|
|
type_decl(). If this (possibly abstract) declarator is a pointer chain
|
|
|
|
it returns the innermost pointed to type (equals *type, but is a different
|
|
|
|
pointer), otherwise returns type itself, that's used for recursive calls. */
|
|
|
|
static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2017-03-06 10:25:33 +08:00
|
|
|
CType *post, *ret;
|
2013-01-07 00:20:44 +08:00
|
|
|
int qualifiers, storage;
|
|
|
|
|
2017-03-06 10:25:33 +08:00
|
|
|
/* recursive type, remove storage bits first, apply them later again */
|
|
|
|
storage = type->t & VT_STORAGE;
|
|
|
|
type->t &= ~VT_STORAGE;
|
|
|
|
post = ret = type;
|
2017-07-09 18:38:25 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
while (tok == '*') {
|
|
|
|
qualifiers = 0;
|
|
|
|
redo:
|
|
|
|
next();
|
|
|
|
switch(tok) {
|
|
|
|
case TOK_CONST1:
|
|
|
|
case TOK_CONST2:
|
|
|
|
case TOK_CONST3:
|
|
|
|
qualifiers |= VT_CONSTANT;
|
|
|
|
goto redo;
|
|
|
|
case TOK_VOLATILE1:
|
|
|
|
case TOK_VOLATILE2:
|
|
|
|
case TOK_VOLATILE3:
|
|
|
|
qualifiers |= VT_VOLATILE;
|
|
|
|
goto redo;
|
|
|
|
case TOK_RESTRICT1:
|
|
|
|
case TOK_RESTRICT2:
|
|
|
|
case TOK_RESTRICT3:
|
|
|
|
goto redo;
|
2016-07-15 23:56:20 +08:00
|
|
|
/* XXX: clarify attribute handling */
|
|
|
|
case TOK_ATTRIBUTE1:
|
|
|
|
case TOK_ATTRIBUTE2:
|
|
|
|
parse_attribute(ad);
|
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
mk_pointer(type);
|
|
|
|
type->t |= qualifiers;
|
2017-03-06 10:25:33 +08:00
|
|
|
if (ret == type)
|
|
|
|
/* innermost pointed to type is the one for the first derivation */
|
|
|
|
ret = pointed_type(type);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-02-27 09:22:28 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == '(') {
|
2017-03-06 10:25:33 +08:00
|
|
|
/* This is possibly a parameter type list for abstract declarators
|
|
|
|
('int ()'), use post_type for testing this. */
|
|
|
|
if (!post_type(type, ad, 0, td)) {
|
|
|
|
/* It's not, so it's a nested declarator, and the post operations
|
|
|
|
apply to the innermost pointed to type (if any). */
|
|
|
|
/* XXX: this is not correct to modify 'ad' at this point, but
|
|
|
|
the syntax is not clear */
|
2017-07-09 18:34:11 +08:00
|
|
|
parse_attribute(ad);
|
2017-03-06 10:25:33 +08:00
|
|
|
post = type_decl(type, ad, v, td);
|
|
|
|
skip(')');
|
2019-03-13 00:27:15 +08:00
|
|
|
} else
|
|
|
|
goto abstract;
|
2017-03-06 10:25:33 +08:00
|
|
|
} else if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
|
|
|
|
/* type identifier */
|
|
|
|
*v = tok;
|
|
|
|
next();
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2019-03-13 00:27:15 +08:00
|
|
|
abstract:
|
2017-03-06 10:25:33 +08:00
|
|
|
if (!(td & TYPE_ABSTRACT))
|
|
|
|
expect("identifier");
|
|
|
|
*v = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-03-06 10:25:33 +08:00
|
|
|
post_type(post, ad, storage, 0);
|
2017-07-09 18:34:11 +08:00
|
|
|
parse_attribute(ad);
|
2017-04-04 14:34:52 +08:00
|
|
|
type->t |= storage;
|
2017-03-06 10:25:33 +08:00
|
|
|
return ret;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compute the lvalue VT_LVAL_xxx needed to match type t. */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC int lvalue_type(int t)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int bt, r;
|
|
|
|
r = VT_LVAL;
|
|
|
|
bt = t & VT_BTYPE;
|
|
|
|
if (bt == VT_BYTE || bt == VT_BOOL)
|
|
|
|
r |= VT_LVAL_BYTE;
|
|
|
|
else if (bt == VT_SHORT)
|
|
|
|
r |= VT_LVAL_SHORT;
|
|
|
|
else
|
|
|
|
return r;
|
|
|
|
if (t & VT_UNSIGNED)
|
|
|
|
r |= VT_LVAL_UNSIGNED;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* indirection with full error checking and bound check */
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void indir(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
|
|
|
|
return;
|
|
|
|
expect("pointer");
|
|
|
|
}
|
2016-12-19 00:23:33 +08:00
|
|
|
if (vtop->r & VT_LVAL)
|
2009-05-06 02:18:10 +08:00
|
|
|
gv(RC_INT);
|
|
|
|
vtop->type = *pointed_type(&vtop->type);
|
|
|
|
/* Arrays and functions are never lvalues */
|
2011-04-08 16:09:39 +08:00
|
|
|
if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
|
2009-05-06 02:18:10 +08:00
|
|
|
&& (vtop->type.t & VT_BTYPE) != VT_FUNC) {
|
|
|
|
vtop->r |= lvalue_type(vtop->type.t);
|
|
|
|
/* if bound checking, the referenced pointer must be checked */
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2009-05-12 00:45:44 +08:00
|
|
|
if (tcc_state->do_bounds_check)
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop->r |= VT_MUSTBOUND;
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pass a parameter to a function and do type checking and casting */
|
|
|
|
static void gfunc_param_typed(Sym *func, Sym *arg)
|
|
|
|
{
|
|
|
|
int func_type;
|
|
|
|
CType type;
|
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
func_type = func->f.func_type;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (func_type == FUNC_OLD ||
|
|
|
|
(func_type == FUNC_ELLIPSIS && arg == NULL)) {
|
|
|
|
/* default casting : only need to convert float to double */
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_DOUBLE);
|
2014-02-04 20:54:28 +08:00
|
|
|
} else if (vtop->type.t & VT_BITFIELD) {
|
|
|
|
type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
|
2016-08-16 00:54:11 +08:00
|
|
|
type.ref = vtop->type.ref;
|
2014-02-04 20:54:28 +08:00
|
|
|
gen_cast(&type);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
} else if (arg == NULL) {
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("too many arguments to function");
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
type = arg->type;
|
|
|
|
type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
|
|
|
|
gen_assign_cast(&type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 04:21:27 +08:00
|
|
|
/* parse an expression and return its type without any side effect. */
|
|
|
|
static void expr_type(CType *type, void (*expr_fn)(void))
|
2017-02-27 10:34:33 +08:00
|
|
|
{
|
|
|
|
nocode_wanted++;
|
2017-07-21 04:21:27 +08:00
|
|
|
expr_fn();
|
2017-02-27 10:34:33 +08:00
|
|
|
*type = vtop->type;
|
|
|
|
vpop();
|
|
|
|
nocode_wanted--;
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* parse an expression of the form '(type)' or '(expr)' and return its
|
|
|
|
type */
|
|
|
|
static void parse_expr_type(CType *type)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
AttributeDef ad;
|
|
|
|
|
|
|
|
skip('(');
|
|
|
|
if (parse_btype(type, &ad)) {
|
|
|
|
type_decl(type, &ad, &n, TYPE_ABSTRACT);
|
|
|
|
} else {
|
2017-07-21 04:21:27 +08:00
|
|
|
expr_type(type, gexpr);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
skip(')');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_type(CType *type)
|
|
|
|
{
|
|
|
|
AttributeDef ad;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!parse_btype(type, &ad)) {
|
|
|
|
expect("type");
|
|
|
|
}
|
|
|
|
type_decl(type, &ad, &n, TYPE_ABSTRACT);
|
|
|
|
}
|
|
|
|
|
2017-02-27 09:22:28 +08:00
|
|
|
static void parse_builtin_params(int nc, const char *args)
|
|
|
|
{
|
|
|
|
char c, sep = '(';
|
|
|
|
CType t;
|
|
|
|
if (nc)
|
|
|
|
nocode_wanted++;
|
|
|
|
next();
|
|
|
|
while ((c = *args++)) {
|
|
|
|
skip(sep);
|
|
|
|
sep = ',';
|
|
|
|
switch (c) {
|
|
|
|
case 'e': expr_eq(); continue;
|
|
|
|
case 't': parse_type(&t); vpush(&t); continue;
|
|
|
|
default: tcc_error("internal error"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skip(')');
|
|
|
|
if (nc)
|
|
|
|
nocode_wanted--;
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
static void try_call_scope_cleanup(Sym *stop)
|
2018-12-20 17:55:22 +08:00
|
|
|
{
|
2019-01-28 08:21:38 +08:00
|
|
|
Sym *cls = current_cleanups;
|
2018-12-20 17:55:22 +08:00
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
for (; cls != stop; cls = cls->ncl) {
|
|
|
|
Sym *fs = cls->next;
|
|
|
|
Sym *vs = cls->prev_tok;
|
2018-12-20 17:55:22 +08:00
|
|
|
|
|
|
|
vpushsym(&fs->type, fs);
|
|
|
|
vset(&vs->type, vs->r, vs->c);
|
|
|
|
vtop->sym = vs;
|
2019-01-28 08:21:38 +08:00
|
|
|
mk_pointer(&vtop->type);
|
2018-12-20 17:55:22 +08:00
|
|
|
gaddrof();
|
|
|
|
gfunc_call(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
static void try_call_cleanup_goto(Sym *cleanupstate)
|
2018-12-20 17:55:22 +08:00
|
|
|
{
|
2019-01-28 08:21:38 +08:00
|
|
|
Sym *oc, *cc;
|
|
|
|
int ocd, ccd;
|
2019-01-24 02:42:04 +08:00
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
if (!current_cleanups)
|
2018-12-20 17:55:22 +08:00
|
|
|
return;
|
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
/* search NCA of both cleanup chains given parents and initial depth */
|
|
|
|
ocd = cleanupstate ? cleanupstate->v & ~SYM_FIELD : 0;
|
|
|
|
for (ccd = ncleanups, oc = cleanupstate; ocd > ccd; --ocd, oc = oc->ncl)
|
|
|
|
;
|
|
|
|
for (cc = current_cleanups; ccd > ocd; --ccd, cc = cc->ncl)
|
|
|
|
;
|
|
|
|
for (; cc != oc; cc = cc->ncl, oc = oc->ncl, --ccd)
|
|
|
|
;
|
2019-01-24 02:42:04 +08:00
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
try_call_scope_cleanup(cc);
|
2018-12-20 17:55:22 +08:00
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void unary(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2010-04-11 07:53:40 +08:00
|
|
|
int n, t, align, size, r, sizeof_caller;
|
2009-05-06 02:18:10 +08:00
|
|
|
CType type;
|
|
|
|
Sym *s;
|
|
|
|
AttributeDef ad;
|
|
|
|
|
2010-04-11 07:53:40 +08:00
|
|
|
sizeof_caller = in_sizeof;
|
|
|
|
in_sizeof = 0;
|
2017-07-09 18:34:11 +08:00
|
|
|
type.ref = NULL;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* XXX: GCC 2.95.3 does not generate a table although it should be
|
|
|
|
better here */
|
|
|
|
tok_next:
|
|
|
|
switch(tok) {
|
|
|
|
case TOK_EXTENSION:
|
|
|
|
next();
|
|
|
|
goto tok_next;
|
2017-09-25 00:57:48 +08:00
|
|
|
case TOK_LCHAR:
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
t = VT_SHORT|VT_UNSIGNED;
|
|
|
|
goto push_tokc;
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_CINT:
|
2015-07-30 04:53:57 +08:00
|
|
|
case TOK_CCHAR:
|
2017-03-07 04:45:41 +08:00
|
|
|
t = VT_INT;
|
|
|
|
push_tokc:
|
|
|
|
type.t = t;
|
|
|
|
vsetc(&type, VT_CONST, &tokc);
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK_CUINT:
|
2017-03-07 04:45:41 +08:00
|
|
|
t = VT_INT | VT_UNSIGNED;
|
|
|
|
goto push_tokc;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_CLLONG:
|
2017-03-07 04:45:41 +08:00
|
|
|
t = VT_LLONG;
|
|
|
|
goto push_tokc;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_CULLONG:
|
2017-03-12 12:25:09 +08:00
|
|
|
t = VT_LLONG | VT_UNSIGNED;
|
2017-03-07 04:45:41 +08:00
|
|
|
goto push_tokc;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_CFLOAT:
|
2017-03-07 04:45:41 +08:00
|
|
|
t = VT_FLOAT;
|
|
|
|
goto push_tokc;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_CDOUBLE:
|
2017-03-07 04:45:41 +08:00
|
|
|
t = VT_DOUBLE;
|
|
|
|
goto push_tokc;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_CLDOUBLE:
|
2017-03-07 04:45:41 +08:00
|
|
|
t = VT_LDOUBLE;
|
|
|
|
goto push_tokc;
|
mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
this case is usefull as with -funsigned-char, 'char *' are unsigned
* change VT_LONG so it's now a qualifier
VT_LONG are never use for code generation, but only durring parsing state,
in _Generic we need to be able to make diference between
'long' and 'long long'
So VT_LONG is now use as a type qualifier, it's old behaviour is still
here, but we can keep trace of what was a long and what wasn't
* add TOK_CLONG and TOK_CULONG
tcc was directly converting value like '7171L' into TOK_CLLONG or
TOK_CINT depending of the machine architecture.
because of that, we was unable to make diference between a long and a
long long, which doesn't work with _Generic.
So now 7171L is a TOK_CLONG, and we can handle _Generic properly
* check that _Generic can make diference between long and long long
* uncomment "type match twice" as it should now pass tests on any platforms
* add inside_generic global
the point of this variable is to use VT_LONG in comparaison only
when we are evaluating a _Generic.
problem is with my lastest patchs tcc can now make the diference between
a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
break when stdint.h and stddef.h are include together.
Another solution woud be to modifie include/stddef.h so it define uint64_t as
unsigned long int when processor is 64 bit, but this could break some
legacy code, so for now, VT_LONG are use only inside generc.
* check that _Generic parse first argument correctly
* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-10 23:44:53 +08:00
|
|
|
case TOK_CLONG:
|
2017-09-25 00:57:48 +08:00
|
|
|
t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG;
|
|
|
|
goto push_tokc;
|
mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
this case is usefull as with -funsigned-char, 'char *' are unsigned
* change VT_LONG so it's now a qualifier
VT_LONG are never use for code generation, but only durring parsing state,
in _Generic we need to be able to make diference between
'long' and 'long long'
So VT_LONG is now use as a type qualifier, it's old behaviour is still
here, but we can keep trace of what was a long and what wasn't
* add TOK_CLONG and TOK_CULONG
tcc was directly converting value like '7171L' into TOK_CLLONG or
TOK_CINT depending of the machine architecture.
because of that, we was unable to make diference between a long and a
long long, which doesn't work with _Generic.
So now 7171L is a TOK_CLONG, and we can handle _Generic properly
* check that _Generic can make diference between long and long long
* uncomment "type match twice" as it should now pass tests on any platforms
* add inside_generic global
the point of this variable is to use VT_LONG in comparaison only
when we are evaluating a _Generic.
problem is with my lastest patchs tcc can now make the diference between
a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
break when stdint.h and stddef.h are include together.
Another solution woud be to modifie include/stddef.h so it define uint64_t as
unsigned long int when processor is 64 bit, but this could break some
legacy code, so for now, VT_LONG are use only inside generc.
* check that _Generic parse first argument correctly
* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-10 23:44:53 +08:00
|
|
|
case TOK_CULONG:
|
2017-09-25 00:57:48 +08:00
|
|
|
t = (LONG_SIZE == 8 ? VT_LLONG : VT_INT) | VT_LONG | VT_UNSIGNED;
|
mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
this case is usefull as with -funsigned-char, 'char *' are unsigned
* change VT_LONG so it's now a qualifier
VT_LONG are never use for code generation, but only durring parsing state,
in _Generic we need to be able to make diference between
'long' and 'long long'
So VT_LONG is now use as a type qualifier, it's old behaviour is still
here, but we can keep trace of what was a long and what wasn't
* add TOK_CLONG and TOK_CULONG
tcc was directly converting value like '7171L' into TOK_CLLONG or
TOK_CINT depending of the machine architecture.
because of that, we was unable to make diference between a long and a
long long, which doesn't work with _Generic.
So now 7171L is a TOK_CLONG, and we can handle _Generic properly
* check that _Generic can make diference between long and long long
* uncomment "type match twice" as it should now pass tests on any platforms
* add inside_generic global
the point of this variable is to use VT_LONG in comparaison only
when we are evaluating a _Generic.
problem is with my lastest patchs tcc can now make the diference between
a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
break when stdint.h and stddef.h are include together.
Another solution woud be to modifie include/stddef.h so it define uint64_t as
unsigned long int when processor is 64 bit, but this could break some
legacy code, so for now, VT_LONG are use only inside generc.
* check that _Generic parse first argument correctly
* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-10 23:44:53 +08:00
|
|
|
goto push_tokc;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK___FUNCTION__:
|
|
|
|
if (!gnu_ext)
|
|
|
|
goto tok_identifier;
|
|
|
|
/* fall thru */
|
|
|
|
case TOK___FUNC__:
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
int len;
|
|
|
|
/* special function name identifier */
|
|
|
|
len = strlen(funcname) + 1;
|
|
|
|
/* generate char[len] type */
|
|
|
|
type.t = VT_BYTE;
|
|
|
|
mk_pointer(&type);
|
|
|
|
type.t |= VT_ARRAY;
|
|
|
|
type.ref->c = len;
|
|
|
|
vpush_ref(&type, data_section, data_section->data_offset, len);
|
2017-07-16 18:10:00 +08:00
|
|
|
if (!NODATA_WANTED) {
|
|
|
|
ptr = section_ptr_add(data_section, len);
|
|
|
|
memcpy(ptr, funcname, len);
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TOK_LSTR:
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
t = VT_SHORT | VT_UNSIGNED;
|
|
|
|
#else
|
|
|
|
t = VT_INT;
|
|
|
|
#endif
|
|
|
|
goto str_init;
|
|
|
|
case TOK_STR:
|
|
|
|
/* string parsing */
|
|
|
|
t = VT_BYTE;
|
2017-07-09 18:07:40 +08:00
|
|
|
if (tcc_state->char_is_unsigned)
|
|
|
|
t = VT_BYTE | VT_UNSIGNED;
|
2009-05-06 02:18:10 +08:00
|
|
|
str_init:
|
2017-07-04 22:37:49 +08:00
|
|
|
if (tcc_state->warn_write_strings)
|
|
|
|
t |= VT_CONSTANT;
|
2009-05-06 02:18:10 +08:00
|
|
|
type.t = t;
|
|
|
|
mk_pointer(&type);
|
|
|
|
type.t |= VT_ARRAY;
|
|
|
|
memset(&ad, 0, sizeof(AttributeDef));
|
2015-11-20 18:22:56 +08:00
|
|
|
decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
next();
|
|
|
|
/* cast ? */
|
|
|
|
if (parse_btype(&type, &ad)) {
|
|
|
|
type_decl(&type, &ad, &n, TYPE_ABSTRACT);
|
|
|
|
skip(')');
|
|
|
|
/* check ISOC99 compound literal */
|
|
|
|
if (tok == '{') {
|
|
|
|
/* data is allocated locally by default */
|
|
|
|
if (global_expr)
|
|
|
|
r = VT_CONST;
|
|
|
|
else
|
|
|
|
r = VT_LOCAL;
|
|
|
|
/* all except arrays are lvalues */
|
|
|
|
if (!(type.t & VT_ARRAY))
|
|
|
|
r |= lvalue_type(type.t);
|
|
|
|
memset(&ad, 0, sizeof(AttributeDef));
|
2015-11-20 18:22:56 +08:00
|
|
|
decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2010-04-11 07:53:40 +08:00
|
|
|
if (sizeof_caller) {
|
|
|
|
vpush(&type);
|
|
|
|
return;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
unary();
|
|
|
|
gen_cast(&type);
|
|
|
|
}
|
|
|
|
} else if (tok == '{') {
|
2016-12-20 11:49:22 +08:00
|
|
|
int saved_nocode_wanted = nocode_wanted;
|
2015-11-26 20:24:34 +08:00
|
|
|
if (const_wanted)
|
|
|
|
tcc_error("expected constant");
|
2009-05-06 02:18:10 +08:00
|
|
|
/* save all registers */
|
2016-12-19 00:23:33 +08:00
|
|
|
save_regs(0);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* statement expression : we do not accept break/continue
|
2016-12-20 11:49:22 +08:00
|
|
|
inside as GCC does. We do retain the nocode_wanted state,
|
|
|
|
as statement expressions can't ever be entered from the
|
|
|
|
outside, so any reactivation of code emission (from labels
|
|
|
|
or loop heads) can be disabled again after the end of it. */
|
2019-04-29 17:18:57 +08:00
|
|
|
block(NULL, NULL, NULL, NULL, 1);
|
2016-12-20 11:49:22 +08:00
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(')');
|
|
|
|
} else {
|
|
|
|
gexpr();
|
|
|
|
skip(')');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
indir();
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
/* functions names must be treated as function pointers,
|
|
|
|
except for unary '&' and sizeof. Since we consider that
|
|
|
|
functions are not lvalues, we only have to handle it
|
|
|
|
there and in function calls. */
|
|
|
|
/* arrays can also be used although they are not lvalues */
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
|
2017-02-19 11:25:46 +08:00
|
|
|
!(vtop->type.t & VT_ARRAY))
|
2009-05-06 02:18:10 +08:00
|
|
|
test_lvalue();
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
gaddrof();
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_BOOL);
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop->c.i = !vtop->c.i;
|
|
|
|
} else if ((vtop->r & VT_VALMASK) == VT_CMP)
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i ^= 1;
|
2015-11-22 08:00:36 +08:00
|
|
|
else {
|
2009-05-06 02:18:10 +08:00
|
|
|
save_regs(1);
|
2013-12-31 23:51:20 +08:00
|
|
|
vseti(VT_JMP, gvtst(1, 0));
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
vpushi(-1);
|
|
|
|
gen_op('^');
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_PTR)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("pointer not accepted for unary plus");
|
2014-01-12 11:44:27 +08:00
|
|
|
/* In order to force cast, we add zero, except for floating point
|
2015-07-30 04:53:57 +08:00
|
|
|
where we really need an noop (otherwise -0.0 will be transformed
|
|
|
|
into +0.0). */
|
|
|
|
if (!is_float(vtop->type.t)) {
|
|
|
|
vpushi(0);
|
|
|
|
gen_op('+');
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case TOK_SIZEOF:
|
|
|
|
case TOK_ALIGNOF1:
|
|
|
|
case TOK_ALIGNOF2:
|
2018-12-13 02:53:58 +08:00
|
|
|
case TOK_ALIGNOF3:
|
2009-05-06 02:18:10 +08:00
|
|
|
t = tok;
|
|
|
|
next();
|
2010-04-11 07:53:40 +08:00
|
|
|
in_sizeof++;
|
2017-07-21 04:21:27 +08:00
|
|
|
expr_type(&type, unary); /* Perform a in_sizeof = 0; */
|
2017-07-09 18:38:59 +08:00
|
|
|
s = vtop[1].sym; /* hack: accessing previous vtop */
|
2011-04-08 16:09:39 +08:00
|
|
|
size = type_size(&type, &align);
|
2017-07-09 18:38:59 +08:00
|
|
|
if (s && s->a.aligned)
|
|
|
|
align = 1 << (s->a.aligned - 1);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (t == TOK_SIZEOF) {
|
2011-04-07 00:17:03 +08:00
|
|
|
if (!(type.t & VT_VLA)) {
|
|
|
|
if (size < 0)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("sizeof applied to an incomplete type");
|
2012-04-16 07:13:25 +08:00
|
|
|
vpushs(size);
|
2011-04-08 16:09:39 +08:00
|
|
|
} else {
|
|
|
|
vla_runtime_type_size(&type, &align);
|
2011-04-07 00:17:03 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2012-04-16 07:13:25 +08:00
|
|
|
vpushs(align);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
vtop->type.t |= VT_UNSIGNED;
|
|
|
|
break;
|
|
|
|
|
2016-04-16 17:41:53 +08:00
|
|
|
case TOK_builtin_expect:
|
2017-02-27 09:22:28 +08:00
|
|
|
/* __builtin_expect is a no-op for now */
|
|
|
|
parse_builtin_params(0, "ee");
|
|
|
|
vpop();
|
2016-04-16 17:41:53 +08:00
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_builtin_types_compatible_p:
|
2017-02-27 09:22:28 +08:00
|
|
|
parse_builtin_params(0, "tt");
|
|
|
|
vtop[-1].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
|
|
|
|
vtop[0].type.t &= ~(VT_CONSTANT | VT_VOLATILE);
|
|
|
|
n = is_compatible_types(&vtop[-1].type, &vtop[0].type);
|
|
|
|
vtop -= 2;
|
|
|
|
vpushi(n);
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
2016-07-13 21:11:40 +08:00
|
|
|
case TOK_builtin_choose_expr:
|
|
|
|
{
|
2016-11-06 12:02:11 +08:00
|
|
|
int64_t c;
|
2016-07-13 21:11:40 +08:00
|
|
|
next();
|
|
|
|
skip('(');
|
2016-11-06 12:02:11 +08:00
|
|
|
c = expr_const64();
|
2016-07-13 21:11:40 +08:00
|
|
|
skip(',');
|
|
|
|
if (!c) {
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted++;
|
2016-07-13 21:11:40 +08:00
|
|
|
}
|
|
|
|
expr_eq();
|
|
|
|
if (!c) {
|
|
|
|
vpop();
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted--;
|
2016-07-13 21:11:40 +08:00
|
|
|
}
|
|
|
|
skip(',');
|
|
|
|
if (c) {
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted++;
|
2016-07-13 21:11:40 +08:00
|
|
|
}
|
|
|
|
expr_eq();
|
|
|
|
if (c) {
|
|
|
|
vpop();
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted--;
|
2016-07-13 21:11:40 +08:00
|
|
|
}
|
|
|
|
skip(')');
|
|
|
|
}
|
|
|
|
break;
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_builtin_constant_p:
|
2017-02-27 09:22:28 +08:00
|
|
|
parse_builtin_params(1, "e");
|
|
|
|
n = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
|
|
|
|
vtop--;
|
|
|
|
vpushi(n);
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case TOK_builtin_frame_address:
|
2015-03-07 05:01:14 +08:00
|
|
|
case TOK_builtin_return_address:
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2015-03-07 05:01:14 +08:00
|
|
|
int tok1 = tok;
|
Add support for __builtin_frame_address(level)
Continuing d6072d37 (Add __builtin_frame_address(0)) implement
__builtin_frame_address for levels greater than zero, in order for
tinycc to be able to compile its own lib/bcheck.c after
cffb7af9 (lib/bcheck: Prevent __bound_local_new / __bound_local_delete
from being miscompiled).
I'm new to the internals, and used the most simple way to do it.
Generated code is not very good for levels >= 2, compare
gcc tcc
level=0 mov %ebp,%eax lea 0x0(%ebp),%eax
level=1 mov 0x0(%ebp),%eax mov 0x0(%ebp),%eax
level=2 mov 0x0(%ebp),%eax mov 0x0(%ebp),%eax
mov (%eax),%eax mov %eax,-0x10(%ebp)
mov -0x10(%ebp),%eax
mov (%eax),%eax
level=3 mov 0x0(%ebp),%eax mov 0x0(%ebp),%eax
mov (%eax),%eax mov (%eax),%ecx
mov (%eax),%eax mov (%ecx),%eax
But this is still an improvement and for bcheck we need level=1 for
which the code is good.
For the tests I had to force gcc use -O0 to not inline the functions.
And -fno-omit-frame-pointer just in case.
If someone knows how to improve the generated code - help is
appreciated.
Thanks,
Kirill
Cc: Michael Matz <matz@suse.de>
Cc: Shinichiro Hamaji <shinichiro.hamaji@gmail.com>
2012-11-15 07:31:49 +08:00
|
|
|
int level;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
skip('(');
|
2015-11-18 03:09:35 +08:00
|
|
|
if (tok != TOK_CINT) {
|
2015-03-07 05:01:14 +08:00
|
|
|
tcc_error("%s only takes positive integers",
|
|
|
|
tok1 == TOK_builtin_return_address ?
|
|
|
|
"__builtin_return_address" :
|
|
|
|
"__builtin_frame_address");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2015-11-18 03:09:35 +08:00
|
|
|
level = (uint32_t)tokc.i;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
skip(')');
|
|
|
|
type.t = VT_VOID;
|
|
|
|
mk_pointer(&type);
|
Add support for __builtin_frame_address(level)
Continuing d6072d37 (Add __builtin_frame_address(0)) implement
__builtin_frame_address for levels greater than zero, in order for
tinycc to be able to compile its own lib/bcheck.c after
cffb7af9 (lib/bcheck: Prevent __bound_local_new / __bound_local_delete
from being miscompiled).
I'm new to the internals, and used the most simple way to do it.
Generated code is not very good for levels >= 2, compare
gcc tcc
level=0 mov %ebp,%eax lea 0x0(%ebp),%eax
level=1 mov 0x0(%ebp),%eax mov 0x0(%ebp),%eax
level=2 mov 0x0(%ebp),%eax mov 0x0(%ebp),%eax
mov (%eax),%eax mov %eax,-0x10(%ebp)
mov -0x10(%ebp),%eax
mov (%eax),%eax
level=3 mov 0x0(%ebp),%eax mov 0x0(%ebp),%eax
mov (%eax),%eax mov (%eax),%ecx
mov (%eax),%eax mov (%ecx),%eax
But this is still an improvement and for bcheck we need level=1 for
which the code is good.
For the tests I had to force gcc use -O0 to not inline the functions.
And -fno-omit-frame-pointer just in case.
If someone knows how to improve the generated code - help is
appreciated.
Thanks,
Kirill
Cc: Michael Matz <matz@suse.de>
Cc: Shinichiro Hamaji <shinichiro.hamaji@gmail.com>
2012-11-15 07:31:49 +08:00
|
|
|
vset(&type, VT_LOCAL, 0); /* local frame */
|
|
|
|
while (level--) {
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
indir(); /* -> parent frame */
|
|
|
|
}
|
2015-03-07 05:01:14 +08:00
|
|
|
if (tok1 == TOK_builtin_return_address) {
|
|
|
|
// assume return address is just above frame pointer on stack
|
|
|
|
vpushi(PTR_SIZE);
|
|
|
|
gen_op('+');
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
indir();
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-04-24 09:19:15 +08:00
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
case TOK_builtin_va_start:
|
2017-02-27 09:22:28 +08:00
|
|
|
parse_builtin_params(0, "ee");
|
2017-05-07 18:41:29 +08:00
|
|
|
r = vtop->r & VT_VALMASK;
|
|
|
|
if (r == VT_LLOCAL)
|
|
|
|
r = VT_LOCAL;
|
|
|
|
if (r != VT_LOCAL)
|
|
|
|
tcc_error("__builtin_va_start expects a local variable");
|
|
|
|
vtop->r = r;
|
2017-02-27 09:22:28 +08:00
|
|
|
vtop->type = char_pointer_type;
|
|
|
|
vtop->c.i += 8;
|
|
|
|
vstore();
|
2013-04-24 09:19:15 +08:00
|
|
|
break;
|
|
|
|
#else
|
2010-12-28 18:32:40 +08:00
|
|
|
case TOK_builtin_va_arg_types:
|
2017-02-27 09:22:28 +08:00
|
|
|
parse_builtin_params(0, "t");
|
|
|
|
vpushi(classify_x86_64_va_arg(&vtop->type));
|
|
|
|
vswap();
|
|
|
|
vpop();
|
2010-12-28 18:32:40 +08:00
|
|
|
break;
|
2013-04-24 09:19:15 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
#endif
|
2015-02-14 02:58:31 +08:00
|
|
|
|
|
|
|
#ifdef TCC_TARGET_ARM64
|
|
|
|
case TOK___va_start: {
|
2017-02-27 09:22:28 +08:00
|
|
|
parse_builtin_params(0, "ee");
|
2015-02-14 02:58:31 +08:00
|
|
|
//xx check types
|
|
|
|
gen_va_start();
|
|
|
|
vpushi(0);
|
|
|
|
vtop->type.t = VT_VOID;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TOK___va_arg: {
|
2017-02-27 09:22:28 +08:00
|
|
|
parse_builtin_params(0, "et");
|
|
|
|
type = vtop->type;
|
|
|
|
vpop();
|
2015-02-14 02:58:31 +08:00
|
|
|
//xx check types
|
|
|
|
gen_va_arg(&type);
|
|
|
|
vtop->type = type;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-08 08:10:44 +08:00
|
|
|
case TOK___arm64_clear_cache: {
|
2017-02-27 09:22:28 +08:00
|
|
|
parse_builtin_params(0, "ee");
|
2015-03-08 08:10:44 +08:00
|
|
|
gen_clear_cache();
|
|
|
|
vpushi(0);
|
|
|
|
vtop->type.t = VT_VOID;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-14 02:58:31 +08:00
|
|
|
#endif
|
2015-03-10 23:23:00 +08:00
|
|
|
/* pre operations */
|
2009-05-06 02:18:10 +08:00
|
|
|
case TOK_INC:
|
|
|
|
case TOK_DEC:
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
unary();
|
|
|
|
inc(0, t);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
next();
|
|
|
|
unary();
|
2014-01-04 12:56:14 +08:00
|
|
|
t = vtop->type.t & VT_BTYPE;
|
2015-07-30 04:53:57 +08:00
|
|
|
if (is_float(t)) {
|
2014-01-12 11:44:27 +08:00
|
|
|
/* In IEEE negate(x) isn't subtract(0,x), but rather
|
2015-07-30 04:53:57 +08:00
|
|
|
subtract(-0, x). */
|
|
|
|
vpush(&vtop->type);
|
|
|
|
if (t == VT_FLOAT)
|
2017-02-09 02:45:31 +08:00
|
|
|
vtop->c.f = -1.0 * 0.0;
|
2015-07-30 04:53:57 +08:00
|
|
|
else if (t == VT_DOUBLE)
|
2017-02-09 02:45:31 +08:00
|
|
|
vtop->c.d = -1.0 * 0.0;
|
2015-07-30 04:53:57 +08:00
|
|
|
else
|
2017-02-09 02:45:31 +08:00
|
|
|
vtop->c.ld = -1.0 * 0.0;
|
2015-07-30 04:53:57 +08:00
|
|
|
} else
|
|
|
|
vpushi(0);
|
|
|
|
vswap();
|
|
|
|
gen_op('-');
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
|
|
|
case TOK_LAND:
|
|
|
|
if (!gnu_ext)
|
|
|
|
goto tok_identifier;
|
|
|
|
next();
|
|
|
|
/* allow to take the address of a label */
|
|
|
|
if (tok < TOK_UIDENT)
|
|
|
|
expect("label identifier");
|
|
|
|
s = label_find(tok);
|
|
|
|
if (!s) {
|
|
|
|
s = label_push(&global_label_stack, tok, LABEL_FORWARD);
|
|
|
|
} else {
|
|
|
|
if (s->r == LABEL_DECLARED)
|
|
|
|
s->r = LABEL_FORWARD;
|
|
|
|
}
|
|
|
|
if (!s->type.t) {
|
|
|
|
s->type.t = VT_VOID;
|
|
|
|
mk_pointer(&s->type);
|
|
|
|
s->type.t |= VT_STATIC;
|
|
|
|
}
|
2014-04-05 02:18:39 +08:00
|
|
|
vpushsym(&s->type, s);
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
break;
|
2017-02-27 09:22:28 +08:00
|
|
|
|
2016-12-09 18:42:41 +08:00
|
|
|
case TOK_GENERIC:
|
|
|
|
{
|
|
|
|
CType controlling_type;
|
|
|
|
int has_default = 0;
|
|
|
|
int has_match = 0;
|
|
|
|
int learn = 0;
|
|
|
|
TokenString *str = NULL;
|
2018-11-13 19:51:16 +08:00
|
|
|
int saved_const_wanted = const_wanted;
|
2016-12-09 18:42:41 +08:00
|
|
|
|
|
|
|
next();
|
|
|
|
skip('(');
|
2018-11-13 19:51:16 +08:00
|
|
|
const_wanted = 0;
|
2017-07-21 04:21:27 +08:00
|
|
|
expr_type(&controlling_type, expr_eq);
|
mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
this case is usefull as with -funsigned-char, 'char *' are unsigned
* change VT_LONG so it's now a qualifier
VT_LONG are never use for code generation, but only durring parsing state,
in _Generic we need to be able to make diference between
'long' and 'long long'
So VT_LONG is now use as a type qualifier, it's old behaviour is still
here, but we can keep trace of what was a long and what wasn't
* add TOK_CLONG and TOK_CULONG
tcc was directly converting value like '7171L' into TOK_CLLONG or
TOK_CINT depending of the machine architecture.
because of that, we was unable to make diference between a long and a
long long, which doesn't work with _Generic.
So now 7171L is a TOK_CLONG, and we can handle _Generic properly
* check that _Generic can make diference between long and long long
* uncomment "type match twice" as it should now pass tests on any platforms
* add inside_generic global
the point of this variable is to use VT_LONG in comparaison only
when we are evaluating a _Generic.
problem is with my lastest patchs tcc can now make the diference between
a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
break when stdint.h and stddef.h are include together.
Another solution woud be to modifie include/stddef.h so it define uint64_t as
unsigned long int when processor is 64 bit, but this could break some
legacy code, so for now, VT_LONG are use only inside generc.
* check that _Generic parse first argument correctly
* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-10 23:44:53 +08:00
|
|
|
controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);
|
2018-03-16 07:26:16 +08:00
|
|
|
if ((controlling_type.t & VT_BTYPE) == VT_FUNC)
|
|
|
|
mk_pointer(&controlling_type);
|
2018-11-13 19:51:16 +08:00
|
|
|
const_wanted = saved_const_wanted;
|
2016-12-09 18:42:41 +08:00
|
|
|
for (;;) {
|
|
|
|
learn = 0;
|
|
|
|
skip(',');
|
|
|
|
if (tok == TOK_DEFAULT) {
|
|
|
|
if (has_default)
|
|
|
|
tcc_error("too many 'default'");
|
2017-07-21 04:21:27 +08:00
|
|
|
has_default = 1;
|
|
|
|
if (!has_match)
|
2016-12-09 18:42:41 +08:00
|
|
|
learn = 1;
|
|
|
|
next();
|
|
|
|
} else {
|
2017-07-21 04:21:27 +08:00
|
|
|
AttributeDef ad_tmp;
|
2016-12-09 18:42:41 +08:00
|
|
|
int itmp;
|
2017-07-21 04:21:27 +08:00
|
|
|
CType cur_type;
|
2016-12-09 18:42:41 +08:00
|
|
|
parse_btype(&cur_type, &ad_tmp);
|
|
|
|
type_decl(&cur_type, &ad_tmp, &itmp, TYPE_ABSTRACT);
|
|
|
|
if (compare_types(&controlling_type, &cur_type, 0)) {
|
|
|
|
if (has_match) {
|
mutiples fix for _Generic
* check that _Generic don't match unsigned char * with char *
this case is usefull as with -funsigned-char, 'char *' are unsigned
* change VT_LONG so it's now a qualifier
VT_LONG are never use for code generation, but only durring parsing state,
in _Generic we need to be able to make diference between
'long' and 'long long'
So VT_LONG is now use as a type qualifier, it's old behaviour is still
here, but we can keep trace of what was a long and what wasn't
* add TOK_CLONG and TOK_CULONG
tcc was directly converting value like '7171L' into TOK_CLLONG or
TOK_CINT depending of the machine architecture.
because of that, we was unable to make diference between a long and a
long long, which doesn't work with _Generic.
So now 7171L is a TOK_CLONG, and we can handle _Generic properly
* check that _Generic can make diference between long and long long
* uncomment "type match twice" as it should now pass tests on any platforms
* add inside_generic global
the point of this variable is to use VT_LONG in comparaison only
when we are evaluating a _Generic.
problem is with my lastest patchs tcc can now make the diference between
a 'long long' and a 'long', but in 64 bit stddef.h typedef uint64_t as
typedef signed long long int int64_t and stdint.h as unsigned long int, so tcc
break when stdint.h and stddef.h are include together.
Another solution woud be to modifie include/stddef.h so it define uint64_t as
unsigned long int when processor is 64 bit, but this could break some
legacy code, so for now, VT_LONG are use only inside generc.
* check that _Generic parse first argument correctly
* check that _Generic evaluate correctly exresion like "f() / 2"
2017-07-10 23:44:53 +08:00
|
|
|
tcc_error("type match twice");
|
2016-12-09 18:42:41 +08:00
|
|
|
}
|
|
|
|
has_match = 1;
|
|
|
|
learn = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skip(':');
|
|
|
|
if (learn) {
|
2017-07-21 04:21:27 +08:00
|
|
|
if (str)
|
|
|
|
tok_str_free(str);
|
2016-12-09 18:42:41 +08:00
|
|
|
skip_or_save_block(&str);
|
|
|
|
} else {
|
|
|
|
skip_or_save_block(NULL);
|
|
|
|
}
|
2017-07-21 04:21:27 +08:00
|
|
|
if (tok == ')')
|
2016-12-09 18:42:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-07-21 04:21:27 +08:00
|
|
|
if (!str) {
|
|
|
|
char buf[60];
|
|
|
|
type_to_str(buf, sizeof buf, &controlling_type, NULL);
|
|
|
|
tcc_error("type '%s' does not match any association", buf);
|
2016-12-09 18:42:41 +08:00
|
|
|
}
|
|
|
|
begin_macro(str, 1);
|
|
|
|
next();
|
|
|
|
expr_eq();
|
2017-07-21 04:21:27 +08:00
|
|
|
if (tok != TOK_EOF)
|
|
|
|
expect(",");
|
2016-12-09 18:42:41 +08:00
|
|
|
end_macro();
|
2017-07-21 04:21:27 +08:00
|
|
|
next();
|
2016-12-09 18:42:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-05-06 08:19:00 +08:00
|
|
|
// special qnan , snan and infinity values
|
|
|
|
case TOK___NAN__:
|
2017-12-24 20:16:09 +08:00
|
|
|
n = 0x7fc00000;
|
|
|
|
special_math_val:
|
|
|
|
vpushi(n);
|
|
|
|
vtop->type.t = VT_FLOAT;
|
2010-05-06 08:19:00 +08:00
|
|
|
next();
|
|
|
|
break;
|
|
|
|
case TOK___SNAN__:
|
2017-12-24 20:16:09 +08:00
|
|
|
n = 0x7f800001;
|
|
|
|
goto special_math_val;
|
2010-05-06 08:19:00 +08:00
|
|
|
case TOK___INF__:
|
2017-12-24 20:16:09 +08:00
|
|
|
n = 0x7f800000;
|
|
|
|
goto special_math_val;
|
2010-05-06 08:19:00 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
default:
|
|
|
|
tok_identifier:
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t < TOK_UIDENT)
|
|
|
|
expect("identifier");
|
|
|
|
s = sym_find(t);
|
2017-11-30 22:15:22 +08:00
|
|
|
if (!s || IS_ASM_SYM(s)) {
|
2014-04-06 16:59:40 +08:00
|
|
|
const char *name = get_tok_str(t, NULL);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok != '(')
|
2014-04-06 16:59:40 +08:00
|
|
|
tcc_error("'%s' undeclared", name);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* for simple function calls, we tolerate undeclared
|
|
|
|
external reference to int() function */
|
2014-04-06 16:59:40 +08:00
|
|
|
if (tcc_state->warn_implicit_function_declaration
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
/* people must be warned about using undeclared WINAPI functions
|
|
|
|
(which usually start with uppercase letter) */
|
|
|
|
|| (name[0] >= 'A' && name[0] <= 'Z')
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
tcc_warning("implicit declaration of function '%s'", name);
|
2019-04-18 09:36:39 +08:00
|
|
|
s = external_global_sym(t, &func_old_type);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-02-14 01:23:43 +08:00
|
|
|
|
|
|
|
r = s->r;
|
|
|
|
/* A symbol that has a register is a local register variable,
|
|
|
|
which starts out as VT_LOCAL value. */
|
|
|
|
if ((r & VT_VALMASK) < VT_CONST)
|
|
|
|
r = (r & ~VT_VALMASK) | VT_LOCAL;
|
|
|
|
|
2011-04-10 13:52:25 +08:00
|
|
|
vset(&s->type, r, s->c);
|
2016-10-06 10:05:30 +08:00
|
|
|
/* Point to s as backpointer (even without r&VT_SYM).
|
|
|
|
Will be used by at least the x86 inline asm parser for
|
|
|
|
regvars. */
|
|
|
|
vtop->sym = s;
|
2017-07-09 18:38:25 +08:00
|
|
|
|
|
|
|
if (r & VT_SYM) {
|
2015-11-18 03:09:35 +08:00
|
|
|
vtop->c.i = 0;
|
2017-07-09 18:38:25 +08:00
|
|
|
} else if (r == VT_CONST && IS_ENUM_VAL(s->type.t)) {
|
|
|
|
vtop->c.i = s->enum_val;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* post operations */
|
|
|
|
while (1) {
|
|
|
|
if (tok == TOK_INC || tok == TOK_DEC) {
|
|
|
|
inc(1, tok);
|
|
|
|
next();
|
2015-09-25 06:44:23 +08:00
|
|
|
} else if (tok == '.' || tok == TOK_ARROW || tok == TOK_CDOUBLE) {
|
2019-04-11 06:30:41 +08:00
|
|
|
int qualifiers, cumofs = 0;
|
2015-07-30 04:53:57 +08:00
|
|
|
/* field */
|
|
|
|
if (tok == TOK_ARROW)
|
2009-05-06 02:18:10 +08:00
|
|
|
indir();
|
2009-06-16 04:26:44 +08:00
|
|
|
qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
|
2009-05-06 02:18:10 +08:00
|
|
|
test_lvalue();
|
|
|
|
gaddrof();
|
|
|
|
/* expect pointer on structure */
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
|
|
|
|
expect("struct or union");
|
2015-09-25 06:44:23 +08:00
|
|
|
if (tok == TOK_CDOUBLE)
|
|
|
|
expect("field name");
|
|
|
|
next();
|
2015-09-25 07:31:34 +08:00
|
|
|
if (tok == TOK_CINT || tok == TOK_CUINT)
|
|
|
|
expect("field name");
|
2019-04-11 06:30:41 +08:00
|
|
|
s = find_field(&vtop->type, tok, &cumofs);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (!s)
|
2015-11-20 18:22:56 +08:00
|
|
|
tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, &tokc));
|
2009-05-06 02:18:10 +08:00
|
|
|
/* add field offset to pointer */
|
|
|
|
vtop->type = char_pointer_type; /* change type to 'char *' */
|
2019-04-11 06:30:41 +08:00
|
|
|
vpushi(cumofs + s->c);
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op('+');
|
|
|
|
/* change type to field type, and set to lvalue */
|
|
|
|
vtop->type = s->type;
|
2009-06-16 04:26:44 +08:00
|
|
|
vtop->type.t |= qualifiers;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* an array is never an lvalue */
|
|
|
|
if (!(vtop->type.t & VT_ARRAY)) {
|
|
|
|
vtop->r |= lvalue_type(vtop->type.t);
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2009-05-06 02:18:10 +08:00
|
|
|
/* if bound checking, the referenced pointer must be checked */
|
2016-11-10 21:05:47 +08:00
|
|
|
if (tcc_state->do_bounds_check && (vtop->r & VT_VALMASK) != VT_LOCAL)
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop->r |= VT_MUSTBOUND;
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
} else if (tok == '[') {
|
|
|
|
next();
|
|
|
|
gexpr();
|
|
|
|
gen_op('+');
|
|
|
|
indir();
|
|
|
|
skip(']');
|
|
|
|
} else if (tok == '(') {
|
|
|
|
SValue ret;
|
|
|
|
Sym *sa;
|
2015-03-09 07:19:59 +08:00
|
|
|
int nb_args, ret_nregs, ret_align, regsize, variadic;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
/* function call */
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
|
|
|
|
/* pointer test (no array accepted) */
|
|
|
|
if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
|
|
|
|
vtop->type = *pointed_type(&vtop->type);
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
|
|
|
|
goto error_func;
|
|
|
|
} else {
|
|
|
|
error_func:
|
|
|
|
expect("function pointer");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vtop->r &= ~VT_LVAL; /* no lvalue */
|
|
|
|
}
|
|
|
|
/* get return type */
|
|
|
|
s = vtop->type.ref;
|
|
|
|
next();
|
|
|
|
sa = s->next; /* first parameter */
|
2017-02-14 01:23:43 +08:00
|
|
|
nb_args = regsize = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
ret.r2 = VT_CONST;
|
|
|
|
/* compute first implicit argument if a structure is returned */
|
|
|
|
if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
|
2017-07-09 18:34:11 +08:00
|
|
|
variadic = (s->f.func_type == FUNC_ELLIPSIS);
|
2015-07-30 04:57:41 +08:00
|
|
|
ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
|
|
|
|
&ret_align, ®size);
|
2013-12-16 22:38:10 +08:00
|
|
|
if (!ret_nregs) {
|
2013-04-19 00:27:34 +08:00
|
|
|
/* get some space for the returned structure */
|
|
|
|
size = type_size(&s->type, &align);
|
2015-02-14 02:58:31 +08:00
|
|
|
#ifdef TCC_TARGET_ARM64
|
|
|
|
/* On arm64, a small struct is return in registers.
|
|
|
|
It is much easier to write it to memory if we know
|
|
|
|
that we are allowed to write some extra bytes, so
|
|
|
|
round the allocated space up to a power of 2: */
|
|
|
|
if (size < 16)
|
|
|
|
while (size & (size - 1))
|
|
|
|
size = (size | (size - 1)) + 1;
|
|
|
|
#endif
|
2013-04-19 00:27:34 +08:00
|
|
|
loc = (loc - size) & -align;
|
|
|
|
ret.type = s->type;
|
|
|
|
ret.r = VT_LOCAL | VT_LVAL;
|
|
|
|
/* pass it as 'int' to avoid structure arg passing
|
|
|
|
problems */
|
|
|
|
vseti(VT_LOCAL, loc);
|
|
|
|
ret.c = vtop->c;
|
|
|
|
nb_args++;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2013-12-16 22:38:10 +08:00
|
|
|
ret_nregs = 1;
|
2013-04-19 00:27:34 +08:00
|
|
|
ret.type = s->type;
|
|
|
|
}
|
|
|
|
|
2013-12-16 22:38:10 +08:00
|
|
|
if (ret_nregs) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* return in register */
|
|
|
|
if (is_float(ret.type.t)) {
|
|
|
|
ret.r = reg_fret(ret.type.t);
|
2013-04-19 22:33:16 +08:00
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
|
|
|
|
ret.r2 = REG_QRET;
|
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2015-02-14 02:58:31 +08:00
|
|
|
#ifndef TCC_TARGET_ARM64
|
2013-04-19 22:33:16 +08:00
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
if ((ret.type.t & VT_BTYPE) == VT_QLONG)
|
|
|
|
#else
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((ret.type.t & VT_BTYPE) == VT_LLONG)
|
2013-04-19 22:33:16 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
ret.r2 = REG_LRET;
|
2015-02-14 02:58:31 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
ret.r = REG_IRET;
|
|
|
|
}
|
|
|
|
ret.c.i = 0;
|
|
|
|
}
|
|
|
|
if (tok != ')') {
|
|
|
|
for(;;) {
|
|
|
|
expr_eq();
|
|
|
|
gfunc_param_typed(s, sa);
|
|
|
|
nb_args++;
|
|
|
|
if (sa)
|
|
|
|
sa = sa->next;
|
|
|
|
if (tok == ')')
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sa)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("too few arguments to function");
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(')');
|
2016-12-19 00:23:33 +08:00
|
|
|
gfunc_call(nb_args);
|
2013-12-16 22:38:10 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* return value */
|
2013-12-16 22:38:10 +08:00
|
|
|
for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
|
2013-11-22 09:27:15 +08:00
|
|
|
vsetc(&ret.type, r, &ret.c);
|
|
|
|
vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
|
|
|
|
}
|
2013-12-16 22:38:10 +08:00
|
|
|
|
2013-04-19 00:27:34 +08:00
|
|
|
/* handle packed struct return */
|
2013-12-16 22:38:10 +08:00
|
|
|
if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
|
2013-11-22 09:27:15 +08:00
|
|
|
int addr, offset;
|
|
|
|
|
2013-04-19 00:27:34 +08:00
|
|
|
size = type_size(&s->type, &align);
|
2015-07-30 04:53:57 +08:00
|
|
|
/* We're writing whole regs often, make sure there's enough
|
|
|
|
space. Assume register size is power of 2. */
|
|
|
|
if (regsize > align)
|
|
|
|
align = regsize;
|
2013-04-19 00:27:34 +08:00
|
|
|
loc = (loc - size) & -align;
|
2013-04-30 06:33:34 +08:00
|
|
|
addr = loc;
|
2013-12-16 22:38:10 +08:00
|
|
|
offset = 0;
|
|
|
|
for (;;) {
|
2013-11-22 09:27:15 +08:00
|
|
|
vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
|
|
|
|
vswap();
|
|
|
|
vstore();
|
|
|
|
vtop--;
|
2013-12-16 22:38:10 +08:00
|
|
|
if (--ret_nregs == 0)
|
|
|
|
break;
|
2015-03-09 07:19:59 +08:00
|
|
|
offset += regsize;
|
2013-11-22 09:27:15 +08:00
|
|
|
}
|
2013-04-19 00:27:34 +08:00
|
|
|
vset(&s->type, VT_LOCAL | VT_LVAL, addr);
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void expr_prod(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
2010-10-25 22:40:30 +08:00
|
|
|
unary();
|
2009-05-06 02:18:10 +08:00
|
|
|
while (tok == '*' || tok == '/' || tok == '%') {
|
|
|
|
t = tok;
|
|
|
|
next();
|
2010-10-25 22:40:30 +08:00
|
|
|
unary();
|
2009-05-06 02:18:10 +08:00
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void expr_sum(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_prod();
|
|
|
|
while (tok == '+' || tok == '-') {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_prod();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_shift(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_sum();
|
|
|
|
while (tok == TOK_SHL || tok == TOK_SAR) {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_sum();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_cmp(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_shift();
|
|
|
|
while ((tok >= TOK_ULE && tok <= TOK_GT) ||
|
|
|
|
tok == TOK_ULT || tok == TOK_UGE) {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_shift();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_cmpeq(void)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
|
|
|
|
expr_cmp();
|
|
|
|
while (tok == TOK_EQ || tok == TOK_NE) {
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
expr_cmp();
|
|
|
|
gen_op(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_and(void)
|
|
|
|
{
|
|
|
|
expr_cmpeq();
|
|
|
|
while (tok == '&') {
|
|
|
|
next();
|
|
|
|
expr_cmpeq();
|
|
|
|
gen_op('&');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_xor(void)
|
|
|
|
{
|
|
|
|
expr_and();
|
|
|
|
while (tok == '^') {
|
|
|
|
next();
|
|
|
|
expr_and();
|
|
|
|
gen_op('^');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_or(void)
|
|
|
|
{
|
|
|
|
expr_xor();
|
|
|
|
while (tok == '|') {
|
|
|
|
next();
|
|
|
|
expr_xor();
|
|
|
|
gen_op('|');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_land(void)
|
|
|
|
{
|
|
|
|
expr_or();
|
|
|
|
if (tok == TOK_LAND) {
|
2016-09-27 03:21:42 +08:00
|
|
|
int t = 0;
|
|
|
|
for(;;) {
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_BOOL);
|
2016-09-27 03:21:42 +08:00
|
|
|
if (vtop->c.i) {
|
|
|
|
vpop();
|
|
|
|
} else {
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted++;
|
2016-09-27 03:21:42 +08:00
|
|
|
while (tok == TOK_LAND) {
|
|
|
|
next();
|
|
|
|
expr_or();
|
|
|
|
vpop();
|
|
|
|
}
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted--;
|
2016-09-27 03:21:42 +08:00
|
|
|
if (t)
|
|
|
|
gsym(t);
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_INT);
|
2016-09-27 03:21:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!t)
|
|
|
|
save_regs(1);
|
|
|
|
t = gvtst(1, t);
|
|
|
|
}
|
|
|
|
if (tok != TOK_LAND) {
|
|
|
|
if (t)
|
|
|
|
vseti(VT_JMPI, t);
|
|
|
|
else
|
|
|
|
vpushi(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
expr_or();
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void expr_lor(void)
|
|
|
|
{
|
|
|
|
expr_land();
|
|
|
|
if (tok == TOK_LOR) {
|
2016-09-27 03:21:42 +08:00
|
|
|
int t = 0;
|
|
|
|
for(;;) {
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_BOOL);
|
2016-09-27 03:21:42 +08:00
|
|
|
if (!vtop->c.i) {
|
|
|
|
vpop();
|
|
|
|
} else {
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted++;
|
2016-09-27 03:21:42 +08:00
|
|
|
while (tok == TOK_LOR) {
|
|
|
|
next();
|
|
|
|
expr_land();
|
|
|
|
vpop();
|
|
|
|
}
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted--;
|
2016-09-27 03:21:42 +08:00
|
|
|
if (t)
|
|
|
|
gsym(t);
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_INT);
|
2016-09-27 03:21:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!t)
|
|
|
|
save_regs(1);
|
|
|
|
t = gvtst(0, t);
|
|
|
|
}
|
|
|
|
if (tok != TOK_LOR) {
|
|
|
|
if (t)
|
|
|
|
vseti(VT_JMP, t);
|
|
|
|
else
|
|
|
|
vpushi(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
expr_land();
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 11:23:57 +08:00
|
|
|
/* Assuming vtop is a value used in a conditional context
|
|
|
|
(i.e. compared with zero) return 0 if it's false, 1 if
|
|
|
|
true and -1 if it can't be statically determined. */
|
|
|
|
static int condition_3way(void)
|
|
|
|
{
|
|
|
|
int c = -1;
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
|
2017-07-09 18:34:11 +08:00
|
|
|
(!(vtop->r & VT_SYM) || !vtop->sym->a.weak)) {
|
2016-09-04 11:23:57 +08:00
|
|
|
vdup();
|
2017-07-09 18:34:11 +08:00
|
|
|
gen_cast_s(VT_BOOL);
|
2016-09-04 11:23:57 +08:00
|
|
|
c = vtop->c.i;
|
|
|
|
vpop();
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2010-10-25 22:40:30 +08:00
|
|
|
static void expr_cond(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2016-12-19 01:55:55 +08:00
|
|
|
int tt, u, r1, r2, rc, t1, t2, bt1, bt2, islv, c, g;
|
2009-05-06 02:18:10 +08:00
|
|
|
SValue sv;
|
|
|
|
CType type, type1, type2;
|
|
|
|
|
2015-11-21 07:33:49 +08:00
|
|
|
expr_lor();
|
2015-11-20 08:24:46 +08:00
|
|
|
if (tok == '?') {
|
|
|
|
next();
|
2016-09-04 11:23:57 +08:00
|
|
|
c = condition_3way();
|
2016-12-19 01:55:55 +08:00
|
|
|
g = (tok == ':' && gnu_ext);
|
|
|
|
if (c < 0) {
|
|
|
|
/* needed to avoid having different registers saved in
|
|
|
|
each branch */
|
|
|
|
if (is_float(vtop->type.t)) {
|
|
|
|
rc = RC_FLOAT;
|
2009-05-06 02:18:10 +08:00
|
|
|
#ifdef TCC_TARGET_X86_64
|
2016-12-19 01:55:55 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
rc = RC_ST0;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-12-19 01:55:55 +08:00
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
rc = RC_INT;
|
|
|
|
gv(rc);
|
2016-12-19 07:27:08 +08:00
|
|
|
save_regs(1);
|
2016-12-19 01:55:55 +08:00
|
|
|
if (g)
|
2009-05-06 02:18:10 +08:00
|
|
|
gv_dup();
|
2016-12-19 01:55:55 +08:00
|
|
|
tt = gvtst(1, 0);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!g)
|
|
|
|
vpop();
|
|
|
|
tt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1) {
|
|
|
|
if (c == 0)
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted++;
|
2016-12-19 01:55:55 +08:00
|
|
|
if (!g)
|
2009-05-06 02:18:10 +08:00
|
|
|
gexpr();
|
2016-12-19 01:55:55 +08:00
|
|
|
|
2018-12-01 06:43:30 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
|
2018-11-29 19:43:01 +08:00
|
|
|
mk_pointer(&vtop->type);
|
2018-12-01 06:43:30 +08:00
|
|
|
type1 = vtop->type;
|
2009-05-06 02:18:10 +08:00
|
|
|
sv = *vtop; /* save value to handle it later */
|
|
|
|
vtop--; /* no vpop so that FP stack is not flushed */
|
|
|
|
skip(':');
|
2016-12-19 01:55:55 +08:00
|
|
|
|
|
|
|
u = 0;
|
|
|
|
if (c < 0)
|
|
|
|
u = gjmp(0);
|
2009-05-06 02:18:10 +08:00
|
|
|
gsym(tt);
|
2016-12-19 01:58:33 +08:00
|
|
|
|
|
|
|
if (c == 0)
|
|
|
|
nocode_wanted--;
|
2016-12-19 01:55:55 +08:00
|
|
|
if (c == 1)
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted++;
|
2010-10-25 22:40:30 +08:00
|
|
|
expr_cond();
|
2016-12-19 01:58:33 +08:00
|
|
|
if (c == 1)
|
|
|
|
nocode_wanted--;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
2018-12-01 06:43:30 +08:00
|
|
|
if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
|
2018-11-29 19:43:01 +08:00
|
|
|
mk_pointer(&vtop->type);
|
2018-12-01 06:43:30 +08:00
|
|
|
type2=vtop->type;
|
2009-05-06 02:18:10 +08:00
|
|
|
t1 = type1.t;
|
|
|
|
bt1 = t1 & VT_BTYPE;
|
|
|
|
t2 = type2.t;
|
|
|
|
bt2 = t2 & VT_BTYPE;
|
2017-07-09 18:34:11 +08:00
|
|
|
type.ref = NULL;
|
|
|
|
|
2018-11-28 17:22:37 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* cast operands to correct type according to ISOC rules */
|
2018-11-29 20:40:48 +08:00
|
|
|
if (bt1 == VT_VOID || bt2 == VT_VOID) {
|
|
|
|
type.t = VT_VOID; /* NOTE: as an extension, we accept void on only one side */
|
2018-12-01 06:43:30 +08:00
|
|
|
} else if (is_float(bt1) || is_float(bt2)) {
|
2009-05-06 02:18:10 +08:00
|
|
|
if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
|
|
|
|
type.t = VT_LDOUBLE;
|
2016-12-19 01:55:55 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
|
|
|
|
type.t = VT_DOUBLE;
|
|
|
|
} else {
|
|
|
|
type.t = VT_FLOAT;
|
|
|
|
}
|
|
|
|
} else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
|
|
|
|
/* cast to biggest op */
|
2017-09-25 00:57:48 +08:00
|
|
|
type.t = VT_LLONG | VT_LONG;
|
|
|
|
if (bt1 == VT_LLONG)
|
|
|
|
type.t &= t1;
|
|
|
|
if (bt2 == VT_LLONG)
|
|
|
|
type.t &= t2;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* convert to unsigned if it does not fit in a long long */
|
bitfields: promote to signed int
For integer promotion with for example arithmetics or
expr_cond (x ? y : z), integral types need to be promoted
to signed if they fit.
According to latest standards, this also applies to bit-field
types taking into account their specific width.
In tcc, VT_BITFIELD set means width < original type width
Field-widths between 33 and 63 are promoted to signed long long
accordingly.
struct { unsigned long long ullb:35; } s = { 1 };
#define X (s.ullb - 2)
int main (void)
{
long long Y = X;
printf("%d %016llx %016llx\n", X < 0, -X, -Y);
return 0;
}
Results:
GCC 4.7 : 0 0000000000000001 FFFFFFF800000001
MSVC : 1 0000000000000001 0000000000000001
TCC : 1 0000000000000001 0000000000000001
Also, gcc would promote long long bitfields of size < 32
to int as well. Example:
struct { unsigned long long x:20; } t = { 123 };
/* with gcc: */ printf("%d %d\n", t.x, 456);
/* with tcc: */ printf("%lld %d\n", t.x, 456);
2017-05-10 00:36:24 +08:00
|
|
|
if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED) ||
|
|
|
|
(t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_LLONG | VT_UNSIGNED))
|
2009-05-06 02:18:10 +08:00
|
|
|
type.t |= VT_UNSIGNED;
|
|
|
|
} else if (bt1 == VT_PTR || bt2 == VT_PTR) {
|
2018-11-28 17:22:37 +08:00
|
|
|
/* http://port70.net/~nsz/c/c99/n1256.html#6.5.15p6 */
|
|
|
|
/* If one is a null ptr constant the result type
|
|
|
|
is the other. */
|
|
|
|
if (is_null_pointer (vtop)) type = type1;
|
|
|
|
else if (is_null_pointer (&sv)) type = type2;
|
2018-12-01 06:43:30 +08:00
|
|
|
else if (bt1 != bt2)
|
|
|
|
tcc_error("incompatible types in conditional expressions");
|
|
|
|
else {
|
|
|
|
CType *pt1 = pointed_type(&type1);
|
|
|
|
CType *pt2 = pointed_type(&type2);
|
|
|
|
int pbt1 = pt1->t & VT_BTYPE;
|
|
|
|
int pbt2 = pt2->t & VT_BTYPE;
|
|
|
|
int newquals, copied = 0;
|
|
|
|
/* pointers to void get preferred, otherwise the
|
|
|
|
pointed to types minus qualifs should be compatible */
|
|
|
|
type = (pbt1 == VT_VOID) ? type1 : type2;
|
|
|
|
if (pbt1 != VT_VOID && pbt2 != VT_VOID) {
|
|
|
|
if(!compare_types(pt1, pt2, 1/*unqualif*/))
|
2018-11-28 17:22:37 +08:00
|
|
|
tcc_warning("pointer type mismatch in conditional expression\n");
|
|
|
|
}
|
2018-12-01 06:43:30 +08:00
|
|
|
/* combine qualifs */
|
|
|
|
newquals = ((pt1->t | pt2->t) & (VT_CONSTANT | VT_VOLATILE));
|
|
|
|
if ((~pointed_type(&type)->t & (VT_CONSTANT | VT_VOLATILE))
|
|
|
|
& newquals)
|
|
|
|
{
|
|
|
|
/* copy the pointer target symbol */
|
|
|
|
type.ref = sym_push(SYM_FIELD, &type.ref->type,
|
|
|
|
0, type.ref->c);
|
|
|
|
copied = 1;
|
|
|
|
pointed_type(&type)->t |= newquals;
|
|
|
|
}
|
|
|
|
/* pointers to incomplete arrays get converted to
|
|
|
|
pointers to completed ones if possible */
|
|
|
|
if (pt1->t & VT_ARRAY
|
|
|
|
&& pt2->t & VT_ARRAY
|
|
|
|
&& pointed_type(&type)->ref->c < 0
|
|
|
|
&& (pt1->ref->c > 0 || pt2->ref->c > 0))
|
|
|
|
{
|
|
|
|
if (!copied)
|
|
|
|
type.ref = sym_push(SYM_FIELD, &type.ref->type,
|
|
|
|
0, type.ref->c);
|
|
|
|
pointed_type(&type)->ref =
|
|
|
|
sym_push(SYM_FIELD, &pointed_type(&type)->ref->type,
|
|
|
|
0, pointed_type(&type)->ref->c);
|
2018-11-28 17:22:37 +08:00
|
|
|
pointed_type(&type)->ref->c =
|
2018-12-01 06:43:30 +08:00
|
|
|
0 < pt1->ref->c ? pt1->ref->c : pt2->ref->c;
|
|
|
|
}
|
2018-11-28 17:22:37 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
|
|
|
|
/* XXX: test structure compatibility */
|
2012-04-15 05:50:21 +08:00
|
|
|
type = bt1 == VT_STRUCT ? type1 : type2;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
/* integer operations */
|
2017-09-25 00:57:48 +08:00
|
|
|
type.t = VT_INT | (VT_LONG & (t1 | t2));
|
2009-05-06 02:18:10 +08:00
|
|
|
/* convert to unsigned if it does not fit in an integer */
|
bitfields: promote to signed int
For integer promotion with for example arithmetics or
expr_cond (x ? y : z), integral types need to be promoted
to signed if they fit.
According to latest standards, this also applies to bit-field
types taking into account their specific width.
In tcc, VT_BITFIELD set means width < original type width
Field-widths between 33 and 63 are promoted to signed long long
accordingly.
struct { unsigned long long ullb:35; } s = { 1 };
#define X (s.ullb - 2)
int main (void)
{
long long Y = X;
printf("%d %016llx %016llx\n", X < 0, -X, -Y);
return 0;
}
Results:
GCC 4.7 : 0 0000000000000001 FFFFFFF800000001
MSVC : 1 0000000000000001 0000000000000001
TCC : 1 0000000000000001 0000000000000001
Also, gcc would promote long long bitfields of size < 32
to int as well. Example:
struct { unsigned long long x:20; } t = { 123 };
/* with gcc: */ printf("%d %d\n", t.x, 456);
/* with tcc: */ printf("%lld %d\n", t.x, 456);
2017-05-10 00:36:24 +08:00
|
|
|
if ((t1 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED) ||
|
|
|
|
(t2 & (VT_BTYPE | VT_UNSIGNED | VT_BITFIELD)) == (VT_INT | VT_UNSIGNED))
|
2009-05-06 02:18:10 +08:00
|
|
|
type.t |= VT_UNSIGNED;
|
|
|
|
}
|
2016-03-13 10:32:18 +08:00
|
|
|
/* keep structs lvalue by transforming `(expr ? a : b)` to `*(expr ? &a : &b)` so
|
|
|
|
that `(expr ? a : b).mem` does not error with "lvalue expected" */
|
|
|
|
islv = (vtop->r & VT_LVAL) && (sv.r & VT_LVAL) && VT_STRUCT == (type.t & VT_BTYPE);
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* now we convert second operand */
|
2016-12-19 01:55:55 +08:00
|
|
|
if (c != 1) {
|
|
|
|
gen_cast(&type);
|
|
|
|
if (islv) {
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
gaddrof();
|
|
|
|
} else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
|
|
|
|
gaddrof();
|
2016-03-13 10:32:18 +08:00
|
|
|
}
|
2016-12-19 01:55:55 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
rc = RC_INT;
|
|
|
|
if (is_float(type.t)) {
|
|
|
|
rc = RC_FLOAT;
|
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
|
|
|
|
rc = RC_ST0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else if ((type.t & VT_BTYPE) == VT_LLONG) {
|
|
|
|
/* for long longs, we use fixed registers to avoid having
|
|
|
|
to handle a complicated move */
|
2016-12-19 01:55:55 +08:00
|
|
|
rc = RC_IRET;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-12-19 01:55:55 +08:00
|
|
|
|
|
|
|
tt = r2 = 0;
|
|
|
|
if (c < 0) {
|
|
|
|
r2 = gv(rc);
|
|
|
|
tt = gjmp(0);
|
|
|
|
}
|
|
|
|
gsym(u);
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* this is horrible, but we must also convert first
|
|
|
|
operand */
|
2016-12-19 05:05:42 +08:00
|
|
|
if (c != 0) {
|
|
|
|
*vtop = sv;
|
2016-12-19 01:55:55 +08:00
|
|
|
gen_cast(&type);
|
|
|
|
if (islv) {
|
|
|
|
mk_pointer(&vtop->type);
|
|
|
|
gaddrof();
|
|
|
|
} else if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
|
|
|
|
gaddrof();
|
|
|
|
}
|
|
|
|
|
2018-11-29 04:12:38 +08:00
|
|
|
if (c < 0 || islv) {
|
2016-12-19 01:55:55 +08:00
|
|
|
r1 = gv(rc);
|
|
|
|
move_reg(r2, r1, type.t);
|
|
|
|
vtop->r = r2;
|
2016-12-19 05:05:42 +08:00
|
|
|
gsym(tt);
|
|
|
|
if (islv)
|
|
|
|
indir();
|
2016-03-13 10:32:18 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-25 22:40:30 +08:00
|
|
|
static void expr_eq(void)
|
|
|
|
{
|
|
|
|
int t;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2010-10-25 22:40:30 +08:00
|
|
|
expr_cond();
|
|
|
|
if (tok == '=' ||
|
|
|
|
(tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
|
|
|
|
tok == TOK_A_XOR || tok == TOK_A_OR ||
|
|
|
|
tok == TOK_A_SHL || tok == TOK_A_SAR) {
|
|
|
|
test_lvalue();
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '=') {
|
|
|
|
expr_eq();
|
|
|
|
} else {
|
|
|
|
vdup();
|
|
|
|
expr_eq();
|
|
|
|
gen_op(t & 0x7f);
|
|
|
|
}
|
|
|
|
vstore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-20 08:53:49 +08:00
|
|
|
ST_FUNC void gexpr(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
expr_eq();
|
|
|
|
if (tok != ',')
|
|
|
|
break;
|
|
|
|
vpop();
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a constant expression and return value in vtop. */
|
|
|
|
static void expr_const1(void)
|
|
|
|
{
|
2016-12-19 01:58:33 +08:00
|
|
|
const_wanted++;
|
2017-07-21 04:21:27 +08:00
|
|
|
nocode_wanted++;
|
2010-10-25 22:40:30 +08:00
|
|
|
expr_cond();
|
2017-07-21 04:21:27 +08:00
|
|
|
nocode_wanted--;
|
2016-12-19 01:58:33 +08:00
|
|
|
const_wanted--;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse an integer constant and return its value. */
|
2016-11-06 12:02:11 +08:00
|
|
|
static inline int64_t expr_const64(void)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2016-11-06 12:02:11 +08:00
|
|
|
int64_t c;
|
2009-05-06 02:18:10 +08:00
|
|
|
expr_const1();
|
|
|
|
if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
|
|
|
|
expect("constant expression");
|
|
|
|
c = vtop->c.i;
|
|
|
|
vpop();
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:02:11 +08:00
|
|
|
/* parse an integer constant and return its value.
|
|
|
|
Complain if it doesn't fit 32bit (signed or unsigned). */
|
|
|
|
ST_FUNC int expr_const(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int64_t wc = expr_const64();
|
|
|
|
c = wc;
|
|
|
|
if (c != wc && (unsigned)c != wc)
|
|
|
|
tcc_error("constant exceeds 32 bit");
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* return the label token if current token is a label, otherwise
|
|
|
|
return zero */
|
|
|
|
static int is_label(void)
|
|
|
|
{
|
|
|
|
int last_tok;
|
|
|
|
|
|
|
|
/* fast test first */
|
|
|
|
if (tok < TOK_UIDENT)
|
|
|
|
return 0;
|
|
|
|
/* no need to save tokc because tok is an identifier */
|
|
|
|
last_tok = tok;
|
|
|
|
next();
|
|
|
|
if (tok == ':') {
|
|
|
|
return last_tok;
|
|
|
|
} else {
|
|
|
|
unget_tok(last_tok);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 02:45:31 +08:00
|
|
|
#ifndef TCC_TARGET_ARM64
|
|
|
|
static void gfunc_return(CType *func_type)
|
|
|
|
{
|
|
|
|
if ((func_type->t & VT_BTYPE) == VT_STRUCT) {
|
|
|
|
CType type, ret_type;
|
|
|
|
int ret_align, ret_nregs, regsize;
|
|
|
|
ret_nregs = gfunc_sret(func_type, func_var, &ret_type,
|
|
|
|
&ret_align, ®size);
|
|
|
|
if (0 == ret_nregs) {
|
|
|
|
/* if returning structure, must copy it to implicit
|
|
|
|
first pointer arg location */
|
|
|
|
type = *func_type;
|
|
|
|
mk_pointer(&type);
|
|
|
|
vset(&type, VT_LOCAL | VT_LVAL, func_vc);
|
|
|
|
indir();
|
|
|
|
vswap();
|
|
|
|
/* copy structure value to pointer */
|
|
|
|
vstore();
|
|
|
|
} else {
|
|
|
|
/* returning structure packed into registers */
|
|
|
|
int r, size, addr, align;
|
|
|
|
size = type_size(func_type,&align);
|
|
|
|
if ((vtop->r != (VT_LOCAL | VT_LVAL) ||
|
|
|
|
(vtop->c.i & (ret_align-1)))
|
|
|
|
&& (align & (ret_align-1))) {
|
|
|
|
loc = (loc - size) & -ret_align;
|
|
|
|
addr = loc;
|
|
|
|
type = *func_type;
|
|
|
|
vset(&type, VT_LOCAL | VT_LVAL, addr);
|
|
|
|
vswap();
|
|
|
|
vstore();
|
|
|
|
vpop();
|
|
|
|
vset(&ret_type, VT_LOCAL | VT_LVAL, addr);
|
|
|
|
}
|
|
|
|
vtop->type = ret_type;
|
|
|
|
if (is_float(ret_type.t))
|
|
|
|
r = rc_fret(ret_type.t);
|
|
|
|
else
|
|
|
|
r = RC_IRET;
|
|
|
|
|
|
|
|
if (ret_nregs == 1)
|
|
|
|
gv(r);
|
|
|
|
else {
|
|
|
|
for (;;) {
|
|
|
|
vdup();
|
|
|
|
gv(r);
|
|
|
|
vpop();
|
|
|
|
if (--ret_nregs == 0)
|
|
|
|
break;
|
|
|
|
/* We assume that when a structure is returned in multiple
|
|
|
|
registers, their classes are consecutive values of the
|
|
|
|
suite s(n) = 2^n */
|
|
|
|
r <<= 1;
|
|
|
|
vtop->c.i += regsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (is_float(func_type->t)) {
|
|
|
|
gv(rc_fret(func_type->t));
|
|
|
|
} else {
|
|
|
|
gv(RC_IRET);
|
|
|
|
}
|
|
|
|
vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-10-03 15:40:37 +08:00
|
|
|
static int case_cmp(const void *pa, const void *pb)
|
|
|
|
{
|
2016-11-06 12:02:11 +08:00
|
|
|
int64_t a = (*(struct case_t**) pa)->v1;
|
|
|
|
int64_t b = (*(struct case_t**) pb)->v1;
|
2016-10-03 15:40:37 +08:00
|
|
|
return a < b ? -1 : a > b;
|
|
|
|
}
|
2016-09-21 23:35:29 +08:00
|
|
|
|
2016-11-06 12:02:11 +08:00
|
|
|
static void gcase(struct case_t **base, int len, int *bsym)
|
2016-10-03 18:14:34 +08:00
|
|
|
{
|
|
|
|
struct case_t *p;
|
|
|
|
int e;
|
2016-11-06 12:02:11 +08:00
|
|
|
int ll = (vtop->type.t & VT_BTYPE) == VT_LLONG;
|
|
|
|
gv(RC_INT);
|
2016-10-11 16:29:56 +08:00
|
|
|
while (len > 4) {
|
|
|
|
/* binary search */
|
2016-10-03 18:14:34 +08:00
|
|
|
p = base[len/2];
|
2016-10-11 03:15:20 +08:00
|
|
|
vdup();
|
2016-11-06 12:02:11 +08:00
|
|
|
if (ll)
|
|
|
|
vpushll(p->v2);
|
|
|
|
else
|
|
|
|
vpushi(p->v2);
|
2016-10-03 18:14:34 +08:00
|
|
|
gen_op(TOK_LE);
|
|
|
|
e = gtst(1, 0);
|
2016-10-11 03:15:20 +08:00
|
|
|
vdup();
|
2016-11-06 12:02:11 +08:00
|
|
|
if (ll)
|
|
|
|
vpushll(p->v1);
|
|
|
|
else
|
|
|
|
vpushi(p->v1);
|
2016-10-03 18:14:34 +08:00
|
|
|
gen_op(TOK_GE);
|
2016-10-11 16:29:56 +08:00
|
|
|
gtst_addr(0, p->sym); /* v1 <= x <= v2 */
|
|
|
|
/* x < v1 */
|
2016-11-06 12:02:11 +08:00
|
|
|
gcase(base, len/2, bsym);
|
2016-10-03 18:14:34 +08:00
|
|
|
if (cur_switch->def_sym)
|
|
|
|
gjmp_addr(cur_switch->def_sym);
|
|
|
|
else
|
|
|
|
*bsym = gjmp(*bsym);
|
2016-10-11 16:29:56 +08:00
|
|
|
/* x > v2 */
|
2016-10-03 18:14:34 +08:00
|
|
|
gsym(e);
|
|
|
|
e = len/2 + 1;
|
2016-10-11 16:29:56 +08:00
|
|
|
base += e; len -= e;
|
|
|
|
}
|
|
|
|
/* linear scan */
|
|
|
|
while (len--) {
|
|
|
|
p = *base++;
|
|
|
|
vdup();
|
2016-11-06 12:02:11 +08:00
|
|
|
if (ll)
|
|
|
|
vpushll(p->v2);
|
|
|
|
else
|
|
|
|
vpushi(p->v2);
|
2016-10-11 16:29:56 +08:00
|
|
|
if (p->v1 == p->v2) {
|
|
|
|
gen_op(TOK_EQ);
|
|
|
|
gtst_addr(0, p->sym);
|
|
|
|
} else {
|
|
|
|
gen_op(TOK_LE);
|
|
|
|
e = gtst(1, 0);
|
|
|
|
vdup();
|
2016-11-06 12:02:11 +08:00
|
|
|
if (ll)
|
|
|
|
vpushll(p->v1);
|
|
|
|
else
|
|
|
|
vpushi(p->v1);
|
2016-10-11 16:29:56 +08:00
|
|
|
gen_op(TOK_GE);
|
|
|
|
gtst_addr(0, p->sym);
|
|
|
|
gsym(e);
|
|
|
|
}
|
2016-10-03 18:14:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 17:18:57 +08:00
|
|
|
static void block(int *bsym, Sym *bcl, int *csym, Sym *ccl, int is_expr)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2016-09-20 00:38:12 +08:00
|
|
|
int a, b, c, d, cond;
|
2016-05-05 16:39:09 +08:00
|
|
|
Sym *s;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
/* generate line number info */
|
2017-02-21 01:58:08 +08:00
|
|
|
if (tcc_state->do_debug)
|
|
|
|
tcc_debug_line(tcc_state);
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
if (is_expr) {
|
|
|
|
/* default return value is (void) */
|
|
|
|
vpushi(0);
|
|
|
|
vtop->type.t = VT_VOID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok == TOK_IF) {
|
|
|
|
/* if test */
|
2016-09-20 00:38:12 +08:00
|
|
|
int saved_nocode_wanted = nocode_wanted;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
gexpr();
|
|
|
|
skip(')');
|
2016-09-20 00:38:12 +08:00
|
|
|
cond = condition_3way();
|
2016-12-19 00:23:33 +08:00
|
|
|
if (cond == 1)
|
|
|
|
a = 0, vpop();
|
|
|
|
else
|
|
|
|
a = gvtst(1, 0);
|
|
|
|
if (cond == 0)
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted |= 0x20000000;
|
2019-04-29 17:18:57 +08:00
|
|
|
block(bsym, bcl, csym, ccl, 0);
|
2016-09-27 02:31:24 +08:00
|
|
|
if (cond != 1)
|
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2019-01-31 08:04:55 +08:00
|
|
|
if (tok == TOK_ELSE) {
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
d = gjmp(0);
|
|
|
|
gsym(a);
|
2016-12-19 00:23:33 +08:00
|
|
|
if (cond == 1)
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted |= 0x20000000;
|
2019-04-29 17:18:57 +08:00
|
|
|
block(bsym, bcl, csym, ccl, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
gsym(d); /* patch else jmp */
|
2016-09-27 02:31:24 +08:00
|
|
|
if (cond != 0)
|
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else
|
|
|
|
gsym(a);
|
|
|
|
} else if (tok == TOK_WHILE) {
|
2016-09-27 02:31:24 +08:00
|
|
|
int saved_nocode_wanted;
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted &= ~0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
d = ind;
|
2015-05-04 09:09:05 +08:00
|
|
|
vla_sp_restore();
|
2009-05-06 02:18:10 +08:00
|
|
|
skip('(');
|
|
|
|
gexpr();
|
|
|
|
skip(')');
|
2013-12-31 23:51:20 +08:00
|
|
|
a = gvtst(1, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
b = 0;
|
2019-01-30 04:32:38 +08:00
|
|
|
++local_scope;
|
2016-09-27 02:31:24 +08:00
|
|
|
saved_nocode_wanted = nocode_wanted;
|
2019-04-29 17:18:57 +08:00
|
|
|
block(&a, current_cleanups, &b, current_cleanups, 0);
|
2016-09-27 02:31:24 +08:00
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2019-01-30 04:32:38 +08:00
|
|
|
--local_scope;
|
2016-12-19 00:23:33 +08:00
|
|
|
gjmp_addr(d);
|
2009-05-06 02:18:10 +08:00
|
|
|
gsym(a);
|
|
|
|
gsym_addr(b, d);
|
|
|
|
} else if (tok == '{') {
|
2019-01-28 08:21:38 +08:00
|
|
|
Sym *llabel, *lcleanup;
|
2015-05-04 09:09:05 +08:00
|
|
|
int block_vla_sp_loc = vla_sp_loc, saved_vlas_in_scope = vlas_in_scope;
|
2019-01-28 08:21:38 +08:00
|
|
|
int lncleanups = ncleanups;
|
2013-04-30 06:33:34 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
/* record local declaration stack position */
|
|
|
|
s = local_stack;
|
|
|
|
llabel = local_label_stack;
|
2019-01-28 08:21:38 +08:00
|
|
|
lcleanup = current_cleanups;
|
2019-01-30 04:32:38 +08:00
|
|
|
++local_scope;
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* handle local labels declarations */
|
2019-01-10 01:20:15 +08:00
|
|
|
while (tok == TOK_LABEL) {
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
for(;;) {
|
|
|
|
if (tok < TOK_UIDENT)
|
|
|
|
expect("label identifier");
|
|
|
|
label_push(&local_label_stack, tok, LABEL_DECLARED);
|
|
|
|
next();
|
|
|
|
if (tok == ',') {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
skip(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (tok != '}') {
|
2017-04-16 02:05:39 +08:00
|
|
|
if ((a = is_label()))
|
|
|
|
unget_tok(a);
|
|
|
|
else
|
|
|
|
decl(VT_LOCAL);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok != '}') {
|
|
|
|
if (is_expr)
|
|
|
|
vpop();
|
2019-04-29 17:18:57 +08:00
|
|
|
block(bsym, bcl, csym, ccl, is_expr);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-20 17:55:22 +08:00
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
if (current_cleanups != lcleanup) {
|
|
|
|
int jmp = 0;
|
|
|
|
Sym *g, **pg;
|
|
|
|
|
|
|
|
for (pg = &pending_gotos; (g = *pg) && g->c > lncleanups;)
|
|
|
|
if (g->prev_tok->r & LABEL_FORWARD) {
|
|
|
|
Sym *pcl = g->next;
|
|
|
|
if (!jmp)
|
|
|
|
jmp = gjmp(0);
|
|
|
|
gsym(pcl->jnext);
|
|
|
|
try_call_scope_cleanup(lcleanup);
|
|
|
|
pcl->jnext = gjmp(0);
|
|
|
|
if (!lncleanups)
|
|
|
|
goto remove_pending;
|
|
|
|
g->c = lncleanups;
|
|
|
|
pg = &g->prev;
|
|
|
|
} else {
|
|
|
|
remove_pending:
|
|
|
|
*pg = g->prev;
|
|
|
|
sym_free(g);
|
|
|
|
}
|
|
|
|
gsym(jmp);
|
|
|
|
if (!nocode_wanted) {
|
|
|
|
try_call_scope_cleanup(lcleanup);
|
|
|
|
}
|
|
|
|
}
|
2018-12-20 17:55:22 +08:00
|
|
|
|
2019-01-28 08:21:38 +08:00
|
|
|
current_cleanups = lcleanup;
|
|
|
|
ncleanups = lncleanups;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* pop locally defined labels */
|
2017-07-11 04:25:11 +08:00
|
|
|
label_pop(&local_label_stack, llabel, is_expr);
|
2011-04-08 15:46:32 +08:00
|
|
|
/* pop locally defined symbols */
|
2019-01-30 04:32:38 +08:00
|
|
|
--local_scope;
|
2016-08-15 11:09:31 +08:00
|
|
|
/* In the is_expr case (a statement expression is finished here),
|
|
|
|
vtop might refer to symbols on the local_stack. Either via the
|
|
|
|
type or via vtop->sym. We can't pop those nor any that in turn
|
|
|
|
might be referred to. To make it easier we don't roll back
|
|
|
|
any symbols in that case; some upper level call to block() will
|
|
|
|
do that. We do have to remove such symbols from the lookup
|
|
|
|
tables, though. sym_pop will do that. */
|
|
|
|
sym_pop(&local_stack, s, is_expr);
|
|
|
|
|
2013-04-28 03:39:34 +08:00
|
|
|
/* Pop VLA frames and restore stack pointer if required */
|
2015-05-04 09:09:05 +08:00
|
|
|
if (vlas_in_scope > saved_vlas_in_scope) {
|
|
|
|
vla_sp_loc = saved_vlas_in_scope ? block_vla_sp_loc : vla_sp_root_loc;
|
|
|
|
vla_sp_restore();
|
2013-04-28 03:39:34 +08:00
|
|
|
}
|
2015-05-04 09:09:05 +08:00
|
|
|
vlas_in_scope = saved_vlas_in_scope;
|
2019-01-28 08:21:38 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
} else if (tok == TOK_RETURN) {
|
|
|
|
next();
|
|
|
|
if (tok != ';') {
|
|
|
|
gexpr();
|
|
|
|
gen_assign_cast(&func_vt);
|
2019-01-28 08:21:38 +08:00
|
|
|
try_call_scope_cleanup(NULL);
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
if ((func_vt.t & VT_BTYPE) == VT_VOID)
|
|
|
|
vtop--;
|
|
|
|
else
|
|
|
|
gfunc_return(&func_vt);
|
2018-12-20 17:55:22 +08:00
|
|
|
} else {
|
2019-01-28 08:21:38 +08:00
|
|
|
try_call_scope_cleanup(NULL);
|
2018-12-20 17:55:22 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(';');
|
2016-08-11 19:59:08 +08:00
|
|
|
/* jump unless last stmt in top-level block */
|
|
|
|
if (tok != '}' || local_scope != 1)
|
|
|
|
rsym = gjmp(rsym);
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted |= 0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (tok == TOK_BREAK) {
|
|
|
|
/* compute jump */
|
|
|
|
if (!bsym)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("cannot break");
|
2019-04-29 17:18:57 +08:00
|
|
|
try_call_scope_cleanup(bcl);
|
2009-05-06 02:18:10 +08:00
|
|
|
*bsym = gjmp(*bsym);
|
|
|
|
next();
|
|
|
|
skip(';');
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted |= 0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (tok == TOK_CONTINUE) {
|
|
|
|
/* compute jump */
|
|
|
|
if (!csym)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("cannot continue");
|
2019-04-29 17:18:57 +08:00
|
|
|
try_call_scope_cleanup(ccl);
|
2015-05-04 09:09:05 +08:00
|
|
|
vla_sp_restore_root();
|
2009-05-06 02:18:10 +08:00
|
|
|
*csym = gjmp(*csym);
|
|
|
|
next();
|
|
|
|
skip(';');
|
2019-01-31 07:37:49 +08:00
|
|
|
nocode_wanted |= 0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (tok == TOK_FOR) {
|
2010-08-21 19:39:12 +08:00
|
|
|
int e;
|
2016-09-27 02:31:24 +08:00
|
|
|
int saved_nocode_wanted;
|
2019-04-30 11:44:15 +08:00
|
|
|
Sym *lcleanup = current_cleanups;
|
|
|
|
int lncleanups = ncleanups;
|
|
|
|
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted &= ~0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
skip('(');
|
2011-03-09 07:19:54 +08:00
|
|
|
s = local_stack;
|
2019-01-30 04:32:38 +08:00
|
|
|
++local_scope;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok != ';') {
|
2011-03-09 05:36:04 +08:00
|
|
|
/* c99 for-loop init decl? */
|
2017-03-11 09:13:59 +08:00
|
|
|
if (!decl0(VT_LOCAL, 1, NULL)) {
|
2011-03-09 05:36:04 +08:00
|
|
|
/* no, regular for-loop init expr */
|
|
|
|
gexpr();
|
|
|
|
vpop();
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2010-08-21 19:39:12 +08:00
|
|
|
skip(';');
|
2009-05-06 02:18:10 +08:00
|
|
|
d = ind;
|
|
|
|
c = ind;
|
2015-05-04 09:09:05 +08:00
|
|
|
vla_sp_restore();
|
2009-05-06 02:18:10 +08:00
|
|
|
a = 0;
|
|
|
|
b = 0;
|
|
|
|
if (tok != ';') {
|
|
|
|
gexpr();
|
2013-12-31 23:51:20 +08:00
|
|
|
a = gvtst(1, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
if (tok != ')') {
|
|
|
|
e = gjmp(0);
|
|
|
|
c = ind;
|
2015-05-04 09:09:05 +08:00
|
|
|
vla_sp_restore();
|
2009-05-06 02:18:10 +08:00
|
|
|
gexpr();
|
|
|
|
vpop();
|
|
|
|
gjmp_addr(d);
|
|
|
|
gsym(e);
|
|
|
|
}
|
|
|
|
skip(')');
|
2016-09-27 02:31:24 +08:00
|
|
|
saved_nocode_wanted = nocode_wanted;
|
2019-04-29 17:18:57 +08:00
|
|
|
block(&a, current_cleanups, &b, current_cleanups, 0);
|
2016-09-27 02:31:24 +08:00
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2016-12-19 00:23:33 +08:00
|
|
|
gjmp_addr(c);
|
2009-05-06 02:18:10 +08:00
|
|
|
gsym(a);
|
|
|
|
gsym_addr(b, c);
|
2019-01-30 04:32:38 +08:00
|
|
|
--local_scope;
|
2019-04-30 11:44:15 +08:00
|
|
|
try_call_scope_cleanup(lcleanup);
|
|
|
|
ncleanups = lncleanups;
|
|
|
|
current_cleanups = lcleanup;
|
2016-08-15 11:09:31 +08:00
|
|
|
sym_pop(&local_stack, s, 0);
|
2016-05-05 16:39:09 +08:00
|
|
|
|
2015-07-30 04:53:57 +08:00
|
|
|
} else
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == TOK_DO) {
|
2016-09-27 02:31:24 +08:00
|
|
|
int saved_nocode_wanted;
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted &= ~0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
a = 0;
|
|
|
|
b = 0;
|
|
|
|
d = ind;
|
2015-05-04 09:09:05 +08:00
|
|
|
vla_sp_restore();
|
2016-09-27 02:31:24 +08:00
|
|
|
saved_nocode_wanted = nocode_wanted;
|
2019-04-29 17:18:57 +08:00
|
|
|
block(&a, current_cleanups, &b, current_cleanups, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(TOK_WHILE);
|
|
|
|
skip('(');
|
|
|
|
gsym(b);
|
2017-11-14 22:38:40 +08:00
|
|
|
if (b)
|
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2016-12-14 00:23:02 +08:00
|
|
|
gexpr();
|
2016-12-19 00:23:33 +08:00
|
|
|
c = gvtst(0, 0);
|
|
|
|
gsym_addr(c, d);
|
2016-12-14 00:23:02 +08:00
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(')');
|
|
|
|
gsym(a);
|
|
|
|
skip(';');
|
|
|
|
} else
|
|
|
|
if (tok == TOK_SWITCH) {
|
2016-09-21 23:35:29 +08:00
|
|
|
struct switch_t *saved, sw;
|
2016-09-27 02:31:24 +08:00
|
|
|
int saved_nocode_wanted = nocode_wanted;
|
2016-11-06 12:02:11 +08:00
|
|
|
SValue switchval;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
gexpr();
|
|
|
|
skip(')');
|
2016-11-06 12:02:11 +08:00
|
|
|
switchval = *vtop--;
|
2009-05-06 02:18:10 +08:00
|
|
|
a = 0;
|
|
|
|
b = gjmp(0); /* jump to first case */
|
2016-09-21 23:35:29 +08:00
|
|
|
sw.p = NULL; sw.n = 0; sw.def_sym = 0;
|
|
|
|
saved = cur_switch;
|
2016-10-11 16:29:56 +08:00
|
|
|
cur_switch = &sw;
|
2019-04-29 17:18:57 +08:00
|
|
|
block(&a, current_cleanups, csym, ccl, 0);
|
2016-09-27 02:31:24 +08:00
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2016-09-21 23:35:29 +08:00
|
|
|
a = gjmp(a); /* add implicit break */
|
2016-10-03 18:14:34 +08:00
|
|
|
/* case lookup */
|
2016-09-21 23:35:29 +08:00
|
|
|
gsym(b);
|
2016-12-19 00:23:33 +08:00
|
|
|
qsort(sw.p, sw.n, sizeof(void*), case_cmp);
|
|
|
|
for (b = 1; b < sw.n; b++)
|
|
|
|
if (sw.p[b - 1]->v2 >= sw.p[b]->v1)
|
|
|
|
tcc_error("duplicate case value");
|
|
|
|
/* Our switch table sorting is signed, so the compared
|
|
|
|
value needs to be as well when it's 64bit. */
|
|
|
|
if ((switchval.type.t & VT_BTYPE) == VT_LLONG)
|
|
|
|
switchval.type.t &= ~VT_UNSIGNED;
|
|
|
|
vpushv(&switchval);
|
|
|
|
gcase(sw.p, sw.n, &a);
|
|
|
|
vpop();
|
|
|
|
if (sw.def_sym)
|
|
|
|
gjmp_addr(sw.def_sym);
|
2016-10-06 00:34:17 +08:00
|
|
|
dynarray_reset(&sw.p, &sw.n);
|
2016-10-03 18:14:34 +08:00
|
|
|
cur_switch = saved;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* break label */
|
|
|
|
gsym(a);
|
|
|
|
} else
|
|
|
|
if (tok == TOK_CASE) {
|
2016-09-21 23:35:29 +08:00
|
|
|
struct case_t *cr = tcc_malloc(sizeof(struct case_t));
|
|
|
|
if (!cur_switch)
|
2009-05-06 02:18:10 +08:00
|
|
|
expect("switch");
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted &= ~0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
2016-11-06 12:02:11 +08:00
|
|
|
cr->v1 = cr->v2 = expr_const64();
|
2009-05-06 02:18:10 +08:00
|
|
|
if (gnu_ext && tok == TOK_DOTS) {
|
|
|
|
next();
|
2016-11-06 12:02:11 +08:00
|
|
|
cr->v2 = expr_const64();
|
2016-09-21 23:35:29 +08:00
|
|
|
if (cr->v2 < cr->v1)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("empty case range");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-09-21 23:35:29 +08:00
|
|
|
cr->sym = ind;
|
2017-02-14 01:23:43 +08:00
|
|
|
dynarray_add(&cur_switch->p, &cur_switch->n, cr);
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(':');
|
|
|
|
is_expr = 0;
|
|
|
|
goto block_after_label;
|
2015-07-30 04:53:57 +08:00
|
|
|
} else
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == TOK_DEFAULT) {
|
|
|
|
next();
|
|
|
|
skip(':');
|
2016-09-21 23:35:29 +08:00
|
|
|
if (!cur_switch)
|
2009-05-06 02:18:10 +08:00
|
|
|
expect("switch");
|
2016-09-21 23:35:29 +08:00
|
|
|
if (cur_switch->def_sym)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("too many 'default'");
|
2016-09-21 23:35:29 +08:00
|
|
|
cur_switch->def_sym = ind;
|
2009-05-06 02:18:10 +08:00
|
|
|
is_expr = 0;
|
|
|
|
goto block_after_label;
|
|
|
|
} else
|
|
|
|
if (tok == TOK_GOTO) {
|
|
|
|
next();
|
|
|
|
if (tok == '*' && gnu_ext) {
|
|
|
|
/* computed goto */
|
|
|
|
next();
|
|
|
|
gexpr();
|
|
|
|
if ((vtop->type.t & VT_BTYPE) != VT_PTR)
|
|
|
|
expect("pointer");
|
2016-12-19 00:23:33 +08:00
|
|
|
ggoto();
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (tok >= TOK_UIDENT) {
|
2018-12-20 17:55:22 +08:00
|
|
|
s = label_find(tok);
|
2019-01-28 08:21:38 +08:00
|
|
|
/* put forward definition if needed */
|
|
|
|
if (!s)
|
|
|
|
s = label_push(&global_label_stack, tok, LABEL_FORWARD);
|
|
|
|
else if (s->r == LABEL_DECLARED)
|
|
|
|
s->r = LABEL_FORWARD;
|
2018-12-20 17:55:22 +08:00
|
|
|
|
|
|
|
vla_sp_restore_root();
|
2019-01-28 08:21:38 +08:00
|
|
|
if (s->r & LABEL_FORWARD) {
|
|
|
|
/* start new goto chain for cleanups, linked via label->next */
|
|
|
|
if (current_cleanups) {
|
|
|
|
sym_push2(&pending_gotos, SYM_FIELD, 0, ncleanups);
|
|
|
|
pending_gotos->prev_tok = s;
|
|
|
|
s = sym_push2(&s->next, SYM_FIELD, 0, 0);
|
|
|
|
pending_gotos->next = s;
|
|
|
|
}
|
2018-12-20 17:55:22 +08:00
|
|
|
s->jnext = gjmp(s->jnext);
|
2019-01-28 08:21:38 +08:00
|
|
|
} else {
|
|
|
|
try_call_cleanup_goto(s->cleanupstate);
|
2018-12-20 17:55:22 +08:00
|
|
|
gjmp_addr(s->jnext);
|
2019-01-28 08:21:38 +08:00
|
|
|
}
|
2018-12-20 17:55:22 +08:00
|
|
|
next();
|
|
|
|
} else {
|
2009-05-06 02:18:10 +08:00
|
|
|
expect("label identifier");
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
} else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
|
|
|
|
asm_instr();
|
|
|
|
} else {
|
|
|
|
b = is_label();
|
|
|
|
if (b) {
|
|
|
|
/* label case */
|
2017-04-16 02:05:39 +08:00
|
|
|
next();
|
2009-05-06 02:18:10 +08:00
|
|
|
s = label_find(b);
|
|
|
|
if (s) {
|
|
|
|
if (s->r == LABEL_DEFINED)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("duplicate label '%s'", get_tok_str(s->v, NULL));
|
2009-05-06 02:18:10 +08:00
|
|
|
s->r = LABEL_DEFINED;
|
2019-01-28 08:21:38 +08:00
|
|
|
if (s->next) {
|
|
|
|
Sym *pcl; /* pending cleanup goto */
|
|
|
|
for (pcl = s->next; pcl; pcl = pcl->prev)
|
|
|
|
gsym(pcl->jnext);
|
|
|
|
sym_pop(&s->next, NULL, 0);
|
|
|
|
} else
|
|
|
|
gsym(s->jnext);
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
s = label_push(&global_label_stack, b, LABEL_DEFINED);
|
|
|
|
}
|
2009-07-19 04:05:58 +08:00
|
|
|
s->jnext = ind;
|
2019-01-28 08:21:38 +08:00
|
|
|
s->cleanupstate = current_cleanups;
|
2015-05-04 09:09:05 +08:00
|
|
|
vla_sp_restore();
|
2009-05-06 02:18:10 +08:00
|
|
|
/* we accept this, but it is a mistake */
|
|
|
|
block_after_label:
|
2016-12-19 01:58:33 +08:00
|
|
|
nocode_wanted &= ~0x20000000;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == '}') {
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("deprecated use of label at end of compound statement");
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
if (is_expr)
|
|
|
|
vpop();
|
2019-04-29 17:18:57 +08:00
|
|
|
block(bsym, bcl, csym, ccl, is_expr);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* expression case */
|
|
|
|
if (tok != ';') {
|
|
|
|
if (is_expr) {
|
|
|
|
vpop();
|
|
|
|
gexpr();
|
|
|
|
} else {
|
|
|
|
gexpr();
|
|
|
|
vpop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skip(';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 04:43:48 +08:00
|
|
|
/* This skips over a stream of tokens containing balanced {} and ()
|
2017-07-04 00:13:15 +08:00
|
|
|
pairs, stopping at outer ',' ';' and '}' (or matching '}' if we started
|
|
|
|
with a '{'). If STR then allocates and stores the skipped tokens
|
|
|
|
in *STR. This doesn't check if () and {} are nested correctly,
|
|
|
|
i.e. "({)}" is accepted. */
|
2017-03-07 04:43:48 +08:00
|
|
|
static void skip_or_save_block(TokenString **str)
|
|
|
|
{
|
2017-07-04 00:13:15 +08:00
|
|
|
int braces = tok == '{';
|
2017-03-07 04:43:48 +08:00
|
|
|
int level = 0;
|
|
|
|
if (str)
|
|
|
|
*str = tok_str_alloc();
|
|
|
|
|
2017-07-04 01:15:16 +08:00
|
|
|
while ((level > 0 || (tok != '}' && tok != ',' && tok != ';' && tok != ')'))) {
|
2017-03-07 04:43:48 +08:00
|
|
|
int t;
|
|
|
|
if (tok == TOK_EOF) {
|
|
|
|
if (str || level > 0)
|
|
|
|
tcc_error("unexpected end of file");
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (str)
|
|
|
|
tok_str_add_tok(*str);
|
|
|
|
t = tok;
|
|
|
|
next();
|
|
|
|
if (t == '{' || t == '(') {
|
|
|
|
level++;
|
|
|
|
} else if (t == '}' || t == ')') {
|
|
|
|
level--;
|
2017-07-04 00:13:15 +08:00
|
|
|
if (level == 0 && braces && t == '}')
|
2017-03-07 04:43:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (str) {
|
|
|
|
tok_str_add(*str, -1);
|
|
|
|
tok_str_add(*str, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-31 11:43:17 +08:00
|
|
|
#define EXPR_CONST 1
|
|
|
|
#define EXPR_ANY 2
|
|
|
|
|
|
|
|
static void parse_init_elem(int expr_type)
|
|
|
|
{
|
|
|
|
int saved_global_expr;
|
|
|
|
switch(expr_type) {
|
|
|
|
case EXPR_CONST:
|
|
|
|
/* compound literals must be allocated globally in this case */
|
|
|
|
saved_global_expr = global_expr;
|
|
|
|
global_expr = 1;
|
|
|
|
expr_const1();
|
|
|
|
global_expr = saved_global_expr;
|
2017-02-19 11:25:46 +08:00
|
|
|
/* NOTE: symbols are accepted, as well as lvalue for anon symbols
|
|
|
|
(compound literals). */
|
|
|
|
if (((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST
|
|
|
|
&& ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
|
|
|
|
|| vtop->sym->v < SYM_FIRST_ANOM))
|
2017-04-04 14:34:52 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2017-07-09 18:34:11 +08:00
|
|
|
|| ((vtop->r & VT_SYM) && vtop->sym->a.dllimport)
|
2017-04-04 14:34:52 +08:00
|
|
|
#endif
|
2017-07-09 18:34:11 +08:00
|
|
|
)
|
2016-07-31 11:43:17 +08:00
|
|
|
tcc_error("initializer element is not constant");
|
|
|
|
break;
|
|
|
|
case EXPR_ANY:
|
|
|
|
expr_eq();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 11:28:13 +08:00
|
|
|
/* put zeros for variable based init */
|
|
|
|
static void init_putz(Section *sec, unsigned long c, int size)
|
|
|
|
{
|
|
|
|
if (sec) {
|
|
|
|
/* nothing to do because globals are already set to zero */
|
|
|
|
} else {
|
|
|
|
vpush_global_sym(&func_old_type, TOK_memset);
|
|
|
|
vseti(VT_LOCAL, c);
|
|
|
|
#ifdef TCC_TARGET_ARM
|
|
|
|
vpushs(size);
|
|
|
|
vpushi(0);
|
|
|
|
#else
|
|
|
|
vpushi(0);
|
|
|
|
vpushs(size);
|
|
|
|
#endif
|
|
|
|
gfunc_call(3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:26:19 +08:00
|
|
|
#define DIF_FIRST 1
|
|
|
|
#define DIF_SIZE_ONLY 2
|
|
|
|
#define DIF_HAVE_ELEM 4
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* t is the array or struct type. c is the array or struct
|
2016-08-01 11:30:55 +08:00
|
|
|
address. cur_field is the pointer to the current
|
2017-05-06 11:28:13 +08:00
|
|
|
field, for arrays the 'c' member contains the current start
|
2019-03-18 10:26:19 +08:00
|
|
|
index. 'flags' is as in decl_initializer.
|
|
|
|
'al' contains the already initialized length of the
|
2017-05-06 11:28:13 +08:00
|
|
|
current container (starting at c). This returns the new length of that. */
|
|
|
|
static int decl_designator(CType *type, Section *sec, unsigned long c,
|
2019-03-18 10:26:19 +08:00
|
|
|
Sym **cur_field, int flags, int al)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
Sym *s, *f;
|
2017-04-16 02:07:26 +08:00
|
|
|
int index, index_last, align, l, nb_elems, elem_size;
|
2017-05-06 11:28:13 +08:00
|
|
|
unsigned long corig = c;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
elem_size = 0;
|
|
|
|
nb_elems = 1;
|
2019-03-18 10:26:19 +08:00
|
|
|
if (flags & DIF_HAVE_ELEM)
|
|
|
|
goto no_designator;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (gnu_ext && (l = is_label()) != 0)
|
|
|
|
goto struct_field;
|
2017-04-16 02:07:26 +08:00
|
|
|
/* NOTE: we only support ranges for last designator */
|
|
|
|
while (nb_elems == 1 && (tok == '[' || tok == '.')) {
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == '[') {
|
|
|
|
if (!(type->t & VT_ARRAY))
|
|
|
|
expect("array type");
|
|
|
|
next();
|
2017-04-16 02:07:26 +08:00
|
|
|
index = index_last = expr_const();
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == TOK_DOTS && gnu_ext) {
|
|
|
|
next();
|
|
|
|
index_last = expr_const();
|
|
|
|
}
|
|
|
|
skip(']');
|
2017-04-16 02:07:26 +08:00
|
|
|
s = type->ref;
|
|
|
|
if (index < 0 || (s->c >= 0 && index_last >= s->c) ||
|
|
|
|
index_last < index)
|
|
|
|
tcc_error("invalid index");
|
2017-05-06 11:28:13 +08:00
|
|
|
if (cur_field)
|
|
|
|
(*cur_field)->c = index_last;
|
2009-05-06 02:18:10 +08:00
|
|
|
type = pointed_type(type);
|
|
|
|
elem_size = type_size(type, &align);
|
|
|
|
c += index * elem_size;
|
|
|
|
nb_elems = index_last - index + 1;
|
|
|
|
} else {
|
2019-04-11 06:30:41 +08:00
|
|
|
int cumofs = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
l = tok;
|
|
|
|
struct_field:
|
2017-04-16 02:07:26 +08:00
|
|
|
next();
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((type->t & VT_BTYPE) != VT_STRUCT)
|
|
|
|
expect("struct/union type");
|
2019-04-11 06:30:41 +08:00
|
|
|
f = find_field(type, l, &cumofs);
|
2009-05-06 02:18:10 +08:00
|
|
|
if (!f)
|
|
|
|
expect("field");
|
2017-04-16 02:07:26 +08:00
|
|
|
if (cur_field)
|
2009-05-06 02:18:10 +08:00
|
|
|
*cur_field = f;
|
2017-03-07 05:38:45 +08:00
|
|
|
type = &f->type;
|
2019-04-11 06:30:41 +08:00
|
|
|
c += cumofs + f->c;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-04-16 02:07:26 +08:00
|
|
|
cur_field = NULL;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-04-16 02:07:26 +08:00
|
|
|
if (!cur_field) {
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == '=') {
|
|
|
|
next();
|
2017-04-16 02:07:26 +08:00
|
|
|
} else if (!gnu_ext) {
|
|
|
|
expect("=");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-03-18 10:26:19 +08:00
|
|
|
no_designator:
|
2009-05-06 02:18:10 +08:00
|
|
|
if (type->t & VT_ARRAY) {
|
2016-08-01 11:30:55 +08:00
|
|
|
index = (*cur_field)->c;
|
|
|
|
if (type->ref->c >= 0 && index >= type->ref->c)
|
|
|
|
tcc_error("index too large");
|
2009-05-06 02:18:10 +08:00
|
|
|
type = pointed_type(type);
|
|
|
|
c += index * type_size(type, &align);
|
|
|
|
} else {
|
|
|
|
f = *cur_field;
|
2017-05-01 12:04:19 +08:00
|
|
|
while (f && (f->v & SYM_FIRST_ANOM) && (f->type.t & VT_BITFIELD))
|
2017-04-30 04:09:10 +08:00
|
|
|
*cur_field = f = f->next;
|
2009-05-06 02:18:10 +08:00
|
|
|
if (!f)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("too many field init");
|
2017-03-07 05:38:45 +08:00
|
|
|
type = &f->type;
|
2009-05-06 02:18:10 +08:00
|
|
|
c += f->c;
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 11:28:13 +08:00
|
|
|
/* must put zero in holes (note that doing it that way
|
|
|
|
ensures that it even works with designators) */
|
2019-03-18 10:26:19 +08:00
|
|
|
if (!(flags & DIF_SIZE_ONLY) && c - corig > al)
|
2017-05-06 11:28:13 +08:00
|
|
|
init_putz(sec, corig + al, c - corig - al);
|
2019-03-18 10:26:19 +08:00
|
|
|
decl_initializer(type, sec, c, flags & ~DIF_FIRST);
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
/* XXX: make it more general */
|
2019-03-18 10:26:19 +08:00
|
|
|
if (!(flags & DIF_SIZE_ONLY) && nb_elems > 1) {
|
2009-05-06 02:18:10 +08:00
|
|
|
unsigned long c_end;
|
|
|
|
uint8_t *src, *dst;
|
|
|
|
int i;
|
|
|
|
|
2016-08-07 04:38:26 +08:00
|
|
|
if (!sec) {
|
|
|
|
vset(type, VT_LOCAL|VT_LVAL, c);
|
|
|
|
for (i = 1; i < nb_elems; i++) {
|
|
|
|
vset(type, VT_LOCAL|VT_LVAL, c + elem_size * i);
|
|
|
|
vswap();
|
|
|
|
vstore();
|
|
|
|
}
|
|
|
|
vpop();
|
2017-07-16 18:10:00 +08:00
|
|
|
} else if (!NODATA_WANTED) {
|
2016-08-07 04:38:26 +08:00
|
|
|
c_end = c + nb_elems * elem_size;
|
|
|
|
if (c_end > sec->data_allocated)
|
|
|
|
section_realloc(sec, c_end);
|
|
|
|
src = sec->data + c;
|
|
|
|
dst = src;
|
|
|
|
for(i = 1; i < nb_elems; i++) {
|
|
|
|
dst += elem_size;
|
|
|
|
memcpy(dst, src, elem_size);
|
|
|
|
}
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-05-06 11:28:13 +08:00
|
|
|
c += nb_elems * type_size(type, &align);
|
|
|
|
if (c - corig > al)
|
|
|
|
al = c - corig;
|
|
|
|
return al;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2016-07-24 06:43:49 +08:00
|
|
|
/* store a value or an expression directly in global data or in local array */
|
2016-07-31 12:18:45 +08:00
|
|
|
static void init_putv(CType *type, Section *sec, unsigned long c)
|
2016-07-24 06:43:49 +08:00
|
|
|
{
|
2017-07-09 18:38:59 +08:00
|
|
|
int bt;
|
2016-07-24 06:43:49 +08:00
|
|
|
void *ptr;
|
|
|
|
CType dtype;
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
dtype = *type;
|
|
|
|
dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
|
|
|
|
|
|
|
|
if (sec) {
|
2016-07-24 06:43:49 +08:00
|
|
|
int size, align;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* XXX: not portable */
|
|
|
|
/* XXX: generate error if incorrect relocation */
|
|
|
|
gen_assign_cast(&dtype);
|
|
|
|
bt = type->t & VT_BTYPE;
|
2017-07-16 18:10:00 +08:00
|
|
|
|
|
|
|
if ((vtop->r & VT_SYM)
|
|
|
|
&& bt != VT_PTR
|
|
|
|
&& bt != VT_FUNC
|
|
|
|
&& (bt != (PTR_SIZE == 8 ? VT_LLONG : VT_INT)
|
|
|
|
|| (type->t & VT_BITFIELD))
|
|
|
|
&& !((vtop->r & VT_CONST) && vtop->sym->v >= SYM_FIRST_ANOM)
|
|
|
|
)
|
|
|
|
tcc_error("initializer element is not computable at load time");
|
|
|
|
|
|
|
|
if (NODATA_WANTED) {
|
|
|
|
vtop--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-24 06:43:49 +08:00
|
|
|
size = type_size(type, &align);
|
2017-03-12 12:25:09 +08:00
|
|
|
section_reserve(sec, c + size);
|
2009-05-06 02:18:10 +08:00
|
|
|
ptr = sec->data + c;
|
2017-07-16 18:10:00 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* XXX: make code faster ? */
|
2016-07-24 06:43:49 +08:00
|
|
|
if ((vtop->r & (VT_SYM|VT_CONST)) == (VT_SYM|VT_CONST) &&
|
|
|
|
vtop->sym->v >= SYM_FIRST_ANOM &&
|
2017-05-08 12:38:09 +08:00
|
|
|
/* XXX This rejects compound literals like
|
2016-07-24 06:43:49 +08:00
|
|
|
'(void *){ptr}'. The problem is that '&sym' is
|
|
|
|
represented the same way, which would be ruled out
|
|
|
|
by the SYM_FIRST_ANOM check above, but also '"string"'
|
|
|
|
in 'char *p = "string"' is represented the same
|
|
|
|
with the type being VT_PTR and the symbol being an
|
|
|
|
anonymous one. That is, there's no difference in vtop
|
|
|
|
between '(void *){x}' and '&(void *){x}'. Ignore
|
|
|
|
pointer typed entities here. Hopefully no real code
|
|
|
|
will every use compound literals with scalar type. */
|
|
|
|
(vtop->type.t & VT_BTYPE) != VT_PTR) {
|
|
|
|
/* These come from compound literals, memcpy stuff over. */
|
|
|
|
Section *ssec;
|
2017-11-27 11:03:03 +08:00
|
|
|
ElfSym *esym;
|
2016-10-04 01:21:10 +08:00
|
|
|
ElfW_Rel *rel;
|
2017-11-27 11:03:03 +08:00
|
|
|
esym = elfsym(vtop->sym);
|
2016-07-24 06:43:49 +08:00
|
|
|
ssec = tcc_state->sections[esym->st_shndx];
|
|
|
|
memmove (ptr, ssec->data + esym->st_value, size);
|
2016-10-04 01:21:10 +08:00
|
|
|
if (ssec->reloc) {
|
|
|
|
/* We need to copy over all memory contents, and that
|
|
|
|
includes relocations. Use the fact that relocs are
|
|
|
|
created it order, so look from the end of relocs
|
|
|
|
until we hit one before the copied region. */
|
|
|
|
int num_relocs = ssec->reloc->data_offset / sizeof(*rel);
|
|
|
|
rel = (ElfW_Rel*)(ssec->reloc->data + ssec->reloc->data_offset);
|
|
|
|
while (num_relocs--) {
|
|
|
|
rel--;
|
|
|
|
if (rel->r_offset >= esym->st_value + size)
|
|
|
|
continue;
|
|
|
|
if (rel->r_offset < esym->st_value)
|
|
|
|
break;
|
2016-10-08 08:44:17 +08:00
|
|
|
/* Note: if the same fields are initialized multiple
|
|
|
|
times (possible with designators) then we possibly
|
|
|
|
add multiple relocations for the same offset here.
|
|
|
|
That would lead to wrong code, the last reloc needs
|
|
|
|
to win. We clean this up later after the whole
|
|
|
|
initializer is parsed. */
|
2016-10-04 01:21:10 +08:00
|
|
|
put_elf_reloca(symtab_section, sec,
|
|
|
|
c + rel->r_offset - esym->st_value,
|
|
|
|
ELFW(R_TYPE)(rel->r_info),
|
|
|
|
ELFW(R_SYM)(rel->r_info),
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2016-10-04 01:21:10 +08:00
|
|
|
rel->r_addend
|
|
|
|
#else
|
|
|
|
0
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-07-24 06:43:49 +08:00
|
|
|
} else {
|
2017-07-09 18:38:59 +08:00
|
|
|
if (type->t & VT_BITFIELD) {
|
|
|
|
int bit_pos, bit_size, bits, n;
|
|
|
|
unsigned char *p, v, m;
|
|
|
|
bit_pos = BIT_POS(vtop->type.t);
|
|
|
|
bit_size = BIT_SIZE(vtop->type.t);
|
|
|
|
p = (unsigned char*)ptr + (bit_pos >> 3);
|
|
|
|
bit_pos &= 7, bits = 0;
|
|
|
|
while (bit_size) {
|
|
|
|
n = 8 - bit_pos;
|
|
|
|
if (n > bit_size)
|
|
|
|
n = bit_size;
|
|
|
|
v = vtop->c.i >> bits << bit_pos;
|
|
|
|
m = ((1 << n) - 1) << bit_pos;
|
|
|
|
*p = (*p & ~m) | (v & m);
|
|
|
|
bits += n, bit_size -= n, bit_pos = 0, ++p;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
switch(bt) {
|
2016-07-24 06:43:49 +08:00
|
|
|
/* XXX: when cross-compiling we assume that each type has the
|
|
|
|
same representation on host and target, which is likely to
|
|
|
|
be wrong in the case of long double */
|
|
|
|
case VT_BOOL:
|
2017-07-09 18:38:59 +08:00
|
|
|
vtop->c.i = vtop->c.i != 0;
|
2016-07-24 06:43:49 +08:00
|
|
|
case VT_BYTE:
|
2017-07-09 18:38:59 +08:00
|
|
|
*(char *)ptr |= vtop->c.i;
|
2016-07-24 06:43:49 +08:00
|
|
|
break;
|
|
|
|
case VT_SHORT:
|
2017-07-09 18:38:59 +08:00
|
|
|
*(short *)ptr |= vtop->c.i;
|
2016-07-24 06:43:49 +08:00
|
|
|
break;
|
2017-03-12 12:25:09 +08:00
|
|
|
case VT_FLOAT:
|
|
|
|
*(float*)ptr = vtop->c.f;
|
|
|
|
break;
|
2016-07-24 06:43:49 +08:00
|
|
|
case VT_DOUBLE:
|
|
|
|
*(double *)ptr = vtop->c.d;
|
|
|
|
break;
|
|
|
|
case VT_LDOUBLE:
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
#if defined TCC_IS_NATIVE_387
|
2017-09-25 00:57:48 +08:00
|
|
|
if (sizeof (long double) >= 10) /* zero pad ten-byte LD */
|
|
|
|
memcpy(ptr, &vtop->c.ld, 10);
|
2017-05-13 14:59:06 +08:00
|
|
|
#ifdef __TINYC__
|
|
|
|
else if (sizeof (long double) == sizeof (double))
|
2017-12-13 00:57:20 +08:00
|
|
|
__asm__("fldl %1\nfstpt %0\n" : "=m" (*ptr) : "m" (vtop->c.ld));
|
2017-05-13 14:59:06 +08:00
|
|
|
#endif
|
2017-12-13 00:57:20 +08:00
|
|
|
else if (vtop->c.ld == 0.0)
|
|
|
|
;
|
2017-09-25 00:57:48 +08:00
|
|
|
else
|
2017-05-07 18:41:29 +08:00
|
|
|
#endif
|
2017-09-25 00:57:48 +08:00
|
|
|
if (sizeof(long double) == LDOUBLE_SIZE)
|
|
|
|
*(long double*)ptr = vtop->c.ld;
|
|
|
|
else if (sizeof(double) == LDOUBLE_SIZE)
|
|
|
|
*(double *)ptr = (double)vtop->c.ld;
|
|
|
|
else
|
2016-07-24 06:43:49 +08:00
|
|
|
tcc_error("can't cross compile long double constants");
|
|
|
|
break;
|
2016-07-11 22:26:36 +08:00
|
|
|
#if PTR_SIZE != 8
|
2016-07-24 06:43:49 +08:00
|
|
|
case VT_LLONG:
|
2017-07-09 18:38:59 +08:00
|
|
|
*(long long *)ptr |= vtop->c.i;
|
2016-07-24 06:43:49 +08:00
|
|
|
break;
|
2016-07-11 22:26:36 +08:00
|
|
|
#else
|
2016-07-24 06:43:49 +08:00
|
|
|
case VT_LLONG:
|
2016-07-11 22:26:36 +08:00
|
|
|
#endif
|
2016-07-24 06:43:49 +08:00
|
|
|
case VT_PTR:
|
|
|
|
{
|
2017-07-09 18:38:59 +08:00
|
|
|
addr_t val = vtop->c.i;
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2016-07-24 06:43:49 +08:00
|
|
|
if (vtop->r & VT_SYM)
|
|
|
|
greloca(sec, vtop->sym, c, R_DATA_PTR, val);
|
|
|
|
else
|
|
|
|
*(addr_t *)ptr |= val;
|
2015-02-22 05:29:03 +08:00
|
|
|
#else
|
2016-07-24 06:43:49 +08:00
|
|
|
if (vtop->r & VT_SYM)
|
|
|
|
greloc(sec, vtop->sym, c, R_DATA_PTR);
|
|
|
|
*(addr_t *)ptr |= val;
|
2015-02-22 05:29:03 +08:00
|
|
|
#endif
|
2016-07-24 06:43:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2017-07-09 18:38:59 +08:00
|
|
|
int val = vtop->c.i;
|
2017-05-13 14:59:06 +08:00
|
|
|
#if PTR_SIZE == 8
|
2016-07-24 06:43:49 +08:00
|
|
|
if (vtop->r & VT_SYM)
|
|
|
|
greloca(sec, vtop->sym, c, R_DATA_PTR, val);
|
|
|
|
else
|
|
|
|
*(int *)ptr |= val;
|
2015-02-22 05:29:03 +08:00
|
|
|
#else
|
2016-07-24 06:43:49 +08:00
|
|
|
if (vtop->r & VT_SYM)
|
|
|
|
greloc(sec, vtop->sym, c, R_DATA_PTR);
|
|
|
|
*(int *)ptr |= val;
|
2015-02-22 05:29:03 +08:00
|
|
|
#endif
|
2016-07-24 06:43:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
vtop--;
|
|
|
|
} else {
|
|
|
|
vset(&dtype, VT_LOCAL|VT_LVAL, c);
|
|
|
|
vswap();
|
|
|
|
vstore();
|
|
|
|
vpop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 't' contains the type and storage info. 'c' is the offset of the
|
|
|
|
object in section 'sec'. If 'sec' is NULL, it means stack based
|
2019-03-18 10:26:19 +08:00
|
|
|
allocation. 'flags & DIF_FIRST' is true if array '{' must be read (multi
|
|
|
|
dimension implicit array init handling). 'flags & DIF_SIZE_ONLY' is true if
|
2009-05-06 02:18:10 +08:00
|
|
|
size only evaluation is wanted (only for arrays). */
|
2015-07-30 04:53:57 +08:00
|
|
|
static void decl_initializer(CType *type, Section *sec, unsigned long c,
|
2019-03-18 10:26:19 +08:00
|
|
|
int flags)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2017-05-06 11:28:13 +08:00
|
|
|
int len, n, no_oblock, nb, i;
|
2016-08-01 11:30:55 +08:00
|
|
|
int size1, align1;
|
2009-05-06 02:18:10 +08:00
|
|
|
Sym *s, *f;
|
2016-08-01 11:30:55 +08:00
|
|
|
Sym indexsym;
|
2009-05-06 02:18:10 +08:00
|
|
|
CType *t1;
|
|
|
|
|
2019-03-18 10:26:19 +08:00
|
|
|
if (!(flags & DIF_HAVE_ELEM) && tok != '{' &&
|
2016-08-01 11:30:55 +08:00
|
|
|
/* In case of strings we have special handling for arrays, so
|
|
|
|
don't consume them as initializer value (which would commit them
|
|
|
|
to some anonymous symbol). */
|
|
|
|
tok != TOK_LSTR && tok != TOK_STR &&
|
2019-03-18 10:26:19 +08:00
|
|
|
!(flags & DIF_SIZE_ONLY)) {
|
2016-07-31 11:43:17 +08:00
|
|
|
parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
|
2019-03-18 10:26:19 +08:00
|
|
|
flags |= DIF_HAVE_ELEM;
|
2016-07-31 11:43:17 +08:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:26:19 +08:00
|
|
|
if ((flags & DIF_HAVE_ELEM) &&
|
2016-08-01 11:30:55 +08:00
|
|
|
!(type->t & VT_ARRAY) &&
|
2016-08-02 04:55:07 +08:00
|
|
|
/* Use i_c_parameter_t, to strip toplevel qualifiers.
|
|
|
|
The source type might have VT_CONSTANT set, which is
|
2016-08-07 04:38:26 +08:00
|
|
|
of course assignable to non-const elements. */
|
2017-07-09 18:38:25 +08:00
|
|
|
is_compatible_unqualified_types(type, &vtop->type)) {
|
2016-08-01 11:30:55 +08:00
|
|
|
init_putv(type, sec, c);
|
2011-04-07 00:17:03 +08:00
|
|
|
} else if (type->t & VT_ARRAY) {
|
2009-05-06 02:18:10 +08:00
|
|
|
s = type->ref;
|
|
|
|
n = s->c;
|
|
|
|
t1 = pointed_type(type);
|
|
|
|
size1 = type_size(t1, &align1);
|
|
|
|
|
|
|
|
no_oblock = 1;
|
2019-03-18 10:26:19 +08:00
|
|
|
if (((flags & DIF_FIRST) && tok != TOK_LSTR && tok != TOK_STR) ||
|
2009-05-06 02:18:10 +08:00
|
|
|
tok == '{') {
|
2010-03-14 01:07:57 +08:00
|
|
|
if (tok != '{')
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("character array initializer must be a literal,"
|
2010-03-14 01:07:57 +08:00
|
|
|
" optionally enclosed in braces");
|
2009-05-06 02:18:10 +08:00
|
|
|
skip('{');
|
|
|
|
no_oblock = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only parse strings here if correct type (otherwise: handle
|
|
|
|
them as ((w)char *) expressions */
|
2015-07-30 04:53:57 +08:00
|
|
|
if ((tok == TOK_LSTR &&
|
2009-05-06 02:18:10 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
(t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
|
|
|
|
#else
|
|
|
|
(t1->t & VT_BTYPE) == VT_INT
|
|
|
|
#endif
|
|
|
|
) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
|
2017-05-06 11:28:13 +08:00
|
|
|
len = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
while (tok == TOK_STR || tok == TOK_LSTR) {
|
|
|
|
int cstr_len, ch;
|
|
|
|
|
|
|
|
/* compute maximum number of chars wanted */
|
|
|
|
if (tok == TOK_STR)
|
2015-11-21 19:23:53 +08:00
|
|
|
cstr_len = tokc.str.size;
|
2009-05-06 02:18:10 +08:00
|
|
|
else
|
2015-11-21 19:23:53 +08:00
|
|
|
cstr_len = tokc.str.size / sizeof(nwchar_t);
|
2009-05-06 02:18:10 +08:00
|
|
|
cstr_len--;
|
|
|
|
nb = cstr_len;
|
2017-05-06 11:28:13 +08:00
|
|
|
if (n >= 0 && nb > (n - len))
|
|
|
|
nb = n - len;
|
2019-03-18 10:26:19 +08:00
|
|
|
if (!(flags & DIF_SIZE_ONLY)) {
|
2009-05-06 02:18:10 +08:00
|
|
|
if (cstr_len > nb)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_warning("initializer-string for array is too long");
|
2009-05-06 02:18:10 +08:00
|
|
|
/* in order to go faster for common case (char
|
|
|
|
string in global variable, we handle it
|
|
|
|
specifically */
|
|
|
|
if (sec && tok == TOK_STR && size1 == 1) {
|
2017-07-16 18:10:00 +08:00
|
|
|
if (!NODATA_WANTED)
|
|
|
|
memcpy(sec->data + c + len, tokc.str.data, nb);
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
for(i=0;i<nb;i++) {
|
|
|
|
if (tok == TOK_STR)
|
2015-11-21 19:23:53 +08:00
|
|
|
ch = ((unsigned char *)tokc.str.data)[i];
|
2009-05-06 02:18:10 +08:00
|
|
|
else
|
2015-11-21 19:23:53 +08:00
|
|
|
ch = ((nwchar_t *)tokc.str.data)[i];
|
2016-07-24 06:43:49 +08:00
|
|
|
vpushi(ch);
|
2017-05-06 11:28:13 +08:00
|
|
|
init_putv(t1, sec, c + (len + i) * size1);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 11:28:13 +08:00
|
|
|
len += nb;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
/* only add trailing zero if enough storage (no
|
|
|
|
warning in this case since it is standard) */
|
2017-05-06 11:28:13 +08:00
|
|
|
if (n < 0 || len < n) {
|
2019-03-18 10:26:19 +08:00
|
|
|
if (!(flags & DIF_SIZE_ONLY)) {
|
2016-07-24 06:43:49 +08:00
|
|
|
vpushi(0);
|
2017-05-06 11:28:13 +08:00
|
|
|
init_putv(t1, sec, c + (len * size1));
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-05-06 11:28:13 +08:00
|
|
|
len++;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-05-06 11:28:13 +08:00
|
|
|
len *= size1;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2016-08-01 11:30:55 +08:00
|
|
|
indexsym.c = 0;
|
|
|
|
f = &indexsym;
|
|
|
|
|
|
|
|
do_init_list:
|
2017-05-06 11:28:13 +08:00
|
|
|
len = 0;
|
2019-03-18 10:26:19 +08:00
|
|
|
while (tok != '}' || (flags & DIF_HAVE_ELEM)) {
|
|
|
|
len = decl_designator(type, sec, c, &f, flags, len);
|
|
|
|
flags &= ~DIF_HAVE_ELEM;
|
2016-08-01 11:30:55 +08:00
|
|
|
if (type->t & VT_ARRAY) {
|
2017-05-06 11:28:13 +08:00
|
|
|
++indexsym.c;
|
2016-08-01 11:30:55 +08:00
|
|
|
/* special test for multi dimensional arrays (may not
|
|
|
|
be strictly correct if designators are used at the
|
|
|
|
same time) */
|
2017-05-06 11:28:13 +08:00
|
|
|
if (no_oblock && len >= n*size1)
|
2016-08-01 11:30:55 +08:00
|
|
|
break;
|
|
|
|
} else {
|
2017-07-09 18:34:11 +08:00
|
|
|
if (s->type.t == VT_UNION)
|
2017-05-06 11:28:13 +08:00
|
|
|
f = NULL;
|
|
|
|
else
|
|
|
|
f = f->next;
|
2016-08-01 11:30:55 +08:00
|
|
|
if (no_oblock && f == NULL)
|
|
|
|
break;
|
|
|
|
}
|
2017-05-06 11:28:13 +08:00
|
|
|
|
2016-08-01 11:30:55 +08:00
|
|
|
if (tok == '}')
|
|
|
|
break;
|
|
|
|
skip(',');
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
/* put zeros at the end */
|
2019-03-18 10:26:19 +08:00
|
|
|
if (!(flags & DIF_SIZE_ONLY) && len < n*size1)
|
2017-05-06 11:28:13 +08:00
|
|
|
init_putz(sec, c + len, n*size1 - len);
|
2016-08-01 11:30:55 +08:00
|
|
|
if (!no_oblock)
|
|
|
|
skip('}');
|
|
|
|
/* patch type size if needed, which happens only for array types */
|
2009-05-06 02:18:10 +08:00
|
|
|
if (n < 0)
|
2017-05-06 11:28:13 +08:00
|
|
|
s->c = size1 == 1 ? len : ((len + size1 - 1)/size1);
|
2016-07-31 12:18:45 +08:00
|
|
|
} else if ((type->t & VT_BTYPE) == VT_STRUCT) {
|
2016-08-01 11:30:55 +08:00
|
|
|
size1 = 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
no_oblock = 1;
|
2019-03-18 10:26:19 +08:00
|
|
|
if ((flags & DIF_FIRST) || tok == '{') {
|
2009-05-06 02:18:10 +08:00
|
|
|
skip('{');
|
|
|
|
no_oblock = 0;
|
|
|
|
}
|
|
|
|
s = type->ref;
|
2010-06-15 23:02:09 +08:00
|
|
|
f = s->next;
|
2009-05-06 02:18:10 +08:00
|
|
|
n = s->c;
|
2016-08-01 11:30:55 +08:00
|
|
|
goto do_init_list;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else if (tok == '{') {
|
2019-03-18 10:26:19 +08:00
|
|
|
if (flags & DIF_HAVE_ELEM)
|
|
|
|
skip(';');
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
2019-03-18 10:26:19 +08:00
|
|
|
decl_initializer(type, sec, c, flags & ~DIF_HAVE_ELEM);
|
2009-05-06 02:18:10 +08:00
|
|
|
skip('}');
|
2019-03-18 10:26:19 +08:00
|
|
|
} else if ((flags & DIF_SIZE_ONLY)) {
|
2016-08-01 11:30:55 +08:00
|
|
|
/* If we supported only ISO C we wouldn't have to accept calling
|
2019-03-18 10:26:19 +08:00
|
|
|
this on anything than an array if DIF_SIZE_ONLY (and even then
|
2016-08-01 11:30:55 +08:00
|
|
|
only on the outermost level, so no recursion would be needed),
|
|
|
|
because initializing a flex array member isn't supported.
|
|
|
|
But GNU C supports it, so we need to recurse even into
|
2019-03-18 10:26:19 +08:00
|
|
|
subfields of structs and arrays when DIF_SIZE_ONLY is set. */
|
2009-05-06 02:18:10 +08:00
|
|
|
/* just skip expression */
|
2017-07-04 00:13:15 +08:00
|
|
|
skip_or_save_block(NULL);
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2019-03-18 10:26:19 +08:00
|
|
|
if (!(flags & DIF_HAVE_ELEM)) {
|
2016-07-31 11:43:17 +08:00
|
|
|
/* This should happen only when we haven't parsed
|
|
|
|
the init element above for fear of committing a
|
|
|
|
string constant to memory too early. */
|
|
|
|
if (tok != TOK_STR && tok != TOK_LSTR)
|
|
|
|
expect("string constant");
|
|
|
|
parse_init_elem(!sec ? EXPR_ANY : EXPR_CONST);
|
|
|
|
}
|
2016-07-31 12:18:45 +08:00
|
|
|
init_putv(type, sec, c);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse an initializer for type 't' if 'has_init' is non zero, and
|
|
|
|
allocate space in local or global data space ('r' is either
|
|
|
|
VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
|
2015-11-20 18:22:56 +08:00
|
|
|
variable 'v' of scope 'scope' is declared before initializers
|
|
|
|
are parsed. If 'v' is zero, then a reference to the new object
|
|
|
|
is put in the value stack. If 'has_init' is 2, a special parsing
|
|
|
|
is done to handle string constants. */
|
2015-07-30 04:53:57 +08:00
|
|
|
static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
|
2015-11-20 18:22:56 +08:00
|
|
|
int has_init, int v, int scope)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
2017-03-12 12:25:09 +08:00
|
|
|
int size, align, addr;
|
2016-10-02 02:26:50 +08:00
|
|
|
TokenString *init_str = NULL;
|
2017-07-21 04:21:27 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
Section *sec;
|
2011-03-19 08:50:42 +08:00
|
|
|
Sym *flexible_array;
|
2017-07-09 18:34:11 +08:00
|
|
|
Sym *sym = NULL;
|
2017-07-16 18:10:00 +08:00
|
|
|
int saved_nocode_wanted = nocode_wanted;
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2018-06-01 05:51:51 +08:00
|
|
|
int bcheck;
|
2017-07-16 18:10:00 +08:00
|
|
|
#endif
|
|
|
|
|
2018-06-01 05:51:51 +08:00
|
|
|
/* Always allocate static or global variables */
|
|
|
|
if (v && (r & VT_VALMASK) == VT_CONST)
|
|
|
|
nocode_wanted |= 0x80000000;
|
|
|
|
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
bcheck = tcc_state->do_bounds_check && !NODATA_WANTED;
|
|
|
|
#endif
|
2011-03-19 08:50:42 +08:00
|
|
|
|
|
|
|
flexible_array = NULL;
|
|
|
|
if ((type->t & VT_BTYPE) == VT_STRUCT) {
|
2013-07-24 23:06:13 +08:00
|
|
|
Sym *field = type->ref->next;
|
|
|
|
if (field) {
|
|
|
|
while (field->next)
|
|
|
|
field = field->next;
|
|
|
|
if (field->type.t & VT_ARRAY && field->type.ref->c < 0)
|
|
|
|
flexible_array = field;
|
|
|
|
}
|
2011-03-19 08:50:42 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
size = type_size(type, &align);
|
|
|
|
/* If unknown size, we must evaluate it before
|
|
|
|
evaluating initializers because
|
|
|
|
initializers can generate global data too
|
|
|
|
(e.g. string pointers or ISOC99 compound
|
|
|
|
literals). It also simplifies local
|
|
|
|
initializers handling */
|
2015-03-03 20:44:29 +08:00
|
|
|
if (size < 0 || (flexible_array && has_init)) {
|
2015-07-30 04:53:57 +08:00
|
|
|
if (!has_init)
|
2015-03-03 20:44:29 +08:00
|
|
|
tcc_error("unknown type size");
|
2009-05-06 02:18:10 +08:00
|
|
|
/* get all init string */
|
|
|
|
if (has_init == 2) {
|
2017-03-07 04:43:48 +08:00
|
|
|
init_str = tok_str_alloc();
|
2009-05-06 02:18:10 +08:00
|
|
|
/* only get strings */
|
|
|
|
while (tok == TOK_STR || tok == TOK_LSTR) {
|
2016-10-02 02:26:50 +08:00
|
|
|
tok_str_add_tok(init_str);
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
}
|
2017-03-07 04:43:48 +08:00
|
|
|
tok_str_add(init_str, -1);
|
|
|
|
tok_str_add(init_str, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2017-03-07 04:43:48 +08:00
|
|
|
skip_or_save_block(&init_str);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-07-21 04:21:27 +08:00
|
|
|
unget_tok(0);
|
2009-05-06 02:18:10 +08:00
|
|
|
|
2017-07-21 04:21:27 +08:00
|
|
|
/* compute size */
|
2016-10-02 02:26:50 +08:00
|
|
|
begin_macro(init_str, 1);
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
2019-03-18 10:26:19 +08:00
|
|
|
decl_initializer(type, NULL, 0, DIF_FIRST | DIF_SIZE_ONLY);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* prepare second initializer parsing */
|
2016-10-02 02:26:50 +08:00
|
|
|
macro_ptr = init_str->str;
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
2015-07-30 04:53:57 +08:00
|
|
|
|
2015-03-03 20:44:29 +08:00
|
|
|
/* if still unknown size, error */
|
2009-05-06 02:18:10 +08:00
|
|
|
size = type_size(type, &align);
|
2015-07-30 04:53:57 +08:00
|
|
|
if (size < 0)
|
2015-03-03 20:44:29 +08:00
|
|
|
tcc_error("unknown type size");
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-03-12 05:35:44 +08:00
|
|
|
/* If there's a flex member and it was used in the initializer
|
|
|
|
adjust size. */
|
|
|
|
if (flexible_array &&
|
|
|
|
flexible_array->type.ref->c > 0)
|
|
|
|
size += flexible_array->type.ref->c
|
|
|
|
* pointed_size(&flexible_array->type);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* take into account specified alignment if bigger */
|
2014-01-07 21:57:07 +08:00
|
|
|
if (ad->a.aligned) {
|
2016-10-09 08:41:34 +08:00
|
|
|
int speca = 1 << (ad->a.aligned - 1);
|
|
|
|
if (speca > align)
|
|
|
|
align = speca;
|
2014-01-07 21:57:07 +08:00
|
|
|
} else if (ad->a.packed) {
|
2009-05-06 02:18:10 +08:00
|
|
|
align = 1;
|
|
|
|
}
|
2017-07-16 18:10:00 +08:00
|
|
|
|
2018-06-01 05:51:51 +08:00
|
|
|
if (!v && NODATA_WANTED)
|
2017-07-16 18:10:00 +08:00
|
|
|
size = 0, align = 1;
|
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((r & VT_VALMASK) == VT_LOCAL) {
|
|
|
|
sec = NULL;
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2017-07-16 18:10:00 +08:00
|
|
|
if (bcheck && (type->t & VT_ARRAY)) {
|
2011-04-08 16:09:39 +08:00
|
|
|
loc--;
|
2011-04-07 00:17:03 +08:00
|
|
|
}
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
loc = (loc - size) & -align;
|
|
|
|
addr = loc;
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2009-05-06 02:18:10 +08:00
|
|
|
/* handles bounds */
|
|
|
|
/* XXX: currently, since we do only one pass, we cannot track
|
|
|
|
'&' operators, so we add only arrays */
|
2017-07-16 18:10:00 +08:00
|
|
|
if (bcheck && (type->t & VT_ARRAY)) {
|
2015-03-26 12:47:45 +08:00
|
|
|
addr_t *bounds_ptr;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* add padding between regions */
|
|
|
|
loc--;
|
|
|
|
/* then add local bound info */
|
2015-03-26 12:47:45 +08:00
|
|
|
bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(addr_t));
|
2009-05-06 02:18:10 +08:00
|
|
|
bounds_ptr[0] = addr;
|
|
|
|
bounds_ptr[1] = size;
|
|
|
|
}
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
if (v) {
|
|
|
|
/* local variable */
|
2016-10-06 10:05:30 +08:00
|
|
|
#ifdef CONFIG_TCC_ASM
|
|
|
|
if (ad->asm_label) {
|
|
|
|
int reg = asm_parse_regvar(ad->asm_label);
|
|
|
|
if (reg >= 0)
|
|
|
|
r = (r & ~VT_VALMASK) | reg;
|
|
|
|
}
|
|
|
|
#endif
|
2017-07-09 18:34:11 +08:00
|
|
|
sym = sym_push(v, type, r, addr);
|
2018-12-20 17:55:22 +08:00
|
|
|
if (ad->cleanup_func) {
|
2019-01-28 08:21:38 +08:00
|
|
|
Sym *cls = sym_push2(&all_cleanups, SYM_FIELD | ++ncleanups, 0, 0);
|
|
|
|
cls->prev_tok = sym;
|
|
|
|
cls->next = ad->cleanup_func;
|
|
|
|
cls->ncl = current_cleanups;
|
|
|
|
current_cleanups = cls;
|
2018-12-20 17:55:22 +08:00
|
|
|
}
|
|
|
|
|
2017-07-09 18:34:11 +08:00
|
|
|
sym->a = ad->a;
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
/* push local reference */
|
|
|
|
vset(type, r, addr);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (v && scope == VT_CONST) {
|
|
|
|
/* see if the symbol was already defined */
|
|
|
|
sym = sym_find(v);
|
|
|
|
if (sym) {
|
2017-07-09 18:34:11 +08:00
|
|
|
patch_storage(sym, ad, type);
|
2017-12-04 03:43:48 +08:00
|
|
|
/* we accept several definitions of the same global variable. */
|
|
|
|
if (!has_init && sym->c && elfsym(sym)->st_shndx != SHN_UNDEF)
|
2017-04-04 14:34:52 +08:00
|
|
|
goto no_alloc;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate symbol in corresponding section */
|
|
|
|
sec = ad->section;
|
|
|
|
if (!sec) {
|
2013-11-03 18:55:54 +08:00
|
|
|
if (has_init)
|
|
|
|
sec = data_section;
|
|
|
|
else if (tcc_state->nocommon)
|
|
|
|
sec = bss_section;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-04-04 14:34:52 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
if (sec) {
|
2017-03-12 12:25:09 +08:00
|
|
|
addr = section_add(sec, size, align);
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2009-05-06 02:18:10 +08:00
|
|
|
/* add padding if bound check */
|
2017-07-16 18:10:00 +08:00
|
|
|
if (bcheck)
|
2017-03-12 12:25:09 +08:00
|
|
|
section_add(sec, 1, 1);
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
2017-03-12 12:25:09 +08:00
|
|
|
addr = align; /* SHN_COMMON is special, symbol value is align */
|
|
|
|
sec = common_section;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (v) {
|
2017-03-12 12:25:09 +08:00
|
|
|
if (!sym) {
|
2009-05-06 02:18:10 +08:00
|
|
|
sym = sym_push(v, type, r | VT_SYM, 0);
|
2017-07-09 18:34:11 +08:00
|
|
|
patch_storage(sym, ad, NULL);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-07-11 04:29:28 +08:00
|
|
|
/* Local statics have a scope until now (for
|
|
|
|
warnings), remove it here. */
|
|
|
|
sym->sym_scope = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* update symbol definition */
|
2017-03-12 12:25:09 +08:00
|
|
|
put_extern_sym(sym, sec, addr, size);
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
/* push global reference */
|
2019-04-18 09:36:39 +08:00
|
|
|
vpush_ref(type, sec, addr, size);
|
|
|
|
sym = vtop->sym;
|
2017-02-19 11:25:46 +08:00
|
|
|
vtop->r |= r;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-04-04 14:34:52 +08:00
|
|
|
|
2009-12-20 05:22:43 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2009-05-06 02:18:10 +08:00
|
|
|
/* handles bounds now because the symbol must be defined
|
|
|
|
before for the relocation */
|
2017-07-16 18:10:00 +08:00
|
|
|
if (bcheck) {
|
2015-03-26 12:47:45 +08:00
|
|
|
addr_t *bounds_ptr;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
2017-05-13 14:59:06 +08:00
|
|
|
greloca(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* then add global bound info */
|
2015-03-26 12:47:45 +08:00
|
|
|
bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(addr_t));
|
2009-05-06 02:18:10 +08:00
|
|
|
bounds_ptr[0] = 0; /* relocated */
|
|
|
|
bounds_ptr[1] = size;
|
|
|
|
}
|
2009-12-20 05:22:43 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-04-04 14:34:52 +08:00
|
|
|
|
2016-08-01 11:30:55 +08:00
|
|
|
if (type->t & VT_VLA) {
|
|
|
|
int a;
|
|
|
|
|
2017-07-16 18:10:00 +08:00
|
|
|
if (NODATA_WANTED)
|
|
|
|
goto no_alloc;
|
|
|
|
|
2016-08-01 11:30:55 +08:00
|
|
|
/* save current stack pointer */
|
|
|
|
if (vlas_in_scope == 0) {
|
|
|
|
if (vla_sp_root_loc == -1)
|
|
|
|
vla_sp_root_loc = (loc -= PTR_SIZE);
|
|
|
|
gen_vla_sp_save(vla_sp_root_loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
vla_runtime_type_size(type, &a);
|
|
|
|
gen_vla_alloc(type, a);
|
2017-12-13 00:57:20 +08:00
|
|
|
#if defined TCC_TARGET_PE && defined TCC_TARGET_X86_64
|
|
|
|
/* on _WIN64, because of the function args scratch area, the
|
|
|
|
result of alloca differs from RSP and is returned in RAX. */
|
|
|
|
gen_vla_result(addr), addr = (loc -= PTR_SIZE);
|
|
|
|
#endif
|
2016-08-01 11:30:55 +08:00
|
|
|
gen_vla_sp_save(addr);
|
|
|
|
vla_sp_loc = addr;
|
|
|
|
vlas_in_scope++;
|
2017-04-04 14:34:52 +08:00
|
|
|
|
2016-08-01 11:30:55 +08:00
|
|
|
} else if (has_init) {
|
2016-10-08 08:44:17 +08:00
|
|
|
size_t oldreloc_offset = 0;
|
|
|
|
if (sec && sec->reloc)
|
|
|
|
oldreloc_offset = sec->reloc->data_offset;
|
2019-03-18 10:26:19 +08:00
|
|
|
decl_initializer(type, sec, addr, DIF_FIRST);
|
2016-10-08 08:44:17 +08:00
|
|
|
if (sec && sec->reloc)
|
|
|
|
squeeze_multi_relocs(sec, oldreloc_offset);
|
2011-03-19 08:50:42 +08:00
|
|
|
/* patch flexible array member size back to -1, */
|
|
|
|
/* for possible subsequent similar declarations */
|
|
|
|
if (flexible_array)
|
|
|
|
flexible_array->type.ref->c = -1;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-04-04 14:34:52 +08:00
|
|
|
|
|
|
|
no_alloc:
|
2016-10-02 02:26:50 +08:00
|
|
|
/* restore parse state if needed */
|
|
|
|
if (init_str) {
|
|
|
|
end_macro();
|
2017-07-21 04:21:27 +08:00
|
|
|
next();
|
2016-10-02 02:26:50 +08:00
|
|
|
}
|
2017-07-16 18:10:00 +08:00
|
|
|
|
|
|
|
nocode_wanted = saved_nocode_wanted;
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a function defined by symbol 'sym' and generate its code in
|
|
|
|
'cur_text_section' */
|
|
|
|
static void gen_function(Sym *sym)
|
|
|
|
{
|
2015-03-03 20:44:29 +08:00
|
|
|
nocode_wanted = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
ind = cur_text_section->data_offset;
|
2018-04-07 05:01:45 +08:00
|
|
|
if (sym->a.aligned) {
|
|
|
|
size_t newoff = section_add(cur_text_section, 0,
|
|
|
|
1 << (sym->a.aligned - 1));
|
2018-06-01 05:51:51 +08:00
|
|
|
gen_fill_nops(newoff - ind);
|
2018-04-07 05:01:45 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
/* NOTE: we patch the symbol size later */
|
|
|
|
put_extern_sym(sym, cur_text_section, ind, 0);
|
2011-02-08 05:42:38 +08:00
|
|
|
funcname = get_tok_str(sym->v, NULL);
|
2009-05-06 02:18:10 +08:00
|
|
|
func_ind = ind;
|
2013-04-28 03:39:34 +08:00
|
|
|
/* Initialize VLA state */
|
2015-05-04 09:09:05 +08:00
|
|
|
vla_sp_loc = -1;
|
|
|
|
vla_sp_root_loc = -1;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* put debug symbol */
|
2017-02-21 01:58:08 +08:00
|
|
|
tcc_debug_funcstart(tcc_state, sym);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* push a dummy symbol to enable local sym storage */
|
|
|
|
sym_push2(&local_stack, SYM_FIELD, 0, 0);
|
2016-05-05 16:39:09 +08:00
|
|
|
local_scope = 1; /* for function parameters */
|
2009-05-06 02:18:10 +08:00
|
|
|
gfunc_prolog(&sym->type);
|
2018-12-20 17:55:22 +08:00
|
|
|
reset_local_scope();
|
2009-05-06 02:18:10 +08:00
|
|
|
rsym = 0;
|
2019-01-10 03:32:23 +08:00
|
|
|
clear_temp_local_var_list();
|
2019-04-29 17:18:57 +08:00
|
|
|
block(NULL, NULL, NULL, NULL, 0);
|
2018-11-04 05:17:20 +08:00
|
|
|
if (!(nocode_wanted & 0x20000000)
|
|
|
|
&& ((func_vt.t & VT_BTYPE) == VT_INT)
|
|
|
|
&& !strcmp (funcname, "main"))
|
|
|
|
{
|
|
|
|
nocode_wanted = 0;
|
|
|
|
vpushi(0);
|
|
|
|
gen_assign_cast(&func_vt);
|
|
|
|
gfunc_return(&func_vt);
|
|
|
|
}
|
2016-12-19 00:23:33 +08:00
|
|
|
nocode_wanted = 0;
|
2009-05-06 02:18:10 +08:00
|
|
|
gsym(rsym);
|
|
|
|
gfunc_epilog();
|
|
|
|
cur_text_section->data_offset = ind;
|
2017-07-11 04:25:11 +08:00
|
|
|
label_pop(&global_label_stack, NULL, 0);
|
2012-10-26 01:40:04 +08:00
|
|
|
/* reset local stack */
|
2018-12-20 17:55:22 +08:00
|
|
|
reset_local_scope();
|
2016-08-15 11:09:31 +08:00
|
|
|
sym_pop(&local_stack, NULL, 0);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* end of function */
|
|
|
|
/* patch symbol size */
|
2017-11-27 11:03:03 +08:00
|
|
|
elfsym(sym)->st_size = ind - func_ind;
|
2017-02-21 01:58:08 +08:00
|
|
|
tcc_debug_funcend(tcc_state, ind - func_ind);
|
2009-05-06 02:18:10 +08:00
|
|
|
/* It's better to crash than to generate wrong code */
|
|
|
|
cur_text_section = NULL;
|
|
|
|
funcname = ""; /* for safety */
|
|
|
|
func_vt.t = VT_VOID; /* for safety */
|
2014-01-06 22:27:39 +08:00
|
|
|
func_var = 0; /* for safety */
|
2009-05-06 02:18:10 +08:00
|
|
|
ind = 0; /* for safety */
|
2017-07-16 18:10:00 +08:00
|
|
|
nocode_wanted = 0x80000000;
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
check_vstack();
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2016-10-18 05:24:01 +08:00
|
|
|
static void gen_inline_functions(TCCState *s)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
Sym *sym;
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
int inline_generated, i, ln;
|
2009-06-30 03:14:53 +08:00
|
|
|
struct InlineFunc *fn;
|
2009-05-06 02:18:10 +08:00
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
ln = file->line_num;
|
2009-05-06 02:18:10 +08:00
|
|
|
/* iterate while inline function are referenced */
|
2017-07-14 23:42:48 +08:00
|
|
|
do {
|
2009-05-06 02:18:10 +08:00
|
|
|
inline_generated = 0;
|
2016-10-18 05:24:01 +08:00
|
|
|
for (i = 0; i < s->nb_inline_fns; ++i) {
|
|
|
|
fn = s->inline_fns[i];
|
2009-06-30 03:14:53 +08:00
|
|
|
sym = fn->sym;
|
2019-06-18 01:28:51 +08:00
|
|
|
if (sym && (sym->c || !(sym->type.t & (VT_INLINE | VT_STATIC)) )) {
|
|
|
|
/* the function was used or forced (and then not internal):
|
|
|
|
generate its code and convert it to a normal function */
|
2009-06-30 03:14:53 +08:00
|
|
|
fn->sym = NULL;
|
|
|
|
if (file)
|
2013-01-31 18:29:45 +08:00
|
|
|
pstrcpy(file->filename, sizeof file->filename, fn->filename);
|
2016-11-12 01:29:45 +08:00
|
|
|
begin_macro(fn->func_str, 1);
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
cur_text_section = text_section;
|
|
|
|
gen_function(sym);
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
end_macro();
|
2009-05-06 02:18:10 +08:00
|
|
|
|
|
|
|
inline_generated = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-07-14 23:42:48 +08:00
|
|
|
} while (inline_generated);
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
file->line_num = ln;
|
2016-10-18 05:24:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ST_FUNC void free_inline_functions(TCCState *s)
|
|
|
|
{
|
|
|
|
int i;
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
/* free tokens of unused inline functions */
|
2016-10-18 05:24:01 +08:00
|
|
|
for (i = 0; i < s->nb_inline_fns; ++i) {
|
|
|
|
struct InlineFunc *fn = s->inline_fns[i];
|
2016-11-12 03:25:13 +08:00
|
|
|
if (fn->sym)
|
|
|
|
tok_str_free(fn->func_str);
|
2009-07-14 10:46:35 +08:00
|
|
|
}
|
2016-10-18 05:24:01 +08:00
|
|
|
dynarray_reset(&s->inline_fns, &s->nb_inline_fns);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2017-03-11 09:13:59 +08:00
|
|
|
/* 'l' is VT_LOCAL or VT_CONST to define default storage type, or VT_CMP
|
|
|
|
if parsing old style parameter decl list (and FUNC_SYM is set then) */
|
|
|
|
static int decl0(int l, int is_for_loop_init, Sym *func_sym)
|
2009-05-06 02:18:10 +08:00
|
|
|
{
|
|
|
|
int v, has_init, r;
|
|
|
|
CType type, btype;
|
|
|
|
Sym *sym;
|
2019-01-28 08:21:38 +08:00
|
|
|
AttributeDef ad, adbase;
|
2012-10-26 01:40:04 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
while (1) {
|
2019-04-28 07:07:01 +08:00
|
|
|
if (tok == TOK_STATIC_ASSERT) {
|
|
|
|
int c;
|
|
|
|
|
|
|
|
next();
|
|
|
|
skip('(');
|
|
|
|
c = expr_const();
|
|
|
|
skip(',');
|
|
|
|
if (c == 0)
|
|
|
|
tcc_error("%s", get_tok_str(tok, &tokc));
|
|
|
|
next();
|
|
|
|
skip(')');
|
2019-05-03 06:22:35 +08:00
|
|
|
skip(';');
|
2019-04-28 07:07:01 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-01-28 08:21:38 +08:00
|
|
|
if (!parse_btype(&btype, &adbase)) {
|
2011-03-09 05:36:04 +08:00
|
|
|
if (is_for_loop_init)
|
|
|
|
return 0;
|
2017-03-11 09:13:59 +08:00
|
|
|
/* skip redundant ';' if not in old parameter decl scope */
|
|
|
|
if (tok == ';' && l != VT_CMP) {
|
2009-05-06 02:18:10 +08:00
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
}
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
if (l != VT_CONST)
|
|
|
|
break;
|
|
|
|
if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* global asm block */
|
|
|
|
asm_global_instr();
|
|
|
|
continue;
|
|
|
|
}
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
if (tok >= TOK_UIDENT) {
|
|
|
|
/* special test for old K&R protos without explicit int
|
|
|
|
type. Only accepted when defining global data */
|
|
|
|
btype.t = VT_INT;
|
|
|
|
} else {
|
|
|
|
if (tok != TOK_EOF)
|
|
|
|
expect("declaration");
|
2009-05-06 02:18:10 +08:00
|
|
|
break;
|
various stuff
win32/Makefile ("for cygwin") removed
- On cygwin, the normal ./configure && make can be used with either
cygwin's "GCC for Win32 Toolchain"
./configure --cross-prefix=i686-w64-mingw32-
or with an existing tcc:
./configure --cc=<old-tccdir>/tcc.exe
tcctest.c:
- exclude test_high_clobbers() on _WIN64 (does not work)
tests2/95_bitfield.c:
- use 'signed char' for ARM (where default 'char' is unsigned)
tests:
- remove -I "expr" diff option to allow tests with
busybox-diff.
libtcc.c, tcc.c:
- removed -iwithprefix option. It is supposed to be
combined with -iprefix which we don't have either.
tccgen.c:
- fix assignments and return of 'void', as in
void f() {
void *p, *q;
*p = *q:
return *p;
}
This appears to be allowed but should do nothing.
tcc.h, libtcc.c, tccpp.c:
- Revert "Introduce VIP sysinclude paths which are always searched first"
This reverts commit 1d5e386b0a78393ac6b670c209a185849ec798a1.
The patch was giving tcc's system includes priority over -I which
is not how it should be.
tccelf.c:
- add DT_TEXTREL tag only if text relocations are actually
used (which is likely not the case on x86_64)
- prepare_dynamic_rel(): avoid relocation of unresolved
(weak) symbols
tccrun.c:
- for HAVE_SELINUX, use two mappings to the same (real) file.
(it was so once except the RX mapping wasn't used at all).
tccpe.c:
- fix relocation constant used for x86_64 (by Andrei E. Warentin)
- #ifndef _WIN32 do "chmod 755 ..." to get runnable exes on cygwin.
tccasm.c:
- keep forward asm labels static, otherwise they will endup
in dynsym eventually.
configure, Makefile:
- mingw32: respect ./configure options --bindir --docdir --libdir
- allow overriding tcc when building libtcc1.a and libtcc.def with
make XTCC=<tcc program to use>
- use $(wildcard ...) for install to allow installing just
a cross compiler for example
make cross-arm
make install
- use name <target>-libtcc1.a
build-tcc.bat:
- add options: -clean, -b bindir
2017-10-12 00:13:43 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2017-07-09 18:38:25 +08:00
|
|
|
if (tok == ';') {
|
2015-07-30 04:53:57 +08:00
|
|
|
if ((btype.t & VT_BTYPE) == VT_STRUCT) {
|
|
|
|
int v = btype.ref->v;
|
|
|
|
if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) >= SYM_FIRST_ANOM)
|
|
|
|
tcc_warning("unnamed struct/union that defines no instances");
|
2017-07-09 18:38:25 +08:00
|
|
|
next();
|
|
|
|
continue;
|
2015-07-30 04:53:57 +08:00
|
|
|
}
|
2017-07-09 18:38:25 +08:00
|
|
|
if (IS_ENUM(btype.t)) {
|
|
|
|
next();
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
while (1) { /* iterate thru each declaration */
|
|
|
|
type = btype;
|
2016-07-13 23:39:15 +08:00
|
|
|
/* If the base type itself was an array type of unspecified
|
|
|
|
size (like in 'typedef int arr[]; arr x = {1};') then
|
|
|
|
we will overwrite the unknown size by the real one for
|
|
|
|
this decl. We need to unshare the ref symbol holding
|
|
|
|
that size. */
|
|
|
|
if ((type.t & VT_ARRAY) && type.ref->c < 0) {
|
|
|
|
type.ref = sym_push(SYM_FIELD, &type.ref->type, 0, type.ref->c);
|
|
|
|
}
|
2019-01-28 08:21:38 +08:00
|
|
|
ad = adbase;
|
2009-05-06 02:18:10 +08:00
|
|
|
type_decl(&type, &ad, &v, TYPE_DIRECT);
|
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
char buf[500];
|
2017-03-06 10:25:33 +08:00
|
|
|
type_to_str(buf, sizeof(buf), &type, get_tok_str(v, NULL));
|
2009-05-06 02:18:10 +08:00
|
|
|
printf("type = '%s'\n", buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if ((type.t & VT_BTYPE) == VT_FUNC) {
|
2011-02-08 06:46:20 +08:00
|
|
|
if ((type.t & VT_STATIC) && (l == VT_LOCAL)) {
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("function without file scope cannot be static");
|
2011-02-08 06:46:20 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
/* if old style function prototype, we accept a
|
|
|
|
declaration list */
|
|
|
|
sym = type.ref;
|
2017-07-09 18:34:11 +08:00
|
|
|
if (sym->f.func_type == FUNC_OLD && l == VT_CONST)
|
2017-03-11 09:13:59 +08:00
|
|
|
decl0(VT_CMP, 0, sym);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
|
2011-03-03 16:55:02 +08:00
|
|
|
if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
|
2015-11-20 18:22:56 +08:00
|
|
|
ad.asm_label = asm_label_instr();
|
2011-03-03 16:55:02 +08:00
|
|
|
/* parse one last attribute list, after asm label */
|
|
|
|
parse_attribute(&ad);
|
2015-11-20 18:22:56 +08:00
|
|
|
if (tok == '{')
|
|
|
|
expect(";");
|
2011-03-03 16:55:02 +08:00
|
|
|
}
|
|
|
|
|
2010-01-15 03:55:51 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2017-07-09 18:34:11 +08:00
|
|
|
if (ad.a.dllimport || ad.a.dllexport) {
|
2017-04-04 14:34:52 +08:00
|
|
|
if (type.t & (VT_STATIC|VT_TYPEDEF))
|
|
|
|
tcc_error("cannot have dll linkage with static or typedef");
|
2017-07-09 18:34:11 +08:00
|
|
|
if (ad.a.dllimport) {
|
|
|
|
if ((type.t & VT_BTYPE) == VT_FUNC)
|
|
|
|
ad.a.dllimport = 0;
|
|
|
|
else
|
|
|
|
type.t |= VT_EXTERN;
|
|
|
|
}
|
2017-04-04 14:34:52 +08:00
|
|
|
}
|
2010-01-15 03:55:51 +08:00
|
|
|
#endif
|
2009-05-06 02:18:10 +08:00
|
|
|
if (tok == '{') {
|
2017-03-11 09:13:59 +08:00
|
|
|
if (l != VT_CONST)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("cannot use local functions");
|
2009-05-06 02:18:10 +08:00
|
|
|
if ((type.t & VT_BTYPE) != VT_FUNC)
|
|
|
|
expect("function definition");
|
|
|
|
|
2017-03-11 09:13:59 +08:00
|
|
|
/* reject abstract declarators in function definition
|
2019-06-11 21:28:42 +08:00
|
|
|
make old style params without decl have int type */
|
2009-05-06 02:18:10 +08:00
|
|
|
sym = type.ref;
|
2017-03-11 09:13:59 +08:00
|
|
|
while ((sym = sym->next) != NULL) {
|
2009-05-06 02:18:10 +08:00
|
|
|
if (!(sym->v & ~SYM_FIELD))
|
2017-03-11 09:13:59 +08:00
|
|
|
expect("identifier");
|
2019-06-11 21:28:42 +08:00
|
|
|
if (sym->type.t == VT_VOID)
|
|
|
|
sym->type = int_type;
|
|
|
|
}
|
2017-02-14 01:23:43 +08:00
|
|
|
|
2017-12-04 03:43:48 +08:00
|
|
|
/* put function symbol */
|
2019-04-18 09:36:39 +08:00
|
|
|
sym = external_sym(v, &type, 0, &ad);
|
2019-06-18 00:28:56 +08:00
|
|
|
/* This is the def, so overwrite any other parameter names
|
|
|
|
we got from prototypes. */
|
|
|
|
sym->type.ref = type.ref;
|
2019-06-17 09:34:03 +08:00
|
|
|
if (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF
|
|
|
|
&& !(elfsym(sym)->st_other & ST_ASM_SET))
|
|
|
|
tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL));
|
2017-02-14 01:23:43 +08:00
|
|
|
|
2009-05-06 02:18:10 +08:00
|
|
|
/* static inline functions are just recorded as a kind
|
|
|
|
of macro. Their code will be emitted at the end of
|
|
|
|
the compilation unit only if they are used */
|
2019-06-17 09:34:03 +08:00
|
|
|
if ((sym->type.t & (VT_INLINE | VT_EXTERN)) == VT_INLINE) {
|
2009-06-30 03:14:53 +08:00
|
|
|
struct InlineFunc *fn;
|
|
|
|
const char *filename;
|
2015-07-30 04:53:57 +08:00
|
|
|
|
tccpp: fix issues, add tests
* fix some macro expansion issues
* add some pp tests in tests/pp
* improved tcc -E output for better diff'ability
* remove -dD feature (quirky code, exotic feature,
didn't work well)
Based partially on ideas / researches from PipCet
Some issues remain with VA_ARGS macros (if used in a
rather tricky way).
Also, to keep it simple, the pp doesn't automtically
add any extra spaces to separate tokens which otherwise
would form wrong tokens if re-read from tcc -E output
(such as '+' '=') GCC does that, other compilers don't.
* cleanups
- #line 01 "file" / # 01 "file" processing
- #pragma comment(lib,"foo")
- tcc -E: forward some pragmas to output (pack, comment(lib))
- fix macro parameter list parsing mess from
a3fc54345949535524d01319e1ca6378b7c2c201
a715d7143d9d17da17e67fec6af1c01409a71a31
(some coffee might help, next time ;)
- introduce TOK_PPSTR - to have character constants as
written in the file (similar to TOK_PPNUM)
- allow '\' appear in macros
- new functions begin/end_macro to:
- fix switching macro levels during expansion
- allow unget_tok to unget more than one tok
- slight speedup by using bitflags in isidnum_table
Also:
- x86_64.c : fix decl after statements
- i386-gen,c : fix a vstack leak with VLA on windows
- configure/Makefile : build on windows (MSYS) was broken
- tcc_warning: fflush stderr to keep output order (win32)
2015-05-09 20:29:39 +08:00
|
|
|
filename = file ? file->filename : "";
|
|
|
|
fn = tcc_malloc(sizeof *fn + strlen(filename));
|
|
|
|
strcpy(fn->filename, filename);
|
|
|
|
fn->sym = sym;
|
2017-03-07 04:43:48 +08:00
|
|
|
skip_or_save_block(&fn->func_str);
|
|
|
|
dynarray_add(&tcc_state->inline_fns,
|
|
|
|
&tcc_state->nb_inline_fns, fn);
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
/* compute text section */
|
|
|
|
cur_text_section = ad.section;
|
|
|
|
if (!cur_text_section)
|
|
|
|
cur_text_section = text_section;
|
|
|
|
gen_function(sym);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
2017-03-11 09:13:59 +08:00
|
|
|
if (l == VT_CMP) {
|
|
|
|
/* find parameter in function parameter list */
|
|
|
|
for (sym = func_sym->next; sym; sym = sym->next)
|
|
|
|
if ((sym->v & ~SYM_FIELD) == v)
|
|
|
|
goto found;
|
|
|
|
tcc_error("declaration for parameter '%s' but no such parameter",
|
|
|
|
get_tok_str(v, NULL));
|
|
|
|
found:
|
|
|
|
if (type.t & VT_STORAGE) /* 'register' is okay */
|
|
|
|
tcc_error("storage class specified for '%s'",
|
|
|
|
get_tok_str(v, NULL));
|
|
|
|
if (sym->type.t != VT_VOID)
|
|
|
|
tcc_error("redefinition of parameter '%s'",
|
|
|
|
get_tok_str(v, NULL));
|
|
|
|
convert_parameter_type(&type);
|
|
|
|
sym->type = type;
|
|
|
|
} else if (type.t & VT_TYPEDEF) {
|
2009-05-06 02:18:10 +08:00
|
|
|
/* save typedefed type */
|
|
|
|
/* XXX: test storage specifiers ? */
|
2016-05-06 14:32:54 +08:00
|
|
|
sym = sym_find(v);
|
2017-07-09 18:34:11 +08:00
|
|
|
if (sym && sym->sym_scope == local_scope) {
|
2016-05-06 14:32:54 +08:00
|
|
|
if (!is_compatible_types(&sym->type, &type)
|
|
|
|
|| !(sym->type.t & VT_TYPEDEF))
|
|
|
|
tcc_error("incompatible redefinition of '%s'",
|
|
|
|
get_tok_str(v, NULL));
|
|
|
|
sym->type = type;
|
|
|
|
} else {
|
|
|
|
sym = sym_push(v, &type, 0, 0);
|
|
|
|
}
|
2014-01-07 21:57:07 +08:00
|
|
|
sym->a = ad.a;
|
2017-07-14 23:42:48 +08:00
|
|
|
sym->f = ad.f;
|
2018-08-04 04:39:00 +08:00
|
|
|
} else if ((type.t & VT_BTYPE) == VT_VOID
|
|
|
|
&& !(type.t & VT_EXTERN)) {
|
|
|
|
tcc_error("declaration of void object");
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
r = 0;
|
2011-03-03 17:07:36 +08:00
|
|
|
if ((type.t & VT_BTYPE) == VT_FUNC) {
|
|
|
|
/* external function definition */
|
|
|
|
/* specific case for func_call attribute */
|
2017-07-14 23:42:48 +08:00
|
|
|
type.ref->f = ad.f;
|
2011-03-03 17:07:36 +08:00
|
|
|
} else if (!(type.t & VT_ARRAY)) {
|
|
|
|
/* not lvalue if array */
|
2009-05-06 02:18:10 +08:00
|
|
|
r |= lvalue_type(type.t);
|
2011-03-03 17:07:36 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
has_init = (tok == '=');
|
2011-04-07 00:17:03 +08:00
|
|
|
if (has_init && (type.t & VT_VLA))
|
2016-08-01 11:30:55 +08:00
|
|
|
tcc_error("variable length array cannot be initialized");
|
2017-05-07 14:10:06 +08:00
|
|
|
if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) ||
|
|
|
|
((type.t & VT_BTYPE) == VT_FUNC) ||
|
2011-04-08 16:09:39 +08:00
|
|
|
((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
|
|
|
|
!has_init && l == VT_CONST && type.ref->c < 0)) {
|
2011-03-03 17:07:36 +08:00
|
|
|
/* external variable or function */
|
2009-05-06 02:18:10 +08:00
|
|
|
/* NOTE: as GCC, uninitialized global static
|
|
|
|
arrays of null size are considered as
|
|
|
|
extern */
|
2019-06-17 09:34:03 +08:00
|
|
|
if ((type.t & VT_BTYPE) != VT_FUNC)
|
|
|
|
type.t |= VT_EXTERN;
|
2017-07-09 18:34:11 +08:00
|
|
|
sym = external_sym(v, &type, r, &ad);
|
2011-03-03 17:58:45 +08:00
|
|
|
if (ad.alias_target) {
|
2017-11-27 11:03:03 +08:00
|
|
|
ElfSym *esym;
|
2011-03-03 17:58:45 +08:00
|
|
|
Sym *alias_target;
|
|
|
|
alias_target = sym_find(ad.alias_target);
|
2017-11-27 11:03:03 +08:00
|
|
|
esym = elfsym(alias_target);
|
|
|
|
if (!esym)
|
2011-08-11 23:07:56 +08:00
|
|
|
tcc_error("unsupported forward __alias__ attribute");
|
2017-07-11 04:29:28 +08:00
|
|
|
/* Local statics have a scope until now (for
|
|
|
|
warnings), remove it here. */
|
|
|
|
sym->sym_scope = 0;
|
2017-12-13 00:57:20 +08:00
|
|
|
put_extern_sym2(sym, esym->st_shndx, esym->st_value, esym->st_size, 0);
|
2011-03-03 17:58:45 +08:00
|
|
|
}
|
2009-05-06 02:18:10 +08:00
|
|
|
} else {
|
|
|
|
if (type.t & VT_STATIC)
|
|
|
|
r |= VT_CONST;
|
|
|
|
else
|
|
|
|
r |= l;
|
|
|
|
if (has_init)
|
|
|
|
next();
|
2019-06-17 09:34:03 +08:00
|
|
|
else if (l == VT_CONST
|
|
|
|
&& (type.t & VT_BTYPE) != VT_FUNC)
|
2017-12-04 03:43:48 +08:00
|
|
|
/* uninitialized global variables may be overridden */
|
|
|
|
type.t |= VT_EXTERN;
|
2015-11-20 18:22:56 +08:00
|
|
|
decl_initializer_alloc(&type, &ad, r, has_init, v, l);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok != ',') {
|
2015-11-20 18:22:56 +08:00
|
|
|
if (is_for_loop_init)
|
2011-03-09 05:36:04 +08:00
|
|
|
return 1;
|
2009-05-06 02:18:10 +08:00
|
|
|
skip(';');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-09 05:36:04 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-21 04:21:27 +08:00
|
|
|
static void decl(int l)
|
2011-03-09 05:36:04 +08:00
|
|
|
{
|
2017-03-11 09:13:59 +08:00
|
|
|
decl0(l, 0, NULL);
|
2009-05-06 02:18:10 +08:00
|
|
|
}
|
2016-10-15 21:55:31 +08:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|