Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed some scoping issues.
Finished moving to using ffi_gen (I think).
  • Loading branch information
chriswailes committed Mar 20, 2012
1 parent bf5331b commit da570b3
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 260 deletions.
154 changes: 20 additions & 134 deletions Rakefile
Expand Up @@ -49,6 +49,25 @@ task :gen_bindings do
'LLVMInitializeNativeTarget'
]

deprecated = [
# BitReader.h
'LLVMGetBitcodeModuleProviderInContext',
'LLVMGetBitcodeModuleProvider',

# BitWriter.h
'LLVMWriteBitcodeToFileHandle',

# Core.h
'LLVMCreateFunctionPassManager',

# ExectionEngine.h
'LLVMCreateExecutionEngine',
'LLVMCreateInterpreter',
'LLVMCreateJITCompiler',
'LLVMAddModuleProvider',
'LLVMRemoveModuleProvider'
]

headers = [
'llvm-c/Core.h',

Expand All @@ -71,7 +90,7 @@ task :gen_bindings do
:headers => headers,
:cflags => `llvm-config --cflags`.split,
:prefixes => ['LLVM'],
:blacklist => blacklist,
:blacklist => blacklist + deprecated,
:output => 'lib/rltk/cg/generated_bindings.rb'
)

Expand All @@ -96,136 +115,3 @@ task :gen_bindings do
end
end

desc 'Check the current bindings in RLTK against the LLVM and LLVM-ECB shared libraries'
task :check_bindings, :verbose do |_, args|
(args = args.to_hash)[:verbose] = (args[:verbose] == 'true')

# Require the file that contains all of the bindings.
require 'lib/rltk/cg/bindings'

# Check for objdump.
if not (bin = `which objdump`.chomp)[0,1] == '/'
warn 'objdump binary not found.'
return
end

# Locate the LLVM shared library.
lib_path = nil

paths = ENV['LD_LIBRARY_PATH'].split(/:/).uniq
paths << '/usr/lib/'
paths << '/usr/lib64/'
paths << '/usr/local/lib'
paths << '/usr/local/lib64'

paths.each do |path|
test_path = File.join(path, "libLLVM-#{RLTK::LLVM_TARGET_VERSION}.so")
lib_path = test_path if File.exists?(test_path)
end

if not lib_path
warn "libLLVM-#{RLTK::LLVM_TARGET_VERSION}.so not found."
next
end

# Grab libLLVM symbols.
lines = `#{bin} -t #{lib_path}`

libsyms = lines.map do |l|
md = l.match(/\s(LLVM\w+)/)
if md then md[1] else nil end
end.compact.uniq

# Grab libLLVM-ECB symbols.
lib_path = nil

paths.each do |path|
test_path = File.join(path, "libLLVM-ECB-#{RLTK::LLVM_TARGET_VERSION}.so")
lib_path = test_path if File.exists?(test_path)
end

if lib_path
lines = `#{bin} -t #{lib_path}`

ecbsyms = lines.map do |l|
md = l.match(/\s(LLVM\w+)/)
if md then md[1] else nil end
end.compact.uniq

else
ecbsyms = nil

warn 'LLVM Extended C Bindings shared library not present.'
end

# Defined symbols.
defsyms = Symbol.all_symbols.map do |sym|
sym = sym.to_s
if sym.match(/^LLVM[a-zA-Z]+/) then sym else nil end
end.compact

# Generate info for the default LLVM C bindings.
bound = Array.new
unbound = Array.new

libsyms.each do |sym|
if defsyms.include?(sym) then bound else unbound end << sym
end

# Print information about the default LLVM C bindings.
puts "Default LLVM C Bindings:"
puts "Bound Functions: #{bound.length}"
puts "Unbound Functions: #{unbound.length}"
puts "Completeness: #{((bound.length / libsyms.length.to_f) * 100).to_i}%"
puts

if args[:verbose]
puts 'Unbound function names:'

unbound.sort.each {|sym| puts "\t#{sym}"}
puts
end

if ecbsyms
# Generate info for the extended LLVM C bindings.
bound = Array.new
unbound = Array.new

ecbsyms.each do |sym|
if defsyms.include?(sym) then bound else unbound end << sym
end

# Print information about the extended LLVM C bindings.
puts "Extended LLVM C Bindings:"
puts "Bound Functions: #{bound.length}"
puts "Unbound Functions: #{unbound.length}"
puts "Completeness: #{((bound.length / ecbsyms.length.to_f) * 100).to_i}%"
puts

if args[:verbose]
puts 'Unbound function names:'

unbound.sort.each {|sym| puts "\t#{sym}"}
puts
end

libsyms |= ecbsyms
end

# Print information about bad bindings.
unbinds = Array.new
defsyms.each do |sym|
if not libsyms.include?(sym) then unbinds << sym end
end

