From c68eb4e8eabbb0b533604bf38e7fce76ecf08d3c Mon Sep 17 00:00:00 2001 From: Patrick Ziegler Date: Fri, 4 Apr 2025 17:19:06 +0200 Subject: [PATCH 1/8] Support Wasm blocks with a single result --- .../hosted/webimage/wasm/ast/Instruction.java | 33 +++++++++++++++--- .../wasm/ast/visitors/WasmPrinter.java | 34 ++++++++----------- .../wasm/ast/visitors/WasmValidator.java | 22 ++++++++---- .../wasm/ast/visitors/WasmVisitor.java | 5 ++- 4 files changed, 63 insertions(+), 31 deletions(-) diff --git a/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/Instruction.java b/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/Instruction.java index 45473ba4baab..5da53a578dcf 100644 --- a/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/Instruction.java +++ b/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/Instruction.java @@ -148,14 +148,33 @@ public abstract static class WasmBlock extends Instruction { * If set to null, the block has no named identifier */ protected final WasmId.Label label; + protected final WasmValType result; - public WasmBlock(WasmId.Label label) { + protected WasmBlock(WasmId.Label label, WasmValType result) { this.label = label; + this.result = result; } public WasmId.Label getLabel() { return label; } + + public WasmValType getResult() { + return result; + } + + public boolean hasResult() { + return result != null; + } + + @Override + protected String toInnerString() { + if (hasResult()) { + return "() -> " + result; + } else { + return "() -> ()"; + } + } } /** @@ -165,7 +184,11 @@ public static final class Block extends WasmBlock { public final Instructions instructions = new Instructions(); public Block(WasmId.Label label) { - super(label); + this(label, null); + } + + public Block(WasmId.Label label, WasmValType result) { + super(label, result); } } @@ -176,7 +199,7 @@ public static final class Loop extends WasmBlock { public final Instructions instructions = new Instructions(); public Loop(WasmId.Label label) { - super(label); + super(label, null); } } @@ -190,7 +213,7 @@ public static final class If extends WasmBlock { public final Instruction condition; public If(WasmId.Label label, Instruction condition) { - super(label); + super(label, null); this.condition = condition; } @@ -222,7 +245,7 @@ private Catch(WasmId.Tag tag) { public final List catchBlocks = new ArrayList<>(); public Try(WasmId.Label label) { - super(label); + super(label, null); } public Instructions addCatch(WasmId.Tag tag) { diff --git a/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmPrinter.java b/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmPrinter.java index d03c6604e506..43fd40e97c7f 100644 --- a/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmPrinter.java +++ b/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmPrinter.java @@ -710,14 +710,22 @@ public void visitTable(Table t) { parenClose(); } - @Override - @SuppressWarnings("try") - public void visitBlock(Instruction.Block block) { - print("block"); + private void printBlockPrefix(String name, Instruction.WasmBlock block) { + print(name); space(); printId(block.getLabel()); + if (block.hasResult()) { + space(); + printResult(block.getResult()); + } space(); printComment(block.getComment()); + } + + @Override + @SuppressWarnings("try") + public void visitBlock(Instruction.Block block) { + printBlockPrefix("block", block); try (var ignored = new Indenter()) { super.visitBlock(block); } @@ -727,11 +735,7 @@ public void visitBlock(Instruction.Block block) { @Override @SuppressWarnings("try") public void visitLoop(Instruction.Loop loop) { - print("loop"); - space(); - printId(loop.getLabel()); - space(); - printComment(loop.getComment()); + printBlockPrefix("loop", loop); try (var ignored = new Indenter()) { super.visitLoop(loop); } @@ -741,11 +745,7 @@ public void visitLoop(Instruction.Loop loop) { @Override @SuppressWarnings("try") public void visitIf(Instruction.If ifBlock) { - print("if"); - space(); - printId(ifBlock.getLabel()); - space(); - printComment(ifBlock.getComment()); + printBlockPrefix("if", ifBlock); try (var ignored = new Indenter()) { visitInstruction(ifBlock.condition); @@ -774,11 +774,7 @@ public void visitIf(Instruction.If ifBlock) { @Override @SuppressWarnings("try") public void visitTry(Instruction.Try tryBlock) { - print("try"); - space(); - printId(tryBlock.getLabel()); - space(); - printComment(tryBlock.getComment()); + printBlockPrefix("try", tryBlock); try (var ignored = new Indenter()) { newline(); diff --git a/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmValidator.java b/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmValidator.java index 8341863d0d88..b0055c26b5fe 100644 --- a/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmValidator.java +++ b/web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmValidator.java @@ -64,7 +64,7 @@ *