@@ -5,6 +5,10 @@ import cllvm
55/// JITError represents the different kinds of errors the JIT compiler can
66/// throw.
77public enum JITError : Error , CustomStringConvertible {
8+ /// A generic error thrown by the JIT during exceptional circumstances.
9+ ///
10+ /// In general, it is not safe to catch and continue after this exception has
11+ /// been thrown.
812 case generic( String )
913
1014 public var description : String {
@@ -23,7 +27,31 @@ public enum JITError: Error, CustomStringConvertible {
2327/// The JIT is fundamentally lazy, and allows control over when and how symbols
2428/// are resolved.
2529public final class JIT {
26- public typealias TargetAddress = LLVMOrcTargetAddress
30+ /// A type that represents an address, either symbolically within the JIT or
31+ /// physically in the execution environment.
32+ public struct TargetAddress : Comparable {
33+ fileprivate var llvm : LLVMOrcTargetAddress
34+
35+ /// Creates a target address value of `0`.
36+ public init ( ) {
37+ self . llvm = 0
38+ }
39+
40+ /// Creates a target address from a raw address value.
41+ public init ( raw: LLVMOrcTargetAddress ) {
42+ self . llvm = raw
43+ }
44+
45+ public static func == ( lhs: TargetAddress , rhs: TargetAddress ) -> Bool {
46+ return lhs. llvm == rhs. llvm
47+ }
48+
49+ public static func < ( lhs: TargetAddress , rhs: TargetAddress ) -> Bool {
50+ return lhs. llvm < rhs. llvm
51+ }
52+ }
53+
54+ /// Represents a handle to a module owned by the JIT stack.
2755 public struct ModuleHandle {
2856 fileprivate var llvm : LLVMOrcModuleHandle
2957 }
@@ -77,13 +105,13 @@ public final class JIT {
77105 /// restrict the search, if any.
78106 /// - returns: The address of the symbol, or 0 if it does not exist.
79107 public func address( of symbol: String , in module: ModuleHandle ? = nil ) throws -> TargetAddress {
80- var retAddr : TargetAddress = 0
108+ var retAddr : LLVMOrcTargetAddress = 0
81109 if let targetModule = module {
82110 try checkForJITError ( LLVMOrcGetSymbolAddressIn ( self . llvm, & retAddr, targetModule. llvm, symbol) )
83111 } else {
84112 try checkForJITError ( LLVMOrcGetSymbolAddress ( self . llvm, & retAddr, symbol) )
85113 }
86- return retAddr
114+ return TargetAddress ( raw : retAddr)
87115 }
88116
89117 // MARK: Lazy Compilation
@@ -103,11 +131,11 @@ public final class JIT {
103131 /// - returns: The target address representing a stub. Calling this stub
104132 /// forces the given compilation callback to fire.
105133 public func registerLazyCompile( _ callback: @escaping ( JIT ) -> TargetAddress ) throws -> TargetAddress {
106- var addr : TargetAddress = 0
134+ var addr : LLVMOrcTargetAddress = 0
107135 let callbackContext = ORCLazyCompileCallbackContext ( callback)
108136 let contextPtr = Unmanaged < ORCLazyCompileCallbackContext > . passRetained ( callbackContext) . toOpaque ( )
109137 try checkForJITError ( LLVMOrcCreateLazyCompileCallback ( self . llvm, & addr, lazyCompileBlockTrampoline, contextPtr) )
110- return addr
138+ return TargetAddress ( raw : addr)
111139 }
112140
113141 // MARK: Stubs
@@ -120,7 +148,7 @@ public final class JIT {
120148 /// - parameter name: The name of the indirect stub.
121149 /// - parameter address: The address of the indirect stub.
122150 public func createIndirectStub( named name: String , address: TargetAddress ) throws {
123- try checkForJITError ( LLVMOrcCreateIndirectStub ( self . llvm, name, address) )
151+ try checkForJITError ( LLVMOrcCreateIndirectStub ( self . llvm, name, address. llvm ) )
124152 }
125153
126154 /// Resets the address of an indirect stub.
@@ -132,7 +160,7 @@ public final class JIT {
132160 /// - parameter name: The name of an indirect stub.
133161 /// - parameter address: The address to set the indirect stub to point to.
134162 public func setIndirectStubPointer( named name: String , address: TargetAddress ) throws {
135- try checkForJITError ( LLVMOrcSetIndirectStubPointer ( self . llvm, name, address) )
163+ try checkForJITError ( LLVMOrcSetIndirectStubPointer ( self . llvm, name, address. llvm ) )
136164 }
137165
138166 // MARK: Adding Code to the JIT
@@ -251,7 +279,7 @@ private let lazyCompileBlockTrampoline : LLVMOrcLazyCompileCallbackFn = { (callb
251279
252280 let tempJIT = JIT ( llvm: jit, ownsContext: false )
253281 let callback = Unmanaged < ORCLazyCompileCallbackContext > . fromOpaque ( ctx) . takeUnretainedValue ( )
254- return callback. block ( tempJIT)
282+ return callback. block ( tempJIT) . llvm
255283}
256284
257285private let symbolBlockTrampoline : LLVMOrcSymbolResolverFn = { ( callbackName, callbackCtx) in
@@ -261,7 +289,7 @@ private let symbolBlockTrampoline : LLVMOrcSymbolResolverFn = { (callbackName, c
261289
262290 let name = String ( cString: cname)
263291 let callback = Unmanaged < ORCSymbolCallbackContext > . fromOpaque ( ctx) . takeUnretainedValue ( )
264- return callback. block ( name)
292+ return callback. block ( name) . llvm
265293}
266294
267295private class ORCLazyCompileCallbackContext {
0 commit comments