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/test/ipo_test.rb ADDED
@@ -0,0 +1,53 @@
1
+ require "test_helper"
2
+ require "llvm/core"
3
+ require 'llvm/transforms/ipo'
4
+ require 'llvm/core/pass_manager'
5
+
6
+ class IPOTestCase < Test::Unit::TestCase
7
+
8
+ def setup
9
+ LLVM.init_x86
10
+ end
11
+
12
+ def test_gdce
13
+ mod = LLVM::Module.new('test')
14
+
15
+ fn1 = mod.functions.add("fn1", [], LLVM.Void) do |fn|
16
+ fn.linkage = :internal
17
+ fn.basic_blocks.append.build do |builder|
18
+ builder.ret_void
19
+ end
20
+ end
21
+
22
+ fn2 = mod.functions.add("fn2", [], LLVM.Void) do |fn|
23
+ fn.linkage = :internal
24
+ fn.basic_blocks.append.build do |builder|
25
+ builder.ret_void
26
+ end
27
+ end
28
+
29
+ main = mod.functions.add("main", [], LLVM.Void) do |fn|
30
+ fn.basic_blocks.append.build do |builder|
31
+ builder.call(fn1)
32
+ builder.ret_void
33
+ end
34
+ end
35
+
36
+ fns = mod.functions.to_a
37
+ assert fns.include?(fn1)
38
+ assert fns.include?(fn2)
39
+ assert fns.include?(main)
40
+
41
+ # optimize
42
+ engine = LLVM::JITCompiler.new(mod)
43
+ passm = LLVM::PassManager.new(engine)
44
+
45
+ passm.gdce!
46
+ passm.run(mod)
47
+
48
+ fns = mod.functions.to_a
49
+ assert fns.include?(fn1)
50
+ assert !fns.include?(fn2), 'fn2 should be eliminated'
51
+ assert fns.include?(main)
52
+ end
53
+ end
@@ -0,0 +1,38 @@
1
+ require "test_helper"
2
+
3
+ class MemoryAccessTestCase < Test::Unit::TestCase
4
+
5
+ def setup
6
+ LLVM.init_x86
7
+ end
8
+
9
+ def test_memory_access
10
+ assert_equal 1 + 2, simple_memory_access_function(1, 2).to_i
11
+ assert_equal 3 + 4, array_memory_access_function(3, 4).to_i
12
+ end
13
+
14
+ def simple_memory_access_function(value1, value2)
15
+ run_function([LLVM::Int, LLVM::Int], [value1, value2], LLVM::Int) do |builder, function, *arguments|
16
+ entry = function.basic_blocks.append
17
+ builder.position_at_end(entry)
18
+ pointer1 = builder.alloca(LLVM::Int)
19
+ pointer2 = builder.alloca(LLVM::Int)
20
+ builder.store(arguments.first, pointer1)
21
+ builder.store(arguments.last, pointer2)
22
+ builder.ret(builder.add(builder.load(pointer1), builder.load(pointer2)))
23
+ end
24
+ end
25
+
26
+ def array_memory_access_function(value1, value2)
27
+ run_function([LLVM::Int, LLVM::Int], [value1, value2], LLVM::Int) do |builder, function, *arguments|
28
+ entry = function.basic_blocks.append
29
+ builder.position_at_end(entry)
30
+ pointer = builder.array_alloca(LLVM::Int, LLVM::Int(2))
31
+ builder.store(arguments.first, builder.gep(pointer, [LLVM::Int(0)]))
32
+ builder.store(arguments.last, builder.gep(pointer, [LLVM::Int(1)]))
33
+ builder.ret(builder.add(builder.load(builder.gep(pointer, [LLVM::Int(0)])),
34
+ builder.load(builder.gep(pointer, [LLVM::Int(1)]))))
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,21 @@
1
+ require "test_helper"
2
+
3
+ class ModuleTestCase < Test::Unit::TestCase
4
+
5
+ def setup
6
+ LLVM.init_x86
7
+ end
8
+
9
+ def test_simple_module
10
+ assert_equal 1, simple_function().to_i
11
+ end
12
+
13
+ def simple_function
14
+ run_function([], [], LLVM::Int) do |builder, function, *arguments|
15
+ entry = function.basic_blocks.append
16
+ builder.position_at_end(entry)
17
+ builder.ret(LLVM::Int(1))
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,28 @@
1
+ require "test_helper"
2
+
3
+ class ParameterCollectionTestCase < Test::Unit::TestCase
4
+ def setup
5
+ @mod = LLVM::Module.new('test')
6
+ @fun = @mod.functions.add('fun', [LLVM::Int, LLVM::Int], LLVM::Int)
7
+ @fun.params[0].name = 'foo'
8
+ @fun.params[1].name = 'bar'
9
+ end
10
+
11
+ def test_positive_index_in_range
12
+ assert_equal 'foo', @fun.params[0].name
13
+ assert_equal 'bar', @fun.params[1].name
14
+ end
15
+
16
+ def test_negative_index_in_range
17
+ assert_equal 'foo', @fun.params[-2].name
18
+ assert_equal 'bar', @fun.params[-1].name
19
+ end
20
+
21
+ def test_positive_index_out_of_range
22
+ assert_nil @fun.params[2]
23
+ end
24
+
25
+ def test_negative_index_out_of_range
26
+ assert_nil @fun.params[-3]
27
+ end
28
+ end
data/test/phi_test.rb ADDED
@@ -0,0 +1,33 @@
1
+ require "test_helper"
2
+
3
+ class PhiTest < Test::Unit::TestCase
4
+ def setup
5
+ LLVM.init_x86
6
+ end
7
+
8
+ def test_phi
9
+ assert_equal 1, run_phi_function(0).to_i
10
+ assert_equal 0, run_phi_function(1).to_i
11
+ end
12
+
13
+ def run_phi_function(argument)
14
+ run_function([LLVM::Int], argument, LLVM::Int) do |builder, function, *arguments|
15
+ entry = function.basic_blocks.append
16
+ block1 = function.basic_blocks.append
17
+ block2 = function.basic_blocks.append
18
+ exit = function.basic_blocks.append
19
+ builder.position_at_end(entry)
20
+ builder.cond(builder.icmp(:eq, arguments.first, LLVM::Int(0)), block1, block2)
21
+ builder.position_at_end(block1)
22
+ result1 = builder.add(arguments.first, LLVM::Int(1))
23
+ builder.br(exit)
24
+ builder.position_at_end(block2)
25
+ result2 = builder.sub(arguments.first, LLVM::Int(1))
26
+ builder.br(exit)
27
+ builder.position_at_end(exit)
28
+ builder.ret(builder.phi(LLVM::Int,
29
+ block1 => result1,
30
+ block2 => result2))
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ require "test_helper"
2
+
3
+ class SelectTestCase < Test::Unit::TestCase
4
+
5
+ def setup
6
+ LLVM.init_x86
7
+ end
8
+
9
+ def test_select
10
+ assert_equal 0, select_function(1).to_i
11
+ assert_equal 1, select_function(0).to_i
12
+ end
13
+
14
+ def select_function(value)
15
+ run_function([LLVM::Int1], [value], LLVM::Int) do |builder, function, *arguments|
16
+ entry = function.basic_blocks.append
17
+ builder.position_at_end(entry)
18
+ builder.ret(builder.select(arguments.first, LLVM::Int(0), LLVM::Int(1)))
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,75 @@
1
+ require "test_helper"
2
+
3
+ class StructTestCase < Test::Unit::TestCase
4
+
5
+ LLVM_UNPACKED = false
6
+ LLVM_PACKED = true
7
+
8
+ def setup
9
+ LLVM.init_x86
10
+ end
11
+
12
+ def test_simple_struct
13
+ struct = LLVM::Struct(LLVM::Int, LLVM::Float)
14
+ assert_instance_of LLVM::Type, struct
15
+ end
16
+
17
+ def test_unpacked_constant_struct_from_size
18
+ struct = LLVM::ConstantStruct.const(2, LLVM_UNPACKED) { |i| LLVM::Int(i) }
19
+ assert_instance_of LLVM::ConstantStruct, struct
20
+ assert_equal 2, struct.operands.size
21
+ end
22
+
23
+ def test_unpacked_constant_struct_from_struct
24
+ struct = LLVM::ConstantStruct.const([LLVM::Int(0), LLVM::Int(1)], LLVM_UNPACKED)
25
+ assert_instance_of LLVM::ConstantStruct, struct
26
+ assert_equal 2, struct.operands.size
27
+ end
28
+
29
+ def test_packed_constant_struct_from_size
30
+ struct = LLVM::ConstantStruct.const(2, LLVM_PACKED) { |i| LLVM::Int(i) }
31
+ assert_instance_of LLVM::ConstantStruct, struct
32
+ assert_equal 2, struct.operands.size
33
+ end
34
+
35
+ def test_packed_constant_struct_from_struct
36
+ struct = LLVM::ConstantStruct.const([LLVM::Int(0), LLVM::Int(1)], LLVM_PACKED)
37
+ assert_instance_of LLVM::ConstantStruct, struct
38
+ assert_equal 2, struct.operands.size
39
+ end
40
+
41
+ def test_struct_values
42
+ assert_equal 2 + 3, run_struct_values(2, 3).to_i
43
+ end
44
+
45
+ def test_struct_access
46
+ assert_in_delta 2 + 3.3, run_struct_access(LLVM::Float, 2, 3.3).to_f, 0.001
47
+ end
48
+
49
+ def run_struct_values(value1, value2)
50
+ run_function([LLVM::Int, LLVM::Int], [value1, value2], LLVM::Int) do |builder, function, *arguments|
51
+ entry = function.basic_blocks.append
52
+ builder.position_at_end(entry)
53
+ pointer = builder.alloca(LLVM::Struct(LLVM::Int, LLVM::Int))
54
+ struct = builder.load(pointer)
55
+ struct = builder.insert_value(struct, arguments.first, 0)
56
+ struct = builder.insert_value(struct, arguments.last, 1)
57
+ builder.ret(builder.add(builder.extract_value(struct, 0),
58
+ builder.extract_value(struct, 1)))
59
+ end
60
+ end
61
+
62
+ def run_struct_access(return_type, value1, value2)
63
+ run_function([LLVM::Int, LLVM::Float], [value1, value2], return_type) do |builder, function, *arguments|
64
+ entry = function.basic_blocks.append
65
+ builder.position_at_end(entry)
66
+ pointer = builder.alloca(LLVM::Struct(LLVM::Float, LLVM::Struct(LLVM::Int, LLVM::Float), LLVM::Int))
67
+ builder.store(arguments.first, builder.gep(pointer, [LLVM::Int(0), LLVM::Int32.from_i(1), LLVM::Int32.from_i(0)]))
68
+ builder.store(arguments.last, builder.gep(pointer, [LLVM::Int(0), LLVM::Int32.from_i(1), LLVM::Int32.from_i(1)]))
69
+ address1 = builder.gep(pointer, [LLVM::Int(0), LLVM::Int32.from_i(1), LLVM::Int32.from_i(0)])
70
+ address2 = builder.gep(pointer, [LLVM::Int(0), LLVM::Int32.from_i(1), LLVM::Int32.from_i(1)])
71
+ builder.ret(builder.fadd(builder.ui2fp(builder.load(address1), LLVM::Float), builder.load(address2)))
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,50 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
2
+
3
+ begin
4
+ require "ruby-debug"
5
+ rescue LoadError
6
+ # Ignore ruby-debug is case it's not installed
7
+ end
8
+
9
+ require "test/unit"
10
+
11
+ require "llvm/core"
12
+ require "llvm/execution_engine"
13
+ require "llvm/transforms/scalar"
14
+
15
+ class Test::Unit::TestCase
16
+
17
+ LLVM_SIGNED = true
18
+ LLVM_UNSIGNED = false
19
+
20
+ LLVM_FALSE = 0
21
+ LLVM_TRUE = 1
22
+
23
+ end
24
+
25
+ def define_module(module_name)
26
+ new_module = LLVM::Module.new(module_name)
27
+ yield new_module
28
+ new_module.verify
29
+ new_module
30
+ end
31
+
32
+ def define_function(host_module, function_name, argument_types, return_type)
33
+ host_module.functions.add(function_name, argument_types, return_type) do |function, *arguments|
34
+ yield(LLVM::Builder.new, function, *arguments)
35
+ end
36
+ end
37
+
38
+ def run_function_on_module(host_module, function_name, *argument_values)
39
+ LLVM::JITCompiler.new(host_module).
40
+ run_function(host_module.functions[function_name], *argument_values)
41
+ end
42
+
43
+ def run_function(argument_types, argument_values, return_type, &block)
44
+ test_module = define_module("test_module") do |host_module|
45
+ define_function(host_module, "test_function", argument_types, return_type, &block)
46
+ end
47
+
48
+ run_function_on_module(test_module, "test_function", *argument_values)
49
+ end
50
+
data/test/type_test.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "test_helper"
2
+
3
+ class TypeTestCase < Test::Unit::TestCase
4
+
5
+ def test_element_type
6
+ pointer = LLVM.Pointer(LLVM::Int32.type)
7
+ pointee = pointer.element_type
8
+
9
+ assert_equal :pointer, pointer.kind
10
+ assert_equal :integer, pointee.kind
11
+
12
+ assert_nil pointee.element_type
13
+ end
14
+
15
+ end
@@ -0,0 +1,64 @@
1
+ require "test_helper"
2
+
3
+ class VectorTestCase < Test::Unit::TestCase
4
+
5
+ def setup
6
+ LLVM.init_x86
7
+ end
8
+
9
+ def test_all_ones_vector
10
+ assert_raise(NotImplementedError) do
11
+ LLVM::ConstantVector.all_ones
12
+ end
13
+ end
14
+
15
+ def test_constant_vector_from_size
16
+ vector = LLVM::ConstantVector.const(2) { |i| LLVM::Int(i) }
17
+ assert_instance_of LLVM::ConstantVector, vector
18
+ assert_equal 2, vector.operands.size
19
+ end
20
+
21
+ def test_constant_vector_from_array
22
+ vector = LLVM::ConstantVector.const([LLVM::Int(0), LLVM::Int(1)])
23
+ assert_instance_of LLVM::ConstantVector, vector
24
+ assert_equal 2, vector.operands.size
25
+ end
26
+
27
+ def test_vector_elements
28
+ assert_equal 2 + 3, run_vector_elements(2, 3).to_i
29
+ end
30
+
31
+ def test_vector_shuffle
32
+ assert_equal 1 + 4, run_vector_shuffle(1, 2, 3, 4).to_i
33
+ end
34
+
35
+ def run_vector_elements(value1, value2)
36
+ run_function([LLVM::Int, LLVM::Int], [value1, value2], LLVM::Int) do |builder, function, *arguments|
37
+ entry = function.basic_blocks.append
38
+ builder.position_at_end(entry)
39
+ pointer = builder.alloca(LLVM::Vector(LLVM::Int, 2))
40
+ vector = builder.load(pointer)
41
+ vector = builder.insert_element(vector, arguments.first, LLVM::Int32.from_i(0))
42
+ vector = builder.insert_element(vector, arguments.last, LLVM::Int32.from_i(1))
43
+ builder.ret(builder.add(builder.extract_element(vector, LLVM::Int32.from_i(0)),
44
+ builder.extract_element(vector, LLVM::Int32.from_i(1))))
45
+ end
46
+ end
47
+
48
+ def run_vector_shuffle(*values)
49
+ run_function([LLVM::Int, LLVM::Int, LLVM::Int, LLVM::Int], values, LLVM::Int) do |builder, function, *arguments|
50
+ entry = function.basic_blocks.append
51
+ builder.position_at_end(entry)
52
+ vector1 = builder.load(builder.alloca(LLVM::Vector(LLVM::Int, 2)))
53
+ vector1 = builder.insert_element(vector1, arguments[0], LLVM::Int32.from_i(0))
54
+ vector1 = builder.insert_element(vector1, arguments[1], LLVM::Int32.from_i(1))
55
+ vector2 = builder.load(builder.alloca(LLVM::Vector(LLVM::Int, 2)))
56
+ vector2 = builder.insert_element(vector2, arguments[2], LLVM::Int32.from_i(0))
57
+ vector2 = builder.insert_element(vector2, arguments[3], LLVM::Int32.from_i(1))
58
+ vector3 = builder.shuffle_vector(vector1, vector2, LLVM::ConstantVector.const([LLVM::Int(0), LLVM::Int(3)]))
59
+ builder.ret(builder.add(builder.extract_element(vector3, LLVM::Int32.from_i(0)),
60
+ builder.extract_element(vector3, LLVM::Int32.from_i(1))))
61
+ end
62
+ end
63
+
64
+ end
metadata CHANGED
@@ -1,39 +1,70 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ruby-llvm
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.9.3
4
5
  prerelease:
