ruby-llvm 2.9.1 → 2.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,7 @@
1
1
  module LLVM
2
2
  class Builder
3
- private_class_method :new
4
-
5
- # @private
6
- def initialize(ptr)
7
- @ptr = ptr
3
+ def initialize
4
+ @ptr = C.LLVMCreateBuilder()
8
5
  end
9
6
 
10
7
  # @private
@@ -12,12 +9,10 @@ module LLVM
12
9
  @ptr
13
10
  end
14
11
 
15
- # Creates a Builder.
16
- def self.create
17
- new(C.LLVMCreateBuilder())
18
- end
19
-
20
- # Positions the builder at the given Instruction in the given BasicBlock.
12
+ # Position the builder at the given Instruction within the given BasicBlock.
13
+ # @param [LLVM::BasicBlock]
14
+ # @param [LLVM::Instruction]
15
+ # @return [LLVM::Builder]
21
16
  def position(block, instruction)
22
17
  raise "Block must not be nil" if block.nil?
23
18
  C.LLVMPositionBuilder(self, block, instruction)
@@ -25,6 +20,8 @@ module LLVM
25
20
  end
26
21
 
27
22
  # Positions the builder before the given Instruction.
23
+ # @param [LLVM::Instruction]
24
+ # @return [LLVM::Builder]
28
25
  def position_before(instruction)
29
26
  raise "Instruction must not be nil" if instruction.nil?
30
27
  C.LLVMPositionBuilderBefore(self, instruction)
@@ -32,6 +29,8 @@ module LLVM
32
29
  end
33
30
 
34
31
  # Positions the builder at the end of the given BasicBlock.
32
+ # @param [LLVM::BasicBlock]
33
+ # @return [LLVM::Builder]
35
34
  def position_at_end(block)
36
35
  raise "Block must not be nil" if block.nil?
37
36
  C.LLVMPositionBuilderAtEnd(self, block)
@@ -39,22 +38,27 @@ module LLVM
39
38
  end
40
39
 
41
40
  # The BasicBlock at which the Builder is currently positioned.
41
+ # @return [LLVM::BasicBlock]
42
42
  def insert_block
43
43
  BasicBlock.from_ptr(C.LLVMGetInsertBlock(self))
44
44
  end
45
45
 
46
-
46
+ # @return [LLVM::Instruction]
47
47
  # @LLVMinst ret
48
48
  def ret_void
49
49
  Instruction.from_ptr(C.LLVMBuildRetVoid(self))
50
50
  end
51
51
 
52
+ # @param [LLVM::Value] val The value to return
53
+ # @return [LLVM::Instruction]
52
54
  # @LLVMinst ret
53
55
  def ret(val)
54
56
  Instruction.from_ptr(C.LLVMBuildRet(self, val))
55
57
  end
56
58
 
57
59
  # Builds a ret instruction returning multiple values.
60
+ # @param [Array<LLVM::Value>] vals
61
+ # @return [LLVM::Instruction]
58
62
  # @LLVMinst ret
59
63
  def aggregate_ret(*vals)
60
64
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * vals.size) do |vals_ptr|
@@ -64,6 +68,8 @@ module LLVM
64
68
  end
65
69
 
66
70
  # Unconditional branching (i.e. goto)
71
+ # @param [LLVM::BasicBlock] block Where to jump
72
+ # @return [LLVM::Instruction]
67
73
  # @LLVMinst br
68
74
  def br(block)
69
75
  Instruction.from_ptr(
@@ -71,6 +77,10 @@ module LLVM
71
77
  end
72
78
 
73
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]
74
84
  # @LLVMinst br
75
85
  def cond(cond, iftrue, iffalse)
76
86
  Instruction.from_ptr(
@@ -78,327 +88,571 @@ module LLVM
78
88
  end
79
89
 
80
90
  # @LLVMinst switch
81
- def switch(val, default, ncases)
82
- SwitchInst.from_ptr(C.LLVMBuildSwitch(self, val, default, ncases))
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
83
103
  end
84
104
 
85
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
86
113
  # @LLVMinst invoke
87
- def invoke(fun, args, _then, _catch, name = "")
114
+ def invoke(fun, args, normal, exception, name = "")
88
115
  Instruction.from_ptr(
89
116
  C.LLVMBuildInvoke(self,
90
- fun, args, args.size, _then, _catch, name))
117
+ fun, args, args.size, normal, exception, name))
91
118
  end
