aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-08 12:22:36 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-08 12:22:36 +0100
commit9a14e772cd0a9574b892cf872c32db1d333b0815 (patch)
treee469e620340ec6ae60b38dd5504aa331de823b61 /src/main/java
parenta0d962a3609abee2148abcffe54fae9b09663830 (diff)
Compile string addition into string-add instruction
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java3
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java3
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java4
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java2
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java11
6 files changed, 23 insertions, 2 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
index 3062d48d..658e331e 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
@@ -235,6 +235,9 @@ public class LlvmCodeGen {
new LlvmReturn(LlvmCodeGenTypes.VALUE_TYPE, value)
);
}
+
+ case StringAddInstruction stringAdd ->
+ throw new UnsupportedOperationException("TODO");
};
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
index b3a6a4ba..dbf667c2 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -241,6 +241,9 @@ public class WasmCodeGen {
case ReturnInstruction returnInstruction ->
context.addInstruction(new WasmReturn());
+
+ case StringAddInstruction stringAdd ->
+ throw new UnsupportedOperationException("TODO");
};
}
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
index c6544550..baaf4b49 100644
--- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
+++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
@@ -141,10 +141,14 @@ public class InstructionSerializer {
treeList(
treeIdentifier("push-unit")
);
+
case ReturnInstruction returnInstruction ->
treeList(
treeIdentifier("return")
);
+
+ case StringAddInstruction stringAddInstruction ->
+ treeList(treeIdentifier("string-add"));
};
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index 6bda0b6d..449360e1 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -25,7 +25,7 @@ public class StackCompiler {
case INT_SUB ->
new IntSubInstruction(node.source());
case STRING_ADD ->
- throw new UnsupportedOperationException("TODO");
+ new StringAddInstruction(node.source());
});
}
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 6d361ddb..a1ab0a39 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, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction {
+ permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, IntSubInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction, StringAddInstruction {
Source source();
}
diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java
new file mode 100644
index 00000000..041ae30e
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/stackir/instructions/StringAddInstruction.java
@@ -0,0 +1,11 @@
+package org.shedlang.compiler.stackir.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Pop the right operand from the stack, then the left operand from the stack,
+ * assume both operands are Strings, concatenate the left and right operands,
+ * then push the result onto the stack.
+ */
+public record StringAddInstruction(Source source) implements Instruction {
+}