aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-08-26 14:25:52 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-08-26 14:25:52 -0400
commita0ae575ff8aef87d2dc4134c625f3a479b484b0f (patch)
treed7442193c336b49eb5a84a477a521ab85ad7d57f /src/codegen.cpp
parent40feecb3e468ca9fc3b5bcbe4b0a4aa30d10f226 (diff)
downloadzig-a0ae575ff8aef87d2dc4134c625f3a479b484b0f.tar.gz
zig-a0ae575ff8aef87d2dc4134c625f3a479b484b0f.zip
codegen for enums chooses best order of tag and union fields
closes #396
Diffstat (limited to 'src/codegen.cpp')
-rw-r--r--src/codegen.cpp20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp
index ac4f94e133..28f1887c9b 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -2250,6 +2250,11 @@ static LLVMValueRef ir_render_struct_field_ptr(CodeGen *g, IrExecutable *executa
static LLVMValueRef ir_render_enum_field_ptr(CodeGen *g, IrExecutable *executable,
IrInstructionEnumFieldPtr *instruction)
{
+ TypeTableEntry *enum_ptr_type = instruction->enum_ptr->value.type;
+ assert(enum_ptr_type->id == TypeTableEntryIdPointer);
+ TypeTableEntry *enum_type = enum_ptr_type->data.pointer.child_type;
+ assert(enum_type->id == TypeTableEntryIdEnum);
+
TypeEnumField *field = instruction->field;
if (!type_has_bits(field->type_entry))
@@ -2257,7 +2262,7 @@ static LLVMValueRef ir_render_enum_field_ptr(CodeGen *g, IrExecutable *executabl
LLVMValueRef enum_ptr = ir_llvm_value(g, instruction->enum_ptr);
LLVMTypeRef field_type_ref = LLVMPointerType(field->type_entry->type_ref, 0);
- LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, enum_ptr, enum_gen_union_index, "");
+ LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, enum_ptr, enum_type->data.enumeration.gen_union_index, "");
LLVMValueRef bitcasted_union_field_ptr = LLVMBuildBitCast(g->builder, union_field_ptr, field_type_ref, "");
return bitcasted_union_field_ptr;
@@ -3112,7 +3117,7 @@ static LLVMValueRef ir_render_enum_tag(CodeGen *g, IrExecutable *executable, IrI
if (enum_type->data.enumeration.gen_field_count == 0)
return enum_val;
- LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, enum_val, enum_gen_tag_index, "");
+ LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, enum_val, enum_type->data.enumeration.gen_tag_index, "");
return get_handle_value(g, tag_field_ptr, tag_type, false);
}
@@ -3127,13 +3132,13 @@ static LLVMValueRef ir_render_init_enum(CodeGen *g, IrExecutable *executable, Ir
LLVMValueRef tmp_struct_ptr = instruction->tmp_ptr;
- LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, enum_gen_tag_index, "");
+ LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, enum_type->data.enumeration.gen_tag_index, "");
LLVMBuildStore(g->builder, tag_value, tag_field_ptr);
TypeTableEntry *union_val_type = instruction->field->type_entry;
if (type_has_bits(union_val_type)) {
LLVMValueRef new_union_val = ir_llvm_value(g, instruction->init_value);
- LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, enum_gen_union_index, "");
+ LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, enum_type->data.enumeration.gen_union_index, "");
LLVMValueRef bitcasted_union_field_ptr = LLVMBuildBitCast(g->builder, union_field_ptr,
LLVMPointerType(union_val_type->type_ref, 0), "");
@@ -3687,10 +3692,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
} else {
union_value = LLVMGetUndef(union_type_ref);
}
- LLVMValueRef fields[] = {
- tag_value,
- union_value,
- };
+ LLVMValueRef fields[2];
+ fields[type_entry->data.enumeration.gen_tag_index] = tag_value;
+ fields[type_entry->data.enumeration.gen_union_index] = union_value;
return LLVMConstStruct(fields, 2, false);
}
}