authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-21 21:49:05-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-21 21:49:05-05:00
log9b616820370c08a97d95805ca31eae5f8ca554f3
tree02156ccb1d8a72c747a08b4312820e5ab649cdfa
parent1f6dacbb2f9a91a20309b271a31e781f11f81819

IR: implement runtime enum init and switch on enum with variable


5 files changed, 90 insertions(+), 19 deletions(-)

src/analyze.cpp+2
......@@ -749,6 +749,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
749749 // next, loop over the parameters again and compute debug information
750750 // and codegen information
751751 if (!skip_debug_info) {
752 ensure_complete_type(g, fn_type_id->return_type);
752753 bool first_arg_return = !fn_type_id->is_extern && handle_is_ptr(fn_type_id->return_type);
753754 // +1 for maybe making the first argument the return value
754755 LLVMTypeRef *gen_param_types = allocate<LLVMTypeRef>(1 + fn_type_id->param_count);
......@@ -2534,6 +2535,7 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
25342535 case TypeTableEntryIdErrorUnion:
25352536 return type_has_bits(type_entry->data.error.child_type);
25362537 case TypeTableEntryIdEnum:
2538 assert(type_entry->data.enumeration.complete);
25372539 return type_entry->data.enumeration.gen_field_count != 0;
25382540 case TypeTableEntryIdMaybe:
25392541 return type_entry->data.maybe.child_type->id != TypeTableEntryIdPointer &&
src/codegen.cpp+32-10
......@@ -645,7 +645,7 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, LLVMValueRef src, LLVMValueRef
645645 return LLVMBuildCall(g->builder, g->memcpy_fn_val, params, 5, "");
646646}
647647
648static LLVMValueRef gen_assign_raw(CodeGen *g, AstNode *source_node,
648static LLVMValueRef gen_assign_raw(CodeGen *g,
649649 LLVMValueRef target_ref, LLVMValueRef value,
650650 TypeTableEntry *op1_type, TypeTableEntry *op2_type)
651651{
......@@ -698,8 +698,7 @@ static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrIns
698698 LLVMBuildRet(g->builder, by_val_value);
699699 } else {
700700 assert(g->cur_ret_ptr);
701 gen_assign_raw(g, return_instruction->base.source_node, g->cur_ret_ptr, value,
702 return_type, return_instruction->value->type_entry);
701 gen_assign_raw(g, g->cur_ret_ptr, value, return_type, return_instruction->value->type_entry);
703702 LLVMBuildRetVoid(g->builder);
704703 }
705704 } else {
......@@ -1232,8 +1231,7 @@ static LLVMValueRef ir_render_decl_var(CodeGen *g, IrExecutable *executable,
12321231 want_zeroes = true;
12331232
12341233 if (have_init_expr) {
1235 gen_assign_raw(g, init_value->source_node, var->value_ref,
1236 ir_llvm_value(g, init_value), var->type, init_value->type_entry);
1234 gen_assign_raw(g, var->value_ref, ir_llvm_value(g, init_value), var->type, init_value->type_entry);
12371235 } else {
12381236 bool ignore_uninit = false;
12391237 // handle runtime stack allocation
......@@ -2078,8 +2076,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
20782076 assert(instruction->tmp_ptr);
20792077
20802078 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_child_index, "");
2081 gen_assign_raw(g, instruction->base.source_node, val_ptr, payload_val, child_type, instruction->value->type_entry);
2082
2079 gen_assign_raw(g, val_ptr, payload_val, child_type, instruction->value->type_entry);
20832080 LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_null_index, "");
20842081 LLVMBuildStore(g->builder, LLVMConstAllOnes(LLVMInt1Type()), maybe_ptr);
20852082
......@@ -2125,7 +2122,7 @@ static LLVMValueRef ir_render_err_wrap_payload(CodeGen *g, IrExecutable *executa
21252122 LLVMBuildStore(g->builder, ok_err_val, err_tag_ptr);
21262123
21272124 LLVMValueRef payload_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, err_union_payload_index, "");
2128 gen_assign_raw(g, instruction->base.source_node, payload_ptr, payload_val, child_type, instruction->value->type_entry);
2125 gen_assign_raw(g, payload_ptr, payload_val, child_type, instruction->value->type_entry);
21292126
21302127 return instruction->tmp_ptr;
21312128}
......@@ -2145,7 +2142,30 @@ static LLVMValueRef ir_render_enum_tag(CodeGen *g, IrExecutable *executable, IrI
21452142}
21462143
21472144static LLVMValueRef ir_render_init_enum(CodeGen *g, IrExecutable *executable, IrInstructionInitEnum *instruction) {
2148 zig_panic("TODO ir_render_init_enum");
2145 TypeTableEntry *enum_type = instruction->enum_type;
2146 uint32_t value = instruction->field->value;
2147 LLVMTypeRef tag_type_ref = enum_type->data.enumeration.tag_type->type_ref;
2148 LLVMValueRef tag_value = LLVMConstInt(tag_type_ref, value, false);
2149
2150 if (enum_type->data.enumeration.gen_field_count == 0)
2151 return tag_value;
2152
2153 LLVMValueRef tmp_struct_ptr = instruction->tmp_ptr;
2154
2155 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, enum_gen_tag_index, "");
2156 LLVMBuildStore(g->builder, tag_value, tag_field_ptr);
2157
2158 TypeTableEntry *union_val_type = instruction->field->type_entry;
2159 if (type_has_bits(union_val_type)) {
2160 LLVMValueRef new_union_val = ir_llvm_value(g, instruction->init_value);
2161 LLVMValueRef union_field_ptr = LLVMBuildStructGEP(g->builder, tmp_struct_ptr, enum_gen_union_index, "");
2162 LLVMValueRef bitcasted_union_field_ptr = LLVMBuildBitCast(g->builder, union_field_ptr,
2163 LLVMPointerType(union_val_type->type_ref, 0), "");
2164
2165 gen_assign_raw(g, bitcasted_union_field_ptr, new_union_val, union_val_type, union_val_type);
2166 }
2167
2168 return tmp_struct_ptr;
21492169}
21502170
21512171static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
......@@ -2750,7 +2770,9 @@ static void do_code_gen(CodeGen *g) {
27502770
27512771 if (!type_has_bits(fn_type->data.fn.fn_type_id.return_type)) {
27522772 // nothing to do
2753 } else if (fn_type->data.fn.fn_type_id.return_type->id == TypeTableEntryIdPointer) {
2773 } else if (fn_type->data.fn.fn_type_id.return_type->id == TypeTableEntryIdPointer ||
2774 fn_type->data.fn.fn_type_id.return_type->id == TypeTableEntryIdFn)
2775 {
27542776 ZigLLVMAddNonNullAttr(fn_val, 0);
27552777 } else if (handle_is_ptr(fn_type->data.fn.fn_type_id.return_type) &&
27562778 !fn_type->data.fn.fn_type_id.is_extern)
src/ir.cpp+39-7
......@@ -4324,6 +4324,13 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction,
43244324 return ir_add_error_node(ira, source_instruction->source_node, msg);
43254325}
43264326
4327static void ir_add_typedef_err_note(IrAnalyze *ira, ErrorMsg *msg, TypeTableEntry *type_entry) {
4328 if (type_entry->id == TypeTableEntryIdTypeDecl) {
4329 // requires tracking source_node in the typedecl type
4330 zig_panic("TODO add error note about typedecls");
4331 }
4332}
4333
43274334static IrInstruction *ir_exec_const_result(IrExecutable *exec) {
43284335 if (exec->basic_block_list.length != 1)
43294336 return nullptr;
......@@ -5451,8 +5458,7 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
54515458 if (value->type_entry->id == TypeTableEntryIdInvalid)
54525459 return ira->codegen->builtin_types.entry_invalid;
54535460
5454 bool is_inline = ir_should_inline(&ira->new_irb);
5455 if (is_inline || instr_is_comptime(value)) {
5461 if (instr_is_comptime(value)) {
54565462 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
54575463 if (!val)
54585464 return ira->codegen->builtin_types.entry_invalid;
......@@ -8223,7 +8229,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
82238229 case TypeTableEntryIdUnion:
82248230 case TypeTableEntryIdBlock:
82258231 case TypeTableEntryIdBoundFn:
8226 ir_add_error_node(ira, switch_target_instruction->base.source_node,
8232 ir_add_error(ira, &switch_target_instruction->base,
82278233 buf_sprintf("invalid switch target type '%s'", buf_ptr(&target_type->name)));
82288234 // TODO if this is a typedecl, add error note showing the declaration of the type decl
82298235 return ira->codegen->builtin_types.entry_invalid;
......@@ -8231,10 +8237,36 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
82318237 zig_unreachable();
82328238}
82338239
8234static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira,
8235 IrInstructionSwitchVar *switch_var_instruction)
8236{
8237 zig_panic("TODO switch var analyze");
8240static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstructionSwitchVar *instruction) {
8241 IrInstruction *target_value_ptr = instruction->target_value_ptr->other;
8242 if (target_value_ptr->type_entry->id == TypeTableEntryIdInvalid)
8243 return ira->codegen->builtin_types.entry_invalid;
8244
8245 IrInstruction *prong_value = instruction->prong_value->other;
8246 if (prong_value->type_entry->id == TypeTableEntryIdInvalid)
8247 return ira->codegen->builtin_types.entry_invalid;
8248
8249 assert(target_value_ptr->type_entry->id == TypeTableEntryIdPointer);
8250 TypeTableEntry *target_type = target_value_ptr->type_entry->data.pointer.child_type;
8251 if (target_type->id == TypeTableEntryIdEnum) {
8252 ConstExprValue *prong_val = ir_resolve_const(ira, prong_value, UndefBad);
8253 if (!prong_val)
8254 return ira->codegen->builtin_types.entry_invalid;
8255
8256 TypeEnumField *field = &target_type->data.enumeration.fields[prong_val->data.x_bignum.data.x_uint];
8257 if (instr_is_comptime(target_value_ptr)) {
8258 zig_panic("TODO comptime switch var");
8259 }
8260
8261 ir_build_enum_field_ptr_from(&ira->new_irb, &instruction->base, target_value_ptr, field);
8262 return get_pointer_to_type(ira->codegen, field->type_entry,
8263 target_value_ptr->type_entry->data.pointer.is_const);
8264 } else {
8265 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
8266 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));
8267 ir_add_typedef_err_note(ira, msg, target_type);
8268 return ira->codegen->builtin_types.entry_invalid;
8269 }
82388270}
82398271
82408272static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira, IrInstructionEnumTag *enum_tag_instruction) {
src/ir_print.cpp+2-2
......@@ -919,9 +919,9 @@ static void ir_print_test_comptime(IrPrint *irp, IrInstructionTestComptime *inst
919919}
920920
921921static void ir_print_init_enum(IrPrint *irp, IrInstructionInitEnum *instruction) {
922 fprintf(irp->f, "%s.%s { ", buf_ptr(&instruction->enum_type->name), buf_ptr(instruction->field->name));
922 fprintf(irp->f, "%s.%s {", buf_ptr(&instruction->enum_type->name), buf_ptr(instruction->field->name));
923923 ir_print_other_instruction(irp, instruction->init_value);
924 fprintf(irp->f, "{");
924 fprintf(irp->f, "}");
925925}
926926
927927static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
test/cases3/enum.zig+15
......@@ -12,6 +12,16 @@ fn enumType() {
1212 assert(@sizeOf(Foo) == expected_foo_size);
1313 assert(@sizeOf(Bar) == 1);
1414}
15
16fn enumAsReturnValue () {
17 @setFnTest(this);
18
19 switch (returnAnInt(13)) {
20 Foo.One => |value| assert(value == 13),
21 else => @unreachable(),
22 }
23}
24
1525const Point = struct {
1626 x: u64,
1727 y: u64,
......@@ -28,6 +38,11 @@ const Bar = enum {
2838 D,
2939};
3040
41fn returnAnInt(x: i32) -> Foo {
42 Foo.One { x }
43}
44
45
3146fn assert(ok: bool) {
3247 if (!ok)
3348 @unreachable();