diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-21 23:52:44 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-21 23:52:44 +0100 |
| commit | 4f8c81e479b5e83bc301f04f01a12466f9e1edf2 (patch) | |
| tree | 4e521f2b12b70e4cf8f57cf5e71a9773cebf1791 /src/main/java | |
| parent | 0984cad6e314800cf1bc3fe604e1e48f0ed85ed7 (diff) | |
Support table imports
Diffstat (limited to 'src/main/java')
5 files changed, 55 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java index 2ac60424..430e1ab4 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormat.java @@ -4,8 +4,7 @@ import org.pcollections.PVector; import org.shedlang.compiler.backends.wasm.structure.ast.WasmModule; import org.shedlang.compiler.backends.wasm.structure.ast.imports.WasmImport; import org.shedlang.compiler.backends.wasm.structure.ast.imports.WasmImportDescriptor; -import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; -import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; +import org.shedlang.compiler.backends.wasm.structure.ast.types.*; import java.io.IOException; import java.io.OutputStream; @@ -102,6 +101,10 @@ public class WasmBinaryFormat { var typeIndex = funcTypeToTypeIndex.get(func.type()); writeTypeIndex(typeIndex, writer); } + case WasmImportDescriptor.Table table -> { + writer.writeByte(0x01); + writeTableType(table.type(), writer); + } } } @@ -119,6 +122,7 @@ public class WasmBinaryFormat { var importedFunctions = module.imports().stream() .flatMap(import_ -> switch (import_.descriptor()) { case WasmImportDescriptor.Func func -> Stream.of(func); + default -> Stream.empty(); }) .toList(); @@ -147,6 +151,19 @@ public class WasmBinaryFormat { writeValueTypeVector(type.results(), writer); } + private static void writeReferenceType(WasmReferenceType type, BinaryWriter writer) { + var encoding = switch (type) { + case FUNC_REF -> 0x70; + case EXTERN_REF -> 0x6f; + }; + writer.writeByte(encoding); + } + + private static void writeTableType(WasmTableType type, BinaryWriter writer) { + writeReferenceType(type.elementType(), writer); + writeLimits(type.limits(), writer); + } + private static void writeValueTypeVector(List<WasmValueType> types, BinaryWriter writer) throws IOException { writeVec( types, @@ -165,6 +182,17 @@ public class WasmBinaryFormat { writer.writeByte((byte) encoding); } + private static void writeLimits(WasmLimits limits, BinaryWriter writer) { + if (limits.max().isPresent()) { + writer.writeByte(0x01); + Leb128Encoding.encodeUnsignedInt32(limits.min(), writer); + Leb128Encoding.encodeUnsignedInt32(limits.max().getAsInt(), writer); + } else { + writer.writeByte(0x00); + Leb128Encoding.encodeUnsignedInt32(limits.min(), writer); + } + } + interface Write { void write() throws IOException; } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java index 7b095863..db0ec936 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImportDescriptor.java @@ -1,7 +1,9 @@ package org.shedlang.compiler.backends.wasm.structure.ast.imports; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmTableType; public sealed interface WasmImportDescriptor { record Func(String identifier, WasmFuncType type) implements WasmImportDescriptor {} + record Table(WasmTableType type) implements WasmImportDescriptor {} } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmLimits.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmLimits.java new file mode 100644 index 00000000..722299b9 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmLimits.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.types; + +import java.util.OptionalInt; + +public record WasmLimits(int min, OptionalInt max) { + public static WasmLimits min(int min) { + return new WasmLimits(min, OptionalInt.empty()); + } + + public static WasmLimits range(int min, int max) { + return new WasmLimits(min, OptionalInt.of(max)); + } +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmReferenceType.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmReferenceType.java new file mode 100644 index 00000000..0bcc5ae2 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmReferenceType.java @@ -0,0 +1,6 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.types; + +public enum WasmReferenceType { + FUNC_REF, + EXTERN_REF, +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmTableType.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmTableType.java new file mode 100644 index 00000000..fc609808 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/types/WasmTableType.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.types; + +public record WasmTableType(WasmReferenceType elementType, WasmLimits limits) { +} |
