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


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

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 RuntimeError, "Error creating JIT compiler: #{message}"
    end
  end
end

Instance Method Details

#data_layoutTargetDataLayout

Get the associated data layout.

Returns:


48
49
50
# File 'lib/llvm/execution_engine.rb', line 48

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

#disposeObject


34
35
36
37
38
# File 'lib/llvm/execution_engine.rb', line 34

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

#functionsFunctionCollection

Returns a FunctionCollection of all the Functions in the engine.

Returns:


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

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

#modulesModuleCollection

Returns a ModuleCollection of all the Modules in the engine.

Returns:


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

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

#pointer_to_global(global) ⇒ Object

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


83
84
85
# File 'lib/llvm/execution_engine.rb', line 83

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.


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

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 { |p, a|
      if a.kind_of?(GenericValue)
        a
      else
        value = LLVM.make_generic_value(p.type, a)
        new_values << value
        value
      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:


55
56
57
# File 'lib/llvm/execution_engine.rb', line 55

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

#to_ptrObject


41
42
43
# File 'lib/llvm/execution_engine.rb', line 41

def to_ptr
  @ptr
end