aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-24 19:52:45 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-24 19:52:45 +0100
commit03a83877a5fa055543ca5bc62adac269cf59dc93 (patch)
tree6167278fe791f724b0169c50225dfe6a32ab98a6 /src/main/java
parent4ea7fb648b0c6f95e6383c34912aad411a43cdbf (diff)
WASM binary format: support memory sections
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmMemory.java6
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java14
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java24
3 files changed, 40 insertions, 4 deletions
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,11 +7,13 @@ import org.shedlang.compiler.util.P;
public record WasmModule(
PVector<WasmFuncType> types,
PVector<WasmTable> tables,
+ PVector<WasmMemory> memories,
PVector<WasmImport> 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());