JIT

public final class JIT

A JIT is a Just-In-Time compiler that will compile and execute LLVM IR that has been generated in a Module. It can execute arbitrary functions and return the value the function generated, allowing you to write interactive programs that will run as soon as they are compiled.

  • Creates a Just In Time compiler that will compile the code in the provided Module to the architecture of the provided TargetMachine, and execute it.

    Throws

    JITError

    Declaration

    Swift

    public init(module: Module, machine: TargetMachine) throws

    Parameters

    module

    The module containing code you wish to execute

    machine

    The target machine which you’re compiling for

  • Runs the specified function with the provided arguments by compiling it to machine code for the target architecture used to initialize this JIT.

    Declaration

    Swift

    public func runFunction(_ function: Function, args: [IRValue]) -> IRValue

    Parameters

    function

    The function you wish to execute

    args

    The arguments you wish to pass to the function

    Return Value

    The LLVM value that the function returned

  • 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)
    

    Declaration

    Swift

    public func addressOfFunction(name: String) -> OpaquePointer?

    Parameters

    name

    The name of the function you wish to look up.

    Return Value

    A pointer to the result of compiling the specified function.

  • Adds the provided module, and all top-level declarations into this JIT. - parameter module: The module you wish to add.

    Declaration

    Swift

    public func addModule(_ module: Module)

    Parameters

    module

    The module you wish to add.

  • Removes the provided module, and all top-level declarations, from this JIT.

    Declaration

    Swift

    public func removeModule(_ module: Module) throws
  • 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.

    Declaration

    Swift

    public func runFunctionAsMain(_ function: Function, args: [String]) -> Int

    Parameters

    function

    The main function you wish to execute

    args

    The string arguments you wish to pass to the function

    Return Value

    The numerical exit code returned by the function