aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-23 11:45:25 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-23 11:45:25 +0100
commit6a3d3bdf076847f0606ccb3bc96f3fb87b87c549 (patch)
tree114cbd157e6eb6ea5a77f50e686c450d03eda47c /src/main/java
parent473df966467c00f1744aa5bf06ee01df11809505 (diff)
Extract WasmCodeGen::generateBinaryOperation()
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java42
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstructionBinaryOperation.java10
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java2
-rw-r--r--src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntEquals.java2
4 files changed, 32 insertions, 24 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
index e6fcbfcb..43a7dadf 100644
--- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmPatioCodeGen.java
@@ -206,29 +206,11 @@ public class WasmPatioCodeGen {
yield context;
}
- case PatioIntAdd patioIntAdd -> {
- context = generateOperand(patioIntAdd.left(), context);
+ case PatioIntAdd patioIntAdd ->
+ generateBinaryOperation(patioIntAdd, new WasmI32Add(), context);
- context = generateOperand(patioIntAdd.right(), context);
-
- context = context.addInstruction(new WasmI32Add());
-
- context = popIntoPatioLocal(patioIntAdd.target(), context);
-
- yield context;
- }
-
- case PatioIntEquals patioIntEquals -> {
- context = generateOperand(patioIntEquals.left(), context);
-
- context = generateOperand(patioIntEquals.right(), context);
-
- context = context.addInstruction(new WasmI32Eq());
-
- context = popIntoPatioLocal(patioIntEquals.target(), context);
-
- yield context;
- }
+ case PatioIntEquals patioIntEquals ->
+ generateBinaryOperation(patioIntEquals, new WasmI32Eq(), context);
case PatioIntToBool patioIntToBool ->
generateOperandToPatioLocal(
@@ -285,6 +267,22 @@ public class WasmPatioCodeGen {
};
}
+ private static Context generateBinaryOperation(
+ PatioInstructionBinaryOperation patioInstruction,
+ WasmInstruction wasmInstruction,
+ Context context
+ ) {
+ context = generateOperand(patioInstruction.left(), context);
+
+ context = generateOperand(patioInstruction.right(), context);
+
+ context = context.addInstruction(wasmInstruction);
+
+ context = popIntoPatioLocal(patioInstruction.target(), context);
+
+ return context;
+ }
+
private static Context generateOperandToPatioLocal(
PatioLocalRef target,
PatioOperand value,
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstructionBinaryOperation.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstructionBinaryOperation.java
new file mode 100644
index 00000000..1737ecf1
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioInstructionBinaryOperation.java
@@ -0,0 +1,10 @@
+package org.shedlang.compiler.patio.ast.instructions;
+
+import org.shedlang.compiler.patio.ast.operands.PatioLocalRef;
+import org.shedlang.compiler.patio.ast.operands.PatioOperand;
+
+public interface PatioInstructionBinaryOperation {
+ PatioLocalRef target();
+ PatioOperand left();
+ PatioOperand right();
+}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java
index ae1f67b7..23ce8736 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntAdd.java
@@ -8,5 +8,5 @@ public record PatioIntAdd(
PatioLocalRef left,
PatioLocalRef right,
Source source
-) implements PatioInstruction {
+) implements PatioInstructionBinaryOperation, PatioInstruction {
}
diff --git a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntEquals.java b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntEquals.java
index f746ee6b..5dd0c8a5 100644
--- a/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntEquals.java
+++ b/src/main/java/org/shedlang/compiler/patio/ast/instructions/PatioIntEquals.java
@@ -9,5 +9,5 @@ public record PatioIntEquals(
PatioOperand left,
PatioOperand right,
Source source
-) implements PatioInstruction {
+) implements PatioInstructionBinaryOperation, PatioInstruction {
}