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
    a3fc543459
    a715d7143d
    (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)
master
grischka 2015-05-09 14:29:39 +02:00
parent 70a6c4601e
commit 30df3189b1
49 changed files with 1060 additions and 988 deletions

6
configure vendored
View File

@ -449,12 +449,6 @@ LIBSUF=$LIBSUF
EXESUF=$EXESUF EXESUF=$EXESUF
HOST_OS=$host_os HOST_OS=$host_os
EOF EOF
if test "$mingw32" = "yes"; then
cat >>config.mak <<EOF
XCC=$cc
XAR=$ar
EOF
fi
print_inc() { print_inc() {
if test -n "$2"; then if test -n "$2"; then

View File

@ -1116,7 +1116,6 @@ ST_FUNC void gen_vla_alloc(CType *type, int align) {
vpush_global_sym(&func_old_type, TOK_alloca); vpush_global_sym(&func_old_type, TOK_alloca);
vswap(); /* Move alloca ref past allocation size */ vswap(); /* Move alloca ref past allocation size */
gfunc_call(1); gfunc_call(1);
vset(type, REG_IRET, 0);
#else #else
int r; int r;
r = gv(RC_INT); /* allocation size */ r = gv(RC_INT); /* allocation size */

View File

@ -608,7 +608,10 @@ static void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
if (!s1->error_func) { if (!s1->error_func) {
/* default case: stderr */ /* default case: stderr */
if (s1->ppfp) /* print a newline during tcc -E */
fprintf(s1->ppfp, "\n"), fflush(s1->ppfp);
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
fflush(stderr); /* print error/warning now (win32) */
} else { } else {
s1->error_func(s1->error_opaque, buf); s1->error_func(s1->error_opaque, buf);
} }
@ -672,7 +675,7 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
BufferedFile *bf; BufferedFile *bf;
int buflen = initlen ? initlen : IO_BUF_SIZE; int buflen = initlen ? initlen : IO_BUF_SIZE;
bf = tcc_malloc(sizeof(BufferedFile) + buflen); bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
bf->buf_ptr = bf->buffer; bf->buf_ptr = bf->buffer;
bf->buf_end = bf->buffer + initlen; bf->buf_end = bf->buffer + initlen;
bf->buf_end[0] = CH_EOB; /* put eob symbol */ bf->buf_end[0] = CH_EOB; /* put eob symbol */
@ -681,7 +684,6 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen)
normalize_slashes(bf->filename); normalize_slashes(bf->filename);
#endif #endif
bf->line_num = 1; bf->line_num = 1;
bf->ifndef_macro = 0;
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr; bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
bf->fd = -1; bf->fd = -1;
bf->prev = file; bf->prev = file;
@ -703,7 +705,7 @@ ST_FUNC int tcc_open(TCCState *s1, const char *filename)
{ {
int fd; int fd;
if (strcmp(filename, "-") == 0) if (strcmp(filename, "-") == 0)
fd = 0, filename = "stdin"; fd = 0, filename = "<stdin>";
else else
fd = open(filename, O_RDONLY | O_BINARY); fd = open(filename, O_RDONLY | O_BINARY);
if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3) if ((s1->verbose == 2 && fd >= 0) || s1->verbose == 3)
@ -721,7 +723,6 @@ ST_FUNC int tcc_open(TCCState *s1, const char *filename)
static int tcc_compile(TCCState *s1) static int tcc_compile(TCCState *s1)
{ {
Sym *define_start; Sym *define_start;
SValue *pvtop;
char buf[512]; char buf[512];
volatile int section_sym; volatile int section_sym;
@ -797,14 +798,12 @@ static int tcc_compile(TCCState *s1)
ch = file->buf_ptr[0]; ch = file->buf_ptr[0];
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM; parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | PARSE_FLAG_TOK_STR;
pvtop = vtop;
next(); next();
decl(VT_CONST); decl(VT_CONST);
if (tok != TOK_EOF) if (tok != TOK_EOF)
expect("declaration"); expect("declaration");
if (pvtop != vtop) check_vstack();
tcc_warning("internal compiler error: vstack leak? (%d)", vtop - pvtop);
/* end of translation unit info */ /* end of translation unit info */
if (s1->do_debug) { if (s1->do_debug) {
@ -829,31 +828,13 @@ static int tcc_compile(TCCState *s1)
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str) LIBTCCAPI int tcc_compile_string(TCCState *s, const char *str)
{ {
int i;
int len, ret; int len, ret;
len = strlen(str);
len = strlen(str);
tcc_open_bf(s, "<string>", len); tcc_open_bf(s, "<string>", len);
memcpy(file->buffer, str, len); memcpy(file->buffer, str, len);
len = s->nb_files;
ret = tcc_compile(s); ret = tcc_compile(s);
tcc_close(); tcc_close();
/* habdle #pragma comment(lib,) */
for(i = len; i < s->nb_files; i++) {
/* int filetype = *(unsigned char *)s->files[i]; */
const char *filename = s->files[i] + 1;
if (filename[0] == '-' && filename[1] == 'l') {
if (tcc_add_library(s, filename + 2) < 0) {
tcc_warning("cannot find library 'lib%s'", filename+2);
ret++;
}
}
tcc_free(s->files[i]);
}
s->nb_files = len;
return ret; return ret;
} }
@ -896,20 +877,11 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s1, const char *sym)
/* cleanup all static data used during compilation */ /* cleanup all static data used during compilation */
static void tcc_cleanup(void) static void tcc_cleanup(void)
{ {
int i, n;
if (NULL == tcc_state) if (NULL == tcc_state)
return; return;
tcc_state = NULL; tcc_state = NULL;
/* free -D defines */ preprocess_delete();
free_defines(NULL);
/* free tokens */
n = tok_ident - TOK_IDENT;
for(i = 0; i < n; i++)
tcc_free(table_ident[i]);
tcc_free(table_ident);
table_ident = NULL;
/* free sym_pools */ /* free sym_pools */
dynarray_reset(&sym_pools, &nb_sym_pools); dynarray_reset(&sym_pools, &nb_sym_pools);
@ -917,8 +889,6 @@ static void tcc_cleanup(void)
cstr_free(&tokcstr); cstr_free(&tokcstr);
/* reset symbol stack */ /* reset symbol stack */
sym_free_first = NULL; sym_free_first = NULL;
/* cleanup from error/setjmp */
macro_ptr = NULL;
} }
LIBTCCAPI TCCState *tcc_new(void) LIBTCCAPI TCCState *tcc_new(void)
@ -1128,6 +1098,7 @@ LIBTCCAPI void tcc_delete(TCCState *s1)
tcc_free(s1->deps_outfile); tcc_free(s1->deps_outfile);
dynarray_reset(&s1->files, &s1->nb_files); dynarray_reset(&s1->files, &s1->nb_files);
dynarray_reset(&s1->target_deps, &s1->nb_target_deps); dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
dynarray_reset(&s1->pragma_libs, &s1->nb_pragma_libs);
#ifdef TCC_IS_NATIVE #ifdef TCC_IS_NATIVE
# ifdef HAVE_SELINUX # ifdef HAVE_SELINUX
@ -1138,8 +1109,7 @@ LIBTCCAPI void tcc_delete(TCCState *s1)
# endif # endif
#endif #endif
if(s1->sym_attrs) tcc_free(s1->sym_attrs); tcc_free(s1->sym_attrs);
tcc_free(s1); tcc_free(s1);
} }
@ -1338,6 +1308,22 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname)
return -1; return -1;
} }
PUB_FUNC int tcc_add_library_err(TCCState *s, const char *libname)
{
int ret = tcc_add_library(s, libname);
if (ret < 0)
tcc_error_noabort("cannot find library 'lib%s'", libname);
return ret;
}
/* habdle #pragma comment(lib,) */
ST_FUNC void tcc_add_pragma_libs(TCCState *s1)
{
int i;
for (i = 0; i < s1->nb_pragma_libs; i++)
tcc_add_library_err(s1, s1->pragma_libs[i]);
}
LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val) LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
{ {
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
@ -1355,8 +1341,6 @@ LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val)
LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type) LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
{ {
s->output_type = output_type; s->output_type = output_type;
if (output_type == TCC_OUTPUT_PREPROCESS)
print_defines();
if (!s->nostdinc) { if (!s->nostdinc) {
/* default include paths */ /* default include paths */
@ -1694,7 +1678,6 @@ enum {
TCC_OPTION_g, TCC_OPTION_g,
TCC_OPTION_c, TCC_OPTION_c,
TCC_OPTION_dumpversion, TCC_OPTION_dumpversion,
TCC_OPTION_d,
TCC_OPTION_float_abi, TCC_OPTION_float_abi,
TCC_OPTION_static, TCC_OPTION_static,
TCC_OPTION_std, TCC_OPTION_std,
@ -1751,7 +1734,6 @@ static const TCCOption tcc_options[] = {
{ "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
{ "c", TCC_OPTION_c, 0 }, { "c", TCC_OPTION_c, 0 },
{ "dumpversion", TCC_OPTION_dumpversion, 0}, { "dumpversion", TCC_OPTION_dumpversion, 0},
{ "d", TCC_OPTION_d, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
#ifdef TCC_TARGET_ARM #ifdef TCC_TARGET_ARM
{ "mfloat-abi", TCC_OPTION_float_abi, TCC_OPTION_HAS_ARG }, { "mfloat-abi", TCC_OPTION_float_abi, TCC_OPTION_HAS_ARG },
#endif #endif
@ -1878,8 +1860,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
case TCC_OPTION_HELP: case TCC_OPTION_HELP:
return 0; return 0;
case TCC_OPTION_I: case TCC_OPTION_I:
if (tcc_add_include_path(s, optarg) < 0) tcc_add_include_path(s, optarg);
tcc_error("too many include paths");
break; break;
case TCC_OPTION_D: case TCC_OPTION_D:
parse_option_D(s, optarg); parse_option_D(s, optarg);
@ -1924,14 +1905,6 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
tcc_warning("-c: some compiler action already specified (%d)", s->output_type); tcc_warning("-c: some compiler action already specified (%d)", s->output_type);
s->output_type = TCC_OUTPUT_OBJ; s->output_type = TCC_OUTPUT_OBJ;
break; break;
case TCC_OPTION_d:
if (*optarg != 'D') {
if (s->warn_unsupported)
goto unsupported_option;
tcc_error("invalid option -- '%s'", r);
}
s->dflag = 1;
break;
#ifdef TCC_TARGET_ARM #ifdef TCC_TARGET_ARM
case TCC_OPTION_float_abi: case TCC_OPTION_float_abi:
/* tcc doesn't support soft float yet */ /* tcc doesn't support soft float yet */

27
tcc.c
View File

@ -55,31 +55,28 @@ static void display_info(TCCState *s, int what)
# endif # endif
#endif #endif
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
", mingw" " Windows"
#else #else
#ifdef __linux " Linux"
", Linux"
#else
", Unknown"
#endif
#endif #endif
")\n", TCC_VERSION); ")\n", TCC_VERSION);
break; break;
case 1: case 1:
printf("install: %s\n", s->tcc_lib_path); printf("install: %s\n", s->tcc_lib_path);
/* print_paths("programs", NULL, 0); */ /* print_paths("programs", NULL, 0); */
print_paths("crt", s->crt_paths, s->nb_crt_paths);
print_paths("libraries", s->library_paths, s->nb_library_paths);
print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths); print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
print_paths("libraries", s->library_paths, s->nb_library_paths);
#ifndef TCC_TARGET_PE
print_paths("crt", s->crt_paths, s->nb_crt_paths);
printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s)); printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
#endif
break; break;
} }
} }
static void help(TCCState *s) static void help(void)
{ {
display_info(s, 0); printf("Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
printf("Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
"Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n" "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
" tcc [options...] -run infile [arguments...]\n" " tcc [options...] -run infile [arguments...]\n"
"General options:\n" "General options:\n"
@ -94,13 +91,11 @@ static void help(TCCState *s)
" -dumpversion\n" " -dumpversion\n"
" -bench show compilation statistics\n" " -bench show compilation statistics\n"
"Preprocessor options:\n" "Preprocessor options:\n"
" -E preprocess only\n"
" -Idir add include path 'dir'\n" " -Idir add include path 'dir'\n"
" -Dsym[=val] define 'sym' with value 'val'\n" " -Dsym[=val] define 'sym' with value 'val'\n"
" -Usym undefine 'sym'\n" " -Usym undefine 'sym'\n"
" -P do not output a #line directive\n" " -E preprocess only\n"
" -P1 use a #line directive in output instead of the gcc style\n" " -P[1] no/alternative output of #line directives with -E\n"
" -dD put a define directive in the output (inside a comment)\n"
"Linker options:\n" "Linker options:\n"
" -Ldir add library path 'dir'\n" " -Ldir add library path 'dir'\n"
" -llib link with dynamic or static library 'lib'\n" " -llib link with dynamic or static library 'lib'\n"
@ -260,7 +255,7 @@ int main(int argc, char **argv)
tcc_set_environment(s); tcc_set_environment(s);
if (optind == 0) { if (optind == 0) {
help(s); help();
return 1; return 1;
} }

45
tcc.h
View File

@ -498,11 +498,12 @@ typedef struct BufferedFile {
int fd; int fd;
struct BufferedFile *prev; struct BufferedFile *prev;
int line_num; /* current line number - here to simplify code */ int line_num; /* current line number - here to simplify code */
int line_ref; /* moved from tcc_preprocess(), needed for a right ouput in other places */ int line_ref; /* tcc -E: last printed line */
int ifndef_macro; /* #ifndef macro / #endif search */ int ifndef_macro; /* #ifndef macro / #endif search */
int ifndef_macro_saved; /* saved ifndef_macro */ int ifndef_macro_saved; /* saved ifndef_macro */
int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */ int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
char filename[1024]; /* filename */ char filename[1024]; /* filename */
unsigned char unget[4];
unsigned char buffer[1]; /* extra size for CH_EOB char */ unsigned char buffer[1]; /* extra size for CH_EOB char */
} BufferedFile; } BufferedFile;
@ -524,11 +525,15 @@ typedef struct TokenString {
int len; int len;
int allocated_len; int allocated_len;
int last_line_num; int last_line_num;
/* used to chain token-strings with begin/end_macro() */
struct TokenString *prev;
const int *prev_ptr;
char alloc;
} TokenString; } TokenString;
/* inline functions */ /* inline functions */
typedef struct InlineFunc { typedef struct InlineFunc {
int *token_str; TokenString func_str;
Sym *sym; Sym *sym;
char filename[1]; char filename[1];
} InlineFunc; } InlineFunc;
@ -663,8 +668,7 @@ struct TCCState {
LINE_MACRO_OUTPUT_FORMAT_GCC, LINE_MACRO_OUTPUT_FORMAT_GCC,
LINE_MACRO_OUTPUT_FORMAT_NONE, LINE_MACRO_OUTPUT_FORMAT_NONE,
LINE_MACRO_OUTPUT_FORMAT_STD, LINE_MACRO_OUTPUT_FORMAT_STD,
} Pflag; } Pflag; /* -P switch */
int dflag; /* for keeping a -dD value */
/* for -MD/-MF: collected dependencies for this compilation */ /* for -MD/-MF: collected dependencies for this compilation */
char **target_deps; char **target_deps;
@ -685,6 +689,8 @@ struct TCCState {
/* #pragma pack stack */ /* #pragma pack stack */
int pack_stack[PACK_STACK_SIZE]; int pack_stack[PACK_STACK_SIZE];
int *pack_stack_ptr; int *pack_stack_ptr;
char **pragma_libs;
int nb_pragma_libs;
/* inline functions are stored as token lists and compiled last /* inline functions are stored as token lists and compiled last
only if referenced */ only if referenced */
@ -860,21 +866,21 @@ struct TCCState {
#define TOK_CDOUBLE 0xbc /* double constant */ #define TOK_CDOUBLE 0xbc /* double constant */
#define TOK_CLDOUBLE 0xbd /* long double constant */ #define TOK_CLDOUBLE 0xbd /* long double constant */
#define TOK_PPNUM 0xbe /* preprocessor number */ #define TOK_PPNUM 0xbe /* preprocessor number */
#define TOK_LINENUM 0xbf /* line number info */ #define TOK_PPSTR 0xbf /* preprocessor string */
#define TOK_LINENUM 0xc0 /* line number info */
/* <-- */ /* <-- */
#define TOK_TWOSHARPS 0xc0 /* ## preprocessing token */
#define TOK_PLCHLDR 0xc1 /* placeholder token as defined in C99 */
#define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */ #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
#define TOK_ADDC1 0xc3 /* add with carry generation */ #define TOK_ADDC1 0xc3 /* add with carry generation */
#define TOK_ADDC2 0xc4 /* add with carry use */ #define TOK_ADDC2 0xc4 /* add with carry use */
#define TOK_SUBC1 0xc5 /* add with carry generation */ #define TOK_SUBC1 0xc5 /* add with carry generation */
#define TOK_SUBC2 0xc6 /* add with carry use */ #define TOK_SUBC2 0xc6 /* add with carry use */
#define TOK_ARROW 0xcb #define TOK_ARROW 0xc7
#define TOK_DOTS 0xcc /* three dots */ #define TOK_DOTS 0xc8 /* three dots */
#define TOK_SHR 0xcd /* unsigned shift right */ #define TOK_SHR 0xc9 /* unsigned shift right */
#define TOK_NOSUBST 0xcf /* means following token has already been pp'd */ #define TOK_TWOSHARPS 0xca /* ## preprocessing token */
#define TOK_GNUCOMMA 0xd0 /* ,## preprocessing token */ #define TOK_PLCHLDR 0xcb /* placeholder token as defined in C99 */
#define TOK_NOSUBST 0xcc /* means following token has already been pp'd */
#define TOK_SHL 0x01 /* shift left */ #define TOK_SHL 0x01 /* shift left */
#define TOK_SAR 0x02 /* signed shift right */ #define TOK_SAR 0x02 /* signed shift right */
@ -1113,10 +1119,11 @@ ST_FUNC void tcc_close(void);
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype); ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype);
ST_FUNC int tcc_add_crt(TCCState *s, const char *filename); ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags); ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
ST_FUNC void tcc_add_pragma_libs(TCCState *s1);
PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f);
PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time); PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time);
PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv); PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv);
PUB_FUNC void tcc_set_environment(TCCState *s); PUB_FUNC void tcc_set_environment(TCCState *s);
/* ------------ tccpp.c ------------ */ /* ------------ tccpp.c ------------ */
@ -1148,9 +1155,12 @@ ST_DATA TokenSym **table_ident;
#define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */ #define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */
#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */ #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
#define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */ #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
#define PARSE_FLAG_TOK_STR 0x0040 /* return parsed strings instead of TOK_PPSTR */
ST_FUNC TokenSym *tok_alloc(const char *str, int len); ST_FUNC TokenSym *tok_alloc(const char *str, int len);
ST_FUNC char *get_tok_str(int v, CValue *cv); ST_FUNC const char *get_tok_str(int v, CValue *cv);
ST_FUNC void begin_macro(TokenString *str, int alloc);
ST_FUNC void end_macro(void);
ST_FUNC void save_parse_state(ParseState *s); ST_FUNC void save_parse_state(ParseState *s);
ST_FUNC void restore_parse_state(ParseState *s); ST_FUNC void restore_parse_state(ParseState *s);
ST_INLN void tok_str_new(TokenString *s); ST_INLN void tok_str_new(TokenString *s);
@ -1161,7 +1171,6 @@ ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
ST_FUNC void define_undef(Sym *s); ST_FUNC void define_undef(Sym *s);
ST_INLN Sym *define_find(int v); ST_INLN Sym *define_find(int v);
ST_FUNC void free_defines(Sym *b); ST_FUNC void free_defines(Sym *b);
ST_FUNC void print_defines(void);
ST_FUNC Sym *label_find(int v); ST_FUNC Sym *label_find(int v);
ST_FUNC Sym *label_push(Sym **ptop, int v, int flags); ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
ST_FUNC void label_pop(Sym **ptop, Sym *slast); ST_FUNC void label_pop(Sym **ptop, Sym *slast);
@ -1172,6 +1181,7 @@ ST_FUNC void next(void);
ST_INLN void unget_tok(int last_tok); ST_INLN void unget_tok(int last_tok);
ST_FUNC void preprocess_init(TCCState *s1); ST_FUNC void preprocess_init(TCCState *s1);
ST_FUNC void preprocess_new(void); ST_FUNC void preprocess_new(void);
ST_FUNC void preprocess_delete(void);
ST_FUNC int tcc_preprocess(TCCState *s1); ST_FUNC int tcc_preprocess(TCCState *s1);
ST_FUNC void skip(int c); ST_FUNC void skip(int c);
ST_FUNC NORETURN void expect(const char *msg); ST_FUNC NORETURN void expect(const char *msg);
@ -1204,7 +1214,7 @@ ST_DATA Sym *local_label_stack;
ST_DATA Sym *global_label_stack; ST_DATA Sym *global_label_stack;
ST_DATA Sym *define_stack; ST_DATA Sym *define_stack;
ST_DATA CType char_pointer_type, func_old_type, int_type, size_type; ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop; ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop, *pvtop;
#define vstack (__vstack + 1) #define vstack (__vstack + 1)
ST_DATA int rsym, anon_sym, ind, loc; ST_DATA int rsym, anon_sym, ind, loc;
@ -1215,8 +1225,9 @@ ST_DATA CType func_vt; /* current function return type (used by return instructi
ST_DATA int func_var; /* true if current function is variadic */ ST_DATA int func_var; /* true if current function is variadic */
ST_DATA int func_vc; ST_DATA int func_vc;
ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
ST_DATA char *funcname; ST_DATA const char *funcname;
ST_FUNC void check_vstack(void);
ST_INLN int is_float(int t); ST_INLN int is_float(int t);
ST_FUNC int ieee_finite(double d); ST_FUNC int ieee_finite(double d);
ST_FUNC void test_lvalue(void); ST_FUNC void test_lvalue(void);