92
119
 
93
120
  # Builds an unwind Instruction.
121
+ # @return [LLVM::Instruction]
94
122
  # @LLVMinst unwind
95
123
  def unwind
96
124
  Instruction.from_ptr(C.LLVMBuildUnwind(self))
97
125
  end
98
126
 
127
+ # Generates an instruction with no defined semantics. Can be used to
128
+ # provide hints to the optimizer.
129
+ # @return [LLVM::Instruction]
99
130
  # @LLVMinst unreachable
100
131
  def unreachable
101
132
  Instruction.from_ptr(C.LLVMBuildUnreachable(self))
102
133
  end
103
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
104
139
  # @LLVMinst add
105
140
  def add(lhs, rhs, name = "")
106
141
  Instruction.from_ptr(C.LLVMBuildAdd(self, lhs, rhs, name))
107
142
  end
108
143
 
109
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
110
149
  # @LLVMinst add
111
150
  def nsw_add(lhs, rhs, name = "")
112
151
  Instruction.from_ptr(C.LLVMBuildNSWAdd(self, lhs, rhs, name))
113
152
  end
114
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
115
158
  # @LLVMinst fadd
116
159
  def fadd(lhs, rhs, name = "")
117
160
  Instruction.from_ptr(C.LLVMBuildFAdd(self, lhs, rhs, name))
118
161
  end
119
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
120
167
  # @LLVMinst sub
121
168
  def sub(lhs, rhs, name = "")
122
169
  Instruction.from_ptr(C.LLVMBuildSub(self, lhs, rhs, name))
123
170
  end
124
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
125
177
  # @LLVMinst fsub
126
178
  def fsub(lhs, rhs, name = "")
127
179
  Instruction.from_ptr(C.LLVMBuildFSub(self, lhs, rhs, name))
128
180
  end
129
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
130
186
  # @LLVMinst mul
131
187
  def mul(lhs, rhs, name = "")
132
188
  Instruction.from_ptr(C.LLVMBuildMul(self, lhs, rhs, name))
133
189
  end
134
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
135
196
  # @LLVMinst fmul
136
197
  def fmul(lhs, rhs, name = "")
137
198
  Instruction.from_ptr(C.LLVMBuildFMul(self, lhs, rhs, name))
138
199
  end
139
200
 
140
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
141
206
  # @LLVMinst udiv
142
207
  def udiv(lhs, rhs, name = "")
143
208
  Instruction.from_ptr(C.LLVMBuildUDiv(self, lhs, rhs, name))
144
209
  end
145
210
 
146
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
147
216
  # @LLVMinst sdiv
148
217
  def sdiv(lhs, rhs, name = "")
149
218
  Instruction.from_ptr(C.LLVMBuildSDiv(self, lhs, rhs, name))
150
219
  end
151
220
 
152
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
153
226
  # @LLVMinst sdiv
154
227
  def exact_sdiv(lhs, rhs, name = "")
155
228
  Instruction.from_ptr(C.LLVMBuildExactSDiv(self, lhs, rhs, name))
156
229
  end
157
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
158
236
  # @LLVMinst fdiv
159
237
  def fdiv(lhs, rhs, name = "")
160
238
  Instruction.from_ptr(C.LLVMBuildFDiv(self, lhs, rhs, name))
161
239
  end
162
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
163
246
  # @LLVMinst urem
164
247
  def urem(lhs, rhs, name = "")
165
248
  Instruction.from_ptr(C.LLVMBuildURem(self, lhs, rhs, name))
166
249
  end
167
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
168
256
  # @LLVMinst srem
169
257
  def srem(lhs, rhs, name = "")
170
258
  Instruction.from_ptr(C.LLVMBuildSRem(self, lhs, rhs, name))
171
259
  end
172
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
173
265
  # @LLVMinst frem
174
266
  def frem(lhs, rhs, name = "")
175
267
  Instruction.from_ptr(C.LLVMBuildFRem(self, lhs, rhs, name))
176
268
  end
