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(
29062906 try gz.addDbgBlockBegin();
29072907 const ident_name = try gz.astgen.identAsString(payload_token);
29082908 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,
2910 // but undefined data to prevent querying of data.bin.
2911 try gz.astgen.instructions.append(gz.astgen.gpa, .{ .tag = .as, .data = undefined });
2909 try gz.astgen.instructions.append(gz.astgen.gpa, .{
2910 .tag = .extended,
2911 .data = .{ .extended = .{
2912 .opcode = .errdefer_err_code,
2913 .small = undefined,
2914 .operand = undefined,
2915 } },
2916 });
29122917 const remapped_err_code_ref = Zir.indexToRef(remapped_err_code);
29132918 local_val_scope = .{
29142919 .parent = &defer_gen.base,
src/Sema.zig+1
......@@ -1198,6 +1198,7 @@ fn analyzeBodyInner(
11981198 i += 1;
11991199 continue;
12001200 },
1201 .errdefer_err_code => unreachable, // never appears in a body
12011202 };
12021203 },
12031204
src/Zir.zig+5-2
......@@ -1979,7 +1979,7 @@ pub const Inst = struct {
19791979 /// `operand` is `src_node: i32`.
19801980 breakpoint,
19811981 /// Implements the `@select` builtin.
1982 /// operand` is payload index to `Select`.
1982 /// `operand` is payload index to `Select`.
19831983 select,
19841984 /// Implement builtin `@errToInt`.
19851985 /// `operand` is payload index to `UnNode`.
......@@ -1999,7 +1999,7 @@ pub const Inst = struct {
19991999 /// `operand` is payload index to `Cmpxchg`.
20002000 cmpxchg,
20012001 /// 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.
20032003 addrspace_cast,
20042004 /// Implement builtin `@cVaArg`.
20052005 /// `operand` is payload index to `BinNode`.
......@@ -2031,6 +2031,9 @@ pub const Inst = struct {
20312031 /// Implements the `@inComptime` builtin.
20322032 /// `operand` is `src_node: i32`.
20332033 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
20352038 pub const InstData = struct {
20362039 opcode: Extended,
src/print_zir.zig+1
......@@ -467,6 +467,7 @@ const Writer = struct {
467467 .breakpoint,
468468 .c_va_start,
469469 .in_comptime,
470 .errdefer_err_code,
470471 => try self.writeExtNode(stream, extended),
471472
472473 .builtin_src => {