| @@ -263,6 +263,7 @@ ST_FUNC int tccgen_compile(TCCState *s1) · 262 unmodified lines |
| 263 | 263 | section_sym = 0; |
| 264 | 264 | const_wanted = 0; |
| 265 | 265 | nocode_wanted = 0x80000000; |
| 266 | + local_scope = 0; |
| 266 | 267 | |
| 267 | 268 | /* define some often used types */ |
| 268 | 269 | int_type.t = VT_INT; |
| @@ -325,8 +326,7 @@ ST_FUNC void update_storage(Sym *sym) · 56 unmodified lines |
| 325 | 326 | esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) |
| 326 | 327 | | sym->a.visibility; |
| 327 | 328 | |
| 328 | | − if ((sym->type.t & VT_STATIC) |
| 329 | | − || (sym->type.t & (VT_EXTERN | VT_INLINE)) == VT_INLINE) |
| 329 | + if (sym->type.t & (VT_STATIC | VT_INLINE)) |
| 330 | 330 | sym_bind = STB_LOCAL; |
| 331 | 331 | else if (sym->a.weak) |
| 332 | 332 | sym_bind = STB_WEAK; |
| @@ -408,7 +408,7 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, · 75 unmodified lines |
| 408 | 408 | } else { |
| 409 | 409 | sym_type = STT_OBJECT; |
| 410 | 410 | } |
| 411 | | − if ((t & VT_STATIC) || (t & (VT_EXTERN | VT_INLINE)) == VT_INLINE) |
| 411 | + if (t & (VT_STATIC | VT_INLINE)) |
| 412 | 412 | sym_bind = STB_LOCAL; |
| 413 | 413 | else |
| 414 | 414 | sym_bind = STB_GLOBAL; |
| @@ -927,8 +927,7 @@ static void merge_attr(AttributeDef *ad, AttributeDef *ad1) · 512 unmodified lines |
| 927 | 927 | /* Merge some type attributes. */ |
| 928 | 928 | static void patch_type(Sym *sym, CType *type) |
| 929 | 929 | { |
| 930 | | − if ((!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) |
| 931 | | − && (type->t & VT_BTYPE) != VT_FUNC) { |
| 930 | + if (!(type->t & VT_EXTERN) || IS_ENUM_VAL(sym->type.t)) { |
| 932 | 931 | if (!(sym->type.t & VT_EXTERN)) |
| 933 | 932 | tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL)); |
| 934 | 933 | sym->type.t &= ~VT_EXTERN; |
| @@ -955,29 +954,30 @@ static void patch_type(Sym *sym, CType *type) · 20 unmodified lines |
| 955 | 954 | tcc_warning("static storage ignored for redefinition of '%s'", |
| 956 | 955 | get_tok_str(sym->v, NULL)); |
| 957 | 956 | |
| 958 | | − /* Force external definition if unequal inline specifier |
| 959 | | − or an explicit extern one. */ |
| 960 | | − if ((sym->type.t | type->t) & VT_STATIC) { |
| 961 | | − type->t |= sym->type.t & VT_INLINE; |
| 962 | | − sym->type.t |= type->t & VT_INLINE; |
| 963 | | − } else if (((type->t & VT_INLINE) != (sym->type.t & VT_INLINE) |
| 964 | | − || (type->t | sym->type.t) & VT_EXTERN) |
| 965 | | − && !static_proto) { |
| 966 | | − type->t &= ~VT_INLINE; |
| 967 | | − sym->type.t &= ~VT_INLINE; |
| 957 | + /* set 'inline' if both agree or if one has static */ |
| 958 | + if ((type->t | sym->type.t) & VT_INLINE) { |
| 959 | + if (!((type->t ^ sym->type.t) & VT_INLINE) |
| 960 | + || ((type->t | sym->type.t) & VT_STATIC)) |
| 961 | + static_proto |= VT_INLINE; |
| 968 | 962 | } |
| 963 | + |
| 969 | 964 | if (0 == (type->t & VT_EXTERN)) { |
| 970 | | − /* put complete type, use static from prototype, but don't |
| 971 | | − overwrite type.ref, it might contain parameter names */ |
| 972 | | − sym->type.t = (type->t & ~VT_STATIC) | static_proto; |
| 965 | + /* put complete type, use static from prototype */ |
| 966 | + sym->type.t = (type->t & ~(VT_STATIC|VT_INLINE)) | static_proto; |
| 967 | + sym->type.ref = type->ref; |
| 968 | + } else { |
| 969 | + sym->type.t &= ~VT_INLINE | static_proto; |
| 970 | + } |
| 971 | + |
| 972 | + if (sym->type.ref->f.func_type == FUNC_OLD |
| 973 | + && type->ref->f.func_type != FUNC_OLD) { |
| 974 | + sym->type.ref = type->ref; |
| 973 | 975 | } |
| 976 | + |
| 974 | 977 | } else { |
| 975 | 978 | if ((sym->type.t & VT_ARRAY) && type->ref->c >= 0) { |
| 976 | 979 | /* set array size if it was omitted in extern declaration */ |
| 977 | | − if (sym->type.ref->c < 0) |
| 978 | | − sym->type.ref->c = type->ref->c; |
| 979 | | − else if (sym->type.ref->c != type->ref->c) |
| 980 | | − tcc_error("conflicting type for '%s'", get_tok_str(sym->v, NULL)); |
| 980 | + sym->type.ref->c = type->ref->c; |
| 981 | 981 | } |
| 982 | 982 | if ((type->t ^ sym->type.t) & VT_STATIC) |
| 983 | 983 | tcc_warning("storage mismatch for redefinition of '%s'", |
| @@ -985,7 +985,6 @@ static void patch_type(Sym *sym, CType *type) · 1 unmodified line |
| 985 | 985 | } |
| 986 | 986 | } |
| 987 | 987 | |
| 988 | | − |
| 989 | 988 | /* Merge some storage attributes. */ |
| 990 | 989 | static void patch_storage(Sym *sym, AttributeDef *ad, CType *type) |
| 991 | 990 | { |
| @@ -1003,29 +1002,55 @@ static void patch_storage(Sym *sym, AttributeDef *ad, CType *type) · 11 unmodified lines |
| 1003 | 1002 | update_storage(sym); |
| 1004 | 1003 | } |
| 1005 | 1004 | |
| 1005 | +/* copy sym to other stack */ |
| 1006 | +static Sym *sym_copy(Sym *s0, Sym **ps) |
| 1007 | +{ |
| 1008 | + Sym *s; |
| 1009 | + s = sym_malloc(), *s = *s0; |
| 1010 | + s->prev = *ps, *ps = s; |
| 1011 | + if (s->v < SYM_FIRST_ANOM) { |
| 1012 | + ps = &table_ident[s->v - TOK_IDENT]->sym_identifier; |
| 1013 | + s->prev_tok = *ps, *ps = s; |
| 1014 | + } |
| 1015 | + return s; |
| 1016 | +} |
| 1017 | + |
| 1018 | +/* copy a list of syms */ |
| 1019 | +static void sym_copy_ref(Sym *s0, Sym **ps) |
| 1020 | +{ |
| 1021 | + Sym *s, **sp = &s0->type.ref; |
| 1022 | + for (s = *sp, *sp = NULL; s; s = s->next) |
| 1023 | + sp = &(*sp = sym_copy(s, ps))->next; |
| 1024 | +} |
| 1025 | + |
| 1006 | 1026 | /* define a new external reference to a symbol 'v' */ |
| 1007 | 1027 | static Sym *external_sym(int v, CType *type, int r, AttributeDef *ad) |
| 1008 | 1028 | { |
| 1009 | | − Sym *s; |
| 1029 | + Sym *s; int bt; |
| 1030 | + |
| 1031 | + /* look for global symbol */ |
| 1010 | 1032 | s = sym_find(v); |
| 1011 | | − if (!s || (!IS_ASM_SYM(s) && !(s->type.t & VT_EXTERN) |
| 1012 | | − && (!(type->t & VT_EXTERN) || s->sym_scope) |
| 1013 | | − && (s->type.t & VT_BTYPE) != VT_FUNC)) { |
| 1014 | | − if (s && !is_compatible_types(&s->type, type)) |
| 1015 | | − tcc_error("conflicting types for '%s'", get_tok_str(s->v, NULL)); |
| 1033 | + while (s && s->sym_scope) |
| 1034 | + s = s->prev_tok; |
| 1035 | + |
| 1036 | + if (!s) { |
| 1016 | 1037 | /* push forward reference */ |
| 1017 | | − s = sym_push(v, type, r | VT_CONST | VT_SYM, 0); |
| 1038 | + s = global_identifier_push(v, type->t, 0); |
| 1039 | + s->r |= r; |
| 1018 | 1040 | s->a = ad->a; |
| 1019 | 1041 | s->asm_label = ad->asm_label; |
| 1020 | | − s->sym_scope = 0; |
| 1042 | + s->type.ref = type->ref; |
| 1043 | + bt = s->type.t & (VT_BTYPE|VT_ARRAY); |
| 1044 | + /* copy type to the global stack also */ |
| 1045 | + if (local_scope && (bt == VT_FUNC || (bt & VT_ARRAY))) |
| 1046 | + sym_copy_ref(s, &global_stack); |
| 1021 | 1047 | } else { |
| 1022 | | − if (s->type.ref == func_old_type.ref) { |
| 1023 | | − s->type.ref = type->ref; |
| 1024 | | − s->r = r | VT_CONST | VT_SYM; |
| 1025 | | − s->type.t |= VT_EXTERN; |
| 1026 | | − } |
| 1027 | 1048 | patch_storage(s, ad, type); |
| 1049 | + bt = s->type.t & VT_BTYPE; |
| 1028 | 1050 | } |
| 1051 | + /* push variables to local scope if any */ |
| 1052 | + if (local_stack && bt != VT_FUNC) |
| 1053 | + s = sym_copy(s, &local_stack); |
| 1029 | 1054 | return s; |
| 1030 | 1055 | } |
| 1031 | 1056 | |
| @@ -2880,27 +2905,29 @@ static int is_compatible_func(CType *type1, CType *type2) · 1848 unmodified lines |
| 2880 | 2905 | |
| 2881 | 2906 | s1 = type1->ref; |
| 2882 | 2907 | s2 = type2->ref; |
| 2883 | | − if (!is_compatible_types(&s1->type, &s2->type)) |
| 2884 | | − return 0; |
| 2885 | | − /* check func_call */ |
| 2886 | 2908 | if (s1->f.func_call != s2->f.func_call) |
| 2887 | 2909 | return 0; |
| 2888 | | − /* XXX: not complete */ |
| 2889 | | − if (s1->f.func_type == FUNC_OLD || s2->f.func_type == FUNC_OLD) |
| 2890 | | − return 1; |
| 2891 | | − if (s1->f.func_type != s2->f.func_type) |
| 2910 | + if (s1->f.func_type != s2->f.func_type |
| 2911 | + && s1->f.func_type != FUNC_OLD |
| 2912 | + && s2->f.func_type != FUNC_OLD) |
| 2892 | 2913 | return 0; |
| 2893 | | − while (s1 != NULL) { |
| 2894 | | − if (s2 == NULL) |
| 2895 | | − return 0; |
| 2914 | + /* we should check the function return type for FUNC_OLD too |
| 2915 | + but that causes problems with the internally used support |
| 2916 | + functions such as TOK_memmove */ |
| 2917 | + if (s1->f.func_type == FUNC_OLD && !s1->next) |
| 2918 | + return 1; |
| 2919 | + if (s2->f.func_type == FUNC_OLD && !s2->next) |
| 2920 | + return 1; |
| 2921 | + for (;;) { |
| 2896 | 2922 | if (!is_compatible_unqualified_types(&s1->type, &s2->type)) |
| 2897 | 2923 | return 0; |
| 2898 | 2924 | s1 = s1->next; |
| 2899 | 2925 | s2 = s2->next; |
| 2926 | + if (!s1) |
| 2927 | + return !s2; |
| 2928 | + if (!s2) |
| 2929 | + return 0; |
| 2900 | 2930 | } |
| 2901 | | − if (s2) |
| 2902 | | − return 0; |
| 2903 | | − return 1; |
| 2904 | 2931 | } |
| 2905 | 2932 | |
| 2906 | 2933 | /* return true if type1 and type2 are the same. If unqualified is |
| @@ -2926,15 +2953,19 @@ static int compare_types(CType *type1, CType *type2, int unqualified) · 19 unmodified lines |
| 2926 | 2953 | /* XXX: bitfields ? */ |
| 2927 | 2954 | if (t1 != t2) |
| 2928 | 2955 | return 0; |
| 2956 | + |
| 2957 | + if ((t1 & VT_ARRAY) |
| 2958 | + && !(type1->ref->c < 0 |
| 2959 | + || type2->ref->c < 0 |
| 2960 | + || type1->ref->c == type2->ref->c)) |
| 2961 | + return 0; |
| 2962 | + |
| 2929 | 2963 | /* test more complicated cases */ |
| 2930 | | − bt1 = t1 & (VT_BTYPE | VT_ARRAY); |
| 2964 | + bt1 = t1 & VT_BTYPE; |
| 2931 | 2965 | if (bt1 == VT_PTR) { |
| 2932 | 2966 | type1 = pointed_type(type1); |
| 2933 | 2967 | type2 = pointed_type(type2); |
| 2934 | 2968 | return is_compatible_types(type1, type2); |
| 2935 | | − } else if (bt1 & VT_ARRAY) { |
| 2936 | | − return type1->ref->c < 0 || type2->ref->c < 0 |
| 2937 | | − || type1->ref->c == type2->ref->c; |
| 2938 | 2969 | } else if (bt1 == VT_STRUCT) { |
| 2939 | 2970 | return (type1->ref == type2->ref); |
| 2940 | 2971 | } else if (bt1 == VT_FUNC) { |
| @@ -7285,9 +7316,6 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 4344 unmodified lines |
| 7285 | 7316 | sym = sym_push(v, type, r | VT_SYM, 0); |
| 7286 | 7317 | patch_storage(sym, ad, NULL); |
| 7287 | 7318 | } |
| 7288 | | − /* Local statics have a scope until now (for |
| 7289 | | − warnings), remove it here. */ |
| 7290 | | − sym->sym_scope = 0; |
| 7291 | 7319 | /* update symbol definition */ |
| 7292 | 7320 | put_extern_sym(sym, sec, addr, size); |
| 7293 | 7321 | } else { |
| @@ -7431,7 +7459,7 @@ static void gen_inline_functions(TCCState *s) · 137 unmodified lines |
| 7431 | 7459 | for (i = 0; i < s->nb_inline_fns; ++i) { |
| 7432 | 7460 | fn = s->inline_fns[i]; |
| 7433 | 7461 | sym = fn->sym; |
| 7434 | | − if (sym && (sym->c || !(sym->type.t & (VT_INLINE | VT_STATIC)) )) { |
| 7462 | + if (sym && (sym->c || !(sym->type.t & VT_INLINE))) { |
| 7435 | 7463 | /* the function was used or forced (and then not internal): |
| 7436 | 7464 | generate its code and convert it to a normal function */ |
| 7437 | 7465 | fn->sym = NULL; |
| @@ -7544,22 +7572,26 @@ static int decl0(int l, int is_for_loop_init, Sym *func_sym) · 106 unmodified lines |
| 7544 | 7572 | } |
| 7545 | 7573 | #endif |
| 7546 | 7574 | if ((type.t & VT_BTYPE) == VT_FUNC) { |
| 7547 | | − if ((type.t & VT_STATIC) && (l == VT_LOCAL)) { |
| 7548 | | − tcc_error("function without file scope cannot be static"); |
| 7549 | | − } |
| 7550 | 7575 | /* if old style function prototype, we accept a |
| 7551 | 7576 | declaration list */ |
| 7552 | 7577 | sym = type.ref; |
| 7553 | 7578 | if (sym->f.func_type == FUNC_OLD && l == VT_CONST) |
| 7554 | 7579 | decl0(VT_CMP, 0, sym); |
| 7580 | + /* always compile 'extern inline' */ |
| 7581 | + if (type.t & VT_EXTERN) |
| 7582 | + type.t &= ~VT_INLINE; |
| 7555 | 7583 | } |
| 7556 | 7584 | |
| 7557 | 7585 | if (gnu_ext && (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) { |
| 7558 | 7586 | ad.asm_label = asm_label_instr(); |
| 7559 | 7587 | /* parse one last attribute list, after asm label */ |
| 7560 | 7588 | parse_attribute(&ad); |
| 7589 | + #if 0 |
| 7590 | + /* gcc does not allow __asm__("label") with function definition, |
| 7591 | + but why not ... */ |
| 7561 | 7592 | if (tok == '{') |
| 7562 | 7593 | expect(";"); |
| 7594 | + #endif |
| 7563 | 7595 | } |
| 7564 | 7596 | |
| 7565 | 7597 | #ifdef TCC_TARGET_PE |
| @@ -7591,18 +7623,12 @@ static int decl0(int l, int is_for_loop_init, Sym *func_sym) · 25 unmodified lines |
| 7591 | 7623 | } |
| 7592 | 7624 | |
| 7593 | 7625 | /* put function symbol */ |
| 7626 | + type.t &= ~VT_EXTERN; |
| 7594 | 7627 | sym = external_sym(v, &type, 0, &ad); |
| 7595 | | − /* This is the def, so overwrite any other parameter names |
| 7596 | | − we got from prototypes. */ |
| 7597 | | − sym->type.ref = type.ref; |
| 7598 | | − if (sym->c && elfsym(sym)->st_shndx != SHN_UNDEF |
| 7599 | | − && !(elfsym(sym)->st_other & ST_ASM_SET)) |
| 7600 | | − tcc_error("redefinition of '%s'", get_tok_str(sym->v, NULL)); |
| 7601 | | − |
| 7602 | 7628 | /* static inline functions are just recorded as a kind |
| 7603 | 7629 | of macro. Their code will be emitted at the end of |
| 7604 | 7630 | the compilation unit only if they are used */ |
| 7605 | | − if ((sym->type.t & (VT_INLINE | VT_EXTERN)) == VT_INLINE) { |
| 7631 | + if (sym->type.t & VT_INLINE) { |
| 7606 | 7632 | struct InlineFunc *fn; |
| 7607 | 7633 | const char *filename; |
| 7608 | 7634 | |
| @@ -7669,16 +7695,15 @@ found: · 60 unmodified lines |
| 7669 | 7695 | has_init = (tok == '='); |
| 7670 | 7696 | if (has_init && (type.t & VT_VLA)) |
| 7671 | 7697 | tcc_error("variable length array cannot be initialized"); |
| 7672 | | − if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) || |
| 7673 | | − ((type.t & VT_BTYPE) == VT_FUNC) || |
| 7674 | | − ((type.t & VT_ARRAY) && (type.t & VT_STATIC) && |
| 7675 | | − !has_init && l == VT_CONST && type.ref->c < 0)) { |
| 7698 | + if (((type.t & VT_EXTERN) && (!has_init || l != VT_CONST)) |
| 7699 | + || (type.t & VT_BTYPE) == VT_FUNC |
| 7700 | + /* as with GCC, uninitialized global arrays with no size |
| 7701 | + are considered extern: */ |
| 7702 | + || ((type.t & VT_ARRAY) && !has_init |
| 7703 | + && l == VT_CONST && type.ref->c < 0) |
| 7704 | + ) { |
| 7676 | 7705 | /* external variable or function */ |
| 7677 | | − /* NOTE: as GCC, uninitialized global static |
| 7678 | | − arrays of null size are considered as |
| 7679 | | − extern */ |
| 7680 | | − if ((type.t & VT_BTYPE) != VT_FUNC) |
| 7681 | | − type.t |= VT_EXTERN; |
| 7706 | + type.t |= VT_EXTERN; |
| 7682 | 7707 | sym = external_sym(v, &type, r, &ad); |
| 7683 | 7708 | if (ad.alias_target) { |
| 7684 | 7709 | ElfSym *esym; |
| @@ -7687,9 +7712,6 @@ found: · 2 unmodified lines |
| 7687 | 7712 | esym = elfsym(alias_target); |
| 7688 | 7713 | if (!esym) |
| 7689 | 7714 | tcc_error("unsupported forward __alias__ attribute"); |
| 7690 | | − /* Local statics have a scope until now (for |
| 7691 | | − warnings), remove it here. */ |
| 7692 | | − sym->sym_scope = 0; |
| 7693 | 7715 | put_extern_sym2(sym, esym->st_shndx, esym->st_value, esym->st_size, 0); |
| 7694 | 7716 | } |
| 7695 | 7717 | } else { |
| @@ -7699,8 +7721,7 @@ found: · 3 unmodified lines |
| 7699 | 7721 | r |= l; |
| 7700 | 7722 | if (has_init) |
| 7701 | 7723 | next(); |
| 7702 | | − else if (l == VT_CONST |
| 7703 | | − && (type.t & VT_BTYPE) != VT_FUNC) |
| 7724 | + else if (l == VT_CONST) |
| 7704 | 7725 | /* uninitialized global variables may be overridden */ |
| 7705 | 7726 | type.t |= VT_EXTERN; |
| 7706 | 7727 | decl_initializer_alloc(&type, &ad, r, has_init, v, l); |