diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-11-07 19:47:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-11-07 19:47:34 +0000 |
| commit | 7488757d0386b7770d321177e2c893ba1c060f26 (patch) | |
| tree | cc6ecf2f0def11291bfe93faa99c73abf0adb1d4 /src/main/java | |
| parent | 965936c9d2983fae0201412df1d7adf4a1bc1c66 (diff) | |
Add define-effect instruction
Diffstat (limited to 'src/main/java')
4 files changed, 39 insertions, 11 deletions
diff --git a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java index 6955def1..19b479a5 100644 --- a/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java +++ b/src/main/java/org/shedlang/compiler/patio/PatioCodeGen.java @@ -194,6 +194,10 @@ public class PatioCodeGen { ).temporaryPush(target); } + case DefineEffectInstruction defineEffect -> { + throw new UnsupportedOperationException("TODO"); + } + case DefineFunctionInstruction defineFunction -> { var targetResult = context.allocateVal( PatioCodeGenLocalSpace.CLOSURE, diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index fb2139f5..ad180d60 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -3,6 +3,7 @@ package org.shedlang.compiler.stackir; import org.shedlang.compiler.stackir.instructions.*; import org.shedlang.compiler.treetext.Tree; import org.shedlang.compiler.types.StructType; +import org.shedlang.compiler.types.UserDefinedEffect; import java.util.List; @@ -33,7 +34,7 @@ public class InstructionSerializer { private static Tree instructionToTree(Instruction instruction) { return switch (instruction) { - case CallInstruction callInstruction -> + case CallInstruction callInstruction -> treeList( treeIdentifier("call"), treeNamed( @@ -49,8 +50,14 @@ public class InstructionSerializer { ) ) ); - - case DefineFunctionInstruction defineFunctionInstruction -> + + case DefineEffectInstruction defineEffectInstruction -> + treeList( + treeIdentifier("define-effect"), + userDefinedEffectToTree(defineEffectInstruction.effect()) + ); + + case DefineFunctionInstruction defineFunctionInstruction -> treeListIndented( treeIdentifier("define-function"), treeNamed("name", treeString(defineFunctionInstruction.name())), @@ -82,7 +89,7 @@ public class InstructionSerializer { .toList() )) ); - + case DiscardInstruction discardInstruction -> treeList( treeIdentifier("discard") @@ -131,13 +138,13 @@ public class InstructionSerializer { case IntSubInstruction intSub -> treeList(treeIdentifier("int-sub")); - + case LocalLoadInstruction localLoadInstruction -> treeList( treeIdentifier("local-load"), treeString(localLoadInstruction.name()) ); - + case LocalStoreInstruction localStoreInstruction -> treeList( treeIdentifier("local-store"), @@ -155,7 +162,7 @@ public class InstructionSerializer { case LoopContinueInstruction loopContinue -> treeList(treeIdentifier("continue")); - + case NamespaceFieldStoreInstruction namespaceFieldStore -> treeList( treeIdentifier("namespace-field-store"), @@ -168,19 +175,19 @@ public class InstructionSerializer { treeIdentifier("namespace-import"), treeString(namespaceImport.namespaceName().toString()) ); - + case PushBoolInstruction pushBoolInstruction -> treeList( treeIdentifier("push-bool"), treeIdentifier(Boolean.toString(pushBoolInstruction.value())) ); - + case PushIntInstruction pushIntInstruction -> treeList( treeIdentifier("push-int"), treeIdentifier(pushIntInstruction.value().toString()) ); - + case PushStringInstruction pushStringInstruction -> treeList( treeIdentifier("push-string"), @@ -240,6 +247,10 @@ public class InstructionSerializer { }; } + private static Tree userDefinedEffectToTree(UserDefinedEffect effect) { + return treeString(effect.describe()); + } + private static Tree structTypeToTree(StructType type) { return treeString(type.describe()); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/DefineEffectInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/DefineEffectInstruction.java new file mode 100644 index 00000000..e6bedda6 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/DefineEffectInstruction.java @@ -0,0 +1,13 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.UserDefinedEffect; + +/** + * Push an effect value onto the stack with the given definition. + */ +public record DefineEffectInstruction( + UserDefinedEffect effect, + Source source +) implements Instruction { +} 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 2966e8a7..fd47da6b 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, DefineStructInstruction, DiscardInstruction, DuplicateInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, LoopBreakInstruction, LoopContinueInstruction, LoopInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, SwitchInstruction, 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, ReturnInstruction, StringAddInstruction, SwitchInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction { Source source(); } |
