@@ -7,9 +7,15 @@ public enum JITError: Error, CustomStringConvertible {
7
7
/// the failure.
8
8
case couldNotInitialize( String )
9
9
10
+ /// The JIT was unable to remove the provided module. A message is provided
11
+ /// explaining the failure
12
+ case couldNotRemoveModule( Module , String )
13
+
10
14
/// A human-readable description of the error.
11
15
public var description : String {
12
16
switch self {
17
+ case . couldNotRemoveModule( let module, let message) :
18
+ return " could not remove module ' \( module. name) ': \( message) "
13
19
case . couldNotInitialize( let message) :
14
20
return " could not initialize JIT: \( message) "
15
21
}
@@ -61,6 +67,42 @@ public final class JIT {
61
67
}
62
68
}
63
69
70
+ /// Retrieves a pointer to the function compiled by this JIT.
71
+ /// - parameter name: The name of the function you wish to look up.
72
+ /// - returns: A pointer to the result of compiling the specified function.
73
+ /// - note: You will have to `unsafeBitCast` this pointer to
74
+ /// the appropriate `@convention(c)` function type to be
75
+ /// able to run it from Swift.
76
+ ///
77
+ /// ```
78
+ /// typealias FnPtr = @convention(c) () -> Double
79
+ /// let fnAddr = jit.addressOfFunction(name: "test")
80
+ /// let fn = unsafeBitCast(fnAddr, to: FnPtr.self)
81
+ /// ```
82
+ public func addressOfFunction( name: String ) -> OpaquePointer ? {
83
+ let addr = LLVMGetFunctionAddress ( llvm, name)
84
+ guard addr != 0 else { return nil }
85
+ return OpaquePointer ( bitPattern: UInt ( addr) )
86
+ }
87
+
88
+ /// Adds the provided module, and all top-level declarations into this JIT.
89
+ /// - parameter module: The module you wish to add.
90
+ public func addModule( _ module: Module ) {
91
+ LLVMAddModule ( llvm, module. llvm)
92
+ }
93
+
94
+ /// Removes the provided module, and all top-level declarations, from this
95
+ /// JIT.
96
+ public func removeModule( _ module: Module ) throws {
97
+ var outMod : LLVMModuleRef ? = module. llvm
98
+ var outError : UnsafeMutablePointer < Int8 > ?
99
+ LLVMRemoveModule ( llvm, module. llvm, & outMod, & outError)
100
+ if let err = outError {
101
+ defer { LLVMDisposeMessage ( err) }
102
+ throw JITError . couldNotRemoveModule ( module, String ( cString: err) )
103
+ }
104
+ }
105
+
64
106
/// Runs the specified function as if it were the `main` function in an
65
107
/// executable. It takes an array of argument strings and passes them
66
108
/// into the function as `argc` and `argv`.
0 commit comments