authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-10 18:38:53-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-10 18:38:53-05:00
log6feae8a4e90118df26738316ab1fb49882ba6431
tree89c113d87eca49f10681c0b6c18c66f51ff0d6fb
parent443e14afbd0615bd4902e53a31f70550a8d4497e

IR: support error union type


3 files changed, 40 insertions(+), 20 deletions(-)

src/codegen.cpp+8-6
......@@ -680,11 +680,6 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
680680 }
681681 assert(instruction->llvm_value);
682682 }
683 if (instruction->static_value.special != ConstValSpecialRuntime) {
684 if (instruction->type_entry->id == TypeTableEntryIdPointer) {
685 return LLVMBuildLoad(g->builder, instruction->static_value.llvm_global, "");
686 }
687 }
688683 return instruction->llvm_value;
689684}
690685
......@@ -1794,7 +1789,14 @@ static LLVMValueRef ir_render_switch_br(CodeGen *g, IrExecutable *executable, Ir
17941789}
17951790
17961791static LLVMValueRef ir_render_phi(CodeGen *g, IrExecutable *executable, IrInstructionPhi *instruction) {
1797 LLVMValueRef phi = LLVMBuildPhi(g->builder, instruction->base.type_entry->type_ref, "");
1792 LLVMTypeRef phi_type;
1793 if (handle_is_ptr(instruction->base.type_entry)) {
1794 phi_type = LLVMPointerType(instruction->base.type_entry->type_ref, 0);
1795 } else {
1796 phi_type = instruction->base.type_entry->type_ref;
1797 }
1798
1799 LLVMValueRef phi = LLVMBuildPhi(g->builder, phi_type, "");
17981800 LLVMValueRef *incoming_values = allocate<LLVMValueRef>(instruction->incoming_count);
17991801 LLVMBasicBlockRef *incoming_blocks = allocate<LLVMBasicBlockRef>(instruction->incoming_count);
18001802 for (size_t i = 0; i < instruction->incoming_count; i += 1) {
src/ir.cpp+26-14
......@@ -3135,12 +3135,19 @@ static TypeTableEntry *ir_determine_peer_types(IrAnalyze *ira, AstNode *source_n
31353135 if (prev_inst->type_entry->id == TypeTableEntryIdInvalid) {
31363136 return ira->codegen->builtin_types.entry_invalid;
31373137 }
3138 bool any_are_pure_error = (prev_inst->type_entry->id == TypeTableEntryIdPureError);
31383139 for (size_t i = 1; i < instruction_count; i += 1) {
31393140 IrInstruction *cur_inst = instructions[i];
31403141 TypeTableEntry *cur_type = cur_inst->type_entry;
31413142 TypeTableEntry *prev_type = prev_inst->type_entry;
31423143 if (cur_type->id == TypeTableEntryIdInvalid) {
31433144 return cur_type;
3145 } else if (prev_type->id == TypeTableEntryIdPureError) {
3146 prev_inst = cur_inst;
3147 continue;
3148 } else if (cur_type->id == TypeTableEntryIdPureError) {
3149 any_are_pure_error = true;
3150 continue;
31443151 } else if (types_match_const_cast_only(prev_type, cur_type)) {
31453152 continue;
31463153 } else if (types_match_const_cast_only(cur_type, prev_type)) {
......@@ -3198,7 +3205,11 @@ static TypeTableEntry *ir_determine_peer_types(IrAnalyze *ira, AstNode *source_n
31983205 return ira->codegen->builtin_types.entry_invalid;
31993206 }
32003207 }
3201 return prev_inst->type_entry;
3208 if (any_are_pure_error && prev_inst->type_entry->id != TypeTableEntryIdPureError) {
3209 return get_error_type(ira->codegen, prev_inst->type_entry);
3210 } else {
3211 return prev_inst->type_entry;
3212 }
32023213}
32033214
32043215enum ImplicitCastMatchResult {
......@@ -3304,6 +3315,14 @@ static TypeTableEntry *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_nod
33043315 return ir_determine_peer_types(ira, source_node, instructions, instruction_count);
33053316}
33063317
3318static void ir_add_alloca(IrAnalyze *ira, IrInstruction *instruction, TypeTableEntry *type_entry) {
3319 if (type_has_bits(type_entry) && handle_is_ptr(type_entry)) {
3320 FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
3321 assert(fn_entry);
3322 fn_entry->alloca_list.append(instruction);
3323 }
3324}
3325
33073326static IrInstruction *ir_resolve_cast(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
33083327 TypeTableEntry *wanted_type, CastOp cast_op, bool need_alloca)
33093328{
......@@ -4696,11 +4715,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
46964715 impl_fn, nullptr, impl_param_count, casted_args);
46974716
46984717 TypeTableEntry *return_type = impl_fn->type_entry->data.fn.fn_type_id.return_type;
4699 if (type_has_bits(return_type) && handle_is_ptr(return_type)) {
4700 FnTableEntry *callsite_fn = exec_fn_entry(ira->new_irb.exec);
4701 assert(callsite_fn);
4702 callsite_fn->alloca_list.append(new_call_instruction);
4703 }
4718 ir_add_alloca(ira, new_call_instruction, return_type);
47044719
47054720 return ir_finish_anal(ira, return_type);
47064721 }
......@@ -4752,12 +4767,7 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
47524767 IrInstruction *new_call_instruction = ir_build_call_from(&ira->new_irb, &call_instruction->base,
47534768 fn_entry, fn_ref, call_param_count, casted_args);
47544769
4755 if (type_has_bits(return_type) && handle_is_ptr(return_type)) {
4756 FnTableEntry *callsite_fn = exec_fn_entry(ira->new_irb.exec);
4757 assert(callsite_fn);
4758 callsite_fn->alloca_list.append(new_call_instruction);
4759 }
4760
4770 ir_add_alloca(ira, new_call_instruction, return_type);
47614771 return ir_finish_anal(ira, return_type);
47624772}
47634773
......@@ -5280,6 +5290,7 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
52805290
52815291 ir_build_phi_from(&ira->new_irb, &phi_instruction->base, new_incoming_blocks.length,
52825292 new_incoming_blocks.items, new_incoming_values.items);
5293
52835294 return resolved_type;
52845295}
52855296
......@@ -6684,7 +6695,8 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
66846695
66856696 IrInstruction *new_instruction = ir_build_struct_init_from(&ira->new_irb, instruction,
66866697 container_type, actual_field_count, new_fields);
6687 fn_entry->alloca_list.append(new_instruction);
6698
6699 ir_add_alloca(ira, new_instruction, container_type);
66886700 return container_type;
66896701}
66906702
......@@ -6754,7 +6766,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
67546766
67556767 IrInstruction *new_instruction = ir_build_container_init_list_from(&ira->new_irb, &instruction->base,
67566768 container_type_value, elem_count, new_items);
6757 fn_entry->alloca_list.append(new_instruction);
6769 ir_add_alloca(ira, new_instruction, fixed_size_array_type);
67586770 return fixed_size_array_type;
67596771 } else if (container_type->id == TypeTableEntryIdArray) {
67606772 // same as slice init but we make a compile error if the length is wrong
test/self_hosted2.zig+6
......@@ -267,6 +267,12 @@ fn testErrorName() {
267267 assert(memeql(@errorName(error.ItBroke), "ItBroke"));
268268}
269269
270//error One;
271//fn getAnErrorValue (b: bool) -> %i32 {
272// const result = if (b) error.One else i32(1234);
273// return result;
274//}
275
270276fn assert(ok: bool) {
271277 if (!ok)
272278 @unreachable();