Skip to content

Commit

Permalink
refactor: refactored backend target registry
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewKosloski committed Sep 26, 2021
1 parent 90f1876 commit 8c01032
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
24 changes: 21 additions & 3 deletions src/main/java/me/mtk/torrey/Torrey.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -114,8 +130,10 @@ public void showRegisteredTargets()
.append("\n\twhere <triple> is of the form ")
.append("<arch>-<vendor>-<sys>.\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);
}
Expand Down
32 changes: 6 additions & 26 deletions src/main/java/me/mtk/torrey/backend/CompilerBackendFactory.java
Original file line number Diff line number Diff line change
@@ -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<String, TargetTriple> 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<String, TargetTriple> targetStringToTripleMap()
{
return targetStringToTripleMap;
}
}
42 changes: 42 additions & 0 deletions src/main/java/me/mtk/torrey/backend/TargetRegistry.java
Original file line number Diff line number Diff line change
@@ -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<String, TargetTriple> 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<String> getKeys()
{
final List<String> keys = new ArrayList<>();
registry.forEach((k, v) -> keys.add(k));
return keys;
}

}

0 comments on commit 8c01032

Please sign in to comment.