Skip to content

Commit 89e573a

Browse files
authored
Merge pull request #161 from CodaFi/target-acquired
Fill out Target's API
2 parents 9358325 + 1286a3a commit 89e573a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Sources/LLVM/TargetMachine.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,61 @@ public class Target {
7575
public init(llvm: LLVMTargetRef) {
7676
self.llvm = llvm
7777
}
78+
79+
/// Returns `true` if this target has a JIT.
80+
var hasJIT: Bool {
81+
return LLVMTargetHasJIT(self.llvm) != 0
82+
}
83+
84+
/// The name of this target.
85+
public var name: String {
86+
guard let str = LLVMGetTargetName(self.llvm) else {
87+
return ""
88+
}
89+
return String(validatingUTF8: UnsafePointer<CChar>(str)) ?? ""
90+
}
91+
92+
/// The description of this target.
93+
public var targetDescription: String {
94+
guard let str = LLVMGetTargetDescription(self.llvm) else {
95+
return ""
96+
}
97+
return String(validatingUTF8: UnsafePointer<CChar>(str)) ?? ""
98+
}
99+
100+
/// Returns `true` if this target has a `TargetMachine` associated with it.
101+
var hasTargetMachine: Bool {
102+
return LLVMTargetHasTargetMachine(self.llvm) != 0
103+
}
104+
105+
/// Returns `true` if this target has an ASM backend
106+
var hasASMBackend: Bool {
107+
return LLVMTargetHasTargetMachine(self.llvm) != 0
108+
}
109+
110+
/// Returns a sequence of all targets in the global list of targets.
111+
public static var allTargets: AnySequence<Target> {
112+
var current = firstTarget
113+
return AnySequence<Target> {
114+
return AnyIterator<Target> {
115+
defer { current = current?.next() }
116+
return current
117+
}
118+
}
119+
}
120+
121+
/// Returns the first target in the global target list, if it exists.
122+
public static var firstTarget: Target? {
123+
guard let targetRef = LLVMGetFirstTarget() else { return nil }
124+
return Target(llvm: targetRef)
125+
}
126+
127+
/// Returns the target following this target in the global target list,
128+
/// if it exists.
129+
public func next() -> Target? {
130+
guard let targetRef = LLVMGetNextTarget(self.llvm) else { return nil }
131+
return Target(llvm: targetRef)
132+
}
78133
}
79134

80135
/// A `TargetMachine` object represents an object that encapsulates information

0 commit comments

Comments
 (0)