diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-30 17:08:57 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-30 17:08:57 +0100 |
| commit | c4bd013b53d3d00db54844573c8254bfbb504cde (patch) | |
| tree | ccfb3d8cd18ea9031bc7ed0f4267d0e25489eddd /src/main/java/org/shedlang/compiler/stackir | |
| parent | 3227422c3f62cba3c414b5ce0bf71137553cdf6b (diff) | |
Add var-load and var-store to Stack IR
Diffstat (limited to 'src/main/java/org/shedlang/compiler/stackir')
4 files changed, 32 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index 0f6881ae..807caa57 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -157,6 +157,18 @@ public class InstructionSerializer { case StringAddInstruction stringAddInstruction -> treeList(treeIdentifier("string-add")); + + case VarLoadInstruction varLoadInstruction -> + treeList( + treeIdentifier("var-load"), + treeString(varLoadInstruction.name()) + ); + + case VarStoreInstruction varStoreInstruction -> + treeList( + treeIdentifier("var-store"), + treeString(varStoreInstruction.name()) + ); }; } } 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 80593264..b4a59136 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 { + permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IfInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction, VarLoadInstruction, VarStoreInstruction { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/VarLoadInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/VarLoadInstruction.java new file mode 100644 index 00000000..2ca49d90 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/VarLoadInstruction.java @@ -0,0 +1,10 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Load the value stored in the var with the given name and push it onto the + * stack. + */ +public record VarLoadInstruction(String name, Source source) implements Instruction { +} diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/VarStoreInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/VarStoreInstruction.java new file mode 100644 index 00000000..b355d39c --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/VarStoreInstruction.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.stackir.instructions; + +import org.shedlang.compiler.sources.Source; + +/** + * Pop a value from the stack and store it in the var with the given name. + */ +public record VarStoreInstruction(String name, Source source) implements Instruction { +} |
