authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-07 22:21:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-07 22:26:42-07:00
log9a538e0d54186451772c6b07b348418089797f83
tree948bcf85b2ce5c92e481171721fba6a446b9ba1a
parent281f657377c46180e0c7044e6ac0c7b8b69ef892

link.Dwarf: minimal changes to remove CountingWriter


2 files changed, 37 insertions(+), 28 deletions(-)

lib/std/Io/Writer.zig+5
...@@ -2239,6 +2239,11 @@ pub const Discarding = struct {...@@ -2239,6 +2239,11 @@ pub const Discarding = struct {
2239 };2239 };
2240 }2240 }
22412241
2242 /// Includes buffered data (no need to flush).
2243 pub fn fullCount(d: *const Discarding) u64 {
2244 return d.count + d.writer.end;
2245 }
2246
2242 pub fn drain(w: *Writer, data: []const []const u8, splat: usize) Error!usize {2247 pub fn drain(w: *Writer, data: []const []const u8, splat: usize) Error!usize {
2243 const d: *Discarding = @alignCast(@fieldParentPtr("writer", w));2248 const d: *Discarding = @alignCast(@fieldParentPtr("writer", w));
2244 const slice = data[0 .. data.len - 1];2249 const slice = data[0 .. data.len - 1];
src/link/Dwarf.zig+32-28
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1gpa: std.mem.Allocator,1gpa: Allocator,
2bin_file: *link.File,2bin_file: *link.File,
3format: DW.Format,3format: DW.Format,
4endian: std.builtin.Endian,4endian: std.builtin.Endian,
...@@ -21,6 +21,7 @@ debug_rnglists: DebugRngLists,...@@ -21,6 +21,7 @@ debug_rnglists: DebugRngLists,
21debug_str: StringSection,21debug_str: StringSection,
2222
23pub const UpdateError = error{23pub const UpdateError = error{
24 WriteFailed,
24 ReinterpretDeclRef,25 ReinterpretDeclRef,
25 Unimplemented,26 Unimplemented,
26 EndOfStream,27 EndOfStream,
...@@ -50,7 +51,7 @@ const ModInfo = struct {...@@ -50,7 +51,7 @@ const ModInfo = struct {
50 dirs: std.AutoArrayHashMapUnmanaged(Unit.Index, void),51 dirs: std.AutoArrayHashMapUnmanaged(Unit.Index, void),
51 files: std.AutoArrayHashMapUnmanaged(Zcu.File.Index, void),52 files: std.AutoArrayHashMapUnmanaged(Zcu.File.Index, void),
5253
53 fn deinit(mod_info: *ModInfo, gpa: std.mem.Allocator) void {54 fn deinit(mod_info: *ModInfo, gpa: Allocator) void {
54 mod_info.dirs.deinit(gpa);55 mod_info.dirs.deinit(gpa);
55 mod_info.files.deinit(gpa);56 mod_info.files.deinit(gpa);
56 mod_info.* = undefined;57 mod_info.* = undefined;
...@@ -220,7 +221,7 @@ const StringSection = struct {...@@ -220,7 +221,7 @@ const StringSection = struct {
220 .section = Section.init,221 .section = Section.init,
221 };222 };
222223
223 fn deinit(str_sec: *StringSection, gpa: std.mem.Allocator) void {224 fn deinit(str_sec: *StringSection, gpa: Allocator) void {
224 str_sec.contents.deinit(gpa);225 str_sec.contents.deinit(gpa);
225 str_sec.map.deinit(gpa);226 str_sec.map.deinit(gpa);
226 str_sec.section.deinit(gpa);227 str_sec.section.deinit(gpa);
...@@ -297,7 +298,7 @@ pub const Section = struct {...@@ -297,7 +298,7 @@ pub const Section = struct {
297 .len = 0,298 .len = 0,
298 };299 };
299300
300 fn deinit(sec: *Section, gpa: std.mem.Allocator) void {301 fn deinit(sec: *Section, gpa: Allocator) void {
301 for (sec.units.items) |*unit| unit.deinit(gpa);302 for (sec.units.items) |*unit| unit.deinit(gpa);
302 sec.units.deinit(gpa);303 sec.units.deinit(gpa);
303 sec.* = undefined;304 sec.* = undefined;
...@@ -357,7 +358,7 @@ pub const Section = struct {...@@ -357,7 +358,7 @@ pub const Section = struct {
357 if (sec.last == unit.toOptional()) sec.last = unit_ptr.prev;358 if (sec.last == unit.toOptional()) sec.last = unit_ptr.prev;
358 }359 }
359360
360 fn popUnit(sec: *Section, gpa: std.mem.Allocator) void {361 fn popUnit(sec: *Section, gpa: Allocator) void {
361 const unit_index: Unit.Index = @enumFromInt(sec.units.items.len - 1);362 const unit_index: Unit.Index = @enumFromInt(sec.units.items.len - 1);
362 sec.unlinkUnit(unit_index);363 sec.unlinkUnit(unit_index);
363 var unit = sec.units.pop().?;364 var unit = sec.units.pop().?;
...@@ -518,7 +519,7 @@ const Unit = struct {...@@ -518,7 +519,7 @@ const Unit = struct {
518 unit.cross_section_relocs.clearRetainingCapacity();519 unit.cross_section_relocs.clearRetainingCapacity();
519 }520 }
520521
521 fn deinit(unit: *Unit, gpa: std.mem.Allocator) void {522 fn deinit(unit: *Unit, gpa: Allocator) void {
522 for (unit.entries.items) |*entry| entry.deinit(gpa);523 for (unit.entries.items) |*entry| entry.deinit(gpa);
523 unit.entries.deinit(gpa);524 unit.entries.deinit(gpa);
524 unit.cross_unit_relocs.deinit(gpa);525 unit.cross_unit_relocs.deinit(gpa);
...@@ -526,7 +527,7 @@ const Unit = struct {...@@ -526,7 +527,7 @@ const Unit = struct {
526 unit.* = undefined;527 unit.* = undefined;
527 }528 }
528529
529 fn addEntry(unit: *Unit, gpa: std.mem.Allocator) std.mem.Allocator.Error!Entry.Index {530 fn addEntry(unit: *Unit, gpa: Allocator) Allocator.Error!Entry.Index {
530 if (unit.free.unwrap()) |entry| {531 if (unit.free.unwrap()) |entry| {
531 const entry_ptr = unit.getEntry(entry);532 const entry_ptr = unit.getEntry(entry);
532 unit.free = entry_ptr.next;533 unit.free = entry_ptr.next;
...@@ -780,7 +781,7 @@ const Entry = struct {...@@ -780,7 +781,7 @@ const Entry = struct {
780 entry.external_relocs.clearRetainingCapacity();781 entry.external_relocs.clearRetainingCapacity();
781 }782 }
782783
783 fn deinit(entry: *Entry, gpa: std.mem.Allocator) void {784 fn deinit(entry: *Entry, gpa: Allocator) void {
784 entry.cross_entry_relocs.deinit(gpa);785 entry.cross_entry_relocs.deinit(gpa);
785 entry.cross_unit_relocs.deinit(gpa);786 entry.cross_unit_relocs.deinit(gpa);
786 entry.cross_section_relocs.deinit(gpa);787 entry.cross_section_relocs.deinit(gpa);
...@@ -1133,7 +1134,7 @@ pub const Loc = union(enum) {...@@ -1133,7 +1134,7 @@ pub const Loc = union(enum) {
1133 };1134 };
1134 }1135 }
11351136
1136 fn writeReg(reg: u32, op0: u8, opx: u8, writer: anytype) @TypeOf(writer).Error!void {1137 fn writeReg(reg: u32, op0: u8, opx: u8, writer: anytype) !void {
1137 if (std.math.cast(u5, reg)) |small_reg| {1138 if (std.math.cast(u5, reg)) |small_reg| {
1138 try writer.writeByte(op0 + small_reg);1139 try writer.writeByte(op0 + small_reg);
1139 } else {1140 } else {
...@@ -1142,7 +1143,7 @@ pub const Loc = union(enum) {...@@ -1142,7 +1143,7 @@ pub const Loc = union(enum) {
1142 }1143 }
1143 }1144 }
11441145
1145 fn write(loc: Loc, adapter: anytype) UpdateError!void {1146 fn write(loc: Loc, adapter: anytype) !void {
1146 const writer = adapter.writer();1147 const writer = adapter.writer();
1147 switch (loc) {1148 switch (loc) {
1148 .empty => {},1149 .empty => {},
...@@ -1712,15 +1713,15 @@ pub const WipNav = struct {...@@ -1712,15 +1713,15 @@ pub const WipNav = struct {
1712 wip_nav.func = func;1713 wip_nav.func = func;
1713 }1714 }
17141715
1715 fn externalReloc(wip_nav: *WipNav, sec: *Section, reloc: ExternalReloc) std.mem.Allocator.Error!void {1716 fn externalReloc(wip_nav: *WipNav, sec: *Section, reloc: ExternalReloc) Allocator.Error!void {
1716 try sec.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(wip_nav.dwarf.gpa, reloc);1717 try sec.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(wip_nav.dwarf.gpa, reloc);
1717 }1718 }
17181719
1719 pub fn infoExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {1720 pub fn infoExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) Allocator.Error!void {
1720 try wip_nav.externalReloc(&wip_nav.dwarf.debug_info.section, reloc);1721 try wip_nav.externalReloc(&wip_nav.dwarf.debug_info.section, reloc);
1721 }1722 }
17221723
1723 fn frameExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {1724 fn frameExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) Allocator.Error!void {
1724 try wip_nav.externalReloc(&wip_nav.dwarf.debug_frame.section, reloc);1725 try wip_nav.externalReloc(&wip_nav.dwarf.debug_frame.section, reloc);
1725 }1726 }
17261727
...@@ -1768,33 +1769,33 @@ pub const WipNav = struct {...@@ -1768,33 +1769,33 @@ pub const WipNav = struct {
1768 }1769 }
17691770
1770 const ExprLocCounter = struct {1771 const ExprLocCounter = struct {
1771 const Stream = std.io.CountingWriter(std.io.NullWriter);1772 stream: Writer.Discarding,
1772 stream: Stream,
1773 section_offset_bytes: u32,1773 section_offset_bytes: u32,
1774 address_size: AddressSize,1774 address_size: AddressSize,
1775 fn init(dwarf: *Dwarf) ExprLocCounter {1775 fn init(dwarf: *Dwarf, trash_buffer: []u8) ExprLocCounter {
1776 return .{1776 return .{
1777 .stream = std.io.countingWriter(std.io.null_writer),1777 .stream = .init(trash_buffer),
1778 .section_offset_bytes = dwarf.sectionOffsetBytes(),1778 .section_offset_bytes = dwarf.sectionOffsetBytes(),
1779 .address_size = dwarf.address_size,1779 .address_size = dwarf.address_size,
1780 };1780 };
1781 }1781 }
1782 fn writer(counter: *ExprLocCounter) Stream.Writer {1782 fn writer(counter: *ExprLocCounter) *Writer {
1783 return counter.stream.writer();1783 return &counter.stream.writer;
1784 }1784 }
1785 fn endian(_: ExprLocCounter) std.builtin.Endian {1785 fn endian(_: ExprLocCounter) std.builtin.Endian {
1786 return @import("builtin").cpu.arch.endian();1786 return @import("builtin").cpu.arch.endian();
1787 }1787 }
1788 fn addrSym(counter: *ExprLocCounter, _: u32) error{}!void {1788 fn addrSym(counter: *ExprLocCounter, _: u32) error{}!void {
1789 counter.stream.bytes_written += @intFromEnum(counter.address_size);1789 counter.stream.count += @intFromEnum(counter.address_size);
1790 }1790 }
1791 fn infoEntry(counter: *ExprLocCounter, _: Unit.Index, _: Entry.Index) error{}!void {1791 fn infoEntry(counter: *ExprLocCounter, _: Unit.Index, _: Entry.Index) error{}!void {
1792 counter.stream.bytes_written += counter.section_offset_bytes;1792 counter.stream.count += counter.section_offset_bytes;
1793 }1793 }
1794 };1794 };
17951795
1796 fn infoExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {1796 fn infoExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1797 var counter: ExprLocCounter = .init(wip_nav.dwarf);1797 var trash_buffer: [64]u8 = undefined;
1798 var counter: ExprLocCounter = .init(wip_nav.dwarf, &trash_buffer);
1798 try loc.write(&counter);1799 try loc.write(&counter);
17991800
1800 const adapter: struct {1801 const adapter: struct {
...@@ -1812,7 +1813,7 @@ pub const WipNav = struct {...@@ -1812,7 +1813,7 @@ pub const WipNav = struct {
1812 try ctx.wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);1813 try ctx.wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
1813 }1814 }
1814 } = .{ .wip_nav = wip_nav };1815 } = .{ .wip_nav = wip_nav };
1815 try uleb128(adapter.writer(), counter.stream.bytes_written);1816 try uleb128(adapter.writer(), counter.stream.fullCount());
1816 try loc.write(adapter);1817 try loc.write(adapter);
1817 }1818 }
18181819
...@@ -1826,7 +1827,8 @@ pub const WipNav = struct {...@@ -1826,7 +1827,8 @@ pub const WipNav = struct {
1826 }1827 }
18271828
1828 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {1829 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1829 var counter: ExprLocCounter = .init(wip_nav.dwarf);1830 var trash_buffer: [64]u8 = undefined;
1831 var counter: ExprLocCounter = .init(wip_nav.dwarf, &trash_buffer);
1830 try loc.write(&counter);1832 try loc.write(&counter);
18311833
1832 const adapter: struct {1834 const adapter: struct {
...@@ -1844,7 +1846,7 @@ pub const WipNav = struct {...@@ -1844,7 +1846,7 @@ pub const WipNav = struct {
1844 try ctx.wip_nav.sectionOffset(.debug_frame, .debug_info, unit, entry, 0);1846 try ctx.wip_nav.sectionOffset(.debug_frame, .debug_info, unit, entry, 0);
1845 }1847 }
1846 } = .{ .wip_nav = wip_nav };1848 } = .{ .wip_nav = wip_nav };
1847 try uleb128(adapter.writer(), counter.stream.bytes_written);1849 try uleb128(adapter.writer(), counter.stream.fullCount());
1848 try loc.write(adapter);1850 try loc.write(adapter);
1849 }1851 }
18501852
...@@ -1922,7 +1924,7 @@ pub const WipNav = struct {...@@ -1922,7 +1924,7 @@ pub const WipNav = struct {
1922 try wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);1924 try wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
1923 }1925 }
19241926
1925 fn refForward(wip_nav: *WipNav) std.mem.Allocator.Error!u32 {1927 fn refForward(wip_nav: *WipNav) Allocator.Error!u32 {
1926 const dwarf = wip_nav.dwarf;1928 const dwarf = wip_nav.dwarf;
1927 const cross_entry_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs;1929 const cross_entry_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs;
1928 const reloc_index: u32 = @intCast(cross_entry_relocs.items.len);1930 const reloc_index: u32 = @intCast(cross_entry_relocs.items.len);
...@@ -6022,14 +6024,14 @@ fn sectionOffsetBytes(dwarf: *Dwarf) u32 {...@@ -6022,14 +6024,14 @@ fn sectionOffsetBytes(dwarf: *Dwarf) u32 {
60226024
6023fn uleb128Bytes(value: anytype) u32 {6025fn uleb128Bytes(value: anytype) u32 {
6024 var trash_buffer: [64]u8 = undefined;6026 var trash_buffer: [64]u8 = undefined;
6025 var d: std.Io.Writer.Discarding = .init(&trash_buffer);6027 var d: Writer.Discarding = .init(&trash_buffer);
6026 d.writer.writeUleb128(value) catch unreachable;6028 d.writer.writeUleb128(value) catch unreachable;
6027 return @intCast(d.count + d.writer.end);6029 return @intCast(d.count + d.writer.end);
6028}6030}
60296031
6030fn sleb128Bytes(value: anytype) u32 {6032fn sleb128Bytes(value: anytype) u32 {
6031 var trash_buffer: [64]u8 = undefined;6033 var trash_buffer: [64]u8 = undefined;
6032 var d: std.Io.Writer.Discarding = .init(&trash_buffer);6034 var d: Writer.Discarding = .init(&trash_buffer);
6033 d.writer.writeSleb128(value) catch unreachable;6035 d.writer.writeSleb128(value) catch unreachable;
6034 return @intCast(d.count + d.writer.end);6036 return @intCast(d.count + d.writer.end);
6035}6037}
...@@ -6057,3 +6059,5 @@ const sleb128 = std.leb.writeIleb128;...@@ -6057,3 +6059,5 @@ const sleb128 = std.leb.writeIleb128;
6057const std = @import("std");6059const std = @import("std");
6058const target_info = @import("../target.zig");6060const target_info = @import("../target.zig");
6059const uleb128 = std.leb.writeUleb128;6061const uleb128 = std.leb.writeUleb128;
6062const Allocator = std.mem.Allocator;
6063const Writer = std.Io.Writer;