authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-27 00:14:19-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-27 00:14:19-05:00
logbd4d4ee51efebb79ed7b563a10eec992a71220fd
tree4f01f7f8c967dc8e94ef94eaedfa2f46978ec04d
parent1fba7f36960551fe0a12aa754c2d789c8784a8cc

IR: detect error for exceeding branch quota


3 files changed, 41 insertions(+), 27 deletions(-)

src/all_types.hpp+2
......@@ -36,6 +36,8 @@ struct IrExecutable {
3636 ZigList<IrBasicBlock *> basic_block_list;
3737 size_t mem_slot_count;
3838 size_t next_debug_id;
39 size_t backward_branch_count;
40 size_t backward_branch_quota;
3941 bool invalid;
4042 ZigList<LabelTableEntry *> all_labels;
4143 ZigList<AstNode *> goto_list;
src/analyze.cpp+5-1
......@@ -17,6 +17,8 @@
1717#include "parser.hpp"
1818#include "zig_llvm.hpp"
1919
20static const size_t default_backward_branch_quota = 1000;
21
2022static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *enum_type);
2123static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *struct_type);
2224
......@@ -836,7 +838,6 @@ static IrInstruction *analyze_const_value(CodeGen *g, BlockContext *scope, AstNo
836838 TypeTableEntry *expected_type)
837839{
838840 IrExecutable ir_executable = {0};
839 IrExecutable analyzed_executable = {0};
840841 ir_gen(g, node, scope, &ir_executable);
841842
842843 if (ir_executable.invalid)
......@@ -849,6 +850,8 @@ static IrInstruction *analyze_const_value(CodeGen *g, BlockContext *scope, AstNo
849850 ir_print(stderr, &ir_executable, 4);
850851 fprintf(stderr, "}\n");
851852 }
853 IrExecutable analyzed_executable = {0};
854 analyzed_executable.backward_branch_quota = default_backward_branch_quota;
852855 TypeTableEntry *result_type = ir_analyze(g, &ir_executable, &analyzed_executable, expected_type, node);
853856 if (result_type->id == TypeTableEntryIdInvalid)
854857 return g->invalid_instruction;
......@@ -1514,6 +1517,7 @@ static void preview_fn_proto_instance(CodeGen *g, ImportTableEntry *import, AstN
15141517 }
15151518
15161519 FnTableEntry *fn_table_entry = allocate<FnTableEntry>(1);
1520 fn_table_entry->analyzed_executable.backward_branch_quota = default_backward_branch_quota;
15171521 fn_table_entry->import_entry = import;
15181522 fn_table_entry->proto_node = proto_node;
15191523 fn_table_entry->fn_def_node = fn_def_node;
src/ir.cpp+34-26
......@@ -2957,8 +2957,24 @@ static void ir_finish_bb(IrAnalyze *ira) {
29572957 }
29582958}
29592959
2960static void ir_inline_bb(IrAnalyze *ira, IrBasicBlock *old_bb) {
2960static TypeTableEntry *ir_unreach_error(IrAnalyze *ira) {
2961 ira->block_queue_index = SIZE_MAX;
2962 ira->new_irb.exec->invalid = true;
2963 return ira->codegen->builtin_types.entry_unreachable;
2964}
2965
2966static TypeTableEntry *ir_inline_bb(IrAnalyze *ira, IrInstruction *source_instruction, IrBasicBlock *old_bb) {
2967 if (old_bb->debug_id <= ira->old_irb.current_basic_block->debug_id) {
2968 ira->new_irb.exec->backward_branch_count += 1;
2969 if (ira->new_irb.exec->backward_branch_count > ira->new_irb.exec->backward_branch_quota) {
2970 add_node_error(ira->codegen, source_instruction->source_node,
2971 buf_sprintf("evaluation exceeded %zu backwards branches", ira->new_irb.exec->backward_branch_quota));
2972 return ir_unreach_error(ira);
2973 }
2974 }
2975
29612976 ir_start_bb(ira, old_bb, ira->old_irb.current_basic_block);
2977 return ira->codegen->builtin_types.entry_unreachable;
29622978}
29632979
29642980static TypeTableEntry *ir_finish_anal(IrAnalyze *ira, TypeTableEntry *result_type) {
......@@ -3439,12 +3455,12 @@ static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira,
34393455{
34403456 IrInstruction *value = return_instruction->value->other;
34413457 if (value->type_entry->id == TypeTableEntryIdInvalid)
3442 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
3458 return ir_unreach_error(ira);
34433459 ira->implicit_return_type_list.append(value);
34443460
34453461 IrInstruction *casted_value = ir_get_casted_value(ira, value, ira->explicit_return_type);
34463462 if (casted_value == ira->codegen->invalid_instruction)
3447 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
3463 return ir_unreach_error(ira);
34483464
34493465 ir_build_return_from(&ira->new_irb, &return_instruction->base, casted_value);
34503466 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
......@@ -4302,11 +4318,8 @@ static TypeTableEntry *ir_analyze_instruction_un_op(IrAnalyze *ira, IrInstructio
43024318static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr *br_instruction) {
43034319 IrBasicBlock *old_dest_block = br_instruction->dest_block;
43044320
4305 // TODO detect backward jumps
4306
43074321 if (br_instruction->is_inline || old_dest_block->ref_count == 1) {
4308 ir_inline_bb(ira, old_dest_block);
4309 return ira->codegen->builtin_types.entry_unreachable;
4322 return ir_inline_bb(ira, &br_instruction->base, old_dest_block);
43104323 }
43114324
43124325 IrBasicBlock *new_bb = ir_get_new_bb(ira, old_dest_block);
......@@ -4317,28 +4330,24 @@ static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr
43174330static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstructionCondBr *cond_br_instruction) {
43184331 IrInstruction *condition = cond_br_instruction->condition->other;
43194332 if (condition->type_entry->id == TypeTableEntryIdInvalid)
4320 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
4321
4322 // TODO detect backward jumps
4333 return ir_unreach_error(ira);
43234334
43244335 if (cond_br_instruction->is_inline || condition->static_value.special != ConstValSpecialRuntime) {
43254336 bool cond_is_true;
43264337 if (!ir_resolve_bool(ira, condition, &cond_is_true))
4327 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
4338 return ir_unreach_error(ira);
43284339
43294340 IrBasicBlock *old_dest_block = cond_is_true ?
43304341 cond_br_instruction->then_block : cond_br_instruction->else_block;
43314342
4332 if (cond_br_instruction->is_inline || old_dest_block->ref_count == 1) {
4333 ir_inline_bb(ira, old_dest_block);
4334 return ira->codegen->builtin_types.entry_unreachable;
4335 }
4343 if (cond_br_instruction->is_inline || old_dest_block->ref_count == 1)
4344 return ir_inline_bb(ira, &cond_br_instruction->base, old_dest_block);
43364345 }
43374346
43384347 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;
43394348 IrInstruction *casted_condition = ir_get_casted_value(ira, condition, bool_type);
43404349 if (casted_condition == ira->codegen->invalid_instruction)
4341 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
4350 return ir_unreach_error(ira);
43424351
43434352 IrBasicBlock *new_then_block = ir_get_new_bb(ira, cond_br_instruction->then_block);
43444353 IrBasicBlock *new_else_block = ir_get_new_bb(ira, cond_br_instruction->else_block);
......@@ -5418,9 +5427,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
54185427{
54195428 IrInstruction *target_value = switch_br_instruction->target_value->other;
54205429 if (target_value->type_entry->id == TypeTableEntryIdInvalid)
5421 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
5422
5423 // TODO detect backward jumps
5430 return ir_unreach_error(ira);
54245431
54255432 size_t case_count = switch_br_instruction->case_count;
54265433 bool is_inline = switch_br_instruction->is_inline;
......@@ -5428,27 +5435,26 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
54285435 if (is_inline || target_value->static_value.special != ConstValSpecialRuntime) {
54295436 ConstExprValue *target_val = ir_resolve_const(ira, target_value);
54305437 if (!target_val)
5431 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
5438 return ir_unreach_error(ira);
54325439
54335440 for (size_t i = 0; i < case_count; i += 1) {
54345441 IrInstructionSwitchBrCase *old_case = &switch_br_instruction->cases[i];
54355442 IrInstruction *case_value = old_case->value->other;
54365443 if (case_value->type_entry->id == TypeTableEntryIdInvalid)
5437 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
5444 return ir_unreach_error(ira);
54385445
54395446 IrInstruction *casted_case_value = ir_get_casted_value(ira, case_value, target_value->type_entry);
54405447 if (casted_case_value->type_entry->id == TypeTableEntryIdInvalid)
5441 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
5448 return ir_unreach_error(ira);
54425449
54435450 ConstExprValue *case_val = ir_resolve_const(ira, casted_case_value);
54445451 if (!case_val)
5445 return ir_finish_anal(ira, ira->codegen->builtin_types.entry_unreachable);
5452 return ir_unreach_error(ira);
54465453
54475454 if (const_values_equal(target_val, case_val, target_value->type_entry)) {
54485455 IrBasicBlock *old_dest_block = old_case->block;
54495456 if (is_inline || old_dest_block->ref_count == 1) {
5450 ir_inline_bb(ira, old_dest_block);
5451 return ira->codegen->builtin_types.entry_unreachable;
5457 return ir_inline_bb(ira, &switch_br_instruction->base, old_dest_block);
54525458 } else {
54535459 IrBasicBlock *new_dest_block = ir_get_new_bb(ira, old_dest_block);
54545460 ir_build_br_from(&ira->new_irb, &switch_br_instruction->base, new_dest_block);
......@@ -5840,7 +5846,9 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl
58405846 ira->instruction_index += 1;
58415847 }
58425848
5843 if (ira->implicit_return_type_list.length == 0) {
5849 if (new_exec->invalid) {
5850 return ira->codegen->builtin_types.entry_invalid;
5851 } else if (ira->implicit_return_type_list.length == 0) {
58445852 return codegen->builtin_types.entry_unreachable;
58455853 } else {
58465854 return ir_resolve_peer_types(ira, expected_type_source_node, ira->implicit_return_type_list.items,