From b0a288f03774d3df6f4502af7748a52012e05029 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Oct 2023 11:38:53 +0100 Subject: Generate Patio code for stack IR switch instruction --- ...mpiler.testing.snapshots.Snapshotter)].expected | 59 ++++++++++++++++++++++ .../org/shedlang/compiler/patio/PatioCodeGen.java | 49 +++++++++++++++++- .../compiler/patio/PatioCodeGenSwitchTests.java | 58 +++++++++++++++++++++ 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected create mode 100644 src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..a738ad65 --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.patio.PatioCodeGenSwitchTests]/[method:canGenerateCodeForSwitchWithSingleResult(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,59 @@ +Compilation unit elements: + + +Locals: + * (val "tmp__0" int) + * (val "tmp__1" int) + * (val "tmp__2" int) + * (val "tmp__3" int) + * (val "tmp__4" int) + * (val "tmp__5" int) + * (val "tmp__6" int) + +Instructions: + +( + ( + switch + ((local "tmp__0")) + (local "condition") + ( + ( + case + 0 + ( + block + ( + (local-store (local "tmp__1") (int 1)) + (local-store (local "tmp__2") (int 2)) + (int-add (local "tmp__3") (local "tmp__1") (local "tmp__2")) + ) + results=((local "tmp__3")) + ) + ) + ( + case + 1 + ( + block + ( + (local-store (local "tmp__4") (int 101)) + (local-store (local "tmp__5") (int 102)) + (int-add (local "tmp__6") (local "tmp__4") (local "tmp__5")) + ) + results=((local "tmp__6")) + ) + ) + ) + ( + block + ( + (unreachable) + ) + results=() + ) + ) +) + +Temporary stack: + * other diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index b68bebdf..3510a249 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -512,8 +512,53 @@ public class PatioCodeGen { context ); - case SwitchInstruction switch_ -> - throw new UnsupportedOperationException("TODO"); + case SwitchInstruction switch_ -> { + var targetResult = context.allocateVal( + PatioCodeGenLocalSpace.TEMPORARY, + PatioCodeGenTypes.VALUE_TYPE + ); + var target = targetResult.value(); + context = targetResult.context(); + + var valueResult = context.temporaryPop(); + var value = valueResult.value(); + context = valueResult.context(); + + var cases = new ArrayList(); + + for (var stackIrCase : switch_.cases()) { + var bodyResult = generateBlock( + 1, + stackIrCase.body(), + stackIrCase.source(), + context + ); + var body = bodyResult.value(); + context = bodyResult.context(); + + cases.add(new PatioSwitch.Case( + context.tag(stackIrCase.caseType()), + body, + stackIrCase.source() + )); + } + + context = context.addInstruction(new PatioSwitch( + List.of(target), + value, + cases, + new PatioBlock( + List.of( + new PatioUnreachable(switch_.source()) + ), + List.of(), + switch_.source() + ), + switch_.source() + )); + + yield context; + } case TupleCreateInstruction tupleCreate -> { var elements = new ArrayList(); diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java new file mode 100644 index 00000000..d42771c6 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenSwitchTests.java @@ -0,0 +1,58 @@ +package org.shedlang.compiler.patio; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.patio.ast.operands.PatioLocalRef; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; +import org.shedlang.compiler.typechecker.TypeInfos; +import org.shedlang.compiler.types.NamespaceName; +import org.shedlang.compiler.types.StructTypeInfo; +import org.shedlang.compiler.types.Types; +import org.shedlang.compiler.util.P; + +import java.io.IOException; +import java.util.List; + +import static org.shedlang.compiler.patio.PatioTesting.serialize; + +@ExtendWith({SnapshotResolver.class}) +public class PatioCodeGenSwitchTests { + @Test + public void canGenerateCodeForSwitchWithSingleResult(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var structType1 = Types.struct(namespaceName, "A"); + var structType2 = Types.struct(namespaceName, "B"); + var instruction = Instructions.switch_( + List.of( + Instructions.switchCase( + structType1, + List.of( + Instructions.pushInt(1), + Instructions.pushInt(2), + Instructions.intAdd() + ) + ), + Instructions.switchCase( + structType2, + List.of( + Instructions.pushInt(101), + Instructions.pushInt(102), + Instructions.intAdd() + ) + ) + ) + ); + var typeInfos = TypeInfos.EMPTY + .addStructTypeInfo(structType1, StructTypeInfo.fromFields(P.orderedMap())) + .addStructTypeInfo(structType2, StructTypeInfo.fromFields(P.orderedMap())); + var context = PatioCodeGenContext.forTypeInfos(typeInfos) + .temporaryPush(new PatioLocalRef("other")) + .temporaryPush(new PatioLocalRef("condition")); + + var result = PatioCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } +} -- cgit v1.2.3