View File

@ -747,7 +747,7 @@ static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
ch = file->buf_ptr[0]; ch = file->buf_ptr[0];
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
parse_flags = PARSE_FLAG_ASM_FILE; parse_flags = PARSE_FLAG_ASM_FILE | PARSE_FLAG_TOK_STR;
if (do_preprocess) if (do_preprocess)
parse_flags |= PARSE_FLAG_PREPROCESS; parse_flags |= PARSE_FLAG_PREPROCESS;
next(); next();

View File

@ -1605,6 +1605,8 @@ ST_FUNC void tcc_add_bcheck(TCCState *s1)
/* add tcc runtime libraries */ /* add tcc runtime libraries */
ST_FUNC void tcc_add_runtime(TCCState *s1) ST_FUNC void tcc_add_runtime(TCCState *s1)
{ {
tcc_add_pragma_libs(s1);
/* add libc */ /* add libc */
if (!s1->nostdlib) { if (!s1->nostdlib) {
tcc_add_library(s1, "c"); tcc_add_library(s1, "c");

View File

@ -59,7 +59,7 @@ ST_DATA int vlas_in_scope; /* number of VLAs that are currently in scope */
ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */ ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
ST_DATA int vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */ ST_DATA int vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop; ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop, *pvtop;
ST_DATA int const_wanted; /* true if constant wanted */ ST_DATA int const_wanted; /* true if constant wanted */
ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */ ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
@ -68,7 +68,7 @@ ST_DATA CType func_vt; /* current function return type (used by return instructi
ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */ ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
ST_DATA int func_vc; ST_DATA int func_vc;
ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
ST_DATA char *funcname; ST_DATA const char *funcname;
ST_DATA CType char_pointer_type, func_old_type, int_type, size_type; ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
@ -118,6 +118,12 @@ ST_FUNC void test_lvalue(void)
expect("lvalue"); expect("lvalue");
} }
ST_FUNC void check_vstack(void)
{
if (pvtop != vtop)
tcc_error("internal compiler error: vstack leak (%d)", vtop - pvtop);
}
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* symbol allocator */ /* symbol allocator */
static Sym *__sym_malloc(void) static Sym *__sym_malloc(void)
@ -4005,9 +4011,9 @@ ST_FUNC void unary(void)
break; break;
} }
case TOK___va_arg: { case TOK___va_arg: {
CType type;
if (nocode_wanted) if (nocode_wanted)
tcc_error("statement in global scope"); tcc_error("statement in global scope");
CType type;
next(); next();
skip('('); skip('(');
expr_eq(); expr_eq();
@ -5777,7 +5783,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
/* compute size */ /* compute size */
save_parse_state(&saved_parse_state); save_parse_state(&saved_parse_state);
macro_ptr = init_str.str; begin_macro(&init_str, 0);
next(); next();
decl_initializer(type, NULL, 0, 1, 1); decl_initializer(type, NULL, 0, 1, 1);
/* prepare second initializer parsing */ /* prepare second initializer parsing */
@ -5937,7 +5943,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
decl_initializer(type, sec, addr, 1, 0); decl_initializer(type, sec, addr, 1, 0);
/* restore parse state if needed */ /* restore parse state if needed */
if (init_str.str) { if (init_str.str) {
tok_str_free(init_str.str); end_macro();
restore_parse_state(&saved_parse_state); restore_parse_state(&saved_parse_state);
} }
/* patch flexible array member size back to -1, */ /* patch flexible array member size back to -1, */
@ -6018,6 +6024,7 @@ static void func_decl_list(Sym *func_sym)
static void gen_function(Sym *sym) static void gen_function(Sym *sym)
{ {
int saved_nocode_wanted = nocode_wanted; int saved_nocode_wanted = nocode_wanted;
nocode_wanted = 0; nocode_wanted = 0;
ind = cur_text_section->data_offset; ind = cur_text_section->data_offset;
/* NOTE: we patch the symbol size later */ /* NOTE: we patch the symbol size later */
@ -6034,11 +6041,9 @@ static void gen_function(Sym *sym)
sym_push2(&local_stack, SYM_FIELD, 0, 0); sym_push2(&local_stack, SYM_FIELD, 0, 0);
gfunc_prolog(&sym->type); gfunc_prolog(&sym->type);
#ifdef CONFIG_TCC_BCHECK #ifdef CONFIG_TCC_BCHECK
if (tcc_state->do_bounds_check if (tcc_state->do_bounds_check && !strcmp(funcname, "main")) {
&& !strcmp(get_tok_str(sym->v, NULL), "main")) {
int i; int i;
Sym *sym;
sym = local_stack;
for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) { for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) {
if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD) if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD)
break; break;
@ -6075,14 +6080,16 @@ static void gen_function(Sym *sym)
func_var = 0; /* for safety */ func_var = 0; /* for safety */
ind = 0; /* for safety */ ind = 0; /* for safety */
nocode_wanted = saved_nocode_wanted; nocode_wanted = saved_nocode_wanted;
check_vstack();
} }
ST_FUNC void gen_inline_functions(void) ST_FUNC void gen_inline_functions(void)
{ {
Sym *sym; Sym *sym;
int *str, inline_generated, i; int inline_generated, i, ln;
struct InlineFunc *fn; struct InlineFunc *fn;
ln = file->line_num;
/* iterate while inline function are referenced */ /* iterate while inline function are referenced */
for(;;) { for(;;) {
inline_generated = 0; inline_generated = 0;
@ -6092,18 +6099,17 @@ ST_FUNC void gen_inline_functions(void)
if (sym && sym->c) { if (sym && sym->c) {
/* the function was used: generate its code and /* the function was used: generate its code and
convert it to a normal function */ convert it to a normal function */
str = fn->token_str;
fn->sym = NULL; fn->sym = NULL;
if (file) if (file)
pstrcpy(file->filename, sizeof file->filename, fn->filename); pstrcpy(file->filename, sizeof file->filename, fn->filename);
sym->r = VT_SYM | VT_CONST; sym->r = VT_SYM | VT_CONST;
sym->type.t &= ~VT_INLINE; sym->type.t &= ~VT_INLINE;
macro_ptr = str; begin_macro(&fn->func_str, 0);
next(); next();
cur_text_section = text_section; cur_text_section = text_section;
gen_function(sym); gen_function(sym);
macro_ptr = NULL; /* fail safe */ end_macro();
inline_generated = 1; inline_generated = 1;
} }
@ -6111,10 +6117,12 @@ ST_FUNC void gen_inline_functions(void)
if (!inline_generated) if (!inline_generated)
break; break;
} }
file->line_num = ln;
/* free tokens of unused inline functions */
for (i = 0; i < tcc_state->nb_inline_fns; ++i) { for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
fn = tcc_state->inline_fns[i]; fn = tcc_state->inline_fns[i];
str = fn->token_str; if (fn->sym)
tok_str_free(str); tok_str_free(fn->func_str.str);
} }
dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns); dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
} }
@ -6267,19 +6275,22 @@ static int decl0(int l, int is_for_loop_init)
the compilation unit only if they are used */ the compilation unit only if they are used */
if ((type.t & (VT_INLINE | VT_STATIC)) == if ((type.t & (VT_INLINE | VT_STATIC)) ==
(VT_INLINE | VT_STATIC)) { (VT_INLINE | VT_STATIC)) {
TokenString func_str;
int block_level; int block_level;
struct InlineFunc *fn; struct InlineFunc *fn;
const char *filename; const char *filename;
tok_str_new(&func_str); filename = file ? file->filename : "";
fn = tcc_malloc(sizeof *fn + strlen(filename));
strcpy(fn->filename, filename);
fn->sym = sym;
tok_str_new(&fn->func_str);
block_level = 0; block_level = 0;
for(;;) { for(;;) {
int t; int t;
if (tok == TOK_EOF) if (tok == TOK_EOF)
tcc_error("unexpected end of file"); tcc_error("unexpected end of file");
tok_str_add_tok(&func_str); tok_str_add_tok(&fn->func_str);
t = tok; t = tok;
next(); next();
if (t == '{') { if (t == '{') {
@ -6290,13 +6301,8 @@ static int decl0(int l, int is_for_loop_init)
break; break;
} }
} }
tok_str_add(&func_str, -1); tok_str_add(&fn->func_str, -1);
tok_str_add(&func_str, 0); tok_str_add(&fn->func_str, 0);
filename = file ? file->filename : "";
fn = tcc_malloc(sizeof *fn + strlen(filename));
strcpy(fn->filename, filename);
fn->sym = sym;
fn->token_str = func_str.str;
dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn); dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
} else { } else {

View File

@ -1767,17 +1767,18 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe)
ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0, ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
SHN_UNDEF, start_symbol); SHN_UNDEF, start_symbol);
tcc_add_pragma_libs(s1);
if (0 == s1->nostdlib) { if (0 == s1->nostdlib) {
static const char *libs[] = { static const char *libs[] = {
"libtcc1.a", "msvcrt", "kernel32", "", "user32", "gdi32", NULL "tcc1", "msvcrt", "kernel32", "", "user32", "gdi32", NULL
}; };
const char **pp, *p; const char **pp, *p;
for (pp = libs; 0 != (p = *pp); ++pp) { for (pp = libs; 0 != (p = *pp); ++pp) {
if (0 == *p) { if (0 == *p) {
if (PE_DLL != pe_type && PE_GUI != pe_type) if (PE_DLL != pe_type && PE_GUI != pe_type)
break; break;
} else if (pp == libs ? tcc_add_dll(s1, p, 0) : tcc_add_library(s1, p)) { } else if (tcc_add_library_err(s1, p) < 0) {
tcc_error_noabort("cannot find library: %s", p);
break; break;
} }
} }
@ -1805,8 +1806,8 @@ ST_FUNC int pe_output_file(TCCState * s1, const char *filename)
pe.filename = filename; pe.filename = filename;
pe.s1 = s1; pe.s1 = s1;
tcc_add_bcheck(s1);
pe_add_runtime(s1, &pe); pe_add_runtime(s1, &pe);
tcc_add_bcheck(s1);
relocate_common_syms(); /* assign bss adresses */ relocate_common_syms(); /* assign bss adresses */
tcc_add_linker_symbols(s1); tcc_add_linker_symbols(s1);

1397
tccpp.c

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,7 @@ TESTS = \
test3 \ test3 \
abitest \ abitest \
vla_test-run \ vla_test-run \
moretests tests2-dir pp-dir
BTESTS = test1b test3b btest BTESTS = test1b test3b btest
@ -36,7 +36,7 @@ ifneq ($(ARCH),i386)
TESTS := $(filter-out $(BTESTS),$(TESTS)) TESTS := $(filter-out $(BTESTS),$(TESTS))
endif endif
ifdef CONFIG_WIN32 ifdef CONFIG_WIN32
TESTS := w32-prep $(filter-out $(BTESTS),$(TESTS)) TESTS := $(filter-out $(BTESTS),$(TESTS))
endif endif
ifeq ($(TARGETOS),Darwin) ifeq ($(TARGETOS),Darwin)
TESTS := $(filter-out hello-exe test3 $(BTESTS),$(TESTS)) TESTS := $(filter-out hello-exe test3 $(BTESTS),$(TESTS))
@ -60,9 +60,9 @@ ifeq ($(TARGETOS),Darwin)
endif endif
# run local version of tcc with local libraries and includes # run local version of tcc with local libraries and includes
TCCFLAGS = -B$(TOP) -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include TCCFLAGS = -B$(TOP) -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include -L$(TOP)
ifdef CONFIG_WIN32 ifdef CONFIG_WIN32
TCCFLAGS = -B$(top_srcdir)/win32 -I$(top_srcdir) -I$(top_srcdir)/include -I$(TOP) -L$(TOP) TCCFLAGS = -B$(top_srcdir)/win32 -I$(top_srcdir) -I$(top_srcdir)/include -L$(TOP)
endif endif
XTCCFLAGS = -B$(TOP) -B$(top_srcdir)/win32 -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include XTCCFLAGS = -B$(TOP) -B$(top_srcdir)/win32 -I$(TOP) -I$(top_srcdir) -I$(top_srcdir)/include
@ -97,17 +97,14 @@ hello-run: ../examples/ex1.c
libtest: libtcc_test$(EXESUF) $(LIBTCC1) libtest: libtcc_test$(EXESUF) $(LIBTCC1)
@echo ------------ $@ ------------ @echo ------------ $@ ------------
./libtcc_test$(EXESUF) lib_path=.. ./libtcc_test$(EXESUF) $(TCCFLAGS)
libtcc_test$(EXESUF): libtcc_test.c $(top_builddir)/$(LIBTCC) libtcc_test$(EXESUF): libtcc_test.c $(top_builddir)/$(LIBTCC)
$(CC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(NATIVE_DEFINES) $(LIBS) $(LINK_LIBTCC) $(LDFLAGS) -I$(top_srcdir) $(CC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(NATIVE_DEFINES) $(LIBS) $(LINK_LIBTCC) $(LDFLAGS) -I$(top_srcdir)
moretests: %-dir:
@echo ------------ $@ ------------ @echo ------------ $@ ------------
$(MAKE) -C tests2 $(MAKE) -k -C $*
w32-prep:
cp ../libtcc1.a ../lib
# test.ref - generate using cc # test.ref - generate using cc
test.ref: tcctest.c test.ref: tcctest.c
@ -223,9 +220,8 @@ endif
abitest: $(ABITESTS) abitest: $(ABITESTS)
@echo ------------ $@ ------------ @echo ------------ $@ ------------
./abitest-cc$(EXESUF) lib_path=.. include="$(top_srcdir)/include" ./abitest-cc$(EXESUF) $(TCCFLAGS)
if [ "$(CONFIG_arm_eabi)" != "yes" ]; then \ if [ "$(CONFIG_arm_eabi)" != "yes" ]; then ./abitest-tcc$(EXESUF) $(TCCFLAGS); fi
./abitest-tcc$(EXESUF) lib_path=.. include="$(top_srcdir)/include"; fi
vla_test$(EXESUF): vla_test.c vla_test$(EXESUF): vla_test.c
$(TCC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(TCC) -o $@ $^ $(CPPFLAGS) $(CFLAGS)
@ -253,5 +249,4 @@ clean:
$(MAKE) -C tests2 $@ $(MAKE) -C tests2 $@
rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc \ rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.cc \
*-cc *-tcc *.exe \ *-cc *-tcc *.exe \
hello libtcc_test vla_test tcctest[1234] ex? tcc_g tcclib.h \ hello libtcc_test vla_test tcctest[1234] ex? tcc_g
../lib/libtcc1.a

View File

@ -13,8 +13,24 @@
#define LONG_DOUBLE_LITERAL(x) x ## L #define LONG_DOUBLE_LITERAL(x) x ## L
#endif #endif
static const char *tccdir = NULL; static int g_argc;
static const char *include_dir = NULL; static char **g_argv;
static void set_options(TCCState *s, int argc, char **argv)
{
int i;
for (i = 1; i < argc; ++i) {
char *a = argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
tcc_set_lib_path(s, a+2);
else if (a[1] == 'I')
tcc_add_include_path(s, a+2);
else if (a[1] == 'L')
tcc_add_library_path(s, a+2);
}
}
}
typedef int (*callback_type) (void*); typedef int (*callback_type) (void*);
@ -29,12 +45,9 @@ static int run_callback(const char *src, callback_type callback) {
s = tcc_new(); s = tcc_new();
if (!s) if (!s)
return -1; return -1;
if (tccdir)
tcc_set_lib_path(s, tccdir); set_options(s, g_argc, g_argv);
if (include_dir) {
if (tcc_add_include_path(s, include_dir) == -1)
return -1;
}
if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1) if (tcc_set_output_type(s, TCC_OUTPUT_MEMORY) == -1)
return -1; return -1;
if (tcc_compile_string(s, src) == -1) if (tcc_compile_string(s, src) == -1)
@ -611,13 +624,11 @@ int main(int argc, char **argv) {
/* if tcclib.h and libtcc1.a are not installed, where can we find them */ /* if tcclib.h and libtcc1.a are not installed, where can we find them */
for (i = 1; i < argc; ++i) { for (i = 1; i < argc; ++i) {
if (!memcmp(argv[i], "lib_path=",9)) if (!memcmp(argv[i], "run_test=", 9))
tccdir = argv[i] + 9;
else if (!memcmp(argv[i], "run_test=", 9))
testname = argv[i] + 9; testname = argv[i] + 9;
else if (!memcmp(argv[i], "include=", 8)) }
include_dir = argv[i] + 8;
} g_argv = argv, g_argc = argc;
RUN_TEST(ret_int_test); RUN_TEST(ret_int_test);
RUN_TEST(ret_longlong_test); RUN_TEST(ret_longlong_test);

View File

@ -16,7 +16,7 @@ int add(int a, int b)
} }
char my_program[] = char my_program[] =
"#include <stdio.h> // printf()\n" "#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
"extern int add(int a, int b);\n" "extern int add(int a, int b);\n"
"int fib(int n)\n" "int fib(int n)\n"
"{\n" "{\n"
@ -37,6 +37,7 @@ char my_program[] =
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
TCCState *s; TCCState *s;
int i;
int (*func)(int); int (*func)(int);
s = tcc_new(); s = tcc_new();
@ -46,8 +47,17 @@ int main(int argc, char **argv)
} }
/* if tcclib.h and libtcc1.a are not installed, where can we find them */ /* if tcclib.h and libtcc1.a are not installed, where can we find them */
if (argc == 2 && !memcmp(argv[1], "lib_path=",9)) for (i = 1; i < argc; ++i) {
tcc_set_lib_path(s, argv[1]+9); char *a = argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
tcc_set_lib_path(s, a+2);
else if (a[1] == 'I')
tcc_add_include_path(s, a+2);
else if (a[1] == 'L')
tcc_add_library_path(s, a+2);
}
}
/* MUST BE CALLED before any compilation */ /* MUST BE CALLED before any compilation */
tcc_set_output_type(s, TCC_OUTPUT_MEMORY); tcc_set_output_type(s, TCC_OUTPUT_MEMORY);

