Class: LLVM::JITCompiler

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

Instance Method Summary collapse

Constructor Details

#initialize(mod, opt_level = 3) ⇒ JITCompiler

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/llvm/execution_engine.rb', line 10

def initialize(mod, opt_level = 3)
  FFI::MemoryPointer.new(FFI.type_size(:pointer)) do |ptr|
    error   = FFI::MemoryPointer.new(FFI.type_size(:pointer))
    status  = C.create_jit_compiler_for_module(ptr, mod, opt_level, error)
    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:



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

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

#disposeObject



27
28
29
30
31
# File 'lib/llvm/execution_engine.rb', line 27

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

#pointer_to_global(global) ⇒ Object

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



69
70
71
# File 'lib/llvm/execution_engine.rb', line 69

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.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/llvm/execution_engine.rb', line 49

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

#to_ptrObject



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

def to_ptr
  @ptr
end