From 85f9578fb680ab4ee959469133714acd908a7c10 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 24 Mar 2024 14:52:59 +0000 Subject: Support custom runtimes when compiling --- .../compiler/backends/llvm/LlvmBackend.java | 26 ++++---- .../compiler/backends/wasm/WasmBackend.java | 28 +++++---- .../compiler/filesystem/ShedCompilerFiles.java | 6 +- .../org/shedlang/compiler/runtime/ShedRuntime.java | 73 ++++++++++++++++++++++ 4 files changed, 106 insertions(+), 27 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java index 586c4555..f2c7c2ef 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java @@ -15,11 +15,10 @@ 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.backends.paths.TargetDir; -import org.shedlang.compiler.config.GcConfig; import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; -import org.shedlang.compiler.filesystem.ShedCompilerFiles; import org.shedlang.compiler.patio.PatioCompilationUnit; +import org.shedlang.compiler.runtime.ShedRuntime; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.typechecker.TypeInfos; import org.shedlang.compiler.types.NamespaceName; @@ -135,16 +134,25 @@ public class LlvmBackend implements Backend { var mainObjectPath = llvmCompiler.compileLlvmModule(mainModule, targetDir.bin(binName)); linkInputs.add(mainObjectPath); - link(linkInputs, targetDir.topLevel(binName), profileConfig.gc().orElse(GcConfig.DEFAULT)); + var shedRuntime = ShedRuntime.runtime( + target.targetName(), + profileConfig.gc(), + targetDir + ); + + link( + linkInputs, + targetDir.topLevel(binName), + shedRuntime + ); } } public void link( List inputs, Path outputPath, - GcConfig gcConfig + ShedRuntime shedRuntime ) throws ProcessError, InterruptedException { - // TODO: handle config for GC var clangArgs = new ArrayList(); clangArgs.add("clang"); @@ -155,12 +163,8 @@ public class LlvmBackend implements Backend { clangArgs.add(input.toString()); } - var runtimeDir = ShedCompilerFiles.runtimeTargetDir( - this.target.targetName(), - gcConfig - ); - clangArgs.add(runtimeDir.resolve("libpatio.a").toString()); - clangArgs.add(runtimeDir.resolve("libshed.a").toString()); + clangArgs.add(shedRuntime.dir().resolve("libpatio.a").toString()); + clangArgs.add(shedRuntime.dir().resolve("libshed.a").toString()); clangArgs.addAll(this.target.clangFlags()); diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java index f850c3a6..60e383ef 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java @@ -12,11 +12,10 @@ import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmExpres import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; -import org.shedlang.compiler.config.GcConfig; import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; -import org.shedlang.compiler.filesystem.ShedCompilerFiles; import org.shedlang.compiler.patio.PatioCompilationUnit; +import org.shedlang.compiler.runtime.ShedRuntime; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.typechecker.TypeInfos; import org.shedlang.compiler.types.NamespaceName; @@ -111,8 +110,18 @@ public class WasmBackend implements Backend { var mainObjectPath = writeObjectFile(mainModule, targetDir.bin(binName)); linkInputs.add(mainObjectPath); + var shedRuntime = ShedRuntime.runtime( + target.targetName(), + profileConfig.gc(), + targetDir + ); + var outputBinPath = targetDir.topLevel(binName + ".wasm"); - link(linkInputs, outputBinPath, profileConfig.gc().orElse(GcConfig.DEFAULT)); + link( + linkInputs, + outputBinPath, + shedRuntime + ); validate(outputBinPath); } } @@ -120,9 +129,8 @@ public class WasmBackend implements Backend { public void link( List inputs, Path outputPath, - GcConfig gcConfig + ShedRuntime shedRuntime ) throws ProcessError, InterruptedException { - // TODO: handle config for GC var ldArgs = new ArrayList(); ldArgs.add("wasm-ld-17"); @@ -135,13 +143,9 @@ public class WasmBackend implements Backend { ldArgs.add(input.toString()); } - var runtimeDir = ShedCompilerFiles.runtimeTargetDir( - this.target.targetName(), - gcConfig - ); - ldArgs.add(runtimeDir.resolve("libpatio.a").toString()); - ldArgs.add(runtimeDir.resolve("libshed.a").toString()); - ldArgs.add(runtimeDir.resolve("libwalloc.a").toString()); + ldArgs.add(shedRuntime.dir().resolve("libpatio.a").toString()); + ldArgs.add(shedRuntime.dir().resolve("libshed.a").toString()); + ldArgs.add(shedRuntime.dir().resolve("libwalloc.a").toString()); runInheritIo(ldArgs); } diff --git a/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java b/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java index 7924f25e..3e75e199 100644 --- a/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java +++ b/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java @@ -1,7 +1,5 @@ package org.shedlang.compiler.filesystem; -import org.shedlang.compiler.config.GcConfig; - import java.nio.file.Path; public class ShedCompilerFiles { @@ -13,10 +11,10 @@ public class ShedCompilerFiles { return Path.of("."); } - public static Path runtimeTargetDir(String targetName, GcConfig gcConfig) { + public static Path standardRuntimeTargetDir(String targetName, String gcName) { return getRoot() .resolve("runtime/target") .resolve(targetName) - .resolve(gcConfig.name()); + .resolve(gcName); } } diff --git a/src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java b/src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java new file mode 100644 index 00000000..25a81345 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/runtime/ShedRuntime.java @@ -0,0 +1,73 @@ +package org.shedlang.compiler.runtime; + +import org.shedlang.compiler.backends.paths.TargetDir; +import org.shedlang.compiler.config.GcConfig; +import org.shedlang.compiler.config.GcMarkAndSweepConfig; +import org.shedlang.compiler.filesystem.ShedCompilerFiles; +import org.shedlang.compiler.util.processes.ProcessError; + +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.shedlang.compiler.util.processes.ProcessRunner.runInheritIo; + +public class ShedRuntime { + private static final Map STANDARD_RUNTIMES = Map.ofEntries( + Map.entry(GcConfig.BUMP, "bump"), + Map.entry(GcMarkAndSweepConfig.DEFAULT, "mark-and-sweep") + ); + + public static ShedRuntime defaultRuntime(String targetName) { + return new ShedRuntime( + ShedCompilerFiles.standardRuntimeTargetDir( + targetName, + "mark-and-sweep" + ) + ); + } + + public static ShedRuntime runtime( + String targetName, + Optional gcConfig, + TargetDir targetDir + ) throws ProcessError, InterruptedException { + var gc = gcConfig.orElse(GcConfig.DEFAULT); + var standardRuntimeName = STANDARD_RUNTIMES.get(gc); + + if (standardRuntimeName != null) { + return new ShedRuntime( + ShedCompilerFiles.standardRuntimeTargetDir( + targetName, + standardRuntimeName + ) + ); + } + + // TODO: deal with stale compiled runtimes (in build.py?) + // TODO: handle GC config + var runtimeDir = targetDir.topLevel("runtime"); + var buildRuntimeCommand = List.of( + "python3", + ShedCompilerFiles.getRoot().resolve("runtime/build.py").toString(), + "custom", + "--target", targetName, + "--gc", gc.name(), + "--output-dir", runtimeDir.toString() + ); + runInheritIo(buildRuntimeCommand); + + return new ShedRuntime(runtimeDir); + } + + private final Path dir; + + private ShedRuntime(Path dir) { + this.dir = dir; + } + + public Path dir() { + return this.dir; + } +} -- cgit v1.2.3