ruby-llvm 2.9.1 → 2.9.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = LLVM
2
2
 
3
3
  Author:: Jeremy Voorhis
4
- Contributors:: Evan Phoenix, David Holroyd, Takanori Ishikawa, Ronaldo M. Ferraz, Mac Malone
4
+ Contributors:: Evan Phoenix, David Holroyd, Takanori Ishikawa, Ronaldo M. Ferraz, Mac Malone, Chris Wailes, Ary Borenszweig
5
5
  Copyright:: Copyright (c) 2010-2011 Jeremy Voorhis
6
6
  License:: BSD 3-clause (see LICENSE)
7
7
 
@@ -13,7 +13,8 @@ implementations.
13
13
  ruby-llvm has been tested on Mac OS X 10.6 using the following Ruby interpreters:
14
14
 
15
15
  * MRI 1.8.7-p174
16
- * MRI 1.9.2-p180
16
+ * MRI 1.9.2-p290
17
+ * MRI 1.9.3-preview1
17
18
  * JRuby 1.4.0
18
19
 
19
20
  If using MRI, ffi >= 1.0.7 is recommended (only ffi >= 1.0.0 is required).
@@ -0,0 +1,18 @@
1
+ require 'rake/clean'
2
+ require 'ffi'
3
+
4
+ CC = "g++"
5
+ LLVM_CONFIG = `llvm-config --cxxflags --ldflags --libs all`.gsub("\n"," ")
6
+ OUTPUT = FFI.map_library_name "RubyLLVMSupport-2.9.2"
7
+ OUTPUT_DIR = "../../lib"
8
+ SRC = "support.cpp"
9
+ CLOBBER.include(OUTPUT)
10
+
11
+ task :default => [:build]
12
+
13
+ desc "Build the shared library"
14
+ task :build => [OUTPUT]
15
+
16
+ file OUTPUT => [SRC] do
17
+ sh "#{CC} -shared #{SRC} #{LLVM_CONFIG} -o #{OUTPUT_DIR}/#{OUTPUT}"
18
+ end
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Extended bindings for LLVM.
3
+ */
4
+
5
+ #include <llvm/Support/DynamicLibrary.h>
6
+
7
+ extern "C" {
8
+ int LLVMLoadLibraryPermanently(const char* filename) {
9
+ return llvm::sys::DynamicLibrary::LoadLibraryPermanently(filename);
10
+ }
11
+ }
12
+
data/lib/llvm/analysis.rb CHANGED
@@ -16,10 +16,15 @@ module LLVM
16
16
  end
17
17
 
18
18
  class Module
19
+ # Verify that the module is valid.
20
+ # @return [nil, String] human-readable description of any invalid
21
+ # constructs if invalid.
19
22
  def verify
20
23
  do_verification(:return_status)
21
24
  end
22
25
 
26
+ # Verify that a module is valid, and abort the process if not.
27
+ # @return [nil]
23
28
  def verify!
24
29
  do_verification(:abort_process)
25
30
  end
@@ -29,14 +34,28 @@ module LLVM
29
34
  str = FFI::MemoryPointer.new(FFI.type_size(:pointer))
30
35
  status = C.LLVMVerifyModule(self, action, str)
31
36
  case status
32
- when 1 then str.read_string
33
- else nil
37
+ when 1 then str.read_string
38
+ else nil
34
39
  end
35
40
  end
36
41
  end
37
42
 
38
43
  class Function
39
- def verify(action = :abort_process)
44
+ # Verify that a function is valid.
45
+ # @return [true, false]
46
+ def verify
47
+ do_verification(:return_status)
48
+ end
49
+
50
+ # Verify that a function is valid, and abort the process if not.
51
+ # @return [true, false]
52
+ def verify!
53
+ do_verification(:abort_process)
54
+ end
55
+
56
+ private
57
+
58
+ def do_verification(action)
40
59
  C.LLVMVerifyFunction(self, action) != 0
41
60
  end
42
61
  end
