authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-07 22:05:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-07 22:26:42-07:00
logc5a5983150c41843f30e136471e77fd3623a7510
treea9aa3347af08c279e1fc35049673e310584aea73
parent94dd28b7f7bdbbebe652d3e7a791bc86591ed9c8

random patches from another branch


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

src/link/Dwarf.zig+715-636
......@@ -1,4 +1,4 @@
1gpa: std.mem.Allocator,
1gpa: 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: std.mem.Allocator) void {
53 fn deinit(mod_info: *ModInfo, gpa: 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: std.mem.Allocator) void {
223 fn deinit(str_sec: *StringSection, gpa: 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) UpdateError!Entry.Index {
229 fn addString(str_sec: *StringSection, dwarf: *Dwarf, str: []const u8) !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: std.mem.Allocator) void {
300 fn deinit(sec: *Section, gpa: 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: std.mem.Allocator) void {
360 fn popUnit(sec: *Section, gpa: 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: std.mem.Allocator) void {
521 fn deinit(unit: *Unit, gpa: 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: std.mem.Allocator) std.mem.Allocator.Error!Entry.Index {
529 fn addEntry(unit: *Unit, gpa: Allocator) Allocator.Error!Entry.Index {
530530 if (unit.free.unwrap()) |entry| {
531531 const entry_ptr = unit.getEntry(entry);
532532 unit.free = entry_ptr.next;
......@@ -648,35 +648,33 @@ 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 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;
651 var bw: Writer = .fixed(&buf);
652 bw.writeByte(DW.LNS.extended_op) catch unreachable;
653 const extended_op_bytes = bw.end;
655654 var op_len_bytes: u5 = 1;
656655 while (true) switch (std.math.order(len - extended_op_bytes - op_len_bytes, @as(u32, 1) << 7 * op_len_bytes)) {
657 .lt => break uleb128(writer, len - extended_op_bytes - op_len_bytes) catch unreachable,
656 .lt => break bw.writeLeb128(len - extended_op_bytes - op_len_bytes) catch unreachable,
658657 .eq => {
659658 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
660659 op_len_bytes += 1;
661 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..op_len_bytes], len - extended_op_bytes - op_len_bytes);
662 fbs.pos += op_len_bytes;
660 std.leb.writeUnsignedExtended((bw.writableSlice(op_len_bytes) catch unreachable), len - extended_op_bytes - op_len_bytes);
663661 break;
664662 },
665663 .gt => op_len_bytes += 1,
666664 };
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);
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);
671669 }
672 var trailer = try std.ArrayList(u8).initCapacity(dwarf.gpa, len);
673 defer trailer.deinit();
670 var trailer_bw: Writer = .fixed(try dwarf.gpa.alloc(u8, len));
671 defer dwarf.gpa.free(trailer_bw.buffer);
674672 const fill_byte: u8 = if (sec == &dwarf.debug_abbrev.section) fill: {
675673 assert(uleb128Bytes(@intFromEnum(AbbrevCode.null)) == 1);
676 trailer.appendAssumeCapacity(@intFromEnum(AbbrevCode.null));
674 trailer_bw.writeByte(@intFromEnum(AbbrevCode.null)) catch unreachable;
677675 break :fill @intFromEnum(AbbrevCode.null);
678676 } else if (sec == &dwarf.debug_aranges.section) fill: {
679 trailer.appendNTimesAssumeCapacity(0, @intFromEnum(dwarf.address_size) * 2);
677 trailer_bw.splatByteAll(0, @intFromEnum(dwarf.address_size) * 2) catch unreachable;
680678 break :fill 0;
681679 } else if (sec == &dwarf.debug_frame.section) fill: {
682680 switch (dwarf.debug_frame.header.format) {
......@@ -684,49 +682,49 @@ const Unit = struct {
684682 .debug_frame, .eh_frame => |format| {
685683 const unit_len = len - dwarf.unitLengthBytes();
686684 switch (dwarf.format) {
687 .@"32" => std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
685 .@"32" => trailer_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
688686 .@"64" => {
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);
687 trailer_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
688 trailer_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
691689 },
692690 }
693691 switch (format) {
694692 .none => unreachable,
695693 .debug_frame => {
696694 switch (dwarf.format) {
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),
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,
699697 }
700 trailer.appendAssumeCapacity(4);
701 trailer.appendSliceAssumeCapacity("\x00");
702 trailer.appendAssumeCapacity(@intFromEnum(dwarf.address_size));
703 trailer.appendAssumeCapacity(0);
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;
704702 },
705703 .eh_frame => {
706 std.mem.writeInt(u32, trailer.addManyAsArrayAssumeCapacity(4), 0, dwarf.endian);
707 trailer.appendAssumeCapacity(1);
708 trailer.appendSliceAssumeCapacity("\x00");
704 trailer_bw.writeInt(u32, 0, dwarf.endian) catch unreachable;
705 trailer_bw.writeByte(1) catch unreachable;
706 trailer_bw.writeAll("\x00") catch unreachable;
709707 },
710708 }
711 uleb128(trailer.fixedWriter(), 1) catch unreachable;
712 sleb128(trailer.fixedWriter(), 1) catch unreachable;
713 uleb128(trailer.fixedWriter(), 0) catch unreachable;
709 trailer_bw.writeUleb128(1) catch unreachable;
710 trailer_bw.writeSleb128(1) catch unreachable;
711 trailer_bw.writeUleb128(0) catch unreachable;
714712 },
715713 }
716 trailer.appendNTimesAssumeCapacity(DW.CFA.nop, unit.trailer_len - trailer.items.len);
714 trailer_bw.splatByteAll(DW.CFA.nop, unit.trailer_len - trailer_bw.end) catch unreachable;
717715 break :fill DW.CFA.nop;
718716 } else if (sec == &dwarf.debug_info.section) fill: {
719717 assert(uleb128Bytes(@intFromEnum(AbbrevCode.null)) == 1);
720 trailer.appendNTimesAssumeCapacity(@intFromEnum(AbbrevCode.null), 2);
718 trailer_bw.splatByteAll(@intFromEnum(AbbrevCode.null), 2) catch unreachable;
721719 break :fill @intFromEnum(AbbrevCode.null);
722720 } else if (sec == &dwarf.debug_rnglists.section) fill: {
723 trailer.appendAssumeCapacity(DW.RLE.end_of_list);
721 trailer_bw.writeByte(DW.RLE.end_of_list) catch unreachable;
724722 break :fill DW.RLE.end_of_list;
725723 } else unreachable;
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);
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);
730728 }
731729
732730 fn resolveRelocs(unit: *Unit, sec: *Section, dwarf: *Dwarf) RelocError!void {
......@@ -780,7 +778,7 @@ const Entry = struct {
780778 entry.external_relocs.clearRetainingCapacity();
781779 }
782780
783 fn deinit(entry: *Entry, gpa: std.mem.Allocator) void {
781 fn deinit(entry: *Entry, gpa: Allocator) void {
784782 entry.cross_entry_relocs.deinit(gpa);
785783 entry.cross_unit_relocs.deinit(gpa);
786784 entry.cross_section_relocs.deinit(gpa);
......@@ -833,52 +831,52 @@ const Entry = struct {
833831 1 + uleb128Bytes(std.math.maxInt(u32)) + 1,
834832 )
835833 ]u8 = undefined;
836 var fbs = std.io.fixedBufferStream(&buf);
837 const writer = fbs.writer();
834 var bw: Writer = .fixed(&buf);
838835 if (sec == &dwarf.debug_info.section) switch (len) {
839836 0 => {},
840 1 => uleb128(writer, try dwarf.refAbbrevCode(.pad_1)) catch unreachable,
837 1 => bw.writeLeb128(try dwarf.refAbbrevCode(.pad_1)) catch unreachable,
841838 else => {
842 uleb128(writer, try dwarf.refAbbrevCode(.pad_n)) catch unreachable;
843 const abbrev_code_bytes = fbs.pos;
839 bw.writeLeb128(try dwarf.refAbbrevCode(.pad_n)) catch unreachable;
840 const abbrev_code_bytes = bw.end;
844841 var block_len_bytes: u5 = 1;
845842 while (true) switch (std.math.order(len - abbrev_code_bytes - block_len_bytes, @as(u32, 1) << 7 * block_len_bytes)) {
846 .lt => break uleb128(writer, len - abbrev_code_bytes - block_len_bytes) catch unreachable,
843 .lt => break bw.writeLeb128(len - abbrev_code_bytes - block_len_bytes) catch unreachable,
847844 .eq => {
848845 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
849846 block_len_bytes += 1;
850 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..block_len_bytes], len - abbrev_code_bytes - block_len_bytes);
851 fbs.pos += block_len_bytes;
847 std.leb.writeUnsignedExtended((try bw.writableSlice(block_len_bytes)), len - abbrev_code_bytes - block_len_bytes);
852848 break;
853849 },
854850 .gt => block_len_bytes += 1,
855851 };
856 assert(fbs.pos == abbrev_code_bytes + block_len_bytes);
852 assert(bw.end == abbrev_code_bytes + block_len_bytes);
857853 },
858854 } else if (sec == &dwarf.debug_line.section) switch (len) {
859855 0 => {},
860 1 => writer.writeByte(DW.LNS.const_add_pc) catch unreachable,
856 1 => bw.writeByte(DW.LNS.const_add_pc) catch unreachable,
861857 else => {
862 writer.writeByte(DW.LNS.extended_op) catch unreachable;
863 const extended_op_bytes = fbs.pos;
858 bw.writeByte(DW.LNS.extended_op) catch unreachable;
859 const extended_op_bytes = bw.end;
864860 var op_len_bytes: u5 = 1;
865861 while (true) switch (std.math.order(len - extended_op_bytes - op_len_bytes, @as(u32, 1) << 7 * op_len_bytes)) {
866 .lt => break uleb128(writer, len - extended_op_bytes - op_len_bytes) catch unreachable,
862 .lt => break bw.writeLeb128(len - extended_op_bytes - op_len_bytes) catch unreachable,
867863 .eq => {
868864 // no length will ever work, so undercount and futz with the leb encoding to make up the missing byte
869865 op_len_bytes += 1;
870 std.leb.writeUnsignedExtended(buf[fbs.pos..][0..op_len_bytes], len - extended_op_bytes - op_len_bytes);
871 fbs.pos += op_len_bytes;
866 std.leb.writeUnsignedExtended(
867 (bw.writableSlice(op_len_bytes) catch unreachable),
868 len - extended_op_bytes - op_len_bytes,
869 );
872870 break;
873871 },
874872 .gt => op_len_bytes += 1,
875873 };
876 assert(fbs.pos == extended_op_bytes + op_len_bytes);
877 if (len > 2) writer.writeByte(DW.LNE.padding) catch unreachable;
874 assert(bw.end == extended_op_bytes + op_len_bytes);
875 if (len > 2) bw.writeByte(DW.LNE.padding) catch unreachable;
878876 },
879877 } else assert(!sec.pad_entries_to_ideal and len == 0);
880 assert(fbs.pos <= len);
881 try dwarf.getFile().?.pwriteAll(fbs.getWritten(), sec.off(dwarf) + unit.off + unit.header_len + start);
878 assert(bw.end <= len);
879 try dwarf.getFile().?.pwriteAll(bw.getWritten(), sec.off(dwarf) + unit.off + unit.header_len + start);
882880 }
883881
884882 fn resize(entry_ptr: *Entry, unit: *Unit, sec: *Section, dwarf: *Dwarf, len: u32) UpdateError!void {
......@@ -1133,150 +1131,149 @@ pub const Loc = union(enum) {
11331131 };
11341132 }
11351133
1136 fn writeReg(reg: u32, op0: u8, opx: u8, writer: anytype) @TypeOf(writer).Error!void {
1134 fn writeReg(bw: *Writer, reg: u32, op0: u8, opx: u8) Writer.Error!void {
11371135 if (std.math.cast(u5, reg)) |small_reg| {
1138 try writer.writeByte(op0 + small_reg);
1136 try bw.writeByte(op0 + small_reg);
11391137 } else {
1140 try writer.writeByte(opx);
1141 try uleb128(writer, reg);
1138 try bw.writeByte(opx);
1139 try bw.writeLeb128(reg);
11421140 }
11431141 }
11441142
1145 fn write(loc: Loc, adapter: anytype) UpdateError!void {
1146 const writer = adapter.writer();
1143 fn write(loc: Loc, bw: *Writer, adapter: anytype) UpdateError!void {
11471144 switch (loc) {
11481145 .empty => {},
11491146 .addr_reloc => |sym_index| {
1150 try writer.writeByte(DW.OP.addr);
1147 try bw.writeByte(DW.OP.addr);
11511148 try adapter.addrSym(sym_index);
11521149 },
11531150 .deref => |addr| {
11541151 try addr.write(adapter);
1155 try writer.writeByte(DW.OP.deref);
1152 try bw.writeByte(DW.OP.deref);
11561153 },
11571154 .constu => |constu| if (std.math.cast(u5, constu)) |lit| {
1158 try writer.writeByte(@as(u8, DW.OP.lit0) + lit);
1155 try bw.writeByte(@as(u8, DW.OP.lit0) + lit);
11591156 } else if (std.math.cast(u8, constu)) |const1u| {
1160 try writer.writeAll(&.{ DW.OP.const1u, const1u });
1157 try bw.writeAll(&.{ DW.OP.const1u, const1u });
11611158 } else if (std.math.cast(u16, constu)) |const2u| {
1162 try writer.writeByte(DW.OP.const2u);
1163 try writer.writeInt(u16, const2u, adapter.endian());
1159 try bw.writeByte(DW.OP.const2u);
1160 try bw.writeInt(u16, const2u, adapter.endian());
11641161 } else if (std.math.cast(u21, constu)) |const3u| {
1165 try writer.writeByte(DW.OP.constu);
1166 try uleb128(writer, const3u);
1162 try bw.writeByte(DW.OP.constu);
1163 try bw.writeLeb128(const3u);
11671164 } else if (std.math.cast(u32, constu)) |const4u| {
1168 try writer.writeByte(DW.OP.const4u);
1169 try writer.writeInt(u32, const4u, adapter.endian());
1165 try bw.writeByte(DW.OP.const4u);
1166 try bw.writeInt(u32, const4u, adapter.endian());
11701167 } else if (std.math.cast(u49, constu)) |const7u| {
1171 try writer.writeByte(DW.OP.constu);
1172 try uleb128(writer, const7u);
1168 try bw.writeByte(DW.OP.constu);
1169 try bw.writeLeb128(const7u);
11731170 } else {
1174 try writer.writeByte(DW.OP.const8u);
1175 try writer.writeInt(u64, constu, adapter.endian());
1171 try bw.writeByte(DW.OP.const8u);
1172 try bw.writeInt(u64, constu, adapter.endian());
11761173 },
11771174 .consts => |consts| if (std.math.cast(i8, consts)) |const1s| {
1178 try writer.writeAll(&.{ DW.OP.const1s, @bitCast(const1s) });
1175 try bw.writeAll(&.{ DW.OP.const1s, @bitCast(const1s) });
11791176 } else if (std.math.cast(i16, consts)) |const2s| {
1180 try writer.writeByte(DW.OP.const2s);
1181 try writer.writeInt(i16, const2s, adapter.endian());
1177 try bw.writeByte(DW.OP.const2s);
1178 try bw.writeInt(i16, const2s, adapter.endian());
11821179 } else if (std.math.cast(i21, consts)) |const3s| {
1183 try writer.writeByte(DW.OP.consts);
1184 try sleb128(writer, const3s);
1180 try bw.writeByte(DW.OP.consts);
1181 try bw.writeLeb128(const3s);
11851182 } else if (std.math.cast(i32, consts)) |const4s| {
1186 try writer.writeByte(DW.OP.const4s);
1187 try writer.writeInt(i32, const4s, adapter.endian());
1183 try bw.writeByte(DW.OP.const4s);
1184 try bw.writeInt(i32, const4s, adapter.endian());
11881185 } else if (std.math.cast(i49, consts)) |const7s| {
1189 try writer.writeByte(DW.OP.consts);
1190 try sleb128(writer, const7s);
1186 try bw.writeByte(DW.OP.consts);
1187 try bw.writeLeb128(const7s);
11911188 } else {
1192 try writer.writeByte(DW.OP.const8s);
1193 try writer.writeInt(i64, consts, adapter.endian());
1189 try bw.writeByte(DW.OP.const8s);
1190 try bw.writeInt(i64, consts, adapter.endian());
11941191 },
11951192 .plus => |plus| done: {
11961193 if (plus[0].getConst(u0)) |_| {
1197 try plus[1].write(adapter);
1194 try plus[1].write(bw, adapter);
11981195 break :done;
11991196 }
12001197 if (plus[1].getConst(u0)) |_| {
1201 try plus[0].write(adapter);
1198 try plus[0].write(bw, adapter);
12021199 break :done;
12031200 }
12041201 if (plus[0].getBaseReg()) |breg| {
12051202 if (plus[1].getConst(i65)) |offset| {
1206 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1207 try sleb128(writer, offset);
1203 try writeReg(bw, breg, DW.OP.breg0, DW.OP.bregx);
1204 try bw.writeLeb128(offset);
12081205 break :done;
12091206 }
12101207 }
12111208 if (plus[1].getBaseReg()) |breg| {
12121209 if (plus[0].getConst(i65)) |offset| {
1213 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1214 try sleb128(writer, offset);
1210 try writeReg(bw, breg, DW.OP.breg0, DW.OP.bregx);
1211 try bw.writeLeb128(offset);
12151212 break :done;
12161213 }
12171214 }
12181215 if (plus[0].getConst(u64)) |uconst| {
1219 try plus[1].write(adapter);
1220 try writer.writeByte(DW.OP.plus_uconst);
1221 try uleb128(writer, uconst);
1216 try plus[1].write(bw, adapter);
1217 try bw.writeByte(DW.OP.plus_uconst);
1218 try bw.writeLeb128(uconst);
12221219 break :done;
12231220 }
12241221 if (plus[1].getConst(u64)) |uconst| {
1225 try plus[0].write(adapter);
1226 try writer.writeByte(DW.OP.plus_uconst);
1227 try uleb128(writer, uconst);
1222 try plus[0].write(bw, adapter);
1223 try bw.writeByte(DW.OP.plus_uconst);
1224 try bw.writeLeb128(uconst);
12281225 break :done;
12291226 }
1230 try plus[0].write(adapter);
1231 try plus[1].write(adapter);
1232 try writer.writeByte(DW.OP.plus);
1227 try plus[0].write(bw, adapter);
1228 try plus[1].write(bw, adapter);
1229 try bw.writeByte(DW.OP.plus);
12331230 },
1234 .reg => |reg| try writeReg(reg, DW.OP.reg0, DW.OP.regx, writer),
1231 .reg => |reg| try writeReg(bw, reg, DW.OP.reg0, DW.OP.regx),
12351232 .breg => |breg| {
1236 try writeReg(breg, DW.OP.breg0, DW.OP.bregx, writer);
1237 try sleb128(writer, 0);
1233 try writeReg(bw, breg, DW.OP.breg0, DW.OP.bregx);
1234 try bw.writeSleb128(0);
12381235 },
1239 .push_object_address => try writer.writeByte(DW.OP.push_object_address),
1236 .push_object_address => try bw.writeByte(DW.OP.push_object_address),
12401237 .call => |call| {
12411238 for (call.args) |arg| try arg.write(adapter);
1242 try writer.writeByte(DW.OP.call_ref);
1239 try bw.writeByte(DW.OP.call_ref);
12431240 try adapter.infoEntry(call.unit, call.entry);
12441241 },
12451242 .form_tls_address => |addr| {
1246 try addr.write(adapter);
1247 try writer.writeByte(DW.OP.form_tls_address);
1243 try addr.write(bw, adapter);
1244 try bw.writeByte(DW.OP.form_tls_address);
12481245 },
12491246 .implicit_value => |value| {
1250 try writer.writeByte(DW.OP.implicit_value);
1251 try uleb128(writer, value.len);
1252 try writer.writeAll(value);
1247 try bw.writeByte(DW.OP.implicit_value);
1248 try bw.writeLeb128(value.len);
1249 try bw.writeAll(value);
12531250 },
12541251 .stack_value => |value| {
1255 try value.write(adapter);
1256 try writer.writeByte(DW.OP.stack_value);
1252 try value.write(bw, adapter);
1253 try bw.writeByte(DW.OP.stack_value);
12571254 },
12581255 .implicit_pointer => |implicit_pointer| {
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);
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);
12621259 },
12631260 .wasm_ext => |wasm_ext| {
1264 try writer.writeByte(DW.OP.WASM_location);
1261 try bw.writeByte(DW.OP.WASM_location);
12651262 switch (wasm_ext) {
12661263 .local => |local| {
1267 try writer.writeByte(DW.OP.WASM_local);
1268 try uleb128(writer, local);
1264 try bw.writeByte(DW.OP.WASM_local);
1265 try bw.writeLeb128(local);
12691266 },
12701267 .global => |global| if (std.math.cast(u21, global)) |global_u21| {
1271 try writer.writeByte(DW.OP.WASM_global);
1272 try uleb128(writer, global_u21);
1268 try bw.writeByte(DW.OP.WASM_global);
1269 try bw.writeLeb128(global_u21);
12731270 } else {
1274 try writer.writeByte(DW.OP.WASM_global_u32);
1275 try writer.writeInt(u32, global, adapter.endian());
1271 try bw.writeByte(DW.OP.WASM_global_u32);
1272 try bw.writeInt(u32, global, adapter.endian());
12761273 },
12771274 .operand_stack => |operand_stack| {
1278 try writer.writeByte(DW.OP.WASM_operand_stack);
1279 try uleb128(writer, operand_stack);
1275 try bw.writeByte(DW.OP.WASM_operand_stack);
1276 try bw.writeLeb128(operand_stack);
12801277 },
12811278 }
12821279 },
......@@ -1309,21 +1306,21 @@ pub const Cfa = union(enum) {
13091306 const RegExpr = struct { reg: u32, expr: Loc };
13101307
13111308 fn write(cfa: Cfa, wip_nav: *WipNav) UpdateError!void {
1312 const writer = wip_nav.debug_frame.writer(wip_nav.dwarf.gpa);
1309 const bw = &wip_nav.debug_frame.buffered_writer;
13131310 switch (cfa) {
1314 .nop => try writer.writeByte(DW.CFA.nop),
1311 .nop => try bw.writeByte(DW.CFA.nop),
13151312 .advance_loc => |loc| {
13161313 const delta = @divExact(loc - wip_nav.cfi.loc, wip_nav.dwarf.debug_frame.header.code_alignment_factor);
13171314 if (delta == 0) {} else if (std.math.cast(u6, delta)) |small_delta|
1318 try writer.writeByte(@as(u8, DW.CFA.advance_loc) + small_delta)
1315 try bw.writeByte(@as(u8, DW.CFA.advance_loc) + small_delta)
13191316 else if (std.math.cast(u8, delta)) |ubyte_delta|
1320 try writer.writeAll(&.{ DW.CFA.advance_loc1, ubyte_delta })
1317 try bw.writeAll(&.{ DW.CFA.advance_loc1, ubyte_delta })
13211318 else if (std.math.cast(u16, delta)) |uhalf_delta| {
1322 try writer.writeByte(DW.CFA.advance_loc2);
1323 try writer.writeInt(u16, uhalf_delta, wip_nav.dwarf.endian);
1319 try bw.writeByte(DW.CFA.advance_loc2);
1320 try bw.writeInt(u16, uhalf_delta, wip_nav.dwarf.endian);
13241321 } else if (std.math.cast(u32, delta)) |uword_delta| {
1325 try writer.writeByte(DW.CFA.advance_loc4);
1326 try writer.writeInt(u32, uword_delta, wip_nav.dwarf.endian);
1322 try bw.writeByte(DW.CFA.advance_loc4);
1323 try bw.writeInt(u32, uword_delta, wip_nav.dwarf.endian);
13271324 }
13281325 wip_nav.cfi.loc = loc;
13291326 },
......@@ -1335,41 +1332,41 @@ pub const Cfa = union(enum) {
13351332 }, wip_nav.dwarf.debug_frame.header.data_alignment_factor);
13361333 if (std.math.cast(u63, factored_off)) |unsigned_off| {
13371334 if (std.math.cast(u6, reg_off.reg)) |small_reg| {
1338 try writer.writeByte(@as(u8, DW.CFA.offset) + small_reg);
1335 try bw.writeByte(@as(u8, DW.CFA.offset) + small_reg);
13391336 } else {
1340 try writer.writeByte(DW.CFA.offset_extended);
1341 try uleb128(writer, reg_off.reg);
1337 try bw.writeByte(DW.CFA.offset_extended);
1338 try bw.writeLeb128(reg_off.reg);
13421339 }
1343 try uleb128(writer, unsigned_off);
1340 try bw.writeLeb128(unsigned_off);
13441341 } else {
1345 try writer.writeByte(DW.CFA.offset_extended_sf);
1346 try uleb128(writer, reg_off.reg);
1347 try sleb128(writer, factored_off);
1342 try bw.writeByte(DW.CFA.offset_extended_sf);
1343 try bw.writeLeb128(reg_off.reg);
1344 try bw.writeLeb128(factored_off);
13481345 }
13491346 },
13501347 .restore => |reg| if (std.math.cast(u6, reg)) |small_reg|
1351 try writer.writeByte(@as(u8, DW.CFA.restore) + small_reg)
1348 try bw.writeByte(@as(u8, DW.CFA.restore) + small_reg)
13521349 else {
1353 try writer.writeByte(DW.CFA.restore_extended);
1354 try uleb128(writer, reg);
1350 try bw.writeByte(DW.CFA.restore_extended);
1351 try bw.writeLeb128(reg);
13551352 },
13561353 .undefined => |reg| {
1357 try writer.writeByte(DW.CFA.undefined);
1358 try uleb128(writer, reg);
1354 try bw.writeByte(DW.CFA.undefined);
1355 try bw.writeLeb128(reg);
13591356 },
13601357 .same_value => |reg| {
1361 try writer.writeByte(DW.CFA.same_value);
1362 try uleb128(writer, reg);
1358 try bw.writeByte(DW.CFA.same_value);
1359 try bw.writeLeb128(reg);
13631360 },
13641361 .register => |regs| if (regs[0] != regs[1]) {
1365 try writer.writeByte(DW.CFA.register);
1366 for (regs) |reg| try uleb128(writer, reg);
1362 try bw.writeByte(DW.CFA.register);
1363 for (regs) |reg| try bw.writeLeb128(reg);
13671364 } else {
1368 try writer.writeByte(DW.CFA.same_value);
1369 try uleb128(writer, regs[0]);
1365 try bw.writeByte(DW.CFA.same_value);
1366 try bw.writeLeb128(regs[0]);
13701367 },
1371 .remember_state => try writer.writeByte(DW.CFA.remember_state),
1372 .restore_state => try writer.writeByte(DW.CFA.restore_state),
1368 .remember_state => try bw.writeByte(DW.CFA.remember_state),
1369 .restore_state => try bw.writeByte(DW.CFA.restore_state),
13731370 .def_cfa, .def_cfa_register, .def_cfa_offset, .adjust_cfa_offset => {
13741371 const reg_off: RegOff = switch (cfa) {
13751372 else => unreachable,
......@@ -1382,51 +1379,51 @@ pub const Cfa = union(enum) {
13821379 const unsigned_off = std.math.cast(u63, reg_off.off);
13831380 if (reg_off.off == wip_nav.cfi.cfa.off) {
13841381 if (changed_reg) {
1385 try writer.writeByte(DW.CFA.def_cfa_register);
1386 try uleb128(writer, reg_off.reg);
1382 try bw.writeByte(DW.CFA.def_cfa_register);
1383 try bw.writeLeb128(reg_off.reg);
13871384 }
13881385 } else if (switch (wip_nav.dwarf.debug_frame.header.data_alignment_factor) {
13891386 0 => unreachable,
13901387 1 => unsigned_off != null,
13911388 else => |data_alignment_factor| @rem(reg_off.off, data_alignment_factor) != 0,
13921389 }) {
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.?);
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.?);
13961393 } else {
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));
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));
14001397 }
14011398 wip_nav.cfi.cfa = reg_off;
14021399 },
14031400 .def_cfa_expression => |expr| {
1404 try writer.writeByte(DW.CFA.def_cfa_expression);
1401 try bw.writeByte(DW.CFA.def_cfa_expression);
14051402 try wip_nav.frameExprLoc(expr);
14061403 },
14071404 .expression => |reg_expr| {
1408 try writer.writeByte(DW.CFA.expression);
1409 try uleb128(writer, reg_expr.reg);
1405 try bw.writeByte(DW.CFA.expression);
1406 try bw.writeLeb128(reg_expr.reg);
14101407 try wip_nav.frameExprLoc(reg_expr.expr);
14111408 },
14121409 .val_offset => |reg_off| {
14131410 const factored_off = @divExact(reg_off.off, wip_nav.dwarf.debug_frame.header.data_alignment_factor);
14141411 if (std.math.cast(u63, factored_off)) |unsigned_off| {
1415 try writer.writeByte(DW.CFA.val_offset);
1416 try uleb128(writer, reg_off.reg);
1417 try uleb128(writer, unsigned_off);
1412 try bw.writeByte(DW.CFA.val_offset);
1413 try bw.writeLeb128(reg_off.reg);
1414 try bw.writeLeb128(unsigned_off);
14181415 } else {
1419 try writer.writeByte(DW.CFA.val_offset_sf);
1420 try uleb128(writer, reg_off.reg);
1421 try sleb128(writer, factored_off);
1416 try bw.writeByte(DW.CFA.val_offset_sf);
1417 try bw.writeLeb128(reg_off.reg);
1418 try bw.writeLeb128(factored_off);
14221419 }
14231420 },
14241421 .val_expression => |reg_expr| {
1425 try writer.writeByte(DW.CFA.val_expression);
1426 try uleb128(writer, reg_expr.reg);
1422 try bw.writeByte(DW.CFA.val_expression);
1423 try bw.writeLeb128(reg_expr.reg);
14271424 try wip_nav.frameExprLoc(reg_expr.expr);
14281425 },
1429 .escape => |bytes| try writer.writeAll(bytes),
1426 .escape => |bytes| try bw.writeAll(bytes),
14301427 }
14311428 }
14321429};
......@@ -1449,19 +1446,27 @@ pub const WipNav = struct {
14491446 loc: u32,
14501447 cfa: Cfa.RegOff,
14511448 },
1452 debug_frame: std.ArrayListUnmanaged(u8),
1453 debug_info: std.ArrayListUnmanaged(u8),
1454 debug_line: std.ArrayListUnmanaged(u8),
1455 debug_loclists: std.ArrayListUnmanaged(u8),
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,
14561453 pending_lazy: PendingLazy,
14571454
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
14581463 pub fn deinit(wip_nav: *WipNav) void {
14591464 const gpa = wip_nav.dwarf.gpa;
14601465 if (wip_nav.func != .none) wip_nav.blocks.deinit(gpa);
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);
1466 wip_nav.debug_frame.deinit();
1467 wip_nav.debug_info.deinit();
1468 wip_nav.debug_line.deinit();
1469 wip_nav.debug_loclists.deinit();
14651470 wip_nav.pending_lazy.types.deinit(gpa);
14661471 wip_nav.pending_lazy.values.deinit(gpa);
14671472 }
......@@ -1538,7 +1543,7 @@ pub const WipNav = struct {
15381543 delta_line: i33,
15391544 delta_pc: u64,
15401545 ) error{OutOfMemory}!void {
1541 const dlw = wip_nav.debug_line.writer(wip_nav.dwarf.gpa);
1546 const dlbw = &wip_nav.debug_line.buffered_writer;
15421547
15431548 const header = wip_nav.dwarf.debug_line.header;
15441549 assert(header.maximum_operations_per_instruction == 1);
......@@ -1548,8 +1553,8 @@ pub const WipNav = struct {
15481553 delta_line - header.line_base >= header.line_range)
15491554 remaining: {
15501555 assert(delta_line != 0);
1551 try dlw.writeByte(DW.LNS.advance_line);
1552 try sleb128(dlw, delta_line);
1556 try dlbw.writeByte(DW.LNS.advance_line);
1557 try dlbw.writeLeb128(delta_line);
15531558 break :remaining 0;
15541559 } else delta_line);
15551560
......@@ -1557,68 +1562,68 @@ pub const WipNav = struct {
15571562 header.maximum_operations_per_instruction + delta_op;
15581563 const max_op_advance: u9 = (std.math.maxInt(u8) - header.opcode_base) / header.line_range;
15591564 const remaining_op_advance: u8 = @intCast(if (op_advance >= 2 * max_op_advance) remaining: {
1560 try dlw.writeByte(DW.LNS.advance_pc);
1561 try uleb128(dlw, op_advance);
1565 try dlbw.writeByte(DW.LNS.advance_pc);
1566 try dlbw.writeLeb128(op_advance);
15621567 break :remaining 0;
15631568 } else if (op_advance >= max_op_advance) remaining: {
1564 try dlw.writeByte(DW.LNS.const_add_pc);
1569 try dlbw.writeByte(DW.LNS.const_add_pc);
15651570 break :remaining op_advance - max_op_advance;
15661571 } else op_advance);
15671572
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));
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 );
15731580 }
15741581
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);
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);
15791586 }
15801587
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);
1588 pub fn negateStmt(wip_nav: *WipNav) Allocator.Error!void {
1589 return wip_nav.debug_line.buffered_writer.writeByte(DW.LNS.negate_stmt);
15841590 }
15851591
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);
1592 pub fn setPrologueEnd(wip_nav: *WipNav) Allocator.Error!void {
1593 return wip_nav.debug_line.buffered_writer.writeByte(DW.LNS.set_prologue_end);
15891594 }
15901595
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);
1596 pub fn setEpilogueBegin(wip_nav: *WipNav) Allocator.Error!void {
1597 return wip_nav.debug_line.buffered_writer.writeByte(DW.LNS.set_epilogue_begin);
15941598 }
15951599
15961600 pub fn enterBlock(wip_nav: *WipNav, code_off: u64) UpdateError!void {
15971601 const dwarf = wip_nav.dwarf;
1598 const diw = wip_nav.debug_info.writer(dwarf.gpa);
1602 const dibw = &wip_nav.debug_info.buffered_writer;
15991603 const block = try wip_nav.blocks.addOne(dwarf.gpa);
16001604
1601 block.abbrev_code = @intCast(wip_nav.debug_info.items.len);
1605 block.abbrev_code = @intCast(dibw.count);
16021606 try wip_nav.abbrevCode(.block);
16031607 block.low_pc_off = code_off;
16041608 try wip_nav.infoAddrSym(wip_nav.func_sym_index, code_off);
1605 block.high_pc = @intCast(wip_nav.debug_info.items.len);
1606 try diw.writeInt(u32, 0, dwarf.endian);
1609 block.high_pc = @intCast(dibw.count);
1610 try dibw.writeInt(u32, 0, dwarf.endian);
16071611 wip_nav.any_children = false;
16081612 }
16091613
16101614 pub fn leaveBlock(wip_nav: *WipNav, code_off: u64) UpdateError!void {
16111615 const block_bytes = comptime uleb128Bytes(@intFromEnum(AbbrevCode.block));
16121616 const block = wip_nav.blocks.pop().?;
1617 const dib = wip_nav.debug_info.getWritten();
16131618 if (wip_nav.any_children)
1614 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), @intFromEnum(AbbrevCode.null))
1619 try wip_nav.debug_info.buffered_writer.writeLeb128(@intFromEnum(AbbrevCode.null))
16151620 else
16161621 std.leb.writeUnsignedFixed(
16171622 block_bytes,
1618 wip_nav.debug_info.items[block.abbrev_code..][0..block_bytes],
1623 dib[block.abbrev_code..][0..block_bytes],
16191624 try wip_nav.dwarf.refAbbrevCode(.empty_block),
16201625 );
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);
1626 std.mem.writeInt(u32, dib[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
16221627 wip_nav.any_children = true;
16231628 }
16241629
......@@ -1631,18 +1636,18 @@ pub const WipNav = struct {
16311636 ) UpdateError!void {
16321637 const dwarf = wip_nav.dwarf;
16331638 const zcu = wip_nav.pt.zcu;
1634 const diw = wip_nav.debug_info.writer(dwarf.gpa);
1639 const dibw = &wip_nav.debug_info.buffered_writer;
16351640 const block = try wip_nav.blocks.addOne(dwarf.gpa);
16361641
1637 block.abbrev_code = @intCast(wip_nav.debug_info.items.len);
1642 block.abbrev_code = @intCast(dibw.count);
16381643 try wip_nav.abbrevCode(.inlined_func);
16391644 try wip_nav.refNav(zcu.funcInfo(func).owner_nav);
1640 try uleb128(diw, zcu.navSrcLine(zcu.funcInfo(wip_nav.func).owner_nav) + line + 1);
1641 try uleb128(diw, column + 1);
1645 try dibw.writeLeb128(zcu.navSrcLine(zcu.funcInfo(wip_nav.func).owner_nav) + line + 1);
1646 try dibw.writeLeb128(column + 1);
16421647 block.low_pc_off = code_off;
16431648 try wip_nav.infoAddrSym(wip_nav.func_sym_index, code_off);
1644 block.high_pc = @intCast(wip_nav.debug_info.items.len);
1645 try diw.writeInt(u32, 0, dwarf.endian);
1649 block.high_pc = @intCast(dibw.count);
1650 try dibw.writeInt(u32, 0, dwarf.endian);
16461651 try wip_nav.setInlineFunc(func);
16471652 wip_nav.any_children = false;
16481653 }
......@@ -1650,15 +1655,16 @@ pub const WipNav = struct {
16501655 pub fn leaveInlineFunc(wip_nav: *WipNav, func: InternPool.Index, code_off: u64) UpdateError!void {
16511656 const inlined_func_bytes = comptime uleb128Bytes(@intFromEnum(AbbrevCode.inlined_func));
16521657 const block = wip_nav.blocks.pop().?;
1658 const dib = wip_nav.debug_info.getWritten();
16531659 if (wip_nav.any_children)
1654 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), @intFromEnum(AbbrevCode.null))
1660 try wip_nav.debug_info.buffered_writer.writeLeb128(@intFromEnum(AbbrevCode.null))
16551661 else
16561662 std.leb.writeUnsignedFixed(
16571663 inlined_func_bytes,
1658 wip_nav.debug_info.items[block.abbrev_code..][0..inlined_func_bytes],
1664 dib[block.abbrev_code..][0..inlined_func_bytes],
16591665 try wip_nav.dwarf.refAbbrevCode(.empty_inlined_func),
16601666 );
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);
1667 std.mem.writeInt(u32, dib[block.high_pc..][0..4], @intCast(code_off - block.low_pc_off), wip_nav.dwarf.endian);
16621668 try wip_nav.setInlineFunc(func);
16631669 wip_nav.any_children = true;
16641670 }
......@@ -1672,22 +1678,22 @@ pub const WipNav = struct {
16721678 const new_file = zcu.navFileScopeIndex(new_func_info.owner_nav);
16731679 const new_unit = try dwarf.getUnit(zcu.fileByIndex(new_file).mod.?);
16741680
1675 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
1681 const dlbw = &wip_nav.debug_line.buffered_writer;
16761682 if (dwarf.incremental()) {
16771683 const new_nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, new_func_info.owner_nav);
16781684 errdefer _ = if (!new_nav_gop.found_existing) dwarf.navs.pop();
16791685 if (!new_nav_gop.found_existing) new_nav_gop.value_ptr.* = try dwarf.addCommonEntry(new_unit);
16801686
1681 try dlw.writeByte(DW.LNS.extended_op);
1682 try uleb128(dlw, 1 + dwarf.sectionOffsetBytes());
1683 try dlw.writeByte(DW.LNE.ZIG_set_decl);
1687 try dlbw.writeByte(DW.LNS.extended_op);
1688 try dlbw.writeLeb128(1 + dwarf.sectionOffsetBytes());
1689 try dlbw.writeByte(DW.LNE.ZIG_set_decl);
16841690 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{
1685 .source_off = @intCast(wip_nav.debug_line.items.len),
1691 .source_off = @intCast(dlbw.count),
16861692 .target_sec = .debug_info,
16871693 .target_unit = new_unit,
16881694 .target_entry = new_nav_gop.value_ptr.toOptional(),
16891695 });
1690 try dlw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
1696 try dlbw.splatByteAll(0, dwarf.sectionOffsetBytes());
16911697 return;
16921698 }
16931699
......@@ -1698,42 +1704,49 @@ pub const WipNav = struct {
16981704 try mod_info.dirs.put(dwarf.gpa, new_unit, {});
16991705 const file_gop = try mod_info.files.getOrPut(dwarf.gpa, new_file);
17001706
1701 try dlw.writeByte(DW.LNS.set_file);
1702 try uleb128(dlw, file_gop.index);
1707 try dlbw.writeByte(DW.LNS.set_file);
1708 try dlbw.writeLeb128(file_gop.index);
17031709 }
17041710
17051711 const old_src_line: i33 = zcu.navSrcLine(old_func_info.owner_nav);
17061712 const new_src_line: i33 = zcu.navSrcLine(new_func_info.owner_nav);
17071713 if (new_src_line != old_src_line) {
1708 try dlw.writeByte(DW.LNS.advance_line);
1709 try sleb128(dlw, new_src_line - old_src_line);
1714 try dlbw.writeByte(DW.LNS.advance_line);
1715 try dlbw.writeLeb128(new_src_line - old_src_line);
17101716 }
17111717
17121718 wip_nav.func = func;
17131719 }
17141720
1715 fn externalReloc(wip_nav: *WipNav, sec: *Section, reloc: ExternalReloc) std.mem.Allocator.Error!void {
1721 fn externalReloc(wip_nav: *WipNav, sec: *Section, reloc: ExternalReloc) Allocator.Error!void {
17161722 try sec.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(wip_nav.dwarf.gpa, reloc);
17171723 }
17181724
1719 pub fn infoExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {
1725 pub fn infoExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) Allocator.Error!void {
17201726 try wip_nav.externalReloc(&wip_nav.dwarf.debug_info.section, reloc);
17211727 }
17221728
1723 fn frameExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) std.mem.Allocator.Error!void {
1729 fn frameExternalReloc(wip_nav: *WipNav, reloc: ExternalReloc) Allocator.Error!void {
17241730 try wip_nav.externalReloc(&wip_nav.dwarf.debug_frame.section, reloc);
17251731 }
17261732
17271733 fn abbrevCode(wip_nav: *WipNav, abbrev_code: AbbrevCode) UpdateError!void {
1728 try uleb128(wip_nav.debug_info.writer(wip_nav.dwarf.gpa), try wip_nav.dwarf.refAbbrevCode(abbrev_code));
1734 try wip_nav.debug_info.buffered_writer.writeLeb128(try wip_nav.dwarf.refAbbrevCode(abbrev_code));
17291735 }
17301736
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 {
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 {
17321745 const dwarf = wip_nav.dwarf;
17331746 const gpa = dwarf.gpa;
17341747 const entry_ptr = @field(dwarf, @tagName(sec)).section.getUnit(wip_nav.unit).getEntry(wip_nav.entry);
1735 const bytes = &@field(wip_nav, @tagName(sec));
1736 const source_off: u32 = @intCast(bytes.items.len);
1748 const bw = &@field(wip_nav, @tagName(sec)).buffered_writer;
1749 const source_off: u32 = @intCast(bw.count);
17371750 if (target_sec != sec) {
17381751 try entry_ptr.cross_section_relocs.append(gpa, .{
17391752 .source_off = source_off,
......@@ -1756,7 +1769,7 @@ pub const WipNav = struct {
17561769 .target_off = target_off,
17571770 });
17581771 }
1759 try bytes.appendNTimes(gpa, 0, dwarf.sectionOffsetBytes());
1772 try bw.splatByteAll(0, dwarf.sectionOffsetBytes());
17601773 }
17611774
17621775 fn infoSectionOffset(wip_nav: *WipNav, target_sec: Section.Index, target_unit: Unit.Index, target_entry: Entry.Index, target_off: u32) UpdateError!void {
......@@ -1768,93 +1781,93 @@ pub const WipNav = struct {
17681781 }
17691782
17701783 const ExprLocCounter = struct {
1771 const Stream = std.io.CountingWriter(std.io.NullWriter);
1772 stream: Stream,
17731784 section_offset_bytes: u32,
17741785 address_size: AddressSize,
17751786 fn init(dwarf: *Dwarf) ExprLocCounter {
17761787 return .{
1777 .stream = std.io.countingWriter(std.io.null_writer),
17781788 .section_offset_bytes = dwarf.sectionOffsetBytes(),
17791789 .address_size = dwarf.address_size,
17801790 };
17811791 }
1782 fn writer(counter: *ExprLocCounter) Stream.Writer {
1783 return counter.stream.writer();
1784 }
17851792 fn endian(_: ExprLocCounter) std.builtin.Endian {
17861793 return @import("builtin").cpu.arch.endian();
17871794 }
1788 fn addrSym(counter: *ExprLocCounter, _: u32) error{}!void {
1789 counter.stream.bytes_written += @intFromEnum(counter.address_size);
1795 fn addrSym(counter: ExprLocCounter, bw: *Writer, _: u32) error{}!void {
1796 bw.count += @intFromEnum(counter.address_size);
17901797 }
1791 fn infoEntry(counter: *ExprLocCounter, _: Unit.Index, _: Entry.Index) error{}!void {
1792 counter.stream.bytes_written += counter.section_offset_bytes;
1798 fn infoEntry(counter: ExprLocCounter, bw: *Writer, _: Unit.Index, _: Entry.Index) error{}!void {
1799 bw.count += counter.section_offset_bytes;
17931800 }
17941801 };
17951802
17961803 fn infoExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1797 var counter: ExprLocCounter = .init(wip_nav.dwarf);
1798 try loc.write(&counter);
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);
17991811
18001812 const adapter: struct {
18011813 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 }
18051814 fn endian(ctx: @This()) std.builtin.Endian {
18061815 return ctx.wip_nav.dwarf.endian;
18071816 }
1808 fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
1817 fn addrSym(ctx: @This(), _: *Writer, sym_index: u32) UpdateError!void {
18091818 try ctx.wip_nav.infoAddrSym(sym_index, 0);
18101819 }
1811 fn infoEntry(ctx: @This(), unit: Unit.Index, entry: Entry.Index) UpdateError!void {
1820 fn infoEntry(ctx: @This(), _: *Writer, unit: Unit.Index, entry: Entry.Index) UpdateError!void {
18121821 try ctx.wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
18131822 }
18141823 } = .{ .wip_nav = wip_nav };
1815 try uleb128(adapter.writer(), counter.stream.bytes_written);
1816 try loc.write(adapter);
1824 try bw.writeLeb128(len);
1825 try loc.write(bw, adapter);
18171826 }
18181827
18191828 fn infoAddrSym(wip_nav: *WipNav, sym_index: u32, sym_off: u64) UpdateError!void {
1829 const dibw = &wip_nav.debug_info.buffered_writer;
18201830 try wip_nav.infoExternalReloc(.{
1821 .source_off = @intCast(wip_nav.debug_info.items.len),
1831 .source_off = @intCast(dibw.count),
18221832 .target_sym = sym_index,
18231833 .target_off = sym_off,
18241834 });
1825 try wip_nav.debug_info.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
1835 try dibw.splatByteAll(0, @intFromEnum(wip_nav.dwarf.address_size));
18261836 }
18271837
18281838 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) UpdateError!void {
1829 var counter: ExprLocCounter = .init(wip_nav.dwarf);
1830 try loc.write(&counter);
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);
18311846
18321847 const adapter: struct {
18331848 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 }
18371849 fn endian(ctx: @This()) std.builtin.Endian {
18381850 return ctx.wip_nav.dwarf.endian;
18391851 }
1840 fn addrSym(ctx: @This(), sym_index: u32) UpdateError!void {
1852 fn addrSym(ctx: @This(), _: *Writer, sym_index: u32) UpdateError!void {
18411853 try ctx.wip_nav.frameAddrSym(sym_index, 0);
18421854 }
1843 fn infoEntry(ctx: @This(), unit: Unit.Index, entry: Entry.Index) UpdateError!void {
1855 fn infoEntry(ctx: @This(), _: *Writer, unit: Unit.Index, entry: Entry.Index) UpdateError!void {
18441856 try ctx.wip_nav.sectionOffset(.debug_frame, .debug_info, unit, entry, 0);
18451857 }
18461858 } = .{ .wip_nav = wip_nav };
1847 try uleb128(adapter.writer(), counter.stream.bytes_written);
1848 try loc.write(adapter);
1859 try bw.writeLeb128(len);
1860 try loc.write(bw, adapter);
18491861 }
18501862
18511863 fn frameAddrSym(wip_nav: *WipNav, sym_index: u32, sym_off: u64) UpdateError!void {
1864 const dfbw = &wip_nav.debug_frame.buffered_writer;
18521865 try wip_nav.frameExternalReloc(.{
1853 .source_off = @intCast(wip_nav.debug_frame.items.len),
1866 .source_off = @intCast(dfbw.count),
18541867 .target_sym = sym_index,
18551868 .target_off = sym_off,
18561869 });
1857 try wip_nav.debug_frame.appendNTimes(wip_nav.dwarf.gpa, 0, @intFromEnum(wip_nav.dwarf.address_size));
1870 try dfbw.splatByteAll(0, @intFromEnum(wip_nav.dwarf.address_size));
18581871 }
18591872
18601873 fn getNavEntry(wip_nav: *WipNav, nav_index: InternPool.Nav.Index) UpdateError!struct { Unit.Index, Entry.Index } {
......@@ -1922,38 +1935,41 @@ pub const WipNav = struct {
19221935 try wip_nav.infoSectionOffset(.debug_info, unit, entry, 0);
19231936 }
19241937
1925 fn refForward(wip_nav: *WipNav) std.mem.Allocator.Error!u32 {
1938 fn refForward(wip_nav: *WipNav) Allocator.Error!u32 {
19261939 const dwarf = wip_nav.dwarf;
1940 const dibw = &wip_nav.debug_info.buffered_writer;
19271941 const cross_entry_relocs = &dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs;
19281942 const reloc_index: u32 = @intCast(cross_entry_relocs.items.len);
19291943 try cross_entry_relocs.append(dwarf.gpa, .{
1930 .source_off = @intCast(wip_nav.debug_info.items.len),
1944 .source_off = @intCast(dibw.count),
19311945 .target_entry = undefined,
19321946 .target_off = undefined,
19331947 });
1934 try wip_nav.debug_info.appendNTimes(dwarf.gpa, 0, dwarf.sectionOffsetBytes());
1948 try dibw.splatByteAll(0, dwarf.sectionOffsetBytes());
19351949 return reloc_index;
19361950 }
19371951
19381952 fn finishForward(wip_nav: *WipNav, reloc_index: u32) void {
19391953 const reloc = &wip_nav.dwarf.debug_info.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_entry_relocs.items[reloc_index];
19401954 reloc.target_entry = wip_nav.entry.toOptional();
1941 reloc.target_off = @intCast(wip_nav.debug_info.items.len);
1955 reloc.target_off = @intCast(wip_nav.debug_info.buffered_writer.count);
19421956 }
19431957
19441958 fn blockValue(wip_nav: *WipNav, src_loc: Zcu.LazySrcLoc, val: Value) UpdateError!void {
19451959 const ty = val.typeOf(wip_nav.pt.zcu);
1946 const diw = wip_nav.debug_info.writer(wip_nav.dwarf.gpa);
1960 const dibw = &wip_nav.debug_info.buffered_writer;
19471961 const bytes = if (ty.hasRuntimeBits(wip_nav.pt.zcu)) ty.abiSize(wip_nav.pt.zcu) else 0;
1948 try uleb128(diw, bytes);
1962 try dibw.writeLeb128(bytes);
19491963 if (bytes == 0) return;
1950 const old_len = wip_nav.debug_info.items.len;
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;
19511967 try codegen.generateSymbol(
19521968 wip_nav.dwarf.bin_file,
19531969 wip_nav.pt,
19541970 src_loc,
19551971 val,
1956 &wip_nav.debug_info,
1972 &dial,
19571973 .{ .debug_output = .{ .dwarf = wip_nav } },
19581974 );
19591975 if (old_len + bytes != wip_nav.debug_info.items.len) {
......@@ -1975,7 +1991,7 @@ pub const WipNav = struct {
19751991 big_int: std.math.big.int.Const,
19761992 ) UpdateError!void {
19771993 const zcu = wip_nav.pt.zcu;
1978 const diw = wip_nav.debug_info.writer(wip_nav.dwarf.gpa);
1994 const dibw = &wip_nav.debug_info.buffered_writer;
19791995 const signedness = switch (ty.toIntern()) {
19801996 .comptime_int_type, .comptime_float_type => .signed,
19811997 else => ty.intInfo(zcu).signedness,
......@@ -1986,7 +2002,7 @@ pub const WipNav = struct {
19862002 .signed => abbrev_code.sdata,
19872003 .unsigned => abbrev_code.udata,
19882004 });
1989 try wip_nav.debug_info.ensureUnusedCapacity(wip_nav.dwarf.gpa, std.math.divCeil(usize, bits, 7) catch unreachable);
2005 _ = try dibw.writableSliceGreedy(std.math.divCeil(usize, bits, 7) catch unreachable);
19902006 var bit: usize = 0;
19912007 var carry: u1 = 1;
19922008 while (bit < bits) {
......@@ -2003,16 +2019,17 @@ pub const WipNav = struct {
20032019 break :twos_comp_part twos_comp_part;
20042020 };
20052021 bit += 7;
2006 wip_nav.debug_info.appendAssumeCapacity(@as(u8, if (bit < bits) 0x80 else 0x00) | twos_comp_part);
2022 dibw.writeByte(@as(u8, if (bit < bits) 0x80 else 0x00) | twos_comp_part) catch unreachable;
20072023 }
20082024 } else {
20092025 try wip_nav.abbrevCode(abbrev_code.block);
20102026 const bytes = @max(ty.abiSize(zcu), std.math.divCeil(usize, bits, 8) catch unreachable);
2011 try uleb128(diw, bytes);
2027 try dibw.writeLeb128(bytes);
20122028 big_int.writeTwosComplement(
2013 try wip_nav.debug_info.addManyAsSlice(wip_nav.dwarf.gpa, @intCast(bytes)),
2029 try dibw.writableSliceGreedy(@intCast(bytes)),
20142030 wip_nav.dwarf.endian,
20152031 );
2032 dibw.advance(@intCast(bytes));
20162033 }
20172034 }
20182035
......@@ -2045,7 +2062,7 @@ pub const WipNav = struct {
20452062 const zcu = wip_nav.pt.zcu;
20462063 const ip = &zcu.intern_pool;
20472064 const dwarf = wip_nav.dwarf;
2048 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2065 const dibw = &wip_nav.debug_info.buffered_writer;
20492066
20502067 const orig_entry = wip_nav.entry;
20512068 defer wip_nav.entry = orig_entry;
......@@ -2096,15 +2113,15 @@ pub const WipNav = struct {
20962113 try wip_nav.abbrevCode(if (is_generic_decl) abbrev_code.generic_decl else abbrev_code.decl);
20972114 try wip_nav.refType((if (is_generic_decl) null else parent_type) orelse
20982115 .fromInterned(zcu.fileRootType(file)));
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);
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);
21032120 try wip_nav.strp(nav.name.toSlice(ip));
21042121
21052122 if (!is_generic_decl) return;
21062123 const generic_decl_entry = wip_nav.entry;
2107 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, generic_decl_entry, dwarf, wip_nav.debug_info.items);
2124 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, generic_decl_entry, dwarf, wip_nav.debug_info.getWritten());
21082125 wip_nav.debug_info.clearRetainingCapacity();
21092126 wip_nav.entry = orig_entry;
21102127 try wip_nav.abbrevCode(abbrev_code.decl_instance);
......@@ -2408,12 +2425,63 @@ pub fn initWipNav(
24082425 nav_index: InternPool.Nav.Index,
24092426 sym_index: u32,
24102427) error{ OutOfMemory, CodegenFail }!?WipNav {
2411 return initWipNavInner(dwarf, pt, nav_index, sym_index) catch |err| switch (err) {
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) {
24122476 error.OutOfMemory => return error.OutOfMemory,
2413 else => |e| return pt.zcu.codegenFail(nav_index, "failed to init dwarf: {s}", .{@errorName(e)}),
2477 else => |e| return pt.zcu.codegenFailType(type_index, "failed to update dwarf comptime nav: {s}", .{@errorName(e)}),
24142478 };
24152479}
24162480
2481pub fn flushModule(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
2482 return dwarf.flushModuleInner(pt);
2483}
2484
24172485fn initWipNavInner(
24182486 dwarf: *Dwarf,
24192487 pt: Zcu.PerThread,
......@@ -2468,17 +2536,17 @@ fn initWipNavInner(
24682536 .func_high_pc = undefined,
24692537 .blocks = undefined,
24702538 .cfi = undefined,
2471 .debug_frame = .empty,
2472 .debug_info = .empty,
2473 .debug_line = .empty,
2474 .debug_loclists = .empty,
2539 .debug_frame = undefined,
2540 .debug_info = undefined,
2541 .debug_line = undefined,
2542 .debug_loclists = undefined,
24752543 .pending_lazy = .empty,
24762544 };
24772545 errdefer wip_nav.deinit();
24782546
24792547 switch (nav_key) {
24802548 else => {
2481 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2549 const dibw = &wip_nav.debug_info.buffered_writer;
24822550 try wip_nav.declCommon(.{
24832551 .decl = .decl_var,
24842552 .generic_decl = .generic_decl_var,
......@@ -2499,9 +2567,9 @@ fn initWipNavInner(
24992567 .@"const" => {
25002568 const const_ty_reloc_index = try wip_nav.refForward();
25012569 try wip_nav.infoExprLoc(loc);
2502 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
2570 try dibw.writeLeb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
25032571 ty.abiAlignment(zcu).toByteUnits().?);
2504 try diw.writeByte(@intFromBool(decl.linkage != .normal));
2572 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
25052573 wip_nav.finishForward(const_ty_reloc_index);
25062574 try wip_nav.abbrevCode(.is_const);
25072575 try wip_nav.refType(ty);
......@@ -2509,9 +2577,9 @@ fn initWipNavInner(
25092577 .@"var" => {
25102578 try wip_nav.refType(ty);
25112579 try wip_nav.infoExprLoc(loc);
2512 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
2580 try dibw.writeLeb128(dibw, nav.status.fully_resolved.alignment.toByteUnits() orelse
25132581 ty.abiAlignment(zcu).toByteUnits().?);
2514 try diw.writeByte(@intFromBool(decl.linkage != .normal));
2582 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
25152583 },
25162584 }
25172585 },
......@@ -2536,39 +2604,39 @@ fn initWipNavInner(
25362604 .none => {},
25372605 .debug_frame, .eh_frame => |format| {
25382606 const entry = dwarf.debug_frame.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry);
2539 const dfw = wip_nav.debug_frame.writer(dwarf.gpa);
2607 const dfbw = &wip_nav.debug_frame.buffered_writer;
25402608 switch (dwarf.format) {
2541 .@"32" => try dfw.writeInt(u32, undefined, dwarf.endian),
2609 .@"32" => try dfbw.writeInt(u32, undefined, dwarf.endian),
25422610 .@"64" => {
2543 try dfw.writeInt(u32, std.math.maxInt(u32), dwarf.endian);
2544 try dfw.writeInt(u64, undefined, dwarf.endian);
2611 try dfbw.writeInt(u32, std.math.maxInt(u32), dwarf.endian);
2612 try dfbw.writeInt(u64, undefined, dwarf.endian);
25452613 },
25462614 }
25472615 switch (format) {
25482616 .none => unreachable,
25492617 .debug_frame => {
25502618 try entry.cross_entry_relocs.append(dwarf.gpa, .{
2551 .source_off = @intCast(wip_nav.debug_frame.items.len),
2619 .source_off = @intCast(dfbw.count),
25522620 });
2553 try dfw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
2621 try dfbw.splatByteAll(0, dwarf.sectionOffsetBytes());
25542622 try wip_nav.frameAddrSym(sym_index, 0);
2555 try dfw.writeByteNTimes(undefined, @intFromEnum(dwarf.address_size));
2623 try dfbw.splatByteAll(undefined, @intFromEnum(dwarf.address_size));
25562624 },
25572625 .eh_frame => {
2558 try dfw.writeInt(u32, undefined, dwarf.endian);
2626 try dfbw.writeInt(u32, undefined, dwarf.endian);
25592627 try wip_nav.frameExternalReloc(.{
2560 .source_off = @intCast(wip_nav.debug_frame.items.len),
2628 .source_off = @intCast(dfbw.count),
25612629 .target_sym = sym_index,
25622630 });
2563 try dfw.writeInt(u32, 0, dwarf.endian);
2564 try dfw.writeInt(u32, undefined, dwarf.endian);
2565 try uleb128(dfw, 0);
2631 try dfbw.writeInt(u32, 0, dwarf.endian);
2632 try dfbw.writeInt(u32, undefined, dwarf.endian);
2633 try dfbw.writeUleb128(0);
25662634 },
25672635 }
25682636 },
25692637 }
25702638
2571 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2639 const dibw = &wip_nav.debug_info.buffered_writer;
25722640 try wip_nav.declCommon(.{
25732641 .decl = .decl_func,
25742642 .generic_decl = .generic_decl_func,
......@@ -2578,47 +2646,47 @@ fn initWipNavInner(
25782646 try wip_nav.refType(.fromInterned(func_type.return_type));
25792647 try wip_nav.infoAddrSym(sym_index, 0);
25802648 wip_nav.func_high_pc = @intCast(wip_nav.debug_info.items.len);
2581 try diw.writeInt(u32, 0, dwarf.endian);
2649 try dibw.writeInt(u32, 0, dwarf.endian);
25822650 const target = &mod.resolved_target.result;
2583 try uleb128(diw, switch (nav.status.fully_resolved.alignment) {
2651 try dibw.writeLeb128(switch (nav.status.fully_resolved.alignment) {
25842652 .none => target_info.defaultFunctionAlignment(target),
25852653 else => |a| a.maxStrict(target_info.minFunctionAlignment(target)),
25862654 }.toByteUnits().?);
2587 try diw.writeByte(@intFromBool(decl.linkage != .normal));
2588 try diw.writeByte(@intFromBool(func_type.return_type == .noreturn_type));
2655 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
2656 try dibw.writeByte(@intFromBool(func_type.return_type == .noreturn_type));
25892657
2590 const dlw = wip_nav.debug_line.writer(dwarf.gpa);
2591 try dlw.writeByte(DW.LNS.extended_op);
2658 const dlbw = &wip_nav.debug_line.buffered_writer;
2659 try dlbw.writeByte(DW.LNS.extended_op);
25922660 if (dwarf.incremental()) {
2593 try uleb128(dlw, 1 + dwarf.sectionOffsetBytes());
2594 try dlw.writeByte(DW.LNE.ZIG_set_decl);
2661 try dlbw.writeLeb128(1 + dwarf.sectionOffsetBytes());
2662 try dlbw.writeByte(DW.LNE.ZIG_set_decl);
25952663 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).cross_section_relocs.append(dwarf.gpa, .{
2596 .source_off = @intCast(wip_nav.debug_line.items.len),
2664 .source_off = @intCast(dlbw.count),
25972665 .target_sec = .debug_info,
25982666 .target_unit = wip_nav.unit,
25992667 .target_entry = wip_nav.entry.toOptional(),
26002668 });
2601 try dlw.writeByteNTimes(0, dwarf.sectionOffsetBytes());
2669 try dlbw.splatByteAll(0, dwarf.sectionOffsetBytes());
26022670
2603 try dlw.writeByte(DW.LNS.set_column);
2604 try uleb128(dlw, func.lbrace_column + 1);
2671 try dlbw.writeByte(DW.LNS.set_column);
2672 try dlbw.writeLeb128(func.lbrace_column + 1);
26052673
26062674 try wip_nav.advancePCAndLine(func.lbrace_line, 0);
26072675 } else {
2608 try uleb128(dlw, 1 + @intFromEnum(dwarf.address_size));
2609 try dlw.writeByte(DW.LNE.set_address);
2676 try dlbw.writeLeb128(1 + @intFromEnum(dwarf.address_size));
2677 try dlbw.writeByte(DW.LNE.set_address);
26102678 try dwarf.debug_line.section.getUnit(wip_nav.unit).getEntry(wip_nav.entry).external_relocs.append(dwarf.gpa, .{
2611 .source_off = @intCast(wip_nav.debug_line.items.len),
2679 .source_off = @intCast(dlbw.count),
26122680 .target_sym = sym_index,
26132681 });
2614 try dlw.writeByteNTimes(0, @intFromEnum(dwarf.address_size));
2682 try dlbw.splatByteAll(0, @intFromEnum(dwarf.address_size));
26152683
26162684 const file_gop = try dwarf.getModInfo(unit).files.getOrPut(dwarf.gpa, inst_info.file);
2617 try dlw.writeByte(DW.LNS.set_file);
2618 try uleb128(dlw, file_gop.index);
2685 try dlbw.writeByte(DW.LNS.set_file);
2686 try dlbw.writeLeb128(file_gop.index);
26192687
2620 try dlw.writeByte(DW.LNS.set_column);
2621 try uleb128(dlw, func.lbrace_column + 1);
2688 try dlbw.writeByte(DW.LNS.set_column);
2689 try dlbw.writeLeb128(func.lbrace_column + 1);
26222690
26232691 try wip_nav.advancePCAndLine(@intCast(decl.src_line + func.lbrace_line), 0);
26242692 }
......@@ -2627,7 +2695,7 @@ fn initWipNavInner(
26272695 return wip_nav;
26282696}
26292697
2630pub fn finishWipNavFunc(
2698fn finishWipNavFuncInner(
26312699 dwarf: *Dwarf,
26322700 pt: Zcu.PerThread,
26332701 nav_index: InternPool.Nav.Index,
......@@ -2656,12 +2724,9 @@ pub fn finishWipNavFunc(
26562724 switch (dwarf.debug_frame.header.format) {
26572725 .none => {},
26582726 .debug_frame, .eh_frame => |format| {
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;
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();
26652730 try dwarf.debug_frame.section.resizeEntry(wip_nav.unit, wip_nav.entry, dwarf, @intCast(contents.len));
26662731 const unit = dwarf.debug_frame.section.getUnit(wip_nav.unit);
26672732 const entry = unit.getEntry(wip_nav.entry);
......@@ -2688,14 +2753,14 @@ pub fn finishWipNavFunc(
26882753 },
26892754 }
26902755 {
2691 std.mem.writeInt(u32, wip_nav.debug_info.items[wip_nav.func_high_pc..][0..4], @intCast(code_size), dwarf.endian);
2756 std.mem.writeInt(u32, wip_nav.debug_info.getWritten()[wip_nav.func_high_pc..][0..4], @intCast(code_size), dwarf.endian);
26922757 if (wip_nav.any_children) {
2693 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2694 try uleb128(diw, @intFromEnum(AbbrevCode.null));
2758 const dibw = &wip_nav.debug_info.buffered_writer;
2759 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
26952760 } else {
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);
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);
26992764 std.leb.writeUnsignedFixed(
27002765 AbbrevCode.decl_bytes,
27012766 abbrev_code_buf,
......@@ -2727,10 +2792,10 @@ pub fn finishWipNavFunc(
27272792 );
27282793 }
27292794
2730 try dwarf.finishWipNav(pt, nav_index, wip_nav);
2795 try dwarf.finishWipNavInner(pt, nav_index, wip_nav);
27312796}
27322797
2733pub fn finishWipNav(
2798fn finishWipNavInner(
27342799 dwarf: *Dwarf,
27352800 pt: Zcu.PerThread,
27362801 nav_index: InternPool.Nav.Index,
......@@ -2741,26 +2806,20 @@ pub fn finishWipNav(
27412806 const nav = ip.getNav(nav_index);
27422807 log.debug("finishWipNav({f})", .{nav.fqn.fmt(ip)});
27432808
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);
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());
27512817 }
2752 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.items);
2818 try dwarf.debug_loclists.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_loclists.getWritten());
27532819
27542820 try wip_nav.updateLazy(zcu.navSrcLoc(nav_index));
27552821}
27562822
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
27642823fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void {
27652824 const zcu = pt.zcu;
27662825 const ip = &zcu.intern_pool;
......@@ -2799,12 +2858,13 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
27992858 .func_high_pc = undefined,
28002859 .blocks = undefined,
28012860 .cfi = undefined,
2802 .debug_frame = .empty,
2803 .debug_info = .empty,
2804 .debug_line = .empty,
2805 .debug_loclists = .empty,
2861 .debug_frame = undefined,
2862 .debug_info = undefined,
2863 .debug_line = undefined,
2864 .debug_loclists = undefined,
28062865 .pending_lazy = .empty,
28072866 };
2867 wip_nav.init();
28082868 defer wip_nav.deinit();
28092869
28102870 const nav_gop = try dwarf.navs.getOrPut(dwarf.gpa, nav_index);
......@@ -2848,7 +2908,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
28482908 }
28492909 wip_nav.entry = nav_gop.value_ptr.*;
28502910
2851 const diw = wip_nav.debug_info.writer(dwarf.gpa);
2911 const dibw = &wip_nav.debug_info.buffered_writer;
28522912
28532913 switch (loaded_struct.layout) {
28542914 .auto, .@"extern" => {
......@@ -2861,9 +2921,9 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
28612921 .generic_decl = .generic_decl_const,
28622922 .decl_instance = .decl_instance_struct,
28632923 }, &nav, inst_info.file, &decl);
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().?);
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().?);
28672927 for (0..loaded_struct.field_types.len) |field_index| {
28682928 const is_comptime = loaded_struct.fieldIsComptime(ip, field_index);
28692929 const field_init = loaded_struct.fieldInit(ip, field_index);
......@@ -2899,8 +2959,8 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
28992959 }
29002960 try wip_nav.refType(field_type);
29012961 if (!is_comptime) {
2902 try uleb128(diw, loaded_struct.offsets.get(ip)[field_index]);
2903 try uleb128(diw, loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
2962 try dibw.writeLeb128(loaded_struct.offsets.get(ip)[field_index]);
2963 try dibw.writeLeb128(loaded_struct.fieldAlign(ip, field_index).toByteUnits() orelse
29042964 field_type.abiAlignment(zcu).toByteUnits().?);
29052965 }
29062966 if (has_comptime_state)
......@@ -2908,7 +2968,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29082968 else if (has_runtime_bits)
29092969 try wip_nav.blockValue(nav_src_loc, .fromInterned(field_init));
29102970 }
2911 try uleb128(diw, @intFromEnum(AbbrevCode.null));
2971 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
29122972 }
29132973 },
29142974 .@"packed" => {
......@@ -2924,10 +2984,10 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29242984 try wip_nav.strp(loaded_struct.fieldName(ip, field_index).unwrap().?.toSlice(ip));
29252985 const field_type: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]);
29262986 try wip_nav.refType(field_type);
2927 try uleb128(diw, field_bit_offset);
2987 try dibw.writeLeb128(field_bit_offset);
29282988 field_bit_offset += @intCast(field_type.bitSize(zcu));
29292989 }
2930 try uleb128(diw, @intFromEnum(AbbrevCode.null));
2990 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
29312991 },
29322992 }
29332993 break :tag .done;
......@@ -2950,7 +3010,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29503010 type_gop.value_ptr.* = nav_gop.value_ptr.*;
29513011 }
29523012 wip_nav.entry = nav_gop.value_ptr.*;
2953 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3013 const dibw = &wip_nav.debug_info.buffered_writer;
29543014 try wip_nav.declCommon(if (loaded_enum.names.len > 0) .{
29553015 .decl = .decl_enum,
29563016 .generic_decl = .generic_decl_const,
......@@ -2969,7 +3029,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29693029 }, field_index);
29703030 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
29713031 }
2972 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
3032 if (loaded_enum.names.len > 0) try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
29733033 break :tag .done;
29743034 },
29753035 .union_type => tag: {
......@@ -2989,15 +3049,15 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
29893049 type_gop.value_ptr.* = nav_gop.value_ptr.*;
29903050 }
29913051 wip_nav.entry = nav_gop.value_ptr.*;
2992 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3052 const dibw = &wip_nav.debug_info.buffered_writer;
29933053 try wip_nav.declCommon(.{
29943054 .decl = .decl_union,
29953055 .generic_decl = .generic_decl_const,
29963056 .decl_instance = .decl_instance_union,
29973057 }, &nav, inst_info.file, &decl);
29983058 const union_layout = Type.getUnionLayout(loaded_union, zcu);
2999 try uleb128(diw, union_layout.abi_size);
3000 try uleb128(diw, union_layout.abi_align.toByteUnits().?);
3059 try dibw.writeLeb128(union_layout.abi_size);
3060 try dibw.writeLeb128(union_layout.abi_align.toByteUnits().?);
30013061 const loaded_tag = loaded_union.loadTagType(ip);
30023062 if (loaded_union.hasTag(ip)) {
30033063 try wip_nav.abbrevCode(.tagged_union);
......@@ -3005,13 +3065,13 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30053065 .debug_info,
30063066 wip_nav.unit,
30073067 wip_nav.entry,
3008 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3068 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
30093069 );
30103070 {
30113071 try wip_nav.abbrevCode(.generated_field);
30123072 try wip_nav.strp("tag");
30133073 try wip_nav.refType(.fromInterned(loaded_union.enum_tag_ty));
3014 try uleb128(diw, union_layout.tagOffset());
3074 try dibw.writeLeb128(union_layout.tagOffset());
30153075
30163076 for (0..loaded_union.field_types.len) |field_index| {
30173077 try wip_nav.enumConstValue(loaded_tag, .{
......@@ -3024,23 +3084,23 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30243084 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
30253085 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
30263086 try wip_nav.refType(field_type);
3027 try uleb128(diw, union_layout.payloadOffset());
3028 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
3087 try dibw.writeLeb128(union_layout.payloadOffset());
3088 try dibw.writeLeb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
30293089 if (field_type.isNoReturn(zcu)) 1 else field_type.abiAlignment(zcu).toByteUnits().?);
30303090 }
3031 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3091 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
30323092 }
30333093 }
3034 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3094 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
30353095 } else for (0..loaded_union.field_types.len) |field_index| {
30363096 try wip_nav.abbrevCode(.untagged_union_field);
30373097 try wip_nav.strp(loaded_tag.names.get(ip)[field_index].toSlice(ip));
30383098 const field_type: Type = .fromInterned(loaded_union.field_types.get(ip)[field_index]);
30393099 try wip_nav.refType(field_type);
3040 try uleb128(diw, loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
3100 try dibw.writeLeb128(loaded_union.fieldAlign(ip, field_index).toByteUnits() orelse
30413101 field_type.abiAlignment(zcu).toByteUnits().?);
30423102 }
3043 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3103 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
30443104 break :tag .done;
30453105 },
30463106 .opaque_type => tag: {
......@@ -3060,13 +3120,13 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
30603120 type_gop.value_ptr.* = nav_gop.value_ptr.*;
30613121 }
30623122 wip_nav.entry = nav_gop.value_ptr.*;
3063 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3123 const dibw = &wip_nav.debug_info.buffered_writer;
30643124 try wip_nav.declCommon(.{
30653125 .decl = .decl_namespace_struct,
30663126 .generic_decl = .generic_decl_const,
30673127 .decl_instance = .decl_instance_namespace_struct,
30683128 }, &nav, inst_info.file, &decl);
3069 try diw.writeByte(@intFromBool(true));
3129 try dibw.writeByte(@intFromBool(true));
30703130 break :tag .done;
30713131 },
30723132 .undef,
......@@ -3104,7 +3164,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31043164 const is_nullary = !func_type.is_var_args and for (0..func_type.param_types.len) |param_index| {
31053165 if (!func_type.paramIsComptime(std.math.cast(u5, param_index) orelse break false)) break false;
31063166 } else true;
3107 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3167 const dibw = &wip_nav.debug_info.buffered_writer;
31083168 try wip_nav.declCommon(if (is_nullary) .{
31093169 .decl = .decl_nullary_func_generic,
31103170 .generic_decl = .generic_decl_func,
......@@ -3123,7 +3183,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31233183 try wip_nav.refType(.fromInterned(func_type.param_types.get(ip)[param_index]));
31243184 }
31253185 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3126 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3186 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
31273187 }
31283188 break :tag .done;
31293189 },
......@@ -3148,7 +3208,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31483208 try wip_nav.refType(nav_val.toType());
31493209 },
31503210 .decl_var => {
3151 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3211 const dibw = &wip_nav.debug_info.buffered_writer;
31523212 try wip_nav.declCommon(.{
31533213 .decl = .decl_var,
31543214 .generic_decl = .generic_decl_var,
......@@ -3158,12 +3218,12 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31583218 const nav_ty = nav_val.typeOf(zcu);
31593219 try wip_nav.refType(nav_ty);
31603220 try wip_nav.blockValue(nav_src_loc, nav_val);
3161 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
3221 try dibw.writeLeb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
31623222 nav_ty.abiAlignment(zcu).toByteUnits().?);
3163 try diw.writeByte(@intFromBool(decl.linkage != .normal));
3223 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
31643224 },
31653225 .decl_const => {
3166 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3226 const dibw = &wip_nav.debug_info.buffered_writer;
31673227 const nav_ty = nav_val.typeOf(zcu);
31683228 const has_runtime_bits = nav_ty.hasRuntimeBits(zcu);
31693229 const has_comptime_state = nav_ty.comptimeOnly(zcu) and try nav_ty.onePossibleValue(pt) == null;
......@@ -3186,9 +3246,9 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
31863246 }, &nav, inst_info.file, &decl);
31873247 try wip_nav.strp(nav.fqn.toSlice(ip));
31883248 const nav_ty_reloc_index = try wip_nav.refForward();
3189 try uleb128(diw, nav.status.fully_resolved.alignment.toByteUnits() orelse
3249 try dibw.writeLeb128(nav.status.fully_resolved.alignment.toByteUnits() orelse
31903250 nav_ty.abiAlignment(zcu).toByteUnits().?);
3191 try diw.writeByte(@intFromBool(decl.linkage != .normal));
3251 try dibw.writeByte(@intFromBool(decl.linkage != .normal));
31923252 if (has_runtime_bits) try wip_nav.blockValue(nav_src_loc, nav_val);
31933253 if (has_comptime_state) try wip_nav.refValue(nav_val);
31943254 wip_nav.finishForward(nav_ty_reloc_index);
......@@ -3204,7 +3264,7 @@ fn updateComptimeNavInner(dwarf: *Dwarf, pt: Zcu.PerThread, nav_index: InternPoo
32043264 try wip_nav.refNav(owner_nav);
32053265 },
32063266 }
3207 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
3267 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
32083268 try wip_nav.updateLazy(nav_src_loc);
32093269}
32103270
......@@ -3235,18 +3295,19 @@ fn updateLazyType(
32353295 .func_high_pc = undefined,
32363296 .blocks = undefined,
32373297 .cfi = undefined,
3238 .debug_frame = .empty,
3239 .debug_info = .empty,
3240 .debug_line = .empty,
3241 .debug_loclists = .empty,
3298 .debug_frame = undefined,
3299 .debug_info = undefined,
3300 .debug_line = undefined,
3301 .debug_loclists = undefined,
32423302 .pending_lazy = pending_lazy.*,
32433303 };
3304 wip_nav.init();
32443305 defer {
32453306 pending_lazy.* = wip_nav.pending_lazy;
32463307 wip_nav.pending_lazy = .empty;
32473308 wip_nav.deinit();
32483309 }
3249 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3310 const dibw = &wip_nav.debug_info.buffered_writer;
32503311 const name = switch (type_index) {
32513312 .generic_poison_type => "",
32523313 else => try std.fmt.allocPrint(dwarf.gpa, "{f}", .{ty.fmt(pt)}),
......@@ -3261,12 +3322,12 @@ fn updateLazyType(
32613322 .int_type => |int_type| {
32623323 try wip_nav.abbrevCode(.numeric_type);
32633324 try wip_nav.strp(name);
3264 try diw.writeByte(switch (int_type.signedness) {
3325 try dibw.writeByte(switch (int_type.signedness) {
32653326 inline .signed, .unsigned => |signedness| @field(DW.ATE, @tagName(signedness)),
32663327 });
3267 try uleb128(diw, int_type.bits);
3268 try uleb128(diw, ty.abiSize(zcu));
3269 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3328 try dibw.writeLeb128(int_type.bits);
3329 try dibw.writeLeb128(ty.abiSize(zcu));
3330 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
32703331 },
32713332 .ptr_type => |ptr_type| switch (ptr_type.flags.size) {
32723333 .one, .many, .c => {
......@@ -3274,14 +3335,14 @@ fn updateLazyType(
32743335 try wip_nav.abbrevCode(if (ptr_type.sentinel == .none) .ptr_type else .ptr_sentinel_type);
32753336 try wip_nav.strp(name);
32763337 if (ptr_type.sentinel != .none) try wip_nav.blockValue(src_loc, .fromInterned(ptr_type.sentinel));
3277 try uleb128(diw, ptr_type.flags.alignment.toByteUnits() orelse
3338 try dibw.writeLeb128(ptr_type.flags.alignment.toByteUnits() orelse
32783339 ptr_child_type.abiAlignment(zcu).toByteUnits().?);
3279 try diw.writeByte(@intFromEnum(ptr_type.flags.address_space));
3340 try dibw.writeByte(@intFromEnum(ptr_type.flags.address_space));
32803341 if (ptr_type.flags.is_const or ptr_type.flags.is_volatile) try wip_nav.infoSectionOffset(
32813342 .debug_info,
32823343 wip_nav.unit,
32833344 wip_nav.entry,
3284 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3345 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
32853346 ) else try wip_nav.refType(ptr_child_type);
32863347 if (ptr_type.flags.is_const) {
32873348 try wip_nav.abbrevCode(.is_const);
......@@ -3289,7 +3350,7 @@ fn updateLazyType(
32893350 .debug_info,
32903351 wip_nav.unit,
32913352 wip_nav.entry,
3292 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3353 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
32933354 ) else try wip_nav.refType(ptr_child_type);
32943355 }
32953356 if (ptr_type.flags.is_volatile) {
......@@ -3300,19 +3361,19 @@ fn updateLazyType(
33003361 .slice => {
33013362 try wip_nav.abbrevCode(.generated_struct_type);
33023363 try wip_nav.strp(name);
3303 try uleb128(diw, ty.abiSize(zcu));
3304 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3364 try dibw.writeLeb128(ty.abiSize(zcu));
3365 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
33053366 try wip_nav.abbrevCode(.generated_field);
33063367 try wip_nav.strp("ptr");
33073368 const ptr_field_type = ty.slicePtrFieldType(zcu);
33083369 try wip_nav.refType(ptr_field_type);
3309 try uleb128(diw, 0);
3370 try dibw.writeUleb128(0);
33103371 try wip_nav.abbrevCode(.generated_field);
33113372 try wip_nav.strp("len");
33123373 const len_field_type: Type = .usize;
33133374 try wip_nav.refType(len_field_type);
3314 try uleb128(diw, len_field_type.abiAlignment(zcu).forward(ptr_field_type.abiSize(zcu)));
3315 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3375 try dibw.writeLeb128(len_field_type.abiAlignment(zcu).forward(ptr_field_type.abiSize(zcu)));
3376 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
33163377 },
33173378 },
33183379 .array_type => |array_type| {
......@@ -3323,8 +3384,8 @@ fn updateLazyType(
33233384 try wip_nav.refType(array_child_type);
33243385 try wip_nav.abbrevCode(.array_index);
33253386 try wip_nav.refType(.usize);
3326 try uleb128(diw, array_type.len);
3327 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3387 try dibw.writeLeb128(array_type.len);
3388 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
33283389 },
33293390 .vector_type => |vector_type| {
33303391 try wip_nav.abbrevCode(.vector_type);
......@@ -3332,22 +3393,22 @@ fn updateLazyType(
33323393 try wip_nav.refType(.fromInterned(vector_type.child));
33333394 try wip_nav.abbrevCode(.array_index);
33343395 try wip_nav.refType(.usize);
3335 try uleb128(diw, vector_type.len);
3336 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3396 try dibw.writeLeb128(vector_type.len);
3397 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
33373398 },
33383399 .opt_type => |opt_child_type_index| {
33393400 const opt_child_type: Type = .fromInterned(opt_child_type_index);
33403401 const opt_repr = optRepr(opt_child_type, zcu);
33413402 try wip_nav.abbrevCode(.generated_union_type);
33423403 try wip_nav.strp(name);
3343 try uleb128(diw, ty.abiSize(zcu));
3344 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3404 try dibw.writeLeb128(ty.abiSize(zcu));
3405 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
33453406 switch (opt_repr) {
33463407 .opv_null => {
33473408 try wip_nav.abbrevCode(.generated_field);
33483409 try wip_nav.strp("null");
33493410 try wip_nav.refType(.null);
3350 try uleb128(diw, 0);
3411 try dibw.writeUleb128(0);
33513412 },
33523413 .unpacked, .error_set, .pointer => {
33533414 try wip_nav.abbrevCode(.tagged_union);
......@@ -3355,7 +3416,7 @@ fn updateLazyType(
33553416 .debug_info,
33563417 wip_nav.unit,
33573418 wip_nav.entry,
3358 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3419 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
33593420 );
33603421 {
33613422 try wip_nav.abbrevCode(.generated_field);
......@@ -3364,7 +3425,7 @@ fn updateLazyType(
33643425 .opv_null => unreachable,
33653426 .unpacked => {
33663427 try wip_nav.refType(.bool);
3367 try uleb128(diw, if (opt_child_type.hasRuntimeBits(zcu))
3428 try dibw.writeLeb128(if (opt_child_type.hasRuntimeBits(zcu))
33683429 opt_child_type.abiSize(zcu)
33693430 else
33703431 0);
......@@ -3374,37 +3435,37 @@ fn updateLazyType(
33743435 .signedness = .unsigned,
33753436 .bits = zcu.errorSetBits(),
33763437 } })));
3377 try uleb128(diw, 0);
3438 try dibw.writeUleb128(0);
33783439 },
33793440 .pointer => {
33803441 try wip_nav.refType(.usize);
3381 try uleb128(diw, 0);
3442 try dibw.writeUleb128(0);
33823443 },
33833444 }
33843445
33853446 try wip_nav.abbrevCode(.unsigned_tagged_union_field);
3386 try uleb128(diw, 0);
3447 try dibw.writeUleb128(0);
33873448 {
33883449 try wip_nav.abbrevCode(.generated_field);
33893450 try wip_nav.strp("null");
33903451 try wip_nav.refType(.null);
3391 try uleb128(diw, 0);
3452 try dibw.writeUleb128(0);
33923453 }
3393 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3454 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
33943455
33953456 try wip_nav.abbrevCode(.tagged_union_default_field);
33963457 {
33973458 try wip_nav.abbrevCode(.generated_field);
33983459 try wip_nav.strp("?");
33993460 try wip_nav.refType(opt_child_type);
3400 try uleb128(diw, 0);
3461 try dibw.writeUleb128(0);
34013462 }
3402 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3463 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
34033464 }
3404 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3465 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
34053466 },
34063467 }
3407 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3468 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
34083469 },
34093470 .anyframe_type => unreachable,
34103471 .error_union_type => |error_union_type| {
......@@ -3423,11 +3484,11 @@ fn updateLazyType(
34233484 if (error_union_type.error_set_type != .generic_poison_type and
34243485 error_union_type.payload_type != .generic_poison_type)
34253486 {
3426 try uleb128(diw, ty.abiSize(zcu));
3427 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3487 try dibw.writeLeb128(ty.abiSize(zcu));
3488 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
34283489 } else {
3429 try uleb128(diw, 0);
3430 try uleb128(diw, 1);
3490 try dibw.writeUleb128(0);
3491 try dibw.writeUleb128(1);
34313492 }
34323493 {
34333494 try wip_nav.abbrevCode(.tagged_union);
......@@ -3435,7 +3496,7 @@ fn updateLazyType(
34353496 .debug_info,
34363497 wip_nav.unit,
34373498 wip_nav.entry,
3438 @intCast(wip_nav.debug_info.items.len + dwarf.sectionOffsetBytes()),
3499 @intCast(dibw.count + dwarf.sectionOffsetBytes()),
34393500 );
34403501 {
34413502 try wip_nav.abbrevCode(.generated_field);
......@@ -3444,30 +3505,30 @@ fn updateLazyType(
34443505 .signedness = .unsigned,
34453506 .bits = zcu.errorSetBits(),
34463507 } })));
3447 try uleb128(diw, error_union_error_set_offset);
3508 try dibw.writeLeb128(error_union_error_set_offset);
34483509
34493510 try wip_nav.abbrevCode(.unsigned_tagged_union_field);
3450 try uleb128(diw, 0);
3511 try dibw.writeUleb128(0);
34513512 {
34523513 try wip_nav.abbrevCode(.generated_field);
34533514 try wip_nav.strp("value");
34543515 try wip_nav.refType(error_union_payload_type);
3455 try uleb128(diw, error_union_payload_offset);
3516 try dibw.writeLeb128(error_union_payload_offset);
34563517 }
3457 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3518 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
34583519
34593520 try wip_nav.abbrevCode(.tagged_union_default_field);
34603521 {
34613522 try wip_nav.abbrevCode(.generated_field);
34623523 try wip_nav.strp("error");
34633524 try wip_nav.refType(error_union_error_set_type);
3464 try uleb128(diw, error_union_error_set_offset);
3525 try dibw.writeLeb128(error_union_error_set_offset);
34653526 }
3466 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3527 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
34673528 }
3468 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3529 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
34693530 }
3470 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3531 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
34713532 },
34723533 .simple_type => |simple_type| switch (simple_type) {
34733534 .f16,
......@@ -3491,7 +3552,7 @@ fn updateLazyType(
34913552 => {
34923553 try wip_nav.abbrevCode(.numeric_type);
34933554 try wip_nav.strp(name);
3494 try diw.writeByte(if (type_index == .bool_type)
3555 try dibw.writeByte(if (type_index == .bool_type)
34953556 DW.ATE.boolean
34963557 else if (ty.isRuntimeFloat())
34973558 DW.ATE.float
......@@ -3501,9 +3562,9 @@ fn updateLazyType(
35013562 DW.ATE.unsigned
35023563 else
35033564 unreachable);
3504 try uleb128(diw, ty.bitSize(zcu));
3505 try uleb128(diw, ty.abiSize(zcu));
3506 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3565 try dibw.writeLeb128(ty.bitSize(zcu));
3566 try dibw.writeLeb128(ty.abiSize(zcu));
3567 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
35073568 },
35083569 .anyopaque,
35093570 .void,
......@@ -3529,12 +3590,12 @@ fn updateLazyType(
35293590 .tuple_type => |tuple_type| if (tuple_type.types.len == 0) {
35303591 try wip_nav.abbrevCode(.generated_empty_struct_type);
35313592 try wip_nav.strp(name);
3532 try diw.writeByte(@intFromBool(false));
3593 try dibw.writeByte(@intFromBool(false));
35333594 } else {
35343595 try wip_nav.abbrevCode(.generated_struct_type);
35353596 try wip_nav.strp(name);
3536 try uleb128(diw, ty.abiSize(zcu));
3537 try uleb128(diw, ty.abiAlignment(zcu).toByteUnits().?);
3597 try dibw.writeLeb128(ty.abiSize(zcu));
3598 try dibw.writeLeb128(ty.abiAlignment(zcu).toByteUnits().?);
35383599 var field_byte_offset: u64 = 0;
35393600 for (0..tuple_type.types.len) |field_index| {
35403601 const comptime_value = tuple_type.values.get(ip)[field_index];
......@@ -3563,8 +3624,8 @@ fn updateLazyType(
35633624 if (comptime_value == .none) {
35643625 const field_align = field_type.abiAlignment(zcu);
35653626 field_byte_offset = field_align.forward(field_byte_offset);
3566 try uleb128(diw, field_byte_offset);
3567 try uleb128(diw, field_type.abiAlignment(zcu).toByteUnits().?);
3627 try dibw.writeLeb128(field_byte_offset);
3628 try dibw.writeLeb128(field_type.abiAlignment(zcu).toByteUnits().?);
35683629 field_byte_offset += field_type.abiSize(zcu);
35693630 }
35703631 if (has_comptime_state)
......@@ -3572,7 +3633,7 @@ fn updateLazyType(
35723633 else if (has_runtime_bits)
35733634 try wip_nav.blockValue(src_loc, .fromInterned(comptime_value));
35743635 }
3575 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3636 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
35763637 },
35773638 .enum_type => {
35783639 const loaded_enum = ip.loadEnumType(type_index);
......@@ -3587,7 +3648,7 @@ fn updateLazyType(
35873648 }, field_index);
35883649 try wip_nav.strp(loaded_enum.names.get(ip)[field_index].toSlice(ip));
35893650 }
3590 if (loaded_enum.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
3651 if (loaded_enum.names.len > 0) try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
35913652 },
35923653 .func_type => |func_type| {
35933654 const is_nullary = func_type.param_types.len == 0 and !func_type.is_var_args;
......@@ -3655,7 +3716,7 @@ fn updateLazyType(
36553716 else => .nocall,
36563717 };
36573718 };
3658 try diw.writeByte(@intFromEnum(cc));
3719 try dibw.writeByte(@intFromEnum(cc));
36593720 try wip_nav.refType(.fromInterned(func_type.return_type));
36603721 if (!is_nullary) {
36613722 for (0..func_type.param_types.len) |param_index| {
......@@ -3663,7 +3724,7 @@ fn updateLazyType(
36633724 try wip_nav.refType(.fromInterned(func_type.param_types.get(ip)[param_index]));
36643725 }
36653726 if (func_type.is_var_args) try wip_nav.abbrevCode(.is_var_args);
3666 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3727 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
36673728 }
36683729 },
36693730 .error_set_type => |error_set_type| {
......@@ -3676,10 +3737,10 @@ fn updateLazyType(
36763737 for (0..error_set_type.names.len) |field_index| {
36773738 const field_name = error_set_type.names.get(ip)[field_index];
36783739 try wip_nav.abbrevCode(.unsigned_enum_field);
3679 try uleb128(diw, ip.getErrorValueIfExists(field_name).?);
3740 try dibw.writeLeb128(ip.getErrorValueIfExists(field_name).?);
36803741 try wip_nav.strp(field_name.toSlice(ip));
36813742 }
3682 if (error_set_type.names.len > 0) try uleb128(diw, @intFromEnum(AbbrevCode.null));
3743 if (error_set_type.names.len > 0) try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
36833744 },
36843745 .inferred_error_set_type => |func| {
36853746 try wip_nav.abbrevCode(.inferred_error_set_type);
......@@ -3711,7 +3772,7 @@ fn updateLazyType(
37113772 .memoized_call,
37123773 => unreachable,
37133774 }
3714 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
3775 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
37153776}
37163777
37173778fn updateLazyValue(
......@@ -3739,18 +3800,19 @@ fn updateLazyValue(
37393800 .func_high_pc = undefined,
37403801 .blocks = undefined,
37413802 .cfi = undefined,
3742 .debug_frame = .empty,
3743 .debug_info = .empty,
3744 .debug_line = .empty,
3745 .debug_loclists = .empty,
3803 .debug_frame = undefined,
3804 .debug_info = undefined,
3805 .debug_line = undefined,
3806 .debug_loclists = undefined,
37463807 .pending_lazy = pending_lazy.*,
37473808 };
3809 wip_nav.init();
37483810 defer {
37493811 pending_lazy.* = wip_nav.pending_lazy;
37503812 wip_nav.pending_lazy = .empty;
37513813 wip_nav.deinit();
37523814 }
3753 const diw = wip_nav.debug_info.writer(dwarf.gpa);
3815 const dibw = &wip_nav.debug_info.buffered_writer;
37543816 var big_int_space: Value.BigIntSpace = undefined;
37553817 switch (ip.indexToKey(value_index)) {
37563818 .int_type,
......@@ -3788,7 +3850,7 @@ fn updateLazyValue(
37883850 .err => |err| {
37893851 try wip_nav.abbrevCode(.udata_comptime_value);
37903852 try wip_nav.refType(.fromInterned(err.ty));
3791 try uleb128(diw, try pt.getErrorValue(err.name));
3853 try dibw.writeLeb128(try pt.getErrorValue(err.name));
37923854 },
37933855 .error_union => |error_union| {
37943856 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -3800,8 +3862,8 @@ fn updateLazyValue(
38003862 {
38013863 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
38023864 try wip_nav.strp("is_error");
3803 try uleb128(diw, err_abi_size);
3804 dwarf.writeInt(try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, err_abi_size), err_value);
3865 try dibw.writeLeb128(err_abi_size);
3866 try dwarf.writeIntTo(dibw, err_abi_size, err_value);
38053867 }
38063868 payload_field: switch (error_union.val) {
38073869 .err_name => {},
......@@ -3825,8 +3887,8 @@ fn updateLazyValue(
38253887 {
38263888 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
38273889 try wip_nav.strp("error");
3828 try uleb128(diw, err_abi_size);
3829 dwarf.writeInt(try wip_nav.debug_info.addManyAsSlice(dwarf.gpa, err_abi_size), err_value);
3890 try dibw.writeLeb128(err_abi_size);
3891 try dwarf.writeIntTo(dibw, err_abi_size, err_value);
38303892 }
38313893 switch (error_union.val) {
38323894 .err_name => {},
......@@ -3836,7 +3898,7 @@ fn updateLazyValue(
38363898 },
38373899 }
38383900 try wip_nav.refType(.fromInterned(error_union.ty));
3839 try uleb128(diw, @intFromEnum(AbbrevCode.null));
3901 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
38403902 },
38413903 .enum_literal => |enum_literal| {
38423904 try wip_nav.abbrevCode(.string_comptime_value);
......@@ -3857,24 +3919,24 @@ fn updateLazyValue(
38573919 switch (float.storage) {
38583920 .f16 => |f16_val| {
38593921 try wip_nav.abbrevCode(.data2_comptime_value);
3860 try diw.writeInt(u16, @bitCast(f16_val), dwarf.endian);
3922 try dibw.writeInt(u16, @bitCast(f16_val), dwarf.endian);
38613923 },
38623924 .f32 => |f32_val| {
38633925 try wip_nav.abbrevCode(.data4_comptime_value);
3864 try diw.writeInt(u32, @bitCast(f32_val), dwarf.endian);
3926 try dibw.writeInt(u32, @bitCast(f32_val), dwarf.endian);
38653927 },
38663928 .f64 => |f64_val| {
38673929 try wip_nav.abbrevCode(.data8_comptime_value);
3868 try diw.writeInt(u64, @bitCast(f64_val), dwarf.endian);
3930 try dibw.writeInt(u64, @bitCast(f64_val), dwarf.endian);
38693931 },
38703932 .f80 => |f80_val| {
38713933 try wip_nav.abbrevCode(.block_comptime_value);
3872 try uleb128(diw, @divExact(80, 8));
3873 try diw.writeInt(u80, @bitCast(f80_val), dwarf.endian);
3934 try dibw.writeUleb128(@divExact(80, 8));
3935 try dibw.writeInt(u80, @bitCast(f80_val), dwarf.endian);
38743936 },
38753937 .f128 => |f128_val| {
38763938 try wip_nav.abbrevCode(.data16_comptime_value);
3877 try diw.writeInt(u128, @bitCast(f128_val), dwarf.endian);
3939 try dibw.writeInt(u128, @bitCast(f128_val), dwarf.endian);
38783940 },
38793941 }
38803942 try wip_nav.refType(.fromInterned(float.ty));
......@@ -3891,14 +3953,14 @@ fn updateLazyValue(
38913953 const uav_ty: Type = .fromInterned(ip.typeOf(uav.val));
38923954 if (try uav_ty.onePossibleValue(pt)) |_| {
38933955 try wip_nav.abbrevCode(.udata_comptime_value);
3894 try uleb128(diw, ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment.toByteUnits() orelse
3956 try dibw.writeLeb128(ip.indexToKey(uav.orig_ty).ptr_type.flags.alignment.toByteUnits() orelse
38953957 uav_ty.abiAlignment(zcu).toByteUnits().?);
38963958 break :location;
38973959 } else break try wip_nav.getValueEntry(.fromInterned(uav.val));
38983960 },
38993961 .int => {
39003962 try wip_nav.abbrevCode(.udata_comptime_value);
3901 try uleb128(diw, byte_offset);
3963 try dibw.writeLeb128(byte_offset);
39023964 break :location;
39033965 },
39043966 .eu_payload => |eu_ptr| {
......@@ -3937,7 +3999,7 @@ fn updateLazyValue(
39373999 try wip_nav.strp("len");
39384000 try wip_nav.blockValue(src_loc, .fromInterned(slice.len));
39394001 }
3940 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4002 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
39414003 },
39424004 .opt => |opt| {
39434005 const opt_child_type: Type = .fromInterned(ip.indexToKey(opt.ty).opt_type);
......@@ -3947,7 +4009,7 @@ fn updateLazyValue(
39474009 try wip_nav.abbrevCode(.comptime_value_field_runtime_bits);
39484010 try wip_nav.strp("has_value");
39494011 switch (optRepr(opt_child_type, zcu)) {
3950 .opv_null => try uleb128(diw, 0),
4012 .opv_null => try dibw.writeUleb128(0),
39514013 .unpacked => try wip_nav.blockValue(src_loc, .makeBool(opt.val != .none)),
39524014 .error_set => try wip_nav.blockValue(src_loc, .fromInterned(value_index)),
39534015 .pointer => if (opt_child_type.comptimeOnly(zcu)) {
......@@ -3957,8 +4019,8 @@ fn updateLazyValue(
39574019 .none => 0,
39584020 else => opt_child_type.ptrAlignment(zcu).toByteUnits().?,
39594021 });
3960 try uleb128(diw, bytes.len);
3961 try diw.writeAll(bytes);
4022 try dibw.writeLeb128(bytes.len);
4023 try dibw.writeAll(bytes);
39624024 } else try wip_nav.blockValue(src_loc, .fromInterned(value_index)),
39634025 }
39644026 }
......@@ -3977,7 +4039,7 @@ fn updateLazyValue(
39774039 else
39784040 try wip_nav.blockValue(src_loc, .fromInterned(opt.val));
39794041 }
3980 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4042 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
39814043 },
39824044 .aggregate => |aggregate| {
39834045 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -4062,7 +4124,7 @@ fn updateLazyValue(
40624124 },
40634125 else => unreachable,
40644126 }
4065 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4127 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
40664128 },
40674129 .un => |un| {
40684130 try wip_nav.abbrevCode(.aggregate_comptime_value);
......@@ -4087,11 +4149,11 @@ fn updateLazyValue(
40874149 else
40884150 try wip_nav.blockValue(src_loc, .fromInterned(un.val));
40894151 }
4090 try uleb128(diw, @intFromEnum(AbbrevCode.null));
4152 try dibw.writeLeb128(@intFromEnum(AbbrevCode.null));
40914153 },
40924154 .memoized_call => unreachable, // not a value
40934155 }
4094 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.items);
4156 try dwarf.debug_info.section.replaceEntry(wip_nav.unit, wip_nav.entry, dwarf, wip_nav.debug_info.getWritten());
40954157}
40964158
40974159fn optRepr(opt_child_type: Type, zcu: *const Zcu) enum {
......@@ -4111,7 +4173,7 @@ fn optRepr(opt_child_type: Type, zcu: *const Zcu) enum {
41114173 };
41124174}
41134175
4114pub fn updateContainerType(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternPool.Index) UpdateError!void {
4176fn updateContainerTypeInner(dwarf: *Dwarf, pt: Zcu.PerThread, type_index: InternPool.Index) UpdateError!void {
41154177 const zcu = pt.zcu;
41164178 const ip = &zcu.intern_pool;
41174179 const ty: Type = .fromInterned(type_index);
......@@ -4438,25 +4500,26 @@ fn refAbbrevCode(dwarf: *Dwarf, abbrev_code: AbbrevCode) UpdateError!@typeInfo(A
44384500 assert(abbrev_code != .null);
44394501 const entry: Entry.Index = @enumFromInt(@intFromEnum(abbrev_code));
44404502 if (dwarf.debug_abbrev.section.getUnit(DebugAbbrev.unit).getEntry(entry).len > 0) return @intFromEnum(abbrev_code);
4441 var debug_abbrev: std.ArrayList(u8) = .init(dwarf.gpa);
4442 defer debug_abbrev.deinit();
4443 const daw = debug_abbrev.writer();
4503 var daaw: std.io.Writer.Allocating = .init(dwarf.gpa);
4504 defer daaw.deinit();
4505 const dabw = &daaw.interface;
44444506 const abbrev = AbbrevCode.abbrevs.get(abbrev_code);
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);
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());
44514513 return @intFromEnum(abbrev_code);
44524514}
44534515
44544516pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
4517 const gpa = dwarf.gpa;
44554518 const zcu = pt.zcu;
44564519 const ip = &zcu.intern_pool;
44574520
44584521 {
4459 const type_gop = try dwarf.types.getOrPut(dwarf.gpa, .anyerror_type);
4522 const type_gop = try dwarf.types.getOrPut(gpa, .anyerror_type);
44604523 if (!type_gop.found_existing) type_gop.value_ptr.* = try dwarf.addCommonEntry(.main);
44614524 var wip_nav: WipNav = .{
44624525 .dwarf = dwarf,
......@@ -4469,14 +4532,15 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
44694532 .func_high_pc = undefined,
44704533 .blocks = undefined,
44714534 .cfi = undefined,
4472 .debug_frame = .empty,
4473 .debug_info = .empty,
4474 .debug_line = .empty,
4475 .debug_loclists = .empty,
4535 .debug_frame = undefined,
4536 .debug_info = undefined,
4537 .debug_line = undefined,
4538 .debug_loclists = undefined,
44764539 .pending_lazy = .empty,
44774540 };
4541 wip_nav.init();
44784542 defer wip_nav.deinit();
4479 const diw = wip_nav.debug_info.writer(dwarf.gpa);
4543 const dibw = &wip_nav.debug_info.buffered_writer;
44804544 const global_error_set_names = ip.global_error_set.getNamesFromMainThread();
44814545 try wip_nav.abbrevCode(if (global_error_set_names.len == 0) .generated_empty_enum_type else .generated_enum_type);
44824546 try wip_nav.strp("anyerror");
......@@ -4486,50 +4550,52 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
44864550 } })));
44874551 for (global_error_set_names, 1..) |name, value| {
44884552 try wip_nav.abbrevCode(.unsigned_enum_field);
4489 try uleb128(diw, value);
4553 try dibw.writeLeb128(value);
44904554 try wip_nav.strp(name.toSlice(ip));
44914555 }
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);
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());
44944558 try wip_nav.updateLazy(.unneeded);
44954559 }
44964560
44974561 for (dwarf.mods.keys(), dwarf.mods.values()) |mod, *mod_info| {
4498 const root_dir_path = try mod.root.toAbsolute(zcu.comp.dirs, dwarf.gpa);
4499 defer dwarf.gpa.free(root_dir_path);
4562 const root_dir_path = try mod.root.toAbsolute(zcu.comp.dirs, gpa);
4563 defer gpa.free(root_dir_path);
45004564 mod_info.root_dir_path = try dwarf.debug_line_str.addString(dwarf, root_dir_path);
45014565 }
45024566
4503 var header: std.ArrayList(u8) = .init(dwarf.gpa);
4504 defer header.deinit();
4567 var header: std.ArrayListUnmanaged(u8) = .empty;
4568 defer header.deinit(gpa);
4569 var header_bw: Writer = undefined;
45054570 if (dwarf.debug_aranges.section.dirty) {
45064571 for (dwarf.debug_aranges.section.units.items, 0..) |*unit_ptr, unit_index| {
45074572 const unit: Unit.Index = @enumFromInt(unit_index);
45084573 unit_ptr.clear();
4509 try unit_ptr.cross_section_relocs.ensureTotalCapacity(dwarf.gpa, 1);
4510 header.clearRetainingCapacity();
4511 try header.ensureTotalCapacity(unit_ptr.header_len);
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);
45124577 const unit_len = (if (unit_ptr.next.unwrap()) |next_unit|
45134578 dwarf.debug_aranges.section.getUnit(next_unit).off
45144579 else
45154580 dwarf.debug_aranges.section.len) - unit_ptr.off - dwarf.unitLengthBytes();
45164581 switch (dwarf.format) {
4517 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4582 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
45184583 .@"64" => {
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);
4584 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4585 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
45214586 },
45224587 }
4523 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 2, dwarf.endian);
4588 header_bw.writeInt(u16, 2, dwarf.endian) catch unreachable;
45244589 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4525 .source_off = @intCast(header.items.len),
4590 .source_off = @intCast(header_bw.end),
45264591 .target_sec = .debug_info,
45274592 .target_unit = unit,
45284593 });
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);
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);
45334599 try unit_ptr.writeTrailer(&dwarf.debug_aranges.section, dwarf);
45344600 }
45354601 dwarf.debug_aranges.section.dirty = false;
......@@ -4544,31 +4610,33 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
45444610 dev.check(.x86_64_backend);
45454611 const Register = @import("../arch/x86_64/bits.zig").Register;
45464612 for (dwarf.debug_frame.section.units.items) |*unit| {
4547 header.clearRetainingCapacity();
4548 try header.ensureTotalCapacity(unit.header_len);
4613 try header.resize(gpa, unit.header_len);
4614 header_bw = .fixed(header.items);
45494615 const unit_len = unit.header_len - dwarf.unitLengthBytes();
45504616 switch (dwarf.format) {
4551 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4617 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
45524618 .@"64" => {
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);
4619 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4620 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
45554621 },
45564622 }
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);
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);
45724640 try unit.writeTrailer(&dwarf.debug_frame.section, dwarf);
45734641 }
45744642 },
......@@ -4581,83 +4649,84 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
45814649 for (dwarf.mods.keys(), dwarf.mods.values(), dwarf.debug_info.section.units.items, 0..) |mod, mod_info, *unit_ptr, unit_index| {
45824650 const unit: Unit.Index = @enumFromInt(unit_index);
45834651 unit_ptr.clear();
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);
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);
45884656 const unit_len = (if (unit_ptr.next.unwrap()) |next_unit|
45894657 dwarf.debug_info.section.getUnit(next_unit).off
45904658 else
45914659 dwarf.debug_info.section.len) - unit_ptr.off - dwarf.unitLengthBytes();
45924660 switch (dwarf.format) {
4593 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4661 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
45944662 .@"64" => {
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);
4663 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4664 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
45974665 },
45984666 }
4599 std.mem.writeInt(u16, header.addManyAsArrayAssumeCapacity(2), 5, dwarf.endian);
4600 header.appendSliceAssumeCapacity(&.{ DW.UT.compile, @intFromEnum(dwarf.address_size) });
4667 header_bw.writeInt(u16, 5, dwarf.endian) catch unreachable;
4668 header_bw.writeAll(&.{ DW.UT.compile, @intFromEnum(dwarf.address_size) }) catch unreachable;
46014669 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4602 .source_off = @intCast(header.items.len),
4670 .source_off = @intCast(header_bw.end),
46034671 .target_sec = .debug_abbrev,
46044672 .target_unit = DebugAbbrev.unit,
46054673 });
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);
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;
46104678 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4611 .source_off = @intCast(header.items.len),
4679 .source_off = @intCast(header_bw.end),
46124680 .target_sec = .debug_line_str,
46134681 .target_unit = StringSection.unit,
46144682 .target_entry = (try dwarf.debug_line_str.addString(dwarf, "zig " ++ @import("build_options").version)).toOptional(),
46154683 });
4616 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4684 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46174685 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4618 .source_off = @intCast(header.items.len),
4686 .source_off = @intCast(header_bw.end),
46194687 .target_sec = .debug_line_str,
46204688 .target_unit = StringSection.unit,
46214689 .target_entry = mod_info.root_dir_path.toOptional(),
46224690 });
4623 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4691 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46244692 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4625 .source_off = @intCast(header.items.len),
4693 .source_off = @intCast(header_bw.end),
46264694 .target_sec = .debug_line_str,
46274695 .target_unit = StringSection.unit,
46284696 .target_entry = (try dwarf.debug_line_str.addString(dwarf, mod.root_src_path)).toOptional(),
46294697 });
4630 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4698 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46314699 unit_ptr.cross_unit_relocs.appendAssumeCapacity(.{
4632 .source_off = @intCast(header.items.len),
4700 .source_off = @intCast(header_bw.end),
46334701 .target_unit = .main,
46344702 .target_off = compile_unit_off,
46354703 });
4636 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4704 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46374705 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4638 .source_off = @intCast(header.items.len),
4706 .source_off = @intCast(header_bw.end),
46394707 .target_sec = .debug_line,
46404708 .target_unit = unit,
46414709 });
4642 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4710 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
46434711 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4644 .source_off = @intCast(header.items.len),
4712 .source_off = @intCast(header_bw.end),
46454713 .target_sec = .debug_rnglists,
46464714 .target_unit = unit,
46474715 .target_off = DebugRngLists.baseOffset(dwarf),
46484716 });
4649 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4650 uleb128(header.fixedWriter(), 0) catch unreachable;
4651 uleb128(header.fixedWriter(), try dwarf.refAbbrevCode(.module)) catch unreachable;
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;
46524720 unit_ptr.cross_section_relocs.appendAssumeCapacity(.{
4653 .source_off = @intCast(header.items.len),
4721 .source_off = @intCast(header_bw.end),
46544722 .target_sec = .debug_str,
46554723 .target_unit = StringSection.unit,
46564724 .target_entry = (try dwarf.debug_str.addString(dwarf, mod.fully_qualified_name)).toOptional(),
46574725 });
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);
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);
46614730 try unit_ptr.writeTrailer(&dwarf.debug_info.section, dwarf);
46624731 }
46634732 dwarf.debug_info.section.dirty = false;
......@@ -4681,33 +4750,37 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
46814750 );
46824751 for (dwarf.mods.values(), dwarf.debug_line.section.units.items) |mod_info, *unit| {
46834752 unit.clear();
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);
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);
46874756 const unit_len = (if (unit.next.unwrap()) |next_unit|
46884757 dwarf.debug_line.section.getUnit(next_unit).off
46894758 else
46904759 dwarf.debug_line.section.len) - unit.off - dwarf.unitLengthBytes();
46914760 switch (dwarf.format) {
4692 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4761 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
46934762 .@"64" => {
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);
4763 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4764 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
46964765 },
46974766 }
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);
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;
47014774 const StandardOpcode = DeclValEnum(DW.LNS);
4702 header.appendSliceAssumeCapacity(&[_]u8{
4775 header_bw.writeAll(&.{
47034776 dwarf.debug_line.header.minimum_instruction_length,
47044777 dwarf.debug_line.header.maximum_operations_per_instruction,
47054778 @intFromBool(dwarf.debug_line.header.default_is_stmt),
47064779 @bitCast(dwarf.debug_line.header.line_base),
47074780 dwarf.debug_line.header.line_range,
47084781 dwarf.debug_line.header.opcode_base,
4709 });
4710 header.appendSliceAssumeCapacity(std.enums.EnumArray(StandardOpcode, u8).init(.{
4782 }) catch unreachable;
4783 header_bw.writeAll(std.enums.EnumArray(StandardOpcode, u8).init(.{
47114784 .extended_op = undefined,
47124785 .copy = 0,
47134786 .advance_pc = 1,
......@@ -4721,44 +4794,45 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47214794 .set_prologue_end = 0,
47224795 .set_epilogue_begin = 0,
47234796 .set_isa = 1,
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;
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;
47294802 for (mod_info.dirs.keys()) |dir_unit| {
47304803 unit.cross_section_relocs.appendAssumeCapacity(.{
4731 .source_off = @intCast(header.items.len),
4804 .source_off = @intCast(header_bw.end),
47324805 .target_sec = .debug_line_str,
47334806 .target_unit = StringSection.unit,
47344807 .target_entry = dwarf.getModInfo(dir_unit).root_dir_path.toOptional(),
47354808 });
4736 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4809 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
47374810 }
47384811 const dir_index_info = DebugLine.dirIndexInfo(@intCast(mod_info.dirs.count()));
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;
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;
47474820 for (mod_info.files.keys()) |file_index| {
47484821 const file = zcu.fileByIndex(file_index);
47494822 unit.cross_section_relocs.appendAssumeCapacity(.{
4750 .source_off = @intCast(header.items.len),
4823 .source_off = @intCast(header_bw.end),
47514824 .target_sec = .debug_line_str,
47524825 .target_unit = StringSection.unit,
47534826 .target_entry = (try dwarf.debug_line_str.addString(dwarf, file.sub_file_path)).toOptional(),
47544827 });
47554828 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4756 dwarf.writeInt(
4757 header.addManyAsSliceAssumeCapacity(dir_index_info.bytes),
4829 dwarf.writeIntTo(
4830 &header_bw,
4831 dir_index_info.bytes,
47584832 mod_info.dirs.getIndex(dwarf.getUnitIfExists(file.mod.?).?) orelse 0,
47594833 );
47604834 unit.cross_section_relocs.appendAssumeCapacity(.{
4761 .source_off = @intCast(header.items.len),
4835 .source_off = @intCast(header_bw.end),
47624836 .target_sec = .debug_line_str,
47634837 .target_unit = StringSection.unit,
47644838 .target_entry = (try dwarf.debug_line_str.addString(
......@@ -4766,9 +4840,10 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47664840 if (file.is_builtin) file.source.? else "",
47674841 )).toOptional(),
47684842 });
4769 header.appendNTimesAssumeCapacity(0, dwarf.sectionOffsetBytes());
4843 header_bw.splatByteAll(0, dwarf.sectionOffsetBytes()) catch unreachable;
47704844 }
4771 try unit.replaceHeader(&dwarf.debug_line.section, dwarf, header.items);
4845 assert(header_bw.end == header_bw.buffer.len);
4846 try unit.replaceHeader(&dwarf.debug_line.section, dwarf, header_bw.buffer);
47724847 try unit.writeTrailer(&dwarf.debug_line.section, dwarf);
47734848 }
47744849 dwarf.debug_line.section.dirty = false;
......@@ -4784,24 +4859,25 @@ pub fn flush(dwarf: *Dwarf, pt: Zcu.PerThread) FlushError!void {
47844859 }
47854860 if (dwarf.debug_rnglists.section.dirty) {
47864861 for (dwarf.debug_rnglists.section.units.items) |*unit| {
4787 header.clearRetainingCapacity();
4788 try header.ensureTotalCapacity(unit.header_len);
4862 try header.resize(gpa, unit.header_len);
4863 header_bw = .fixed(header.items);
47894864 const unit_len = (if (unit.next.unwrap()) |next_unit|
47904865 dwarf.debug_rnglists.section.getUnit(next_unit).off
47914866 else
47924867 dwarf.debug_rnglists.section.len) - unit.off - dwarf.unitLengthBytes();
47934868 switch (dwarf.format) {
4794 .@"32" => std.mem.writeInt(u32, header.addManyAsArrayAssumeCapacity(4), @intCast(unit_len), dwarf.endian),
4869 .@"32" => header_bw.writeInt(u32, @intCast(unit_len), dwarf.endian) catch unreachable,
47954870 .@"64" => {
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);
4871 header_bw.writeInt(u32, std.math.maxInt(u32), dwarf.endian) catch unreachable;
4872 header_bw.writeInt(u64, unit_len, dwarf.endian) catch unreachable;
47984873 },
47994874 }
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);
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);
48054881 try unit.writeTrailer(&dwarf.debug_rnglists.section, dwarf);
48064882 }
48074883 dwarf.debug_rnglists.section.dirty = false;
......@@ -6000,6 +6076,10 @@ fn writeInt(dwarf: *Dwarf, buf: []u8, int: u64) void {
60006076 }
60016077}
60026078
6079fn writeIntTo(dwarf: *Dwarf, bw: *Writer, len: usize, int: u64) !void {
6080 dwarf.writeInt(try bw.writableSlice(len), int);
6081}
6082
60036083fn resolveReloc(dwarf: *Dwarf, source: u64, target: u64, size: u32) RelocError!void {
60046084 var buf: [8]u8 = undefined;
60056085 dwarf.writeInt(buf[0..size], target);
......@@ -6026,7 +6106,6 @@ fn uleb128Bytes(value: anytype) u32 {
60266106 d.writer.writeUleb128(value) catch unreachable;
60276107 return @intCast(d.count + d.writer.end);
60286108}
6029
60306109fn sleb128Bytes(value: anytype) u32 {
60316110 var trash_buffer: [64]u8 = undefined;
60326111 var d: std.Io.Writer.Discarding = .init(&trash_buffer);
......@@ -6053,7 +6132,7 @@ const codegen = @import("../codegen.zig");
60536132const dev = @import("../dev.zig");
60546133const link = @import("../link.zig");
60556134const log = std.log.scoped(.dwarf);
6056const sleb128 = std.leb.writeIleb128;
60576135const std = @import("std");
60586136const target_info = @import("../target.zig");
6059const uleb128 = std.leb.writeUleb128;
6137const Allocator = std.mem.Allocator;
6138const Writer = std.io.Writer;