authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-16 10:06:58-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-16 10:06:58-05:00
logf12d36641f67564d2103f75ed7a5445219197db5
tree63f967ea88d81475466fbc00929e05d8fe1ec2a3
parent018cbff438cedc19d0ad18021619ec7ede997307

union secret field is the tag index instead of distinct type index

See #144

4 files changed, 15 insertions(+), 17 deletions(-)

src/all_types.hpp+3-3
......@@ -105,7 +105,8 @@ struct ConstStructValue {
105105};
106106
107107struct ConstUnionValue {
108 ConstExprValue *value;
108 uint64_t tag;
109 ConstExprValue *payload;
109110 ConstParent parent;
110111};
111112
......@@ -349,6 +350,7 @@ struct TypeEnumField {
349350struct TypeUnionField {
350351 Buf *name;
351352 TypeTableEntry *type_entry;
353 uint32_t value;
352354 uint32_t gen_index;
353355};
354356
......@@ -1067,8 +1069,6 @@ struct TypeTableEntryUnion {
10671069
10681070 uint32_t union_size_bytes;
10691071 TypeTableEntry *most_aligned_union_member;
1070
1071 HashMap<const TypeTableEntry *, uint32_t, type_ptr_hash, type_ptr_eql> distinct_types = {};
10721072};
10731073
10741074struct FnGenParamInfo {
src/analyze.cpp+8-10
......@@ -1872,8 +1872,6 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
18721872
18731873 bool auto_layout = (union_type->data.unionation.layout == ContainerLayoutAuto);
18741874 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
1875 auto distinct_types = &union_type->data.unionation.distinct_types;
1876 distinct_types->init(4);
18771875
18781876 Scope *scope = &union_type->data.unionation.decls_scope->base;
18791877 ImportTableEntry *import = get_scope_import(scope);
......@@ -1895,10 +1893,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
18951893 if (!type_has_bits(field_type))
18961894 continue;
18971895
1898 size_t distinct_type_index = distinct_types->size();
1899 if (distinct_types->put_unique(field_type, distinct_type_index) == nullptr) {
1900 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(&field_type->name), distinct_type_index);
1901 }
1896 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(type_union_field->name), i);
19021897
19031898 uint64_t store_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, field_type->type_ref);
19041899 uint64_t abi_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, field_type->type_ref);
......@@ -1954,7 +1949,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
19541949
19551950 assert(most_aligned_union_member != nullptr);
19561951
1957 bool want_safety = (distinct_types->size() > 1) && auto_layout;
1952 bool want_safety = auto_layout && (field_count >= 2);
19581953 uint64_t padding_in_bits = biggest_size_in_bits - size_of_most_aligned_member_in_bits;
19591954
19601955
......@@ -2007,7 +2002,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
20072002 assert(8*LLVMStoreSizeOfType(g->target_data_ref, union_type_ref) >= biggest_size_in_bits);
20082003
20092004 // create llvm type for root struct
2010 TypeTableEntry *tag_int_type = get_smallest_unsigned_int_type(g, distinct_types->size() - 1);
2005 TypeTableEntry *tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
20112006 TypeTableEntry *tag_type_entry = tag_int_type;
20122007 union_type->data.unionation.tag_type = tag_type_entry;
20132008 uint64_t align_of_tag_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_int_type->type_ref);
......@@ -2034,7 +2029,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
20342029 ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
20352030 ZigLLVMTypeToScope(union_type->di_type), "AnonEnum",
20362031 import->di_file, (unsigned)(decl_node->line + 1),
2037 tag_debug_size_in_bits, tag_debug_align_in_bits, di_enumerators, distinct_types->size(),
2032 tag_debug_size_in_bits, tag_debug_align_in_bits, di_enumerators, field_count,
20382033 tag_type_entry->di_type, "");
20392034
20402035 // create debug type for union
......@@ -2257,6 +2252,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
22572252 type_union_field->name = field_node->data.struct_field.name;
22582253 TypeTableEntry *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
22592254 type_union_field->type_entry = field_type;
2255 type_union_field->value = i;
22602256
22612257 type_ensure_zero_bits_known(g, field_type);
22622258 if (type_is_invalid(field_type)) {
......@@ -2276,9 +2272,11 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
22762272 }
22772273 }
22782274
2275 bool auto_layout = (union_type->data.unionation.layout == ContainerLayoutAuto);
2276
22792277 union_type->data.unionation.zero_bits_loop_flag = false;
22802278 union_type->data.unionation.gen_field_count = gen_field_index;
2281 union_type->zero_bits = (gen_field_index == 0);
2279 union_type->zero_bits = (gen_field_index == 0 && (field_count < 2 || !auto_layout));
22822280 union_type->data.unionation.zero_bits_known = true;
22832281
22842282 // also compute abi_alignment
src/codegen.cpp+2-3
......@@ -3962,7 +3962,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
39623962 case TypeTableEntryIdUnion:
39633963 {
39643964 LLVMTypeRef union_type_ref = type_entry->data.unionation.union_type_ref;
3965 ConstExprValue *payload_value = const_val->data.x_union.value;
3965 ConstExprValue *payload_value = const_val->data.x_union.payload;
39663966 assert(payload_value != nullptr);
39673967
39683968 if (!type_has_bits(payload_value->type)) {
......@@ -3999,8 +3999,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
39993999 return union_value_ref;
40004000 }
40014001
4002 size_t distinct_type_index = type_entry->data.unionation.distinct_types.get(const_val->data.x_union.value->type);
4003 LLVMValueRef tag_value = LLVMConstInt(type_entry->data.unionation.tag_type->type_ref, distinct_type_index, false);
4002 LLVMValueRef tag_value = LLVMConstInt(type_entry->data.unionation.tag_type->type_ref, const_val->data.x_union.tag, false);
40044003
40054004 LLVMValueRef fields[2];
40064005 fields[type_entry->data.unionation.gen_union_index] = union_value_ref;
src/ir.cpp+2-1
......@@ -13158,7 +13158,8 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir
1315813158 return ira->codegen->builtin_types.entry_invalid;
1315913159
1316013160 ConstExprValue *out_val = ir_build_const_from(ira, instruction);
13161 out_val->data.x_union.value = field_val;
13161 out_val->data.x_union.payload = field_val;
13162 out_val->data.x_union.tag = type_field->value;
1316213163
1316313164 ConstParent *parent = get_const_val_parent(ira->codegen, field_val);
1316413165 if (parent != nullptr) {