aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-08 11:30:37 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-08 11:30:37 +0100
commit74163c2028e8d09b66cc1bb8d8002aba1a14ffed (patch)
treedf22c99b3d1222fc4fdb5d50593ed7aebd18e218 /src
parentcf0e7fa6e20b49b7a2ccd755aead11f2e78d18a9 (diff)
Add Patio switch instruction
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java4
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java43
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioSwitch.java26
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java92
-rw-r--r--src/test/java/org/shedlang/compiler/patio/instructions/Patio.java13
7 files changed, 168 insertions, 16 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java
index b055e1e2..45745796 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java
@@ -506,6 +506,10 @@ public class LlvmPatioCodeGen {
patioOperandToLlvmOperand(patioReturn.value(), context)
));
+ case PatioSwitch patioSwitch -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+
case PatioUnreachable patioUnreachable ->
context.addTerminatorInstruction(new LlvmUnreachable());
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
index 47ce14e3..fe01d9c5 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
@@ -306,6 +306,10 @@ public class WasmPatioCodeGen {
yield context;
}
+ case PatioSwitch patioSwitch -> {
+ throw new UnsupportedOperationException("TODO");
+ }
+
case PatioUnreachable patioUnreachable ->
context.addInstruction(new WasmUnreachable());
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
index c581e180..f329e679 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java
@@ -152,11 +152,7 @@ public class PatioSerializer {
treeIdentifier("call"),
optionalOperandToTree(call.target()),
treeString(call.functionName()),
- treeList(
- call.args().stream()
- .map(arg -> operandToTree(arg))
- .toList()
- )
+ operandsToTree(call.args())
);
case PatioCallPointer callPointer ->
@@ -164,11 +160,7 @@ public class PatioSerializer {
treeIdentifier("call-pointer"),
operandToTree(callPointer.target()),
operandToTree(callPointer.functionPointer()),
- treeList(
- callPointer.args().stream()
- .map(arg -> operandToTree(arg))
- .toList()
- )
+ operandsToTree(callPointer.args())
);
case PatioDataPtrToInt dataPtrToInt ->
@@ -181,11 +173,7 @@ public class PatioSerializer {
case PatioIf if_ ->
treeListIndented(
treeIdentifier("if"),
- treeList(
- if_.targets().stream()
- .map(target -> operandToTree(target))
- .toList()
- ),
+ operandsToTree(if_.targets()),
operandToTree(if_.condition()),
blockToTree(if_.trueBody()),
blockToTree(if_.falseBody())
@@ -293,6 +281,23 @@ public class PatioSerializer {
operandToTree(return_.value())
);
+ case PatioSwitch switch_ ->
+ treeListIndented(
+ treeIdentifier("switch"),
+ operandsToTree(switch_.targets()),
+ operandToTree(switch_.value()),
+ treeListIndented(
+ switch_.cases().stream()
+ .map(switchCase -> treeListIndented(
+ treeIdentifier("case"),
+ treeInt(switchCase.value()),
+ blockToTree(switchCase.body())
+ ))
+ .toList()
+ ),
+ blockToTree(switch_.defaultBody())
+ );
+
case PatioUnreachable unreachable ->
treeList(treeIdentifier("unreachable"));
@@ -354,6 +359,14 @@ public class PatioSerializer {
}
}
+ private static Tree operandsToTree(List<? extends PatioOperand> operands) {
+ return treeList(
+ operands.stream()
+ .map(target -> operandToTree(target))
+ .toList()
+ );
+ }
+
private static Tree operandToTree(PatioOperand operand) {
return switch (operand) {
case PatioBool bool ->
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java
index 8b11d25b..f473afdd 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstruction.java
@@ -2,6 +2,6 @@ package org.shedlang.compiler.patio.ast.instructions;
import org.shedlang.compiler.sources.Source;
-public sealed interface PatioInstruction permits PatioAllocHeap, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioUnreachable, PatioVarLoad, PatioVarStore {
+public sealed interface PatioInstruction permits PatioAllocHeap, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioSwitch, PatioUnreachable, PatioVarLoad, PatioVarStore {
Source source();
}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioSwitch.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioSwitch.java
new file mode 100644
index 00000000..d68b44a3
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioSwitch.java
@@ -0,0 +1,26 @@
+package org.shedlang.compiler.patio.ast.instructions;
+
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioOperand;
+import org.shedlang.compiler.sources.Source;
+
+import java.util.List;
+
+/**
+ * Find a case with a value matching that of the instruction's value. If such a
+ * case is found, execute the body of that case, otherwise execute the default
+ * body.
+ * <p>
+ * Each block must have the same number of results as targets. Each result
+ * is assigned to the target with the same index.
+ */
+public record PatioSwitch(
+ List<PatioLocalRef> targets,
+ PatioOperand value,
+ List<Case> cases,
+ PatioBlock defaultBody,
+ Source source
+) implements PatioInstruction {
+ public record Case(int value, PatioBlock body, Source source) {
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
index d2dd9205..a4ede83c 100644
--- a/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
+++ b/src/test/java/org/shedlang/compiler/patio/PatioSerializerTests.java
@@ -442,6 +442,98 @@ public class PatioSerializerTests {
}
@Test
+ public void canSerializeSwitch() {
+ var instruction = Patio.switch_(
+ List.of(
+ Patio.localRef("x"),
+ Patio.localRef("y")
+ ),
+ Patio.localRef("value"),
+ List.of(
+ Patio.switchCase(
+ 1,
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("x1"), Patio.int_(11)),
+ Patio.localStore(Patio.localRef("y1"), Patio.int_(12))
+ ),
+ List.of(
+ Patio.localRef("x1"),
+ Patio.localRef("y1")
+ )
+ )
+ ),
+ Patio.switchCase(
+ 2,
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("x2"), Patio.int_(21)),
+ Patio.localStore(Patio.localRef("y2"), Patio.int_(22))
+ ),
+ List.of(
+ Patio.localRef("x2"),
+ Patio.localRef("y2")
+ )
+ )
+ )
+ ),
+ Patio.block(
+ List.of(
+ Patio.localStore(Patio.localRef("x3"), Patio.int_(31)),
+ Patio.localStore(Patio.localRef("y3"), Patio.int_(32))
+ ),
+ List.of(
+ Patio.localRef("x3"),
+ Patio.localRef("y3")
+ )
+ )
+ );
+
+ var string = PatioTesting.serializeInstruction(instruction);
+
+ assertThat(string, equalTo("""
+ (
+ switch
+ ((local "x") (local "y"))
+ (local "value")
+ (
+ (
+ case
+ 1
+ (
+ block
+ (
+ (local-store (local "x1") (int 11))
+ (local-store (local "y1") (int 12))
+ )
+ results=((local "x1") (local "y1"))
+ )
+ )
+ (
+ case
+ 2
+ (
+ block
+ (
+ (local-store (local "x2") (int 21))
+ (local-store (local "y2") (int 22))
+ )
+ results=((local "x2") (local "y2"))
+ )
+ )
+ )
+ (
+ block
+ (
+ (local-store (local "x3") (int 31))
+ (local-store (local "y3") (int 32))
+ )
+ results=((local "x3") (local "y3"))
+ )
+ )"""));
+ }
+
+ @Test
public void canSerializeUnreachable() {
var instruction = Patio.unreachable();
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 075a9aca..451628da 100644
--- a/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java
+++ b/src/test/java/org/shedlang/compiler/patio/instructions/Patio.java
@@ -225,6 +225,19 @@ public class Patio {
return new PatioReturn(value, Sources.NULL);
}
+ public static PatioInstruction switch_(
+ List<PatioLocalRef> targets,
+ PatioOperand value,
+ List<PatioSwitch.Case> cases,
+ PatioBlock defaultBody
+ ) {
+ return new PatioSwitch(targets, value, cases, defaultBody, Sources.NULL);
+ }
+
+ public static PatioSwitch.Case switchCase(int value, PatioBlock body) {
+ return new PatioSwitch.Case(value, body, Sources.NULL);
+ }
+
public static PatioInstruction unreachable() {
return new PatioUnreachable(Sources.NULL);
}