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

engine[RW]

@!attribute [rw] engine

@return [ExecutionEngine, nil] Execution engine associated with this module.

Public Class Methods

new(overloaded, context = nil, &block) click to toggle source

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
read_bitcode(overloaded, context = nil) click to toggle source

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
read_ir(overloaded, context = Context.global) click to toggle source

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(file_name, emit_type = :object, machine = TargetMachine.host) click to toggle source

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
context() click to toggle source

@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
dump() click to toggle source

Print the LLVM IR representation of this value to standard error. This function is the debugging version of the more general purpose {#print} method.

@see print

@return [void]

# File lib/rcgtk/module.rb, line 135
def dump
        Bindings.dump_module(@ptr)
end
fpm()
function_pass_manager() click to toggle source

@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
Also aliased as: fpm
functions() click to toggle source

@return [FunctionCollection] Proxy object for inspecting this module’s functions.

# File lib/rcgtk/module.rb, line 195
def functions
        @functions ||= FunctionCollection.new(self)
end
Also aliased as: funs
funs()
Alias for: functions
globals() click to toggle source

@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
pass_manager() click to toggle source

@return [PassManager] Pass manager for this module.

# File lib/rcgtk/module.rb, line 168
def pass_manager
        @pass_manager ||= PassManager.new(self)
end
Also aliased as: pm
pm()
Alias for: pass_manager
print(file_name) click to toggle source

Print the LLVM IR representation of this module to a file.

@param [String] file_name Name of file to print to

@return [void]

target() click to toggle source

Get the module’s target triple.

@return [String]

# File lib/rcgtk/module.rb, line 217
def target
        Bindings.get_target(@ptr)
end
target=(triple) click to toggle source

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
to_s() click to toggle source

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() click to toggle source

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!() click to toggle source

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_bitcode(overloaded) click to toggle source

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

do_verification(action) click to toggle source

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