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:
57475747
57485748/// This is only enabled for debug builds.
57495749fn cmdDumpZir(arena: Allocator, io: Io, args: []const []const u8) !void {
5750 if (args.len == 0)
5751 fatal("zir cache file is required", .{});
5752
57505753 const Zir = std.zig.Zir;
57515754
57525755 const cache_file = args[0];
src/print_zir.zig+23-23
......@@ -725,7 +725,7 @@ const Writer = struct {
725725
726726 fn writeExtNode(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
727727 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));
729729 try self.writeSrcNode(stream, src_node);
730730 }
731731
......@@ -819,25 +819,25 @@ const Writer = struct {
819819 var extra_index = extra.end;
820820 if (inst_data.flags.has_sentinel) {
821821 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]));
823823 extra_index += 1;
824824 }
825825 if (inst_data.flags.has_align) {
826826 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]));
828828 extra_index += 1;
829829 if (inst_data.flags.has_bit_range) {
830830 const bit_start = extra_index + @intFromBool(inst_data.flags.has_addrspace);
831831 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]));
833833 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]));
835835 }
836836 try stream.writeAll(")");
837837 }
838838 if (inst_data.flags.has_addrspace) {
839839 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]));
841841 try stream.writeAll(")");
842842 }
843843 try stream.writeAll(") ");
......@@ -1294,7 +1294,7 @@ const Writer = struct {
12941294
12951295 try self.writeFlag(stream, "volatile, ", small.is_volatile);
12961296 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)));
12981298 } else {
12991299 const asm_source = self.code.nullTerminatedString(extra.data.asm_source);
13001300 try stream.print("\"{f}\"", .{std.zig.fmtString(asm_source)});
......@@ -1675,9 +1675,9 @@ const Writer = struct {
16751675 extra_index += 2;
16761676
16771677 try stream.print("@\"{d}\": ", .{field_idx});
1678 try self.writeInstRef(stream, @fromBackingInt(@intCast(field_ty)));
1678 try self.writeInstRef(stream, @fromBackingInt(field_ty));
16791679 try stream.writeAll(" = ");
1680 try self.writeInstRef(stream, @fromBackingInt(@intCast(field_init)));
1680 try self.writeInstRef(stream, @fromBackingInt(field_init));
16811681 }
16821682
16831683 try stream.writeAll(" }) ");
......@@ -1696,10 +1696,10 @@ const Writer = struct {
16961696 try stream.writeAll("{\n");
16971697 self.indent += 2;
16981698
1699 var extra_index = @as(u32, @intCast(extra.end));
1699 var extra_index: u32 = @intCast(extra.end);
17001700 const extra_index_end = extra_index + extra.data.fields_len;
17011701 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]);
17031703 const name = self.code.nullTerminatedString(name_index);
17041704 try stream.splatByteAll(' ', self.indent);
17051705 try stream.print("{f},\n", .{std.zig.fmtIdP(name)});
......@@ -1923,7 +1923,7 @@ const Writer = struct {
19231923 ret_ty_ref = .void_type;
19241924 },
19251925 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]);
19271927 extra_index += 1;
19281928 },
19291929 else => {
......@@ -1974,7 +1974,7 @@ const Writer = struct {
19741974 cc_body = self.code.bodySlice(extra_index, body_len);
19751975 extra_index += cc_body.len;
19761976 } 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]);
19781978 extra_index += 1;
19791979 }
19801980 if (extra.data.bits.has_ret_ty_body) {
......@@ -1983,7 +1983,7 @@ const Writer = struct {
19831983 ret_ty_body = self.code.bodySlice(extra_index, body_len);
19841984 extra_index += ret_ty_body.len;
19851985 } 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]);
19871987 extra_index += 1;
19881988 }
19891989
......@@ -2019,16 +2019,16 @@ const Writer = struct {
20192019
20202020 fn writeAllocExtended(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
20212021 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
20242024 var extra_index: usize = extra.end;
20252025 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]);
20272027 extra_index += 1;
20282028 break :blk type_inst;
20292029 };
20302030 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]);
20322032 extra_index += 1;
20332033 break :blk align_inst;
20342034 };
......@@ -2269,20 +2269,20 @@ const Writer = struct {
22692269
22702270 fn writeClosureGet(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
22712271 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));
22732273 try self.writeSrcNode(stream, src_node);
22742274 }
22752275
22762276 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);
22782278 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));
22802280 try self.writeSrcNode(stream, src_node);
22812281 }
22822282
22832283 fn writeInplaceArithResultTy(self: *Writer, stream: *std.Io.Writer, extended: Zir.Inst.Extended.InstData) !void {
2284 const op: Zir.Inst.InplaceOp = @fromBackingInt(@intCast(extended.small));
2285 try self.writeInstRef(stream, @fromBackingInt(@intCast(extended.operand)));
2284 const op: Zir.Inst.InplaceOp = @fromBackingInt(extended.small);
2285 try self.writeInstRef(stream, @fromBackingInt(extended.operand));
22862286 try stream.print(", {s}))", .{@tagName(op)});
22872287 }
22882288
......@@ -2292,7 +2292,7 @@ const Writer = struct {
22922292 } else if (ref.toIndex()) |i| {
22932293 return self.writeInstIndex(stream, i);
22942294 } else {
2295 const val: InternPool.Index = @fromBackingInt(@intCast(@backingInt(ref)));
2295 const val: InternPool.Index = @fromBackingInt(@backingInt(ref));
22962296 return stream.print("@{s}", .{@tagName(val)});
22972297 }
22982298 }