6
tests/pp/01.c 100644
View File

@ -0,0 +1,6 @@
#define hash_hash # ## #
#define mkstr(a) # a
#define in_between(a) mkstr(a)
#define join(c, d) in_between(c hash_hash d)
char p[] = join(x, y);
// char p[] = "x ## y";

View File

@ -0,0 +1 @@
char p[] = "x ## y";

28
tests/pp/02.c 100644
View File

@ -0,0 +1,28 @@
#define x 3
#define f(a) f(x * (a))
#undef x
#define x 2
#define g f
#define z z[0]
#define h g(~
#define m(a) a(w)
#define w 0,1
#define t(a) a
#define p() int
#define q(x) x
#define r(x,y) x ## y
#define str(x) # x
f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
g(x+(3,4)-w) | h 5) & m
(f)^m(m);
char c[2][6] = { str(hello), str() };
/*
* f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
* f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
* char c[2][6] = { "hello", "" };
*/
#define L21 f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);
#define L22 g(x+(3,4)-w) | h 5) & m\
(f)^m(m);
L21
L22

View File

@ -0,0 +1,5 @@
f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);
char c[2][6] = { "hello", "" };
f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);
f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))^m(0,1);

15
tests/pp/03.c 100644
View File

@ -0,0 +1,15 @@
#define str(s) # s
#define xstr(s) str(s)
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s", \
x ## s, x ## t)
#define INCFILE(n) vers ## n
#define glue(a, b) a ## b
#define xglue(a, b) glue(a, b)
#define HIGHLOW "hello"
#define LOW LOW ", world"
debug(1, 2);
fputs(str(strncmp("abc\0d", "abc", '\4') // this goes away
== 0) str(: @\n), s);
\#include xstr(INCFILE(2).h)
glue(HIGH, LOW);
xglue(HIGH, LOW)

