Skip to content

Commit f697e0a

Browse files
committed
plan9 linker: link lineinfo and filenames
1 parent 84ab03a commit f697e0a

File tree

3 files changed

+312
-70
lines changed

3 files changed

+312
-70
lines changed

src/codegen.zig

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,23 @@ pub const DebugInfoOutput = union(enum) {
5252
/// assume all numbers/variables are bytes
5353
/// 0 w x y z -> interpret w x y z as a big-endian i32, and add it to the line offset
5454
/// x when x < 65 -> add x to line offset
55-
/// x when x < 129 -> subtract 64 from x and add it to the line offset
55+
/// x when x < 129 -> subtract 64 from x and subtract it from the line offset
5656
/// x -> subtract 129 from x, multiply it by the quanta of the instruction size
5757
/// (1 on x86_64), and add it to the pc
5858
/// after every opcode, add the quanta of the instruction size to the pc
5959
plan9: struct {
6060
/// the actual opcodes
6161
dbg_line: *std.ArrayList(u8),
62+
/// what line the debuginfo starts on
63+
/// this helps because the linker might have to insert some opcodes to make sure that the line count starts at the right amount for the next decl
64+
start_line: *?u32,
6265
/// what the line count ends on after codegen
6366
/// this helps because the linker might have to insert some opcodes to make sure that the line count starts at the right amount for the next decl
6467
end_line: *u32,
68+
/// the last pc change op
69+
/// This is very useful for adding quanta
70+
/// to it if its not actually the last one.
71+
pcop_change_index: *?u32,
6572
},
6673
none,
6774
};
@@ -946,7 +953,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
946953

947954
fn dbgAdvancePCAndLine(self: *Self, line: u32, column: u32) InnerError!void {
948955
const delta_line = @intCast(i32, line) - @intCast(i32, self.prev_di_line);
949-
const delta_pc = self.code.items.len - self.prev_di_pc;
956+
const delta_pc: usize = self.code.items.len - self.prev_di_pc;
950957
switch (self.debug_output) {
951958
.dwarf => |dbg_out| {
952959
// TODO Look into using the DWARF special opcodes to compress this data.
@@ -960,30 +967,39 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
960967
leb128.writeILEB128(dbg_out.dbg_line.writer(), delta_line) catch unreachable;
961968
}
962969
dbg_out.dbg_line.appendAssumeCapacity(DW.LNS.copy);
970+
self.prev_di_pc = self.code.items.len;
971+
self.prev_di_line = line;
972+
self.prev_di_column = column;
973+
self.prev_di_pc = self.code.items.len;
963974
},
964975
.plan9 => |dbg_out| {
976+
if (delta_pc <= 0) return; // only do this when the pc changes
965977
// we have already checked the target in the linker to make sure it is compatable
966978
const quant = @import("link/Plan9/aout.zig").getPCQuant(self.target.cpu.arch) catch unreachable;
967979

968980
// increasing the line number
969-
if (delta_line > 0 and delta_line < 65) {
970-
try dbg_out.dbg_line.append(@intCast(u8, delta_line));
971-
} else if (delta_line < 0 and delta_line > -65) {
972-
try dbg_out.dbg_line.append(@intCast(u8, -delta_line + 64));
973-
} else if (delta_line != 0) {
974-
try dbg_out.dbg_line.writer().writeIntBig(i32, delta_line);
975-
}
981+
try @import("link/Plan9.zig").changeLine(dbg_out.dbg_line, delta_line);
976982
// increasing the pc
977-
if (delta_pc - quant != 0) {
978-
try dbg_out.dbg_line.append(@intCast(u8, delta_pc - quant + 129));
979-
}
983+
const d_pc_p9 = @intCast(i64, delta_pc) - quant;
984+
if (d_pc_p9 > 0) {
985+
// minus one becaue if its the last one, we want to leave space to change the line which is one quanta
986+
try dbg_out.dbg_line.append(@intCast(u8, @divExact(d_pc_p9, quant) + 128) - quant);
987+
if (dbg_out.pcop_change_index.*) |pci|
988+
dbg_out.dbg_line.items[pci] += 1;
989+
dbg_out.pcop_change_index.* = @intCast(u32, dbg_out.dbg_line.items.len - 1);
990+
} else if (d_pc_p9 == 0) {
991+
// we don't need to do anything, because adding the quant does it for us
992+
} else unreachable;
993+
if (dbg_out.start_line.* == null)
994+
dbg_out.start_line.* = self.prev_di_line;
980995
dbg_out.end_line.* = line;
996+
// only do this if the pc changed
997+
self.prev_di_line = line;
998+
self.prev_di_column = column;
999+
self.prev_di_pc = self.code.items.len;
9811000
},
9821001
.none => {},
9831002
}
984-
self.prev_di_line = line;
985-
self.prev_di_column = column;
986-
self.prev_di_pc = self.code.items.len;
9871003
}
9881004

9891005
/// Asserts there is already capacity to insert into top branch inst_table.

0 commit comments

Comments
 (0)