Skip to content

Commit d6d1dbe

Browse files
committed
Add try_table instruction
1 parent 9c8e252 commit d6d1dbe

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/Instruction.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,50 @@ public boolean hasElse() {
223223
}
224224

225225
/**
226-
* The WASM try block from the exception handling proposal.
226+
* The WASM try_table from the exception handling proposal.
227227
* <p>
228228
* Ref: https://github.com/WebAssembly/exception-handling
229229
*/
230+
public static final class TryTable extends WasmBlock {
231+
/**
232+
* A catch clause for a certain tag.
233+
* <p>
234+
* When an exception is caught, the block branches to the label of the appropriate clause.
235+
*/
236+
public static final class Catch {
237+
public final WasmId.Tag tag;
238+
public final WasmId.Label label;
239+
240+
private Catch(WasmId.Tag tag, WasmId.Label label) {
241+
this.tag = tag;
242+
this.label = label;
243+
}
244+
245+
@Override
246+
public String toString() {
247+
return "Catch{tag=" + tag + ", label=" + label + '}';
248+
}
249+
}
250+
251+
public final Instructions instructions = new Instructions();
252+
public final List<Catch> catchBlocks = new ArrayList<>();
253+
254+
public TryTable(WasmId.Label label) {
255+
super(label, null);
256+
}
257+
258+
public void addCatch(WasmId.Tag tag, WasmId.Label catchLabel) {
259+
var catchBlock = new Catch(tag, catchLabel);
260+
catchBlocks.add(catchBlock);
261+
}
262+
}
263+
264+
/**
265+
* The WASM try block from the legacy exception handling proposal.
266+
* <p>
267+
* Ref:
268+
* https://github.com/WebAssembly/exception-handling/blob/master/proposals/exception-handling/legacy/Exceptions.md
269+
*/
230270
public static final class Try extends WasmBlock {
231271

232272
/**

web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmPrinter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,26 @@ public void visitIf(Instruction.If ifBlock) {
771771
newline();
772772
}
773773

774+
@Override
775+
@SuppressWarnings("try")
776+
public void visitTryTable(Instruction.TryTable tryBlock) {
777+
printBlockPrefix("try_table", tryBlock);
778+
779+
try (var ignored = new Indenter()) {
780+
for (Instruction.TryTable.Catch catchBlock : tryBlock.catchBlocks) {
781+
newline();
782+
parenOpen("catch");
783+
space();
784+
printId(catchBlock.tag);
785+
space();
786+
printId(catchBlock.label);
787+
parenClose();
788+
}
789+
super.visitInstructions(tryBlock.instructions);
790+
}
791+
newline();
792+
}
793+
774794
@Override
775795
@SuppressWarnings("try")
776796
public void visitTry(Instruction.Try tryBlock) {

web-image/src/com.oracle.svm.hosted.webimage/src/com/oracle/svm/hosted/webimage/wasm/ast/visitors/WasmVisitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ public void visitInstruction(Instruction inst) {
278278
visitBlockInstr(i);
279279
visitIf(i);
280280
}
281+
case Instruction.TryTable i -> {
282+
visitBlockInstr(i);
283+
visitTryTable(i);
284+
}
281285
case Instruction.Try i -> {
282286
visitBlockInstr(i);
283287
visitTry(i);
@@ -360,6 +364,14 @@ public void visitIf(Instruction.If ifBlock) {
360364
}
361365
}
362366

367+
public void visitTryTable(Instruction.TryTable tryBlock) {
368+
for (Instruction.TryTable.Catch catchBlock : tryBlock.catchBlocks) {
369+
visitId(catchBlock.tag);
370+
visitId(catchBlock.label);
371+
}
372+
visitInstructions(tryBlock.instructions);
373+
}
374+
363375
public void visitTry(Instruction.Try tryBlock) {
364376
for (Instruction.Try.Catch catchBlock : tryBlock.catchBlocks) {
365377
visitId(catchBlock.tag);

0 commit comments

Comments
 (0)