authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-08-08 18:47:28-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-11 12:00:50-07:00
log60f8584927a1df4b08d1868ec4db5e6da4cad33d
treeededc4a075b9c8eac1ef2f033a1381086231b11e
parent38dfa6537ee4a2042e3a80d2e3020c9059ca16cc

Dwarf: port to new Writer API


2 files changed, 854 insertions(+), 627 deletions(-)

lib/std/Io/Writer.zig+22-28
......@@ -359,9 +359,12 @@ pub fn writableSlice(w: *Writer, len: usize) Error![]u8 {
359359///
360360/// If `minimum_length` is zero, this is equivalent to `unusedCapacitySlice`.
361361pub fn writableSliceGreedy(w: *Writer, minimum_length: usize) Error![]u8 {
362 assert(w.buffer.len >= minimum_length);
363362 while (w.buffer.len - w.end < minimum_length) {
364363 assert(0 == try w.vtable.drain(w, &.{""}, 1));
364 // If the loop condition was false this assertion would have passed
365 // anyway. Otherwise, give the implementation a chance to grow the
366 // buffer before asserting on the buffer length.
367 assert(w.buffer.len >= minimum_length);
365368 } else {
366369 @branchHint(.likely);
367370 return w.buffer[w.end..];
......@@ -1847,39 +1850,30 @@ pub fn writeLeb128(w: *Writer, value: anytype) Error!void {
18471850 const value_info = @typeInfo(@TypeOf(value)).int;
18481851 try w.writeMultipleOf7Leb128(@as(@Type(.{ .int = .{
18491852 .signedness = value_info.signedness,
1850 .bits = std.mem.alignForwardAnyAlign(u16, value_info.bits, 7),
1853 .bits = @max(std.mem.alignForwardAnyAlign(u16, value_info.bits, 7), 7),
18511854 } }), value));
18521855}
18531856
18541857fn writeMultipleOf7Leb128(w: *Writer, value: anytype) Error!void {
18551858 const value_info = @typeInfo(@TypeOf(value)).int;
1856 comptime assert(value_info.bits % 7 == 0);
1859 const Byte = packed struct(u8) { bits: u7, more: bool };
1860 var bytes: [@divExact(value_info.bits, 7)]Byte = undefined;
18571861 var remaining = value;
1858 while (true) {
1859 const buffer: []packed struct(u8) { bits: u7, more: bool } = @ptrCast(try w.writableSliceGreedy(1));
1860 for (buffer, 1..) |*byte, len| {
1861 const more = switch (value_info.signedness) {
1862 .signed => remaining >> 6 != remaining >> (value_info.bits - 1),
1863 .unsigned => remaining > std.math.maxInt(u7),
1864 };
1865 byte.* = if (@inComptime()) @typeInfo(@TypeOf(buffer)).pointer.child{
1866 .bits = @bitCast(@as(@Type(.{ .int = .{
1867 .signedness = value_info.signedness,
1868 .bits = 7,
1869 } }), @truncate(remaining))),
1870 .more = more,
1871 } else .{
1872 .bits = @bitCast(@as(@Type(.{ .int = .{
1873 .signedness = value_info.signedness,
1874 .bits = 7,
1875 } }), @truncate(remaining))),
1876 .more = more,
1877 };
1878 if (value_info.bits > 7) remaining >>= 7;
1879 if (!more) return w.advance(len);
1880 }
1881 w.advance(buffer.len);
1882 }
1862 for (&bytes, 1..) |*byte, len| {
1863 const more = switch (value_info.signedness) {
1864 .signed => remaining >> 6 != remaining >> (value_info.bits - 1),
1865 .unsigned => remaining > std.math.maxInt(u7),
1866 };
1867 byte.* = .{
1868 .bits = @bitCast(@as(@Type(.{ .int = .{
1869 .signedness = value_info.signedness,
1870 .bits = 7,
1871 } }), @truncate(remaining))),
1872 .more = more,
1873 };
1874 if (value_info.bits > 7) remaining >>= 7;
1875 if (!more) return w.writeAll(@ptrCast(bytes[0..len]));
1876 } else unreachable;
18831877}
18841878
18851879test "printValue max_depth" {
src/link/Dwarf.zig+832-599
......@@ -144,7 +144,9 @@ const DebugInfo = struct {
144144 debug_info.section.off(dwarf) + unit_ptr.off + unit_ptr.header_len + entry_ptr.off,
145145 ) != abbrev_code_buf.len) return error.InputOutput;
146146 var abbrev_code_reader: std.Io.Reader = .fixed(&abbrev_code_buf);
147 return @enumFromInt(abbrev_code_reader.takeLeb128(@typeInfo(AbbrevCode).@"enum".tag_type) catch unreachable);
147 return @enumFromInt(
148 abbrev_code_reader.takeLeb128(@typeInfo(AbbrevCode).@"enum".tag_type) catch unreachable,
149 );
148150 }
149151
150152 const trailer_bytes = 1 + 1;
......@@ -369,7 +371,13 @@ pub const Section = struct {
369371 return &sec.units.items[@intFromEnum(unit)];
370372 }
371373
372 fn resizeEntry(sec: *Section, unit: Unit.Index, entry: Entry.Index, dwarf: *Dwarf, len: u32) UpdateError!void {
374 fn resizeEntry(
375 sec: *Section,
376 unit: Unit.Index,
377 entry: Entry.Index,
378 dwarf: *Dwarf,
379 len: u32,
380 ) (UpdateError || Writer.Error)!void {
373381 const unit_ptr = sec.getUnit(unit);
374382 const entry_ptr = unit_ptr.getEntry(entry);
375383 if (len > 0) {
......@@ -390,13 +398,24 @@ pub const Section = struct {
390398 assert(entry_ptr.len == len);
391399 }
392400
393 fn replaceEntry(sec: *Section, unit: Unit.Index, entry: Entry.Index, dwarf: *Dwarf, contents: []const u8) UpdateError!void {
401 fn replaceEntry(
402 sec: *Section,
403 unit: Unit.Index,
404 entry: Entry.Index,
405 dwarf: *Dwarf,
406 contents: []const u8,
407 ) (UpdateError || Writer.Error)!void {
394408 try sec.resizeEntry(unit, entry, dwarf, @intCast(contents.len));
395409 const unit_ptr = sec.getUnit(unit);
396410 try unit_ptr.getEntry(entry).replace(unit_ptr, sec, dwarf, contents);
397411 }
398412
399 fn freeEntry(sec: *Section, unit: Unit.Index, entry: Entry.Index, dwarf: *Dwarf) UpdateError!void {
413 fn freeEntry(
414 sec: *Section,
415 unit: Unit.Index,
416 entry: Entry.Index,
417 dwarf: *Dwarf,
418 ) (UpdateError || Writer.Error)!void {
400419 const unit_ptr = sec.getUnit(unit);
401420 const entry_ptr = unit_ptr.getEntry(entry);
402421 if (entry_ptr.len > 0) {
......@@ -649,35 +668,37 @@ const Unit = struct {
649668 assert(len >= unit.trailer_len);
650669 if (sec == &dwarf.debug_line.section) {
651670 var buf: [1 + uleb128Bytes(std.math.maxInt(u32)) + 1]u8 = undefined;
652 var fbs = std.io.fixedBufferStream(&buf);
653 const writer = fbs.writer();
654 writer.writeByte(DW.LNS.extended_op) catch unreachable;
655 const extended_op_bytes = fbs.pos;
671 var fw: Writer = .fixed(&buf);
672 fw.writeByte(DW.LNS.extended_op) catch unreachable;
673 const extended_op_bytes = fw.end;
656674 var op_len_bytes: u5 = 1;
657675 while (true) switch (std.math.order(len - extended_op_bytes - op_len_bytes, @as(u32, 1) << 7 * op_len_bytes)) {
658 .lt => break uleb128(writer, len - extended_op_bytes - op_len_bytes) catch unreachable,
676 .lt => break fw.writeUleb128(len - extended_op_bytes - op_len_bytes) catch unreachable,
659677 .eq => {
660678 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
661679 op_len_bytes += 1;
662 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..op_len_bytes], len - extended_op_bytes - op_len_bytes);
663 fbs.pos += op_len_bytes;
680 std.leb.writeUnsignedExtended(
681 fw.writableSlice(op_len_bytes) catch unreachable,
682 len - extended_op_bytes - op_len_bytes,
683 );
664684 break;
665685 },
666686 .gt => op_len_bytes += 1,
667687 };
668 assert(fbs.pos == extended_op_bytes + op_len_bytes);
669 writer.writeByte(DW.LNE.padding) catch unreachable;
670 assert(fbs.pos >= unit.trailer_len and fbs.pos <= len);
671 return dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + start);
688 assert(fw.end == extended_op_bytes + op_len_bytes);
689 fw.writeByte(DW.LNE.padding) catch unreachable;
690 assert(fw.end >= unit.trailer_len and fw.end <= len);
691 return dwarf.getFile().?.pwriteAll(fw.buffered(), sec.off(dwarf) + start);
672692 }
673 var trailer = try std.ArrayList(u8).initCapacity(dwarf.gpa, len);
674 defer trailer.deinit();
693 var trailer_aw: Writer.Allocating = try .initCapacity(dwarf.gpa, len);
694 defer trailer_aw.deinit();
695 const tw = &trailer_aw.writer;
675696 const fill_byte: u8 = if (sec == &dwarf.debug_abbrev.section) fill: {
697 tw.writeUleb128(@intFromEnum(AbbrevCode.null)) catch unreachable;
676698 assert(uleb128Bytes(@intFromEnum(AbbrevCode.null)) == 1);
677 trailer.appendAssumeCapacity(@intFromEnum(AbbrevCode.null));
678699 break :fill @intFromEnum(AbbrevCode.null);
679700 } else if (sec == &dwarf.debug_aranges.section) fill: {
680 trailer.appendNTimesAssumeCapacity(0, @intFromEnum(dwarf.address_size) * 2);
701 tw.splatByteAll(0, @intFromEnum(dwarf.address_size) * 2) catch unreachable;
681702 break :fill 0;
682703 } else if (sec == &dwarf.debug_frame.section) fill: {
683704 switch (dwarf.debug_frame.header.format) {
......@@ -685,49 +706,49 @@ const Unit = struct {
685706 .debug_frame, .eh_frame => |format| {
686707 const unit_len = len - dwarf.unitLengthBytes();
687708 switch (dwarf.format) {
688 .@"32" => std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
709 .@"32" => tw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
689710 .@"64" => {
690 std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
691 std.mem.writeInt(u64, trailer.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
711 tw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
712 tw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
692713 },
693714 }
694715 switch (format) {
695716 .none => unreachable,
696717 .debug_frame => {
697718 switch (dwarf.format) {
698 .@"32" => std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian),
699 .@"64" => std.mem.writeInt(u64, trailer.addManyAsArrayAssumeCapacity(8), std.math.maxInt(u64), dwarf.endian),
719 .@"32" => tw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable,
720 .@"64" => tw.writeInt(u64, std.math.maxInt(u64), dwarf.endian) catch unreachable,
700721 }
701 trailer.appendAssumeCapacity(4);
702 trailer.appendSliceAssumeCapacity("\x00");
703 trailer.appendAssumeCapacity(@intFromEnum(dwarf.address_size));
704 trailer.appendAssumeCapacity(0);
722 tw.writeByte(4) catch unreachable;
723 tw.writeAll("\x00") catch unreachable;
724 tw.writeByte(@intFromEnum(dwarf.address_size)) catch unreachable;
725 tw.writeByte(0) catch unreachable;
705726 },
706727 .eh_frame => {
707 std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), 0, dwarf.endian);
708 trailer.appendAssumeCapacity(1);
709 trailer.appendSliceAssumeCapacity("\x00");
728 tw.writeInt(u32, 0, dwarf.endian) catch unreachable;
729 tw.writeByte(1) catch unreachable;
730 tw.writeAll("\x00") catch unreachable;
710731 },
711732 }
712 uleb128(trailer.fixedWriter(), 1) catch unreachable;
713 sleb128(trailer.fixedWriter(), 1) catch unreachable;
714 uleb128(trailer.fixedWriter(), 0) catch unreachable;
733 tw.writeUleb128(1) catch unreachable;
734 tw.writeSleb128(1) catch unreachable;
735 tw.writeUleb128(0) catch unreachable;
715736 },
716737 }
717 trailer.appendNTimesAssumeCapacity(DW.CFA.nop, unit.trailer_len - trailer.items.len);
738 tw.splatByteAll(DW.CFA.nop, unit.trailer_len - tw.end) catch unreachable;
718739 break :fill DW.CFA.nop;
719740 } else if (sec == &dwarf.debug_info.section) fill: {
741 for (0..2) |_| tw.writeUleb128(@intFromEnum(AbbrevCode.null)) catch unreachable;
720742 assert(uleb128Bytes(@intFromEnum(AbbrevCode.null)) == 1);
721 trailer.appendNTimesAssumeCapacity(@intFromEnum(AbbrevCode.null), 2);
722743 break :fill @intFromEnum(AbbrevCode.null);
723744 } else if (sec == &dwarf.debug_rnglists.section) fill: {
724 trailer.appendAssumeCapacity(DW.RLE.end_of_list);
745 tw.writeByte(DW.RLE.end_of_list) catch unreachable;
725746 break :fill DW.RLE.end_of_list;
726747 } else unreachable;
727 assert(trailer.items.len == unit.trailer_len);
728 trailer.appendNTimesAssumeCapacity(fill_byte, len - unit.trailer_len);
729 assert(trailer.items.len == len);
730 try dwarf.getFile().?.pwriteAll(trailer.items, sec.off(dwarf) + start);
748 assert(tw.end == unit.trailer_len);
749 tw.splatByteAll(fill_byte, len - unit.trailer_len) catch unreachable;
750 assert(tw.end == len);
751 try dwarf.getFile().?.pwriteAll(trailer_aw.getWritten(), sec.off(dwarf) + start);
731752 }
732753
733754 fn resolveRelocs(unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void {
......@@ -806,7 +827,12 @@ const Entry = struct {
806827 }
807828 };
808829
809 fn pad(entry: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf) UpdateError!void {
830 fn pad(
831 entry: *Entry,
832 unit: *Unit,
833 sec: *Section,
834 dwarf: *Dwarf,
835 ) (UpdateError || Writer.Error)!void {
810836 assert(entry.len > 0);
811837 const start = entry.off + entry.len;
812838 if (sec == &dwarf.debug_frame.section) {
......@@ -814,12 +840,10 @@ const Entry = struct {
814840 unit.getEntry(next_entry).off - entry.off
815841 else
816842 entry.len;
817 var unit_len: [8]u8 = undefined;
818 dwarf.writeInt(unit_len[0..dwarf.sectionOffsetBytes()], len - dwarf.unitLengthBytes());
819 try dwarf.getFile().?.pwriteAll(
820 unit_len[0..dwarf.sectionOffsetBytes()],
821 sec.off(dwarf) + unit.off + unit.header_len + entry.off,
822 );
843 var unit_len_buf: [8]u8 = undefined;
844 const unit_len_bytes = unit_len_buf[0..dwarf.sectionOffsetBytes()];
845 dwarf.writeInt(unit_len_bytes, len - dwarf.unitLengthBytes());
846 try dwarf.getFile().?.pwriteAll(unit_len_bytes, sec.off(dwarf) + unit.off + unit.header_len + entry.off);
823847 const buf = try dwarf.gpa.alloc(u8, len - entry.len);
824848 defer dwarf.gpa.free(buf);
825849 @memset(buf, DW.CFA.nop);
......@@ -834,55 +858,64 @@ const Entry = struct {
834858 1 + uleb128Bytes(std.math.maxInt(u32)) + 1,
835859 )
836860 ]u8 = undefined;
837 var fbs = std.io.fixedBufferStream(&buf);
838 const writer = fbs.writer();
861 var fw: Writer = .fixed(&buf);
839862 if (sec == &dwarf.debug_info.section) switch (len) {
840863 0 => {},
841 1 => uleb128(writer, try dwarf.refAbbrevCode(.pad_1)) catch unreachable,
864 1 => fw.writeUleb128(try dwarf.refAbbrevCode(.pad_1)) catch unreachable,
842865 else => {
843 uleb128(writer, try dwarf.refAbbrevCode(.pad_n)) catch unreachable;
844 const abbrev_code_bytes = fbs.pos;
866 fw.writeUleb128(try dwarf.refAbbrevCode(.pad_n)) catch unreachable;
867 const abbrev_code_bytes = fw.end;
845868 var block_len_bytes: u5 = 1;
846869 while (true) switch (std.math.order(len - abbrev_code_bytes - block_len_bytes, @as(u32, 1) << 7 * block_len_bytes)) {
847 .lt => break uleb128(writer, len - abbrev_code_bytes - block_len_bytes) catch unreachable,
870 .lt => break fw.writeUleb128(len - abbrev_code_bytes - block_len_bytes) catch unreachable,
848871 .eq => {
849872 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
850873 block_len_bytes += 1;
851 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..block_len_bytes], len - abbrev_code_bytes - block_len_bytes);
852 fbs.pos += block_len_bytes;
874 std.leb.writeUnsignedExtended(
875 fw.writableSlice(block_len_bytes) catch unreachable,
876 len - abbrev_code_bytes - block_len_bytes,
877 );
853878 break;
854879 },
855880 .gt => block_len_bytes += 1,
856881 };
857 assert(fbs.pos == abbrev_code_bytes + block_len_bytes);
882 assert(fw.end == abbrev_code_bytes + block_len_bytes);
858883 },
859884 } else if (sec == &dwarf.debug_line.section) switch (len) {
860885 0 => {},
861 1 => writer.writeByte(DW.LNS.const_add_pc) catch unreachable,
886 1 => fw.writeByte(DW.LNS.const_add_pc) catch unreachable,
862887 else => {
863 writer.writeByte(DW.LNS.extended_op) catch unreachable;
864 const extended_op_bytes = fbs.pos;
888 fw.writeByte(DW.LNS.extended_op) catch unreachable;
889 const extended_op_bytes = fw.end;
865890 var op_len_bytes: u5 = 1;
866891 while (true) switch (std.math.order(len - extended_op_bytes - op_len_bytes, @as(u32, 1) << 7 * op_len_bytes)) {
867 .lt => break uleb128(writer, len - extended_op_bytes - op_len_bytes) catch unreachable,
892 .lt => break fw.writeUleb128(len - extended_op_bytes - op_len_bytes) catch unreachable,
868893 .eq => {
869894 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
870895 op_len_bytes += 1;
871 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..op_len_bytes], len - extended_op_bytes - op_len_bytes);
872 fbs.pos += op_len_bytes;
896 std.leb.writeUnsignedExtended(
897 fw.writableSlice(op_len_bytes) catch unreachable,
898 len - extended_op_bytes - op_len_bytes,
899 );
873900 break;
874901 },
875902 .gt => op_len_bytes += 1,
876903 };
877 assert(fbs.pos == extended_op_bytes + op_len_bytes);
878 if (len > 2) writer.writeByte(DW.LNE.padding) catch unreachable;
904 assert(fw.end == extended_op_bytes + op_len_bytes);
905 if (len > 2) fw.writeByte(DW.LNE.padding) catch unreachable;
879906 },
880907 } else assert(!sec.pad_entries_to_ideal and len == 0);
881 assert(fbs.pos <= len);
882 try dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + unit.off + unit.header_len + start);
908 assert(fw.end <= len);
909 try dwarf.getFile().?.pwriteAll(fw.buffered(), sec.off(dwarf) + unit.off + unit.header_len + start);
883910 }
884911
885 fn resize(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, len: u32) UpdateError!void {
912 fn resize(
913 entry_ptr: *Entry,
914 unit: *Unit,
915 sec: *Section,
916 dwarf: *Dwarf,
917 len: u32,
918 ) (UpdateError || Writer.Error)!void {
886919 assert(len > 0);
887920 assert(sec.alignment.check(len));
888921 if (entry_ptr.len == len) return;
......@@ -1134,16 +1167,16 @@ pub const Loc = union(enum) {
11341167 };
11351168 }
11361169
1137 fn writeReg(reg: u32, op0: u8, opx: u8, writer: anytype) !void {
1170 fn writeReg(reg: u32, op0: u8, opx: u8, writer: *Writer) Writer.Error!void {
11381171 if (std.math.cast(u5, reg)) |small_reg| {
11391172 try writer.writeByte(op0 + small_reg);
11401173 } else {
11411174 try writer.writeByte(opx);
1142 try uleb128(writer, reg);
1175 try writer.writeUleb128(reg);
11431176 }
11441177 }
11451178
1146 fn write(loc: Loc, adapter: anytype) !void {
1179 fn write(loc: Loc, adapter: anytype) (UpdateError || Writer.Error)!void {
11471180 const writer = adapter.writer();
11481181 switch (loc) {
11491182 .empty => {},
......@@ -1164,13 +1197,13 @@ pub const Loc = union(enum) {
11641197 try writer.writeInt(u16, const2u, adapter.endian());
11651198 } else if (std.math.cast(u21, constu)) |const3u| {
11661199 try writer.writeByte(DW.OP.constu);
1167 try uleb128(writer, const3u);
1200 try writer.writeUleb128(const3u);
11681201 } else if (std.math.cast(u32, constu)) |const4u| {
11691202 try writer.writeByte(DW.OP.const4u);
11701203 try writer.writeInt(u32, const4u, adapter.endian());
11711204 } else if (std.math.cast(u49, constu)) |const7u| {
11721205 try writer.writeByte(DW.OP.constu);
1173 try uleb128(writer, const7u);
1206 try writer.writeUleb128(const7u);
11741207 } else {
11751208 try writer.writeByte(DW.OP.const8u);
11761209 try writer.writeInt(u64, constu, adapter.endian());
......@@ -1182,13 +1215,13 @@ pub const Loc = union(enum) {
11821215 try writer.writeInt(i16, const2s, adapter.endian());
11831216 } else if (std.math.cast(i21, consts)) |const3s| {
11841217 try writer.writeByte(DW.OP.consts);
1185 try sleb128(writer, const3s);
1218 try writer.writeSleb128(const3s);
11861219 } else if (std.math.cast(i32, consts)) |const4s| {
11871220 try writer.writeByte(DW.OP.const4s);
11881221 try writer.writeInt(i32, const4s, adapter.endian());
11891222 } else if (std.math.cast(i49, consts)) |const7s| {
11901223 try writer.writeByte(DW.OP.consts);
1191 try sleb128(writer, const7s);
1224 try writer.writeSleb128(const7s);
11921225 } else {
11931226 try writer.writeByte(DW.OP.const8s);
11941227 try writer.writeInt(i64, consts, adapter.endian());
......@@ -1205,27 +1238,27 @@ pub const Loc = union(enum) {
12051238 if (plus[0].getBaseReg()) |breg| {
12061239 if (plus[1].getConst(i65)) |offset| {
12071240 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1208 try sleb128(writer, offset);
1241 try writer.writeSleb128(offset);
12091242 break :done;
12101243 }
12111244 }
12121245 if (plus[1].getBaseReg()) |breg| {
12131246 if (plus[0].getConst(i65)) |offset| {
12141247 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1215 try sleb128(writer, offset);
1248 try writer.writeSleb128(offset);
12161249 break :done;
12171250 }
12181251 }
12191252 if (plus[0].getConst(u64)) |uconst| {
12201253 try plus[1].write(adapter);
12211254 try writer.writeByte(DW.OP.plus_uconst);
1222 try uleb128(writer, uconst);
1255 try writer.writeUleb128(uconst);
12231256 break :done;
12241257 }
12251258 if (plus[1].getConst(u64)) |uconst| {
12261259 try plus[0].write(adapter);
12271260 try writer.writeByte(DW.OP.plus_uconst);
1228 try uleb128(writer, uconst);
1261 try writer.writeUleb128(uconst);
12291262 break :done;
12301263 }
12311264 try plus[0].write(adapter);
......@@ -1235,7 +1268,7 @@ pub const Loc = union(enum) {
12351268 .reg => |reg| try writeReg(reg, DW.OP.reg0, DW.OP.regx, writer),
12361269 .breg => |breg| {
12371270 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1238 try sleb128(writer, 0);
1271 try writer.writeSleb128(0);
12391272 },
12401273 .push_object_address => try writer.writeByte(DW.OP.push_object_address),
12411274 .call => |call| {
......@@ -1249,7 +1282,7 @@ pub const Loc = union(enum) {
12491282 },
12501283 .implicit_value => |value| {
12511284 try writer.writeByte(DW.OP.implicit_value);
1252 try uleb128(writer, value.len);
1285 try writer.writeUleb128(value.len);
12531286 try writer.writeAll(value);
12541287 },
12551288 .stack_value => |value| {
......@@ -1259,25 +1292,25 @@ pub const Loc = union(enum) {
12591292 .implicit_pointer => |implicit_pointer| {
12601293 try writer.writeByte(DW.OP.implicit_pointer);
12611294 try adapter.infoEntry(implicit_pointer.unit, implicit_pointer.entry);
1262 try sleb128(writer, implicit_pointer.offset);
1295 try writer.writeSleb128(implicit_pointer.offset);
12631296 },
12641297 .wasm_ext => |wasm_ext| {
12651298 try writer.writeByte(DW.OP.WASM_location);
12661299 switch (wasm_ext) {
12671300 .local => |local| {
12681301 try writer.writeByte(DW.OP.WASM_local);
1269 try uleb128(writer, local);
1302 try writer.writeUleb128(local);
12701303 },
12711304 .global => |global| if (std.math.cast(u21, global)) |global_u21| {
12721305 try writer.writeByte(DW.OP.WASM_global);
1273 try uleb128(writer, global_u21);
1306 try writer.writeUleb128(global_u21);
12741307 } else {
12751308 try writer.writeByte(DW.OP.WASM_global_u32);
12761309 try writer.writeInt(u32, global, adapter.endian());
12771310 },
12781311 .operand_stack => |operand_stack| {
12791312 try writer.writeByte(DW.OP.WASM_operand_stack);
1280 try uleb128(writer, operand_stack);
1313 try writer.writeUleb128(operand_stack);
12811314 },
12821315 }
12831316 },
......@@ -1309,22 +1342,22 @@ pub const Cfa = union(enum) {
13091342 const RegOff = struct { reg: u32, off: i64 };
13101343 const RegExpr = struct { reg: u32, expr: Loc };
13111344
1312 fn write(cfa: Cfa, wip_nav: *WipNav) UpdateError!void {
1313 const writer = wip_nav.debug_frame.writer(wip_nav.dwarf.gpa);
1345 fn write(cfa: Cfa, wip_nav: *WipNav) (UpdateError || Writer.Error)!void {
1346 const dfw = &wip_nav.debug_frame.writer;
13141347 switch (cfa) {
1315 .nop => try writer.writeByte(DW.CFA.nop),
1348 .nop => try dfw.writeByte(DW.CFA.nop),
13161349 .advance_loc => |loc| {
13171350 const delta = @divExact(loc - wip_nav.cfi.loc, wip_nav.dwarf.debug_frame.header.code_alignment_factor);
13181351 if (delta == 0) {} else if (std.math.cast(u6, delta)) |small_delta|
1319 try writer.writeByte(@as(u8, DW.CFA.advance_loc) + small_delta)
1352 try dfw.writeByte(@as(u8, DW.CFA.advance_loc) + small_delta)
13201353 else if (std.math.cast(u8, delta)) |ubyte_delta|
1321 try writer.writeAll(&.{ DW.CFA.advance_loc1, ubyte_delta })
1354 try dfw.writeAll(&.{ DW.CFA.advance_loc1, ubyte_delta })
13221355 else if (std.math.cast(u16, delta)) |uhalf_delta| {
1323 try writer.writeByte(DW.CFA.advance_loc2);
1324 try writer.writeInt(u16, uhalf_delta, wip_nav.dwarf.endian);
1356 try dfw.writeByte(DW.CFA.advance_loc2);
1357 try dfw.writeInt(u16, uhalf_delta, wip_nav.dwarf.endian);
13251358 } else if (std.math.cast(u32, delta)) |uword_delta| {
1326 try writer.writeByte(DW.CFA.advance_loc4);
1327 try writer.writeInt(u32, uword_delta, wip_nav.dwarf.endian);
1359 try dfw.writeByte(DW.CFA.advance_loc4);
1360 try dfw.writeInt(u32, uword_delta, wip_nav.dwarf.endian);
13281361 }
13291362 wip_nav.cfi.loc = loc;
13301363 },
......@@ -1336,41 +1369,41 @@ pub const Cfa = union(enum) {
13361369 }, wip_nav.dwarf.debug_frame.header.data_alignment_factor);
13371370 if (std.math.cast(u63, factored_off)) |unsigned_off| {
13381371 if (std.math.cast(u6, reg_off.reg)) |small_reg| {
1339 try writer.writeByte(@as(u8, DW.CFA.offset) + small_reg);
1372 try dfw.writeByte(@as(u8, DW.CFA.offset) + small_reg);
13401373 } else {
1341 try writer.writeByte(DW.CFA.offset_extended);
1342 try uleb128(writer, reg_off.reg);
1374 try dfw.writeByte(DW.CFA.offset_extended);
1375 try dfw.writeUleb128(reg_off.reg);
13431376 }
1344 try uleb128(writer, unsigned_off);
1377 try dfw.writeUleb128(unsigned_off);
13451378 } else {
1346 try writer.writeByte(DW.CFA.offset_extended_sf);
1347 try uleb128(writer, reg_off.reg);
1348 try sleb128(writer, factored_off);
1379 try dfw.writeByte(DW.CFA.offset_extended_sf);
1380 try dfw.writeUleb128(reg_off.reg);
1381 try dfw.writeSleb128(factored_off);
13491382 }
13501383 },
13511384 .restore => |reg| if (std.math.cast(u6, reg)) |small_reg|
1352 try writer.writeByte(@as(u8, DW.CFA.restore) + small_reg)
1385 try dfw.writeByte(@as(u8, DW.CFA.restore) + small_reg)
13531386 else {
1354 try writer.writeByte(DW.CFA.restore_extended);
1355 try uleb128(writer, reg);
1387 try dfw.writeByte(DW.CFA.restore_extended);
1388 try dfw.writeUleb128(reg);
13561389 },
13571390 .undefined => |reg| {
1358 try writer.writeByte(DW.CFA.undefined);
1359 try uleb128(writer, reg);
1391 try dfw.writeByte(DW.CFA.undefined);
1392 try dfw.writeUleb128(reg);
13601393 },
13611394 .same_value => |reg| {
1362 try writer.writeByte(DW.CFA.same_value);
1363 try uleb128(writer, reg);
1395 try dfw.writeByte(DW.CFA.same_value);
1396 try dfw.writeUleb128(reg);
13641397 },
13651398 .register => |regs| if (regs[0] != regs[1]) {
1366 try writer.writeByte(DW.CFA.register);
1367 for (regs) |reg| try uleb128(writer, reg);
1399 try dfw.writeByte(DW.CFA.register);
1400 for (regs) |reg| try dfw.writeUleb128(reg);
13681401 } else {
1369 try writer.writeByte(DW.CFA.same_value);
1370 try uleb128(writer, regs[0]);
1402 try dfw.writeByte(DW.CFA.same_value);
1403 try dfw.writeUleb128(regs[0]);
13711404 },
1372 .remember_state => try writer.writeByte(DW.CFA.remember_state),
1373 .restore_state => try writer.writeByte(DW.CFA.restore_state),
1405 .remember_state => try dfw.writeByte(DW.CFA.remember_state),
1406 .restore_state => try dfw.writeByte(DW.CFA.restore_state),
13741407 .def_cfa, .def_cfa_register, .def_cfa_offset, .adjust_cfa_offset => {
13751408 const reg_off: RegOff = switch (cfa) {
13761409 else => unreachable,
......@@ -1383,51 +1416,51 @@ pub const Cfa = union(enum) {
13831416 const unsigned_off = std.math.cast(u63, reg_off.off);
13841417 if (reg_off.off == wip_nav.cfi.cfa.off) {
13851418 if (changed_reg) {
1386 try writer.writeByte(DW.CFA.def_cfa_register);
1387 try uleb128(writer, reg_off.reg);
1419 try dfw.writeByte(DW.CFA.def_cfa_register);
1420 try dfw.writeUleb128(reg_off.reg);
13881421 }
13891422 } else if (switch (wip_nav.dwarf.debug_frame.header.data_alignment_factor) {
13901423 0 => unreachable,
13911424 1 => unsigned_off != null,
13921425 else => |data_alignment_factor| @rem(reg_off.off, data_alignment_factor) != 0,
13931426 }) {
1394 try writer.writeByte(if (changed_reg) DW.CFA.def_cfa else DW.CFA.def_cfa_offset);
1395 if (changed_reg) try uleb128(writer, reg_off.reg);
1396 try uleb128(writer, unsigned_off.?);
1427 try dfw.writeByte(if (changed_reg) DW.CFA.def_cfa else DW.CFA.def_cfa_offset);
1428 if (changed_reg) try dfw.writeUleb128(reg_off.reg);
1429 try dfw.writeUleb128(unsigned_off.?);
13971430 } else {
1398 try writer.writeByte(if (changed_reg) DW.CFA.def_cfa_sf else DW.CFA.def_cfa_offset_sf);
1399 if (changed_reg) try uleb128(writer, reg_off.reg);
1400 try sleb128(writer, @divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor));
1431 try dfw.writeByte(if (changed_reg) DW.CFA.def_cfa_sf else DW.CFA.def_cfa_offset_sf);
1432 if (changed_reg) try dfw.writeUleb128(reg_off.reg);
1433 try dfw.writeSleb128(@divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor));
14011434 }
14021435 wip_nav.cfi.cfa = reg_off;
14031436 },
14041437 .def_cfa_expression => |expr| {
1405 try writer.writeByte(DW.CFA.def_cfa_expression);
1438 try dfw.writeByte(DW.CFA.def_cfa_expression);
14061439 try wip_nav.frameExprLoc(expr);
14071440 },
14081441 .expression => |reg_expr| {
1409 try writer.writeByte(DW.CFA.expression);
1410 try uleb128(writer, reg_expr.reg);
1442 try dfw.writeByte(DW.CFA.expression);
1443 try dfw.writeUleb128(reg_expr.reg);
14111444 try wip_nav.frameExprLoc(reg_expr.expr);
14121445 },
14131446 .val_offset => |reg_off| {
14141447 const factored_off = @divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor);
14151448 if (std.math.cast(u63, factored_off)) |unsigned_off| {
1416 try writer.writeByte(DW.CFA.val_offset);
1417 try uleb128(writer, reg_off.reg);
1418 try uleb128(writer, unsigned_off);
1449 try dfw.writeByte(DW.CFA.val_offset);
1450 try dfw.writeUleb128(reg_off.reg);
1451 try dfw.writeUleb128(unsigned_off);
14191452 } else {
1420 try writer.writeByte(DW.CFA.val_offset_sf);
1421 try uleb128(writer, reg_off.reg);
1422 try sleb128(writer, factored_off);
1453 try dfw.writeByte(DW.CFA.val_offset_sf);
1454 try dfw.writeUleb128(reg_off.reg);
1455 try dfw.writeSleb128(factored_off);
14231456 }
14241457 },
14251458 .val_expression => |reg_expr| {
1426 try writer.writeByte(DW.CFA.val_expression);
1427 try uleb128(writer, reg_expr.reg);
1459 try dfw.writeByte(DW.CFA.val_expression);
1460 try dfw.writeUleb128(reg_expr.reg);
14281461 try wip_nav.frameExprLoc(reg_expr.expr);
14291462 },
1430 .escape => |bytes| try writer.writeAll(bytes),
1463 .escape => |bytes| try dfw.writeAll(bytes),
14311464 }
14321465 }
14331466};
......@@ -1450,24 +1483,30 @@ pub const WipNav = struct {
14501483 loc: u32,
14511484 cfa: Cfa.RegOff,
14521485 },
1453 debug_frame: std.ArrayListUnmanaged(u8),
1454 debug_info: std.ArrayListUnmanaged(u8),
1455 debug_line: std.ArrayListUnmanaged(u8),
1456 debug_loclists: std.ArrayListUnmanaged(u8),
1486 debug_frame: Writer.Allocating,
1487 debug_info: Writer.Allocating,
1488 debug_line: Writer.Allocating,
1489 debug_loclists: Writer.Allocating,
14571490 pending_lazy: PendingLazy,
14581491
14591492 pub fn deinit(wip_nav: *WipNav) void {
14601493 const gpa = wip_nav.dwarf.gpa;
14611494 if (wip_nav.func != .none) wip_nav.blocks.deinit(gpa);
1462 wip_nav.debug_frame.deinit(gpa);
1463 wip_nav.debug_info.deinit(gpa);
1464 wip_nav.debug_line.deinit(gpa);
1465 wip_nav.debug_loclists.deinit(gpa);
1495 wip_nav.debug_frame.deinit();
1496 wip_nav.debug_info.deinit();
1497 wip_nav.debug_line.deinit();
1498 wip_nav.debug_loclists.deinit();
14661499 wip_nav.pending_lazy.types.deinit(gpa);
14671500 wip_nav.pending_lazy.values.deinit(gpa);
14681501 }
14691502
14701503 pub fn genDebugFrame(wip_nav: *WipNav, loc: u32, cfa: Cfa) UpdateError!void {
1504 return wip_nav.genDebugFrameWriterError(loc, cfa) catch |err| switch (err) {
1505 error.WriteFailed => error.OutOfMemory,
1506 else => |e| e,
1507 };
1508 }
1509 fn genDebugFrameWriterError(wip_nav: *WipNav, loc: u32, cfa: Cfa) (UpdateError || Writer.Error)!void {
14711510 assert(wip_nav.func != .none);
14721511 if (wip_nav.dwarf.debug_frame.header.format == .none) return;
14731512 const loc_cfa: Cfa = .{ .advance_loc = loc };
......@@ -1483,6 +1522,18 @@ pub const WipNav = struct {
14831522 ty: Type,
14841523 loc: Loc,
14851524 ) UpdateError!void {
1525 return wip_nav.genLocalVarDebugInfoWriterError(tag, opt_name, ty, loc) catch |err| switch (err) {
1526 error.WriteFailed => error.OutOfMemory,
1527 else => |e| e,
1528 };
1529 }
1530 fn genLocalVarDebugInfoWriterError(
1531 wip_nav: *WipNav,
1532 tag: LocalVarTag,
1533 opt_name: ?[]const u8,
1534 ty: Type,
1535 loc: Loc,
1536 ) (UpdateError || Writer.Error)!void {
14861537 assert(wip_nav.func != .none);
14871538 try wip_nav.abbrevCode(switch (tag) {
14881539 .arg => if (opt_name) |_| .arg else .unnamed_arg,
......@@ -1502,6 +1553,18 @@ pub const WipNav = struct {
15021553 opt_name: ?[]const u8,
15031554 val: Value,
15041555 ) UpdateError!void {
1556 return wip_nav.genLocalConstDebugInfoWriterError(src_loc, tag, opt_name, val) catch |err| switch (err) {
1557 error.WriteFailed => error.OutOfMemory,
1558 else => |e| e,
1559 };
1560 }
1561 fn genLocalConstDebugInfoWriterError(
1562 wip_nav: *WipNav,
1563 src_loc: Zcu.LazySrcLoc,
1564 tag: LocalConstTag,
1565 opt_name: ?[]const u8,
1566 val: Value,
1567 ) (UpdateError || Writer.Error)!void {
15051568 assert(wip_nav.func != .none);
15061569 const pt = wip_nav.pt;
15071570 const zcu = pt.zcu;
......@@ -1529,17 +1592,28 @@ pub const WipNav = struct {
15291592 }
15301593
15311594 pub fn genVarArgsDebugInfo(wip_nav: *WipNav) UpdateError!void {
1595 return wip_nav.genVarArgsDebugInfoWriterError() catch |err| switch (err) {
1596 error.WriteFailed => error.OutOfMemory,
1597 else => |e| e,
1598 };
1599 }
1600 fn genVarArgsDebugInfoWriterError(wip_nav: *WipNav) (UpdateError || Writer.Error)!void {
15321601 assert(wip_nav.func != .none);
15331602 try wip_nav.abbrevCode(.is_var_args);
15341603 wip_nav.any_children = true;
15351604 }
15361605
1537 pub fn advancePCAndLine(
1606 pub fn advancePCAndLine(wip_nav: *WipNav, delta_line: i33, delta_pc: u64) Allocator.Error!void {
1607 return wip_nav.advancePCAndLineWriterError(delta_line, delta_pc) catch |err| switch (err) {
1608 error.WriteFailed => error.OutOfMemory,
1609 };
1610 }
1611 fn advancePCAndLineWriterError(
15381612 wip_nav: *WipNav,
15391613 delta_line: i33,
15401614 delta_pc: u64,
1541 ) error{OutOfMemory}!void {
1542 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1615 ) Writer.Error!void {
1616 const dlw = &wip_nav.debug_line.writer;
15431617
15441618 const header = wip_nav.dwarf.debug_line.header;
15451619 assert(header.maximum_operations_per_instruction == 1);
......@@ -1550,7 +1624,7 @@ pub const WipNav = struct {
15501624 remaining: {
15511625 assert(delta_line != 0);
15521626 try dlw.writeByte(DW.LNS.advance_line);
1553 try sleb128(dlw, delta_line);
1627 try dlw.writeSleb128(delta_line);
15541628 break :remaining 0;
15551629 } else delta_line);
15561630
......@@ -1559,7 +1633,7 @@ pub const WipNav = struct {
15591633 const max_op_advance: u9 = (std.math.maxInt(u8) - header.opcode_base) / header.line_range;
15601634 const remaining_op_advance: u8 = @intCast(if (op_advance >= 2 * max_op_advance) remaining: {
15611635 try dlw.writeByte(DW.LNS.advance_pc);
1562 try uleb128(dlw, op_advance);
1636 try dlw.writeUleb128(op_advance);
15631637 break :remaining 0;
15641638 } else if (op_advance >= max_op_advance) remaining: {
15651639 try dlw.writeByte(DW.LNS.const_add_pc);
......@@ -1573,53 +1647,82 @@ pub const WipNav = struct {
15731647 (header.line_range * remaining_op_advance) + header.opcode_base));
15741648 }
15751649
1576 pub fn setColumn(wip_nav: *WipNav, column: u32) error{OutOfMemory}!void {
1577 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1650 pub fn setColumn(wip_nav: *WipNav, column: u32) Allocator.Error!void {
1651 return wip_nav.setColumnWriterError(column) catch |err| switch (err) {
1652 error.WriteFailed => error.OutOfMemory,
1653 };
1654 }
1655 fn setColumnWriterError(wip_nav: *WipNav, column: u32) Writer.Error!void {
1656 const dlw = &wip_nav.debug_line.writer;
15781657 try dlw.writeByte(DW.LNS.set_column);
1579 try uleb128(dlw, column + 1);
1658 try dlw.writeUleb128(column + 1);
15801659 }
15811660
1582 pub fn negateStmt(wip_nav: *WipNav) error{OutOfMemory}!void {
1583 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1584 try dlw.writeByte(DW.LNS.negate_stmt);
1661 pub fn negateStmt(wip_nav: *WipNav) Allocator.Error!void {
1662 return wip_nav.negateStmtWriterError() catch |err| switch (err) {
1663 error.WriteFailed => error.OutOfMemory,
1664 };
1665 }
1666 fn negateStmtWriterError(wip_nav: *WipNav) Writer.Error!void {
1667 try wip_nav.debug_line.writer.writeByte(DW.LNS.negate_stmt);
15851668 }
15861669
1587 pub fn setPrologueEnd(wip_nav: *WipNav) error{OutOfMemory}!void {
1588 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1589 try dlw.writeByte(DW.LNS.set_prologue_end);
1670 pub fn setPrologueEnd(wip_nav: *WipNav) Allocator.Error!void {
1671 return wip_nav.setPrologueEndWriterError() catch |err| switch (err) {
1672 error.WriteFailed => error.OutOfMemory,
1673 };
1674 }
1675 fn setPrologueEndWriterError(wip_nav: *WipNav) Writer.Error!void {
1676 try wip_nav.debug_line.writer.writeByte(DW.LNS.set_prologue_end);
15901677 }
15911678
1592 pub fn setEpilogueBegin(wip_nav: *WipNav) error{OutOfMemory}!void {
1593 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1594 try dlw.writeByte(DW.LNS.set_epilogue_begin);
1679 pub fn setEpilogueBegin(wip_nav: *WipNav) Allocator.Error!void {
1680 return wip_nav.setEpilogueBeginWriterError() catch |err| switch (err) {
1681 error.WriteFailed => error.OutOfMemory,
1682 };
1683 }
1684 fn setEpilogueBeginWriterError(wip_nav: *WipNav) Writer.Error!void {
1685 try wip_nav.debug_line.writer.writeByte(DW.LNS.set_epilogue_begin);
15951686 }
15961687
15971688 pub fn enterBlock(wip_nav: *WipNav, code_off: u64) UpdateError!void {
1689 return wip_nav.enterBlockWriterError(code_off) catch |err| switch (err) {
1690 error.WriteFailed => error.OutOfMemory,
1691 else => |e| e,
1692 };
1693 }
1694 fn enterBlockWriterError(wip_nav: *WipNav, code_off: u64) (UpdateError || Writer.Error)!void {
15981695 const dwarf = wip_nav.dwarf;
1599 const diw = wip_nav.debug_info.writer(dwarf.gpa);
1696 const diw = &wip_nav.debug_info.writer;
16001697 const block = try wip_nav.blocks.addOne(dwarf.gpa);
16011698
1602 block.abbrev_code = @intCast(wip_nav.debug_info.items.len);
1699 block.abbrev_code = @intCast(diw.end);
16031700 try wip_nav.abbrevCode(.block);
16041701 block.low_pc_off = code_off;
16051702 try wip_nav.infoAddrSym(wip_nav.func_sym_index, code_off);
1606 block.high_pc = @intCast(wip_nav.debug_info.items.len);
1703 block.high_pc = @intCast(diw.end);
16071704 try diw.writeInt(u32, 0, dwarf.endian);
16081705 wip_nav.any_children = false;
16091706 }
16101707
16111708 pub fn leaveBlock(wip_nav: *WipNav, code_off: u64) UpdateError!void {
1709 return wip_nav.leaveBlockWriterError(code_off) catch |err| switch (err) {
1710 error.WriteFailed => error.OutOfMemory,
1711 else => |e| e,
1712 };
1713 }
1714 fn leaveBlockWriterError(wip_nav: *WipNav, code_off: u64) (UpdateError || Writer.Error)!void {
16121715 const block_bytes = comptime uleb128Bytes(@intFromEnum(AbbrevCode.block));
16131716 const block = wip_nav.blocks.pop().?;
16141717 if (wip_nav.any_children)
1615 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), @intFromEnum(AbbrevCode.null))
1718 try wip_nav.debug_info.writer.writeUleb128(@intFromEnum(AbbrevCode.null))
16161719 else
16171720 std.leb.writeUnsignedFixed(
16181721 block_bytes,
1619 wip_nav.debug_info.items[block.abbrev_code..][0..block_bytes],
1722 wip_nav.debug_info.getWritten()[block.abbrev_code..][0..block_bytes],
16201723 try wip_nav.dwarf.refAbbrevCode(.empty_block),
16211724 );
1622 std.mem.writeInt(u32, wip_nav.debug_info.items[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
1725 std.mem.writeInt(u32, wip_nav.debug_info.getWritten()[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
16231726 wip_nav.any_children = true;
16241727 }
16251728
......@@ -1630,41 +1733,69 @@ pub const WipNav = struct {
16301733 line: u32,
16311734 column: u32,
16321735 ) UpdateError!void {
1736 return wip_nav.enterInlineFuncWriterError(func, code_off, line, column) catch |err| switch (err) {
1737 error.WriteFailed => error.OutOfMemory,
1738 else => |e| e,
1739 };
1740 }
1741 fn enterInlineFuncWriterError(
1742 wip_nav: *WipNav,
1743 func: InternPool.Index,
1744 code_off: u64,
1745 line: u32,
1746 column: u32,
1747 ) (UpdateError || Writer.Error)!void {
16331748 const dwarf = wip_nav.dwarf;
16341749 const zcu = wip_nav.pt.zcu;
1635 const diw = wip_nav.debug_info.writer(dwarf.gpa);
1750 const diw = &wip_nav.debug_info.writer;
16361751 const block = try wip_nav.blocks.addOne(dwarf.gpa);
16371752
1638 block.abbrev_code = @intCast(wip_nav.debug_info.items.len);
1753 block.abbrev_code = @intCast(diw.end);
16391754 try wip_nav.abbrevCode(.inlined_func);
16401755 try wip_nav.refNav(zcu.funcInfo(func).owner_nav);
1641 try uleb128(diw, zcu.navSrcLine(zcu.funcInfo(wip_nav.func).owner_nav) + line + 1);
1642 try uleb128(diw, column + 1);
1756 try diw.writeUleb128(zcu.navSrcLine(zcu.funcInfo(wip_nav.func).owner_nav) + line + 1);
1757 try diw.writeUleb128(column + 1);
16431758 block.low_pc_off = code_off;
16441759 try wip_nav.infoAddrSym(wip_nav.func_sym_index, code_off);
1645 block.high_pc = @intCast(wip_nav.debug_info.items.len);
1760 block.high_pc = @intCast(diw.end);
16461761 try diw.writeInt(u32, 0, dwarf.endian);
16471762 try wip_nav.setInlineFunc(func);
16481763 wip_nav.any_children = false;
16491764 }
16501765
16511766 pub fn leaveInlineFunc(wip_nav: *WipNav, func: InternPool.Index, code_off: u64) UpdateError!void {
1767 return wip_nav.leaveInlineFuncWriterError(func, code_off) catch |err| switch (err) {
1768 error.WriteFailed => error.OutOfMemory,
1769 else => |e| e,
1770 };
1771 }
1772 fn leaveInlineFuncWriterError(
1773 wip_nav: *WipNav,
1774 func: InternPool.Index,
1775 code_off: u64,
1776 ) (UpdateError || Writer.Error)!void {
16521777 const inlined_func_bytes = comptime uleb128Bytes(@intFromEnum(AbbrevCode.inlined_func));
16531778 const block = wip_nav.blocks.pop().?;
16541779 if (wip_nav.any_children)
1655 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), @intFromEnum(AbbrevCode.null))
1780 try wip_nav.debug_info.writer.writeUleb128(@intFromEnum(AbbrevCode.null))
16561781 else
16571782 std.leb.writeUnsignedFixed(
16581783 inlined_func_bytes,
1659 wip_nav.debug_info.items[block.abbrev_code..][0..inlined_func_bytes],
1784 wip_nav.debug_info.getWritten()[block.abbrev_code..][0..inlined_func_bytes],
16601785 try wip_nav.dwarf.refAbbrevCode(.empty_inlined_func),
16611786 );
1662 std.mem.writeInt(u32, wip_nav.debug_info.items[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
1787 std.mem.writeInt(u32, wip_nav.debug_info.getWritten()[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
16631788 try wip_nav.setInlineFunc(func);
16641789 wip_nav.any_children = true;
16651790 }
16661791
16671792 pub fn setInlineFunc(wip_nav: *WipNav, func: InternPool.Index) UpdateError!void {
1793 return wip_nav.setInlineFuncWriterError(func) catch |err| switch (err) {
1794 error.WriteFailed => error.OutOfMemory,
1795 else => |e| e,
1796 };
1797 }
1798 fn setInlineFuncWriterError(wip_nav: *WipNav, func: InternPool.Index) (UpdateError || Writer.Error)!void {
16681799 const zcu = wip_nav.pt.zcu;
16691800 const dwarf = wip_nav.dwarf;
16701801 if (wip_nav.func == func) return;
......@@ -1673,22 +1804,22 @@ pub const WipNav = struct {
16731804 const new_file = zcu.navFileScopeIndex(new_func_info.owner_nav);
16741805 const new_unit = try dwarf.getUnit(zcu.fileByIndex(new_file).mod.?);
16751806
1676 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
1807 const dlw = &wip_nav.debug_line.writer;
16771808 if (dwarf.incremental()) {
16781809 const new_nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, new_func_info.owner_nav);
16791810 errdefer _ = if (!new_nav_gop.found_existing) dwarf.navs.pop();
16801811 if (!new_nav_gop.found_existing) new_nav_gop.value_ptr.* = try dwarf.addCommonEntry(new_unit);
16811812
16821813 try dlw.writeByte(DW.LNS.extended_op);
1683 try uleb128(dlw, 1 + dwarf.sectionOffsetBytes());
1814 try dlw.writeUleb128(1 + dwarf.sectionOffsetBytes());
16841815 try dlw.writeByte(DW.LNE.ZIG_set_decl);
16851816 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{
1686 .source_off = @intCast(wip_nav.debug_line.items.len),
1817 .source_off = @intCast(dlw.end),
16871818 .target_sec = .debug_info,
16881819 .target_unit = new_unit,
16891820 .target_entry = new_nav_gop.value_ptr.toOptional(),
16901821 });
1691 try dlw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
1822 try dlw.splatByteAll(0, dwarf.sectionOffsetBytes());
16921823 return;
16931824 }
16941825
......@@ -1700,14 +1831,14 @@ pub const WipNav = struct {
17001831 const file_gop = try mod_info.files.getOrPut(dwarf.gpa, new_file);
17011832
17021833 try dlw.writeByte(DW.LNS.set_file);
1703 try uleb128(dlw, file_gop.index);
1834 try dlw.writeUleb128(file_gop.index);
17041835 }
17051836
17061837 const old_src_line: i33 = zcu.navSrcLine(old_func_info.owner_nav);
17071838 const new_src_line: i33 = zcu.navSrcLine(new_func_info.owner_nav);
17081839 if (new_src_line != old_src_line) {
17091840 try dlw.writeByte(DW.LNS.advance_line);
1710 try sleb128(dlw, new_src_line - old_src_line);
1841 try dlw.writeSleb128(new_src_line - old_src_line);
17111842 }
17121843
17131844 wip_nav.func = func;
......@@ -1725,16 +1856,23 @@ pub const WipNav = struct {
17251856 try wip_nav.externalReloc(&wip_nav.dwarf.debug_frame.section, reloc);
17261857 }
17271858
1728 fn abbrevCode(wip_nav: *WipNav, abbrev_code: AbbrevCode) UpdateError!void {
1729 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), try wip_nav.dwarf.refAbbrevCode(abbrev_code));
1859 fn abbrevCode(wip_nav: *WipNav, abbrev_code: AbbrevCode) (UpdateError || Writer.Error)!void {
1860 try wip_nav.debug_info.writer.writeUleb128(try wip_nav.dwarf.refAbbrevCode(abbrev_code));
17301861 }
17311862
1732 fn sectionOffset(wip_nav: *WipNav, comptime sec: Section.Index, target_sec: Section.Index, target_unit: Unit.Index, target_entry: Entry.Index, target_off: u32) UpdateError!void {
1863 fn sectionOffset(
1864 wip_nav: *WipNav,
1865 comptime sec: Section.Index,
1866 target_sec: Section.Index,
1867 target_unit: Unit.Index,
1868 target_entry: Entry.Index,
1869 target_off: u32,
1870 ) (UpdateError || Writer.Error)!void {
17331871 const dwarf = wip_nav.dwarf;
17341872 const gpa = dwarf.gpa;
17351873 const entry_ptr = @field(dwarf, @tagName(sec)).section.getUnit(wip_nav.unit).getEntry(wip_nav.entry);
1736 const bytes = &@field(wip_nav, @tagName(sec));
1737 const source_off: u32 = @intCast(bytes.items.len);
1874 const sw = &@field(wip_nav, @tagName(sec)).writer;
1875 const source_off: u32 = @intCast(sw.end);
17381876 if (target_sec != sec) {
17391877 try entry_ptr.cross_section_relocs.append(gpa, .{
17401878 .source_off = source_off,
......@@ -1757,109 +1895,136 @@ pub const WipNav = struct {
17571895 .target_off = target_off,
17581896 });
17591897 }
1760 try bytes.appendNTimes(gpa, 0, dwarf.sectionOffsetBytes());
1898 try sw.splatByteAll(0, dwarf.sectionOffsetBytes());
17611899 }
17621900
1763 fn infoSectionOffset(wip_nav: *WipNav, target_sec: Section.Index, target_unit: Unit.Index, target_entry: Entry.Index, target_off: u32) UpdateError!void {
1901 fn infoSectionOffset(
1902 wip_nav: *WipNav,
1903 target_sec: Section.Index,
1904 target_unit: Unit.Index,
1905 target_entry: Entry.Index,
1906 target_off: u32,
1907 ) (UpdateError || Writer.Error)!void {
17641908 try wip_nav.sectionOffset(.debug_info, target_sec, target_unit, target_entry, target_off);
17651909 }
17661910
1767 fn strp(wip_nav: *WipNav, str: []const u8) UpdateError!void {
1911 fn strp(wip_nav: *WipNav, str: []const u8) (UpdateError || Writer.Error)!void {
17681912 try wip_nav.infoSectionOffset(.debug_str, StringSection.unit, try wip_nav.dwarf.debug_str.addString(wip_nav.dwarf, str), 0);
17691913 }
17701914
17711915 const ExprLocCounter = struct {
1772 stream: Writer.Discarding,
1916 dw: Writer.Discarding,
17731917 section_offset_bytes: u32,
17741918 address_size: AddressSize,
1775 fn init(dwarf: *Dwarf, trash_buffer: []u8) ExprLocCounter {
1919 fn init(dwarf: *Dwarf, buf: []u8) ExprLocCounter {
17761920 return .{
1777 .stream = .init(trash_buffer),
1921 .dw = .init(buf),
17781922 .section_offset_bytes = dwarf.sectionOffsetBytes(),
17791923 .address_size = dwarf.address_size,
17801924 };
17811925 }
17821926 fn writer(counter: *ExprLocCounter) *Writer {
1783 return &counter.stream.writer;
1927 return &counter.dw.writer;
17841928 }
17851929 fn endian(_: ExprLocCounter) std.builtin.Endian {
17861930 return @import("builtin").cpu.arch.endian();
17871931 }
1788 fn addrSym(counter: *ExprLocCounter, _: u32) error{}!void {
1789 counter.stream.count += @intFromEnum(counter.address_size);
1932 fn addrSym(counter: *ExprLocCounter, _: u32) Writer.Error!void {
1933 try counter.dw.writer.splatByteAll(undefined, @intFromEnum(counter.address_size));
17901934 }
1791 fn infoEntry(counter: *ExprLocCounter, _: Unit.Index, _: Entry.Index) error{}!void {
1792 counter.stream.count += counter.section_offset_bytes;
1935 fn infoEntry(counter: *ExprLocCounter, _: Unit.Index, _: Entry.Index) Writer.Error!void {
1936 try counter.dw.writer.splatByteAll(undefined, counter.section_offset_bytes);
17931937 }
17941938 };
17951939
1796 fn infoExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1797 var trash_buffer: [64]u8 = undefined;
1798 var counter: ExprLocCounter = .init(wip_nav.dwarf, &trash_buffer);
1940 fn infoExprLoc(wip_nav: *WipNav, loc: Loc) (UpdateError || Writer.Error)!void {
1941 var buf: [64]u8 = undefined;
1942 var counter: ExprLocCounter = .init(wip_nav.dwarf, &buf);
17991943 try loc.write(&counter);
18001944
18011945 const adapter: struct {
18021946 wip_nav: *WipNav,
1803 fn writer(ctx: @This()) std.ArrayListUnmanaged(u8).Writer {
1804 return ctx.wip_nav.debug_info.writer(ctx.wip_nav.dwarf.gpa);
1947 fn writer(ctx: @This()) *Writer {
1948 return &ctx.wip_nav.debug_info.writer;
18051949 }
18061950 fn endian(ctx: @This()) std.builtin.Endian {
18071951 return ctx.wip_nav.dwarf.endian;
18081952 }
1809 fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
1953 fn addrSym(ctx: @This(), sym_index: u32) (UpdateError || Writer.Error)!void {
18101954 try ctx.wip_nav.infoAddrSym(sym_index, 0);
18111955 }
1812 fn infoEntry(ctx: @This(), unit: Unit.Index, entry: Entry.Index) UpdateError!void {
1956 fn infoEntry(
1957 ctx: @This(),
1958 unit: Unit.Index,
1959 entry: Entry.Index,
1960 ) (UpdateError || Writer.Error)!void {
18131961 try ctx.wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
18141962 }
18151963 } = .{ .wip_nav = wip_nav };
1816 try uleb128(adapter.writer(), counter.stream.fullCount());
1964 try adapter.writer().writeUleb128(counter.dw.count + counter.dw.writer.end);
18171965 try loc.write(adapter);
18181966 }
18191967
1820 fn infoAddrSym(wip_nav: *WipNav, sym_index: u32, sym_off: u64) UpdateError!void {
1968 fn infoAddrSym(
1969 wip_nav: *WipNav,
1970 sym_index: u32,
1971 sym_off: u64,
1972 ) (UpdateError || Writer.Error)!void {
1973 const diw = &wip_nav.debug_info.writer;
18211974 try wip_nav.infoExternalReloc(.{
1822 .source_off = @intCast(wip_nav.debug_info.items.len),
1975 .source_off = @intCast(diw.end),
18231976 .target_sym = sym_index,
18241977 .target_off = sym_off,
18251978 });
1826 try wip_nav.debug_info.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
1979 try diw.splatByteAll(0, @intFromEnum(wip_nav.dwarf.address_size));
18271980 }
18281981
1829 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1830 var trash_buffer: [64]u8 = undefined;
1831 var counter: ExprLocCounter = .init(wip_nav.dwarf, &trash_buffer);
1982 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) (UpdateError || Writer.Error)!void {
1983 var buf: [64]u8 = undefined;
1984 var counter: ExprLocCounter = .init(wip_nav.dwarf, &buf);
18321985 try loc.write(&counter);
18331986
18341987 const adapter: struct {
18351988 wip_nav: *WipNav,
1836 fn writer(ctx: @This()) std.ArrayListUnmanaged(u8).Writer {
1837 return ctx.wip_nav.debug_frame.writer(ctx.wip_nav.dwarf.gpa);
1989 fn writer(ctx: @This()) *Writer {
1990 return &ctx.wip_nav.debug_frame.writer;
18381991 }
18391992 fn endian(ctx: @This()) std.builtin.Endian {
18401993 return ctx.wip_nav.dwarf.endian;
18411994 }
1842 fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
1995 fn addrSym(ctx: @This(), sym_index: u32) (UpdateError || Writer.Error)!void {
18431996 try ctx.wip_nav.frameAddrSym(sym_index, 0);
18441997 }
1845 fn infoEntry(ctx: @This(), unit: Unit.Index, entry: Entry.Index) UpdateError!void {
1998 fn infoEntry(
1999 ctx: @This(),
2000 unit: Unit.Index,
2001 entry: Entry.Index,
2002 ) (UpdateError || Writer.Error)!void {
18462003 try ctx.wip_nav.sectionOffset(.debug_frame, .debug_info, unit, entry, 0);
18472004 }
18482005 } = .{ .wip_nav = wip_nav };
1849 try uleb128(adapter.writer(), counter.stream.fullCount());
2006 try adapter.writer().writeUleb128(counter.dw.count + counter.dw.writer.end);
18502007 try loc.write(adapter);
18512008 }
18522009
1853 fn frameAddrSym(wip_nav: *WipNav, sym_index: u32, sym_off: u64) UpdateError!void {
2010 fn frameAddrSym(
2011 wip_nav: *WipNav,
2012 sym_index: u32,
2013 sym_off: u64,
2014 ) (UpdateError || Writer.Error)!void {
2015 const dfw = &wip_nav.debug_frame.writer;
18542016 try wip_nav.frameExternalReloc(.{
1855 .source_off = @intCast(wip_nav.debug_frame.items.len),
2017 .source_off = @intCast(dfw.end),
18562018 .target_sym = sym_index,
18572019 .target_off = sym_off,
18582020 });
1859 try wip_nav.debug_frame.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
2021 try dfw.splatByteAll(0, @intFromEnum(wip_nav.dwarf.address_size));
18602022 }
18612023
1862 fn getNavEntry(wip_nav: *WipNav, nav_index: InternPool.Nav.Index) UpdateError!struct { Unit.Index, Entry.Index } {
2024 fn getNavEntry(
2025 wip_nav: *WipNav,
2026 nav_index: InternPool.Nav.Index,
2027 ) UpdateError!struct { Unit.Index, Entry.Index } {
18632028 const zcu = wip_nav.pt.zcu;
18642029 const ip = &zcu.intern_pool;
18652030 const nav = ip.getNav(nav_index);
......@@ -1871,7 +2036,10 @@ pub const WipNav = struct {
18712036 return .{ unit, entry };
18722037 }
18732038
1874 fn refNav(wip_nav: *WipNav, nav_index: InternPool.Nav.Index) UpdateError!void {
2039 fn refNav(
2040 wip_nav: *WipNav,
2041 nav_index: InternPool.Nav.Index,
2042 ) (UpdateError || Writer.Error)!void {
18752043 const unit, const entry = try wip_nav.getNavEntry(nav_index);
18762044 try wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
18772045 }
......@@ -1898,7 +2066,7 @@ pub const WipNav = struct {
18982066 return .{ unit, entry };
18992067 }
19002068
1901 fn refType(wip_nav: *WipNav, ty: Type) UpdateError!void {
2069 fn refType(wip_nav: *WipNav, ty: Type) (UpdateError || Writer.Error)!void {
19022070 const unit, const entry = try wip_nav.getTypeEntry(ty);
19032071 try wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
19042072 }
......@@ -1919,47 +2087,54 @@ pub const WipNav = struct {
19192087 return .{ unit, entry };
19202088 }
19212089
1922 fn refValue(wip_nav: *WipNav, value: Value) UpdateError!void {
2090 fn refValue(wip_nav: *WipNav, value: Value) (UpdateError || Writer.Error)!void {
19232091 const unit, const entry = try wip_nav.getValueEntry(value);
19242092 try wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
19252093 }
19262094
1927 fn refForward(wip_nav: *WipNav) Allocator.Error!u32 {
2095 fn refForward(wip_nav: *WipNav) (Allocator.Error || Writer.Error)!u32 {
19282096 const dwarf = wip_nav.dwarf;
2097 const diw = &wip_nav.debug_info.writer;
19292098 const cross_entry_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs;
19302099 const reloc_index: u32 = @intCast(cross_entry_relocs.items.len);
19312100 try cross_entry_relocs.append(dwarf.gpa, .{
1932 .source_off = @intCast(wip_nav.debug_info.items.len),
2101 .source_off = @intCast(diw.end),
19332102 .target_entry = undefined,
19342103 .target_off = undefined,
19352104 });
1936 try wip_nav.debug_info.appendNTimes(dwarf.gpa, 0, dwarf.sectionOffsetBytes());
2105 try diw.splatByteAll(0, dwarf.sectionOffsetBytes());
19372106 return reloc_index;
19382107 }
19392108
19402109 fn finishForward(wip_nav: *WipNav, reloc_index: u32) void {
19412110 const reloc = &wip_nav.dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs.items[reloc_index];
19422111 reloc.target_entry = wip_nav.entry.toOptional();
1943 reloc.target_off = @intCast(wip_nav.debug_info.items.len);
2112 reloc.target_off = @intCast(wip_nav.debug_info.writer.end);
19442113 }
19452114
1946 fn blockValue(wip_nav: *WipNav, src_loc: Zcu.LazySrcLoc, val: Value) UpdateError!void {
2115 fn blockValue(
2116 wip_nav: *WipNav,
2117 src_loc: Zcu.LazySrcLoc,
2118 val: Value,
2119 ) (UpdateError || Writer.Error)!void {
19472120 const ty = val.typeOf(wip_nav.pt.zcu);
1948 const diw = wip_nav.debug_info.writer(wip_nav.dwarf.gpa);
1949 const bytes = if (ty.hasRuntimeBits(wip_nav.pt.zcu)) ty.abiSize(wip_nav.pt.zcu) else 0;
1950 try uleb128(diw, bytes);
1951 if (bytes == 0) return;
1952 const old_len = wip_nav.debug_info.items.len;
2121 const diw = &wip_nav.debug_info.writer;
2122 const size = if (ty.hasRuntimeBits(wip_nav.pt.zcu)) ty.abiSize(wip_nav.pt.zcu) else 0;
2123 try diw.writeUleb128(size);
2124 if (size == 0) return;
2125 var bytes = wip_nav.debug_info.toArrayList();
2126 defer wip_nav.debug_info = .fromArrayList(wip_nav.dwarf.gpa, &bytes);
2127 const old_len = bytes.items.len;
19532128 try codegen.generateSymbol(
19542129 wip_nav.dwarf.bin_file,
19552130 wip_nav.pt,
19562131 src_loc,
19572132 val,
1958 &wip_nav.debug_info,
2133 &bytes,
19592134 .{ .debug_output = .{ .dwarf = wip_nav } },
19602135 );
1961 if (old_len + bytes != wip_nav.debug_info.items.len) {
1962 std.debug.print("{f} [{}]: {} != {}\n", .{ ty.fmt(wip_nav.pt), ty.toIntern(), bytes, wip_nav.debug_info.items.len - old_len });
2136 if (old_len + size != bytes.items.len) {
2137 std.debug.print("{f} [{}]: {} != {}\n", .{ ty.fmt(wip_nav.pt), ty.toIntern(), size, bytes.items.len - old_len });
19632138 unreachable;
19642139 }
19652140 }
......@@ -1975,9 +2150,9 @@ pub const WipNav = struct {
19752150 abbrev_code: AbbrevCodeForForm,
19762151 ty: Type,
19772152 big_int: std.math.big.int.Const,
1978 ) UpdateError!void {
2153 ) (UpdateError || Writer.Error)!void {
19792154 const zcu = wip_nav.pt.zcu;
1980 const diw = wip_nav.debug_info.writer(wip_nav.dwarf.gpa);
2155 const diw = &wip_nav.debug_info.writer;
19812156 const signedness = switch (ty.toIntern()) {
19822157 .comptime_int_type, .comptime_float_type => .signed,
19832158 else => ty.intInfo(zcu).signedness,
......@@ -1988,7 +2163,7 @@ pub const WipNav = struct {
19882163 .signed => abbrev_code.sdata,
19892164 .unsigned => abbrev_code.udata,
19902165 });
1991 try wip_nav.debug_info.ensureUnusedCapacity(wip_nav.dwarf.gpa, std.math.divCeil(usize, bits, 7) catch unreachable);
2166 try wip_nav.debug_info.ensureUnusedCapacity(std.math.divCeil(usize, bits, 7) catch unreachable);
19922167 var bit: usize = 0;
19932168 var carry: u1 = 1;
19942169 while (bit < bits) {
......@@ -2005,14 +2180,15 @@ pub const WipNav = struct {
20052180 break :twos_comp_part twos_comp_part;
20062181 };
20072182 bit += 7;
2008 wip_nav.debug_info.appendAssumeCapacity(@as(u8, if (bit < bits) 0x80 else 0x00) | twos_comp_part);
2183 diw.writeByte(@as(u8, if (bit < bits) 0x80 else 0x00) | twos_comp_part) catch unreachable;
20092184 }
20102185 } else {
20112186 try wip_nav.abbrevCode(abbrev_code.block);
20122187 const bytes = @max(ty.abiSize(zcu), std.math.divCeil(usize, bits, 8) catch unreachable);
2013 try uleb128(diw, bytes);
2188 try diw.writeUleb128(bytes);
2189 try wip_nav.debug_info.ensureUnusedCapacity(@intCast(bytes));
20142190 big_int.writeTwosComplement(
2015 try wip_nav.debug_info.addManyAsSlice(wip_nav.dwarf.gpa, @intCast(bytes)),
2191 try diw.writableSlice(@intCast(bytes)),
20162192 wip_nav.dwarf.endian,
20172193 );
20182194 }
......@@ -2023,7 +2199,7 @@ pub const WipNav = struct {
20232199 loaded_enum: InternPool.LoadedEnumType,
20242200 abbrev_code: AbbrevCodeForForm,
20252201 field_index: usize,
2026 ) UpdateError!void {
2202 ) (UpdateError || Writer.Error)!void {
20272203 const zcu = wip_nav.pt.zcu;
20282204 const ip = &zcu.intern_pool;
20292205 var big_int_space: Value.BigIntSpace = undefined;
......@@ -2043,11 +2219,11 @@ pub const WipNav = struct {
20432219 nav: *const InternPool.Nav,
20442220 file: Zcu.File.Index,
20452221 decl: *const std.zig.Zir.Inst.Declaration.Unwrapped,
2046 ) UpdateError!void {
2222 ) (UpdateError || Writer.Error)!void {
20472223 const zcu = wip_nav.pt.zcu;
20482224 const ip = &zcu.intern_pool;
20492225 const dwarf = wip_nav.dwarf;
2050 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2226 const diw = &wip_nav.debug_info.writer;
20512227
20522228 const orig_entry = wip_nav.entry;
20532229 defer wip_nav.entry = orig_entry;
......@@ -2098,15 +2274,15 @@ pub const WipNav = struct {
20982274 try wip_nav.abbrevCode(if (is_generic_decl) abbrev_code.generic_decl else abbrev_code.decl);
20992275 try wip_nav.refType((if (is_generic_decl) null else parent_type) orelse
21002276 .fromInterned(zcu.fileRootType(file)));
2101 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2277 assert(diw.end == DebugInfo.declEntryLineOff(dwarf));
21022278 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
2103 try uleb128(diw, decl.src_column + 1);
2279 try diw.writeUleb128(decl.src_column + 1);
21042280 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
21052281 try wip_nav.strp(nav.name.toSlice(ip));
21062282
21072283 if (!is_generic_decl) return;
21082284 const generic_decl_entry = wip_nav.entry;
2109 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, generic_decl_entry, dwarf, wip_nav.debug_info.items);
2285 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, generic_decl_entry, dwarf, wip_nav.debug_info.getWritten());
21102286 wip_nav.debug_info.clearRetainingCapacity();
21112287 wip_nav.entry = orig_entry;
21122288 try wip_nav.abbrevCode(abbrev_code.decl_instance);
......@@ -2121,7 +2297,7 @@ pub const WipNav = struct {
21212297 const empty: PendingLazy = .{ .types = .empty, .values = .empty };
21222298 };
21232299
2124 fn updateLazy(wip_nav: *WipNav, src_loc: Zcu.LazySrcLoc) UpdateError!void {
2300 fn updateLazy(wip_nav: *WipNav, src_loc: Zcu.LazySrcLoc) (UpdateError || Writer.Error)!void {
21252301 while (true) if (wip_nav.pending_lazy.types.pop()) |pending_ty|
21262302 try wip_nav.dwarf.updateLazyType(wip_nav.pt, src_loc, pending_ty, &wip_nav.pending_lazy)
21272303 else if (wip_nav.pending_lazy.values.pop()) |pending_val|
......@@ -2411,8 +2587,8 @@ pub fn initWipNav(
24112587 sym_index: u32,
24122588) error{ OutOfMemory, CodegenFail }!?WipNav {
24132589 return initWipNavInner(dwarf, pt, nav_index, sym_index) catch |err| switch (err) {
2414 error.OutOfMemory => return error.OutOfMemory,
2415 else => |e| return pt.zcu.codegenFail(nav_index, "failed to init dwarf: {s}", .{@errorName(e)}),
2590 error.OutOfMemory => error.OutOfMemory,
2591 else => |e| pt.zcu.codegenFail(nav_index, "failed to init dwarf: {s}", .{@errorName(e)}),
24162592 };
24172593}
24182594
......@@ -2470,17 +2646,17 @@ fn initWipNavInner(
24702646 .func_high_pc = undefined,
24712647 .blocks = undefined,
24722648 .cfi = undefined,
2473 .debug_frame = .empty,
2474 .debug_info = .empty,
2475 .debug_line = .empty,
2476 .debug_loclists = .empty,
2649 .debug_frame = .init(dwarf.gpa),
2650 .debug_info = .init(dwarf.gpa),
2651 .debug_line = .init(dwarf.gpa),
2652 .debug_loclists = .init(dwarf.gpa),
24772653 .pending_lazy = .empty,
24782654 };
24792655 errdefer wip_nav.deinit();
24802656
24812657 switch (nav_key) {
24822658 else => {
2483 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2659 const diw = &wip_nav.debug_info.writer;
24842660 try wip_nav.declCommon(.{
24852661 .decl = .decl_var,
24862662 .generic_decl = .generic_decl_var,
......@@ -2501,7 +2677,7 @@ fn initWipNavInner(
25012677 .@"const" => {
25022678 const const_ty_reloc_index = try wip_nav.refForward();
25032679 try wip_nav.infoExprLoc(loc);
2504 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
2680 try diw.writeUleb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
25052681 ty.abiAlignment(zcu).toByteUnits().?);
25062682 try diw.writeByte(@intFromBool(decl.linkage != .normal));
25072683 wip_nav.finishForward(const_ty_reloc_index);
......@@ -2511,7 +2687,7 @@ fn initWipNavInner(
25112687 .@"var" => {
25122688 try wip_nav.refType(ty);
25132689 try wip_nav.infoExprLoc(loc);
2514 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
2690 try diw.writeUleb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
25152691 ty.abiAlignment(zcu).toByteUnits().?);
25162692 try diw.writeByte(@intFromBool(decl.linkage != .normal));
25172693 },
......@@ -2538,7 +2714,7 @@ fn initWipNavInner(
25382714 .none => {},
25392715 .debug_frame, .eh_frame => |format| {
25402716 const entry = dwarf.debug_frame.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry);
2541 const dfw = wip_nav.debug_frame.writer(dwarf.gpa);
2717 const dfw = &wip_nav.debug_frame.writer;
25422718 switch (dwarf.format) {
25432719 .@"32" => try dfw.writeInt(u32, undefined, dwarf.endian),
25442720 .@"64" => {
......@@ -2550,27 +2726,27 @@ fn initWipNavInner(
25502726 .none => unreachable,
25512727 .debug_frame => {
25522728 try entry.cross_entry_relocs.append(dwarf.gpa, .{
2553 .source_off = @intCast(wip_nav.debug_frame.items.len),
2729 .source_off = @intCast(dfw.end),
25542730 });
2555 try dfw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
2731 try dfw.splatByteAll(0, dwarf.sectionOffsetBytes());
25562732 try wip_nav.frameAddrSym(sym_index, 0);
2557 try dfw.writeByteNTimes(undefined, @intFromEnum(dwarf.address_size));
2733 try dfw.splatByteAll(undefined, @intFromEnum(dwarf.address_size));
25582734 },
25592735 .eh_frame => {
25602736 try dfw.writeInt(u32, undefined, dwarf.endian);
25612737 try wip_nav.frameExternalReloc(.{
2562 .source_off = @intCast(wip_nav.debug_frame.items.len),
2738 .source_off = @intCast(dfw.end),
25632739 .target_sym = sym_index,
25642740 });
25652741 try dfw.writeInt(u32, 0, dwarf.endian);
25662742 try dfw.writeInt(u32, undefined, dwarf.endian);
2567 try uleb128(dfw, 0);
2743 try dfw.writeUleb128(0);
25682744 },
25692745 }
25702746 },
25712747 }
25722748
2573 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2749 const diw = &wip_nav.debug_info.writer;
25742750 try wip_nav.declCommon(.{
25752751 .decl = .decl_func,
25762752 .generic_decl = .generic_decl_func,
......@@ -2579,48 +2755,48 @@ fn initWipNavInner(
25792755 try wip_nav.strp(nav.fqn.toSlice(ip));
25802756 try wip_nav.refType(.fromInterned(func_type.return_type));
25812757 try wip_nav.infoAddrSym(sym_index, 0);
2582 wip_nav.func_high_pc = @intCast(wip_nav.debug_info.items.len);
2758 wip_nav.func_high_pc = @intCast(diw.end);
25832759 try diw.writeInt(u32, 0, dwarf.endian);
25842760 const target = &mod.resolved_target.result;
2585 try uleb128(diw, switch (nav.status.fully_resolved.alignment) {
2761 try diw.writeUleb128(switch (nav.status.fully_resolved.alignment) {
25862762 .none => target_info.defaultFunctionAlignment(target),
25872763 else => |a| a.maxStrict(target_info.minFunctionAlignment(target)),
25882764 }.toByteUnits().?);
25892765 try diw.writeByte(@intFromBool(decl.linkage != .normal));
25902766 try diw.writeByte(@intFromBool(func_type.return_type == .noreturn_type));
25912767
2592 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
2768 const dlw = &wip_nav.debug_line.writer;
25932769 try dlw.writeByte(DW.LNS.extended_op);
25942770 if (dwarf.incremental()) {
2595 try uleb128(dlw, 1 + dwarf.sectionOffsetBytes());
2771 try dlw.writeUleb128(1 + dwarf.sectionOffsetBytes());
25962772 try dlw.writeByte(DW.LNE.ZIG_set_decl);
25972773 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{
2598 .source_off = @intCast(wip_nav.debug_line.items.len),
2774 .source_off = @intCast(dlw.end),
25992775 .target_sec = .debug_info,
26002776 .target_unit = wip_nav.unit,
26012777 .target_entry = wip_nav.entry.toOptional(),
26022778 });
2603 try dlw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
2779 try dlw.splatByteAll(0, dwarf.sectionOffsetBytes());
26042780
26052781 try dlw.writeByte(DW.LNS.set_column);
2606 try uleb128(dlw, func.lbrace_column + 1);
2782 try dlw.writeUleb128(func.lbrace_column + 1);
26072783
26082784 try wip_nav.advancePCAndLine(func.lbrace_line, 0);
26092785 } else {
2610 try uleb128(dlw, 1 + @intFromEnum(dwarf.address_size));
2786 try dlw.writeUleb128(1 + @intFromEnum(dwarf.address_size));
26112787 try dlw.writeByte(DW.LNE.set_address);
26122788 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{
2613 .source_off = @intCast(wip_nav.debug_line.items.len),
2789 .source_off = @intCast(dlw.end),
26142790 .target_sym = sym_index,
26152791 });
2616 try dlw.writeByteNTimes(0, @intFromEnum(dwarf.address_size));
2792 try dlw.splatByteAll(0, @intFromEnum(dwarf.address_size));
26172793
26182794 const file_gop = try dwarf.getModInfo(unit).files.getOrPut(dwarf.gpa, inst_info.file);
26192795 try dlw.writeByte(DW.LNS.set_file);
2620 try uleb128(dlw, file_gop.index);
2796 try dlw.writeUleb128(file_gop.index);
26212797
26222798 try dlw.writeByte(DW.LNS.set_column);
2623 try uleb128(dlw, func.lbrace_column + 1);
2799 try dlw.writeUleb128(func.lbrace_column + 1);
26242800
26252801 try wip_nav.advancePCAndLine(@intCast(decl.src_line + func.lbrace_line), 0);
26262802 }
......@@ -2636,6 +2812,18 @@ pub fn finishWipNavFunc(
26362812 code_size: u64,
26372813 wip_nav: *WipNav,
26382814) UpdateError!void {
2815 return dwarf.finishWipNavFuncWriterError(pt, nav_index, code_size, wip_nav) catch |err| switch (err) {
2816 error.WriteFailed => error.OutOfMemory,
2817 else => |e| e,
2818 };
2819}
2820fn finishWipNavFuncWriterError(
2821 dwarf: *Dwarf,
2822 pt: Zcu.PerThread,
2823 nav_index: InternPool.Nav.Index,
2824 code_size: u64,
2825 wip_nav: *WipNav,
2826) (UpdateError || Writer.Error)!void {
26392827 const zcu = pt.zcu;
26402828 const ip = &zcu.intern_pool;
26412829 const nav = ip.getNav(nav_index);
......@@ -2658,12 +2846,12 @@ pub fn finishWipNavFunc(
26582846 switch (dwarf.debug_frame.header.format) {
26592847 .none => {},
26602848 .debug_frame, .eh_frame => |format| {
2661 try wip_nav.debug_frame.appendNTimes(
2662 dwarf.gpa,
2849 const dfw = &wip_nav.debug_frame.writer;
2850 try dfw.splatByteAll(
26632851 DW.CFA.nop,
2664 @intCast(dwarf.debug_frame.section.alignment.forward(wip_nav.debug_frame.items.len) - wip_nav.debug_frame.items.len),
2852 @intCast(dwarf.debug_frame.section.alignment.forward(dfw.end) - dfw.end),
26652853 );
2666 const contents = wip_nav.debug_frame.items;
2854 const contents = wip_nav.debug_frame.getWritten();
26672855 try dwarf.debug_frame.section.resizeEntry(wip_nav.unit, wip_nav.entry, dwarf, @intCast(contents.len));
26682856 const unit = dwarf.debug_frame.section.getUnit(wip_nav.unit);
26692857 const entry = unit.getEntry(wip_nav.entry);
......@@ -2690,14 +2878,16 @@ pub fn finishWipNavFunc(
26902878 },
26912879 }
26922880 {
2693 std.mem.writeInt(u32, wip_nav.debug_info.items[wip_nav.func_high_pc..][0..4], @intCast(code_size), dwarf.endian);
2881 std.mem.writeInt(u32, wip_nav.debug_info.getWritten()[wip_nav.func_high_pc..][0..4], @intCast(code_size), dwarf.endian);
26942882 if (wip_nav.any_children) {
2695 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2696 try uleb128(diw, @intFromEnum(AbbrevCode.null));
2883 const diw = &wip_nav.debug_info.writer;
2884 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
26972885 } else {
2698 const abbrev_code_buf = wip_nav.debug_info.items[0..AbbrevCode.decl_bytes];
2699 var abbrev_code_fbs = std.io.fixedBufferStream(abbrev_code_buf);
2700 const abbrev_code: AbbrevCode = @enumFromInt(std.leb.readUleb128(@typeInfo(AbbrevCode).@"enum".tag_type, abbrev_code_fbs.reader()) catch unreachable);
2886 const abbrev_code_buf = wip_nav.debug_info.getWritten()[0..AbbrevCode.decl_bytes];
2887 var abbrev_code_fr: std.Io.Reader = .fixed(abbrev_code_buf);
2888 const abbrev_code: AbbrevCode = @enumFromInt(
2889 abbrev_code_fr.takeLeb128(@typeInfo(AbbrevCode).@"enum".tag_type) catch unreachable,
2890 );
27012891 std.leb.writeUnsignedFixed(
27022892 AbbrevCode.decl_bytes,
27032893 abbrev_code_buf,
......@@ -2738,28 +2928,39 @@ pub fn finishWipNav(
27382928 nav_index: InternPool.Nav.Index,
27392929 wip_nav: *WipNav,
27402930) UpdateError!void {
2931 return dwarf.finishWipNavWriterError(pt, nav_index, wip_nav) catch |err| switch (err) {
2932 error.WriteFailed => error.OutOfMemory,
2933 else => |e| e,
2934 };
2935}
2936fn finishWipNavWriterError(
2937 dwarf: *Dwarf,
2938 pt: Zcu.PerThread,
2939 nav_index: InternPool.Nav.Index,
2940 wip_nav: *WipNav,
2941) (UpdateError || Writer.Error)!void {
27412942 const zcu = pt.zcu;
27422943 const ip = &zcu.intern_pool;
27432944 const nav = ip.getNav(nav_index);
27442945 log.debug("finishWipNav({f})", .{nav.fqn.fmt(ip)});
27452946
2746 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
2747 if (wip_nav.debug_line.items.len > 0) {
2748 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
2947 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
2948 const dlw = &wip_nav.debug_line.writer;
2949 if (dlw.end > 0) {
27492950 try dlw.writeByte(DW.LNS.extended_op);
2750 try uleb128(dlw, 1);
2951 try dlw.writeUleb128(1);
27512952 try dlw.writeByte(DW.LNE.end_sequence);
2752 try dwarf.debug_line.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_line.items);
2953 try dwarf.debug_line.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_line.getWritten());
27532954 }
2754 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.items);
2955 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.getWritten());
27552956
27562957 try wip_nav.updateLazy(zcu.navSrcLoc(nav_index));
27572958}
27582959
27592960pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) error{ OutOfMemory, CodegenFail }!void {
27602961 return updateComptimeNavInner(dwarf, pt, nav_index) catch |err| switch (err) {
2761 error.OutOfMemory => return error.OutOfMemory,
2762 else => |e| return pt.zcu.codegenFail(nav_index, "failed to update dwarf: {s}", .{@errorName(e)}),
2962 error.OutOfMemory => error.OutOfMemory,
2963 else => |e| pt.zcu.codegenFail(nav_index, "failed to update dwarf: {s}", .{@errorName(e)}),
27632964 };
27642965}
27652966
......@@ -2801,10 +3002,10 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
28013002 .func_high_pc = undefined,
28023003 .blocks = undefined,
28033004 .cfi = undefined,
2804 .debug_frame = .empty,
2805 .debug_info = .empty,
2806 .debug_line = .empty,
2807 .debug_loclists = .empty,
3005 .debug_frame = .init(dwarf.gpa),
3006 .debug_info = .init(dwarf.gpa),
3007 .debug_line = .init(dwarf.gpa),
3008 .debug_loclists = .init(dwarf.gpa),
28083009 .pending_lazy = .empty,
28093010 };
28103011 defer wip_nav.deinit();
......@@ -2850,7 +3051,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
28503051 }
28513052 wip_nav.entry = nav_gop.value_ptr.*;
28523053
2853 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3054 const diw = &wip_nav.debug_info.writer;
28543055
28553056 switch (loaded_struct.layout) {
28563057 .auto, .@"extern" => {
......@@ -2864,8 +3065,8 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
28643065 .decl_instance = .decl_instance_struct,
28653066 }, &nav, inst_info.file, &decl);
28663067 if (loaded_struct.field_types.len == 0) try diw.writeByte(@intFromBool(false)) else {
2867 try uleb128(diw, nav_val.toType().abiSize(zcu));
2868 try uleb128(diw, nav_val.toType().abiAlignment(zcu).toByteUnits().?);
3068 try diw.writeUleb128(nav_val.toType().abiSize(zcu));
3069 try diw.writeUleb128(nav_val.toType().abiAlignment(zcu).toByteUnits().?);
28693070 for (0..loaded_struct.field_types.len) |field_index| {
28703071 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
28713072 const field_init = loaded_struct.fieldInit(ip, field_index);
......@@ -2901,8 +3102,8 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29013102 }
29023103 try wip_nav.refType(field_type);
29033104 if (!is_comptime) {
2904 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
2905 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
3105 try diw.writeUleb128(loaded_struct.offsets.get(ip)[field_index]);
3106 try diw.writeUleb128(loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
29063107 field_type.abiAlignment(zcu).toByteUnits().?);
29073108 }
29083109 if (has_comptime_state)
......@@ -2910,7 +3111,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29103111 else if (has_runtime_bits)
29113112 try wip_nav.blockValue(nav_src_loc, .fromInterned(field_init));
29123113 }
2913 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3114 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
29143115 }
29153116 },
29163117 .@"packed" => {
......@@ -2926,10 +3127,10 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29263127 try wip_nav.strp(loaded_struct.fieldName(ip, field_index).unwrap().?.toSlice(ip));
29273128 const field_type: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
29283129 try wip_nav.refType(field_type);
2929 try uleb128(diw, field_bit_offset);
3130 try diw.writeUleb128(field_bit_offset);
29303131 field_bit_offset += @intCast(field_type.bitSize(zcu));
29313132 }
2932 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3133 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
29333134 },
29343135 }
29353136 break :tag .done;
......@@ -2952,7 +3153,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29523153 type_gop.value_ptr.* = nav_gop.value_ptr.*;
29533154 }
29543155 wip_nav.entry = nav_gop.value_ptr.*;
2955 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3156 const diw = &wip_nav.debug_info.writer;
29563157 try wip_nav.declCommon(if (loaded_enum.names.len > 0) .{
29573158 .decl = .decl_enum,
29583159 .generic_decl = .generic_decl_const,
......@@ -2971,7 +3172,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29713172 }, field_index);
29723173 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
29733174 }
2974 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
3175 if (loaded_enum.names.len > 0) try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
29753176 break :tag .done;
29763177 },
29773178 .union_type => tag: {
......@@ -2991,15 +3192,15 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29913192 type_gop.value_ptr.* = nav_gop.value_ptr.*;
29923193 }
29933194 wip_nav.entry = nav_gop.value_ptr.*;
2994 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3195 const diw = &wip_nav.debug_info.writer;
29953196 try wip_nav.declCommon(.{
29963197 .decl = .decl_union,
29973198 .generic_decl = .generic_decl_const,
29983199 .decl_instance = .decl_instance_union,
29993200 }, &nav, inst_info.file, &decl);
30003201 const union_layout = Type.getUnionLayout(loaded_union, zcu);
3001 try uleb128(diw, union_layout.abi_size);
3002 try uleb128(diw, union_layout.abi_align.toByteUnits().?);
3202 try diw.writeUleb128(union_layout.abi_size);
3203 try diw.writeUleb128(union_layout.abi_align.toByteUnits().?);
30033204 const loaded_tag = loaded_union.loadTagType(ip);
30043205 if (loaded_union.hasTag(ip)) {
30053206 try wip_nav.abbrevCode(.tagged_union);
......@@ -3007,13 +3208,13 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30073208 .debug_info,
30083209 wip_nav.unit,
30093210 wip_nav.entry,
3010 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3211 @intCast(diw.end + dwarf.sectionOffsetBytes()),
30113212 );
30123213 {
30133214 try wip_nav.abbrevCode(.generated_field);
30143215 try wip_nav.strp("tag");
30153216 try wip_nav.refType(.fromInterned(loaded_union.enum_tag_ty));
3016 try uleb128(diw, union_layout.tagOffset());
3217 try diw.writeUleb128(union_layout.tagOffset());
30173218
30183219 for (0..loaded_union.field_types.len) |field_index| {
30193220 try wip_nav.enumConstValue(loaded_tag, .{
......@@ -3026,23 +3227,23 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30263227 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
30273228 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
30283229 try wip_nav.refType(field_type);
3029 try uleb128(diw, union_layout.payloadOffset());
3030 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
3230 try diw.writeUleb128(union_layout.payloadOffset());
3231 try diw.writeUleb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
30313232 if (field_type.isNoReturn(zcu)) 1 else field_type.abiAlignment(zcu).toByteUnits().?);
30323233 }
3033 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3234 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
30343235 }
30353236 }
3036 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3237 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
30373238 } else for (0..loaded_union.field_types.len) |field_index| {
30383239 try wip_nav.abbrevCode(.untagged_union_field);
30393240 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
30403241 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
30413242 try wip_nav.refType(field_type);
3042 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
3243 try diw.writeUleb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
30433244 field_type.abiAlignment(zcu).toByteUnits().?);
30443245 }
3045 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3246 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
30463247 break :tag .done;
30473248 },
30483249 .opaque_type => tag: {
......@@ -3062,7 +3263,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30623263 type_gop.value_ptr.* = nav_gop.value_ptr.*;
30633264 }
30643265 wip_nav.entry = nav_gop.value_ptr.*;
3065 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3266 const diw = &wip_nav.debug_info.writer;
30663267 try wip_nav.declCommon(.{
30673268 .decl = .decl_namespace_struct,
30683269 .generic_decl = .generic_decl_const,
......@@ -3106,7 +3307,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31063307 const is_nullary = !func_type.is_var_args and for (0..func_type.param_types.len) |param_index| {
31073308 if (!func_type.paramIsComptime(std.math.cast(u5, param_index) orelse break false)) break false;
31083309 } else true;
3109 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3310 const diw = &wip_nav.debug_info.writer;
31103311 try wip_nav.declCommon(if (is_nullary) .{
31113312 .decl = .decl_nullary_func_generic,
31123313 .generic_decl = .generic_decl_func,
......@@ -3125,7 +3326,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31253326 try wip_nav.refType(.fromInterned(func_type.param_types.get(ip)[param_index]));
31263327 }
31273328 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3128 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3329 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
31293330 }
31303331 break :tag .done;
31313332 },
......@@ -3150,7 +3351,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31503351 try wip_nav.refType(nav_val.toType());
31513352 },
31523353 .decl_var => {
3153 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3354 const diw = &wip_nav.debug_info.writer;
31543355 try wip_nav.declCommon(.{
31553356 .decl = .decl_var,
31563357 .generic_decl = .generic_decl_var,
......@@ -3160,12 +3361,12 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31603361 const nav_ty = nav_val.typeOf(zcu);
31613362 try wip_nav.refType(nav_ty);
31623363 try wip_nav.blockValue(nav_src_loc, nav_val);
3163 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
3364 try diw.writeUleb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
31643365 nav_ty.abiAlignment(zcu).toByteUnits().?);
31653366 try diw.writeByte(@intFromBool(decl.linkage != .normal));
31663367 },
31673368 .decl_const => {
3168 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3369 const diw = &wip_nav.debug_info.writer;
31693370 const nav_ty = nav_val.typeOf(zcu);
31703371 const has_runtime_bits = nav_ty.hasRuntimeBits(zcu);
31713372 const has_comptime_state = nav_ty.comptimeOnly(zcu) and try nav_ty.onePossibleValue(pt) == null;
......@@ -3188,7 +3389,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31883389 }, &nav, inst_info.file, &decl);
31893390 try wip_nav.strp(nav.fqn.toSlice(ip));
31903391 const nav_ty_reloc_index = try wip_nav.refForward();
3191 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
3392 try diw.writeUleb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
31923393 nav_ty.abiAlignment(zcu).toByteUnits().?);
31933394 try diw.writeByte(@intFromBool(decl.linkage != .normal));
31943395 if (has_runtime_bits) try wip_nav.blockValue(nav_src_loc, nav_val);
......@@ -3206,7 +3407,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32063407 try wip_nav.refNav(owner_nav);
32073408 },
32083409 }
3209 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
3410 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
32103411 try wip_nav.updateLazy(nav_src_loc);
32113412}
32123413
......@@ -3216,7 +3417,7 @@ fn updateLazyType(
32163417 src_loc: Zcu.LazySrcLoc,
32173418 type_index: InternPool.Index,
32183419 pending_lazy: *WipNav.PendingLazy,
3219) UpdateError!void {
3420) (UpdateError || Writer.Error)!void {
32203421 const zcu = pt.zcu;
32213422 const ip = &zcu.intern_pool;
32223423 assert(ip.typeOf(type_index) == .type_type);
......@@ -3237,10 +3438,10 @@ fn updateLazyType(
32373438 .func_high_pc = undefined,
32383439 .blocks = undefined,
32393440 .cfi = undefined,
3240 .debug_frame = .empty,
3241 .debug_info = .empty,
3242 .debug_line = .empty,
3243 .debug_loclists = .empty,
3441 .debug_frame = .init(dwarf.gpa),
3442 .debug_info = .init(dwarf.gpa),
3443 .debug_line = .init(dwarf.gpa),
3444 .debug_loclists = .init(dwarf.gpa),
32443445 .pending_lazy = pending_lazy.*,
32453446 };
32463447 defer {
......@@ -3248,7 +3449,7 @@ fn updateLazyType(
32483449 wip_nav.pending_lazy = .empty;
32493450 wip_nav.deinit();
32503451 }
3251 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3452 const diw = &wip_nav.debug_info.writer;
32523453 const name = switch (type_index) {
32533454 .generic_poison_type => "",
32543455 else => try std.fmt.allocPrint(dwarf.gpa, "{f}", .{ty.fmt(pt)}),
......@@ -3266,9 +3467,9 @@ fn updateLazyType(
32663467 try diw.writeByte(switch (int_type.signedness) {
32673468 inline .signed, .unsigned => |signedness| @field(DW.ATE, @tagName(signedness)),
32683469 });
3269 try uleb128(diw, int_type.bits);
3270 try uleb128(diw, ty.abiSize(zcu));
3271 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3470 try diw.writeUleb128(int_type.bits);
3471 try diw.writeUleb128(ty.abiSize(zcu));
3472 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
32723473 },
32733474 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {
32743475 .one, .many, .c => {
......@@ -3276,14 +3477,14 @@ fn updateLazyType(
32763477 try wip_nav.abbrevCode(if (ptr_type.sentinel == .none) .ptr_type else .ptr_sentinel_type);
32773478 try wip_nav.strp(name);
32783479 if (ptr_type.sentinel != .none) try wip_nav.blockValue(src_loc, .fromInterned(ptr_type.sentinel));
3279 try uleb128(diw, ptr_type.flags.alignment.toByteUnits() orelse
3480 try diw.writeUleb128(ptr_type.flags.alignment.toByteUnits() orelse
32803481 ptr_child_type.abiAlignment(zcu).toByteUnits().?);
32813482 try diw.writeByte(@intFromEnum(ptr_type.flags.address_space));
32823483 if (ptr_type.flags.is_const or ptr_type.flags.is_volatile) try wip_nav.infoSectionOffset(
32833484 .debug_info,
32843485 wip_nav.unit,
32853486 wip_nav.entry,
3286 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3487 @intCast(diw.end + dwarf.sectionOffsetBytes()),
32873488 ) else try wip_nav.refType(ptr_child_type);
32883489 if (ptr_type.flags.is_const) {
32893490 try wip_nav.abbrevCode(.is_const);
......@@ -3291,7 +3492,7 @@ fn updateLazyType(
32913492 .debug_info,
32923493 wip_nav.unit,
32933494 wip_nav.entry,
3294 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3495 @intCast(diw.end + dwarf.sectionOffsetBytes()),
32953496 ) else try wip_nav.refType(ptr_child_type);
32963497 }
32973498 if (ptr_type.flags.is_volatile) {
......@@ -3302,19 +3503,19 @@ fn updateLazyType(
33023503 .slice => {
33033504 try wip_nav.abbrevCode(.generated_struct_type);
33043505 try wip_nav.strp(name);
3305 try uleb128(diw, ty.abiSize(zcu));
3306 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3506 try diw.writeUleb128(ty.abiSize(zcu));
3507 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
33073508 try wip_nav.abbrevCode(.generated_field);
33083509 try wip_nav.strp("ptr");
33093510 const ptr_field_type = ty.slicePtrFieldType(zcu);
33103511 try wip_nav.refType(ptr_field_type);
3311 try uleb128(diw, 0);
3512 try diw.writeUleb128(0);
33123513 try wip_nav.abbrevCode(.generated_field);
33133514 try wip_nav.strp("len");
33143515 const len_field_type: Type = .usize;
33153516 try wip_nav.refType(len_field_type);
3316 try uleb128(diw, len_field_type.abiAlignment(zcu).forward(ptr_field_type.abiSize(zcu)));
3317 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3517 try diw.writeUleb128(len_field_type.abiAlignment(zcu).forward(ptr_field_type.abiSize(zcu)));
3518 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
33183519 },
33193520 },
33203521 .array_type => |array_type| {
......@@ -3325,8 +3526,8 @@ fn updateLazyType(
33253526 try wip_nav.refType(array_child_type);
33263527 try wip_nav.abbrevCode(.array_index);
33273528 try wip_nav.refType(.usize);
3328 try uleb128(diw, array_type.len);
3329 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3529 try diw.writeUleb128(array_type.len);
3530 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
33303531 },
33313532 .vector_type => |vector_type| {
33323533 try wip_nav.abbrevCode(.vector_type);
......@@ -3334,22 +3535,22 @@ fn updateLazyType(
33343535 try wip_nav.refType(.fromInterned(vector_type.child));
33353536 try wip_nav.abbrevCode(.array_index);
33363537 try wip_nav.refType(.usize);
3337 try uleb128(diw, vector_type.len);
3338 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3538 try diw.writeUleb128(vector_type.len);
3539 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
33393540 },
33403541 .opt_type => |opt_child_type_index| {
33413542 const opt_child_type: Type = .fromInterned(opt_child_type_index);
33423543 const opt_repr = optRepr(opt_child_type, zcu);
33433544 try wip_nav.abbrevCode(.generated_union_type);
33443545 try wip_nav.strp(name);
3345 try uleb128(diw, ty.abiSize(zcu));
3346 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3546 try diw.writeUleb128(ty.abiSize(zcu));
3547 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
33473548 switch (opt_repr) {
33483549 .opv_null => {
33493550 try wip_nav.abbrevCode(.generated_field);
33503551 try wip_nav.strp("null");
33513552 try wip_nav.refType(.null);
3352 try uleb128(diw, 0);
3553 try diw.writeUleb128(0);
33533554 },
33543555 .unpacked, .error_set, .pointer => {
33553556 try wip_nav.abbrevCode(.tagged_union);
......@@ -3357,7 +3558,7 @@ fn updateLazyType(
33573558 .debug_info,
33583559 wip_nav.unit,
33593560 wip_nav.entry,
3360 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3561 @intCast(diw.end + dwarf.sectionOffsetBytes()),
33613562 );
33623563 {
33633564 try wip_nav.abbrevCode(.generated_field);
......@@ -3366,7 +3567,7 @@ fn updateLazyType(
33663567 .opv_null => unreachable,
33673568 .unpacked => {
33683569 try wip_nav.refType(.bool);
3369 try uleb128(diw, if (opt_child_type.hasRuntimeBits(zcu))
3570 try diw.writeUleb128(if (opt_child_type.hasRuntimeBits(zcu))
33703571 opt_child_type.abiSize(zcu)
33713572 else
33723573 0);
......@@ -3376,37 +3577,37 @@ fn updateLazyType(
33763577 .signedness = .unsigned,
33773578 .bits = zcu.errorSetBits(),
33783579 } })));
3379 try uleb128(diw, 0);
3580 try diw.writeUleb128(0);
33803581 },
33813582 .pointer => {
33823583 try wip_nav.refType(.usize);
3383 try uleb128(diw, 0);
3584 try diw.writeUleb128(0);
33843585 },
33853586 }
33863587
33873588 try wip_nav.abbrevCode(.unsigned_tagged_union_field);
3388 try uleb128(diw, 0);
3589 try diw.writeUleb128(0);
33893590 {
33903591 try wip_nav.abbrevCode(.generated_field);
33913592 try wip_nav.strp("null");
33923593 try wip_nav.refType(.null);
3393 try uleb128(diw, 0);
3594 try diw.writeUleb128(0);
33943595 }
3395 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3596 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
33963597
33973598 try wip_nav.abbrevCode(.tagged_union_default_field);
33983599 {
33993600 try wip_nav.abbrevCode(.generated_field);
34003601 try wip_nav.strp("?");
34013602 try wip_nav.refType(opt_child_type);
3402 try uleb128(diw, 0);
3603 try diw.writeUleb128(0);
34033604 }
3404 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3605 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
34053606 }
3406 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3607 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
34073608 },
34083609 }
3409 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3610 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
34103611 },
34113612 .anyframe_type => unreachable,
34123613 .error_union_type => |error_union_type| {
......@@ -3425,11 +3626,11 @@ fn updateLazyType(
34253626 if (error_union_type.error_set_type != .generic_poison_type and
34263627 error_union_type.payload_type != .generic_poison_type)
34273628 {
3428 try uleb128(diw, ty.abiSize(zcu));
3429 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3629 try diw.writeUleb128(ty.abiSize(zcu));
3630 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
34303631 } else {
3431 try uleb128(diw, 0);
3432 try uleb128(diw, 1);
3632 try diw.writeUleb128(0);
3633 try diw.writeUleb128(1);
34333634 }
34343635 {
34353636 try wip_nav.abbrevCode(.tagged_union);
......@@ -3437,7 +3638,7 @@ fn updateLazyType(
34373638 .debug_info,
34383639 wip_nav.unit,
34393640 wip_nav.entry,
3440 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3641 @intCast(diw.end + dwarf.sectionOffsetBytes()),
34413642 );
34423643 {
34433644 try wip_nav.abbrevCode(.generated_field);
......@@ -3446,30 +3647,30 @@ fn updateLazyType(
34463647 .signedness = .unsigned,
34473648 .bits = zcu.errorSetBits(),
34483649 } })));
3449 try uleb128(diw, error_union_error_set_offset);
3650 try diw.writeUleb128(error_union_error_set_offset);
34503651
34513652 try wip_nav.abbrevCode(.unsigned_tagged_union_field);
3452 try uleb128(diw, 0);
3653 try diw.writeUleb128(0);
34533654 {
34543655 try wip_nav.abbrevCode(.generated_field);
34553656 try wip_nav.strp("value");
34563657 try wip_nav.refType(error_union_payload_type);
3457 try uleb128(diw, error_union_payload_offset);
3658 try diw.writeUleb128(error_union_payload_offset);
34583659 }
3459 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3660 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
34603661
34613662 try wip_nav.abbrevCode(.tagged_union_default_field);
34623663 {
34633664 try wip_nav.abbrevCode(.generated_field);
34643665 try wip_nav.strp("error");
34653666 try wip_nav.refType(error_union_error_set_type);
3466 try uleb128(diw, error_union_error_set_offset);
3667 try diw.writeUleb128(error_union_error_set_offset);
34673668 }
3468 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3669 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
34693670 }
3470 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3671 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
34713672 }
3472 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3673 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
34733674 },
34743675 .simple_type => |simple_type| switch (simple_type) {
34753676 .f16,
......@@ -3503,9 +3704,9 @@ fn updateLazyType(
35033704 DW.ATE.unsigned
35043705 else
35053706 unreachable);
3506 try uleb128(diw, ty.bitSize(zcu));
3507 try uleb128(diw, ty.abiSize(zcu));
3508 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3707 try diw.writeUleb128(ty.bitSize(zcu));
3708 try diw.writeUleb128(ty.abiSize(zcu));
3709 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
35093710 },
35103711 .anyopaque,
35113712 .void,
......@@ -3535,8 +3736,8 @@ fn updateLazyType(
35353736 } else {
35363737 try wip_nav.abbrevCode(.generated_struct_type);
35373738 try wip_nav.strp(name);
3538 try uleb128(diw, ty.abiSize(zcu));
3539 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3739 try diw.writeUleb128(ty.abiSize(zcu));
3740 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
35403741 var field_byte_offset: u64 = 0;
35413742 for (0..tuple_type.types.len) |field_index| {
35423743 const comptime_value = tuple_type.values.get(ip)[field_index];
......@@ -3565,8 +3766,8 @@ fn updateLazyType(
35653766 if (comptime_value == .none) {
35663767 const field_align = field_type.abiAlignment(zcu);
35673768 field_byte_offset = field_align.forward(field_byte_offset);
3568 try uleb128(diw, field_byte_offset);
3569 try uleb128(diw, field_type.abiAlignment(zcu).toByteUnits().?);
3769 try diw.writeUleb128(field_byte_offset);
3770 try diw.writeUleb128(field_type.abiAlignment(zcu).toByteUnits().?);
35703771 field_byte_offset += field_type.abiSize(zcu);
35713772 }
35723773 if (has_comptime_state)
......@@ -3574,7 +3775,7 @@ fn updateLazyType(
35743775 else if (has_runtime_bits)
35753776 try wip_nav.blockValue(src_loc, .fromInterned(comptime_value));
35763777 }
3577 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3778 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
35783779 },
35793780 .enum_type => {
35803781 const loaded_enum = ip.loadEnumType(type_index);
......@@ -3589,7 +3790,7 @@ fn updateLazyType(
35893790 }, field_index);
35903791 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
35913792 }
3592 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
3793 if (loaded_enum.names.len > 0) try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
35933794 },
35943795 .func_type => |func_type| {
35953796 const is_nullary = func_type.param_types.len == 0 and !func_type.is_var_args;
......@@ -3665,7 +3866,7 @@ fn updateLazyType(
36653866 try wip_nav.refType(.fromInterned(func_type.param_types.get(ip)[param_index]));
36663867 }
36673868 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3668 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3869 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
36693870 }
36703871 },
36713872 .error_set_type => |error_set_type| {
......@@ -3678,10 +3879,10 @@ fn updateLazyType(
36783879 for (0..error_set_type.names.len) |field_index| {
36793880 const field_name = error_set_type.names.get(ip)[field_index];
36803881 try wip_nav.abbrevCode(.unsigned_enum_field);
3681 try uleb128(diw, ip.getErrorValueIfExists(field_name).?);
3882 try diw.writeUleb128(ip.getErrorValueIfExists(field_name).?);
36823883 try wip_nav.strp(field_name.toSlice(ip));
36833884 }
3684 if (error_set_type.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
3885 if (error_set_type.names.len > 0) try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
36853886 },
36863887 .inferred_error_set_type => |func| {
36873888 try wip_nav.abbrevCode(.inferred_error_set_type);
......@@ -3713,7 +3914,7 @@ fn updateLazyType(
37133914 .memoized_call,
37143915 => unreachable,
37153916 }
3716 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
3917 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
37173918}
37183919
37193920fn updateLazyValue(
......@@ -3722,7 +3923,7 @@ fn updateLazyValue(
37223923 src_loc: Zcu.LazySrcLoc,
37233924 value_index: InternPool.Index,
37243925 pending_lazy: *WipNav.PendingLazy,
3725) UpdateError!void {
3926) (UpdateError || Writer.Error)!void {
37263927 const zcu = pt.zcu;
37273928 const ip = &zcu.intern_pool;
37283929 assert(ip.typeOf(value_index) != .type_type);
......@@ -3741,10 +3942,10 @@ fn updateLazyValue(
37413942 .func_high_pc = undefined,
37423943 .blocks = undefined,
37433944 .cfi = undefined,
3744 .debug_frame = .empty,
3745 .debug_info = .empty,
3746 .debug_line = .empty,
3747 .debug_loclists = .empty,
3945 .debug_frame = .init(dwarf.gpa),
3946 .debug_info = .init(dwarf.gpa),
3947 .debug_line = .init(dwarf.gpa),
3948 .debug_loclists = .init(dwarf.gpa),
37483949 .pending_lazy = pending_lazy.*,
37493950 };
37503951 defer {
......@@ -3752,7 +3953,7 @@ fn updateLazyValue(
37523953 wip_nav.pending_lazy = .empty;
37533954 wip_nav.deinit();
37543955 }
3755 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3956 const diw = &wip_nav.debug_info.writer;
37563957 var big_int_space: Value.BigIntSpace = undefined;
37573958 switch (ip.indexToKey(value_index)) {
37583959 .int_type,
......@@ -3790,20 +3991,21 @@ fn updateLazyValue(
37903991 .err => |err| {
37913992 try wip_nav.abbrevCode(.udata_comptime_value);
37923993 try wip_nav.refType(.fromInterned(err.ty));
3793 try uleb128(diw, try pt.getErrorValue(err.name));
3994 try diw.writeUleb128(try pt.getErrorValue(err.name));
37943995 },
37953996 .error_union => |error_union| {
37963997 try wip_nav.abbrevCode(.aggregate_comptime_value);
3797 const err_abi_size = std.math.divCeil(u17, zcu.errorSetBits(), 8) catch unreachable;
3798 const err_value = switch (error_union.val) {
3998 var err_buf: [4]u8 = undefined;
3999 const err_bytes = err_buf[0 .. std.math.divCeil(u17, zcu.errorSetBits(), 8) catch unreachable];
4000 dwarf.writeInt(err_bytes, switch (error_union.val) {
37994001 .err_name => |err_name| try pt.getErrorValue(err_name),
38004002 .payload => 0,
3801 };
4003 });
38024004 {
38034005 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
38044006 try wip_nav.strp("is_error");
3805 try uleb128(diw, err_abi_size);
3806 dwarf.writeInt(try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, err_abi_size), err_value);
4007 try diw.writeUleb128(err_bytes.len);
4008 try diw.writeAll(err_bytes);
38074009 }
38084010 payload_field: switch (error_union.val) {
38094011 .err_name => {},
......@@ -3827,18 +4029,11 @@ fn updateLazyValue(
38274029 {
38284030 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
38294031 try wip_nav.strp("error");
3830 try uleb128(diw, err_abi_size);
3831 dwarf.writeInt(try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, err_abi_size), err_value);
3832 }
3833 switch (error_union.val) {
3834 .err_name => {},
3835 .payload => |payload| {
3836 _ = payload;
3837 try wip_nav.abbrevCode(.aggregate_comptime_value);
3838 },
4032 try diw.writeUleb128(err_bytes.len);
4033 try diw.writeAll(err_bytes);
38394034 }
38404035 try wip_nav.refType(.fromInterned(error_union.ty));
3841 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4036 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
38424037 },
38434038 .enum_literal => |enum_literal| {
38444039 try wip_nav.abbrevCode(.string_comptime_value);
......@@ -3871,7 +4066,7 @@ fn updateLazyValue(
38714066 },
38724067 .f80 => |f80_val| {
38734068 try wip_nav.abbrevCode(.block_comptime_value);
3874 try uleb128(diw, @divExact(80, 8));
4069 try diw.writeUleb128(@divExact(80, 8));
38754070 try diw.writeInt(u80, @bitCast(f80_val), dwarf.endian);
38764071 },
38774072 .f128 => |f128_val| {
......@@ -3893,14 +4088,14 @@ fn updateLazyValue(
38934088 const uav_ty: Type = .fromInterned(ip.typeOf(uav.val));
38944089 if (try uav_ty.onePossibleValue(pt)) |_| {
38954090 try wip_nav.abbrevCode(.udata_comptime_value);
3896 try uleb128(diw, ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment.toByteUnits() orelse
4091 try diw.writeUleb128(ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment.toByteUnits() orelse
38974092 uav_ty.abiAlignment(zcu).toByteUnits().?);
38984093 break :location;
38994094 } else break try wip_nav.getValueEntry(.fromInterned(uav.val));
39004095 },
39014096 .int => {
39024097 try wip_nav.abbrevCode(.udata_comptime_value);
3903 try uleb128(diw, byte_offset);
4098 try diw.writeUleb128(byte_offset);
39044099 break :location;
39054100 },
39064101 .eu_payload => |eu_ptr| {
......@@ -3939,7 +4134,7 @@ fn updateLazyValue(
39394134 try wip_nav.strp("len");
39404135 try wip_nav.blockValue(src_loc, .fromInterned(slice.len));
39414136 }
3942 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4137 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
39434138 },
39444139 .opt => |opt| {
39454140 const opt_child_type: Type = .fromInterned(ip.indexToKey(opt.ty).opt_type);
......@@ -3949,7 +4144,7 @@ fn updateLazyValue(
39494144 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
39504145 try wip_nav.strp("has_value");
39514146 switch (optRepr(opt_child_type, zcu)) {
3952 .opv_null => try uleb128(diw, 0),
4147 .opv_null => try diw.writeUleb128(0),
39534148 .unpacked => try wip_nav.blockValue(src_loc, .makeBool(opt.val != .none)),
39544149 .error_set => try wip_nav.blockValue(src_loc, .fromInterned(value_index)),
39554150 .pointer => if (opt_child_type.comptimeOnly(zcu)) {
......@@ -3959,7 +4154,7 @@ fn updateLazyValue(
39594154 .none => 0,
39604155 else => opt_child_type.ptrAlignment(zcu).toByteUnits().?,
39614156 });
3962 try uleb128(diw, bytes.len);
4157 try diw.writeUleb128(bytes.len);
39634158 try diw.writeAll(bytes);
39644159 } else try wip_nav.blockValue(src_loc, .fromInterned(value_index)),
39654160 }
......@@ -3979,7 +4174,7 @@ fn updateLazyValue(
39794174 else
39804175 try wip_nav.blockValue(src_loc, .fromInterned(opt.val));
39814176 }
3982 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4177 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
39834178 },
39844179 .aggregate => |aggregate| {
39854180 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -4064,7 +4259,7 @@ fn updateLazyValue(
40644259 },
40654260 else => unreachable,
40664261 }
4067 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4262 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
40684263 },
40694264 .un => |un| {
40704265 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -4089,11 +4284,11 @@ fn updateLazyValue(
40894284 else
40904285 try wip_nav.blockValue(src_loc, .fromInterned(un.val));
40914286 }
4092 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4287 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
40934288 },
40944289 .memoized_call => unreachable, // not a value
40954290 }
4096 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
4291 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
40974292}
40984293
40994294fn optRepr(opt_child_type: Type, zcu: *const Zcu) enum {
......@@ -4113,7 +4308,21 @@ fn optRepr(opt_child_type: Type, zcu: *const Zcu) enum {
41134308 };
41144309}
41154310
4116pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternPool.Index) UpdateError!void {
4311pub fn updateContainerType(
4312 dwarf: *Dwarf,
4313 pt: Zcu.PerThread,
4314 type_index: InternPool.Index,
4315) UpdateError!void {
4316 return dwarf.updateContainerTypeWriterError(pt, type_index) catch |err| switch (err) {
4317 error.WriteFailed => error.OutOfMemory,
4318 else => |e| e,
4319 };
4320}
4321fn updateContainerTypeWriterError(
4322 dwarf: *Dwarf,
4323 pt: Zcu.PerThread,
4324 type_index: InternPool.Index,
4325) (UpdateError || Writer.Error)!void {
41174326 const zcu = pt.zcu;
41184327 const ip = &zcu.intern_pool;
41194328 const ty: Type = .fromInterned(type_index);
......@@ -4138,23 +4347,23 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
41384347 .func_high_pc = undefined,
41394348 .blocks = undefined,
41404349 .cfi = undefined,
4141 .debug_frame = .empty,
4142 .debug_info = .empty,
4143 .debug_line = .empty,
4144 .debug_loclists = .empty,
4350 .debug_frame = .init(dwarf.gpa),
4351 .debug_info = .init(dwarf.gpa),
4352 .debug_line = .init(dwarf.gpa),
4353 .debug_loclists = .init(dwarf.gpa),
41454354 .pending_lazy = .empty,
41464355 };
41474356 defer wip_nav.deinit();
41484357
41494358 const loaded_struct = ip.loadStructType(type_index);
41504359
4151 const diw = wip_nav.debug_info.writer(dwarf.gpa);
4360 const diw = &wip_nav.debug_info.writer;
41524361 try wip_nav.abbrevCode(if (loaded_struct.field_types.len == 0) .empty_file else .file);
4153 try uleb128(diw, file_gop.index);
4362 try diw.writeUleb128(file_gop.index);
41544363 try wip_nav.strp(loaded_struct.name.toSlice(ip));
41554364 if (loaded_struct.field_types.len > 0) {
4156 try uleb128(diw, ty.abiSize(zcu));
4157 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
4365 try diw.writeUleb128(ty.abiSize(zcu));
4366 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
41584367 for (0..loaded_struct.field_types.len) |field_index| {
41594368 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
41604369 const field_init = loaded_struct.fieldInit(ip, field_index);
......@@ -4190,8 +4399,8 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
41904399 }
41914400 try wip_nav.refType(field_type);
41924401 if (!is_comptime) {
4193 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
4194 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
4402 try diw.writeUleb128(loaded_struct.offsets.get(ip)[field_index]);
4403 try diw.writeUleb128(loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
41954404 field_type.abiAlignment(zcu).toByteUnits().?);
41964405 }
41974406 if (has_comptime_state)
......@@ -4199,10 +4408,10 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
41994408 else if (has_runtime_bits)
42004409 try wip_nav.blockValue(ty_src_loc, .fromInterned(field_init));
42014410 }
4202 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4411 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
42034412 }
42044413
4205 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
4414 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
42064415 try wip_nav.updateLazy(ty_src_loc);
42074416 } else {
42084417 {
......@@ -4239,14 +4448,14 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
42394448 .func_high_pc = undefined,
42404449 .blocks = undefined,
42414450 .cfi = undefined,
4242 .debug_frame = .empty,
4243 .debug_info = .empty,
4244 .debug_line = .empty,
4245 .debug_loclists = .empty,
4451 .debug_frame = .init(dwarf.gpa),
4452 .debug_info = .init(dwarf.gpa),
4453 .debug_line = .init(dwarf.gpa),
4454 .debug_loclists = .init(dwarf.gpa),
42464455 .pending_lazy = .empty,
42474456 };
42484457 defer wip_nav.deinit();
4249 const diw = wip_nav.debug_info.writer(dwarf.gpa);
4458 const diw = &wip_nav.debug_info.writer;
42504459 const name = try std.fmt.allocPrint(dwarf.gpa, "{f}", .{ty.fmt(pt)});
42514460 defer dwarf.gpa.free(name);
42524461
......@@ -4256,11 +4465,11 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
42564465 switch (loaded_struct.layout) {
42574466 .auto, .@"extern" => {
42584467 try wip_nav.abbrevCode(if (loaded_struct.field_types.len == 0) .empty_struct_type else .struct_type);
4259 try uleb128(diw, file_gop.index);
4468 try diw.writeUleb128(file_gop.index);
42604469 try wip_nav.strp(name);
42614470 if (loaded_struct.field_types.len == 0) try diw.writeByte(@intFromBool(false)) else {
4262 try uleb128(diw, ty.abiSize(zcu));
4263 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
4471 try diw.writeUleb128(ty.abiSize(zcu));
4472 try diw.writeUleb128(ty.abiAlignment(zcu).toByteUnits().?);
42644473 for (0..loaded_struct.field_types.len) |field_index| {
42654474 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
42664475 const field_init = loaded_struct.fieldInit(ip, field_index);
......@@ -4296,8 +4505,8 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
42964505 }
42974506 try wip_nav.refType(field_type);
42984507 if (!is_comptime) {
4299 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
4300 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
4508 try diw.writeUleb128(loaded_struct.offsets.get(ip)[field_index]);
4509 try diw.writeUleb128(loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
43014510 field_type.abiAlignment(zcu).toByteUnits().?);
43024511 }
43034512 if (has_comptime_state)
......@@ -4305,12 +4514,12 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
43054514 else if (has_runtime_bits)
43064515 try wip_nav.blockValue(ty_src_loc, .fromInterned(field_init));
43074516 }
4308 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4517 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
43094518 }
43104519 },
43114520 .@"packed" => {
43124521 try wip_nav.abbrevCode(if (loaded_struct.field_types.len > 0) .packed_struct_type else .empty_packed_struct_type);
4313 try uleb128(diw, file_gop.index);
4522 try diw.writeUleb128(file_gop.index);
43144523 try wip_nav.strp(name);
43154524 try wip_nav.refType(.fromInterned(loaded_struct.backingIntTypeUnordered(ip)));
43164525 var field_bit_offset: u16 = 0;
......@@ -4319,17 +4528,17 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
43194528 try wip_nav.strp(loaded_struct.fieldName(ip, field_index).unwrap().?.toSlice(ip));
43204529 const field_type: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
43214530 try wip_nav.refType(field_type);
4322 try uleb128(diw, field_bit_offset);
4531 try diw.writeUleb128(field_bit_offset);
43234532 field_bit_offset += @intCast(field_type.bitSize(zcu));
43244533 }
4325 if (loaded_struct.field_types.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
4534 if (loaded_struct.field_types.len > 0) try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
43264535 },
43274536 }
43284537 },
43294538 .enum_type => {
43304539 const loaded_enum = ip.loadEnumType(type_index);
43314540 try wip_nav.abbrevCode(if (loaded_enum.names.len > 0) .enum_type else .empty_enum_type);
4332 try uleb128(diw, file_gop.index);
4541 try diw.writeUleb128(file_gop.index);
43334542 try wip_nav.strp(name);
43344543 try wip_nav.refType(.fromInterned(loaded_enum.tag_ty));
43354544 for (0..loaded_enum.names.len) |field_index| {
......@@ -4340,16 +4549,16 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
43404549 }, field_index);
43414550 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
43424551 }
4343 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
4552 if (loaded_enum.names.len > 0) try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
43444553 },
43454554 .union_type => {
43464555 const loaded_union = ip.loadUnionType(type_index);
43474556 try wip_nav.abbrevCode(if (loaded_union.field_types.len > 0) .union_type else .empty_union_type);
4348 try uleb128(diw, file_gop.index);
4557 try diw.writeUleb128(file_gop.index);
43494558 try wip_nav.strp(name);
43504559 const union_layout = Type.getUnionLayout(loaded_union, zcu);
4351 try uleb128(diw, union_layout.abi_size);
4352 try uleb128(diw, union_layout.abi_align.toByteUnits().?);
4560 try diw.writeUleb128(union_layout.abi_size);
4561 try diw.writeUleb128(union_layout.abi_align.toByteUnits().?);
43534562 const loaded_tag = loaded_union.loadTagType(ip);
43544563 if (loaded_union.hasTag(ip)) {
43554564 try wip_nav.abbrevCode(.tagged_union);
......@@ -4357,13 +4566,13 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
43574566 .debug_info,
43584567 wip_nav.unit,
43594568 wip_nav.entry,
4360 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
4569 @intCast(diw.end + dwarf.sectionOffsetBytes()),
43614570 );
43624571 {
43634572 try wip_nav.abbrevCode(.generated_field);
43644573 try wip_nav.strp("tag");
43654574 try wip_nav.refType(.fromInterned(loaded_union.enum_tag_ty));
4366 try uleb128(diw, union_layout.tagOffset());
4575 try diw.writeUleb128(union_layout.tagOffset());
43674576
43684577 for (0..loaded_union.field_types.len) |field_index| {
43694578 try wip_nav.enumConstValue(loaded_tag, .{
......@@ -4376,34 +4585,34 @@ pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternP
43764585 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
43774586 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
43784587 try wip_nav.refType(field_type);
4379 try uleb128(diw, union_layout.payloadOffset());
4380 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
4588 try diw.writeUleb128(union_layout.payloadOffset());
4589 try diw.writeUleb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
43814590 if (field_type.isNoReturn(zcu)) 1 else field_type.abiAlignment(zcu).toByteUnits().?);
43824591 }
4383 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4592 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
43844593 }
43854594 }
4386 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4595 try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
43874596 } else for (0..loaded_union.field_types.len) |field_index| {
43884597 try wip_nav.abbrevCode(.untagged_union_field);
43894598 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
43904599 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
43914600 try wip_nav.refType(field_type);
4392 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
4601 try diw.writeUleb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
43934602 field_type.abiAlignment(zcu).toByteUnits().?);
43944603 }
4395 if (loaded_union.field_types.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
4604 if (loaded_union.field_types.len > 0) try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
43964605 },
43974606 .opaque_type => {
43984607 try wip_nav.abbrevCode(.empty_struct_type);
4399 try uleb128(diw, file_gop.index);
4608 try diw.writeUleb128(file_gop.index);
44004609 try wip_nav.strp(name);
44014610 try diw.writeByte(@intFromBool(true));
44024611 },
44034612 else => unreachable,
44044613 }
4405 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
4406 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.items);
4614 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
4615 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.getWritten());
44074616 try wip_nav.updateLazy(ty_src_loc);
44084617 }
44094618}
......@@ -4436,24 +4645,33 @@ pub fn freeNav(dwarf: *Dwarf, nav_index: InternPool.Nav.Index) void {
44364645 _ = nav_index;
44374646}
44384647
4439fn refAbbrevCode(dwarf: *Dwarf, abbrev_code: AbbrevCode) UpdateError!@typeInfo(AbbrevCode).@"enum".tag_type {
4648fn refAbbrevCode(
4649 dwarf: *Dwarf,
4650 abbrev_code: AbbrevCode,
4651) (UpdateError || Writer.Error)!@typeInfo(AbbrevCode).@"enum".tag_type {
44404652 assert(abbrev_code != .null);
44414653 const entry: Entry.Index = @enumFromInt(@intFromEnum(abbrev_code));
44424654 if (dwarf.debug_abbrev.section.getUnit(DebugAbbrev.unit).getEntry(entry).len > 0) return @intFromEnum(abbrev_code);
4443 var debug_abbrev: std.ArrayList(u8) = .init(dwarf.gpa);
4444 defer debug_abbrev.deinit();
4445 const daw = debug_abbrev.writer();
4655 var debug_abbrev_aw: Writer.Allocating = .init(dwarf.gpa);
4656 defer debug_abbrev_aw.deinit();
4657 const daw = &debug_abbrev_aw.writer;
44464658 const abbrev = AbbrevCode.abbrevs.get(abbrev_code);
4447 try uleb128(daw, @intFromEnum(abbrev_code));
4448 try uleb128(daw, @intFromEnum(abbrev.tag));
4659 try daw.writeUleb128(@intFromEnum(abbrev_code));
4660 try daw.writeUleb128(@intFromEnum(abbrev.tag));
44494661 try daw.writeByte(if (abbrev.children) DW.CHILDREN.yes else DW.CHILDREN.no);
4450 for (abbrev.attrs) |*attr| inline for (attr) |info| try uleb128(daw, @intFromEnum(info));
4451 for (0..2) |_| try uleb128(daw, 0);
4452 try dwarf.debug_abbrev.section.replaceEntry(DebugAbbrev.unit, entry, dwarf, debug_abbrev.items);
4662 for (abbrev.attrs) |*attr| inline for (attr) |info| try daw.writeUleb128(@intFromEnum(info));
4663 for (0..2) |_| try daw.writeUleb128(0);
4664 try dwarf.debug_abbrev.section.replaceEntry(DebugAbbrev.unit, entry, dwarf, debug_abbrev_aw.getWritten());
44534665 return @intFromEnum(abbrev_code);
44544666}
44554667
44564668pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
4669 return dwarf.flushWriterError(pt) catch |err| switch (err) {
4670 error.WriteFailed => error.OutOfMemory,
4671 else => |e| e,
4672 };
4673}
4674fn flushWriterError(dwarf: *Dwarf, pt: Zcu.PerThread) (FlushError || Writer.Error)!void {
44574675 const zcu = pt.zcu;
44584676 const ip = &zcu.intern_pool;
44594677
......@@ -4471,14 +4689,14 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
44714689 .func_high_pc = undefined,
44724690 .blocks = undefined,
44734691 .cfi = undefined,
4474 .debug_frame = .empty,
4475 .debug_info = .empty,
4476 .debug_line = .empty,
4477 .debug_loclists = .empty,
4692 .debug_frame = .init(dwarf.gpa),
4693 .debug_info = .init(dwarf.gpa),
4694 .debug_line = .init(dwarf.gpa),
4695 .debug_loclists = .init(dwarf.gpa),
44784696 .pending_lazy = .empty,
44794697 };
44804698 defer wip_nav.deinit();
4481 const diw = wip_nav.debug_info.writer(dwarf.gpa);
4699 const diw = &wip_nav.debug_info.writer;
44824700 const global_error_set_names = ip.global_error_set.getNamesFromMainThread();
44834701 try wip_nav.abbrevCode(if (global_error_set_names.len == 0) .generated_empty_enum_type else .generated_enum_type);
44844702 try wip_nav.strp("anyerror");
......@@ -4488,11 +4706,11 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
44884706 } })));
44894707 for (global_error_set_names, 1..) |name, value| {
44904708 try wip_nav.abbrevCode(.unsigned_enum_field);
4491 try uleb128(diw, value);
4709 try diw.writeUleb128(value);
44924710 try wip_nav.strp(name.toSlice(ip));
44934711 }
4494 if (global_error_set_names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
4495 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
4712 if (global_error_set_names.len > 0) try diw.writeUleb128(@intFromEnum(AbbrevCode.null));
4713 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
44964714 try wip_nav.updateLazy(.unneeded);
44974715 }
44984716
......@@ -4502,36 +4720,38 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
45024720 mod_info.root_dir_path = try dwarf.debug_line_str.addString(dwarf, root_dir_path);
45034721 }
45044722
4505 var header: std.ArrayList(u8) = .init(dwarf.gpa);
4506 defer header.deinit();
4723 var header_aw: Writer.Allocating = .init(dwarf.gpa);
4724 defer header_aw.deinit();
4725 const hw = &header_aw.writer;
45074726 if (dwarf.debug_aranges.section.dirty) {
45084727 for (dwarf.debug_aranges.section.units.items, 0..) |*unit_ptr, unit_index| {
45094728 const unit: Unit.Index = @enumFromInt(unit_index);
45104729 unit_ptr.clear();
45114730 try unit_ptr.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 1);
4512 header.clearRetainingCapacity();
4513 try header.ensureTotalCapacity(unit_ptr.header_len);
4731 header_aw.clearRetainingCapacity();
4732 try header_aw.ensureTotalCapacity(unit_ptr.header_len);
45144733 const unit_len = (if (unit_ptr.next.unwrap()) |next_unit|
45154734 dwarf.debug_aranges.section.getUnit(next_unit).off
45164735 else
45174736 dwarf.debug_aranges.section.len) - unit_ptr.off - dwarf.unitLengthBytes();
45184737 switch (dwarf.format) {
4519 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4738 .@"32" => hw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
45204739 .@"64" => {
4521 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4522 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
4740 hw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4741 hw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
45234742 },
45244743 }
4525 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 2, dwarf.endian);
4744 hw.writeInt(u16, 2, dwarf.endian) catch unreachable;
45264745 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4527 .source_off = @intCast(header.items.len),
4746 .source_off = @intCast(hw.end),
45284747 .target_sec = .debug_info,
45294748 .target_unit = unit,
45304749 });
4531 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4532 header.appendSliceAssumeCapacity(&.{ @intFromEnum(dwarf.address_size), 0 });
4533 header.appendNTimesAssumeCapacity(0, unit_ptr.header_len - header.items.len);
4534 try unit_ptr.replaceHeader(&dwarf.debug_aranges.section, dwarf, header.items);
4750 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4751 hw.writeByte(@intFromEnum(dwarf.address_size)) catch unreachable;
4752 hw.writeByte(0) catch unreachable;
4753 hw.splatByteAll(0, unit_ptr.header_len - hw.end) catch unreachable;
4754 try unit_ptr.replaceHeader(&dwarf.debug_aranges.section, dwarf, header_aw.getWritten());
45354755 try unit_ptr.writeTrailer(&dwarf.debug_aranges.section, dwarf);
45364756 }
45374757 dwarf.debug_aranges.section.dirty = false;
......@@ -4546,31 +4766,31 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
45464766 dev.check(.x86_64_backend);
45474767 const Register = @import("../arch/x86_64/bits.zig").Register;
45484768 for (dwarf.debug_frame.section.units.items) |*unit| {
4549 header.clearRetainingCapacity();
4550 try header.ensureTotalCapacity(unit.header_len);
4769 header_aw.clearRetainingCapacity();
4770 try header_aw.ensureTotalCapacity(unit.header_len);
45514771 const unit_len = unit.header_len - dwarf.unitLengthBytes();
45524772 switch (dwarf.format) {
4553 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4773 .@"32" => hw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
45544774 .@"64" => {
4555 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4556 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
4775 hw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4776 hw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
45574777 },
45584778 }
4559 header.appendNTimesAssumeCapacity(0, 4);
4560 header.appendAssumeCapacity(1);
4561 header.appendSliceAssumeCapacity("zR\x00");
4562 uleb128(header.fixedWriter(), dwarf.debug_frame.header.code_alignment_factor) catch unreachable;
4563 sleb128(header.fixedWriter(), dwarf.debug_frame.header.data_alignment_factor) catch unreachable;
4564 uleb128(header.fixedWriter(), dwarf.debug_frame.header.return_address_register) catch unreachable;
4565 uleb128(header.fixedWriter(), 1) catch unreachable;
4566 header.appendAssumeCapacity(DW.EH.PE.pcrel | DW.EH.PE.sdata4);
4567 header.appendAssumeCapacity(DW.CFA.def_cfa_sf);
4568 uleb128(header.fixedWriter(), Register.rsp.dwarfNum()) catch unreachable;
4569 sleb128(header.fixedWriter(), -1) catch unreachable;
4570 header.appendAssumeCapacity(@as(u8, DW.CFA.offset) + Register.rip.dwarfNum());
4571 uleb128(header.fixedWriter(), 1) catch unreachable;
4572 header.appendNTimesAssumeCapacity(DW.CFA.nop, unit.header_len - header.items.len);
4573 try unit.replaceHeader(&dwarf.debug_frame.section, dwarf, header.items);
4779 hw.splatByteAll(0, 4) catch unreachable;
4780 hw.writeByte(1) catch unreachable;
4781 hw.writeAll("zR\x00") catch unreachable;
4782 hw.writeUleb128(dwarf.debug_frame.header.code_alignment_factor) catch unreachable;
4783 hw.writeSleb128(dwarf.debug_frame.header.data_alignment_factor) catch unreachable;
4784 hw.writeUleb128(dwarf.debug_frame.header.return_address_register) catch unreachable;
4785 hw.writeUleb128(1) catch unreachable;
4786 hw.writeByte(DW.EH.PE.pcrel | DW.EH.PE.sdata4) catch unreachable;
4787 hw.writeByte(DW.CFA.def_cfa_sf) catch unreachable;
4788 hw.writeUleb128(Register.rsp.dwarfNum()) catch unreachable;
4789 hw.writeSleb128(-1) catch unreachable;
4790 hw.writeByte(@as(u8, DW.CFA.offset) + Register.rip.dwarfNum()) catch unreachable;
4791 hw.writeUleb128(1) catch unreachable;
4792 hw.splatByteAll(DW.CFA.nop, unit.header_len - hw.end) catch unreachable;
4793 try unit.replaceHeader(&dwarf.debug_frame.section, dwarf, header_aw.getWritten());
45744794 try unit.writeTrailer(&dwarf.debug_frame.section, dwarf);
45754795 }
45764796 },
......@@ -4585,81 +4805,82 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
45854805 unit_ptr.clear();
45864806 try unit_ptr.cross_unit_relocs.ensureTotalCapacity(dwarf.gpa, 1);
45874807 try unit_ptr.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 7);
4588 header.clearRetainingCapacity();
4589 try header.ensureTotalCapacity(unit_ptr.header_len);
4808 header_aw.clearRetainingCapacity();
4809 try header_aw.ensureTotalCapacity(unit_ptr.header_len);
45904810 const unit_len = (if (unit_ptr.next.unwrap()) |next_unit|
45914811 dwarf.debug_info.section.getUnit(next_unit).off
45924812 else
45934813 dwarf.debug_info.section.len) - unit_ptr.off - dwarf.unitLengthBytes();
45944814 switch (dwarf.format) {
4595 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4815 .@"32" => hw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
45964816 .@"64" => {
4597 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4598 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
4817 hw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4818 hw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
45994819 },
46004820 }
4601 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 5, dwarf.endian);
4602 header.appendSliceAssumeCapacity(&.{ DW.UT.compile, @intFromEnum(dwarf.address_size) });
4821 hw.writeInt(u16, 5, dwarf.endian) catch unreachable;
4822 hw.writeByte(DW.UT.compile) catch unreachable;
4823 hw.writeByte(@intFromEnum(dwarf.address_size)) catch unreachable;
46034824 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4604 .source_off = @intCast(header.items.len),
4825 .source_off = @intCast(hw.end),
46054826 .target_sec = .debug_abbrev,
46064827 .target_unit = DebugAbbrev.unit,
46074828 });
4608 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4609 const compile_unit_off: u32 = @intCast(header.items.len);
4610 uleb128(header.fixedWriter(), try dwarf.refAbbrevCode(.compile_unit)) catch unreachable;
4611 header.appendAssumeCapacity(DW.LANG.Zig);
4829 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4830 const compile_unit_off: u32 = @intCast(hw.end);
4831 hw.writeUleb128(try dwarf.refAbbrevCode(.compile_unit)) catch unreachable;
4832 hw.writeByte(DW.LANG.Zig) catch unreachable;
46124833 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4613 .source_off = @intCast(header.items.len),
4834 .source_off = @intCast(hw.end),
46144835 .target_sec = .debug_line_str,
46154836 .target_unit = StringSection.unit,
46164837 .target_entry = (try dwarf.debug_line_str.addString(dwarf, "zig " ++ @import("build_options").version)).toOptional(),
46174838 });
4618 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4839 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46194840 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4620 .source_off = @intCast(header.items.len),
4841 .source_off = @intCast(hw.end),
46214842 .target_sec = .debug_line_str,
46224843 .target_unit = StringSection.unit,
46234844 .target_entry = mod_info.root_dir_path.toOptional(),
46244845 });
4625 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4846 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46264847 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4627 .source_off = @intCast(header.items.len),
4848 .source_off = @intCast(hw.end),
46284849 .target_sec = .debug_line_str,
46294850 .target_unit = StringSection.unit,
46304851 .target_entry = (try dwarf.debug_line_str.addString(dwarf, mod.root_src_path)).toOptional(),
46314852 });
4632 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4853 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46334854 unit_ptr.cross_unit_relocs.appendAssumeCapacity(.{
4634 .source_off = @intCast(header.items.len),
4855 .source_off = @intCast(hw.end),
46354856 .target_unit = .main,
46364857 .target_off = compile_unit_off,
46374858 });
4638 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4859 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46394860 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4640 .source_off = @intCast(header.items.len),
4861 .source_off = @intCast(hw.end),
46414862 .target_sec = .debug_line,
46424863 .target_unit = unit,
46434864 });
4644 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4865 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46454866 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4646 .source_off = @intCast(header.items.len),
4867 .source_off = @intCast(hw.end),
46474868 .target_sec = .debug_rnglists,
46484869 .target_unit = unit,
46494870 .target_off = DebugRngLists.baseOffset(dwarf),
46504871 });
4651 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4652 uleb128(header.fixedWriter(), 0) catch unreachable;
4653 uleb128(header.fixedWriter(), try dwarf.refAbbrevCode(.module)) catch unreachable;
4872 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4873 hw.writeUleb128(0) catch unreachable;
4874 hw.writeUleb128(try dwarf.refAbbrevCode(.module)) catch unreachable;
46544875 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4655 .source_off = @intCast(header.items.len),
4876 .source_off = @intCast(hw.end),
46564877 .target_sec = .debug_str,
46574878 .target_unit = StringSection.unit,
46584879 .target_entry = (try dwarf.debug_str.addString(dwarf, mod.fully_qualified_name)).toOptional(),
46594880 });
4660 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4661 uleb128(header.fixedWriter(), 0) catch unreachable;
4662 try unit_ptr.replaceHeader(&dwarf.debug_info.section, dwarf, header.items);
4881 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4882 hw.writeUleb128(0) catch unreachable;
4883 try unit_ptr.replaceHeader(&dwarf.debug_info.section, dwarf, header_aw.getWritten());
46634884 try unit_ptr.writeTrailer(&dwarf.debug_info.section, dwarf);
46644885 }
46654886 dwarf.debug_info.section.dirty = false;
......@@ -4684,32 +4905,36 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
46844905 for (dwarf.mods.values(), dwarf.debug_line.section.units.items) |mod_info, *unit| {
46854906 unit.clear();
46864907 try unit.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, mod_info.dirs.count() + 2 * (mod_info.files.count()));
4687 header.clearRetainingCapacity();
4688 try header.ensureTotalCapacity(unit.header_len);
4908 header_aw.clearRetainingCapacity();
4909 try header_aw.ensureTotalCapacity(unit.header_len);
46894910 const unit_len = (if (unit.next.unwrap()) |next_unit|
46904911 dwarf.debug_line.section.getUnit(next_unit).off
46914912 else
46924913 dwarf.debug_line.section.len) - unit.off - dwarf.unitLengthBytes();
46934914 switch (dwarf.format) {
4694 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4915 .@"32" => hw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
46954916 .@"64" => {
4696 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4697 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
4917 hw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4918 hw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
46984919 },
46994920 }
4700 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 5, dwarf.endian);
4701 header.appendSliceAssumeCapacity(&.{ @intFromEnum(dwarf.address_size), 0 });
4702 dwarf.writeInt(header.addManyAsSliceAssumeCapacity(dwarf.sectionOffsetBytes()), unit.header_len - header.items.len);
4921 hw.writeInt(u16, 5, dwarf.endian) catch unreachable;
4922 hw.writeByte(@intFromEnum(dwarf.address_size)) catch unreachable;
4923 hw.writeByte(0) catch unreachable;
4924 switch (dwarf.format) {
4925 .@"32" => hw.writeInt(u32, @intCast(unit.header_len - hw.end - 4), dwarf.endian) catch unreachable,
4926 .@"64" => hw.writeInt(u64, @intCast(unit.header_len - hw.end - 8), dwarf.endian) catch unreachable,
4927 }
47034928 const StandardOpcode = DeclValEnum(DW.LNS);
4704 header.appendSliceAssumeCapacity(&[_]u8{
4929 hw.writeAll(&.{
47054930 dwarf.debug_line.header.minimum_instruction_length,
47064931 dwarf.debug_line.header.maximum_operations_per_instruction,
47074932 @intFromBool(dwarf.debug_line.header.default_is_stmt),
47084933 @bitCast(dwarf.debug_line.header.line_base),
47094934 dwarf.debug_line.header.line_range,
47104935 dwarf.debug_line.header.opcode_base,
4711 });
4712 header.appendSliceAssumeCapacity(std.enums.EnumArray(StandardOpcode, u8).init(.{
4936 }) catch unreachable;
4937 hw.writeAll(std.enums.EnumArray(StandardOpcode, u8).init(.{
47134938 .extended_op = undefined,
47144939 .copy = 0,
47154940 .advance_pc = 1,
......@@ -4723,44 +4948,46 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47234948 .set_prologue_end = 0,
47244949 .set_epilogue_begin = 0,
47254950 .set_isa = 1,
4726 }).values[1..dwarf.debug_line.header.opcode_base]);
4727 header.appendAssumeCapacity(1);
4728 uleb128(header.fixedWriter(), DW.LNCT.path) catch unreachable;
4729 uleb128(header.fixedWriter(), DW.FORM.line_strp) catch unreachable;
4730 uleb128(header.fixedWriter(), mod_info.dirs.count()) catch unreachable;
4951 }).values[1..dwarf.debug_line.header.opcode_base]) catch unreachable;
4952 hw.writeByte(1) catch unreachable;
4953 hw.writeUleb128(DW.LNCT.path) catch unreachable;
4954 hw.writeUleb128(DW.FORM.line_strp) catch unreachable;
4955 hw.writeUleb128(mod_info.dirs.count()) catch unreachable;
47314956 for (mod_info.dirs.keys()) |dir_unit| {
47324957 unit.cross_section_relocs.appendAssumeCapacity(.{
4733 .source_off = @intCast(header.items.len),
4958 .source_off = @intCast(hw.end),
47344959 .target_sec = .debug_line_str,
47354960 .target_unit = StringSection.unit,
47364961 .target_entry = dwarf.getModInfo(dir_unit).root_dir_path.toOptional(),
47374962 });
4738 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4963 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
47394964 }
47404965 const dir_index_info = DebugLine.dirIndexInfo(@intCast(mod_info.dirs.count()));
4741 header.appendAssumeCapacity(3);
4742 uleb128(header.fixedWriter(), DW.LNCT.path) catch unreachable;
4743 uleb128(header.fixedWriter(), DW.FORM.line_strp) catch unreachable;
4744 uleb128(header.fixedWriter(), DW.LNCT.directory_index) catch unreachable;
4745 uleb128(header.fixedWriter(), @intFromEnum(dir_index_info.form)) catch unreachable;
4746 uleb128(header.fixedWriter(), DW.LNCT.LLVM_source) catch unreachable;
4747 uleb128(header.fixedWriter(), DW.FORM.line_strp) catch unreachable;
4748 uleb128(header.fixedWriter(), mod_info.files.count()) catch unreachable;
4966 hw.writeByte(3) catch unreachable;
4967 hw.writeUleb128(DW.LNCT.path) catch unreachable;
4968 hw.writeUleb128(DW.FORM.line_strp) catch unreachable;
4969 hw.writeUleb128(DW.LNCT.directory_index) catch unreachable;
4970 hw.writeUleb128(@intFromEnum(dir_index_info.form)) catch unreachable;
4971 hw.writeUleb128(DW.LNCT.LLVM_source) catch unreachable;
4972 hw.writeUleb128(DW.FORM.line_strp) catch unreachable;
4973 hw.writeUleb128(mod_info.files.count()) catch unreachable;
47494974 for (mod_info.files.keys()) |file_index| {
47504975 const file = zcu.fileByIndex(file_index);
47514976 unit.cross_section_relocs.appendAssumeCapacity(.{
4752 .source_off = @intCast(header.items.len),
4977 .source_off = @intCast(hw.end),
47534978 .target_sec = .debug_line_str,
47544979 .target_unit = StringSection.unit,
47554980 .target_entry = (try dwarf.debug_line_str.addString(dwarf, file.sub_file_path)).toOptional(),
47564981 });
4757 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4758 dwarf.writeInt(
4759 header.addManyAsSliceAssumeCapacity(dir_index_info.bytes),
4760 mod_info.dirs.getIndex(dwarf.getUnitIfExists(file.mod.?).?) orelse 0,
4761 );
4982 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4983 const dir_index = mod_info.dirs.getIndex(dwarf.getUnitIfExists(file.mod.?).?) orelse 0;
4984 switch (dir_index_info.bytes) {
4985 else => unreachable,
4986 1 => hw.writeByte(@intCast(dir_index)) catch unreachable,
4987 2 => hw.writeInt(u16, @intCast(dir_index), dwarf.endian) catch unreachable,
4988 }
47624989 unit.cross_section_relocs.appendAssumeCapacity(.{
4763 .source_off = @intCast(header.items.len),
4990 .source_off = @intCast(hw.end),
47644991 .target_sec = .debug_line_str,
47654992 .target_unit = StringSection.unit,
47664993 .target_entry = (try dwarf.debug_line_str.addString(
......@@ -4768,9 +4995,9 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47684995 if (file.is_builtin) file.source.? else "",
47694996 )).toOptional(),
47704997 });
4771 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4998 hw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
47724999 }
4773 try unit.replaceHeader(&dwarf.debug_line.section, dwarf, header.items);
5000 try unit.replaceHeader(&dwarf.debug_line.section, dwarf, header_aw.getWritten());
47745001 try unit.writeTrailer(&dwarf.debug_line.section, dwarf);
47755002 }
47765003 dwarf.debug_line.section.dirty = false;
......@@ -4786,24 +5013,28 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47865013 }
47875014 if (dwarf.debug_rnglists.section.dirty) {
47885015 for (dwarf.debug_rnglists.section.units.items) |*unit| {
4789 header.clearRetainingCapacity();
4790 try header.ensureTotalCapacity(unit.header_len);
5016 header_aw.clearRetainingCapacity();
5017 try header_aw.ensureTotalCapacity(unit.header_len);
47915018 const unit_len = (if (unit.next.unwrap()) |next_unit|
47925019 dwarf.debug_rnglists.section.getUnit(next_unit).off
47935020 else
47945021 dwarf.debug_rnglists.section.len) - unit.off - dwarf.unitLengthBytes();
47955022 switch (dwarf.format) {
4796 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
5023 .@"32" => hw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
47975024 .@"64" => {
4798 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4799 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
5025 hw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
5026 hw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
48005027 },
48015028 }
4802 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 5, dwarf.endian);
4803 header.appendSliceAssumeCapacity(&.{ @intFromEnum(dwarf.address_size), 0 });
4804 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), 1, dwarf.endian);
4805 dwarf.writeInt(header.addManyAsSliceAssumeCapacity(dwarf.sectionOffsetBytes()), dwarf.sectionOffsetBytes() * 1);
4806 try unit.replaceHeader(&dwarf.debug_rnglists.section, dwarf, header.items);
5029 hw.writeInt(u16, 5, dwarf.endian) catch unreachable;
5030 hw.writeByte(@intFromEnum(dwarf.address_size)) catch unreachable;
5031 hw.writeByte(0) catch unreachable;
5032 hw.writeInt(u32, 1, dwarf.endian) catch unreachable;
5033 switch (dwarf.format) {
5034 .@"32" => hw.writeInt(u32, dwarf.sectionOffsetBytes() * 1, dwarf.endian) catch unreachable,
5035 .@"64" => hw.writeInt(u64, dwarf.sectionOffsetBytes() * 1, dwarf.endian) catch unreachable,
5036 }
5037 try unit.replaceHeader(&dwarf.debug_rnglists.section, dwarf, header_aw.getWritten());
48075038 try unit.writeTrailer(&dwarf.debug_rnglists.section, dwarf);
48085039 }
48095040 dwarf.debug_rnglists.section.dirty = false;
......@@ -5983,7 +6214,11 @@ fn addCommonEntry(dwarf: *Dwarf, unit: Unit.Index) UpdateError!Entry.Index {
59836214 return entry;
59846215}
59856216
5986fn freeCommonEntry(dwarf: *Dwarf, unit: Unit.Index, entry: Entry.Index) UpdateError!void {
6217fn freeCommonEntry(
6218 dwarf: *Dwarf,
6219 unit: Unit.Index,
6220 entry: Entry.Index,
6221) (UpdateError || Writer.Error)!void {
59876222 try dwarf.debug_aranges.section.freeEntry(unit, entry, dwarf);
59886223 try dwarf.debug_frame.section.freeEntry(unit, entry, dwarf);
59896224 try dwarf.debug_info.section.freeEntry(unit, entry, dwarf);
......@@ -6023,17 +6258,17 @@ fn sectionOffsetBytes(dwarf: *Dwarf) u32 {
60236258}
60246259
60256260fn uleb128Bytes(value: anytype) u32 {
6026 var trash_buffer: [64]u8 = undefined;
6027 var d: Writer.Discarding = .init(&trash_buffer);
6028 d.writer.writeUleb128(value) catch unreachable;
6029 return @intCast(d.count + d.writer.end);
6261 var buf: [64]u8 = undefined;
6262 var dw: Writer.Discarding = .init(&buf);
6263 dw.writer.writeUleb128(value) catch unreachable;
6264 return @intCast(dw.count + dw.writer.end);
60306265}
60316266
60326267fn sleb128Bytes(value: anytype) u32 {
6033 var trash_buffer: [64]u8 = undefined;
6034 var d: Writer.Discarding = .init(&trash_buffer);
6035 d.writer.writeSleb128(value) catch unreachable;
6036 return @intCast(d.count + d.writer.end);
6268 var buf: [64]u8 = undefined;
6269 var dw: Writer.Discarding = .init(&buf);
6270 dw.writer.writeSleb128(value) catch unreachable;
6271 return @intCast(dw.count + dw.writer.end);
60376272}
60386273
60396274/// overrides `-fno-incremental` for testing incremental debug info until `-fincremental` is functional
......@@ -6042,6 +6277,7 @@ inline fn incremental(dwarf: Dwarf) bool {
60426277 return force_incremental or dwarf.bin_file.comp.incremental;
60436278}
60446279
6280const Allocator = std.mem.Allocator;
60456281const DW = std.dwarf;
60466282const Dwarf = @This();
60476283const InternPool = @import("../InternPool.zig");
......@@ -6055,9 +6291,6 @@ const codegen = @import("../codegen.zig");
60556291const dev = @import("../dev.zig");
60566292const link = @import("../link.zig");
60576293const log = std.log.scoped(.dwarf);
6058const sleb128 = std.leb.writeIleb128;
60596294const std = @import("std");
60606295const target_info = @import("../target.zig");
6061const uleb128 = std.leb.writeUleb128;
6062const Allocator = std.mem.Allocator;
60636296const Writer = std.Io.Writer;