From 69ce0b822262c9ea5ae9cc4e89be0fa4498eaf25 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 7 Jul 2023 18:01:28 +0100 Subject: Add int-ne instruction --- .../backends/llvm/LlvmCodeGenIntNotEqualTests.java | 28 ++++++++++++++++++++++ .../backends/wasm/WasmCodeGenIntNotEqualTests.java | 24 +++++++++++++++++++ .../stackir/InstructionSerializerTests.java | 12 +++++++++- .../stackir/instructions/Instructions.java | 12 ++++++---- 4 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntNotEqualTests.java create mode 100644 src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntNotEqualTests.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntNotEqualTests.java new file mode 100644 index 00000000..b5759f61 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIntNotEqualTests.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 LlvmCodeGenIntNotEqualTests { + @Test + public void intNotEqualIsCompiledToComparison(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.intNotEqual(); + 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/WasmCodeGenIntNotEqualTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntNotEqualTests.java new file mode 100644 index 00000000..dc924eb9 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIntNotEqualTests.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 WasmCodeGenIntNotEqualTests { + @Test + public void intNotEqualIsCompiledToI32Ne(Snapshotter snapshotter) throws IOException, InterruptedException { + var instruction = Instructions.intNotEqual(); + 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 10588ec1..a2d5065a 100644 --- a/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/InstructionSerializerTests.java @@ -74,13 +74,23 @@ public class InstructionSerializerTests { assertThat(string, equalTo("(field-load \"Example/Lib\" \"x\")")); } + @Test public void canSerializeIntEquals() { var instruction = Instructions.intEquals(); var string = serialize(instruction); - assertThat(string, equalTo("(int-equals)")); + assertThat(string, equalTo("(int-eq)")); + } + + @Test + public void canSerializeIntNotEqual() { + var instruction = Instructions.intNotEqual(); + + var string = serialize(instruction); + + assertThat(string, equalTo("(int-ne)")); } @Test 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 a1630727..2b198617 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,14 @@ public class Instructions { return new FieldLoadInstruction(namespaceName, fieldName, Sources.NULL); } + public static Instruction intEquals() { + return new IntEqualsInstruction(Sources.NULL); + } + + public static Instruction intNotEqual() { + return new IntNotEqualInstruction(Sources.NULL); + } + public static Instruction localLoad(String name) { return new LocalLoadInstruction(name, Sources.NULL); } @@ -61,8 +69,4 @@ public class Instructions { public static Instruction return_() { return new ReturnInstruction(Sources.NULL); } - - public static Instruction intEquals() { - return new IntEqualsInstruction(Sources.NULL); - } } -- cgit v1.2.3