ruby-llvm-next 10.0.0

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.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +30 -0
  3. data/README.md +67 -0
  4. data/ext/ruby-llvm-support/Rakefile +110 -0
  5. data/ext/ruby-llvm-support/support.cpp +32 -0
  6. data/lib/llvm.rb +29 -0
  7. data/lib/llvm/analysis.rb +49 -0
  8. data/lib/llvm/analysis_ffi.rb +77 -0
  9. data/lib/llvm/config.rb +10 -0
  10. data/lib/llvm/core.rb +97 -0
  11. data/lib/llvm/core/bitcode.rb +84 -0
  12. data/lib/llvm/core/bitcode_ffi.rb +132 -0
  13. data/lib/llvm/core/builder.rb +944 -0
  14. data/lib/llvm/core/context.rb +24 -0
  15. data/lib/llvm/core/module.rb +240 -0
  16. data/lib/llvm/core/pass_manager.rb +80 -0
  17. data/lib/llvm/core/type.rb +210 -0
  18. data/lib/llvm/core/value.rb +1005 -0
  19. data/lib/llvm/core_ffi.rb +6021 -0
  20. data/lib/llvm/execution_engine.rb +323 -0
  21. data/lib/llvm/execution_engine_ffi.rb +421 -0
  22. data/lib/llvm/linker.rb +16 -0
  23. data/lib/llvm/linker_ffi.rb +44 -0
  24. data/lib/llvm/support.rb +38 -0
  25. data/lib/llvm/target.rb +318 -0
  26. data/lib/llvm/target_ffi.rb +628 -0
  27. data/lib/llvm/transforms/builder.rb +107 -0
  28. data/lib/llvm/transforms/builder_ffi.rb +117 -0
  29. data/lib/llvm/transforms/ipo.rb +78 -0
  30. data/lib/llvm/transforms/ipo_ffi.rb +127 -0
  31. data/lib/llvm/transforms/scalar.rb +152 -0
  32. data/lib/llvm/transforms/scalar_ffi.rb +344 -0
  33. data/lib/llvm/transforms/vectorize.rb +22 -0
  34. data/lib/llvm/transforms/vectorize_ffi.rb +38 -0
  35. data/lib/llvm/version.rb +5 -0
  36. data/test/array_test.rb +38 -0
  37. data/test/basic_block_test.rb +87 -0
  38. data/test/binary_operations_test.rb +58 -0
  39. data/test/bitcode_test.rb +24 -0
  40. data/test/branch_test.rb +57 -0
  41. data/test/call_test.rb +82 -0
  42. data/test/comparisons_test.rb +66 -0
  43. data/test/conversions_test.rb +92 -0
  44. data/test/double_test.rb +34 -0
  45. data/test/equality_test.rb +89 -0
  46. data/test/function_test.rb +100 -0
  47. data/test/generic_value_test.rb +22 -0
  48. data/test/instruction_test.rb +30 -0
  49. data/test/ipo_test.rb +53 -0
  50. data/test/linker_test.rb +37 -0
  51. data/test/mcjit_test.rb +94 -0
  52. data/test/memory_access_test.rb +38 -0
  53. data/test/module_test.rb +93 -0
  54. data/test/parameter_collection_test.rb +28 -0
  55. data/test/pass_manager_builder_test.rb +53 -0
  56. data/test/phi_test.rb +33 -0
  57. data/test/select_test.rb +22 -0
  58. data/test/struct_test.rb +98 -0
  59. data/test/target_test.rb +113 -0
  60. data/test/test_helper.rb +62 -0
  61. data/test/type_test.rb +15 -0
  62. data/test/vector_test.rb +64 -0
  63. metadata +240 -0
