From f57079390c546757fc36c296499bea13b7ec162f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 27 Dec 2023 15:36:00 +0000 Subject: Add LEB128 encoding for padded 64-bit ints --- .../wasm/structure/binary/Leb128Encoding.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/main/java') 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)); + } + } } -- cgit v1.2.3