177
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
178
274
  # @LLVMinst shl
179
275
  def shl(lhs, rhs, name = "")
180
276
  Instruction.from_ptr(C.LLVMBuildShl(self, lhs, rhs, name))
181
277
  end
182
278
 
183
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
184
284
  # @LLVMinst lshr
185
285
  def lshr(lhs, rhs, name = "")
186
286
  Instruction.from_ptr(C.LLVMBuildLShr(self, lhs, rhs, name))
187
287
  end
188
288
 
189
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
190
294
  # @LLVMinst ashr
191
295
  def ashr(lhs, rhs, name = "")
192
296
  Instruction.from_ptr(C.LLVMBuildAShr(self, lhs, rhs, name))
193
297
  end
194
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
195
303
  # @LLVMinst and
196
304
  def and(lhs, rhs, name = "")
197
305
  Instruction.from_ptr(C.LLVMBuildAnd(self, lhs, rhs, name))
198
306
  end
199
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
200
312
  # @LLVMinst or
201
313
  def or(lhs, rhs, name = "")
202
314
  Instruction.from_ptr(C.LLVMBuildOr(self, lhs, rhs, name))
203
315
  end
204
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
205
321
  # @LLVMinst xor
206
322
  def xor(lhs, rhs, name = "")
207
323
  Instruction.from_ptr(C.LLVMBuildXor(self, lhs, rhs, name))
208
324
  end
209
325
 
210
- # Integer negation (i.e. multiplication by -1).
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
211
332
  def neg(arg, name = "")
212
333
  Instruction.from_ptr(C.LLVMBuildNeg(self, arg, name))
213
334
  end
214
335
 
215
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
216
340
  def not(arg, name = "")
217
341
  Instruction.from_ptr(C.LLVMBuildNot(self, arg, name))
218
342
  end
219
343
 
220
- # Builds a malloc Instruction for the given type.
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
221
348
  def malloc(ty, name = "")
222
349
  Instruction.from_ptr(C.LLVMBuildMalloc(self, LLVM::Type(ty), name))
223
350
  end
224
351
 
225
- # Builds a malloc Instruction for the given array type.
226
- def array_malloc(ty, val, name = "")
227
- Instruction.from_ptr(C.LLVMBuildArrayMalloc(self, LLVM::Type(ty), val, name))
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))
228
359
  end
229
360
 
230
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
231
366
  # @LLVMinst alloca
232
367
  def alloca(ty, name = "")
233
368
  Instruction.from_ptr(C.LLVMBuildAlloca(self, LLVM::Type(ty), name))
234
369
  end
235
370
 
236
371
  # Array stack allocation
237
- # @param LLVM::Value used to initialize each element.
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
238
377
  # @LLVMinst alloca
239
- def array_alloca(ty, val, name = "")
240
- Instruction.from_ptr(C.LLVMBuildArrayAlloca(self, LLVM::Type(ty), val, name))
378
+ def array_alloca(ty, sz, name = "")
379
+ Instruction.from_ptr(C.LLVMBuildArrayAlloca(self, LLVM::Type(ty), sz, name))
241
380
  end
242
381
 
243
- # Builds a free Instruction. Frees the given pointer (an Instruction).
244
- def free(pointer)
245
- Instruction.from_ptr(C.LLVMBuildFree(self, pointer))
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))
246
386
  end
247
387
 
248
- # Builds a load Instruction with the given name. Loads the value of the
249
- # given pointer (an Instruction).
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.
250
393
  # @LLVMinst load
251
- def load(pointer, name = "")
252
- Instruction.from_ptr(C.LLVMBuildLoad(self, pointer, name))
394
+ def load(ptr, name = "")
395
+ Instruction.from_ptr(C.LLVMBuildLoad(self, ptr, name))
253
396
  end
254
397
 
255
- # Builds a store Instruction. Stores the given Value into the given
256
- # pointer (an Instruction).
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
257
402
  # @LLVMinst store
258
- def store(val, pointer)
259
- Instruction.from_ptr(C.LLVMBuildStore(self, val, pointer))
403
+ def store(val, ptr)
404
+ Instruction.from_ptr(C.LLVMBuildStore(self, val, ptr))
260
405
  end
261
406
 