5
- version: 2.9.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Jeremy Voorhis
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-04-23 00:00:00 -07:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2011-12-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: ffi
18
- prerelease: false
19
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70109058811900 !ruby/object:Gem::Requirement
20
17
  none: false
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
24
21
  version: 1.0.0
25
22
  type: :runtime
26
- version_requirements: *id001
27
- description:
23
+ prerelease: false
24
+ version_requirements: *70109058811900
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70109058811480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70109058811480
36
+ - !ruby/object:Gem::Dependency
37
+ name: rcov
38
+ requirement: &70109058811000 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70109058811000
47
+ - !ruby/object:Gem::Dependency
48
+ name: yard
49
+ requirement: &70109058810580 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70109058810580
58
+ description: LLVM bindings for Ruby
28
59
  email: jvoorhis@gmail.com
29
60
  executables: []
30
-
31
- extensions: []
32
-
33
- extra_rdoc_files:
61
+ extensions:
62
+ - ext/ruby-llvm-support/Rakefile
63
+ extra_rdoc_files:
34
64
  - README.rdoc
35
- files:
65
+ files:
36
66
  - lib/llvm/analysis.rb
67
+ - lib/llvm/core/bitcode.rb
37
68
  - lib/llvm/core/builder.rb
38
69
  - lib/llvm/core/context.rb
