diff --git a/src/main/java/me/mtk/torrey/Torrey.java b/src/main/java/me/mtk/torrey/Torrey.java index 5aa0b18..f225b9b 100644 --- a/src/main/java/me/mtk/torrey/Torrey.java +++ b/src/main/java/me/mtk/torrey/Torrey.java @@ -8,12 +8,28 @@ import me.mtk.torrey.backend.CompilerBackend; import me.mtk.torrey.backend.CompilerBackendFactory; import me.mtk.torrey.backend.TargetProgram; +import me.mtk.torrey.backend.TargetRegistry; +import me.mtk.torrey.backend.targets.x86_64.pc.linux.X8664PCLinuxTarget; public final class Torrey { // The semantic version number of the compiler. public static String SEMANTIC_VERSION = "3.0.8"; + private static TargetRegistry targetRegistry; + private static CompilerBackendFactory backendFactory; + + static + { + targetRegistry = new TargetRegistry(); + + // Install the compiler backends. + targetRegistry.add(new X8664PCLinuxTarget()); + + // Initialize the backend factory with the target registry. + backendFactory = new CompilerBackendFactory(targetRegistry); + } + public static void main(String ... args) { try @@ -64,7 +80,7 @@ else if (input == null && !config.help()) frontend.setInput(input); final IRProgram irProgram = frontend.run(); - CompilerBackend backend = CompilerBackendFactory + CompilerBackend backend = Torrey.backendFactory .makeBackendFromTarget(config.target()); if (backend == null) @@ -114,8 +130,10 @@ public void showRegisteredTargets() .append("\n\twhere is of the form ") .append("--.\n\n"); sb.append("Registered targets (triples):\n"); - CompilerBackendFactory.targetStringToTripleMap() - .forEach((k, v) -> sb.append("\t").append(k)); + + for (String targetStr : Torrey.targetRegistry.getKeys()) + sb.append("\t").append(targetStr); + System.out.println(sb.toString()); System.exit(0); } diff --git a/src/main/java/me/mtk/torrey/backend/CompilerBackendFactory.java b/src/main/java/me/mtk/torrey/backend/CompilerBackendFactory.java index 993825f..eed03a9 100644 --- a/src/main/java/me/mtk/torrey/backend/CompilerBackendFactory.java +++ b/src/main/java/me/mtk/torrey/backend/CompilerBackendFactory.java @@ -1,45 +1,25 @@ package me.mtk.torrey.backend; -import java.util.Map; -import java.util.HashMap; - -import me.mtk.torrey.backend.targets.x86_64.pc.linux.X8664PCLinuxTarget; -import me.mtk.torrey.backend.triple.*; - - /** * Handles the construction of compiler backends at runtime. */ public final class CompilerBackendFactory { - // Maps target triple strings to target triple instances. - private static Map targetStringToTripleMap; + private TargetRegistry registry; - static + public CompilerBackendFactory(TargetRegistry registry) { - targetStringToTripleMap = new HashMap<>(); - - // This is where the compiler backends are installed. - - final TargetTriple x8664PCLinuxTarget = new X8664PCLinuxTarget(); - targetStringToTripleMap.put(x8664PCLinuxTarget.toString(), - x8664PCLinuxTarget); + this.registry = registry; } - public static CompilerBackend makeBackendFromTarget(String target) + public CompilerBackend makeBackendFromTarget(String target) { - if (targetStringToTripleMap.containsKey(target)) + if (registry.hasTarget(target)) { - return targetStringToTripleMap.get(target) - .makeBackend(); + return registry.get(target).makeBackend(); } // Maybe replace this with an empty optional?? return null; } - - public static Map targetStringToTripleMap() - { - return targetStringToTripleMap; - } } diff --git a/src/main/java/me/mtk/torrey/backend/TargetRegistry.java b/src/main/java/me/mtk/torrey/backend/TargetRegistry.java new file mode 100644 index 0000000..b0e7eeb --- /dev/null +++ b/src/main/java/me/mtk/torrey/backend/TargetRegistry.java @@ -0,0 +1,42 @@ +package me.mtk.torrey.backend; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import me.mtk.torrey.backend.triple.TargetTriple; + +public class TargetRegistry +{ + private Map registry; + + public TargetRegistry() + { + registry = new HashMap<>(); + } + + public boolean hasTarget(String target) + { + return registry.containsKey(target); + } + + public TargetRegistry add(TargetTriple triple) + { + registry.put(triple.toString(), triple); + return this; + } + + public TargetTriple get(String target) + { + return registry.get(target); + } + + public List getKeys() + { + final List keys = new ArrayList<>(); + registry.forEach((k, v) -> keys.add(k)); + return keys; + } + +}