authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-05 16:07:27-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-05 16:09:59-04:00
logdf4849c4f5aafc7eb80fd7b2299b49162fa2aa79
tree864575631cbefdbaffb8228e6085a224684d2eb4
parentda878dc0776b56c1e2c251857a40415a35e9535a

AstGen: cleanup previous fix

Allocating an extended tag is much cleaner and easier to reason about than reusing an existing tag. The previous `.data = undefined` was a clear indication that we don't have any data to store, and so might as well store an extended tag in that space almost for free.

4 files changed, 15 insertions(+), 5 deletions(-)

src/AstGen.zig+8-3
...@@ -2906,9 +2906,14 @@ fn deferStmt(...@@ -2906,9 +2906,14 @@ fn deferStmt(
2906 try gz.addDbgBlockBegin();2906 try gz.addDbgBlockBegin();
2907 const ident_name = try gz.astgen.identAsString(payload_token);2907 const ident_name = try gz.astgen.identAsString(payload_token);
2908 remapped_err_code = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);2908 remapped_err_code = @intCast(Zir.Inst.Index, gz.astgen.instructions.len);
2909 // Use a placeholder tag of .as to allow querying things that depend on the tag,2909 try gz.astgen.instructions.append(gz.astgen.gpa, .{
2910 // but undefined data to prevent querying of data.bin.2910 .tag = .extended,
2911 try gz.astgen.instructions.append(gz.astgen.gpa, .{ .tag = .as, .data = undefined });2911 .data = .{ .extended = .{
2912 .opcode = .errdefer_err_code,
2913 .small = undefined,
2914 .operand = undefined,
2915 } },
2916 });
2912 const remapped_err_code_ref = Zir.indexToRef(remapped_err_code);2917 const remapped_err_code_ref = Zir.indexToRef(remapped_err_code);
2913 local_val_scope = .{2918 local_val_scope = .{
2914 .parent = &defer_gen.base,2919 .parent = &defer_gen.base,
src/Sema.zig+1
...@@ -1198,6 +1198,7 @@ fn analyzeBodyInner(...@@ -1198,6 +1198,7 @@ fn analyzeBodyInner(
1198 i += 1;1198 i += 1;
1199 continue;1199 continue;
1200 },1200 },
1201 .errdefer_err_code => unreachable, // never appears in a body
1201 };1202 };
1202 },1203 },
12031204
src/Zir.zig+5-2
...@@ -1979,7 +1979,7 @@ pub const Inst = struct {...@@ -1979,7 +1979,7 @@ pub const Inst = struct {
1979 /// `operand` is `src_node: i32`.1979 /// `operand` is `src_node: i32`.
1980 breakpoint,1980 breakpoint,
1981 /// Implements the `@select` builtin.1981 /// Implements the `@select` builtin.
1982 /// operand` is payload index to `Select`.1982 /// `operand` is payload index to `Select`.
1983 select,1983 select,
1984 /// Implement builtin `@errToInt`.1984 /// Implement builtin `@errToInt`.
1985 /// `operand` is payload index to `UnNode`.1985 /// `operand` is payload index to `UnNode`.
...@@ -1999,7 +1999,7 @@ pub const Inst = struct {...@@ -1999,7 +1999,7 @@ pub const Inst = struct {
1999 /// `operand` is payload index to `Cmpxchg`.1999 /// `operand` is payload index to `Cmpxchg`.
2000 cmpxchg,2000 cmpxchg,
2001 /// Implement the builtin `@addrSpaceCast`2001 /// Implement the builtin `@addrSpaceCast`
2002 /// `Operand` is payload index to `BinNode`. `lhs` is dest type, `rhs` is operand.2002 /// `operand` is payload index to `BinNode`. `lhs` is dest type, `rhs` is operand.
2003 addrspace_cast,2003 addrspace_cast,
2004 /// Implement builtin `@cVaArg`.2004 /// Implement builtin `@cVaArg`.
2005 /// `operand` is payload index to `BinNode`.2005 /// `operand` is payload index to `BinNode`.
...@@ -2031,6 +2031,9 @@ pub const Inst = struct {...@@ -2031,6 +2031,9 @@ pub const Inst = struct {
2031 /// Implements the `@inComptime` builtin.2031 /// Implements the `@inComptime` builtin.
2032 /// `operand` is `src_node: i32`.2032 /// `operand` is `src_node: i32`.
2033 in_comptime,2033 in_comptime,
2034 /// Used as a placeholder for the capture of an `errdefer`.
2035 /// This is replaced by Sema with the captured value.
2036 errdefer_err_code,
20342037
2035 pub const InstData = struct {2038 pub const InstData = struct {
2036 opcode: Extended,2039 opcode: Extended,
src/print_zir.zig+1
...@@ -467,6 +467,7 @@ const Writer = struct {...@@ -467,6 +467,7 @@ const Writer = struct {
467 .breakpoint,467 .breakpoint,
468 .c_va_start,468 .c_va_start,
469 .in_comptime,469 .in_comptime,
470 .errdefer_err_code,
470 => try self.writeExtNode(stream, extended),471 => try self.writeExtNode(stream, extended),
471472
472 .builtin_src => {473 .builtin_src => {