waj-ruby-llvm 2.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README.rdoc +31 -0
  2. data/ext/ruby-llvm-support/Makefile.am +1 -0
  3. data/ext/ruby-llvm-support/Makefile.in +612 -0
  4. data/ext/ruby-llvm-support/config.guess +1500 -0
  5. data/ext/ruby-llvm-support/config.sub +1616 -0
  6. data/ext/ruby-llvm-support/configure +17190 -0
  7. data/ext/ruby-llvm-support/configure.ac +16 -0
  8. data/ext/ruby-llvm-support/depcomp +584 -0
  9. data/ext/ruby-llvm-support/install-sh +507 -0
  10. data/ext/ruby-llvm-support/libtool +9403 -0
  11. data/ext/ruby-llvm-support/ltmain.sh +8745 -0
  12. data/ext/ruby-llvm-support/missing +367 -0
  13. data/ext/ruby-llvm-support/src/Makefile.am +5 -0
  14. data/ext/ruby-llvm-support/src/Makefile.in +472 -0
  15. data/ext/ruby-llvm-support/src/support.cpp +12 -0
  16. data/lib/llvm.rb +12 -0
  17. data/lib/llvm/analysis.rb +62 -0
  18. data/lib/llvm/core.rb +598 -0
  19. data/lib/llvm/core/bitcode.rb +88 -0
  20. data/lib/llvm/core/builder.rb +851 -0
  21. data/lib/llvm/core/context.rb +22 -0
  22. data/lib/llvm/core/module.rb +232 -0
  23. data/lib/llvm/core/pass_manager.rb +76 -0
  24. data/lib/llvm/core/type.rb +173 -0
  25. data/lib/llvm/core/value.rb +782 -0
  26. data/lib/llvm/execution_engine.rb +169 -0
  27. data/lib/llvm/support.rb +22 -0
  28. data/lib/llvm/target.rb +9 -0
  29. data/lib/llvm/transforms/ipo.rb +23 -0
  30. data/lib/llvm/transforms/scalar.rb +136 -0
  31. data/test/array_test.rb +38 -0
  32. data/test/basic_block_test.rb +88 -0
  33. data/test/basic_test.rb +11 -0
  34. data/test/binary_operations_test.rb +58 -0
  35. data/test/bitcode_test.rb +25 -0
  36. data/test/branch_test.rb +57 -0
  37. data/test/call_test.rb +82 -0
  38. data/test/comparisons_test.rb +66 -0
  39. data/test/conversions_test.rb +86 -0
  40. data/test/double_test.rb +33 -0
  41. data/test/equality_test.rb +91 -0
  42. data/test/generic_value_test.rb +22 -0
  43. data/test/instruction_test.rb +32 -0
  44. data/test/ipo_test.rb +53 -0
  45. data/test/memory_access_test.rb +38 -0
  46. data/test/module_test.rb +21 -0
  47. data/test/parameter_collection_test.rb +28 -0
  48. data/test/phi_test.rb +33 -0
  49. data/test/select_test.rb +22 -0
  50. data/test/struct_test.rb +75 -0
  51. data/test/test_helper.rb +50 -0
  52. data/test/type_test.rb +15 -0
  53. data/test/vector_test.rb +64 -0
  54. metadata +133 -0
