aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/backends
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-07 18:22:49 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-07 18:22:49 +0100
commitbbd0ad7fad36761f55555347b01a32887fc4a5f9 (patch)
tree4f91676bba01b3aa85e9d8bf87b140f4ba3f8722 /src/main/java/org/shedlang/compiler/backends
parent4123de472e0442cccd8fd01767e399dc05cc6b11 (diff)
Add int-sub instruction
Diffstat (limited to 'src/main/java/org/shedlang/compiler/backends')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java7
-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/WasmI32Sub.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
5 files changed, 18 insertions, 1 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 e7132d56..3062d48d 100644
--- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmCodeGen.java
@@ -167,6 +167,13 @@ public class LlvmCodeGen {
case IntNotEqualInstruction intNotEqual ->
generateBinaryComparison(LlvmConditionCode.NE, context);
+ case IntSubInstruction intSub ->
+ generateBinaryOperation(
+ (target, left, right) ->
+ new LlvmSub(target, LlvmCodeGenTypes.INT_TYPE, left, right),
+ context
+ );
+
case LocalLoadInstruction localLoad ->
context.push(context.shedLocal(localLoad.name()));
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 3c57853f..b3a6a4ba 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java
@@ -188,6 +188,9 @@ public class WasmCodeGen {
case IntNotEqualInstruction intNotEqual ->
context.addInstruction(new WasmI32Ne());
+ case IntSubInstruction intSub ->
+ context.addInstruction(new WasmI32Sub());
+
case LocalLoadInstruction localLoad ->
context.addInstruction(new WasmLocalGet(context.shedLocal(localLoad.name())));
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Sub.java b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Sub.java
new file mode 100644
index 00000000..757cdaa2
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/structure/ast/instructions/WasmI32Sub.java
@@ -0,0 +1,4 @@
+package org.shedlang.compiler.backends.wasm.structure.ast.instructions;
+
+public record WasmI32Sub() 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 2e2be69f..1c3a1c9f 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, WasmI32Add, 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, WasmI32Sub, 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 9bfdd2ae..00766dbd 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
@@ -113,6 +113,9 @@ public class WasmBinaryFormatInstructions {
case WasmI32Add i32Add -> {
writer.writeByte(0x6a);
}
+ case WasmI32Sub i32Sub -> {
+ writer.writeByte(0x6b);
+ }
// == Relocatable Instructions ==