From c3784202feea6d9e16044007dbb992dab3628553 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 27 Aug 2023 20:51:49 +0100 Subject: Add loop instructions to Stack IR --- .../java/org/shedlang/compiler/patio/PatioCodeGen.java | 9 +++++++++ .../compiler/stackir/InstructionSerializer.java | 12 ++++++++++++ .../compiler/stackir/instructions/Instruction.java | 2 +- .../stackir/instructions/LoopBreakInstruction.java | 9 +++++++++ .../stackir/instructions/LoopContinueInstruction.java | 9 +++++++++ .../compiler/stackir/instructions/LoopInstruction.java | 17 +++++++++++++++++ 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 57a08d69..c16dbc4a 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -366,6 +366,15 @@ public class PatioCodeGen { (local, c) -> c.temporaryPopIntoLocal(local, localStore.source()) ); + case LoopBreakInstruction loopBreakInstruction -> + throw new UnsupportedOperationException("TODO"); + + case LoopContinueInstruction loopContinueInstruction -> + throw new UnsupportedOperationException("TODO"); + + case LoopInstruction loopInstruction -> + throw new UnsupportedOperationException("TODO"); + case NamespaceFieldStoreInstruction namespaceFieldStore -> { var fieldValueResult = context.temporaryPop(); var fieldValue = fieldValueResult.value(); diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index 807caa57..1ae8f04d 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -113,6 +113,18 @@ public class InstructionSerializer { treeIdentifier("local-store"), treeString(localStoreInstruction.name()) ); + + case LoopInstruction loop -> + treeListIndented( + treeIdentifier("loop"), + instructionsToTree(loop.body()) + ); + + case LoopBreakInstruction loopBreak -> + treeList(treeIdentifier("break")); + + case LoopContinueInstruction loopContinue -> + treeList(treeIdentifier("continue")); case NamespaceFieldStoreInstruction namespaceFieldStore -> treeList( diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java index b4a59136..01af3588 100644 --- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.stackir.instructions; import org.shedlang.compiler.sources.Source; public sealed interface Instruction - permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, VarLoadInstruction, VarStoreInstruction { + permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, VarLoadInstruction, VarStoreInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java new file mode 100644 index 00000000..60a1d056 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Jump to the end of the enclosing loop instruction's body. + */ +public record LoopBreakInstruction(Source source) implements Instruction { +} diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java new file mode 100644 index 00000000..d7111ea9 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Jump to the start of the enclosing loop instruction's body. + */ +public record LoopContinueInstruction(Source source) implements Instruction { +} diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java new file mode 100644 index 00000000..119d9a52 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java @@ -0,0 +1,17 @@ +package org.shedlang.compiler.stackir.instructions; + + +import org.shedlang.compiler.sources.Source; + +import java.util.List; + +/** + * Execute the body. The continue instruction can jump back to the + * start of the body. The break instruction can jump to the end of + * the body. + */ +public record LoopInstruction( + List body, + Source source +) implements Instruction { +} -- cgit v1.2.3