ruby-llvm 13.0.1 → 13.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4728b4463e1ffaea0e8b00c976923fe77404b01c55ff3c25b015aa2fef5decf
4
- data.tar.gz: 35e6017a7035f29b9a39cfd6cbf18116a181f9e2706b12c0318a251525250bc7
3
+ metadata.gz: 84318238934c7be3357466dfcda5fdf52b587164a4756f1663a749edd3754964
4
+ data.tar.gz: 2fee5d96b91ebe3edf7d835c4cbe5111e1d9ce8fa63042a32123b506cb65085d
5
5
  SHA512:
6
- metadata.gz: 5353f3dfc22fcaa701ffa5bc70d5987e03a678881ad03a530ed99f4baada579396c9d8720a999cb9fff9fb93d4efad13fb2cd4ed6486b9a0658f54ad5676f663
7
- data.tar.gz: 95cbae2bfda937b9d8eb1a821083bc04858a3e19942ffbf8c195671f51f817d17be2829c51015e3bcd9b699eb467e9dca127e0ff0bb743a2f76288180014846d
6
+ metadata.gz: 5e03ffebe80a650098d8974ea5b218a727b3f165400cd797bc1d3e652d10cb06f4a621310917e6ee9011b31f4b3ab2dfe215165373c666e78b5d68c8e15703e6
7
+ data.tar.gz: bd40aa282aeac16fe3e189c05ab26db1230af73f7b536803e218f1d78a86087fe6f4389e3dfe4ae66eab4fdc7a4417dd712bd2449c6c12b77efd1d99bfffc7a3
@@ -24,7 +24,10 @@ module LLVM
24
24
  # @param [LLVM::Instruction] instruction
25
25
  # @return [LLVM::Builder]
26
26
  def position(block, instruction)
27
- raise "Block must not be nil" if block.nil?
27
+ raise ArgumentError, "Block must be LLVM::BasicBlock" if !block.is_a?(LLVM::BasicBlock)
28
+
29
+ raise ArgumentError, "Instruction must be LLVM::Instruction" if !instruction.is_a?(LLVM::Instruction)
30
+
28
31
  C.position_builder(self, block, instruction)
29
32
  self
30
33
  end
@@ -34,7 +37,8 @@ module LLVM
34
37
  # @param [LLVM::Instruction] instruction
35
38
  # @return [LLVM::Builder]
36
39
  def position_before(instruction)
37
- raise "Instruction must not be nil" if instruction.nil?
40
+ raise ArgumentError, "Instruction must be LLVM::Instruction" if !instruction.is_a?(LLVM::Instruction)
41
+
38
42
  C.position_builder_before(self, instruction)
39
43
  self
40
44
  end
@@ -44,7 +48,8 @@ module LLVM
44
48
  # @param [LLVM::BasicBlock] block
45
49
  # @return [LLVM::Builder]
46
50
  def position_at_end(block)
47
- raise "Block must not be nil" if block.nil?
51
+ raise ArgumentError, "Block must be LLVM::BasicBlock" if !block.is_a?(LLVM::BasicBlock)
52
+
48
53
  C.position_builder_at_end(self, block)
49
54
  self
50
55
  end
@@ -65,7 +70,11 @@ module LLVM
65
70
  # @param [LLVM::Value] val The value to return
66
71
  # @return [LLVM::Instruction]
67
72
  # @LLVMinst ret
68
- def ret(val)
73
+ def ret(val = nil)
74
+ unless [LLVM::Value, NilClass].any? { |c| val.is_a?(c) }
75
+ raise ArgumentError, "Trying to build LLVM ret with non-value: #{val.inspect}"
76
+ end
77
+
69
78
  Instruction.from_ptr(C.build_ret(self, val))
70
79
  end
71
80
 
@@ -85,7 +94,8 @@ module LLVM
85
94
  # @return [LLVM::Instruction]
86
95
  # @LLVMinst br
87
96
  def br(block)
88
- raise "Block must not be nil" if block.nil?
97
+ raise ArgumentError, "Trying to build LLVM br with non-block: #{block.inspect}" if !block.is_a?(LLVM::BasicBlock)
98
+
89
99
  Instruction.from_ptr(
90
100
  C.build_br(self, block))
91
101
  end
@@ -107,8 +117,31 @@ module LLVM
107
117
  # @return [LLVM::Instruction]
108
118
  # @LLVMinst br
