Skip to content

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

Merged
merged 3 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Sources/LLVM/JIT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
Expand Down Expand Up @@ -61,6 +67,42 @@ 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? {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the return type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by document it, other than what's there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- returns:

Copy link
Member Author

Choose a reason for hiding this comment

The 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`.
Expand Down
11 changes: 11 additions & 0 deletions Sources/LLVM/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public final class Module: CustomStringConvertible {
return TargetData(llvm: LLVMGetModuleDataLayout(llvm))
}

/// The identifier of this module.
public var name: String {
get {
guard let id = LLVMGetModuleIdentifier(llvm, nil) else { return "" }
return String(cString: id)
}
set {
LLVMSetModuleIdentifier(llvm, newValue, newValue.utf8.count)
}
}

/// Print a representation of a module to a file at the given path.
///
/// If the provided path is not suitable for writing, this function will throw
Expand Down