262
- # Builds a getelementptr Instruction with the given name. Retrieves the
263
- # element pointer at the given indices of the pointer (an Instruction).
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
264
413
  # @LLVMinst gep
265
414
  # @see http://llvm.org/docs/GetElementPtr.html
266
- def gep(pointer, indices, name = "")
415
+ def gep(ptr, indices, name = "")
267
416
  indices = Array(indices)
268
417
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
269
418
  indices_ptr.write_array_of_pointer(indices)
270
419
  return Instruction.from_ptr(
271
- C.LLVMBuildGEP(self, pointer, indices_ptr, indices.size, name))
420
+ C.LLVMBuildGEP(self, ptr, indices_ptr, indices.size, name))
272
421
  end
273
422
  end
274
423
 
275
- # Builds a inbounds getelementptr Instruction with the given name.
276
- # Retrieves the element pointer at the given indices of the pointer (an
277
- # Instruction). If the indices are outside the allocated pointer the
278
- # retrieved value is undefined.
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
279
431
  # @LLVMinst gep
280
432
  # @see http://llvm.org/docs/GetElementPtr.html
281
- def inbounds_gep(pointer, indices, name = "")
433
+ def inbounds_gep(ptr, indices, name = "")
282
434
  indices = Array(indices)
283
435
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
284
436
  indices_ptr.write_array_of_pointer(indices)
285
437
  return Instruction.from_ptr(
286
- C.LLVMBuildInBoundsGEP(self, pointer, indices_ptr, indices.size, name))
438
+ C.LLVMBuildInBoundsGEP(self, ptr, indices_ptr, indices.size, name))
287
439
  end
288
440
  end
289
441
 
290
- # Builds a struct getelementptr Instruction with the given name.
291
- # Retrieves the element pointer at the given indices (idx) of the pointer
292
- # (an Instruction). See http://llvm.org/docs/GetElementPtr.html for
293
- # discussion.
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
294
448
  # @LLVMinst gep
295
449
  # @see http://llvm.org/docs/GetElementPtr.html
296
450
  def struct_gep(pointer, idx, name = "")
297
451
  Instruction.from_ptr(C.LLVMBuildStructGEP(self, pointer, idx, name))
298
452
  end
299
453
 
300
- # Builds a global string Instruction with the given name. Creates a global
301
- # string.
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
302
458
  def global_string(string, name = "")
303
459
  Instruction.from_ptr(C.LLVMBuildGlobalString(self, string, name))
304
460
  end
305
461
 
306
- # Builds a global string Instruction with the given name. Creates a global
307
- # string pointer.
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
308
466
  def global_string_pointer(string, name = "")
309
467
  Instruction.from_ptr(C.LLVMBuildGlobalStringPtr(self, string, name))
310
468
  end
311
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
312
477
  # @LLVMinst trunc
313
478
  def trunc(val, ty, name = "")
314
479
  Instruction.from_ptr(C.LLVMBuildTrunc(self, val, LLVM::Type(ty), name))
315
480
  end
316
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
317
489
  # @LLVMinst zext
318
490
  def zext(val, ty, name = "")
319
491
  Instruction.from_ptr(C.LLVMBuildZExt(self, val, LLVM::Type(ty), name))
320
492
  end
321
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
322
501
  # @LLVMinst sext
323
502
  def sext(val, ty, name = "")
324
503
  Instruction.from_ptr(C.LLVMBuildSExt(self, val, LLVM::Type(ty), name))
325
504
  end
326
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
327
512
  # @LLVMinst fptoui
328
513
  def fp2ui(val, ty, name = "")
329
514
  Instruction.from_ptr(C.LLVMBuildFPToUI(self, val, LLVM::Type(ty), name))
330
515
  end
331
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
332
523
  # @LLVMinst fptosi
333
524
  def fp2si(val, ty, name = "")
334
525
  Instruction.from_ptr(C.LLVMBuildFPToSI(self, val, LLVM::Type(ty), name))
335
526
  end
336
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
337
535
  # @LLVMinst uitofp
338
536
  def ui2fp(val, ty, name = "")
339
537
  Instruction.from_ptr(C.LLVMBuildUIToFP(self, val, LLVM::Type(ty), name))