109
119
  def cond(cond, iftrue, iffalse)
120
+ raise ArgumentError, "Trying to build LLVM cond br with non-block (true branch): #{iftrue.inspect}" if !iftrue.is_a?(LLVM::BasicBlock)
121
+
122
+ raise ArgumentError, "Trying to build LLVM cond br with non-block (false branch): #{iffalse.inspect}" if !iffalse.is_a?(LLVM::BasicBlock)
123
+
124
+ cond2 = cond_condition(cond)
125
+
110
126
  Instruction.from_ptr(
111
- C.build_cond_br(self, cond, iftrue, iffalse))
127
+ C.build_cond_br(self, cond2, iftrue, iffalse))
128
+ end
129
+
130
+ private def cond_condition(cond) # rubocop:disable Style/AccessModifierDeclarations
131
+ case cond
132
+ when LLVM::Value
133
+ cond_type = cond.type
134
+ if (cond_type.kind != :integer) || (cond_type.width != 1)
135
+ raise ArgumentError, "Trying to build LLVM cond br with non-i1 condition: #{cond_type}"
136
+ end
137
+ cond
138
+ when true
139
+ LLVM::Int1.from_i(1)
140
+ when false
141
+ LLVM::Int1.from_i(0)
142
+ else
143
+ raise ArgumentError, "Trying to build LLVM cond br with non-value condition: #{cond.inspect}"
144
+ end
112
145
  end
113
146
 
114
147
  # @LLVMinst switch
@@ -835,7 +868,8 @@ module LLVM
835
868
  # @param [LLVM::Instruction]
836
869
  # @LLVMinst call
837
870
  def call(fun, *args)
838
- raise "No fun" if fun.nil?
871
+ raise ArgumentError, "Trying to build LLVM call with non-function: #{fun.inspect}" if !fun.is_a?(LLVM::Function)
872
+
839
873
  if args.last.kind_of? String
840
874
  name = args.pop
841
875
  else
@@ -870,6 +904,10 @@ module LLVM
870
904
  # @return [LLVM::Instruction] The extracted element
871
905
  # @LLVMinst extractelement
872
906
  def extract_element(vector, idx, name = "")
907
+ error = element_error(vector, idx)
908
+
909
+ raise ArgumentError, "Error building extract_element with #{error}" if error
910
+
873
911
  Instruction.from_ptr(C.build_extract_element(self, vector, idx, name))
874
912
  end
875
913
 
@@ -881,9 +919,27 @@ module LLVM
881
919
  # @return [LLVM::Instruction] A vector the same type as 'vector'
882
920
  # @LLVMinst insertelement
883
921
  def insert_element(vector, elem, idx, name = "")
922
+ error = element_error(vector, idx)
923
+
924
+ error ||= if !elem.is_a?(LLVM::Value)
925
+ "elem: #{elem.inspect}"
926
+ end
927
+
928
+ raise ArgumentError, "Error building insert_element with #{error}" if error
929
+
884
930
  Instruction.from_ptr(C.build_insert_element(self, vector, elem, idx, name))
885
931
  end
886
932
 
933
+ private def element_error(vector, idx) # rubocop:disable Style/AccessModifierDeclarations
934
+ if !vector.is_a?(LLVM::Value)
935
+ "non-value: #{vector.inspect}"
936
+ elsif vector.type.kind != :vector
937
+ "non-vector: #{vector.type.kind}"
938
+ elsif !idx.is_a?(LLVM::Value)
939
+ "index: #{idx}"
940
+ end
941
+ end
942
+
887
943
  # Shuffle two vectors according to a given mask
888
944
  # @param [LLVM::Value] vec1 A vector
889
945
  # @param [LLVM::Value] vec2 A vector of the same type and arity as vec1
@@ -898,25 +954,47 @@ module LLVM
898
954
 
899
955
  # Extract the value of a member field from an aggregate value
900
956
  # @param [LLVM::Value] aggregate An aggregate value
901
- # @param [LLVM::Value] idx The index of the member to extract
957
+ # @param [Integer] idx The index of the member to extract
902
958
  # @param [String] name The name of the result in LLVM IR
903
959
  # @return [LLVM::Instruction] The extracted value
904
960
  # @LLVMinst extractvalue
905
961
  def extract_value(aggregate, idx, name = "")
