authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-02 13:24:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-07 22:43:52-07:00
log3afc6fbac63b31fd250b6cbc4451758e50f85b24
treecafa66d30c871a162cd061af2afb003c1d6263d3
parentcce32bd1d505ee5c5ea7878c9e9e57b6b63856f2

std.zig.llvm.Builder: update format API


5 files changed, 340 insertions(+), 398 deletions(-)

lib/std/zig/llvm/Builder.zig+234-203
......@@ -110,15 +110,28 @@ pub const String = enum(u32) {
110110 const quote_behavior = data.quote_behavior orelse return w.writeAll(string_slice);
111111 return printEscapedString(string_slice, quote_behavior, w);
112112 }
113 pub fn fmt(
114 self: String,
115 builder: *const Builder,
116 quote_behavior: ?QuoteBehavior,
117 ) std.fmt.Formatter(FormatData, format) {
113
114 pub fn fmt(self: String, builder: *const Builder) std.fmt.Formatter(FormatData, format) {
118115 return .{ .data = .{
119116 .string = self,
120117 .builder = builder,
121 .quote_behavior = quote_behavior,
118 .quote_behavior = .quote_unless_valid_identifier,
119 } };
120 }
121
122 pub fn fmtQ(self: String, builder: *const Builder) std.fmt.Formatter(FormatData, format) {
123 return .{ .data = .{
124 .string = self,
125 .builder = builder,
126 .quote_behavior = .always_quote,
127 } };
128 }
129
130 pub fn fmtRaw(self: String, builder: *const Builder) std.fmt.Formatter(FormatData, format) {
131 return .{ .data = .{
132 .string = self,
133 .builder = builder,
134 .quote_behavior = null,
122135 } };
123136 }
124137
......@@ -684,8 +697,8 @@ pub const Type = enum(u32) {
684697 .function, .vararg_function => |kind| {
685698 var extra = data.builder.typeExtraDataTrail(Type.Function, item.data);
686699 const params = extra.trail.next(extra.data.params_len, Type, data.builder);
687 try w.print("f_{fm}", .{extra.data.ret.fmt(data.builder)});
688 for (params) |param| try w.print("{fm}", .{param.fmt(data.builder)});
700 try w.print("f_{f}", .{extra.data.ret.fmt(data.builder, .m)});
701 for (params) |param| try w.print("{f}", .{param.fmt(data.builder, .m)});
689702 switch (kind) {
690703 .function => {},
691704 .vararg_function => try w.writeAll("vararg"),
......@@ -700,20 +713,20 @@ pub const Type = enum(u32) {
700713 const types = extra.trail.next(extra.data.types_len, Type, data.builder);
701714 const ints = extra.trail.next(extra.data.ints_len, u32, data.builder);
702715 try w.print("t{s}", .{extra.data.name.slice(data.builder).?});
703 for (types) |ty| try w.print("_{fm}", .{ty.fmt(data.builder)});
716 for (types) |ty| try w.print("_{f}", .{ty.fmt(data.builder, .m)});
704717 for (ints) |int| try w.print("_{d}", .{int});
705718 try w.writeByte('t');
706719 },
707720 .vector, .scalable_vector => |kind| {
708721 const extra = data.builder.typeExtraData(Type.Vector, item.data);
709 try w.print("{s}v{d}{fm}", .{
722 try w.print("{s}v{d}{f}", .{
710723 switch (kind) {
711724 .vector => "",
712725 .scalable_vector => "nx",
713726 else => unreachable,
714727 },
715728 extra.len,
716 extra.child.fmt(data.builder),
729 extra.child.fmt(data.builder, .m),
717730 });
718731 },
719732 inline .small_array, .array => |kind| {
......@@ -722,13 +735,13 @@ pub const Type = enum(u32) {
722735 .array => Type.Array,
723736 else => unreachable,
724737 }, item.data);
725 try w.print("a{d}{fm}", .{ extra.length(), extra.child.fmt(data.builder) });
738 try w.print("a{d}{f}", .{ extra.length(), extra.child.fmt(data.builder, .m) });
726739 },
727740 .structure, .packed_structure => {
728741 var extra = data.builder.typeExtraDataTrail(Type.Structure, item.data);
729742 const fields = extra.trail.next(extra.data.fields_len, Type, data.builder);
730743 try w.writeAll("sl_");
731 for (fields) |field| try w.print("{fm}", .{field.fmt(data.builder)});
744 for (fields) |field| try w.print("{f}", .{field.fmt(data.builder, .m)});
732745 try w.writeByte('s');
733746 },
734747 .named_structure => {
......@@ -747,12 +760,12 @@ pub const Type = enum(u32) {
747760 var extra = data.builder.typeExtraDataTrail(Type.Function, item.data);
748761 const params = extra.trail.next(extra.data.params_len, Type, data.builder);
749762 if (data.mode != .gt)
750 try w.print("{f%} ", .{extra.data.ret.fmt(data.builder)});
763 try w.print("{f} ", .{extra.data.ret.fmt(data.builder, .percent)});
751764 if (data.mode != .lt) {
752765 try w.writeByte('(');
753766 for (params, 0..) |param, index| {
754767 if (index > 0) try w.writeAll(", ");
755 try w.print("{f%}", .{param.fmt(data.builder)});
768 try w.print("{f}", .{param.fmt(data.builder, .percent)});
756769 }
757770 switch (kind) {
758771 .function => {},
......@@ -772,22 +785,22 @@ pub const Type = enum(u32) {
772785 const types = extra.trail.next(extra.data.types_len, Type, data.builder);
773786 const ints = extra.trail.next(extra.data.ints_len, u32, data.builder);
774787 try w.print(
775 \\target({f"}
776 , .{extra.data.name.fmt(data.builder)});
777 for (types) |ty| try w.print(", {f%}", .{ty.fmt(data.builder)});
788 \\target({f}
789 , .{extra.data.name.fmtQ(data.builder)});
790 for (types) |ty| try w.print(", {f}", .{ty.fmt(data.builder, .percent)});
778791 for (ints) |int| try w.print(", {d}", .{int});
779792 try w.writeByte(')');
780793 },
781794 .vector, .scalable_vector => |kind| {
782795 const extra = data.builder.typeExtraData(Type.Vector, item.data);
783 try w.print("<{s}{d} x {f%}>", .{
796 try w.print("<{s}{d} x {f}>", .{
784797 switch (kind) {
785798 .vector => "",
786799 .scalable_vector => "vscale x ",
787800 else => unreachable,
788801 },
789802 extra.len,
790 extra.child.fmt(data.builder),
803 extra.child.fmt(data.builder, .percent),
791804 });
792805 },
793806 inline .small_array, .array => |kind| {
......@@ -796,7 +809,7 @@ pub const Type = enum(u32) {
796809 .array => Type.Array,
797810 else => unreachable,
798811 }, item.data);
799 try w.print("[{d} x {f%}]", .{ extra.length(), extra.child.fmt(data.builder) });
812 try w.print("[{d} x {f}]", .{ extra.length(), extra.child.fmt(data.builder, .percent) });
800813 },
801814 .structure, .packed_structure => |kind| {
802815 var extra = data.builder.typeExtraDataTrail(Type.Structure, item.data);
......@@ -809,7 +822,7 @@ pub const Type = enum(u32) {
809822 try w.writeAll("{ ");
810823 for (fields, 0..) |field, index| {
811824 if (index > 0) try w.writeAll(", ");
812 try w.print("{f%}", .{field.fmt(data.builder)});
825 try w.print("{f}", .{field.fmt(data.builder, .percent)});
813826 }
814827 try w.writeAll(" }");
815828 switch (kind) {
......@@ -1225,7 +1238,7 @@ pub const Attribute = union(Kind) {
12251238 .inalloca,
12261239 .sret,
12271240 .elementtype,
1228 => |ty| try w.print(" {s}({f%})", .{ @tagName(attribute), ty.fmt(data.builder) }),
1241 => |ty| try w.print(" {s}({f})", .{ @tagName(attribute), ty.fmt(data.builder, .percent) }),
12291242 .@"align" => |alignment| try w.print("{f }", .{alignment}),
12301243 .dereferenceable,
12311244 .dereferenceable_or_null,
......@@ -1248,10 +1261,14 @@ pub const Attribute = union(Kind) {
12481261 }
12491262 try w.writeByte(')');
12501263 },
1251 .alignstack => |alignment| try w.print(
1252 if (data.mode == .pound) " {s}={d}" else " {s}({d})",
1253 .{ @tagName(attribute), alignment.toByteUnits() orelse return },
1254 ),
1264 .alignstack => |alignment| {
1265 try w.print(" {s}", .{attribute});
1266 const alignment_bytes = alignment.toByteUnits() orelse return;
1267 switch (data.mode) {
1268 .pound => try w.print("({d})", .{alignment_bytes}),
1269 else => try w.print("={d}", .{alignment_bytes}),
1270 }
1271 },
12551272 .allockind => |allockind| {
12561273 try w.print(" {s}(\"", .{@tagName(attribute)});
12571274 var any = false;
......@@ -1297,9 +1314,9 @@ pub const Attribute = union(Kind) {
12971314 vscale_range.max.toByteUnits() orelse 0,
12981315 }),
12991316 .string => |string_attr| if (data.mode == .quote) {
1300 try w.print(" {f\"}", .{string_attr.kind.fmt(data.builder)});
1317 try w.print(" {f}", .{string_attr.kind.fmtQ(data.builder)});
13011318 if (string_attr.value != .empty)
1302 try w.print("={f\"}", .{string_attr.value.fmt(data.builder)});
1319 try w.print("={f}", .{string_attr.value.fmtQ(data.builder)});
13031320 },
13041321 .none => unreachable,
13051322 }
......@@ -1583,6 +1600,7 @@ pub const Attributes = enum(u32) {
15831600 for (data.attributes.slice(data.builder)) |attribute_index| try Attribute.Index.format(.{
15841601 .attribute_index = attribute_index,
15851602 .builder = data.builder,
1603 .mode = .default,
15861604 }, w);
15871605 }
15881606 pub fn fmt(self: Attributes, builder: *const Builder) std.fmt.Formatter(FormatData, format) {
......@@ -2315,7 +2333,7 @@ pub const Global = struct {
23152333 };
23162334 fn format(data: FormatData, w: *Writer) Writer.Error!void {
23172335 try w.print("@{f}", .{
2318 data.global.unwrap(data.builder).name(data.builder).fmt(data.builder),
2336 data.global.unwrap(data.builder).name(data.builder).fmt(data.builder, null),
23192337 });
23202338 }
23212339 pub fn fmt(self: Index, builder: *const Builder) std.fmt.Formatter(FormatData, format) {
......@@ -4758,12 +4776,7 @@ pub const Function = struct {
47584776 instruction: Instruction.Index,
47594777 function: Function.Index,
47604778 builder: *Builder,
4761 flags: Flags,
4762 const Flags = struct {
4763 comma: bool = false,
4764 space: bool = false,
4765 percent: bool = false,
4766 };
4779 flags: FormatFlags,
47674780 };
47684781 fn format(data: FormatData, w: *Writer) Writer.Error!void {
47694782 if (data.flags.comma) {
......@@ -4775,8 +4788,8 @@ pub const Function = struct {
47754788 try w.writeByte(' ');
47764789 }
47774790 if (data.flags.percent) try w.print(
4778 "{f%} ",
4779 .{data.instruction.typeOf(data.function, data.builder).fmt(data.builder)},
4791 "{f} ",
4792 .{data.instruction.typeOf(data.function, data.builder).fmt(data.builder, .percent)},
47804793 );
47814794 assert(data.instruction != .none);
47824795 try w.print("%{f}", .{
......@@ -4787,7 +4800,7 @@ pub const Function = struct {
47874800 self: Instruction.Index,
47884801 function: Function.Index,
47894802 builder: *Builder,
4790 flags: FormatData.Flags,
4803 flags: FormatFlags,
47914804 ) std.fmt.Formatter(FormatData, format) {
47924805 return .{ .data = .{
47934806 .instruction = self,
......@@ -6291,10 +6304,10 @@ pub const WipFunction = struct {
62916304
62926305 while (true) {
62936306 gop.value_ptr.* = @enumFromInt(@intFromEnum(gop.value_ptr.*) + 1);
6294 const unique_name = try wip_name.builder.fmt("{fr}{s}{fr}", .{
6295 name.fmt(wip_name.builder),
6307 const unique_name = try wip_name.builder.fmt("{f}{s}{f}", .{
6308 name.fmtRaw(wip_name.builder),
62966309 sep,
6297 gop.value_ptr.fmt(wip_name.builder),
6310 gop.value_ptr.fmtRaw(wip_name.builder),
62986311 });
62996312 const unique_gop = try wip_name.next_unique_name.getOrPut(unique_name);
63006313 if (!unique_gop.found_existing) {
......@@ -7401,12 +7414,7 @@ pub const Constant = enum(u32) {
74017414 const FormatData = struct {
74027415 constant: Constant,
74037416 builder: *Builder,
7404 flags: Flags,
7405 const Flags = struct {
7406 comma: bool = false,
7407 space: bool = false,
7408 percent: bool = false,
7409 };
7417 flags: FormatFlags,
74107418 };
74117419 fn format(data: FormatData, w: *Writer) Writer.Error!void {
74127420 if (data.flags.comma) {
......@@ -7418,7 +7426,7 @@ pub const Constant = enum(u32) {
74187426 try w.writeByte(' ');
74197427 }
74207428 if (data.flags.percent)
7421 try w.print("{f%} ", .{data.constant.typeOf(data.builder).fmt(data.builder)});
7429 try w.print("{f} ", .{data.constant.typeOf(data.builder).fmt(data.builder, .percent)});
74227430 assert(data.constant != .no_init);
74237431 if (std.enums.tagName(Constant, data.constant)) |name| return w.writeAll(name);
74247432 switch (data.constant.unwrap()) {
......@@ -7457,7 +7465,7 @@ pub const Constant = enum(u32) {
74577465 var stack align(@alignOf(ExpectedContents)) =
74587466 std.heap.stackFallback(@sizeOf(ExpectedContents), data.builder.gpa);
74597467 const allocator = stack.get();
7460 const str = try bigint.toStringAlloc(allocator, 10, undefined);
7468 const str = bigint.toStringAlloc(allocator, 10, undefined) catch return error.WriteFailed;
74617469 defer allocator.free(str);
74627470 try w.writeAll(str);
74637471 },
......@@ -7563,7 +7571,7 @@ pub const Constant = enum(u32) {
75637571 });
75647572 for (vals, 0..) |val, index| {
75657573 if (index > 0) try w.writeAll(", ");
7566 try w.print("{f%}", .{val.fmt(data.builder)});
7574 try w.print("{f}", .{val.fmt(data.builder, .{ .percent = true })});
75677575 }
75687576 try w.writeAll(switch (tag) {
75697577 .structure => " }",
......@@ -7579,12 +7587,12 @@ pub const Constant = enum(u32) {
75797587 try w.writeByte('<');
75807588 for (0..len) |index| {
75817589 if (index > 0) try w.writeAll(", ");
7582 try w.print("{f%}", .{extra.value.fmt(data.builder)});
7590 try w.print("{f}", .{extra.value.fmt(data.builder, .{ .percent = true })});
75837591 }
75847592 try w.writeByte('>');
75857593 },
7586 .string => try w.print("c{f\"}", .{
7587 @as(String, @enumFromInt(item.data)).fmt(data.builder),
7594 .string => try w.print("c{f}", .{
7595 @as(String, @enumFromInt(item.data)).fmtQ(data.builder),
75887596 }),
75897597 .blockaddress => |tag| {
75907598 const extra = data.builder.constantExtraData(BlockAddress, item.data);
......@@ -7592,7 +7600,7 @@ pub const Constant = enum(u32) {
75927600 try w.print("{s}({f}, {f})", .{
75937601 @tagName(tag),
75947602 function.global.fmt(data.builder),
7595 extra.block.toInst(function).fmt(extra.function, data.builder),
7603 extra.block.toInst(function).fmt(extra.function, data.builder, .{}),
75967604 });
75977605 },
75987606 .dso_local_equivalent,
......@@ -7611,10 +7619,10 @@ pub const Constant = enum(u32) {
76117619 .addrspacecast,
76127620 => |tag| {
76137621 const extra = data.builder.constantExtraData(Cast, item.data);
7614 try w.print("{s} ({f%} to {f%})", .{
7622 try w.print("{s} ({f} to {f})", .{
76157623 @tagName(tag),
7616 extra.val.fmt(data.builder),
7617 extra.type.fmt(data.builder),
7624 extra.val.fmt(data.builder, .{ .percent = true }),
7625 extra.type.fmt(data.builder, .percent),
76187626 });
76197627 },
76207628 .getelementptr,
......@@ -7623,12 +7631,12 @@ pub const Constant = enum(u32) {
76237631 var extra = data.builder.constantExtraDataTrail(GetElementPtr, item.data);
76247632 const indices =
76257633 extra.trail.next(extra.data.info.indices_len, Constant, data.builder);
7626 try w.print("{s} ({f%}, {f%}", .{
7634 try w.print("{s} ({f}, {f}", .{
76277635 @tagName(tag),
7628 extra.data.type.fmt(data.builder),
7629 extra.data.base.fmt(data.builder),
7636 extra.data.type.fmt(data.builder, .percent),
7637 extra.data.base.fmt(data.builder, .{ .percent = true }),
76307638 });
7631 for (indices) |index| try w.print(", {f%}", .{index.fmt(data.builder)});
7639 for (indices) |index| try w.print(", {f}", .{index.fmt(data.builder, .{ .percent = true })});
76327640 try w.writeByte(')');
76337641 },
76347642 .add,
......@@ -7641,10 +7649,10 @@ pub const Constant = enum(u32) {
76417649 .xor,
76427650 => |tag| {
76437651 const extra = data.builder.constantExtraData(Binary, item.data);
7644 try w.print("{s} ({f%}, {f%})", .{
7652 try w.print("{s} ({f}, {f})", .{
76457653 @tagName(tag),
7646 extra.lhs.fmt(data.builder),
7647 extra.rhs.fmt(data.builder),
7654 extra.lhs.fmt(data.builder, .{ .percent = true }),
7655 extra.rhs.fmt(data.builder, .{ .percent = true }),
76487656 });
76497657 },
76507658 .@"asm",
......@@ -7665,10 +7673,10 @@ pub const Constant = enum(u32) {
76657673 .@"asm sideeffect alignstack inteldialect unwind",
76667674 => |tag| {
76677675 const extra = data.builder.constantExtraData(Assembly, item.data);
7668 try w.print("{s} {f\"}, {f\"}", .{
7676 try w.print("{s} {f}, {f}", .{
76697677 @tagName(tag),
7670 extra.assembly.fmt(data.builder),
7671 extra.constraints.fmt(data.builder),
7678 extra.assembly.fmtQ(data.builder),
7679 extra.constraints.fmtQ(data.builder),
76727680 });
76737681 },
76747682 }
......@@ -7676,7 +7684,7 @@ pub const Constant = enum(u32) {
76767684 .global => |global| try w.print("{f}", .{global.fmt(data.builder)}),
76777685 }
76787686 }
7679 pub fn fmt(self: Constant, builder: *Builder, flags: FormatData.Flags) std.fmt.Formatter(FormatData, format) {
7687 pub fn fmt(self: Constant, builder: *Builder, flags: FormatFlags) std.fmt.Formatter(FormatData, format) {
76807688 return .{ .data = .{
76817689 .constant = self,
76827690 .builder = builder,
......@@ -7736,6 +7744,7 @@ pub const Value = enum(u32) {
77367744 value: Value,
77377745 function: Function.Index,
77387746 builder: *Builder,
7747 flags: FormatFlags,
77397748 };
77407749 fn format(data: FormatData, w: *Writer) Writer.Error!void {
77417750 switch (data.value.unwrap()) {
......@@ -7743,16 +7752,18 @@ pub const Value = enum(u32) {
77437752 .instruction = instruction,
77447753 .function = data.function,
77457754 .builder = data.builder,
7755 .flags = data.flags,
77467756 }, w),
77477757 .constant => |constant| try Constant.format(.{
77487758 .constant = constant,
77497759 .builder = data.builder,
7760 .flags = data.flags,
77507761 }, w),
77517762 .metadata => unreachable,
77527763 }
77537764 }
7754 pub fn fmt(self: Value, function: Function.Index, builder: *Builder) std.fmt.Formatter(FormatData, format) {
7755 return .{ .data = .{ .value = self, .function = function, .builder = builder } };
7765 pub fn fmt(self: Value, function: Function.Index, builder: *Builder, flags: FormatFlags) std.fmt.Formatter(FormatData, format) {
7766 return .{ .data = .{ .value = self, .function = function, .builder = builder, .flags = flags } };
77567767 }
77577768};
77587769
......@@ -8196,9 +8207,7 @@ pub const Metadata = enum(u32) {
81968207 formatter: *Formatter,
81978208 prefix: []const u8 = "",
81988209 node: Node,
8199 specialized: ?TODO,
8200
8201 const TODO = opaque {};
8210 specialized: ?FormatFlags,
82028211
82038212 const Node = union(enum) {
82048213 none,
......@@ -8228,7 +8237,6 @@ pub const Metadata = enum(u32) {
82288237 if (data.node == .none) return;
82298238
82308239 const is_specialized = data.specialized != null;
8231 const recurse_fmt_str = data.specialized orelse {};
82328240
82338241 if (data.formatter.need_comma) try w.writeAll(", ");
82348242 defer data.formatter.need_comma = true;
......@@ -8251,13 +8259,15 @@ pub const Metadata = enum(u32) {
82518259 for (elements) |element| try format(.{
82528260 .formatter = data.formatter,
82538261 .node = .{ .u64 = element },
8254 }, w, "%");
8262 .specialized = .{ .percent = true },
8263 }, w);
82558264 try w.writeByte(')');
82568265 },
82578266 .constant => try Constant.format(.{
82588267 .constant = @enumFromInt(item.data),
82598268 .builder = builder,
8260 }, w, recurse_fmt_str),
8269 .flags = data.specialized orelse .{},
8270 }, w),
82618271 else => unreachable,
82628272 }
82638273 },
......@@ -8266,28 +8276,33 @@ pub const Metadata = enum(u32) {
82668276 .value = node.value,
82678277 .function = node.function,
82688278 .builder = builder,
8269 }, w, switch (tag) {
8270 .local_value => recurse_fmt_str,
8271 .local_metadata => "%",
8272 else => unreachable,
8273 }),
8279 .flags = switch (tag) {
8280 .local_value => data.specialized orelse .{},
8281 .local_metadata => .{ .percent = true },
8282 else => unreachable,
8283 },
8284 }, w),
82748285 inline .local_inline, .local_index => |node, tag| {
8275 if (comptime std.mem.eql(u8, recurse_fmt_str, "%"))
8276 try w.print("{f%} ", .{Type.metadata.fmt(builder)});
8286 if (data.specialized) |flags| {
8287 if (flags.onlyPercent()) {
8288 try w.print("{f} ", .{Type.metadata.fmt(builder, .percent)});
8289 }
8290 }
82778291 try format(.{
82788292 .formatter = data.formatter,
82798293 .node = @unionInit(FormatData.Node, @tagName(tag)["local_".len..], node),
8280 }, w, "%");
8294 .specialized = .{ .percent = true },
8295 }, w);
82818296 },
8282 .string => |node| try w.print((if (is_specialized) "" else "!") ++ "{f}", .{
8283 node.fmt(builder),
8297 .string => |node| try w.print("{s}{f}", .{
8298 @as([]const u8, if (is_specialized) "" else "!"), node.fmt(builder),
82848299 }),
82858300 inline .bool, .u32, .u64 => |node| try w.print("{}", .{node}),
82868301 inline .di_flags, .sp_flags => |node| try w.print("{f}", .{node}),
82878302 .raw => |node| try w.writeAll(node),
82888303 }
82898304 }
8290 inline fn fmt(formatter: *Formatter, prefix: []const u8, node: anytype) switch (@TypeOf(node)) {
8305 inline fn fmt(formatter: *Formatter, prefix: []const u8, node: anytype, special: ?FormatFlags) switch (@TypeOf(node)) {
82918306 Metadata => Allocator.Error,
82928307 else => error{},
82938308 }!std.fmt.Formatter(FormatData, format) {
......@@ -8327,6 +8342,7 @@ pub const Metadata = enum(u32) {
83278342 .optional, .null => .none,
83288343 else => unreachable,
83298344 },
8345 .specialized = special,
83308346 } };
83318347 }
83328348 inline fn fmtLocal(
......@@ -8359,6 +8375,7 @@ pub const Metadata = enum(u32) {
83598375 };
83608376 },
83618377 },
8378 .specialized = null,
83628379 } };
83638380 }
83648381 fn refUnwrapped(formatter: *Formatter, node: Metadata) Allocator.Error!FormatData.Node {
......@@ -8437,6 +8454,7 @@ pub const Metadata = enum(u32) {
84378454 inline for (names) |name| @field(fmt_args, name) = try formatter.fmt(
84388455 name ++ ": ",
84398456 @field(nodes, name),
8457 null,
84408458 );
84418459 try w.print(fmt_str, fmt_args);
84428460 }
......@@ -8965,7 +8983,7 @@ pub fn getIntrinsic(
89658983 const w = &aw.writer;
89668984 defer self.strtab_string_bytes = aw.toArrayList();
89678985 w.print("llvm.{s}", .{@tagName(id)}) catch return error.OutOfMemory;
8968 for (overload) |ty| w.print(".{fm}", .{ty.fmt(self)}) catch return error.OutOfMemory;
8986 for (overload) |ty| w.print(".{f}", .{ty.fmt(self, .m)}) catch return error.OutOfMemory;
89698987 }
89708988 break :name try self.trailingStrtabString();
89718989 };
......@@ -9399,7 +9417,7 @@ pub fn printToFile(b: *Builder, file: std.fs.File, buffer: []u8) !void {
93999417 try fw.interface.flush();
94009418}
94019419
9402pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
9420pub fn print(self: *Builder, w: *Writer) (Writer.Error || Allocator.Error)!void {
94039421 var need_newline = false;
94049422 var metadata_formatter: Metadata.Formatter = .{ .builder = self, .need_comma = undefined };
94059423 defer metadata_formatter.map.deinit(self.gpa);
......@@ -9408,17 +9426,17 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
94089426 if (need_newline) try w.writeByte('\n') else need_newline = true;
94099427 if (self.source_filename != .none) try w.print(
94109428 \\; ModuleID = '{s}'
9411 \\source_filename = {f"}
9429 \\source_filename = {f}
94129430 \\
9413 , .{ self.source_filename.slice(self).?, self.source_filename.fmt(self) });
9431 , .{ self.source_filename.slice(self).?, self.source_filename.fmtQ(self) });
94149432 if (self.data_layout != .none) try w.print(
9415 \\target datalayout = {f"}
9433 \\target datalayout = {f}
94169434 \\
9417 , .{self.data_layout.fmt(self)});
9435 , .{self.data_layout.fmtQ(self)});
94189436 if (self.target_triple != .none) try w.print(
9419 \\target triple = {f"}
9437 \\target triple = {f}
94209438 \\
9421 , .{self.target_triple.fmt(self)});
9439 , .{self.target_triple.fmtQ(self)});
94229440 }
94239441
94249442 if (self.module_asm.items.len > 0) {
......@@ -9436,7 +9454,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
94369454 for (self.types.keys(), self.types.values()) |id, ty| try w.print(
94379455 \\%{f} = type {f}
94389456 \\
9439 , .{ id.fmt(self), ty.fmt(self) });
9457 , .{ id.fmt(self), ty.fmt(self, .default) });
94409458 }
94419459
94429460 if (self.variables.items.len > 0) {
......@@ -9447,7 +9465,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
94479465 metadata_formatter.need_comma = true;
94489466 defer metadata_formatter.need_comma = undefined;
94499467 try w.print(
9450 \\{f} ={f}{f}{f}{f}{f }{f}{f }{f} {s} {f%}{f }{f, }{f}
9468 \\{f} ={f}{f}{f}{f}{f }{f}{f }{f} {s} {f}{f}{f, }{f}
94519469 \\
94529470 , .{
94539471 variable.global.fmt(self),
......@@ -9461,10 +9479,10 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
94619479 global.addr_space,
94629480 global.externally_initialized,
94639481 @tagName(variable.mutability),
9464 global.type.fmt(self),
9465 variable.init.fmt(self),
9482 global.type.fmt(self, .percent),
9483 variable.init.fmt(self, .{ .space = true }),
94669484 variable.alignment,
9467 try metadata_formatter.fmt("!dbg ", global.dbg),
9485 try metadata_formatter.fmt("!dbg ", global.dbg, null),
94689486 });
94699487 }
94709488 }
......@@ -9477,7 +9495,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
94779495 metadata_formatter.need_comma = true;
94789496 defer metadata_formatter.need_comma = undefined;
94799497 try w.print(
9480 \\{f} ={f}{f}{f}{f}{f }{f} alias {f%}, {f%}{f}
9498 \\{f} ={f}{f}{f}{f}{f }{f} alias {f}, {f}{f}
94819499 \\
94829500 , .{
94839501 alias.global.fmt(self),
......@@ -9487,9 +9505,9 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
94879505 global.dll_storage_class,
94889506 alias.thread_local,
94899507 global.unnamed_addr,
9490 global.type.fmt(self),
9491 alias.aliasee.fmt(self),
9492 try metadata_formatter.fmt("!dbg ", global.dbg),
9508 global.type.fmt(self, .percent),
9509 alias.aliasee.fmt(self, .{ .percent = true }),
9510 try metadata_formatter.fmt("!dbg ", global.dbg, null),
94939511 });
94949512 }
94959513 }
......@@ -9509,7 +9527,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
95099527 \\
95109528 , .{function_attributes.fmt(self)});
95119529 try w.print(
9512 \\{s}{f}{f}{f}{f}{f}{f"} {f%} {f}(
9530 \\{s}{f}{f}{f}{f}{f}{f} {f} {f}(
95139531 , .{
95149532 if (function.instructions.len > 0) "define" else "declare",
95159533 global.linkage,
......@@ -9518,19 +9536,19 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
95189536 global.dll_storage_class,
95199537 function.call_conv,
95209538 function.attributes.ret(self).fmt(self),
9521 global.type.functionReturn(self).fmt(self),
9539 global.type.functionReturn(self).fmt(self, .percent),
95229540 function.global.fmt(self),
95239541 });
95249542 for (0..params_len) |arg| {
95259543 if (arg > 0) try w.writeAll(", ");
95269544 try w.print(
9527 \\{f%}{f"}
9545 \\{f}{f}
95289546 , .{
9529 global.type.functionParameters(self)[arg].fmt(self),
9547 global.type.functionParameters(self)[arg].fmt(self, .percent),
95309548 function.attributes.param(arg, self).fmt(self),
95319549 });
95329550 if (function.instructions.len > 0)
9533 try w.print(" {f}", .{function.arg(@intCast(arg)).fmt(function_index, self)})
9551 try w.print(" {f}", .{function.arg(@intCast(arg)).fmt(function_index, self, .{})})
95349552 else
95359553 try w.print(" %{d}", .{arg});
95369554 }
......@@ -9550,7 +9568,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
95509568 defer metadata_formatter.need_comma = undefined;
95519569 try w.print("{f }{f}", .{
95529570 function.alignment,
9553 try metadata_formatter.fmt(" !dbg ", global.dbg),
9571 try metadata_formatter.fmt(" !dbg ", global.dbg, null),
95549572 });
95559573 }
95569574 if (function.instructions.len > 0) {
......@@ -9653,11 +9671,11 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
96539671 .xor,
96549672 => |tag| {
96559673 const extra = function.extraData(Function.Instruction.Binary, instruction.data);
9656 try w.print(" %{f} = {s} {f%}, {f}", .{
9674 try w.print(" %{f} = {s} {f}, {f}", .{
96579675 instruction_index.name(&function).fmt(self),
96589676 @tagName(tag),
9659 extra.lhs.fmt(function_index, self),
9660 extra.rhs.fmt(function_index, self),
9677 extra.lhs.fmt(function_index, self, .{ .percent = true }),
9678 extra.rhs.fmt(function_index, self, .{ .percent = true }),
96619679 });
96629680 },
96639681 .addrspacecast,
......@@ -9675,25 +9693,28 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
96759693 .zext,
96769694 => |tag| {
96779695 const extra = function.extraData(Function.Instruction.Cast, instruction.data);
9678 try w.print(" %{f} = {s} {f%} to {f%}", .{
9696 try w.print(" %{f} = {s} {f} to {f}", .{
96799697 instruction_index.name(&function).fmt(self),
96809698 @tagName(tag),
9681 extra.val.fmt(function_index, self),
9682 extra.type.fmt(self),
9699 extra.val.fmt(function_index, self, .{ .percent = true }),
9700 extra.type.fmt(self, .percent),
96839701 });
96849702 },
96859703 .alloca,
96869704 .@"alloca inalloca",
96879705 => |tag| {
96889706 const extra = function.extraData(Function.Instruction.Alloca, instruction.data);
9689 try w.print(" %{f} = {s} {f%}{f,%}{f, }{f, }", .{
9707 try w.print(" %{f} = {s} {f}{f}{f, }{f, }", .{
96909708 instruction_index.name(&function).fmt(self),
96919709 @tagName(tag),
9692 extra.type.fmt(self),
9710 extra.type.fmt(self, .percent),
96939711 Value.fmt(switch (extra.len) {
96949712 .@"1" => .none,
96959713 else => extra.len,
9696 }, function_index, self),
9714 }, function_index, self, .{
9715 .comma = true,
9716 .percent = true,
9717 }),
96979718 extra.info.alignment,
96989719 extra.info.addr_space,
96999720 });
......@@ -9702,13 +9723,13 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
97029723 .atomicrmw => |tag| {
97039724 const extra =
97049725 function.extraData(Function.Instruction.AtomicRmw, instruction.data);
9705 try w.print(" %{f} = {s}{f } {s} {f%}, {f%}{f }{f }{f, }", .{
9726 try w.print(" %{f} = {s}{f } {s} {f}, {f}{f }{f }{f, }", .{
97069727 instruction_index.name(&function).fmt(self),
97079728 @tagName(tag),
97089729 extra.info.access_kind,
97099730 @tagName(extra.info.atomic_rmw_operation),
9710 extra.ptr.fmt(function_index, self),
9711 extra.val.fmt(function_index, self),
9731 extra.ptr.fmt(function_index, self, .{ .percent = true }),
9732 extra.val.fmt(function_index, self, .{ .percent = true }),
97129733 extra.info.sync_scope,
97139734 extra.info.success_ordering,
97149735 extra.info.alignment,
......@@ -9724,16 +9745,16 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
97249745 },
97259746 .br => |tag| {
97269747 const target: Function.Block.Index = @enumFromInt(instruction.data);
9727 try w.print(" {s} {f%}", .{
9728 @tagName(tag), target.toInst(&function).fmt(function_index, self),
9748 try w.print(" {s} {f}", .{
9749 @tagName(tag), target.toInst(&function).fmt(function_index, self, .{ .percent = true }),
97299750 });
97309751 },
97319752 .br_cond => {
97329753 const extra = function.extraData(Function.Instruction.BrCond, instruction.data);
9733 try w.print(" br {f%}, {f%}, {f%}", .{
9734 extra.cond.fmt(function_index, self),
9735 extra.then.toInst(&function).fmt(function_index, self),
9736 extra.@"else".toInst(&function).fmt(function_index, self),
9754 try w.print(" br {f}, {f}, {f}", .{
9755 extra.cond.fmt(function_index, self, .{ .percent = true }),
9756 extra.then.toInst(&function).fmt(function_index, self, .{ .percent = true }),
9757 extra.@"else".toInst(&function).fmt(function_index, self, .{ .percent = true }),
97379758 });
97389759 metadata_formatter.need_comma = true;
97399760 defer metadata_formatter.need_comma = undefined;
......@@ -9741,7 +9762,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
97419762 .none => {},
97429763 .unpredictable => try w.writeAll("!unpredictable !{}"),
97439764 _ => try w.print("{f}", .{
9744 try metadata_formatter.fmt("!prof ", @as(Metadata, @enumFromInt(@intFromEnum(extra.weights)))),
9765 try metadata_formatter.fmt("!prof ", @as(Metadata, @enumFromInt(@intFromEnum(extra.weights))), null),
97459766 }),
97469767 }
97479768 },
......@@ -9766,7 +9787,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
97669787 }),
97679788 .none => unreachable,
97689789 }
9769 try w.print("{s}{f}{f}{f} {f%} {f}(", .{
9790 try w.print("{s}{f}{f}{f} {f} {f}(", .{
97709791 @tagName(tag),
97719792 extra.data.info.call_conv,
97729793 extra.data.attributes.ret(self).fmt(self),
......@@ -9774,15 +9795,15 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
97749795 switch (extra.data.ty.functionKind(self)) {
97759796 .normal => ret_ty,
97769797 .vararg => extra.data.ty,
9777 }.fmt(self),
9778 extra.data.callee.fmt(function_index, self),
9798 }.fmt(self, .percent),
9799 extra.data.callee.fmt(function_index, self, .{}),
97799800 });
97809801 for (0.., args) |arg_index, arg| {
97819802 if (arg_index > 0) try w.writeAll(", ");
97829803 metadata_formatter.need_comma = false;
97839804 defer metadata_formatter.need_comma = undefined;
9784 try w.print("{f%}{f}{f}", .{
9785 arg.typeOf(function_index, self).fmt(self),
9805 try w.print("{f}{f}{f}", .{
9806 arg.typeOf(function_index, self).fmt(self, .percent),
97869807 extra.data.attributes.param(arg_index, self).fmt(self),
97879808 try metadata_formatter.fmtLocal(" ", arg, function_index),
97889809 });
......@@ -9805,13 +9826,13 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
98059826 => |tag| {
98069827 const extra =
98079828 function.extraData(Function.Instruction.CmpXchg, instruction.data);
9808 try w.print(" %{f} = {s}{f } {f%}, {f%}, {f%}{f }{f }{f }{f, }", .{
9829 try w.print(" %{f} = {s}{f } {f}, {f}, {f}{f }{f }{f }{f, }", .{
98099830 instruction_index.name(&function).fmt(self),
98109831 @tagName(tag),
98119832 extra.info.access_kind,
9812 extra.ptr.fmt(function_index, self),
9813 extra.cmp.fmt(function_index, self),
9814 extra.new.fmt(function_index, self),
9833 extra.ptr.fmt(function_index, self, .{ .percent = true }),
9834 extra.cmp.fmt(function_index, self, .{ .percent = true }),
9835 extra.new.fmt(function_index, self, .{ .percent = true }),
98159836 extra.info.sync_scope,
98169837 extra.info.success_ordering,
98179838 extra.info.failure_ordering,
......@@ -9821,11 +9842,11 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
98219842 .extractelement => |tag| {
98229843 const extra =
98239844 function.extraData(Function.Instruction.ExtractElement, instruction.data);
9824 try w.print(" %{f} = {s} {f%}, {f%}", .{
9845 try w.print(" %{f} = {s} {f}, {f}", .{
98259846 instruction_index.name(&function).fmt(self),
98269847 @tagName(tag),
9827 extra.val.fmt(function_index, self),
9828 extra.index.fmt(function_index, self),
9848 extra.val.fmt(function_index, self, .{ .percent = true }),
9849 extra.index.fmt(function_index, self, .{ .percent = true }),
98299850 });
98309851 },
98319852 .extractvalue => |tag| {
......@@ -9834,10 +9855,10 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
98349855 instruction.data,
98359856 );
98369857 const indices = extra.trail.next(extra.data.indices_len, u32, &function);
9837 try w.print(" %{f} = {s} {f%}", .{
9858 try w.print(" %{f} = {s} {f}", .{
98389859 instruction_index.name(&function).fmt(self),
98399860 @tagName(tag),
9840 extra.data.val.fmt(function_index, self),
9861 extra.data.val.fmt(function_index, self, .{ .percent = true }),
98419862 });
98429863 for (indices) |index| try w.print(", {d}", .{index});
98439864 },
......@@ -9853,10 +9874,10 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
98539874 .@"fneg fast",
98549875 => |tag| {
98559876 const val: Value = @enumFromInt(instruction.data);
9856 try w.print(" %{f} = {s} {f%}", .{
9877 try w.print(" %{f} = {s} {f}", .{
98579878 instruction_index.name(&function).fmt(self),
98589879 @tagName(tag),
9859 val.fmt(function_index, self),
9880 val.fmt(function_index, self, .{ .percent = true }),
98609881 });
98619882 },
98629883 .getelementptr,
......@@ -9867,14 +9888,14 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
98679888 instruction.data,
98689889 );
98699890 const indices = extra.trail.next(extra.data.indices_len, Value, &function);
9870 try w.print(" %{f} = {s} {f%}, {f%}", .{
9891 try w.print(" %{f} = {s} {f}, {f}", .{
98719892 instruction_index.name(&function).fmt(self),
98729893 @tagName(tag),
9873 extra.data.type.fmt(self),
9874 extra.data.base.fmt(function_index, self),
9894 extra.data.type.fmt(self, .percent),
9895 extra.data.base.fmt(function_index, self, .{ .percent = true }),
98759896 });
9876 for (indices) |index| try w.print(", {f%}", .{
9877 index.fmt(function_index, self),
9897 for (indices) |index| try w.print(", {f}", .{
9898 index.fmt(function_index, self, .{ .percent = true }),
98789899 });
98799900 },
98809901 .indirectbr => |tag| {
......@@ -9882,14 +9903,14 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
98829903 function.extraDataTrail(Function.Instruction.IndirectBr, instruction.data);
98839904 const targets =
98849905 extra.trail.next(extra.data.targets_len, Function.Block.Index, &function);
9885 try w.print(" {s} {f%}, [", .{
9906 try w.print(" {s} {f}, [", .{
98869907 @tagName(tag),
9887 extra.data.addr.fmt(function_index, self),
9908 extra.data.addr.fmt(function_index, self, .{ .percent = true }),
98889909 });
98899910 for (0.., targets) |target_index, target| {
98909911 if (target_index > 0) try w.writeAll(", ");
9891 try w.print("{f%}", .{
9892 target.toInst(&function).fmt(function_index, self),
9912 try w.print("{f}", .{
9913 target.toInst(&function).fmt(function_index, self, .{ .percent = true }),
98939914 });
98949915 }
98959916 try w.writeByte(']');
......@@ -9897,23 +9918,23 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
98979918 .insertelement => |tag| {
98989919 const extra =
98999920 function.extraData(Function.Instruction.InsertElement, instruction.data);
9900 try w.print(" %{f} = {s} {f%}, {f%}, {f%}", .{
9921 try w.print(" %{f} = {s} {f}, {f}, {f}", .{
99019922 instruction_index.name(&function).fmt(self),
99029923 @tagName(tag),
9903 extra.val.fmt(function_index, self),
9904 extra.elem.fmt(function_index, self),
9905 extra.index.fmt(function_index, self),
9924 extra.val.fmt(function_index, self, .{ .percent = true }),
9925 extra.elem.fmt(function_index, self, .{ .percent = true }),
9926 extra.index.fmt(function_index, self, .{ .percent = true }),
99069927 });
99079928 },
99089929 .insertvalue => |tag| {
99099930 var extra =
99109931 function.extraDataTrail(Function.Instruction.InsertValue, instruction.data);
99119932 const indices = extra.trail.next(extra.data.indices_len, u32, &function);
9912 try w.print(" %{f} = {s} {f%}, {f%}", .{
9933 try w.print(" %{f} = {s} {f}, {f}", .{
99139934 instruction_index.name(&function).fmt(self),
99149935 @tagName(tag),
9915 extra.data.val.fmt(function_index, self),
9916 extra.data.elem.fmt(function_index, self),
9936 extra.data.val.fmt(function_index, self, .{ .percent = true }),
9937 extra.data.elem.fmt(function_index, self, .{ .percent = true }),
99179938 });
99189939 for (indices) |index| try w.print(", {d}", .{index});
99199940 },
......@@ -9921,12 +9942,12 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
99219942 .@"load atomic",
99229943 => |tag| {
99239944 const extra = function.extraData(Function.Instruction.Load, instruction.data);
9924 try w.print(" %{f} = {s}{f } {f%}, {f%}{f }{f }{f, }", .{
9945 try w.print(" %{f} = {s}{f } {f}, {f}{f }{f }{f, }", .{
99259946 instruction_index.name(&function).fmt(self),
99269947 @tagName(tag),
99279948 extra.info.access_kind,
9928 extra.type.fmt(self),
9929 extra.ptr.fmt(function_index, self),
9949 extra.type.fmt(self, .percent),
9950 extra.ptr.fmt(function_index, self, .{ .percent = true }),
99309951 extra.info.sync_scope,
99319952 extra.info.success_ordering,
99329953 extra.info.alignment,
......@@ -9939,24 +9960,24 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
99399960 const vals = extra.trail.next(block_incoming_len, Value, &function);
99409961 const blocks =
99419962 extra.trail.next(block_incoming_len, Function.Block.Index, &function);
9942 try w.print(" %{f} = {s} {f%} ", .{
9963 try w.print(" %{f} = {s} {f} ", .{
99439964 instruction_index.name(&function).fmt(self),
99449965 @tagName(tag),
9945 vals[0].typeOf(function_index, self).fmt(self),
9966 vals[0].typeOf(function_index, self).fmt(self, .percent),
99469967 });
99479968 for (0.., vals, blocks) |incoming_index, incoming_val, incoming_block| {
99489969 if (incoming_index > 0) try w.writeAll(", ");
99499970 try w.print("[ {f}, {f} ]", .{
9950 incoming_val.fmt(function_index, self),
9951 incoming_block.toInst(&function).fmt(function_index, self),
9971 incoming_val.fmt(function_index, self, .{}),
9972 incoming_block.toInst(&function).fmt(function_index, self, .{}),
99529973 });
99539974 }
99549975 },
99559976 .ret => |tag| {
99569977 const val: Value = @enumFromInt(instruction.data);
9957 try w.print(" {s} {f%}", .{
9978 try w.print(" {s} {f}", .{
99589979 @tagName(tag),
9959 val.fmt(function_index, self),
9980 val.fmt(function_index, self, .{ .percent = true }),
99609981 });
99619982 },
99629983 .@"ret void",
......@@ -9966,34 +9987,34 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
99669987 .@"select fast",
99679988 => |tag| {
99689989 const extra = function.extraData(Function.Instruction.Select, instruction.data);
9969 try w.print(" %{f} = {s} {f%}, {f%}, {f%}", .{
9990 try w.print(" %{f} = {s} {f}, {f}, {f}", .{
99709991 instruction_index.name(&function).fmt(self),
99719992 @tagName(tag),
9972 extra.cond.fmt(function_index, self),
9973 extra.lhs.fmt(function_index, self),
9974 extra.rhs.fmt(function_index, self),
9993 extra.cond.fmt(function_index, self, .{ .percent = true }),
9994 extra.lhs.fmt(function_index, self, .{ .percent = true }),
9995 extra.rhs.fmt(function_index, self, .{ .percent = true }),
99759996 });
99769997 },
99779998 .shufflevector => |tag| {
99789999 const extra =
997910000 function.extraData(Function.Instruction.ShuffleVector, instruction.data);
9980 try w.print(" %{f} = {s} {f%}, {f%}, {f%}", .{
10001 try w.print(" %{f} = {s} {f}, {f}, {f}", .{
998110002 instruction_index.name(&function).fmt(self),
998210003 @tagName(tag),
9983 extra.lhs.fmt(function_index, self),
9984 extra.rhs.fmt(function_index, self),
9985 extra.mask.fmt(function_index, self),
10004 extra.lhs.fmt(function_index, self, .{ .percent = true }),
10005 extra.rhs.fmt(function_index, self, .{ .percent = true }),
10006 extra.mask.fmt(function_index, self, .{ .percent = true }),
998610007 });
998710008 },
998810009 .store,
998910010 .@"store atomic",
999010011 => |tag| {
999110012 const extra = function.extraData(Function.Instruction.Store, instruction.data);
9992 try w.print(" {s}{f } {f%}, {f%}{f }{f }{f, }", .{
10013 try w.print(" {s}{f } {f}, {f}{f }{f }{f, }", .{
999310014 @tagName(tag),
999410015 extra.info.access_kind,
9995 extra.val.fmt(function_index, self),
9996 extra.ptr.fmt(function_index, self),
10016 extra.val.fmt(function_index, self, .{ .percent = true }),
10017 extra.ptr.fmt(function_index, self, .{ .percent = true }),
999710018 extra.info.sync_scope,
999810019 extra.info.success_ordering,
999910020 extra.info.alignment,
......@@ -10005,16 +10026,16 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
1000510026 const vals = extra.trail.next(extra.data.cases_len, Constant, &function);
1000610027 const blocks =
1000710028 extra.trail.next(extra.data.cases_len, Function.Block.Index, &function);
10008 try w.print(" {s} {f%}, {f%} [\n", .{
10029 try w.print(" {s} {f}, {f} [\n", .{
1000910030 @tagName(tag),
10010 extra.data.val.fmt(function_index, self),
10011 extra.data.default.toInst(&function).fmt(function_index, self),
10031 extra.data.val.fmt(function_index, self, .{ .percent = true }),
10032 extra.data.default.toInst(&function).fmt(function_index, self, .{ .percent = true }),
1001210033 });
1001310034 for (vals, blocks) |case_val, case_block| try w.print(
10014 " {f%}, {f%}\n",
10035 " {f}, {f}\n",
1001510036 .{
10016 case_val.fmt(self),
10017 case_block.toInst(&function).fmt(function_index, self),
10037 case_val.fmt(self, .{ .percent = true }),
10038 case_block.toInst(&function).fmt(function_index, self, .{ .percent = true }),
1001810039 },
1001910040 );
1002010041 try w.writeAll(" ]");
......@@ -10024,17 +10045,17 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
1002410045 .none => {},
1002510046 .unpredictable => try w.writeAll("!unpredictable !{}"),
1002610047 _ => try w.print("{f}", .{
10027 try metadata_formatter.fmt("!prof ", @as(Metadata, @enumFromInt(@intFromEnum(extra.data.weights)))),
10048 try metadata_formatter.fmt("!prof ", @as(Metadata, @enumFromInt(@intFromEnum(extra.data.weights))), null),
1002810049 }),
1002910050 }
1003010051 },
1003110052 .va_arg => |tag| {
1003210053 const extra = function.extraData(Function.Instruction.VaArg, instruction.data);
10033 try w.print(" %{f} = {s} {f%}, {f%}", .{
10054 try w.print(" %{f} = {s} {f}, {f}", .{
1003410055 instruction_index.name(&function).fmt(self),
1003510056 @tagName(tag),
10036 extra.list.fmt(function_index, self),
10037 extra.type.fmt(self),
10057 extra.list.fmt(function_index, self, .{ .percent = true }),
10058 extra.type.fmt(self, .percent),
1003810059 });
1003910060 },
1004010061 }
......@@ -10068,7 +10089,7 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
1006810089 try w.writeAll(" = !{");
1006910090 metadata_formatter.need_comma = false;
1007010091 defer metadata_formatter.need_comma = undefined;
10071 for (elements) |element| try w.print("{f}", .{try metadata_formatter.fmt("", element)});
10092 for (elements) |element| try w.print("{f}", .{try metadata_formatter.fmt("", element, null)});
1007210093 try w.writeAll("}\n");
1007310094 }
1007410095 }
......@@ -10371,28 +10392,28 @@ pub fn print(self: *Builder, w: *Writer) Writer.Error!void {
1037110392 var extra = self.metadataExtraDataTrail(Metadata.Tuple, metadata_item.data);
1037210393 const elements = extra.trail.next(extra.data.elements_len, Metadata, self);
1037310394 try w.writeAll("!{");
10374 for (elements) |element| try w.print("{[element]f%}", .{
10375 .element = try metadata_formatter.fmt("", element),
10395 for (elements) |element| try w.print("{[element]f}", .{
10396 .element = try metadata_formatter.fmt("", element, .{ .percent = true }),
1037610397 });
1037710398 try w.writeAll("}\n");
1037810399 },
1037910400 .str_tuple => {
1038010401 var extra = self.metadataExtraDataTrail(Metadata.StrTuple, metadata_item.data);
1038110402 const elements = extra.trail.next(extra.data.elements_len, Metadata, self);
10382 try w.print("!{{{[str]f%}", .{
10383 .str = try metadata_formatter.fmt("", extra.data.str),
10403 try w.print("!{{{[str]f}", .{
10404 .str = try metadata_formatter.fmt("", extra.data.str, .{ .percent = true }),
1038410405 });
10385 for (elements) |element| try w.print("{[element]f%}", .{
10386 .element = try metadata_formatter.fmt("", element),
10406 for (elements) |element| try w.print("{[element]f}", .{
10407 .element = try metadata_formatter.fmt("", element, .{ .percent = true }),
1038710408 });
1038810409 try w.writeAll("}\n");
1038910410 },
1039010411 .module_flag => {
1039110412 const extra = self.metadataExtraData(Metadata.ModuleFlag, metadata_item.data);
10392 try w.print("!{{{[behavior]f%}{[name]f%}{[constant]f%}}}\n", .{
10393 .behavior = try metadata_formatter.fmt("", extra.behavior),
10394 .name = try metadata_formatter.fmt("", extra.name),
10395 .constant = try metadata_formatter.fmt("", extra.constant),
10413 try w.print("!{{{[behavior]f}{[name]f}{[constant]f}}}\n", .{
10414 .behavior = try metadata_formatter.fmt("", extra.behavior, .{ .percent = true }),
10415 .name = try metadata_formatter.fmt("", extra.name, .{ .percent = true }),
10416 .constant = try metadata_formatter.fmt("", extra.constant, .{ .percent = true }),
1039610417 });
1039710418 },
1039810419 .local_var => {
......@@ -15109,3 +15130,13 @@ pub fn toBitcode(self: *Builder, allocator: Allocator, producer: Producer) bitco
1510915130
1511015131 return bitcode.toOwnedSlice();
1511115132}
15133
15134const FormatFlags = struct {
15135 comma: bool = false,
15136 space: bool = false,
15137 percent: bool = false,
15138
15139 fn onlyPercent(f: FormatFlags) bool {
15140 return !f.comma and !f.space and f.percent;
15141 }
15142};
lib/std/zip.zig+9-9
......@@ -124,7 +124,7 @@ pub fn findEndRecord(seekable_stream: anytype, stream_len: u64) !EndRecord {
124124
125125 try seekable_stream.seekTo(stream_len - @as(u64, new_loaded_len));
126126 const read_buf: []u8 = buf[buf.len - new_loaded_len ..][0..read_len];
127 const len = try seekable_stream.context.reader().readAll(read_buf);
127 const len = try seekable_stream.context.deprecatedReader().readAll(read_buf);
128128 if (len != read_len)
129129 return error.ZipTruncated;
130130 loaded_len = new_loaded_len;
......@@ -295,7 +295,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
295295 if (locator_end_offset > stream_len)
296296 return error.ZipTruncated;
297297 try stream.seekTo(stream_len - locator_end_offset);
298 const locator = try stream.context.reader().readStructEndian(EndLocator64, .little);
298 const locator = try stream.context.deprecatedReader().readStructEndian(EndLocator64, .little);
299299 if (!std.mem.eql(u8, &locator.signature, &end_locator64_sig))
300300 return error.ZipBadLocatorSig;
301301 if (locator.zip64_disk_count != 0)
......@@ -305,7 +305,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
305305
306306 try stream.seekTo(locator.record_file_offset);
307307
308 const record64 = try stream.context.reader().readStructEndian(EndRecord64, .little);
308 const record64 = try stream.context.deprecatedReader().readStructEndian(EndRecord64, .little);
309309
310310 if (!std.mem.eql(u8, &record64.signature, &end_record64_sig))
311311 return error.ZipBadEndRecord64Sig;
......@@ -357,7 +357,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
357357
358358 const header_zip_offset = self.cd_zip_offset + self.cd_record_offset;
359359 try self.stream.seekTo(header_zip_offset);
360 const header = try self.stream.context.reader().readStructEndian(CentralDirectoryFileHeader, .little);
360 const header = try self.stream.context.deprecatedReader().readStructEndian(CentralDirectoryFileHeader, .little);
361361 if (!std.mem.eql(u8, &header.signature, &central_file_header_sig))
362362 return error.ZipBadCdOffset;
363363
......@@ -386,7 +386,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
386386
387387 {
388388 try self.stream.seekTo(header_zip_offset + @sizeOf(CentralDirectoryFileHeader) + header.filename_len);
389 const len = try self.stream.context.reader().readAll(extra);
389 const len = try self.stream.context.deprecatedReader().readAll(extra);
390390 if (len != extra.len)
391391 return error.ZipTruncated;
392392 }
......@@ -449,7 +449,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
449449 try stream.seekTo(self.header_zip_offset + @sizeOf(CentralDirectoryFileHeader));
450450
451451 {
452 const len = try stream.context.reader().readAll(filename);
452 const len = try stream.context.deprecatedReader().readAll(filename);
453453 if (len != filename.len)
454454 return error.ZipBadFileOffset;
455455 }
......@@ -457,7 +457,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
457457 const local_data_header_offset: u64 = local_data_header_offset: {
458458 const local_header = blk: {
459459 try stream.seekTo(self.file_offset);
460 break :blk try stream.context.reader().readStructEndian(LocalFileHeader, .little);
460 break :blk try stream.context.deprecatedReader().readStructEndian(LocalFileHeader, .little);
461461 };
462462 if (!std.mem.eql(u8, &local_header.signature, &local_file_header_sig))
463463 return error.ZipBadFileOffset;
......@@ -483,7 +483,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
483483
484484 {
485485 try stream.seekTo(self.file_offset + @sizeOf(LocalFileHeader) + local_header.filename_len);
486 const len = try stream.context.reader().readAll(extra);
486 const len = try stream.context.deprecatedReader().readAll(extra);
487487 if (len != extra.len)
488488 return error.ZipTruncated;
489489 }
......@@ -552,7 +552,7 @@ pub fn Iterator(comptime SeekableStream: type) type {
552552 @as(u64, @sizeOf(LocalFileHeader)) +
553553 local_data_header_offset;
554554 try stream.seekTo(local_data_file_offset);
555 var limited_reader = std.io.limitedReader(stream.context.reader(), self.compressed_size);
555 var limited_reader = std.io.limitedReader(stream.context.deprecatedReader(), self.compressed_size);
556556 const crc = try decompress(
557557 self.compression_method,
558558 self.uncompressed_size,
src/Air/print.zig+2-2
......@@ -710,7 +710,7 @@ const Writer = struct {
710710 }
711711 }
712712 const asm_source = std.mem.sliceAsBytes(w.air.extra.items[extra_i..])[0..extra.data.source_len];
713 try s.print(", \"{f}\"", .{std.zig.fmtEscapes(asm_source)});
713 try s.print(", \"{f}\"", .{std.zig.fmtString(asm_source)});
714714 }
715715
716716 fn writeDbgStmt(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void {
......@@ -722,7 +722,7 @@ const Writer = struct {
722722 const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
723723 try w.writeOperand(s, inst, 0, pl_op.operand);
724724 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);
725 try s.print(", \"{f}\"", .{std.zig.fmtEscapes(name.toSlice(w.air))});
725 try s.print(", \"{f}\"", .{std.zig.fmtString(name.toSlice(w.air))});
726726 }
727727
728728 fn writeCall(w: *Writer, s: *std.io.Writer, inst: Air.Inst.Index) Error!void {
src/Zcu.zig+94-183
......@@ -15,6 +15,7 @@ const BigIntConst = std.math.big.int.Const;
1515const BigIntMutable = std.math.big.int.Mutable;
1616const Target = std.Target;
1717const Ast = std.zig.Ast;
18const Writer = std.io.Writer;
1819
1920const Zcu = @This();
2021const Compilation = @import("Compilation.zig");
......@@ -858,7 +859,7 @@ pub const Namespace = struct {
858859 try ns.fileScope(zcu).renderFullyQualifiedDebugName(writer);
859860 break :sep ':';
860861 };
861 if (name != .empty) try writer.print("{c}{}", .{ sep, name.fmt(&zcu.intern_pool) });
862 if (name != .empty) try writer.print("{c}{f}", .{ sep, name.fmt(&zcu.intern_pool) });
862863 }
863864
864865 pub fn internFullyQualifiedName(
......@@ -870,7 +871,7 @@ pub const Namespace = struct {
870871 ) !InternPool.NullTerminatedString {
871872 const ns_name = Type.fromInterned(ns.owner_type).containerTypeName(ip);
872873 if (name == .empty) return ns_name;
873 return ip.getOrPutStringFmt(gpa, tid, "{}.{}", .{ ns_name.fmt(ip), name.fmt(ip) }, .no_embedded_nulls);
874 return ip.getOrPutStringFmt(gpa, tid, "{f}.{f}", .{ ns_name.fmt(ip), name.fmt(ip) }, .no_embedded_nulls);
874875 }
875876};
876877
......@@ -1039,12 +1040,12 @@ pub const File = struct {
10391040 if (stat.size > std.math.maxInt(u32))
10401041 return error.FileTooBig;
10411042
1042 const source = try gpa.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0);
1043 const source = try gpa.allocSentinel(u8, @intCast(stat.size), 0);
10431044 errdefer gpa.free(source);
10441045
1045 const amt = try f.readAll(source);
1046 if (amt != stat.size)
1047 return error.UnexpectedEndOfFile;
1046 var file_reader = f.reader(&.{});
1047 file_reader.size = stat.size;
1048 try file_reader.interface.readSliceAll(source);
10481049
10491050 // Here we do not modify stat fields because this function is the one
10501051 // used for error reporting. We need to keep the stat fields stale so that
......@@ -1097,11 +1098,10 @@ pub const File = struct {
10971098 const gpa = pt.zcu.gpa;
10981099 const ip = &pt.zcu.intern_pool;
10991100 const strings = ip.getLocal(pt.tid).getMutableStrings(gpa);
1100 const slice = try strings.addManyAsSlice(file.fullyQualifiedNameLen());
1101 var fbs = std.io.fixedBufferStream(slice[0]);
1102 file.renderFullyQualifiedName(fbs.writer()) catch unreachable;
1103 assert(fbs.pos == slice[0].len);
1104 return ip.getOrPutTrailingString(gpa, pt.tid, @intCast(slice[0].len), .no_embedded_nulls);
1101 var w: Writer = .fixed((try strings.addManyAsSlice(file.fullyQualifiedNameLen()))[0]);
1102 file.renderFullyQualifiedName(&w) catch unreachable;
1103 assert(w.end == w.buffer.len);
1104 return ip.getOrPutTrailingString(gpa, pt.tid, @intCast(w.end), .no_embedded_nulls);
11051105 }
11061106
11071107 pub const Index = InternPool.FileIndex;
......@@ -1190,13 +1190,8 @@ pub const ErrorMsg = struct {
11901190 gpa.destroy(err_msg);
11911191 }
11921192
1193 pub fn init(
1194 gpa: Allocator,
1195 src_loc: LazySrcLoc,
1196 comptime format: []const u8,
1197 args: anytype,
1198 ) !ErrorMsg {
1199 return ErrorMsg{
1193 pub fn init(gpa: Allocator, src_loc: LazySrcLoc, comptime format: []const u8, args: anytype) !ErrorMsg {
1194 return .{
12001195 .src_loc = src_loc,
12011196 .msg = try std.fmt.allocPrint(gpa, format, args),
12021197 };
......@@ -2811,10 +2806,18 @@ comptime {
28112806}
28122807
28132808pub fn loadZirCache(gpa: Allocator, cache_file: std.fs.File) !Zir {
2814 return loadZirCacheBody(gpa, try cache_file.deprecatedReader().readStruct(Zir.Header), cache_file);
2809 var buffer: [2000]u8 = undefined;
2810 var file_reader = cache_file.reader(&buffer);
2811 return result: {
2812 const header = file_reader.interface.takeStruct(Zir.Header) catch |err| break :result err;
2813 break :result loadZirCacheBody(gpa, header.*, &file_reader.interface);
2814 } catch |err| switch (err) {
2815 error.ReadFailed => return file_reader.err.?,
2816 else => |e| return e,
2817 };
28152818}
28162819
2817pub fn loadZirCacheBody(gpa: Allocator, header: Zir.Header, cache_file: std.fs.File) !Zir {
2820pub fn loadZirCacheBody(gpa: Allocator, header: Zir.Header, cache_br: *std.io.Reader) !Zir {
28182821 var instructions: std.MultiArrayList(Zir.Inst) = .{};
28192822 errdefer instructions.deinit(gpa);
28202823
......@@ -2837,34 +2840,16 @@ pub fn loadZirCacheBody(gpa: Allocator, header: Zir.Header, cache_file: std.fs.F
28372840 undefined;
28382841 defer if (data_has_safety_tag) gpa.free(safety_buffer);
28392842
2840 const data_ptr = if (data_has_safety_tag)
2841 @as([*]u8, @ptrCast(safety_buffer.ptr))
2842 else
2843 @as([*]u8, @ptrCast(zir.instructions.items(.data).ptr));
2844
2845 var iovecs = [_]std.posix.iovec{
2846 .{
2847 .base = @as([*]u8, @ptrCast(zir.instructions.items(.tag).ptr)),
2848 .len = header.instructions_len,
2849 },
2850 .{
2851 .base = data_ptr,
2852 .len = header.instructions_len * 8,
2853 },
2854 .{
2855 .base = zir.string_bytes.ptr,
2856 .len = header.string_bytes_len,
2857 },
2858 .{
2859 .base = @as([*]u8, @ptrCast(zir.extra.ptr)),
2860 .len = header.extra_len * 4,
2861 },
2843 var vecs = [_][]u8{
2844 @ptrCast(zir.instructions.items(.tag)),
2845 if (data_has_safety_tag)
2846 @ptrCast(safety_buffer)
2847 else
2848 @ptrCast(zir.instructions.items(.data)),
2849 zir.string_bytes,
2850 @ptrCast(zir.extra),
28622851 };
2863 const amt_read = try cache_file.readvAll(&iovecs);
2864 const amt_expected = zir.instructions.len * 9 +
2865 zir.string_bytes.len +
2866 zir.extra.len * 4;
2867 if (amt_read != amt_expected) return error.UnexpectedFileSize;
2852 try cache_br.readVecAll(&vecs);
28682853 if (data_has_safety_tag) {
28692854 const tags = zir.instructions.items(.tag);
28702855 for (zir.instructions.items(.data), 0..) |*data, i| {
......@@ -2876,7 +2861,6 @@ pub fn loadZirCacheBody(gpa: Allocator, header: Zir.Header, cache_file: std.fs.F
28762861 };
28772862 }
28782863 }
2879
28802864 return zir;
28812865}
28822866
......@@ -2887,14 +2871,6 @@ pub fn saveZirCache(gpa: Allocator, cache_file: std.fs.File, stat: std.fs.File.S
28872871 undefined;
28882872 defer if (data_has_safety_tag) gpa.free(safety_buffer);
28892873
2890 const data_ptr: [*]const u8 = if (data_has_safety_tag)
2891 if (zir.instructions.len == 0)
2892 undefined
2893 else
2894 @ptrCast(safety_buffer.ptr)
2895 else
2896 @ptrCast(zir.instructions.items(.data).ptr);
2897
28982874 if (data_has_safety_tag) {
28992875 // The `Data` union has a safety tag but in the file format we store it without.
29002876 for (zir.instructions.items(.data), 0..) |*data, i| {
......@@ -2912,29 +2888,20 @@ pub fn saveZirCache(gpa: Allocator, cache_file: std.fs.File, stat: std.fs.File.S
29122888 .stat_inode = stat.inode,
29132889 .stat_mtime = stat.mtime,
29142890 };
2915 var iovecs: [5]std.posix.iovec_const = .{
2916 .{
2917 .base = @ptrCast(&header),
2918 .len = @sizeOf(Zir.Header),
2919 },
2920 .{
2921 .base = @ptrCast(zir.instructions.items(.tag).ptr),
2922 .len = zir.instructions.len,
2923 },
2924 .{
2925 .base = data_ptr,
2926 .len = zir.instructions.len * 8,
2927 },
2928 .{
2929 .base = zir.string_bytes.ptr,
2930 .len = zir.string_bytes.len,
2931 },
2932 .{
2933 .base = @ptrCast(zir.extra.ptr),
2934 .len = zir.extra.len * 4,
2935 },
2891 var vecs = [_][]const u8{
2892 @ptrCast((&header)[0..1]),
2893 @ptrCast(zir.instructions.items(.tag)),
2894 if (data_has_safety_tag)
2895 @ptrCast(safety_buffer)
2896 else
2897 @ptrCast(zir.instructions.items(.data)),
2898 zir.string_bytes,
2899 @ptrCast(zir.extra),
2900 };
2901 var cache_fw = cache_file.writer(&.{});
2902 cache_fw.interface.writeVecAll(&vecs) catch |err| switch (err) {
2903 error.WriteFailed => return cache_fw.err.?,
29362904 };
2937 try cache_file.writevAll(&iovecs);
29382905}
29392906
29402907pub fn saveZoirCache(cache_file: std.fs.File, stat: std.fs.File.Stat, zoir: Zoir) std.fs.File.WriteError!void {
......@@ -2950,48 +2917,24 @@ pub fn saveZoirCache(cache_file: std.fs.File, stat: std.fs.File.Stat, zoir: Zoir
29502917 .stat_inode = stat.inode,
29512918 .stat_mtime = stat.mtime,
29522919 };
2953 var iovecs: [9]std.posix.iovec_const = .{
2954 .{
2955 .base = @ptrCast(&header),
2956 .len = @sizeOf(Zoir.Header),
2957 },
2958 .{
2959 .base = @ptrCast(zoir.nodes.items(.tag)),
2960 .len = zoir.nodes.len * @sizeOf(Zoir.Node.Repr.Tag),
2961 },
2962 .{
2963 .base = @ptrCast(zoir.nodes.items(.data)),
2964 .len = zoir.nodes.len * 4,
2965 },
2966 .{
2967 .base = @ptrCast(zoir.nodes.items(.ast_node)),
2968 .len = zoir.nodes.len * 4,
2969 },
2970 .{
2971 .base = @ptrCast(zoir.extra),
2972 .len = zoir.extra.len * 4,
2973 },
2974 .{
2975 .base = @ptrCast(zoir.limbs),
2976 .len = zoir.limbs.len * @sizeOf(std.math.big.Limb),
2977 },
2978 .{
2979 .base = zoir.string_bytes.ptr,
2980 .len = zoir.string_bytes.len,
2981 },
2982 .{
2983 .base = @ptrCast(zoir.compile_errors),
2984 .len = zoir.compile_errors.len * @sizeOf(Zoir.CompileError),
2985 },
2986 .{
2987 .base = @ptrCast(zoir.error_notes),
2988 .len = zoir.error_notes.len * @sizeOf(Zoir.CompileError.Note),
2989 },
2920 var vecs = [_][]const u8{
2921 @ptrCast((&header)[0..1]),
2922 @ptrCast(zoir.nodes.items(.tag)),
2923 @ptrCast(zoir.nodes.items(.data)),
2924 @ptrCast(zoir.nodes.items(.ast_node)),
2925 @ptrCast(zoir.extra),
2926 @ptrCast(zoir.limbs),
2927 zoir.string_bytes,
2928 @ptrCast(zoir.compile_errors),
2929 @ptrCast(zoir.error_notes),
2930 };
2931 var cache_fw = cache_file.writer(&.{});
2932 cache_fw.interface.writeVecAll(&vecs) catch |err| switch (err) {
2933 error.WriteFailed => return cache_fw.err.?,
29902934 };
2991 try cache_file.writevAll(&iovecs);
29922935}
29932936
2994pub fn loadZoirCacheBody(gpa: Allocator, header: Zoir.Header, cache_file: std.fs.File) !Zoir {
2937pub fn loadZoirCacheBody(gpa: Allocator, header: Zoir.Header, cache_br: *std.io.Reader) !Zoir {
29952938 var zoir: Zoir = .{
29962939 .nodes = .empty,
29972940 .extra = &.{},
......@@ -3017,49 +2960,17 @@ pub fn loadZoirCacheBody(gpa: Allocator, header: Zoir.Header, cache_file: std.fs
30172960 zoir.compile_errors = try gpa.alloc(Zoir.CompileError, header.compile_errors_len);
30182961 zoir.error_notes = try gpa.alloc(Zoir.CompileError.Note, header.error_notes_len);
30192962
3020 var iovecs: [8]std.posix.iovec = .{
3021 .{
3022 .base = @ptrCast(zoir.nodes.items(.tag)),
3023 .len = header.nodes_len * @sizeOf(Zoir.Node.Repr.Tag),
3024 },
3025 .{
3026 .base = @ptrCast(zoir.nodes.items(.data)),
3027 .len = header.nodes_len * 4,
3028 },
3029 .{
3030 .base = @ptrCast(zoir.nodes.items(.ast_node)),
3031 .len = header.nodes_len * 4,
3032 },
3033 .{
3034 .base = @ptrCast(zoir.extra),
3035 .len = header.extra_len * 4,
3036 },
3037 .{
3038 .base = @ptrCast(zoir.limbs),
3039 .len = header.limbs_len * @sizeOf(std.math.big.Limb),
3040 },
3041 .{
3042 .base = zoir.string_bytes.ptr,
3043 .len = header.string_bytes_len,
3044 },
3045 .{
3046 .base = @ptrCast(zoir.compile_errors),
3047 .len = header.compile_errors_len * @sizeOf(Zoir.CompileError),
3048 },
3049 .{
3050 .base = @ptrCast(zoir.error_notes),
3051 .len = header.error_notes_len * @sizeOf(Zoir.CompileError.Note),
3052 },
2963 var vecs = [_][]u8{
2964 @ptrCast(zoir.nodes.items(.tag)),
2965 @ptrCast(zoir.nodes.items(.data)),
2966 @ptrCast(zoir.nodes.items(.ast_node)),
2967 @ptrCast(zoir.extra),
2968 @ptrCast(zoir.limbs),
2969 zoir.string_bytes,
2970 @ptrCast(zoir.compile_errors),
2971 @ptrCast(zoir.error_notes),
30532972 };
3054
3055 const bytes_expected = expected: {
3056 var n: usize = 0;
3057 for (iovecs) |v| n += v.len;
3058 break :expected n;
3059 };
3060
3061 const bytes_read = try cache_file.readvAll(&iovecs);
3062 if (bytes_read != bytes_expected) return error.UnexpectedFileSize;
2973 try cache_br.readVecAll(&vecs);
30632974 return zoir;
30642975}
30652976
......@@ -3071,7 +2982,7 @@ pub fn markDependeeOutdated(
30712982 marked_po: enum { not_marked_po, marked_po },
30722983 dependee: InternPool.Dependee,
30732984) !void {
3074 log.debug("outdated dependee: {}", .{zcu.fmtDependee(dependee)});
2985 log.debug("outdated dependee: {f}", .{zcu.fmtDependee(dependee)});
30752986 var it = zcu.intern_pool.dependencyIterator(dependee);
30762987 while (it.next()) |depender| {
30772988 if (zcu.outdated.getPtr(depender)) |po_dep_count| {
......@@ -3079,9 +2990,9 @@ pub fn markDependeeOutdated(
30792990 .not_marked_po => {},
30802991 .marked_po => {
30812992 po_dep_count.* -= 1;
3082 log.debug("outdated {} => already outdated {} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), po_dep_count.* });
2993 log.debug("outdated {f} => already outdated {f} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), po_dep_count.* });
30832994 if (po_dep_count.* == 0) {
3084 log.debug("outdated ready: {}", .{zcu.fmtAnalUnit(depender)});
2995 log.debug("outdated ready: {f}", .{zcu.fmtAnalUnit(depender)});
30852996 try zcu.outdated_ready.put(zcu.gpa, depender, {});
30862997 }
30872998 },
......@@ -3102,9 +3013,9 @@ pub fn markDependeeOutdated(
31023013 depender,
31033014 new_po_dep_count,
31043015 );
3105 log.debug("outdated {} => new outdated {} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), new_po_dep_count });
3016 log.debug("outdated {f} => new outdated {f} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), new_po_dep_count });
31063017 if (new_po_dep_count == 0) {
3107 log.debug("outdated ready: {}", .{zcu.fmtAnalUnit(depender)});
3018 log.debug("outdated ready: {f}", .{zcu.fmtAnalUnit(depender)});
31083019 try zcu.outdated_ready.put(zcu.gpa, depender, {});
31093020 }
31103021 // If this is a Decl and was not previously PO, we must recursively
......@@ -3117,16 +3028,16 @@ pub fn markDependeeOutdated(
31173028}
31183029
31193030pub fn markPoDependeeUpToDate(zcu: *Zcu, dependee: InternPool.Dependee) !void {
3120 log.debug("up-to-date dependee: {}", .{zcu.fmtDependee(dependee)});
3031 log.debug("up-to-date dependee: {f}", .{zcu.fmtDependee(dependee)});
31213032 var it = zcu.intern_pool.dependencyIterator(dependee);
31223033 while (it.next()) |depender| {
31233034 if (zcu.outdated.getPtr(depender)) |po_dep_count| {
31243035 // This depender is already outdated, but it now has one
31253036 // less PO dependency!
31263037 po_dep_count.* -= 1;
3127 log.debug("up-to-date {} => {} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), po_dep_count.* });
3038 log.debug("up-to-date {f} => {f} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), po_dep_count.* });
31283039 if (po_dep_count.* == 0) {
3129 log.debug("outdated ready: {}", .{zcu.fmtAnalUnit(depender)});
3040 log.debug("outdated ready: {f}", .{zcu.fmtAnalUnit(depender)});
31303041 try zcu.outdated_ready.put(zcu.gpa, depender, {});
31313042 }
31323043 continue;
......@@ -3140,11 +3051,11 @@ pub fn markPoDependeeUpToDate(zcu: *Zcu, dependee: InternPool.Dependee) !void {
31403051 };
31413052 if (ptr.* > 1) {
31423053 ptr.* -= 1;
3143 log.debug("up-to-date {} => {} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), ptr.* });
3054 log.debug("up-to-date {f} => {f} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender), ptr.* });
31443055 continue;
31453056 }
31463057
3147 log.debug("up-to-date {} => {} po_deps=0 (up-to-date)", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender) });
3058 log.debug("up-to-date {f} => {f} po_deps=0 (up-to-date)", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(depender) });
31483059
31493060 // This dependency is no longer PO, i.e. is known to be up-to-date.
31503061 assert(zcu.potentially_outdated.swapRemove(depender));
......@@ -3173,7 +3084,7 @@ fn markTransitiveDependersPotentiallyOutdated(zcu: *Zcu, maybe_outdated: AnalUni
31733084 .func => |func_index| .{ .interned = func_index }, // IES
31743085 .memoized_state => |stage| .{ .memoized_state = stage },
31753086 };
3176 log.debug("potentially outdated dependee: {}", .{zcu.fmtDependee(dependee)});
3087 log.debug("potentially outdated dependee: {f}", .{zcu.fmtDependee(dependee)});
31773088 var it = ip.dependencyIterator(dependee);
31783089 while (it.next()) |po| {
31793090 if (zcu.outdated.getPtr(po)) |po_dep_count| {
......@@ -3183,17 +3094,17 @@ fn markTransitiveDependersPotentiallyOutdated(zcu: *Zcu, maybe_outdated: AnalUni
31833094 _ = zcu.outdated_ready.swapRemove(po);
31843095 }
31853096 po_dep_count.* += 1;
3186 log.debug("po {} => {} [outdated] po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(po), po_dep_count.* });
3097 log.debug("po {f} => {f} [outdated] po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(po), po_dep_count.* });
31873098 continue;
31883099 }
31893100 if (zcu.potentially_outdated.getPtr(po)) |n| {
31903101 // There is now one more PO dependency.
31913102 n.* += 1;
3192 log.debug("po {} => {} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(po), n.* });
3103 log.debug("po {f} => {f} po_deps={}", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(po), n.* });
31933104 continue;
31943105 }
31953106 try zcu.potentially_outdated.putNoClobber(zcu.gpa, po, 1);
3196 log.debug("po {} => {} po_deps=1", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(po) });
3107 log.debug("po {f} => {f} po_deps=1", .{ zcu.fmtDependee(dependee), zcu.fmtAnalUnit(po) });
31973108 // This AnalUnit was not already PO, so we must recursively mark its dependers as also PO.
31983109 try zcu.markTransitiveDependersPotentiallyOutdated(po);
31993110 }
......@@ -3222,7 +3133,7 @@ pub fn findOutdatedToAnalyze(zcu: *Zcu) Allocator.Error!?AnalUnit {
32223133
32233134 if (zcu.outdated_ready.count() > 0) {
32243135 const unit = zcu.outdated_ready.keys()[0];
3225 log.debug("findOutdatedToAnalyze: trivial {}", .{zcu.fmtAnalUnit(unit)});
3136 log.debug("findOutdatedToAnalyze: trivial {f}", .{zcu.fmtAnalUnit(unit)});
32263137 return unit;
32273138 }
32283139
......@@ -3273,7 +3184,7 @@ pub fn findOutdatedToAnalyze(zcu: *Zcu) Allocator.Error!?AnalUnit {
32733184 }
32743185 }
32753186
3276 log.debug("findOutdatedToAnalyze: heuristic returned '{}' ({d} dependers)", .{
3187 log.debug("findOutdatedToAnalyze: heuristic returned '{f}' ({d} dependers)", .{
32773188 zcu.fmtAnalUnit(chosen_unit.?),
32783189 chosen_unit_dependers,
32793190 });
......@@ -4072,7 +3983,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
40723983 const referencer = kv.value;
40733984 try checked_types.putNoClobber(gpa, ty, {});
40743985
4075 log.debug("handle type '{}'", .{Type.fromInterned(ty).containerTypeName(ip).fmt(ip)});
3986 log.debug("handle type '{f}'", .{Type.fromInterned(ty).containerTypeName(ip).fmt(ip)});
40763987
40773988 // If this type undergoes type resolution, the corresponding `AnalUnit` is automatically referenced.
40783989 const has_resolution: bool = switch (ip.indexToKey(ty)) {
......@@ -4108,7 +4019,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
41084019 // `comptime` decls are always analyzed.
41094020 const unit: AnalUnit = .wrap(.{ .@"comptime" = cu });
41104021 if (!result.contains(unit)) {
4111 log.debug("type '{}': ref comptime %{}", .{
4022 log.debug("type '{f}': ref comptime %{}", .{
41124023 Type.fromInterned(ty).containerTypeName(ip).fmt(ip),
41134024 @intFromEnum(ip.getComptimeUnit(cu).zir_index.resolve(ip) orelse continue),
41144025 });
......@@ -4139,7 +4050,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
41394050 },
41404051 };
41414052 if (want_analysis) {
4142 log.debug("type '{}': ref test %{}", .{
4053 log.debug("type '{f}': ref test %{}", .{
41434054 Type.fromInterned(ty).containerTypeName(ip).fmt(ip),
41444055 @intFromEnum(inst_info.inst),
41454056 });
......@@ -4158,7 +4069,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
41584069 if (decl.linkage == .@"export") {
41594070 const unit: AnalUnit = .wrap(.{ .nav_val = nav });
41604071 if (!result.contains(unit)) {
4161 log.debug("type '{}': ref named %{}", .{
4072 log.debug("type '{f}': ref named %{}", .{
41624073 Type.fromInterned(ty).containerTypeName(ip).fmt(ip),
41634074 @intFromEnum(inst_info.inst),
41644075 });
......@@ -4174,7 +4085,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
41744085 if (decl.linkage == .@"export") {
41754086 const unit: AnalUnit = .wrap(.{ .nav_val = nav });
41764087 if (!result.contains(unit)) {
4177 log.debug("type '{}': ref named %{}", .{
4088 log.debug("type '{f}': ref named %{}", .{
41784089 Type.fromInterned(ty).containerTypeName(ip).fmt(ip),
41794090 @intFromEnum(inst_info.inst),
41804091 });
......@@ -4199,7 +4110,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
41994110 try unit_queue.put(gpa, other, kv.value); // same reference location
42004111 }
42014112
4202 log.debug("handle unit '{}'", .{zcu.fmtAnalUnit(unit)});
4113 log.debug("handle unit '{f}'", .{zcu.fmtAnalUnit(unit)});
42034114
42044115 if (zcu.reference_table.get(unit)) |first_ref_idx| {
42054116 assert(first_ref_idx != std.math.maxInt(u32));
......@@ -4207,7 +4118,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
42074118 while (ref_idx != std.math.maxInt(u32)) {
42084119 const ref = zcu.all_references.items[ref_idx];
42094120 if (!result.contains(ref.referenced)) {
4210 log.debug("unit '{}': ref unit '{}'", .{
4121 log.debug("unit '{f}': ref unit '{f}'", .{
42114122 zcu.fmtAnalUnit(unit),
42124123 zcu.fmtAnalUnit(ref.referenced),
42134124 });
......@@ -4226,7 +4137,7 @@ fn resolveReferencesInner(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, ?Resolv
42264137 while (ref_idx != std.math.maxInt(u32)) {
42274138 const ref = zcu.all_type_references.items[ref_idx];
42284139 if (!checked_types.contains(ref.referenced)) {
4229 log.debug("unit '{}': ref type '{}'", .{
4140 log.debug("unit '{f}': ref type '{f}'", .{
42304141 zcu.fmtAnalUnit(unit),
42314142 Type.fromInterned(ref.referenced).containerTypeName(ip).fmt(ip),
42324143 });
src/Zcu/PerThread.zig+1-1
......@@ -343,7 +343,7 @@ fn loadZirZoirCache(
343343 .zon => Zoir.Header,
344344 };
345345
346 var buffer: [@sizeOf(Header)]u8 = undefined;
346 var buffer: [2000]u8 = undefined;
347347 var cache_fr = cache_file.reader(&buffer);
348348 cache_fr.size = stat.size;
349349 const cache_br = &cache_fr.interface;