View File

@ -0,0 +1,5 @@
printf("x" "1" "= %d, x" "2" "= %s", x1, x2);
fputs("strncmp(\"abc\\0d\", \"abc\", '\\4') == 0" ": @\n", s);
\#include "vers2.h"
"hello";
"hello" ", world"

4
tests/pp/04.c 100644
View File

@ -0,0 +1,4 @@
#define foobar 1
#define C(x,y) x##y
#define D(x) (C(x,bar))
D(foo)

View File

@ -0,0 +1 @@
(1)

7
tests/pp/05.c 100644
View File

@ -0,0 +1,7 @@
#define t(x,y,z) x ## y ## z
#define xxx(s) int s[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,), \
t(10,,), t(,11,), t(,,12), t(,,) };
int j[] = { t(1,2,3), t(,4,5), t(6,,7), t(8,9,),
t(10,,), t(,11,), t(,,12), t(,,) };
xxx(j)

View File

@ -0,0 +1,3 @@
int j[] = { 123, 45, 67, 89,
10, 11, 12, };
int j[] = { 123, 45, 67, 89, 10, 11, 12, };

5
tests/pp/06.c 100644
View File

@ -0,0 +1,5 @@
#define X(a,b, \
c,d) \
foo
X(1,2,3,4)

View File

