-
Notifications
You must be signed in to change notification settings - Fork 57
Added module-related functions to JIT #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,15 @@ public enum JITError: Error, CustomStringConvertible { | |
/// the failure. | ||
case couldNotInitialize(String) | ||
|
||
/// The JIT was unable to remove the provided module. A message is provided | ||
/// explaining the failure | ||
case couldNotRemoveModule(Module, String) | ||
|
||
/// A human-readable description of the error. | ||
public var description: String { | ||
switch self { | ||
case .couldNotRemoveModule(let module, let message): | ||
return "could not remove module '\(module.name)': \(message)" | ||
case .couldNotInitialize(let message): | ||
return "could not initialize JIT: \(message)" | ||
} | ||
|
@@ -61,6 +67,40 @@ public final class JIT { | |
} | ||
} | ||
|
||
/// Retrieves a pointer to the function compiled by this JIT. | ||
/// - parameter name: The name of the function you wish to look up. | ||
/// - returns: A pointer to the result of compiling the specified function. | ||
/// - note: You will have to `unsafeBitCast` this pointer to | ||
/// the appropriate `@convention(c)` function type to be | ||
/// able to run it from Swift. | ||
/// | ||
/// typealias FnPtr = @convention(c) () -> Double | ||
/// let fnAddr = jit.addressOfFunction(name: "test") | ||
/// let fn = unsafeBitCast(fnAddr, to: FnPtr.self) | ||
public func addressOfFunction(name: String) -> OpaquePointer? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the return type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by document it, other than what's there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's there! 😅 |
||
let addr = LLVMGetFunctionAddress(llvm, name) | ||
guard addr != 0 else { return nil } | ||
return OpaquePointer(bitPattern: UInt(addr)) | ||
} | ||
|
||
/// Adds the provided module, and all top-level declarations into this JIT. | ||
/// - parameter module: The module you wish to add. | ||
public func addModule(_ module: Module) { | ||
LLVMAddModule(llvm, module.llvm) | ||
} | ||
|
||
/// Removes the provided module, and all top-level declarations, from this | ||
/// JIT. | ||
public func removeModule(_ module: Module) throws { | ||
var outMod: LLVMModuleRef? = module.llvm | ||
var outError: UnsafeMutablePointer<Int8>? | ||
LLVMRemoveModule(llvm, module.llvm, &outMod, &outError) | ||
if let err = outError { | ||
defer { LLVMDisposeMessage(err) } | ||
throw JITError.couldNotRemoveModule(module, String(cString: err)) | ||
} | ||
} | ||
|
||
/// Runs the specified function as if it were the `main` function in an | ||
/// executable. It takes an array of argument strings and passes them | ||
/// into the function as `argc` and `argv`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code voice plz.