Class: RLTK::CG::ExecutionEngine Abstract
- Includes:
- AbstractClass, BindingClass
- Defined in:
- lib/rltk/cg/execution_engine.rb
Overview
Implemented by Interpreter and JITCompiler.
The ExecutionEngine class and its subclasses execute code from the provided module, as well as providing a PassManager and FunctionPassManager for optimizing modules.
Direct Known Subclasses
Constant Summary collapse
- CLASS_FINALIZER =
The Proc object called by the garbage collector to free resources used by LLVM.
Proc.new { |id| Bindings.dispose_execution_engine(ptr) if ptr = ObjectSpace._id2ref(id).ptr }
Instance Attribute Summary collapse
- #module ⇒ Module readonly
Attributes included from BindingClass
Instance Method Summary collapse
-
#initialize(mod, &block) ⇒ ExecutionEngine
constructor
Create a new execution engine.
-
#pointer_to_global(global) ⇒ FFI::Pointer
Builds a pointer to a global value.
-
#run_function(fun, *args) ⇒ GenericValue
(also: #run)
Execute a function in the engine’s module with the given arguments.
-
#run_function_as_main(fun, *args) ⇒ GenericValue
(also: #run_main)
Execute a function in the engine’s module with the given arguments as the main function of a program.
-
#target_data ⇒ TargetData
Information about the target architecture for this execution engine.
Methods included from BindingClass
Methods included from AbstractClass
Constructor Details
#initialize(mod, &block) ⇒ ExecutionEngine
Create a new execution engine.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rltk/cg/execution_engine.rb', line 44 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 = errorp.null? ? 'Unknown' : errorp.read_string error.autorelease = false Bindings.(error) raise "Error creating execution engine: #{}" end end |
Instance Attribute Details
#module ⇒ Module (readonly)
36 37 38 |
# File 'lib/rltk/cg/execution_engine.rb', line 36 def module @module end |
Instance Method Details
#pointer_to_global(global) ⇒ FFI::Pointer
Builds a pointer to a global value.
80 81 82 |
# File 'lib/rltk/cg/execution_engine.rb', line 80 def pointer_to_global(global) Bindings.get_pointer_to_global(@ptr, global) end |
#run_function(fun, *args) ⇒ GenericValue Also known as: run
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.
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rltk/cg/execution_engine.rb', line 92 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 |
#run_function_as_main(fun, *args) ⇒ GenericValue Also known as: run_main
Execute a function in the engine’s module with the given arguments as the main function of a program.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rltk/cg/execution_engine.rb', line 112 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 |
#target_data ⇒ TargetData
Returns Information about the target architecture for this execution engine.
126 127 128 |
# File 'lib/rltk/cg/execution_engine.rb', line 126 def target_data TargetData.new(Bindings.get_execution_engine_target_data(@ptr)) end |