@ -0,0 +1 @@
foo

4
tests/pp/07.c 100644
View File

@ -0,0 +1,4 @@
#define a() YES
#define b() a
b()
b()()

View File

@ -0,0 +1,2 @@
a
YES

4
tests/pp/08.c 100644
View File

@ -0,0 +1,4 @@
// test macro expansion in arguments
#define s_pos s_s.s_pos
#define foo(x) (x)
foo(hej.s_pos)

View File

@ -0,0 +1 @@
(hej.s_s.s_pos)

4
tests/pp/09.c 100644
View File

@ -0,0 +1,4 @@
#define C(a,b,c) a##b##c
#define N(x,y) C(x,_,y)
#define A_O aaaaoooo
N(A,O)

View File

@ -0,0 +1 @@
aaaaoooo

10
tests/pp/10.c 100644
View File

@ -0,0 +1,10 @@
#define f(x) x
#define g(x) f(x) f(x
#define i(x) g(x)) g(x
#define h(x) i(x))) i(x
#define k(x) i(x))) i(x))))
f(x)
g(x))
i(x)))
h(x))))
k(x))))

View File

@ -0,0 +1,5 @@
x
x x
x x x x
x x x x x x x x
x x x x x x x x))))

31
tests/pp/11.c 100644
View File

@ -0,0 +1,31 @@
#define D1(s, ...) s
#define D2(s, ...) s D1(__VA_ARGS__)
#define D3(s, ...) s D2(__VA_ARGS__)
#define D4(s, ...) s D3(__VA_ARGS__)
D1(a)
D2(a, b)
D3(a, b, c)
D4(a, b, c, d)
x D4(a, b, c, d) y
x D4(a, b, c) y
x D4(a, b) y
x D4(a) y
x D4() y
#define GNU_COMMA(X,Y...) X,## Y
x GNU_COMMA(A,B,C) y
x GNU_COMMA(A,B) y
x GNU_COMMA(A) y
x GNU_COMMA() y
#define __sun_attr___noreturn__ __attribute__((__noreturn__))
#define ___sun_attr_inner(__a) __sun_attr_##__a
#define __sun_attr__(__a) ___sun_attr_inner __a
#define __NORETURN __sun_attr__((__noreturn__))
__NORETURN
#define X(...)
#define Y(...) 1 __VA_ARGS__ 2
Y(X X() ())

