class RCGTK::Module
This class represents a collection of functions, constants, and global variables.
Constants
- CLASS_FINALIZER
The Proc object called by the garbage collector to free resources used by
LLVM
.
Attributes
@!attribute [rw] engine
@return [ExecutionEngine, nil] Execution engine associated with this module.
Public Class Methods
Create a new LLVM
module.
@param [FFI::Pointer, String] overloaded Pointer to existing module or name of new module. @param [Context, nil] context Optional context in which to create the module. @param [Proc] block Block to be executed inside the context of the module.
# File lib/rcgtk/module.rb, line 86 def initialize(overloaded, context = nil, &block) @ptr = case overloaded when FFI::Pointer overloaded when String if context Bindings.module_create_with_name_in_context(overloaded, check_type(context, Context, 'context')) else Bindings.module_create_with_name(overloaded) end else raise 'Argument `overloaded` must be a FFI::Pointer of String.' end # Define a finalizer to free the memory used by LLVM for this # module. ObjectSpace.define_finalizer(self, CLASS_FINALIZER) self.instance_exec(&block) if block end
Load a module from LLVM
bitcode.
@param [MemoryBuffer, String] overloaded Where to read the bitecode from @param [Context, nil] context Context
in which to parse bitcode
@return [Module]
# File lib/rcgtk/module.rb, line 40 def self.read_bitcode(overloaded, context = nil) buffer = overloaded.is_a?(MemoryBuffer) ? overloaded : MemoryBuffer.new(overloaded) mod_ptr = FFI::MemoryPointer.new(:pointer) msg_ptr = FFI::MemoryPointer.new(:pointer) status = if context Bindings.parse_bitcode_in_context(context, buffer, mod_ptr, msg_ptr) else Bindings.parse_bitcode(buffer, mod_ptr, msg_ptr) end if status.zero? Module.new(mod_ptr.get_pointer(0)) else raise msg_ptr.get_pointer(0).get_string(0) end end
Load a Module
form an LLVM
IR.
@param [MemoryBuffer, String] overloaded Where to read the IR from @param [Context] context Context
in which to parse IR
@return [Module]
# File lib/rcgtk/module.rb, line 66 def self.read_ir(overloaded, context = Context.global) buffer = overloaded.is_a?(MemoryBuffer) ? overloaded : MemoryBuffer.new(overloaded) mod_ptr = FFI::MemoryPointer.new(:pointer) msg_ptr = FFI::MemoryPointer.new(:pointer) status = Bindings.parse_ir_in_context(context, buffer, mod_ptr, msg_ptr) if status.zero? Module.new(mod_ptr.get_pointer(0)) else raise msg_ptr.get_pointer(0).get_string(0) end end
Public Instance Methods
Compile this module to an assembly or object file.
@param [String] file_name File to emit code to @param [:assembly, :object] emit_type Type
of code to emit @param [TargetMachine] machine TargetMachine
used to generate code
@return [void]
@raise LLVM
error message if unable to emit code for module
# File lib/rcgtk/module.rb, line 119 def compile(file_name, emit_type = :object, machine = TargetMachine.host) machine.emite_module(self, file_name, emit_type) end
@return [Context] Context
in which this module exists.
# File lib/rcgtk/module.rb, line 124 def context Context.new(Bindings.get_module_context(@ptr)) end
@return [FunctionPassManager] Function
pass manager for this module.
# File lib/rcgtk/module.rb, line 140 def function_pass_manager @function_pass_manager ||= FunctionPassManager.new(self) end
@return [FunctionCollection] Proxy object for inspecting this module’s functions.
# File lib/rcgtk/module.rb, line 195 def functions @functions ||= FunctionCollection.new(self) end
@return [GlobalCollection] Proxy object for inspecting this module’s global values and variables.
# File lib/rcgtk/module.rb, line 201 def globals @globals ||= GlobalCollection.new(self) end
Link another module into this one, taking ownership of it. You may not access the other module again once linking it.
@param [Module] other Module
to be linked
@raise Errors encountered during linking
# File lib/rcgtk/module.rb, line 151 def link(other) error = FFI::MemoryPointer.new(:pointer) status = Bindings.link_modules(@ptr, other, :linker_destroy_source, error) if not status.zero? errorp = error.read_pointer message = errorp.null? ? 'Unknown' : errorp.read_string error.autorelease = false Bindings.dispose_message(error) raise "Error linking modules: #{message}" end end
@return [PassManager] Pass manager for this module.
# File lib/rcgtk/module.rb, line 168 def pass_manager @pass_manager ||= PassManager.new(self) end
Print the LLVM
IR representation of this module to a file.
@param [String] file_name Name of file to print to
@return [void]
# File lib/rcgtk/module.rb, line 178 def print(file_name) error = FFI::MemoryPointer.new(:pointer) status = Bindings.print_module_to_file(@ptr, file_name, error) if not status.zero? errorp = error.read_pointer message = errorp.null? ? 'Unknown' : errorp.read_string error.autorelease = false Bindings.dispose_message(error) raise "Error printing module: #{message}" end end
Get the module’s target triple.
@return [String]
# File lib/rcgtk/module.rb, line 217 def target Bindings.get_target(@ptr) end
Set the module’s target triple.
@param [String] triple Triple
value to set.
@return [void]
# File lib/rcgtk/module.rb, line 210 def target=(triple) Bindings.set_target(@ptr, triple) end
Return a LLVM
IR representation of this file as a string.
@return [String]
# File lib/rcgtk/module.rb, line 224 def to_s Bindings.print_module_to_string(@ptr) end
Verify that the module is valid LLVM
IR.
@return [nil, String] Human-readable description of any invalid constructs if invalid.
# File lib/rcgtk/module.rb, line 252 def verify do_verification(:return_status) end
Verify that a module is valid LLVM
IR and abort the process if it isn’t.
@return [nil]
# File lib/rcgtk/module.rb, line 259 def verify! do_verification(:abort_process) end
Write the module as LLVM
bitcode to a file.
@param [#path, fileno, Integer, String] overloaded Where to write the bitcode.
@return [Boolean] If the write was successful.
# File lib/rcgtk/module.rb, line 233 def write_bitcode(overloaded) 0 == if overloaded.respond_to?(:path) Bindings.write_bitcode_to_file(@ptr, overloaded.path) elsif overloaded.respond_to?(:fileno) Bindings.write_bitcode_to_fd(@ptr, overloaded.fileno, 0, 1) elsif overloaded.is_a?(Integer) Bindings.write_bitcode_to_fd(@ptr, overloaded, 0, 1) elsif overloaded.is_a?(String) Bindings.write_bitcode_to_file(@ptr, overloaded) end end
Private Instance Methods
Helper function for {#verify} and {#verify!}
# File lib/rcgtk/module.rb, line 264 def do_verification(action) str_ptr = FFI::MemoryPointer.new(:pointer) status = Bindings.verify_module(@ptr, action, str_ptr) status == 1 ? str_ptr.read_string : nil end