962
+ error = value_error(aggregate, idx)
963
+
964
+ raise ArgumentError, "Error building extract_value with #{error}" if error
965
+
906
966
  Instruction.from_ptr(C.build_extract_value(self, aggregate, idx, name))
907
967
  end
908
968
 
909
969
  # Insert a value into an aggregate value's member field
910
970
  # @param [LLVM::Value] aggregate An aggregate value
911
971
  # @param [LLVM::Value] elem The value to insert into 'aggregate'
912
- # @param [LLVM::Value] idx The index at which to insert the value
972
+ # @param [Integer] idx The index at which to insert the value
913
973
  # @param [String] name The name of the result in LLVM IR
914
974
  # @return [LLVM::Instruction] An aggregate value of the same type as 'aggregate'
915
975
  # @LLVMinst insertvalue
916
976
  def insert_value(aggregate, elem, idx, name = "")
977
+ error = value_error(aggregate, idx)
978
+
979
+ error ||= if !elem.is_a?(LLVM::Value)
980
+ "elem: #{elem.inspect}"
981
+ end
982
+
983
+ raise ArgumentError, "Error building insert_value with #{error}" if error
984
+
917
985
  Instruction.from_ptr(C.build_insert_value(self, aggregate, elem, idx, name))
918
986
  end
919
987
 
988
+ private def value_error(aggregate, idx) # rubocop:disable Style/AccessModifierDeclarations
989
+ if !aggregate.is_a?(LLVM::Value)
990
+ "non-value: #{aggregate.inspect}"
991
+ elsif !aggregate.type.aggregate?
992
+ "non-aggregate: #{aggregate.type.kind}"
993
+ elsif !idx.is_a?(Integer) || idx.negative?
994
+ "index: #{idx}"
995
+ end
996
+ end
997
+
920
998
  # Check if a value is null
921
999
  # @param [LLVM::Value] val The value to check
922
1000
  # @param [String] name The name of the result in LLVM IR
@@ -66,6 +66,10 @@ module LLVM
66
66
  C.print_type_to_string(self)
67
67
  end
68
68
 
69
+ def aggregate?
70
+ [:struct, :array].include?(kind)
71
+ end
72
+
69
73
  # Creates an array type of Type with the given size.
70
74
  def self.array(ty, sz = 0)
71
75
  from_ptr(C.array_type(LLVM::Type(ty), sz), :array)
data/lib/llvm/version.rb CHANGED
@@ -3,5 +3,5 @@
3
3
  module LLVM
4
4
  LLVM_VERSION = "13"
5
5
  LLVM_REQUIRED_VERSION = "13.0"
6
- RUBY_LLVM_VERSION = "13.0.1"
6
+ RUBY_LLVM_VERSION = "13.0.2"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-llvm
3
3
  version: !ruby/object:Gem::Version
4
- version: 13.0.1
4
+ version: 13.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Johnson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-12-21 00:00:00.000000000 Z
12
+ date: 2022-06-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ffi
@@ -65,42 +65,42 @@ dependencies:
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 5.14.1
68
+ version: 5.15.0
69
69
  type: :development
70
70
  prerelease: false
71
71
  version_requirements: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 5.14.1
75
+ version: 5.15.0
76
76
  - !ruby/object:Gem::Dependency
77
77
  name: minitest-reporters
78
78
  requirement: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 1.4.2
82
+ version: 1.5.0
83
83
  type: :development
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 1.4.2
89
+ version: 1.5.0
90
90
  - !ruby/object:Gem::Dependency
91
91
  name: rubocop
92
92
  requirement: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 1.22.1
96
+ version: 1.30.0
97
97
  type: :development
98
98
  prerelease: false
99
99
  version_requirements: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 1.22.1
103
+ version: 1.30.0
104
104
  - !ruby/object:Gem::Dependency
105
105
  name: rubocop-minitest
106
106
  requirement: !ruby/object:Gem::Requirement
@@ -203,44 +203,17 @@ files:
203
203
  - lib/llvm/transforms/vectorize.rb
204
204
  - lib/llvm/transforms/vectorize_ffi.rb
205
205
  - lib/llvm/version.rb
