authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-04 18:54:18+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-12-05 14:19:22+01:00
log8796da028320b97a4b67a401109dce1137ee2bbf
treeeac0ec8d74fe773619bec1e67b87fa620a78849c
parent3575048c0ae8b5bae2cd5c6fd3dbc9cb569c4b1f

dwarf: reuse getDbgInfoAtom helper in all of Dwarf.zig

We need to access it outside of `DeclState` too so why not reuse the helper anyway.

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

src/link/Dwarf.zig+18-27
......@@ -586,7 +586,7 @@ pub const DeclState = struct {
586586 loc: DbgInfoLoc,
587587 ) error{OutOfMemory}!void {
588588 const dbg_info = &self.dbg_info;
589 const atom = self.getDbgInfoAtom(tag, owner_decl);
589 const atom = getDbgInfoAtom(tag, self.mod, owner_decl);
590590 const name_with_null = name.ptr[0 .. name.len + 1];
591591
592592 switch (loc) {
......@@ -645,7 +645,7 @@ pub const DeclState = struct {
645645 loc: DbgInfoLoc,
646646 ) error{OutOfMemory}!void {
647647 const dbg_info = &self.dbg_info;
648 const atom = self.getDbgInfoAtom(tag, owner_decl);
648 const atom = getDbgInfoAtom(tag, self.mod, owner_decl);
649649 const name_with_null = name.ptr[0 .. name.len + 1];
650650 try dbg_info.append(@enumToInt(AbbrevKind.variable));
651651 const target = self.mod.getTarget();
......@@ -778,16 +778,6 @@ pub const DeclState = struct {
778778 try self.addTypeRelocGlobal(atom, child_ty, @intCast(u32, index));
779779 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
780780 }
781
782 fn getDbgInfoAtom(self: *DeclState, tag: File.Tag, decl_index: Module.Decl.Index) *Atom {
783 const decl = self.mod.declPtr(decl_index);
784 return switch (tag) {
785 .elf => &decl.link.elf.dbg_info_atom,
786 .macho => &decl.link.macho.dbg_info_atom,
787 .wasm => &decl.link.wasm.dbg_info_atom,
788 else => unreachable,
789 };
790 }
791781};
792782
793783pub const AbbrevEntry = struct {
......@@ -899,10 +889,11 @@ pub fn deinit(self: *Dwarf) void {
899889
900890/// Initializes Decl's state and its matching output buffers.
901891/// Call this before `commitDeclState`.
902pub fn initDeclState(self: *Dwarf, mod: *Module, decl: *Module.Decl) !DeclState {
892pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: Module.Decl.Index) !DeclState {
903893 const tracy = trace(@src());
904894 defer tracy.end();
905895
896 const decl = mod.declPtr(decl_index);
906897 const decl_name = try decl.getFullyQualifiedName(mod);
907898 defer self.allocator.free(decl_name);
908899
......@@ -977,12 +968,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl: *Module.Decl) !DeclState
977968 dbg_info_buffer.items.len += 4; // DW.AT.high_pc, DW.FORM.data4
978969 //
979970 if (fn_ret_has_bits) {
980 const atom = switch (self.tag) {
981 .elf => &decl.link.elf.dbg_info_atom,
982 .macho => &decl.link.macho.dbg_info_atom,
983 .wasm => &decl.link.wasm.dbg_info_atom,
984 else => unreachable,
985 };
971 const atom = getDbgInfoAtom(self.tag, mod, decl_index);
986972 try decl_state.addTypeRelocGlobal(atom, fn_ret_type, @intCast(u32, dbg_info_buffer.items.len));
987973 dbg_info_buffer.items.len += 4; // DW.AT.type, DW.FORM.ref4
988974 }
......@@ -1002,7 +988,7 @@ pub fn commitDeclState(
1002988 self: *Dwarf,
1003989 file: *File,
1004990 module: *Module,
1005 decl: *Module.Decl,
991 decl_index: Module.Decl.Index,
1006992 sym_addr: u64,
1007993 sym_size: u64,
1008994 decl_state: *DeclState,
......@@ -1013,6 +999,7 @@ pub fn commitDeclState(
1013999 const gpa = self.allocator;
10141000 var dbg_line_buffer = &decl_state.dbg_line;
10151001 var dbg_info_buffer = &decl_state.dbg_info;
1002 const decl = module.declPtr(decl_index);
10161003
10171004 const target_endian = self.target.cpu.arch.endian();
10181005
......@@ -1233,13 +1220,7 @@ pub fn commitDeclState(
12331220 if (dbg_info_buffer.items.len == 0)
12341221 return;
12351222
1236 const atom = switch (self.tag) {
1237 .elf => &decl.link.elf.dbg_info_atom,
1238 .macho => &decl.link.macho.dbg_info_atom,
1239 .wasm => &decl.link.wasm.dbg_info_atom,
1240 else => unreachable,
1241 };
1242
1223 const atom = getDbgInfoAtom(self.tag, module, decl_index);
12431224 if (decl_state.abbrev_table.items.len > 0) {
12441225 // Now we emit the .debug_info types of the Decl. These will count towards the size of
12451226 // the buffer, so we have to do it before computing the offset, and we can't perform the actual
......@@ -2563,3 +2544,13 @@ fn addDbgInfoErrorSet(
25632544 // DW.AT.enumeration_type delimit children
25642545 try dbg_info_buffer.append(0);
25652546}
2547
2548fn getDbgInfoAtom(tag: File.Tag, mod: *Module, decl_index: Module.Decl.Index) *Atom {
2549 const decl = mod.declPtr(decl_index);
2550 return switch (tag) {
2551 .elf => &decl.link.elf.dbg_info_atom,
2552 .macho => &decl.link.macho.dbg_info_atom,
2553 .wasm => &decl.link.wasm.dbg_info_atom,
2554 else => unreachable,
2555 };
2556}
src/link/Elf.zig+4-4
......@@ -2420,7 +2420,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
24202420 const decl = module.declPtr(decl_index);
24212421 self.freeUnnamedConsts(decl_index);
24222422
2423 var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl) else null;
2423 var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null;
24242424 defer if (decl_state) |*ds| ds.deinit();
24252425
24262426 const res = if (decl_state) |*ds|
......@@ -2443,7 +2443,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
24432443 try self.dwarf.?.commitDeclState(
24442444 &self.base,
24452445 module,
2446 decl,
2446 decl_index,
24472447 local_sym.st_value,
24482448 local_sym.st_size,
24492449 ds,
......@@ -2483,7 +2483,7 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
24832483 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
24842484 defer code_buffer.deinit();
24852485
2486 var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl) else null;
2486 var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(module, decl_index) else null;
24872487 defer if (decl_state) |*ds| ds.deinit();
24882488
24892489 // TODO implement .debug_info for global variables
......@@ -2520,7 +2520,7 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
25202520 try self.dwarf.?.commitDeclState(
25212521 &self.base,
25222522 module,
2523 decl,
2523 decl_index,
25242524 local_sym.st_value,
25252525 local_sym.st_size,
25262526 ds,
src/link/MachO.zig+4-4
......@@ -2190,7 +2190,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
21902190 defer code_buffer.deinit();
21912191
21922192 var decl_state = if (self.d_sym) |*d_sym|
2193 try d_sym.dwarf.initDeclState(module, decl)
2193 try d_sym.dwarf.initDeclState(module, decl_index)
21942194 else
21952195 null;
21962196 defer if (decl_state) |*ds| ds.deinit();
......@@ -2217,7 +2217,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
22172217 try self.d_sym.?.dwarf.commitDeclState(
22182218 &self.base,
22192219 module,
2220 decl,
2220 decl_index,
22212221 addr,
22222222 decl.link.macho.size,
22232223 ds,
......@@ -2330,7 +2330,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
23302330 defer code_buffer.deinit();
23312331
23322332 var decl_state: ?Dwarf.DeclState = if (self.d_sym) |*d_sym|
2333 try d_sym.dwarf.initDeclState(module, decl)
2333 try d_sym.dwarf.initDeclState(module, decl_index)
23342334 else
23352335 null;
23362336 defer if (decl_state) |*ds| ds.deinit();
......@@ -2368,7 +2368,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
23682368 try self.d_sym.?.dwarf.commitDeclState(
23692369 &self.base,
23702370 module,
2371 decl,
2371 decl_index,
23722372 addr,
23732373 decl.link.macho.size,
23742374 ds,
src/link/Wasm.zig+2-2
......@@ -885,7 +885,7 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes
885885
886886 decl.link.wasm.clear();
887887
888 var decl_state: ?Dwarf.DeclState = if (wasm.dwarf) |*dwarf| try dwarf.initDeclState(mod, decl) else null;
888 var decl_state: ?Dwarf.DeclState = if (wasm.dwarf) |*dwarf| try dwarf.initDeclState(mod, decl_index) else null;
889889 defer if (decl_state) |*ds| ds.deinit();
890890
891891 var code_writer = std.ArrayList(u8).init(wasm.base.allocator);
......@@ -913,7 +913,7 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes
913913 try dwarf.commitDeclState(
914914 &wasm.base,
915915 mod,
916 decl,
916 decl_index,
917917 // Actual value will be written after relocation.
918918 // For Wasm, this is the offset relative to the code section
919919 // which isn't known until flush().