From ebf5f513c8305ae37d03d990fb24e5b7a6623244 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Oct 2023 16:49:03 +0100 Subject: Support Patio switch with reachable default in LLVM code gen --- .../llvm/LlvmCodeGenLocalVariableSpace.java | 3 +- .../compiler/backends/llvm/LlvmPatioCodeGen.java | 36 ++++++++++--------- .../shedlang/compiler/patio/PatioExampleTests.java | 41 +++++++++++++++++++++- 3 files changed, 62 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenLocalVariableSpace.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenLocalVariableSpace.java index 989c657f..fb65ffb8 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenLocalVariableSpace.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenLocalVariableSpace.java @@ -27,7 +27,8 @@ public enum LlvmCodeGenLocalVariableSpace { SIZE("size"), SWITCH_CASE_START("switchCaseStart"), SWITCH_CASE_END("switchCaseEnd"), - SWITCH_DEFAULT("switchDefault"), + SWITCH_DEFAULT_START("switchDefaultStart"), + SWITCH_DEFAULT_END("switchDefaultEnd"), SWITCH_END("switchEnd"), TEMPORARY("tmp"); 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 fce77ab8..e431a851 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java @@ -300,8 +300,8 @@ public class LlvmPatioCodeGen { context = generateBranches( patioIf.targets(), List.of( - new Branch(ifTrueStartLabel, Optional.of(ifTrueEndLabel), patioIf.trueBody()), - new Branch(ifFalseStartLabel, Optional.of(ifFalseEndLabel), patioIf.falseBody()) + new Branch(ifTrueStartLabel, ifTrueEndLabel, patioIf.trueBody()), + new Branch(ifFalseStartLabel, ifFalseEndLabel, patioIf.falseBody()) ), endLabel, context @@ -480,11 +480,17 @@ public class LlvmPatioCodeGen { )); case PatioSwitch patioSwitch -> { - var defaultLabelResult = context.allocateLocalName( - LlvmCodeGenLocalVariableSpace.SWITCH_DEFAULT + var switchDefaultStartLabelResult = context.allocateLocalName( + LlvmCodeGenLocalVariableSpace.SWITCH_DEFAULT_START ); - var switchDefaultLabel = defaultLabelResult.value(); - context = defaultLabelResult.context(); + var switchDefaultStartLabel = switchDefaultStartLabelResult.value(); + context = switchDefaultStartLabelResult.context(); + + var switchDefaultEndLabelResult = context.allocateLocalName( + LlvmCodeGenLocalVariableSpace.SWITCH_DEFAULT_END + ); + var switchDefaultEndLabel = switchDefaultEndLabelResult.value(); + context = switchDefaultEndLabelResult.context(); var switchEndLabelResult = context.allocateLocalName( LlvmCodeGenLocalVariableSpace.SWITCH_END @@ -511,7 +517,7 @@ public class LlvmPatioCodeGen { branches.add(new Branch( caseStartLabel, - Optional.of(caseEndLabel), + caseEndLabel, patioSwitchCase.body() )); @@ -522,16 +528,15 @@ public class LlvmPatioCodeGen { } branches.add(new Branch( - switchDefaultLabel, - // TODO: handle defaults that don't terminate - Optional.empty(), + switchDefaultStartLabel, + switchDefaultEndLabel, patioSwitch.defaultBody() )); context = context.addTerminatorInstruction(new LlvmSwitch( LlvmTypes.INT_64, patioOperandToLlvmOperand(patioSwitch.value(), context), - switchDefaultLabel, + switchDefaultStartLabel, llvmSwitchCases )); @@ -587,7 +592,7 @@ public class LlvmPatioCodeGen { record Branch( LlvmVariableLocal startLabel, - Optional endLabel, + LlvmVariableLocal endLabel, PatioBlock body ) {} @@ -607,17 +612,16 @@ public class LlvmPatioCodeGen { .anyMatch(instruction -> instruction instanceof PatioUnreachable); if (!endIsUnreachable) { - var branchEndLabel = branch.endLabel.orElseThrow(); context = context.addTerminatorInstruction(new LlvmBrUnconditional( - branchEndLabel + branch.endLabel )); // TODO: only allow basic block label to be set with terminator instruction? - context = context.setBasicBlockLabel(branchEndLabel.name()); + context = context.setBasicBlockLabel(branch.endLabel.name()); context = context.addTerminatorInstruction(new LlvmBrUnconditional( endLabel )); - phiPredecessors.add(new PhiPredecessor(branch.body, branchEndLabel)); + phiPredecessors.add(new PhiPredecessor(branch.body, branch.endLabel)); } } diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index 265dfbde..d5e296c6 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -50,7 +50,7 @@ public abstract class PatioExampleTests { @Test @Disabled("WIP") - public void switch_() throws IOException, InterruptedException { + public void switchWithUnreachableDefault() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() .withName(FUNCTION_NAME) .withReturnType(PatioTypes.INT) @@ -94,6 +94,45 @@ public abstract class PatioExampleTests { assertThat(result, isSilentExit(47)); } + @Test + @Disabled("WIP") + public void switchWithReachableDefault() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.INT) + .addLocal(Patio.val("x", PatioTypes.INT)) + .addLocal(Patio.val("x1", PatioTypes.INT)) + .addLocal(Patio.val("x2", PatioTypes.INT)) + .addBodyInstruction(Patio.switch_( + List.of(Patio.localRef("x")), + Patio.int_(2), + List.of( + Patio.switchCase( + 1, + Patio.block( + List.of( + Patio.localStore(Patio.localRef("x1"), Patio.int_(42)) + ), + List.of(Patio.localRef("x1")) + ) + ) + ), + Patio.block( + List.of( + Patio.localStore(Patio.localRef("x2"), Patio.int_(47)) + ), + List.of(Patio.localRef("x2")) + ) + )) + .addBodyInstruction(Patio.return_(Patio.localRef("x"))) + .build(); + var patio = Patio.compilationUnit(List.of(patioFunction)); + + var result = run(patio); + + assertThat(result, isSilentExit(47)); + } + @Test public void vars() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() -- cgit v1.2.3