From 04cf66d6b53121998b33889a5495aaef3925cdfd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 24 Mar 2024 14:10:30 +0000 Subject: Use profiles when compiling --- src/main/java/org/shedlang/compiler/ShedCompiler.java | 19 ++++++++++++------- .../java/org/shedlang/compiler/backends/Backend.java | 3 ++- .../org/shedlang/compiler/backends/PatioBackends.java | 11 ++++++----- .../shedlang/compiler/backends/PatioTextBackend.java | 7 ++++--- .../shedlang/compiler/backends/ShidTextBackend.java | 3 ++- .../shedlang/compiler/backends/llvm/LlvmBackend.java | 15 +++++++++------ .../shedlang/compiler/backends/wasm/WasmBackend.java | 15 +++++++++------ src/main/java/org/shedlang/compiler/cli/ShedCli.java | 6 +++--- .../org/shedlang/compiler/config/GcBumpConfig.java | 4 ++++ .../java/org/shedlang/compiler/config/GcConfig.java | 3 +++ .../compiler/config/GcMarkAndSweepConfig.java | 9 +++++++++ .../org/shedlang/compiler/config/ProfileConfig.java | 2 ++ .../org/shedlang/compiler/config/ProjectConfig.java | 8 ++++++++ .../compiler/filesystem/ShedCompilerFiles.java | 6 ++++-- 14 files changed, 77 insertions(+), 34 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java index 1f04c0fa..45c189bf 100644 --- a/src/main/java/org/shedlang/compiler/ShedCompiler.java +++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java @@ -4,6 +4,7 @@ import org.shedlang.compiler.ast.untyped.UntypedNamespaceNode; import org.shedlang.compiler.backends.Backend; import org.shedlang.compiler.backends.paths.TargetDir; import org.shedlang.compiler.config.ConfigChecker; +import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.parser.Parser; import org.shedlang.compiler.shid.ShidCodeGen; @@ -21,10 +22,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.StreamSupport; import static org.shedlang.compiler.parser.Tokeniser.tokenise; @@ -34,7 +32,7 @@ public class ShedCompiler { public void compile( Path projectRoot, Backend backend, - String profileName + Optional profileName ) throws IOException, InterruptedException { var config = ProjectConfig.read(projectRoot); @@ -75,12 +73,19 @@ public class ShedCompiler { ConfigChecker.configCheck(config, typeInfos); + var profileConfig = profileName.map(config::profile) + .orElse(ProfileConfig.DEFAULT); + backend.compile( config, - profileName, + profileConfig, namespaces, typeInfos, - new TargetDir(config.targetDir().resolve(backend.targetName())) + new TargetDir( + config.targetDir() + .resolve(backend.targetName()) + .resolve(profileConfig.name()) + ) ); } diff --git a/src/main/java/org/shedlang/compiler/backends/Backend.java b/src/main/java/org/shedlang/compiler/backends/Backend.java index 17b8400f..ddd57f8e 100644 --- a/src/main/java/org/shedlang/compiler/backends/Backend.java +++ b/src/main/java/org/shedlang/compiler/backends/Backend.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.backends; import org.shedlang.compiler.backends.paths.TargetDir; +import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.typechecker.TypeInfos; @@ -13,7 +14,7 @@ public interface Backend { void compile( ProjectConfig config, - String profileName, + ProfileConfig profileConfig, List namespaces, TypeInfos typeInfos, TargetDir targetDir diff --git a/src/main/java/org/shedlang/compiler/backends/PatioBackends.java b/src/main/java/org/shedlang/compiler/backends/PatioBackends.java index 8bdc15e0..bc90c6b4 100644 --- a/src/main/java/org/shedlang/compiler/backends/PatioBackends.java +++ b/src/main/java/org/shedlang/compiler/backends/PatioBackends.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.backends; import org.shedlang.compiler.config.BinConfig; +import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.patio.PatioCompilationUnit; import org.shedlang.compiler.shid.ShidNamespace; @@ -24,13 +25,13 @@ public class PatioBackends { String binName, PatioCompilationUnit patioCompilationUnit, String mainFunctionName, - String profileName + ProfileConfig profileConfig ) throws IOException, InterruptedException; } public static void compile( ProjectConfig config, - String profileName, + ProfileConfig profileConfig, List namespaces, TypeInfos typeInfos, PatioToTarget patioToTarget @@ -49,12 +50,12 @@ public class PatioBackends { } for (var bin : config.bins()) { - generateMain(profileName, bin, shidToPatioContext, patioToTarget); + generateMain(profileConfig, bin, shidToPatioContext, patioToTarget); } } private static void generateMain( - String profileName, + ProfileConfig profileConfig, BinConfig binConfig, ShidToPatioContext shidToPatioContext, PatioToTarget patioToTarget @@ -83,7 +84,7 @@ public class PatioBackends { binConfig.name(), patioCompilationUnit, mainFunctionName, - profileName + profileConfig ); } } diff --git a/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java b/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java index 929feac3..052408c0 100644 --- a/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/PatioTextBackend.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.backends; import org.shedlang.compiler.backends.paths.PartialTargetPath; import org.shedlang.compiler.backends.paths.TargetDir; +import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.patio.PatioCompilationUnit; import org.shedlang.compiler.patio.PatioSerializer; @@ -26,14 +27,14 @@ public class PatioTextBackend implements Backend { public void compile( ProjectConfig config, - String profileName, + ProfileConfig profileConfig, List namespaces, TypeInfos typeInfos, TargetDir targetDir ) throws IOException, InterruptedException { PatioBackends.compile( config, - profileName, + profileConfig, namespaces, typeInfos, new PatioToText(targetDir) @@ -66,7 +67,7 @@ public class PatioTextBackend implements Backend { String binName, PatioCompilationUnit patioCompilationUnit, String mainFunctionName, - String profileName + ProfileConfig profileConfig ) throws IOException, InterruptedException { serializeToDisk( targetDir.bin(binName), diff --git a/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java b/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java index 36af4357..f34c18b2 100644 --- a/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/ShidTextBackend.java @@ -1,6 +1,7 @@ package org.shedlang.compiler.backends; import org.shedlang.compiler.backends.paths.TargetDir; +import org.shedlang.compiler.config.ProfileConfig; import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.shid.ShidSerializer; @@ -19,7 +20,7 @@ public class ShidTextBackend implements Backend { @Override public void compile( ProjectConfig config, - String profileName, + ProfileConfig profileConfig, List namespaces, TypeInfos typeInfos, TargetDir targetDir 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 70430da7..bb951a9f 100644 --- a/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/llvm/LlvmBackend.java @@ -15,6 +15,8 @@ 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; @@ -54,14 +56,14 @@ public class LlvmBackend implements Backend { @Override public void compile( ProjectConfig config, - String profileName, + ProfileConfig profileConfig, List namespaces, TypeInfos typeInfos, TargetDir targetDir ) throws IOException, InterruptedException { PatioBackends.compile( config, - profileName, + profileConfig, namespaces, typeInfos, new PatioToBinary( @@ -121,7 +123,7 @@ public class LlvmBackend implements Backend { String binName, PatioCompilationUnit patioCompilationUnit, String mainFunctionName, - String profileName + ProfileConfig profileConfig ) throws IOException, InterruptedException { var linkInputs = new ArrayList<>(objectFilePaths); @@ -133,15 +135,16 @@ public class LlvmBackend implements Backend { var mainObjectPath = llvmCompiler.compileLlvmModule(mainModule, targetDir.bin(binName)); linkInputs.add(mainObjectPath); - link(linkInputs, targetDir.topLevel(binName), profileName); + link(linkInputs, targetDir.topLevel(binName), profileConfig.gc().orElse(GcConfig.DEFAULT)); } } public void link( List inputs, Path outputPath, - String profileName + GcConfig gcConfig ) throws ProcessError, InterruptedException { + // TODO: handle config for GC var clangArgs = new ArrayList(); clangArgs.add("clang"); @@ -154,7 +157,7 @@ public class LlvmBackend implements Backend { var runtimeDir = ShedCompilerFiles.targetDir( this.target.targetName(), - profileName + gcConfig ); clangArgs.add(runtimeDir.resolve("libpatio.a").toString()); clangArgs.add(runtimeDir.resolve("libshed.a").toString()); 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 6069563f..3a7a4733 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmBackend.java @@ -12,6 +12,8 @@ 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; @@ -52,14 +54,14 @@ public class WasmBackend implements Backend { @Override public void compile( ProjectConfig config, - String profileName, + ProfileConfig profileConfig, List namespaces, TypeInfos typeInfos, TargetDir targetDir ) throws IOException, InterruptedException { PatioBackends.compile( config, - profileName, + profileConfig, namespaces, typeInfos, new PatioToWasmBinary(targetDir, P.vector()) @@ -101,7 +103,7 @@ public class WasmBackend implements Backend { String binName, PatioCompilationUnit patioCompilationUnit, String mainFunctionName, - String profileName + ProfileConfig profileConfig ) throws IOException, InterruptedException { var linkInputs = new ArrayList<>(objectFilePaths); @@ -110,7 +112,7 @@ public class WasmBackend implements Backend { linkInputs.add(mainObjectPath); var outputBinPath = targetDir.topLevel(binName + ".wasm"); - link(linkInputs, outputBinPath, profileName); + link(linkInputs, outputBinPath, profileConfig.gc().orElse(GcConfig.DEFAULT)); validate(outputBinPath); } } @@ -118,8 +120,9 @@ public class WasmBackend implements Backend { public void link( List inputs, Path outputPath, - String profileName + GcConfig gcConfig ) throws ProcessError, InterruptedException { + // TODO: handle config for GC var ldArgs = new ArrayList(); ldArgs.add("wasm-ld-17"); @@ -134,7 +137,7 @@ public class WasmBackend implements Backend { var runtimeDir = ShedCompilerFiles.targetDir( this.target.targetName(), - profileName + gcConfig ); ldArgs.add(runtimeDir.resolve("libpatio.a").toString()); ldArgs.add(runtimeDir.resolve("libshed.a").toString()); diff --git a/src/main/java/org/shedlang/compiler/cli/ShedCli.java b/src/main/java/org/shedlang/compiler/cli/ShedCli.java index 5d7baf63..6e5d115b 100644 --- a/src/main/java/org/shedlang/compiler/cli/ShedCli.java +++ b/src/main/java/org/shedlang/compiler/cli/ShedCli.java @@ -9,6 +9,7 @@ import org.shedlang.compiler.errors.CompilerError; import java.io.IOException; import java.nio.file.Paths; +import java.util.Optional; public class ShedCli { public static void main(String[] rawArgs) throws IOException, InterruptedException { @@ -30,7 +31,7 @@ public class ShedCli { switch (args.get("command")) { case COMPILE -> { var backend = Backends.backendForTarget(args.getString("target")); - var profile = args.getString("profile"); + var profile = Optional.ofNullable(args.getString("profile")); compiler.compile(sourcePath, backend, profile); } } @@ -46,8 +47,7 @@ public class ShedCli { compileSubParser.setDefault("command", Command.COMPILE); compileSubParser.addArgument("source").required(true); compileSubParser.addArgument("--target").choices(Backends.targetNames()).required(true); - // TODO: support profiles properly (define profile, allow GC as an option) - compileSubParser.addArgument("--profile").setDefault("mark-and-sweep").required(false); + compileSubParser.addArgument("--profile").required(false); try { return parser.parseArgs(args); diff --git a/src/main/java/org/shedlang/compiler/config/GcBumpConfig.java b/src/main/java/org/shedlang/compiler/config/GcBumpConfig.java index bfbd7d4e..02f39173 100644 --- a/src/main/java/org/shedlang/compiler/config/GcBumpConfig.java +++ b/src/main/java/org/shedlang/compiler/config/GcBumpConfig.java @@ -1,4 +1,8 @@ package org.shedlang.compiler.config; public record GcBumpConfig() implements GcConfig { + @Override + public String name() { + return "bump"; + } } diff --git a/src/main/java/org/shedlang/compiler/config/GcConfig.java b/src/main/java/org/shedlang/compiler/config/GcConfig.java index 6d8ed950..12691658 100644 --- a/src/main/java/org/shedlang/compiler/config/GcConfig.java +++ b/src/main/java/org/shedlang/compiler/config/GcConfig.java @@ -4,6 +4,7 @@ import org.tomlj.TomlTable; public sealed interface GcConfig permits GcBumpConfig, GcMarkAndSweepConfig { GcConfig BUMP = new GcBumpConfig(); + GcConfig DEFAULT = GcMarkAndSweepConfig.DEFAULT; static GcConfig fromToml(TomlTable table) { var name = table.getString("name"); @@ -23,4 +24,6 @@ public sealed interface GcConfig permits GcBumpConfig, GcMarkAndSweepConfig { throw new IllegalArgumentException("Unrecognised GC name: \"%s\"".formatted(name)); }; } + + String name(); } diff --git a/src/main/java/org/shedlang/compiler/config/GcMarkAndSweepConfig.java b/src/main/java/org/shedlang/compiler/config/GcMarkAndSweepConfig.java index 695654ea..b54add24 100644 --- a/src/main/java/org/shedlang/compiler/config/GcMarkAndSweepConfig.java +++ b/src/main/java/org/shedlang/compiler/config/GcMarkAndSweepConfig.java @@ -7,8 +7,17 @@ import java.util.Optional; public record GcMarkAndSweepConfig( Optional collectInterval ) implements GcConfig { + public static final GcMarkAndSweepConfig DEFAULT = new GcMarkAndSweepConfig( + Optional.empty() + ); + public static GcMarkAndSweepConfig fromToml(TomlTable table) { var collectInterval = Optional.ofNullable(table.getLong("collect-interval")); return new GcMarkAndSweepConfig(collectInterval); } + + @Override + public String name() { + return "mark-and-sweep"; + } } diff --git a/src/main/java/org/shedlang/compiler/config/ProfileConfig.java b/src/main/java/org/shedlang/compiler/config/ProfileConfig.java index b8854721..eba2878f 100644 --- a/src/main/java/org/shedlang/compiler/config/ProfileConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ProfileConfig.java @@ -5,6 +5,8 @@ import org.tomlj.TomlTable; import java.util.Optional; public record ProfileConfig(String name, Optional gc) { + public static final ProfileConfig DEFAULT = new ProfileConfig("dev", Optional.empty()); + public static ProfileConfig fromToml(TomlTable table) { var name = table.getString("name"); var gc = Optional.ofNullable(table.getTable("gc")) diff --git a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java index b7453f70..3381e521 100644 --- a/src/main/java/org/shedlang/compiler/config/ProjectConfig.java +++ b/src/main/java/org/shedlang/compiler/config/ProjectConfig.java @@ -68,4 +68,12 @@ public record ProjectConfig( public Path targetDir() { return root.resolve("target"); } + + public ProfileConfig profile(String profileName) { + return this.profiles.stream() + .filter(profile -> profile.name().equals(profileName)) + .findFirst() + // TODO: better error + .orElseThrow(); + } } diff --git a/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java b/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java index 5c195448..57f24ae4 100644 --- a/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java +++ b/src/main/java/org/shedlang/compiler/filesystem/ShedCompilerFiles.java @@ -1,5 +1,7 @@ package org.shedlang.compiler.filesystem; +import org.shedlang.compiler.config.GcConfig; + import java.nio.file.Path; public class ShedCompilerFiles { @@ -11,10 +13,10 @@ public class ShedCompilerFiles { return Path.of("."); } - public static Path targetDir(String targetName, String profileName) { + public static Path targetDir(String targetName, GcConfig gcConfig) { return getRoot() .resolve("runtime/target") .resolve(targetName) - .resolve(profileName); + .resolve(gcConfig.name()); } } -- cgit v1.2.3