From 03a83877a5fa055543ca5bc62adac269cf59dc93 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 24 Jun 2023 19:52:45 +0100 Subject: WASM binary format: support memory sections --- .../wasm/structure/ast/modules/WasmMemory.java | 6 ++++++ .../wasm/structure/ast/modules/WasmModule.java | 14 +++++++++++++ .../structure/binary/WasmBinaryFormatModules.java | 24 ++++++++++++++++++---- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmMemory.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmMemory.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmMemory.java new file mode 100644 index 00000000..01f5141a --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmMemory.java @@ -0,0 +1,6 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmMemoryType; + +public record WasmMemory(WasmMemoryType type) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java index e37c4641..d8757402 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java @@ -7,9 +7,11 @@ import org.shedlang.compiler.util.P; public record WasmModule( PVector types, PVector tables, + PVector memories, PVector imports ) { public static final WasmModule EMPTY = new WasmModule( + P.vector(), P.vector(), P.vector(), P.vector() @@ -19,6 +21,7 @@ public record WasmModule( return new WasmModule( types.plus(type), tables, + memories, imports ); } @@ -27,6 +30,16 @@ public record WasmModule( return new WasmModule( types, tables.plus(table), + memories, + imports + ); + } + + public WasmModule addMemory(WasmMemory memory) { + return new WasmModule( + types, + tables, + memories.plus(memory), imports ); } @@ -35,6 +48,7 @@ public record WasmModule( return new WasmModule( types, tables, + memories, imports.plus(import_) ); } 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 bd4525af..8945d7ed 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,10 +2,7 @@ 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.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.modules.*; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import java.io.IOException; @@ -186,6 +183,24 @@ public class WasmBinaryFormatModules { writeTableType(table.type(), writer); } + // == Memory Section == + + private static void writeMemorySection(WasmModule module, BinaryWriter writer) throws IOException { + if (module.memories().size() > 0) { + writeSection(SectionId.MEMORY, writer, () -> { + writeVec( + module.memories(), + memory -> writeMemory(memory, writer), + writer + ); + }); + } + } + + private static void writeMemory(WasmMemory memory, BinaryWriter writer) { + writeMemoryType(memory.type(), writer); + } + // == Modules == private static final byte[] WASM_MAGIC = HexFormat.of().parseHex("0061736D"); @@ -198,6 +213,7 @@ public class WasmBinaryFormatModules { writeTypeSection(module.types(), writer); writeImportSection(module, writer); writeTableSection(module, writer); + writeMemorySection(module, writer); writeNameSection(module, writer); outputStream.write(writer.toBytes()); -- cgit v1.2.3