Commit 7d0af639d8

Jakub Konka <kubkon@jakubkonka.com>
2022-12-01 14:32:09
dwarf: update arm and riscv codegens to the new model
1 parent 5ee99f8
Changed files (4)
src
arch
src/arch/aarch64/CodeGen.zig
@@ -195,32 +195,28 @@ const DbgInfoReloc = struct {
         };
 
         switch (function.debug_output) {
-            .dwarf => |dw| {
-                switch (reloc.mcv) {
-                    .register => |reg| {
-                        try dw.genArgDbgInfo(reloc.name, reloc.ty, atom, .{
-                            .register = reg.dwarfLocOp(),
-                        });
-                    },
-
-                    .stack_offset,
-                    .stack_argument_offset,
-                    => |offset| {
-                        const adjusted_offset = switch (reloc.mcv) {
-                            .stack_offset => -@intCast(i32, offset),
-                            .stack_argument_offset => @intCast(i32, function.saved_regs_stack_space + offset),
-                            else => unreachable,
-                        };
-                        try dw.genArgDbgInfo(reloc.name, reloc.ty, atom, .{
-                            .stack = .{
-                                .fp_register = Register.x29.dwarfLocOpDeref(),
-                                .offset = adjusted_offset,
-                            },
-                        });
-                    },
+            .dwarf => |dw| switch (reloc.mcv) {
+                .register => |reg| try dw.genArgDbgInfo(reloc.name, reloc.ty, atom, .{
+                    .register = reg.dwarfLocOp(),
+                }),
+
+                .stack_offset,
+                .stack_argument_offset,
+                => |offset| {
+                    const adjusted_offset = switch (reloc.mcv) {
+                        .stack_offset => -@intCast(i32, offset),
+                        .stack_argument_offset => @intCast(i32, function.saved_regs_stack_space + offset),
+                        else => unreachable,
+                    };
+                    try dw.genArgDbgInfo(reloc.name, reloc.ty, atom, .{
+                        .stack = .{
+                            .fp_register = Register.x29.dwarfLocOpDeref(),
+                            .offset = adjusted_offset,
+                        },
+                    });
+                },
 
-                    else => unreachable, // not a possible argument
-                }
+                else => unreachable, // not a possible argument
             },
             .plan9 => {},
             .none => {},
src/arch/arm/CodeGen.zig
@@ -4029,86 +4029,43 @@ fn genInlineMemsetCode(
     // end:
 }
 
-/// Adds a Type to the .debug_info at the current position. The bytes will be populated later,
-/// after codegen for this symbol is done.
-fn addDbgInfoTypeReloc(self: *Self, ty: Type) error{OutOfMemory}!void {
-    switch (self.debug_output) {
-        .dwarf => |dw| {
-            assert(ty.hasRuntimeBits());
-            const dbg_info = &dw.dbg_info;
-            const index = dbg_info.items.len;
-            try dbg_info.resize(index + 4); // DW.AT.type,  DW.FORM.ref4
-            const mod = self.bin_file.options.module.?;
-            const atom = switch (self.bin_file.tag) {
-                .elf => &mod.declPtr(self.mod_fn.owner_decl).link.elf.dbg_info_atom,
-                .macho => unreachable,
-                else => unreachable,
-            };
-            try dw.addTypeRelocGlobal(atom, ty, @intCast(u32, index));
-        },
-        .plan9 => {},
-        .none => {},
-    }
-}
-
 fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, arg_index: u32) error{OutOfMemory}!void {
     const mcv = self.args[arg_index];
     const ty = self.air.instructions.items(.data)[inst].ty;
     const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
-    const name_with_null = name.ptr[0 .. name.len + 1];
 
-    switch (mcv) {
-        .register => |reg| {
-            switch (self.debug_output) {
-                .dwarf => |dw| {
-                    const dbg_info = &dw.dbg_info;
-                    try dbg_info.ensureUnusedCapacity(3);
-                    dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
-                    dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
-                        1, // ULEB128 dwarf expression length
-                        reg.dwarfLocOp(),
-                    });
-                    try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
-                    try self.addDbgInfoTypeReloc(ty); // DW.AT.type,  DW.FORM.ref4
-                    dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
-                },
-                .plan9 => {},
-                .none => {},
-            }
-        },
-        .stack_offset,
-        .stack_argument_offset,
-        => {
-            switch (self.debug_output) {
-                .dwarf => |dw| {
-                    const adjusted_stack_offset = switch (mcv) {
-                        .stack_offset => |offset| -@intCast(i32, offset),
-                        .stack_argument_offset => |offset| @intCast(i32, self.saved_regs_stack_space + offset),
-                        else => unreachable,
-                    };
-
-                    const dbg_info = &dw.dbg_info;
-                    try dbg_info.append(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
-
-                    // Get length of the LEB128 stack offset
-                    var counting_writer = std.io.countingWriter(std.io.null_writer);
-                    leb128.writeILEB128(counting_writer.writer(), adjusted_stack_offset) catch unreachable;
-
-                    // DW.AT.location, DW.FORM.exprloc
-                    // ULEB128 dwarf expression length
-                    try leb128.writeULEB128(dbg_info.writer(), counting_writer.bytes_written + 1);
-                    try dbg_info.append(DW.OP.breg11);
-                    try leb128.writeILEB128(dbg_info.writer(), adjusted_stack_offset);
+    const mod = self.bin_file.options.module.?;
+    const fn_owner_decl = mod.declPtr(self.mod_fn.owner_decl);
+    const atom = switch (self.bin_file.tag) {
+        .elf => &fn_owner_decl.link.elf.dbg_info_atom,
+        .macho => &fn_owner_decl.link.macho.dbg_info_atom,
+        else => unreachable,
+    };
 
-                    try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
-                    try self.addDbgInfoTypeReloc(ty); // DW.AT.type,  DW.FORM.ref4
-                    dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
-                },
-                .plan9 => {},
-                .none => {},
-            }
+    switch (self.debug_output) {
+        .dwarf => |dw| switch (mcv) {
+            .register => |reg| try dw.genArgDbgInfo(name, ty, atom, .{
+                .register = reg.dwarfLocOp(),
+            }),
+            .stack_offset,
+            .stack_argument_offset,
+            => {
+                const adjusted_stack_offset = switch (mcv) {
+                    .stack_offset => |offset| -@intCast(i32, offset),
+                    .stack_argument_offset => |offset| @intCast(i32, self.saved_regs_stack_space + offset),
+                    else => unreachable,
+                };
+                try dw.genArgDbgInfo(name, ty, atom, .{
+                    .stack = .{
+                        .fp_register = DW.OP.breg11,
+                        .offset = adjusted_stack_offset,
+                    },
+                });
+            },
+            else => unreachable, // not a possible argument
         },
-        else => unreachable, // not a possible argument
+        .plan9 => {},
+        .none => {},
     }
 }
 
src/arch/riscv64/CodeGen.zig
@@ -772,28 +772,6 @@ fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void {
     try table.ensureUnusedCapacity(self.gpa, additional_count);
 }
 
-/// Adds a Type to the .debug_info at the current position. The bytes will be populated later,
-/// after codegen for this symbol is done.
-fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void {
-    switch (self.debug_output) {
-        .dwarf => |dw| {
-            assert(ty.hasRuntimeBits());
-            const dbg_info = &dw.dbg_info;
-            const index = dbg_info.items.len;
-            try dbg_info.resize(index + 4); // DW.AT.type,  DW.FORM.ref4
-            const mod = self.bin_file.options.module.?;
-            const atom = switch (self.bin_file.tag) {
-                .elf => &mod.declPtr(self.mod_fn.owner_decl).link.elf.dbg_info_atom,
-                .macho => unreachable,
-                else => unreachable,
-            };
-            try dw.addTypeRelocGlobal(atom, ty, @intCast(u32, index));
-        },
-        .plan9 => {},
-        .none => {},
-    }
-}
-
 fn allocMem(self: *Self, inst: Air.Inst.Index, abi_size: u32, abi_align: u32) !u32 {
     if (abi_align > self.stack_align)
         self.stack_align = abi_align;
@@ -1627,36 +1605,25 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void {
 fn genArgDbgInfo(self: *Self, inst: Air.Inst.Index, mcv: MCValue, arg_index: u32) !void {
     const ty = self.air.instructions.items(.data)[inst].ty;
     const name = self.mod_fn.getParamName(self.bin_file.options.module.?, arg_index);
-    const name_with_null = name.ptr[0 .. name.len + 1];
 
-    switch (mcv) {
-        .register => |reg| {
-            switch (self.debug_output) {
-                .dwarf => |dw| {
-                    const dbg_info = &dw.dbg_info;
-                    try dbg_info.ensureUnusedCapacity(3);
-                    dbg_info.appendAssumeCapacity(@enumToInt(link.File.Dwarf.AbbrevKind.parameter));
-                    dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc
-                        1, // ULEB128 dwarf expression length
-                        reg.dwarfLocOp(),
-                    });
-                    try dbg_info.ensureUnusedCapacity(5 + name_with_null.len);
-                    try self.addDbgInfoTypeReloc(ty); // DW.AT.type,  DW.FORM.ref4
-                    dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT.name, DW.FORM.string
-                },
-                .plan9 => {},
-                .none => {},
-            }
-        },
-        .stack_offset => |offset| {
-            _ = offset;
-            switch (self.debug_output) {
-                .dwarf => {},
-                .plan9 => {},
-                .none => {},
-            }
+    const mod = self.bin_file.options.module.?;
+    const fn_owner_decl = mod.declPtr(self.mod_fn.owner_decl);
+    const atom = switch (self.bin_file.tag) {
+        .elf => &fn_owner_decl.link.elf.dbg_info_atom,
+        .macho => &fn_owner_decl.link.macho.dbg_info_atom,
+        else => unreachable,
+    };
+
+    switch (self.debug_output) {
+        .dwarf => |dw| switch (mcv) {
+            .register => |reg| try dw.genArgDbgInfo(name, ty, atom, .{
+                .register = reg.dwarfLocOp(),
+            }),
+            .stack_offset => {},
+            else => {},
         },
-        else => {},
+        .plan9 => {},
+        .none => {},
     }
 }
 
src/arch/x86_64/CodeGen.zig
@@ -3827,21 +3827,18 @@ fn genArgDbgInfo(self: Self, ty: Type, name: [:0]const u8, mcv: MCValue) !void {
     };
 
     switch (self.debug_output) {
-        .dwarf => |dw| {
-            switch (mcv) {
-                .register => |reg| try dw.genArgDbgInfo(name, ty, atom, .{
-                    .register = reg.dwarfLocOp(),
-                }),
-
-                .stack_offset => |off| try dw.genArgDbgInfo(name, ty, atom, .{
-                    .stack = .{
-                        .fp_register = Register.rbp.dwarfLocOpDeref(), // TODO handle -fomit-frame-pointer
-                        .offset = -off,
-                    },
-                }),
+        .dwarf => |dw| switch (mcv) {
+            .register => |reg| try dw.genArgDbgInfo(name, ty, atom, .{
+                .register = reg.dwarfLocOp(),
+            }),
+            .stack_offset => |off| try dw.genArgDbgInfo(name, ty, atom, .{
+                .stack = .{
+                    .fp_register = Register.rbp.dwarfLocOpDeref(), // TODO handle -fomit-frame-pointer
+                    .offset = -off,
+                },
+            }),
 
-                else => unreachable, // not a valid function parameter
-            }
+            else => unreachable, // not a valid function parameter
         },
         .plan9 => {},
         .none => {},