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,...@@ -8,3 +8,6 @@ export fn decode_base_64(dest_ptr: &u8, dest_len: usize, source_ptr: &const u8,
8 base64_decoder.decode(dest[0..decoded_size], src);8 base64_decoder.decode(dest[0..decoded_size], src);
9 return decoded_size;9 return decoded_size;
10}10}
11
12var x: c_int = 1234;
13export var x_ptr = &x;
example/mix_o_files/test.c+4
...@@ -4,6 +4,8 @@...@@ -4,6 +4,8 @@
4#include <assert.h>4#include <assert.h>
5#include <string.h>5#include <string.h>
66
7extern int *x_ptr;
8
7int main(int argc, char **argv) {9int main(int argc, char **argv) {
8 const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz";10 const char *encoded = "YWxsIHlvdXIgYmFzZSBhcmUgYmVsb25nIHRvIHVz";
9 char buf[200];11 char buf[200];
...@@ -12,5 +14,7 @@ int main(int argc, char **argv) {...@@ -12,5 +14,7 @@ int main(int argc, char **argv) {
12 buf[len] = 0;14 buf[len] = 0;
13 assert(strcmp(buf, "all your base are belong to us") == 0);15 assert(strcmp(buf, "all your base are belong to us") == 0);
1416
17 assert(*x_ptr == 1234);
18
15 return 0;19 return 0;
16}20}
src/codegen.cpp+26-25
...@@ -283,9 +283,9 @@ void codegen_set_linker_script(CodeGen *g, const char *linker_script) {...@@ -283,9 +283,9 @@ void codegen_set_linker_script(CodeGen *g, const char *linker_script) {
283}283}
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);
287static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, const char *name);287static 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);
289static void generate_error_name_table(CodeGen *g);289static void generate_error_name_table(CodeGen *g);
290290
291static void addLLVMAttr(LLVMValueRef val, LLVMAttributeIndex attr_index, const char *attr_name) {291static 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) {...@@ -874,7 +874,7 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
874 ConstExprValue *array_val = create_const_str_lit(g, buf_msg);874 ConstExprValue *array_val = create_const_str_lit(g, buf_msg);
875 init_const_slice(g, val, array_val, 0, buf_len(buf_msg), true);875 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, "");
878 render_const_val_global(g, val, "");878 render_const_val_global(g, val, "");
879879
880 assert(val->global_refs->llvm_global);880 assert(val->global_refs->llvm_global);
...@@ -1413,7 +1413,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {...@@ -1413,7 +1413,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
1413 if (!instruction->llvm_value) {1413 if (!instruction->llvm_value) {
1414 assert(instruction->value.special != ConstValSpecialRuntime);1414 assert(instruction->value.special != ConstValSpecialRuntime);
1415 assert(instruction->value.type);1415 assert(instruction->value.type);
1416 render_const_val(g, &instruction->value);1416 render_const_val(g, &instruction->value, "");
1417 // we might have to do some pointer casting here due to the way union1417 // we might have to do some pointer casting here due to the way union
1418 // values are rendered with a type other than the one we expect1418 // values are rendered with a type other than the one we expect
1419 if (handle_is_ptr(instruction->value.type)) {1419 if (handle_is_ptr(instruction->value.type)) {
...@@ -3892,7 +3892,7 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *ar...@@ -3892,7 +3892,7 @@ static LLVMValueRef gen_const_ptr_union_recursive(CodeGen *g, ConstExprValue *ar
3892static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent *parent) {3892static LLVMValueRef gen_parent_ptr(CodeGen *g, ConstExprValue *val, ConstParent *parent) {
3893 switch (parent->id) {3893 switch (parent->id) {
3894 case ConstParentIdNone:3894 case ConstParentIdNone:
3895 render_const_val(g, val);3895 render_const_val(g, val, "");
3896 render_const_val_global(g, val, "");3896 render_const_val_global(g, val, "");
3897 return val->global_refs->llvm_global;3897 return val->global_refs->llvm_global;
3898 case ConstParentIdStruct:3898 case ConstParentIdStruct:
...@@ -3991,17 +3991,17 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -3991,17 +3991,17 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
3991 case TypeTableEntryIdEnum:3991 case TypeTableEntryIdEnum:
3992 {3992 {
3993 assert(type_entry->data.enumeration.decl_node->data.container_decl.init_arg_expr != nullptr);3993 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, "");
3995 return LLVMConstZExt(int_val, big_int_type_ref);3995 return LLVMConstZExt(int_val, big_int_type_ref);
3996 }3996 }
3997 case TypeTableEntryIdInt:3997 case TypeTableEntryIdInt:
3998 {3998 {
3999 LLVMValueRef int_val = gen_const_val(g, const_val);3999 LLVMValueRef int_val = gen_const_val(g, const_val, "");
4000 return LLVMConstZExt(int_val, big_int_type_ref);4000 return LLVMConstZExt(int_val, big_int_type_ref);
4001 }4001 }
4002 case TypeTableEntryIdFloat:4002 case TypeTableEntryIdFloat:
4003 {4003 {
4004 LLVMValueRef float_val = gen_const_val(g, const_val);4004 LLVMValueRef float_val = gen_const_val(g, const_val, "");
4005 LLVMValueRef int_val = LLVMConstFPToUI(float_val,4005 LLVMValueRef int_val = LLVMConstFPToUI(float_val,
4006 LLVMIntType((unsigned)type_entry->data.floating.bit_count));4006 LLVMIntType((unsigned)type_entry->data.floating.bit_count));
4007 return LLVMConstZExt(int_val, big_int_type_ref);4007 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...@@ -4010,7 +4010,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
4010 case TypeTableEntryIdFn:4010 case TypeTableEntryIdFn:
4011 case TypeTableEntryIdMaybe:4011 case TypeTableEntryIdMaybe:
4012 {4012 {
4013 LLVMValueRef ptr_val = gen_const_val(g, const_val);4013 LLVMValueRef ptr_val = gen_const_val(g, const_val, "");
4014 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->type_ref);4014 LLVMValueRef ptr_size_int_val = LLVMConstPtrToInt(ptr_val, g->builtin_types.entry_usize->type_ref);
4015 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);4015 return LLVMConstZExt(ptr_size_int_val, big_int_type_ref);
4016 }4016 }
...@@ -4055,7 +4055,7 @@ static bool is_llvm_value_unnamed_type(TypeTableEntry *type_entry, LLVMValueRef...@@ -4055,7 +4055,7 @@ static bool is_llvm_value_unnamed_type(TypeTableEntry *type_entry, LLVMValueRef
4055 return LLVMTypeOf(val) != type_entry->type_ref;4055 return LLVMTypeOf(val) != type_entry->type_ref;
4056}4056}
40574057
4058static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {4058static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
4059 TypeTableEntry *type_entry = const_val->type;4059 TypeTableEntry *type_entry = const_val->type;
4060 assert(!type_entry->zero_bits);4060 assert(!type_entry->zero_bits);
40614061
...@@ -4108,7 +4108,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4108,7 +4108,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4108 child_type->id == TypeTableEntryIdFn)4108 child_type->id == TypeTableEntryIdFn)
4109 {4109 {
4110 if (const_val->data.x_maybe) {4110 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, "");
4112 } else {4112 } else {
4113 return LLVMConstNull(child_type->type_ref);4113 return LLVMConstNull(child_type->type_ref);
4114 }4114 }
...@@ -4117,7 +4117,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4117,7 +4117,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4117 LLVMValueRef maybe_val;4117 LLVMValueRef maybe_val;
4118 bool make_unnamed_struct;4118 bool make_unnamed_struct;
4119 if (const_val->data.x_maybe) {4119 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, "");
4121 maybe_val = LLVMConstAllOnes(LLVMInt1Type());4121 maybe_val = LLVMConstAllOnes(LLVMInt1Type());
41224122
4123 make_unnamed_struct = is_llvm_value_unnamed_type(const_val->type, child_val);4123 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) {...@@ -4161,7 +4161,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
41614161
4162 if (src_field_index + 1 == src_field_index_end) {4162 if (src_field_index + 1 == src_field_index_end) {
4163 ConstExprValue *field_val = &const_val->data.x_struct.fields[src_field_index];4163 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, "");
4165 fields[type_struct_field->gen_index] = val;4165 fields[type_struct_field->gen_index] = val;
4166 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);4166 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
4167 } else {4167 } else {
...@@ -4201,7 +4201,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4201,7 +4201,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4201 continue;4201 continue;
4202 }4202 }
4203 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];4203 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, "");
4205 fields[type_struct_field->gen_index] = val;4205 fields[type_struct_field->gen_index] = val;
4206 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);4206 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(field_val->type, val);
4207 }4207 }
...@@ -4225,7 +4225,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4225,7 +4225,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4225 bool make_unnamed_struct = false;4225 bool make_unnamed_struct = false;
4226 for (uint64_t i = 0; i < len; i += 1) {4226 for (uint64_t i = 0; i < len; i += 1) {
4227 ConstExprValue *elem_value = &const_val->data.x_array.s_none.elements[i];4227 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, "");
4229 values[i] = val;4229 values[i] = val;
4230 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);4230 make_unnamed_struct = make_unnamed_struct || is_llvm_value_unnamed_type(elem_value->type, val);
4231 }4231 }
...@@ -4260,7 +4260,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4260,7 +4260,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4260 } else {4260 } else {
4261 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, payload_value->type->type_ref);4261 uint64_t field_type_bytes = LLVMStoreSizeOfType(g->target_data_ref, payload_value->type->type_ref);
4262 uint64_t pad_bytes = type_entry->data.unionation.union_size_bytes - field_type_bytes;4262 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, "");
4264 make_unnamed_struct = is_llvm_value_unnamed_type(payload_value->type, correctly_typed_value) ||4264 make_unnamed_struct = is_llvm_value_unnamed_type(payload_value->type, correctly_typed_value) ||
4265 payload_value->type != type_entry->data.unionation.most_aligned_union_member;4265 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) {...@@ -4305,7 +4305,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4305 return fn_llvm_value(g, const_val->data.x_fn.fn_entry);4305 return fn_llvm_value(g, const_val->data.x_fn.fn_entry);
4306 case TypeTableEntryIdPointer:4306 case TypeTableEntryIdPointer:
4307 {4307 {
4308 render_const_val_global(g, const_val, "");4308 render_const_val_global(g, const_val, name);
4309 switch (const_val->data.x_ptr.special) {4309 switch (const_val->data.x_ptr.special) {
4310 case ConstPtrSpecialInvalid:4310 case ConstPtrSpecialInvalid:
4311 case ConstPtrSpecialDiscard:4311 case ConstPtrSpecialDiscard:
...@@ -4313,7 +4313,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4313,7 +4313,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4313 case ConstPtrSpecialRef:4313 case ConstPtrSpecialRef:
4314 {4314 {
4315 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;4315 ConstExprValue *pointee = const_val->data.x_ptr.data.ref.pointee;
4316 render_const_val(g, pointee);4316 render_const_val(g, pointee, "");
4317 render_const_val_global(g, pointee, "");4317 render_const_val_global(g, pointee, "");
4318 ConstExprValue *other_val = pointee;4318 ConstExprValue *other_val = pointee;
4319 const_val->global_refs->llvm_value = LLVMConstBitCast(other_val->global_refs->llvm_global, const_val->type->type_ref);4319 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) {...@@ -4383,7 +4383,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4383 return LLVMConstInt(g->err_tag_type->type_ref, value, false);4383 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
4384 } else if (!type_has_bits(err_set_type)) {4384 } else if (!type_has_bits(err_set_type)) {
4385 assert(type_has_bits(payload_type));4385 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, "");
4387 } else {4387 } else {
4388 LLVMValueRef err_tag_value;4388 LLVMValueRef err_tag_value;
4389 LLVMValueRef err_payload_value;4389 LLVMValueRef err_payload_value;
...@@ -4395,7 +4395,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4395,7 +4395,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4395 } else {4395 } else {
4396 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);4396 err_tag_value = LLVMConstNull(g->err_tag_type->type_ref);
4397 ConstExprValue *payload_val = const_val->data.x_err_union.payload;4397 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, "");
4399 make_unnamed_struct = is_llvm_value_unnamed_type(payload_val->type, err_payload_value);4399 make_unnamed_struct = is_llvm_value_unnamed_type(payload_val->type, err_payload_value);
4400 }4400 }
4401 LLVMValueRef fields[] = {4401 LLVMValueRef fields[] = {
...@@ -4430,11 +4430,11 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -4430,11 +4430,11 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
4430 zig_unreachable();4430 zig_unreachable();
4431}4431}
44324432
4433static void render_const_val(CodeGen *g, ConstExprValue *const_val) {4433static void render_const_val(CodeGen *g, ConstExprValue *const_val, const char *name) {
4434 if (!const_val->global_refs)4434 if (!const_val->global_refs)
4435 const_val->global_refs = allocate<ConstGlobalRefs>(1);4435 const_val->global_refs = allocate<ConstGlobalRefs>(1);
4436 if (!const_val->global_refs->llvm_value)4436 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
4439 if (const_val->global_refs->llvm_global)4439 if (const_val->global_refs->llvm_global)
4440 LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value);4440 LLVMSetInitializer(const_val->global_refs->llvm_global, const_val->global_refs->llvm_value);
...@@ -4663,7 +4663,7 @@ static void do_code_gen(CodeGen *g) {...@@ -4663,7 +4663,7 @@ static void do_code_gen(CodeGen *g) {
4663 coerced_value.special = ConstValSpecialStatic;4663 coerced_value.special = ConstValSpecialStatic;
4664 coerced_value.type = var_type;4664 coerced_value.type = var_type;
4665 coerced_value.data.x_f128 = bigfloat_to_f128(&const_val->data.x_bigfloat);4665 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, "");
4667 gen_global_var(g, var, init_val, var_type);4667 gen_global_var(g, var, init_val, var_type);
4668 continue;4668 continue;
4669 }4669 }
...@@ -4697,8 +4697,9 @@ static void do_code_gen(CodeGen *g) {...@@ -4697,8 +4697,9 @@ static void do_code_gen(CodeGen *g) {
4697 LLVMSetAlignment(global_value, var->align_bytes);4697 LLVMSetAlignment(global_value, var->align_bytes);
4698 } else {4698 } else {
4699 bool exported = (var->linkage == VarLinkageExport);4699 bool exported = (var->linkage == VarLinkageExport);
4700 render_const_val(g, var->value);4700 const char *mangled_name = buf_ptr(get_mangled_name(g, &var->name, exported));
4701 render_const_val_global(g, var->value, 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);
4702 global_value = var->value->global_refs->llvm_global;4703 global_value = var->value->global_refs->llvm_global;
47034704
4704 if (exported) {4705 if (exported) {