authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-13 16:53:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-13 16:55:59-04:00
log68d7e4a1b60a52b59bc148a81458a89b9b739ab1
treedb7f34838133a838c19634c8870c78ef154ca306
parenta43fd7a550c3cae7443f09e4294cfccb6bdf0320
signature Commit is signed but in an unrecognized format.

better handle quota of setEvalBranchQuota

Now that c58b80203443dcbf8b737ebdaa1f17fb20c77711 has removed the "top of the comptime stack" requirement, the branch quota can be modified somewhere other than the top of the comptime stack. This means that the quota of a parent IrExecutable has to be modifiable by an instruction in the child. Closes #2261

4 files changed, 21 insertions(+), 25 deletions(-)

src/all_types.hpp+2-1
...@@ -55,7 +55,7 @@ struct IrExecutable {...@@ -55,7 +55,7 @@ struct IrExecutable {
55 size_t mem_slot_count;55 size_t mem_slot_count;
56 size_t next_debug_id;56 size_t next_debug_id;
57 size_t *backward_branch_count;57 size_t *backward_branch_count;
58 size_t backward_branch_quota;58 size_t *backward_branch_quota;
59 ZigFn *fn_entry;59 ZigFn *fn_entry;
60 Buf *c_import_buf;60 Buf *c_import_buf;
61 AstNode *source_node;61 AstNode *source_node;
...@@ -1350,6 +1350,7 @@ struct ZigFn {...@@ -1350,6 +1350,7 @@ struct ZigFn {
1350 IrExecutable ir_executable;1350 IrExecutable ir_executable;
1351 IrExecutable analyzed_executable;1351 IrExecutable analyzed_executable;
1352 size_t prealloc_bbc;1352 size_t prealloc_bbc;
1353 size_t prealloc_backward_branch_quota;
1353 AstNode **param_source_nodes;1354 AstNode **param_source_nodes;
1354 Buf **param_names;1355 Buf **param_names;
13551356
src/analyze.cpp+5-2
...@@ -969,8 +969,9 @@ static ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *no...@@ -969,8 +969,9 @@ static ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *no
969 Buf *type_name)969 Buf *type_name)
970{970{
971 size_t backward_branch_count = 0;971 size_t backward_branch_count = 0;
972 size_t backward_branch_quota = default_backward_branch_quota;
972 return ir_eval_const_value(g, scope, node, type_entry,973 return ir_eval_const_value(g, scope, node, type_entry,
973 &backward_branch_count, default_backward_branch_quota,974 &backward_branch_count, &backward_branch_quota,
974 nullptr, nullptr, node, type_name, nullptr, nullptr);975 nullptr, nullptr, node, type_name, nullptr, nullptr);
975}976}
976977
...@@ -2623,9 +2624,11 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) {...@@ -2623,9 +2624,11 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) {
2623ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value) {2624ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value) {
2624 ZigFn *fn_entry = allocate<ZigFn>(1);2625 ZigFn *fn_entry = allocate<ZigFn>(1);
26252626
2627 fn_entry->prealloc_backward_branch_quota = default_backward_branch_quota;
2628
2626 fn_entry->codegen = g;2629 fn_entry->codegen = g;
2627 fn_entry->analyzed_executable.backward_branch_count = &fn_entry->prealloc_bbc;2630 fn_entry->analyzed_executable.backward_branch_count = &fn_entry->prealloc_bbc;
2628 fn_entry->analyzed_executable.backward_branch_quota = default_backward_branch_quota;2631 fn_entry->analyzed_executable.backward_branch_quota = &fn_entry->prealloc_backward_branch_quota;
2629 fn_entry->analyzed_executable.fn_entry = fn_entry;2632 fn_entry->analyzed_executable.fn_entry = fn_entry;
2630 fn_entry->ir_executable.fn_entry = fn_entry;2633 fn_entry->ir_executable.fn_entry = fn_entry;
2631 fn_entry->fn_inline = inline_value;2634 fn_entry->fn_inline = inline_value;
src/ir.cpp+13-21
...@@ -357,19 +357,9 @@ static void ir_ref_var(ZigVar *var) {...@@ -357,19 +357,9 @@ static void ir_ref_var(ZigVar *var) {
357}357}
358358
359ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {359ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {
360 ConstExprValue *result = ir_eval_const_value( ira->codegen360 ConstExprValue *result = ir_eval_const_value(ira->codegen, scope, node, ira->codegen->builtin_types.entry_type,
361 , scope361 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr, nullptr,
362 , node362 node, nullptr, ira->new_irb.exec, nullptr);
363 , ira->codegen->builtin_types.entry_type
364 , ira->new_irb.exec->backward_branch_count
365 , ira->new_irb.exec->backward_branch_quota
366 , nullptr
367 , nullptr
368 , node
369 , nullptr
370 , ira->new_irb.exec
371 , nullptr
372 );
373363
374 if (type_is_invalid(result->type))364 if (type_is_invalid(result->type))
375 return ira->codegen->builtin_types.entry_invalid;365 return ira->codegen->builtin_types.entry_invalid;
...@@ -7965,7 +7955,7 @@ static ConstExprValue *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec...@@ -7965,7 +7955,7 @@ static ConstExprValue *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec
7965 return &codegen->invalid_instruction->value;7955 return &codegen->invalid_instruction->value;
7966 }7956 }
7967 }7957 }
7968 return &codegen->invalid_instruction->value;7958 zig_unreachable();
7969}7959}
79707960
7971static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *source_instruction) {7961static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *source_instruction) {
...@@ -10227,16 +10217,18 @@ static IrInstruction *ir_unreach_error(IrAnalyze *ira) {...@@ -10227,16 +10217,18 @@ static IrInstruction *ir_unreach_error(IrAnalyze *ira) {
1022710217
10228static bool ir_emit_backward_branch(IrAnalyze *ira, IrInstruction *source_instruction) {10218static bool ir_emit_backward_branch(IrAnalyze *ira, IrInstruction *source_instruction) {
10229 size_t *bbc = ira->new_irb.exec->backward_branch_count;10219 size_t *bbc = ira->new_irb.exec->backward_branch_count;
10230 size_t quota = ira->new_irb.exec->backward_branch_quota;10220 size_t *quota = ira->new_irb.exec->backward_branch_quota;
1023110221
10232 // If we're already over quota, we've already given an error message for this.10222 // If we're already over quota, we've already given an error message for this.
10233 if (*bbc > quota) {10223 if (*bbc > *quota) {
10224 assert(ira->codegen->errors.length > 0);
10234 return false;10225 return false;
10235 }10226 }
1023610227
10237 *bbc += 1;10228 *bbc += 1;
10238 if (*bbc > quota) {10229 if (*bbc > *quota) {
10239 ir_add_error(ira, source_instruction, buf_sprintf("evaluation exceeded %" ZIG_PRI_usize " backwards branches", quota));10230 ir_add_error(ira, source_instruction,
10231 buf_sprintf("evaluation exceeded %" ZIG_PRI_usize " backwards branches", *quota));
10240 return false;10232 return false;
10241 }10233 }
10242 return true;10234 return true;
...@@ -10317,7 +10309,7 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un...@@ -10317,7 +10309,7 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
10317}10309}
1031810310
10319ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,10311ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
10320 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,10312 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
10321 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,10313 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
10322 IrExecutable *parent_exec, AstNode *expected_type_source_node)10314 IrExecutable *parent_exec, AstNode *expected_type_source_node)
10323{10315{
...@@ -18983,8 +18975,8 @@ static IrInstruction *ir_analyze_instruction_set_eval_branch_quota(IrAnalyze *ir...@@ -18983,8 +18975,8 @@ static IrInstruction *ir_analyze_instruction_set_eval_branch_quota(IrAnalyze *ir
18983 if (!ir_resolve_usize(ira, instruction->new_quota->child, &new_quota))18975 if (!ir_resolve_usize(ira, instruction->new_quota->child, &new_quota))
18984 return ira->codegen->invalid_instruction;18976 return ira->codegen->invalid_instruction;
1898518977
18986 if (new_quota > ira->new_irb.exec->backward_branch_quota) {18978 if (new_quota > *ira->new_irb.exec->backward_branch_quota) {
18987 ira->new_irb.exec->backward_branch_quota = new_quota;18979 *ira->new_irb.exec->backward_branch_quota = new_quota;
18988 }18980 }
1898918981
18990 return ir_const_void(ira, &instruction->base);18982 return ir_const_void(ira, &instruction->base);
src/ir.hpp+1-1
...@@ -14,7 +14,7 @@ bool ir_gen(CodeGen *g, AstNode *node, Scope *scope, IrExecutable *ir_executable...@@ -14,7 +14,7 @@ bool ir_gen(CodeGen *g, AstNode *node, Scope *scope, IrExecutable *ir_executable
14bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);14bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
1515
16ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,16ConstExprValue *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
17 ZigType *expected_type, size_t *backward_branch_count, size_t backward_branch_quota,17 ZigType *expected_type, size_t *backward_branch_count, size_t *backward_branch_quota,
18 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,18 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
19 IrExecutable *parent_exec, AstNode *expected_type_source_node);19 IrExecutable *parent_exec, AstNode *expected_type_source_node);
2020