@@ -0,0 +1,84 @@
1
+ require 'llvm/core/bitcode_ffi'
2
+
3
+ module LLVM
4
+ class Module
5
+ # Parse a module from a memory buffer
6
+ # @param [String, LLVM::MemoryBuffer] path_or_memory_buffer
7
+ # @return [LLVM::Module]
8
+ def self.parse_bitcode(path_or_memory_buffer)
9
+ memory_buffer = case path_or_memory_buffer
10
+ when MemoryBuffer then path_or_memory_buffer
11
+ else MemoryBuffer.from_file(path_or_memory_buffer)
12
+ end
13
+ FFI::MemoryPointer.new(:pointer) do |mod_ref|
14
+ FFI::MemoryPointer.new(:pointer) do |msg_ref|
15
+ status = C.parse_bitcode(memory_buffer, mod_ref, msg_ref)
16
+ raise msg_ref.get_pointer(0).get_string(0) if status != 0
17
+ return from_ptr(mod_ref.get_pointer(0))
18
+ end
19
+ end
20
+ end
21
+
22
+ # Write bitcode to the given path, IO object or file descriptor
23
+ # @param [String, IO, Integer] path_or_io Pathname, IO object or file descriptor
24
+ # @return [true, false] Success
25
+ def write_bitcode(path_or_io)
26
+ status = if path_or_io.respond_to?(:path)
27
+ C.write_bitcode_to_file(self, path_or_io.path)
28
+ elsif path_or_io.respond_to?(:fileno)
29
+ C.write_bitcode_to_fd(self, path_or_io.fileno, 0, 1)
30
+ elsif path_or_io.kind_of?(Integer)
31
+ C.write_bitcode_to_fd(self, path_or_io, 0, 1)
32
+ else
33
+ C.write_bitcode_to_file(self, path_or_io.to_str)
34
+ end
35
+ status == 0
36
+ end
37
+ end
38
+
39
+ # @private
40
+ class MemoryBuffer
41
+ private_class_method :new
42
+
43
+ # @private
44
+ def initialize(ptr)
45
+ @ptr = ptr
46
+ end
47
+
48
+ # @private
49
+ def to_ptr
50
+ @ptr
51
+ end
52
+
53
+ # Read the contents of a file into a memory buffer
54
+ # @param [String] path
55
+ # @return [LLVM::MemoryBuffer]
56
+ def self.from_file(path)
57
+ FFI::MemoryPointer.new(:pointer) do |buf_ref|
58
+ FFI::MemoryPointer.new(:pointer) do |msg_ref|
59
+ status = C.create_memory_buffer_with_contents_of_file(path.to_str, buf_ref, msg_ref)
60
+ raise msg_ref.get_pointer(0).get_string(0) if status != 0
61
+ return new(buf_ref.get_pointer(0))
62
+ end
63
+ end
64
+ end
65
+
66
+ # Read STDIN into a memory buffer
67
+ # @return [LLVM::MemoryBuffer]
68
+ def self.from_stdin
69
+ FFI::Buffer.new(:pointer) do |buf_ref|
70
+ FFI::Buffer.new(:pointer) do |msg_ref|
71
+ status = C.create_memory_buffer_with_stdin(buf_ref, msg_ref)
72
+ raise msg_ref.get_pointer(0).get_string(0) if status != 0
73
+ return new(buf_ref.get_pointer(0))
74
+ end
75
+ end
76
+ end
77
+
78
+ def dispose
79
+ return if @ptr.nil?
80
+ C.dispose_memory_buffer(@ptr)
81
+ @ptr = nil
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,132 @@
1
+ # Generated by ffi_gen. Please do not change this file by hand.
2
+
3
+ require 'ffi'
4
+
5
+ module LLVM::C
6
+ extend FFI::Library
7
+ ffi_lib ["libLLVM-10.so.1", "LLVM-10"]
8
+
9
+ def self.attach_function(name, *_)
10
+ begin; super; rescue FFI::NotFoundError => e
11
+ (class << self; self; end).class_eval { define_method(name) { |*_| raise e } }
12
+ end
13
+ end
14
+
15
+ # (Not documented)
16
+ #
17
+ # @method parse_bitcode(mem_buf, out_module, out_message)
18
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
19
+ # @param [FFI::Pointer(*ModuleRef)] out_module
20
+ # @param [FFI::Pointer(**CharS)] out_message
21
+ # @return [Integer]
22
+ # @scope class
23
+ attach_function :parse_bitcode, :LLVMParseBitcode, [:pointer, :pointer, :pointer], :int
24
+
25
+ # (Not documented)
26
+ #
27
+ # @method parse_bitcode2(mem_buf, out_module)
28
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
29
+ # @param [FFI::Pointer(*ModuleRef)] out_module
30
+ # @return [Integer]
31
+ # @scope class
32
+ attach_function :parse_bitcode2, :LLVMParseBitcode2, [:pointer, :pointer], :int
33
+
34
+ # (Not documented)
35
+ #
36
+ # @method parse_bitcode_in_context(context_ref, mem_buf, out_module, out_message)
37
+ # @param [FFI::Pointer(ContextRef)] context_ref
38
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
39
+ # @param [FFI::Pointer(*ModuleRef)] out_module
40
+ # @param [FFI::Pointer(**CharS)] out_message
41
+ # @return [Integer]
42
+ # @scope class
43
+ attach_function :parse_bitcode_in_context, :LLVMParseBitcodeInContext, [:pointer, :pointer, :pointer, :pointer], :int
44
+
45
+ # (Not documented)
46
+ #
47
+ # @method parse_bitcode_in_context2(context_ref, mem_buf, out_module)
48
+ # @param [FFI::Pointer(ContextRef)] context_ref
49
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
50
+ # @param [FFI::Pointer(*ModuleRef)] out_module
51
+ # @return [Integer]
52
+ # @scope class
53
+ attach_function :parse_bitcode_in_context2, :LLVMParseBitcodeInContext2, [:pointer, :pointer, :pointer], :int
54
+
55
+ # (Not documented)
56
+ #
57
+ # @method get_bitcode_module_in_context(context_ref, mem_buf, out_m, out_message)
58
+ # @param [FFI::Pointer(ContextRef)] context_ref
59
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
60
+ # @param [FFI::Pointer(*ModuleRef)] out_m
61
+ # @param [FFI::Pointer(**CharS)] out_message
62
+ # @return [Integer]
63
+ # @scope class
64
+ attach_function :get_bitcode_module_in_context, :LLVMGetBitcodeModuleInContext, [:pointer, :pointer, :pointer, :pointer], :int
65
+
66
+ # (Not documented)
67
+ #
68
+ # @method get_bitcode_module_in_context2(context_ref, mem_buf, out_m)
69
+ # @param [FFI::Pointer(ContextRef)] context_ref
70
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
71
+ # @param [FFI::Pointer(*ModuleRef)] out_m
72
+ # @return [Integer]
73
+ # @scope class
74
+ attach_function :get_bitcode_module_in_context2, :LLVMGetBitcodeModuleInContext2, [:pointer, :pointer, :pointer], :int
75
+
76
+ # (Not documented)
77
+ #
78
+ # @method get_bitcode_module(mem_buf, out_m, out_message)
79
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
80
+ # @param [FFI::Pointer(*ModuleRef)] out_m
81
+ # @param [FFI::Pointer(**CharS)] out_message
82
+ # @return [Integer]
83
+ # @scope class
84
+ attach_function :get_bitcode_module, :LLVMGetBitcodeModule, [:pointer, :pointer, :pointer], :int
85
+
86
+ # (Not documented)
87
+ #
88
+ # @method get_bitcode_module2(mem_buf, out_m)
89
+ # @param [FFI::Pointer(MemoryBufferRef)] mem_buf
90
+ # @param [FFI::Pointer(*ModuleRef)] out_m
91
+ # @return [Integer]
92
+ # @scope class
93
+ attach_function :get_bitcode_module2, :LLVMGetBitcodeModule2, [:pointer, :pointer], :int
94
+
95
+ # Writes a module to the specified path. Returns 0 on success.
96
+ #
97
+ # @method write_bitcode_to_file(m, path)
98
+ # @param [FFI::Pointer(ModuleRef)] m
99
+ # @param [String] path
100
+ # @return [Integer]
101
+ # @scope class
102
+ attach_function :write_bitcode_to_file, :LLVMWriteBitcodeToFile, [:pointer, :string], :int
103
+
104
+ # Writes a module to an open file descriptor. Returns 0 on success.
105
+ #
106
+ # @method write_bitcode_to_fd(m, fd, should_close, unbuffered)
107
+ # @param [FFI::Pointer(ModuleRef)] m
108
+ # @param [Integer] fd
109
+ # @param [Integer] should_close
110
+ # @param [Integer] unbuffered
111
+ # @return [Integer]
112
+ # @scope class
113
+ attach_function :write_bitcode_to_fd, :LLVMWriteBitcodeToFD, [:pointer, :int, :int, :int], :int
114
+
115
+ # Deprecated for LLVMWriteBitcodeToFD. Writes a module to an open file
116
+ # descriptor. Returns 0 on success. Closes the Handle.
117
+ #
118
+ # @method write_bitcode_to_file_handle(m, handle)
119
+ # @param [FFI::Pointer(ModuleRef)] m
120
+ # @param [Integer] handle
121
+ # @return [Integer]
122
+ # @scope class
123
+ attach_function :write_bitcode_to_file_handle, :LLVMWriteBitcodeToFileHandle, [:pointer, :int], :int
124
+
125
+ # Writes a module to a new memory buffer and returns it.
126
+ #
127
+ # @method write_bitcode_to_memory_buffer(m)
128
+ # @param [FFI::Pointer(ModuleRef)] m
129
+ # @return [FFI::Pointer(MemoryBufferRef)]
130
+ # @scope class
131
+ attach_function :write_bitcode_to_memory_buffer, :LLVMWriteBitcodeToMemoryBuffer, [:pointer], :pointer
132
+ end
@@ -0,0 +1,944 @@
1
+ module LLVM
2
+ class Builder
3
+ # Important: Call #dispose to free backend memory after use.
4
+ def initialize
5
+ @ptr = C.create_builder()
6
+ end
7
+
8
+ def dispose
9
+ return if @ptr.nil?
10
+ C.dispose_builder(@ptr)
11
+ @ptr = nil
12
+ end
13
+
14
+ # @private
15
+ def to_ptr
16
+ @ptr
17
+ end
18
+
19
+ # Position the builder at the given Instruction within the given BasicBlock.
20
+ #
21
+ # @param [LLVM::BasicBlock] block
22
+ # @param [LLVM::Instruction] instruction
23
+ # @return [LLVM::Builder]
24
+ def position(block, instruction)
25
+ raise "Block must not be nil" if block.nil?
26
+ C.position_builder(self, block, instruction)
27
+ self
28
+ end
29
+
30
+ # Positions the builder before the given Instruction.
31
+ #
32
+ # @param [LLVM::Instruction] instruction
33
+ # @return [LLVM::Builder]
34
+ def position_before(instruction)
35
+ raise "Instruction must not be nil" if instruction.nil?
36
+ C.position_builder_before(self, instruction)
37
+ self
38
+ end
39
+
40
+ # Positions the builder at the end of the given BasicBlock.
41
+ #
42
+ # @param [LLVM::BasicBlock] block
43
+ # @return [LLVM::Builder]
44
+ def position_at_end(block)
45
+ raise "Block must not be nil" if block.nil?
46
+ C.position_builder_at_end(self, block)
47
+ self
48
+ end
49
+
50
+ # The BasicBlock at which the Builder is currently positioned.
51
+ #
52
+ # @return [LLVM::BasicBlock]
53
+ def insert_block
54
+ BasicBlock.from_ptr(C.get_insert_block(self))
55
+ end
56
+
57
+ # @return [LLVM::Instruction]
58
+ # @LLVMinst ret
59
+ def ret_void
60
+ Instruction.from_ptr(C.build_ret_void(self))
61
+ end
62
+
63
+ # @param [LLVM::Value] val The value to return
64
+ # @return [LLVM::Instruction]
65
+ # @LLVMinst ret
66
+ def ret(val)
67
+ Instruction.from_ptr(C.build_ret(self, val))
68
+ end
69
+
70
+ # Builds a ret instruction returning multiple values.
71
+ # @param [Array<LLVM::Value>] vals
72
+ # @return [LLVM::Instruction]
73
+ # @LLVMinst ret
74
+ def aggregate_ret(*vals)
75
+ FFI::MemoryPointer.new(FFI.type_size(:pointer) * vals.size) do |vals_ptr|
76
+ vals_ptr.write_array_of_pointer(vals)
77
+ Instruction.from_ptr(C.build_aggregate_ret(self, vals_ptr, vals.size))
78
+ end
79
+ end
80
+
81
+ # Unconditional branching (i.e. goto)
82
+ # @param [LLVM::BasicBlock] block Where to jump
83
+ # @return [LLVM::Instruction]
84
+ # @LLVMinst br
85
+ def br(block)
86
+ raise "Block must not be nil" if block.nil?
87
+ Instruction.from_ptr(
88
+ C.build_br(self, block))
89
+ end
90
+
91
+ # Indirect branching (i.e. computed goto)
92
+ # @param [LLVM::BasicBlock] addr Where to jump
93
+ # @param [Integer] num_dests Number of possible destinations to be added
94
+ # @return [LLVM::Instruction]
95
+ # @LLVMinst indirectbr
96
+ def ibr(addr, num_dests)
97
+ IndirectBr.from_ptr(
98
+ C.build_indirect_br(self, addr, num_dests))
99
+ end
100
+
101
+ # Conditional branching (i.e. if)
102
+ # @param [LLVM::Value] cond The condition
103
+ # @param [LLVM::BasicBlock] iftrue Where to jump if condition is true
104
+ # @param [LLVM::BasicBlock] iffalse Where to jump if condition is false
105
+ # @return [LLVM::Instruction]
106
+ # @LLVMinst br
107
+ def cond(cond, iftrue, iffalse)
108
+ Instruction.from_ptr(
109
+ C.build_cond_br(self, cond, iftrue, iffalse))
110
+ end
111
+
112
+ # @LLVMinst switch
113
+ # @param [LLVM::Value] val The value to switch on
114
+ # @param [LLVM::BasicBlock] default The default case
115
+ # @param [Hash{LLVM::Value => LLVM::BasicBlock}] cases A Hash mapping
116
+ # values to basic blocks. When a value is matched, control will jump
117
+ # to the corresponding basic block.
118
+ # @return [LLVM::Instruction]
119
+ def switch(val, default, cases)
120
+ inst = SwitchInst.from_ptr(C.build_switch(self, val, default, cases.size))
121
+ cases.each do |(c, block)|
122
+ inst.add_case(c, block)
123
+ end
124
+ inst
125
+ end
126
+
127
+ # Invoke a function which may potentially unwind
128
+ # @param [LLVM::Function] fun The function to invoke
129
+ # @param [Array<LLVM::Value>] args Arguments passed to fun
130
+ # @param [LLVM::BasicBlock] normal Where to jump if fun does not unwind
131
+ # @param [LLVM::BasicBlock] exception Where to jump if fun unwinds
132
+ # @param [String] name Name of the result in LLVM IR
133
+ # @return [LLVM::Instruction] The value returned by 'fun', unless an
134
+ # unwind instruction occurs
135
+ # @LLVMinst invoke
136
+ def invoke(fun, args, normal, exception, name = "")
137
+ s = args.size
138
+ FFI::MemoryPointer.new(FFI.type_size(:pointer) * s) do |args_ptr|
139
+ args_ptr.write_array_of_pointer(args)
140
+ return Instruction.from_ptr(
141
+ C.build_invoke(self,
142
+ fun, args_ptr, s, normal, exception, name))
143
+ end
144
+ end
145
+
146
+ # Builds an unwind Instruction.
147
+ # @LLVMinst unwind
148
+ def unwind
149
+ Instruction.from_ptr(C.build_unwind(self))
150
+ end
151
+
152
+ # Generates an instruction with no defined semantics. Can be used to
153
+ # provide hints to the optimizer.
154
+ # @return [LLVM::Instruction]
155
+ # @LLVMinst unreachable
156
+ def unreachable
157
+ Instruction.from_ptr(C.build_unreachable(self))
158
+ end
159
+
160
+ # Integer addition.
161
+ # @param [LLVM::Value] lhs Integer or vector of integers
162
+ # @param [LLVM::Value] rhs Integer or vector of integers
163
+ # @param [String] name Name of the result in LLVM IR
164
+ # @return [LLVM::Instruction] The integer sum of the two operands
165
+ # @LLVMinst add
166
+ def add(lhs, rhs, name = "")
167
+ Instruction.from_ptr(C.build_add(self, lhs, rhs, name))
168
+ end
169
+
170
+ # "No signed wrap" integer addition.
171
+ # @param [LLVM::Value] lhs Integer or vector of integers
172
+ # @param [LLVM::Value] rhs Integer or vector of integers
173
+ # @param [String] name Name of the result in LLVM IR
174
+ # @return [LLVM::Instruction] The integer sum of the two operands
175
+ # @LLVMinst add
176
+ def nsw_add(lhs, rhs, name = "")
177
+ Instruction.from_ptr(C.build_nsw_add(self, lhs, rhs, name))
178
+ end
179
+
180
+ # "No unsigned wrap" integer addition.
181
+ # @param [LLVM::Value] lhs Integer or vector of integers
182
+ # @param [LLVM::Value] rhs Integer or vector of integers
183
+ # @param [String] name Name of the result in LLVM IR
184
+ # @return [LLVM::Instruction] The integer sum of the two operands
185
+ # @LLVMinst add
186
+ def nuw_add(lhs, rhs, name = "")
187
+ Instruction.from_ptr(C.build_nuw_add(self, lhs, rhs, name))
188
+ end
189
+
190
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
191
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
192
+ # @param [String] name Name of the result in LLVM IR
193
+ # @return [LLVM::Instruction] The floating point sum of the two operands
194
+ # @LLVMinst fadd
195
+ def fadd(lhs, rhs, name = "")
196
+ Instruction.from_ptr(C.build_f_add(self, lhs, rhs, name))
197
+ end
198
+
199
+ # Integer subtraction.
200
+ # @param [LLVM::Value] lhs Integer or vector of integers
201
+ # @param [LLVM::Value] rhs Integer or vector of integers
202
+ # @param [String] name Name of the result in LLVM IR
203
+ # @return [LLVM::Instruction] The integer difference of the two operands
204
+ # @LLVMinst sub
205
+ def sub(lhs, rhs, name = "")
206
+ Instruction.from_ptr(C.build_sub(self, lhs, rhs, name))
207
+ end
208
+
209
+ # No signed wrap integer subtraction.
210
+ # @param [LLVM::Value] lhs Integer or vector of integers
211
+ # @param [LLVM::Value] rhs Integer or vector of integers
212
+ # @param [String] name Name of the result in LLVM IR
213
+ # @return [LLVM::Instruction] The integer difference of the two operands
214
+ # @LLVMinst sub
215
+ def nsw_sub(lhs, rhs, name = "")
216
+ Instruction.from_ptr(C.build_nsw_sub(self, lhs, rhs, name))
217
+ end
218
+
219
+ # No unsigned wrap integer subtraction.
220
+ # @param [LLVM::Value] lhs Integer or vector of integers
221
+ # @param [LLVM::Value] rhs Integer or vector of integers
222
+ # @param [String] name Name of the result in LLVM IR
223
+ # @return [LLVM::Instruction] The integer difference of the two operands
224
+ # @LLVMinst sub
225
+ def nuw_sub(lhs, rhs, name = "")
226
+ Instruction.from_ptr(C.build_nuw_sub(self, lhs, rhs, name))
227
+ end
228
+
229
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
230
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
231
+ # @param [String] name Name of the result in LLVM IR
232
+ # @return [LLVM::Instruction] The floating point difference of the two
233
+ # operands
234
+ # @LLVMinst fsub
235
+ def fsub(lhs, rhs, name = "")
236
+ Instruction.from_ptr(C.build_f_sub(self, lhs, rhs, name))
237
+ end
238
+
239
+ # Integer multiplication.
240
+ # @param [LLVM::Value] lhs Integer or vector of integers
241
+ # @param [LLVM::Value] rhs Integer or vector of integers
242
+ # @param [String] name Name of the result in LLVM IR
243
+ # @return [LLVM::Instruction] The integer product of the two operands
244
+ # @LLVMinst mul
245
+ def mul(lhs, rhs, name = "")
246
+ Instruction.from_ptr(C.build_mul(self, lhs, rhs, name))
247
+ end
248
+
249
+ # "No signed wrap" integer multiplication.
250
+ # @param [LLVM::Value] lhs Integer or vector of integers
251
+ # @param [LLVM::Value] rhs Integer or vector of integers
252
+ # @param [String] name Name of the result in LLVM IR
253
+ # @return [LLVM::Instruction] The integer product of the two operands
254
+ # @LLVMinst mul
255
+ def nsw_mul(lhs, rhs, name = "")
256
+ Instruction.from_ptr(C.build_nsw_mul(self, lhs, rhs, name))
257
+ end
258
+
259
+ # "No unsigned wrap" integer multiplication.
260
+ # @param [LLVM::Value] lhs Integer or vector of integers
261
+ # @param [LLVM::Value] rhs Integer or vector of integers
262
+ # @param [String] name Name of the result in LLVM IR
263
+ # @return [LLVM::Instruction] The integer product of the two operands
264
+ # @LLVMinst mul
265
+ def nuw_mul(lhs, rhs, name = "")
266
+ Instruction.from_ptr(C.build_nuw_mul(self, lhs, rhs, name))
267
+ end
268
+
269
+ # Floating point multiplication
270
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
271
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
272
+ # @param [String] name Name of the result in LLVM IR
273
+ # @return [LLVM::Instruction] The floating point product of the two
274
+ # operands
275
+ # @LLVMinst fmul
276
+ def fmul(lhs, rhs, name = "")
277
+ Instruction.from_ptr(C.build_f_mul(self, lhs, rhs, name))
278
+ end
279
+
280
+ # Unsigned integer division
281
+ # @param [LLVM::Value] lhs Integer or vector of integers
282
+ # @param [LLVM::Value] rhs Integer or vector of integers
283
+ # @param [String] name Name of the result in LLVM IR
284
+ # @return [LLVM::Instruction] The integer quotient of the two operands
285
+ # @LLVMinst udiv
286
+ def udiv(lhs, rhs, name = "")
287
+ Instruction.from_ptr(C.build_u_div(self, lhs, rhs, name))
288
+ end
289
+
290
+ # Signed division
291
+ # @param [LLVM::Value] lhs Integer or vector of integers
292
+ # @param [LLVM::Value] rhs Integer or vector of integers
293
+ # @param [String] name Name of the result in LLVM IR
294
+ # @return [LLVM::Instruction] The integer quotient of the two operands
295
+ # @LLVMinst sdiv
296
+ def sdiv(lhs, rhs, name = "")
297
+ Instruction.from_ptr(C.build_s_div(self, lhs, rhs, name))
298
+ end
299
+
300
+ # Signed exact division
301
+ # @param [LLVM::Value] lhs Integer or vector of integers
302
+ # @param [LLVM::Value] rhs Integer or vector of integers
303
+ # @param [String] name Name of the result in LLVM IR
304
+ # @return [LLVM::Instruction] The integer quotient of the two operands
305
+ # @LLVMinst sdiv
306
+ def exact_sdiv(lhs, rhs, name = "")
307
+ Instruction.from_ptr(C.build_exact_s_div(self, lhs, rhs, name))
308
+ end
309
+
310
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
311
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
312
+ # @param [String] name Name of the result in LLVM IR
313
+ # @return [LLVM::Instruction] The floating point quotient of the two
314
+ # operands
315
+ # @LLVMinst fdiv
316
+ def fdiv(lhs, rhs, name = "")
317
+ Instruction.from_ptr(C.build_f_div(self, lhs, rhs, name))
318
+ end
319
+
320
+ # Unsigned remainder
321
+ # @param [LLVM::Value] lhs Integer or vector of integers
322
+ # @param [LLVM::Value] rhs Integer or vector of integers
323
+ # @param [String] name Name of the result in LLVM IR
324
+ # @return [LLVM::Instruction] The integer remainder
325
+ # @LLVMinst urem
326
+ def urem(lhs, rhs, name = "")
327
+ Instruction.from_ptr(C.build_u_rem(self, lhs, rhs, name))
328
+ end
329
+
330
+ # Signed remainder
331
+ # @param [LLVM::Value] lhs Integer or vector of integers
332
+ # @param [LLVM::Value] rhs Integer or vector of integers
333
+ # @param [String] name Name of the result in LLVM IR
334
+ # @return [LLVM::Instruction] The integer remainder
335
+ # @LLVMinst srem
336
+ def srem(lhs, rhs, name = "")
337
+ Instruction.from_ptr(C.build_s_rem(self, lhs, rhs, name))
338
+ end
339
+
340
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
341
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
342
+ # @param [String] name Name of the result in LLVM IR
343
+ # @return [LLVM::Instruction] The floating point remainder
344
+ # @LLVMinst frem
345
+ def frem(lhs, rhs, name = "")
346
+ Instruction.from_ptr(C.build_f_rem(self, lhs, rhs, name))
347
+ end
348
+
349
+ # @param [LLVM::Value] lhs Integer or vector of integers
350
+ # @param [LLVM::Value] rhs Integer or vector of integers
351
+ # @param [String] name Name of the result in LLVM IR
352
+ # @return [LLVM::Instruction] An integer instruction
353
+ # @LLVMinst shl
354
+ def shl(lhs, rhs, name = "")
355
+ Instruction.from_ptr(C.build_shl(self, lhs, rhs, name))
356
+ end
357
+
358
+ # Shifts right with zero fill.
359
+ # @param [LLVM::Value] lhs Integer or vector of integers
360
+ # @param [LLVM::Value] rhs Integer or vector of integers
361
+ # @param [String] name Name of the result in LLVM IR
362
+ # @return [LLVM::Instruction] An integer instruction
363
+ # @LLVMinst lshr
364
+ def lshr(lhs, rhs, name = "")
365
+ Instruction.from_ptr(C.build_l_shr(self, lhs, rhs, name))
366
+ end
367
+
368
+ # Arithmatic shift right.
369
+ # @param [LLVM::Value] lhs Integer or vector of integers
370
+ # @param [LLVM::Value] rhs Integer or vector of integers
371
+ # @param [String] name Name of the result in LLVM IR
372
+ # @return [LLVM::Instruction] An integer instruction
373
+ # @LLVMinst ashr
374
+ def ashr(lhs, rhs, name = "")
375
+ Instruction.from_ptr(C.build_a_shr(self, lhs, rhs, name))
376
+ end
377
+
378
+ # @param [LLVM::Value] lhs Integer or vector of integers
379
+ # @param [LLVM::Value] rhs Integer or vector of integers
380
+ # @param [String] name Name of the result in LLVM IR
381
+ # @return [LLVM::Instruction] An integer instruction
382
+ # @LLVMinst and
383
+ def and(lhs, rhs, name = "")
384
+ Instruction.from_ptr(C.build_and(self, lhs, rhs, name))
385
+ end
386
+
387
+ # @param [LLVM::Value] lhs Integer or vector of integers
388
+ # @param [LLVM::Value] rhs Integer or vector of integers
389
+ # @param [String] name Name of the result in LLVM IR
390
+ # @return [LLVM::Instruction] An integer instruction
391
+ # @LLVMinst or
392
+ def or(lhs, rhs, name = "")
393
+ Instruction.from_ptr(C.build_or(self, lhs, rhs, name))
394
+ end
395
+
396
+ # @param [LLVM::Value] lhs Integer or vector of integers
397
+ # @param [LLVM::Value] rhs Integer or vector of integers
398
+ # @param [String] name Name of the result in LLVM IR
399
+ # @return [LLVM::Instruction] An integer instruction
400
+ # @LLVMinst xor
401
+ def xor(lhs, rhs, name = "")
402
+ Instruction.from_ptr(C.build_xor(self, lhs, rhs, name))
403
+ end
404
+
405
+ # Integer negation. Implemented as a shortcut to the equivalent sub
406
+ # instruction.
407
+ # @param [LLVM::Value] arg Integer or vector of integers
408
+ # @param [String] name Name of the result in LLVM IR
409
+ # @return [LLVM::Instruction] The negated operand
410
+ # @LLVMinst sub
411
+ def neg(arg, name = "")
412
+ Instruction.from_ptr(C.build_neg(self, arg, name))
413
+ end
414
+
415
+ # "No signed wrap" integer negation.
416
+ # @param [LLVM::Value] arg Integer or vector of integers
417
+ # @param [String] name Name of the result in LLVM IR
418
+ # @return [LLVM::Instruction] The negated operand
419
+ # @LLVMinst sub
420
+ def nsw_neg(arg, name = "")
421
+ Instruction.from_ptr(C.build_nsw_neg(self, arg, name))
422
+ end
423
+
424
+ # "No unsigned wrap" integer negation.
425
+ # @param [LLVM::Value] arg Integer or vector of integers
426
+ # @param [String] name Name of the result in LLVM IR
427
+ # @return [LLVM::Instruction] The negated operand
428
+ # @LLVMinst sub
429
+ def nuw_neg(arg, name = "")
430
+ Instruction.from_ptr(C.build_nuw_neg(self, arg, name))
431
+ end
432
+
433
+ # Boolean negation.
434
+ # @param [LLVM::Value] arg Integer or vector of integers
435
+ # @param [String] name The name of the result in LLVM IR
436
+ # @return [LLVM::Instruction] The negated operand
437
+ def not(arg, name = "")
438
+ Instruction.from_ptr(C.build_not(self, arg, name))
439
+ end
440
+
441
+ # @param [LLVM::Type, #type] ty The type or value whose type
442
+ # should be malloced
443
+ # @param [String] name The name of the result in LLVM IR
444
+ # @return [LLVM::Instruction] A pointer to the malloced bytes
445
+ def malloc(ty, name = "")
446
+ Instruction.from_ptr(C.build_malloc(self, LLVM::Type(ty), name))
447
+ end
448
+
449
+ # @param [LLVM::Type, #type] ty The type or value whose type will be the
450
+ # element type of the malloced array
451
+ # @param [LLVM::Value] sz Unsigned integer representing size of the array
452
+ # @param [String] name The name of the result in LLVM IR
453
+ # @return [LLVM::Instruction] A pointer to the malloced array
454
+ def array_malloc(ty, sz, name = "")
455
+ Instruction.from_ptr(C.build_array_malloc(self, LLVM::Type(ty), sz, name))
456
+ end
457
+
458
+ # Stack allocation.
459
+ # @param [LLVM::Type, #type] ty The type or value whose type should be
460
+ # allocad
461
+ # @param [String] name The name of the result in LLVM IR
462
+ # @return [LLVM::Instruction] A pointer to the allocad bytes
463
+ # @LLVMinst alloca
464
+ def alloca(ty, name = "")
465
+ Instruction.from_ptr(C.build_alloca(self, LLVM::Type(ty), name))
466
+ end
467
+
468
+ # Array stack allocation
469
+ # @param [LLVM::Type, #type] ty The type or value whose type will be the
470
+ # element type of the allocad array
471
+ # @param [LLVM::Value] sz Unsigned integer representing size of the array
472
+ # @param [String] name The name of the result in LLVM IR
473
+ # @return [LLVM::Instruction] A pointer to the allocad array
474
+ # @LLVMinst alloca
475
+ def array_alloca(ty, sz, name = "")
476
+ Instruction.from_ptr(C.build_array_alloca(self, LLVM::Type(ty), sz, name))
477
+ end
478
+
479
+ # @param [LLVM::Value] ptr The pointer to be freed
480
+ # @return [LLVM::Instruction] The result of the free instruction
481
+ def free(ptr)
482
+ Instruction.from_ptr(C.build_free(self, ptr))
483
+ end
484
+
485
+ # Load the value of a given pointer
486
+ # @param [LLVM::Value] ptr The pointer to be loaded
487
+ # @param [String] name The name of the result in LLVM IR
488
+ # @return [LLVM::Instruction] The result of the load operation. Represents
489
+ # a value of the pointer's type.
490
+ # @LLVMinst load
491
+ def load(ptr, name = "")
492
+ Instruction.from_ptr(C.build_load(self, ptr, name))
493
+ end
494
+
495
+ # Store a value at a given pointer
496
+ # @param [LLVM::Value] val The value to be stored
497
+ # @param [LLVM::Value] ptr A pointer to the same type as val
498
+ # @return [LLVM::Instruction] The result of the store operation
499
+ # @LLVMinst store
500
+ def store(val, ptr)
501
+ raise "val must be a Value, got #{val.class.name}" unless Value === val
502
+ Instruction.from_ptr(C.build_store(self, val, ptr))
503
+ end
504
+
505
+ # Obtain a pointer to the element at the given indices
506
+ # @param [LLVM::Value] ptr A pointer to an aggregate value
507
+ # @param [Array<LLVM::Value>] indices Ruby array of LLVM::Value representing
508
+ # indices into the aggregate
509
+ # @param [String] name The name of the result in LLVM IR
510
+ # @return [LLVM::Instruction] The resulting pointer
511
+ # @LLVMinst gep
512
+ # @see http://llvm.org/docs/GetElementPtr.html
513
+ def gep(ptr, indices, name = "")
514
+ indices = Array(indices)
515
+ FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
516
+ indices_ptr.write_array_of_pointer(indices)
517
+ return Instruction.from_ptr(
518
+ C.build_gep(self, ptr, indices_ptr, indices.size, name))
519
+ end
520
+ end
521
+
522
+ # Builds a inbounds getelementptr instruction. If the indices are outside
523
+ # the allocated pointer the value is undefined.
524
+ # @param [LLVM::Value] ptr A pointer to an aggregate value
525
+ # @param [Array<LLVM::Value>] indices Ruby array of LLVM::Value representing
526
+ # indices into the aggregate
527
+ # @param [String] name The name of the result in LLVM IR
528
+ # @return [LLVM::Instruction] The resulting pointer
529
+ # @LLVMinst gep
530
+ # @see http://llvm.org/docs/GetElementPtr.html
531
+ def inbounds_gep(ptr, indices, name = "")
532
+ indices = Array(indices)
533
+ FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
534
+ indices_ptr.write_array_of_pointer(indices)
535
+ return Instruction.from_ptr(
536
+ C.build_in_bounds_gep(self, ptr, indices_ptr, indices.size, name))
537
+ end
538
+ end
539
+
540
+ # Builds a struct getelementptr Instruction.
541
+ #
542
+ # @param [LLVM::Value] pointer A pointer to a structure
543
+ # @param [LLVM::Value] idx Unsigned integer representing the index of a
544
+ # structure member
545
+ # @param [String] name The name of the result in LLVM IR
546
+ # @return [LLVM::Instruction] The resulting pointer
547
+ # @LLVMinst gep
548
+ # @see http://llvm.org/docs/GetElementPtr.html
549
+ def struct_gep(pointer, idx, name = "")
550
+ Instruction.from_ptr(C.build_struct_gep(self, pointer, idx, name))
551
+ end
552
+
553
+ # Creates a global string initialized to a given value.
554
+ # @param [String] string The string used by the initialize
555
+ # @param [Name] name Name of the result in LLVM IR
556
+ # @return [LLVM::Instruction] Reference to the global string
557
+ def global_string(string, name = "")
558
+ Instruction.from_ptr(C.build_global_string(self, string, name))
559
+ end
560
+
561
+ # Creates a pointer to a global string initialized to a given value.
562
+ # @param [String] string The string used by the initializer
563
+ # @param [String] name The name of the result in LLVM IR
564
+ # @return [LLVM::Instruction] Reference to the global string pointer
565
+ def global_string_pointer(string, name = "")
566
+ Instruction.from_ptr(C.build_global_string_ptr(self, string, name))
567
+ end
568
+
569
+ # Truncates its operand to the given type. The size of the value type must
570
+ # be greater than the size of the target type.
571
+ # @param [LLVM::Value] val Integer or vector of integers to be truncated
572
+ # @param [LLVM::Type, #type] ty Integer or vector of integers of equal size
573
+ # to val
574
+ # @param [String] name The name of the result in LLVM IR
575
+ # @return [LLVM::Instruction] The truncated value
576
+ # @LLVMinst trunc
577
+ def trunc(val, ty, name = "")
578
+ Instruction.from_ptr(C.build_trunc(self, val, LLVM::Type(ty), name))
579
+ end
580
+
581
+ # Zero extends its operand to the given type. The size of the value type
582
+ # must be greater than the size of the target type.
583
+ # @param [LLVM::Value] val Integer or vector of integers to be extended
584
+ # @param [LLVM::Type, #type] ty Integer or vector of integer type of
585
+ # greater size than val
586
+ # @param [String] name The name of the result in LLVM IR
587
+ # @return [LLVM::Instruction] The extended value
588
+ # @LLVMinst zext
589
+ def zext(val, ty, name = "")
590
+ Instruction.from_ptr(C.build_z_ext(self, val, LLVM::Type(ty), name))
591
+ end
592
+
593
+ # Sign extension by copying the sign bit (highest order bit) of the value
594
+ # until it reaches the bit size of the given type.
595
+ # @param [LLVM::Value] val Integer or vector of integers to be extended
596
+ # @param [LLVM::Type] ty Integer or vector of integer type of greater size
597
+ # than the size of val
598
+ # @param [String] name The name of the result in LLVM IR
599
+ # @return [LLVM::Instruction] The extended value
600
+ # @LLVMinst sext
601
+ def sext(val, ty, name = "")
602
+ Instruction.from_ptr(C.build_s_ext(self, val, LLVM::Type(ty), name))
603
+ end
604
+
605
+ # Convert a floating point to an unsigned integer
606
+ # @param [LLVM::Value] val Floating point or vector of floating points to
607
+ # convert
608
+ # @param [LLVM::Type, #type] ty Integer or vector of integer target type
609
+ # @param [String] name The name of the result in LLVM IR
610
+ # @return [LLVM::Instruction] The converted value
611
+ # @LLVMinst fptoui
612
+ def fp2ui(val, ty, name = "")
613
+ Instruction.from_ptr(C.build_fp_to_ui(self, val, LLVM::Type(ty), name))
614
+ end
615
+
616
+ # Convert a floating point to a signed integer
617
+ # @param [LLVM::Value] val Floating point or vector of floating points to
618
+ # convert
619
+ # @param [LLVM::Type, #type] ty Integer or vector of integer target type
620
+ # @param [String] name The name of the result in LLVM IR
621
+ # @return [LLVM::Instruction] The converted value
622
+ # @LLVMinst fptosi
623
+ def fp2si(val, ty, name = "")
624
+ Instruction.from_ptr(C.build_fp_to_si(self, val, LLVM::Type(ty), name))
625
+ end
626
+
627
+ # Convert an unsigned integer to a floating point
628
+ # @param [LLVM::Value] val Unsigned integer or vector of unsigned integer
629
+ # to convert
630
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
631
+ # target type
632
+ # @param [String] name The name of the result in LLVM IR
633
+ # @return [LLVM::Instruction] The converted value
634
+ # @LLVMinst uitofp
635
+ def ui2fp(val, ty, name = "")
636
+ Instruction.from_ptr(C.build_ui_to_fp(self, val, LLVM::Type(ty), name))
637
+ end
638
+
639
+ # Convert a signed integer to a floating point
640
+ # @param [LLVM::Value] val Signed integer or vector of signed integer
641
+ # to convert
642
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
643
+ # target type
644
+ # @param [String] name The name of the result in LLVM IR
645
+ # @return [LLVM::Instruction] The converted value
646
+ # @LLVMinst sitofp
647
+ def si2fp(val, ty, name = "")
648
+ Instruction.from_ptr(C.build_si_to_fp(self, val, LLVM::Type(ty), name))
649
+ end
650
+
651
+ # Truncate a floating point value
652
+ # @param [LLVM::Value] val Floating point or vector of floating point
653
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
654
+ # type of lesser size than val's type
655
+ # @param [String] name The name of the result in LLVM IR
656
+ # @return [LLVM::Instruction] The truncated value
657
+ # @LLVMinst fptrunc
658
+ def fp_trunc(val, ty, name = "")
659
+ Instruction.from_ptr(C.build_fp_trunc(self, val, LLVM::Type(ty), name))
660
+ end
661
+
662
+ # Extend a floating point value
663
+ # @param [LLVM::Value] val Floating point or vector of floating point
664
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
665
+ # type of greater size than val's type
666
+ # @param [String] name The name of the result in LLVM IR
667
+ # @return [LLVM::Instruction] The extended value
668
+ # @LLVMinst fpext
669
+ def fp_ext(val, ty, name = "")
670
+ Instruction.from_ptr(C.build_fp_ext(self, val, LLVM::Type(ty), name))
671
+ end
672
+
673
+ # Cast a pointer to an int. Useful for pointer arithmetic.
674
+ # @param [LLVM::Value] val A pointer
675
+ # @param [LLVM::Type, #type] ty An integer type
676
+ # @param [String] name The name of the result in LLVM IR
677
+ # @return [LLVM::Instruction] An integer of the given type representing
678
+ # the pointer's address
679
+ # @LLVMinst ptrtoint
680
+ def ptr2int(val, ty, name = "")
681
+ Instruction.from_ptr(C.build_ptr_to_int(self, val, LLVM::Type(ty), name))
682
+ end
683
+
684
+ # Cast an int to a pointer
685
+ # @param [LLVM::Value] val An integer value
686
+ # @param [LLVM::Type, #ty] ty A pointer type
687
+ # @param [String] name The name of the result in LLVM IR
688
+ # @return [LLVM::Instruction] A pointer of the given type and the address
689
+ # held in val
690
+ # @LLVMinst inttoptr
691
+ def int2ptr(val, ty, name = "")
692
+ Instruction.from_ptr(C.build_int_to_ptr(self, val, LLVM::Type(ty), name))
693
+ end
694
+
695
+ # Cast a value to the given type without changing any bits
696
+ # @param [LLVM::Value] val The value to cast
697
+ # @param [LLVM::Type, #ty] ty The target type
698
+ # @param [String] name The name of the result in LLVM IR
699
+ # @return [LLVM::Instruction] A value of the target type
700
+ # @LLVMinst bitcast
701
+ def bit_cast(val, ty, name = "")
702
+ Instruction.from_ptr(C.build_bit_cast(self, val, LLVM::Type(ty), name))
703
+ end
704
+
705
+ # @param [LLVM::Value] val
706
+ # @param [LLVM::Type, #ty] ty
707
+ # @param [String] name The name of the result in LLVM IR
708
+ # @return [LLVM::Instruction]
709
+ # @LLVMinst zext
710
+ # @LLVMinst bitcast
711
+ def zext_or_bit_cast(val, ty, name = "")
712
+ Instruction.from_ptr(C.build_z_ext_or_bit_cast(self, val, LLVM::Type(ty), name))
713
+ end
714
+
715
+ # @param [LLVM::Value] val
716
+ # @param [LLVM::Type, #ty] ty
717
+ # @param [String] name The name of the result in LLVM IR
718
+ # @return [LLVM::Instruction]
719
+ # @LLVMinst sext
720
+ # @LLVMinst bitcast
721
+ def sext_or_bit_cast(val, ty, name = "")
722
+ Instruction.from_ptr(C.build_s_ext_or_bit_cast(self, val, LLVM::Type(ty), name))
723
+ end
724
+
725
+ # @param [LLVM::Value] val
726
+ # @param [LLVM::Type, #ty] ty
727
+ # @param [String] name The name of the result in LLVM IR
728
+ # @return [LLVM::Instruction]
729
+ # @LLVMinst trunc
730
+ # @LLVMinst bitcast
731
+ def trunc_or_bit_cast(val, ty, name = "")
732
+ Instruction.from_ptr(C.build_trunc_or_bit_cast(self, val, LLVM::Type(ty), name))
733
+ end
734
+
735
+ # @param [LLVM::Value] val
736
+ # @param [LLVM::Type, #ty] ty
737
+ # @param [String] name The name of the result in LLVM IR
738
+ # @return [LLVM::Instruction]
739
+ def pointer_cast(val, ty, name = "")
740
+ Instruction.from_ptr(C.build_pointer_cast(self, val, LLVM::Type(ty), name))
741
+ end
742
+
743
+ # @param [LLVM::Value] val
744
+ # @param [LLVM::Type, #ty] ty
745
+ # @param [String] name The name of the result in LLVM IR
746
+ # @return [LLVM::Instruction]
747
+ def int_cast(val, ty, name = "")
748
+ Instruction.from_ptr(C.build_int_cast(self, val, LLVM::Type(ty), name))
749
+ end
750
+
751
+ # @param [LLVM::Value] val
752
+ # @param [LLVM::Type, #ty] ty
753
+ # @param [String] name The name of the result in LLVM IR
754
+ # @return [LLVM::Instruction]
755
+ def fp_cast(val, ty, name = "")
756
+ Instruction.from_ptr(C.build_fp_cast(self, val, LLVM::Type(ty), name))
757
+ end
758
+
759
+ # Builds an icmp Instruction. Compares lhs to rhs (Instructions)
760
+ # using the given symbol predicate (pred):
761
+ # :eq - equal to
762
+ # :ne - not equal to
763
+ # :ugt - unsigned greater than
764
+ # :uge - unsigned greater than or equal to
765
+ # :ult - unsigned less than
766
+ # :ule - unsigned less than or equal to
767
+ # :sgt - signed greater than
768
+ # :sge - signed greater than or equal to
769
+ # :slt - signed less than
770
+ # :sle - signed less than or equal to
771
+ # @param [Symbol] pred A predicate
772
+ # @param [LLVM::Value] lhs The left hand side of the comparison, of integer
773
+ # or pointer type
774
+ # @param [LLVM::Value] rhs The right hand side of the comparison, of the
775
+ # same type as lhs
776
+ # @param [String] name The name of the result in LLVM IR
777
+ # @return [LLVM::Instruction] A boolean represented as i1
778
+ # @LLVMinst icmp
779
+ def icmp(pred, lhs, rhs, name = "")
780
+ Instruction.from_ptr(C.build_i_cmp(self, pred, lhs, rhs, name))
781
+ end
782
+
783
+ # Builds an fcmp Instruction. Compares lhs to rhs (Instructions) as Reals
784
+ # using the given symbol predicate (pred):
785
+ # :ord - ordered
786
+ # :uno - unordered: isnan(X) | isnan(Y)
787
+ # :oeq - ordered and equal to
788
+ # :oeq - unordered and equal to
789
+ # :one - ordered and not equal to
790
+ # :one - unordered and not equal to
791
+ # :ogt - ordered and greater than
792
+ # :uge - unordered and greater than or equal to
793
+ # :olt - ordered and less than
794
+ # :ule - unordered and less than or equal to
795
+ # :oge - ordered and greater than or equal to
796
+ # :sge - unordered and greater than or equal to
797
+ # :ole - ordered and less than or equal to
798
+ # :sle - unordered and less than or equal to
799
+ # :true - always true and folded
800
+ # :false - always false and folded
801
+ # @param [Symbol] pred A predicate
802
+ # @param [LLVM::Value] lhs The left hand side of the comparison, of
803
+ # floating point type
804
+ # @param [LLVM::Value] rhs The right hand side of the comparison, of
805
+ # the same type as lhs
806
+ # @param [String] name The name of the result in LLVM IR
807
+ # @return [LLVM::Instruction] A boolean represented as i1
808
+ # @LLVMinst fcmp
809
+ def fcmp(pred, lhs, rhs, name = "")
810
+ Instruction.from_ptr(C.build_f_cmp(self, pred, lhs, rhs, name))
811
+ end
812
+
813
+ # Build a Phi node of the given type with the given incoming branches
814
+ # @param [LLVM::Type] ty Specifies the result type
815
+ # @param [Hash{LLVM::BasicBlock => LLVM::Value}] incoming A hash mapping
816
+ # basic blocks to a corresponding value. If the phi node is jumped to
817
+ # from a given basic block, the phi instruction takes on its
818
+ # corresponding value.
819
+ # @param [String] name The name of the result in LLVM IR
820
+ # @return [LLVM::Instruction] The phi node
821
+ # @LLVMinst phi
822
+ def phi(ty, incoming, name = "")
823
+ phi = Phi.from_ptr(C.build_phi(self, LLVM::Type(ty), name))
824
+ phi.add_incoming(incoming)
825
+ phi
826
+ end
827
+
828
+ # Builds a call Instruction. Calls the given Function with the given
829
+ # args (Instructions).
830
+ #
831
+ # @param [LLVM::Function] fun
832
+ # @param [Array<LLVM::Value>] args
833
+ # @param [LLVM::Instruction]
834
+ # @LLVMinst call
835
+ def call(fun, *args)
836
+ raise "No fun" if fun.nil?
837
+ if args.last.kind_of? String
838
+ name = args.pop
839
+ else
840
+ name = ""
841
+ end
842
+
843
+ args_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * args.size)
844
+ args_ptr.write_array_of_pointer(args)
845
+ CallInst.from_ptr(C.build_call(self, fun, args_ptr, args.size, name))
846
+ end
847
+
848
+ # Return a value based on a condition. This differs from 'cond' in that
849
+ # its operands are values rather than basic blocks. As a consequence, both
850
+ # arguments must be evaluated.
851
+ # @param [LLVM::Value] _if An i1 or a vector of i1
852
+ # @param [LLVM::Value] _then A value or vector of the same arity as _if
853
+ # @param [LLVM::Value] _else A value or vector of values of the same arity
854
+ # as _if, and of the same type as _then
855
+ # @param [String] name The name of the result in LLVM IR
856
+ # @return [LLVM::Instruction] An instruction representing either _then or
857
+ # _else
858
+ # @LLVMinst select
859
+ def select(_if, _then, _else, name = "")
860
+ Instruction.from_ptr(C.build_select(self, _if, _then, _else, name))
861
+ end
862
+
863
+ # Extract an element from a vector
864
+ # @param [LLVM::Value] vector The vector from which to extract a value
865
+ # @param [LLVM::Value] idx The index of the element to extract, an
866
+ # unsigned integer
867
+ # @param [String] name The value of the result in LLVM IR
868
+ # @return [LLVM::Instruction] The extracted element
869
+ # @LLVMinst extractelement
870
+ def extract_element(vector, idx, name = "")
871
+ Instruction.from_ptr(C.build_extract_element(self, vector, idx, name))
872
+ end
873
+
874
+ # Insert an element into a vector
875
+ # @param [LLVM::Value] vector The vector into which to insert the element
876
+ # @param [LLVM::Value] elem The element to be inserted into the vector
877
+ # @param [LLVM::Value] idx The index at which to insert the element
878
+ # @param [String] name The name of the result in LLVM IR
879
+ # @return [LLVM::Instruction] A vector the same type as 'vector'
880
+ # @LLVMinst insertelement
881
+ def insert_element(vector, elem, idx, name = "")
882
+ Instruction.from_ptr(C.build_insert_element(self, vector, elem, idx, name))
883
+ end
884
+
885
+ # Shuffle two vectors according to a given mask
886
+ # @param [LLVM::Value] vec1 A vector
887
+ # @param [LLVM::Value] vec2 A vector of the same type and arity as vec1
888
+ # @param [LLVM::Value] mask A vector of i1 of the same arity as vec1 and
889
+ # vec2
890
+ # @param [String] name The name of the result in LLVM IR
891
+ # @return [LLVM::Instruction] The shuffled vector
892
+ # @LLVMinst shufflevector
893
+ def shuffle_vector(vec1, vec2, mask, name = "")
894
+ Instruction.from_ptr(C.build_shuffle_vector(self, vec1, vec2, mask, name))
895
+ end
896
+
897
+ # Extract the value of a member field from an aggregate value
898
+ # @param [LLVM::Value] aggregate An aggregate value
899
+ # @param [LLVM::Value] idx The index of the member to extract
900
+ # @param [String] name The name of the result in LLVM IR
901
+ # @return [LLVM::Instruction] The extracted value
902
+ # @LLVMinst extractvalue
903
+ def extract_value(aggregate, idx, name = "")
904
+ Instruction.from_ptr(C.build_extract_value(self, aggregate, idx, name))
905
+ end
906
+
907
+ # Insert a value into an aggregate value's member field
908
+ # @param [LLVM::Value] aggregate An aggregate value
909
+ # @param [LLVM::Value] elem The value to insert into 'aggregate'
910
+ # @param [LLVM::Value] idx The index at which to insert the value
911
+ # @param [String] name The name of the result in LLVM IR
912
+ # @return [LLVM::Instruction] An aggregate value of the same type as 'aggregate'
913
+ # @LLVMinst insertvalue
914
+ def insert_value(aggregate, elem, idx, name = "")
915
+ Instruction.from_ptr(C.build_insert_value(self, aggregate, elem, idx, name))
916
+ end
917
+
918
+ # Check if a value is null
919
+ # @param [LLVM::Value] val The value to check
920
+ # @param [String] name The name of the result in LLVM IR
921
+ # @return [LLVM::Instruction] An i1
922
+ def is_null(val, name = "")
923
+ Instruction.from_ptr(C.build_is_null(self, val, name))
924
+ end
925
+
926
+ # Check if a value is not null
927
+ # @param [LLVM::Value] val The value to check
928
+ # @param [String] name The name of the result in LLVM IR
929
+ # @return [LLVM::Instruction] An i1
930
+ def is_not_null(val, name = "")
931
+ Instruction.from_ptr(C.build_is_not_null(self, val, name))
932
+ end
933
+
934
+ # Calculate the difference between two pointers
935
+ # @param [LLVM::Value] lhs A pointer
936
+ # @param [LLVM::Value] rhs A pointer
937
+ # @param [String] name The name of the result in LLVM IR
938
+ # @return [LLVM::Instruction] The integer difference between the two
939
+ # pointers
940
+ def ptr_diff(lhs, rhs, name = "")
941
+ Instruction.from_ptr(C.build_ptr_diff(lhs, rhs, name))
942
+ end
943
+ end
944
+ end