authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-04 14:06:00-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-04 16:58:19-04:00
logcbb3f1d76c6a89acdb04cb628c349eee69dfacf5
treebb96691ed6733770dac3d5eeb2f6e3fd7f67c622
parentb00007056de3c0c6bed30e1031ee7180ee08a380
signaturelock-open Commit is signed but in an unrecognized format.

ir: consistent error checking for br and cond_br instructions


1 files changed, 27 insertions(+), 11 deletions(-)

src/ir.cpp+27-11
...@@ -9532,6 +9532,19 @@ static IrBasicBlock *ir_get_new_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrInstr...@@ -9532,6 +9532,19 @@ static IrBasicBlock *ir_get_new_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrInstr
9532 return new_bb;9532 return new_bb;
9533}9533}
95349534
9535static IrBasicBlock *ir_get_new_bb_runtime(IrAnalyze *ira, IrBasicBlock *old_bb, IrInstruction *ref_old_instruction) {
9536 assert(ref_old_instruction != nullptr);
9537 IrBasicBlock *new_bb = ir_get_new_bb(ira, old_bb, ref_old_instruction);
9538 if (new_bb->must_be_comptime_source_instr) {
9539 ErrorMsg *msg = ir_add_error(ira, ref_old_instruction,
9540 buf_sprintf("control flow attempts to use compile-time variable at runtime"));
9541 add_error_note(ira->codegen, msg, new_bb->must_be_comptime_source_instr->source_node,
9542 buf_sprintf("compile-time variable assigned here"));
9543 return nullptr;
9544 }
9545 return new_bb;
9546}
9547
9535static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *const_predecessor_bb) {9548static void ir_start_bb(IrAnalyze *ira, IrBasicBlock *old_bb, IrBasicBlock *const_predecessor_bb) {
9536 ira->instruction_index = 0;9549 ira->instruction_index = 0;
9537 ira->old_irb.current_basic_block = old_bb;9550 ira->old_irb.current_basic_block = old_bb;
...@@ -13891,15 +13904,9 @@ static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr...@@ -13891,15 +13904,9 @@ static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr
13891 if (is_comptime || old_dest_block->ref_count == 1)13904 if (is_comptime || old_dest_block->ref_count == 1)
13892 return ir_inline_bb(ira, &br_instruction->base, old_dest_block);13905 return ir_inline_bb(ira, &br_instruction->base, old_dest_block);
1389313906
13894 IrBasicBlock *new_bb = ir_get_new_bb(ira, old_dest_block, &br_instruction->base);13907 IrBasicBlock *new_bb = ir_get_new_bb_runtime(ira, old_dest_block, &br_instruction->base);
1389513908 if (new_bb == nullptr)
13896 if (new_bb->must_be_comptime_source_instr) {
13897 ErrorMsg *msg = ir_add_error(ira, &br_instruction->base,
13898 buf_sprintf("control flow attempts to use compile-time variable at runtime"));
13899 add_error_note(ira->codegen, msg, new_bb->must_be_comptime_source_instr->source_node,
13900 buf_sprintf("compile-time variable assigned here"));
13901 return ir_unreach_error(ira);13909 return ir_unreach_error(ira);
13902 }
1390313910
13904 ir_build_br_from(&ira->new_irb, &br_instruction->base, new_bb);13911 ir_build_br_from(&ira->new_irb, &br_instruction->base, new_bb);
13905 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);13912 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
...@@ -13925,7 +13932,10 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct...@@ -13925,7 +13932,10 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct
13925 if (is_comptime || old_dest_block->ref_count == 1)13932 if (is_comptime || old_dest_block->ref_count == 1)
13926 return ir_inline_bb(ira, &cond_br_instruction->base, old_dest_block);13933 return ir_inline_bb(ira, &cond_br_instruction->base, old_dest_block);
1392713934
13928 IrBasicBlock *new_dest_block = ir_get_new_bb(ira, old_dest_block, &cond_br_instruction->base);13935 IrBasicBlock *new_dest_block = ir_get_new_bb_runtime(ira, old_dest_block, &cond_br_instruction->base);
13936 if (new_dest_block == nullptr)
13937 return ir_unreach_error(ira);
13938
13929 ir_build_br_from(&ira->new_irb, &cond_br_instruction->base, new_dest_block);13939 ir_build_br_from(&ira->new_irb, &cond_br_instruction->base, new_dest_block);
13930 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);13940 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
13931 }13941 }
...@@ -13936,8 +13946,14 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct...@@ -13936,8 +13946,14 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct
13936 return ir_unreach_error(ira);13946 return ir_unreach_error(ira);
1393713947
13938 assert(cond_br_instruction->then_block != cond_br_instruction->else_block);13948 assert(cond_br_instruction->then_block != cond_br_instruction->else_block);
13939 IrBasicBlock *new_then_block = ir_get_new_bb(ira, cond_br_instruction->then_block, &cond_br_instruction->base);13949 IrBasicBlock *new_then_block = ir_get_new_bb_runtime(ira, cond_br_instruction->then_block, &cond_br_instruction->base);
13940 IrBasicBlock *new_else_block = ir_get_new_bb(ira, cond_br_instruction->else_block, &cond_br_instruction->base);13950 if (new_then_block == nullptr)
13951 return ir_unreach_error(ira);
13952
13953 IrBasicBlock *new_else_block = ir_get_new_bb_runtime(ira, cond_br_instruction->else_block, &cond_br_instruction->base);
13954 if (new_else_block == nullptr)
13955 return ir_unreach_error(ira);
13956
13941 ir_build_cond_br_from(&ira->new_irb, &cond_br_instruction->base,13957 ir_build_cond_br_from(&ira->new_irb, &cond_br_instruction->base,
13942 casted_condition, new_then_block, new_else_block, nullptr);13958 casted_condition, new_then_block, new_else_block, nullptr);
13943 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);13959 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);