diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-08 11:38:53 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-08 11:38:53 +0100 |
| commit | b0a288f03774d3df6f4502af7748a52012e05029 (patch) | |
| tree | 50aa09baec9467dfbe82cfda9e38aa001d9b31d1 /src/main/java | |
| parent | 74163c2028e8d09b66cc1bb8d8002aba1a14ffed (diff) | |
Generate Patio code for stack IR switch instruction
Diffstat (limited to 'src/main/java')
| -rw-r--r-- | src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java | 49 |
1 files changed, 47 insertions, 2 deletions
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<PatioSwitch.Case>(); + + 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<PatioOperand>(); |