39
70
  - lib/llvm/core/module.rb
@@ -42,38 +73,84 @@ files:
42
73
  - lib/llvm/core/value.rb
43
74
  - lib/llvm/core.rb
44
75
  - lib/llvm/execution_engine.rb
76
+ - lib/llvm/support.rb
45
77
  - lib/llvm/target.rb
46
78
  - lib/llvm/transforms/ipo.rb
47
79
  - lib/llvm/transforms/scalar.rb
80
+ - lib/llvm/version.rb
48
81
  - lib/llvm.rb
82
+ - ext/ruby-llvm-support/Rakefile
83
+ - ext/ruby-llvm-support/support.cpp
49
84
  - README.rdoc
50
- has_rdoc: true
85
+ - test/array_test.rb
86
+ - test/basic_block_test.rb
87
+ - test/basic_test.rb
88
+ - test/binary_operations_test.rb
89
+ - test/bitcode_test.rb
90
+ - test/branch_test.rb
91
+ - test/call_test.rb
92
+ - test/comparisons_test.rb
93
+ - test/conversions_test.rb
94
+ - test/double_test.rb
95
+ - test/equality_test.rb
96
+ - test/generic_value_test.rb
97
+ - test/instruction_test.rb
98
+ - test/ipo_test.rb
99
+ - test/memory_access_test.rb
100
+ - test/module_test.rb
101
+ - test/parameter_collection_test.rb
102
+ - test/phi_test.rb
103
+ - test/select_test.rb
104
+ - test/struct_test.rb
105
+ - test/test_helper.rb
106
+ - test/type_test.rb
107
+ - test/vector_test.rb
51
108
  homepage: http://github.com/jvoorhis/ruby-llvm
