diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2023-11-11 16:06:01 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-11-12 03:21:52 -0500 |
| commit | 2eeb7358227d13ff4d77ef73c54a0e2ae12c1d58 (patch) | |
| tree | 94660b851f8734a6b9520e6754937bb30511e648 /lib/std/leb128.zig | |
| parent | 3fc6a2f11399e84b9cfa4cfef65ef40aa6de173b (diff) | |
| download | zig-2eeb7358227d13ff4d77ef73c54a0e2ae12c1d58.tar.gz zig-2eeb7358227d13ff4d77ef73c54a0e2ae12c1d58.zip | |
Dwarf: improve x86_64 backend debug info
Closes #17811
Diffstat (limited to 'lib/std/leb128.zig')
| -rw-r--r-- | lib/std/leb128.zig | 63 |
1 files changed, 61 insertions, 2 deletions
diff --git a/lib/std/leb128.zig b/lib/std/leb128.zig index 474279afb3..f649dd6519 100644 --- a/lib/std/leb128.zig +++ b/lib/std/leb128.zig @@ -148,10 +148,10 @@ pub fn writeUnsignedFixed(comptime l: usize, ptr: *[l]u8, int: std.meta.Int(.uns value >>= 7; ptr[i] = byte; } - ptr[i] = @as(u8, @truncate(value)); + ptr[i] = @truncate(value); } -test "writeUnsignedFixed" { +test writeUnsignedFixed { { var buf: [4]u8 = undefined; writeUnsignedFixed(4, &buf, 0); @@ -174,6 +174,65 @@ test "writeUnsignedFixed" { } } +/// This is an "advanced" function. It allows one to use a fixed amount of memory to store an +/// ILEB128. This defeats the entire purpose of using this data encoding; it will no longer use +/// fewer bytes to store smaller numbers. The advantage of using a fixed width is that it makes +/// fields have a predictable size and so depending on the use case this tradeoff can be worthwhile. +/// An example use case of this is in emitting DWARF info where one wants to make a ILEB128 field +/// "relocatable", meaning that it becomes possible to later go back and patch the number to be a +/// different value without shifting all the following code. +pub fn writeSignedFixed(comptime l: usize, ptr: *[l]u8, int: std.meta.Int(.signed, l * 7)) void { + const T = @TypeOf(int); + const U = if (@typeInfo(T).Int.bits < 8) u8 else T; + var value: U = @intCast(int); + + comptime var i = 0; + inline while (i < (l - 1)) : (i += 1) { + const byte: u8 = @bitCast(@as(i8, @truncate(value)) | -0b1000_0000); + value >>= 7; + ptr[i] = byte; + } + ptr[i] = @as(u7, @bitCast(@as(i7, @truncate(value)))); +} + +test writeSignedFixed { + { + var buf: [4]u8 = undefined; + writeSignedFixed(4, &buf, 0); + try testing.expect((try test_read_ileb128(i64, &buf)) == 0); + } + { + var buf: [4]u8 = undefined; + writeSignedFixed(4, &buf, 1); + try testing.expect((try test_read_ileb128(i64, &buf)) == 1); + } + { + var buf: [4]u8 = undefined; + writeSignedFixed(4, &buf, -1); + try testing.expect((try test_read_ileb128(i64, &buf)) == -1); + } + { + var buf: [4]u8 = undefined; + writeSignedFixed(4, &buf, 1000); + try testing.expect((try test_read_ileb128(i64, &buf)) == 1000); + } + { + var buf: [4]u8 = undefined; + writeSignedFixed(4, &buf, -1000); + try testing.expect((try test_read_ileb128(i64, &buf)) == -1000); + } + { + var buf: [4]u8 = undefined; + writeSignedFixed(4, &buf, -10000000); + try testing.expect((try test_read_ileb128(i64, &buf)) == -10000000); + } + { + var buf: [4]u8 = undefined; + writeSignedFixed(4, &buf, 10000000); + try testing.expect((try test_read_ileb128(i64, &buf)) == 10000000); + } +} + // tests fn test_read_stream_ileb128(comptime T: type, encoded: []const u8) !T { var reader = std.io.fixedBufferStream(encoded); |
