aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-12-22 20:31:03 +0000
committerMichael Williamson <mike@zwobble.org>2024-12-22 20:31:03 +0000
commitc9b5c7bd986f15114df7dcfc405c4eca11cbf25f (patch)
tree9026849d171307bc5d47a416ed913c200649ea0b /src/test/java
parent4db39d53fa6524e43f57252fc60dcc6d831775ff (diff)
Support Wasm block types with multiple results
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructionsTests.java27
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java87
-rw-r--r--src/test/java/org/shedlang/compiler/patio/instructions/Patio.java2
3 files changed, 108 insertions, 8 deletions
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)
@@ -126,6 +126,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()
.withName(FUNCTION_NAME)
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<PatioLocalRef> destinations,
- PatioLocalRef condition,
+ PatioOperand condition,
PatioBlock trueBody,
PatioBlock falseBody
) {