diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-03-24 20:45:14 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-03-24 20:45:14 -0700 |
| commit | 01bfd835bb9613d21f09c0c4f5b905b077b3d5f9 (patch) | |
| tree | 89a9fe2ca68d2055abebee9f17b2136f8e5997cf /src/Module.zig | |
| parent | ea42ab34abc0408b66ce2c0212afd4f5705e8d43 (diff) | |
| download | zig-01bfd835bb9613d21f09c0c4f5b905b077b3d5f9.tar.gz zig-01bfd835bb9613d21f09c0c4f5b905b077b3d5f9.zip | |
stage2: clean up break / noreturn astgen
* Module.addBreak and addBreakVoid return zir.Inst.Index not Ref
because Index is the simpler type and we never need a Ref for these.
* astgen: make noreturn stuff return the unreachable_value and avoid
unnecessary calls to rvalue()
* breakExpr: avoid unnecessary access into the tokens array
* breakExpr: fix incorrect `@intCast` (previously this unsafely
casted an Index to a Ref)
Diffstat (limited to 'src/Module.zig')
| -rw-r--r-- | src/Module.zig | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/Module.zig b/src/Module.zig index e3e8fa813b..0258e703cf 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.Ref) = .{}, + labeled_breaks: std.ArrayListUnmanaged(zir.Inst.Index) = .{}, /// 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.Ref) = .{}, + labeled_store_to_block_ptr_list: std.ArrayListUnmanaged(zir.Inst.Index) = .{}, pub const Label = struct { token: ast.TokenIndex, @@ -1226,8 +1226,8 @@ pub const Scope = struct { gz: *GenZir, break_block: zir.Inst.Index, operand: zir.Inst.Ref, - ) !zir.Inst.Ref { - return try gz.add(.{ + ) !zir.Inst.Index { + return gz.addAsIndex(.{ .tag = .@"break", .data = .{ .@"break" = .{ .block_inst = break_block, @@ -1237,15 +1237,14 @@ pub const Scope = struct { } pub fn addBreakVoid( - inner_gz: *GenZir, - block_gz: *GenZir, + gz: *GenZir, break_block: zir.Inst.Index, node_index: ast.Node.Index, - ) !zir.Inst.Ref { - return try inner_gz.add(.{ + ) !zir.Inst.Index { + return gz.addAsIndex(.{ .tag = .break_void_node, .data = .{ .break_void_node = .{ - .src_node = block_gz.zir_code.decl.nodeIndexToRelative(node_index), + .src_node = gz.zir_code.decl.nodeIndexToRelative(node_index), .block_inst = break_block, } }, }); @@ -1339,6 +1338,10 @@ pub const Scope = struct { } pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref { + return gz.zir_code.indexToRef(try gz.addAsIndex(inst)); + } + + pub fn addAsIndex(gz: *GenZir, inst: zir.Inst) !zir.Inst.Index { const gpa = gz.zir_code.gpa; try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1); @@ -1346,7 +1349,7 @@ pub const Scope = struct { const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len); gz.zir_code.instructions.appendAssumeCapacity(inst); gz.instructions.appendAssumeCapacity(new_index); - return gz.zir_code.indexToRef(new_index); + return new_index; } }; |
