From e139747e1593885bc90f2ae5427a6215475b76fd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 25 Jun 2023 12:38:32 +0100 Subject: Support globals in WASM binary format --- .../wasm/structure/ast/modules/WasmGlobal.java | 7 +++ .../wasm/structure/ast/modules/WasmModule.java | 20 +++++++ .../structure/binary/WasmBinaryFormatModules.java | 62 +++++++++++++++------- 3 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmGlobal.java (limited to 'src/main/java') 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 functions, PVector tables, PVector memories, + PVector globals, PVector elementSegments, PVector 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); -- cgit v1.2.3