Class: LLVM::ExecutionEngine Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/llvm/execution_engine.rb

Overview

This class is abstract.

Subclass and override #create_execution_engine_for_module.

Direct Known Subclasses

MCJITCompiler

Defined Under Namespace

Classes: FunctionCollection, ModuleCollection

Instance Method Summary collapse

Constructor Details

#initialize(mod, options) ⇒ ExecutionEngine

Note:

Important: Call #dispose to free backend memory after use. Do not call #dispose on mod any more.

Create a JIT execution engine for module with the given options.

Parameters:

  • mod (LLVM::Module)

    module

  • options (Hash{Symbol => Object})

    options



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/llvm/execution_engine.rb', line 19

def initialize(mod, options)
  FFI::MemoryPointer.new(FFI.type_size(:pointer)) do |ptr|
    error   = FFI::MemoryPointer.new(FFI.type_size(:pointer))
    status  = create_execution_engine_for_module(ptr, mod, error, options)
    errorp  = error.read_pointer
    message = errorp.read_string unless errorp.null?

    if status.zero?
      @ptr = ptr.read_pointer
    else
      C.dispose_message(error)
      error.autorelease = false
      raise "Error creating JIT compiler: #{message}"
    end
  end
end

Instance Method Details

#data_layoutTargetDataLayout

Get the associated data layout.

Returns:



50
51
52
# File 'lib/llvm/execution_engine.rb', line 50

def data_layout
  TargetDataLayout.from_ptr(C.get_execution_engine_target_data(self))
end

#disposeObject



36
37
38
39
40
# File 'lib/llvm/execution_engine.rb', line 36

def dispose
  return if @ptr.nil?
  C.dispose_execution_engine(@ptr)
  @ptr = nil
end

#function_address(name) ⇒ Object



89
90
91
# File 'lib/llvm/execution_engine.rb', line 89

def function_address(name)
  C.get_function_address(self, name)
end

#functionsFunctionCollection

Returns a FunctionCollection of all the Functions in the engine.

Returns:



101
102
103
# File 'lib/llvm/execution_engine.rb', line 101

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

#modulesModuleCollection

Returns a ModuleCollection of all the Modules in the engine.

Returns:



95
96
97
# File 'lib/llvm/execution_engine.rb', line 95

def modules
  @modules ||= ModuleCollection.new(self)
end

#pointer_to_global(global) ⇒ Object

Obtain an FFI::Pointer to a global within the current module.



85
86
87
# File 'lib/llvm/execution_engine.rb', line 85

def pointer_to_global(global)
  C.get_pointer_to_global(self, global)
end

#run_function(fun, *args) ⇒ Object

Execute the given LLVM::Function with the supplied args (as GenericValues). Important: Call #dispose on the returned GenericValue to free backend memory after use.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/llvm/execution_engine.rb', line 65

def run_function(fun, *args)
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * args.size) do |args_ptr|
    new_values = []
    args_ptr.write_array_of_pointer(fun.params.zip(args).map do |p, a|
      if a.kind_of?(GenericValue)
        a
      else
        value = LLVM.make_generic_value(p.type, a)
        new_values << value
        value
      end
    end)
    result = LLVM::GenericValue.from_ptr(
      C.run_function(self, fun, args.size, args_ptr))
    new_values.each(&:dispose)
    return result
  end
end

#target_machineTargetMachine

Get the associated target machine.

Returns:



57
58
59
# File 'lib/llvm/execution_engine.rb', line 57

def target_machine
  TargetMachine.from_ptr(C.get_execution_engine_target_machine(self))
end

#to_ptrObject



43
44
45
# File 'lib/llvm/execution_engine.rb', line 43

def to_ptr
  @ptr
end