15
tests/pp/11.expect 100644
View File

@ -0,0 +1,15 @@
a
a b
a b c
a b c d
x a b c d y
x a b c y
x a b y
x a y
x y
x A,B,C y
x A,B y
x A y
x y
__attribute__((__noreturn__))
1 2

35
tests/pp/Makefile 100644
View File

@ -0,0 +1,35 @@
#
# credits: 01..13.c from the pcc cpp-tests suite
#
TCC = ../../tcc
TESTS = $(patsubst %.c,%.test,$(wildcard *.c))
all test : $(TESTS)
%.test: %.c %.expect
@echo PPTest $* ...
@$(TCC) -E -P $< >$*.output 2>&1 ; \
diff -Nu -b -B -I "^#" $(EXTRA_DIFF_OPTS) $*.expect $*.output \
&& rm -f $*.output
# automatically generate .expect files with gcc:
%.expect :
gcc -E -P $*.c >$*.expect 2>&1
# tell make not to delete
.PRECIOUS: %.expect
clean:
rm -vf *.output
# 02.test : EXTRA_DIFF_OPTS = -w
# 03.test : EXTRA_DIFF_OPTS = -w
# 04.test : EXTRA_DIFF_OPTS = -w
# 10.test : EXTRA_DIFF_OPTS = -w
# diff options:
# -b ighore space changes
# -w ighore all whitespace
# -B ignore blank lines
# -I <RE> ignore lines matching RE

