authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-07 22:06:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-07 22:26:42-07:00
log281f657377c46180e0c7044e6ac0c7b8b69ef892
tree49c9e9f8c69b800c998b1d7b99fc71f7463d2903
parentc5a5983150c41843f30e136471e77fd3623a7510

Revert "random patches from another branch"

This reverts commit 76ed05523d6327789a8365571b67a214ac527ef9. they don't work

1 files changed, 636 insertions(+), 715 deletions(-)

src/link/Dwarf.zig+636-715
......@@ -1,4 +1,4 @@
1gpa: Allocator,
1gpa: std.mem.Allocator,
22bin_file: *link.File,
33format: DW.Format,
44endian: std.builtin.Endian,
......@@ -50,7 +50,7 @@ const ModInfo = struct {
5050 dirs: std.AutoArrayHashMapUnmanaged(Unit.Index, void),
5151 files: std.AutoArrayHashMapUnmanaged(Zcu.File.Index, void),
5252
53 fn deinit(mod_info: *ModInfo, gpa: Allocator) void {
53 fn deinit(mod_info: *ModInfo, gpa: std.mem.Allocator) void {
5454 mod_info.dirs.deinit(gpa);
5555 mod_info.files.deinit(gpa);
5656 mod_info.* = undefined;
......@@ -220,13 +220,13 @@ const StringSection = struct {
220220 .section = Section.init,
221221 };
222222
223 fn deinit(str_sec: *StringSection, gpa: Allocator) void {
223 fn deinit(str_sec: *StringSection, gpa: std.mem.Allocator) void {
224224 str_sec.contents.deinit(gpa);
225225 str_sec.map.deinit(gpa);
226226 str_sec.section.deinit(gpa);
227227 }
228228
229 fn addString(str_sec: *StringSection, dwarf: *Dwarf, str: []const u8) !Entry.Index {
229 fn addString(str_sec: *StringSection, dwarf: *Dwarf, str: []const u8) UpdateError!Entry.Index {
230230 const gop = try str_sec.map.getOrPutAdapted(dwarf.gpa, str, Adapter{ .str_sec = str_sec });
231231 const entry: Entry.Index = @enumFromInt(gop.index);
232232 if (!gop.found_existing) {
......@@ -297,7 +297,7 @@ pub const Section = struct {
297297 .len = 0,
298298 };
299299
300 fn deinit(sec: *Section, gpa: Allocator) void {
300 fn deinit(sec: *Section, gpa: std.mem.Allocator) void {
301301 for (sec.units.items) |*unit| unit.deinit(gpa);
302302 sec.units.deinit(gpa);
303303 sec.* = undefined;
......@@ -357,7 +357,7 @@ pub const Section = struct {
357357 if (sec.last == unit.toOptional()) sec.last = unit_ptr.prev;
358358 }
359359
360 fn popUnit(sec: *Section, gpa: Allocator) void {
360 fn popUnit(sec: *Section, gpa: std.mem.Allocator) void {
361361 const unit_index: Unit.Index = @enumFromInt(sec.units.items.len - 1);
362362 sec.unlinkUnit(unit_index);
363363 var unit = sec.units.pop().?;
......@@ -518,7 +518,7 @@ const Unit = struct {
518518 unit.cross_section_relocs.clearRetainingCapacity();
519519 }
520520
521 fn deinit(unit: *Unit, gpa: Allocator) void {
521 fn deinit(unit: *Unit, gpa: std.mem.Allocator) void {
522522 for (unit.entries.items) |*entry| entry.deinit(gpa);
523523 unit.entries.deinit(gpa);
524524 unit.cross_unit_relocs.deinit(gpa);
......@@ -526,7 +526,7 @@ const Unit = struct {
526526 unit.* = undefined;
527527 }
528528
529 fn addEntry(unit: *Unit, gpa: Allocator) Allocator.Error!Entry.Index {
529 fn addEntry(unit: *Unit, gpa: std.mem.Allocator) std.mem.Allocator.Error!Entry.Index {
530530 if (unit.free.unwrap()) |entry| {
531531 const entry_ptr = unit.getEntry(entry);
532532 unit.free = entry_ptr.next;
......@@ -648,33 +648,35 @@ const Unit = struct {
648648 assert(len >= unit.trailer_len);
649649 if (sec == &dwarf.debug_line.section) {
650650 var buf: [1 + uleb128Bytes(std.math.maxInt(u32)) + 1]u8 = undefined;
651 var bw: Writer = .fixed(&buf);
652 bw.writeByte(DW.LNS.extended_op) catch unreachable;
653 const extended_op_bytes = bw.end;
651 var fbs = std.io.fixedBufferStream(&buf);
652 const writer = fbs.writer();
653 writer.writeByte(DW.LNS.extended_op) catch unreachable;
654 const extended_op_bytes = fbs.pos;
654655 var op_len_bytes: u5 = 1;
655656 while (true) switch (std.math.order(len - extended_op_bytes - op_len_bytes, @as(u32, 1) << 7 * op_len_bytes)) {
656 .lt => break bw.writeLeb128(len - extended_op_bytes - op_len_bytes) catch unreachable,
657 .lt => break uleb128(writer, len - extended_op_bytes - op_len_bytes) catch unreachable,
657658 .eq => {
658659 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
659660 op_len_bytes += 1;
660 std.leb.writeUnsignedExtended((bw.writableSlice(op_len_bytes) catch unreachable), len - extended_op_bytes - op_len_bytes);
661 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..op_len_bytes], len - extended_op_bytes - op_len_bytes);
662 fbs.pos += op_len_bytes;
661663 break;
662664 },
663665 .gt => op_len_bytes += 1,
664666 };
665 assert(bw.end == extended_op_bytes + op_len_bytes);
666 bw.writeByte(DW.LNE.padding) catch unreachable;
667 assert(bw.end >= unit.trailer_len and bw.end <= len);
668 return dwarf.getFile().?.pwriteAll(bw.getWritten(), sec.off(dwarf) + start);
667 assert(fbs.pos == extended_op_bytes + op_len_bytes);
668 writer.writeByte(DW.LNE.padding) catch unreachable;
669 assert(fbs.pos >= unit.trailer_len and fbs.pos <= len);
670 return dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + start);
669671 }
670 var trailer_bw: Writer = .fixed(try dwarf.gpa.alloc(u8, len));
671 defer dwarf.gpa.free(trailer_bw.buffer);
672 var trailer = try std.ArrayList(u8).initCapacity(dwarf.gpa, len);
673 defer trailer.deinit();
672674 const fill_byte: u8 = if (sec == &dwarf.debug_abbrev.section) fill: {
673675 assert(uleb128Bytes(@intFromEnum(AbbrevCode.null)) == 1);
674 trailer_bw.writeByte(@intFromEnum(AbbrevCode.null)) catch unreachable;
676 trailer.appendAssumeCapacity(@intFromEnum(AbbrevCode.null));
675677 break :fill @intFromEnum(AbbrevCode.null);
676678 } else if (sec == &dwarf.debug_aranges.section) fill: {
677 trailer_bw.splatByteAll(0, @intFromEnum(dwarf.address_size) * 2) catch unreachable;
679 trailer.appendNTimesAssumeCapacity(0, @intFromEnum(dwarf.address_size) * 2);
678680 break :fill 0;
679681 } else if (sec == &dwarf.debug_frame.section) fill: {
680682 switch (dwarf.debug_frame.header.format) {
......@@ -682,49 +684,49 @@ const Unit = struct {
682684 .debug_frame, .eh_frame => |format| {
683685 const unit_len = len - dwarf.unitLengthBytes();
684686 switch (dwarf.format) {
685 .@"32" => trailer_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
687 .@"32" => std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
686688 .@"64" => {
687 trailer_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
688 trailer_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
689 std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
690 std.mem.writeInt(u64, trailer.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
689691 },
690692 }
691693 switch (format) {
692694 .none => unreachable,
693695 .debug_frame => {
694696 switch (dwarf.format) {
695 .@"32" => trailer_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable,
696 .@"64" => trailer_bw.writeInt(u64, std.math.maxInt(u64), dwarf.endian) catch unreachable,
697 .@"32" => std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian),
698 .@"64" => std.mem.writeInt(u64, trailer.addManyAsArrayAssumeCapacity(8), std.math.maxInt(u64), dwarf.endian),
697699 }
698 trailer_bw.writeByte(4) catch unreachable;
699 trailer_bw.writeAll("\x00") catch unreachable;
700 trailer_bw.writeByte(@intFromEnum(dwarf.address_size)) catch unreachable;
701 trailer_bw.writeByte(0) catch unreachable;
700 trailer.appendAssumeCapacity(4);
701 trailer.appendSliceAssumeCapacity("\x00");
702 trailer.appendAssumeCapacity(@intFromEnum(dwarf.address_size));
703 trailer.appendAssumeCapacity(0);
702704 },
703705 .eh_frame => {
704 trailer_bw.writeInt(u32, 0, dwarf.endian) catch unreachable;
705 trailer_bw.writeByte(1) catch unreachable;
706 trailer_bw.writeAll("\x00") catch unreachable;
706 std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), 0, dwarf.endian);
707 trailer.appendAssumeCapacity(1);
708 trailer.appendSliceAssumeCapacity("\x00");
707709 },
708710 }
709 trailer_bw.writeUleb128(1) catch unreachable;
710 trailer_bw.writeSleb128(1) catch unreachable;
711 trailer_bw.writeUleb128(0) catch unreachable;
711 uleb128(trailer.fixedWriter(), 1) catch unreachable;
712 sleb128(trailer.fixedWriter(), 1) catch unreachable;
713 uleb128(trailer.fixedWriter(), 0) catch unreachable;
712714 },
713715 }
714 trailer_bw.splatByteAll(DW.CFA.nop, unit.trailer_len - trailer_bw.end) catch unreachable;
716 trailer.appendNTimesAssumeCapacity(DW.CFA.nop, unit.trailer_len - trailer.items.len);
715717 break :fill DW.CFA.nop;
716718 } else if (sec == &dwarf.debug_info.section) fill: {
717719 assert(uleb128Bytes(@intFromEnum(AbbrevCode.null)) == 1);
718 trailer_bw.splatByteAll(@intFromEnum(AbbrevCode.null), 2) catch unreachable;
720 trailer.appendNTimesAssumeCapacity(@intFromEnum(AbbrevCode.null), 2);
719721 break :fill @intFromEnum(AbbrevCode.null);
720722 } else if (sec == &dwarf.debug_rnglists.section) fill: {
721 trailer_bw.writeByte(DW.RLE.end_of_list) catch unreachable;
723 trailer.appendAssumeCapacity(DW.RLE.end_of_list);
722724 break :fill DW.RLE.end_of_list;
723725 } else unreachable;
724 assert(trailer_bw.end == unit.trailer_len);
725 trailer_bw.splatByteAll(fill_byte, len - unit.trailer_len) catch unreachable;
726 assert(trailer_bw.end == len);
727 try dwarf.getFile().?.pwriteAll(trailer_bw.buffer, sec.off(dwarf) + start);
726 assert(trailer.items.len == unit.trailer_len);
727 trailer.appendNTimesAssumeCapacity(fill_byte, len - unit.trailer_len);
728 assert(trailer.items.len == len);
729 try dwarf.getFile().?.pwriteAll(trailer.items, sec.off(dwarf) + start);
728730 }
729731
730732 fn resolveRelocs(unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void {
......@@ -778,7 +780,7 @@ const Entry = struct {
778780 entry.external_relocs.clearRetainingCapacity();
779781 }
780782
781 fn deinit(entry: *Entry, gpa: Allocator) void {
783 fn deinit(entry: *Entry, gpa: std.mem.Allocator) void {
782784 entry.cross_entry_relocs.deinit(gpa);
783785 entry.cross_unit_relocs.deinit(gpa);
784786 entry.cross_section_relocs.deinit(gpa);
......@@ -831,52 +833,52 @@ const Entry = struct {
831833 1 + uleb128Bytes(std.math.maxInt(u32)) + 1,
832834 )
833835 ]u8 = undefined;
834 var bw: Writer = .fixed(&buf);
836 var fbs = std.io.fixedBufferStream(&buf);
837 const writer = fbs.writer();
835838 if (sec == &dwarf.debug_info.section) switch (len) {
836839 0 => {},
837 1 => bw.writeLeb128(try dwarf.refAbbrevCode(.pad_1)) catch unreachable,
840 1 => uleb128(writer, try dwarf.refAbbrevCode(.pad_1)) catch unreachable,
838841 else => {
839 bw.writeLeb128(try dwarf.refAbbrevCode(.pad_n)) catch unreachable;
840 const abbrev_code_bytes = bw.end;
842 uleb128(writer, try dwarf.refAbbrevCode(.pad_n)) catch unreachable;
843 const abbrev_code_bytes = fbs.pos;
841844 var block_len_bytes: u5 = 1;
842845 while (true) switch (std.math.order(len - abbrev_code_bytes - block_len_bytes, @as(u32, 1) << 7 * block_len_bytes)) {
843 .lt => break bw.writeLeb128(len - abbrev_code_bytes - block_len_bytes) catch unreachable,
846 .lt => break uleb128(writer, len - abbrev_code_bytes - block_len_bytes) catch unreachable,
844847 .eq => {
845848 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
846849 block_len_bytes += 1;
847 std.leb.writeUnsignedExtended((try bw.writableSlice(block_len_bytes)), len - abbrev_code_bytes - block_len_bytes);
850 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..block_len_bytes], len - abbrev_code_bytes - block_len_bytes);
851 fbs.pos += block_len_bytes;
848852 break;
849853 },
850854 .gt => block_len_bytes += 1,
851855 };
852 assert(bw.end == abbrev_code_bytes + block_len_bytes);
856 assert(fbs.pos == abbrev_code_bytes + block_len_bytes);
853857 },
854858 } else if (sec == &dwarf.debug_line.section) switch (len) {
855859 0 => {},
856 1 => bw.writeByte(DW.LNS.const_add_pc) catch unreachable,
860 1 => writer.writeByte(DW.LNS.const_add_pc) catch unreachable,
857861 else => {
858 bw.writeByte(DW.LNS.extended_op) catch unreachable;
859 const extended_op_bytes = bw.end;
862 writer.writeByte(DW.LNS.extended_op) catch unreachable;
863 const extended_op_bytes = fbs.pos;
860864 var op_len_bytes: u5 = 1;
861865 while (true) switch (std.math.order(len - extended_op_bytes - op_len_bytes, @as(u32, 1) << 7 * op_len_bytes)) {
862 .lt => break bw.writeLeb128(len - extended_op_bytes - op_len_bytes) catch unreachable,
866 .lt => break uleb128(writer, len - extended_op_bytes - op_len_bytes) catch unreachable,
863867 .eq => {
864868 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
865869 op_len_bytes += 1;
866 std.leb.writeUnsignedExtended(
867 (bw.writableSlice(op_len_bytes) catch unreachable),
868 len - extended_op_bytes - op_len_bytes,
869 );
870 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..op_len_bytes], len - extended_op_bytes - op_len_bytes);
871 fbs.pos += op_len_bytes;
870872 break;
871873 },
872874 .gt => op_len_bytes += 1,
873875 };
874 assert(bw.end == extended_op_bytes + op_len_bytes);
875 if (len > 2) bw.writeByte(DW.LNE.padding) catch unreachable;
876 assert(fbs.pos == extended_op_bytes + op_len_bytes);
877 if (len > 2) writer.writeByte(DW.LNE.padding) catch unreachable;
876878 },
877879 } else assert(!sec.pad_entries_to_ideal and len == 0);
878 assert(bw.end <= len);
879 try dwarf.getFile().?.pwriteAll(bw.getWritten(), sec.off(dwarf) + unit.off + unit.header_len + start);
880 assert(fbs.pos <= len);
881 try dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + unit.off + unit.header_len + start);
880882 }
881883
882884 fn resize(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, len: u32) UpdateError!void {
......@@ -1131,149 +1133,150 @@ pub const Loc = union(enum) {
11311133 };
11321134 }
11331135
1134 fn writeReg(bw: *Writer, reg: u32, op0: u8, opx: u8) Writer.Error!void {
1136 fn writeReg(reg: u32, op0: u8, opx: u8, writer: anytype) @TypeOf(writer).Error!void {
11351137 if (std.math.cast(u5, reg)) |small_reg| {
1136 try bw.writeByte(op0 + small_reg);
1138 try writer.writeByte(op0 + small_reg);
11371139 } else {
1138 try bw.writeByte(opx);
1139 try bw.writeLeb128(reg);
1140 try writer.writeByte(opx);
1141 try uleb128(writer, reg);
11401142 }
11411143 }
11421144
1143 fn write(loc: Loc, bw: *Writer, adapter: anytype) UpdateError!void {
1145 fn write(loc: Loc, adapter: anytype) UpdateError!void {
1146 const writer = adapter.writer();
11441147 switch (loc) {
11451148 .empty => {},
11461149 .addr_reloc => |sym_index| {
1147 try bw.writeByte(DW.OP.addr);
1150 try writer.writeByte(DW.OP.addr);
11481151 try adapter.addrSym(sym_index);
11491152 },
11501153 .deref => |addr| {
11511154 try addr.write(adapter);
1152 try bw.writeByte(DW.OP.deref);
1155 try writer.writeByte(DW.OP.deref);
11531156 },
11541157 .constu => |constu| if (std.math.cast(u5, constu)) |lit| {
1155 try bw.writeByte(@as(u8, DW.OP.lit0) + lit);
1158 try writer.writeByte(@as(u8, DW.OP.lit0) + lit);
11561159 } else if (std.math.cast(u8, constu)) |const1u| {
1157 try bw.writeAll(&.{ DW.OP.const1u, const1u });
1160 try writer.writeAll(&.{ DW.OP.const1u, const1u });
11581161 } else if (std.math.cast(u16, constu)) |const2u| {
1159 try bw.writeByte(DW.OP.const2u);
1160 try bw.writeInt(u16, const2u, adapter.endian());
1162 try writer.writeByte(DW.OP.const2u);
1163 try writer.writeInt(u16, const2u, adapter.endian());
11611164 } else if (std.math.cast(u21, constu)) |const3u| {
1162 try bw.writeByte(DW.OP.constu);
1163 try bw.writeLeb128(const3u);
1165 try writer.writeByte(DW.OP.constu);
1166 try uleb128(writer, const3u);
11641167 } else if (std.math.cast(u32, constu)) |const4u| {
1165 try bw.writeByte(DW.OP.const4u);
1166 try bw.writeInt(u32, const4u, adapter.endian());
1168 try writer.writeByte(DW.OP.const4u);
1169 try writer.writeInt(u32, const4u, adapter.endian());
11671170 } else if (std.math.cast(u49, constu)) |const7u| {
1168 try bw.writeByte(DW.OP.constu);
1169 try bw.writeLeb128(const7u);
1171 try writer.writeByte(DW.OP.constu);
1172 try uleb128(writer, const7u);
11701173 } else {
1171 try bw.writeByte(DW.OP.const8u);
1172 try bw.writeInt(u64, constu, adapter.endian());
1174 try writer.writeByte(DW.OP.const8u);
1175 try writer.writeInt(u64, constu, adapter.endian());
11731176 },
11741177 .consts => |consts| if (std.math.cast(i8, consts)) |const1s| {
1175 try bw.writeAll(&.{ DW.OP.const1s, @bitCast(const1s) });
1178 try writer.writeAll(&.{ DW.OP.const1s, @bitCast(const1s) });
11761179 } else if (std.math.cast(i16, consts)) |const2s| {
1177 try bw.writeByte(DW.OP.const2s);
1178 try bw.writeInt(i16, const2s, adapter.endian());
1180 try writer.writeByte(DW.OP.const2s);
1181 try writer.writeInt(i16, const2s, adapter.endian());
11791182 } else if (std.math.cast(i21, consts)) |const3s| {
1180 try bw.writeByte(DW.OP.consts);
1181 try bw.writeLeb128(const3s);
1183 try writer.writeByte(DW.OP.consts);
1184 try sleb128(writer, const3s);
11821185 } else if (std.math.cast(i32, consts)) |const4s| {
1183 try bw.writeByte(DW.OP.const4s);
1184 try bw.writeInt(i32, const4s, adapter.endian());
1186 try writer.writeByte(DW.OP.const4s);
1187 try writer.writeInt(i32, const4s, adapter.endian());
11851188 } else if (std.math.cast(i49, consts)) |const7s| {
1186 try bw.writeByte(DW.OP.consts);
1187 try bw.writeLeb128(const7s);
1189 try writer.writeByte(DW.OP.consts);
1190 try sleb128(writer, const7s);
11881191 } else {
1189 try bw.writeByte(DW.OP.const8s);
1190 try bw.writeInt(i64, consts, adapter.endian());
1192 try writer.writeByte(DW.OP.const8s);
1193 try writer.writeInt(i64, consts, adapter.endian());
11911194 },
11921195 .plus => |plus| done: {
11931196 if (plus[0].getConst(u0)) |_| {
1194 try plus[1].write(bw, adapter);
1197 try plus[1].write(adapter);
11951198 break :done;
11961199 }
11971200 if (plus[1].getConst(u0)) |_| {
1198 try plus[0].write(bw, adapter);
1201 try plus[0].write(adapter);
11991202 break :done;
12001203 }
12011204 if (plus[0].getBaseReg()) |breg| {
12021205 if (plus[1].getConst(i65)) |offset| {
1203 try writeReg(bw, breg, DW.OP.breg0, DW.OP.bregx);
1204 try bw.writeLeb128(offset);
1206 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1207 try sleb128(writer, offset);
12051208 break :done;
12061209 }
12071210 }
12081211 if (plus[1].getBaseReg()) |breg| {
12091212 if (plus[0].getConst(i65)) |offset| {
1210 try writeReg(bw, breg, DW.OP.breg0, DW.OP.bregx);
1211 try bw.writeLeb128(offset);
1213 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1214 try sleb128(writer, offset);
12121215 break :done;
12131216 }
12141217 }
12151218 if (plus[0].getConst(u64)) |uconst| {
1216 try plus[1].write(bw, adapter);
1217 try bw.writeByte(DW.OP.plus_uconst);
1218 try bw.writeLeb128(uconst);
1219 try plus[1].write(adapter);
1220 try writer.writeByte(DW.OP.plus_uconst);
1221 try uleb128(writer, uconst);
12191222 break :done;
12201223 }
12211224 if (plus[1].getConst(u64)) |uconst| {
1222 try plus[0].write(bw, adapter);
1223 try bw.writeByte(DW.OP.plus_uconst);
1224 try bw.writeLeb128(uconst);
1225 try plus[0].write(adapter);
1226 try writer.writeByte(DW.OP.plus_uconst);
1227 try uleb128(writer, uconst);
12251228 break :done;
12261229 }
1227 try plus[0].write(bw, adapter);
1228 try plus[1].write(bw, adapter);
1229 try bw.writeByte(DW.OP.plus);
1230 try plus[0].write(adapter);
1231 try plus[1].write(adapter);
1232 try writer.writeByte(DW.OP.plus);
12301233 },
1231 .reg => |reg| try writeReg(bw, reg, DW.OP.reg0, DW.OP.regx),
1234 .reg => |reg| try writeReg(reg, DW.OP.reg0, DW.OP.regx, writer),
12321235 .breg => |breg| {
1233 try writeReg(bw, breg, DW.OP.breg0, DW.OP.bregx);
1234 try bw.writeSleb128(0);
1236 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1237 try sleb128(writer, 0);
12351238 },
1236 .push_object_address => try bw.writeByte(DW.OP.push_object_address),
1239 .push_object_address => try writer.writeByte(DW.OP.push_object_address),
12371240 .call => |call| {
12381241 for (call.args) |arg| try arg.write(adapter);
1239 try bw.writeByte(DW.OP.call_ref);
1242 try writer.writeByte(DW.OP.call_ref);
12401243 try adapter.infoEntry(call.unit, call.entry);
12411244 },
12421245 .form_tls_address => |addr| {
1243 try addr.write(bw, adapter);
1244 try bw.writeByte(DW.OP.form_tls_address);
1246 try addr.write(adapter);
1247 try writer.writeByte(DW.OP.form_tls_address);
12451248 },
12461249 .implicit_value => |value| {
1247 try bw.writeByte(DW.OP.implicit_value);
1248 try bw.writeLeb128(value.len);
1249 try bw.writeAll(value);
1250 try writer.writeByte(DW.OP.implicit_value);
1251 try uleb128(writer, value.len);
1252 try writer.writeAll(value);
12501253 },
12511254 .stack_value => |value| {
1252 try value.write(bw, adapter);
1253 try bw.writeByte(DW.OP.stack_value);
1255 try value.write(adapter);
1256 try writer.writeByte(DW.OP.stack_value);
12541257 },
12551258 .implicit_pointer => |implicit_pointer| {
1256 try bw.writeByte(DW.OP.implicit_pointer);
1257 try adapter.infoEntry(bw, implicit_pointer.unit, implicit_pointer.entry);
1258 try bw.writeLeb128(implicit_pointer.offset);
1259 try writer.writeByte(DW.OP.implicit_pointer);
1260 try adapter.infoEntry(implicit_pointer.unit, implicit_pointer.entry);
1261 try sleb128(writer, implicit_pointer.offset);
12591262 },
12601263 .wasm_ext => |wasm_ext| {
1261 try bw.writeByte(DW.OP.WASM_location);
1264 try writer.writeByte(DW.OP.WASM_location);
12621265 switch (wasm_ext) {
12631266 .local => |local| {
1264 try bw.writeByte(DW.OP.WASM_local);
1265 try bw.writeLeb128(local);
1267 try writer.writeByte(DW.OP.WASM_local);
1268 try uleb128(writer, local);
12661269 },
12671270 .global => |global| if (std.math.cast(u21, global)) |global_u21| {
1268 try bw.writeByte(DW.OP.WASM_global);
1269 try bw.writeLeb128(global_u21);
1271 try writer.writeByte(DW.OP.WASM_global);
1272 try uleb128(writer, global_u21);
12701273 } else {
1271 try bw.writeByte(DW.OP.WASM_global_u32);
1272 try bw.writeInt(u32, global, adapter.endian());
1274 try writer.writeByte(DW.OP.WASM_global_u32);
1275 try writer.writeInt(u32, global, adapter.endian());
12731276 },
12741277 .operand_stack => |operand_stack| {
1275 try bw.writeByte(DW.OP.WASM_operand_stack);
1276 try bw.writeLeb128(operand_stack);
1278 try writer.writeByte(DW.OP.WASM_operand_stack);
1279 try uleb128(writer, operand_stack);
12771280 },
12781281 }
12791282 },
......@@ -1306,21 +1309,21 @@ pub const Cfa = union(enum) {
13061309 const RegExpr = struct { reg: u32, expr: Loc };
13071310
13081311 fn write(cfa: Cfa, wip_nav: *WipNav) UpdateError!void {
1309 const bw = &wip_nav.debug_frame.buffered_writer;
1312 const writer = wip_nav.debug_frame.writer(wip_nav.dwarf.gpa);
13101313 switch (cfa) {
1311 .nop => try bw.writeByte(DW.CFA.nop),
1314 .nop => try writer.writeByte(DW.CFA.nop),
13121315 .advance_loc => |loc| {
13131316 const delta = @divExact(loc - wip_nav.cfi.loc, wip_nav.dwarf.debug_frame.header.code_alignment_factor);
13141317 if (delta == 0) {} else if (std.math.cast(u6, delta)) |small_delta|
1315 try bw.writeByte(@as(u8, DW.CFA.advance_loc) + small_delta)
1318 try writer.writeByte(@as(u8, DW.CFA.advance_loc) + small_delta)
13161319 else if (std.math.cast(u8, delta)) |ubyte_delta|
1317 try bw.writeAll(&.{ DW.CFA.advance_loc1, ubyte_delta })
1320 try writer.writeAll(&.{ DW.CFA.advance_loc1, ubyte_delta })
13181321 else if (std.math.cast(u16, delta)) |uhalf_delta| {
1319 try bw.writeByte(DW.CFA.advance_loc2);
1320 try bw.writeInt(u16, uhalf_delta, wip_nav.dwarf.endian);
1322 try writer.writeByte(DW.CFA.advance_loc2);
1323 try writer.writeInt(u16, uhalf_delta, wip_nav.dwarf.endian);
13211324 } else if (std.math.cast(u32, delta)) |uword_delta| {
1322 try bw.writeByte(DW.CFA.advance_loc4);
1323 try bw.writeInt(u32, uword_delta, wip_nav.dwarf.endian);
1325 try writer.writeByte(DW.CFA.advance_loc4);
1326 try writer.writeInt(u32, uword_delta, wip_nav.dwarf.endian);
13241327 }
13251328 wip_nav.cfi.loc = loc;
13261329 },
......@@ -1332,41 +1335,41 @@ pub const Cfa = union(enum) {
13321335 }, wip_nav.dwarf.debug_frame.header.data_alignment_factor);
13331336 if (std.math.cast(u63, factored_off)) |unsigned_off| {
13341337 if (std.math.cast(u6, reg_off.reg)) |small_reg| {
1335 try bw.writeByte(@as(u8, DW.CFA.offset) + small_reg);
1338 try writer.writeByte(@as(u8, DW.CFA.offset) + small_reg);
13361339 } else {
1337 try bw.writeByte(DW.CFA.offset_extended);
1338 try bw.writeLeb128(reg_off.reg);
1340 try writer.writeByte(DW.CFA.offset_extended);
1341 try uleb128(writer, reg_off.reg);
13391342 }
1340 try bw.writeLeb128(unsigned_off);
1343 try uleb128(writer, unsigned_off);
13411344 } else {
1342 try bw.writeByte(DW.CFA.offset_extended_sf);
1343 try bw.writeLeb128(reg_off.reg);
1344 try bw.writeLeb128(factored_off);
1345 try writer.writeByte(DW.CFA.offset_extended_sf);
1346 try uleb128(writer, reg_off.reg);
1347 try sleb128(writer, factored_off);
13451348 }
13461349 },
13471350 .restore => |reg| if (std.math.cast(u6, reg)) |small_reg|
1348 try bw.writeByte(@as(u8, DW.CFA.restore) + small_reg)
1351 try writer.writeByte(@as(u8, DW.CFA.restore) + small_reg)
13491352 else {
1350 try bw.writeByte(DW.CFA.restore_extended);
1351 try bw.writeLeb128(reg);
1353 try writer.writeByte(DW.CFA.restore_extended);
1354 try uleb128(writer, reg);
13521355 },
13531356 .undefined => |reg| {
1354 try bw.writeByte(DW.CFA.undefined);
1355 try bw.writeLeb128(reg);
1357 try writer.writeByte(DW.CFA.undefined);
1358 try uleb128(writer, reg);
13561359 },
13571360 .same_value => |reg| {
1358 try bw.writeByte(DW.CFA.same_value);
1359 try bw.writeLeb128(reg);
1361 try writer.writeByte(DW.CFA.same_value);
1362 try uleb128(writer, reg);
13601363 },
13611364 .register => |regs| if (regs[0] != regs[1]) {
1362 try bw.writeByte(DW.CFA.register);
1363 for (regs) |reg| try bw.writeLeb128(reg);
1365 try writer.writeByte(DW.CFA.register);
1366 for (regs) |reg| try uleb128(writer, reg);
13641367 } else {
1365 try bw.writeByte(DW.CFA.same_value);
1366 try bw.writeLeb128(regs[0]);
1368 try writer.writeByte(DW.CFA.same_value);
1369 try uleb128(writer, regs[0]);
13671370 },
1368 .remember_state => try bw.writeByte(DW.CFA.remember_state),
1369 .restore_state => try bw.writeByte(DW.CFA.restore_state),
1371 .remember_state => try writer.writeByte(DW.CFA.remember_state),
1372 .restore_state => try writer.writeByte(DW.CFA.restore_state),
13701373 .def_cfa, .def_cfa_register, .def_cfa_offset, .adjust_cfa_offset => {
13711374 const reg_off: RegOff = switch (cfa) {
13721375 else => unreachable,
......@@ -1379,51 +1382,51 @@ pub const Cfa = union(enum) {
13791382 const unsigned_off = std.math.cast(u63, reg_off.off);
13801383 if (reg_off.off == wip_nav.cfi.cfa.off) {
13811384 if (changed_reg) {
1382 try bw.writeByte(DW.CFA.def_cfa_register);
1383 try bw.writeLeb128(reg_off.reg);
1385 try writer.writeByte(DW.CFA.def_cfa_register);
1386 try uleb128(writer, reg_off.reg);
13841387 }
13851388 } else if (switch (wip_nav.dwarf.debug_frame.header.data_alignment_factor) {
13861389 0 => unreachable,
13871390 1 => unsigned_off != null,
13881391 else => |data_alignment_factor| @rem(reg_off.off, data_alignment_factor) != 0,
13891392 }) {
1390 try bw.writeByte(if (changed_reg) DW.CFA.def_cfa else DW.CFA.def_cfa_offset);
1391 if (changed_reg) try bw.writeLeb128(reg_off.reg);
1392 try bw.writeLeb128(unsigned_off.?);
1393 try writer.writeByte(if (changed_reg) DW.CFA.def_cfa else DW.CFA.def_cfa_offset);
1394 if (changed_reg) try uleb128(writer, reg_off.reg);
1395 try uleb128(writer, unsigned_off.?);
13931396 } else {
1394 try bw.writeByte(if (changed_reg) DW.CFA.def_cfa_sf else DW.CFA.def_cfa_offset_sf);
1395 if (changed_reg) try bw.writeLeb128(reg_off.reg);
1396 try bw.writeLeb128(@divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor));
1397 try writer.writeByte(if (changed_reg) DW.CFA.def_cfa_sf else DW.CFA.def_cfa_offset_sf);
1398 if (changed_reg) try uleb128(writer, reg_off.reg);
1399 try sleb128(writer, @divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor));
13971400 }
13981401 wip_nav.cfi.cfa = reg_off;
13991402 },
14001403 .def_cfa_expression => |expr| {
1401 try bw.writeByte(DW.CFA.def_cfa_expression);
1404 try writer.writeByte(DW.CFA.def_cfa_expression);
14021405 try wip_nav.frameExprLoc(expr);
14031406 },
14041407 .expression => |reg_expr| {
1405 try bw.writeByte(DW.CFA.expression);
1406 try bw.writeLeb128(reg_expr.reg);
1408 try writer.writeByte(DW.CFA.expression);
1409 try uleb128(writer, reg_expr.reg);
14071410 try wip_nav.frameExprLoc(reg_expr.expr);
14081411 },
14091412 .val_offset => |reg_off| {
14101413 const factored_off = @divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor);
14111414 if (std.math.cast(u63, factored_off)) |unsigned_off| {
1412 try bw.writeByte(DW.CFA.val_offset);
1413 try bw.writeLeb128(reg_off.reg);
1414 try bw.writeLeb128(unsigned_off);
1415 try writer.writeByte(DW.CFA.val_offset);
1416 try uleb128(writer, reg_off.reg);
1417 try uleb128(writer, unsigned_off);
14151418 } else {
1416 try bw.writeByte(DW.CFA.val_offset_sf);
1417 try bw.writeLeb128(reg_off.reg);
1418 try bw.writeLeb128(factored_off);
1419 try writer.writeByte(DW.CFA.val_offset_sf);
1420 try uleb128(writer, reg_off.reg);
1421 try sleb128(writer, factored_off);
14191422 }
14201423 },
14211424 .val_expression => |reg_expr| {
1422 try bw.writeByte(DW.CFA.val_expression);
1423 try bw.writeLeb128(reg_expr.reg);
1425 try writer.writeByte(DW.CFA.val_expression);
1426 try uleb128(writer, reg_expr.reg);
14241427 try wip_nav.frameExprLoc(reg_expr.expr);
14251428 },
1426 .escape => |bytes| try bw.writeAll(bytes),
1429 .escape => |bytes| try writer.writeAll(bytes),
14271430 }
14281431 }
14291432};
......@@ -1446,27 +1449,19 @@ pub const WipNav = struct {
14461449 loc: u32,
14471450 cfa: Cfa.RegOff,
14481451 },
1449 debug_frame: std.io.Writer.Allocating,
1450 debug_info: std.io.Writer.Allocating,
1451 debug_line: std.io.Writer.Allocating,
1452 debug_loclists: std.io.Writer.Allocating,
1452 debug_frame: std.ArrayListUnmanaged(u8),
1453 debug_info: std.ArrayListUnmanaged(u8),
1454 debug_line: std.ArrayListUnmanaged(u8),
1455 debug_loclists: std.ArrayListUnmanaged(u8),
14531456 pending_lazy: PendingLazy,
14541457
1455 pub fn init(wip_nav: *WipNav) void {
1456 const gpa = wip_nav.dwarf.gpa;
1457 wip_nav.debug_frame.init(gpa);
1458 wip_nav.debug_info.init(gpa);
1459 wip_nav.debug_line.init(gpa);
1460 wip_nav.debug_loclists.init(gpa);
1461 }
1462
14631458 pub fn deinit(wip_nav: *WipNav) void {
14641459 const gpa = wip_nav.dwarf.gpa;
14651460 if (wip_nav.func != .none) wip_nav.blocks.deinit(gpa);
1466 wip_nav.debug_frame.deinit();
1467 wip_nav.debug_info.deinit();
1468 wip_nav.debug_line.deinit();
1469 wip_nav.debug_loclists.deinit();
1461 wip_nav.debug_frame.deinit(gpa);
1462 wip_nav.debug_info.deinit(gpa);
1463 wip_nav.debug_line.deinit(gpa);
1464 wip_nav.debug_loclists.deinit(gpa);
14701465 wip_nav.pending_lazy.types.deinit(gpa);
14711466 wip_nav.pending_lazy.values.deinit(gpa);
14721467 }
......@@ -1543,7 +1538,7 @@ pub const WipNav = struct {
15431538 delta_line: i33,
15441539 delta_pc: u64,
15451540 ) error{OutOfMemory}!void {
1546 const dlbw = &wip_nav.debug_line.buffered_writer;
1541 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
15471542
15481543 const header = wip_nav.dwarf.debug_line.header;
15491544 assert(header.maximum_operations_per_instruction == 1);
......@@ -1553,8 +1548,8 @@ pub const WipNav = struct {
15531548 delta_line - header.line_base >= header.line_range)
15541549 remaining: {
15551550 assert(delta_line != 0);
1556 try dlbw.writeByte(DW.LNS.advance_line);
1557 try dlbw.writeLeb128(delta_line);
1551 try dlw.writeByte(DW.LNS.advance_line);
1552 try sleb128(dlw, delta_line);
15581553 break :remaining 0;
15591554 } else delta_line);
15601555
......@@ -1562,68 +1557,68 @@ pub const WipNav = struct {
15621557 header.maximum_operations_per_instruction + delta_op;
15631558 const max_op_advance: u9 = (std.math.maxInt(u8) - header.opcode_base) / header.line_range;
15641559 const remaining_op_advance: u8 = @intCast(if (op_advance >= 2 * max_op_advance) remaining: {
1565 try dlbw.writeByte(DW.LNS.advance_pc);
1566 try dlbw.writeLeb128(op_advance);
1560 try dlw.writeByte(DW.LNS.advance_pc);
1561 try uleb128(dlw, op_advance);
15671562 break :remaining 0;
15681563 } else if (op_advance >= max_op_advance) remaining: {
1569 try dlbw.writeByte(DW.LNS.const_add_pc);
1564 try dlw.writeByte(DW.LNS.const_add_pc);
15701565 break :remaining op_advance - max_op_advance;
15711566 } else op_advance);
15721567
1573 try dlbw.writeByte(
1574 if (remaining_delta_line == 0 and remaining_op_advance == 0)
1575 DW.LNS.copy
1576 else
1577 @intCast((remaining_delta_line - header.line_base) +
1578 (header.line_range * remaining_op_advance) + header.opcode_base),
1579 );
1568 if (remaining_delta_line == 0 and remaining_op_advance == 0)
1569 try dlw.writeByte(DW.LNS.copy)
1570 else
1571 try dlw.writeByte(@intCast((remaining_delta_line - header.line_base) +
1572 (header.line_range * remaining_op_advance) + header.opcode_base));
15801573 }
15811574
1582 pub fn setColumn(wip_nav: *WipNav, column: u32) Allocator.Error!void {
1583 const dlbw = &wip_nav.debug_line.buffered_writer;
1584 try dlbw.writeByte(DW.LNS.set_column);
1585 try dlbw.writeLeb128(column + 1);
1575 pub fn setColumn(wip_nav: *WipNav, column: u32) error{OutOfMemory}!void {
1576 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1577 try dlw.writeByte(DW.LNS.set_column);
1578 try uleb128(dlw, column + 1);
15861579 }
15871580
1588 pub fn negateStmt(wip_nav: *WipNav) Allocator.Error!void {
1589 return wip_nav.debug_line.buffered_writer.writeByte(DW.LNS.negate_stmt);
1581 pub fn negateStmt(wip_nav: *WipNav) error{OutOfMemory}!void {
1582 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1583 try dlw.writeByte(DW.LNS.negate_stmt);
15901584 }
15911585
1592 pub fn setPrologueEnd(wip_nav: *WipNav) Allocator.Error!void {
1593 return wip_nav.debug_line.buffered_writer.writeByte(DW.LNS.set_prologue_end);
1586 pub fn setPrologueEnd(wip_nav: *WipNav) error{OutOfMemory}!void {
1587 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1588 try dlw.writeByte(DW.LNS.set_prologue_end);
15941589 }
15951590
1596 pub fn setEpilogueBegin(wip_nav: *WipNav) Allocator.Error!void {
1597 return wip_nav.debug_line.buffered_writer.writeByte(DW.LNS.set_epilogue_begin);
1591 pub fn setEpilogueBegin(wip_nav: *WipNav) error{OutOfMemory}!void {
1592 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1593 try dlw.writeByte(DW.LNS.set_epilogue_begin);
15981594 }
15991595
16001596 pub fn enterBlock(wip_nav: *WipNav, code_off: u64) UpdateError!void {
16011597 const dwarf = wip_nav.dwarf;
1602 const dibw = &wip_nav.debug_info.buffered_writer;
1598 const diw = wip_nav.debug_info.writer(dwarf.gpa);
16031599 const block = try wip_nav.blocks.addOne(dwarf.gpa);
16041600
1605 block.abbrev_code = @intCast(dibw.count);
1601 block.abbrev_code = @intCast(wip_nav.debug_info.items.len);
16061602 try wip_nav.abbrevCode(.block);
16071603 block.low_pc_off = code_off;
16081604 try wip_nav.infoAddrSym(wip_nav.func_sym_index, code_off);
1609 block.high_pc = @intCast(dibw.count);
1610 try dibw.writeInt(u32, 0, dwarf.endian);
1605 block.high_pc = @intCast(wip_nav.debug_info.items.len);
1606 try diw.writeInt(u32, 0, dwarf.endian);
16111607 wip_nav.any_children = false;
16121608 }
16131609
16141610 pub fn leaveBlock(wip_nav: *WipNav, code_off: u64) UpdateError!void {
16151611 const block_bytes = comptime uleb128Bytes(@intFromEnum(AbbrevCode.block));
16161612 const block = wip_nav.blocks.pop().?;
1617 const dib = wip_nav.debug_info.getWritten();
16181613 if (wip_nav.any_children)
1619 try wip_nav.debug_info.buffered_writer.writeLeb128(@intFromEnum(AbbrevCode.null))
1614 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), @intFromEnum(AbbrevCode.null))
16201615 else
16211616 std.leb.writeUnsignedFixed(
16221617 block_bytes,
1623 dib[block.abbrev_code..][0..block_bytes],
1618 wip_nav.debug_info.items[block.abbrev_code..][0..block_bytes],
16241619 try wip_nav.dwarf.refAbbrevCode(.empty_block),
16251620 );
1626 std.mem.writeInt(u32, dib[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
1621 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);
16271622 wip_nav.any_children = true;
16281623 }
16291624
......@@ -1636,18 +1631,18 @@ pub const WipNav = struct {
16361631 ) UpdateError!void {
16371632 const dwarf = wip_nav.dwarf;
16381633 const zcu = wip_nav.pt.zcu;
1639 const dibw = &wip_nav.debug_info.buffered_writer;
1634 const diw = wip_nav.debug_info.writer(dwarf.gpa);
16401635 const block = try wip_nav.blocks.addOne(dwarf.gpa);
16411636
1642 block.abbrev_code = @intCast(dibw.count);
1637 block.abbrev_code = @intCast(wip_nav.debug_info.items.len);
16431638 try wip_nav.abbrevCode(.inlined_func);
16441639 try wip_nav.refNav(zcu.funcInfo(func).owner_nav);
1645 try dibw.writeLeb128(zcu.navSrcLine(zcu.funcInfo(wip_nav.func).owner_nav) + line + 1);
1646 try dibw.writeLeb128(column + 1);
1640 try uleb128(diw, zcu.navSrcLine(zcu.funcInfo(wip_nav.func).owner_nav) + line + 1);
1641 try uleb128(diw, column + 1);
16471642 block.low_pc_off = code_off;
16481643 try wip_nav.infoAddrSym(wip_nav.func_sym_index, code_off);
1649 block.high_pc = @intCast(dibw.count);
1650 try dibw.writeInt(u32, 0, dwarf.endian);
1644 block.high_pc = @intCast(wip_nav.debug_info.items.len);
1645 try diw.writeInt(u32, 0, dwarf.endian);
16511646 try wip_nav.setInlineFunc(func);
16521647 wip_nav.any_children = false;
16531648 }
......@@ -1655,16 +1650,15 @@ pub const WipNav = struct {
16551650 pub fn leaveInlineFunc(wip_nav: *WipNav, func: InternPool.Index, code_off: u64) UpdateError!void {
16561651 const inlined_func_bytes = comptime uleb128Bytes(@intFromEnum(AbbrevCode.inlined_func));
16571652 const block = wip_nav.blocks.pop().?;
1658 const dib = wip_nav.debug_info.getWritten();
16591653 if (wip_nav.any_children)
1660 try wip_nav.debug_info.buffered_writer.writeLeb128(@intFromEnum(AbbrevCode.null))
1654 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), @intFromEnum(AbbrevCode.null))
16611655 else
16621656 std.leb.writeUnsignedFixed(
16631657 inlined_func_bytes,
1664 dib[block.abbrev_code..][0..inlined_func_bytes],
1658 wip_nav.debug_info.items[block.abbrev_code..][0..inlined_func_bytes],
16651659 try wip_nav.dwarf.refAbbrevCode(.empty_inlined_func),
16661660 );
1667 std.mem.writeInt(u32, dib[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
1661 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);
16681662 try wip_nav.setInlineFunc(func);
16691663 wip_nav.any_children = true;
16701664 }
......@@ -1678,22 +1672,22 @@ pub const WipNav = struct {
16781672 const new_file = zcu.navFileScopeIndex(new_func_info.owner_nav);
16791673 const new_unit = try dwarf.getUnit(zcu.fileByIndex(new_file).mod.?);
16801674
1681 const dlbw = &wip_nav.debug_line.buffered_writer;
1675 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
16821676 if (dwarf.incremental()) {
16831677 const new_nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, new_func_info.owner_nav);
16841678 errdefer _ = if (!new_nav_gop.found_existing) dwarf.navs.pop();
16851679 if (!new_nav_gop.found_existing) new_nav_gop.value_ptr.* = try dwarf.addCommonEntry(new_unit);
16861680
1687 try dlbw.writeByte(DW.LNS.extended_op);
1688 try dlbw.writeLeb128(1 + dwarf.sectionOffsetBytes());
1689 try dlbw.writeByte(DW.LNE.ZIG_set_decl);
1681 try dlw.writeByte(DW.LNS.extended_op);
1682 try uleb128(dlw, 1 + dwarf.sectionOffsetBytes());
1683 try dlw.writeByte(DW.LNE.ZIG_set_decl);
16901684 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{
1691 .source_off = @intCast(dlbw.count),
1685 .source_off = @intCast(wip_nav.debug_line.items.len),
16921686 .target_sec = .debug_info,
16931687 .target_unit = new_unit,
16941688 .target_entry = new_nav_gop.value_ptr.toOptional(),
16951689 });
1696 try dlbw.splatByteAll(0, dwarf.sectionOffsetBytes());
1690 try dlw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
16971691 return;
16981692 }
16991693
......@@ -1704,49 +1698,42 @@ pub const WipNav = struct {
17041698 try mod_info.dirs.put(dwarf.gpa, new_unit, {});
17051699 const file_gop = try mod_info.files.getOrPut(dwarf.gpa, new_file);
17061700
1707 try dlbw.writeByte(DW.LNS.set_file);
1708 try dlbw.writeLeb128(file_gop.index);
1701 try dlw.writeByte(DW.LNS.set_file);
1702 try uleb128(dlw, file_gop.index);
17091703 }
17101704
17111705 const old_src_line: i33 = zcu.navSrcLine(old_func_info.owner_nav);
17121706 const new_src_line: i33 = zcu.navSrcLine(new_func_info.owner_nav);
17131707 if (new_src_line != old_src_line) {
1714 try dlbw.writeByte(DW.LNS.advance_line);
1715 try dlbw.writeLeb128(new_src_line - old_src_line);
1708 try dlw.writeByte(DW.LNS.advance_line);
1709 try sleb128(dlw, new_src_line - old_src_line);
17161710 }
17171711
17181712 wip_nav.func = func;
17191713 }
17201714
1721 fn externalReloc(wip_nav: *WipNav, sec: *Section, reloc: ExternalReloc) Allocator.Error!void {
1715 fn externalReloc(wip_nav: *WipNav, sec: *Section, reloc: ExternalReloc) std.mem.Allocator.Error!void {
17221716 try sec.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(wip_nav.dwarf.gpa, reloc);
17231717 }
17241718
1725 pub fn infoExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) Allocator.Error!void {
1719 pub fn infoExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {
17261720 try wip_nav.externalReloc(&wip_nav.dwarf.debug_info.section, reloc);
17271721 }
17281722
1729 fn frameExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) Allocator.Error!void {
1723 fn frameExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {
17301724 try wip_nav.externalReloc(&wip_nav.dwarf.debug_frame.section, reloc);
17311725 }
17321726
17331727 fn abbrevCode(wip_nav: *WipNav, abbrev_code: AbbrevCode) UpdateError!void {
1734 try wip_nav.debug_info.buffered_writer.writeLeb128(try wip_nav.dwarf.refAbbrevCode(abbrev_code));
1728 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), try wip_nav.dwarf.refAbbrevCode(abbrev_code));
17351729 }
17361730
1737 fn sectionOffset(
1738 wip_nav: *WipNav,
1739 comptime sec: Section.Index,
1740 target_sec: Section.Index,
1741 target_unit: Unit.Index,
1742 target_entry: Entry.Index,
1743 target_off: u32,
1744 ) UpdateError!void {
1731 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 {
17451732 const dwarf = wip_nav.dwarf;
17461733 const gpa = dwarf.gpa;
17471734 const entry_ptr = @field(dwarf, @tagName(sec)).section.getUnit(wip_nav.unit).getEntry(wip_nav.entry);
1748 const bw = &@field(wip_nav, @tagName(sec)).buffered_writer;
1749 const source_off: u32 = @intCast(bw.count);
1735 const bytes = &@field(wip_nav, @tagName(sec));
1736 const source_off: u32 = @intCast(bytes.items.len);
17501737 if (target_sec != sec) {
17511738 try entry_ptr.cross_section_relocs.append(gpa, .{
17521739 .source_off = source_off,
......@@ -1769,7 +1756,7 @@ pub const WipNav = struct {
17691756 .target_off = target_off,
17701757 });
17711758 }
1772 try bw.splatByteAll(0, dwarf.sectionOffsetBytes());
1759 try bytes.appendNTimes(gpa, 0, dwarf.sectionOffsetBytes());
17731760 }
17741761
17751762 fn infoSectionOffset(wip_nav: *WipNav, target_sec: Section.Index, target_unit: Unit.Index, target_entry: Entry.Index, target_off: u32) UpdateError!void {
......@@ -1781,93 +1768,93 @@ pub const WipNav = struct {
17811768 }
17821769
17831770 const ExprLocCounter = struct {
1771 const Stream = std.io.CountingWriter(std.io.NullWriter);
1772 stream: Stream,
17841773 section_offset_bytes: u32,
17851774 address_size: AddressSize,
17861775 fn init(dwarf: *Dwarf) ExprLocCounter {
17871776 return .{
1777 .stream = std.io.countingWriter(std.io.null_writer),
17881778 .section_offset_bytes = dwarf.sectionOffsetBytes(),
17891779 .address_size = dwarf.address_size,
17901780 };
17911781 }
1782 fn writer(counter: *ExprLocCounter) Stream.Writer {
1783 return counter.stream.writer();
1784 }
17921785 fn endian(_: ExprLocCounter) std.builtin.Endian {
17931786 return @import("builtin").cpu.arch.endian();
17941787 }
1795 fn addrSym(counter: ExprLocCounter, bw: *Writer, _: u32) error{}!void {
1796 bw.count += @intFromEnum(counter.address_size);
1788 fn addrSym(counter: *ExprLocCounter, _: u32) error{}!void {
1789 counter.stream.bytes_written += @intFromEnum(counter.address_size);
17971790 }
1798 fn infoEntry(counter: ExprLocCounter, bw: *Writer, _: Unit.Index, _: Entry.Index) error{}!void {
1799 bw.count += counter.section_offset_bytes;
1791 fn infoEntry(counter: *ExprLocCounter, _: Unit.Index, _: Entry.Index) error{}!void {
1792 counter.stream.bytes_written += counter.section_offset_bytes;
18001793 }
18011794 };
18021795
18031796 fn infoExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1804 const bw = &wip_nav.debug_info.buffered_writer;
1805 const counter: ExprLocCounter = .init(wip_nav.dwarf);
1806 const start = bw.count;
1807 try loc.write(bw, counter);
1808 const len = bw.count - start;
1809 bw.count = start;
1810 wip_nav.debug_info.shrinkRetainingCapacity(start);
1797 var counter: ExprLocCounter = .init(wip_nav.dwarf);
1798 try loc.write(&counter);
18111799
18121800 const adapter: struct {
18131801 wip_nav: *WipNav,
1802 fn writer(ctx: @This()) std.ArrayListUnmanaged(u8).Writer {
1803 return ctx.wip_nav.debug_info.writer(ctx.wip_nav.dwarf.gpa);
1804 }
18141805 fn endian(ctx: @This()) std.builtin.Endian {
18151806 return ctx.wip_nav.dwarf.endian;
18161807 }
1817 fn addrSym(ctx: @This(), _: *Writer, sym_index: u32) UpdateError!void {
1808 fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
18181809 try ctx.wip_nav.infoAddrSym(sym_index, 0);
18191810 }
1820 fn infoEntry(ctx: @This(), _: *Writer, unit: Unit.Index, entry: Entry.Index) UpdateError!void {
1811 fn infoEntry(ctx: @This(), unit: Unit.Index, entry: Entry.Index) UpdateError!void {
18211812 try ctx.wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
18221813 }
18231814 } = .{ .wip_nav = wip_nav };
1824 try bw.writeLeb128(len);
1825 try loc.write(bw, adapter);
1815 try uleb128(adapter.writer(), counter.stream.bytes_written);
1816 try loc.write(adapter);
18261817 }
18271818
18281819 fn infoAddrSym(wip_nav: *WipNav, sym_index: u32, sym_off: u64) UpdateError!void {
1829 const dibw = &wip_nav.debug_info.buffered_writer;
18301820 try wip_nav.infoExternalReloc(.{
1831 .source_off = @intCast(dibw.count),
1821 .source_off = @intCast(wip_nav.debug_info.items.len),
18321822 .target_sym = sym_index,
18331823 .target_off = sym_off,
18341824 });
1835 try dibw.splatByteAll(0, @intFromEnum(wip_nav.dwarf.address_size));
1825 try wip_nav.debug_info.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
18361826 }
18371827
18381828 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1839 const bw = &wip_nav.debug_frame.buffered_writer;
1840 const counter: ExprLocCounter = .init(wip_nav.dwarf);
1841 const start = bw.count;
1842 try loc.write(bw, counter);
1843 const len = bw.count - start;
1844 bw.count = start;
1845 wip_nav.debug_frame.shrinkRetainingCapacity(start);
1829 var counter: ExprLocCounter = .init(wip_nav.dwarf);
1830 try loc.write(&counter);
18461831
18471832 const adapter: struct {
18481833 wip_nav: *WipNav,
1834 fn writer(ctx: @This()) std.ArrayListUnmanaged(u8).Writer {
1835 return ctx.wip_nav.debug_frame.writer(ctx.wip_nav.dwarf.gpa);
1836 }
18491837 fn endian(ctx: @This()) std.builtin.Endian {
18501838 return ctx.wip_nav.dwarf.endian;
18511839 }
1852 fn addrSym(ctx: @This(), _: *Writer, sym_index: u32) UpdateError!void {
1840 fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
18531841 try ctx.wip_nav.frameAddrSym(sym_index, 0);
18541842 }
1855 fn infoEntry(ctx: @This(), _: *Writer, unit: Unit.Index, entry: Entry.Index) UpdateError!void {
1843 fn infoEntry(ctx: @This(), unit: Unit.Index, entry: Entry.Index) UpdateError!void {
18561844 try ctx.wip_nav.sectionOffset(.debug_frame, .debug_info, unit, entry, 0);
18571845 }
18581846 } = .{ .wip_nav = wip_nav };
1859 try bw.writeLeb128(len);
1860 try loc.write(bw, adapter);
1847 try uleb128(adapter.writer(), counter.stream.bytes_written);
1848 try loc.write(adapter);
18611849 }
18621850
18631851 fn frameAddrSym(wip_nav: *WipNav, sym_index: u32, sym_off: u64) UpdateError!void {
1864 const dfbw = &wip_nav.debug_frame.buffered_writer;
18651852 try wip_nav.frameExternalReloc(.{
1866 .source_off = @intCast(dfbw.count),
1853 .source_off = @intCast(wip_nav.debug_frame.items.len),
18671854 .target_sym = sym_index,
18681855 .target_off = sym_off,
18691856 });
1870 try dfbw.splatByteAll(0, @intFromEnum(wip_nav.dwarf.address_size));
1857 try wip_nav.debug_frame.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
18711858 }
18721859
18731860 fn getNavEntry(wip_nav: *WipNav, nav_index: InternPool.Nav.Index) UpdateError!struct { Unit.Index, Entry.Index } {
......@@ -1935,41 +1922,38 @@ pub const WipNav = struct {
19351922 try wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
19361923 }
19371924
1938 fn refForward(wip_nav: *WipNav) Allocator.Error!u32 {
1925 fn refForward(wip_nav: *WipNav) std.mem.Allocator.Error!u32 {
19391926 const dwarf = wip_nav.dwarf;
1940 const dibw = &wip_nav.debug_info.buffered_writer;
19411927 const cross_entry_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs;
19421928 const reloc_index: u32 = @intCast(cross_entry_relocs.items.len);
19431929 try cross_entry_relocs.append(dwarf.gpa, .{
1944 .source_off = @intCast(dibw.count),
1930 .source_off = @intCast(wip_nav.debug_info.items.len),
19451931 .target_entry = undefined,
19461932 .target_off = undefined,
19471933 });
1948 try dibw.splatByteAll(0, dwarf.sectionOffsetBytes());
1934 try wip_nav.debug_info.appendNTimes(dwarf.gpa, 0, dwarf.sectionOffsetBytes());
19491935 return reloc_index;
19501936 }
19511937
19521938 fn finishForward(wip_nav: *WipNav, reloc_index: u32) void {
19531939 const reloc = &wip_nav.dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs.items[reloc_index];
19541940 reloc.target_entry = wip_nav.entry.toOptional();
1955 reloc.target_off = @intCast(wip_nav.debug_info.buffered_writer.count);
1941 reloc.target_off = @intCast(wip_nav.debug_info.items.len);
19561942 }
19571943
19581944 fn blockValue(wip_nav: *WipNav, src_loc: Zcu.LazySrcLoc, val: Value) UpdateError!void {
19591945 const ty = val.typeOf(wip_nav.pt.zcu);
1960 const dibw = &wip_nav.debug_info.buffered_writer;
1946 const diw = wip_nav.debug_info.writer(wip_nav.dwarf.gpa);
19611947 const bytes = if (ty.hasRuntimeBits(wip_nav.pt.zcu)) ty.abiSize(wip_nav.pt.zcu) else 0;
1962 try dibw.writeLeb128(bytes);
1948 try uleb128(diw, bytes);
19631949 if (bytes == 0) return;
1964 var dial = wip_nav.debug_info.toArrayList();
1965 defer _ = wip_nav.debug_info.fromArrayList(wip_nav.dwarf.gpa, &dial);
1966 const old_len = dial.items.len;
1950 const old_len = wip_nav.debug_info.items.len;
19671951 try codegen.generateSymbol(
19681952 wip_nav.dwarf.bin_file,
19691953 wip_nav.pt,
19701954 src_loc,
19711955 val,
1972 &dial,
1956 &wip_nav.debug_info,
19731957 .{ .debug_output = .{ .dwarf = wip_nav } },
19741958 );
19751959 if (old_len + bytes != wip_nav.debug_info.items.len) {
......@@ -1991,7 +1975,7 @@ pub const WipNav = struct {
19911975 big_int: std.math.big.int.Const,
19921976 ) UpdateError!void {
19931977 const zcu = wip_nav.pt.zcu;
1994 const dibw = &wip_nav.debug_info.buffered_writer;
1978 const diw = wip_nav.debug_info.writer(wip_nav.dwarf.gpa);
19951979 const signedness = switch (ty.toIntern()) {
19961980 .comptime_int_type, .comptime_float_type => .signed,
19971981 else => ty.intInfo(zcu).signedness,
......@@ -2002,7 +1986,7 @@ pub const WipNav = struct {
20021986 .signed => abbrev_code.sdata,
20031987 .unsigned => abbrev_code.udata,
20041988 });
2005 _ = try dibw.writableSliceGreedy(std.math.divCeil(usize, bits, 7) catch unreachable);
1989 try wip_nav.debug_info.ensureUnusedCapacity(wip_nav.dwarf.gpa, std.math.divCeil(usize, bits, 7) catch unreachable);
20061990 var bit: usize = 0;
20071991 var carry: u1 = 1;
20081992 while (bit < bits) {
......@@ -2019,17 +2003,16 @@ pub const WipNav = struct {
20192003 break :twos_comp_part twos_comp_part;
20202004 };
20212005 bit += 7;
2022 dibw.writeByte(@as(u8, if (bit < bits) 0x80 else 0x00) | twos_comp_part) catch unreachable;
2006 wip_nav.debug_info.appendAssumeCapacity(@as(u8, if (bit < bits) 0x80 else 0x00) | twos_comp_part);
20232007 }
20242008 } else {
20252009 try wip_nav.abbrevCode(abbrev_code.block);
20262010 const bytes = @max(ty.abiSize(zcu), std.math.divCeil(usize, bits, 8) catch unreachable);
2027 try dibw.writeLeb128(bytes);
2011 try uleb128(diw, bytes);
20282012 big_int.writeTwosComplement(
2029 try dibw.writableSliceGreedy(@intCast(bytes)),
2013 try wip_nav.debug_info.addManyAsSlice(wip_nav.dwarf.gpa, @intCast(bytes)),
20302014 wip_nav.dwarf.endian,
20312015 );
2032 dibw.advance(@intCast(bytes));
20332016 }
20342017 }
20352018
......@@ -2062,7 +2045,7 @@ pub const WipNav = struct {
20622045 const zcu = wip_nav.pt.zcu;
20632046 const ip = &zcu.intern_pool;
20642047 const dwarf = wip_nav.dwarf;
2065 const dibw = &wip_nav.debug_info.buffered_writer;
2048 const diw = wip_nav.debug_info.writer(dwarf.gpa);
20662049
20672050 const orig_entry = wip_nav.entry;
20682051 defer wip_nav.entry = orig_entry;
......@@ -2113,15 +2096,15 @@ pub const WipNav = struct {
21132096 try wip_nav.abbrevCode(if (is_generic_decl) abbrev_code.generic_decl else abbrev_code.decl);
21142097 try wip_nav.refType((if (is_generic_decl) null else parent_type) orelse
21152098 .fromInterned(zcu.fileRootType(file)));
2116 assert(dibw.count == DebugInfo.declEntryLineOff(dwarf));
2117 try dibw.writeInt(u32, decl.src_line + 1, dwarf.endian);
2118 try dibw.writeLeb128(decl.src_column + 1);
2119 try dibw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
2099 assert(wip_nav.debug_info.items.len == DebugInfo.declEntryLineOff(dwarf));
2100 try diw.writeInt(u32, decl.src_line + 1, dwarf.endian);
2101 try uleb128(diw, decl.src_column + 1);
2102 try diw.writeByte(if (decl.is_pub) DW.ACCESS.public else DW.ACCESS.private);
21202103 try wip_nav.strp(nav.name.toSlice(ip));
21212104
21222105 if (!is_generic_decl) return;
21232106 const generic_decl_entry = wip_nav.entry;
2124 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, generic_decl_entry, dwarf, wip_nav.debug_info.getWritten());
2107 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, generic_decl_entry, dwarf, wip_nav.debug_info.items);
21252108 wip_nav.debug_info.clearRetainingCapacity();
21262109 wip_nav.entry = orig_entry;
21272110 try wip_nav.abbrevCode(abbrev_code.decl_instance);
......@@ -2425,63 +2408,12 @@ pub fn initWipNav(
24252408 nav_index: InternPool.Nav.Index,
24262409 sym_index: u32,
24272410) error{ OutOfMemory, CodegenFail }!?WipNav {
2428 return dwarf.initWipNavInner(pt, nav_index, sym_index) catch |err| switch (err) {
2429 error.OutOfMemory => return error.OutOfMemory,
2430 else => |e| return pt.zcu.codegenFail(nav_index, "failed to init dwarf nav: {s}", .{@errorName(e)}),
2431 };
2432}
2433
2434pub fn finishWipNavFunc(
2435 dwarf: *Dwarf,
2436 pt: Zcu.PerThread,
2437 nav_index: InternPool.Nav.Index,
2438 code_size: u64,
2439 wip_nav: *WipNav,
2440) error{ OutOfMemory, CodegenFail }!void {
2441 return dwarf.finishWipNavFuncInner(pt, nav_index, code_size, wip_nav) catch |err| switch (err) {
2442 error.OutOfMemory => return error.OutOfMemory,
2443 else => |e| return pt.zcu.codegenFail(nav_index, "failed to finish dwarf func nav: {s}", .{@errorName(e)}),
2444 };
2445}
2446
2447pub fn finishWipNav(
2448 dwarf: *Dwarf,
2449 pt: Zcu.PerThread,
2450 nav_index: InternPool.Nav.Index,
2451 wip_nav: *WipNav,
2452) error{ OutOfMemory, CodegenFail }!void {
2453 return dwarf.finishWipNavInner(pt, nav_index, wip_nav) catch |err| switch (err) {
2454 error.OutOfMemory => return error.OutOfMemory,
2455 else => |e| return pt.zcu.codegenFail(nav_index, "failed to finish dwarf nav: {s}", .{@errorName(e)}),
2456 };
2457}
2458
2459pub fn updateComptimeNav(
2460 dwarf: *Dwarf,
2461 pt: Zcu.PerThread,
2462 nav_index: InternPool.Nav.Index,
2463) error{ OutOfMemory, CodegenFail }!void {
2464 return dwarf.updateComptimeNavInner(pt, nav_index) catch |err| switch (err) {
2465 error.OutOfMemory => return error.OutOfMemory,
2466 else => |e| return pt.zcu.codegenFail(nav_index, "failed to update dwarf comptime nav: {s}", .{@errorName(e)}),
2467 };
2468}
2469
2470pub fn updateContainerType(
2471 dwarf: *Dwarf,
2472 pt: Zcu.PerThread,
2473 type_index: InternPool.Index,
2474) error{ OutOfMemory, CodegenFail }!void {
2475 return dwarf.updateContainerType(pt, type_index) catch |err| switch (err) {
2411 return initWipNavInner(dwarf, pt, nav_index, sym_index) catch |err| switch (err) {
24762412 error.OutOfMemory => return error.OutOfMemory,
2477 else => |e| return pt.zcu.codegenFailType(type_index, "failed to update dwarf comptime nav: {s}", .{@errorName(e)}),
2413 else => |e| return pt.zcu.codegenFail(nav_index, "failed to init dwarf: {s}", .{@errorName(e)}),
24782414 };
24792415}
24802416
2481pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
2482 return dwarf.flushModuleInner(pt);
2483}
2484
24852417fn initWipNavInner(
24862418 dwarf: *Dwarf,
24872419 pt: Zcu.PerThread,
......@@ -2536,17 +2468,17 @@ fn initWipNavInner(
25362468 .func_high_pc = undefined,
25372469 .blocks = undefined,
25382470 .cfi = undefined,
2539 .debug_frame = undefined,
2540 .debug_info = undefined,
2541 .debug_line = undefined,
2542 .debug_loclists = undefined,
2471 .debug_frame = .empty,
2472 .debug_info = .empty,
2473 .debug_line = .empty,
2474 .debug_loclists = .empty,
25432475 .pending_lazy = .empty,
25442476 };
25452477 errdefer wip_nav.deinit();
25462478
25472479 switch (nav_key) {
25482480 else => {
2549 const dibw = &wip_nav.debug_info.buffered_writer;
2481 const diw = wip_nav.debug_info.writer(dwarf.gpa);
25502482 try wip_nav.declCommon(.{
25512483 .decl = .decl_var,
25522484 .generic_decl = .generic_decl_var,
......@@ -2567,9 +2499,9 @@ fn initWipNavInner(
25672499 .@"const" => {
25682500 const const_ty_reloc_index = try wip_nav.refForward();
25692501 try wip_nav.infoExprLoc(loc);
2570 try dibw.writeLeb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
2502 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
25712503 ty.abiAlignment(zcu).toByteUnits().?);
2572 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
2504 try diw.writeByte(@intFromBool(decl.linkage != .normal));
25732505 wip_nav.finishForward(const_ty_reloc_index);
25742506 try wip_nav.abbrevCode(.is_const);
25752507 try wip_nav.refType(ty);
......@@ -2577,9 +2509,9 @@ fn initWipNavInner(
25772509 .@"var" => {
25782510 try wip_nav.refType(ty);
25792511 try wip_nav.infoExprLoc(loc);
2580 try dibw.writeLeb128(dibw, nav.status.fully_resolved.alignment.toByteUnits() orelse
2512 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
25812513 ty.abiAlignment(zcu).toByteUnits().?);
2582 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
2514 try diw.writeByte(@intFromBool(decl.linkage != .normal));
25832515 },
25842516 }
25852517 },
......@@ -2604,39 +2536,39 @@ fn initWipNavInner(
26042536 .none => {},
26052537 .debug_frame, .eh_frame => |format| {
26062538 const entry = dwarf.debug_frame.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry);
2607 const dfbw = &wip_nav.debug_frame.buffered_writer;
2539 const dfw = wip_nav.debug_frame.writer(dwarf.gpa);
26082540 switch (dwarf.format) {
2609 .@"32" => try dfbw.writeInt(u32, undefined, dwarf.endian),
2541 .@"32" => try dfw.writeInt(u32, undefined, dwarf.endian),
26102542 .@"64" => {
2611 try dfbw.writeInt(u32, std.math.maxInt(u32), dwarf.endian);
2612 try dfbw.writeInt(u64, undefined, dwarf.endian);
2543 try dfw.writeInt(u32, std.math.maxInt(u32), dwarf.endian);
2544 try dfw.writeInt(u64, undefined, dwarf.endian);
26132545 },
26142546 }
26152547 switch (format) {
26162548 .none => unreachable,
26172549 .debug_frame => {
26182550 try entry.cross_entry_relocs.append(dwarf.gpa, .{
2619 .source_off = @intCast(dfbw.count),
2551 .source_off = @intCast(wip_nav.debug_frame.items.len),
26202552 });
2621 try dfbw.splatByteAll(0, dwarf.sectionOffsetBytes());
2553 try dfw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
26222554 try wip_nav.frameAddrSym(sym_index, 0);
2623 try dfbw.splatByteAll(undefined, @intFromEnum(dwarf.address_size));
2555 try dfw.writeByteNTimes(undefined, @intFromEnum(dwarf.address_size));
26242556 },
26252557 .eh_frame => {
2626 try dfbw.writeInt(u32, undefined, dwarf.endian);
2558 try dfw.writeInt(u32, undefined, dwarf.endian);
26272559 try wip_nav.frameExternalReloc(.{
2628 .source_off = @intCast(dfbw.count),
2560 .source_off = @intCast(wip_nav.debug_frame.items.len),
26292561 .target_sym = sym_index,
26302562 });
2631 try dfbw.writeInt(u32, 0, dwarf.endian);
2632 try dfbw.writeInt(u32, undefined, dwarf.endian);
2633 try dfbw.writeUleb128(0);
2563 try dfw.writeInt(u32, 0, dwarf.endian);
2564 try dfw.writeInt(u32, undefined, dwarf.endian);
2565 try uleb128(dfw, 0);
26342566 },
26352567 }
26362568 },
26372569 }
26382570
2639 const dibw = &wip_nav.debug_info.buffered_writer;
2571 const diw = wip_nav.debug_info.writer(dwarf.gpa);
26402572 try wip_nav.declCommon(.{
26412573 .decl = .decl_func,
26422574 .generic_decl = .generic_decl_func,
......@@ -2646,47 +2578,47 @@ fn initWipNavInner(
26462578 try wip_nav.refType(.fromInterned(func_type.return_type));
26472579 try wip_nav.infoAddrSym(sym_index, 0);
26482580 wip_nav.func_high_pc = @intCast(wip_nav.debug_info.items.len);
2649 try dibw.writeInt(u32, 0, dwarf.endian);
2581 try diw.writeInt(u32, 0, dwarf.endian);
26502582 const target = &mod.resolved_target.result;
2651 try dibw.writeLeb128(switch (nav.status.fully_resolved.alignment) {
2583 try uleb128(diw, switch (nav.status.fully_resolved.alignment) {
26522584 .none => target_info.defaultFunctionAlignment(target),
26532585 else => |a| a.maxStrict(target_info.minFunctionAlignment(target)),
26542586 }.toByteUnits().?);
2655 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
2656 try dibw.writeByte(@intFromBool(func_type.return_type == .noreturn_type));
2587 try diw.writeByte(@intFromBool(decl.linkage != .normal));
2588 try diw.writeByte(@intFromBool(func_type.return_type == .noreturn_type));
26572589
2658 const dlbw = &wip_nav.debug_line.buffered_writer;
2659 try dlbw.writeByte(DW.LNS.extended_op);
2590 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
2591 try dlw.writeByte(DW.LNS.extended_op);
26602592 if (dwarf.incremental()) {
2661 try dlbw.writeLeb128(1 + dwarf.sectionOffsetBytes());
2662 try dlbw.writeByte(DW.LNE.ZIG_set_decl);
2593 try uleb128(dlw, 1 + dwarf.sectionOffsetBytes());
2594 try dlw.writeByte(DW.LNE.ZIG_set_decl);
26632595 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{
2664 .source_off = @intCast(dlbw.count),
2596 .source_off = @intCast(wip_nav.debug_line.items.len),
26652597 .target_sec = .debug_info,
26662598 .target_unit = wip_nav.unit,
26672599 .target_entry = wip_nav.entry.toOptional(),
26682600 });
2669 try dlbw.splatByteAll(0, dwarf.sectionOffsetBytes());
2601 try dlw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
26702602
2671 try dlbw.writeByte(DW.LNS.set_column);
2672 try dlbw.writeLeb128(func.lbrace_column + 1);
2603 try dlw.writeByte(DW.LNS.set_column);
2604 try uleb128(dlw, func.lbrace_column + 1);
26732605
26742606 try wip_nav.advancePCAndLine(func.lbrace_line, 0);
26752607 } else {
2676 try dlbw.writeLeb128(1 + @intFromEnum(dwarf.address_size));
2677 try dlbw.writeByte(DW.LNE.set_address);
2608 try uleb128(dlw, 1 + @intFromEnum(dwarf.address_size));
2609 try dlw.writeByte(DW.LNE.set_address);
26782610 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{
2679 .source_off = @intCast(dlbw.count),
2611 .source_off = @intCast(wip_nav.debug_line.items.len),
26802612 .target_sym = sym_index,
26812613 });
2682 try dlbw.splatByteAll(0, @intFromEnum(dwarf.address_size));
2614 try dlw.writeByteNTimes(0, @intFromEnum(dwarf.address_size));
26832615
26842616 const file_gop = try dwarf.getModInfo(unit).files.getOrPut(dwarf.gpa, inst_info.file);
2685 try dlbw.writeByte(DW.LNS.set_file);
2686 try dlbw.writeLeb128(file_gop.index);
2617 try dlw.writeByte(DW.LNS.set_file);
2618 try uleb128(dlw, file_gop.index);
26872619
2688 try dlbw.writeByte(DW.LNS.set_column);
2689 try dlbw.writeLeb128(func.lbrace_column + 1);
2620 try dlw.writeByte(DW.LNS.set_column);
2621 try uleb128(dlw, func.lbrace_column + 1);
26902622
26912623 try wip_nav.advancePCAndLine(@intCast(decl.src_line + func.lbrace_line), 0);
26922624 }
......@@ -2695,7 +2627,7 @@ fn initWipNavInner(
26952627 return wip_nav;
26962628}
26972629
2698fn finishWipNavFuncInner(
2630pub fn finishWipNavFunc(
26992631 dwarf: *Dwarf,
27002632 pt: Zcu.PerThread,
27012633 nav_index: InternPool.Nav.Index,
......@@ -2724,9 +2656,12 @@ fn finishWipNavFuncInner(
27242656 switch (dwarf.debug_frame.header.format) {
27252657 .none => {},
27262658 .debug_frame, .eh_frame => |format| {
2727 const dfbw = &wip_nav.debug_frame.buffered_writer;
2728 try dfbw.splatByteAll(DW.CFA.nop, @intCast(dwarf.debug_frame.section.alignment.forward(dfbw.count) - dfbw.count));
2729 const contents = wip_nav.debug_frame.getWritten();
2659 try wip_nav.debug_frame.appendNTimes(
2660 dwarf.gpa,
2661 DW.CFA.nop,
2662 @intCast(dwarf.debug_frame.section.alignment.forward(wip_nav.debug_frame.items.len) - wip_nav.debug_frame.items.len),
2663 );
2664 const contents = wip_nav.debug_frame.items;
27302665 try dwarf.debug_frame.section.resizeEntry(wip_nav.unit, wip_nav.entry, dwarf, @intCast(contents.len));
27312666 const unit = dwarf.debug_frame.section.getUnit(wip_nav.unit);
27322667 const entry = unit.getEntry(wip_nav.entry);
......@@ -2753,14 +2688,14 @@ fn finishWipNavFuncInner(
27532688 },
27542689 }
27552690 {
2756 std.mem.writeInt(u32, wip_nav.debug_info.getWritten()[wip_nav.func_high_pc..][0..4], @intCast(code_size), dwarf.endian);
2691 std.mem.writeInt(u32, wip_nav.debug_info.items[wip_nav.func_high_pc..][0..4], @intCast(code_size), dwarf.endian);
27572692 if (wip_nav.any_children) {
2758 const dibw = &wip_nav.debug_info.buffered_writer;
2759 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
2693 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2694 try uleb128(diw, @intFromEnum(AbbrevCode.null));
27602695 } else {
2761 const abbrev_code_buf = wip_nav.debug_info.getWritten()[0..AbbrevCode.decl_bytes];
2762 var abbrev_code_br: std.io.Reader = .fixed(abbrev_code_buf);
2763 const abbrev_code: AbbrevCode = @enumFromInt(abbrev_code_br.takeLeb128(@typeInfo(AbbrevCode).@"enum".tag_type) catch unreachable);
2696 const abbrev_code_buf = wip_nav.debug_info.items[0..AbbrevCode.decl_bytes];
2697 var abbrev_code_fbs = std.io.fixedBufferStream(abbrev_code_buf);
2698 const abbrev_code: AbbrevCode = @enumFromInt(std.leb.readUleb128(@typeInfo(AbbrevCode).@"enum".tag_type, abbrev_code_fbs.reader()) catch unreachable);
27642699 std.leb.writeUnsignedFixed(
27652700 AbbrevCode.decl_bytes,
27662701 abbrev_code_buf,
......@@ -2792,10 +2727,10 @@ fn finishWipNavFuncInner(
27922727 );
27932728 }
27942729
2795 try dwarf.finishWipNavInner(pt, nav_index, wip_nav);
2730 try dwarf.finishWipNav(pt, nav_index, wip_nav);
27962731}
27972732
2798fn finishWipNavInner(
2733pub fn finishWipNav(
27992734 dwarf: *Dwarf,
28002735 pt: Zcu.PerThread,
28012736 nav_index: InternPool.Nav.Index,
......@@ -2806,20 +2741,26 @@ fn finishWipNavInner(
28062741 const nav = ip.getNav(nav_index);
28072742 log.debug("finishWipNav({f})", .{nav.fqn.fmt(ip)});
28082743
2809 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
2810 const debug_line = wip_nav.debug_line.getWritten();
2811 if (debug_line.len > 0) {
2812 const dlbw = &wip_nav.debug_line.buffered_writer;
2813 try dlbw.writeByte(DW.LNS.extended_op);
2814 try dlbw.writeUleb128(1);
2815 try dlbw.writeByte(DW.LNE.end_sequence);
2816 try dwarf.debug_line.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_line.getWritten());
2744 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
2745 if (wip_nav.debug_line.items.len > 0) {
2746 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
2747 try dlw.writeByte(DW.LNS.extended_op);
2748 try uleb128(dlw, 1);
2749 try dlw.writeByte(DW.LNE.end_sequence);
2750 try dwarf.debug_line.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_line.items);
28172751 }
2818 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.getWritten());
2752 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.items);
28192753
28202754 try wip_nav.updateLazy(zcu.navSrcLoc(nav_index));
28212755}
28222756
2757pub fn updateComptimeNav(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) error{ OutOfMemory, CodegenFail }!void {
2758 return updateComptimeNavInner(dwarf, pt, nav_index) catch |err| switch (err) {
2759 error.OutOfMemory => return error.OutOfMemory,
2760 else => |e| return pt.zcu.codegenFail(nav_index, "failed to update dwarf: {s}", .{@errorName(e)}),
2761 };
2762}
2763
28232764fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void {
28242765 const zcu = pt.zcu;
28252766 const ip = &zcu.intern_pool;
......@@ -2858,13 +2799,12 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
28582799 .func_high_pc = undefined,
28592800 .blocks = undefined,
28602801 .cfi = undefined,
2861 .debug_frame = undefined,
2862 .debug_info = undefined,
2863 .debug_line = undefined,
2864 .debug_loclists = undefined,
2802 .debug_frame = .empty,
2803 .debug_info = .empty,
2804 .debug_line = .empty,
2805 .debug_loclists = .empty,
28652806 .pending_lazy = .empty,
28662807 };
2867 wip_nav.init();
28682808 defer wip_nav.deinit();
28692809
28702810 const nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, nav_index);
......@@ -2908,7 +2848,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29082848 }
29092849 wip_nav.entry = nav_gop.value_ptr.*;
29102850
2911 const dibw = &wip_nav.debug_info.buffered_writer;
2851 const diw = wip_nav.debug_info.writer(dwarf.gpa);
29122852
29132853 switch (loaded_struct.layout) {
29142854 .auto, .@"extern" => {
......@@ -2921,9 +2861,9 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29212861 .generic_decl = .generic_decl_const,
29222862 .decl_instance = .decl_instance_struct,
29232863 }, &nav, inst_info.file, &decl);
2924 if (loaded_struct.field_types.len == 0) try dibw.writeByte(@intFromBool(false)) else {
2925 try dibw.writeLeb128(nav_val.toType().abiSize(zcu));
2926 try dibw.writeLeb128(nav_val.toType().abiAlignment(zcu).toByteUnits().?);
2864 if (loaded_struct.field_types.len == 0) try diw.writeByte(@intFromBool(false)) else {
2865 try uleb128(diw, nav_val.toType().abiSize(zcu));
2866 try uleb128(diw, nav_val.toType().abiAlignment(zcu).toByteUnits().?);
29272867 for (0..loaded_struct.field_types.len) |field_index| {
29282868 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
29292869 const field_init = loaded_struct.fieldInit(ip, field_index);
......@@ -2959,8 +2899,8 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29592899 }
29602900 try wip_nav.refType(field_type);
29612901 if (!is_comptime) {
2962 try dibw.writeLeb128(loaded_struct.offsets.get(ip)[field_index]);
2963 try dibw.writeLeb128(loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
2902 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
2903 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
29642904 field_type.abiAlignment(zcu).toByteUnits().?);
29652905 }
29662906 if (has_comptime_state)
......@@ -2968,7 +2908,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29682908 else if (has_runtime_bits)
29692909 try wip_nav.blockValue(nav_src_loc, .fromInterned(field_init));
29702910 }
2971 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
2911 try uleb128(diw, @intFromEnum(AbbrevCode.null));
29722912 }
29732913 },
29742914 .@"packed" => {
......@@ -2984,10 +2924,10 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29842924 try wip_nav.strp(loaded_struct.fieldName(ip, field_index).unwrap().?.toSlice(ip));
29852925 const field_type: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
29862926 try wip_nav.refType(field_type);
2987 try dibw.writeLeb128(field_bit_offset);
2927 try uleb128(diw, field_bit_offset);
29882928 field_bit_offset += @intCast(field_type.bitSize(zcu));
29892929 }
2990 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
2930 try uleb128(diw, @intFromEnum(AbbrevCode.null));
29912931 },
29922932 }
29932933 break :tag .done;
......@@ -3010,7 +2950,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30102950 type_gop.value_ptr.* = nav_gop.value_ptr.*;
30112951 }
30122952 wip_nav.entry = nav_gop.value_ptr.*;
3013 const dibw = &wip_nav.debug_info.buffered_writer;
2953 const diw = wip_nav.debug_info.writer(dwarf.gpa);
30142954 try wip_nav.declCommon(if (loaded_enum.names.len > 0) .{
30152955 .decl = .decl_enum,
30162956 .generic_decl = .generic_decl_const,
......@@ -3029,7 +2969,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30292969 }, field_index);
30302970 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
30312971 }
3032 if (loaded_enum.names.len > 0) try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
2972 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
30332973 break :tag .done;
30342974 },
30352975 .union_type => tag: {
......@@ -3049,15 +2989,15 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30492989 type_gop.value_ptr.* = nav_gop.value_ptr.*;
30502990 }
30512991 wip_nav.entry = nav_gop.value_ptr.*;
3052 const dibw = &wip_nav.debug_info.buffered_writer;
2992 const diw = wip_nav.debug_info.writer(dwarf.gpa);
30532993 try wip_nav.declCommon(.{
30542994 .decl = .decl_union,
30552995 .generic_decl = .generic_decl_const,
30562996 .decl_instance = .decl_instance_union,
30572997 }, &nav, inst_info.file, &decl);
30582998 const union_layout = Type.getUnionLayout(loaded_union, zcu);
3059 try dibw.writeLeb128(union_layout.abi_size);
3060 try dibw.writeLeb128(union_layout.abi_align.toByteUnits().?);
2999 try uleb128(diw, union_layout.abi_size);
3000 try uleb128(diw, union_layout.abi_align.toByteUnits().?);
30613001 const loaded_tag = loaded_union.loadTagType(ip);
30623002 if (loaded_union.hasTag(ip)) {
30633003 try wip_nav.abbrevCode(.tagged_union);
......@@ -3065,13 +3005,13 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30653005 .debug_info,
30663006 wip_nav.unit,
30673007 wip_nav.entry,
3068 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
3008 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
30693009 );
30703010 {
30713011 try wip_nav.abbrevCode(.generated_field);
30723012 try wip_nav.strp("tag");
30733013 try wip_nav.refType(.fromInterned(loaded_union.enum_tag_ty));
3074 try dibw.writeLeb128(union_layout.tagOffset());
3014 try uleb128(diw, union_layout.tagOffset());
30753015
30763016 for (0..loaded_union.field_types.len) |field_index| {
30773017 try wip_nav.enumConstValue(loaded_tag, .{
......@@ -3084,23 +3024,23 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30843024 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
30853025 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
30863026 try wip_nav.refType(field_type);
3087 try dibw.writeLeb128(union_layout.payloadOffset());
3088 try dibw.writeLeb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
3027 try uleb128(diw, union_layout.payloadOffset());
3028 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
30893029 if (field_type.isNoReturn(zcu)) 1 else field_type.abiAlignment(zcu).toByteUnits().?);
30903030 }
3091 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3031 try uleb128(diw, @intFromEnum(AbbrevCode.null));
30923032 }
30933033 }
3094 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3034 try uleb128(diw, @intFromEnum(AbbrevCode.null));
30953035 } else for (0..loaded_union.field_types.len) |field_index| {
30963036 try wip_nav.abbrevCode(.untagged_union_field);
30973037 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
30983038 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
30993039 try wip_nav.refType(field_type);
3100 try dibw.writeLeb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
3040 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
31013041 field_type.abiAlignment(zcu).toByteUnits().?);
31023042 }
3103 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3043 try uleb128(diw, @intFromEnum(AbbrevCode.null));
31043044 break :tag .done;
31053045 },
31063046 .opaque_type => tag: {
......@@ -3120,13 +3060,13 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31203060 type_gop.value_ptr.* = nav_gop.value_ptr.*;
31213061 }
31223062 wip_nav.entry = nav_gop.value_ptr.*;
3123 const dibw = &wip_nav.debug_info.buffered_writer;
3063 const diw = wip_nav.debug_info.writer(dwarf.gpa);
31243064 try wip_nav.declCommon(.{
31253065 .decl = .decl_namespace_struct,
31263066 .generic_decl = .generic_decl_const,
31273067 .decl_instance = .decl_instance_namespace_struct,
31283068 }, &nav, inst_info.file, &decl);
3129 try dibw.writeByte(@intFromBool(true));
3069 try diw.writeByte(@intFromBool(true));
31303070 break :tag .done;
31313071 },
31323072 .undef,
......@@ -3164,7 +3104,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31643104 const is_nullary = !func_type.is_var_args and for (0..func_type.param_types.len) |param_index| {
31653105 if (!func_type.paramIsComptime(std.math.cast(u5, param_index) orelse break false)) break false;
31663106 } else true;
3167 const dibw = &wip_nav.debug_info.buffered_writer;
3107 const diw = wip_nav.debug_info.writer(dwarf.gpa);
31683108 try wip_nav.declCommon(if (is_nullary) .{
31693109 .decl = .decl_nullary_func_generic,
31703110 .generic_decl = .generic_decl_func,
......@@ -3183,7 +3123,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31833123 try wip_nav.refType(.fromInterned(func_type.param_types.get(ip)[param_index]));
31843124 }
31853125 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3186 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3126 try uleb128(diw, @intFromEnum(AbbrevCode.null));
31873127 }
31883128 break :tag .done;
31893129 },
......@@ -3208,7 +3148,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32083148 try wip_nav.refType(nav_val.toType());
32093149 },
32103150 .decl_var => {
3211 const dibw = &wip_nav.debug_info.buffered_writer;
3151 const diw = wip_nav.debug_info.writer(dwarf.gpa);
32123152 try wip_nav.declCommon(.{
32133153 .decl = .decl_var,
32143154 .generic_decl = .generic_decl_var,
......@@ -3218,12 +3158,12 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32183158 const nav_ty = nav_val.typeOf(zcu);
32193159 try wip_nav.refType(nav_ty);
32203160 try wip_nav.blockValue(nav_src_loc, nav_val);
3221 try dibw.writeLeb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
3161 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
32223162 nav_ty.abiAlignment(zcu).toByteUnits().?);
3223 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
3163 try diw.writeByte(@intFromBool(decl.linkage != .normal));
32243164 },
32253165 .decl_const => {
3226 const dibw = &wip_nav.debug_info.buffered_writer;
3166 const diw = wip_nav.debug_info.writer(dwarf.gpa);
32273167 const nav_ty = nav_val.typeOf(zcu);
32283168 const has_runtime_bits = nav_ty.hasRuntimeBits(zcu);
32293169 const has_comptime_state = nav_ty.comptimeOnly(zcu) and try nav_ty.onePossibleValue(pt) == null;
......@@ -3246,9 +3186,9 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32463186 }, &nav, inst_info.file, &decl);
32473187 try wip_nav.strp(nav.fqn.toSlice(ip));
32483188 const nav_ty_reloc_index = try wip_nav.refForward();
3249 try dibw.writeLeb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
3189 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
32503190 nav_ty.abiAlignment(zcu).toByteUnits().?);
3251 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
3191 try diw.writeByte(@intFromBool(decl.linkage != .normal));
32523192 if (has_runtime_bits) try wip_nav.blockValue(nav_src_loc, nav_val);
32533193 if (has_comptime_state) try wip_nav.refValue(nav_val);
32543194 wip_nav.finishForward(nav_ty_reloc_index);
......@@ -3264,7 +3204,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32643204 try wip_nav.refNav(owner_nav);
32653205 },
32663206 }
3267 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
3207 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
32683208 try wip_nav.updateLazy(nav_src_loc);
32693209}
32703210
......@@ -3295,19 +3235,18 @@ fn updateLazyType(
32953235 .func_high_pc = undefined,
32963236 .blocks = undefined,
32973237 .cfi = undefined,
3298 .debug_frame = undefined,
3299 .debug_info = undefined,
3300 .debug_line = undefined,
3301 .debug_loclists = undefined,
3238 .debug_frame = .empty,
3239 .debug_info = .empty,
3240 .debug_line = .empty,
3241 .debug_loclists = .empty,
33023242 .pending_lazy = pending_lazy.*,
33033243 };
3304 wip_nav.init();
33053244 defer {
33063245 pending_lazy.* = wip_nav.pending_lazy;
33073246 wip_nav.pending_lazy = .empty;
33083247 wip_nav.deinit();
33093248 }
3310 const dibw = &wip_nav.debug_info.buffered_writer;
3249 const diw = wip_nav.debug_info.writer(dwarf.gpa);
33113250 const name = switch (type_index) {
33123251 .generic_poison_type => "",
33133252 else => try std.fmt.allocPrint(dwarf.gpa, "{f}", .{ty.fmt(pt)}),
......@@ -3322,12 +3261,12 @@ fn updateLazyType(
33223261 .int_type => |int_type| {
33233262 try wip_nav.abbrevCode(.numeric_type);
33243263 try wip_nav.strp(name);
3325 try dibw.writeByte(switch (int_type.signedness) {
3264 try diw.writeByte(switch (int_type.signedness) {
33263265 inline .signed, .unsigned => |signedness| @field(DW.ATE, @tagName(signedness)),
33273266 });
3328 try dibw.writeLeb128(int_type.bits);
3329 try dibw.writeLeb128(ty.abiSize(zcu));
3330 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
3267 try uleb128(diw, int_type.bits);
3268 try uleb128(diw, ty.abiSize(zcu));
3269 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
33313270 },
33323271 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {
33333272 .one, .many, .c => {
......@@ -3335,14 +3274,14 @@ fn updateLazyType(
33353274 try wip_nav.abbrevCode(if (ptr_type.sentinel == .none) .ptr_type else .ptr_sentinel_type);
33363275 try wip_nav.strp(name);
33373276 if (ptr_type.sentinel != .none) try wip_nav.blockValue(src_loc, .fromInterned(ptr_type.sentinel));
3338 try dibw.writeLeb128(ptr_type.flags.alignment.toByteUnits() orelse
3277 try uleb128(diw, ptr_type.flags.alignment.toByteUnits() orelse
33393278 ptr_child_type.abiAlignment(zcu).toByteUnits().?);
3340 try dibw.writeByte(@intFromEnum(ptr_type.flags.address_space));
3279 try diw.writeByte(@intFromEnum(ptr_type.flags.address_space));
33413280 if (ptr_type.flags.is_const or ptr_type.flags.is_volatile) try wip_nav.infoSectionOffset(
33423281 .debug_info,
33433282 wip_nav.unit,
33443283 wip_nav.entry,
3345 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
3284 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
33463285 ) else try wip_nav.refType(ptr_child_type);
33473286 if (ptr_type.flags.is_const) {
33483287 try wip_nav.abbrevCode(.is_const);
......@@ -3350,7 +3289,7 @@ fn updateLazyType(
33503289 .debug_info,
33513290 wip_nav.unit,
33523291 wip_nav.entry,
3353 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
3292 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
33543293 ) else try wip_nav.refType(ptr_child_type);
33553294 }
33563295 if (ptr_type.flags.is_volatile) {
......@@ -3361,19 +3300,19 @@ fn updateLazyType(
33613300 .slice => {
33623301 try wip_nav.abbrevCode(.generated_struct_type);
33633302 try wip_nav.strp(name);
3364 try dibw.writeLeb128(ty.abiSize(zcu));
3365 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
3303 try uleb128(diw, ty.abiSize(zcu));
3304 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
33663305 try wip_nav.abbrevCode(.generated_field);
33673306 try wip_nav.strp("ptr");
33683307 const ptr_field_type = ty.slicePtrFieldType(zcu);
33693308 try wip_nav.refType(ptr_field_type);
3370 try dibw.writeUleb128(0);
3309 try uleb128(diw, 0);
33713310 try wip_nav.abbrevCode(.generated_field);
33723311 try wip_nav.strp("len");
33733312 const len_field_type: Type = .usize;
33743313 try wip_nav.refType(len_field_type);
3375 try dibw.writeLeb128(len_field_type.abiAlignment(zcu).forward(ptr_field_type.abiSize(zcu)));
3376 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3314 try uleb128(diw, len_field_type.abiAlignment(zcu).forward(ptr_field_type.abiSize(zcu)));
3315 try uleb128(diw, @intFromEnum(AbbrevCode.null));
33773316 },
33783317 },
33793318 .array_type => |array_type| {
......@@ -3384,8 +3323,8 @@ fn updateLazyType(
33843323 try wip_nav.refType(array_child_type);
33853324 try wip_nav.abbrevCode(.array_index);
33863325 try wip_nav.refType(.usize);
3387 try dibw.writeLeb128(array_type.len);
3388 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3326 try uleb128(diw, array_type.len);
3327 try uleb128(diw, @intFromEnum(AbbrevCode.null));
33893328 },
33903329 .vector_type => |vector_type| {
33913330 try wip_nav.abbrevCode(.vector_type);
......@@ -3393,22 +3332,22 @@ fn updateLazyType(
33933332 try wip_nav.refType(.fromInterned(vector_type.child));
33943333 try wip_nav.abbrevCode(.array_index);
33953334 try wip_nav.refType(.usize);
3396 try dibw.writeLeb128(vector_type.len);
3397 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3335 try uleb128(diw, vector_type.len);
3336 try uleb128(diw, @intFromEnum(AbbrevCode.null));
33983337 },
33993338 .opt_type => |opt_child_type_index| {
34003339 const opt_child_type: Type = .fromInterned(opt_child_type_index);
34013340 const opt_repr = optRepr(opt_child_type, zcu);
34023341 try wip_nav.abbrevCode(.generated_union_type);
34033342 try wip_nav.strp(name);
3404 try dibw.writeLeb128(ty.abiSize(zcu));
3405 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
3343 try uleb128(diw, ty.abiSize(zcu));
3344 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
34063345 switch (opt_repr) {
34073346 .opv_null => {
34083347 try wip_nav.abbrevCode(.generated_field);
34093348 try wip_nav.strp("null");
34103349 try wip_nav.refType(.null);
3411 try dibw.writeUleb128(0);
3350 try uleb128(diw, 0);
34123351 },
34133352 .unpacked, .error_set, .pointer => {
34143353 try wip_nav.abbrevCode(.tagged_union);
......@@ -3416,7 +3355,7 @@ fn updateLazyType(
34163355 .debug_info,
34173356 wip_nav.unit,
34183357 wip_nav.entry,
3419 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
3358 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
34203359 );
34213360 {
34223361 try wip_nav.abbrevCode(.generated_field);
......@@ -3425,7 +3364,7 @@ fn updateLazyType(
34253364 .opv_null => unreachable,
34263365 .unpacked => {
34273366 try wip_nav.refType(.bool);
3428 try dibw.writeLeb128(if (opt_child_type.hasRuntimeBits(zcu))
3367 try uleb128(diw, if (opt_child_type.hasRuntimeBits(zcu))
34293368 opt_child_type.abiSize(zcu)
34303369 else
34313370 0);
......@@ -3435,37 +3374,37 @@ fn updateLazyType(
34353374 .signedness = .unsigned,
34363375 .bits = zcu.errorSetBits(),
34373376 } })));
3438 try dibw.writeUleb128(0);
3377 try uleb128(diw, 0);
34393378 },
34403379 .pointer => {
34413380 try wip_nav.refType(.usize);
3442 try dibw.writeUleb128(0);
3381 try uleb128(diw, 0);
34433382 },
34443383 }
34453384
34463385 try wip_nav.abbrevCode(.unsigned_tagged_union_field);
3447 try dibw.writeUleb128(0);
3386 try uleb128(diw, 0);
34483387 {
34493388 try wip_nav.abbrevCode(.generated_field);
34503389 try wip_nav.strp("null");
34513390 try wip_nav.refType(.null);
3452 try dibw.writeUleb128(0);
3391 try uleb128(diw, 0);
34533392 }
3454 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3393 try uleb128(diw, @intFromEnum(AbbrevCode.null));
34553394
34563395 try wip_nav.abbrevCode(.tagged_union_default_field);
34573396 {
34583397 try wip_nav.abbrevCode(.generated_field);
34593398 try wip_nav.strp("?");
34603399 try wip_nav.refType(opt_child_type);
3461 try dibw.writeUleb128(0);
3400 try uleb128(diw, 0);
34623401 }
3463 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3402 try uleb128(diw, @intFromEnum(AbbrevCode.null));
34643403 }
3465 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3404 try uleb128(diw, @intFromEnum(AbbrevCode.null));
34663405 },
34673406 }
3468 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3407 try uleb128(diw, @intFromEnum(AbbrevCode.null));
34693408 },
34703409 .anyframe_type => unreachable,
34713410 .error_union_type => |error_union_type| {
......@@ -3484,11 +3423,11 @@ fn updateLazyType(
34843423 if (error_union_type.error_set_type != .generic_poison_type and
34853424 error_union_type.payload_type != .generic_poison_type)
34863425 {
3487 try dibw.writeLeb128(ty.abiSize(zcu));
3488 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
3426 try uleb128(diw, ty.abiSize(zcu));
3427 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
34893428 } else {
3490 try dibw.writeUleb128(0);
3491 try dibw.writeUleb128(1);
3429 try uleb128(diw, 0);
3430 try uleb128(diw, 1);
34923431 }
34933432 {
34943433 try wip_nav.abbrevCode(.tagged_union);
......@@ -3496,7 +3435,7 @@ fn updateLazyType(
34963435 .debug_info,
34973436 wip_nav.unit,
34983437 wip_nav.entry,
3499 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
3438 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
35003439 );
35013440 {
35023441 try wip_nav.abbrevCode(.generated_field);
......@@ -3505,30 +3444,30 @@ fn updateLazyType(
35053444 .signedness = .unsigned,
35063445 .bits = zcu.errorSetBits(),
35073446 } })));
3508 try dibw.writeLeb128(error_union_error_set_offset);
3447 try uleb128(diw, error_union_error_set_offset);
35093448
35103449 try wip_nav.abbrevCode(.unsigned_tagged_union_field);
3511 try dibw.writeUleb128(0);
3450 try uleb128(diw, 0);
35123451 {
35133452 try wip_nav.abbrevCode(.generated_field);
35143453 try wip_nav.strp("value");
35153454 try wip_nav.refType(error_union_payload_type);
3516 try dibw.writeLeb128(error_union_payload_offset);
3455 try uleb128(diw, error_union_payload_offset);
35173456 }
3518 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3457 try uleb128(diw, @intFromEnum(AbbrevCode.null));
35193458
35203459 try wip_nav.abbrevCode(.tagged_union_default_field);
35213460 {
35223461 try wip_nav.abbrevCode(.generated_field);
35233462 try wip_nav.strp("error");
35243463 try wip_nav.refType(error_union_error_set_type);
3525 try dibw.writeLeb128(error_union_error_set_offset);
3464 try uleb128(diw, error_union_error_set_offset);
35263465 }
3527 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3466 try uleb128(diw, @intFromEnum(AbbrevCode.null));
35283467 }
3529 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3468 try uleb128(diw, @intFromEnum(AbbrevCode.null));
35303469 }
3531 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3470 try uleb128(diw, @intFromEnum(AbbrevCode.null));
35323471 },
35333472 .simple_type => |simple_type| switch (simple_type) {
35343473 .f16,
......@@ -3552,7 +3491,7 @@ fn updateLazyType(
35523491 => {
35533492 try wip_nav.abbrevCode(.numeric_type);
35543493 try wip_nav.strp(name);
3555 try dibw.writeByte(if (type_index == .bool_type)
3494 try diw.writeByte(if (type_index == .bool_type)
35563495 DW.ATE.boolean
35573496 else if (ty.isRuntimeFloat())
35583497 DW.ATE.float
......@@ -3562,9 +3501,9 @@ fn updateLazyType(
35623501 DW.ATE.unsigned
35633502 else
35643503 unreachable);
3565 try dibw.writeLeb128(ty.bitSize(zcu));
3566 try dibw.writeLeb128(ty.abiSize(zcu));
3567 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
3504 try uleb128(diw, ty.bitSize(zcu));
3505 try uleb128(diw, ty.abiSize(zcu));
3506 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
35683507 },
35693508 .anyopaque,
35703509 .void,
......@@ -3590,12 +3529,12 @@ fn updateLazyType(
35903529 .tuple_type => |tuple_type| if (tuple_type.types.len == 0) {
35913530 try wip_nav.abbrevCode(.generated_empty_struct_type);
35923531 try wip_nav.strp(name);
3593 try dibw.writeByte(@intFromBool(false));
3532 try diw.writeByte(@intFromBool(false));
35943533 } else {
35953534 try wip_nav.abbrevCode(.generated_struct_type);
35963535 try wip_nav.strp(name);
3597 try dibw.writeLeb128(ty.abiSize(zcu));
3598 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
3536 try uleb128(diw, ty.abiSize(zcu));
3537 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
35993538 var field_byte_offset: u64 = 0;
36003539 for (0..tuple_type.types.len) |field_index| {
36013540 const comptime_value = tuple_type.values.get(ip)[field_index];
......@@ -3624,8 +3563,8 @@ fn updateLazyType(
36243563 if (comptime_value == .none) {
36253564 const field_align = field_type.abiAlignment(zcu);
36263565 field_byte_offset = field_align.forward(field_byte_offset);
3627 try dibw.writeLeb128(field_byte_offset);
3628 try dibw.writeLeb128(field_type.abiAlignment(zcu).toByteUnits().?);
3566 try uleb128(diw, field_byte_offset);
3567 try uleb128(diw, field_type.abiAlignment(zcu).toByteUnits().?);
36293568 field_byte_offset += field_type.abiSize(zcu);
36303569 }
36313570 if (has_comptime_state)
......@@ -3633,7 +3572,7 @@ fn updateLazyType(
36333572 else if (has_runtime_bits)
36343573 try wip_nav.blockValue(src_loc, .fromInterned(comptime_value));
36353574 }
3636 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3575 try uleb128(diw, @intFromEnum(AbbrevCode.null));
36373576 },
36383577 .enum_type => {
36393578 const loaded_enum = ip.loadEnumType(type_index);
......@@ -3648,7 +3587,7 @@ fn updateLazyType(
36483587 }, field_index);
36493588 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
36503589 }
3651 if (loaded_enum.names.len > 0) try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3590 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
36523591 },
36533592 .func_type => |func_type| {
36543593 const is_nullary = func_type.param_types.len == 0 and !func_type.is_var_args;
......@@ -3716,7 +3655,7 @@ fn updateLazyType(
37163655 else => .nocall,
37173656 };
37183657 };
3719 try dibw.writeByte(@intFromEnum(cc));
3658 try diw.writeByte(@intFromEnum(cc));
37203659 try wip_nav.refType(.fromInterned(func_type.return_type));
37213660 if (!is_nullary) {
37223661 for (0..func_type.param_types.len) |param_index| {
......@@ -3724,7 +3663,7 @@ fn updateLazyType(
37243663 try wip_nav.refType(.fromInterned(func_type.param_types.get(ip)[param_index]));
37253664 }
37263665 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3727 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3666 try uleb128(diw, @intFromEnum(AbbrevCode.null));
37283667 }
37293668 },
37303669 .error_set_type => |error_set_type| {
......@@ -3737,10 +3676,10 @@ fn updateLazyType(
37373676 for (0..error_set_type.names.len) |field_index| {
37383677 const field_name = error_set_type.names.get(ip)[field_index];
37393678 try wip_nav.abbrevCode(.unsigned_enum_field);
3740 try dibw.writeLeb128(ip.getErrorValueIfExists(field_name).?);
3679 try uleb128(diw, ip.getErrorValueIfExists(field_name).?);
37413680 try wip_nav.strp(field_name.toSlice(ip));
37423681 }
3743 if (error_set_type.names.len > 0) try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3682 if (error_set_type.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
37443683 },
37453684 .inferred_error_set_type => |func| {
37463685 try wip_nav.abbrevCode(.inferred_error_set_type);
......@@ -3772,7 +3711,7 @@ fn updateLazyType(
37723711 .memoized_call,
37733712 => unreachable,
37743713 }
3775 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
3714 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
37763715}
37773716
37783717fn updateLazyValue(
......@@ -3800,19 +3739,18 @@ fn updateLazyValue(
38003739 .func_high_pc = undefined,
38013740 .blocks = undefined,
38023741 .cfi = undefined,
3803 .debug_frame = undefined,
3804 .debug_info = undefined,
3805 .debug_line = undefined,
3806 .debug_loclists = undefined,
3742 .debug_frame = .empty,
3743 .debug_info = .empty,
3744 .debug_line = .empty,
3745 .debug_loclists = .empty,
38073746 .pending_lazy = pending_lazy.*,
38083747 };
3809 wip_nav.init();
38103748 defer {
38113749 pending_lazy.* = wip_nav.pending_lazy;
38123750 wip_nav.pending_lazy = .empty;
38133751 wip_nav.deinit();
38143752 }
3815 const dibw = &wip_nav.debug_info.buffered_writer;
3753 const diw = wip_nav.debug_info.writer(dwarf.gpa);
38163754 var big_int_space: Value.BigIntSpace = undefined;
38173755 switch (ip.indexToKey(value_index)) {
38183756 .int_type,
......@@ -3850,7 +3788,7 @@ fn updateLazyValue(
38503788 .err => |err| {
38513789 try wip_nav.abbrevCode(.udata_comptime_value);
38523790 try wip_nav.refType(.fromInterned(err.ty));
3853 try dibw.writeLeb128(try pt.getErrorValue(err.name));
3791 try uleb128(diw, try pt.getErrorValue(err.name));
38543792 },
38553793 .error_union => |error_union| {
38563794 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -3862,8 +3800,8 @@ fn updateLazyValue(
38623800 {
38633801 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
38643802 try wip_nav.strp("is_error");
3865 try dibw.writeLeb128(err_abi_size);
3866 try dwarf.writeIntTo(dibw, err_abi_size, err_value);
3803 try uleb128(diw, err_abi_size);
3804 dwarf.writeInt(try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, err_abi_size), err_value);
38673805 }
38683806 payload_field: switch (error_union.val) {
38693807 .err_name => {},
......@@ -3887,8 +3825,8 @@ fn updateLazyValue(
38873825 {
38883826 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
38893827 try wip_nav.strp("error");
3890 try dibw.writeLeb128(err_abi_size);
3891 try dwarf.writeIntTo(dibw, err_abi_size, err_value);
3828 try uleb128(diw, err_abi_size);
3829 dwarf.writeInt(try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, err_abi_size), err_value);
38923830 }
38933831 switch (error_union.val) {
38943832 .err_name => {},
......@@ -3898,7 +3836,7 @@ fn updateLazyValue(
38983836 },
38993837 }
39003838 try wip_nav.refType(.fromInterned(error_union.ty));
3901 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3839 try uleb128(diw, @intFromEnum(AbbrevCode.null));
39023840 },
39033841 .enum_literal => |enum_literal| {
39043842 try wip_nav.abbrevCode(.string_comptime_value);
......@@ -3919,24 +3857,24 @@ fn updateLazyValue(
39193857 switch (float.storage) {
39203858 .f16 => |f16_val| {
39213859 try wip_nav.abbrevCode(.data2_comptime_value);
3922 try dibw.writeInt(u16, @bitCast(f16_val), dwarf.endian);
3860 try diw.writeInt(u16, @bitCast(f16_val), dwarf.endian);
39233861 },
39243862 .f32 => |f32_val| {
39253863 try wip_nav.abbrevCode(.data4_comptime_value);
3926 try dibw.writeInt(u32, @bitCast(f32_val), dwarf.endian);
3864 try diw.writeInt(u32, @bitCast(f32_val), dwarf.endian);
39273865 },
39283866 .f64 => |f64_val| {
39293867 try wip_nav.abbrevCode(.data8_comptime_value);
3930 try dibw.writeInt(u64, @bitCast(f64_val), dwarf.endian);
3868 try diw.writeInt(u64, @bitCast(f64_val), dwarf.endian);
39313869 },
39323870 .f80 => |f80_val| {
39333871 try wip_nav.abbrevCode(.block_comptime_value);
3934 try dibw.writeUleb128(@divExact(80, 8));
3935 try dibw.writeInt(u80, @bitCast(f80_val), dwarf.endian);
3872 try uleb128(diw, @divExact(80, 8));
3873 try diw.writeInt(u80, @bitCast(f80_val), dwarf.endian);
39363874 },
39373875 .f128 => |f128_val| {
39383876 try wip_nav.abbrevCode(.data16_comptime_value);
3939 try dibw.writeInt(u128, @bitCast(f128_val), dwarf.endian);
3877 try diw.writeInt(u128, @bitCast(f128_val), dwarf.endian);
39403878 },
39413879 }
39423880 try wip_nav.refType(.fromInterned(float.ty));
......@@ -3953,14 +3891,14 @@ fn updateLazyValue(
39533891 const uav_ty: Type = .fromInterned(ip.typeOf(uav.val));
39543892 if (try uav_ty.onePossibleValue(pt)) |_| {
39553893 try wip_nav.abbrevCode(.udata_comptime_value);
3956 try dibw.writeLeb128(ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment.toByteUnits() orelse
3894 try uleb128(diw, ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment.toByteUnits() orelse
39573895 uav_ty.abiAlignment(zcu).toByteUnits().?);
39583896 break :location;
39593897 } else break try wip_nav.getValueEntry(.fromInterned(uav.val));
39603898 },
39613899 .int => {
39623900 try wip_nav.abbrevCode(.udata_comptime_value);
3963 try dibw.writeLeb128(byte_offset);
3901 try uleb128(diw, byte_offset);
39643902 break :location;
39653903 },
39663904 .eu_payload => |eu_ptr| {
......@@ -3999,7 +3937,7 @@ fn updateLazyValue(
39993937 try wip_nav.strp("len");
40003938 try wip_nav.blockValue(src_loc, .fromInterned(slice.len));
40013939 }
4002 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3940 try uleb128(diw, @intFromEnum(AbbrevCode.null));
40033941 },
40043942 .opt => |opt| {
40053943 const opt_child_type: Type = .fromInterned(ip.indexToKey(opt.ty).opt_type);
......@@ -4009,7 +3947,7 @@ fn updateLazyValue(
40093947 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
40103948 try wip_nav.strp("has_value");
40113949 switch (optRepr(opt_child_type, zcu)) {
4012 .opv_null => try dibw.writeUleb128(0),
3950 .opv_null => try uleb128(diw, 0),
40133951 .unpacked => try wip_nav.blockValue(src_loc, .makeBool(opt.val != .none)),
40143952 .error_set => try wip_nav.blockValue(src_loc, .fromInterned(value_index)),
40153953 .pointer => if (opt_child_type.comptimeOnly(zcu)) {
......@@ -4019,8 +3957,8 @@ fn updateLazyValue(
40193957 .none => 0,
40203958 else => opt_child_type.ptrAlignment(zcu).toByteUnits().?,
40213959 });
4022 try dibw.writeLeb128(bytes.len);
4023 try dibw.writeAll(bytes);
3960 try uleb128(diw, bytes.len);
3961 try diw.writeAll(bytes);
40243962 } else try wip_nav.blockValue(src_loc, .fromInterned(value_index)),
40253963 }
40263964 }
......@@ -4039,7 +3977,7 @@ fn updateLazyValue(
40393977 else
40403978 try wip_nav.blockValue(src_loc, .fromInterned(opt.val));
40413979 }
4042 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
3980 try uleb128(diw, @intFromEnum(AbbrevCode.null));
40433981 },
40443982 .aggregate => |aggregate| {
40453983 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -4124,7 +4062,7 @@ fn updateLazyValue(
41244062 },
41254063 else => unreachable,
41264064 }
4127 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
4065 try uleb128(diw, @intFromEnum(AbbrevCode.null));
41284066 },
41294067 .un => |un| {
41304068 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -4149,11 +4087,11 @@ fn updateLazyValue(
41494087 else
41504088 try wip_nav.blockValue(src_loc, .fromInterned(un.val));
41514089 }
4152 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
4090 try uleb128(diw, @intFromEnum(AbbrevCode.null));
41534091 },
41544092 .memoized_call => unreachable, // not a value
41554093 }
4156 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
4094 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
41574095}
41584096
41594097fn optRepr(opt_child_type: Type, zcu: *const Zcu) enum {
......@@ -4173,7 +4111,7 @@ fn optRepr(opt_child_type: Type, zcu: *const Zcu) enum {
41734111 };
41744112}
41754113
4176fn updateContainerTypeInner(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternPool.Index) UpdateError!void {
4114pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternPool.Index) UpdateError!void {
41774115 const zcu = pt.zcu;
41784116 const ip = &zcu.intern_pool;
41794117 const ty: Type = .fromInterned(type_index);
......@@ -4500,26 +4438,25 @@ fn refAbbrevCode(dwarf: *Dwarf, abbrev_code: AbbrevCode) UpdateError!@typeInfo(A
45004438 assert(abbrev_code != .null);
45014439 const entry: Entry.Index = @enumFromInt(@intFromEnum(abbrev_code));
45024440 if (dwarf.debug_abbrev.section.getUnit(DebugAbbrev.unit).getEntry(entry).len > 0) return @intFromEnum(abbrev_code);
4503 var daaw: std.io.Writer.Allocating = .init(dwarf.gpa);
4504 defer daaw.deinit();
4505 const dabw = &daaw.interface;
4441 var debug_abbrev: std.ArrayList(u8) = .init(dwarf.gpa);
4442 defer debug_abbrev.deinit();
4443 const daw = debug_abbrev.writer();
45064444 const abbrev = AbbrevCode.abbrevs.get(abbrev_code);
4507 try dabw.writeLeb128(@intFromEnum(abbrev_code));
4508 try dabw.writeLeb128(@intFromEnum(abbrev.tag));
4509 try dabw.writeByte(if (abbrev.children) DW.CHILDREN.yes else DW.CHILDREN.no);
4510 for (abbrev.attrs) |*attr| inline for (attr) |info| try dabw.writeLeb128(@intFromEnum(info));
4511 for (0..2) |_| try dabw.writeUleb128(0);
4512 try dwarf.debug_abbrev.section.replaceEntry(DebugAbbrev.unit, entry, dwarf, daaw.getWritten());
4445 try uleb128(daw, @intFromEnum(abbrev_code));
4446 try uleb128(daw, @intFromEnum(abbrev.tag));
4447 try daw.writeByte(if (abbrev.children) DW.CHILDREN.yes else DW.CHILDREN.no);
4448 for (abbrev.attrs) |*attr| inline for (attr) |info| try uleb128(daw, @intFromEnum(info));
4449 for (0..2) |_| try uleb128(daw, 0);
4450 try dwarf.debug_abbrev.section.replaceEntry(DebugAbbrev.unit, entry, dwarf, debug_abbrev.items);
45134451 return @intFromEnum(abbrev_code);
45144452}
45154453
45164454pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
4517 const gpa = dwarf.gpa;
45184455 const zcu = pt.zcu;
45194456 const ip = &zcu.intern_pool;
45204457
45214458 {
4522 const type_gop = try dwarf.types.getOrPut(gpa, .anyerror_type);
4459 const type_gop = try dwarf.types.getOrPut(dwarf.gpa, .anyerror_type);
45234460 if (!type_gop.found_existing) type_gop.value_ptr.* = try dwarf.addCommonEntry(.main);
45244461 var wip_nav: WipNav = .{
45254462 .dwarf = dwarf,
......@@ -4532,15 +4469,14 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
45324469 .func_high_pc = undefined,
45334470 .blocks = undefined,
45344471 .cfi = undefined,
4535 .debug_frame = undefined,
4536 .debug_info = undefined,
4537 .debug_line = undefined,
4538 .debug_loclists = undefined,
4472 .debug_frame = .empty,
4473 .debug_info = .empty,
4474 .debug_line = .empty,
4475 .debug_loclists = .empty,
45394476 .pending_lazy = .empty,
45404477 };
4541 wip_nav.init();
45424478 defer wip_nav.deinit();
4543 const dibw = &wip_nav.debug_info.buffered_writer;
4479 const diw = wip_nav.debug_info.writer(dwarf.gpa);
45444480 const global_error_set_names = ip.global_error_set.getNamesFromMainThread();
45454481 try wip_nav.abbrevCode(if (global_error_set_names.len == 0) .generated_empty_enum_type else .generated_enum_type);
45464482 try wip_nav.strp("anyerror");
......@@ -4550,52 +4486,50 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
45504486 } })));
45514487 for (global_error_set_names, 1..) |name, value| {
45524488 try wip_nav.abbrevCode(.unsigned_enum_field);
4553 try dibw.writeLeb128(value);
4489 try uleb128(diw, value);
45544490 try wip_nav.strp(name.toSlice(ip));
45554491 }
4556 if (global_error_set_names.len > 0) try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
4557 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, dibw.getWritten());
4492 if (global_error_set_names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
4493 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
45584494 try wip_nav.updateLazy(.unneeded);
45594495 }
45604496
45614497 for (dwarf.mods.keys(), dwarf.mods.values()) |mod, *mod_info| {
4562 const root_dir_path = try mod.root.toAbsolute(zcu.comp.dirs, gpa);
4563 defer gpa.free(root_dir_path);
4498 const root_dir_path = try mod.root.toAbsolute(zcu.comp.dirs, dwarf.gpa);
4499 defer dwarf.gpa.free(root_dir_path);
45644500 mod_info.root_dir_path = try dwarf.debug_line_str.addString(dwarf, root_dir_path);
45654501 }
45664502
4567 var header: std.ArrayListUnmanaged(u8) = .empty;
4568 defer header.deinit(gpa);
4569 var header_bw: Writer = undefined;
4503 var header: std.ArrayList(u8) = .init(dwarf.gpa);
4504 defer header.deinit();
45704505 if (dwarf.debug_aranges.section.dirty) {
45714506 for (dwarf.debug_aranges.section.units.items, 0..) |*unit_ptr, unit_index| {
45724507 const unit: Unit.Index = @enumFromInt(unit_index);
45734508 unit_ptr.clear();
4574 try unit_ptr.cross_section_relocs.ensureTotalCapacity(gpa, 1);
4575 try header.resize(gpa, unit_ptr.header_len);
4576 header_bw = .fixed(header.items);
4509 try unit_ptr.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 1);
4510 header.clearRetainingCapacity();
4511 try header.ensureTotalCapacity(unit_ptr.header_len);
45774512 const unit_len = (if (unit_ptr.next.unwrap()) |next_unit|
45784513 dwarf.debug_aranges.section.getUnit(next_unit).off
45794514 else
45804515 dwarf.debug_aranges.section.len) - unit_ptr.off - dwarf.unitLengthBytes();
45814516 switch (dwarf.format) {
4582 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
4517 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
45834518 .@"64" => {
4584 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4585 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
4519 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4520 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
45864521 },
45874522 }
4588 header_bw.writeInt(u16, 2, dwarf.endian) catch unreachable;
4523 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 2, dwarf.endian);
45894524 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4590 .source_off = @intCast(header_bw.end),
4525 .source_off = @intCast(header.items.len),
45914526 .target_sec = .debug_info,
45924527 .target_unit = unit,
45934528 });
4594 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4595 header_bw.writeAll(&.{ @intFromEnum(dwarf.address_size), 0 }) catch unreachable;
4596 header_bw.splatByteAll(0, unit_ptr.header_len - header_bw.end) catch unreachable;
4597 assert(header_bw.end == header_bw.buffer.len);
4598 try unit_ptr.replaceHeader(&dwarf.debug_aranges.section, dwarf, header_bw.buffer);
4529 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4530 header.appendSliceAssumeCapacity(&.{ @intFromEnum(dwarf.address_size), 0 });
4531 header.appendNTimesAssumeCapacity(0, unit_ptr.header_len - header.items.len);
4532 try unit_ptr.replaceHeader(&dwarf.debug_aranges.section, dwarf, header.items);
45994533 try unit_ptr.writeTrailer(&dwarf.debug_aranges.section, dwarf);
46004534 }
46014535 dwarf.debug_aranges.section.dirty = false;
......@@ -4610,33 +4544,31 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
46104544 dev.check(.x86_64_backend);
46114545 const Register = @import("../arch/x86_64/bits.zig").Register;
46124546 for (dwarf.debug_frame.section.units.items) |*unit| {
4613 try header.resize(gpa, unit.header_len);
4614 header_bw = .fixed(header.items);
4547 header.clearRetainingCapacity();
4548 try header.ensureTotalCapacity(unit.header_len);
46154549 const unit_len = unit.header_len - dwarf.unitLengthBytes();
46164550 switch (dwarf.format) {
4617 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
4551 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
46184552 .@"64" => {
4619 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4620 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
4553 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4554 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
46214555 },
46224556 }
4623 header_bw.splatByteAll(0, 4) catch unreachable;
4624 header_bw.writeByte(1) catch unreachable;
4625 header_bw.writeAll("zR\x00") catch unreachable;
4626 header_bw.writeLeb128(dwarf.debug_frame.header.code_alignment_factor) catch unreachable;
4627 header_bw.writeLeb128(dwarf.debug_frame.header.data_alignment_factor) catch unreachable;
4628 header_bw.writeLeb128(dwarf.debug_frame.header.return_address_register) catch unreachable;
4629 header_bw.writeUleb128(1) catch unreachable;
4630 header_bw.writeByte(DW.EH.PE.pcrel | DW.EH.PE.sdata4) catch unreachable;
4631 header_bw.writeByte(DW.CFA.def_cfa_sf) catch unreachable;
4632 header_bw.writeUleb128(1) catch unreachable;
4633 header_bw.writeLeb128(Register.rsp.dwarfNum()) catch unreachable;
4634 header_bw.writeSleb128(-1) catch unreachable;
4635 header_bw.writeByte(@as(u8, DW.CFA.offset) + Register.rip.dwarfNum()) catch unreachable;
4636 header_bw.writeUleb128(1) catch unreachable;
4637 header_bw.splatByteAll(DW.CFA.nop, unit.header_len - header_bw.end) catch unreachable;
4638 assert(header_bw.end == header_bw.buffer.len);
4639 try unit.replaceHeader(&dwarf.debug_frame.section, dwarf, header_bw.buffer);
4557 header.appendNTimesAssumeCapacity(0, 4);
4558 header.appendAssumeCapacity(1);
4559 header.appendSliceAssumeCapacity("zR\x00");
4560 uleb128(header.fixedWriter(), dwarf.debug_frame.header.code_alignment_factor) catch unreachable;
4561 sleb128(header.fixedWriter(), dwarf.debug_frame.header.data_alignment_factor) catch unreachable;
4562 uleb128(header.fixedWriter(), dwarf.debug_frame.header.return_address_register) catch unreachable;
4563 uleb128(header.fixedWriter(), 1) catch unreachable;
4564 header.appendAssumeCapacity(DW.EH.PE.pcrel | DW.EH.PE.sdata4);
4565 header.appendAssumeCapacity(DW.CFA.def_cfa_sf);
4566 uleb128(header.fixedWriter(), Register.rsp.dwarfNum()) catch unreachable;
4567 sleb128(header.fixedWriter(), -1) catch unreachable;
4568 header.appendAssumeCapacity(@as(u8, DW.CFA.offset) + Register.rip.dwarfNum());
4569 uleb128(header.fixedWriter(), 1) catch unreachable;
4570 header.appendNTimesAssumeCapacity(DW.CFA.nop, unit.header_len - header.items.len);
4571 try unit.replaceHeader(&dwarf.debug_frame.section, dwarf, header.items);
46404572 try unit.writeTrailer(&dwarf.debug_frame.section, dwarf);
46414573 }
46424574 },
......@@ -4649,84 +4581,83 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
46494581 for (dwarf.mods.keys(), dwarf.mods.values(), dwarf.debug_info.section.units.items, 0..) |mod, mod_info, *unit_ptr, unit_index| {
46504582 const unit: Unit.Index = @enumFromInt(unit_index);
46514583 unit_ptr.clear();
4652 try unit_ptr.cross_unit_relocs.ensureTotalCapacity(gpa, 1);
4653 try unit_ptr.cross_section_relocs.ensureTotalCapacity(gpa, 7);
4654 try header.resize(gpa, unit_ptr.header_len);
4655 header_bw = .fixed(header.items);
4584 try unit_ptr.cross_unit_relocs.ensureTotalCapacity(dwarf.gpa, 1);
4585 try unit_ptr.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 7);
4586 header.clearRetainingCapacity();
4587 try header.ensureTotalCapacity(unit_ptr.header_len);
46564588 const unit_len = (if (unit_ptr.next.unwrap()) |next_unit|
46574589 dwarf.debug_info.section.getUnit(next_unit).off
46584590 else
46594591 dwarf.debug_info.section.len) - unit_ptr.off - dwarf.unitLengthBytes();
46604592 switch (dwarf.format) {
4661 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
4593 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
46624594 .@"64" => {
4663 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4664 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
4595 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4596 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
46654597 },
46664598 }
4667 header_bw.writeInt(u16, 5, dwarf.endian) catch unreachable;
4668 header_bw.writeAll(&.{ DW.UT.compile, @intFromEnum(dwarf.address_size) }) catch unreachable;
4599 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 5, dwarf.endian);
4600 header.appendSliceAssumeCapacity(&.{ DW.UT.compile, @intFromEnum(dwarf.address_size) });
46694601 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4670 .source_off = @intCast(header_bw.end),
4602 .source_off = @intCast(header.items.len),
46714603 .target_sec = .debug_abbrev,
46724604 .target_unit = DebugAbbrev.unit,
46734605 });
4674 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4675 const compile_unit_off: u32 = @intCast(header_bw.end);
4676 header_bw.writeLeb128(try dwarf.refAbbrevCode(.compile_unit)) catch unreachable;
4677 header_bw.writeByte(DW.LANG.Zig) catch unreachable;
4606 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4607 const compile_unit_off: u32 = @intCast(header.items.len);
4608 uleb128(header.fixedWriter(), try dwarf.refAbbrevCode(.compile_unit)) catch unreachable;
4609 header.appendAssumeCapacity(DW.LANG.Zig);
46784610 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4679 .source_off = @intCast(header_bw.end),
4611 .source_off = @intCast(header.items.len),
46804612 .target_sec = .debug_line_str,
46814613 .target_unit = StringSection.unit,
46824614 .target_entry = (try dwarf.debug_line_str.addString(dwarf, "zig " ++ @import("build_options").version)).toOptional(),
46834615 });
4684 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4616 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
46854617 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4686 .source_off = @intCast(header_bw.end),
4618 .source_off = @intCast(header.items.len),
46874619 .target_sec = .debug_line_str,
46884620 .target_unit = StringSection.unit,
46894621 .target_entry = mod_info.root_dir_path.toOptional(),
46904622 });
4691 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4623 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
46924624 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4693 .source_off = @intCast(header_bw.end),
4625 .source_off = @intCast(header.items.len),
46944626 .target_sec = .debug_line_str,
46954627 .target_unit = StringSection.unit,
46964628 .target_entry = (try dwarf.debug_line_str.addString(dwarf, mod.root_src_path)).toOptional(),
46974629 });
4698 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4630 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
46994631 unit_ptr.cross_unit_relocs.appendAssumeCapacity(.{
4700 .source_off = @intCast(header_bw.end),
4632 .source_off = @intCast(header.items.len),
47014633 .target_unit = .main,
47024634 .target_off = compile_unit_off,
47034635 });
4704 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4636 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
47054637 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4706 .source_off = @intCast(header_bw.end),
4638 .source_off = @intCast(header.items.len),
47074639 .target_sec = .debug_line,
47084640 .target_unit = unit,
47094641 });
4710 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4642 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
47114643 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4712 .source_off = @intCast(header_bw.end),
4644 .source_off = @intCast(header.items.len),
47134645 .target_sec = .debug_rnglists,
47144646 .target_unit = unit,
47154647 .target_off = DebugRngLists.baseOffset(dwarf),
47164648 });
4717 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4718 header_bw.writeUleb128(0) catch unreachable;
4719 header_bw.writeLeb128(try dwarf.refAbbrevCode(.module)) catch unreachable;
4649 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4650 uleb128(header.fixedWriter(), 0) catch unreachable;
4651 uleb128(header.fixedWriter(), try dwarf.refAbbrevCode(.module)) catch unreachable;
47204652 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4721 .source_off = @intCast(header_bw.end),
4653 .source_off = @intCast(header.items.len),
47224654 .target_sec = .debug_str,
47234655 .target_unit = StringSection.unit,
47244656 .target_entry = (try dwarf.debug_str.addString(dwarf, mod.fully_qualified_name)).toOptional(),
47254657 });
4726 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4727 header_bw.writeUleb128(0) catch unreachable;
4728 assert(header_bw.end == header_bw.buffer.len);
4729 try unit_ptr.replaceHeader(&dwarf.debug_info.section, dwarf, header_bw.buffer);
4658 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4659 uleb128(header.fixedWriter(), 0) catch unreachable;
4660 try unit_ptr.replaceHeader(&dwarf.debug_info.section, dwarf, header.items);
47304661 try unit_ptr.writeTrailer(&dwarf.debug_info.section, dwarf);
47314662 }
47324663 dwarf.debug_info.section.dirty = false;
......@@ -4750,37 +4681,33 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47504681 );
47514682 for (dwarf.mods.values(), dwarf.debug_line.section.units.items) |mod_info, *unit| {
47524683 unit.clear();
4753 try unit.cross_section_relocs.ensureTotalCapacity(gpa, mod_info.dirs.count() + 2 * (mod_info.files.count()));
4754 try header.resize(gpa, unit.header_len);
4755 header_bw = .fixed(header.items);
4684 try unit.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, mod_info.dirs.count() + 2 * (mod_info.files.count()));
4685 header.clearRetainingCapacity();
4686 try header.ensureTotalCapacity(unit.header_len);
47564687 const unit_len = (if (unit.next.unwrap()) |next_unit|
47574688 dwarf.debug_line.section.getUnit(next_unit).off
47584689 else
47594690 dwarf.debug_line.section.len) - unit.off - dwarf.unitLengthBytes();
47604691 switch (dwarf.format) {
4761 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
4692 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
47624693 .@"64" => {
4763 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4764 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
4694 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4695 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
47654696 },
47664697 }
4767 header_bw.writeInt(u16, 5, dwarf.endian) catch unreachable;
4768 header_bw.writeAll(&.{ @intFromEnum(dwarf.address_size), 0 }) catch unreachable;
4769 dwarf.writeIntTo(
4770 &header_bw,
4771 dwarf.sectionOffsetBytes(),
4772 unit.header_len - header_bw.end,
4773 ) catch unreachable;
4698 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 5, dwarf.endian);
4699 header.appendSliceAssumeCapacity(&.{ @intFromEnum(dwarf.address_size), 0 });
4700 dwarf.writeInt(header.addManyAsSliceAssumeCapacity(dwarf.sectionOffsetBytes()), unit.header_len - header.items.len);
47744701 const StandardOpcode = DeclValEnum(DW.LNS);
4775 header_bw.writeAll(&.{
4702 header.appendSliceAssumeCapacity(&[_]u8{
47764703 dwarf.debug_line.header.minimum_instruction_length,
47774704 dwarf.debug_line.header.maximum_operations_per_instruction,
47784705 @intFromBool(dwarf.debug_line.header.default_is_stmt),
47794706 @bitCast(dwarf.debug_line.header.line_base),
47804707 dwarf.debug_line.header.line_range,
47814708 dwarf.debug_line.header.opcode_base,
4782 }) catch unreachable;
4783 header_bw.writeAll(std.enums.EnumArray(StandardOpcode, u8).init(.{
4709 });
4710 header.appendSliceAssumeCapacity(std.enums.EnumArray(StandardOpcode, u8).init(.{
47844711 .extended_op = undefined,
47854712 .copy = 0,
47864713 .advance_pc = 1,
......@@ -4794,45 +4721,44 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47944721 .set_prologue_end = 0,
47954722 .set_epilogue_begin = 0,
47964723 .set_isa = 1,
4797 }).values[1..dwarf.debug_line.header.opcode_base]) catch unreachable;
4798 header_bw.writeByte(1) catch unreachable;
4799 header_bw.writeLeb128(@as(u14, DW.LNCT.path)) catch unreachable;
4800 header_bw.writeLeb128(@as(u13, DW.FORM.line_strp)) catch unreachable;
4801 header_bw.writeLeb128(mod_info.dirs.count()) catch unreachable;
4724 }).values[1..dwarf.debug_line.header.opcode_base]);
4725 header.appendAssumeCapacity(1);
4726 uleb128(header.fixedWriter(), DW.LNCT.path) catch unreachable;
4727 uleb128(header.fixedWriter(), DW.FORM.line_strp) catch unreachable;
4728 uleb128(header.fixedWriter(), mod_info.dirs.count()) catch unreachable;
48024729 for (mod_info.dirs.keys()) |dir_unit| {
48034730 unit.cross_section_relocs.appendAssumeCapacity(.{
4804 .source_off = @intCast(header_bw.end),
4731 .source_off = @intCast(header.items.len),
48054732 .target_sec = .debug_line_str,
48064733 .target_unit = StringSection.unit,
48074734 .target_entry = dwarf.getModInfo(dir_unit).root_dir_path.toOptional(),
48084735 });
4809 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4736 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
48104737 }
48114738 const dir_index_info = DebugLine.dirIndexInfo(@intCast(mod_info.dirs.count()));
4812 header_bw.writeByte(3) catch unreachable;
4813 header_bw.writeLeb128(@as(u14, DW.LNCT.path)) catch unreachable;
4814 header_bw.writeLeb128(@as(u13, DW.FORM.line_strp)) catch unreachable;
4815 header_bw.writeLeb128(@as(u14, DW.LNCT.directory_index)) catch unreachable;
4816 header_bw.writeLeb128(@intFromEnum(dir_index_info.form)) catch unreachable;
4817 header_bw.writeLeb128(@as(u14, DW.LNCT.LLVM_source)) catch unreachable;
4818 header_bw.writeLeb128(@as(u13, DW.FORM.line_strp)) catch unreachable;
4819 header_bw.writeLeb128(mod_info.files.count()) catch unreachable;
4739 header.appendAssumeCapacity(3);
4740 uleb128(header.fixedWriter(), DW.LNCT.path) catch unreachable;
4741 uleb128(header.fixedWriter(), DW.FORM.line_strp) catch unreachable;
4742 uleb128(header.fixedWriter(), DW.LNCT.directory_index) catch unreachable;
4743 uleb128(header.fixedWriter(), @intFromEnum(dir_index_info.form)) catch unreachable;
4744 uleb128(header.fixedWriter(), DW.LNCT.LLVM_source) catch unreachable;
4745 uleb128(header.fixedWriter(), DW.FORM.line_strp) catch unreachable;
4746 uleb128(header.fixedWriter(), mod_info.files.count()) catch unreachable;
48204747 for (mod_info.files.keys()) |file_index| {
48214748 const file = zcu.fileByIndex(file_index);
48224749 unit.cross_section_relocs.appendAssumeCapacity(.{
4823 .source_off = @intCast(header_bw.end),
4750 .source_off = @intCast(header.items.len),
48244751 .target_sec = .debug_line_str,
48254752 .target_unit = StringSection.unit,
48264753 .target_entry = (try dwarf.debug_line_str.addString(dwarf, file.sub_file_path)).toOptional(),
48274754 });
48284755 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4829 dwarf.writeIntTo(
4830 &header_bw,
4831 dir_index_info.bytes,
4756 dwarf.writeInt(
4757 header.addManyAsSliceAssumeCapacity(dir_index_info.bytes),
48324758 mod_info.dirs.getIndex(dwarf.getUnitIfExists(file.mod.?).?) orelse 0,
48334759 );
48344760 unit.cross_section_relocs.appendAssumeCapacity(.{
4835 .source_off = @intCast(header_bw.end),
4761 .source_off = @intCast(header.items.len),
48364762 .target_sec = .debug_line_str,
48374763 .target_unit = StringSection.unit,
48384764 .target_entry = (try dwarf.debug_line_str.addString(
......@@ -4840,10 +4766,9 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
48404766 if (file.is_builtin) file.source.? else "",
48414767 )).toOptional(),
48424768 });
4843 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
4769 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
48444770 }
4845 assert(header_bw.end == header_bw.buffer.len);
4846 try unit.replaceHeader(&dwarf.debug_line.section, dwarf, header_bw.buffer);
4771 try unit.replaceHeader(&dwarf.debug_line.section, dwarf, header.items);
48474772 try unit.writeTrailer(&dwarf.debug_line.section, dwarf);
48484773 }
48494774 dwarf.debug_line.section.dirty = false;
......@@ -4859,25 +4784,24 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
48594784 }
48604785 if (dwarf.debug_rnglists.section.dirty) {
48614786 for (dwarf.debug_rnglists.section.units.items) |*unit| {
4862 try header.resize(gpa, unit.header_len);
4863 header_bw = .fixed(header.items);
4787 header.clearRetainingCapacity();
4788 try header.ensureTotalCapacity(unit.header_len);
48644789 const unit_len = (if (unit.next.unwrap()) |next_unit|
48654790 dwarf.debug_rnglists.section.getUnit(next_unit).off
48664791 else
48674792 dwarf.debug_rnglists.section.len) - unit.off - dwarf.unitLengthBytes();
48684793 switch (dwarf.format) {
4869 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
4794 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
48704795 .@"64" => {
4871 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4872 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
4796 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), std.math.maxInt(u32), dwarf.endian);
4797 std.mem.writeInt(u64, header.addManyAsArrayAssumeCapacity(8), unit_len, dwarf.endian);
48734798 },
48744799 }
4875 header_bw.writeInt(u16, 5, dwarf.endian) catch unreachable;
4876 header_bw.writeAll(&.{ @intFromEnum(dwarf.address_size), 0 }) catch unreachable;
4877 header_bw.writeInt(u32, 1, dwarf.endian) catch unreachable;
4878 dwarf.writeIntTo(&header_bw, dwarf.sectionOffsetBytes(), dwarf.sectionOffsetBytes() * 1) catch unreachable;
4879 assert(header_bw.end == header_bw.buffer.len);
4880 try unit.replaceHeader(&dwarf.debug_rnglists.section, dwarf, header_bw.buffer);
4800 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 5, dwarf.endian);
4801 header.appendSliceAssumeCapacity(&.{ @intFromEnum(dwarf.address_size), 0 });
4802 std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), 1, dwarf.endian);
4803 dwarf.writeInt(header.addManyAsSliceAssumeCapacity(dwarf.sectionOffsetBytes()), dwarf.sectionOffsetBytes() * 1);
4804 try unit.replaceHeader(&dwarf.debug_rnglists.section, dwarf, header.items);
48814805 try unit.writeTrailer(&dwarf.debug_rnglists.section, dwarf);
48824806 }
48834807 dwarf.debug_rnglists.section.dirty = false;
......@@ -6076,10 +6000,6 @@ fn writeInt(dwarf: *Dwarf, buf: []u8, int: u64) void {
60766000 }
60776001}
60786002
6079fn writeIntTo(dwarf: *Dwarf, bw: *Writer, len: usize, int: u64) !void {
6080 dwarf.writeInt(try bw.writableSlice(len), int);
6081}
6082
60836003fn resolveReloc(dwarf: *Dwarf, source: u64, target: u64, size: u32) RelocError!void {
60846004 var buf: [8]u8 = undefined;
60856005 dwarf.writeInt(buf[0..size], target);
......@@ -6106,6 +6026,7 @@ fn uleb128Bytes(value: anytype) u32 {
61066026 d.writer.writeUleb128(value) catch unreachable;
61076027 return @intCast(d.count + d.writer.end);
61086028}
6029
61096030fn sleb128Bytes(value: anytype) u32 {
61106031 var trash_buffer: [64]u8 = undefined;
61116032 var d: std.Io.Writer.Discarding = .init(&trash_buffer);
......@@ -6132,7 +6053,7 @@ const codegen = @import("../codegen.zig");
61326053const dev = @import("../dev.zig");
61336054const link = @import("../link.zig");
61346055const log = std.log.scoped(.dwarf);
6056const sleb128 = std.leb.writeIleb128;
61356057const std = @import("std");
61366058const target_info = @import("../target.zig");
6137const Allocator = std.mem.Allocator;
6138const Writer = std.io.Writer;
6059const uleb128 = std.leb.writeUleb128;