From 689c9c1e8af8fd2fff2b390649a59e613e85eb6a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 2 Dec 2023 14:25:46 +0000 Subject: Add resume instruction --- .../org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java | 6 ++++++ .../org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java | 8 ++++++++ src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java | 10 ++++++++++ .../java/org/shedlang/compiler/patio/ast/PatioSerializer.java | 6 ++++++ .../compiler/patio/ast/instructions/PatioInstruction.java | 2 +- .../shedlang/compiler/patio/ast/instructions/PatioResume.java | 11 +++++++++++ .../org/shedlang/compiler/stackir/InstructionSerializer.java | 5 +++++ .../java/org/shedlang/compiler/stackir/StackCompiler.java | 2 +- .../shedlang/compiler/stackir/instructions/Instruction.java | 2 +- .../compiler/stackir/instructions/ResumeInstruction.java | 10 ++++++++++ 10 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioResume.java create mode 100644 src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java (limited to 'src/main/java') 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 1c31f3e9..12c58729 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGen.java @@ -542,6 +542,12 @@ public class LlvmPatioCodeGen { yield context; } + case PatioResume patioResume -> + context.addTerminatorInstruction(new LlvmReturn( + patioOperandToLlvmType(patioResume.value(), context), + patioOperandToLlvmOperand(patioResume.value(), context) + )); + case PatioReturn patioReturn -> context.addTerminatorInstruction(new LlvmReturn( patioOperandToLlvmType(patioReturn.value(), context), 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 e276b2a0..3f7e7eca 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java @@ -305,6 +305,14 @@ public class WasmPatioCodeGen { yield context; } + case PatioResume patioResume -> { + context = generateOperand(patioResume.value(), context); + + context = context.addInstruction(new WasmReturn()); + + yield context; + } + case PatioReturn patioReturn -> { context = generateOperand(patioReturn.value(), context); diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 1fb32e5c..e2f1b6ba 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -545,6 +545,14 @@ public class PatioCodeGen { case PushUnitInstruction pushUnit -> pushValue(0, pushUnit.source(), context); + case ResumeInstruction resume -> { + var valueResult = context.temporaryPop(); + var value = valueResult.value(); + context = valueResult.context(); + + yield context.addInstruction(new PatioResume(value, resume.source())); + } + case ReturnInstruction return_ -> { var valueResult = context.temporaryPop(); var value = valueResult.value(); @@ -963,6 +971,8 @@ public class PatioCodeGen { } case PushUnitInstruction pushUnitInstruction -> { } + case ResumeInstruction resumeInstruction -> { + } case ReturnInstruction returnInstruction -> { } case StringAddInstruction stringAddInstruction -> { 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 aba2a732..9ccc2766 100644 --- a/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java +++ b/src/main/java/org/shedlang/compiler/patio/ast/PatioSerializer.java @@ -289,6 +289,12 @@ public class PatioSerializer { treeNamed("value", operandToTree(memoryStore.value())) ); + case PatioResume resume -> + treeList( + treeIdentifier("resume"), + operandToTree(resume.value()) + ); + case PatioReturn return_ -> treeList( treeIdentifier("return"), 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 3230b622..c02a6041 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, PatioAllocStack, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioReturn, PatioSwitch, PatioThrow, PatioTry, PatioUnreachable, PatioVarLoad, PatioVarStore { +public sealed interface PatioInstruction permits PatioAllocHeap, PatioAllocStack, PatioBoolToInt, PatioCall, PatioCallPointer, PatioDataPtrToInt, PatioIf, PatioIntAdd, PatioIntEquals, PatioIntNotEqual, PatioIntSub, PatioIntToBool, PatioIntToDataPtr, PatioLocalStore, PatioLoop, PatioLoopBreak, PatioLoopContinue, PatioMemoryLoad, PatioMemoryStore, PatioResume, PatioReturn, PatioSwitch, PatioThrow, PatioTry, PatioUnreachable, PatioVarLoad, PatioVarStore { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioResume.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioResume.java new file mode 100644 index 00000000..3f19908f --- /dev/null +++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioResume.java @@ -0,0 +1,11 @@ +package org.shedlang.compiler.patio.ast.instructions; + +import org.shedlang.compiler.patio.ast.operands.PatioOperand; +import org.shedlang.compiler.sources.Source; + +/** + * Return with the given value from the effect handler method back to the effect + * call. + */ +public record PatioResume(PatioOperand value, Source source) implements PatioInstruction { +} diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index 86e64df5..2cf812c4 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -205,6 +205,11 @@ public class InstructionSerializer { treeIdentifier("push-unit") ); + case ResumeInstruction resumeInstruction -> + treeList( + treeIdentifier("resume") + ); + case ReturnInstruction returnInstruction -> treeList( treeIdentifier("return") diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index a6da0635..2e2b43b7 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -500,7 +500,7 @@ public class StackCompiler { private static Context compileResume(TypedResumeNode node, Context context) { context = compileExpression(node.expression(), context); - context = context.addInstruction(new ReturnInstruction(node.source())); + context = context.addInstruction(new ResumeInstruction(node.source())); return context; } 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 30216032..f84de210 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, DefineEffectInstruction, DefineFunctionInstruction, DefineStructInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, SwitchInstruction, TryInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction { + permits CallInstruction, DefineEffectInstruction, DefineFunctionInstruction, DefineStructInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ResumeInstruction, ReturnInstruction, StringAddInstruction, SwitchInstruction, TryInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java new file mode 100644 index 00000000..836b5bac --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java @@ -0,0 +1,10 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Pop a value from the temporary value stack and return with that value from + * the current effect handler method back to the effect call. + */ +public record ResumeInstruction(Source source) implements Instruction { +} -- cgit v1.2.3