diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-24 19:45:54 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-24 19:45:54 +0100 |
| commit | 4ea7fb648b0c6f95e6383c34912aad411a43cdbf (patch) | |
| tree | 5b0a30c00773c528bb6ff861fdf16d6e5b34426f /src/main/java | |
| parent | 103534c8966b12fcb2f415487bd3b30d569ba09f (diff) | |
WASM binary format: support tables
Diffstat (limited to 'src/main/java')
7 files changed, 50 insertions, 10 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 761c9ea5..ed10ef2d 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 @@ -1,6 +1,6 @@ package org.shedlang.compiler.backends.wasm.structure; -import org.shedlang.compiler.backends.wasm.structure.ast.WasmModule; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; import org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules; import java.io.IOException; diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImport.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmImport.java index bc576758..1e38505e 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/imports/WasmImport.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmImport.java @@ -1,4 +1,4 @@ -package org.shedlang.compiler.backends.wasm.structure.ast.imports; +package org.shedlang.compiler.backends.wasm.structure.ast.modules; public record WasmImport( String moduleName, 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/modules/WasmImportDescriptor.java index c9e6ba5f..95e9c8cb 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/modules/WasmImportDescriptor.java @@ -1,4 +1,4 @@ -package org.shedlang.compiler.backends.wasm.structure.ast.imports; +package org.shedlang.compiler.backends.wasm.structure.ast.modules; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmGlobalType; diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/WasmModule.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java index 7bffd4c6..e37c4641 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/WasmModule.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java @@ -1,22 +1,32 @@ -package org.shedlang.compiler.backends.wasm.structure.ast; +package org.shedlang.compiler.backends.wasm.structure.ast.modules; import org.pcollections.PVector; -import org.shedlang.compiler.backends.wasm.structure.ast.imports.WasmImport; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.util.P; public record WasmModule( PVector<WasmFuncType> types, + PVector<WasmTable> tables, PVector<WasmImport> imports ) { public static final WasmModule EMPTY = new WasmModule( P.vector(), + P.vector(), P.vector() ); public WasmModule addType(WasmFuncType type) { return new WasmModule( types.plus(type), + tables, + imports + ); + } + + public WasmModule addTable(WasmTable table) { + return new WasmModule( + types, + tables.plus(table), imports ); } @@ -24,6 +34,7 @@ public record WasmModule( public WasmModule addImport(WasmImport import_) { return new WasmModule( types, + tables, imports.plus(import_) ); } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTable.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTable.java new file mode 100644 index 00000000..88178528 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTable.java @@ -0,0 +1,6 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmTableType; + +public record WasmTable(WasmTableType type) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java index 919149aa..bd4525af 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java @@ -2,9 +2,10 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.pcollections.PVector; import org.shedlang.compiler.backends.wasm.structure.BinaryWriter; -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.modules.WasmModule; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImport; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImportDescriptor; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmTable; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import java.io.IOException; @@ -164,6 +165,27 @@ public class WasmBinaryFormatModules { } } + // == Table Section == + + private static void writeTableSection( + WasmModule module, + BinaryWriter writer + ) throws IOException { + if (module.tables().size() > 0) { + writeSection(SectionId.TABLE, writer, () -> { + writeVec( + module.tables(), + table -> writeTable(table, writer), + writer + ); + }); + } + } + + private static void writeTable(WasmTable table, BinaryWriter writer) { + writeTableType(table.type(), writer); + } + // == Modules == private static final byte[] WASM_MAGIC = HexFormat.of().parseHex("0061736D"); @@ -175,6 +197,7 @@ public class WasmBinaryFormatModules { writer.writeBytes(WASM_VERSION); writeTypeSection(module.types(), writer); writeImportSection(module, writer); + writeTableSection(module, writer); writeNameSection(module, writer); outputStream.write(writer.toBytes()); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java index 31b5a9ab..efa2d055 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatNameSection.java @@ -1,8 +1,8 @@ package org.shedlang.compiler.backends.wasm.structure.binary; import org.shedlang.compiler.backends.wasm.structure.BinaryWriter; -import org.shedlang.compiler.backends.wasm.structure.ast.WasmModule; -import org.shedlang.compiler.backends.wasm.structure.ast.imports.WasmImportDescriptor; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmImportDescriptor; import org.shedlang.compiler.backends.wasm.structure.binary.WasmBinaryFormatModules.SectionId; import java.io.IOException; |
