Class: LLVM::Module
- Inherits:
-
Object
- Object
- LLVM::Module
- Includes:
- PointerIdentity
- Defined in:
- lib/llvm/linker.rb,
lib/llvm/analysis.rb,
lib/llvm/core/module.rb,
lib/llvm/core/bitcode.rb
Defined Under Namespace
Classes: FunctionCollection, GlobalCollection, TypeCollection
Instance Attribute Summary
Attributes included from PointerIdentity
Class Method Summary collapse
- .from_ptr(ptr) ⇒ Object
-
.parse_bitcode(path_or_memory_buffer) ⇒ LLVM::Module
Parse a module from a memory buffer : (untyped) -> LLVM::Module.
-
.parse_ir(path_or_memory_buffer, context = Context.global) ⇒ Object
: (untyped, ?LLVM::Context) -> LLVM::Module.
Instance Method Summary collapse
-
#clone_module ⇒ Object
: -> LLVM::Module.
-
#data_layout ⇒ String
Get module data layout.
-
#data_layout=(data_layout) ⇒ Object
Set module data layout.
- #dispose ⇒ Object
-
#dump ⇒ Object
Print the module’s IR to the standard error.
-
#functions ⇒ Object
Returns a FunctionCollection of all the Functions in the module.
-
#globals ⇒ Object
Returns an Enumerable of all the GlobalVariables in the module.
-
#initialize(name) ⇒ Module
constructor
Important: Call #dispose to free backend memory after use, but not when using JITCompiler with this module.
-
#inspect ⇒ Object
: -> String.
-
#link_into(other) ⇒ nil, String
Link the current module into
other
. -
#to_s ⇒ Object
Returns the LLVM IR of the module as a string.
-
#triple ⇒ String
Get module triple.
-
#triple=(triple) ⇒ Object
Set module triple.
-
#types ⇒ Object
Returns a TypeCollection of all the Types in the module.
-
#valid? ⇒ Boolean
: -> bool.
-
#verify ⇒ nil, String
Verify that the module is valid.
-
#verify! ⇒ nil
Verify that a module is valid, and abort the process if not.
-
#write_bitcode(path_or_io) ⇒ true, false
Write bitcode to the given path, IO object or file descriptor : (untyped) -> bool.
-
#write_ir!(filename) ⇒ Object
: (String) -> self.
Methods included from PointerIdentity
Constructor Details
#initialize(name) ⇒ Module
Important: Call #dispose to free backend memory after use, but not when using JITCompiler with this module.
17 18 19 |
# File 'lib/llvm/core/module.rb', line 17 def initialize(name) @ptr = C.module_create_with_name(name) end |
Class Method Details
.from_ptr(ptr) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/llvm/core/module.rb', line 9 def self.from_ptr(ptr) return if ptr.null? mod = allocate mod.instance_variable_set(:@ptr, ptr) mod end |
.parse_bitcode(path_or_memory_buffer) ⇒ LLVM::Module
Parse a module from a memory buffer : (untyped) -> LLVM::Module
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/llvm/core/bitcode.rb', line 12 def self.parse_bitcode(path_or_memory_buffer) memory_buffer = case path_or_memory_buffer when MemoryBuffer then path_or_memory_buffer else MemoryBuffer.from_file(path_or_memory_buffer) end llvm_module = nil #: LLVM::Module? FFI::MemoryPointer.new(:pointer) do |mod_ref| FFI::MemoryPointer.new(:pointer) do |msg_ref| status = C.parse_bitcode(memory_buffer, mod_ref, msg_ref) raise msg_ref.get_pointer(0).get_string(0) if status != 0 llvm_module = from_ptr(mod_ref.get_pointer(0)) end end llvm_module #: as !nil end |
.parse_ir(path_or_memory_buffer, context = Context.global) ⇒ Object
: (untyped, ?LLVM::Context) -> LLVM::Module
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/llvm/core/bitcode.rb', line 46 def self.parse_ir(path_or_memory_buffer, context = Context.global) memory_buffer = case path_or_memory_buffer when MemoryBuffer then path_or_memory_buffer else MemoryBuffer.from_file(path_or_memory_buffer) end llvm_module = nil #: LLVM::Module? FFI::MemoryPointer.new(:pointer) do |mod_ref| FFI::MemoryPointer.new(:pointer) do |msg_ref| status = C.parse_ir_in_context(context, memory_buffer, mod_ref, msg_ref) raise msg_ref.get_pointer(0).get_string(0) if status llvm_module = from_ptr(mod_ref.get_pointer(0)) end end llvm_module #: as !nil end |
Instance Method Details
#clone_module ⇒ Object
: -> LLVM::Module
29 30 31 |
# File 'lib/llvm/core/module.rb', line 29 def clone_module Module.from_ptr(C.clone_module(self)) end |
#data_layout ⇒ String
Get module data layout.
: -> String
64 65 66 |
# File 'lib/llvm/core/module.rb', line 64 def data_layout C.get_data_layout(self) end |
#data_layout=(data_layout) ⇒ Object
Set module data layout.
: (String) -> void
72 73 74 |
# File 'lib/llvm/core/module.rb', line 72 def data_layout=(data_layout) C.set_data_layout(self, data_layout.to_s) end |
#dispose ⇒ Object
21 22 23 24 25 26 |
# File 'lib/llvm/core/module.rb', line 21 def dispose return if @ptr.nil? C.dispose_module(@ptr) @ptr = nil end |
#dump ⇒ Object
Print the module’s IR to the standard error.
270 271 272 |
# File 'lib/llvm/core/module.rb', line 270 def dump C.dump_module(self) end |
#functions ⇒ Object
Returns a FunctionCollection of all the Functions in the module. : -> FunctionCollection
179 180 181 |
# File 'lib/llvm/core/module.rb', line 179 def functions @functions ||= FunctionCollection.new(self) end |
#globals ⇒ Object
Returns an Enumerable of all the GlobalVariables in the module. : -> GlobalCollection
98 99 100 |
# File 'lib/llvm/core/module.rb', line 98 def globals @globals ||= GlobalCollection.new(self) end |
#inspect ⇒ Object
: -> String
34 35 36 37 38 39 40 41 42 |
# File 'lib/llvm/core/module.rb', line 34 def inspect { triple: triple, globals: globals.count, functions: functions.count, lines: to_s.lines.size, valid: valid?, }.to_s end |
#link_into(other) ⇒ nil, String
Link the current module into other
.
: (LLVM::Module) -> String?
14 15 16 17 18 |
# File 'lib/llvm/linker.rb', line 14 def link_into(other) LLVM. do |msg| C.link_modules2(other, self) end end |
#to_s ⇒ Object
Returns the LLVM IR of the module as a string.
265 266 267 |
# File 'lib/llvm/core/module.rb', line 265 def to_s C.print_module_to_string(self) end |
#triple ⇒ String
Get module triple.
: -> String
48 49 50 |
# File 'lib/llvm/core/module.rb', line 48 def triple C.get_target(self) end |
#triple=(triple) ⇒ Object
Set module triple.
: (String) -> void
56 57 58 |
# File 'lib/llvm/core/module.rb', line 56 def triple=(triple) C.set_target(self, triple.to_s) end |
#types ⇒ Object
Returns a TypeCollection of all the Types in the module. : -> TypeCollection
78 79 80 |
# File 'lib/llvm/core/module.rb', line 78 def types @types ||= TypeCollection.new(self) end |
#valid? ⇒ Boolean
: -> bool
29 30 31 |
# File 'lib/llvm/analysis.rb', line 29 def valid? verify.nil? end |
#verify ⇒ nil, String
Verify that the module is valid. : -> String?
15 16 17 |
# File 'lib/llvm/analysis.rb', line 15 def verify do_verification(:return_status) end |
#verify! ⇒ nil
Verify that a module is valid, and abort the process if not. : -> String?
22 23 24 25 26 |
# File 'lib/llvm/analysis.rb', line 22 def verify! # :nocov: do_verification(:abort_process) # :nocov: end |
#write_bitcode(path_or_io) ⇒ true, false
Write bitcode to the given path, IO object or file descriptor : (untyped) -> bool
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/llvm/core/bitcode.rb', line 32 def write_bitcode(path_or_io) status = if path_or_io.respond_to?(:path) C.write_bitcode_to_file(self, path_or_io.path) elsif path_or_io.respond_to?(:fileno) C.write_bitcode_to_fd(self, path_or_io.fileno, 0, 1) elsif path_or_io.kind_of?(Integer) C.write_bitcode_to_fd(self, path_or_io, 0, 1) else C.write_bitcode_to_file(self, path_or_io.to_str) end status.zero? end |
#write_ir!(filename) ⇒ Object
: (String) -> self
63 64 65 66 67 68 69 |
# File 'lib/llvm/core/bitcode.rb', line 63 def write_ir!(filename) FFI::MemoryPointer.new(:pointer) do |msg_ref| status = C.print_module_to_file(self, filename, msg_ref) raise msg_ref.get_pointer(0).get_string(0) if status != 0 end self end |