From 6f462f32793ae1329259e5613940a50a7c2dd5e7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:15:50 +0100 Subject: Add int-add instruction --- .../backends/llvm/LlvmCodeGenIntAddTests.java | 28 ++++++++++++++++++++++ .../backends/wasm/WasmCodeGenIntAddTests.java | 24 +++++++++++++++++++ .../stackir/InstructionSerializerTests.java | 9 +++++++ .../stackir/instructions/Instructions.java | 4 ++++ 4 files changed, 65 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntAddTests.java create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntAddTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntAddTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntAddTests.java new file mode 100644 index 00000000..e2fc5ccb --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntAddTests.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 LlvmCodeGenIntAddTests { + @Test + public void intAddIsCompiledToAdd(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.intAdd(); + 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/WasmCodeGenIntAddTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntAddTests.java new file mode 100644 index 00000000..4e960683 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntAddTests.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 WasmCodeGenIntAddTests { + @Test + public void intAddIsCompiledToI32Add(Snapshotter snapshotter) throws IOException, InterruptedException { + var instruction = Instructions.intAdd(); + 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 a2d5065a..3f8398cc 100644 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java @@ -75,6 +75,15 @@ public class InstructionSerializerTests { assertThat(string, equalTo("(field-load \"Example/Lib\" \"x\")")); } + @Test + public void canSerializeIntAdd() { + var instruction = Instructions.intAdd(); + + var string = serialize(instruction); + + assertThat(string, equalTo("(int-add)")); + } + @Test public void canSerializeIntEquals() { var instruction = Instructions.intEquals(); 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 2b198617..5b155878 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/Instructions.java @@ -22,6 +22,10 @@ public class Instructions { return new FieldLoadInstruction(namespaceName, fieldName, Sources.NULL); } + public static Instruction intAdd() { + return new IntAddInstruction(Sources.NULL); + } + public static Instruction intEquals() { return new IntEqualsInstruction(Sources.NULL); } -- cgit v1.2.3