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
signaturelock-open 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 {
5555 size_t mem_slot_count;
5656 size_t next_debug_id;
5757 size_t *backward_branch_count;
58 size_t backward_branch_quota;
58 size_t *backward_branch_quota;
5959 ZigFn *fn_entry;
6060 Buf *c_import_buf;
6161 AstNode *source_node;
......@@ -1350,6 +1350,7 @@ struct ZigFn {
13501350 IrExecutable ir_executable;
13511351 IrExecutable analyzed_executable;
13521352 size_t prealloc_bbc;
1353 size_t prealloc_backward_branch_quota;
13531354 AstNode **param_source_nodes;
13541355 Buf **param_names;
13551356
src/analyze.cpp+5-2
......@@ -969,8 +969,9 @@ static ConstExprValue *analyze_const_value(CodeGen *g, Scope *scope, AstNode *no
969969 Buf *type_name)
970970{
971971 size_t backward_branch_count = 0;
972 size_t backward_branch_quota = default_backward_branch_quota;
972973 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,
974975 nullptr, nullptr, node, type_name, nullptr, nullptr);
975976}
976977
......@@ -2623,9 +2624,11 @@ static void get_fully_qualified_decl_name(Buf *buf, Tld *tld) {
26232624ZigFn *create_fn_raw(CodeGen *g, FnInline inline_value) {
26242625 ZigFn *fn_entry = allocate<ZigFn>(1);
26252626
2627 fn_entry->prealloc_backward_branch_quota = default_backward_branch_quota;
2628
26262629 fn_entry->codegen = g;
26272630 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;
26292632 fn_entry->analyzed_executable.fn_entry = fn_entry;
26302633 fn_entry->ir_executable.fn_entry = fn_entry;
26312634 fn_entry->fn_inline = inline_value;
src/ir.cpp+13-21
......@@ -357,19 +357,9 @@ static void ir_ref_var(ZigVar *var) {
357357}
358358
359359ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {
360 ConstExprValue *result = ir_eval_const_value( ira->codegen
361 , scope
362 , node
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 );
360 ConstExprValue *result = ir_eval_const_value(ira->codegen, scope, node, ira->codegen->builtin_types.entry_type,
361 ira->new_irb.exec->backward_branch_count, ira->new_irb.exec->backward_branch_quota, nullptr, nullptr,
362 node, nullptr, ira->new_irb.exec, nullptr);
373363
374364 if (type_is_invalid(result->type))
375365 return ira->codegen->builtin_types.entry_invalid;
......@@ -7965,7 +7955,7 @@ static ConstExprValue *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec
79657955 return &codegen->invalid_instruction->value;
79667956 }
79677957 }
7968 return &codegen->invalid_instruction->value;
7958 zig_unreachable();
79697959}
79707960
79717961static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *source_instruction) {
......@@ -10227,16 +10217,18 @@ static IrInstruction *ir_unreach_error(IrAnalyze *ira) {
1022710217
1022810218static bool ir_emit_backward_branch(IrAnalyze *ira, IrInstruction *source_instruction) {
1022910219 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
1023210222 // 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);
1023410225 return false;
1023510226 }
1023610227
1023710228 *bbc += 1;
10238 if (*bbc > quota) {
10239 ir_add_error(ira, source_instruction, buf_sprintf("evaluation exceeded %" ZIG_PRI_usize " backwards branches", quota));
10229 if (*bbc > *quota) {
10230 ir_add_error(ira, source_instruction,
10231 buf_sprintf("evaluation exceeded %" ZIG_PRI_usize " backwards branches", *quota));
1024010232 return false;
1024110233 }
1024210234 return true;
......@@ -10317,7 +10309,7 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
1031710309}
1031810310
1031910311ConstExprValue *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,
1032110313 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
1032210314 IrExecutable *parent_exec, AstNode *expected_type_source_node)
1032310315{
......@@ -18983,8 +18975,8 @@ static IrInstruction *ir_analyze_instruction_set_eval_branch_quota(IrAnalyze *ir
1898318975 if (!ir_resolve_usize(ira, instruction->new_quota->child, &new_quota))
1898418976 return ira->codegen->invalid_instruction;
1898518977
18986 if (new_quota > ira->new_irb.exec->backward_branch_quota) {
18987 ira->new_irb.exec->backward_branch_quota = new_quota;
18978 if (new_quota > *ira->new_irb.exec->backward_branch_quota) {
18979 *ira->new_irb.exec->backward_branch_quota = new_quota;
1898818980 }
1898918981
1899018982 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
1414bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);
1515
1616ConstExprValue *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,
1818 ZigFn *fn_entry, Buf *c_import_buf, AstNode *source_node, Buf *exec_name,
1919 IrExecutable *parent_exec, AstNode *expected_type_source_node);
2020