From 1b7e1f872b27493895d44d038763cbb20e869d77 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 26 Nov 2023 15:47:13 +0000 Subject: Support Wasm try with no catch blocks --- .../wasm/structure/ast/instructions/WasmCatchBlock.java | 11 +++++++++++ .../wasm/structure/ast/instructions/WasmInstruction.java | 2 +- .../backends/wasm/structure/ast/instructions/WasmTry.java | 10 ++++++++++ .../backends/wasm/structure/ast/modules/WasmTagName.java | 4 ++++ .../wasm/structure/binary/WasmBinaryFormatExceptions.java | 12 +++++++++++- .../wasm/structure/binary/WasmBinaryFormatInstructions.java | 11 +++++++++++ 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmCatchBlock.java create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmTry.java create mode 100644 src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTagName.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmCatchBlock.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmCatchBlock.java new file mode 100644 index 00000000..c0ab485b --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmCatchBlock.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.instructions; + +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmTag; + +import java.util.List; + +public record WasmCatchBlock( + WasmTag tag, + List body +) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java index eef4ec24..155cfdf2 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java @@ -1,4 +1,4 @@ package org.shedlang.compiler.backends.wasm.structure.ast.instructions; -public sealed interface WasmInstruction permits WasmBlock, WasmBr, WasmCall, WasmCallIndirect, WasmDrop, WasmGlobalGet, WasmGlobalSet, WasmI32Add, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Ne, WasmI32Store, WasmI32Sub, WasmI64Const, WasmIf, WasmLocalGet, WasmLocalSet, WasmLoop, WasmMemoryAddressConst, WasmMemoryGrow, WasmReturn, WasmTableElementIndexConst, WasmUnreachable { +public sealed interface WasmInstruction permits WasmBlock, WasmBr, WasmCall, WasmCallIndirect, WasmDrop, WasmGlobalGet, WasmGlobalSet, WasmI32Add, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Ne, WasmI32Store, WasmI32Sub, WasmI64Const, WasmIf, WasmLocalGet, WasmLocalSet, WasmLoop, WasmMemoryAddressConst, WasmMemoryGrow, WasmReturn, WasmTableElementIndexConst, WasmTry, WasmUnreachable { } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmTry.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmTry.java new file mode 100644 index 00000000..15436919 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmTry.java @@ -0,0 +1,10 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.instructions; + +import java.util.List; + +public record WasmTry( + WasmBlockType blockType, + List catchBlocks, + List body +) implements WasmInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTagName.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTagName.java new file mode 100644 index 00000000..02e5d9b3 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/modules/WasmTagName.java @@ -0,0 +1,4 @@ +package org.shedlang.compiler.backends.wasm.structure.ast.modules; + +public record WasmTagName(String name) { +} diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java index fa1dbe9c..d3a66853 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatExceptions.java @@ -7,7 +7,17 @@ import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmTagIndex; import java.io.IOException; public class WasmBinaryFormatExceptions { - // Corresponds to https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md + // Corresponds to https://github.com/WebAssembly/exception-handling/blob/0f577bde2019ea6474d245ff096488bf8fe9a835/proposals/exception-handling/Exceptions.md + // + // Note that this version (v3) of the exception handling proposal has been + // superseded, see: + // + // * https://github.com/WebAssembly/exception-handling/blob/0f577bde2019ea6474d245ff096488bf8fe9a835/proposals/exception-handling/Exceptions.md + // * https://github.com/WebAssembly/exception-handling/issues/281 + // + // However, this version of the proposal has been widely implemented, and + // is likely to continue to be supported for some time. We therefore use + // this version of the proposal for the time being. private WasmBinaryFormatExceptions() { } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java index 70d48997..ccdd4e7a 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java @@ -145,6 +145,17 @@ public class WasmBinaryFormatInstructions { writer.writeByte(0x6b); } + // == Exception Instructions (v3 proposal) == + + case WasmTry try_ -> { + writer.writeByte(0x06); + writeBlockType(try_.blockType(), writer); + for (var bodyInstruction : try_.body()) { + writeInstruction(bodyInstruction, writer); + } + writer.writeByte(0x0b); + } + // == Relocatable Instructions == case WasmMemoryAddressConst memoryAddressConst -> { -- cgit v1.2.3