340
538
  end
341
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
342
547
  # @LLVMinst sitofp
343
548
  def si2fp(val, ty, name = "")
344
549
  Instruction.from_ptr(C.LLVMBuildSIToFP(self, val, LLVM::Type(ty), name))
345
550
  end
346
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
347
558
  # @LLVMinst fptrunc
348
559
  def fp_trunc(val, ty, name = "")
349
560
  Instruction.from_ptr(C.LLVMBuildFPTrunc(self, val, LLVM::Type(ty), name))
350
561
  end
351
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
352
569
  # @LLVMinst fpext
353
570
  def fp_ext(val, ty, name = "")
354
571
  Instruction.from_ptr(C.LLVMBuildFPExt(self, val, LLVM::Type(ty), name))
355
572
  end
356
573
 
357
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
358
580
  # @LLVMinst ptrtoint
359
581
  def ptr2int(val, ty, name = "")
360
582
  Instruction.from_ptr(C.LLVMBuildPtrToInt(self, val, LLVM::Type(ty), name))
361
583
  end
362
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
363
591
  # @LLVMinst inttoptr
364
592
  def int2ptr(val, ty, name = "")
365
593
  Instruction.from_ptr(C.LLVMBuildIntToPtr(self, val, LLVM::Type(ty), name))
366
594
  end
367
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
368
601
  # @LLVMinst bitcast
369
602
  def bit_cast(val, ty, name = "")
370
603
  Instruction.from_ptr(C.LLVMBuildBitCast(self, val, LLVM::Type(ty), name))
371
604
  end
372
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]
373
610
  # @LLVMinst zext
374
611
  # @LLVMinst bitcast
375
612
  def zext_or_bit_cast(val, ty, name = "")
376
613
  Instruction.from_ptr(C.LLVMBuildZExtOrBitCast(self, val, LLVM::Type(ty), name))
377
614
  end
378
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]
379
620
  # @LLVMinst sext
380
621
  # @LLVMinst bitcast
381
622
  def sext_or_bit_cast(val, ty, name = "")
382
623
  Instruction.from_ptr(C.LLVMBuildSExtOrBitCast(self, val, LLVM::Type(ty), name))
383
624
  end
384
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]
385
630
  # @LLVMinst trunc
386
631
  # @LLVMinst bitcast
387
632
  def trunc_or_bit_cast(val, ty, name = "")
388
633
  Instruction.from_ptr(C.LLVMBuildTruncOrBitCast(self, val, LLVM::Type(ty), name))
389
634
  end
390
635
 
391
- # Builds a pointer cast Instruction with the given name.
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]
392
640
  def pointer_cast(val, ty, name = "")
393
641
  Instruction.from_ptr(C.LLVMBuildPointerCast(self, val, LLVM::Type(ty), name))
394
642
  end
395
643
 
396
- # Builds a int cast Instruction with the given name.
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]
397
648
  def int_cast(val, ty, name = "")
398
649
  Instruction.from_ptr(C.LLVMBuildIntCast(self, val, LLVM::Type(ty), name))
399
650
  end
400
651
 
401
- # Builds a fp cast Instruction with the given name.
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]
402
656
  def fp_cast(val, ty, name = "")
403
657
  Instruction.from_ptr(C.LLVMBuildFPCast(self, val, LLVM::Type(ty), name))
404
658
  end
@@ -415,6 +669,13 @@ module LLVM
415
669
  # :sge - signed greater than or equal to
416
670
  # :slt - signed less than
417
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
418
679
  # @LLVMinst icmp
419
680
  def icmp(pred, lhs, rhs, name = "")
420
681
  Instruction.from_ptr(C.LLVMBuildICmp(self, pred, lhs, rhs, name))
@@ -438,27 +699,39 @@ module LLVM
438
699
  # :sle - unordered and less than or equal to
439
700
  # :true - always true and folded
440
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
441
709
  # @LLVMinst fcmp
442
710
  def fcmp(pred, lhs, rhs, name = "")
443
711
  Instruction.from_ptr(C.LLVMBuildFCmp(self, pred, lhs, rhs, name))
444
712
  end
445
713
 
446
- # Builds a Phi node of the given Type with the given incoming branches.
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
447
722
  # @LLVMinst phi
