authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-14 17:23:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-14 17:23:24-04:00
logf8f054b354088eb9e76d9207972022bc1d3dfc28
treea45396b18548f911ae88067c98c9c5afd1ad3358
parent42ea2d0d1c3b8cafdfc9a383cbb1bab274eb0140
signature Commit is signed but in an unrecognized format.

fix `@export` for arrays not respecting the symbol name

Previously, the symbol name parameter of `@export` would be ignored for variables, and the variable name would be used for the symbol name. Now it works as expected. See #2679

6 files changed, 61 insertions(+), 71 deletions(-)

doc/langref.html.in+1-1
......@@ -6648,7 +6648,7 @@ test "main" {
66486648 {#header_close#}
66496649
66506650 {#header_open|@export#}
6651 <pre>{#syntax#}@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) []const u8{#endsyntax#}</pre>
6651 <pre>{#syntax#}@export(comptime name: []const u8, target: var, linkage: builtin.GlobalLinkage) void{#endsyntax#}</pre>
66526652 <p>
66536653 Creates a symbol in the output object file.
66546654 </p>
src/all_types.hpp+4-11
......@@ -1334,7 +1334,7 @@ enum FnInline {
13341334 FnInlineNever,
13351335};
13361336
1337struct FnExport {
1337struct GlobalExport {
13381338 Buf name;
13391339 GlobalLinkageId linkage;
13401340};
......@@ -1372,7 +1372,7 @@ struct ZigFn {
13721372
13731373 AstNode *set_cold_node;
13741374
1375 ZigList<FnExport> export_list;
1375 ZigList<GlobalExport> export_list;
13761376
13771377 LLVMValueRef valgrind_client_request_array;
13781378
......@@ -1896,14 +1896,6 @@ struct CodeGen {
18961896 size_t clang_argv_len;
18971897};
18981898
1899enum VarLinkage {
1900 VarLinkageInternal,
1901 VarLinkageExportStrong,
1902 VarLinkageExportWeak,
1903 VarLinkageExportLinkOnce,
1904 VarLinkageExternal,
1905};
1906
19071899struct ZigVar {
19081900 Buf name;
19091901 ConstExprValue *const_value;
......@@ -1926,8 +1918,9 @@ struct ZigVar {
19261918 // this pointer to the redefined variable.
19271919 ZigVar *next_var;
19281920
1921 ZigList<GlobalExport> export_list;
1922
19291923 uint32_t align_bytes;
1930 VarLinkage linkage;
19311924
19321925 bool shadowable;
19331926 bool src_is_const;
src/analyze.cpp+15-14
......@@ -2712,6 +2712,13 @@ ZigType *get_test_fn_type(CodeGen *g) {
27122712 return g->test_fn_type;
27132713}
27142714
2715void add_var_export(CodeGen *g, ZigVar *var, Buf *symbol_name, GlobalLinkageId linkage) {
2716 GlobalExport *global_export = var->export_list.add_one();
2717 memset(global_export, 0, sizeof(GlobalExport));
2718 buf_init_from_buf(&global_export->name, symbol_name);
2719 global_export->linkage = linkage;
2720}
2721
27152722void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLinkageId linkage, bool ccc) {
27162723 if (ccc) {
27172724 if (buf_eql_str(symbol_name, "main") && g->libc_link_lib != nullptr) {
......@@ -2731,8 +2738,8 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLi
27312738 }
27322739 }
27332740
2734 FnExport *fn_export = fn_table_entry->export_list.add_one();
2735 memset(fn_export, 0, sizeof(FnExport));
2741 GlobalExport *fn_export = fn_table_entry->export_list.add_one();
2742 memset(fn_export, 0, sizeof(GlobalExport));
27362743 buf_init_from_buf(&fn_export->name, symbol_name);
27372744 fn_export->linkage = linkage;
27382745}
......@@ -3189,15 +3196,6 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
31893196
31903197 assert(!is_export || !is_extern);
31913198
3192 VarLinkage linkage;
3193 if (is_export) {
3194 linkage = VarLinkageExportStrong;
3195 } else if (is_extern) {
3196 linkage = VarLinkageExternal;
3197 } else {
3198 linkage = VarLinkageInternal;
3199 }
3200
32013199 ConstExprValue *init_value = nullptr;
32023200
32033201 // TODO more validation for types that can't be used for export/extern variables
......@@ -3212,7 +3210,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32123210 if (implicit_type->id == ZigTypeIdUnreachable) {
32133211 add_node_error(g, source_node, buf_sprintf("variable initialization is unreachable"));
32143212 implicit_type = g->builtin_types.entry_invalid;
3215 } else if ((!is_const || linkage == VarLinkageExternal) &&
3213 } else if ((!is_const || is_extern) &&
32163214 (implicit_type->id == ZigTypeIdComptimeFloat ||
32173215 implicit_type->id == ZigTypeIdComptimeInt ||
32183216 implicit_type->id == ZigTypeIdEnumLiteral))
......@@ -3227,7 +3225,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32273225 implicit_type = g->builtin_types.entry_invalid;
32283226 }
32293227 assert(implicit_type->id == ZigTypeIdInvalid || init_value->special != ConstValSpecialRuntime);
3230 } else if (linkage != VarLinkageExternal) {
3228 } else if (!is_extern) {
32313229 add_node_error(g, source_node, buf_sprintf("variables must be initialized"));
32323230 implicit_type = g->builtin_types.entry_invalid;
32333231 }
......@@ -3239,7 +3237,6 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32393237
32403238 tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope, var_decl->symbol,
32413239 is_const, init_val, &tld_var->base, type);
3242 tld_var->var->linkage = linkage;
32433240 tld_var->var->is_thread_local = is_thread_local;
32443241
32453242 if (implicit_type != nullptr && type_is_invalid(implicit_type)) {
......@@ -3262,6 +3259,10 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
32623259 add_node_error(g, source_node, buf_sprintf("threadlocal variable cannot be constant"));
32633260 }
32643261
3262 if (is_export) {
3263 add_var_export(g, tld_var->var, &tld_var->var->name, GlobalLinkageIdStrong);
3264 }
3265
32653266 g->global_vars.append(tld_var);
32663267}
32673268
src/analyze.hpp+1
......@@ -198,6 +198,7 @@ ZigPackage *new_anonymous_package(void);
198198
199199Buf *const_value_to_buffer(ConstExprValue *const_val);
200200void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLinkageId linkage, bool ccc);
201void add_var_export(CodeGen *g, ZigVar *fn_table_entry, Buf *symbol_name, GlobalLinkageId linkage);
201202
202203
203204ConstExprValue *get_builtin_value(CodeGen *codegen, const char *name);
src/codegen.cpp+38-29
......@@ -475,7 +475,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
475475 symbol_name = get_mangled_name(g, unmangled_name, false);
476476 linkage = GlobalLinkageIdInternal;
477477 } else {
478 FnExport *fn_export = &fn_table_entry->export_list.items[0];
478 GlobalExport *fn_export = &fn_table_entry->export_list.items[0];
479479 symbol_name = &fn_export->name;
480480 linkage = fn_export->linkage;
481481 }
......@@ -529,7 +529,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, ZigFn *fn_table_entry) {
529529 }
530530
531531 for (size_t i = 1; i < fn_table_entry->export_list.length; i += 1) {
532 FnExport *fn_export = &fn_table_entry->export_list.items[i];
532 GlobalExport *fn_export = &fn_table_entry->export_list.items[i];
533533 LLVMAddAlias(g->module, LLVMTypeOf(fn_table_entry->llvm_value),
534534 fn_table_entry->llvm_value, buf_ptr(&fn_export->name));
535535 }
......@@ -6691,27 +6691,14 @@ static void validate_inline_fns(CodeGen *g) {
66916691}
66926692
66936693static void set_global_tls(CodeGen *g, ZigVar *var, LLVMValueRef global_value) {
6694 if (var->is_thread_local && (!g->is_single_threaded || var->linkage != VarLinkageInternal)) {
6694 bool is_extern = var->decl_node->data.variable_declaration.is_extern;
6695 bool is_export = var->decl_node->data.variable_declaration.is_export;
6696 bool is_internal_linkage = !is_extern && !is_export;
6697 if (var->is_thread_local && (!g->is_single_threaded || !is_internal_linkage)) {
66956698 LLVMSetThreadLocalMode(global_value, LLVMGeneralDynamicTLSModel);
66966699 }
66976700}
66986701
6699static LLVMLinkage var_linkage_to_llvm(VarLinkage var_linkage) {
6700 switch (var_linkage) {
6701 case VarLinkageInternal:
6702 return LLVMInternalLinkage;
6703 case VarLinkageExportStrong:
6704 return LLVMExternalLinkage;
6705 case VarLinkageExportWeak:
6706 return LLVMWeakODRLinkage;
6707 case VarLinkageExportLinkOnce:
6708 return LLVMLinkOnceODRLinkage;
6709 case VarLinkageExternal:
6710 return LLVMExternalLinkage;
6711 }
6712 zig_unreachable();
6713}
6714
67156702static void do_code_gen(CodeGen *g) {
67166703 assert(!g->errors.length);
67176704
......@@ -6761,31 +6748,48 @@ static void do_code_gen(CodeGen *g) {
67616748
67626749 assert(var->decl_node);
67636750
6751 GlobalLinkageId linkage;
6752 Buf *unmangled_name = &var->name;
6753 Buf *symbol_name;
6754 if (var->export_list.length == 0) {
6755 if (var->decl_node->data.variable_declaration.is_extern) {
6756 symbol_name = unmangled_name;
6757 linkage = GlobalLinkageIdStrong;
6758 } else {
6759 symbol_name = get_mangled_name(g, unmangled_name, false);
6760 linkage = GlobalLinkageIdInternal;
6761 }
6762 } else {
6763 GlobalExport *global_export = &var->export_list.items[0];
6764 symbol_name = &global_export->name;
6765 linkage = global_export->linkage;
6766 }
6767
67646768 LLVMValueRef global_value;
6765 if (var->linkage == VarLinkageExternal) {
6766 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));
6769 bool externally_initialized = var->decl_node->data.variable_declaration.expr == nullptr;
6770 if (externally_initialized) {
6771 LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(symbol_name));
67676772 if (existing_llvm_var) {
67686773 global_value = LLVMConstBitCast(existing_llvm_var,
67696774 LLVMPointerType(get_llvm_type(g, var->var_type), 0));
67706775 } else {
6771 global_value = LLVMAddGlobal(g->module, get_llvm_type(g, var->var_type), buf_ptr(&var->name));
6776 global_value = LLVMAddGlobal(g->module, get_llvm_type(g, var->var_type), buf_ptr(symbol_name));
67726777 // TODO debug info for the extern variable
67736778
6774 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));
6779 LLVMSetLinkage(global_value, to_llvm_linkage(linkage));
67756780 maybe_import_dll(g, global_value, GlobalLinkageIdStrong);
67766781 LLVMSetAlignment(global_value, var->align_bytes);
67776782 LLVMSetGlobalConstant(global_value, var->gen_is_const);
67786783 set_global_tls(g, var, global_value);
67796784 }
67806785 } else {
6781 bool exported = (var->linkage != VarLinkageInternal);
6782 const char *mangled_name = buf_ptr(get_mangled_name(g, &var->name, exported));
6783 render_const_val(g, var->const_value, mangled_name);
6784 render_const_val_global(g, var->const_value, mangled_name);
6786 bool exported = (linkage != GlobalLinkageIdInternal);
6787 render_const_val(g, var->const_value, buf_ptr(symbol_name));
6788 render_const_val_global(g, var->const_value, buf_ptr(symbol_name));
67856789 global_value = var->const_value->global_refs->llvm_global;
67866790
67876791 if (exported) {
6788 LLVMSetLinkage(global_value, var_linkage_to_llvm(var->linkage));
6792 LLVMSetLinkage(global_value, to_llvm_linkage(linkage));
67896793 maybe_export_dll(g, global_value, GlobalLinkageIdStrong);
67906794 }
67916795 if (tld_var->section_name) {
......@@ -6805,6 +6809,11 @@ static void do_code_gen(CodeGen *g) {
68056809 }
68066810
68076811 var->value_ref = global_value;
6812
6813 for (size_t export_i = 1; export_i < var->export_list.length; export_i += 1) {
6814 GlobalExport *global_export = &var->export_list.items[export_i];
6815 LLVMAddAlias(g->module, LLVMTypeOf(var->value_ref), var->value_ref, buf_ptr(&global_export->name));
6816 }
68086817 }
68096818
68106819 // Generate function definitions.
......@@ -9168,7 +9177,7 @@ static void gen_h_file(CodeGen *g) {
91689177 if (fn_table_entry->export_list.length == 0) {
91699178 symbol_name = &fn_table_entry->symbol_name;
91709179 } else {
9171 FnExport *fn_export = &fn_table_entry->export_list.items[0];
9180 GlobalExport *fn_export = &fn_table_entry->export_list.items[0];
91729181 symbol_name = &fn_export->name;
91739182 }
91749183
src/ir.cpp+2-16
......@@ -13791,20 +13791,6 @@ static IrInstruction *ir_analyze_instruction_decl_var(IrAnalyze *ira,
1379113791 return ir_build_var_decl_gen(ira, &decl_var_instruction->base, var, casted_init_value);
1379213792}
1379313793
13794static VarLinkage global_linkage_to_var_linkage(GlobalLinkageId id) {
13795 switch (id) {
13796 case GlobalLinkageIdStrong:
13797 return VarLinkageExportStrong;
13798 case GlobalLinkageIdWeak:
13799 return VarLinkageExportWeak;
13800 case GlobalLinkageIdLinkOnce:
13801 return VarLinkageExportLinkOnce;
13802 case GlobalLinkageIdInternal:
13803 return VarLinkageInternal;
13804 }
13805 zig_unreachable();
13806}
13807
1380813794static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructionExport *instruction) {
1380913795 IrInstruction *name = instruction->name->child;
1381013796 Buf *symbol_name = ir_resolve_str(ira, name);
......@@ -14002,7 +13988,7 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1400213988 if (load_ptr->ptr->id == IrInstructionIdVarPtr) {
1400313989 IrInstructionVarPtr *var_ptr = reinterpret_cast<IrInstructionVarPtr *>(load_ptr->ptr);
1400413990 ZigVar *var = var_ptr->var;
14005 var->linkage = global_linkage_to_var_linkage(global_linkage_id);
13991 add_var_export(ira->codegen, var, symbol_name, global_linkage_id);
1400613992 }
1400713993 }
1400813994
......@@ -14295,7 +14281,7 @@ static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction,
1429514281 ConstExprValue *mem_slot = nullptr;
1429614282
1429714283 bool comptime_var_mem = ir_get_var_is_comptime(var);
14298 bool linkage_makes_it_runtime = var->linkage == VarLinkageExternal;
14284 bool linkage_makes_it_runtime = var->decl_node->data.variable_declaration.is_extern;
1429914285 bool is_const = var->src_is_const;
1430014286 bool is_volatile = false;
1430114287