aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-03-28 09:29:49 +0000
committerMichael Williamson <mike@zwobble.org>2026-03-28 09:29:49 +0000
commit17c745e977dbe696d3293b363d0aa36cdbf35e9c (patch)
treef5f95380df3b75868c7b153e002eef33215dbb1e
parent81fedf90c181c305703a088f097c81d37e6745a4 (diff)
Add initial skeleton of cranelift backend
-rw-r--r--src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java50
-rw-r--r--src/test/java/org/shedlang/compiler/backends/cranelift/PatioToCraneliftTests.java44
2 files changed, 94 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java b/src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java
new file mode 100644
index 00000000..97d0a156
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java
@@ -0,0 +1,50 @@
+package org.shedlang.compiler.backends.cranelift;
+
+import com.google.flatbuffers.FlatBufferBuilder;
+import org.shedlang.compiler.backends.llvm.LlvmBackend;
+import org.shedlang.compiler.backends.llvm.LlvmTarget;
+import org.shedlang.compiler.backends.paths.PartialTargetPath;
+import org.shedlang.compiler.filesystem.ShedCompilerFiles;
+import org.shedlang.compiler.patio.PatioCompilationUnit;
+import org.shedlang.compiler.patio.serialization.PatioToFlatbuffers;
+import org.shedlang.compiler.runtime.ShedRuntime;
+import org.shedlang.compiler.util.processes.ProcessError;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+import static org.shedlang.compiler.util.processes.ProcessRunner.runInheritIo;
+
+public class CraneliftBackend {
+ public static Path compileCompilationUnit(
+ PatioCompilationUnit patio,
+ PartialTargetPath partialTargetPath
+ ) throws IOException, InterruptedException {
+ var fbsBuilder = new FlatBufferBuilder();
+ var compilationUnitOffset = PatioToFlatbuffers.serializeCompilationUnit(fbsBuilder, patio);
+ fbsBuilder.finish(compilationUnitOffset);
+
+ // TODO: avoid the copy
+ var patioPath = partialTargetPath.withExtension("patio");
+ Files.write(patioPath, fbsBuilder.sizedByteArray());
+
+ runInheritIo(List.of(
+ ShedCompilerFiles.getRoot().resolve("patio-to-cranelift/target/debug/patio-to-cranelift").toString(),
+ patioPath.toString()
+ ));
+
+ var objectFilePath = partialTargetPath.withExtension(".o");
+ return objectFilePath;
+ }
+
+ public static void link(
+ List<Path> inputs,
+ Path outputPath,
+ ShedRuntime shedRuntime
+ ) throws ProcessError, InterruptedException {
+ // TODO: extract linking if it's not specific to LLVM.
+ LlvmBackend.forTarget(LlvmTarget.AMD64).link(inputs, outputPath, shedRuntime);
+ }
+}
diff --git a/src/test/java/org/shedlang/compiler/backends/cranelift/PatioToCraneliftTests.java b/src/test/java/org/shedlang/compiler/backends/cranelift/PatioToCraneliftTests.java
new file mode 100644
index 00000000..ff9b1d82
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/backends/cranelift/PatioToCraneliftTests.java
@@ -0,0 +1,44 @@
+package org.shedlang.compiler.backends.cranelift;
+
+import org.junit.jupiter.api.Disabled;
+import org.shedlang.compiler.backends.llvm.LlvmTarget;
+import org.shedlang.compiler.backends.paths.PartialTargetPath;
+import org.shedlang.compiler.patio.PatioCompilationUnit;
+import org.shedlang.compiler.patio.PatioExampleTests;
+import org.shedlang.compiler.runtime.ShedRuntime;
+import org.shedlang.compiler.testing.ExecutionResult;
+import org.shedlang.compiler.testing.TestProcessRunner;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+
+@Disabled
+public class PatioToCraneliftTests extends PatioExampleTests {
+ @Override
+ protected ExecutionResult executeCompilationUnit(PatioCompilationUnit patio) throws IOException, InterruptedException {
+ Files.createDirectories(Path.of("tests-tmp"));
+ var workingDirectory = Files.createTempDirectory(Path.of("tests-tmp"), "PatioToCraneliftTests");
+
+ var objectFilePath = CraneliftBackend.compileCompilationUnit(
+ patio,
+ new PartialTargetPath(workingDirectory.resolve("main"))
+ );
+
+ var executablePath = workingDirectory.resolve("main");
+
+ var runtime = ShedRuntime.defaultRuntime(LlvmTarget.AMD64.targetName());
+ CraneliftBackend.link(
+ List.of(
+ objectFilePath,
+ runtime.dir().resolve("libpatio_test.a")
+ ),
+ executablePath,
+ runtime
+ );
+
+ return TestProcessRunner.runCaptureOutput(List.of(executablePath.toString()));
+
+ }
+}