View File

@ -316,6 +316,7 @@ void macro_test(void)
printf("__LINE__=%d __FILE__=%s\n", printf("__LINE__=%d __FILE__=%s\n",
__LINE__, __FILE__); __LINE__, __FILE__);
#if 0
#line 200 #line 200
printf("__LINE__=%d __FILE__=%s\n", printf("__LINE__=%d __FILE__=%s\n",
__LINE__, __FILE__); __LINE__, __FILE__);
@ -323,6 +324,7 @@ void macro_test(void)
printf("__LINE__=%d __FILE__=%s\n", printf("__LINE__=%d __FILE__=%s\n",
__LINE__, __FILE__); __LINE__, __FILE__);
#line 227 "tcctest.c" #line 227 "tcctest.c"
#endif
/* not strictly preprocessor, but we test it there */ /* not strictly preprocessor, but we test it there */
#ifdef C99_MACROS #ifdef C99_MACROS

View File

@ -1,2 +0,0 @@
#define paste(A,B) ##A B
paste(x,y)

View File

@ -1 +0,0 @@
65_macro_concat_start.c:1: error: '##' invalid at start of macro

View File

@ -1,2 +0,0 @@
#define paste(A,B) A B##
paste(x,y)

View File

@ -1 +0,0 @@
66_macro_concat_end.c:2: error: '##' invalid at end of macro

View File

@ -1,9 +0,0 @@
#include <stdio.h>
#define hexCh(c (c >= 10 ? 'a' + c - 10 : '0' + c)
int main(void)
{
int c = 0xa;
printf("hex: %c\n", hexCh(c));
return 0;
}

View File

@ -1 +0,0 @@
68_macro_param_list_err_1.c:2: error: '(' may not appear in parameter list

View File

@ -1,9 +0,0 @@
#include <stdio.h>
#define hexCh(c/3) (c >= 10 ? 'a' + c - 10 : '0' + c)
int main(void)
{
int c = 0xa;
printf("hex: %c\n", hexCh(c));
return 0;
}

View File

@ -1 +0,0 @@
69_macro_param_list_err_2.c:2: error: '/' may not appear in parameter list

View File

@ -13,5 +13,7 @@ int main()
printf("Error: 2147483647 < 0\n"); printf("Error: 2147483647 < 0\n");
return 2; return 2;
} }
else
printf("long long constant test ok.\n");
return 0; return 0;
} }

View File

@ -0,0 +1 @@
long long constant test ok.

View File

