Class: LLVM::ExecutionEngine

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ ExecutionEngine

Returns a new instance of ExecutionEngine.



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

def initialize(ptr)
  @ptr = ptr
end

Class Method Details

.create_jit_compiler(mod, opt_level = 3) ⇒ Object

Create a JIT compiler with an LLVM::Module.



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

def self.create_jit_compiler(mod, opt_level = 3)
  FFI::MemoryPointer.new(FFI.type_size(:pointer)) do |ptr|
    error   = FFI::MemoryPointer.new(FFI.type_size(:pointer))
    status  = C.LLVMCreateJITCompilerForModule(ptr, mod, opt_level, error)
    errorp  = error.read_pointer
    message = errorp.read_string unless errorp.null?

    if status.zero?
      return new(ptr.read_pointer)
    else
      C.LLVMDisposeMessage(error)
      error.autorelease=false
      raise RuntimeError, "Error creating JIT compiler: #{message}"
    end
  end
end

Instance Method Details

#pointer_to_global(global) ⇒ Object

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



97
98
99
# File 'lib/llvm/execution_engine.rb', line 97

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

#run_function(fun, *args) ⇒ Object

Execute the given LLVM::Function with the supplied args (as GenericValues).



88
89
90
91
92
93
94
# File 'lib/llvm/execution_engine.rb', line 88

def run_function(fun, *args)
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * args.size) do |args_ptr|
    args_ptr.write_array_of_pointer(args.map { |arg| LLVM.GenericValue(arg).to_ptr })
    return LLVM::GenericValue.from_ptr(
      C.LLVMRunFunction(self, fun, args.size, args_ptr))
  end
end

#to_ptrObject



64
65
66
# File 'lib/llvm/execution_engine.rb', line 64

def to_ptr
  @ptr
end