From 522707622e95ef17b94c7a3d78ca81cadde5274d Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Wed, 24 Mar 2021 15:26:09 +0100 Subject: astgen: implement breaking from a block --- src/Module.zig | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src/Module.zig') diff --git a/src/Module.zig b/src/Module.zig index a97ba364ab..e3e8fa813b 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -935,11 +935,11 @@ pub const Scope = struct { break_count: usize = 0, /// Tracks `break :foo bar` instructions so they can possibly be elided later if /// the labeled block ends up not needing a result location pointer. - labeled_breaks: std.ArrayListUnmanaged(zir.Inst.Index) = .{}, + labeled_breaks: std.ArrayListUnmanaged(zir.Inst.Ref) = .{}, /// Tracks `store_to_block_ptr` instructions that correspond to break instructions /// so they can possibly be elided later if the labeled block ends up not needing /// a result location pointer. - labeled_store_to_block_ptr_list: std.ArrayListUnmanaged(zir.Inst.Index) = .{}, + labeled_store_to_block_ptr_list: std.ArrayListUnmanaged(zir.Inst.Ref) = .{}, pub const Label = struct { token: ast.TokenIndex, @@ -1222,6 +1222,35 @@ pub const Scope = struct { }); } + pub fn addBreak( + gz: *GenZir, + break_block: zir.Inst.Index, + operand: zir.Inst.Ref, + ) !zir.Inst.Ref { + return try gz.add(.{ + .tag = .@"break", + .data = .{ .@"break" = .{ + .block_inst = break_block, + .operand = operand, + } }, + }); + } + + pub fn addBreakVoid( + inner_gz: *GenZir, + block_gz: *GenZir, + break_block: zir.Inst.Index, + node_index: ast.Node.Index, + ) !zir.Inst.Ref { + return try inner_gz.add(.{ + .tag = .break_void_node, + .data = .{ .break_void_node = .{ + .src_node = block_gz.zir_code.decl.nodeIndexToRelative(node_index), + .block_inst = break_block, + } }, + }); + } + pub fn addBin( gz: *GenZir, tag: zir.Inst.Tag, -- cgit v1.2.3