@ -19,89 +19,9 @@ endif
TCC = $(TOP)/tcc $(TCCFLAGS) TCC = $(TOP)/tcc $(TCCFLAGS)
TESTS = \ TESTS = $(patsubst %.c,%.test,$(wildcard *.c))
00_assignment.test \
01_comment.test \
02_printf.test \
03_struct.test \
04_for.test \
05_array.test \
06_case.test \
07_function.test \
08_while.test \
09_do_while.test \
10_pointer.test \
11_precedence.test \
12_hashdefine.test \
13_integer_literals.test \
14_if.test \
15_recursion.test \
16_nesting.test \
17_enum.test \
18_include.test \
19_pointer_arithmetic.test \
20_pointer_comparison.test \
21_char_array.test \
22_floating_point.test \
23_type_coercion.test \
24_math_library.test \
25_quicksort.test \
26_character_constants.test \
27_sizeof.test \
28_strings.test \
29_array_address.test \
30_hanoi.test \
31_args.test \
32_led.test \
33_ternary_op.test \
34_array_assignment.test \
35_sizeof.test \
36_array_initialisers.test \
37_sprintf.test \
38_multiple_array_index.test \
39_typedef.test \
40_stdio.test \
41_hashif.test \
42_function_pointer.test \
43_void_param.test \
44_scoped_declarations.test \
45_empty_for.test \
46_grep.test \
47_switch_return.test \
48_nested_break.test \
49_bracket_evaluation.test \
50_logical_second_arg.test \
51_static.test \
52_unnamed_enum.test \
54_goto.test \
55_lshift_type.test \
56_btype_excess-1.test \
57_btype_excess-2.test \
58_function_redefinition.test \
59_function_array.test \
60_enum_redefinition.test \
61_undefined_enum.test \
62_enumerator_redefinition.test \
63_local_enumerator_redefinition.test \
64_macro_nesting.test \
65_macro_concat_start.test \
66_macro_concat_end.test \
67_macro_concat.test \
68_macro_param_list_err_1.test \
69_macro_param_list_err_2.test \
70_floating_point_literals.test \
71_macro_empty_arg.test \
72_long_long_constant.test \
73_arm64.test \
74_nocode_wanted.test \
75_array_in_struct_init.test \
76_dollars_in_identifiers.test \
77_push_pop_macro.test \
78_vla_label.test \
79_vla_continue.test
# 34_array_assignment.test -- array assignment is not in C standard # 34_array_assignment.test -- array assignment is not in C standard
SKIP = 34_array_assignment.test SKIP = 34_array_assignment.test
# some tests do not pass on all platforms, remove them for now # some tests do not pass on all platforms, remove them for now
@ -128,16 +48,29 @@ ARGS =
FLAGS = FLAGS =
76_dollars_in_identifiers.test : FLAGS = -fdollars-in-identifiers 76_dollars_in_identifiers.test : FLAGS = -fdollars-in-identifiers
# Filter some always-warning
FILTER =
ifeq (-$(findstring arm,$(ARCH))-,-arm-)
FILTER = 2>&1 | grep -v 'warning: soft float ABI currently not supported'
endif
all test: $(filter-out $(SKIP),$(TESTS)) all test: $(filter-out $(SKIP),$(TESTS))
%.test: %.c %.test: %.c %.expect
@echo Test: $*... @echo Test: $*...
# test -run
@$(TCC) -run $(FLAGS) $< $(ARGS) 2>&1 | grep -v 'warning: soft float ABI currently not supported: default to softfp' >$*.output || true @$(TCC) $(FLAGS) -run $< $(ARGS) $(FILTER) >$*.output 2>&1 || true
@diff -Nbu $*.expect $*.output && rm -f $*.output @diff -Nbu $*.expect $*.output && rm -f $*.output
# test exe (disabled for speed)
# @($(TCC) $(FLAGS) $< -o $*.exe && ./$*.exe $(ARGS)) $(FiLTER) >$*.output2 2>&1 ; \
# diff -Nbu $*.expect $*.output2 && rm -f $*.output2 $*.exe
@($(TCC) $(FLAGS) $< -o $*.exe && ./$*.exe $(ARGS)) 2>&1 | grep -v 'warning: soft float ABI currently not supported: default to softfp' >$*.output2 || true # automatically generate .expect files with gcc:
@diff -Nbu $*.expect $*.output2 && rm -f $*.output2 $*.exe %.expect :
(gcc $*.c -o a.exe && ./a.exe $(ARGS)) >$*.expect 2>&1; rm -f a.exe
# tell make not to delete
.PRECIOUS: %.expect
clean: clean:
rm -vf fred.txt *.output* *.exe rm -vf fred.txt *.output a.exe

View File

@ -1254,12 +1254,12 @@ void gfunc_call(int nb_args)
in on the stack and swap it back to its original position in on the stack and swap it back to its original position
if it is a register. */ if it is a register. */
SValue tmp = vtop[0]; SValue tmp = vtop[0];
int arg_stored = 1;
vtop[0] = vtop[-i]; vtop[0] = vtop[-i];
vtop[-i] = tmp; vtop[-i] = tmp;
mode = classify_x86_64_arg(&vtop->type, NULL, &size, &align, &reg_count); mode = classify_x86_64_arg(&vtop->type, NULL, &size, &align, &reg_count);
int arg_stored = 1;
switch (vtop->type.t & VT_BTYPE) { switch (vtop->type.t & VT_BTYPE) {
case VT_STRUCT: case VT_STRUCT:
if (mode == x86_64_mode_sse) { if (mode == x86_64_mode_sse) {
@ -1413,9 +1413,10 @@ void gfunc_call(int nb_args)
} else if (mode == x86_64_mode_integer) { } else if (mode == x86_64_mode_integer) {
/* simple type */ /* simple type */
/* XXX: implicit cast ? */ /* XXX: implicit cast ? */
int d;
gen_reg -= reg_count; gen_reg -= reg_count;
r = gv(RC_INT); r = gv(RC_INT);
int d = arg_prepare_reg(gen_reg); d = arg_prepare_reg(gen_reg);
orex(1,d,r,0x89); /* mov */ orex(1,d,r,0x89); /* mov */
o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d)); o(0xc0 + REG_VALUE(r) * 8 + REG_VALUE(d));
if (reg_count == 2) { if (reg_count == 2) {
@ -2225,7 +2226,6 @@ ST_FUNC void gen_vla_sp_restore(int addr) {
/* Subtract from the stack pointer, and push the resulting value onto the stack */ /* Subtract from the stack pointer, and push the resulting value onto the stack */
ST_FUNC void gen_vla_alloc(CType *type, int align) { ST_FUNC void gen_vla_alloc(CType *type, int align) {
int r;
#ifdef TCC_TARGET_PE #ifdef TCC_TARGET_PE
/* alloca does more than just adjust %rsp on Windows */ /* alloca does more than just adjust %rsp on Windows */
vpush_global_sym(&func_old_type, TOK_alloca); vpush_global_sym(&func_old_type, TOK_alloca);
@ -2233,6 +2233,7 @@ ST_FUNC void gen_vla_alloc(CType *type, int align) {
gfunc_call(1); gfunc_call(1);
vset(type, REG_IRET, 0); vset(type, REG_IRET, 0);
#else #else
int r;
r = gv(RC_INT); /* allocation size */ r = gv(RC_INT); /* allocation size */
/* sub r,%rsp */ /* sub r,%rsp */
o(0x2b48); o(0x2b48);