206
- - test/array_test.rb
207
- - test/basic_block_test.rb
208
- - test/binary_operations_test.rb
209
- - test/bitcode_test.rb
210
- - test/branch_test.rb
211
- - test/call_test.rb
212
- - test/comparisons_test.rb
213
- - test/conversions_test.rb
214
- - test/double_test.rb
215
- - test/equality_test.rb
216
- - test/function_test.rb
217
- - test/generic_value_test.rb
218
- - test/instruction_test.rb
219
- - test/integer_test.rb
220
- - test/ipo_test.rb
221
- - test/linker_test.rb
222
- - test/mcjit_test.rb
223
- - test/memory_access_test.rb
224
- - test/module_test.rb
225
- - test/parameter_collection_test.rb
226
- - test/pass_manager_builder_test.rb
227
- - test/phi_test.rb
228
- - test/select_test.rb
229
- - test/struct_test.rb
230
- - test/target_test.rb
231
- - test/test_helper.rb
232
- - test/type_test.rb
233
- - test/vector_test.rb
234
206
  homepage: http://github.com/ruby-llvm/ruby-llvm
235
207
  licenses: []
236
- metadata: {}
208
+ metadata:
209
+ rubygems_mfa_required: 'true'
237
210
  post_install_message:
238
211
  rdoc_options: []
239
212
  require_paths:
240
213
  - lib
241
214
  required_ruby_version: !ruby/object:Gem::Requirement
242
215
  requirements:
243
- - - '='
216
+ - - ">="
244
217
  - !ruby/object:Gem::Version
245
218
  version: '2.7'
246
219
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -249,36 +222,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
249
222
  - !ruby/object:Gem::Version
250
223
  version: '0'
251
224
  requirements: []
252
- rubygems_version: 3.1.2
225
+ rubygems_version: 3.3.5
253
226
  signing_key:
254
227
  specification_version: 4
255
228
  summary: LLVM bindings for Ruby