448
- def phi(ty, *incoming)
449
- if incoming.last.kind_of? String
450
- name = incoming.pop
451
- else
452
- name = ""
453
- end
723
+ def phi(ty, incoming, name = "")
454
724
 
455
725
  phi = Phi.from_ptr(C.LLVMBuildPhi(self, LLVM::Type(ty), name))
456
- phi.add_incoming(*incoming) unless incoming.empty?
726
+ phi.add_incoming(incoming)
457
727
  phi
458
728
  end
459
729
 
460
730
  # Builds a call Instruction. Calls the given Function with the given
461
731
  # args (Instructions).
732
+ # @param [LLVM::Function] fun
733
+ # @param [Array<LLVM::Value>] args
734
+ # @param [LLVM::Instruction]
462
735
  # @LLVMinst call
463
736
  def call(fun, *args)
464
737
  raise "No fun" if fun.nil?
@@ -473,57 +746,104 @@ module LLVM
473
746
  CallInst.from_ptr(C.LLVMBuildCall(self, fun, args_ptr, args.size, name))
474
747
  end
475
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
476
759
  # @LLVMinst select
477
760
  def select(_if, _then, _else, name = "")
478
761
  Instruction.from_ptr(C.LLVMBuildSelect(self, _if, _then, _else, name))
479
762
  end
480
763
 
481
- # @LLVMinst va_arg
482
- def va_arg(list, ty, name = "")
483
- Instruction.from_ptr(C.LLVMBuildVAArg(self, list, LLVM::Type(ty), name))
484
- end
485
-
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
486
770
  # @LLVMinst extractelement
487
- def extract_element(vector, index, name = "")
488
- Instruction.from_ptr(C.LLVMBuildExtractElement(self, vector, index, name))
771
+ def extract_element(vector, idx, name = "")
772
+ Instruction.from_ptr(C.LLVMBuildExtractElement(self, vector, idx, name))
489
773
  end
490
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'
491
781
  # @LLVMinst insertelement
492
- def insert_element(vector, elem, index, name = "")
493
- Instruction.from_ptr(C.LLVMBuildInsertElement(self, vector, elem, index, name))
782
+ def insert_element(vector, elem, idx, name = "")
783
+ Instruction.from_ptr(C.LLVMBuildInsertElement(self, vector, elem, idx, name))
494
784
  end
495
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
496
793
  # @LLVMinst shufflevector
497
794
  def shuffle_vector(vec1, vec2, mask, name = "")
498
795
  Instruction.from_ptr(C.LLVMBuildShuffleVector(self, vec1, vec2, mask, name))
499
796
  end
500
797
 
501
- # LLVMinst extractvalue
502
- def extract_value(aggregate, index, name = "")
503
- Instruction.from_ptr(C.LLVMBuildExtractValue(self, aggregate, index, name))
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))
504
806
  end
505
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'
506
814
  # @LLVMinst insertvalue
507
- def insert_value(aggregate, elem, index, name = "")
508
- Instruction.from_ptr(C.LLVMBuildInsertValue(self, aggregate, elem, index, name))
815
+ def insert_value(aggregate, elem, idx, name = "")
816
+ Instruction.from_ptr(C.LLVMBuildInsertValue(self, aggregate, elem, idx, name))
509
817
  end
510
818
 
511
- # Check if a value is null.
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
512
823
  def is_null(val, name = "")
513
824
  Instruction.from_ptr(C.LLVMBuildIsNull(self, val, name))
514
825
  end
515
826
 
516
- # Check if a value is not null.
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
517
831
  def is_not_null(val, name = "")
518
832
  Instruction.from_ptr(C.LLVMBuildIsNotNull(self, val, name))
519
833
  end
520
834
 
521
- # Retrieves the pointer difference between the given lhs and rhs.
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
522
841
  def ptr_diff(lhs, rhs, name = "")
523
842
  Instruction.from_ptr(C.LLVMBuildPtrDiff(lhs, rhs, name))
524
843
  end
525
844
 
526
845
  # Disposes the builder.
846
+ # @return nil
527
847
  def dispose
528
848
  C.LLVMDisposeBuilder(@ptr)
529
849
  end