52
109
  licenses: []
53
-
54
110
  post_install_message:
55
111
  rdoc_options: []
56
-
57
- require_paths:
112
+ require_paths:
58
113
  - lib
59
- required_ruby_version: !ruby/object:Gem::Requirement
114
+ required_ruby_version: !ruby/object:Gem::Requirement
60
115
  none: false
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- version: "0"
65
- required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
121
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: "0"
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
71
126
  requirements: []
72
-
73
127
  rubyforge_project:
74
- rubygems_version: 1.6.0
128
+ rubygems_version: 1.8.10
75
129
  signing_key:
76
130
  specification_version: 3
77
131
  summary: LLVM bindings for Ruby
78
- test_files: []
79
-
132
+ test_files:
133
+ - test/array_test.rb
134
+ - test/basic_block_test.rb
135
+ - test/basic_test.rb
136
+ - test/binary_operations_test.rb
137
+ - test/bitcode_test.rb
138
+ - test/branch_test.rb
139
+ - test/call_test.rb
140
+ - test/comparisons_test.rb
141
+ - test/conversions_test.rb
142
+ - test/double_test.rb
143
+ - test/equality_test.rb
144
+ - test/generic_value_test.rb
145
+ - test/instruction_test.rb
146
+ - test/ipo_test.rb
147
+ - test/memory_access_test.rb
148
+ - test/module_test.rb
149
+ - test/parameter_collection_test.rb
150
+ - test/phi_test.rb
151
+ - test/select_test.rb
152
+ - test/struct_test.rb
153
+ - test/test_helper.rb
154
+ - test/type_test.rb
155
+ - test/vector_test.rb
156
+ has_rdoc: true