aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/stackir
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-02 19:47:35 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-02 19:47:35 +0000
commit07d62885c5150fe6eadfa9372a190dd73fd754cc (patch)
tree17959fa67c7ab5d375f3966003358afc0715b210 /src/main/java/org/shedlang/compiler/stackir
parent0ad5be30c1d301942f8cec567d0719fd9ff18f3f (diff)
Rename stack IR to Shid
Diffstat (limited to 'src/main/java/org/shedlang/compiler/stackir')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java280
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java848
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java12
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/CallInstruction.java25
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/DefineEffectInstruction.java14
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/DefineFunctionInstruction.java105
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/DefineStructInstruction.java21
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/DiscardInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/DuplicateInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java22
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/IfInstruction.java22
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/IntAddInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/IntEqualsInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java11
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LocalStoreInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java17
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceFieldStoreInstruction.java15
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceImportInstruction.java13
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/PushBoolInstruction.java12
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java11
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/PushStringInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/PushUnitInstruction.java9
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/ReturnInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java12
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.java21
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java26
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java14
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java14
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/VarLoadInstruction.java10
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/VarStoreInstruction.java10
36 files changed, 0 insertions, 1668 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
deleted file mode 100644
index 2cf812c4..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
+++ /dev/null
@@ -1,280 +0,0 @@
-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;
-
-import static org.shedlang.compiler.treetext.TreeSerializer.serializeTree;
-import static org.shedlang.compiler.treetext.Trees.*;
-
-public class InstructionSerializer {
- private InstructionSerializer() {
- }
-
- public static void serializeInstructions(List<Instruction> instructions, StringBuilder builder) {
- var tree = instructionsToTree(instructions);
- serializeTree(tree, builder, 0);
- }
-
- public static void serializeInstruction(Instruction instruction, StringBuilder builder) {
- var tree = instructionToTree(instruction);
- serializeTree(tree, builder, 0);
- }
-
- private static Tree instructionsToTree(List<? extends Instruction> instructions) {
- return treeListIndented(
- instructions.stream()
- .map(instruction -> instructionToTree(instruction))
- .toList()
- );
- }
-
- private static Tree instructionToTree(Instruction instruction) {
- return switch (instruction) {
- case CallInstruction callInstruction ->
- treeList(
- treeIdentifier("call"),
- treeNamed(
- "positional-arg-count",
- treeInt(callInstruction.positionalArgCount())
- ),
- treeNamed(
- "named-arg-names",
- treeList(
- callInstruction.namedArgNames().stream()
- .map(arg -> treeString(arg))
- .toList()
- )
- )
- );
-
- case DefineEffectInstruction defineEffectInstruction ->
- treeList(
- treeIdentifier("define-effect"),
- userDefinedEffectToTree(defineEffectInstruction.effect())
- );
-
- case DefineFunctionInstruction defineFunctionInstruction ->
- treeListIndented(
- treeIdentifier("define-function"),
- treeNamed("name", treeString(defineFunctionInstruction.name())),
- treeNamed("positional-params", treeList(
- defineFunctionInstruction.positionalParams().stream()
- .map(param -> treeString(param.name()))
- .toList()
- )),
- treeNamed("named-params", treeList(
- defineFunctionInstruction.namedParams().stream()
- .map(param -> treeString(param.name()))
- .toList()
- )),
- treeNamed("free-variables", treeList(
- defineFunctionInstruction.freeVariables().stream()
- .map(freeVariable -> treeString(freeVariable.name()))
- .toList()
- )),
- treeNamed("body", instructionsToTree(defineFunctionInstruction.body()))
- );
-
- case DefineStructInstruction defineStructInstruction ->
- treeListIndented(
- treeIdentifier("define-struct"),
- treeNamed("type", structTypeToTree(defineStructInstruction.type())),
- treeNamed("fields", treeList(
- defineStructInstruction.fields().stream()
- .map(field -> treeString(field.name()))
- .toList()
- ))
- );
-
- case DiscardInstruction discardInstruction ->
- treeList(
- treeIdentifier("discard")
- );
-
- case DuplicateInstruction duplicate ->
- treeList(
- treeIdentifier("duplicate")
- );
-
- case FieldLoadInstruction fieldLoad ->
- treeList(
- treeIdentifier("field-load"),
- switch (fieldLoad.receiver()) {
- case FieldLoadInstruction.Receiver.Effect effect ->
- treeList(
- treeIdentifier("effect"),
- userDefinedEffectToTree(effect.effect())
- );
-
- case FieldLoadInstruction.Receiver.Namespace namespace ->
- treeList(
- treeIdentifier("namespace"),
- treeString(namespace.name().toString())
- );
-
- case FieldLoadInstruction.Receiver.Struct struct ->
- treeList(
- treeIdentifier("struct"),
- structTypeToTree(struct.type())
- );
- },
- treeString(fieldLoad.fieldName())
- );
-
- case IfInstruction if_ ->
- treeListIndented(
- treeIdentifier("if"),
- treeNamed("result-count", treeInt(if_.resultCount())),
- instructionsToTree(if_.trueBody()),
- instructionsToTree(if_.falseBody())
- );
-
- case IntAddInstruction intAdd ->
- treeList(treeIdentifier("int-add"));
-
- case IntEqualsInstruction intEquals ->
- treeList(treeIdentifier("int-eq"));
-
- case IntNotEqualInstruction intNotEqual ->
- treeList(treeIdentifier("int-ne"));
-
- case IntSubInstruction intSub ->
- treeList(treeIdentifier("int-sub"));
-
- case LocalLoadInstruction localLoadInstruction ->
- treeList(
- treeIdentifier("local-load"),
- treeString(localLoadInstruction.name())
- );
-
- case LocalStoreInstruction localStoreInstruction ->
- treeList(
- 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(
- treeIdentifier("namespace-field-store"),
- treeString(namespaceFieldStore.namespaceName().toString()),
- treeString(namespaceFieldStore.fieldName())
- );
-
- case NamespaceImportInstruction namespaceImport ->
- treeList(
- 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"),
- treeString(pushStringInstruction.value())
- );
-
- case PushUnitInstruction pushUnitInstruction ->
- treeList(
- treeIdentifier("push-unit")
- );
-
- case ResumeInstruction resumeInstruction ->
- treeList(
- treeIdentifier("resume")
- );
-
- case ReturnInstruction returnInstruction ->
- treeList(
- treeIdentifier("return")
- );
-
- case StringAddInstruction stringAddInstruction ->
- treeList(treeIdentifier("string-add"));
-
- case SwitchInstruction switchInstruction ->
- treeListIndented(
- treeIdentifier("switch"),
- treeListIndented(
- switchInstruction.cases().stream()
- .map(switchCase -> treeListIndented(
- structTypeToTree(switchCase.caseType()),
- instructionsToTree(switchCase.body())
- ))
- .toList()
- )
- );
-
- case TryInstruction tryInstruction ->
- treeListIndented(
- treeIdentifier("try"),
- userDefinedEffectToTree(tryInstruction.handledEffect()),
- treeListIndented(
- tryInstruction.handlerMethodNames().stream()
- .map(name -> treeString(name))
- .toList()
- ),
- instructionsToTree(tryInstruction.body())
- );
-
- case TupleCreateInstruction tupleCreateInstruction ->
- treeList(
- treeIdentifier("tuple-create"),
- treeInt(tupleCreateInstruction.elementCount())
- );
-
- case TupleGetInstruction tupleGetInstruction ->
- treeList(
- treeIdentifier("tuple-get"),
- treeInt(tupleGetInstruction.elementIndex()),
- treeInt(tupleGetInstruction.elementCount())
- );
-
- case VarLoadInstruction varLoadInstruction ->
- treeList(
- treeIdentifier("var-load"),
- treeString(varLoadInstruction.name())
- );
-
- case VarStoreInstruction varStoreInstruction ->
- treeList(
- treeIdentifier("var-store"),
- treeString(varStoreInstruction.name())
- );
- };
- }
-
- 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/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
deleted file mode 100644
index 2e2b43b7..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ /dev/null
@@ -1,848 +0,0 @@
-package org.shedlang.compiler.stackir;
-
-import org.pcollections.*;
-import org.shedlang.compiler.ShedBuiltins;
-import org.shedlang.compiler.ast.typed.*;
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.stackir.instructions.*;
-import org.shedlang.compiler.types.*;
-import org.shedlang.compiler.util.Iterables;
-import org.shedlang.compiler.util.P;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class StackCompiler {
- private StackCompiler() {
- }
-
- private static Context compileAssign(TypedAssignNode node, Context context) {
- context = compileExpression(node.expression(), context);
- context = compileTarget(node.target(), context);
- return context;
- }
-
- private static Context compileBinaryOperation(
- TypedBinaryOperationNode node,
- Context context
- ) {
- context = compileExpression(node.left(), context);
-
- context = compileExpression(node.right(), context);
-
- context = context.addInstruction(switch (node.operator()) {
- case INT_ADD ->
- new IntAddInstruction(node.source());
- case INT_EQUALS ->
- new IntEqualsInstruction(node.source());
- case INT_NOT_EQUAL ->
- new IntNotEqualInstruction(node.source());
- case INT_SUB ->
- new IntSubInstruction(node.source());
- case STRING_ADD ->
- new StringAddInstruction(node.source());
- });
-
- return context;
- }
-
- static Context compileBlockWithYield(TypedBlockNode node, Context context) {
- for (var statement : node.statements()) {
- context = compileFunctionStatement(statement, context);
- }
-
- switch (node.behaviour()) {
- case TypedFunctionStatementBehaviour.BlockReturn blockReturn -> {
- }
- case TypedFunctionStatementBehaviour.Continue continue_ -> {
- context = context.addInstruction(new PushUnitInstruction(node.source()));
- }
- case TypedFunctionStatementBehaviour.Terminate terminate -> {
- }
- }
-
- return context;
- }
-
- static Context compileBlockWithoutYield(TypedBlockNode node, Context context) {
- for (var statement : node.statements()) {
- context = compileFunctionStatement(statement, context);
- }
-
- switch (node.behaviour()) {
- case TypedFunctionStatementBehaviour.BlockReturn blockReturn -> {
- context = context.addInstruction(new DiscardInstruction(node.source()));
- }
- case TypedFunctionStatementBehaviour.Continue continue_ -> {
- }
- case TypedFunctionStatementBehaviour.Terminate terminate -> {
- }
- }
-
- return context;
- }
-
- private static Context compileBlockReturn(TypedBlockReturnNode blockReturn, Context context) {
- return compileExpression(blockReturn.expression(), context);
- }
-
- private static Context compileBoolLiteral(TypedBoolLiteralNode node, Context context) {
- return context.addInstruction(new PushBoolInstruction(node.value(), node.source()));
- }
-
- private static Context compileCall(TypedCallNode node, Context context) {
- context = compileExpression(node.receiver(), context);
-
- for (var arg : node.args().positional()) {
- context = compileExpression(arg, context);
- }
-
- for (var arg : node.args().named()) {
- context = compileExpression(arg.expression(), context);
- }
-
- var callInstruction = new CallInstruction(
- node.args().positional().size(),
- node.args().named().stream()
- .map(arg -> arg.name())
- .toList(),
- node.source()
- );
-
- context = context.addInstruction(callInstruction);
-
- return context;
- }
-
- private static Context compileEffectDefinition(
- TypedEffectDefinitionNode node,
- Context context
- ) {
- var localName = context.shedVariableToLocal(node.effect().name());
-
- return context
- .addInstruction(new DefineEffectInstruction(node.effect(), node.source()))
- .addInstruction(new LocalStoreInstruction(localName, node.source()));
- }
-
- public static Context compileExpression(TypedExpressionNode node, Context context) {
- return switch (node) {
- case TypedBinaryOperationNode typedBinaryOperationNode ->
- compileBinaryOperation(typedBinaryOperationNode, context);
-
- case TypedBoolLiteralNode typedBoolLiteralNode ->
- compileBoolLiteral(typedBoolLiteralNode, context);
-
- case TypedCallNode typedCallNode ->
- compileCall(typedCallNode, context);
-
- case TypedFieldAccessNode typedFieldAccessNode ->
- compileFieldAccess(typedFieldAccessNode, context);
-
- case TypedFunctionExpressionNode typedFunctionExpressionNode ->
- compileFunctionExpression(typedFunctionExpressionNode, context);
-
- case TypedIfNode typedIfNode ->
- compileIf(typedIfNode, context);
-
- case TypedIntLiteralNode typedIntLiteralNode ->
- compileIntLiteral(typedIntLiteralNode, context);
-
- case TypedReferenceNode typedReferenceNode ->
- compileReference(typedReferenceNode, context);
-
- case TypedStringLiteralNode typedStringLiteralNode ->
- compileStringLiteral(typedStringLiteralNode, context);
-
- case TypedTryNode typedTryNode ->
- compileTry(typedTryNode, context);
-
- case TypedTupleNode typedTupleNode ->
- compileTuple(typedTupleNode, context);
-
- case TypedUnitLiteralNode typedUnitLiteralNode ->
- compileUnitLiteral(typedUnitLiteralNode, context);
-
- case TypedWhenNode typedWhenNode ->
- compileWhen(typedWhenNode, context);
- };
- }
-
- private static Context compileExpressionStatement(
- TypedExpressionStatementNode node,
- Context context
- ) {
- context = compileExpression(node.expression(), context);
- context = context.addInstruction(new DiscardInstruction(node.source()));
- return context;
- }
-
- private static Context compileFieldAccess(
- TypedFieldAccessNode node,
- Context context
- ) {
- context = compileExpression(node.receiver(), context);
- // TODO: handle not a type with fields
- var receiver = switch (node.receiver().type()) {
- case NamespaceType namespaceType ->
- new FieldLoadInstruction.Receiver.Namespace(namespaceType.namespaceName());
-
- case StructType structType ->
- new FieldLoadInstruction.Receiver.Struct(structType);
-
- case TypeLevelValueType typeLevelValueType -> {
- if (typeLevelValueType.value() instanceof UserDefinedEffect effect) {
- yield new FieldLoadInstruction.Receiver.Effect(effect);
- } else {
- throw new RuntimeException("not a type with fields");
- }
- }
-
- default ->
- throw new RuntimeException("not a type with fields");
- };
-
- context = context.addInstruction(new FieldLoadInstruction(
- receiver,
- node.fieldName(),
- node.source()
- ));
- return context;
- }
-
- private static Context compileFunction(
- String name,
- TypedParamsNode params,
- TypedFunctionBodyNode body,
- Source source,
- Context context
- ) {
- context = context.enterBlock(body.boundVariables());
-
- var c = context;
-
- var positionalParams = params.positional().stream()
- .map(param -> new DefineFunctionInstruction.Param(
- c.shedVariableToLocal(param.name()),
- param.source()
- ))
- .toList();
-
- var namedParams = params.named().stream()
- .map(param -> new DefineFunctionInstruction.Param(
- c.shedVariableToLocal(param.name()),
- param.source()
- ))
- .toList();
-
- context = compileBlockWithYield(body.block(), context);
-
- switch (body.behaviour()) {
- case TypedFunctionStatementBehaviour.BlockReturn blockReturn -> {
- context = context.addInstruction(new ReturnInstruction(body.source()));
- }
- case TypedFunctionStatementBehaviour.Continue continue_ -> {
- context = context.addInstruction(new ReturnInstruction(body.source()));
- }
- case TypedFunctionStatementBehaviour.Terminate terminate -> {
- }
- }
-
- var bodyInstructions = context.instructions;
-
- context = context.exitBlock();
-
- var freeVariables = FreeVariables.builder();
- for (var shedFreeVariable : body.freeVariables()) {
- var freeVariableLocalName = context.shedVariableToLocal(shedFreeVariable.name());
- for (var freeVariableSource : shedFreeVariable.referenceSources()) {
- freeVariables = freeVariables.plus(
- freeVariableLocalName,
- freeVariableSource
- );
- }
- }
-
- var defineFunctionInstruction = new DefineFunctionInstruction(
- name,
- positionalParams,
- namedParams,
- freeVariables.build(),
- bodyInstructions,
- source
- );
-
- return context.addInstruction(defineFunctionInstruction);
- }
-
- private static Context compileFunctionDefinition(
- TypedFunctionDefinitionNode node,
- Context context
- ) {
- context = compileFunction(
- node.name(),
- node.signature().params(),
- node.body(),
- node.source(),
- context
- );
-
- var localName = context.shedVariableToLocal(node.name());
- var storeInstruction = new LocalStoreInstruction(localName, node.source());
-
- return context.addInstruction(storeInstruction);
- }
-
- private static Context compileFunctionExpression(
- TypedFunctionExpressionNode node,
- Context context
- ) {
- return compileFunction(
- // TODO: include source location?
- "anonymous",
- node.signature().params(),
- node.body(),
- node.source(),
- context
- );
- }
-
- private static Context compileFunctionReturn(
- TypedFunctionReturnNode node,
- Context context
- ) {
- context = compileExpression(node.expression(), context);
- context = context.addInstruction(new ReturnInstruction(node.source()));
- return context;
- }
-
- public static Context compileFunctionStatement(
- TypedFunctionStatementNode node,
- Context context
- ) {
- return switch (node) {
- case TypedAssignNode assign ->
- compileAssign(assign, context);
-
- case TypedBlockReturnNode blockReturn ->
- compileBlockReturn(blockReturn, context);
-
- case TypedExpressionStatementNode expressionStatement ->
- compileExpressionStatement(expressionStatement, context);
-
- case TypedFunctionReturnNode functionReturn ->
- compileFunctionReturn(functionReturn, context);
-
- case TypedResumeNode resume ->
- compileResume(resume, context);
-
- case TypedWhileNode while_ ->
- compileWhile(while_, context);
- };
- }
-
- private static Context compileIf(TypedIfNode node, Context context) {
- return compileIf(
- node.conditionalBranches(),
- node.elseBody(),
- context
- );
- }
-
- private static Context compileIf(
- List<TypedConditionalBranchNode> conditionalBranches,
- TypedBlockNode elseBody,
- Context context
- ) {
- if (conditionalBranches.isEmpty()) {
- context = context.enterBlock(elseBody.boundVariables());
- context = compileBlockWithYield(elseBody, context);
- var elseInstructions = context.instructions;
- context = context.exitBlock();
- for (var instruction : elseInstructions) {
- context = context.addInstruction(instruction);
- }
- } else {
- var conditionalBranch = conditionalBranches.get(0);
- context = compileExpression(conditionalBranch.condition(), context);
-
- context = context.enterBlock(conditionalBranch.body().boundVariables());
- context = compileBlockWithYield(conditionalBranch.body(), context);
- var ifTrueInstructions = context.instructions;
- context = context.exitBlock();
-
- context = context.enterBlock(List.of());
- context = compileIf(
- conditionalBranches.subList(1, conditionalBranches.size()),
- elseBody,
- context
- );
- var ifFalseInstructions = context.instructions;
- context = context.exitBlock();
-
- context = context.addInstruction(new IfInstruction(
- 1,
- ifTrueInstructions,
- ifFalseInstructions,
- conditionalBranch.source()
- ));
- }
-
- return context;
- }
-
- private static Context compileIntLiteral(TypedIntLiteralNode node, Context context) {
- return context.addInstruction(new PushIntInstruction(node.value(), node.source()));
- }
-
- public static StackIrNamespace compileNamespace(TypedNamespaceNode node) {
- var context = Context.EMPTY;
-
- context = context.addShedVal(ShedBuiltins.PRINT_NAME);
- var printLocalName = context.shedVariableToLocal(ShedBuiltins.PRINT_NAME);
-
- context = context
- .addInstruction(new NamespaceImportInstruction(ShedBuiltins.NAMESPACE_NAME, node.source()))
- .addInstruction(new FieldLoadInstruction(
- new FieldLoadInstruction.Receiver.Namespace(ShedBuiltins.NAMESPACE_NAME),
- ShedBuiltins.PRINT_NAME,
- node.source()
- ))
- .addInstruction(new LocalStoreInstruction(printLocalName, node.source()));
-
- for (var boundVariable : node.boundVariables()) {
- context = context.addShedVariable(boundVariable);
- }
-
- for (var importNode : node.imports()) {
- var localName = context.shedVariableToLocal(importNode.variableName());
- context = context
- .addInstruction(new NamespaceImportInstruction(
- importNode.namespaceName(),
- importNode.source()
- ))
- .addInstruction(new LocalStoreInstruction(
- localName,
- importNode.source()
- ));
-
- }
-
- var statementsAfterHoisting = Iterables.sortedBy(node.body(), statement -> switch (statement) {
- case TypedAssignNode assign ->
- 1;
-
- case TypedEffectDefinitionNode effectDefinition ->
- 0;
-
- case TypedFunctionDefinitionNode functionDefinition ->
- 1;
-
- case TypedStructDefinitionNode structDefinition ->
- 0;
-
- case TypedSumTypeDefinitionNode sumTypeDefinition ->
- 0;
- });
- for (var statement : statementsAfterHoisting) {
- context = compileNamespaceStatement(statement, context);
- }
-
- for (var fieldName : node.fieldNames()) {
- var localName = context.shedVariableToLocal(fieldName);
-
- context = context
- .addInstruction(new LocalLoadInstruction(localName, node.source()))
- .addInstruction(new NamespaceFieldStoreInstruction(
- node.namespaceName(),
- fieldName,
- node.source()
- ));
- }
-
- return new StackIrNamespace(node.namespaceName(), context.instructions);
- }
-
- public static Context compileNamespaceStatement(TypedNamespaceStatementNode node, Context context) {
- return switch (node) {
- case TypedAssignNode assign ->
- compileAssign(assign, context);
-
- case TypedEffectDefinitionNode effectDefinition ->
- compileEffectDefinition(effectDefinition, context);
-
- case TypedFunctionDefinitionNode functionDefinition ->
- compileFunctionDefinition(functionDefinition, context);
-
- case TypedStructDefinitionNode structDefinition ->
- compileStructDefinition(structDefinition, context);
-
- case TypedSumTypeDefinitionNode sumDefinition ->
- compileSumTypeDefinition(sumDefinition, context);
- };
- }
-
- private static Context compileReference(TypedReferenceNode node, Context context) {
- var shedVariable = context.shedVariable(node.name());
-
- if (shedVariable.isVar) {
- return context.addInstruction(new VarLoadInstruction(
- shedVariable.stackIrLocalName,
- node.source()
- ));
- } else {
- return context.addInstruction(new LocalLoadInstruction(
- shedVariable.stackIrLocalName,
- node.source()
- ));
- }
- }
-
- private static Context compileResume(TypedResumeNode node, Context context) {
- context = compileExpression(node.expression(), context);
- context = context.addInstruction(new ResumeInstruction(node.source()));
- return context;
- }
-
- private static Context compileStringLiteral(
- TypedStringLiteralNode node,
- Context context
- ) {
- var instruction = new PushStringInstruction(node.value(), node.source());
- return context.addInstruction(instruction);
- }
-
- private static Context compileStructDefinition(
- TypedStructDefinitionNode node,
- Context context
- ) {
- var defineStructInstruction = new DefineStructInstruction(
- node.type(),
- node.fields().stream()
- .map(field -> new DefineStructInstruction.Field(field.name(), field.source()))
- .toList(),
- node.source()
- );
-
- var localName = context.shedVariableToLocal(node.type().name());
- var storeInstruction = new LocalStoreInstruction(localName, node.source());
-
- return context
- .addInstruction(defineStructInstruction)
- .addInstruction(storeInstruction);
- }
-
- private static Context compileSumTypeDefinition(
- TypedSumTypeDefinitionNode node,
- Context context
- ) {
- var defineSumTypeInstruction = new PushUnitInstruction(node.source());
-
- var localName = context.shedVariableToLocal(node.type().name());
- var storeInstruction = new LocalStoreInstruction(localName, node.source());
-
- return context
- .addInstruction(defineSumTypeInstruction)
- .addInstruction(storeInstruction);
- }
-
- private static Context compileTarget(
- TypedTargetNode target,
- Context context
- ) {
- return switch (target) {
- case TypedTargetNode.Ignore ignore -> {
- yield context.addInstruction(new DiscardInstruction(ignore.source()));
- }
-
- case TypedTargetNode.Reassign reassign -> {
- context = context.addInstruction(
- new VarStoreInstruction(
- context.shedVariableToLocal(reassign.name()),
- reassign.source()
- )
- );
-
- yield context;
- }
-
- case TypedTargetNode.Tuple tuple -> {
- if (tuple.elements().size() == 0) {
- context = context.addInstruction(new DiscardInstruction(tuple.source()));
- }
-
- for (var elementIndex = 0; elementIndex < tuple.elements().size(); elementIndex++) {
- if (elementIndex < tuple.elements().size() - 1) {
- context = context
- .addInstruction(new DuplicateInstruction(tuple.source()));
- }
-
- context = context
- .addInstruction(new TupleGetInstruction(
- elementIndex,
- tuple.elements().size(),
- tuple.source()
- ));
-
- context = compileTarget(tuple.elements().get(elementIndex), context);
- }
-
- yield context;
- }
-
- case TypedTargetNode.Val val -> {
- context = context.addInstruction(
- new LocalStoreInstruction(
- context.shedVariableToLocal(val.name()),
- val.source()
- )
- );
-
- yield context;
- }
-
- case TypedTargetNode.Var var -> {
- context = context.addInstruction(
- new VarStoreInstruction(
- context.shedVariableToLocal(var.name()),
- var.source()
- )
- );
-
- yield context;
- }
- };
- }
-
- private static Context compileTry(TypedTryNode node, Context context) {
- for (var handlerMethod : node.handlerMethods()) {
- context = compileTryHandlerMethod(handlerMethod, context);
- }
-
- context = context.enterBlock(node.body().boundVariables());
- context = compileBlockWithYield(node.body(), context);
- var bodyInstructions = context.instructions;
- context = context.exitBlock();
-
- var instruction = new TryInstruction(
- node.handledEffect().value(),
- node.handlerMethods().stream()
- .map(methodNode -> methodNode.name())
- .toList(),
- bodyInstructions,
- node.source()
- );
-
- return context.addInstruction(instruction);
- }
-
- private static Context compileTryHandlerMethod(
- TypedTryHandlerMethodNode node,
- Context context
- ) {
- return compileFunction(
- node.name(),
- node.params(),
- node.body(),
- node.source(),
- context
- );
- }
-
- private static Context compileTuple(TypedTupleNode node, Context context) {
- for (var elementNode : node.elements()) {
- context = compileExpression(elementNode, context);
- }
-
- var instruction = new TupleCreateInstruction(node.elements().size(), node.source());
- return context.addInstruction(instruction);
- }
-
- private static Context compileUnitLiteral(
- TypedUnitLiteralNode node,
- Context context
- ) {
- return context.addInstruction(new PushUnitInstruction(node.source()));
- }
-
- private static Context compileWhen(TypedWhenNode node, Context context) {
- context = compileExpression(node.expression(), context);
-
- var switchCases = new ArrayList<SwitchInstruction.Case>();
-
- for (var whenCase : node.cases()) {
- context = context.enterBlock(whenCase.body().boundVariables());
- context = compileBlockWithYield(whenCase.body(), context);
- var caseBodyInstructions = context.instructions;
- context = context.exitBlock();
-
- var switchCase = new SwitchInstruction.Case(
- whenCase.caseType().value(),
- caseBodyInstructions,
- whenCase.source()
- );
- switchCases.add(switchCase);
- }
-
- context = context.addInstruction(new SwitchInstruction(
- switchCases,
- node.source()
- ));
-
- return context;
- }
-
- private static Context compileWhile(TypedWhileNode node, Context context) {
- context = context.enterBlock(node.body().boundVariables());
- context = compileBlockWithoutYield(node.body(), context);
- var ifTrueInstructions = context.instructions;
- context = context.exitBlock();
-
- context = context.enterBlock(List.of());
-
- context = compileExpression(node.condition(), context);
- context = context.addInstruction(new IfInstruction(
- 0,
- List.of(),
- List.of(
- new LoopBreakInstruction(node.source())
- ),
- node.source()
- ));
-
- context = context.addInstructions(ifTrueInstructions);
-
- context = context.addInstruction(new LoopContinueInstruction(node.source()));
-
- var bodyInstructions = context.instructions;
- context = context.exitBlock();
-
- context = context.addInstruction(new LoopInstruction(bodyInstructions, node.source()));
-
- return context;
- }
-
- public static class Context {
- public static final Context EMPTY = new Context(
- P.stack(),
- P.vector(),
- Empty.set(),
- P.stack(),
- P.map()
- );
-
- private final PStack<PVector<Instruction>> instructionsStack;
- private final PVector<Instruction> instructions;
- private final PSet<String> localNames;
- private final PStack<PMap<String, ShedVariable>> shedVariablesByNameStack;
- private final PMap<String, ShedVariable> shedVariablesByName;
-
- private Context(
- PStack<PVector<Instruction>> instructionsStack,
- PVector<Instruction> instructions,
- PSet<String> localNames,
- PStack<PMap<String, ShedVariable>> shedVariablesByNameStack,
- PMap<String, ShedVariable> shedVariablesByName
- ) {
- this.instructionsStack = instructionsStack;
- this.instructions = instructions;
- this.localNames = localNames;
- this.shedVariablesByNameStack = shedVariablesByNameStack;
- this.shedVariablesByName = shedVariablesByName;
- }
-
- public Context enterBlock(List<BoundVariable> shedVariables) {
- var context = new Context(
- this.instructionsStack.plus(this.instructions),
- P.vector(),
- this.localNames,
- this.shedVariablesByNameStack.plus(this.shedVariablesByName),
- this.shedVariablesByName
- );
-
- for (var shedVariable : shedVariables) {
- context = context.addShedVariable(shedVariable);
- }
-
- return context;
- }
-
- public Context exitBlock() {
- return new Context(
- this.instructionsStack.minus(0),
- this.instructionsStack.get(0),
- this.localNames,
- this.shedVariablesByNameStack.minus(0),
- this.shedVariablesByNameStack.get(0)
- );
- }
-
- private Context addInstruction(Instruction instruction) {
- return new Context(
- this.instructionsStack,
- this.instructions.plus(instruction),
- this.localNames,
- this.shedVariablesByNameStack,
- this.shedVariablesByName
- );
- }
-
- public Context addInstructions(PVector<Instruction> instructions) {
- return new Context(
- this.instructionsStack,
- this.instructions.plusAll(instructions),
- this.localNames,
- this.shedVariablesByNameStack,
- this.shedVariablesByName
- );
- }
-
- public String shedVariableToLocal(String variableName) {
- return shedVariable(variableName).stackIrLocalName;
- }
-
- public ShedVariable shedVariable(String variableName) {
- var shedVariable = this.shedVariablesByName.get(variableName);
-
- if (shedVariable == null) {
- throw new RuntimeException("Unknown Shed variable: " + variableName);
- }
-
- return shedVariable;
- }
-
- public Context addShedVal(String variableName) {
- return addShedVariable(BoundVariable.immutable(variableName));
- }
-
- public Context addShedVariable(BoundVariable boundVariable) {
- var localName = generateLocalNameForVariable(boundVariable.name());
-
- return new Context(
- this.instructionsStack,
- this.instructions,
- this.localNames.plus(localName),
- this.shedVariablesByNameStack,
- this.shedVariablesByName.plus(boundVariable.name(), new ShedVariable(localName, boundVariable.isMutable()))
- );
- }
-
- private String generateLocalNameForVariable(String variableName) {
- var index = 0;
- while (true) {
- var localName = variableName + "__" + index;
- if (this.localNames.contains(localName)) {
- index++;
- } else {
- return localName;
- }
- }
- }
-
- public List<Instruction> instructions() {
- return this.instructions;
- }
- }
-
- private record ShedVariable(String stackIrLocalName, boolean isVar) {}
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java b/src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java
deleted file mode 100644
index 65fd41d3..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/StackIrNamespace.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package org.shedlang.compiler.stackir;
-
-import org.shedlang.compiler.stackir.instructions.Instruction;
-import org.shedlang.compiler.types.NamespaceName;
-
-import java.util.List;
-
-public record StackIrNamespace(
- NamespaceName namespaceName,
- List<Instruction> instructions
-) {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/CallInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/CallInstruction.java
deleted file mode 100644
index a84c4af1..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/CallInstruction.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-import java.util.List;
-
-/**
- * Pop a receiver and arguments from the temporary value stack and call the
- * receiver with those arguments.
- *
- * <p>Specifically, the values should have been pushed onto the temporary value
- * stack in the following order:</p>
- *
- * <ul>
- * <li>The receiver i.e. the function to be called.</li>
- * <li>The positional arguments, from left to right.</li>
- * <li>The named arguments, in the same order as <code>namedArgNames</code>.</li>
- * </ul>
- */
-public record CallInstruction(
- int positionalArgCount,
- List<String> namedArgNames,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/DefineEffectInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/DefineEffectInstruction.java
deleted file mode 100644
index d87a6412..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/DefineEffectInstruction.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.types.UserDefinedEffect;
-
-/**
- * Push an effect value onto the temporary value 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/DefineFunctionInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/DefineFunctionInstruction.java
deleted file mode 100644
index 6ca40775..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/DefineFunctionInstruction.java
+++ /dev/null
@@ -1,105 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.pcollections.PVector;
-import org.shedlang.compiler.ast.typed.FreeVariables;
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.sources.Sources;
-import org.shedlang.compiler.util.P;
-
-import java.util.List;
-
-/**
- * Push a function value onto the temporary value stack with the given
- * definition.
- */
-public record DefineFunctionInstruction(
- String name,
- List<Param> positionalParams,
- List<Param> namedParams,
- FreeVariables freeVariables,
- List<Instruction> body,
- Source source
-) implements Instruction {
- public record Param(String name, Source source) {
-
- }
-
- public static Builder builder() {
- return new Builder(
- "f",
- P.vector(),
- P.vector(),
- FreeVariables.builder(),
- P.vector()
- );
- }
-
- public record Builder(
- String name,
- PVector<Param> positionalParams,
- PVector<Param> namedParams,
- FreeVariables.Builder freeVariables,
- PVector<Instruction> body
- ) {
- public DefineFunctionInstruction build() {
- return new DefineFunctionInstruction(
- name,
- positionalParams,
- namedParams,
- freeVariables.build(),
- body,
- Sources.NULL
- );
- }
-
- public Builder withName(String name) {
- return new Builder(
- name,
- this.positionalParams,
- this.namedParams,
- this.freeVariables,
- this.body
- );
- }
-
- public Builder addPositionalParam(String name) {
- return new Builder(
- this.name,
- this.positionalParams.plus(new Param(name, Sources.NULL)),
- this.namedParams,
- this.freeVariables,
- this.body
- );
- }
-
- public Builder addNamedParam(String name) {
- return new Builder(
- this.name,
- this.positionalParams,
- this.namedParams.plus(new Param(name, Sources.NULL)),
- this.freeVariables,
- this.body
- );
- }
-
- public Builder addFreeVariable(String name) {
- return new Builder(
- this.name,
- this.positionalParams,
- this.namedParams,
- this.freeVariables.plus(name, Sources.NULL),
- this.body
- );
- }
-
- public Builder addBodyInstruction(Instruction instruction) {
- return new Builder(
- this.name,
- this.positionalParams,
- this.namedParams,
- this.freeVariables,
- this.body.plus(instruction)
- );
- }
- }
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/DefineStructInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/DefineStructInstruction.java
deleted file mode 100644
index 098c9245..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/DefineStructInstruction.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.types.StructType;
-
-import java.util.List;
-
-/**
- * Push a struct type value onto the temporary value stack with the given
- * definition.
- */
-public record DefineStructInstruction(
- StructType type,
- // TODO: remove fields? We should have fields from the type info.
- List<Field> fields,
- Source source
-) implements Instruction {
- public record Field(String name, Source source) {
-
- }
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/DiscardInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/DiscardInstruction.java
deleted file mode 100644
index 503ecc53..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/DiscardInstruction.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop a value from the temporary value stack and discard it.
- */
-public record DiscardInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/DuplicateInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/DuplicateInstruction.java
deleted file mode 100644
index 03685e54..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/DuplicateInstruction.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Duplicates the value on top of the temporary value stack.
- */
-public record DuplicateInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java
deleted file mode 100644
index 53dc1ee8..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/FieldLoadInstruction.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.types.NamespaceName;
-import org.shedlang.compiler.types.StructType;
-import org.shedlang.compiler.types.UserDefinedEffect;
-
-/**
- * Pop a value from the temporary value stack, get the given field from the
- * popped value, and push the field's value onto the temporary value stack.
- */
-public record FieldLoadInstruction(
- Receiver receiver,
- String fieldName,
- Source source
-) implements Instruction {
- public sealed interface Receiver {
- record Effect(UserDefinedEffect effect) implements Receiver {}
- record Namespace(NamespaceName name) implements Receiver {}
- record Struct(StructType type) implements Receiver {}
- }
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IfInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IfInstruction.java
deleted file mode 100644
index a6636b91..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/IfInstruction.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-
-import org.shedlang.compiler.sources.Source;
-
-import java.util.List;
-
-/**
- * Pop a value from the temporary value stack, and assume it is a Bool. If it is
- * true, execute the instructions in the "true" body, otherwise execute the
- * instructions in the "false" body.
- * <p>
- * Each branch body should push <code>resultCount</code> values onto the
- * temporary value stack.
- */
-public record IfInstruction(
- int resultCount,
- List<Instruction> trueBody,
- List<Instruction> falseBody,
- 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
deleted file mode 100644
index f84de210..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java
+++ /dev/null
@@ -1,9 +0,0 @@
-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, ResumeInstruction, ReturnInstruction, StringAddInstruction, SwitchInstruction, TryInstruction, TupleCreateInstruction, TupleGetInstruction, VarLoadInstruction, VarStoreInstruction {
-
- Source source();
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IntAddInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IntAddInstruction.java
deleted file mode 100644
index 55a4fb1c..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/IntAddInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop two values from the temporary value stack, assumed to be Ints, add them
- * together, and push the result onto the temporary value stack.
- */
-public record IntAddInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IntEqualsInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IntEqualsInstruction.java
deleted file mode 100644
index 7f0c97cf..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/IntEqualsInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop two values from the temporary value stack, compare them as Ints, and push
- * a Bool onto the temporary value stack: true if equal, false if not equal.
- */
-public record IntEqualsInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java
deleted file mode 100644
index 6b155f67..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/IntNotEqualInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop two values from the temporary value stack, compare them as Ints, and push
- * a Bool onto the temporary value stack: true if not equal, false if equal.
- */
-public record IntNotEqualInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java
deleted file mode 100644
index e2fa09b6..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop the right operand from the temporary value stack, then the left operand
- * from the temporary value stack, calculate the left operand minus the right
- * operand, then push the result onto the temporary value stack.
- */
-public record IntSubInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java
deleted file mode 100644
index d0753609..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/LocalLoadInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Load the local with the given name and push it onto the temporary value
- * stack.
- */
-public record LocalLoadInstruction(String name, Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LocalStoreInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LocalStoreInstruction.java
deleted file mode 100644
index 880c91f8..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/LocalStoreInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop a value from the temporary value stack and store it as the local with the
- * given name.
- */
-public record LocalStoreInstruction(String name, Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java
deleted file mode 100644
index 60a1d056..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopBreakInstruction.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Jump to the end of the enclosing <code>loop</code> 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
deleted file mode 100644
index d7111ea9..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopContinueInstruction.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Jump to the start of the enclosing <code>loop</code> 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
deleted file mode 100644
index 119d9a52..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/LoopInstruction.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-
-import org.shedlang.compiler.sources.Source;
-
-import java.util.List;
-
-/**
- * Execute the body. The <code>continue</code> instruction can jump back to the
- * start of the body. The <code>break</code> instruction can jump to the end of
- * the body.
- */
-public record LoopInstruction(
- List<Instruction> body,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceFieldStoreInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceFieldStoreInstruction.java
deleted file mode 100644
index b83964a1..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceFieldStoreInstruction.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.types.NamespaceName;
-
-/**
- * Pop a value from the temporary value stack and store it as the field with the
- * given name on the given namespace.
- */
-public record NamespaceFieldStoreInstruction(
- NamespaceName namespaceName,
- String fieldName,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceImportInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceImportInstruction.java
deleted file mode 100644
index cc600d5b..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/NamespaceImportInstruction.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.types.NamespaceName;
-
-/**
- * Import the given namespace and push it onto the temporary value stack.
- */
-public record NamespaceImportInstruction(
- NamespaceName namespaceName,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/PushBoolInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/PushBoolInstruction.java
deleted file mode 100644
index c732a390..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/PushBoolInstruction.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Push a boolean onto the temporary value stack.
- */
-public record PushBoolInstruction(
- boolean value,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java
deleted file mode 100644
index cba3f217..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/PushIntInstruction.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-import java.math.BigInteger;
-
-/**
- * Push an integer onto the temporary value stack.
- */
-public record PushIntInstruction(BigInteger value, Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/PushStringInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/PushStringInstruction.java
deleted file mode 100644
index e33051e5..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/PushStringInstruction.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Push a string onto the temporary value stack.
- */
-public record PushStringInstruction(String value, Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/PushUnitInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/PushUnitInstruction.java
deleted file mode 100644
index 998624e1..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/PushUnitInstruction.java
+++ /dev/null
@@ -1,9 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Push the unit value onto the temporary value stack.
- */
-public record PushUnitInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java
deleted file mode 100644
index 836b5bac..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/ResumeInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-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 {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/ReturnInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/ReturnInstruction.java
deleted file mode 100644
index 1b9e4213..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/ReturnInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-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 function.
- */
-public record ReturnInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java
deleted file mode 100644
index 5114beff..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java
+++ /dev/null
@@ -1,12 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop the right operand from the temporary value stack, then the left operand
- * from the temporary value stack, assume both operands are Strings, concatenate
- * the left and right operands, then push the result onto the temporary value
- * stack.
- */
-public record StringAddInstruction(Source source) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.java
deleted file mode 100644
index 880349de..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/SwitchInstruction.java
+++ /dev/null
@@ -1,21 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.types.StructType;
-
-import java.util.List;
-
-/**
- * Pop a value from the temporary value stack, find the case corresponding to
- * that value's type, and execute the instructions in the case's body. If there
- * is no such case, the behaviour of this instruction is not defined.
- * <p>
- * Each case body should push exactly one value onto the temporary value stack.
- */
-public record SwitchInstruction(
- List<Case> cases,
- Source source
-) implements Instruction {
- public record Case(StructType caseType, List<Instruction> body, Source source) {
- }
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java
deleted file mode 100644
index 0b5051df..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/TryInstruction.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-
-import org.shedlang.compiler.sources.Source;
-import org.shedlang.compiler.types.UserDefinedEffect;
-
-import java.util.List;
-
-/**
- * For each handler method name, pop a value off the temporary value stack. The
- * first value (that is, the first pushed / last popped) corresponds to the
- * first handler method name, and so on. Push an effect handler for the given
- * effect with the popped handler methods onto the effect handler stack, and
- * execute the body. Once the body has exited, pop the effect handler from the
- * effect handler stack.
- * <p>
- * If the body runs to completion, it should leave a single value on the
- * temporary value stack.
- */
-public record TryInstruction(
- UserDefinedEffect handledEffect,
- List<String> handlerMethodNames,
- List<Instruction> body,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java
deleted file mode 100644
index fa331d1c..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/TupleCreateInstruction.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop {@code elementCount} values from the temporary value stack, create a
- * tuple from those elements, and push the tuple onto the temporary value stack.
- * Elements should have been pushed onto the stack in index order.
- */
-public record TupleCreateInstruction(
- int elementCount,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java
deleted file mode 100644
index ed52cc29..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/TupleGetInstruction.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop a tuple from the temporary value stack, get the element at the given
- * index, and push it onto the temporary value stack.
- */
-public record TupleGetInstruction(
- int elementIndex,
- int elementCount,
- Source source
-) implements Instruction {
-}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/VarLoadInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/VarLoadInstruction.java
deleted file mode 100644
index fe35db14..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/VarLoadInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-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
- * temporary value 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
deleted file mode 100644
index 33a3ab45..00000000
--- a/src/main/java/org/shedlang/compiler/stackir/instructions/VarStoreInstruction.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.shedlang.compiler.stackir.instructions;
-
-import org.shedlang.compiler.sources.Source;
-
-/**
- * Pop a value from the temporary value stack and store it in the var with the
- * given name.
- */
-public record VarStoreInstruction(String name, Source source) implements Instruction {
-}