authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-11 16:45:33-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-11 16:46:02-05:00
logf2d601661d286b135293373a83ce1a8628272379
treebfae8484bb359695bfa1c70c28fed21a5fc231cd
parente743b30bbfe09541f306f09c1fdadb122736110c

fix exported variable not named in the object file

closes #771

3 files changed, 33 insertions(+), 25 deletions(-)

example/mix_o_files/base64.zig+3
......@@ -8,3 +8,6 @@ export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8,
88 base64_decoder.decode(dest[0..decoded_size], src);
99 return decoded_size;
1010}
11
12var x: c_int = 1234;
13export var x_ptr = &x;
example/mix_o_files/test.c+4
......@@ -4,6 +4,8 @@
44#include <assert.h>
55#include <string.h>
66
7extern int *x_ptr;
8
79int main(int argc, char **argv) {
810 const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz";
911 char buf[200];
......@@ -12,5 +14,7 @@ int main(int argc, char **argv) {
1214 buf[len] = 0;
1315 assert(strcmp(buf, "all your base are belong to us") == 0);
1416
17 assert(*x_ptr == 1234);
18
1519 return 0;
1620}
src/codegen.cpp+26-25
......@@ -283,9 +283,9 @@ void codegen_set_linker_script(CodeGen *g, const char *linker_script) {
283283}
284284
285285
286static void render_const_val(CodeGen *g, ConstExprValue *const_val);
286static void render_const_val(CodeGen *g, ConstExprValue *const_val, const char *name);
287287static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const char *name);
288static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val);
288static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name);
289289static void generate_error_name_table(CodeGen *g);
290290
291291static void addLLVMAttr(LLVMValueRef val, LLVMAttributeIndex attr_index, const char *attr_name) {
......@@ -874,7 +874,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
874874 ConstExprValue *array_val = create_const_str_lit(g, buf_msg);
875875 init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true);
876876
877 render_const_val(g, val);
877 render_const_val(g, val, "");
878878 render_const_val_global(g, val, "");
879879
880880 assert(val->global_refs->llvm_global);
......@@ -1413,7 +1413,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
14131413 if (!instruction->llvm_value) {
14141414 assert(instruction->value.special != ConstValSpecialRuntime);
14151415 assert(instruction->value.type);
1416 render_const_val(g, &instruction->value);
1416 render_const_val(g, &instruction->value, "");
14171417 // we might have to do some pointer casting here due to the way union
14181418 // values are rendered with a type other than the one we expect
14191419 if (handle_is_ptr(instruction->value.type)) {
......@@ -3892,7 +3892,7 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *ar
38923892static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent *parent) {
38933893 switch (parent->id) {
38943894 case ConstParentIdNone:
3895 render_const_val(g, val);
3895 render_const_val(g, val, "");
38963896 render_const_val_global(g, val, "");
38973897 return val->global_refs->llvm_global;
38983898 case ConstParentIdStruct:
......@@ -3991,17 +3991,17 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
39913991 case TypeTableEntryIdEnum:
39923992 {
39933993 assert(type_entry->data.enumeration.decl_node->data.container_decl.init_arg_expr != nullptr);
3994 LLVMValueRef int_val = gen_const_val(g, const_val);
3994 LLVMValueRef int_val = gen_const_val(g, const_val, "");
39953995 return LLVMConstZExt(int_val, big_int_type_ref);
39963996 }
39973997 case TypeTableEntryIdInt:
39983998 {
3999 LLVMValueRef int_val = gen_const_val(g, const_val);
3999 LLVMValueRef int_val = gen_const_val(g, const_val, "");
40004000 return LLVMConstZExt(int_val, big_int_type_ref);
40014001 }
40024002 case TypeTableEntryIdFloat:
40034003 {
4004 LLVMValueRef float_val = gen_const_val(g, const_val);
4004 LLVMValueRef float_val = gen_const_val(g, const_val, "");
40054005 LLVMValueRef int_val = LLVMConstFPToUI(float_val,
40064006 LLVMIntType((unsigned)type_entry->data.floating.bit_count));
40074007 return LLVMConstZExt(int_val, big_int_type_ref);
......@@ -4010,7 +4010,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
40104010 case TypeTableEntryIdFn:
40114011 case TypeTableEntryIdMaybe:
40124012 {
4013 LLVMValueRef ptr_val = gen_const_val(g, const_val);
4013 LLVMValueRef ptr_val = gen_const_val(g, const_val, "");
40144014 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->type_ref);
40154015 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);
40164016 }
......@@ -4055,7 +4055,7 @@ static bool is_llvm_value_unnamed_type(TypeTableEntry *type_entry, LLVMValueRef
40554055 return LLVMTypeOf(val) != type_entry->type_ref;
40564056}
40574057
4058static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4058static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
40594059 TypeTableEntry *type_entry = const_val->type;
40604060 assert(!type_entry->zero_bits);
40614061
......@@ -4108,7 +4108,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
41084108 child_type->id == TypeTableEntryIdFn)
41094109 {
41104110 if (const_val->data.x_maybe) {
4111 return gen_const_val(g, const_val->data.x_maybe);
4111 return gen_const_val(g, const_val->data.x_maybe, "");
41124112 } else {
41134113 return LLVMConstNull(child_type->type_ref);
41144114 }
......@@ -4117,7 +4117,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
41174117 LLVMValueRef maybe_val;
41184118 bool make_unnamed_struct;
41194119 if (const_val->data.x_maybe) {
4120 child_val = gen_const_val(g, const_val->data.x_maybe);
4120 child_val = gen_const_val(g, const_val->data.x_maybe, "");
41214121 maybe_val = LLVMConstAllOnes(LLVMInt1Type());
41224122
41234123 make_unnamed_struct = is_llvm_value_unnamed_type(const_val->type, child_val);
......@@ -4161,7 +4161,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
41614161
41624162 if (src_field_index + 1 == src_field_index_end) {
41634163 ConstExprValue *field_val = &const_val->data.x_struct.fields[src_field_index];
4164 LLVMValueRef val = gen_const_val(g, field_val);
4164 LLVMValueRef val = gen_const_val(g, field_val, "");
41654165 fields[type_struct_field->gen_index] = val;
41664166 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
41674167 } else {
......@@ -4201,7 +4201,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
42014201 continue;
42024202 }
42034203 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
4204 LLVMValueRef val = gen_const_val(g, field_val);
4204 LLVMValueRef val = gen_const_val(g, field_val, "");
42054205 fields[type_struct_field->gen_index] = val;
42064206 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
42074207 }
......@@ -4225,7 +4225,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
42254225 bool make_unnamed_struct = false;
42264226 for (uint64_t i = 0; i < len; i += 1) {
42274227 ConstExprValue *elem_value = &const_val->data.x_array.s_none.elements[i];
4228 LLVMValueRef val = gen_const_val(g, elem_value);
4228 LLVMValueRef val = gen_const_val(g, elem_value, "");
42294229 values[i] = val;
42304230 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);
42314231 }
......@@ -4260,7 +4260,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
42604260 } else {
42614261 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, payload_value->type->type_ref);
42624262 uint64_t pad_bytes = type_entry->data.unionation.union_size_bytes - field_type_bytes;
4263 LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value);
4263 LLVMValueRef correctly_typed_value = gen_const_val(g, payload_value, "");
42644264 make_unnamed_struct = is_llvm_value_unnamed_type(payload_value->type, correctly_typed_value) ||
42654265 payload_value->type != type_entry->data.unionation.most_aligned_union_member;
42664266
......@@ -4305,7 +4305,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
43054305 return fn_llvm_value(g, const_val->data.x_fn.fn_entry);
43064306 case TypeTableEntryIdPointer:
43074307 {
4308 render_const_val_global(g, const_val, "");
4308 render_const_val_global(g, const_val, name);
43094309 switch (const_val->data.x_ptr.special) {
43104310 case ConstPtrSpecialInvalid:
43114311 case ConstPtrSpecialDiscard:
......@@ -4313,7 +4313,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
43134313 case ConstPtrSpecialRef:
43144314 {
43154315 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
4316 render_const_val(g, pointee);
4316 render_const_val(g, pointee, "");
43174317 render_const_val_global(g, pointee, "");
43184318 ConstExprValue *other_val = pointee;
43194319 const_val->global_refs->llvm_value = LLVMConstBitCast(other_val->global_refs->llvm_global, const_val->type->type_ref);
......@@ -4383,7 +4383,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
43834383 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
43844384 } else if (!type_has_bits(err_set_type)) {
43854385 assert(type_has_bits(payload_type));
4386 return gen_const_val(g, const_val->data.x_err_union.payload);
4386 return gen_const_val(g, const_val->data.x_err_union.payload, "");
43874387 } else {
43884388 LLVMValueRef err_tag_value;
43894389 LLVMValueRef err_payload_value;
......@@ -4395,7 +4395,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
43954395 } else {
43964396 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);
43974397 ConstExprValue *payload_val = const_val->data.x_err_union.payload;
4398 err_payload_value = gen_const_val(g, payload_val);
4398 err_payload_value = gen_const_val(g, payload_val, "");
43994399 make_unnamed_struct = is_llvm_value_unnamed_type(payload_val->type, err_payload_value);
44004400 }
44014401 LLVMValueRef fields[] = {
......@@ -4430,11 +4430,11 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
44304430 zig_unreachable();
44314431}
44324432
4433static void render_const_val(CodeGen *g, ConstExprValue *const_val) {
4433static void render_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
44344434 if (!const_val->global_refs)
44354435 const_val->global_refs = allocate<ConstGlobalRefs>(1);
44364436 if (!const_val->global_refs->llvm_value)
4437 const_val->global_refs->llvm_value = gen_const_val(g, const_val);
4437 const_val->global_refs->llvm_value = gen_const_val(g, const_val, name);
44384438
44394439 if (const_val->global_refs->llvm_global)
44404440 LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value);
......@@ -4663,7 +4663,7 @@ static void do_code_gen(CodeGen *g) {
46634663 coerced_value.special = ConstValSpecialStatic;
46644664 coerced_value.type = var_type;
46654665 coerced_value.data.x_f128 = bigfloat_to_f128(&const_val->data.x_bigfloat);
4666 LLVMValueRef init_val = gen_const_val(g, &coerced_value);
4666 LLVMValueRef init_val = gen_const_val(g, &coerced_value, "");
46674667 gen_global_var(g, var, init_val, var_type);
46684668 continue;
46694669 }
......@@ -4697,8 +4697,9 @@ static void do_code_gen(CodeGen *g) {
46974697 LLVMSetAlignment(global_value, var->align_bytes);
46984698 } else {
46994699 bool exported = (var->linkage == VarLinkageExport);
4700 render_const_val(g, var->value);
4701 render_const_val_global(g, var->value, buf_ptr(get_mangled_name(g, &var->name, exported)));
4700 const char *mangled_name = buf_ptr(get_mangled_name(g, &var->name, exported));
4701 render_const_val(g, var->value, mangled_name);
4702 render_const_val_global(g, var->value, mangled_name);
47024703 global_value = var->value->global_refs->llvm_global;
47034704
47044705 if (exported) {