aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-07 18:15:50 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-07 18:15:50 +0100
commit6f462f32793ae1329259e5613940a50a7c2dd5e7 (patch)
treec9400cf4a1e1730a375e75f3f17945d2033db26f /src/main/java
parent69ce0b822262c9ea5ae9cc4e89be0fa4498eaf25 (diff)
Add int-add instruction
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java46
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java3
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Add.java4
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java3
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java3
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/Instruction.java2
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/instructions/IntAddInstruction.java10
8 files changed, 67 insertions, 6 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 389067b9..e7132d56 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
@@ -5,12 +5,10 @@ import org.shedlang.compiler.backends.llvm.ir.ast.LlvmFunctionDeclaration;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmGlobalDefinition;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmModule;
import org.shedlang.compiler.backends.llvm.ir.ast.LlvmParam;
-import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmConditionCode;
-import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmIcmp;
-import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmReturn;
-import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmZext;
+import org.shedlang.compiler.backends.llvm.ir.ast.instructions.*;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmOperand;
import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmOperandInt;
+import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariable;
import org.shedlang.compiler.backends.llvm.ir.ast.types.LlvmTypes;
import org.shedlang.compiler.stackir.instructions.*;
import org.shedlang.compiler.typechecker.TypeInfo;
@@ -156,6 +154,13 @@ public class LlvmCodeGen {
yield context.push(fieldValue);
}
+ case IntAddInstruction intAdd ->
+ generateBinaryOperation(
+ (target, left, right) ->
+ new LlvmAdd(target, LlvmCodeGenTypes.INT_TYPE, left, right),
+ context
+ );
+
case IntEqualsInstruction intEquals ->
generateBinaryComparison(LlvmConditionCode.EQ, context);
@@ -226,6 +231,39 @@ public class LlvmCodeGen {
};
}
+ interface BinaryOperation {
+ LlvmInstruction generate(
+ LlvmVariable target,
+ LlvmOperand left,
+ LlvmOperand right
+ );
+ }
+
+ private static LlvmCodeGenContext generateBinaryOperation(
+ BinaryOperation operation,
+ LlvmCodeGenContext context
+ ) {
+ var rightResult = context.pop();
+ var right = rightResult.value();
+ context = rightResult.context();
+
+ var leftResult = context.pop();
+ var left = leftResult.value();
+ context = leftResult.context();
+
+ var targetResult = context.generateVariableLocal(LlvmCodeGenLocalVariableSpace.TEMPORARY);
+ var target = targetResult.value();
+ context = targetResult.context();
+
+ return context
+ .addInstruction(operation.generate(
+ target,
+ left,
+ right
+ ))
+ .push(target);
+ }
+
private static LlvmCodeGenContext generateBinaryComparison(
LlvmConditionCode conditionCode,
LlvmCodeGenContext context
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 d3d99bbc..3c57853f 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -179,6 +179,9 @@ public class WasmCodeGen {
);
}
+ case IntAddInstruction intAdd ->
+ context.addInstruction(new WasmI32Add());
+
case IntEqualsInstruction intEquals ->
context.addInstruction(new WasmI32Eq());
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Add.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Add.java
new file mode 100644
index 00000000..10079da8
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Add.java
@@ -0,0 +1,4 @@
+package org.shedlang.compiler.backends.wasm.structure.ast.instructions;
+
+public record WasmI32Add() implements WasmInstruction {
+}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java
index 2286536a..2e2be69f 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmInstruction.java
@@ -1,4 +1,4 @@
package org.shedlang.compiler.backends.wasm.structure.ast.instructions;
-public sealed interface WasmInstruction permits WasmCall, WasmCallIndirect, WasmDrop, WasmGlobalGet, WasmGlobalSet, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Ne, WasmI32Store, WasmI64Const, WasmIf, WasmLocalGet, WasmLocalSet, WasmMemoryAddressConst, WasmMemoryGrow, WasmReturn, WasmTableElementIndexConst {
+public sealed interface WasmInstruction permits WasmCall, WasmCallIndirect, WasmDrop, WasmGlobalGet, WasmGlobalSet, WasmI32Add, WasmI32Const, WasmI32Eq, WasmI32Load, WasmI32Ne, WasmI32Store, WasmI64Const, WasmIf, WasmLocalGet, WasmLocalSet, WasmMemoryAddressConst, WasmMemoryGrow, WasmReturn, WasmTableElementIndexConst {
}
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java
index ea162fbd..9bfdd2ae 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryFormatInstructions.java
@@ -110,6 +110,9 @@ public class WasmBinaryFormatInstructions {
case WasmI32Ne i32Ne -> {
writer.writeByte(0x47);
}
+ case WasmI32Add i32Add -> {
+ writer.writeByte(0x6a);
+ }
// == Relocatable Instructions ==
diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
index 00757988..e98c4e02 100644
--- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
+++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java
@@ -82,6 +82,9 @@ public class InstructionSerializer {
treeString(fieldLoad.fieldName())
);
+ case IntAddInstruction intAdd ->
+ treeList(treeIdentifier("int-add"));
+
case IntEqualsInstruction intEquals ->
treeList(treeIdentifier("int-eq"));
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 5d4bd139..6be42213 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, IntEqualsInstruction, IntNotEqualInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction {
+ permits CallInstruction, DefineFunctionInstruction, DiscardInstruction, FieldLoadInstruction, IntAddInstruction, IntEqualsInstruction, IntNotEqualInstruction, LocalLoadInstruction, LocalStoreInstruction, NamespaceFieldStoreInstruction, NamespaceImportInstruction, PushBoolInstruction, PushIntInstruction, PushStringInstruction, PushUnitInstruction, ReturnInstruction {
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
new file mode 100644
index 00000000..9f131255
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/stackir/instructions/IntAddInstruction.java
@@ -0,0 +1,10 @@
+package org.shedlang.compiler.stackir.instructions;
+
+import org.shedlang.compiler.sources.Source;
+
+/**
+ * Pop two values from the stack, assumed to be Ints, add them together, and
+ * push the result onto the stack.
+ */
+public record IntAddInstruction(Source source) implements Instruction {
+}