diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:22:49 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-07 18:22:49 +0100 |
| commit | bbd0ad7fad36761f55555347b01a32887fc4a5f9 (patch) | |
| tree | 4f91676bba01b3aa85e9d8bf87b140f4ba3f8722 /src | |
| parent | 4123de472e0442cccd8fd01767e399dc05cc6b11 (diff) | |
Add int-sub instruction
Diffstat (limited to 'src')
12 files changed, 98 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 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 == diff --git a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java index e98c4e02..c6544550 100644 --- a/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java +++ b/src/main/java/org/shedlang/compiler/stackir/InstructionSerializer.java @@ -90,6 +90,9 @@ public class InstructionSerializer { case IntNotEqualInstruction intNotEqual -> treeList(treeIdentifier("int-ne")); + + case IntSubInstruction intSub -> + treeList(treeIdentifier("int-sub")); case LocalLoadInstruction localLoadInstruction -> treeList( 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 6be42213..6d361ddb 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, 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 { Source source(); } diff --git a/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java b/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.java new file mode 100644 index 00000000..4d105695 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/stackir/instructions/IntSubInstruction.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, + * calculate the left operand minus the right operand, then push the result onto + * the stack. + */ +public record IntSubInstruction(Source source) implements Instruction { +} diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntSubTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntSubTests.java new file mode 100644 index 00000000..dfde4836 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntSubTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.backends.llvm; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.backends.llvm.ir.ast.LlvmIr; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.backends.llvm.LlvmCodeGenTesting.serializeContext; + +@ExtendWith({SnapshotResolver.class}) +public class LlvmCodeGenIntSubTests { + @Test + public void intSubIsCompiledToSub(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.intSub(); + var context = LlvmCodeGenContext.EMPTY + .push(LlvmIr.variableLocal("a")) + .push(LlvmIr.variableLocal("b")) + .push(LlvmIr.variableLocal("c")); + + var result = LlvmCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(serializeContext(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntSubTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntSubTests.java new file mode 100644 index 00000000..f394d823 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntSubTests.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.backends.wasm; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.shedlang.compiler.stackir.instructions.Instructions; +import org.shedlang.compiler.testing.snapshots.SnapshotResolver; +import org.shedlang.compiler.testing.snapshots.Snapshotter; + +import java.io.IOException; + +import static org.shedlang.compiler.backends.wasm.WasmCodeGenTesting.objdumpContext; + +@ExtendWith({SnapshotResolver.class}) +public class WasmCodeGenIntSubTests { + @Test + public void intSubIsCompiledToI32Sub(Snapshotter snapshotter) throws IOException, InterruptedException { + var instruction = Instructions.intSub(); + var context = WasmCodeGenContext.EMPTY; + + var result = WasmCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(objdumpContext(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java index 3f8398cc..c6046847 100644 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java @@ -103,6 +103,15 @@ public class InstructionSerializerTests { } @Test + public void canSerializeIntSub() { + var instruction = Instructions.intSub(); + + var string = serialize(instruction); + + assertThat(string, equalTo("(int-sub)")); + } + + @Test public void canSerializeLocalLoad() { var instruction = Instructions.localLoad("x"); diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java index 5b155878..6f769591 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java @@ -34,6 +34,10 @@ public class Instructions { return new IntNotEqualInstruction(Sources.NULL); } + public static Instruction intSub() { + return new IntSubInstruction(Sources.NULL); + } + public static Instruction localLoad(String name) { return new LocalLoadInstruction(name, Sources.NULL); } |
