diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-27 15:36:00 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-27 15:36:00 +0000 |
| commit | f57079390c546757fc36c296499bea13b7ec162f (patch) | |
| tree | 776d9106ca26b16391d44ecc2eeb397d92d6b3ca /src/main/java | |
| parent | 3b3813cf2df399dd2c396b3c347dd63df1bbb32e (diff) | |
Add LEB128 encoding for padded 64-bit ints
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128Encoding.java | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128Encoding.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128Encoding.java index 87d91520..71076da9 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128Encoding.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128Encoding.java @@ -65,6 +65,15 @@ public class Leb128Encoding { } } + static void encodePaddedUnsignedInt64(long value, BinaryWriter writer) { + for (var byteIndex = 0; byteIndex < 9; byteIndex++) { + var nextByte = value & 0x7f; + writer.writeByte((byte) (nextByte | 0x80)); + value >>>= 7; + } + writer.writeByte((byte) value); + } + static void encodeSignedInt64(long value, BinaryWriter writer) { if (value >= 0) { while (true) { @@ -88,4 +97,17 @@ public class Leb128Encoding { } } } + + static void encodePaddedSignedInt64(long value, BinaryWriter writer) { + if (value > 0) { + encodePaddedUnsignedInt64(value, writer); + } else { + for (var byteIndex = 0; byteIndex < 9; byteIndex++) { + var nextByte = value & 0x7f; + writer.writeByte((byte) (nextByte | 0x80)); + value >>= 7; + } + writer.writeByte((byte) (value & 0x7f)); + } + } } |
