From c9b5c7bd986f15114df7dcfc405c4eca11cbf25f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Dec 2024 20:31:03 +0000 Subject: Support Wasm block types with multiple results --- .../binary/WasmBinaryFormatInstructionsTests.java | 27 +++++-- .../shedlang/compiler/patio/PatioExampleTests.java | 87 +++++++++++++++++++++- .../compiler/patio/instructions/Patio.java | 2 +- 3 files changed, 108 insertions(+), 8 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java index a14763d5..b9f2d9c2 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java @@ -1,10 +1,10 @@ package org.shedlang.compiler.backends.wasm.structure.binary; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.*; import org.shedlang.compiler.backends.wasm.structure.ast.modules.*; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmLimits; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmMemoryType; import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmValueType; @@ -55,12 +55,16 @@ public class WasmBinaryFormatInstructionsTests { } @Test - @Disabled("WIP") public void canSerializeBlockTypeMultipleValues(Snapshotter snapshotter) throws IOException, InterruptedException { + var funcType = new WasmFuncType( + List.of(), + List.of(WasmValueType.I64, WasmValueType.I32) + ); + var instructions = List.of( new WasmI32Const(0), new WasmIf( - WasmBlockType.of(WasmValueType.I64, WasmValueType.I32), + WasmBlockType.of(funcType), List.of( new WasmI64Const(0), new WasmI32Const(0) @@ -70,10 +74,21 @@ public class WasmBinaryFormatInstructionsTests { new WasmI32Const(1) ) ), + new WasmDrop(), new WasmDrop() ); - var result = serializeInstructions(instructions); + var module = WasmModule.EMPTY + .addType(funcType) + .addFunction(new WasmFunction( + "f", + List.of(), + List.of(), + List.of(), + new WasmExpression(instructions) + )); + + var result = serializeModuleWasm32(module); snapshotter.assertSnapshot(result); } @@ -326,7 +341,7 @@ public class WasmBinaryFormatInstructionsTests { List.of(), new WasmExpression(List.of( new WasmTry( - new WasmBlockType(List.of(WasmValueType.I32)), + WasmBlockType.of(WasmValueType.I32), List.of(), List.of( new WasmI32Const(42) @@ -361,7 +376,7 @@ public class WasmBinaryFormatInstructionsTests { List.of(), new WasmExpression(List.of( new WasmTry( - new WasmBlockType(List.of(WasmValueType.I32)), + WasmBlockType.of(WasmValueType.I32), List.of( new WasmCatchBlock( new WasmTagName("Error1"), diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index b4f51813..e1566c99 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -99,7 +99,7 @@ public abstract class PatioExampleTests { } @Test - public void block() throws IOException, InterruptedException { + public void blockSingleResult() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() .withName(FUNCTION_NAME) .withReturnType(PatioTypes.VOID) @@ -125,6 +125,91 @@ public abstract class PatioExampleTests { )); } + @Test + public void blockMultipleResults() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.VOID) + .addLocal(Patio.val("x", PatioTypes.INT)) + .addLocal(Patio.val("y", PatioTypes.INT)) + .addBodyInstruction(Patio.block( + List.of( + Patio.localStore( + Patio.localRef("x"), + Patio.intConst(42) + ), + Patio.localStore( + Patio.localRef("y"), + Patio.intConst(47) + ) + ), + List.of(Patio.localRef("x"), Patio.localRef("y")) + )) + .addBodyInstruction(emitInt(Patio.localRef("x"))) + .addBodyInstruction(emitInt(Patio.localRef("y"))) + .addBodyInstruction(Patio.returnVoid()) + .build(); + var patio = Patio.compilationUnit(List.of(patioFunction)); + + var result = run(patio); + + assertThat(result, isSequence( + isInt(42), + isInt(47) + )); + } + + @Test + public void ifCanHaveOneResult() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.VOID) + .addLocal(Patio.val("x", PatioTypes.INT)) + .addBodyInstruction(Patio.if_( + List.of(Patio.localRef("x")), + Patio.boolConst(true), + Patio.block(List.of(), List.of(Patio.intConst(42))), + Patio.block(List.of(), List.of(Patio.intConst(47))) + )) + .addBodyInstruction(emitInt(Patio.localRef("x"))) + .addBodyInstruction(Patio.returnVoid()) + .build(); + var patio = Patio.compilationUnit(List.of(patioFunction)); + + var result = run(patio); + + assertThat(result, isSequence( + isInt(42) + )); + } + + @Test + public void ifCanHaveMultipleResults() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.VOID) + .addLocal(Patio.val("x", PatioTypes.INT)) + .addLocal(Patio.val("y", PatioTypes.INT)) + .addBodyInstruction(Patio.if_( + List.of(Patio.localRef("x"), Patio.localRef("y")), + Patio.boolConst(true), + Patio.block(List.of(), List.of(Patio.intConst(42), Patio.intConst(47))), + Patio.block(List.of(), List.of(Patio.intConst(52), Patio.intConst(57))) + )) + .addBodyInstruction(emitInt(Patio.localRef("x"))) + .addBodyInstruction(emitInt(Patio.localRef("y"))) + .addBodyInstruction(Patio.returnVoid()) + .build(); + var patio = Patio.compilationUnit(List.of(patioFunction)); + + var result = run(patio); + + assertThat(result, isSequence( + isInt(42), + isInt(47) + )); + } + @Test public void boolToIntToBoolIsIdentityForFalse() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() diff --git a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java index 6980eb2a..35d34741 100644 --- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java +++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java @@ -134,7 +134,7 @@ public class Patio { public static PatioInstruction if_( List destinations, - PatioLocalRef condition, + PatioOperand condition, PatioBlock trueBody, PatioBlock falseBody ) { -- cgit v1.2.3