256
- test_files:
257
- - test/double_test.rb
258
- - test/type_test.rb
259
- - test/basic_block_test.rb
260
- - test/struct_test.rb
261
- - test/array_test.rb
262
- - test/vector_test.rb
263
- - test/instruction_test.rb
264
- - test/ipo_test.rb
265
- - test/branch_test.rb
266
- - test/binary_operations_test.rb
267
- - test/bitcode_test.rb
268
- - test/pass_manager_builder_test.rb
269
- - test/equality_test.rb
270
- - test/function_test.rb
271
- - test/parameter_collection_test.rb
272
- - test/memory_access_test.rb
273
- - test/select_test.rb
274
- - test/module_test.rb
275
- - test/test_helper.rb
276
- - test/comparisons_test.rb
277
- - test/target_test.rb
278
- - test/call_test.rb
279
- - test/conversions_test.rb
280
- - test/linker_test.rb
281
- - test/generic_value_test.rb
282
- - test/phi_test.rb
283
- - test/integer_test.rb
284
- - test/mcjit_test.rb
229
+ test_files: []
data/test/array_test.rb DELETED
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class ArrayTestCase < Minitest::Test
6
-
7
- def setup
8
- LLVM.init_jit
9
- end
10
-
11
- def test_constant_array_from_size
12
- array = LLVM::ConstantArray.const(LLVM::Int, 2) { |i| LLVM::Int(i) }
13
- assert_instance_of LLVM::ConstantArray, array
14
- assert_equal 2, array.size
15
- end
16
-
17
- def test_constant_array_from_array
18
- array = LLVM::ConstantArray.const(LLVM::Int, [LLVM::Int(0), LLVM::Int(1)])
19
- assert_instance_of LLVM::ConstantArray, array
20
- assert_equal 2, array.size
21
- end
22
-
23
- def test_array_values
24
- assert_equal 2 + 3, run_array_values(2, 3).to_i
25
- end
26
-
27
- def run_array_values(value1, value2)
28
- run_function([LLVM::Int, LLVM::Int], [value1, value2], LLVM::Int) do |builder, function, *arguments|
29
- entry = function.basic_blocks.append
30
- builder.position_at_end(entry)
31
- pointer = builder.alloca(LLVM::Array(LLVM::Int, 2))
32
- array = builder.load(pointer)
33
- array = builder.insert_value(array, arguments.first, 0)
34
- array = builder.insert_value(array, arguments.last, 1)
35
- builder.ret(builder.add(builder.extract_value(array, 0),
36
- builder.extract_value(array, 1)))
37
- end
38
- end
39
-
40
- end
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class BasicBlockTestCase < Minitest::Test
6
-
7
- def setup
8
- LLVM.init_jit
9
- @module = LLVM::Module.new("BasicBlockTestCase")
10
- end
11
-
12
- def test_basic_block_collection
13
- @module.functions.add("test_basic_block_collection", [], LLVM.Void) do |fn|
14
- coll = fn.basic_blocks
15
-
16
- block1 = coll.append
17
- assert_instance_of LLVM::BasicBlock, block1
18
-
19
- assert_equal 1, coll.size
20
- assert_equal coll.first, coll.last,
21
- 'Only one block exists in the function'
22
- assert_equal coll.first, coll.entry,
23
- 'The entry block for the function is always the first block'
24
-
25
- block2 = coll.append
26
- assert_equal 2, coll.size
27
- assert_equal block1, coll.first
28
- assert_equal block2, coll.last
29
-
30
- [ coll.each.to_a, coll.to_a ].each do |blocks|
31
- assert_equal 2, blocks.size
32
- assert_equal block1, blocks[0]
33
- assert_equal block2, blocks[1]
34
- end
35
- end
36
- end
37
-
38
- def test_basic_block
39
- @module.functions.add("test_basic_block", [], LLVM.Void) do |fn|
40
- coll = fn.basic_blocks
41
-
42
- block1 = coll.append
43
- block2 = coll.append
44
-
45
- assert_equal fn, block1.parent
46
- assert_equal fn, block2.parent
47
-
48
- assert_equal block2, block1.next
49
- assert_equal block1, block2.previous
50
-
51
- block1.build do |builder|
52
- builder.br(block2)
53
- end
54
-
55
- block2.build do |builder|
56
- builder.ret_void
57
- end
58
-
59
- assert_equal block1.first_instruction,
60
- block1.last_instruction
61
- assert_equal block2.first_instruction,
62
- block2.last_instruction
63
- end
64
- end
65
-
66
- def test_basic_block_enumerable
67
- @module.functions.add("test_basic_block_enumerable", [LLVM::Double], LLVM::Double) do |fn, arg|
68
- block1 = fn.basic_blocks.append
69
-
70
- [ block1.instructions.to_a, block1.instructions.each.to_a ].each do |insts|
71
- assert_equal 0, insts.size, 'Empty basic block'
72
- end
73
-
74
- block1.build do |builder|
75
- builder.ret(builder.fadd(arg, LLVM.Double(1.0)))
76
- end
77
-
78
- [ block1.instructions.to_a, block1.instructions.each.to_a ].each do |insts|
79
- assert_equal 2, insts.size
80
- assert_equal block1.first_instruction, insts[0] # deprecated
81
- assert_equal block1.last_instruction, insts[1] # deprecated
82
- assert_equal block1.instructions.first, insts[0]
83
- assert_equal block1.instructions.last, insts[1]
84
- end
85
-
86
- end
87
- end
88
-
89
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class BasicOperationsTestCase < Minitest::Test
6
-
7
- def setup
8
- LLVM.init_jit
9
- end
10
-
11
- def test_integer_binary_operations
12
- integer_binary_operation_assertion(:add, 3, 2, 3 + 2)
13
- integer_binary_operation_assertion(:sub, 3, 2, 3 - 2)
14
- integer_binary_operation_assertion(:mul, 3, 2, 3 * 2)
15
- integer_binary_operation_assertion(:udiv, 10, 2, 10 / 2)
16
- integer_binary_operation_assertion(:sdiv, 10, 2, 10 / 2)
17
- integer_binary_operation_assertion(:urem, 10, 3, 10 % 3)
18
- integer_binary_operation_assertion(:srem, 10, 3, 10 % 3)
19
- end
20
-
21
- def test_integer_bitwise_binary_operations
22
- integer_binary_operation_assertion(:shl, 2, 3, 2 << 3)
23
- integer_binary_operation_assertion(:lshr, 16, 3, 16 >> 3)
24
- integer_binary_operation_assertion(:ashr, 16, 3, 16 >> 3)
25
- integer_binary_operation_assertion(:and, 2, 1, 2 & 1)
26
- integer_binary_operation_assertion(:or, 2, 1, 2 | 1)
27
- integer_binary_operation_assertion(:xor, 3, 2, 3 ^ 2)
28
- end
29
-
30
- def test_float_binary_operations
31
- float_binary_operation_assertion(:fadd, 3.1, 2.2, 3.1 + 2.2)
32
- float_binary_operation_assertion(:fsub, 3.1, 2.2, 3.1 - 2.2)
33
- float_binary_operation_assertion(:fmul, 3.1, 2.2, 3.1 * 2.2)
34
- float_binary_operation_assertion(:fdiv, 3.1, 2.2, 3.1 / 2.2)
35
- float_binary_operation_assertion(:frem, 3.1, 2.2, 3.1 % 2.2)
36
- end
37
-
38
- def integer_binary_operation_assertion(operation, operand1, operand2, expected_result)
39
- result = run_binary_operation(operation,
40
- LLVM::Int(operand1), LLVM::Int(operand2),
41
- LLVM::Int).to_i
42
- assert_equal expected_result, result
43
- end
44
-
45
- def float_binary_operation_assertion(operation, operand1, operand2, expected_result)
46
- result = run_binary_operation(operation,
47
- LLVM::Float(operand1), LLVM::Float(operand2),
48
- LLVM::Float).to_f
49
- assert_in_delta expected_result, result, 0.001
50
- end
51
-
52
- def run_binary_operation(operation, operand1, operand2, return_type)
53
- run_function([], [], return_type) do |builder, function, *arguments|
54
- entry = function.basic_blocks.append
55
- builder.position_at_end(entry)
56
- builder.ret(builder.send(operation, operand1, operand2))
57
- end
58
- end
59
-
60
- end
data/test/bitcode_test.rb DELETED
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
- require "tempfile"
5
-
6
- class BitcodeTestCase < Minitest::Test
7
- def setup
8
- LLVM.init_jit
9
- end
10
-
11
- def test_bitcode
12
- test_module = define_module("test_module") do |mod|
13
- define_function(mod, "test_function", [], LLVM::Int) do |builder, function, *arguments|
14
- entry = function.basic_blocks.append
15
- builder.position_at_end(entry)
16
- builder.ret(LLVM::Int(1))
17
- end
18
- end
19
- Tempfile.open("bitcode") do |tmp|
20
- assert test_module.write_bitcode(tmp)
21
- new_module = LLVM::Module.parse_bitcode(tmp.path)
22
- result = run_function_on_module(new_module, "test_function").to_i
23
- assert_equal 1, result
24
- end
25
- end
26
- end
data/test/branch_test.rb DELETED
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class BranchTestCase < Minitest::Test
6
-
7
- def setup
8
- LLVM.init_jit
9
- end
10
-
11
- def test_branching
12
- assert_equal 0, direct_jump_function().to_i
13
- assert_equal 0, conditional_jump_function().to_i
14
- assert_equal 0, switched_jump_function().to_i
15
- end
16
-
17
- def direct_jump_function
18
- run_function([], [], LLVM::Int) do |builder, function, *arguments|
19
- entry = function.basic_blocks.append
20
- branch_1 = function.basic_blocks.append
21
- branch_2 = function.basic_blocks.append
22
- builder.position_at_end(entry)
23
- builder.br(branch_2)
24
- builder.position_at_end(branch_1)
25
- builder.ret(LLVM::Int(1))
26
- builder.position_at_end(branch_2)
27
- builder.ret(LLVM::Int(0))
28
- end
29
- end
30
-
31
- def conditional_jump_function
32
- run_function([], [], LLVM::Int) do |builder, function, *arguments|
33
- entry = function.basic_blocks.append
34
- branch_1 = function.basic_blocks.append
35
- branch_2 = function.basic_blocks.append
36
- builder.position_at_end(entry)
37
- builder.cond(builder.icmp(:eq, LLVM::Int(1), LLVM::Int(2)), branch_1, branch_2)
38
- builder.position_at_end(branch_1)
39
- builder.ret(LLVM::Int(1))
40
- builder.position_at_end(branch_2)
41
- builder.ret(LLVM::Int(0))
42
- end
43
- end
44
-
45
- def switched_jump_function
46
- run_function([], [], LLVM::Int) do |builder, function, *arguments|
47
- entry = function.basic_blocks.append
48
- branch_1 = function.basic_blocks.append
49
- branch_2 = function.basic_blocks.append
50
- builder.position_at_end(entry)
51
- switch = builder.switch(LLVM::Int(1), branch_1, LLVM::Int(1) => branch_2)
52
- builder.position_at_end(branch_1)
53
- builder.ret(LLVM::Int(1))
54
- builder.position_at_end(branch_2)
55
- builder.ret(LLVM::Int(0))
56
- end
57
- end
58
-
59
- end