Class: LLVM::Module

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PointerIdentity

#==, #eql?, #hash, #to_ptr

Constructor Details

#initialize(name) ⇒ Module

Important: Call #dispose to free backend memory after use, but not when using JITCompiler with this module.



14
15
16
# File 'lib/llvm/core/module.rb', line 14

def initialize(name)
  @ptr = C.module_create_with_name(name)
end

Class Method Details

.from_ptr(ptr) ⇒ Object



6
7
8
9
10
11
# File 'lib/llvm/core/module.rb', line 6

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

Parameters:

Returns:



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/llvm/core/bitcode.rb', line 8

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
  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
      return from_ptr(mod_ref.get_pointer(0))
    end
  end
end

Instance Method Details

#data_layoutString

Get module data layout.

Returns:

  • (String)


42
43
44
# File 'lib/llvm/core/module.rb', line 42

def data_layout
  C.get_data_layout(self)
end

#data_layout=(data_layout) ⇒ Object

Set module data layout.

Parameters:



49
50
51
# File 'lib/llvm/core/module.rb', line 49

def data_layout=(data_layout)
  C.set_data_layout(self, data_layout.to_s)
end

#disposeObject



18
19
20
21
22
23
# File 'lib/llvm/core/module.rb', line 18

def dispose
  return if @ptr.nil?

  C.dispose_module(@ptr)
  @ptr = nil
end

#dump(fd_or_io = nil) ⇒ Object

Print the module’s IR to the given IO object or integer file descriptor.

The IO object#fileno must not be nil. If fd_or_io is nil or omitted, dump the module to the output stream for debugging (stderr).



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/llvm/core/module.rb', line 233

def dump(fd_or_io=nil)
  if fd_or_io.nil?
    C.dump_module(self)
  else
    fd = if fd_or_io.kind_of? ::Integer
      fd_or_io
    elsif fd_or_io.respond_to? :fileno
      fd_or_io.fileno
    end

    raise ArgumentError, 'Expected IO object or Integer file descriptor' if fd.nil?
    Support::C.print_module(
      self,
      fd,
      0,  # should_close=false : not be closed automatically
      0   # unbuffered=false   : buffered internally
    )
  end
end

#functionsObject

Returns a FunctionCollection of all the Functions in the module.



146
147
148
# File 'lib/llvm/core/module.rb', line 146

def functions
  @functions ||= FunctionCollection.new(self)
end

#globalsObject

Returns an Enumerable of all the GlobalVariables in the module.



72
73
74
# File 'lib/llvm/core/module.rb', line 72

def globals
  @globals ||= GlobalCollection.new(self)
end

Link the current module into other.

Returns:

  • (nil, String)

    human-readable error if linking has failed



10
11
12
13
14
15
16
17
18
# File 'lib/llvm/linker.rb', line 10

def link_into(other)
  LLVM.with_message_output do |msg|
    # HACK ALERT: ffi-gen missed LLVMLinkerPreserveSource enumeration for
    # some reason. It is inlined as a constant here.

    # C.link_modules(mod, self, :linker_preserve_source, msg)
    C.link_modules(other, self, 1, msg)
  end
end

Link the current module into other, and dispose the current module.

Returns:

  • (nil, String)

    human-readable error if linking has failed



23
24
25
26
27
28
29
30
31
# File 'lib/llvm/linker.rb', line 23

def link_into_and_destroy(other)
  result = LLVM.with_message_output do |msg|
    C.link_modules(other, self, :linker_destroy_source, msg)
  end

  @ptr = nil

  result
end

#tripleString

Get module triple.

Returns:

  • (String)


28
29
30
# File 'lib/llvm/core/module.rb', line 28

def triple
  C.get_target(self)
end

#triple=(triple) ⇒ Object

Set module triple.

Parameters:

  • triple (String)


35
36
37
# File 'lib/llvm/core/module.rb', line 35

def triple=(triple)
  C.set_target(self, triple.to_s)
end

#typesObject

Returns a TypeCollection of all the Types in the module.



54
55
56
# File 'lib/llvm/core/module.rb', line 54

def types
  @types ||= TypeCollection.new(self)
end

#verifynil, String

Verify that the module is valid.

Returns:

  • (nil, String)

    human-readable description of any invalid constructs if invalid.



11
12
13
# File 'lib/llvm/analysis.rb', line 11

def verify
  do_verification(:return_status)
end

#verify!nil

Verify that a module is valid, and abort the process if not.

Returns:

  • (nil)


17
18
19
# File 'lib/llvm/analysis.rb', line 17

def verify!
  do_verification(:abort_process)
end

#write_bitcode(path_or_io) ⇒ true, false

Write bitcode to the given path, IO object or file descriptor

Parameters:

  • path_or_io (String, IO, Integer)

    Pathname, IO object or file descriptor

Returns:

  • (true, false)

    Success



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/llvm/core/bitcode.rb', line 25

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
  return status == 0
end