@@ -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
@@ -0,0 +1,851 @@
1
+ module LLVM
2
+ class Builder
3
+ def initialize
4
+ @ptr = C.LLVMCreateBuilder()
5
+ end
6
+
7
+ # @private
8
+ def to_ptr
9
+ @ptr
10
+ end
11
+
12
+ # Position the builder at the given Instruction within the given BasicBlock.
13
+ # @param [LLVM::BasicBlock]
14
+ # @param [LLVM::Instruction]
15
+ # @return [LLVM::Builder]
16
+ def position(block, instruction)
17
+ raise "Block must not be nil" if block.nil?
18
+ C.LLVMPositionBuilder(self, block, instruction)
19
+ self
20
+ end
21
+
22
+ # Positions the builder before the given Instruction.
23
+ # @param [LLVM::Instruction]
24
+ # @return [LLVM::Builder]
25
+ def position_before(instruction)
26
+ raise "Instruction must not be nil" if instruction.nil?
27
+ C.LLVMPositionBuilderBefore(self, instruction)
28
+ self
29
+ end
30
+
31
+ # Positions the builder at the end of the given BasicBlock.
32
+ # @param [LLVM::BasicBlock]
33
+ # @return [LLVM::Builder]
34
+ def position_at_end(block)
35
+ raise "Block must not be nil" if block.nil?
36
+ C.LLVMPositionBuilderAtEnd(self, block)
37
+ self
38
+ end
39
+
40
+ # The BasicBlock at which the Builder is currently positioned.
41
+ # @return [LLVM::BasicBlock]
42
+ def insert_block
43
+ BasicBlock.from_ptr(C.LLVMGetInsertBlock(self))
44
+ end
45
+
46
+ # @return [LLVM::Instruction]
47
+ # @LLVMinst ret
48
+ def ret_void
49
+ Instruction.from_ptr(C.LLVMBuildRetVoid(self))
50
+ end
51
+
52
+ # @param [LLVM::Value] val The value to return
53
+ # @return [LLVM::Instruction]
54
+ # @LLVMinst ret
55
+ def ret(val)
56
+ Instruction.from_ptr(C.LLVMBuildRet(self, val))
57
+ end
58
+
59
+ # Builds a ret instruction returning multiple values.
60
+ # @param [Array<LLVM::Value>] vals
61
+ # @return [LLVM::Instruction]
62
+ # @LLVMinst ret
63
+ def aggregate_ret(*vals)
64
+ FFI::MemoryPointer.new(FFI.type_size(:pointer) * vals.size) do |vals_ptr|
65
+ vals_ptr.write_array_of_pointer(vals)
66
+ Instruction.from_ptr(C.LLVMBuildAggregateRet(self, vals_ptr, vals.size))
67
+ end
68
+ end
69
+
70
+ # Unconditional branching (i.e. goto)
71
+ # @param [LLVM::BasicBlock] block Where to jump
72
+ # @return [LLVM::Instruction]
73
+ # @LLVMinst br
74
+ def br(block)
75
+ Instruction.from_ptr(
76
+ C.LLVMBuildBr(self, block))
77
+ end
78
+
79
+ # Conditional branching (i.e. if)
80
+ # @param [LLVM::Value] cond The condition
81
+ # @param [LLVM::BasicBlock] iftrue Where to jump if condition is true
82
+ # @param [LLVM::BasicBlock] iffalse Where to jump if condition is false
83
+ # @return [LLVM::Instruction]
84
+ # @LLVMinst br
85
+ def cond(cond, iftrue, iffalse)
86
+ Instruction.from_ptr(
87
+ C.LLVMBuildCondBr(self, cond, iftrue, iffalse))
88
+ end
89
+
90
+ # @LLVMinst switch
91
+ # @param [LLVM::Value] val The value to switch on
92
+ # @param [LLVM::BasicBlock] default The default case
93
+ # @param [Hash{LLVM::Value => LLVM::BasicBlock}] cases A Hash mapping
94
+ # values to basic blocks. When a value is matched, control will jump
95
+ # to the corresponding basic block.
96
+ # @return [LLVM::Instruction]
97
+ def switch(val, default, cases)
98
+ inst = SwitchInst.from_ptr(C.LLVMBuildSwitch(self, val, default, cases.size))
99
+ cases.each do |(val, block)|
100
+ inst.add_case(val, block)
101
+ end
102
+ inst
103
+ end
104
+
105
+ # Invoke a function which may potentially unwind
106
+ # @param [LLVM::Function] fun The function to invoke
107
+ # @param [Array<LLVM::Value>] args Arguments passed to fun
108
+ # @param [LLVM::BasicBlock] normal Where to jump if fun does not unwind
109
+ # @param [LLVM::BasicBlock] exception Where to jump if fun unwinds
110
+ # @param [String] name Name of the result in LLVM IR
111
+ # @return [LLVM::Instruction] The value returned by 'fun', unless an
112
+ # unwind instruction occurs
113
+ # @LLVMinst invoke
114
+ def invoke(fun, args, normal, exception, name = "")
115
+ Instruction.from_ptr(
116
+ C.LLVMBuildInvoke(self,
117
+ fun, args, args.size, normal, exception, name))
118
+ end
119
+
120
+ # Builds an unwind Instruction.
121
+ # @return [LLVM::Instruction]
122
+ # @LLVMinst unwind
123
+ def unwind
124
+ Instruction.from_ptr(C.LLVMBuildUnwind(self))
125
+ end
126
+
127
+ # Generates an instruction with no defined semantics. Can be used to
128
+ # provide hints to the optimizer.
129
+ # @return [LLVM::Instruction]
130
+ # @LLVMinst unreachable
131
+ def unreachable
132
+ Instruction.from_ptr(C.LLVMBuildUnreachable(self))
133
+ end
134
+
135
+ # @param [LLVM::Value] lhs Integer or vector of integers
136
+ # @param [LLVM::Value] rhs Integer or vector of integers
137
+ # @param [String] name Name of the result in LLVM IR
138
+ # @return [LLVM::Instruction] The integer sum of the two operands
139
+ # @LLVMinst add
140
+ def add(lhs, rhs, name = "")
141
+ Instruction.from_ptr(C.LLVMBuildAdd(self, lhs, rhs, name))
142
+ end
143
+
144
+ # No signed wrap addition.
145
+ # @param [LLVM::Value] lhs Integer or vector of integers
146
+ # @param [LLVM::Value] rhs Integer or vector of integers
147
+ # @param [String] name Name of the result in LLVM IR
148
+ # @return [LLVM::Instruction] The integer sum of the two operands
149
+ # @LLVMinst add
150
+ def nsw_add(lhs, rhs, name = "")
151
+ Instruction.from_ptr(C.LLVMBuildNSWAdd(self, lhs, rhs, name))
152
+ end
153
+
154
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
155
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
156
+ # @param [String] name Name of the result in LLVM IR
157
+ # @return [LLVM::Instruction] The floating point sum of the two operands
158
+ # @LLVMinst fadd
159
+ def fadd(lhs, rhs, name = "")
160
+ Instruction.from_ptr(C.LLVMBuildFAdd(self, lhs, rhs, name))
161
+ end
162
+
163
+ # @param [LLVM::Value] lhs Integer or vector of integers
164
+ # @param [LLVM::Value] rhs Integer or vector of integers
165
+ # @param [String] name Name of the result in LLVM IR
166
+ # @return [LLVM::Instruction] The integer difference of the two operands
167
+ # @LLVMinst sub
168
+ def sub(lhs, rhs, name = "")
169
+ Instruction.from_ptr(C.LLVMBuildSub(self, lhs, rhs, name))
170
+ end
171
+
172
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
173
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
174
+ # @param [String] name Name of the result in LLVM IR
175
+ # @return [LLVM::Instruction] The floating point difference of the two
176
+ # operands
177
+ # @LLVMinst fsub
178
+ def fsub(lhs, rhs, name = "")
179
+ Instruction.from_ptr(C.LLVMBuildFSub(self, lhs, rhs, name))
180
+ end
181
+
182
+ # @param [LLVM::Value] lhs Integer or vector of integers
183
+ # @param [LLVM::Value] rhs Integer or vector of integers
184
+ # @param [String] name Name of the result in LLVM IR
185
+ # @return [LLVM::Instruction] The integer product of the two operands
186
+ # @LLVMinst mul
187
+ def mul(lhs, rhs, name = "")
188
+ Instruction.from_ptr(C.LLVMBuildMul(self, lhs, rhs, name))
189
+ end
190
+
191
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
192
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
193
+ # @param [String] name Name of the result in LLVM IR
194
+ # @return [LLVM::Instruction] The floating point product of the two
195
+ # operands
196
+ # @LLVMinst fmul
197
+ def fmul(lhs, rhs, name = "")
198
+ Instruction.from_ptr(C.LLVMBuildFMul(self, lhs, rhs, name))
199
+ end
200
+
201
+ # Unsigned integer division
202
+ # @param [LLVM::Value] lhs Integer or vector of integers
203
+ # @param [LLVM::Value] rhs Integer or vector of integers
204
+ # @param [String] name Name of the result in LLVM IR
205
+ # @return [LLVM::Instruction] The integer quotient of the two operands
206
+ # @LLVMinst udiv
207
+ def udiv(lhs, rhs, name = "")
208
+ Instruction.from_ptr(C.LLVMBuildUDiv(self, lhs, rhs, name))
209
+ end
210
+
211
+ # Signed division
212
+ # @param [LLVM::Value] lhs Integer or vector of integers
213
+ # @param [LLVM::Value] rhs Integer or vector of integers
214
+ # @param [String] name Name of the result in LLVM IR
215
+ # @return [LLVM::Instruction] The integer quotient of the two operands
216
+ # @LLVMinst sdiv
217
+ def sdiv(lhs, rhs, name = "")
218
+ Instruction.from_ptr(C.LLVMBuildSDiv(self, lhs, rhs, name))
219
+ end
220
+
221
+ # Signed exact division
222
+ # @param [LLVM::Value] lhs Integer or vector of integers
223
+ # @param [LLVM::Value] rhs Integer or vector of integers
224
+ # @param [String] name Name of the result in LLVM IR
225
+ # @return [LLVM::Instruction] The integer quotient of the two operands
226
+ # @LLVMinst sdiv
227
+ def exact_sdiv(lhs, rhs, name = "")
228
+ Instruction.from_ptr(C.LLVMBuildExactSDiv(self, lhs, rhs, name))
229
+ end
230
+
231
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
232
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
233
+ # @param [String] name Name of the result in LLVM IR
234
+ # @return [LLVM::Instruction] The floating point quotient of the two
235
+ # operands
236
+ # @LLVMinst fdiv
237
+ def fdiv(lhs, rhs, name = "")
238
+ Instruction.from_ptr(C.LLVMBuildFDiv(self, lhs, rhs, name))
239
+ end
240
+
241
+ # Unsigned remainder
242
+ # @param [LLVM::Value] lhs Integer or vector of integers
243
+ # @param [LLVM::Value] rhs Integer or vector of integers
244
+ # @param [String] name Name of the result in LLVM IR
245
+ # @return [LLVM::Instruction] The integer remainder
246
+ # @LLVMinst urem
247
+ def urem(lhs, rhs, name = "")
248
+ Instruction.from_ptr(C.LLVMBuildURem(self, lhs, rhs, name))
249
+ end
250
+
251
+ # Signed remainder
252
+ # @param [LLVM::Value] lhs Integer or vector of integers
253
+ # @param [LLVM::Value] rhs Integer or vector of integers
254
+ # @param [String] name Name of the result in LLVM IR
255
+ # @return [LLVM::Instruction] The integer remainder
256
+ # @LLVMinst srem
257
+ def srem(lhs, rhs, name = "")
258
+ Instruction.from_ptr(C.LLVMBuildSRem(self, lhs, rhs, name))
259
+ end
260
+
261
+ # @param [LLVM::Value] lhs Floating point or vector of floating points
262
+ # @param [LLVM::Value] rhs Floating point or vector of floating points
263
+ # @param [String] name Name of the result in LLVM IR
264
+ # @return [LLVM::Instruction] The floating point remainder
265
+ # @LLVMinst frem
266
+ def frem(lhs, rhs, name = "")
267
+ Instruction.from_ptr(C.LLVMBuildFRem(self, lhs, rhs, name))
268
+ end
269
+
270
+ # @param [LLVM::Value] lhs Integer or vector of integers
271
+ # @param [LLVM::Value] rhs Integer or vector of integers
272
+ # @param [String] name Name of the result in LLVM IR
273
+ # @return [LLVM::Instruction] An integer instruction
274
+ # @LLVMinst shl
275
+ def shl(lhs, rhs, name = "")
276
+ Instruction.from_ptr(C.LLVMBuildShl(self, lhs, rhs, name))
277
+ end
278
+
279
+ # Shifts right with zero fill.
280
+ # @param [LLVM::Value] lhs Integer or vector of integers
281
+ # @param [LLVM::Value] rhs Integer or vector of integers
282
+ # @param [String] name Name of the result in LLVM IR
283
+ # @return [LLVM::Instruction] An integer instruction
284
+ # @LLVMinst lshr
285
+ def lshr(lhs, rhs, name = "")
286
+ Instruction.from_ptr(C.LLVMBuildLShr(self, lhs, rhs, name))
287
+ end
288
+
289
+ # Arithmatic shift right.
290
+ # @param [LLVM::Value] lhs Integer or vector of integers
291
+ # @param [LLVM::Value] rhs Integer or vector of integers
292
+ # @param [String] name Name of the result in LLVM IR
293
+ # @return [LLVM::Instruction] An integer instruction
294
+ # @LLVMinst ashr
295
+ def ashr(lhs, rhs, name = "")
296
+ Instruction.from_ptr(C.LLVMBuildAShr(self, lhs, rhs, name))
297
+ end
298
+
299
+ # @param [LLVM::Value] lhs Integer or vector of integers
300
+ # @param [LLVM::Value] rhs Integer or vector of integers
301
+ # @param [String] name Name of the result in LLVM IR
302
+ # @return [LLVM::Instruction] An integer instruction
303
+ # @LLVMinst and
304
+ def and(lhs, rhs, name = "")
305
+ Instruction.from_ptr(C.LLVMBuildAnd(self, lhs, rhs, name))
306
+ end
307
+
308
+ # @param [LLVM::Value] lhs Integer or vector of integers
309
+ # @param [LLVM::Value] rhs Integer or vector of integers
310
+ # @param [String] name Name of the result in LLVM IR
311
+ # @return [LLVM::Instruction] An integer instruction
312
+ # @LLVMinst or
313
+ def or(lhs, rhs, name = "")
314
+ Instruction.from_ptr(C.LLVMBuildOr(self, lhs, rhs, name))
315
+ end
316
+
317
+ # @param [LLVM::Value] lhs Integer or vector of integers
318
+ # @param [LLVM::Value] rhs Integer or vector of integers
319
+ # @param [String] name Name of the result in LLVM IR
320
+ # @return [LLVM::Instruction] An integer instruction
321
+ # @LLVMinst xor
322
+ def xor(lhs, rhs, name = "")
323
+ Instruction.from_ptr(C.LLVMBuildXor(self, lhs, rhs, name))
324
+ end
325
+
326
+ # Integer negation. Implemented as a shortcut to the equivalent sub
327
+ # instruction.
328
+ # @param [LLVM::Value] arg Integer or vector of integers
329
+ # @param [String] name Name of the result in LLVM IR
330
+ # @return [LLVM::Instruction] The negated operand
331
+ # @LLVMinst sub
332
+ def neg(arg, name = "")
333
+ Instruction.from_ptr(C.LLVMBuildNeg(self, arg, name))
334
+ end
335
+
336
+ # Boolean negation.
337
+ # @param [LLVM::Value] arg Integer or vector of integers
338
+ # @param [String] name The name of the result in LLVM IR
339
+ # @return [LLVM::Instruction] The negated operand
340
+ def not(arg, name = "")
341
+ Instruction.from_ptr(C.LLVMBuildNot(self, arg, name))
342
+ end
343
+
344
+ # @param [LLVM::Type, #type] ty The type or value whose type
345
+ # should be malloced
346
+ # @param [String] name The name of the result in LLVM IR
347
+ # @return [LLVM::Instruction] A pointer to the malloced bytes
348
+ def malloc(ty, name = "")
349
+ Instruction.from_ptr(C.LLVMBuildMalloc(self, LLVM::Type(ty), name))
350
+ end
351
+
352
+ # @param [LLVM::Type, #type] ty The type or value whose type will be the
353
+ # element type of the malloced array
354
+ # @param [LLVM::Value] sz Unsigned integer representing size of the array
355
+ # @param [String] name The name of the result in LLVM IR
356
+ # @return [LLVM::Instruction] A pointer to the malloced array
357
+ def array_malloc(ty, sz, name = "")
358
+ Instruction.from_ptr(C.LLVMBuildArrayMalloc(self, LLVM::Type(ty), sz, name))
359
+ end
360
+
361
+ # Stack allocation.
362
+ # @param [LLVM::Type, #type] ty The type or value whose type should be
363
+ # allocad
364
+ # @param [String] name The name of the result in LLVM IR
365
+ # @return [LLVM::Instruction] A pointer to the allocad bytes
366
+ # @LLVMinst alloca
367
+ def alloca(ty, name = "")
368
+ Instruction.from_ptr(C.LLVMBuildAlloca(self, LLVM::Type(ty), name))
369
+ end
370
+
371
+ # Array stack allocation
372
+ # @param [LLVM::Type, #type] ty The type or value whose type will be the
373
+ # element type of the allocad array
374
+ # @param [LLVM::Value] sz Unsigned integer representing size of the array
375
+ # @param [String] name The name of the result in LLVM IR
376
+ # @return [LLVM::Instruction] A pointer to the allocad array
377
+ # @LLVMinst alloca
378
+ def array_alloca(ty, sz, name = "")
379
+ Instruction.from_ptr(C.LLVMBuildArrayAlloca(self, LLVM::Type(ty), sz, name))
380
+ end
381
+
382
+ # @param [LLVM::Value] ptr The pointer to be freed
383
+ # @return [LLVM::Instruction] The result of the free instruction
384
+ def free(ptr)
385
+ Instruction.from_ptr(C.LLVMBuildFree(self, ptr))
386
+ end
387
+
388
+ # Load the value of a given pointer
389
+ # @param [LLVM::Value] ptr The pointer to be loaded
390
+ # @param [String] name The name of the result in LLVM IR
391
+ # @return [LLVM::Instruction] The result of the load operation. Represents
392
+ # a value of the pointer's type.
393
+ # @LLVMinst load
394
+ def load(ptr, name = "")
395
+ Instruction.from_ptr(C.LLVMBuildLoad(self, ptr, name))
396
+ end
397
+
398
+ # Store a value at a given pointer
399
+ # @param [LLVM::Value] val The value to be stored
400
+ # @param [LLVM::Value] ptr A pointer to the same type as val
401
+ # @return [LLVM::Instruction] The result of the store operation
402
+ # @LLVMinst store
403
+ def store(val, ptr)
404
+ Instruction.from_ptr(C.LLVMBuildStore(self, val, ptr))
405
+ end
406
+
407
+ # Obtain a pointer to the element at the given indices
408
+ # @param [LLVM::Value] ptr A pointer to an aggregate value
409
+ # @param [Array<LLVM::Value>] indices Ruby array of LLVM::Value representing
410
+ # indices into the aggregate
411
+ # @param [String] name The name of the result in LLVM IR
412
+ # @return [LLVM::Instruction] The resulting pointer
413
+ # @LLVMinst gep
414
+ # @see http://llvm.org/docs/GetElementPtr.html
415
+ def gep(ptr, indices, name = "")
416
+ indices = Array(indices)
417
+ FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
418
+ indices_ptr.write_array_of_pointer(indices)
419
+ return Instruction.from_ptr(
420
+ C.LLVMBuildGEP(self, ptr, indices_ptr, indices.size, name))
421
+ end
422
+ end
423
+
424
+ # Builds a inbounds getelementptr instruction. If the indices are outside
425
+ # the allocated pointer the value is undefined.
426
+ # @param [LLVM::Value] ptr A pointer to an aggregate value
427
+ # @param [Array<LLVM::Value>] indices Ruby array of LLVM::Value representing
428
+ # indices into the aggregate
429
+ # @param [String] name The name of the result in LLVM IR
430
+ # @return [LLVM::Instruction] The resulting pointer
431
+ # @LLVMinst gep
432
+ # @see http://llvm.org/docs/GetElementPtr.html
433
+ def inbounds_gep(ptr, indices, name = "")
434
+ indices = Array(indices)
435
+ FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
436
+ indices_ptr.write_array_of_pointer(indices)
437
+ return Instruction.from_ptr(
438
+ C.LLVMBuildInBoundsGEP(self, ptr, indices_ptr, indices.size, name))
439
+ end
440
+ end
441
+
442
+ # Builds a struct getelementptr Instruction.
443
+ # @param [LLVM::Value] ptr A pointer to a structure
444
+ # @param [LLVM::Value] idx Unsigned integer representing the index of a
445
+ # structure member
446
+ # @param [String] name The name of the result in LLVM IR
447
+ # @return [LLVM::Instruction] The resulting pointer
448
+ # @LLVMinst gep
449
+ # @see http://llvm.org/docs/GetElementPtr.html
450
+ def struct_gep(pointer, idx, name = "")
451
+ Instruction.from_ptr(C.LLVMBuildStructGEP(self, pointer, idx, name))
452
+ end
453
+
454
+ # Creates a global string initialized to a given value.
455
+ # @param [String] string The string used by the initialize
456
+ # @param [Name] name Name of the result in LLVM IR
457
+ # @return [LLVM::Instruction] Reference to the global string
458
+ def global_string(string, name = "")
459
+ Instruction.from_ptr(C.LLVMBuildGlobalString(self, string, name))
460
+ end
461
+
462
+ # Creates a pointer to a global string initialized to a given value.
463
+ # @param [String] string The string used by the initializer
464
+ # @param [String] name The name of the result in LLVM IR
465
+ # @return [LLVM::Instruction] Reference to the global string pointer
466
+ def global_string_pointer(string, name = "")
467
+ Instruction.from_ptr(C.LLVMBuildGlobalStringPtr(self, string, name))
468
+ end
469
+
470
+ # Truncates its operand to the given type. The size of the value type must
471
+ # be greater than the size of the target type.
472
+ # @param [LLVM::Value] val Integer or vector of integers to be truncated
473
+ # @param [LLVM::Type, #type] ty Integer or vector of integers of equal size
474
+ # to val
475
+ # @param [String] name The name of the result in LLVM IR
476
+ # @return [LLVM::Instruction] The truncated value
477
+ # @LLVMinst trunc
478
+ def trunc(val, ty, name = "")
479
+ Instruction.from_ptr(C.LLVMBuildTrunc(self, val, LLVM::Type(ty), name))
480
+ end
481
+
482
+ # Zero extends its operand to the given type. The size of the value type
483
+ # must be greater than the size of the target type.
484
+ # @param [LLVM::Value] val Integer or vector of integers to be extended
485
+ # @param [LLVM::Type, #type] ty Integer or vector of integer type of
486
+ # greater size than val
487
+ # @param [String] name The name of the result in LLVM IR
488
+ # @return [LLVM::Instruction] The extended value
489
+ # @LLVMinst zext
490
+ def zext(val, ty, name = "")
491
+ Instruction.from_ptr(C.LLVMBuildZExt(self, val, LLVM::Type(ty), name))
492
+ end
493
+
494
+ # Sign extension by copying the sign bit (highest order bit) of the value
495
+ # until it reaches the bit size of the given type.
496
+ # @param [LLVM::Value] val Integer or vector of integers to be extended
497
+ # @param [LLVM::Type] ty Integer or vector of integer type of greater size
498
+ # than the size of val
499
+ # @param [String] name The name of the result in LLVM IR
500
+ # @return [LLVM::Instruction] The extended value
501
+ # @LLVMinst sext
502
+ def sext(val, ty, name = "")
503
+ Instruction.from_ptr(C.LLVMBuildSExt(self, val, LLVM::Type(ty), name))
504
+ end
505
+
506
+ # Convert a floating point to an unsigned integer
507
+ # @param [LLVM::Value] val Floating point or vector of floating points to
508
+ # convert
509
+ # @param [LLVM::Type, #type] ty Integer or vector of integer target type
510
+ # @param [String] name The name of the result in LLVM IR
511
+ # @return [LLVM::Instruction] The converted value
512
+ # @LLVMinst fptoui
513
+ def fp2ui(val, ty, name = "")
514
+ Instruction.from_ptr(C.LLVMBuildFPToUI(self, val, LLVM::Type(ty), name))
515
+ end
516
+
517
+ # Convert a floating point to a signed integer
518
+ # @param [LLVM::Value] val Floating point or vector of floating points to
519
+ # convert
520
+ # @param [LLVM::Type, #type] ty Integer or vector of integer target type
521
+ # @param [String] name The name of the result in LLVM IR
522
+ # @return [LLVM::Instruction] The converted value
523
+ # @LLVMinst fptosi
524
+ def fp2si(val, ty, name = "")
525
+ Instruction.from_ptr(C.LLVMBuildFPToSI(self, val, LLVM::Type(ty), name))
526
+ end
527
+
528
+ # Convert an unsigned integer to a floating point
529
+ # @param [LLVM::Value] val Unsigned integer or vector of unsigned integer
530
+ # to convert
531
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
532
+ # target type
533
+ # @param [String] name The name of the result in LLVM IR
534
+ # @return [LLVM::Instruction] The converted value
535
+ # @LLVMinst uitofp
536
+ def ui2fp(val, ty, name = "")
537
+ Instruction.from_ptr(C.LLVMBuildUIToFP(self, val, LLVM::Type(ty), name))
538
+ end
539
+
540
+ # Convert a signed integer to a floating point
541
+ # @param [LLVM::Value] val Signed integer or vector of signed integer
542
+ # to convert
543
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
544
+ # target type
545
+ # @param [String] name The name of the result in LLVM IR
546
+ # @return [LLVM::Instruction] The converted value
547
+ # @LLVMinst sitofp
548
+ def si2fp(val, ty, name = "")
549
+ Instruction.from_ptr(C.LLVMBuildSIToFP(self, val, LLVM::Type(ty), name))
550
+ end
551
+
552
+ # Truncate a floating point value
553
+ # @param [LLVM::Value] val Floating point or vector of floating point
554
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
555
+ # type of lesser size than val's type
556
+ # @param [String] name The name of the result in LLVM IR
557
+ # @return [LLVM::Instruction] The truncated value
558
+ # @LLVMinst fptrunc
559
+ def fp_trunc(val, ty, name = "")
560
+ Instruction.from_ptr(C.LLVMBuildFPTrunc(self, val, LLVM::Type(ty), name))
561
+ end
562
+
563
+ # Extend a floating point value
564
+ # @param [LLVM::Value] val Floating point or vector of floating point
565
+ # @param [LLVM::Type, #type] ty Floating point or vector of floating point
566
+ # type of greater size than val's type
567
+ # @param [String] name The name of the result in LLVM IR
568
+ # @return [LLVM::Instruction] The extended value
569
+ # @LLVMinst fpext
570
+ def fp_ext(val, ty, name = "")
571
+ Instruction.from_ptr(C.LLVMBuildFPExt(self, val, LLVM::Type(ty), name))
572
+ end
573
+
574
+ # Cast a pointer to an int. Useful for pointer arithmetic.
575
+ # @param [LLVM::Value] val A pointer
576
+ # @param [LLVM::Type, #type] ty An integer type
577
+ # @param [String] name The name of the result in LLVM IR
578
+ # @return [LLVM::Instruction] An integer of the given type representing
579
+ # the pointer's address
580
+ # @LLVMinst ptrtoint
581
+ def ptr2int(val, ty, name = "")
582
+ Instruction.from_ptr(C.LLVMBuildPtrToInt(self, val, LLVM::Type(ty), name))
583
+ end
584
+
585
+ # Cast an int to a pointer
586
+ # @param [LLVM::Value] val An integer value
587
+ # @param [LLVM::Type, #ty] ty A pointer type
588
+ # @param [String] name The name of the result in LLVM IR
589
+ # @return [LLVM::Instruction] A pointer of the given type and the address
590
+ # held in val
591
+ # @LLVMinst inttoptr
592
+ def int2ptr(val, ty, name = "")
593
+ Instruction.from_ptr(C.LLVMBuildIntToPtr(self, val, LLVM::Type(ty), name))
594
+ end
595
+
596
+ # Cast a value to the given type without changing any bits
597
+ # @param [LLVM::Value] val The value to cast
598
+ # @param [LLVM::Type, #ty] ty The target type
599
+ # @param [String] name The name of the result in LLVM IR
600
+ # @return [LLVM::Instruction] A value of the target type
601
+ # @LLVMinst bitcast
602
+ def bit_cast(val, ty, name = "")
603
+ Instruction.from_ptr(C.LLVMBuildBitCast(self, val, LLVM::Type(ty), name))
604
+ end
605
+
606
+ # @param [LLVM::Value] val
607
+ # @param [LLVM::Type, #ty] ty
608
+ # @param [String] name The name of the result in LLVM IR
609
+ # @return [LLVM::Instruction]
610
+ # @LLVMinst zext
611
+ # @LLVMinst bitcast
612
+ def zext_or_bit_cast(val, ty, name = "")
613
+ Instruction.from_ptr(C.LLVMBuildZExtOrBitCast(self, val, LLVM::Type(ty), name))
614
+ end
615
+
616
+ # @param [LLVM::Value] val
617
+ # @param [LLVM::Type, #ty] ty
618
+ # @param [String] name The name of the result in LLVM IR
619
+ # @return [LLVM::Instruction]
620
+ # @LLVMinst sext
621
+ # @LLVMinst bitcast
622
+ def sext_or_bit_cast(val, ty, name = "")
623
+ Instruction.from_ptr(C.LLVMBuildSExtOrBitCast(self, val, LLVM::Type(ty), name))
624
+ end
625
+
626
+ # @param [LLVM::Value] val
627
+ # @param [LLVM::Type, #ty] ty
628
+ # @param [String] name The name of the result in LLVM IR
629
+ # @return [LLVM::Instruction]
630
+ # @LLVMinst trunc
631
+ # @LLVMinst bitcast
632
+ def trunc_or_bit_cast(val, ty, name = "")
633
+ Instruction.from_ptr(C.LLVMBuildTruncOrBitCast(self, val, LLVM::Type(ty), name))
634
+ end
635
+
636
+ # @param [LLVM::Value] val
637
+ # @param [LLVM::Type, #ty] ty
638
+ # @param [String] name The name of the result in LLVM IR
639
+ # @return [LLVM::Instruction]
640
+ def pointer_cast(val, ty, name = "")
641
+ Instruction.from_ptr(C.LLVMBuildPointerCast(self, val, LLVM::Type(ty), name))
642
+ end
643
+
644
+ # @param [LLVM::Value] val
645
+ # @param [LLVM::Type, #ty] ty
646
+ # @param [String] name The name of the result in LLVM IR
647
+ # @return [LLVM::Instruction]
648
+ def int_cast(val, ty, name = "")
649
+ Instruction.from_ptr(C.LLVMBuildIntCast(self, val, LLVM::Type(ty), name))
650
+ end
651
+
652
+ # @param [LLVM::Value] val
653
+ # @param [LLVM::Type, #ty] ty
654
+ # @param [String] name The name of the result in LLVM IR
655
+ # @return [LLVM::Instruction]
656
+ def fp_cast(val, ty, name = "")
657
+ Instruction.from_ptr(C.LLVMBuildFPCast(self, val, LLVM::Type(ty), name))
658
+ end
659
+
660
+ # Builds an icmp Instruction. Compares lhs to rhs (Instructions)
661
+ # using the given symbol predicate (pred):
662
+ # :eq - equal to
663
+ # :ne - not equal to
664
+ # :ugt - unsigned greater than
665
+ # :uge - unsigned greater than or equal to
666
+ # :ult - unsigned less than
667
+ # :ule - unsigned less than or equal to
668
+ # :sgt - signed greater than
669
+ # :sge - signed greater than or equal to
670
+ # :slt - signed less than
671
+ # :sle - signed less than or equal to
672
+ # @param [Symbol] pred A predicate
673
+ # @param [LLVM::Value] lhs The left hand side of the comparison, of integer
674
+ # or pointer type
675
+ # @param [LLVM::Value] rhs The right hand side of the comparison, of the
676
+ # same type as lhs
677
+ # @param [String] name The name of the result in LLVM IR
678
+ # @return [LLVM::Instruction] A boolean represented as i1
679
+ # @LLVMinst icmp
680
+ def icmp(pred, lhs, rhs, name = "")
681
+ Instruction.from_ptr(C.LLVMBuildICmp(self, pred, lhs, rhs, name))
682
+ end
683
+
684
+ # Builds an fcmp Instruction. Compares lhs to rhs (Instructions) as Reals
685
+ # using the given symbol predicate (pred):
686
+ # :ord - ordered
687
+ # :uno - unordered: isnan(X) | isnan(Y)
688
+ # :oeq - ordered and equal to
689
+ # :oeq - unordered and equal to
690
+ # :one - ordered and not equal to
691
+ # :one - unordered and not equal to
692
+ # :ogt - ordered and greater than
693
+ # :uge - unordered and greater than or equal to
694
+ # :olt - ordered and less than
695
+ # :ule - unordered and less than or equal to
696
+ # :oge - ordered and greater than or equal to
697
+ # :sge - unordered and greater than or equal to
698
+ # :ole - ordered and less than or equal to
699
+ # :sle - unordered and less than or equal to
700
+ # :true - always true and folded
701
+ # :false - always false and folded
702
+ # @param [Symbol] pred A predicate
703
+ # @param [LLVM::Value] lhs The left hand side of the comparison, of
704
+ # floating point type
705
+ # @param [LLVM::Value] rhs The right hand side of the comparison, of
706
+ # the same type as lhs
707
+ # @param [String] name The name of the result in LLVM IR
708
+ # @return [LLVM::Instruction] A boolean represented as i1
709
+ # @LLVMinst fcmp
710
+ def fcmp(pred, lhs, rhs, name = "")
711
+ Instruction.from_ptr(C.LLVMBuildFCmp(self, pred, lhs, rhs, name))
712
+ end
713
+
714
+ # Build a Phi node of the given type with the given incoming branches
715
+ # @param [LLVM::Type] ty Specifies the result type
716
+ # @param [Hash{LLVM::BasicBlock => LLVM::Value}] incoming A hash mapping
717
+ # basic blocks to a corresponding value. If the phi node is jumped to
718
+ # from a given basic block, the phi instruction takes on its
719
+ # corresponding value.
720
+ # @param [String] name The name of the result in LLVM IR
721
+ # @return [LLVM::Instruction] The phi node
722
+ # @LLVMinst phi
723
+ def phi(ty, incoming, name = "")
724
+
725
+ phi = Phi.from_ptr(C.LLVMBuildPhi(self, LLVM::Type(ty), name))
726
+ phi.add_incoming(incoming)
727
+ phi
728
+ end
729
+
730
+ # Builds a call Instruction. Calls the given Function with the given
731
+ # args (Instructions).
732
+ # @param [LLVM::Function] fun
733
+ # @param [Array<LLVM::Value>] args
734
+ # @param [LLVM::Instruction]
735
+ # @LLVMinst call
736
+ def call(fun, *args)
737
+ raise "No fun" if fun.nil?
738
+ if args.last.kind_of? String
739
+ name = args.pop
740
+ else
741
+ name = ""
742
+ end
743
+
744
+ args_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * args.size)
745
+ args_ptr.write_array_of_pointer(args)
746
+ CallInst.from_ptr(C.LLVMBuildCall(self, fun, args_ptr, args.size, name))
747
+ end
748
+
749
+ # Return a value based on a condition. This differs from 'cond' in that
750
+ # its operands are values rather than basic blocks. As a consequence, both
751
+ # arguments must be evaluated.
752
+ # @param [LLVM::Value] _if An i1 or a vector of i1
753
+ # @param [LLVM::Value] _then A value or vector of the same arity as _if
754
+ # @param [LLVM::Value] _else A value or vector of values of the same arity
755
+ # as _if, and of the same type as _then
756
+ # @param [String] name The name of the result in LLVM IR
757
+ # @return [LLVM::Instruction] An instruction representing either _then or
758
+ # _else
759
+ # @LLVMinst select
760
+ def select(_if, _then, _else, name = "")
761
+ Instruction.from_ptr(C.LLVMBuildSelect(self, _if, _then, _else, name))
762
+ end
763
+
764
+ # Extract an element from a vector
765
+ # @param [LLVM::Value] vector The vector from which to extract a value
766
+ # @param [LLVM::Value] idx The index of the element to extract, an
767
+ # unsigned integer
768
+ # @param [String] name The value of the result in LLVM IR
769
+ # @return [LLVM::Instruction] The extracted element
770
+ # @LLVMinst extractelement
771
+ def extract_element(vector, idx, name = "")
772
+ Instruction.from_ptr(C.LLVMBuildExtractElement(self, vector, idx, name))
773
+ end
774
+
775
+ # Insert an element into a vector
776
+ # @param [LLVM::Value] vector The vector into which to insert the element
777
+ # @param [LLVM::Value] elem The element to be inserted into the vector
778
+ # @param [LLVM::Value] idx The index at which to insert the element
779
+ # @param [String] name The name of the result in LLVM IR
780
+ # @return [LLVM::Instruction] A vector the same type as 'vector'
781
+ # @LLVMinst insertelement
782
+ def insert_element(vector, elem, idx, name = "")
783
+ Instruction.from_ptr(C.LLVMBuildInsertElement(self, vector, elem, idx, name))
784
+ end
785
+
786
+ # Shuffle two vectors according to a given mask
787
+ # @param [LLVM::Value] vec1 A vector
788
+ # @param [LLVM::Value] vec2 A vector of the same type and arity as vec1
789
+ # @param [LLVM::Value] mask A vector of i1 of the same arity as vec1 and
790
+ # vec2
791
+ # @param [String] name The name of the result in LLVM IR
792
+ # @return [LLVM::Instruction] The shuffled vector
793
+ # @LLVMinst shufflevector
794
+ def shuffle_vector(vec1, vec2, mask, name = "")
795
+ Instruction.from_ptr(C.LLVMBuildShuffleVector(self, vec1, vec2, mask, name))
796
+ end
797
+
798
+ # Extract the value of a member field from an aggregate value
799
+ # @param [LLVM::Value] aggregate An aggregate value
800
+ # @param [LLVM::Value] idx The index of the member to extract
801
+ # @param [String] name The name of the result in LLVM IR
802
+ # @return [LLVM::Instruction] The extracted value
803
+ # @LLVMinst extractvalue
804
+ def extract_value(aggregate, idx, name = "")
805
+ Instruction.from_ptr(C.LLVMBuildExtractValue(self, aggregate, idx, name))
806
+ end
807
+
808
+ # Insert a value into an aggregate value's member field
809
+ # @param [LLVM::Value] aggregate An aggregate value
810
+ # @param [LLVM::Value] elem The value to insert into 'aggregate'
811
+ # @param [LLVM::Value] idx The index at which to insert the value
812
+ # @param [String] name The name of the result in LLVM IR
813
+ # @return [LLVM::Instruction] An aggregate value of the same type as 'aggregate'
814
+ # @LLVMinst insertvalue
815
+ def insert_value(aggregate, elem, idx, name = "")
816
+ Instruction.from_ptr(C.LLVMBuildInsertValue(self, aggregate, elem, idx, name))
817
+ end
818
+
819
+ # Check if a value is null
820
+ # @param [LLVM::Value] val The value to check
821
+ # @param [String] name The name of the result in LLVM IR
822
+ # @return [LLVM::Instruction] An i1
823
+ def is_null(val, name = "")
824
+ Instruction.from_ptr(C.LLVMBuildIsNull(self, val, name))
825
+ end
826
+
827
+ # Check if a value is not null
828
+ # @param [LLVM::Value] val The value to check
829
+ # @param [String] name The name of the result in LLVM IR
830
+ # @return [LLVM::Instruction] An i1
831
+ def is_not_null(val, name = "")
832
+ Instruction.from_ptr(C.LLVMBuildIsNotNull(self, val, name))
833
+ end
834
+
835
+ # Calculate the difference between two pointers
836
+ # @param [LLVM::Value] lhs A pointer
837
+ # @param [LLVM::Value] rhs A pointer
838
+ # @param [String] name The name of the result in LLVM IR
839
+ # @return [LLVM::Instruction] The integer difference between the two
840
+ # pointers
841
+ def ptr_diff(lhs, rhs, name = "")
842
+ Instruction.from_ptr(C.LLVMBuildPtrDiff(lhs, rhs, name))
843
+ end
844
+
845
+ # Disposes the builder.
846
+ # @return nil
847
+ def dispose
848
+ C.LLVMDisposeBuilder(@ptr)
849
+ end
850
+ end
851
+ end