Skip to content

Commit 3aff14a

Browse files
authored
Merge pull request #186 from CodaFi/target-practice
Port Target API
2 parents 81935f3 + d7f7c84 commit 3aff14a

File tree

8 files changed

+2301
-21
lines changed

8 files changed

+2301
-21
lines changed

Sources/LLVM/Module.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ public final class Module: CustomStringConvertible {
9393
}
9494

9595
/// Obtain the target triple for this module.
96-
var targetTriple: String {
96+
var targetTriple: Triple {
9797
get {
98-
guard let id = LLVMGetTarget(llvm) else { return "" }
99-
return String(cString: id)
98+
guard let id = LLVMGetTarget(llvm) else { return Triple("") }
99+
return Triple(String(cString: id))
100100
}
101-
set { LLVMSetTarget(llvm, newValue) }
101+
set { LLVMSetTarget(llvm, newValue.data) }
102102
}
103103

104104
/// Returns the context associated with this module.

Sources/LLVM/TargetMachine.swift

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,8 @@ public class TargetMachine {
144144
/// The data layout semantics associated with this target machine.
145145
public let dataLayout: TargetData
146146

147-
/// A string representing the target triple for this target machine. In the
148-
/// form `<arch><sub>-<vendor>-<sys>-<abi>` where
149-
///
150-
/// - arch = x86_64, i386, arm, thumb, mips, etc.
151-
/// - sub = for ex. on ARM: v5, v6m, v7a, v7m, etc.
152-
/// - vendor = pc, apple, nvidia, ibm, etc.
153-
/// - sys = none, linux, win32, darwin, cuda, etc.
154-
/// - abi = eabi, gnu, android, macho, elf, etc.
155-
public let triple: String
147+
/// The target triple for this target machine.
148+
public let triple: Triple
156149

157150
/// The CPU associated with this target machine.
158151
public var cpu: String {
@@ -190,24 +183,24 @@ public class TargetMachine {
190183
/// - parameter codeModel: The kind of code to produce for this target. If
191184
/// no model is provided, the default model for the target architecture is
192185
/// assumed.
193-
public init(triple: String? = nil, cpu: String = "", features: String = "",
186+
public init(triple: Triple = .default, cpu: String = "", features: String = "",
194187
optLevel: CodeGenOptLevel = .default, relocations: RelocationModel = .default,
195188
codeModel: CodeModel = .default) throws {
196189

197190
// Ensure the LLVM initializer is called when the first module is created
198191
initializeLLVM()
199192

200-
self.triple = triple ?? String(cString: LLVMGetDefaultTargetTriple()!)
193+
self.triple = triple
201194
var target: LLVMTargetRef?
202195
var error: UnsafeMutablePointer<Int8>?
203-
LLVMGetTargetFromTriple(self.triple, &target, &error)
196+
LLVMGetTargetFromTriple(self.triple.data, &target, &error)
204197
if let error = error {
205198
defer { LLVMDisposeMessage(error) }
206-
throw TargetMachineError.couldNotCreateTarget(self.triple,
199+
throw TargetMachineError.couldNotCreateTarget(self.triple.data,
207200
String(cString: error))
208201
}
209202
self.target = Target(llvm: target!)
210-
self.llvm = LLVMCreateTargetMachine(target!, self.triple, cpu, features,
203+
self.llvm = LLVMCreateTargetMachine(target!, self.triple.data, cpu, features,
211204
optLevel.asLLVM(),
212205
relocations.asLLVM(),
213206
codeModel.asLLVM())

0 commit comments

Comments
 (0)