From 69c2e7f96c95ba088657ce8bb9754c12c3e89397 Mon Sep 17 00:00:00 2001 From: grischka Date: Wed, 24 Jul 2013 17:06:13 +0200 Subject: tccgen: fix crash with undeclared struct ... as in: #include int main() { struct asdasd x; printf("%d\n", sizeof(x)); } This fixes commit 17571298f30bf204fafe9cf1aca5258d2d087d63 --- tccgen.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 14a5a54..4849b6c 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2764,6 +2764,7 @@ static void struct_decl(CType *type, int u) v = anon_sym++; } type1.t = a; + type1.ref = NULL; /* 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 */ @@ -5337,12 +5338,13 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, flexible_array = NULL; if ((type->t & VT_BTYPE) == VT_STRUCT) { - Sym *field; - field = type->ref; - while (field && field->next) - field = field->next; - if (field->type.t & VT_ARRAY && field->type.ref->c < 0) - flexible_array = field; + 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; + } } size = type_size(type, &align); -- cgit v1.3.1 From 76cb1144ef91924c53c57ea71e6f67ce73ce1cc6 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Mon, 16 Sep 2013 14:48:33 +0200 Subject: Generate an error when a function is redefined Use one more bit in AttributeDef to differenciate between declared function (only its prototype is known) and defined function (its body is also known). This allows to generate an error in cases like: int f(){return 0;} int f(){return 1;} --- tcc.h | 4 +++- tccgen.c | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'tccgen.c') diff --git a/tcc.h b/tcc.h index 98f28db..5ed3e21 100644 --- a/tcc.h +++ b/tcc.h @@ -389,9 +389,10 @@ typedef struct AttributeDef { func_export : 1, func_import : 1, func_args : 5, + func_proto : 1, mode : 4, weak : 1, - fill : 11; + fill : 10; struct Section *section; int alias_target; /* token */ } AttributeDef; @@ -401,6 +402,7 @@ typedef struct AttributeDef { #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export) #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import) #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args) +#define FUNC_PROTO(r) (((AttributeDef*)&(r))->func_proto) #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned) #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed) #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode) diff --git a/tccgen.c b/tccgen.c index 4849b6c..a937358 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5818,6 +5818,10 @@ static int decl0(int l, int is_for_loop_init) goto func_error1; r = sym->type.ref->r; + + if (!FUNC_PROTO(r)) + tcc_error("redefinition of '%s'", get_tok_str(v, NULL)); + /* use func_call from prototype if not defined */ if (FUNC_CALL(r) != FUNC_CDECL && FUNC_CALL(type.ref->r) == FUNC_CDECL) @@ -5836,6 +5840,7 @@ static int decl0(int l, int is_for_loop_init) tcc_error("incompatible types for redefinition of '%s'", get_tok_str(v, NULL)); } + FUNC_PROTO(type.ref->r) = 0; /* if symbol is already defined, then put complete type */ sym->type = type; } else { @@ -5901,6 +5906,7 @@ static int decl0(int l, int is_for_loop_init) if ((type.t & VT_BTYPE) == VT_FUNC) { /* external function definition */ /* specific case for func_call attribute */ + ad.func_proto = 1; type.ref->r = INT_ATTR(&ad); } else if (!(type.t & VT_ARRAY)) { /* not lvalue if array */ -- cgit v1.3.1 From a465b7f58fdea15caa1bfb81ff5e985c94c4df4a Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Thu, 19 Sep 2013 18:58:46 +0200 Subject: Forbid the use of array of functions Prevent the following code from compiling: int (*fct)[42](int x); Reported-by: Abdul Wadud Mohammad Mohibur Rashid --- tccgen.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index a937358..33c42de 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3278,6 +3278,8 @@ static void post_type(CType *type, AttributeDef *ad) skip(']'); /* parse next post type */ post_type(type, ad); + if (type->t == VT_FUNC) + tcc_error("declaration of an array of functions"); t1 |= type->t & VT_VLA; if (t1 & VT_VLA) { -- cgit v1.3.1 From 0f522fb32a635dafce30f3ce3ff2cb15bcec809e Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Fri, 20 Sep 2013 01:06:43 +0200 Subject: Forbid enum redefinition. Prevent the following code from compiling: enum color {RED, GREEN, BLUE}; enum color {R, G, B}; int main() { return R; } Reported-by: John Haque --- tccgen.c | 1 + 1 file changed, 1 insertion(+) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 33c42de..53a2b6b 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2801,6 +2801,7 @@ static void struct_decl(CType *type, int u) if (tok == '}') break; } + s->c = type_size(&int_type, &align); skip('}'); } else { maxalign = 1; -- cgit v1.3.1 From 82969f045c99b4d1ef833de35117c17b326b46c0 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Fri, 20 Sep 2013 21:16:53 +0200 Subject: Report error when using undefined enum Prevent the following code from compiling: int main(void) { enum rgb c = 42; return c; } Reported-by: John Haque --- tccgen.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 53a2b6b..77ff87c 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2738,7 +2738,7 @@ static void parse_attribute(AttributeDef *ad) } /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */ -static void struct_decl(CType *type, int u) +static void struct_decl(CType *type, int u, int tdef) { int a, v, size, align, maxalign, c, offset; int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt; @@ -2759,7 +2759,8 @@ static void struct_decl(CType *type, int u) if (s->type.t != a) tcc_error("invalid type"); goto do_decl; - } + } else if (tok >= TOK_IDENT && !tdef) + tcc_error("unknown struct/union/enum"); } else { v = anon_sym++; } @@ -3014,14 +3015,14 @@ static int parse_btype(CType *type, AttributeDef *ad) } break; case TOK_ENUM: - struct_decl(&type1, VT_ENUM); + struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF); basic_type2: u = type1.t; type->ref = type1.ref; goto basic_type1; case TOK_STRUCT: case TOK_UNION: - struct_decl(&type1, VT_STRUCT); + struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF); goto basic_type2; /* type modifiers */ -- cgit v1.3.1 From 673befd2d7745a90c1c4fcb6d2f0e266c04f8c97 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Fri, 20 Sep 2013 22:49:49 +0200 Subject: Report error when redefining enumerator Prevent the following code from compiling: enum color {RED, GREEN, BLUE}; enum rgb {RED, G, B}; --- tccgen.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 77ff87c..0f0aac5 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2786,6 +2786,10 @@ static void struct_decl(CType *type, int u, int tdef) v = tok; if (v < TOK_UIDENT) expect("identifier"); + ss = sym_find(v); + if (ss) + tcc_error("redefinition of enumerator '%s'", + get_tok_str(v, NULL)); next(); if (tok == '=') { next(); -- cgit v1.3.1 From 0f5942c6b382105075dabb6f975a313efc63a5f9 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Tue, 24 Sep 2013 15:36:04 +0200 Subject: Avoid warnings with gcc 4.8 + default CFLAGS --- tccgen.c | 3 +-- x86_64-gen.c | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 0f0aac5..d5b915b 100644 --- a/tccgen.c +++ b/tccgen.c @@ -800,9 +800,9 @@ ST_FUNC int gv(int rc) #else if ((vtop->type.t & VT_BTYPE) == VT_LLONG) { int addr_type = VT_INT, load_size = 4, load_type = VT_INT; + unsigned long long ll; #endif int r2, original_type; - unsigned long long ll; original_type = vtop->type.t; /* two register type load : expand to two words temporarily */ @@ -3765,7 +3765,6 @@ ST_FUNC void unary(void) case TOK_builtin_va_arg_types: { CType type; - int bt; next(); skip('('); parse_type(&type); diff --git a/x86_64-gen.c b/x86_64-gen.c index d1bf75c..3cb211a 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -239,13 +239,6 @@ static int is64_type(int t) (t & VT_BTYPE) == VT_LLONG); } -static int is_sse_float(int t) { - int bt; - bt = t & VT_BTYPE; - return bt == VT_DOUBLE || bt == VT_FLOAT; -} - - /* instruction + 4 bytes data. Return the address of the data */ ST_FUNC int oad(int c, int s) { @@ -687,6 +680,12 @@ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) } } +static int is_sse_float(int t) { + int bt; + bt = t & VT_BTYPE; + return bt == VT_DOUBLE || bt == VT_FLOAT; +} + int gfunc_arg_size(CType *type) { int align; if (type->t & (VT_ARRAY|VT_BITFIELD)) @@ -989,7 +988,7 @@ static X86_64_Mode classify_x86_64_inner(CType *ty) { static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count) { X86_64_Mode mode; - int size, align, ret_t; + int size, align, ret_t = 0; if (ty->t & (VT_BITFIELD|VT_ARRAY)) { *psize = 8; @@ -1030,6 +1029,9 @@ static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *p ret_t = (size > 4) ? VT_DOUBLE : VT_FLOAT; } break; + case x86_64_mode_memory: /* avoid warning */ + case x86_64_mode_none: + tcc_error("argument type not handled in classify_x86_64_arg\n"); } } } @@ -1083,7 +1085,7 @@ void gfunc_call(int nb_args) { X86_64_Mode mode; CType type; - int size, align, r, args_size, stack_adjust, run_start, run_end, i, j, reg_count; + int size, align, r, args_size, stack_adjust, run_start, run_end, i, reg_count; int nb_reg_args = 0; int nb_sse_args = 0; int sse_reg, gen_reg; @@ -1133,6 +1135,8 @@ void gfunc_call(int nb_args) gen_reg -= reg_count; if (gen_reg + reg_count > REGN) goto stack_arg; break; + case x86_64_mode_none: /* avoid warning */ + tcc_error("argument type not handled in gfunc_call"); } } @@ -1366,7 +1370,7 @@ void gfunc_prolog(CType *func_type) { X86_64_Mode mode; int i, addr, align, size, reg_count; - int param_addr, reg_param_index, sse_param_index; + int param_addr = 0, reg_param_index, sse_param_index; Sym *sym; CType *type; @@ -1499,6 +1503,8 @@ void gfunc_prolog(CType *func_type) } break; } + case x86_64_mode_none: + tcc_error("argument type not handled in gfunc_prolog\n"); } sym_push(sym->v & ~SYM_FIELD, type, VT_LOCAL | VT_LVAL, param_addr); -- cgit v1.3.1 From 3b07a15fd12d5452bf539cd855edde8139db1686 Mon Sep 17 00:00:00 2001 From: Amine Najahi Date: Sun, 6 Oct 2013 14:43:16 +0200 Subject: Detect usage of incomplete types inside struct/union Make sure the only exception is for a flexible array member as the last element of a structure --- tccgen.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index d5b915b..bab4f7c 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2740,7 +2740,7 @@ static void parse_attribute(AttributeDef *ad) /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */ static void struct_decl(CType *type, int u, int tdef) { - int a, v, size, align, maxalign, c, offset; + int a, v, size, align, maxalign, c, offset, flexible; int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt; Sym *s, *ss, *ass, **ps; AttributeDef ad; @@ -2814,9 +2814,13 @@ static void struct_decl(CType *type, int u, int tdef) prevbt = VT_INT; bit_pos = 0; offset = 0; + flexible = 0; while (tok != '}') { parse_btype(&btype, &ad); while (1) { + if (flexible) + tcc_error("flexible array member '%s' not at the end of struct", + get_tok_str(v, NULL)); bit_size = -1; v = 0; type1 = btype; @@ -2824,6 +2828,13 @@ static void struct_decl(CType *type, int u, int tdef) type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT); if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT) expect("identifier"); + if (type_size(&type1, &align) < 0) { + if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c) + flexible = 1; + else + tcc_error("field '%s' has incomplete type", + get_tok_str(v, NULL)); + } if ((type1.t & VT_BTYPE) == VT_FUNC || (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE))) tcc_error("invalid type for '%s'", -- cgit v1.3.1 From 1c4afd13501f07a673aed5f130166f2ee0f30927 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Tue, 29 Oct 2013 22:10:02 +0800 Subject: Add support for thread-local storage variables --- elf.h | 4 +++- libtcc.c | 5 ++++- tccelf.c | 29 +++++++++++++++++++++-------- tccgen.c | 29 +++++++++++++++++++++++++---- tcctok.h | 1 + 5 files changed, 54 insertions(+), 14 deletions(-) (limited to 'tccgen.c') diff --git a/elf.h b/elf.h index 2983c75..039a697 100644 --- a/elf.h +++ b/elf.h @@ -447,6 +447,7 @@ typedef struct #define STT_SECTION 3 /* Symbol associated with a section */ #define STT_FILE 4 /* Symbol's name is file name */ #define STT_NUM 5 /* Number of defined types. */ +#define STT_TLS 6 /* Symbol is a thread-local data object */ #define STT_GNU_IFUNC 10 /* Symbol is a indirect code object */ #define STT_LOOS 11 /* Start of OS-specific */ #define STT_HIOS 12 /* End of OS-specific */ @@ -555,7 +556,8 @@ typedef struct #define PT_NOTE 4 /* Auxiliary information */ #define PT_SHLIB 5 /* Reserved */ #define PT_PHDR 6 /* Entry for header table itself */ -#define PT_NUM 7 /* Number of defined types. */ +#define PT_TLS 7 /* Thread-local program segment */ +#define PT_NUM 8 /* Number of defined types. */ #define PT_LOOS 0x60000000 /* Start of OS-specific */ #define PT_HIOS 0x6fffffff /* End of OS-specific */ #define PT_LOPROC 0x70000000 /* Start of processor-specific */ diff --git a/libtcc.c b/libtcc.c index f841eb0..fbea50b 100644 --- a/libtcc.c +++ b/libtcc.c @@ -444,7 +444,10 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, } else if ((sym->type.t & VT_BTYPE) == VT_VOID) { sym_type = STT_NOTYPE; } else { - sym_type = STT_OBJECT; + if (section && section->sh_flags & SHF_TLS) + sym_type = STT_TLS; + else + sym_type = STT_OBJECT; } if (sym->type.t & VT_STATIC) diff --git a/tccelf.c b/tccelf.c index 8af4bb6..4602ce8 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1543,6 +1543,7 @@ static int elf_output_file(TCCState *s1, const char *filename) int fd, mode, ret; int *section_order; int shnum, i, phnum, file_offset, offset, size, j, sh_order_index, k; + int have_tls_section = 0; long long tmp; addr_t addr; Section *strsec, *s; @@ -1861,6 +1862,11 @@ static int elf_output_file(TCCState *s1, const char *filename) /* we output all sections if debug or object file */ s->sh_size = s->data_offset; } + /* if tls section we'll need to add one segment */ + if (s->sh_flags & SHF_TLS) { + have_tls_section = 1; + phnum++; + } } /* allocate program segment headers */ @@ -1904,12 +1910,16 @@ static int elf_output_file(TCCState *s1, const char *filename) if (interp) ph += 1 + HAVE_PHDR; - for(j = 0; j < 2; j++) { - ph->p_type = PT_LOAD; - if (j == 0) - ph->p_flags = PF_R | PF_X; + for(j = 0; j < 2 + have_tls_section; j++) { + if (j != 2) + ph->p_type = PT_LOAD; else - ph->p_flags = PF_R | PF_W; + ph->p_type = PT_TLS; + ph->p_flags = PF_R; + if (j == 0) + ph->p_flags |= PF_X; + else if (j == 1) + ph->p_flags |= PF_W; ph->p_align = s1->section_align; /* we do the following ordering: interp, symbol tables, @@ -1920,13 +1930,16 @@ static int elf_output_file(TCCState *s1, const char *filename) s = s1->sections[i]; /* compute if section should be included */ if (j == 0) { - if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_TLS)) != SHF_ALLOC) continue; - } else { - if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != + } else if (j == 1) { + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_TLS)) != (SHF_ALLOC | SHF_WRITE)) continue; + } else { + if ((s->sh_flags & SHF_TLS) != SHF_TLS) + continue; } if (s == interp) { if (k != 0) diff --git a/tccgen.c b/tccgen.c index bab4f7c..bfe461f 100644 --- a/tccgen.c +++ b/tccgen.c @@ -31,6 +31,7 @@ ST_DATA int rsym, anon_sym, ind, loc; ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */ +ST_DATA Section *tdata_section, *tbss_section; /* thread-local storage sections */ ST_DATA Section *cur_text_section; /* current section where function code is generated */ #ifdef CONFIG_TCC_ASM ST_DATA Section *last_text_section; /* to handle .previous asm directive */ @@ -3092,6 +3093,10 @@ static int parse_btype(CType *type, AttributeDef *ad) t |= VT_INLINE; next(); break; + case TOK_THREAD: + t |= VT_TLS; + next(); + break; /* GNUC attribute */ case TOK_ATTRIBUTE1: @@ -5495,10 +5500,26 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, /* allocate symbol in corresponding section */ sec = ad->section; if (!sec) { - if (has_init) - sec = data_section; - else if (tcc_state->nocommon) - sec = bss_section; + if (has_init) { + if (type->t & VT_TLS) { + if (!tdata_section) + tdata_section = new_section(tcc_state, ".tdata", + SHT_PROGBITS, + SHF_ALLOC | SHF_WRITE | SHF_TLS); + sec = tdata_section; + } else + sec = data_section; + } + else if (tcc_state->nocommon) { + if (type->t & VT_TLS) { + if (!tbss_section) + tbss_section = new_section(tcc_state, ".tbss", + SHT_NOBITS, + SHF_ALLOC | SHF_WRITE | SHF_TLS); + sec = tbss_section; + } else + sec = bss_section; + } } if (sec) { data_offset = sec->data_offset; diff --git a/tcctok.h b/tcctok.h index 9b47a60..d55c9d7 100644 --- a/tcctok.h +++ b/tcctok.h @@ -10,6 +10,7 @@ DEF(TOK_FOR, "for") DEF(TOK_EXTERN, "extern") DEF(TOK_STATIC, "static") + DEF(TOK_THREAD, "__thread") DEF(TOK_UNSIGNED, "unsigned") DEF(TOK_GOTO, "goto") DEF(TOK_DO, "do") -- cgit v1.3.1 From cf02f920c148a77794b05ba09d73586e5f0b3601 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Sun, 3 Nov 2013 18:55:54 +0800 Subject: Revert "Add support for thread-local storage variables" TLS support in tinyCC is absolutely not ready: - segment register not select in load and store - no relocation added for computing offset of per-thread symbol - no support for TLS-specific relocations - no program header added as per Drepper document about TLS This reverts commit 1c4afd13501f07a673aed5f130166f2ee0f30927. --- elf.h | 4 +--- libtcc.c | 5 +---- tccelf.c | 29 ++++++++--------------------- tccgen.c | 29 ++++------------------------- tcctok.h | 1 - 5 files changed, 14 insertions(+), 54 deletions(-) (limited to 'tccgen.c') diff --git a/elf.h b/elf.h index 039a697..2983c75 100644 --- a/elf.h +++ b/elf.h @@ -447,7 +447,6 @@ typedef struct #define STT_SECTION 3 /* Symbol associated with a section */ #define STT_FILE 4 /* Symbol's name is file name */ #define STT_NUM 5 /* Number of defined types. */ -#define STT_TLS 6 /* Symbol is a thread-local data object */ #define STT_GNU_IFUNC 10 /* Symbol is a indirect code object */ #define STT_LOOS 11 /* Start of OS-specific */ #define STT_HIOS 12 /* End of OS-specific */ @@ -556,8 +555,7 @@ typedef struct #define PT_NOTE 4 /* Auxiliary information */ #define PT_SHLIB 5 /* Reserved */ #define PT_PHDR 6 /* Entry for header table itself */ -#define PT_TLS 7 /* Thread-local program segment */ -#define PT_NUM 8 /* Number of defined types. */ +#define PT_NUM 7 /* Number of defined types. */ #define PT_LOOS 0x60000000 /* Start of OS-specific */ #define PT_HIOS 0x6fffffff /* End of OS-specific */ #define PT_LOPROC 0x70000000 /* Start of processor-specific */ diff --git a/libtcc.c b/libtcc.c index fbea50b..f841eb0 100644 --- a/libtcc.c +++ b/libtcc.c @@ -444,10 +444,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, } else if ((sym->type.t & VT_BTYPE) == VT_VOID) { sym_type = STT_NOTYPE; } else { - if (section && section->sh_flags & SHF_TLS) - sym_type = STT_TLS; - else - sym_type = STT_OBJECT; + sym_type = STT_OBJECT; } if (sym->type.t & VT_STATIC) diff --git a/tccelf.c b/tccelf.c index 4602ce8..8af4bb6 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1543,7 +1543,6 @@ static int elf_output_file(TCCState *s1, const char *filename) int fd, mode, ret; int *section_order; int shnum, i, phnum, file_offset, offset, size, j, sh_order_index, k; - int have_tls_section = 0; long long tmp; addr_t addr; Section *strsec, *s; @@ -1862,11 +1861,6 @@ static int elf_output_file(TCCState *s1, const char *filename) /* we output all sections if debug or object file */ s->sh_size = s->data_offset; } - /* if tls section we'll need to add one segment */ - if (s->sh_flags & SHF_TLS) { - have_tls_section = 1; - phnum++; - } } /* allocate program segment headers */ @@ -1910,16 +1904,12 @@ static int elf_output_file(TCCState *s1, const char *filename) if (interp) ph += 1 + HAVE_PHDR; - for(j = 0; j < 2 + have_tls_section; j++) { - if (j != 2) - ph->p_type = PT_LOAD; - else - ph->p_type = PT_TLS; - ph->p_flags = PF_R; + for(j = 0; j < 2; j++) { + ph->p_type = PT_LOAD; if (j == 0) - ph->p_flags |= PF_X; - else if (j == 1) - ph->p_flags |= PF_W; + ph->p_flags = PF_R | PF_X; + else + ph->p_flags = PF_R | PF_W; ph->p_align = s1->section_align; /* we do the following ordering: interp, symbol tables, @@ -1930,15 +1920,12 @@ static int elf_output_file(TCCState *s1, const char *filename) s = s1->sections[i]; /* compute if section should be included */ if (j == 0) { - if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_TLS)) != + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != SHF_ALLOC) continue; - } else if (j == 1) { - if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_TLS)) != - (SHF_ALLOC | SHF_WRITE)) - continue; } else { - if ((s->sh_flags & SHF_TLS) != SHF_TLS) + if ((s->sh_flags & (SHF_ALLOC | SHF_WRITE)) != + (SHF_ALLOC | SHF_WRITE)) continue; } if (s == interp) { diff --git a/tccgen.c b/tccgen.c index bfe461f..bab4f7c 100644 --- a/tccgen.c +++ b/tccgen.c @@ -31,7 +31,6 @@ ST_DATA int rsym, anon_sym, ind, loc; ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */ -ST_DATA Section *tdata_section, *tbss_section; /* thread-local storage sections */ ST_DATA Section *cur_text_section; /* current section where function code is generated */ #ifdef CONFIG_TCC_ASM ST_DATA Section *last_text_section; /* to handle .previous asm directive */ @@ -3093,10 +3092,6 @@ static int parse_btype(CType *type, AttributeDef *ad) t |= VT_INLINE; next(); break; - case TOK_THREAD: - t |= VT_TLS; - next(); - break; /* GNUC attribute */ case TOK_ATTRIBUTE1: @@ -5500,26 +5495,10 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, /* allocate symbol in corresponding section */ sec = ad->section; if (!sec) { - if (has_init) { - if (type->t & VT_TLS) { - if (!tdata_section) - tdata_section = new_section(tcc_state, ".tdata", - SHT_PROGBITS, - SHF_ALLOC | SHF_WRITE | SHF_TLS); - sec = tdata_section; - } else - sec = data_section; - } - else if (tcc_state->nocommon) { - if (type->t & VT_TLS) { - if (!tbss_section) - tbss_section = new_section(tcc_state, ".tbss", - SHT_NOBITS, - SHF_ALLOC | SHF_WRITE | SHF_TLS); - sec = tbss_section; - } else - sec = bss_section; - } + if (has_init) + sec = data_section; + else if (tcc_state->nocommon) + sec = bss_section; } if (sec) { data_offset = sec->data_offset; diff --git a/tcctok.h b/tcctok.h index d55c9d7..9b47a60 100644 --- a/tcctok.h +++ b/tcctok.h @@ -10,7 +10,6 @@ DEF(TOK_FOR, "for") DEF(TOK_EXTERN, "extern") DEF(TOK_STATIC, "static") - DEF(TOK_THREAD, "__thread") DEF(TOK_UNSIGNED, "unsigned") DEF(TOK_GOTO, "goto") DEF(TOK_DO, "do") -- cgit v1.3.1 From dcec8673f21da86ae3dcf1ca3e9498127715b795 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Fri, 22 Nov 2013 09:27:15 +0800 Subject: Add support for struct > 4B returned via registers On ARM with hardfloat calling convention, structure containing 4 fields or less of the same float type are returned via float registers. This means that a structure can be returned in up to 4 double registers in a structure is composed of 4 doubles. This commit adds support for return of structures in several registers. --- arm-gen.c | 48 ++++++++++++++++++++++++++++++------------------ c67-gen.c | 5 +++-- i386-gen.c | 9 +++++---- tccgen.c | 46 +++++++++++++++++++++++++++++----------------- x86_64-gen.c | 18 ++++++++++-------- 5 files changed, 77 insertions(+), 49 deletions(-) (limited to 'tccgen.c') diff --git a/arm-gen.c b/arm-gen.c index 92b080d..6d0acd8 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -746,24 +746,6 @@ static void gcall_or_jmp(int is_jmp) } } -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { -#ifdef TCC_ARM_EABI - int size, align; - size = type_size(vt, &align); - if (size > 4) { - return 1; - } else { - *ret_align = 4; - ret->ref = NULL; - ret->t = VT_INT; - return 0; - } -#else - return 1; -#endif -} - #ifdef TCC_ARM_HARDFLOAT /* Return whether a structure is an homogeneous float aggregate or not. The answer is true if all the elements of the structure are of the same @@ -831,6 +813,33 @@ int assign_vfpreg(struct avail_regs *avregs, int align, int size) } #endif +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ +ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { +#ifdef TCC_ARM_EABI + int size, align; + size = type_size(vt, &align); +#ifdef TCC_ARM_HARDFLOAT + if (is_float(vt->t) || is_hgen_float_aggr(vt)) { + *ret_align = 8; + ret->ref = NULL; + ret->t = VT_DOUBLE; + return (size + 7) >> 3; + } else +#endif + if (size > 4) { + return 0; + } else { + *ret_align = 4; + ret->ref = NULL; + ret->t = VT_INT; + return 1; + } +#else + return 0; +#endif +} + /* Parameters are classified according to how they are copied to their final destination for the function call. Because the copying is performed class after class according to the order in the union below, it is important that @@ -1198,6 +1207,9 @@ void gfunc_prolog(CType *func_type) n = nf = 0; variadic = (func_type->ref->c == FUNC_ELLIPSIS); if((func_vt.t & VT_BTYPE) == VT_STRUCT +#ifdef TCC_ARM_HARDFLOAT + && (variadic || !is_hgen_float_aggr(&func_vt)) +#endif && type_size(&func_vt,&align) > 4) { n++; diff --git a/c67-gen.c b/c67-gen.c index 0d5e33f..1189dbb 100644 --- a/c67-gen.c +++ b/c67-gen.c @@ -1879,10 +1879,11 @@ static void gcall_or_jmp(int is_jmp) } } -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { *ret_align = 1; // Never have to re-align return values for x86-64 - return 1; + return 0; } /* generate function call with address in (vtop->t, vtop->c) and free function diff --git a/i386-gen.c b/i386-gen.c index 0a6d4d3..eaab2b7 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -374,7 +374,8 @@ static void gcall_or_jmp(int is_jmp) static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX }; static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX }; -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { #ifdef TCC_TARGET_PE @@ -383,11 +384,11 @@ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) *ret_align = 1; // Never have to re-align return values for x86 size = type_size(vt, &align); if (size > 8) { - return 1; + return 0; } else if (size > 4) { ret->ref = NULL; ret->t = VT_LLONG; - return 0; + return 1; } else { ret->ref = NULL; ret->t = VT_INT; @@ -395,7 +396,7 @@ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) } #else *ret_align = 1; // Never have to re-align return values for x86 - return 1; + return 0; #endif } diff --git a/tccgen.c b/tccgen.c index bab4f7c..500e99e 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3927,7 +3927,7 @@ ST_FUNC void unary(void) } else if (tok == '(') { SValue ret; Sym *sa; - int nb_args, sret; + int nb_args, sret, ret_align; /* function call */ if ((vtop->type.t & VT_BTYPE) != VT_FUNC) { @@ -3951,9 +3951,8 @@ ST_FUNC void unary(void) ret.r2 = VT_CONST; /* compute first implicit argument if a structure is returned */ if ((s->type.t & VT_BTYPE) == VT_STRUCT) { - int ret_align; sret = gfunc_sret(&s->type, &ret.type, &ret_align); - if (sret) { + if (!sret) { /* get some space for the returned structure */ size = type_size(&s->type, &align); loc = (loc - size) & -align; @@ -3966,11 +3965,11 @@ ST_FUNC void unary(void) nb_args++; } } else { - sret = 0; + sret = 1; ret.type = s->type; } - if (!sret) { + if (sret) { /* return in register */ if (is_float(ret.type.t)) { ret.r = reg_fret(ret.type.t); @@ -4010,18 +4009,23 @@ ST_FUNC void unary(void) vtop -= (nb_args + 1); } /* return value */ - vsetc(&ret.type, ret.r, &ret.c); - vtop->r2 = ret.r2; + for (r = ret.r + sret + !sret; r-- > ret.r;) { + vsetc(&ret.type, r, &ret.c); + vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */ + } /* handle packed struct return */ - if (((s->type.t & VT_BTYPE) == VT_STRUCT) && !sret) { - int addr; + if (((s->type.t & VT_BTYPE) == VT_STRUCT) && sret) { + int addr, offset; + size = type_size(&s->type, &align); loc = (loc - size) & -align; addr = loc; - vset(&ret.type, VT_LOCAL | VT_LVAL, addr); - vswap(); - vstore(); - vtop--; + for(offset = 0; offset < size; offset += ret_align) { + vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset); + vswap(); + vstore(); + vtop--; + } vset(&s->type, VT_LOCAL | VT_LVAL, addr); } } else { @@ -4593,7 +4597,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { CType type, ret_type; int ret_align; - if (gfunc_sret(&func_vt, &ret_type, &ret_align)) { + if (!gfunc_sret(&func_vt, &ret_type, &ret_align)) { /* if returning structure, must copy it to implicit first pointer arg location */ type = func_vt; @@ -4605,7 +4609,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, vstore(); } else { /* returning structure packed into registers */ - int size, addr, align; + int r, size, addr, offset, align; size = type_size(&func_vt,&align); if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1))) && (align & (ret_align-1))) { @@ -4619,9 +4623,17 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, } vtop->type = ret_type; if (is_float(ret_type.t)) - gv(rc_fret(ret_type.t)); + r = rc_fret(ret_type.t); else - gv(RC_IRET); + r = RC_IRET; + /* We assume that when a structure is returned in multiple + registers, their classes are consecutive values of the + suite s(n) = 2^n */ + for (offset = 0; offset < size; offset += ret_align, r<<=1) { + gv(r); + vtop->c.i += ret_align; + vtop->r = VT_LOCAL | VT_LVAL; + } } } else if (is_float(func_vt.t)) { gv(rc_fret(func_vt.t)); diff --git a/x86_64-gen.c b/x86_64-gen.c index 690236e..0962056 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -656,7 +656,8 @@ void gen_offs_sp(int b, int r, int d) } } -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { int size, align; @@ -664,19 +665,19 @@ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) size = type_size(vt, &align); ret->ref = NULL; if (size > 8) { - return 1; + return 0; } else if (size > 4) { ret->t = VT_LLONG; - return 0; + return 1; } else if (size > 2) { ret->t = VT_INT; - return 0; + return 1; } else if (size > 1) { ret->t = VT_SHORT; - return 0; + return 1; } else { ret->t = VT_BYTE; - return 0; + return 1; } } @@ -1056,11 +1057,12 @@ ST_FUNC int classify_x86_64_va_arg(CType *ty) { } } -/* Return 1 if this function returns via an sret pointer, 0 otherwise */ +/* Return the number of registers needed to return the struct, or 0 if + returning via struct pointer. */ int gfunc_sret(CType *vt, CType *ret, int *ret_align) { int size, align, reg_count; *ret_align = 1; // Never have to re-align return values for x86-64 - return (classify_x86_64_arg(vt, ret, &size, &align, ®_count) == x86_64_mode_memory); + return (classify_x86_64_arg(vt, ret, &size, &align, ®_count) != x86_64_mode_memory); } #define REGN 6 -- cgit v1.3.1 From fbc8810334e6a087bed6de4dd84635cb6037b4dc Mon Sep 17 00:00:00 2001 From: grischka Date: Mon, 16 Dec 2013 15:38:10 +0100 Subject: Fix "Add support for struct > 4B returned via registers" - avoid assumption "ret_align == register_size" which is false for non-arm targets - rename symbol "sret" to more descriptive "ret_nregs" This fixes commit dcec8673f21da86ae3dcf1ca3e9498127715b795 Also: - remove multiple definitions in win32/include/math.h --- i386-gen.c | 2 +- tccgen.c | 43 ++++++++++++++++++++++++++++--------------- win32/include/math.h | 2 ++ 3 files changed, 31 insertions(+), 16 deletions(-) (limited to 'tccgen.c') diff --git a/i386-gen.c b/i386-gen.c index eaab2b7..b26b844 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -392,7 +392,7 @@ ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) } else { ret->ref = NULL; ret->t = VT_INT; - return 0; + return 1; } #else *ret_align = 1; // Never have to re-align return values for x86 diff --git a/tccgen.c b/tccgen.c index 500e99e..bf208af 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3927,7 +3927,7 @@ ST_FUNC void unary(void) } else if (tok == '(') { SValue ret; Sym *sa; - int nb_args, sret, ret_align; + int nb_args, ret_nregs, ret_align; /* function call */ if ((vtop->type.t & VT_BTYPE) != VT_FUNC) { @@ -3951,8 +3951,8 @@ ST_FUNC void unary(void) ret.r2 = VT_CONST; /* compute first implicit argument if a structure is returned */ if ((s->type.t & VT_BTYPE) == VT_STRUCT) { - sret = gfunc_sret(&s->type, &ret.type, &ret_align); - if (!sret) { + ret_nregs = gfunc_sret(&s->type, &ret.type, &ret_align); + if (!ret_nregs) { /* get some space for the returned structure */ size = type_size(&s->type, &align); loc = (loc - size) & -align; @@ -3965,11 +3965,11 @@ ST_FUNC void unary(void) nb_args++; } } else { - sret = 1; + ret_nregs = 1; ret.type = s->type; } - if (sret) { + if (ret_nregs) { /* return in register */ if (is_float(ret.type.t)) { ret.r = reg_fret(ret.type.t); @@ -4008,23 +4008,30 @@ ST_FUNC void unary(void) } else { vtop -= (nb_args + 1); } + /* return value */ - for (r = ret.r + sret + !sret; r-- > ret.r;) { + for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) { vsetc(&ret.type, r, &ret.c); vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */ } + /* handle packed struct return */ - if (((s->type.t & VT_BTYPE) == VT_STRUCT) && sret) { + if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) { int addr, offset; size = type_size(&s->type, &align); loc = (loc - size) & -align; addr = loc; - for(offset = 0; offset < size; offset += ret_align) { + offset = 0; + for (;;) { vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset); vswap(); vstore(); vtop--; + if (--ret_nregs == 0) + break; + /* XXX: compatible with arm only: ret_align == register_size */ + offset += ret_align; } vset(&s->type, VT_LOCAL | VT_LVAL, addr); } @@ -4596,8 +4603,9 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, gen_assign_cast(&func_vt); if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { CType type, ret_type; - int ret_align; - if (!gfunc_sret(&func_vt, &ret_type, &ret_align)) { + int ret_align, ret_nregs; + ret_nregs = gfunc_sret(&func_vt, &ret_type, &ret_align); + if (0 == ret_nregs) { /* if returning structure, must copy it to implicit first pointer arg location */ type = func_vt; @@ -4609,7 +4617,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, vstore(); } else { /* returning structure packed into registers */ - int r, size, addr, offset, align; + int r, size, addr, align; size = type_size(&func_vt,&align); if ((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & (ret_align-1))) && (align & (ret_align-1))) { @@ -4626,11 +4634,16 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, r = rc_fret(ret_type.t); else r = RC_IRET; - /* We assume that when a structure is returned in multiple - registers, their classes are consecutive values of the - suite s(n) = 2^n */ - for (offset = 0; offset < size; offset += ret_align, r<<=1) { + + for (;;) { gv(r); + 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; + /* XXX: compatible with arm only: ret_align == register_size */ vtop->c.i += ret_align; vtop->r = VT_LOCAL | VT_LVAL; } diff --git a/win32/include/math.h b/win32/include/math.h index 984a717..4fe64e7 100644 --- a/win32/include/math.h +++ b/win32/include/math.h @@ -666,6 +666,7 @@ extern "C" { extern long double __cdecl fmal (long double, long double, long double); +#if 0 // gr: duplicate, see below /* 7.12.14 */ /* * With these functions, comparisons involving quiet NaNs set the FP @@ -708,6 +709,7 @@ extern "C" { & 0x4500) == 0x4500) #endif +#endif //0 #endif /* __STDC_VERSION__ >= 199901L */ -- cgit v1.3.1 From 3eed3506b4bf5b31eca4001d43d211a20c2376f1 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Sat, 4 Jan 2014 12:56:14 +0800 Subject: Fix negation of 0.0 and -0.0 --- tccgen.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index bf208af..e8f7f82 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3794,9 +3794,22 @@ ST_FUNC void unary(void) break; case '-': next(); - vpushi(0); unary(); - gen_op('-'); + t = vtop->type.t & VT_BTYPE; + /* handle (-)0.0 */ + if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST && + is_float(t)) { + if (t == VT_FLOAT) + vtop->c.f = -vtop->c.f; + else if (t == VT_DOUBLE) + vtop->c.d = -vtop->c.d; + else + vtop->c.ld = -vtop->c.ld; + } else { + vpushi(0); + vswap(); + gen_op('-'); + } break; case TOK_LAND: if (!gnu_ext) -- cgit v1.3.1 From eda2c756edc4dca004ba217d5bf361235dd9de1f Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Tue, 31 Dec 2013 23:51:20 +0800 Subject: Move logic for if (int value) to tccgen.c Move the logic to do a test of an integer value (ex if (0)) out of arch-specific code to tccgen.c to avoid code duplication. This also fixes test of long long value which was only testing the bottom half of such values on 32 bits architectures. --- arm-gen.c | 25 +------------------------ c67-gen.c | 33 +-------------------------------- i386-gen.c | 19 +------------------ il-gen.c | 15 +-------------- tccgen.c | 44 ++++++++++++++++++++++++++++++++------------ x86_64-gen.c | 19 +------------------ 6 files changed, 37 insertions(+), 118 deletions(-) (limited to 'tccgen.c') diff --git a/arm-gen.c b/arm-gen.c index 0aa07b1..eecb7d2 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -1390,7 +1390,7 @@ int gtst(int inv, int t) op|=encbranch(r,t,1); o(op); t=r; - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ if ((v & 1) == inv) { if(!vtop->c.i) vtop->c.i=t; @@ -1412,29 +1412,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t)) { - r=gv(RC_FLOAT); -#ifdef TCC_ARM_VFP - o(0xEEB50A40|(vfpr(r)<<12)|T2CPR(vtop->type.t)); /* fcmpzX */ - o(0xEEF1FA10); /* fmstat */ -#else - o(0xEE90F118|(fpr(r)<<16)); -#endif - vtop->r = VT_CMP; - vtop->c.i = TOK_NE; - return gtst(inv, t); - } else if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) { - /* constant jmp optimization */ - if ((vtop->c.i != 0) != inv) - t = gjmp(t); - } else { - v = gv(RC_INT); - o(0xE3300000|(intr(v)<<16)); - vtop->r = VT_CMP; - vtop->c.i = TOK_NE; - return gtst(inv, t); - } } vtop--; return t; diff --git a/c67-gen.c b/c67-gen.c index 1189dbb..df4b5d3 100644 --- a/c67-gen.c +++ b/c67-gen.c @@ -2103,7 +2103,7 @@ int gtst(int inv, int t) C67_NOP(5); t = ind1; //return where we need to patch - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -2129,37 +2129,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t)) { - 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); - } else { - // I think we need to get the value on the stack - // into a register, test it, and generate a branch - // return the address of the branch, so it can be - // later patched - - v = gv(RC_INT); // get value into a reg - ind1 = ind; - C67_MVKL(C67_A0, t); //r=reg to load, constant - C67_MVKH(C67_A0, t); //r=reg to load, constant - - if (v != TREG_EAX && // check if not already in a conditional test reg - v != TREG_EDX && v != TREG_ST0 && v != C67_B2) { - C67_MV(v, C67_B2); - v = C67_B2; - } - - C67_IREG_B_REG(inv, v, C67_A0); // [!R] B.S2x A0 - C67_NOP(5); - t = ind1; //return where we need to patch - ind1 = ind; - } } vtop--; return t; diff --git a/i386-gen.c b/i386-gen.c index ebc0d14..2cb31ff 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -682,7 +682,7 @@ ST_FUNC int gtst(int inv, int t) /* fast case : can jump directly since flags are set */ g(0x0f); t = psym((vtop->c.i - 16) ^ inv, t); - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -695,23 +695,6 @@ ST_FUNC int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t) || - (vtop->type.t & VT_BTYPE) == VT_LLONG) { - 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); - } else { - v = gv(RC_INT); - o(0x85); - o(0xc0 + v * 9); - g(0x0f); - t = psym(0x85 ^ inv, t); - } } vtop--; return t; diff --git a/il-gen.c b/il-gen.c index 170f436..33f9f36 100644 --- a/il-gen.c +++ b/il-gen.c @@ -515,7 +515,7 @@ int gtst(int inv, int t) break; } t = out_opj(c, t); - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -528,19 +528,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->t)) { - vpushi(0); - gen_op(TOK_NE); - } - if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_FORWARD)) == VT_CONST) { - /* constant jmp optimization */ - if ((vtop->c.i != 0) != inv) - t = gjmp(t); - } else { - v = gv(RC_INT); - t = out_opj(IL_OP_BRTRUE - inv, t); - } } vtop--; return t; diff --git a/tccgen.c b/tccgen.c index e8f7f82..55b03e6 100644 --- a/tccgen.c +++ b/tccgen.c @@ -1095,6 +1095,26 @@ static void gv_dup(void) } } +/* Generate value test + * + * Generate a test for any value (jump, comparison and integers) */ +int gvtst(int inv, int t) +{ + 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); +} + #ifndef TCC_TARGET_X86_64 /* generate CPU independent (unsigned) long long operations */ static void gen_opl(int op) @@ -1293,13 +1313,13 @@ static void gen_opl(int op) b = 0; gen_op(op1); if (op1 != TOK_NE) { - a = gtst(1, 0); + a = gvtst(1, 0); } if (op != TOK_EQ) { /* generate non equal test */ /* XXX: NOT PORTABLE yet */ if (a == 0) { - b = gtst(0, 0); + b = gvtst(0, 0); } else { #if defined(TCC_TARGET_I386) b = psym(0x850f, 0); @@ -1324,7 +1344,7 @@ static void gen_opl(int op) else if (op1 == TOK_GE) op1 = TOK_UGE; gen_op(op1); - a = gtst(1, a); + a = gvtst(1, a); gsym(b); vseti(VT_JMPI, a); break; @@ -3665,7 +3685,7 @@ ST_FUNC void unary(void) vtop->c.i = vtop->c.i ^ 1; else { save_regs(1); - vseti(VT_JMP, gtst(1, 0)); + vseti(VT_JMP, gvtst(1, 0)); } break; case '~': @@ -4182,7 +4202,7 @@ static void expr_land(void) t = 0; save_regs(1); for(;;) { - t = gtst(1, t); + t = gvtst(1, t); if (tok != TOK_LAND) { vseti(VT_JMPI, t); break; @@ -4202,7 +4222,7 @@ static void expr_lor(void) t = 0; save_regs(1); for(;;) { - t = gtst(0, t); + t = gvtst(0, t); if (tok != TOK_LOR) { vseti(VT_JMP, t); break; @@ -4264,9 +4284,9 @@ static void expr_cond(void) } if (tok == ':' && gnu_ext) { gv_dup(); - tt = gtst(1, 0); + tt = gvtst(1, 0); } else { - tt = gtst(1, 0); + tt = gvtst(1, 0); gexpr(); } type1 = vtop->type; @@ -4512,7 +4532,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, skip('('); gexpr(); skip(')'); - a = gtst(1, 0); + a = gvtst(1, 0); block(bsym, csym, case_sym, def_sym, case_reg, 0); c = tok; if (c == TOK_ELSE) { @@ -4529,7 +4549,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, skip('('); gexpr(); skip(')'); - a = gtst(1, 0); + a = gvtst(1, 0); b = 0; block(&a, &b, case_sym, def_sym, case_reg, 0); gjmp_addr(d); @@ -4707,7 +4727,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, b = 0; if (tok != ';') { gexpr(); - a = gtst(1, 0); + a = gvtst(1, 0); } skip(';'); if (tok != ')') { @@ -4736,7 +4756,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, skip('('); gsym(b); gexpr(); - c = gtst(0, 0); + c = gvtst(0, 0); gsym_addr(c, d); skip(')'); gsym(a); diff --git a/x86_64-gen.c b/x86_64-gen.c index 1550c07..fe028d9 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -1582,7 +1582,7 @@ int gtst(int inv, int t) } g(0x0f); t = psym((vtop->c.i - 16) ^ inv, t); - } else if (v == VT_JMP || v == VT_JMPI) { + } else { /* VT_JMP || VT_JMPI */ /* && or || optimization */ if ((v & 1) == inv) { /* insert vtop->c jump list in t */ @@ -1595,23 +1595,6 @@ int gtst(int inv, int t) t = gjmp(t); gsym(vtop->c.i); } - } else { - if (is_float(vtop->type.t) || - (vtop->type.t & VT_BTYPE) == VT_LLONG) { - 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); - } else { - v = gv(RC_INT); - orex(0,v,v,0x85); - o(0xc0 + REG_VALUE(v) * 9); - g(0x0f); - t = psym(0x85 ^ inv, t); - } } vtop--; return t; -- cgit v1.3.1 From 8efaa711904b897f9a4821656ac10f980c5ae9fe Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Mon, 6 Jan 2014 22:27:39 +0800 Subject: Fix struct ret in variadic fct with ARM hardfloat The procedure calling standard for ARM architecture mandate the use of the base standard for variadic function. Therefore, hgen float aggregate must be returned via stack when greater than 4 bytes and via core registers else in case of variadic function. This patch improve gfunc_sret() to take into account whether the function is variadic or not and make use of gfunc_sret() return value to determine whether to pass a structure via stack in gfunc_prolog(). It also take advantage of knowing if a function is variadic or not move float result value from VFP register to core register in gfunc_epilog(). --- arm-gen.c | 34 ++++++++++++++++++---------------- c67-gen.c | 3 ++- i386-gen.c | 3 ++- il-gen.c | 1 + tcc.h | 3 ++- tccgen.c | 11 ++++++++--- x86_64-gen.c | 3 ++- 7 files changed, 35 insertions(+), 23 deletions(-) (limited to 'tccgen.c') diff --git a/arm-gen.c b/arm-gen.c index eecb7d2..9e6c638 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -839,12 +839,12 @@ int floats_in_core_regs(SValue *sval) /* Return the number of registers needed to return the struct, or 0 if returning via struct pointer. */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { #ifdef TCC_ARM_EABI int size, align; size = type_size(vt, &align); #ifdef TCC_ARM_HARDFLOAT - if (is_float(vt->t) || is_hgen_float_aggr(vt)) { + if (!variadic && (is_float(vt->t) || is_hgen_float_aggr(vt))) { *ret_align = 8; ret->ref = NULL; ret->t = VT_DOUBLE; @@ -1221,21 +1221,19 @@ void gfunc_call(int nb_args) void gfunc_prolog(CType *func_type) { Sym *sym,*sym2; - int n,nf,size,align, variadic, struct_ret = 0; + int n, nf, size, align, struct_ret = 0; #ifdef TCC_ARM_HARDFLOAT struct avail_regs avregs = AVAIL_REGS_INITIALIZER; #endif + CType ret_type; sym = func_type->ref; func_vt = sym->type; + func_var = (func_type->ref->c == FUNC_ELLIPSIS); n = nf = 0; - variadic = (func_type->ref->c == FUNC_ELLIPSIS); - if((func_vt.t & VT_BTYPE) == VT_STRUCT -#ifdef TCC_ARM_HARDFLOAT - && (variadic || !is_hgen_float_aggr(&func_vt)) -#endif - && type_size(&func_vt,&align) > 4) + if ((func_vt.t & VT_BTYPE) == VT_STRUCT && + !gfunc_sret(&func_vt, func_var, &ret_type, &align)) { n++; struct_ret = 1; @@ -1244,7 +1242,7 @@ void gfunc_prolog(CType *func_type) for(sym2=sym->next;sym2 && (n<4 || nf<16);sym2=sym2->next) { size = type_size(&sym2->type, &align); #ifdef TCC_ARM_HARDFLOAT - if (!variadic && (is_float(sym2->type.t) + if (!func_var && (is_float(sym2->type.t) || is_hgen_float_aggr(&sym2->type))) { int tmpnf = assign_vfpreg(&avregs, align, size); tmpnf += (size + 3) / 4; @@ -1255,9 +1253,9 @@ void gfunc_prolog(CType *func_type) n += (size + 3) / 4; } o(0xE1A0C00D); /* mov ip,sp */ - if(variadic) + if (func_var) n=4; - if(n) { + if (n) { if(n>4) n=4; #ifdef TCC_ARM_EABI @@ -1289,7 +1287,7 @@ void gfunc_prolog(CType *func_type) size = (size + 3) >> 2; align = (align + 3) & ~3; #ifdef TCC_ARM_HARDFLOAT - if (!variadic && (is_float(sym->type.t) + if (!func_var && (is_float(sym->type.t) || is_hgen_float_aggr(&sym->type))) { int fpn = assign_vfpreg(&avregs, align, size << 2); if (fpn >= 0) { @@ -1329,10 +1327,14 @@ void gfunc_epilog(void) { uint32_t x; int diff; + /* Copy float return value to core register if base standard is used and + float computation is made with VFP */ #ifdef TCC_ARM_EABI - /* Useless but harmless copy of the float result into main register(s) in case - of variadic function in the hardfloat variant */ - if(is_float(func_vt.t)) { + if ( +#ifdef TCC_ARM_HARDFLOAT + func_var && +#endif + is_float(func_vt.t)) { if((func_vt.t & VT_BTYPE) == VT_FLOAT) o(0xEE100A10); /* fmrs r0, s0 */ else { diff --git a/c67-gen.c b/c67-gen.c index df4b5d3..f2baea5 100644 --- a/c67-gen.c +++ b/c67-gen.c @@ -1881,7 +1881,7 @@ static void gcall_or_jmp(int is_jmp) /* Return the number of registers needed to return the struct, or 0 if returning via struct pointer. */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) { +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { *ret_align = 1; // Never have to re-align return values for x86-64 return 0; } @@ -1971,6 +1971,7 @@ void gfunc_prolog(CType * func_type) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->type; + func_var = (sym->c == FUNC_ELLIPSIS); if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { func_vc = addr; addr += 4; diff --git a/i386-gen.c b/i386-gen.c index 4201ac2..2eb6922 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -376,7 +376,7 @@ static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX }; /* Return the number of registers needed to return the struct, or 0 if returning via struct pointer. */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { #ifdef TCC_TARGET_PE int size, align; @@ -527,6 +527,7 @@ ST_FUNC void gfunc_prolog(CType *func_type) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->type; + func_var = (sym->c == FUNC_ELLIPSIS); #ifdef TCC_TARGET_PE size = type_size(&func_vt,&align); if (((func_vt.t & VT_BTYPE) == VT_STRUCT) && (size > 8)) { diff --git a/il-gen.c b/il-gen.c index 33f9f36..9e1ec64 100644 --- a/il-gen.c +++ b/il-gen.c @@ -441,6 +441,7 @@ void gfunc_prolog(int t) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->t; + func_var = (sym->c == FUNC_ELLIPSIS); if ((func_vt & VT_BTYPE) == VT_STRUCT) { func_vc = addr; addr++; diff --git a/tcc.h b/tcc.h index 50642b7..21957e7 100644 --- a/tcc.h +++ b/tcc.h @@ -1176,6 +1176,7 @@ 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 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) */ +ST_DATA int func_var; /* true if current function is variadic */ ST_DATA int func_vc; ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ ST_DATA char *funcname; @@ -1288,7 +1289,7 @@ ST_FUNC void gsym_addr(int t, int a); ST_FUNC void gsym(int t); ST_FUNC void load(int r, SValue *sv); ST_FUNC void store(int r, SValue *v); -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *align); +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align); ST_FUNC void gfunc_call(int nb_args); ST_FUNC void gfunc_prolog(CType *func_type); ST_FUNC void gfunc_epilog(void); diff --git a/tccgen.c b/tccgen.c index 55b03e6..8355aae 100644 --- a/tccgen.c +++ b/tccgen.c @@ -66,6 +66,7 @@ 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 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) */ +ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */ ST_DATA int func_vc; ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */ ST_DATA char *funcname; @@ -3960,7 +3961,7 @@ ST_FUNC void unary(void) } else if (tok == '(') { SValue ret; Sym *sa; - int nb_args, ret_nregs, ret_align; + int nb_args, ret_nregs, ret_align, variadic; /* function call */ if ((vtop->type.t & VT_BTYPE) != VT_FUNC) { @@ -3984,7 +3985,9 @@ ST_FUNC void unary(void) ret.r2 = VT_CONST; /* compute first implicit argument if a structure is returned */ if ((s->type.t & VT_BTYPE) == VT_STRUCT) { - ret_nregs = gfunc_sret(&s->type, &ret.type, &ret_align); + variadic = (s->c == FUNC_ELLIPSIS); + ret_nregs = gfunc_sret(&s->type, variadic, &ret.type, + &ret_align); if (!ret_nregs) { /* get some space for the returned structure */ size = type_size(&s->type, &align); @@ -4637,7 +4640,8 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, if ((func_vt.t & VT_BTYPE) == VT_STRUCT) { CType type, ret_type; int ret_align, ret_nregs; - ret_nregs = gfunc_sret(&func_vt, &ret_type, &ret_align); + ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type, + &ret_align); if (0 == ret_nregs) { /* if returning structure, must copy it to implicit first pointer arg location */ @@ -5747,6 +5751,7 @@ static void gen_function(Sym *sym) cur_text_section = NULL; funcname = ""; /* for safety */ func_vt.t = VT_VOID; /* for safety */ + func_var = 0; /* for safety */ ind = 0; /* for safety */ nocode_wanted = saved_nocode_wanted; } diff --git a/x86_64-gen.c b/x86_64-gen.c index fe028d9..9aee875 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -658,7 +658,7 @@ void gen_offs_sp(int b, int r, int d) /* Return the number of registers needed to return the struct, or 0 if returning via struct pointer. */ -ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *ret_align) +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) { int size, align; *ret_align = 1; // Never have to re-align return values for x86-64 @@ -833,6 +833,7 @@ void gfunc_prolog(CType *func_type) /* if the function returns a structure, then add an implicit pointer parameter */ func_vt = sym->type; + func_var = (sym->c == FUNC_ELLIPSIS); size = gfunc_arg_size(&func_vt); if (size > 8) { gen_modrm64(0x89, arg_regs[reg_param_index], VT_LOCAL, NULL, addr); -- cgit v1.3.1 From 2bd0daabbe1fc40e65e4a4631e68f5ca093ea1fb Mon Sep 17 00:00:00 2001 From: grischka Date: Mon, 6 Jan 2014 19:56:26 +0100 Subject: misc. fixes - tccgen: error out for cast to void, as in void foo(void) { return 1; } This avoids an assertion failure in x86_64-gen.c, also. also fix tests2/03_struct.c accordingly - Error: "memory full" - be more specific - Makefiles: remove circular dependencies, lookup tcctest.c from VPATH - tcc.h: cleanup lib, include, crt and libgcc search paths" avoid duplication or trailing slashes with no CONFIG_MULTIARCHDIR (as from 9382d6f1a0e2d0104a82ed805207d9e742c6b068) - tcc.h: remove ";{B}" from PE search path in ce5e12c2f950052d8109b6b7a56d900547705c08 James Lyon wrote: "... I'm not sure this is the right way to fix this problem." And the answer is: No, please. (copying libtcc1.a for tests instead) - win32/build_tcc.bat: do not move away a versioned file --- Makefile | 3 --- lib/Makefile | 3 --- libtcc.c | 4 ++-- tcc.h | 30 +++++++++++++++--------------- tccgen.c | 8 ++++---- tccpp.c | 4 ++-- tests/Makefile | 39 ++++++++++++++++++++------------------- tests/tests2/03_struct.c | 2 +- tests/tests2/Makefile | 3 --- win32/build-tcc.bat | 4 ++-- 10 files changed, 46 insertions(+), 54 deletions(-) (limited to 'tccgen.c') diff --git a/Makefile b/Makefile index 24a7c09..5052758 100644 --- a/Makefile +++ b/Makefile @@ -362,9 +362,6 @@ tar: tcc-doc.html rm -rf $(TCC-VERSION) git reset -Makefile: $(top_srcdir)/Makefile - cp $< $@ - .PHONY: all clean tar distclean install uninstall FORCE endif # ifeq ($(TOP),.) diff --git a/lib/Makefile b/lib/Makefile index a8a2b5d..394df67 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -111,6 +111,3 @@ $(DIR)/exists : clean : rm -rfv i386-win32 x86_64-win32 i386 x86_64 - -Makefile: $(top_srcdir)/lib/Makefile - cp $< $@ diff --git a/libtcc.c b/libtcc.c index df201ae..072b77f 100644 --- a/libtcc.c +++ b/libtcc.c @@ -214,7 +214,7 @@ PUB_FUNC void *tcc_malloc(unsigned long size) void *ptr; ptr = malloc(size); if (!ptr && size) - tcc_error("memory full"); + tcc_error("memory full (malloc)"); #ifdef MEM_DEBUG mem_cur_size += malloc_usable_size(ptr); if (mem_cur_size > mem_max_size) @@ -239,7 +239,7 @@ PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size) #endif ptr1 = realloc(ptr, size); if (!ptr1 && size) - tcc_error("memory full"); + tcc_error("memory full (realloc)"); #ifdef MEM_DEBUG /* NOTE: count not correct if alloc error, but not critical */ mem_cur_size += malloc_usable_size(ptr1); diff --git a/tcc.h b/tcc.h index 21957e7..0933b01 100644 --- a/tcc.h +++ b/tcc.h @@ -169,13 +169,18 @@ #ifndef CONFIG_LDDIR # define CONFIG_LDDIR "lib" #endif -#ifndef CONFIG_MULTIARCHDIR -#define CONFIG_MULTIARCHDIR + +#ifdef CONFIG_MULTIARCHDIR +# define USE_MUADIR(s) s "/" CONFIG_MULTIARCHDIR +# define ALSO_MUADIR(s) s "/" CONFIG_MULTIARCHDIR ":" s +#else +# define USE_MUADIR(s) s +# define ALSO_MUADIR(s) s #endif /* path to find crt1.o, crti.o and crtn.o */ #ifndef CONFIG_TCC_CRTPREFIX -# define CONFIG_TCC_CRTPREFIX CONFIG_SYSROOT "/usr/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR +# define CONFIG_TCC_CRTPREFIX USE_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) #endif /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */ @@ -186,10 +191,8 @@ # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi" # else # define CONFIG_TCC_SYSINCLUDEPATHS \ - CONFIG_SYSROOT "/usr/local/include/" CONFIG_MULTIARCHDIR \ - ":" CONFIG_SYSROOT "/usr/local/include" \ - ":" CONFIG_SYSROOT "/usr/include/" CONFIG_MULTIARCHDIR \ - ":" CONFIG_SYSROOT "/usr/include" \ + ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/include") \ + ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/include") \ ":" "{B}/include" # endif #endif @@ -197,15 +200,12 @@ /* library search paths */ #ifndef CONFIG_TCC_LIBPATHS # ifdef TCC_TARGET_PE -# define CONFIG_TCC_LIBPATHS "{B}/lib;{B}" +# define CONFIG_TCC_LIBPATHS "{B}/lib" # else # define CONFIG_TCC_LIBPATHS \ - CONFIG_SYSROOT "/usr/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR \ - ":" CONFIG_SYSROOT "/usr/" CONFIG_LDDIR \ - ":" CONFIG_SYSROOT "/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR \ - ":" CONFIG_SYSROOT "/" CONFIG_LDDIR \ - ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR \ - ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR + ALSO_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \ + ":" ALSO_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) \ + ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR) # endif #endif @@ -237,7 +237,7 @@ #endif /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */ -#define TCC_LIBGCC CONFIG_SYSROOT "/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR "/libgcc_s.so.1" +#define TCC_LIBGCC USE_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1" /* -------------------------------------------- */ /* include the target specific definitions */ diff --git a/tccgen.c b/tccgen.c index 8355aae..f23cd07 100644 --- a/tccgen.c +++ b/tccgen.c @@ -309,7 +309,7 @@ static void vsetc(CType *type, int r, CValue *vc) int v; if (vtop >= vstack + (VSTACK_SIZE - 1)) - tcc_error("memory full"); + tcc_error("memory full (vstack)"); /* cannot let cpu flags if other instruction are generated. Also avoid leaving VT_JMP anywhere except on the top of the stack because it would complicate the code generator. */ @@ -483,7 +483,7 @@ ST_FUNC void vswap(void) ST_FUNC void vpushv(SValue *v) { if (vtop >= vstack + (VSTACK_SIZE - 1)) - tcc_error("memory full"); + tcc_error("memory full (vstack)"); vtop++; *vtop = *v; } @@ -2348,8 +2348,8 @@ static void gen_assign_cast(CType *dt) st = &vtop->type; /* source type */ dbt = dt->t & VT_BTYPE; sbt = st->t & VT_BTYPE; - if (sbt == VT_VOID) - tcc_error("Cannot assign void value"); + if (sbt == VT_VOID || dbt == VT_VOID) + tcc_error("cannot cast from/to void"); if (dt->t & VT_CONSTANT) tcc_warning("assignment of read-only location"); switch(dbt) { diff --git a/tccpp.c b/tccpp.c index aeaf6be..e1ccded 100644 --- a/tccpp.c +++ b/tccpp.c @@ -197,7 +197,7 @@ static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len) int i; if (tok_ident >= SYM_FIRST_ANOM) - tcc_error("memory full"); + tcc_error("memory full (symbols)"); /* expand token table if needed */ i = tok_ident - TOK_IDENT; @@ -1528,7 +1528,7 @@ include_done: c = (define_find(tok) != 0) ^ c; do_if: if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE) - tcc_error("memory full"); + tcc_error("memory full (ifdef)"); *s1->ifdef_stack_ptr++ = c; goto test_skip; case TOK_ELSE: diff --git a/tests/Makefile b/tests/Makefile index 08dfa42..b958a48 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -27,7 +27,7 @@ ifneq ($(ARCH),i386) TESTS := $(filter-out btest,$(TESTS)) endif ifdef CONFIG_WIN32 - TESTS := $(filter-out test3,$(TESTS)) + TESTS := w32-prep $(filter-out test3,$(TESTS)) endif ifeq ($(TARGETOS),Darwin) TESTS := $(filter-out hello-exe test3 btest,$(TESTS)) @@ -84,6 +84,9 @@ moretests: @echo ------------ $@ ------------ $(MAKE) -C tests2 +w32-prep: + cp ../libtcc1.a ../lib + # test.ref - generate using gcc # copy only tcclib.h so GCC's stddef and stdarg will be used test.ref: tcctest.c @@ -91,41 +94,41 @@ test.ref: tcctest.c ./tcctest.gcc > $@ # auto test -test1: test.ref +test1: tcctest.c test.ref @echo ------------ $@ ------------ - $(TCC) -run $(SRCDIR)/tcctest.c > test.out1 + $(TCC) -run $< > test.out1 @if diff -u test.ref test.out1 ; then echo "Auto Test OK"; fi # iterated test2 (compile tcc then compile tcctest.c !) -test2: test.ref +test2: tcctest.c test.ref @echo ------------ $@ ------------ - $(TCC) $(RUN_TCC) $(RUN_TCC) -run $(SRCDIR)/tcctest.c > test.out2 + $(TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out2 @if diff -u test.ref test.out2 ; then echo "Auto Test2 OK"; fi # iterated test3 (compile tcc then compile tcc then compile tcctest.c !) -test3: test.ref +test3: tcctest.c test.ref @echo ------------ $@ ------------ - $(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -run $(SRCDIR)/tcctest.c > test.out3 + $(TCC) $(RUN_TCC) $(RUN_TCC) $(RUN_TCC) -run $< > test.out3 @if diff -u test.ref test.out3 ; then echo "Auto Test3 OK"; fi # binary output test -test4: test.ref +test4: tcctest.c test.ref @echo ------------ $@ ------------ # object + link output - $(TCC) -c -o tcctest3.o $(SRCDIR)/tcctest.c + $(TCC) -c -o tcctest3.o $< $(TCC) -o tcctest3 tcctest3.o ./tcctest3 > test3.out @if diff -u test.ref test3.out ; then echo "Object Auto Test OK"; fi # dynamic output - $(TCC) -o tcctest1 $(SRCDIR)/tcctest.c + $(TCC) -o tcctest1 $< ./tcctest1 > test1.out @if diff -u test.ref test1.out ; then echo "Dynamic Auto Test OK"; fi # dynamic output + bound check - $(TCC) -b -o tcctest4 $(SRCDIR)/tcctest.c + $(TCC) -b -o tcctest4 $< ./tcctest4 > test4.out @if diff -u test.ref test4.out ; then echo "BCheck Auto Test OK"; fi # static output - $(TCC) -static -o tcctest2 $(SRCDIR)/tcctest.c + $(TCC) -static -o tcctest2 $< ./tcctest2 > test2.out @if diff -u test.ref test2.out ; then echo "Static Auto Test OK"; fi @@ -161,9 +164,9 @@ speedtest: ex2 ex3 time ./ex3 35 time $(TCC) -run $(top_srcdir)/examples/ex3.c 35 -weaktest: test.ref - $(TCC) -c tcctest.c -o weaktest.tcc.o $(CPPFLAGS) $(CFLAGS) - $(CC) -c tcctest.c -o weaktest.gcc.o -I. $(CPPFLAGS) -w $(CFLAGS) +weaktest: tcctest.c test.ref + $(TCC) -c $< -o weaktest.tcc.o $(CPPFLAGS) $(CFLAGS) + $(CC) -c $< -o weaktest.gcc.o -I. $(CPPFLAGS) -w $(CFLAGS) objdump -t weaktest.tcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.tcc.o.txt objdump -t weaktest.gcc.o | grep ' w ' | sed -e 's/.* \([a-zA-Z0-9_]*\)$$/\1/' | LC_ALL=C sort > weaktest.gcc.o.txt diff weaktest.gcc.o.txt weaktest.tcc.o.txt && echo "Weak Auto Test OK" @@ -220,7 +223,5 @@ cache: tcc_g clean: $(MAKE) -C tests2 $@ rm -vf *~ *.o *.a *.bin *.i *.ref *.out *.out? *.out?b *.gcc *.exe \ - hello libtcc_test tcctest[1234] ex? tcc_g tcclib.h - -Makefile: $(SRCDIR)/Makefile - cp $< $@ + hello libtcc_test tcctest[1234] ex? tcc_g tcclib.h \ + ../lib/libtcc1.a diff --git a/tests/tests2/03_struct.c b/tests/tests2/03_struct.c index df0d3e7..c5d48c5 100644 --- a/tests/tests2/03_struct.c +++ b/tests/tests2/03_struct.c @@ -6,7 +6,7 @@ struct fred int natasha; }; -void main() +int main() { struct fred bloggs; diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index 369ed47..51dc38d 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -96,6 +96,3 @@ all test: $(TESTS) clean: rm -vf fred.txt *.output - -Makefile: $(top_srcdir)/tests/tests2/Makefile - cp $< $@ diff --git a/win32/build-tcc.bat b/win32/build-tcc.bat index 772ed26..bd897c4 100644 --- a/win32/build-tcc.bat +++ b/win32/build-tcc.bat @@ -63,5 +63,5 @@ del *.o echo>..\config.texi @set VERSION %VERSION% if not exist doc md doc makeinfo --html --no-split -o doc\tcc-doc.html ../tcc-doc.texi -if exist tcc-win32.txt move tcc-win32.txt doc\ -copy ..\tests\libtcc_test.c examples\ +copy tcc-win32.txt doc +copy ..\tests\libtcc_test.c examples -- cgit v1.3.1 From 3fe2a95d7fe854c8cf496a009a467bb65cdf030d Mon Sep 17 00:00:00 2001 From: grischka Date: Tue, 7 Jan 2014 14:57:07 +0100 Subject: be stricter with aliasing Refactoring (no logical changes): - use memcpy in tccgen.c:ieee_finite(double d) - use union to store attribute flags in Sym Makefile: "CFLAGS+=-fno-strict-aliasing" basically not necessary anymore but I left it for now because gcc sometimes behaves unexpectedly without. Also: - configure: back to mode 100755 - tcc.h: remove unused variables tdata/tbss_section - x86_64-gen.c: adjust gfunc_sret for prototype --- configure | 0 i386-gen.c | 4 +-- libtcc.c | 8 ++--- tcc.h | 57 ++++++++++++++++------------------- tccgen.c | 97 +++++++++++++++++++++++++++++++----------------------------- x86_64-gen.c | 15 ++++++---- 6 files changed, 92 insertions(+), 89 deletions(-) mode change 100644 => 100755 configure (limited to 'tccgen.c') diff --git a/configure b/configure old mode 100644 new mode 100755 diff --git a/i386-gen.c b/i386-gen.c index 4c4a54b..ece054b 100644 --- a/i386-gen.c +++ b/i386-gen.c @@ -457,7 +457,7 @@ ST_FUNC void gfunc_call(int nb_args) } save_regs(0); /* save used temporary registers */ func_sym = vtop->type.ref; - func_call = FUNC_CALL(func_sym->r); + func_call = func_sym->a.func_call; /* fast call case */ if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) || func_call == FUNC_FASTCALLW) { @@ -505,7 +505,7 @@ ST_FUNC void gfunc_prolog(CType *func_type) CType *type; sym = func_type->ref; - func_call = FUNC_CALL(sym->r); + func_call = sym->a.func_call; addr = 8; loc = 0; func_vc = 0; diff --git a/libtcc.c b/libtcc.c index 072b77f..154a266 100644 --- a/libtcc.c +++ b/libtcc.c @@ -493,11 +493,11 @@ ST_FUNC void put_extern_sym2(Sym *sym, Section *section, if (sym->type.t & VT_EXPORT) other |= 1; if (sym_type == STT_FUNC && sym->type.ref) { - int attr = sym->type.ref->r; - if (FUNC_EXPORT(attr)) + Sym *ref = sym->type.ref; + if (ref->a.func_export) other |= 1; - if (FUNC_CALL(attr) == FUNC_STDCALL && can_add_underscore) { - sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr) * PTR_SIZE); + if (ref->a.func_call == FUNC_STDCALL && can_add_underscore) { + sprintf(buf1, "_%s@%d", name, ref->a.func_args * PTR_SIZE); name = buf1; other |= 2; can_add_underscore = 0; diff --git a/tcc.h b/tcc.h index 0933b01..f3d868f 100644 --- a/tcc.h +++ b/tcc.h @@ -326,11 +326,35 @@ typedef struct SValue { struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */ } SValue; +struct Attribute { + unsigned + func_call : 3, /* calling convention (0..5), see below */ + aligned : 5, /* alignement (0..16) */ + packed : 1, + func_export : 1, + func_import : 1, + func_args : 5, + func_proto : 1, + mode : 4, + weak : 1, + fill : 10; // 10 bits left to fit well in union below +}; + +/* GNUC attribute definition */ +typedef struct AttributeDef { + struct Attribute a; + struct Section *section; + int alias_target; /* token */ +} AttributeDef; + /* symbol management */ typedef struct Sym { int v; /* symbol token */ char *asm_label; /* associated asm label */ - long r; /* associated register */ + union { + long r; /* associated register */ + struct Attribute a; + }; union { long c; /* associated number */ int *d; /* define token stream */ @@ -381,34 +405,6 @@ typedef struct DLLReference { char name[1]; } DLLReference; -/* GNUC attribute definition */ -typedef struct AttributeDef { - unsigned - func_call : 3, /* calling convention (0..5), see below */ - aligned : 5, /* alignement (0..16) */ - packed : 1, - func_export : 1, - func_import : 1, - func_args : 5, - func_proto : 1, - mode : 4, - weak : 1, - fill : 10; - struct Section *section; - int alias_target; /* token */ -} AttributeDef; - -/* gr: wrappers for casting sym->r for other purposes */ -#define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call) -#define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export) -#define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import) -#define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args) -#define FUNC_PROTO(r) (((AttributeDef*)&(r))->func_proto) -#define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned) -#define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed) -#define ATTR_MODE(r) (((AttributeDef*)&(r))->mode) -#define INT_ATTR(ad) (*(int*)(ad)) - /* -------------------------------------------------- */ #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ @@ -1142,7 +1138,6 @@ ST_FUNC void expect(const char *msg); /* ------------ tccgen.c ------------ */ ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */ -ST_DATA Section *tdata_section, *tbss_section; /* thread-local storage sections */ ST_DATA Section *cur_text_section; /* current section where function code is generated */ #ifdef CONFIG_TCC_ASM ST_DATA Section *last_text_section; /* to handle .previous asm directive */ @@ -1268,7 +1263,7 @@ ST_FUNC void build_got_entries(TCCState *s1); ST_FUNC void tcc_add_runtime(TCCState *s1); ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err); -#ifdef TCC_IS_NATIVE +#if defined TCC_IS_NATIVE || defined TCC_TARGET_PE ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name); #endif diff --git a/tccgen.c b/tccgen.c index f23cd07..7a675cc 100644 --- a/tccgen.c +++ b/tccgen.c @@ -103,7 +103,8 @@ ST_INLN int is_float(int t) /* XXX: endianness dependent */ ST_FUNC int ieee_finite(double d) { - int *p = (int *)&d; + int p[4]; + memcpy(p, &d, sizeof(double)); return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31; } @@ -2172,7 +2173,7 @@ static int is_compatible_func(CType *type1, CType *type2) if (!is_compatible_types(&s1->type, &s2->type)) return 0; /* check func_call */ - if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r)) + if (s1->a.func_call != s2->a.func_call) return 0; /* XXX: not complete */ if (s1->c == FUNC_OLD || s2->c == FUNC_OLD) @@ -2659,15 +2660,15 @@ static void parse_attribute(AttributeDef *ad) } else { n = MAX_ALIGN; } - ad->aligned = n; + ad->a.aligned = n; break; case TOK_PACKED1: case TOK_PACKED2: - ad->packed = 1; + ad->a.packed = 1; break; case TOK_WEAK1: case TOK_WEAK2: - ad->weak = 1; + ad->a.weak = 1; break; case TOK_UNUSED1: case TOK_UNUSED2: @@ -2682,12 +2683,12 @@ static void parse_attribute(AttributeDef *ad) case TOK_CDECL1: case TOK_CDECL2: case TOK_CDECL3: - ad->func_call = FUNC_CDECL; + ad->a.func_call = FUNC_CDECL; break; case TOK_STDCALL1: case TOK_STDCALL2: case TOK_STDCALL3: - ad->func_call = FUNC_STDCALL; + ad->a.func_call = FUNC_STDCALL; break; #ifdef TCC_TARGET_I386 case TOK_REGPARM1: @@ -2699,26 +2700,26 @@ static void parse_attribute(AttributeDef *ad) else if (n < 0) n = 0; if (n > 0) - ad->func_call = FUNC_FASTCALL1 + n - 1; + ad->a.func_call = FUNC_FASTCALL1 + n - 1; skip(')'); break; case TOK_FASTCALL1: case TOK_FASTCALL2: case TOK_FASTCALL3: - ad->func_call = FUNC_FASTCALLW; + ad->a.func_call = FUNC_FASTCALLW; break; #endif case TOK_MODE: skip('('); switch(tok) { case TOK_MODE_DI: - ad->mode = VT_LLONG + 1; + ad->a.mode = VT_LLONG + 1; break; case TOK_MODE_HI: - ad->mode = VT_SHORT + 1; + ad->a.mode = VT_SHORT + 1; break; case TOK_MODE_SI: - ad->mode = VT_INT + 1; + ad->a.mode = VT_INT + 1; break; default: tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL)); @@ -2728,10 +2729,10 @@ static void parse_attribute(AttributeDef *ad) skip(')'); break; case TOK_DLLEXPORT: - ad->func_export = 1; + ad->a.func_export = 1; break; case TOK_DLLIMPORT: - ad->func_import = 1; + ad->a.func_import = 1; break; default: if (tcc_state->warn_unsupported) @@ -2873,10 +2874,10 @@ static void struct_decl(CType *type, int u, int tdef) get_tok_str(v, NULL)); } size = type_size(&type1, &align); - if (ad.aligned) { - if (align < ad.aligned) - align = ad.aligned; - } else if (ad.packed) { + if (ad.a.aligned) { + if (align < ad.a.aligned) + align = ad.a.aligned; + } else if (ad.a.packed) { align = 1; } else if (*tcc_state->pack_stack_ptr) { if (align > *tcc_state->pack_stack_ptr) @@ -3118,8 +3119,8 @@ static int parse_btype(CType *type, AttributeDef *ad) case TOK_ATTRIBUTE1: case TOK_ATTRIBUTE2: parse_attribute(ad); - if (ad->mode) { - u = ad->mode -1; + if (ad->a.mode) { + u = ad->a.mode -1; t = (t & ~VT_BTYPE) | u; } break; @@ -3143,11 +3144,11 @@ static int parse_btype(CType *type, AttributeDef *ad) type->ref = s->type.ref; if (s->r) { /* get attributes from typedef */ - if (0 == ad->aligned) - ad->aligned = FUNC_ALIGN(s->r); - if (0 == ad->func_call) - ad->func_call = FUNC_CALL(s->r); - ad->packed |= FUNC_PACKED(s->r); + if (0 == ad->a.aligned) + ad->a.aligned = s->a.aligned; + if (0 == ad->a.func_call) + ad->a.func_call = s->a.func_call; + ad->a.packed |= s->a.packed; } next(); typespec_found = 1; @@ -3287,8 +3288,9 @@ static void post_type(CType *type, AttributeDef *ad) type->t |= VT_PTR; } /* we push a anonymous symbol which will contain the function prototype */ - ad->func_args = arg_size; - s = sym_push(SYM_FIELD, type, INT_ATTR(ad), l); + ad->a.func_args = arg_size; + s = sym_push(SYM_FIELD, type, 0, l); + s->a = ad->a; s->next = first; type->t = VT_FUNC; type->ref = s; @@ -5484,10 +5486,10 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, if (flexible_array) size += flexible_array->type.ref->c * pointed_size(&flexible_array->type); /* take into account specified alignment if bigger */ - if (ad->aligned) { - if (ad->aligned > align) - align = ad->aligned; - } else if (ad->packed) { + if (ad->a.aligned) { + if (ad->a.aligned > align) + align = ad->a.aligned; + } else if (ad->a.packed) { align = 1; } if ((r & VT_VALMASK) == VT_LOCAL) { @@ -5869,12 +5871,12 @@ static int decl0(int l, int is_for_loop_init) parse_attribute(&ad); } - if (ad.weak) + if (ad.a.weak) type.t |= VT_WEAK; #ifdef TCC_TARGET_PE - if (ad.func_import) + if (ad.a.func_import) type.t |= VT_IMPORT; - if (ad.func_export) + if (ad.a.func_export) type.t |= VT_EXPORT; #endif if (tok == '{') { @@ -5895,22 +5897,22 @@ static int decl0(int l, int is_for_loop_init) sym = sym_find(v); if (sym) { + Sym *ref; if ((sym->type.t & VT_BTYPE) != VT_FUNC) goto func_error1; - r = sym->type.ref->r; - - if (!FUNC_PROTO(r)) + ref = sym->type.ref; + if (0 == ref->a.func_proto) tcc_error("redefinition of '%s'", get_tok_str(v, NULL)); /* use func_call from prototype if not defined */ - if (FUNC_CALL(r) != FUNC_CDECL - && FUNC_CALL(type.ref->r) == FUNC_CDECL) - FUNC_CALL(type.ref->r) = FUNC_CALL(r); + if (ref->a.func_call != FUNC_CDECL + && type.ref->a.func_call == FUNC_CDECL) + type.ref->a.func_call = ref->a.func_call; /* use export from prototype */ - if (FUNC_EXPORT(r)) - FUNC_EXPORT(type.ref->r) = 1; + if (ref->a.func_export) + type.ref->a.func_export = 1; /* use static from prototype */ if (sym->type.t & VT_STATIC) @@ -5921,7 +5923,7 @@ static int decl0(int l, int is_for_loop_init) tcc_error("incompatible types for redefinition of '%s'", get_tok_str(v, NULL)); } - FUNC_PROTO(type.ref->r) = 0; + type.ref->a.func_proto = 0; /* if symbol is already defined, then put complete type */ sym->type = type; } else { @@ -5980,15 +5982,16 @@ static int decl0(int l, int is_for_loop_init) if (btype.t & VT_TYPEDEF) { /* save typedefed type */ /* XXX: test storage specifiers ? */ - sym = sym_push(v, &type, INT_ATTR(&ad), 0); + sym = sym_push(v, &type, 0, 0); + sym->a = ad.a; sym->type.t |= VT_TYPEDEF; } else { r = 0; if ((type.t & VT_BTYPE) == VT_FUNC) { /* external function definition */ /* specific case for func_call attribute */ - ad.func_proto = 1; - type.ref->r = INT_ATTR(&ad); + ad.a.func_proto = 1; + type.ref->a = ad.a; } else if (!(type.t & VT_ARRAY)) { /* not lvalue if array */ r |= lvalue_type(type.t); @@ -6039,7 +6042,7 @@ static int decl0(int l, int is_for_loop_init) } next(); } - ad.aligned = 0; + ad.a.aligned = 0; } } return 0; diff --git a/x86_64-gen.c b/x86_64-gen.c index 9aee875..9acca3c 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -933,7 +933,8 @@ typedef enum X86_64_Mode { x86_64_mode_x87 } X86_64_Mode; -static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) { +static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) +{ if (a == b) return a; else if (a == x86_64_mode_none) @@ -950,7 +951,8 @@ static X86_64_Mode classify_x86_64_merge(X86_64_Mode a, X86_64_Mode b) { return x86_64_mode_sse; } -static X86_64_Mode classify_x86_64_inner(CType *ty) { +static X86_64_Mode classify_x86_64_inner(CType *ty) +{ X86_64_Mode mode; Sym *f; @@ -988,7 +990,8 @@ static X86_64_Mode classify_x86_64_inner(CType *ty) { assert(0); } -static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count) { +static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *palign, int *reg_count) +{ X86_64_Mode mode; int size, align, ret_t = 0; @@ -1045,7 +1048,8 @@ static X86_64_Mode classify_x86_64_arg(CType *ty, CType *ret, int *psize, int *p return mode; } -ST_FUNC int classify_x86_64_va_arg(CType *ty) { +ST_FUNC int classify_x86_64_va_arg(CType *ty) +{ /* This definition must be synced with stdarg.h */ enum __va_arg_type { __va_gen_reg, __va_float_reg, __va_stack @@ -1061,7 +1065,8 @@ ST_FUNC int classify_x86_64_va_arg(CType *ty) { /* Return the number of registers needed to return the struct, or 0 if returning via struct pointer. */ -int gfunc_sret(CType *vt, CType *ret, int *ret_align) { +ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *ret_align) +{ int size, align, reg_count; *ret_align = 1; // Never have to re-align return values for x86-64 return (classify_x86_64_arg(vt, ret, &size, &align, ®_count) != x86_64_mode_memory); -- cgit v1.3.1 From ea7b17f641cb962d0e9a79137e93c7e1e24b99ce Mon Sep 17 00:00:00 2001 From: Archidemon <_dangerdl@mail.ru> Date: Fri, 10 Jan 2014 09:45:18 +0600 Subject: Fixes for PE x86_64 for fail in code int (*fn1)=0x13fde16b5; and int fn1(int a) {...} struct { int (*fn2)(int a); } b = { fn1 }; --- tccgen.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 7a675cc..99cab53 100644 --- a/tccgen.c +++ b/tccgen.c @@ -446,7 +446,7 @@ ST_FUNC void vpush_global_sym(CType *type, int v) ST_FUNC void vset(CType *type, int r, int v) { - CValue cval; + CValue cval = {0}; cval.i = v; vsetc(type, r, &cval); @@ -1955,7 +1955,9 @@ static void gen_cast(CType *type) s = 24; else if ((dbt & VT_BTYPE) == VT_SHORT) s = 16; - +#ifdef TCC_TARGET_X86_64 + if (!(dbt & (VT_PTR|VT_LLONG|VT_FUNC|VT_STRUCT))) +#endif if(dbt & VT_UNSIGNED) vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s; else @@ -3906,7 +3908,11 @@ ST_FUNC void unary(void) /* if forward reference, we must point to s */ if (vtop->r & VT_SYM) { vtop->sym = s; - vtop->c.ul = 0; +#ifdef TCC_TARGET_X86_64 + s1->vtop->c.ull = 0; +#else + s1->vtop->c.ul = 0; +#endif } break; } @@ -5120,6 +5126,12 @@ static void init_putv(CType *type, Section *sec, unsigned long c, case VT_LLONG: *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos; break; + case VT_PTR: + if (s1->vtop->r & VT_SYM) { + greloc(s1, sec, s1->vtop->sym, c, R_DATA_PTR); + } + *(addr_t *)ptr |= (s1->vtop->c.ull & bit_mask) << bit_pos; + break; default: if (vtop->r & VT_SYM) { greloc(sec, vtop->sym, c, R_DATA_PTR); -- cgit v1.3.1 From fdf9fba5785f5c0d285c8cbf2fa51df32ddd878d Mon Sep 17 00:00:00 2001 From: Archidemon <_dangerdl@mail.ru> Date: Fri, 10 Jan 2014 11:58:16 +0600 Subject: Fixes previous fixes --- tccgen.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 99cab53..c7f0a87 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3909,9 +3909,9 @@ ST_FUNC void unary(void) if (vtop->r & VT_SYM) { vtop->sym = s; #ifdef TCC_TARGET_X86_64 - s1->vtop->c.ull = 0; + vtop->c.ull = 0; #else - s1->vtop->c.ul = 0; + vtop->c.ul = 0; #endif } break; @@ -5127,10 +5127,10 @@ static void init_putv(CType *type, Section *sec, unsigned long c, *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos; break; case VT_PTR: - if (s1->vtop->r & VT_SYM) { - greloc(s1, sec, s1->vtop->sym, c, R_DATA_PTR); + if (vtop->r & VT_SYM) { + greloc(sec, vtop->sym, c, R_DATA_PTR); } - *(addr_t *)ptr |= (s1->vtop->c.ull & bit_mask) << bit_pos; + *(addr_t *)ptr |= (vtop->c.ull & bit_mask) << bit_pos; break; default: if (vtop->r & VT_SYM) { -- cgit v1.3.1 From 9e11476e1534152c7ec994afd4e23553c44722cc Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Sat, 11 Jan 2014 23:42:58 +0100 Subject: Fix Fixes for PE x86_64 for fail in code Applying 64bit relocs assumes that the CVal is initialized to zero for the whole 64bit. Consolidate this a bit, at the same time zeroing the .ull member more consistently when needed. Fixes segfault on x86_64-linux using global vars in tcctest.c. --- tccgen.c | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index c7f0a87..4d7e1fb 100644 --- a/tccgen.c +++ b/tccgen.c @@ -369,6 +369,16 @@ static inline void vpushll(long long v) vpush64(VT_LLONG, v); } +/* push a symbol value of TYPE */ +static inline void vpushsym(CType *type, Sym *sym) +{ + CValue cval; + + cval.ull = 0; + vsetc(type, VT_CONST | VT_SYM, &cval); + vtop->sym = sym; +} + /* Return a static symbol pointing to a section */ ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size) { @@ -386,11 +396,7 @@ ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsign /* 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) { - CValue cval; - - cval.ul = 0; - vsetc(type, VT_CONST | VT_SYM, &cval); - vtop->sym = get_sym_ref(type, sec, offset, size); + vpushsym(type, get_sym_ref(type, sec, offset, size)); } /* define a new external reference to a symbol 'v' of type 'u' */ @@ -435,18 +441,12 @@ static Sym *external_sym(int v, CType *type, int r, char *asm_label) /* push a reference to global symbol v */ ST_FUNC void vpush_global_sym(CType *type, int v) { - Sym *sym; - CValue cval; - - sym = external_global_sym(v, type, 0); - cval.ul = 0; - vsetc(type, VT_CONST | VT_SYM, &cval); - vtop->sym = sym; + vpushsym(type, external_global_sym(v, type, 0)); } ST_FUNC void vset(CType *type, int r, int v) { - CValue cval = {0}; + CValue cval; cval.i = v; vsetc(type, r, &cval); @@ -764,7 +764,7 @@ ST_FUNC int gv(int rc) sym = get_sym_ref(&vtop->type, data_section, offset, size << 2); vtop->r |= VT_LVAL | VT_SYM; vtop->sym = sym; - vtop->c.ul = 0; + vtop->c.ull = 0; } #ifdef CONFIG_TCC_BCHECK if (vtop->r & VT_MUSTBOUND) @@ -1949,15 +1949,16 @@ static void gen_cast(CType *type) vtop->c.ull = vtop->c.ll; else if (dbt == VT_BOOL) vtop->c.i = (vtop->c.ll != 0); +#ifdef TCC_TARGET_X86_64 + else if (dbt == VT_PTR) + ; +#endif else if (dbt != VT_LLONG) { int s = 0; if ((dbt & VT_BTYPE) == VT_BYTE) s = 24; else if ((dbt & VT_BTYPE) == VT_SHORT) s = 16; -#ifdef TCC_TARGET_X86_64 - if (!(dbt & (VT_PTR|VT_LLONG|VT_FUNC|VT_STRUCT))) -#endif if(dbt & VT_UNSIGNED) vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s; else @@ -3908,11 +3909,7 @@ ST_FUNC void unary(void) /* if forward reference, we must point to s */ if (vtop->r & VT_SYM) { vtop->sym = s; -#ifdef TCC_TARGET_X86_64 - vtop->c.ull = 0; -#else - vtop->c.ul = 0; -#endif + vtop->c.ull = 0; } break; } @@ -5617,13 +5614,9 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, esym->st_shndx = SHN_COMMON; } } else { - CValue cval; - /* push global reference */ sym = get_sym_ref(type, sec, addr, size); - cval.ul = 0; - vsetc(type, VT_CONST | VT_SYM, &cval); - vtop->sym = sym; + vpushsym(type, sym); } /* patch symbol weakness */ if (type->t & VT_WEAK) -- cgit v1.3.1 From 05c9b76131e9d0a2e1b011eeb70ad7897efc9084 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Sun, 12 Jan 2014 04:44:27 +0100 Subject: Fix floating point unary minus and plus negate(x) is subtract(-0,x), not subtract(+0,x), which makes a difference with signed zeros. Also +x was expressed as x+0, in order for the integer promotions to happen, but also mangles signed zeros, so just don't do that with floating types. --- tccgen.c | 38 +++++++++++++++++++++----------------- tests/tcctest.c | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 17 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 4d7e1fb..6a5ba03 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3702,12 +3702,16 @@ ST_FUNC void unary(void) break; case '+': next(); - /* in order to force cast, we add zero */ unary(); if ((vtop->type.t & VT_BTYPE) == VT_PTR) tcc_error("pointer not accepted for unary plus"); - vpushi(0); - gen_op('+'); + /* In order to force cast, we add zero, except for floating point + 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('+'); + } break; case TOK_SIZEOF: case TOK_ALIGNOF1: @@ -3822,20 +3826,20 @@ ST_FUNC void unary(void) next(); unary(); t = vtop->type.t & VT_BTYPE; - /* handle (-)0.0 */ - if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST && - is_float(t)) { - if (t == VT_FLOAT) - vtop->c.f = -vtop->c.f; - else if (t == VT_DOUBLE) - vtop->c.d = -vtop->c.d; - else - vtop->c.ld = -vtop->c.ld; - } else { - vpushi(0); - vswap(); - gen_op('-'); - } + if (is_float(t)) { + /* In IEEE negate(x) isn't subtract(0,x), but rather + subtract(-0, x). */ + vpush(&vtop->type); + if (t == VT_FLOAT) + vtop->c.f = -0.0f; + else if (t == VT_DOUBLE) + vtop->c.d = -0.0; + else + vtop->c.ld = -0.0; + } else + vpushi(0); + vswap(); + gen_op('-'); break; case TOK_LAND: if (!gnu_ext) diff --git a/tests/tcctest.c b/tests/tcctest.c index eb284f0..d96c5a1 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -1699,6 +1699,29 @@ void prefix ## call(void)\ printf("strto%s: %f\n", #prefix, (double)strto ## prefix("1.2", NULL));\ }\ \ +void prefix ## signed_zeros(void) \ +{\ + type x = 0.0, y = -0.0, n, p;\ + if (x == y)\ + printf ("Test 1.0 / x != 1.0 / y returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / y);\ + else\ + printf ("x != y; this is wrong!\n");\ +\ + n = -x;\ + if (x == n)\ + printf ("Test 1.0 / x != 1.0 / -x returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / n);\ + else\ + printf ("x != -x; this is wrong!\n");\ +\ + p = +y;\ + if (x == p)\ + printf ("Test 1.0 / x != 1.0 / +y returns %d (should be 1).\n",\ + 1.0 / x != 1.0 / p);\ + else\ + printf ("x != +y; this is wrong!\n");\ +}\ void prefix ## test(void)\ {\ printf("testing '%s'\n", #typename);\ @@ -1708,6 +1731,7 @@ void prefix ## test(void)\ prefix ## fcast(234.6);\ prefix ## fcast(-2334.6);\ prefix ## call();\ + prefix ## signed_zeros();\ } FTEST(f, float, float, "%f") -- cgit v1.3.1 From 4e5f15c6851c69c4cc5da18209218eb918dbee77 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Mon, 3 Feb 2014 22:27:23 +0800 Subject: switch last 2 params of TOK_memset on ARM On ARM, TOK_memset is executed via __aeabi_memset which reverse the order of the last two parameters. --- tccgen.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 6a5ba03..c5e368e 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5157,8 +5157,13 @@ static void init_putz(CType *t, Section *sec, unsigned long c, int size) } 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); } } -- cgit v1.3.1 From 17314a1fb316eb712ae0c539ccea64fe1aeb5c93 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Tue, 4 Feb 2014 20:54:28 +0800 Subject: Fix parameter passing of long long bitfield --- Changelog | 1 + tccgen.c | 3 +++ 2 files changed, 4 insertions(+) (limited to 'tccgen.c') diff --git a/Changelog b/Changelog index 20814c1..966375d 100644 --- a/Changelog +++ b/Changelog @@ -66,6 +66,7 @@ Bug fixes: - use libtcc for static linking with runtime library (Thomas Preud'homme) - fix negation of 0.0 and -0.0 values (Thomas Preud'homme) - fix integer to double conversion on ARM (Thomas Preud'homme) +- fix parameter passing of (unsigned) long long bitfield (Thomas Preud'homme) version 0.9.26: diff --git a/tccgen.c b/tccgen.c index c5e368e..03a446a 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3490,6 +3490,9 @@ static void gfunc_param_typed(Sym *func, Sym *arg) if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) { type.t = VT_DOUBLE; gen_cast(&type); + } else if (vtop->type.t & VT_BITFIELD) { + type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED); + gen_cast(&type); } } else if (arg == NULL) { tcc_error("too many arguments to function"); -- cgit v1.3.1 From b0b5165d1668373c5d7b7933da599426f33e723b Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Thu, 6 Feb 2014 20:51:47 +0800 Subject: Def signedness != signed != unsigned for char When checking for exact compatibility between types (such as in __builtin_types_compatible_p) consider the case of default signedness to be incompatible with both of the explicit signedness for char. That is, char is incompatible with signed char *and* unsigned char, no matter what the default signedness for char is. --- Changelog | 1 + tcc-doc.texi | 2 +- tcc.h | 2 +- tccgen.c | 36 +++++++++++++++++++++++------------- 4 files changed, 26 insertions(+), 15 deletions(-) (limited to 'tccgen.c') diff --git a/Changelog b/Changelog index b7ddd42..af206e3 100644 --- a/Changelog +++ b/Changelog @@ -68,6 +68,7 @@ Bug fixes: - fix integer to double conversion on ARM (Thomas Preud'homme) - fix parameter passing of (unsigned) long long bitfield (Thomas Preud'homme) - fix relocation of Thumb branch to ARM function (Thomas Preud'homme) +- fix char wrong compatibility with [un]signed char (Thomas Preud'homme) version 0.9.26: diff --git a/tcc-doc.texi b/tcc-doc.texi index e8832f6..3a1c7df 100644 --- a/tcc-doc.texi +++ b/tcc-doc.texi @@ -970,7 +970,7 @@ be the best solution. #define VT_BITFIELD 0x0040 /* bitfield modifier */ #define VT_CONSTANT 0x0800 /* const modifier */ #define VT_VOLATILE 0x1000 /* volatile modifier */ -#define VT_SIGNED 0x2000 /* signed type */ +#define VT_DEFSIGN 0x2000 /* signed type */ #define VT_STRUCT_SHIFT 18 /* structure/enum name shift (14 bits left) */ @end example diff --git a/tcc.h b/tcc.h index 73285ae..476cdf7 100644 --- a/tcc.h +++ b/tcc.h @@ -745,7 +745,7 @@ struct TCCState { #define VT_BITFIELD 0x0040 /* bitfield modifier */ #define VT_CONSTANT 0x0800 /* const modifier */ #define VT_VOLATILE 0x1000 /* volatile modifier */ -#define VT_SIGNED 0x2000 /* signed type */ +#define VT_DEFSIGN 0x2000 /* signed type */ #define VT_VLA 0x00020000 /* VLA type (also has VT_PTR and VT_ARRAY) */ /* storage */ diff --git a/tccgen.c b/tccgen.c index 03a446a..2f5b366 100644 --- a/tccgen.c +++ b/tccgen.c @@ -949,7 +949,7 @@ static void lexpand(void) { int u; - u = vtop->type.t & VT_UNSIGNED; + u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED); gv(RC_INT); vdup(); vtop[0].r = vtop[-1].r2; @@ -965,7 +965,7 @@ ST_FUNC void lexpand_nr(void) { int u,v; - u = vtop->type.t & VT_UNSIGNED; + u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED); vdup(); vtop->r2 = VT_CONST; vtop->type.t = VT_INT | u; @@ -1621,8 +1621,8 @@ static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op) return; tmp_type1 = *type1; tmp_type2 = *type2; - tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); - tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); if (!is_compatible_types(&tmp_type1, &tmp_type2)) { /* gcc-like error if '-' is used */ if (op == '-') @@ -2212,6 +2212,11 @@ static int compare_types(CType *type1, CType *type2, int unqualified) t1 &= ~(VT_CONSTANT | VT_VOLATILE); t2 &= ~(VT_CONSTANT | VT_VOLATILE); } + /* Default Vs explicit signedness only matters for char */ + if ((t1 & VT_BTYPE) != VT_BYTE) { + t1 &= ~VT_DEFSIGN; + t2 &= ~VT_DEFSIGN; + } /* XXX: bitfields ? */ if (t1 != t2) return 0; @@ -2264,8 +2269,10 @@ static void type_to_str(char *buf, int buf_size, pstrcat(buf, buf_size, "const "); if (t & VT_VOLATILE) pstrcat(buf, buf_size, "volatile "); - if (t & VT_UNSIGNED) + if (t & (VT_DEFSIGN | VT_UNSIGNED)) pstrcat(buf, buf_size, "unsigned "); + else if (t & VT_DEFSIGN) + pstrcat(buf, buf_size, "signed "); switch(bt) { case VT_VOID: tstr = "void"; @@ -2385,8 +2392,10 @@ static void gen_assign_cast(CType *dt) /* exact type match, except for unsigned */ tmp_type1 = *type1; tmp_type2 = *type2; - tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); - tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE); + tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | + VT_VOLATILE); + tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | + VT_VOLATILE); if (!is_compatible_types(&tmp_type1, &tmp_type2)) tcc_warning("assignment from incompatible pointer type"); } @@ -3081,8 +3090,10 @@ static int parse_btype(CType *type, AttributeDef *ad) case TOK_SIGNED1: case TOK_SIGNED2: case TOK_SIGNED3: + if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED)) + tcc_error("signed and unsigned modifier"); typespec_found = 1; - t |= VT_SIGNED; + t |= VT_DEFSIGN; next(); break; case TOK_REGISTER: @@ -3093,7 +3104,9 @@ static int parse_btype(CType *type, AttributeDef *ad) next(); break; case TOK_UNSIGNED: - t |= VT_UNSIGNED; + if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN) + tcc_error("signed and unsigned modifier"); + t |= VT_DEFSIGN | VT_UNSIGNED; next(); typespec_found = 1; break; @@ -3160,13 +3173,10 @@ static int parse_btype(CType *type, AttributeDef *ad) type_found = 1; } the_end: - if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED)) - tcc_error("signed and unsigned modifier"); if (tcc_state->char_is_unsigned) { - if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE) + if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE) t |= VT_UNSIGNED; } - t &= ~VT_SIGNED; /* long is never used as type */ if ((t & VT_BTYPE) == VT_LONG) -- cgit v1.3.1 From fdb3b10d0693cf33ce5a0acf17f0f323d79ee5f1 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Sat, 8 Mar 2014 18:36:02 +0800 Subject: Fix various errors uncovered by static analysis Reported-by: Carlos Montiers --- c67-gen.c | 2 -- tccgen.c | 1 - tccpp.c | 2 +- x86_64-gen.c | 2 +- 4 files changed, 2 insertions(+), 5 deletions(-) (limited to 'tccgen.c') diff --git a/c67-gen.c b/c67-gen.c index f2baea5..6d9068a 100644 --- a/c67-gen.c +++ b/c67-gen.c @@ -1901,8 +1901,6 @@ void gfunc_call(int nb_args) for (i = 0; i < nb_args; i++) { if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { ALWAYS_ASSERT(FALSE); - } else if ((vtop->type.t & VT_BTYPE) == VT_STRUCT) { - ALWAYS_ASSERT(FALSE); } else { /* simple type (currently always same size) */ /* XXX: implicit cast ? */ diff --git a/tccgen.c b/tccgen.c index 2f5b366..d8e4614 100644 --- a/tccgen.c +++ b/tccgen.c @@ -162,7 +162,6 @@ ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c) tcc_error("incompatible types for redefinition of '%s'", get_tok_str(v, NULL)); } - s = *ps; s = sym_malloc(); s->asm_label = NULL; s->v = v; diff --git a/tccpp.c b/tccpp.c index e1ccded..b12b120 100644 --- a/tccpp.c +++ b/tccpp.c @@ -276,7 +276,7 @@ ST_FUNC char *get_tok_str(int v, CValue *cv) #ifdef _WIN32 sprintf(p, "%u", (unsigned)cv->ull); #else - sprintf(p, "%Lu", cv->ull); + sprintf(p, "%llu", cv->ull); #endif break; case TOK_LCHAR: diff --git a/x86_64-gen.c b/x86_64-gen.c index 407bd96..fc4178e 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -501,7 +501,7 @@ void load(int r, SValue *sv) o(0xc0 + REG_VALUE(v) + REG_VALUE(r)*8); } } else if (r == TREG_ST0) { - assert((v >= TREG_XMM0) || (v <= TREG_XMM7)); + assert((v >= TREG_XMM0) && (v <= TREG_XMM7)); /* gen_cvt_ftof(VT_LDOUBLE); */ /* movsd %xmmN,-0x10(%rsp) */ o(0x110ff2); -- cgit v1.3.1 From 33cea54dc7b18e865c355dc1a7fee3a08a63d587 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Sun, 9 Mar 2014 13:32:24 +0800 Subject: Fix type_to_str test for unsigned int --- tccgen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index d8e4614..e12501d 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2268,7 +2268,7 @@ static void type_to_str(char *buf, int buf_size, pstrcat(buf, buf_size, "const "); if (t & VT_VOLATILE) pstrcat(buf, buf_size, "volatile "); - if (t & (VT_DEFSIGN | VT_UNSIGNED)) + if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED)) pstrcat(buf, buf_size, "unsigned "); else if (t & VT_DEFSIGN) pstrcat(buf, buf_size, "signed "); -- cgit v1.3.1 From b68499e971396b7399bd9679e0154c7557d033de Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Mon, 24 Mar 2014 23:28:56 +0800 Subject: Make parse_btype only accept one basic type This makes int char c; and struct {} int c; generate an error. Thanks Mobi Phil for reporting. --- tccgen.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index e12501d..9c12c92 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2988,19 +2988,25 @@ static void struct_decl(CType *type, int u, int tdef) } } +/* return 1 if basic type is a type size (short, long, long long) */ +int is_btype_size (int bt) +{ + return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG; +} + /* return 0 if no type declaration. otherwise, return the basic type and skip it. */ static int parse_btype(CType *type, AttributeDef *ad) { - int t, u, type_found, typespec_found, typedef_found; + int t, u, bt_size, complete, type_found, typespec_found; Sym *s; CType type1; memset(ad, 0, sizeof(AttributeDef)); + complete = 0; type_found = 0; typespec_found = 0; - typedef_found = 0; t = 0; while(1) { switch(tok) { @@ -3015,9 +3021,12 @@ static int parse_btype(CType *type, AttributeDef *ad) basic_type: next(); basic_type1: - if ((t & VT_BTYPE) != 0) + if (complete) tcc_error("too many basic types"); t |= u; + bt_size = is_btype_size (u & VT_BTYPE); + if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF))) + complete = 1; typespec_found = 1; break; case TOK_VOID: @@ -3027,9 +3036,8 @@ static int parse_btype(CType *type, AttributeDef *ad) u = VT_SHORT; goto basic_type; case TOK_INT: - next(); - typespec_found = 1; - break; + u = VT_INT; + goto basic_type; case TOK_LONG: next(); if ((t & VT_BTYPE) == VT_DOUBLE) { @@ -3149,12 +3157,11 @@ static int parse_btype(CType *type, AttributeDef *ad) type1.t &= ~(VT_STORAGE&~VT_TYPEDEF); goto basic_type2; default: - if (typespec_found || typedef_found) + if (typespec_found) goto the_end; s = sym_find(tok); if (!s || !(s->type.t & VT_TYPEDEF)) goto the_end; - typedef_found = 1; t |= (s->type.t & ~VT_TYPEDEF); type->ref = s->type.ref; if (s->r) { -- cgit v1.3.1 From 4bc83ac3933efa565ae3326b55fcd711b63c073d Mon Sep 17 00:00:00 2001 From: mingodad Date: Wed, 26 Mar 2014 20:14:39 +0000 Subject: 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 --- tccgen.c | 9 ++++++++- tccpp.c | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 9c12c92..b4f97f4 100644 --- a/tccgen.c +++ b/tccgen.c @@ -329,6 +329,7 @@ static void vsetc(CType *type, int r, CValue *vc) void vpush(CType *type) { CValue cval; + memset(&cval, 0, sizeof(CValue)); vsetc(type, VT_CONST, &cval); } @@ -336,6 +337,7 @@ void vpush(CType *type) ST_FUNC void vpushi(int v) { CValue cval; + memset(&cval, 0, sizeof(CValue)); cval.i = v; vsetc(&int_type, VT_CONST, &cval); } @@ -344,6 +346,7 @@ ST_FUNC void vpushi(int v) static void vpushs(long long v) { CValue cval; + memset(&cval, 0, sizeof(CValue)); if (PTR_SIZE == 4) cval.i = (int)v; else @@ -354,8 +357,9 @@ static void vpushs(long long v) /* push arbitrary 64bit constant */ void vpush64(int ty, unsigned long long v) { - CValue cval; CType ctype; + CValue cval; + memset(&cval, 0, sizeof(CValue)); ctype.t = ty; ctype.ref = NULL; cval.ull = v; @@ -372,6 +376,7 @@ static inline void vpushll(long long v) static inline void vpushsym(CType *type, Sym *sym) { CValue cval; + memset(&cval, 0, sizeof(CValue)); cval.ull = 0; vsetc(type, VT_CONST | VT_SYM, &cval); @@ -446,6 +451,7 @@ ST_FUNC void vpush_global_sym(CType *type, int v) ST_FUNC void vset(CType *type, int r, int v) { CValue cval; + memset(&cval, 0, sizeof(CValue)); cval.i = v; vsetc(type, r, &cval); @@ -731,6 +737,7 @@ ST_FUNC int gv(int rc) unsigned long offset; #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP) CValue check; + memset(&check, 0, sizeof(CValue)); #endif /* XXX: unify with initializers handling ? */ diff --git a/tccpp.c b/tccpp.c index cf1fc65..2609ad7 100644 --- a/tccpp.c +++ b/tccpp.c @@ -936,6 +936,7 @@ static void tok_str_add2(TokenString *s, int t, CValue *cv) ST_FUNC void tok_str_add_tok(TokenString *s) { CValue cval; + memset(&cval, 0, sizeof(CValue)); /* save line number info */ if (file->line_num != s->last_line_num) { @@ -999,8 +1000,9 @@ static inline void TOK_GET(int *t, const int **pp, CValue *cv) static int macro_is_equal(const int *a, const int *b) { char buf[STRING_MAX_SIZE + 1]; - CValue cv; int t; + CValue cv; + memset(&cv, 0, sizeof(CValue)); while (*a && *b) { TOK_GET(&t, &a, &cv); pstrcpy(buf, sizeof buf, get_tok_str(t, &cv)); @@ -1159,6 +1161,7 @@ static void tok_print(int *str) { int t; CValue cval; + memset(&cval, 0, sizeof(CValue)); printf("<"); while (1) { @@ -2525,9 +2528,10 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) int last_tok, t, spc; const int *st; Sym *s; - CValue cval; TokenString str; CString cstr; + CValue cval; + memset(&cval, 0, sizeof(CValue)); tok_str_new(&str); last_tok = 0; @@ -2629,9 +2633,10 @@ static int macro_subst_tok(TokenString *tok_str, const int *p; TokenString str; char *cstrval; - CValue cval; CString cstr; char buf[32]; + CValue cval; + memset(&cval, 0, sizeof(CValue)); /* if symbol is a macro, prepare substitution */ /* special macros */ @@ -2806,6 +2811,7 @@ static inline int *macro_twosharps(const int *macro_str) /* we search the first '##' */ for(ptr = macro_str;;) { CValue cval; + memset(&cval, 0, sizeof(CValue)); TOK_GET(&t, &ptr, &cval); if (t == TOK_TWOSHARPS) break; @@ -2836,6 +2842,7 @@ static inline int *macro_twosharps(const int *macro_str) t = *++ptr; if (t && t != TOK_TWOSHARPS) { CValue cval; + memset(&cval, 0, sizeof(CValue)); TOK_GET(&t, &ptr, &cval); /* We concatenate the two tokens */ cstr_new(&cstr); @@ -2877,9 +2884,10 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, int *macro_str1; const int *ptr; int t, ret, spc; - CValue cval; struct macro_level ml; int force_blank; + CValue cval; + memset(&cval, 0, sizeof(CValue)); /* first scan for '##' operator handling */ ptr = macro_str; -- cgit v1.3.1 From b125743323f92b3492634cd875be820c890d5f29 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Sat, 29 Mar 2014 14:28:02 +0800 Subject: Create bcheck region for argv and arge argument For program manipulating argv or arge as pointer with construct such as: (while *argv++) { do_something_with_argv; } it is necessary to have argv and arge inside a region. This patch create regions argv and arge) if main is declared with those parameters. --- lib/bcheck.c | 7 +++++++ tccgen.c | 15 +++++++++++++++ tcctok.h | 1 + 3 files changed, 23 insertions(+) (limited to 'tccgen.c') diff --git a/lib/bcheck.c b/lib/bcheck.c index 064fb5d..968cdf4 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -418,6 +418,13 @@ void __bound_init(void) } } +void __bound_main_arg(void **p) +{ + void *start = p; + while (*p++); + __bound_new_region(start, (void *) p - start); +} + void __bound_exit(void) { restore_malloc_hooks(); diff --git a/tccgen.c b/tccgen.c index b4f97f4..fa03daf 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5770,6 +5770,21 @@ static void gen_function(Sym *sym) /* push a dummy symbol to enable local sym storage */ sym_push2(&local_stack, SYM_FIELD, 0, 0); gfunc_prolog(&sym->type); +#ifdef CONFIG_TCC_BCHECK + if (tcc_state->do_bound_check + && !strcmp(get_tok_str(sym->v, NULL), "main")) { + int i; + + sym = local_stack; + for (i = 0, sym = local_stack; i < 2; i++, sym = sym->prev) { + if (sym->v & SYM_FIELD || sym->prev->v & SYM_FIELD) + break; + vpush_global_sym(&func_old_type, TOK___bound_main_arg); + vset(&sym->type, sym->r, sym->c); + gfunc_call(1); + } + } +#endif rsym = 0; block(NULL, NULL, NULL, NULL, 0, 0); gsym(rsym); diff --git a/tcctok.h b/tcctok.h index 73b0cf9..c17711f 100644 --- a/tcctok.h +++ b/tcctok.h @@ -237,6 +237,7 @@ DEF(TOK___bound_ptr_indir8, "__bound_ptr_indir8") DEF(TOK___bound_ptr_indir12, "__bound_ptr_indir12") DEF(TOK___bound_ptr_indir16, "__bound_ptr_indir16") + DEF(TOK___bound_main_arg, "__bound_main_arg") DEF(TOK___bound_local_new, "__bound_local_new") DEF(TOK___bound_local_delete, "__bound_local_delete") # ifdef TCC_TARGET_PE -- cgit v1.3.1 From f272407353f2abdc09f9f6bcb54bcf4b6521d782 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Sat, 29 Mar 2014 14:57:29 +0800 Subject: Fix typo in code added by b018bac9c8 --- tccgen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index fa03daf..3171720 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5771,7 +5771,7 @@ static void gen_function(Sym *sym) sym_push2(&local_stack, SYM_FIELD, 0, 0); gfunc_prolog(&sym->type); #ifdef CONFIG_TCC_BCHECK - if (tcc_state->do_bound_check + if (tcc_state->do_bounds_check && !strcmp(get_tok_str(sym->v, NULL), "main")) { int i; -- cgit v1.3.1 From 3e56584223a67a6c2f41a43cf38e0960e9992238 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Mon, 31 Mar 2014 22:58:17 +0800 Subject: Allow local redefinition of enumerator --- tccgen.c | 2 +- tests/tests2/63_local_enumerator_redefinition.c | 14 ++++++++++++++ tests/tests2/63_local_enumerator_redefinition.expect | 0 tests/tests2/Makefile | 3 ++- 4 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/tests2/63_local_enumerator_redefinition.c create mode 100644 tests/tests2/63_local_enumerator_redefinition.expect (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 3171720..84188ad 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2827,7 +2827,7 @@ static void struct_decl(CType *type, int u, int tdef) if (v < TOK_UIDENT) expect("identifier"); ss = sym_find(v); - if (ss) + if (ss && !local_stack) tcc_error("redefinition of enumerator '%s'", get_tok_str(v, NULL)); next(); diff --git a/tests/tests2/63_local_enumerator_redefinition.c b/tests/tests2/63_local_enumerator_redefinition.c new file mode 100644 index 0000000..dd4d8e0 --- /dev/null +++ b/tests/tests2/63_local_enumerator_redefinition.c @@ -0,0 +1,14 @@ +enum { + FOO, + BAR +}; + +int main(void) +{ + enum { + FOO = 2, + BAR + }; + + return BAR - FOO; +} diff --git a/tests/tests2/63_local_enumerator_redefinition.expect b/tests/tests2/63_local_enumerator_redefinition.expect new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests2/Makefile b/tests/tests2/Makefile index fa564f9..bd6f2c1 100644 --- a/tests/tests2/Makefile +++ b/tests/tests2/Makefile @@ -74,7 +74,8 @@ TESTS = \ 59_function_array.test \ 60_enum_redefinition.test \ 61_undefined_enum.test \ - 62_enumerator_redefinition.test + 62_enumerator_redefinition.test \ + 63_local_enumerator_redefinition.test # 30_hanoi.test -- seg fault in the code, gcc as well # 34_array_assignment.test -- array assignment is not in C standard -- cgit v1.3.1 From 5879c854fb94f722a7ffdd4e895c9ce418548959 Mon Sep 17 00:00:00 2001 From: grischka Date: Fri, 4 Apr 2014 20:18:39 +0200 Subject: tccgen: x86_64: fix garbage in the SValue upper bits This was going wrong (case TOK_LAND in unary: computed labels) - vset(&s->type, VT_CONST | VT_SYM, 0); - vtop->sym = s; This does the right thing and is shorter: + vpushsym(&s->type, s); Test case was: int main(int argc, char **argv) { int x; static void *label_return = &&lbl_return; printf("label_return = %p\n", label_return); goto *label_return; //<<<<< here segfault on linux X86_64 without the memset on vset printf("unreachable\n"); lbl_return: return 0; } Also:: - Rename "void* CValue.ptr" to more usable "addr_t ptr_offset" and start to use it in obvious cases. - use __attribute__ ((noreturn)) only with gnu compiler - Revert CValue memsets ("After several days searching ...") commit 4bc83ac3933efa565ae3326b55fcd711b63c073d Doesn't mean that the vsetX/vpush thingy isn't brittle and there still might be bugs as to differences in how the CValue union was set and is then interpreted later on. However the big memset hammer was just too slow (-3% overall). --- tcc.h | 76 +++++++++++++++++++++++++++++++++------------------------------- tccgen.c | 30 ++++++++----------------- tccpp.c | 16 ++++---------- 3 files changed, 52 insertions(+), 70 deletions(-) (limited to 'tccgen.c') diff --git a/tcc.h b/tcc.h index f508097..093e758 100644 --- a/tcc.h +++ b/tcc.h @@ -55,18 +55,27 @@ # ifndef CONFIG_TCC_STATIC # include # endif -#else +/* XXX: need to define this to use them in non ISOC99 context */ + extern float strtof (const char *__nptr, char **__endptr); + extern long double strtold (const char *__nptr, char **__endptr); +#else /* on _WIN32: */ # include # include # include /* open, close etc. */ # include /* getcwd */ # ifdef __GNUC__ # include -# else - typedef UINT_PTR uintptr_t; # endif # define inline __inline # define inp next_inp +# define snprintf _snprintf +# define vsnprintf _vsnprintf +# ifndef __GNUC__ +# define strtold (long double)strtod +# define strtof (float)strtod +# define strtoll _strtoi64 +# define strtoull _strtoui64 +# endif # ifdef LIBTCC_AS_DLL # define LIBTCCAPI __declspec(dllexport) # define PUB_FUNC LIBTCCAPI @@ -79,6 +88,30 @@ # define O_BINARY 0 #endif +#ifdef __GNUC__ +# define NORETURN __attribute__ ((noreturn)) +#elif defined _MSC_VER +# define NORETURN __declspec(noreturn) +#else +# define NORETURN +#endif + +#ifdef _WIN32 +# define IS_DIRSEP(c) (c == '/' || c == '\\') +# define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2]))) +# define PATHCMP stricmp +#else +# define IS_DIRSEP(c) (c == '/') +# define IS_ABSPATH(p) IS_DIRSEP(p[0]) +# define PATHCMP strcmp +#endif + +#ifdef TCC_TARGET_PE +#define PATHSEP ';' +#else +#define PATHSEP ':' +#endif + #include "elf.h" #ifdef TCC_TARGET_X86_64 # define ELFCLASSW ELFCLASS64 @@ -315,7 +348,7 @@ typedef union CValue { long long ll; unsigned long long ull; struct CString *cstr; - void *ptr; + addr_t ptr_offset; int tab[LDOUBLE_SIZE/4]; } CValue; @@ -938,37 +971,6 @@ enum tcc_token { #define TOK_UIDENT TOK_DEFINE -#ifdef _WIN32 -#define snprintf _snprintf -#define vsnprintf _vsnprintf -#ifndef __GNUC__ -# define strtold (long double)strtod -# define strtof (float)strtod -# define strtoll _strtoi64 -# define strtoull _strtoui64 -#endif -#else -/* XXX: need to define this to use them in non ISOC99 context */ -extern float strtof (const char *__nptr, char **__endptr); -extern long double strtold (const char *__nptr, char **__endptr); -#endif - -#ifdef _WIN32 -#define IS_DIRSEP(c) (c == '/' || c == '\\') -#define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2]))) -#define PATHCMP stricmp -#else -#define IS_DIRSEP(c) (c == '/') -#define IS_ABSPATH(p) IS_DIRSEP(p[0]) -#define PATHCMP strcmp -#endif - -#ifdef TCC_TARGET_PE -#define PATHSEP ';' -#else -#define PATHSEP ':' -#endif - /* space exlcuding newline */ static inline int is_space(int ch) { @@ -1045,7 +1047,7 @@ PUB_FUNC char *tcc_strdup(const char *str); #define strdup(s) use_tcc_strdup(s) PUB_FUNC void tcc_memstats(void); PUB_FUNC void tcc_error_noabort(const char *fmt, ...); -PUB_FUNC void tcc_error(const char *fmt, ...) __attribute__ ((noreturn)); +PUB_FUNC NORETURN void tcc_error(const char *fmt, ...); PUB_FUNC void tcc_warning(const char *fmt, ...); /* other utilities */ @@ -1143,7 +1145,7 @@ ST_FUNC void preprocess_init(TCCState *s1); ST_FUNC void preprocess_new(void); ST_FUNC int tcc_preprocess(TCCState *s1); ST_FUNC void skip(int c); -ST_FUNC void expect(const char *msg) __attribute__ ((noreturn)); +ST_FUNC NORETURN void expect(const char *msg); /* ------------ tccgen.c ------------ */ diff --git a/tccgen.c b/tccgen.c index 84188ad..b698da9 100644 --- a/tccgen.c +++ b/tccgen.c @@ -329,7 +329,6 @@ static void vsetc(CType *type, int r, CValue *vc) void vpush(CType *type) { CValue cval; - memset(&cval, 0, sizeof(CValue)); vsetc(type, VT_CONST, &cval); } @@ -337,29 +336,23 @@ void vpush(CType *type) ST_FUNC void vpushi(int v) { CValue cval; - memset(&cval, 0, sizeof(CValue)); cval.i = v; vsetc(&int_type, VT_CONST, &cval); } /* push a pointer sized constant */ -static void vpushs(long long v) +static void vpushs(addr_t v) { CValue cval; - memset(&cval, 0, sizeof(CValue)); - if (PTR_SIZE == 4) - cval.i = (int)v; - else - cval.ull = v; + cval.ptr_offset = v; vsetc(&size_type, VT_CONST, &cval); } /* push arbitrary 64bit constant */ void vpush64(int ty, unsigned long long v) { - CType ctype; CValue cval; - memset(&cval, 0, sizeof(CValue)); + CType ctype; ctype.t = ty; ctype.ref = NULL; cval.ull = v; @@ -376,9 +369,7 @@ static inline void vpushll(long long v) static inline void vpushsym(CType *type, Sym *sym) { CValue cval; - memset(&cval, 0, sizeof(CValue)); - - cval.ull = 0; + cval.ptr_offset = 0; vsetc(type, VT_CONST | VT_SYM, &cval); vtop->sym = sym; } @@ -451,7 +442,6 @@ ST_FUNC void vpush_global_sym(CType *type, int v) ST_FUNC void vset(CType *type, int r, int v) { CValue cval; - memset(&cval, 0, sizeof(CValue)); cval.i = v; vsetc(type, r, &cval); @@ -737,7 +727,6 @@ ST_FUNC int gv(int rc) unsigned long offset; #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP) CValue check; - memset(&check, 0, sizeof(CValue)); #endif /* XXX: unify with initializers handling ? */ @@ -770,7 +759,7 @@ ST_FUNC int gv(int rc) sym = get_sym_ref(&vtop->type, data_section, offset, size << 2); vtop->r |= VT_LVAL | VT_SYM; vtop->sym = sym; - vtop->c.ull = 0; + vtop->c.ptr_offset = 0; } #ifdef CONFIG_TCC_BCHECK if (vtop->r & VT_MUSTBOUND) @@ -1581,7 +1570,7 @@ static inline int is_null_pointer(SValue *p) return 0; return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) || ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) || - ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr == 0); + ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr_offset == 0); } static inline int is_integer_btype(int bt) @@ -3886,8 +3875,7 @@ ST_FUNC void unary(void) mk_pointer(&s->type); s->type.t |= VT_STATIC; } - vset(&s->type, VT_CONST | VT_SYM, 0); - vtop->sym = s; + vpushsym(&s->type, s); next(); break; @@ -3939,7 +3927,7 @@ ST_FUNC void unary(void) /* if forward reference, we must point to s */ if (vtop->r & VT_SYM) { vtop->sym = s; - vtop->c.ull = 0; + vtop->c.ptr_offset = 0; } break; } @@ -5157,7 +5145,7 @@ static void init_putv(CType *type, Section *sec, unsigned long c, if (vtop->r & VT_SYM) { greloc(sec, vtop->sym, c, R_DATA_PTR); } - *(addr_t *)ptr |= (vtop->c.ull & bit_mask) << bit_pos; + *(addr_t *)ptr |= (vtop->c.ptr_offset & bit_mask) << bit_pos; break; default: if (vtop->r & VT_SYM) { diff --git a/tccpp.c b/tccpp.c index fbf109b..7144ee4 100644 --- a/tccpp.c +++ b/tccpp.c @@ -974,7 +974,6 @@ static void tok_str_add2(TokenString *s, int t, CValue *cv) ST_FUNC void tok_str_add_tok(TokenString *s) { CValue cval; - memset(&cval, 0, sizeof(CValue)); /* save line number info */ if (file->line_num != s->last_line_num) { @@ -1038,9 +1037,8 @@ static inline void TOK_GET(int *t, const int **pp, CValue *cv) static int macro_is_equal(const int *a, const int *b) { char buf[STRING_MAX_SIZE + 1]; - int t; CValue cv; - memset(&cv, 0, sizeof(CValue)); + int t; while (*a && *b) { TOK_GET(&t, &a, &cv); pstrcpy(buf, sizeof buf, get_tok_str(t, &cv)); @@ -1199,7 +1197,6 @@ static void tok_print(int *str) { int t; CValue cval; - memset(&cval, 0, sizeof(CValue)); printf("<"); while (1) { @@ -2566,10 +2563,9 @@ static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args) int last_tok, t, spc; const int *st; Sym *s; + CValue cval; TokenString str; CString cstr; - CValue cval; - memset(&cval, 0, sizeof(CValue)); tok_str_new(&str); last_tok = 0; @@ -2671,10 +2667,9 @@ static int macro_subst_tok(TokenString *tok_str, const int *p; TokenString str; char *cstrval; + CValue cval; CString cstr; char buf[32]; - CValue cval; - memset(&cval, 0, sizeof(CValue)); /* if symbol is a macro, prepare substitution */ /* special macros */ @@ -2849,7 +2844,6 @@ static inline int *macro_twosharps(const int *macro_str) /* we search the first '##' */ for(ptr = macro_str;;) { CValue cval; - memset(&cval, 0, sizeof(CValue)); TOK_GET(&t, &ptr, &cval); if (t == TOK_TWOSHARPS) break; @@ -2880,7 +2874,6 @@ static inline int *macro_twosharps(const int *macro_str) t = *++ptr; if (t && t != TOK_TWOSHARPS) { CValue cval; - memset(&cval, 0, sizeof(CValue)); TOK_GET(&t, &ptr, &cval); /* We concatenate the two tokens */ cstr_new(&cstr); @@ -2922,10 +2915,9 @@ static void macro_subst(TokenString *tok_str, Sym **nested_list, int *macro_str1; const int *ptr; int t, ret, spc; + CValue cval; struct macro_level ml; int force_blank; - CValue cval; - memset(&cval, 0, sizeof(CValue)); /* first scan for '##' operator handling */ ptr = macro_str; -- cgit v1.3.1 From 0e43f3aef401f87030a540646f061b025a5a130b Mon Sep 17 00:00:00 2001 From: grischka Date: Sun, 6 Apr 2014 10:59:40 +0200 Subject: win32: warn people about using undeclared WINAPI functions *** UNCONDITIONALLY *** Esp. sihce tinycc winapi headers are not as complete as people might expect this can otherwise lead to obscure problems that are difficult to debug. (Originally 'warn_implicit_function_declaration' was set to 1 always for windows but someone must have deleted that line) --- tccgen.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index b698da9..e6e0fe1 100644 --- a/tccgen.c +++ b/tccgen.c @@ -3901,13 +3901,19 @@ ST_FUNC void unary(void) expect("identifier"); s = sym_find(t); if (!s) { + const char *name = get_tok_str(t, NULL); if (tok != '(') - tcc_error("'%s' undeclared", get_tok_str(t, NULL)); + tcc_error("'%s' undeclared", name); /* for simple function calls, we tolerate undeclared external reference to int() function */ - if (tcc_state->warn_implicit_function_declaration) - tcc_warning("implicit declaration of function '%s'", - get_tok_str(t, NULL)); + 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); s = external_global_sym(t, &func_old_type, 0); } if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) == -- cgit v1.3.1 From 3e9a7e9d69e3adb0e9ed65d11caf415e74458fe9 Mon Sep 17 00:00:00 2001 From: Vincent Lefevre Date: Mon, 7 Apr 2014 13:31:00 +0200 Subject: Corrected spelling mistakes in comments and strings --- arm-gen.c | 4 ++-- c67-gen.c | 4 ++-- i386-asm.c | 2 +- i386-tok.h | 2 +- include/stddef.h | 2 +- lib/bcheck.c | 2 +- tccasm.c | 2 +- tccgen.c | 4 ++-- tests/Makefile | 2 +- tests/tcctest.c | 2 +- tests/tests2/46_grep.c | 10 +++++----- 11 files changed, 18 insertions(+), 18 deletions(-) (limited to 'tccgen.c') diff --git a/arm-gen.c b/arm-gen.c index a9c05fe..680a490 100644 --- a/arm-gen.c +++ b/arm-gen.c @@ -943,7 +943,7 @@ struct plan { Returns the amount of stack space needed for parameter passing Note: this function allocated an array in plan->pplans with tcc_malloc. It - is the responsability of the caller to free this array once used (ie not + is the responsibility of the caller to free this array once used (ie not before copy_params). */ static int assign_regs(int nb_args, int float_abi, struct plan *plan, int *todo) { @@ -1860,7 +1860,7 @@ void gen_opf(int op) case TOK_UGE: case TOK_ULE: case TOK_UGT: - tcc_error("unsigned comparision on floats?"); + tcc_error("unsigned comparison on floats?"); break; case TOK_LT: op=TOK_Nset; diff --git a/c67-gen.c b/c67-gen.c index 6d9068a..a26dfaa 100644 --- a/c67-gen.c +++ b/c67-gen.c @@ -245,8 +245,8 @@ void gsym(int t) } // these are regs that tcc doesn't really know about, -// but asign them unique values so the mapping routines -// can distinquish them +// but assign them unique values so the mapping routines +// can distinguish them #define C67_A0 105 #define C67_SP 106 diff --git a/i386-asm.c b/i386-asm.c index 8473d06..a524658 100644 --- a/i386-asm.c +++ b/i386-asm.c @@ -1382,7 +1382,7 @@ ST_FUNC void subst_asm_operand(CString *add_str, } } -/* generate prolog and epilog code for asm statment */ +/* generate prolog and epilog code for asm statement */ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, diff --git a/i386-tok.h b/i386-tok.h index d1e4bf3..6ba865d 100644 --- a/i386-tok.h +++ b/i386-tok.h @@ -191,7 +191,7 @@ DEF_FP(mul) DEF_ASM(fcom) - DEF_ASM(fcom_1) /* non existant op, just to have a regular table */ + DEF_ASM(fcom_1) /* non existent op, just to have a regular table */ DEF_FP1(com) DEF_FP(comp) diff --git a/include/stddef.h b/include/stddef.h index eaf0669..9e43de9 100644 --- a/include/stddef.h +++ b/include/stddef.h @@ -31,7 +31,7 @@ void *alloca(size_t size); by __need_wint_t, as otherwise stddef.h isn't allowed to define this type). Note that this must be outside the normal _STDDEF_H guard, so that it works even when we've included the file - already (without requring wint_t). Some other libs define _WINT_T + already (without requiring wint_t). Some other libs define _WINT_T if they've already provided that type, so we can use that as guard. TCC defines __WINT_TYPE__ for us. */ #if defined (__need_wint_t) diff --git a/lib/bcheck.c b/lib/bcheck.c index 968cdf4..76413ad 100644 --- a/lib/bcheck.c +++ b/lib/bcheck.c @@ -635,7 +635,7 @@ int __bound_delete_region(void *p) } /* return the size of the region starting at p, or EMPTY_SIZE if non - existant region. */ + existent region. */ static unsigned long get_region_size(void *p) { unsigned long addr = (unsigned long)p; diff --git a/tccasm.c b/tccasm.c index 9c77960..1c6a65d 100644 --- a/tccasm.c +++ b/tccasm.c @@ -232,7 +232,7 @@ static inline void asm_expr_sum(TCCState *s1, ExprValue *pe) } else { goto cannot_relocate; } - pe->sym = NULL; /* same symbols can be substracted to NULL */ + pe->sym = NULL; /* same symbols can be subtracted to NULL */ } else { cannot_relocate: tcc_error("invalid operation with label"); diff --git a/tccgen.c b/tccgen.c index e6e0fe1..ec92797 100644 --- a/tccgen.c +++ b/tccgen.c @@ -1579,7 +1579,7 @@ static inline int is_integer_btype(int bt) bt == VT_INT || bt == VT_LLONG); } -/* check types for comparison or substraction of pointers */ +/* check types for comparison or subtraction of pointers */ static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op) { CType *type1, *type2, tmp_type1, tmp_type2; @@ -5574,7 +5574,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, if (sym->type.t & VT_EXTERN) { /* if the variable is extern, it was not allocated */ sym->type.t &= ~VT_EXTERN; - /* set array size if it was ommited in extern + /* set array size if it was omitted in extern declaration */ if ((sym->type.t & VT_ARRAY) && sym->type.ref->c < 0 && diff --git a/tests/Makefile b/tests/Makefile index 55bf29c..ee0eafe 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -158,7 +158,7 @@ btest: boundtest.c @for i in $(BOUNDS_OK); do \ echo ; echo --- boundtest $$i ---; \ if $(TCC) -b -run $< $$i ; then \ - echo succeded as expected; \ + echo succeeded as expected; \ else\ echo Failed positive test $$i ; exit 1 ; \ fi ;\ diff --git a/tests/tcctest.c b/tests/tcctest.c index c48c1bc..cc8ffd8 100644 --- a/tests/tcctest.c +++ b/tests/tcctest.c @@ -2254,7 +2254,7 @@ void c99_vla_test(int size1, int size2) printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED"); tab1_ptr = tab1; tab2_ptr = tab2; - printf("Test C99 VLA 2 (ptrs substract): "); + printf("Test C99 VLA 2 (ptrs subtract): "); printf("%s\n", (tab2 - tab1 == (tab2_ptr - tab1_ptr) / (sizeof(int) * 2)) ? "PASSED" : "FAILED"); printf("Test C99 VLA 3 (ptr add): "); printf("%s\n", &tab1[5][1] == (tab1_ptr + (5 * 2 + 1) * sizeof(int)) ? "PASSED" : "FAILED"); diff --git a/tests/tests2/46_grep.c b/tests/tests2/46_grep.c index 5f52220..27589a4 100644 --- a/tests/tests2/46_grep.c +++ b/tests/tests2/46_grep.c @@ -29,10 +29,10 @@ char *documentation[] = { "grep searches a file for a given pattern. Execute by", " grep [flags] regular_expression file_list\n", - "Flags are single characters preceeded by '-':", + "Flags are single characters preceded by '-':", " -c Only a count of matching lines is printed", " -f Print file name for matching lines switch, see below", - " -n Each line is preceeded by its line number", + " -n Each line is preceded by its line number", " -v Only print non-matching lines\n", "The file_list is a list of files (wildcards are acceptable on RSX modes).", "\nThe file name is normally printed if there is a file given.", @@ -54,10 +54,10 @@ char *patdoc[] = { "':n' \":n\" matches alphanumerics, \": \" matches spaces, tabs, and", "': ' other control characters, such as new-line.", "'*' An expression followed by an asterisk matches zero or more", - " occurrances of that expression: \"fo*\" matches \"f\", \"fo\"", + " occurrences of that expression: \"fo*\" matches \"f\", \"fo\"", " \"foo\", etc.", "'+' An expression followed by a plus sign matches one or more", - " occurrances of that expression: \"fo+\" matches \"fo\", etc.", + " occurrences of that expression: \"fo+\" matches \"fo\", etc.", "'-' An expression followed by a minus sign optionally matches", " the expression.", "'[]' A string enclosed in square brackets matches any character in", @@ -153,7 +153,7 @@ void compile(char *source) o == STAR || o == PLUS || o == MINUS) - badpat("Illegal occurrance op.", source, s); + badpat("Illegal occurrence op.", source, s); store(ENDPAT); store(ENDPAT); spp = pp; /* Save pattern end */ -- cgit v1.3.1 From c2422ba87fb8ad322f0ef6ac47ee9f996a876c06 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Mon, 7 Apr 2014 21:12:08 +0800 Subject: Fix test for macro nesting --- tccgen.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index ec92797..5705db3 100644 --- a/tccgen.c +++ b/tccgen.c @@ -185,6 +185,8 @@ ST_FUNC Sym *sym_find2(Sym *s, int v) while (s) { if (s->v == v) return s; + else if (s->v == -1) + return NULL; s = s->prev; } return NULL; -- cgit v1.3.1 From 822f4630e30bb52729ff6aa3c857c9504675107e Mon Sep 17 00:00:00 2001 From: Urs Janssen Date: Thu, 10 Apr 2014 11:53:54 +0200 Subject: add missing prototypes --- tccgen.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 5705db3..78f24aa 100644 --- a/tccgen.c +++ b/tccgen.c @@ -90,6 +90,10 @@ static void vla_runtime_type_size(CType *type, int *a); static void vla_sp_save(void); static int is_compatible_parameter_types(CType *type1, CType *type2); static void expr_type(CType *type); +ST_FUNC void vpush64(int ty, unsigned long long v); +ST_FUNC void vpush(CType *type); +ST_FUNC int gvtst(int inv, int t); +ST_FUNC int is_btype_size(int bt); ST_INLN int is_float(int t) { @@ -328,7 +332,7 @@ static void vsetc(CType *type, int r, CValue *vc) } /* push constant of type "type" with useless value */ -void vpush(CType *type) +ST_FUNC void vpush(CType *type) { CValue cval; vsetc(type, VT_CONST, &cval); @@ -351,7 +355,7 @@ static void vpushs(addr_t v) } /* push arbitrary 64bit constant */ -void vpush64(int ty, unsigned long long v) +ST_FUNC void vpush64(int ty, unsigned long long v) { CValue cval; CType ctype; @@ -1097,7 +1101,7 @@ static void gv_dup(void) /* Generate value test * * Generate a test for any value (jump, comparison and integers) */ -int gvtst(int inv, int t) +ST_FUNC int gvtst(int inv, int t) { int v = vtop->r & VT_VALMASK; if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) { @@ -2987,7 +2991,7 @@ static void struct_decl(CType *type, int u, int tdef) } /* return 1 if basic type is a type size (short, long, long long) */ -int is_btype_size (int bt) +ST_FUNC int is_btype_size(int bt) { return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG; } -- cgit v1.3.1 From fbda78aefeaaa97182658bb81b5a6f215cc24b17 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 14 Apr 2014 02:53:11 +0200 Subject: Parse and emit hidden visibility This adds parsing of (GCC compatible) visibility attribute in order to mark selected global symbols as hidden. The generated .o files contain hidden symbols already, the TCC linker doesn't yet do the right thing. --- tcc.h | 12 ++++++++++-- tccgen.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- tcctok.h | 2 ++ 3 files changed, 69 insertions(+), 5 deletions(-) (limited to 'tccgen.c') diff --git a/tcc.h b/tcc.h index dda2fc1..ab14b38 100644 --- a/tcc.h +++ b/tcc.h @@ -375,7 +375,8 @@ struct Attribute { func_proto : 1, mode : 4, weak : 1, - fill : 10; // 10 bits left to fit well in union below + visibility : 2, + fill : 8; // 8 bits left to fit well in union below }; /* GNUC attribute definition */ @@ -787,11 +788,18 @@ struct TCCState { #define VT_EXPORT 0x00008000 /* win32: data exported from dll */ #define VT_WEAK 0x00010000 /* weak symbol */ #define VT_TLS 0x00040000 /* thread-local storage */ +#define VT_VIS_SHIFT 19 /* shift for symbol visibility, overlapping + bitfield values, because bitfields never + have linkage and hence never have + visibility. */ +#define VT_VIS_SIZE 2 /* We have four visibilities. */ +#define VT_VIS_MASK (((1 << VT_VIS_SIZE)-1) << VT_VIS_SHIFT) #define VT_STRUCT_SHIFT 19 /* shift for bitfield shift values (max: 32 - 2*6) */ + /* type mask (except storage) */ -#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK) +#define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK | VT_VIS_MASK) #define VT_TYPE (~(VT_STORAGE)) /* token values */ diff --git a/tccgen.c b/tccgen.c index 78f24aa..9b00824 100644 --- a/tccgen.c +++ b/tccgen.c @@ -300,6 +300,29 @@ static void weaken_symbol(Sym *sym) } } +static void apply_visibility(Sym *sym, CType *type) +{ + int vis = sym->type.t & VT_VIS_MASK; + int vis2 = type->t & VT_VIS_MASK; + if (vis == (STV_DEFAULT << VT_VIS_SHIFT)) + vis = vis2; + else if (vis2 == (STV_DEFAULT << VT_VIS_SHIFT)) + ; + else + vis = (vis < vis2) ? vis : vis2; + sym->type.t &= ~VT_VIS_MASK; + sym->type.t |= vis; + + if (sym->c > 0) { + int esym_type; + ElfW(Sym) *esym; + + esym = &((ElfW(Sym) *)symtab_section->data)[sym->c]; + vis >>= VT_VIS_SHIFT; + esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) | vis; + } +} + /* ------------------------------------------------------------------------- */ ST_FUNC void swap(int *p, int *q) @@ -436,6 +459,13 @@ static Sym *external_sym(int v, CType *type, int r, char *asm_label) tcc_error("incompatible types for redefinition of '%s'", get_tok_str(v, NULL)); } + /* Merge some storage attributes. */ + if (type->t & VT_WEAK) + weaken_symbol(s); + + if (type->t & VT_VIS_MASK) + apply_visibility(s, type); + return s; } @@ -2662,6 +2692,24 @@ static void parse_attribute(AttributeDef *ad) next(); skip(')'); break; + case TOK_VISIBILITY1: + case TOK_VISIBILITY2: + skip('('); + if (tok != TOK_STR) + expect("visibility(\"default|hidden|internal|protected\")"); + if (!strcmp (tokc.cstr->data, "default")) + ad->a.visibility = STV_DEFAULT; + else if (!strcmp (tokc.cstr->data, "hidden")) + ad->a.visibility = STV_HIDDEN; + else if (!strcmp (tokc.cstr->data, "internal")) + ad->a.visibility = STV_INTERNAL; + else if (!strcmp (tokc.cstr->data, "protected")) + ad->a.visibility = STV_PROTECTED; + else + expect("visibility(\"default|hidden|internal|protected\")"); + next(); + skip(')'); + break; case TOK_ALIGNED1: case TOK_ALIGNED2: if (tok == '(') { @@ -5656,6 +5704,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, /* patch symbol weakness */ if (type->t & VT_WEAK) weaken_symbol(sym); + apply_visibility(sym, type); #ifdef CONFIG_TCC_BCHECK /* handles bounds now because the symbol must be defined before for the relocation */ @@ -5801,6 +5850,7 @@ static void gen_function(Sym *sym) /* patch symbol weakness (this definition overrules any prototype) */ if (sym->type.t & VT_WEAK) weaken_symbol(sym); + apply_visibility(sym, &sym->type); if (tcc_state->do_debug) { put_stabn(N_FUN, 0, 0, ind - func_ind); } @@ -5934,6 +5984,8 @@ static int decl0(int l, int is_for_loop_init) if (ad.a.func_export) type.t |= VT_EXPORT; #endif + type.t |= ad.a.visibility << VT_VIS_SHIFT; + if (tok == '{') { if (l == VT_LOCAL) tcc_error("cannot use local functions"); @@ -5973,6 +6025,11 @@ static int decl0(int l, int is_for_loop_init) if (sym->type.t & VT_STATIC) type.t = (type.t & ~VT_EXTERN) | VT_STATIC; + /* If the definition has no visibility use the + one from prototype. */ + if (! (type.t & VT_VIS_MASK)) + type.t |= sym->type.t & VT_VIS_MASK; + if (!is_compatible_types(&sym->type, &type)) { func_error1: tcc_error("incompatible types for redefinition of '%s'", @@ -6063,9 +6120,6 @@ static int decl0(int l, int is_for_loop_init) extern */ sym = external_sym(v, &type, r, asm_label); - if (type.t & VT_WEAK) - weaken_symbol(sym); - if (ad.alias_target) { Section tsec; Elf32_Sym *esym; diff --git a/tcctok.h b/tcctok.h index c17711f..d8c0344 100644 --- a/tcctok.h +++ b/tcctok.h @@ -121,6 +121,8 @@ DEF(TOK_DLLIMPORT, "dllimport") DEF(TOK_NORETURN1, "noreturn") DEF(TOK_NORETURN2, "__noreturn__") + DEF(TOK_VISIBILITY1, "visibility") + DEF(TOK_VISIBILITY2, "__visibility__") DEF(TOK_builtin_types_compatible_p, "__builtin_types_compatible_p") DEF(TOK_builtin_constant_p, "__builtin_constant_p") DEF(TOK_builtin_frame_address, "__builtin_frame_address") -- cgit v1.3.1 From 356c6f6293fdd4d1ce232c6abf8741ac5f731c63 Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Mon, 14 Apr 2014 05:41:57 +0200 Subject: Remove unused variable --- tccgen.c | 1 - 1 file changed, 1 deletion(-) (limited to 'tccgen.c') diff --git a/tccgen.c b/tccgen.c index 9b00824..1a89d4a 100644 --- a/tccgen.c +++ b/tccgen.c @@ -314,7 +314,6 @@ static void apply_visibility(Sym *sym, CType *type) sym->type.t |= vis; if (sym->c > 0) { - int esym_type; ElfW(Sym) *esym; esym = &((ElfW(Sym) *)symtab_section->data)[sym->c]; -- cgit v1.3.1