From 1286a3a650f06c018184eb2149dff9d6bbda06ea Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Tue, 6 Nov 2018 14:39:25 -0500 Subject: [PATCH] Fill out Target's API --- Sources/LLVM/TargetMachine.swift | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Sources/LLVM/TargetMachine.swift b/Sources/LLVM/TargetMachine.swift index 66cfb313..7ae34235 100644 --- a/Sources/LLVM/TargetMachine.swift +++ b/Sources/LLVM/TargetMachine.swift @@ -75,6 +75,61 @@ public class Target { public init(llvm: LLVMTargetRef) { self.llvm = llvm } + + /// Returns `true` if this target has a JIT. + var hasJIT: Bool { + return LLVMTargetHasJIT(self.llvm) != 0 + } + + /// The name of this target. + public var name: String { + guard let str = LLVMGetTargetName(self.llvm) else { + return "" + } + return String(validatingUTF8: UnsafePointer(str)) ?? "" + } + + /// The description of this target. + public var targetDescription: String { + guard let str = LLVMGetTargetDescription(self.llvm) else { + return "" + } + return String(validatingUTF8: UnsafePointer(str)) ?? "" + } + + /// Returns `true` if this target has a `TargetMachine` associated with it. + var hasTargetMachine: Bool { + return LLVMTargetHasTargetMachine(self.llvm) != 0 + } + + /// Returns `true` if this target has an ASM backend + var hasASMBackend: Bool { + return LLVMTargetHasTargetMachine(self.llvm) != 0 + } + + /// Returns a sequence of all targets in the global list of targets. + public static var allTargets: AnySequence { + var current = firstTarget + return AnySequence { + return AnyIterator { + defer { current = current?.next() } + return current + } + } + } + + /// Returns the first target in the global target list, if it exists. + public static var firstTarget: Target? { + guard let targetRef = LLVMGetFirstTarget() else { return nil } + return Target(llvm: targetRef) + } + + /// Returns the target following this target in the global target list, + /// if it exists. + public func next() -> Target? { + guard let targetRef = LLVMGetNextTarget(self.llvm) else { return nil } + return Target(llvm: targetRef) + } } /// A `TargetMachine` object represents an object that encapsulates information