aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler
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 /src/main/java/org/shedlang/compiler
parent81fedf90c181c305703a088f097c81d37e6745a4 (diff)
Add initial skeleton of cranelift backend
Diffstat (limited to 'src/main/java/org/shedlang/compiler')
-rw-r--r--src/main/java/org/shedlang/compiler/backends/cranelift/CraneliftBackend.java50
1 files changed, 50 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);
+ }
+}