Dwarf: port to new Writer API

This commit is contained in:
Jacob Young 2025-08-08 18:47:28 -04:00 committed by Andrew Kelley
parent 38dfa6537e
commit 60f8584927
2 changed files with 854 additions and 627 deletions

View file

@ -359,9 +359,12 @@ pub fn writableSlice(w: *Writer, len: usize) Error![]u8 {
/// ///
/// If `minimum_length` is zero, this is equivalent to `unusedCapacitySlice`. /// If `minimum_length` is zero, this is equivalent to `unusedCapacitySlice`.
pub fn writableSliceGreedy(w: *Writer, minimum_length: usize) Error![]u8 { pub fn writableSliceGreedy(w: *Writer, minimum_length: usize) Error![]u8 {
assert(w.buffer.len >= minimum_length);
while (w.buffer.len - w.end < minimum_length) { while (w.buffer.len - w.end < minimum_length) {
assert(0 == try w.vtable.drain(w, &.{""}, 1)); assert(0 == try w.vtable.drain(w, &.{""}, 1));
// If the loop condition was false this assertion would have passed
// anyway. Otherwise, give the implementation a chance to grow the
// buffer before asserting on the buffer length.
assert(w.buffer.len >= minimum_length);
} else { } else {
@branchHint(.likely); @branchHint(.likely);
return w.buffer[w.end..]; return w.buffer[w.end..];
@ -1847,28 +1850,21 @@ pub fn writeLeb128(w: *Writer, value: anytype) Error!void {
const value_info = @typeInfo(@TypeOf(value)).int; const value_info = @typeInfo(@TypeOf(value)).int;
try w.writeMultipleOf7Leb128(@as(@Type(.{ .int = .{ try w.writeMultipleOf7Leb128(@as(@Type(.{ .int = .{
.signedness = value_info.signedness, .signedness = value_info.signedness,
.bits = std.mem.alignForwardAnyAlign(u16, value_info.bits, 7), .bits = @max(std.mem.alignForwardAnyAlign(u16, value_info.bits, 7), 7),
} }), value)); } }), value));
} }
fn writeMultipleOf7Leb128(w: *Writer, value: anytype) Error!void { fn writeMultipleOf7Leb128(w: *Writer, value: anytype) Error!void {
const value_info = @typeInfo(@TypeOf(value)).int; const value_info = @typeInfo(@TypeOf(value)).int;
comptime assert(value_info.bits % 7 == 0); const Byte = packed struct(u8) { bits: u7, more: bool };
var bytes: [@divExact(value_info.bits, 7)]Byte = undefined;
var remaining = value; var remaining = value;
while (true) { for (&bytes, 1..) |*byte, len| {
const buffer: []packed struct(u8) { bits: u7, more: bool } = @ptrCast(try w.writableSliceGreedy(1));
for (buffer, 1..) |*byte, len| {
const more = switch (value_info.signedness) { const more = switch (value_info.signedness) {
.signed => remaining >> 6 != remaining >> (value_info.bits - 1), .signed => remaining >> 6 != remaining >> (value_info.bits - 1),
.unsigned => remaining > std.math.maxInt(u7), .unsigned => remaining > std.math.maxInt(u7),
}; };
byte.* = if (@inComptime()) @typeInfo(@TypeOf(buffer)).pointer.child{ byte.* = .{
.bits = @bitCast(@as(@Type(.{ .int = .{
.signedness = value_info.signedness,
.bits = 7,
} }), @truncate(remaining))),
.more = more,
} else .{
.bits = @bitCast(@as(@Type(.{ .int = .{ .bits = @bitCast(@as(@Type(.{ .int = .{
.signedness = value_info.signedness, .signedness = value_info.signedness,
.bits = 7, .bits = 7,
@ -1876,10 +1872,8 @@ fn writeMultipleOf7Leb128(w: *Writer, value: anytype) Error!void {
.more = more, .more = more,
}; };
if (value_info.bits > 7) remaining >>= 7; if (value_info.bits > 7) remaining >>= 7;
if (!more) return w.advance(len); if (!more) return w.writeAll(@ptrCast(bytes[0..len]));
} } else unreachable;
w.advance(buffer.len);
}
} }
test "printValue max_depth" { test "printValue max_depth" {

File diff suppressed because it is too large Load diff