puts "Bad Bindings: #{unbinds.length}"
puts

if args[:verbose]
puts 'Bad binding names:'

unbinds.sort.each {|sym| puts "\t#{sym}"}
puts
end
end

26 changes: 25 additions & 1 deletion lib/rltk/cg/bindings.rb
Expand Up @@ -7,7 +7,7 @@
# Requires #
############

# Ruby Gems
# Gems
require 'rubygems'
require 'ffi'

Expand All @@ -20,6 +20,9 @@
#######################

module RLTK::CG::Bindings
extend FFI::Library
ffi_lib("LLVM-#{RLTK::LLVM_TARGET_VERSION}")

class LibraryMismatch < Exception; end

# Require the generated bindings files while handeling errors.
Expand Down Expand Up @@ -70,4 +73,25 @@ class LibraryMismatch < Exception; end
def self.ecb?
@ecb
end

def self.get_bname(name)
name.to_s.
gsub(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
downcase.to_sym
end

def self.add_binding(func, args, returns)
attach_function(get_bname(func.to_s[4..-1]), func, args, returns)
end

####################
# Missing Bindings #
####################

ARCHS.each do |arch|
add_binding("LLVMInitialize#{arch}Target", [], :void)
add_binding("LLVMInitialize#{arch}TargetInfo", [], :void)
add_binding("LLVMInitialize#{arch}TargetMC", [], :void)
end
end
14 changes: 4 additions & 10 deletions lib/rltk/cg/execution_engine.rb
Expand Up @@ -17,10 +17,8 @@

module RLTK::CG
class ExecutionEngine
extend RLTK::CG::Bindings::ExecutionEngine

def initialize(mod, &block)
block = Proc.new { create_execution_engine_for_module(ptr, mod, error) } if block == nil
block = Proc.new { Bindings.create_execution_engine_for_module(ptr, mod, error) } if block == nil

ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer))
error = FFI::MemoryPointer.new(FFI.type_size(:pointer))
Expand All @@ -36,29 +34,25 @@ def initialize(mod, &block)

error.autorelease = false

dispose_message(error)
Bindings.dispose_message(error)

raise "Error creating execution engine: #{message}"
end
end
end

class Interpreter < ExecutionEngine
extend RLTK::CG::Bindings::Interpreter

def initialize(mod)
super do |ptr, error|
create_interpreter_for_module(ptr, mod, error)
Bindings.create_interpreter_for_module(ptr, mod, error)
end
end
end

class JITCompiler < ExecutionEngine
extend RLTK::CG::Bindings::JITCompiler

def initialize(mod, opt_level = 3)
super do |ptr, error|
create_jit_compiler_for_module(ptr, mod, opt_level, error)
Bindings.create_jit_compiler_for_module(ptr, mod, opt_level, error)
end
end
end
Expand Down
90 changes: 0 additions & 90 deletions lib/rltk/cg/generated_bindings.rb
Expand Up @@ -4641,14 +4641,6 @@ class OpaqueUse < FFI::Struct
# @scope class
attach_function :create_function_pass_manager_for_module, :LLVMCreateFunctionPassManagerForModule, [OpaqueModule], OpaquePassManager

# Deprecated: Use LLVMCreateFunctionPassManagerForModule instead.
#
# @method create_function_pass_manager(mp)
# @param [OpaqueModuleProvider] mp
# @return [OpaquePassManager]
# @scope class
attach_function :create_function_pass_manager, :LLVMCreateFunctionPassManager, [OpaqueModuleProvider], OpaquePassManager

# Initializes, executes on the provided module, and finalizes all of the
# passes scheduled in the pass manager. Returns 1 if any of the passes
# modified the module, 0 otherwise. See llvm::PassManager::run(Module&).
Expand Down Expand Up @@ -4806,27 +4798,6 @@ class OpaqueUse < FFI::Struct
# @scope class
attach_function :get_bitcode_module, :LLVMGetBitcodeModule, [OpaqueMemoryBuffer, :pointer, :pointer], :int

# Deprecated: Use LLVMGetBitcodeModuleInContext instead.
#
# @method get_bitcode_module_provider_in_context(context_ref, mem_buf, out_mp, out_message)
# @param [OpaqueContext] context_ref
# @param [OpaqueMemoryBuffer] mem_buf
# @param [FFI::Pointer(*ModuleProviderRef)] out_mp
# @param [FFI::Pointer(**Char_S)] out_message
# @return [Integer]
# @scope class
attach_function :get_bitcode_module_provider_in_context, :LLVMGetBitcodeModuleProviderInContext, [OpaqueContext, OpaqueMemoryBuffer, :pointer, :pointer], :int

