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 {...@@ -586,7 +586,7 @@ pub const DeclState = struct {
586 loc: DbgInfoLoc,586 loc: DbgInfoLoc,
587 ) error{OutOfMemory}!void {587 ) error{OutOfMemory}!void {
588 const dbg_info = &self.dbg_info;588 const dbg_info = &self.dbg_info;
589 const atom = self.getDbgInfoAtom(tag, owner_decl);589 const atom = getDbgInfoAtom(tag, self.mod, owner_decl);
590 const name_with_null = name.ptr[0 .. name.len + 1];590 const name_with_null = name.ptr[0 .. name.len + 1];
591591
592 switch (loc) {592 switch (loc) {
...@@ -645,7 +645,7 @@ pub const DeclState = struct {...@@ -645,7 +645,7 @@ pub const DeclState = struct {
645 loc: DbgInfoLoc,645 loc: DbgInfoLoc,
646 ) error{OutOfMemory}!void {646 ) error{OutOfMemory}!void {
647 const dbg_info = &self.dbg_info;647 const dbg_info = &self.dbg_info;
648 const atom = self.getDbgInfoAtom(tag, owner_decl);648 const atom = getDbgInfoAtom(tag, self.mod, owner_decl);
649 const name_with_null = name.ptr[0 .. name.len + 1];649 const name_with_null = name.ptr[0 .. name.len + 1];
650 try dbg_info.append(@enumToInt(AbbrevKind.variable));650 try dbg_info.append(@enumToInt(AbbrevKind.variable));
651 const target = self.mod.getTarget();651 const target = self.mod.getTarget();
...@@ -778,16 +778,6 @@ pub const DeclState = struct {...@@ -778,16 +778,6 @@ pub const DeclState = struct {
778 try self.addTypeRelocGlobal(atom, child_ty, @intCast(u32, index));778 try self.addTypeRelocGlobal(atom, child_ty, @intCast(u32, index));
779 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string779 dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
780 }780 }
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 }
791};781};
792782
793pub const AbbrevEntry = struct {783pub const AbbrevEntry = struct {
...@@ -899,10 +889,11 @@ pub fn deinit(self: *Dwarf) void {...@@ -899,10 +889,11 @@ pub fn deinit(self: *Dwarf) void {
899889
900/// Initializes Decl's state and its matching output buffers.890/// Initializes Decl's state and its matching output buffers.
901/// Call this before `commitDeclState`.891/// 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 {
903 const tracy = trace(@src());893 const tracy = trace(@src());
904 defer tracy.end();894 defer tracy.end();
905895
896 const decl = mod.declPtr(decl_index);
906 const decl_name = try decl.getFullyQualifiedName(mod);897 const decl_name = try decl.getFullyQualifiedName(mod);
907 defer self.allocator.free(decl_name);898 defer self.allocator.free(decl_name);
908899
...@@ -977,12 +968,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl: *Module.Decl) !DeclState...@@ -977,12 +968,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl: *Module.Decl) !DeclState
977 dbg_info_buffer.items.len += 4; // DW.AT.high_pc, DW.FORM.data4968 dbg_info_buffer.items.len += 4; // DW.AT.high_pc, DW.FORM.data4
978 //969 //
979 if (fn_ret_has_bits) {970 if (fn_ret_has_bits) {
980 const atom = switch (self.tag) {971 const atom = getDbgInfoAtom(self.tag, mod, decl_index);
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 };
986 try decl_state.addTypeRelocGlobal(atom, fn_ret_type, @intCast(u32, dbg_info_buffer.items.len));972 try decl_state.addTypeRelocGlobal(atom, fn_ret_type, @intCast(u32, dbg_info_buffer.items.len));
987 dbg_info_buffer.items.len += 4; // DW.AT.type, DW.FORM.ref4973 dbg_info_buffer.items.len += 4; // DW.AT.type, DW.FORM.ref4
988 }974 }
...@@ -1002,7 +988,7 @@ pub fn commitDeclState(...@@ -1002,7 +988,7 @@ pub fn commitDeclState(
1002 self: *Dwarf,988 self: *Dwarf,
1003 file: *File,989 file: *File,
1004 module: *Module,990 module: *Module,
1005 decl: *Module.Decl,991 decl_index: Module.Decl.Index,
1006 sym_addr: u64,992 sym_addr: u64,
1007 sym_size: u64,993 sym_size: u64,
1008 decl_state: *DeclState,994 decl_state: *DeclState,
...@@ -1013,6 +999,7 @@ pub fn commitDeclState(...@@ -1013,6 +999,7 @@ pub fn commitDeclState(
1013 const gpa = self.allocator;999 const gpa = self.allocator;
1014 var dbg_line_buffer = &decl_state.dbg_line;1000 var dbg_line_buffer = &decl_state.dbg_line;
1015 var dbg_info_buffer = &decl_state.dbg_info;1001 var dbg_info_buffer = &decl_state.dbg_info;
1002 const decl = module.declPtr(decl_index);
10161003
1017 const target_endian = self.target.cpu.arch.endian();1004 const target_endian = self.target.cpu.arch.endian();
10181005
...@@ -1233,13 +1220,7 @@ pub fn commitDeclState(...@@ -1233,13 +1220,7 @@ pub fn commitDeclState(
1233 if (dbg_info_buffer.items.len == 0)1220 if (dbg_info_buffer.items.len == 0)
1234 return;1221 return;
12351222
1236 const atom = switch (self.tag) {1223 const atom = getDbgInfoAtom(self.tag, module, decl_index);
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
1243 if (decl_state.abbrev_table.items.len > 0) {1224 if (decl_state.abbrev_table.items.len > 0) {
1244 // Now we emit the .debug_info types of the Decl. These will count towards the size of1225 // Now we emit the .debug_info types of the Decl. These will count towards the size of
1245 // the buffer, so we have to do it before computing the offset, and we can't perform the actual1226 // 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(...@@ -2563,3 +2544,13 @@ fn addDbgInfoErrorSet(
2563 // DW.AT.enumeration_type delimit children2544 // DW.AT.enumeration_type delimit children
2564 try dbg_info_buffer.append(0);2545 try dbg_info_buffer.append(0);
2565}2546}
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...@@ -2420,7 +2420,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
2420 const decl = module.declPtr(decl_index);2420 const decl = module.declPtr(decl_index);
2421 self.freeUnnamedConsts(decl_index);2421 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;
2424 defer if (decl_state) |*ds| ds.deinit();2424 defer if (decl_state) |*ds| ds.deinit();
24252425
2426 const res = if (decl_state) |*ds|2426 const res = if (decl_state) |*ds|
...@@ -2443,7 +2443,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven...@@ -2443,7 +2443,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
2443 try self.dwarf.?.commitDeclState(2443 try self.dwarf.?.commitDeclState(
2444 &self.base,2444 &self.base,
2445 module,2445 module,
2446 decl,2446 decl_index,
2447 local_sym.st_value,2447 local_sym.st_value,
2448 local_sym.st_size,2448 local_sym.st_size,
2449 ds,2449 ds,
...@@ -2483,7 +2483,7 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v...@@ -2483,7 +2483,7 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
2483 var code_buffer = std.ArrayList(u8).init(self.base.allocator);2483 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
2484 defer code_buffer.deinit();2484 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;
2487 defer if (decl_state) |*ds| ds.deinit();2487 defer if (decl_state) |*ds| ds.deinit();
24882488
2489 // TODO implement .debug_info for global variables2489 // TODO implement .debug_info for global variables
...@@ -2520,7 +2520,7 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v...@@ -2520,7 +2520,7 @@ pub fn updateDecl(self: *Elf, module: *Module, decl_index: Module.Decl.Index) !v
2520 try self.dwarf.?.commitDeclState(2520 try self.dwarf.?.commitDeclState(
2521 &self.base,2521 &self.base,
2522 module,2522 module,
2523 decl,2523 decl_index,
2524 local_sym.st_value,2524 local_sym.st_value,
2525 local_sym.st_size,2525 local_sym.st_size,
2526 ds,2526 ds,
src/link/MachO.zig+4-4
...@@ -2190,7 +2190,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv...@@ -2190,7 +2190,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
2190 defer code_buffer.deinit();2190 defer code_buffer.deinit();
21912191
2192 var decl_state = if (self.d_sym) |*d_sym|2192 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)
2194 else2194 else
2195 null;2195 null;
2196 defer if (decl_state) |*ds| ds.deinit();2196 defer if (decl_state) |*ds| ds.deinit();
...@@ -2217,7 +2217,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv...@@ -2217,7 +2217,7 @@ pub fn updateFunc(self: *MachO, module: *Module, func: *Module.Fn, air: Air, liv
2217 try self.d_sym.?.dwarf.commitDeclState(2217 try self.d_sym.?.dwarf.commitDeclState(
2218 &self.base,2218 &self.base,
2219 module,2219 module,
2220 decl,2220 decl_index,
2221 addr,2221 addr,
2222 decl.link.macho.size,2222 decl.link.macho.size,
2223 ds,2223 ds,
...@@ -2330,7 +2330,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)...@@ -2330,7 +2330,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
2330 defer code_buffer.deinit();2330 defer code_buffer.deinit();
23312331
2332 var decl_state: ?Dwarf.DeclState = if (self.d_sym) |*d_sym|2332 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)
2334 else2334 else
2335 null;2335 null;
2336 defer if (decl_state) |*ds| ds.deinit();2336 defer if (decl_state) |*ds| ds.deinit();
...@@ -2368,7 +2368,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)...@@ -2368,7 +2368,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl_index: Module.Decl.Index)
2368 try self.d_sym.?.dwarf.commitDeclState(2368 try self.d_sym.?.dwarf.commitDeclState(
2369 &self.base,2369 &self.base,
2370 module,2370 module,
2371 decl,2371 decl_index,
2372 addr,2372 addr,
2373 decl.link.macho.size,2373 decl.link.macho.size,
2374 ds,2374 ds,
src/link/Wasm.zig+2-2
...@@ -885,7 +885,7 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes...@@ -885,7 +885,7 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes
885885
886 decl.link.wasm.clear();886 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;
889 defer if (decl_state) |*ds| ds.deinit();889 defer if (decl_state) |*ds| ds.deinit();
890890
891 var code_writer = std.ArrayList(u8).init(wasm.base.allocator);891 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...@@ -913,7 +913,7 @@ pub fn updateFunc(wasm: *Wasm, mod: *Module, func: *Module.Fn, air: Air, livenes
913 try dwarf.commitDeclState(913 try dwarf.commitDeclState(
914 &wasm.base,914 &wasm.base,
915 mod,915 mod,
916 decl,916 decl_index,
917 // Actual value will be written after relocation.917 // Actual value will be written after relocation.
918 // For Wasm, this is the offset relative to the code section918 // For Wasm, this is the offset relative to the code section
919 // which isn't known until flush().919 // which isn't known until flush().