authorgravatar for leon@makase.roLeon Lombar <leon@makase.ro> 2026-10-20 20:28:07+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-24 23:16:49+02:00
log3423b14e83f29817cc01bf15382d2253cd5d2cce
tree85c249ce64885394f40b1bd1e29cd2141c92a532
parent723085b9bf47f26ee8a91aa10b3859e2946034e9

print_zir: simplify casts

Not sure how useful this is, but I got Jacob's blessing so yeah, yolo

2 files changed, 26 insertions(+), 23 deletions(-)

src/main.zig+3
...@@ -5747,6 +5747,9 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8, environ_map:...@@ -5747,6 +5747,9 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8, environ_map:
57475747
5748/// This is only enabled for debug builds.5748/// This is only enabled for debug builds.
5749fn cmdDumpZir(arena: Allocator, io: Io, args: []const []const u8) !void {5749fn cmdDumpZir(arena: Allocator, io: Io, args: []const []const u8) !void {
5750 if (args.len == 0)
5751 fatal("zir cache file is required", .{});
5752
5750 const Zir = std.zig.Zir;5753 const Zir = std.zig.Zir;
57515754
5752 const cache_file = args[0];5755 const cache_file = args[0];
src/print_zir.zig+23-23
...@@ -725,7 +725,7 @@ const Writer = struct {...@@ -725,7 +725,7 @@ const Writer = struct {
725725
726 fn writeExtNode(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {726 fn writeExtNode(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
727 try stream.writeAll(")) ");727 try stream.writeAll(")) ");
728 const src_node: Ast.Node.Offset = @fromBackingInt(@intCast(@as(i32, @bitCast(extended.operand))));728 const src_node: Ast.Node.Offset = @fromBackingInt(@bitCast(extended.operand));
729 try self.writeSrcNode(stream, src_node);729 try self.writeSrcNode(stream, src_node);
730 }730 }
731731
...@@ -819,25 +819,25 @@ const Writer = struct {...@@ -819,25 +819,25 @@ const Writer = struct {
819 var extra_index = extra.end;819 var extra_index = extra.end;
820 if (inst_data.flags.has_sentinel) {820 if (inst_data.flags.has_sentinel) {
821 try stream.writeAll(", ");821 try stream.writeAll(", ");
822 try self.writeInstRef(stream, @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index]))));822 try self.writeInstRef(stream, @fromBackingInt(self.code.extra[extra_index]));
823 extra_index += 1;823 extra_index += 1;
824 }824 }
825 if (inst_data.flags.has_align) {825 if (inst_data.flags.has_align) {
826 try stream.writeAll(", align(");826 try stream.writeAll(", align(");
827 try self.writeInstRef(stream, @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index]))));827 try self.writeInstRef(stream, @fromBackingInt(self.code.extra[extra_index]));
828 extra_index += 1;828 extra_index += 1;
829 if (inst_data.flags.has_bit_range) {829 if (inst_data.flags.has_bit_range) {
830 const bit_start = extra_index + @intFromBool(inst_data.flags.has_addrspace);830 const bit_start = extra_index + @intFromBool(inst_data.flags.has_addrspace);
831 try stream.writeAll(":");831 try stream.writeAll(":");
832 try self.writeInstRef(stream, @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[bit_start]))));832 try self.writeInstRef(stream, @fromBackingInt(self.code.extra[bit_start]));
833 try stream.writeAll(":");833 try stream.writeAll(":");
834 try self.writeInstRef(stream, @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[bit_start + 1]))));834 try self.writeInstRef(stream, @fromBackingInt(self.code.extra[bit_start + 1]));
835 }835 }
836 try stream.writeAll(")");836 try stream.writeAll(")");
837 }837 }
838 if (inst_data.flags.has_addrspace) {838 if (inst_data.flags.has_addrspace) {
839 try stream.writeAll(", addrspace(");839 try stream.writeAll(", addrspace(");
840 try self.writeInstRef(stream, @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index]))));840 try self.writeInstRef(stream, @fromBackingInt(self.code.extra[extra_index]));
841 try stream.writeAll(")");841 try stream.writeAll(")");
842 }842 }
843 try stream.writeAll(") ");843 try stream.writeAll(") ");
...@@ -1294,7 +1294,7 @@ const Writer = struct {...@@ -1294,7 +1294,7 @@ const Writer = struct {
12941294
1295 try self.writeFlag(stream, "volatile, ", small.is_volatile);1295 try self.writeFlag(stream, "volatile, ", small.is_volatile);
1296 if (tmpl_is_expr) {1296 if (tmpl_is_expr) {
1297 try self.writeInstRef(stream, @fromBackingInt(@intCast(@backingInt(extra.data.asm_source))));1297 try self.writeInstRef(stream, @fromBackingInt(@backingInt(extra.data.asm_source)));
1298 } else {1298 } else {
1299 const asm_source = self.code.nullTerminatedString(extra.data.asm_source);1299 const asm_source = self.code.nullTerminatedString(extra.data.asm_source);
1300 try stream.print("\"{f}\"", .{std.zig.fmtString(asm_source)});1300 try stream.print("\"{f}\"", .{std.zig.fmtString(asm_source)});
...@@ -1675,9 +1675,9 @@ const Writer = struct {...@@ -1675,9 +1675,9 @@ const Writer = struct {
1675 extra_index += 2;1675 extra_index += 2;
16761676
1677 try stream.print("@\"{d}\": ", .{field_idx});1677 try stream.print("@\"{d}\": ", .{field_idx});
1678 try self.writeInstRef(stream, @fromBackingInt(@intCast(field_ty)));1678 try self.writeInstRef(stream, @fromBackingInt(field_ty));
1679 try stream.writeAll(" = ");1679 try stream.writeAll(" = ");
1680 try self.writeInstRef(stream, @fromBackingInt(@intCast(field_init)));1680 try self.writeInstRef(stream, @fromBackingInt(field_init));
1681 }1681 }
16821682
1683 try stream.writeAll(" }) ");1683 try stream.writeAll(" }) ");
...@@ -1696,10 +1696,10 @@ const Writer = struct {...@@ -1696,10 +1696,10 @@ const Writer = struct {
1696 try stream.writeAll("{\n");1696 try stream.writeAll("{\n");
1697 self.indent += 2;1697 self.indent += 2;
16981698
1699 var extra_index = @as(u32, @intCast(extra.end));1699 var extra_index: u32 = @intCast(extra.end);
1700 const extra_index_end = extra_index + extra.data.fields_len;1700 const extra_index_end = extra_index + extra.data.fields_len;
1701 while (extra_index < extra_index_end) : (extra_index += 1) {1701 while (extra_index < extra_index_end) : (extra_index += 1) {
1702 const name_index: Zir.NullTerminatedString = @fromBackingInt(@intCast(self.code.extra[extra_index]));1702 const name_index: Zir.NullTerminatedString = @fromBackingInt(self.code.extra[extra_index]);
1703 const name = self.code.nullTerminatedString(name_index);1703 const name = self.code.nullTerminatedString(name_index);
1704 try stream.splatByteAll(' ', self.indent);1704 try stream.splatByteAll(' ', self.indent);
1705 try stream.print("{f},\n", .{std.zig.fmtIdP(name)});1705 try stream.print("{f},\n", .{std.zig.fmtIdP(name)});
...@@ -1923,7 +1923,7 @@ const Writer = struct {...@@ -1923,7 +1923,7 @@ const Writer = struct {
1923 ret_ty_ref = .void_type;1923 ret_ty_ref = .void_type;
1924 },1924 },
1925 1 => {1925 1 => {
1926 ret_ty_ref = @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index])));1926 ret_ty_ref = @fromBackingInt(self.code.extra[extra_index]);
1927 extra_index += 1;1927 extra_index += 1;
1928 },1928 },
1929 else => {1929 else => {
...@@ -1974,7 +1974,7 @@ const Writer = struct {...@@ -1974,7 +1974,7 @@ const Writer = struct {
1974 cc_body = self.code.bodySlice(extra_index, body_len);1974 cc_body = self.code.bodySlice(extra_index, body_len);
1975 extra_index += cc_body.len;1975 extra_index += cc_body.len;
1976 } else if (extra.data.bits.has_cc_ref) {1976 } else if (extra.data.bits.has_cc_ref) {
1977 cc_ref = @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index])));1977 cc_ref = @fromBackingInt(self.code.extra[extra_index]);
1978 extra_index += 1;1978 extra_index += 1;
1979 }1979 }
1980 if (extra.data.bits.has_ret_ty_body) {1980 if (extra.data.bits.has_ret_ty_body) {
...@@ -1983,7 +1983,7 @@ const Writer = struct {...@@ -1983,7 +1983,7 @@ const Writer = struct {
1983 ret_ty_body = self.code.bodySlice(extra_index, body_len);1983 ret_ty_body = self.code.bodySlice(extra_index, body_len);
1984 extra_index += ret_ty_body.len;1984 extra_index += ret_ty_body.len;
1985 } else if (extra.data.bits.has_ret_ty_ref) {1985 } else if (extra.data.bits.has_ret_ty_ref) {
1986 ret_ty_ref = @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index])));1986 ret_ty_ref = @fromBackingInt(self.code.extra[extra_index]);
1987 extra_index += 1;1987 extra_index += 1;
1988 }1988 }
19891989
...@@ -2019,16 +2019,16 @@ const Writer = struct {...@@ -2019,16 +2019,16 @@ const Writer = struct {
20192019
2020 fn writeAllocExtended(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {2020 fn writeAllocExtended(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
2021 const extra = self.code.extraData(Zir.Inst.AllocExtended, extended.operand);2021 const extra = self.code.extraData(Zir.Inst.AllocExtended, extended.operand);
2022 const small = @as(Zir.Inst.AllocExtended.Small, @bitCast(extended.small));2022 const small: Zir.Inst.AllocExtended.Small = @bitCast(extended.small);
20232023
2024 var extra_index: usize = extra.end;2024 var extra_index: usize = extra.end;
2025 const type_inst: Zir.Inst.Ref = if (!small.has_type) .none else blk: {2025 const type_inst: Zir.Inst.Ref = if (!small.has_type) .none else blk: {
2026 const type_inst = @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index])));2026 const type_inst: Zir.Inst.Ref = @fromBackingInt(self.code.extra[extra_index]);
2027 extra_index += 1;2027 extra_index += 1;
2028 break :blk type_inst;2028 break :blk type_inst;
2029 };2029 };
2030 const align_inst: Zir.Inst.Ref = if (!small.has_align) .none else blk: {2030 const align_inst: Zir.Inst.Ref = if (!small.has_align) .none else blk: {
2031 const align_inst = @as(Zir.Inst.Ref, @fromBackingInt(@intCast(self.code.extra[extra_index])));2031 const align_inst: Zir.Inst.Ref = @fromBackingInt(self.code.extra[extra_index]);
2032 extra_index += 1;2032 extra_index += 1;
2033 break :blk align_inst;2033 break :blk align_inst;
2034 };2034 };
...@@ -2269,20 +2269,20 @@ const Writer = struct {...@@ -2269,20 +2269,20 @@ const Writer = struct {
22692269
2270 fn writeClosureGet(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {2270 fn writeClosureGet(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
2271 try stream.print("{d})) ", .{extended.small});2271 try stream.print("{d})) ", .{extended.small});
2272 const src_node: Ast.Node.Offset = @fromBackingInt(@intCast(@as(i32, @bitCast(extended.operand))));2272 const src_node: Ast.Node.Offset = @fromBackingInt(@bitCast(extended.operand));
2273 try self.writeSrcNode(stream, src_node);2273 try self.writeSrcNode(stream, src_node);
2274 }2274 }
22752275
2276 fn writeStdLangValue(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {2276 fn writeStdLangValue(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
2277 const val: Zir.Inst.StdLangValue = @fromBackingInt(@intCast(extended.small));2277 const val: Zir.Inst.StdLangValue = @fromBackingInt(extended.small);
2278 try stream.print("{s})) ", .{@tagName(val)});2278 try stream.print("{s})) ", .{@tagName(val)});
2279 const src_node: Ast.Node.Offset = @fromBackingInt(@intCast(@as(i32, @bitCast(extended.operand))));2279 const src_node: Ast.Node.Offset = @fromBackingInt(@bitCast(extended.operand));
2280 try self.writeSrcNode(stream, src_node);2280 try self.writeSrcNode(stream, src_node);
2281 }2281 }
22822282
2283 fn writeInplaceArithResultTy(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {2283 fn writeInplaceArithResultTy(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
2284 const op: Zir.Inst.InplaceOp = @fromBackingInt(@intCast(extended.small));2284 const op: Zir.Inst.InplaceOp = @fromBackingInt(extended.small);
2285 try self.writeInstRef(stream, @fromBackingInt(@intCast(extended.operand)));2285 try self.writeInstRef(stream, @fromBackingInt(extended.operand));
2286 try stream.print(", {s}))", .{@tagName(op)});2286 try stream.print(", {s}))", .{@tagName(op)});
2287 }2287 }
22882288
...@@ -2292,7 +2292,7 @@ const Writer = struct {...@@ -2292,7 +2292,7 @@ const Writer = struct {
2292 } else if (ref.toIndex()) |i| {2292 } else if (ref.toIndex()) |i| {
2293 return self.writeInstIndex(stream, i);2293 return self.writeInstIndex(stream, i);
2294 } else {2294 } else {
2295 const val: InternPool.Index = @fromBackingInt(@intCast(@backingInt(ref)));2295 const val: InternPool.Index = @fromBackingInt(@backingInt(ref));
2296 return stream.print("@{s}", .{@tagName(val)});2296 return stream.print("@{s}", .{@tagName(val)});
2297 }2297 }
2298 }2298 }