# Deprecated: Use LLVMGetBitcodeModule instead.
#
# @method get_bitcode_module_provider(mem_buf, out_mp, out_message)
# @param [OpaqueMemoryBuffer] mem_buf
# @param [FFI::Pointer(*ModuleProviderRef)] out_mp
# @param [FFI::Pointer(**Char_S)] out_message
# @return [Integer]
# @scope class
attach_function :get_bitcode_module_provider, :LLVMGetBitcodeModuleProvider, [OpaqueMemoryBuffer, :pointer, :pointer], :int

# (Not documented)
#
# @method write_bitcode_to_file(m, path)
Expand All @@ -4847,16 +4818,6 @@ class OpaqueUse < FFI::Struct
# @scope class
attach_function :write_bitcode_to_fd, :LLVMWriteBitcodeToFD, [OpaqueModule, :int, :int, :int], :int

# Deprecated for LLVMWriteBitcodeToFD. Writes a module to an open file
# descriptor. Returns 0 on success. Closes the Handle.
#
# @method write_bitcode_to_file_handle(m, handle)
# @param [OpaqueModule] m
# @param [Integer] handle
# @return [Integer]
# @scope class
attach_function :write_bitcode_to_file_handle, :LLVMWriteBitcodeToFileHandle, [OpaqueModule, :int], :int

# The type for the operand information call back function. This is called to
# get the symbolic information for an operand of an instruction. Typically
# this is from the relocation information, symbol table, etc. That block of
Expand Down Expand Up @@ -5323,37 +5284,6 @@ class OpaqueExecutionEngine < FFI::Struct
# @scope class
attach_function :create_jit_compiler_for_module, :LLVMCreateJITCompilerForModule, [:pointer, OpaqueModule, :uint, :pointer], :int

# Deprecated: Use LLVMCreateExecutionEngineForModule instead.
#
# @method create_execution_engine(out_ee, mp, out_error)
# @param [FFI::Pointer(*ExecutionEngineRef)] out_ee
# @param [OpaqueModuleProvider] mp
# @param [FFI::Pointer(**Char_S)] out_error
# @return [Integer]
# @scope class
attach_function :create_execution_engine, :LLVMCreateExecutionEngine, [:pointer, OpaqueModuleProvider, :pointer], :int

# Deprecated: Use LLVMCreateInterpreterForModule instead.
#
# @method create_interpreter(out_interp, mp, out_error)
# @param [FFI::Pointer(*ExecutionEngineRef)] out_interp
# @param [OpaqueModuleProvider] mp
# @param [FFI::Pointer(**Char_S)] out_error
# @return [Integer]
# @scope class
attach_function :create_interpreter, :LLVMCreateInterpreter, [:pointer, OpaqueModuleProvider, :pointer], :int

# Deprecated: Use LLVMCreateJITCompilerForModule instead.
#
# @method create_jit_compiler(out_jit, mp, opt_level, out_error)
# @param [FFI::Pointer(*ExecutionEngineRef)] out_jit
# @param [OpaqueModuleProvider] mp
# @param [Integer] opt_level
# @param [FFI::Pointer(**Char_S)] out_error
# @return [Integer]
# @scope class
attach_function :create_jit_compiler, :LLVMCreateJITCompiler, [:pointer, OpaqueModuleProvider, :uint, :pointer], :int

# (Not documented)
#
# @method dispose_execution_engine(ee)
Expand Down Expand Up @@ -5419,15 +5349,6 @@ class OpaqueExecutionEngine < FFI::Struct
# @scope class
attach_function :add_module, :LLVMAddModule, [OpaqueExecutionEngine, OpaqueModule], :void

# Deprecated: Use LLVMAddModule instead.
#
# @method add_module_provider(ee, mp)
# @param [OpaqueExecutionEngine] ee
# @param [OpaqueModuleProvider] mp
# @return [nil]
# @scope class
attach_function :add_module_provider, :LLVMAddModuleProvider, [OpaqueExecutionEngine, OpaqueModuleProvider], :void

# (Not documented)
#
# @method remove_module(ee, m, out_mod, out_error)
Expand All @@ -5439,17 +5360,6 @@ class OpaqueExecutionEngine < FFI::Struct
# @scope class
attach_function :remove_module, :LLVMRemoveModule, [OpaqueExecutionEngine, OpaqueModule, :pointer, :pointer], :int

# Deprecated: Use LLVMRemoveModule instead.
#
# @method remove_module_provider(ee, mp, out_mod, out_error)
# @param [OpaqueExecutionEngine] ee
# @param [OpaqueModuleProvider] mp
# @param [FFI::Pointer(*ModuleRef)] out_mod
# @param [FFI::Pointer(**Char_S)] out_error
# @return [Integer]
# @scope class
attach_function :remove_module_provider, :LLVMRemoveModuleProvider, [OpaqueExecutionEngine, OpaqueModuleProvider, :pointer, :pointer], :int

# (Not documented)
#
# @method find_function(ee, name, out_fn)
Expand Down

0 comments on commit da570b3

Please sign in to comment.