From 3703fd741f19ca15d6dafb5dcd11f773d3b5f450 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 29 Jul 2023 13:50:45 +0100 Subject: Add simple test for LLVM Patio code gen --- .../backends/llvm/LlvmPatioCodeGenTests.java | 76 ++++++++++++++++++++++ .../shedlang/compiler/patio/PatioExampleTests.java | 50 ++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioCodeGenTests.java create mode 100644 src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java (limited to 'src/test/java') 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(); + 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; +} -- cgit v1.2.3