From 40671f76e4ed685bb1ad774df0ac52715f81ee71 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 6 Jul 2020 18:12:35 +0200 Subject: [PATCH] Fix parsing of .s files those aren't preprocessed, but our use of a fake file in preprocess_start requires inline stack processing (which isn't done without preprocessing). Just don't try to setup anything requiring preprocessing at all in this case. --- libtcc.c | 6 ++---- tcc.h | 2 +- tccpp.c | 39 +++++++++++++++++++++------------------ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/libtcc.c b/libtcc.c index fbbbd16c..88612725 100644 --- a/libtcc.c +++ b/libtcc.c @@ -708,7 +708,6 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) tcc_enter_state(s1); if (setjmp(s1->error_jmp_buf) == 0) { - int is_asm; s1->error_set_jmp_enabled = 1; s1->nb_errors = 0; @@ -721,13 +720,12 @@ static int tcc_compile(TCCState *s1, int filetype, const char *str, int fd) file->fd = fd; } - is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP)); tccelf_begin_file(s1); - preprocess_start(s1, is_asm); + preprocess_start(s1, filetype); tccgen_init(s1); if (s1->output_type == TCC_OUTPUT_PREPROCESS) { tcc_preprocess(s1); - } else if (is_asm) { + } else if (filetype & (AFF_TYPE_ASM | AFF_TYPE_ASMPP)) { #ifdef CONFIG_TCC_ASM tcc_assemble(s1, !!(filetype & AFF_TYPE_ASMPP)); #else diff --git a/tcc.h b/tcc.h index 63bec3a4..bca6f65d 100644 --- a/tcc.h +++ b/tcc.h @@ -1365,7 +1365,7 @@ ST_FUNC void parse_define(void); ST_FUNC void preprocess(int is_bof); ST_FUNC void next(void); ST_INLN void unget_tok(int last_tok); -ST_FUNC void preprocess_start(TCCState *s1, int is_asm); +ST_FUNC void preprocess_start(TCCState *s1, int filetype); ST_FUNC void preprocess_end(TCCState *s1); ST_FUNC void tccpp_new(TCCState *s); ST_FUNC void tccpp_delete(TCCState *s); diff --git a/tccpp.c b/tccpp.c index 37592f6b..fd32cdca 100644 --- a/tccpp.c +++ b/tccpp.c @@ -3720,8 +3720,9 @@ static void tcc_predefs(CString *cstr) , -1); } -ST_FUNC void preprocess_start(TCCState *s1, int is_asm) +ST_FUNC void preprocess_start(TCCState *s1, int filetype) { + int is_asm = !!(filetype & (AFF_TYPE_ASM|AFF_TYPE_ASMPP)); CString cstr; tccpp_new(s1); @@ -3739,23 +3740,25 @@ ST_FUNC void preprocess_start(TCCState *s1, int is_asm) set_idnum('$', !is_asm && s1->dollars_in_identifiers ? IS_ID : 0); set_idnum('.', is_asm ? IS_ID : 0); - cstr_new(&cstr); - if (s1->cmdline_defs.size) - cstr_cat(&cstr, s1->cmdline_defs.data, s1->cmdline_defs.size); - cstr_printf(&cstr, "#define __BASE_FILE__ \"%s\"\n", file->filename); - if (is_asm) - cstr_printf(&cstr, "#define __ASSEMBLER__ 1\n"); - if (s1->output_type == TCC_OUTPUT_MEMORY) - cstr_printf(&cstr, "#define __TCC_RUN__ 1\n"); - if (!is_asm && s1->output_type != TCC_OUTPUT_PREPROCESS) - tcc_predefs(&cstr); - if (s1->cmdline_incl.size) - cstr_cat(&cstr, s1->cmdline_incl.data, s1->cmdline_incl.size); - //printf("%s\n", (char*)cstr.data); - *s1->include_stack_ptr++ = file; - tcc_open_bf(s1, "", cstr.size); - memcpy(file->buffer, cstr.data, cstr.size); - cstr_free(&cstr); + if (!(filetype & AFF_TYPE_ASM)) { + cstr_new(&cstr); + if (s1->cmdline_defs.size) + cstr_cat(&cstr, s1->cmdline_defs.data, s1->cmdline_defs.size); + cstr_printf(&cstr, "#define __BASE_FILE__ \"%s\"\n", file->filename); + if (is_asm) + cstr_printf(&cstr, "#define __ASSEMBLER__ 1\n"); + if (s1->output_type == TCC_OUTPUT_MEMORY) + cstr_printf(&cstr, "#define __TCC_RUN__ 1\n"); + if (!is_asm && s1->output_type != TCC_OUTPUT_PREPROCESS) + tcc_predefs(&cstr); + if (s1->cmdline_incl.size) + cstr_cat(&cstr, s1->cmdline_incl.data, s1->cmdline_incl.size); + //printf("%s\n", (char*)cstr.data); + *s1->include_stack_ptr++ = file; + tcc_open_bf(s1, "", cstr.size); + memcpy(file->buffer, cstr.data, cstr.size); + cstr_free(&cstr); + } parse_flags = is_asm ? PARSE_FLAG_ASM_FILE : 0; tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;