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



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

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:



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

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

#disposeObject



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

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

#function_address(name) ⇒ Object



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

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

#functionsFunctionCollection

Returns a FunctionCollection of all the Functions in the engine.

Returns:



107
108
109
# File 'lib/llvm/execution_engine.rb', line 107

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

#modulesModuleCollection

Returns a ModuleCollection of all the Modules in the engine.

Returns:



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

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

#pointer_to_global(global) ⇒ Object

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



91
92
93
# File 'lib/llvm/execution_engine.rb', line 91

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. : (LLVM::Function, *untyped) -> GenericValue?



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/llvm/execution_engine.rb', line 67

def run_function(fun, *args)
  result = nil #: GenericValue?
  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 = begin
      LLVM::GenericValue.from_ptr(C.send(:run_function, self, fun, args.size, args_ptr))
    rescue ArgumentError
      nil
    end
    new_values.each(&:dispose)
  end
  result
end

#target_machineTargetMachine

Get the associated target machine.

Returns:



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

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

#to_ptrObject



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

def to_ptr
  @ptr
end