aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-29 13:50:45 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-29 13:50:45 +0100
commit3703fd741f19ca15d6dafb5dcd11f773d3b5f450 (patch)
treea9d7656e373d434bf3380385af1fe7ae2e471a18 /src/test/java
parent20d101c8c90fab89c75de2a7a3953c554e6302ad (diff)
Add simple test for LLVM Patio code gen
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGenTests.java76
-rw-r--r--src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java50
2 files changed, 126 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGenTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGenTests.java
new file mode 100644
index 00000000..ae32312c
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGenTests.java
@@ -0,0 +1,76 @@
+package org.shedlang.compiler.backends.llvm;
+
+import org.shedlang.compiler.backends.llvm.ir.ast.LlvmBasicBlock;
+import org.shedlang.compiler.backends.llvm.ir.ast.LlvmFunctionDefinition;
+import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmCall;
+import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmReturn;
+import org.shedlang.compiler.backends.llvm.ir.ast.instructions.LlvmTrunc;
+import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariableGlobal;
+import org.shedlang.compiler.backends.llvm.ir.ast.operands.LlvmVariableLocal;
+import org.shedlang.compiler.backends.llvm.ir.ast.types.LlvmTypes;
+import org.shedlang.compiler.filesystem.ShedCompilerFiles;
+import org.shedlang.compiler.patio.PatioExampleTests;
+import org.shedlang.compiler.patio.ast.PatioCompilationUnit;
+import org.shedlang.compiler.testing.TestProcessRunner;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+import static org.shedlang.compiler.backends.ProcessRunner.runInheritIo;
+
+public class LlvmPatioCodeGenTests extends PatioExampleTests {
+ @Override
+ protected ExecutionResult run(PatioCompilationUnit patio) throws IOException, InterruptedException {
+ var llvmModule = LlvmPatioCodeGen.generateCompilationUnit(patio);
+
+ llvmModule = llvmModule.addTopLevelEntity(new LlvmFunctionDefinition(
+ "main",
+ LlvmTypes.INT_32,
+ List.of(),
+ List.of(
+ new LlvmBasicBlock(
+ Optional.empty(),
+ List.of(
+ new LlvmCall(
+ new LlvmVariableLocal("exitCode"),
+ LlvmTypes.INT_64,
+ new LlvmVariableGlobal(FUNCTION_NAME),
+ List.of()
+ ),
+ new LlvmTrunc(
+ new LlvmVariableLocal("exitCode32"),
+ LlvmTypes.INT_64,
+ new LlvmVariableLocal("exitCode"),
+ LlvmTypes.INT_32
+ )
+ ),
+ new LlvmReturn(LlvmTypes.INT_32, new LlvmVariableLocal("exitCode32"))
+ )
+ )
+ ));
+
+ Files.createDirectories(Path.of("tests-tmp"));
+ var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "LlvmPatioCodeGen");
+
+ var objectFilePath = new LlvmCompiler(workingDirectory).compileLlvmModule(llvmModule, "main");
+
+ var gccArgs = new ArrayList<String>();
+ gccArgs.add("gcc");
+
+ gccArgs.add("-o");
+ var executablePath = workingDirectory.resolve("main");
+ gccArgs.add(executablePath.toString());
+
+ gccArgs.add(objectFilePath.toString());
+
+ gccArgs.add(ShedCompilerFiles.getRoot().resolve("runtime/build/libshed.a").toString());
+
+ runInheritIo(gccArgs);
+
+ return TestProcessRunner.runCaptureOutput(List.of(executablePath.toString()));
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
new file mode 100644
index 00000000..8b03edae
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java
@@ -0,0 +1,50 @@
+package org.shedlang.compiler.patio;
+
+import org.junit.jupiter.api.Test;
+import org.shedlang.compiler.backends.llvm.ExecutionResult;
+import org.shedlang.compiler.patio.ast.PatioCompilationUnit;
+import org.shedlang.compiler.patio.ast.PatioFunctionDefinition;
+import org.shedlang.compiler.patio.ast.types.PatioTypes;
+import org.shedlang.compiler.patio.instructions.Patio;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public abstract class PatioExampleTests {
+ protected static final String FUNCTION_NAME = "run";
+
+ @Test
+ public void intAdd() throws IOException, InterruptedException {
+ var patioFunction = PatioFunctionDefinition.builder()
+ .withName(FUNCTION_NAME)
+ .withReturnType(PatioTypes.INT)
+ .addLocal(Patio.local("x", PatioTypes.INT))
+ .addLocal(Patio.local("y", PatioTypes.INT))
+ .addLocal(Patio.local("z", PatioTypes.INT))
+ .addBodyInstruction(Patio.localStore(
+ Patio.localRef("x"),
+ Patio.int_(42)
+ ))
+ .addBodyInstruction(Patio.localStore(
+ Patio.localRef("y"),
+ Patio.int_(100)
+ ))
+ .addBodyInstruction(Patio.intAdd(
+ Patio.localRef("z"),
+ Patio.localRef("x"),
+ Patio.localRef("y")
+ ))
+ .addBodyInstruction(Patio.return_(Patio.localRef("z")))
+ .build();
+ var patio = Patio.compilationUnit(List.of(patioFunction));
+
+ var result = run(patio);
+
+ assertThat(result.exitCode(), equalTo(142));
+ }
+
+ protected abstract ExecutionResult run(PatioCompilationUnit patio) throws IOException, InterruptedException;
+}