diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-14 23:54:41 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-14 23:54:41 +0100 |
| commit | b02657e9a6a172be263eaefaea842fb0c7572a6e (patch) | |
| tree | 69fe19748b900ff7b79b2db5a994e5caeb003332 /src/test/java | |
| parent | efa34e180428f0161c49cef814aacadbe3e1166d (diff) | |
Support code generation for ifs in LLVM backend
Diffstat (limited to 'src/test/java')
3 files changed, 75 insertions, 3 deletions
diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIfTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIfTests.java new file mode 100644 index 00000000..3a04ce3f --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmCodeGenIfTests.java @@ -0,0 +1,70 @@ +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 java.util.List; + +@ExtendWith({SnapshotResolver.class}) +public class LlvmCodeGenIfTests { + @Test + public void ifIsCompiled(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.if_( + List.of( + Instructions.pushInt(1), + Instructions.pushInt(2), + Instructions.intAdd() + ), + List.of( + Instructions.pushInt(101), + Instructions.pushInt(102), + Instructions.intAdd() + ) + ); + var context = LlvmCodeGenContext.EMPTY + .push(LlvmIr.variableLocal("x")) + .push(LlvmIr.variableLocal("condition")); + + var result = LlvmCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(LlvmCodeGenTesting.serializeContext(result)); + } + + @Test + public void canCompileNestedIf(Snapshotter snapshotter) throws IOException { + var instruction = Instructions.if_( + List.of( + Instructions.pushInt(1), + Instructions.pushInt(2), + Instructions.intAdd() + ), + List.of( + Instructions.pushBool(true), + Instructions.if_( + List.of( + Instructions.pushInt(101), + Instructions.pushInt(102), + Instructions.intAdd() + ), + List.of( + Instructions.pushInt(201), + Instructions.pushInt(202), + Instructions.intAdd() + ) + ) + ) + ); + var context = LlvmCodeGenContext.EMPTY + .push(LlvmIr.variableLocal("x")) + .push(LlvmIr.variableLocal("condition")); + + var result = LlvmCodeGen.generateInstruction(instruction, context); + + snapshotter.assertSnapshot(LlvmCodeGenTesting.serializeContext(result)); + } +} diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java index 4ecd63eb..ce9986cf 100644 --- a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmExampleTests.java @@ -23,6 +23,6 @@ public class LlvmExampleTests extends ExampleTests { @Override protected boolean isDisabled(String testName) { - return testName.equals("if"); + return false; } } diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIfTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIfTests.java index 82c034a5..37cf28af 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIfTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmCodeGenIfTests.java @@ -18,11 +18,13 @@ public class WasmCodeGenIfTests { var instruction = Instructions.if_( List.of( Instructions.pushInt(1), - Instructions.pushInt(2) + Instructions.pushInt(2), + Instructions.intAdd() ), List.of( Instructions.pushInt(101), - Instructions.pushInt(102) + Instructions.pushInt(102), + Instructions.intAdd() ) ); var context = WasmCodeGenContext.EMPTY; |
