aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-25 12:38:32 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-25 12:38:32 +0100
commite139747e1593885bc90f2ae5427a6215475b76fd (patch)
tree720e6a0b754a253cbec817f04e760cb484451f97 /src/main/java
parentbb0029571483cdb4359359c7f978fed62976c891 (diff)
Support globals in WASM binary format
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmGlobal.java7
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmModule.java20
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatModules.java62
3 files changed, 71 insertions, 18 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmGlobal.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmGlobal.java
new file mode 100644
index 00000000..637376c2
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmGlobal.java
@@ -0,0 +1,7 @@
+package org.shedlang.compiler.backends.wasm.structure.ast.modules;
+
+import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpression;
+import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmGlobalType;
+
+public record WasmGlobal(WasmGlobalType type, WasmExpression init) {
+}
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 7395d996..79a1e6f5 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
@@ -13,6 +13,7 @@ public record WasmModule(
PVector<WasmFunction> functions,
PVector<WasmTable> tables,
PVector<WasmMemory> memories,
+ PVector<WasmGlobal> globals,
PVector<WasmElementSegment> elementSegments,
PVector<WasmImport> imports
) {
@@ -22,6 +23,7 @@ public record WasmModule(
P.vector(),
P.vector(),
P.vector(),
+ P.vector(),
P.vector()
);
@@ -83,6 +85,7 @@ public record WasmModule(
functions,
tables,
memories,
+ globals,
elementSegments,
imports
);
@@ -94,6 +97,7 @@ public record WasmModule(
functions.plus(function),
tables,
memories,
+ globals,
elementSegments,
imports
);
@@ -105,6 +109,7 @@ public record WasmModule(
functions,
tables.plus(table),
memories,
+ globals,
elementSegments,
imports
);
@@ -116,6 +121,19 @@ public record WasmModule(
functions,
tables,
memories.plus(memory),
+ globals,
+ elementSegments,
+ imports
+ );
+ }
+
+ public WasmModule addGlobal(WasmGlobal global) {
+ return new WasmModule(
+ types,
+ functions,
+ tables,
+ memories,
+ globals.plus(global),
elementSegments,
imports
);
@@ -127,6 +145,7 @@ public record WasmModule(
functions,
tables,
memories,
+ globals,
elementSegments.plus(elementSegment),
imports
);
@@ -145,6 +164,7 @@ public record WasmModule(
functions,
tables,
memories,
+ globals,
elementSegments,
newImports
);
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 ef1c6afb..3525bbe5 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
@@ -207,6 +207,49 @@ 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);
+ }
+
+ // == Global Section ==
+
+ private static void writeGlobalSection(
+ WasmModule module,
+ BinaryWriter writer
+ ) throws IOException {
+ if (module.globals().size() > 0) {
+ writeSection(SectionId.GLOBAL, writer, () -> {
+ writeVec(
+ module.globals(),
+ global -> writeGlobal(global, writer),
+ writer
+ );
+ });
+ }
+ }
+
+ private static void writeGlobal(
+ WasmGlobal global,
+ BinaryWriter writer
+ ) throws IOException {
+ writeGlobalType(global.type(), writer);
+ writeExpression(global.init(), writer);
+ }
+
// == Element Section ==
private static void writeElementSection(
@@ -269,24 +312,6 @@ public class WasmBinaryFormatModules {
writeExpression(function.body(), 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");
@@ -303,6 +328,7 @@ public class WasmBinaryFormatModules {
writeFunctionSection(module, indexTable, writer);
writeTableSection(module, writer);
writeMemorySection(module, writer);
+ writeGlobalSection(module, writer);
writeElementSection(module, writer);
writeCodeSection(module, writer);
writeNameSection(module, indexTable, writer);