class RCGTK::ExecutionEngine

The ExecutionEngine class and its subclasses execute code from the provided module, as well as providing a {PassManager} and {FunctionPassManager} for optimizing modules.

@abstract Implemented by {Interpreter} and {JITCompiler}.

Constants

CLASS_FINALIZER

The Proc object called by the garbage collector to free resources used by LLVM.

Attributes

module[R]

@return [Module]

Public Class Methods

new(mod, &block) click to toggle source

Create a new execution engine.

@param [Module] mod Module to be executed. @param [Proc] block Block used by subclass constructors. Don’t use this parameter.

@raise [RuntimeError] An error is raised if something went horribly wrong inside LLVM during the creation of this engine.

# File lib/rcgtk/execution_engine.rb, line 46
def initialize(mod, &block)
        check_type(mod, Module, 'mod')

        block = Proc.new { |ptr, error| Bindings.create_execution_engine_for_module(ptr, mod, error) } if block == nil

        ptr    = FFI::MemoryPointer.new(:pointer)
        error  = FFI::MemoryPointer.new(:pointer)
        status = block.call(ptr, error)

        if status.zero?
                @ptr    = ptr.read_pointer
                @module = mod

                # Associate this engine with the provided module.
                @module.engine = self

                # Define a finalizer to free the memory used by LLVM for
                # this execution engine.
                ObjectSpace.define_finalizer(self, CLASS_FINALIZER)
        else
                errorp  = error.read_pointer
                message = errorp.null? ? 'Unknown' : errorp.read_string

                error.autorelease = false

                Bindings.dispose_message(error)

                raise "Error creating execution engine: #{message}"
        end
end

Public Instance Methods

pointer_to_global(global) click to toggle source

Builds a pointer to a global value.

@param [GlobalValue] global Value you want a pointer to.

@return [FFI::Pointer]

# File lib/rcgtk/execution_engine.rb, line 82
def pointer_to_global(global)
        Bindings.get_pointer_to_global(@ptr, global)
end
run(fun, *args)
Alias for: run_function
run_function(fun, *args) click to toggle source

Execute a function in the engine’s module with the given arguments. The arguments may be either GnericValue objects or any object that can be turned into a GenericValue.

@param [Function] fun Function object to be executed. @param [Array<GenericValue, Object>] args Arguments to be passed to the function.

@return [GenericValue]

# File lib/rcgtk/execution_engine.rb, line 94
def run_function(fun, *args)
        new_args =
        fun.params.zip(args).map do |param, arg|
                if arg.is_a?(GenericValue) then arg else GenericValue.new(arg) end
        end

        args_ptr = FFI::MemoryPointer.new(:pointer, args.length)
        args_ptr.write_array_of_pointer(new_args)

        GenericValue.new(Bindings.run_function(@ptr, fun, args.length, args_ptr))
end
Also aliased as: run
run_function_as_main(fun, *args) click to toggle source

Execute a function in the engine’s module with the given arguments as the main function of a program.

@param [Function] fun Function object to be executed. @param [Array<String>] args Arguments to be passed to the function.

@return [GenericValue]

# File lib/rcgtk/execution_engine.rb, line 114
def run_function_as_main(fun, *args)
        # Prepare the ARGV parameter.
        argv = FFI::MemoryPointer.new(:pointer, argc)
        argv.write_array_of_pointer(args.map { |str| FFI::MemoryPointer.from_string(str) })

        # Prepare the ENV parameter.
        env = FFI::MemoryPointer.new(:pointer, ENV.size)
        env.write_array_of_pointer(ENV.to_a.map { |pair| FFI::MemoryPointer.from_string(pair[0] + '=' + pair[1]) })

        GenericValue.new(Bindings.run_function_as_main(@ptr, fun, args.length, argv, env))
end
Also aliased as: run_main
run_main(fun, *args)
target_data() click to toggle source

@return [TargetData] Information about the target architecture for this execution engine.

# File lib/rcgtk/execution_engine.rb, line 128
def target_data
        TargetData.new(Bindings.get_execution_engine_target_data(@ptr))
end