aboutsummaryrefslogtreecommitdiff
path: root/src/Module.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-01-04 13:40:51 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-01-04 13:40:51 -0700
commitef2fa67ef018a4a49124a40e60941149efd34553 (patch)
tree5c5c538cf0b1d79e6626637ea5b3192e7cdec868 /src/Module.zig
parentaa0906e9aaaf36bc928b5502bdb34e7a0409b2c0 (diff)
parent7e64dc42215c93a2d1d6b7fa4f5e07b885788a7d (diff)
downloadzig-ef2fa67ef018a4a49124a40e60941149efd34553.tar.gz
zig-ef2fa67ef018a4a49124a40e60941149efd34553.zip
Merge branch 'g-w1-stage2-evalbranch'
closes #7682
Diffstat (limited to 'src/Module.zig')
-rw-r--r--src/Module.zig27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/Module.zig b/src/Module.zig
index 24ea48043b..6a4575394a 100644
--- a/src/Module.zig
+++ b/src/Module.zig
@@ -23,6 +23,8 @@ const trace = @import("tracy.zig").trace;
const astgen = @import("astgen.zig");
const zir_sema = @import("zir_sema.zig");
+const default_eval_branch_quota = 1000;
+
/// General-purpose allocator. Used for both temporary and long-term storage.
gpa: *Allocator,
comp: *Compilation,
@@ -765,6 +767,8 @@ pub const Scope = struct {
label: ?Label = null,
inlining: ?*Inlining,
is_comptime: bool,
+ /// Shared to sub-blocks.
+ branch_quota: *u32,
pub const InstTable = std.AutoHashMap(*zir.Inst, *Inst);
@@ -792,8 +796,7 @@ pub const Scope = struct {
pub const Shared = struct {
caller: ?*Fn,
- branch_count: u64,
- branch_quota: u64,
+ branch_count: u32,
};
};
@@ -1104,6 +1107,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
var inst_table = Scope.Block.InstTable.init(self.gpa);
defer inst_table.deinit();
+ var branch_quota: u32 = default_eval_branch_quota;
+
var block_scope: Scope.Block = .{
.parent = null,
.inst_table = &inst_table,
@@ -1113,6 +1118,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
.arena = &decl_arena.allocator,
.inlining = null,
.is_comptime = false,
+ .branch_quota = &branch_quota,
};
defer block_scope.instructions.deinit(self.gpa);
@@ -1297,6 +1303,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
var decl_inst_table = Scope.Block.InstTable.init(self.gpa);
defer decl_inst_table.deinit();
+ var branch_quota: u32 = default_eval_branch_quota;
+
var block_scope: Scope.Block = .{
.parent = null,
.inst_table = &decl_inst_table,
@@ -1306,6 +1314,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
.arena = &decl_arena.allocator,
.inlining = null,
.is_comptime = true,
+ .branch_quota = &branch_quota,
};
defer block_scope.instructions.deinit(self.gpa);
@@ -1367,6 +1376,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
var var_inst_table = Scope.Block.InstTable.init(self.gpa);
defer var_inst_table.deinit();
+ var branch_quota_vi: u32 = default_eval_branch_quota;
var inner_block: Scope.Block = .{
.parent = null,
.inst_table = &var_inst_table,
@@ -1376,6 +1386,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
.arena = &gen_scope_arena.allocator,
.inlining = null,
.is_comptime = true,
+ .branch_quota = &branch_quota_vi,
};
defer inner_block.instructions.deinit(self.gpa);
try zir_sema.analyzeBody(self, &inner_block, .{
@@ -1494,6 +1505,8 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
var inst_table = Scope.Block.InstTable.init(self.gpa);
defer inst_table.deinit();
+ var branch_quota: u32 = default_eval_branch_quota;
+
var block_scope: Scope.Block = .{
.parent = null,
.inst_table = &inst_table,
@@ -1503,6 +1516,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
.arena = &analysis_arena.allocator,
.inlining = null,
.is_comptime = true,
+ .branch_quota = &branch_quota,
};
defer block_scope.instructions.deinit(self.gpa);
@@ -1875,6 +1889,8 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
defer decl.typed_value.most_recent.arena.?.* = arena.state;
var inst_table = Scope.Block.InstTable.init(self.gpa);
defer inst_table.deinit();
+ var branch_quota: u32 = default_eval_branch_quota;
+
var inner_block: Scope.Block = .{
.parent = null,
.inst_table = &inst_table,
@@ -1884,6 +1900,7 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
.arena = &arena.allocator,
.inlining = null,
.is_comptime = false,
+ .branch_quota = &branch_quota,
};
defer inner_block.instructions.deinit(self.gpa);
@@ -3466,7 +3483,9 @@ pub fn addSafetyCheck(mod: *Module, parent_block: *Scope.Block, ok: *Inst, panic
.arena = parent_block.arena,
.inlining = parent_block.inlining,
.is_comptime = parent_block.is_comptime,
+ .branch_quota = parent_block.branch_quota,
};
+
defer fail_block.instructions.deinit(mod.gpa);
_ = try mod.safetyPanic(&fail_block, ok.src, panic_id);
@@ -3532,10 +3551,10 @@ pub fn identifierTokenString(mod: *Module, scope: *Scope, token: ast.TokenIndex)
pub fn emitBackwardBranch(mod: *Module, block: *Scope.Block, src: usize) !void {
const shared = block.inlining.?.shared;
shared.branch_count += 1;
- if (shared.branch_count > shared.branch_quota) {
+ if (shared.branch_count > block.branch_quota.*) {
// TODO show the "called from here" stack
return mod.fail(&block.base, src, "evaluation exceeded {d} backwards branches", .{
- shared.branch_quota,
+ block.branch_quota.*,
});
}
}