-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactored backend target registry
- Loading branch information
1 parent
90f1876
commit 8c01032
Showing
3 changed files
with
69 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 6 additions & 26 deletions
32
src/main/java/me/mtk/torrey/backend/CompilerBackendFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |