Class: RCGTK::Module
- Inherits:
-
Object
- Object
- RCGTK::Module
- Includes:
- BindingClass
- Defined in:
- lib/rcgtk/module.rb
Overview
This class represents a collection of functions, constants, and global variables.
Defined Under Namespace
Classes: FunctionCollection, GlobalCollection
Constant Summary collapse
- CLASS_FINALIZER =
The Proc object called by the garbage collector to free resources used by LLVM.
Proc.new { |id| Bindings.dispose_module(ptr) if ptr = ObjectSpace._id2ref(id).ptr }
Instance Attribute Summary collapse
-
#engine ⇒ ExecutionEngine?
Execution engine associated with this module.
Attributes included from BindingClass
Class Method Summary collapse
-
.read_bitcode(overloaded, context = nil) ⇒ Module
Load a module from LLVM bitcode.
-
.read_ir(overloaded, context = Context.global) ⇒ Module
Load a Module form an LLVM IR.
Instance Method Summary collapse
-
#compile(file_name, emit_type = :object, machine = TargetMachine.host) ⇒ void
Compile this module to an assembly or object file.
-
#context ⇒ Context
Context in which this module exists.
-
#dump ⇒ void
Print the LLVM IR representation of this value to standard error.
-
#function_pass_manager ⇒ FunctionPassManager
(also: #fpm)
Function pass manager for this module.
-
#functions ⇒ FunctionCollection
(also: #funs)
Proxy object for inspecting this module’s functions.
-
#globals ⇒ GlobalCollection
Proxy object for inspecting this module’s global values and variables.
-
#initialize(overloaded, context = nil, &block) ⇒ Module
constructor
Create a new LLVM module.
-
#link(other) ⇒ Object
Link another module into this one, taking ownership of it.
-
#pass_manager ⇒ PassManager
(also: #pm)
Pass manager for this module.
-
#print(file_name) ⇒ void
Print the LLVM IR representation of this module to a file.
-
#target ⇒ String
Get the module’s target triple.
-
#target=(triple) ⇒ void
Set the module’s target triple.
-
#to_s ⇒ String
Return a LLVM IR representation of this file as a string.
-
#verify ⇒ nil, String
Verify that the module is valid LLVM IR.
-
#verify! ⇒ nil
Verify that a module is valid LLVM IR and abort the process if it isn’t.
-
#write_bitcode(overloaded) ⇒ Boolean
Write the module as LLVM bitcode to a file.
Methods included from BindingClass
Constructor Details
#initialize(overloaded, context = nil, &block) ⇒ Module
Create a new LLVM module.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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 |
Instance Attribute Details
#engine ⇒ ExecutionEngine?
Returns Execution engine associated with this module.
32 33 34 |
# File 'lib/rcgtk/module.rb', line 32 def engine @engine end |
Class Method Details
.read_bitcode(overloaded, context = nil) ⇒ Module
Load a module from LLVM bitcode.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Module
Load a Module form an LLVM IR.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 |
Instance Method Details
#compile(file_name, emit_type = :object, machine = TargetMachine.host) ⇒ void
This method returns an undefined value.
Compile this module to an assembly or object file.
119 120 121 |
# 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 ⇒ Context
Returns Context in which this module exists.
124 125 126 |
# File 'lib/rcgtk/module.rb', line 124 def context Context.new(Bindings.get_module_context(@ptr)) end |
#dump ⇒ void
This method returns an undefined value.
Print the LLVM IR representation of this value to standard error. This function is the debugging version of the more general purpose #print method.
135 136 137 |
# File 'lib/rcgtk/module.rb', line 135 def dump Bindings.dump_module(@ptr) end |
#function_pass_manager ⇒ FunctionPassManager Also known as: fpm
Returns Function pass manager for this module.
140 141 142 |
# File 'lib/rcgtk/module.rb', line 140 def function_pass_manager @function_pass_manager ||= FunctionPassManager.new(self) end |
#functions ⇒ FunctionCollection Also known as: funs
Returns Proxy object for inspecting this module’s functions.
195 196 197 |
# File 'lib/rcgtk/module.rb', line 195 def functions @functions ||= FunctionCollection.new(self) end |
#globals ⇒ GlobalCollection
Returns Proxy object for inspecting this module’s global values and variables.
201 202 203 |
# File 'lib/rcgtk/module.rb', line 201 def globals @globals ||= GlobalCollection.new(self) end |
#link(other) ⇒ Object
Link another module into this one, taking ownership of it. You may not access the other module again once linking it.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# 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 = errorp.null? ? 'Unknown' : errorp.read_string error.autorelease = false Bindings.(error) raise "Error linking modules: #{}" end end |
#pass_manager ⇒ PassManager Also known as: pm
Returns Pass manager for this module.
168 169 170 |
# File 'lib/rcgtk/module.rb', line 168 def pass_manager @pass_manager ||= PassManager.new(self) end |
#print(file_name) ⇒ void
This method returns an undefined value.
Print the LLVM IR representation of this module to a file.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# 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 = errorp.null? ? 'Unknown' : errorp.read_string error.autorelease = false Bindings.(error) raise "Error printing module: #{}" end end |
#target ⇒ String
Get the module’s target triple.
217 218 219 |
# File 'lib/rcgtk/module.rb', line 217 def target Bindings.get_target(@ptr) end |
#target=(triple) ⇒ void
This method returns an undefined value.
Set the module’s target triple.
210 211 212 |
# File 'lib/rcgtk/module.rb', line 210 def target=(triple) Bindings.set_target(@ptr, triple) end |
#to_s ⇒ String
Return a LLVM IR representation of this file as a string.
224 225 226 |
# File 'lib/rcgtk/module.rb', line 224 def to_s Bindings.print_module_to_string(@ptr) end |
#verify ⇒ nil, String
Verify that the module is valid LLVM IR.
252 253 254 |
# File 'lib/rcgtk/module.rb', line 252 def verify do_verification(:return_status) end |
#verify! ⇒ nil
Verify that a module is valid LLVM IR and abort the process if it isn’t.
259 260 261 |
# File 'lib/rcgtk/module.rb', line 259 def verify! do_verification(:abort_process) end |
#write_bitcode(overloaded) ⇒ Boolean
Write the module as LLVM bitcode to a file.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# 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 |