aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-21 20:59:37 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-21 20:59:37 +0100
commitb92d46a9fc641afca93571e1b398b968816b1b57 (patch)
tree63e8f70e5a06b632e2e6de7bbb982fabc0820c5e /src
parent46471000c1851940777f6dce77fa9a4bc2fe877c (diff)
Add LEB128 encoding
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/BinaryWriter.java57
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/Leb128Encoding.java65
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/Leb128EncodingTests.java82
3 files changed, 204 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/BinaryWriter.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/BinaryWriter.java
new file mode 100644
index 00000000..d5600a5c
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/BinaryWriter.java
@@ -0,0 +1,57 @@
+package org.shedlang.compiler.backends.wasm.structure;
+
+import java.nio.ByteBuffer;
+
+public class BinaryWriter {
+ private ByteBuffer buffer = ByteBuffer.allocate(1024);
+
+ public void writeByte(byte value) {
+ ensureSpareCapacity(1);
+ buffer.put(value);
+ }
+
+ public void writeBytes(byte[] bytes) {
+ ensureSpareCapacity(bytes.length);
+ buffer.put(bytes, 0, bytes.length);
+ }
+
+ public ReservedBytes reserve(int length) {
+ var position = buffer.position();
+ advance(length);
+ return new ReservedBytes(position);
+ }
+
+ private void ensureSpareCapacity(int length) {
+ ensureCapacity(buffer.position() + length);
+ }
+
+ private void ensureCapacity(int minCapacity) {
+ if (minCapacity > buffer.capacity()) {
+ var newCapacity = buffer.capacity() * 2;
+ while (minCapacity > newCapacity) {
+ newCapacity *= 2;
+ }
+ var newBuffer = ByteBuffer.allocate(newCapacity);
+ buffer.flip();
+ newBuffer.put(buffer);
+ buffer = newBuffer;
+ }
+ }
+
+ private void advance(int length) {
+ ensureSpareCapacity(length);
+ buffer.position(buffer.position() + length);
+ }
+
+ public byte[] toBytes() {
+ var result = new byte[buffer.position()];
+ buffer.get(0, result);
+ return result;
+ }
+
+ public static class ReservedBytes {
+ public ReservedBytes(int position) {
+
+ }
+ }
+}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/Leb128Encoding.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/Leb128Encoding.java
new file mode 100644
index 00000000..bcd7de5f
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/Leb128Encoding.java
@@ -0,0 +1,65 @@
+package org.shedlang.compiler.backends.wasm.structure;
+
+public class Leb128Encoding {
+ private Leb128Encoding() {
+ }
+
+ static void encodeUnsignedInt32(int value, BinaryWriter writer) {
+ while (true) {
+ var nextByte = value & 0x7f;
+ value >>>= 7;
+ if (value == 0) {
+ writer.writeByte((byte) nextByte);
+ return;
+ } else {
+ writer.writeByte((byte) (nextByte | 0x80));
+ }
+ }
+ }
+
+ static void encodePaddedUnsignedInt32(int value, BinaryWriter writer) {
+ for (var byteIndex = 0; byteIndex < 4; byteIndex++) {
+ var nextByte = value & 0x7f;
+ writer.writeByte((byte) (nextByte | 0x80));
+ value >>>= 7;
+ }
+ writer.writeByte((byte) value);
+ }
+
+ static void encodeSignedInt32(int value, BinaryWriter writer) {
+ if (value >= 0) {
+ while (true) {
+ if ((value >> 6) == 0) {
+ writer.writeByte((byte) value);
+ return;
+ } else {
+ writer.writeByte((byte) ((value & 0x7f) | 0x80));
+ value >>= 7;
+ }
+ }
+ } else {
+ while (true) {
+ if ((value >> 6) == -1) {
+ writer.writeByte((byte) (value & 0x7f));
+ return;
+ } else {
+ writer.writeByte((byte) ((value & 0x7f) | 0x80));
+ value >>= 7;
+ }
+ }
+ }
+ }
+
+ static void encodePaddedSignedInt32(int value, BinaryWriter writer) {
+ if (value > 0) {
+ encodePaddedUnsignedInt32(value, writer);
+ } else {
+ for (var byteIndex = 0; byteIndex < 4; byteIndex++) {
+ var nextByte = value & 0x7f;
+ writer.writeByte((byte) (nextByte | 0x80));
+ value >>= 7;
+ }
+ writer.writeByte((byte) (value & 0x7f));
+ }
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/Leb128EncodingTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/Leb128EncodingTests.java
new file mode 100644
index 00000000..de7f39e9
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/Leb128EncodingTests.java
@@ -0,0 +1,82 @@
+package org.shedlang.compiler.backends.wasm.structure;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.HexFormat;
+
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class Leb128EncodingTests {
+ @Test
+ public void unsignedEncoding() {
+ assertUnsignedInt32Encoding(0x0, "00");
+ assertUnsignedInt32Encoding(0x1, "01");
+ assertUnsignedInt32Encoding(0x7F, "7f");
+ assertUnsignedInt32Encoding(0x80, "8001");
+ assertUnsignedInt32Encoding(0xffffffff, "ffffffff0f");
+ }
+
+ private void assertUnsignedInt32Encoding(int value, String expectedHex) {
+ var writer = new BinaryWriter();
+ Leb128Encoding.encodeUnsignedInt32(value, writer);
+ assertByteArray(writer.toBytes(), expectedHex);
+ }
+
+ @Test
+ public void paddedUnsignedEncoding() {
+ assertPaddedUnsignedInt32Encoding(0x0, "8080808000");
+ assertPaddedUnsignedInt32Encoding(0x1, "8180808000");
+ assertPaddedUnsignedInt32Encoding(0x7F, "ff80808000");
+ assertPaddedUnsignedInt32Encoding(0x80, "8081808000");
+ assertPaddedUnsignedInt32Encoding(0xffffffff, "ffffffff0f");
+ }
+
+ private void assertPaddedUnsignedInt32Encoding(int value, String expectedHex) {
+ var writer = new BinaryWriter();
+ Leb128Encoding.encodePaddedUnsignedInt32(value, writer);
+ assertByteArray(writer.toBytes(), expectedHex);
+ }
+
+ @Test
+ public void signedEncoding() {
+ assertSignedInt32Encoding(0x0, "00");
+ assertSignedInt32Encoding(0x1, "01");
+ assertSignedInt32Encoding(0x3F, "3f");
+ assertSignedInt32Encoding(0x40, "c000");
+ assertSignedInt32Encoding(0x7F, "ff00");
+ assertSignedInt32Encoding(0x80, "8001");
+ assertSignedInt32Encoding(-0x1, "7f");
+ assertSignedInt32Encoding(-0x40, "40");
+ assertSignedInt32Encoding(-0x41, "bf7f");
+ }
+
+ private void assertSignedInt32Encoding(int value, String expectedHex) {
+ var writer = new BinaryWriter();
+ Leb128Encoding.encodeSignedInt32(value, writer);
+ assertByteArray(writer.toBytes(), expectedHex);
+ }
+
+ @Test
+ public void paddedSignedEncoding() {
+ assertPaddedSignedInt32Encoding(0x0, "8080808000");
+ assertPaddedSignedInt32Encoding(0x1, "8180808000");
+ assertPaddedSignedInt32Encoding(0x3F, "bf80808000");
+ assertPaddedSignedInt32Encoding(0x40, "c080808000");
+ assertPaddedSignedInt32Encoding(0x7F, "ff80808000");
+ assertPaddedSignedInt32Encoding(0x80, "8081808000");
+ assertPaddedSignedInt32Encoding(-0x1, "ffffffff7f");
+ assertPaddedSignedInt32Encoding(-0x40, "c0ffffff7f");
+ assertPaddedSignedInt32Encoding(-0x41, "bfffffff7f");
+ }
+
+ private void assertPaddedSignedInt32Encoding(int value, String expectedHex) {
+ var writer = new BinaryWriter();
+ Leb128Encoding.encodePaddedSignedInt32(value, writer);
+ assertByteArray(writer.toBytes(), expectedHex);
+ }
+
+ private void assertByteArray(byte[] actual, String expectedHex) {
+ assertThat(HexFormat.of().formatHex(actual), equalTo(expectedHex));
+ }
+}