Browse Source

Redefine how union tag size is calculated to match alignment of the union

gingerBill 6 years ago
parent
commit
784c48c9e3
2 changed files with 17 additions and 0 deletions
  1. 2 0
      src/ir.cpp
  2. 15 0
      src/types.cpp

+ 2 - 0
src/ir.cpp

@@ -7028,6 +7028,8 @@ irAddr ir_build_addr(irProcedure *proc, Ast *expr) {
 					Type *fet = ir_type(field_expr);
 					Type *fet = ir_type(field_expr);
 					// HACK TODO(bill): THIS IS A MASSIVE HACK!!!!
 					// HACK TODO(bill): THIS IS A MASSIVE HACK!!!!
 					if (is_type_union(ft) && !are_types_identical(fet, ft) && !is_type_untyped(fet)) {
 					if (is_type_union(ft) && !are_types_identical(fet, ft) && !is_type_untyped(fet)) {
+						GB_ASSERT_MSG(union_variant_index(ft, fet) > 0, "%s", type_to_string(fet));
+
 						irValue *gep = ir_emit_struct_ep(proc, v, cast(i32)index);
 						irValue *gep = ir_emit_struct_ep(proc, v, cast(i32)index);
 						ir_emit_store_union_variant(proc, gep, field_expr, fet);
 						ir_emit_store_union_variant(proc, gep, field_expr, fet);
 					} else {
 					} else {

+ 15 - 0
src/types.cpp

@@ -1632,11 +1632,26 @@ i64 union_tag_size(Type *u) {
 		return 0;
 		return 0;
 	}
 	}
 
 
+#if 1
+	// TODO(bill): Is this an okay approach?
+	i64 max_align = 1;
+	for_array(i, u->Union.variants) {
+		Type *variant_type = u->Union.variants[i];
+		i64 align = type_align_of(variant_type);
+		if (max_align < align) {
+			max_align = align;
+		}
+	}
+
+	u->Union.tag_size = gb_min(max_align, build_context.max_align);
+	return max_align;
+#else
 	i64 bytes = next_pow2(cast(i64)(floor_log2(n)/8 + 1));
 	i64 bytes = next_pow2(cast(i64)(floor_log2(n)/8 + 1));
 	i64 tag_size = gb_max(bytes, 1);
 	i64 tag_size = gb_max(bytes, 1);
 
 
 	u->Union.tag_size = tag_size;
 	u->Union.tag_size = tag_size;
 	return tag_size;
 	return tag_size;
+#endif
 }
 }
 
 
 Type *union_tag_type(Type *u) {
 Type *union_tag_type(Type *u) {