@@ -0,0 +1,88 @@
1
+ module LLVM
2
+ # @private
3
+ module C
4
+ attach_function :LLVMParseBitcode, [:pointer, :buffer_out, :buffer_out], :int
5
+ attach_function :LLVMParseBitcodeInContext, [:pointer, :pointer, :buffer_out, :buffer_out], :int
6
+ attach_function :LLVMWriteBitcodeToFile, [:pointer, :string], :int
7
+ attach_function :LLVMWriteBitcodeToFD, [:pointer, :int, :int, :int], :int
8
+ end
9
+
10
+ class Module
11
+ # Parse a module from a memory buffer
12
+ # @param [String, LLVM::MemoryBuffer] path_or_memory_buffer
13
+ # @return [LLVM::Module]
14
+ def self.parse_bitcode(path_or_memory_buffer)
15
+ memory_buffer = case path_or_memory_buffer
16
+ when MemoryBuffer then path_or_memory_buffer
17
+ else MemoryBuffer.from_file(path_or_memory_buffer)
18
+ end
19
+ FFI::MemoryPointer.new(:pointer) do |mod_ref|
20
+ FFI::MemoryPointer.new(:pointer) do |msg_ref|
21
+ status = C.LLVMParseBitcode(memory_buffer, mod_ref, msg_ref)
22
+ raise msg_ref.get_pointer(0).get_string(0) if status != 0
23
+ return from_ptr(mod_ref.get_pointer(0))
24
+ end
25
+ end
26
+ end
27
+
28
+ # Write bitcode to the given path, IO object or file descriptor
29
+ # @param [String, IO, Integer] path_or_io Pathname, IO object or file descriptor
30
+ # @return [true, false] Success
31
+ def write_bitcode(path_or_io)
32
+ status = if path_or_io.respond_to?(:path)
33
+ C.LLVMWriteBitcodeToFile(self, path_or_io.path)
34
+ elsif path_or_io.respond_to?(:fileno)
35
+ C.LLVMWriteBitcodeToFD(self, path_or_io.fileno, 0, 1)
36
+ elsif path_or_io.kind_of?(Integer)
37
+ C.LLVMWriteBitcodeToFD(self, path_or_io, 0, 1)
38
+ else
39
+ C.LLVMWriteBitcodeToFile(self, path_or_io.to_str)
40
+ end
41
+ return status == 0
42
+ end
43
+ end
44
+
45
+ # @private
46
+ class MemoryBuffer
47
+ private_class_method :new
48
+
49
+ # @private
50
+ def initialize(ptr)
51
+ @ptr = ptr
52
+ end
53
+
54
+ # @private
55
+ def to_ptr
56
+ @ptr
57
+ end
58
+
59
+ # Read the contents of a file into a memory buffer
60
+ # @param [String] path
61
+ # @return [LLVM::MemoryBuffer]
62
+ def self.from_file(path)
63
+ FFI::MemoryPointer.new(:pointer) do |buf_ref|
64
+ FFI::MemoryPointer.new(:pointer) do |msg_ref|
65
+ status = C.LLVMCreateMemoryBufferWithContentsOfFile(path.to_str, buf_ref, msg_ref)
66
+ raise msg_ref.get_pointer(0).get_string(0) if status != 0
67
+ return new(buf_ref.get_pointer(0))
68
+ end
69
+ end
70
+ end
71
+
72
+ # Read STDIN into a memory buffer
73
+ # @return [LLVM::MemoryBuffer]
74
+ def self.from_stdin
75
+ FFI::Buffer.new(:pointer) do |buf_ref|
76
+ FFI::Buffer.new(:pointer) do |msg_ref|
77
+ status = C.LLVMCreateMemoryBufferWithSTDIN(buf_ref, msg_ref)
78
+ raise msg_ref.get_pointer(0).get_string(0) if status != 0
79
+ return new(buf_ref.get_pointer(0))
80
+ end
81
+ end
82
+ end
83
+
84
+ def dispose
85
+ C.LLVMDisposeMemoryBuffer(@ptr)
86
+ end
87
+ end
88
+ end