rltk 1.2.0 → 2.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.
@@ -0,0 +1,1095 @@
1
+ # Author: Chris Wailes <chris.wailes@gmail.com>
2
+ # Project: Ruby Language Toolkit
3
+ # Date: 2012/03/20
4
+ # Description: This file defines the Builder class.
5
+
6
+ ############
7
+ # Requires #
8
+ ############
9
+
10
+ # Ruby Language Toolkit
11
+ require 'rltk/cg/bindings'
12
+ require 'rltk/cg/instruction'
13
+
14
+ #######################
15
+ # Classes and Modules #
16
+ #######################
17
+
18
+ module RLTK::CG # :nodoc:
19
+
20
+ # This class is responsible for adding {Instruction Instructions} to {BasicBlock BasicBlocks}.
21
+ class Builder
22
+ include BindingClass
23
+
24
+ # @return [Builder] A global Builder object.
25
+ def self.global
26
+ @@global_builder ||= Builder.new
27
+ end
28
+
29
+ # Creates a new Builder object, optionally positioning it at the end
30
+ # of *block*.
31
+ #
32
+ # @param [BasicBlock, nil] block BasicBlock used to position the Builder.
33
+ def initialize(block = nil)
34
+ @ptr = Bindings.create_builder
35
+
36
+ position_at_end(block) if block
37
+ end
38
+
39
+ # Frees the resources used by LLVM for this Builder.
40
+ #
41
+ # @return [void]
42
+ def dispose
43
+ if @ptr
44
+ Bindings.dispose_builder(@ptr)
45
+ @ptr = nil
46
+ end
47
+ end
48
+
49
+ # Executes a given block inside the context of this builder. If the
50
+ # *bb* parameter isn't nill, the Builder will be positioned at the
51
+ # end of the specified BasicBlock.
52
+ #
53
+ # @param [BasicBlock] bb Optional BasicBlock used to position the Builder.
54
+ # @param [Array<Object>] block_args Arguments to be passed to *block*.
55
+ # @param [Proc] block Block to execute in the context of this Builder.
56
+ #
57
+ # @return [Object] The result of evaluating *block* in the context of this Builder.
58
+ def build(bb = nil, *block_args, &block)
59
+ self.position_at_end(bb) if bb
60
+ self.instance_exec(*block_args, &block)
61
+ end
62
+
63
+ # Build an instruction.
64
+ #
65
+ # @param [Symbol] inst Name of instruction building method.
66
+ # @param [Array<Object>] args Arguments to be passed to building method.
67
+ #
68
+ # @return [Instruction] Build instruction.
69
+ def build_inst(inst, *args)
70
+ self.send(inst.to_sym, *args)
71
+ end
72
+ alias :'<<' :build_inst
73
+
74
+ # Position the Builder after the given instruction.
75
+ #
76
+ # @param [BasicBlock] block
77
+ # @param [Instruction] instruction
78
+ #
79
+ # @return [Builder] self
80
+ def position(block, instruction)
81
+ Bindings.position_builder(@ptr, block, instruction)
82
+ self
83
+ end
84
+
85
+ # Position the Builder at the end of the given BasicBlock.
86
+ #
87
+ # @param [BasicBlock] block
88
+ #
89
+ # @return [Bulder] self
90
+ def position_at_end(block)
91
+ Bindings.position_builder_at_end(@ptr, block)
92
+ self
93
+ end
94
+
95
+ # Position the Builder before the given Instruction.
96
+ #
97
+ # @param [Instruction] instruction
98
+ #
99
+ # @return [Builder] self
100
+ def position_before(instruction)
101
+ Bindings.position_builder_before(@ptr, instruction)
102
+ self
103
+ end
104
+
105
+ ################################
106
+ # Instruction Building Methods #
107
+ ################################
108
+
109
+ #################
110
+ # Miscellaneous #
111
+ #################
112
+
113
+ # @return [BasicBlock] BasicBlock the Builder is currently positioned on.
114
+ def current_block
115
+ BasicBlock.new(Bindings.get_insert_block(@ptr))
116
+ end
117
+ alias :insertion_block :current_block
118
+
119
+ # Generates an instruction with no defined semantics. Can be used to
120
+ # provide hints to the optimizer.
121
+ #
122
+ # @return [UnreachableInst]
123
+ def unreachable
124
+ UnreachableInst.new(Bindings.build_unreachable(@ptr))
125
+ end
126
+
127
+ ###########
128
+ # Returns #
129
+ ###########
130
+
131
+ # @param [Value] val The Value to return.
132
+ #
133
+ # @return [ReturnInst]
134
+ def ret(val)
135
+ ReturnInst.new(Bindings.build_ret(@ptr, val))
136
+ end
137
+
138
+ # @return [RetVoidInst]
139
+ def ret_void
140
+ ReturnVoidInst.new(Bindings.build_ret_void(@ptr))
141
+ end
142
+
143
+ # @return [RetAggregateInst]
144
+ def ret_aggregate(*vals)
145
+ vals = vals.first if vals.length == 1 and vals.first.instance_of?(::Array)
146
+
147
+ vals_ptr = FFI::MemoryPointer.new(:pointer, vals.length)
148
+ vals_ptr.write_array_of_pointer(vals)
149
+
150
+ ReturnAggregateInst.new(Bindings.build_aggregate_ret(@ptr, vals_ptr, vals.length))
151
+ end
152
+
153
+ ################
154
+ # Control Flow #
155
+ ################
156
+
157
+ # Unconditional branching.
158
+ #
159
+ # @param [BasicBlock] block Where to jump.
160
+ #
161
+ # @return [BranchInst]
162
+ def branch(block)
163
+ BranchInst.new(Bindings.build_br(@ptr, block))
164
+ end
165
+ alias :br :branch
166
+
167
+ # Build an instruction that performs a function call.
168
+ #
169
+ # @param [Function] fun Function to call.
170
+ # @param [Array<Value>] args Arguments to pass to function.
171
+ #
172
+ # @return [CallInst]
173
+ def call(fun, *args)
174
+ name = if args.last.is_a?(String) then args.pop else '' end
175
+
176
+ args_ptr = FFI::MemoryPointer.new(:pointer, args.length)
177
+ args_ptr.write_array_of_pointer(args)
178
+
179
+ CallInst.new(Bindings.build_call(@ptr, fun, args_ptr, args.length, name))
180
+ end
181
+
182
+ # Conditional branching.
183
+ #
184
+ # @param [Value] val Condition value.
185
+ # @param [BasicBlock] iffalse Where to jump if condition is true.
186
+ # @param [BasicBlock] iftrue Where to jump if condition is false.
187
+ #
188
+ # @return [CondBranchInst]
189
+ def cond_branch(val, iftrue, iffalse)
190
+ CondBranchInst.new(Bindings.build_cond_br(@ptr, val, iftrue, iffalse))
191
+ end
192
+ alias :cond :cond_branch
193
+
194
+ # Extract an element from a vector.
195
+ #
196
+ # @param [Value] vector Vector from which to extract a value.
197
+ # @param [Value] index Index of the element to extract, an unsigned integer.
198
+ # @param [String] name Value of the result in LLVM IR.
199
+ #
200
+ # @return [ExtractElementInst] The extracted element.
201
+ def extract_element(vector, index, name = '')
202
+ ExtractElementInst.new(Bindings.build_extract_element(@ptr, vector, index, name))
203
+ end
204
+
205
+ # Extract the value of a member field from an aggregate value.
206
+ #
207
+ # @param [Value] aggregate An aggregate value.
208
+ # @param [Value] index Index of the member to extract.
209
+ # @param [String] name Name of the result in LLVM IR.
210
+ #
211
+ # @return [ExtractValueInst] The extracted value.
212
+ def extract_value(aggregate, index, name = '')
213
+ ExtractValueInst.new(Bindings.build_extract_value(@ptr, aggregate, index, name))
214
+ end
215
+
216
+ # Insert an element into a vector.
217
+ #
218
+ # @param [Value] vector Vector into which to insert the element.
219
+ # @param [Value] element Element to be inserted into the vector.
220
+ # @param [Value] index Index at which to insert the element.
221
+ # @param [String] name Name of the result in LLVM IR.
222
+ #
223
+ # @return [InsertElementInst] A vector the same type as *vector*.
224
+ def insert_element(vector, element, index, name = '')
225
+ InsertElementInst.new(Bindings.build_insert_element(@ptr, vector, element, index, name))
226
+ end
227
+
228
+ # Insert a value into an aggregate value's member field.
229
+ #
230
+ # @param [Value] aggregate An aggregate value.
231
+ # @param [Value] val Value to insert into *aggregate*.
232
+ # @param [Value] index Index at which to insert the value.
233
+ # @param [String] name Name of the result in LLVM IR.
234
+ #
235
+ # @return [InsertValueInst] An aggregate value of the same type as *aggregate*.
236
+ def insert_value(aggregate, val, index, name = '')
237
+ InsertValueInst.new(Bindings.build_insert_value(@ptr, aggregate, val, index, name))
238
+ end
239
+
240
+ # Invoke a function which may potentially unwind.
241
+ #
242
+ # @param [Function] fun Function to invoke.
243
+ # @param [Array<Value>] args Arguments passed to fun.
244
+ # @param [BasicBlock] normal Where to jump if fun does not unwind.
245
+ # @param [BasicBlock] exception Where to jump if fun unwinds.
246
+ # @param [String] name Name of the result in LLVM IR.
247
+ #
248
+ # @return [InvokeInst] The value returned by *fun*, unless an unwind instruction occurs.
249
+ def invoke(fun, args, normal, exception, name = '')
250
+ InvokeInst.new(Bindings.build_invoke(@ptr, fun, args, args.length, normal, exception, name))
251
+ end
252
+
253
+ # Build a Phi node of the given type with the given incoming
254
+ # branches.
255
+ #
256
+ # @param [Type] type Specifies the result type.
257
+ # @param [Hash{BasicBlock => Value}] incoming A hash mapping basic blocks to a
258
+ # corresponding value. If the phi node is jumped to from a given basic block,
259
+ # the phi instruction takes on its corresponding value.
260
+ # @param [String] name Name of the result in LLVM IR.
261
+ #
262
+ # @return [PhiInst] The phi node.
263
+ def phi(type, incoming, name = '')
264
+ returning PhiInst.new(Bindings.build_phi(@ptr, check_cg_type(type), name)) do |phi|
265
+ phi.incoming.add(incoming)
266
+ end
267
+ end
268
+
269
+ # Return a value based on a condition. This differs from *cond* in
270
+ # that its operands are values rather than basic blocks. As a
271
+ # consequence, both arguments must be evaluated.
272
+ #
273
+ # @param [Value] if_val An Int1 or a vector of Int1.
274
+ # @param [Value] then_val Value or vector of the same arity as *if_val*.
275
+ # @param [Value] else_val Value or vector of values of the same arity
276
+ # as *if_val*, and of the same type as *then_val*.
277
+ # @param [String] name Name of the result in LLVM IR.
278
+ #
279
+ # @return [SelectInst] An instruction representing either *then_val* or *else_val*.
280
+ def select(if_val, then_val, else_val, name = '')
281
+ SelectInst.new(Bindings.build_select(@ptr, if_val, then_val, else_val, name))
282
+ end
283
+
284
+ # Shuffle two vectors according to a given mask.
285
+ #
286
+ # @param [Value] vec1 Vector
287
+ # @param [Value] vec2 Vector of the same type and arity as *vec1*.
288
+ # @param [Value] mask Vector of Int1 of the same arity as *vec1* and *vec2*.
289
+ # @param [String] name Name of the result in LLVM IR.
290
+ #
291
+ # @return [ShuffleVectorInst] The shuffled vector.
292
+ def shuffle_vector(vec1, vec2, mask, name = '')
293
+ ShuffleVectorInst.new(Bindings.build_shuffle_vector(@ptr, vec1, vec2, mask, name))
294
+ end
295
+
296
+ # Select a value based on an incoming value.
297
+ # @param [Value] val Value to switch on.
298
+ # @param [BasicBlock] default Default case.
299
+ # @param [Hash{Value => BasicBlock}] cases Hash mapping values
300
+ # to basic blocks. When a value is matched, control will jump to
301
+ # the corresponding basic block.
302
+ #
303
+ # @return [SwitchInst]
304
+ def switch(val, default, cases)
305
+ returning SwitchInst.new(Bindings.build_switch(@ptr, val, default, cases.size)) do |inst|
306
+ cases.each { |val, block| inst.add_case(val, block) }
307
+ end
308
+ end
309
+
310
+ ########
311
+ # Math #
312
+ ########
313
+
314
+ # Addition
315
+
316
+ # @param [Value] lhs Integer or vector of integers.
317
+ # @param [Value] rhs Integer or vector of integers.
318
+ # @param [String] name Name of the result in LLVM IR.
319
+ #
320
+ # @return [AddInst] The integer sum of the two operands.
321
+ def add(lhs, rhs, name = '')
322
+ AddInst.new(Bindings.build_add(@ptr, lhs, rhs, name))
323
+ end
324
+
325
+ # @param [Value] lhs Floating point or vector of floating points.
326
+ # @param [Value] rhs Floating point or vector of floating points.
327
+ # @param [String] name Name of the result in LLVM IR.
328
+ #
329
+ # @return [FAddInst] The floating point sum of the two operands.
330
+ def fadd(lhs, rhs, name = '')
331
+ FAddInst.new(Bindings.build_f_add(@ptr, lhs, rhs, name))
332
+ end
333
+
334
+ # No signed wrap addition.
335
+ #
336
+ # @param [Value] lhs Integer or vector of integers.
337
+ # @param [Value] rhs Integer or vector of integers.
338
+ # @param [String] name Name of the result in LLVM IR.
339
+ #
340
+ # @return [NSWAddInst] The integer sum of the two operands.
341
+ def nsw_add(lhs, rhs, name = '')
342
+ NSWAddInst.new(Bindings.build_nsw_add(@ptr, lhs, rhs, name))
343
+ end
344
+
345
+ # No unsigned wrap addition.
346
+ #
347
+ # @param [Value] lhs Integer or vector of integers.
348
+ # @param [Value] rhs Integer or vector of integers.
349
+ # @param [String] name Name of the result in LLVM IR.
350
+ #
351
+ # @return [NSWAddInst] The integer sum of the two operands.
352
+ def nuw_add(lhs, rhs, name = '')
353
+ NUWAddInst.new(Bindings.build_nuw_add(@ptr, lhs, rhs, name))
354
+ end
355
+
356
+ # Subtraction
357
+
358
+ # @param [Value] lhs Integer or vector of integers.
359
+ # @param [Value] rhs Integer or vector of integers.
360
+ # @param [String] name Name of the result in LLVM IR.
361
+ #
362
+ # @return [SubInst] The integer difference of the two operands.
363
+ def sub(lhs, rhs, name = '')
364
+ SubInst.new(Bindings.build_sub(@ptr, lhs, rhs, name))
365
+ end
366
+
367
+ # @param [Value] lhs Floating point or vector of floating points.
368
+ # @param [Value] rhs Floating point or vector of floating points.
369
+ # @param [String] name Name of the result in LLVM IR.
370
+ #
371
+ # @return [FSubInst] The floating point difference of the two operands.
372
+ def fsub(lhs, rhs, name = '')
373
+ FSubInst.new(Bindings.build_f_sub(@ptr, lhs, rhs, name))
374
+ end
375
+
376
+ # No signed wrap subtraction.
377
+ #
378
+ # @param [Value] lhs Integer or vector of integers.
379
+ # @param [Value] rhs Integer or vector of integers.
380
+ # @param [String] name Name of the result in LLVM IR.
381
+ #
382
+ # @return [SubInst] The integer difference of the two operands.
383
+ def nsw_sub(lhs, rhs, name = '')
384
+ NSWSubInst.new(Bindings.build_nsw_sub(@ptr, lhs, rhs, name))
385
+ end
386
+
387
+ # No unsigned wrap subtraction.
388
+ #
389
+ # @param [Value] lhs Integer or vector of integers.
390
+ # @param [Value] rhs Integer or vector of integers.
391
+ # @param [String] name Name of the result in LLVM IR.
392
+ #
393
+ # @return [SubInst] The integer difference of the two operands.
394
+ def nuw_sub(lhs, rhs, name = '')
395
+ NUWSubInst.new(Bindings.build_nuw_sub(@ptr, lhs, rhs, name))
396
+ end
397
+
398
+ # Multiplication
399
+
400
+ # @param [Value] lhs Integer or vector of integers.
401
+ # @param [Value] rhs Integer or vector of integers.
402
+ # @param [String] name Name of the result in LLVM IR.
403
+ #
404
+ # @return [MulInst] The integer product of the two operands.
405
+ def mul(lhs, rhs, name = '')
406
+ MulInst.new(Bindings.build_mul(@ptr, lhs, rhs, name))
407
+ end
408
+
409
+ # @param [Value] lhs Floating point or vector of floating points.
410
+ # @param [Value] rhs Floating point or vector of floating points.
411
+ # @param [String] name Name of the result in LLVM IR.
412
+ #
413
+ # @return [FMulInst] The floating point product of the two operands.
414
+ def fmul(lhs, rhs, name = '')
415
+ FMulInst.new(Bindings.build_f_mul(@ptr, lhs, rhs, name))
416
+ end
417
+
418
+ # No signed wrap multiplication.
419
+ #
420
+ # @param [Value] lhs Integer or vector of integers.
421
+ # @param [Value] rhs Integer or vector of integers.
422
+ # @param [String] name Name of the result in LLVM IR.
423
+ #
424
+ # @return [MulInst] The integer product of the two operands.
425
+ def nsw_mul(lhs, rhs, name = '')
426
+ NSWMulInst.new(Bindings.build_nsw_mul(@ptr, lhs, rhs, name))
427
+ end
428
+
429
+ # No unsigned wrap multiplication.
430
+ #
431
+ # @param [Value] lhs Integer or vector of integers.
432
+ # @param [Value] rhs Integer or vector of integers.
433
+ # @param [String] name Name of the result in LLVM IR.
434
+ #
435
+ # @return [MulInst] The integer product of the two operands.
436
+ def nuw_mul(lhs, rhs, name = '')
437
+ NUWMulInst.new(Bindings.build_nuw_mul(@ptr, lhs, rhs, name))
438
+ end
439
+
440
+ # Division
441
+
442
+ # @param [Value] lhs Floating point or vector of floating points.
443
+ # @param [Value] rhs Floating point or vector of floating points.
444
+ # @param [String] name Name of the result in LLVM IR.
445
+ #
446
+ # @return [FDivInst] The floating point quotient of the two operands.
447
+ def fdiv(lhs, rhs, name = '')
448
+ FDivInst.new(Bindings.build_f_div(@ptr, lhs, rhs, name))
449
+ end
450
+
451
+ # Signed integer division.
452
+ #
453
+ # @param [Value] lhs Integer or vector of integers.
454
+ # @param [Value] rhs Integer or vector of integers.
455
+ # @param [String] name Name of the result in LLVM IR.
456
+ #
457
+ # @return [SDivInst] The integer quotient of the two operands.
458
+ def sdiv(lhs, rhs, name = '')
459
+ SDivInst.new(Bindings.build_s_div(@ptr, lhs, rhs, name))
460
+ end
461
+
462
+ # Signed exact integer division.
463
+ #
464
+ # @param [Value] lhs Integer or vector of integers.
465
+ # @param [Value] rhs Integer or vector of integers.
466
+ # @param [String] name Name of the result in LLVM IR.
467
+ #
468
+ # @return [SDivInst] The integer quotient of the two operands.
469
+ def exact_sdiv(lhs, rhs, name = '')
470
+ ExactSDivInst.new(Bindings.build_exact_s_div(@ptr, lhs, rhs, name))
471
+ end
472
+
473
+ # Unsigned integer division.
474
+ #
475
+ # @param [Value] lhs Integer or vector of integers.
476
+ # @param [Value] rhs Integer or vector of integers.
477
+ # @param [String] name Name of the result in LLVM IR.
478
+ #
479
+ # @return [SDivInst] The integer quotient of the two operands.
480
+ def udiv(lhs, rhs, name = '')
481
+ UDivInst.new(Bindings.build_u_div(@ptr, lhs, rhs, name))
482
+ end
483
+
484
+ # Remainder
485
+
486
+ # @param [Value] lhs Floating point or vector of floating points.
487
+ # @param [Value] rhs Floating point or vector of floating points.
488
+ # @param [String] name Name of the result in LLVM IR.
489
+ #
490
+ # @return [FRemInst] The floating point remainder.
491
+ def frem(lhs, rhs, name = '')
492
+ FRemInst.new(Bindings.build_f_rem(@ptr, lhs, rhs, name))
493
+ end
494
+
495
+ # Signed remainder.
496
+ #
497
+ # @param [Value] lhs Integer or vector of integers.
498
+ # @param [Value] rhs Integer or vector of integers.
499
+ # @param [String] name Name of the result in LLVM IR.
500
+ #
501
+ # @return [SRemInst] The integer remainder.
502
+ def srem(lhs, rhs, name = '')
503
+ SRemInst.new(Bindings.build_s_rem(@ptr, lhs, rhs, name))
504
+ end
505
+
506
+ # Unsigned remainder.
507
+ #
508
+ # @param [Value] lhs Integer or vector of integers.
509
+ # @param [Value] rhs Integer or vector of integers.
510
+ # @param [String] name Name of the result in LLVM IR.
511
+ #
512
+ # @return [SRemInst] The integer remainder.
513
+ def urem(lhs, rhs, name = '')
514
+ URemInst.new(Bindings.build_u_rem(@ptr, lhs, rhs, name))
515
+ end
516
+
517
+ # Negation
518
+
519
+ # Integer negation. Implemented as a shortcut to the equivalent sub
520
+ # instruction.
521
+ #
522
+ # @param [Value] val Integer or vector of integers.
523
+ # @param [String] name Name of the result in LLVM IR.
524
+ #
525
+ # @return [NegInst] The negated operand.
526
+ def neg(val, name = '')
527
+ NegInst.new(Bindings.build_neg(@ptr, val, name))
528
+ end
529
+
530
+ # Floating point negation. Implemented as a shortcut to the
531
+ # equivalent sub instruction.
532
+ #
533
+ # @param [Value] val Floating point or vector of floating points.
534
+ # @param [String] name Name of the result in LLVM IR.
535
+ #
536
+ # @return [NegInst] The negated operand.
537
+ def fneg(val, name = '')
538
+ FNegInst.new(Bindings.build_f_neg(@ptr, val, name))
539
+ end
540
+
541
+ # No signed wrap integer negation. Implemented as a shortcut to the
542
+ # equivalent sub instruction.
543
+ #
544
+ # @param [Value] val Integer or vector of integers.
545
+ # @param [String] name Name of the result in LLVM IR.
546
+ #
547
+ # @return [NegInst] The negated operand.
548
+ def nsw_neg(val, name = '')
549
+ NSWNegInst.new(Bindings.build_nsw_neg(@ptr, val, name))
550
+ end
551
+
552
+ # No unsigned wrap integer negation. Implemented as a shortcut to the
553
+ # equivalent sub instruction.
554
+ #
555
+ # @param [Value] val Integer or vector of integers.
556
+ # @param [String] name Name of the result in LLVM IR.
557
+ #
558
+ # @return [NegInst] The negated operand.
559
+ def nuw_neg(val, name = '')
560
+ NUWNegInst.new(Bindings.build_nuw_neg(@ptr, val, name))
561
+ end
562
+
563
+ ######################
564
+ # Bitwise Operations #
565
+ ######################
566
+
567
+ # A wrapper method around the {#shift_left} and {#shift_right}
568
+ # methods.
569
+ #
570
+ # @param [:left, :right] dir The direction to shift.
571
+ # @param [Value] lhs Integer or vector of integers.
572
+ # @param [Value] rhs Integer or vector of integers.
573
+ # @param [:arithmetic, :logical] mode Shift mode for right shifts.
574
+ # @param [String] name Name of the result in LLVM IR.
575
+ #
576
+ # @return [LeftShiftInst, ARightShiftInst, LRightShiftInst] An integer instruction.
577
+ def shift(dir, lhs, rhs, mode = :arithmetic, name = '')
578
+ case dir
579
+ when :left then shift_left(lhs, rhs, name)
580
+ when :right then shift_right(lhs, rhs, mode, name)
581
+ end
582
+ end
583
+
584
+ # @param [Value] lhs Integer or vector of integers
585
+ # @param [Value] rhs Integer or vector of integers
586
+ # @param [String] name Name of the result in LLVM IR
587
+ #
588
+ # @return [LeftShiftInst] An integer instruction.
589
+ def shift_left(lhs, rhs, name = '')
590
+ LeftShiftInst.new(Bindings.build_shl(@ptr, lhs, rhs, name))
591
+ end
592
+ alias :shl :shift_left
593
+
594
+ # A wrapper function around {#ashr} and {#lshr}.
595
+ #
596
+ # @param [Value] lhs Integer or vector of integers
597
+ # @param [Value] rhs Integer or vector of integers
598
+ # @param [:arithmetic, :logical] mode The filling mode.
599
+ # @param [String] name Name of the result in LLVM IR
600
+ #
601
+ # @return [LeftShiftInst] An integer instruction.
602
+ def shift_right(lhs, rhs, mode = :arithmetic, name = '')
603
+ case mode
604
+ when :arithmetic then ashr(lhs, rhs, name)
605
+ when :logical then lshr(lhs, rhs, name)
606
+ end
607
+ end
608
+
609
+ # Arithmetic (sign extended) shift right.
610
+ #
611
+ # @param [Value] lhs Integer or vector of integers.
612
+ # @param [Value] rhs Integer or vector of integers.
613
+ # @param [String] name Name of the result in LLVM IR.
614
+ #
615
+ # @return [ARightShiftInst] An integer instruction.
616
+ def ashr(lhs, rhs, name = '')
617
+ ARightShiftInst.new(Bindings.build_a_shr(@ptr, lhs, rhs, name))
618
+ end
619
+
620
+ # Logical (zero fill) shift right.
621
+ #
622
+ # @param [Value] lhs Integer or vector of integers.
623
+ # @param [Value] rhs Integer or vector of integers.
624
+ # @param [String] name Name of the result in LLVM IR.
625
+ #
626
+ # @return [ARightShiftInst] An integer instruction.
627
+ def lshr(lhs, rhs, name = '')
628
+ LRightShiftInst.new(Bindings.build_l_shr(@ptr, lhs, rhs, name))
629
+ end
630
+
631
+ # @param [Value] lhs Integer or vector of integers.
632
+ # @param [Value] rhs Integer or vector of integers.
633
+ # @param [String] name Name of the result in LLVM IR.
634
+ #
635
+ # @return [AndInst] An integer instruction.
636
+ def and(lhs, rhs, name = '')
637
+ AndInst.new(Bindings.build_and(@ptr, lhs, rhs, name))
638
+ end
639
+
640
+ # @param [Value] lhs Integer or vector of integers.
641
+ # @param [Value] rhs Integer or vector of integers.
642
+ # @param [String] name Name of the result in LLVM IR.
643
+ #
644
+ # @return [OrInst] An integer instruction.
645
+ def or(lhs, rhs, name = '')
646
+ OrInst.new(Bindings.build_or(@ptr, lhs, rhs, name))
647
+ end
648
+
649
+ # @param [Value] lhs Integer or vector of integers.
650
+ # @param [Value] rhs Integer or vector of integers.
651
+ # @param [String] name Name of the result in LLVM IR.
652
+ #
653
+ # @return [XOrInst] An integer instruction.
654
+ def xor(lhs, rhs, name = '')
655
+ XOrInst.new(Bindings.build_xor(@ptr, lhs, rhs, name))
656
+ end
657
+
658
+ # Boolean negation.
659
+ #
660
+ # @param [Value] val Integer or vector of integers.
661
+ # @param [String] name Name of the result in LLVM IR.
662
+ #
663
+ # @return [NotInst] An integer instruction.
664
+ def not(val, name = '')
665
+ NotInst.new(Bindings.build_not(@ptr, val, name))
666
+ end
667
+
668
+ #####################
669
+ # Memory Management #
670
+ #####################
671
+
672
+ # Heap allocation.
673
+ #
674
+ # @param [Type] type Type or value whose type should be malloced.
675
+ # @param [String] name Name of the result in LLVM IR.
676
+ #
677
+ # @return [MallocInst] A pointer to the malloced bytes.
678
+ def malloc(type, name = '')
679
+ MallocInst.new(Bindings.build_malloc(@ptr, check_type(type), name))
680
+ end
681
+
682
+ # Heap array allocation.
683
+ #
684
+ # @param [Type] type Type or value whose type will be the element type of the malloced array.
685
+ # @param [Value] size Unsigned integer representing size of the array.
686
+ # @param [String] name Name of the result in LLVM IR.
687
+ #
688
+ # @return [ArrayMallocInst] A pointer to the malloced array
689
+ def array_malloc(type, size, name = '')
690
+ ArrayMallocInst.new(Bindings.build_array_malloc(@ptr, check_cg_type(type), size, name))
691
+ end
692
+
693
+ # Stack allocation.
694
+ #
695
+ # @param [Type] type Type or value whose type should be allocad.
696
+ # @param [String] name Name of the result in LLVM IR.
697
+ #
698
+ # @return [AllocaInst] A pointer to the allocad bytes.
699
+ def alloca(type, name = '')
700
+ AllocaInst.new(Bindings.build_alloca(@ptr, check_cg_type(type), name))
701
+ end
702
+
703
+ # Stack array allocation.
704
+ #
705
+ # @param [Type] type Type or value whose type should be allocad.
706
+ # @param [Value] size Unsigned integer representing size of the array.
707
+ # @param [String] name Name of the result in LLVM IR.
708
+ #
709
+ # @return [ArrayAllocaInst] A pointer to the allocad bytes.
710
+ def array_alloca(type, size, name = '')
711
+ ArrayAllocaInst.new(Bindings.build_array_alloca(@ptr, check_cg_type(type), size, name))
712
+ end
713
+
714
+ # @param [LLVM::Value] ptr The pointer to be freed.
715
+ #
716
+ # @return [FreeInst] The result of the free instruction.
717
+ def free(ptr)
718
+ FreeInst.new(Bindings.build_free(@ptr, ptr))
719
+ end
720
+
721
+ # Load the value of a given pointer.
722
+ #
723
+ # @param [Value] ptr Pointer to be loaded.
724
+ # @param [String] name Name of the result in LLVM IR.
725
+ #
726
+ # @return [LoadInst] The result of the load operation. Represents a value of the pointer's type.
727
+ def load(ptr, name = '')
728
+ LoadInst.new(Bindings.build_load(@ptr, ptr, name))
729
+ end
730
+
731
+ # Store a value at a given pointer.
732
+ #
733
+ # @param [Value] val The value to be stored.
734
+ # @param [Value] ptr Pointer to the same type as val.
735
+ #
736
+ # @return [StoreInst] The result of the store operation.
737
+ def store(val, ptr)
738
+ StoreInst.new(Bindings.build_store(@ptr, val, ptr))
739
+ end
740
+
741
+ # Obtain a pointer to the element at the given indices.
742
+ #
743
+ # @param [Value] ptr Pointer to an aggregate value
744
+ # @param [Array<Value>] indices Ruby array of Value representing
745
+ # indices into the aggregate.
746
+ # @param [String] name The name of the result in LLVM IR.
747
+ #
748
+ # @return [GetElementPtrInst] The resulting pointer.
749
+ def get_element_ptr(ptr, indices, name = '')
750
+ check_array_type(indices, Value, 'indices')
751
+
752
+ indices_ptr = FFI::MemoryPointer.new(:pointer, indices.length)
753
+ indices_ptr.write_array_of_pointer(indices)
754
+
755
+ GetElementPtrInst.new(Bindings.build_gep(@ptr, ptr, indices_ptr, indices.length, name))
756
+ end
757
+ alias :gep :get_element_ptr
758
+
759
+ # Builds a in-bounds getelementptr instruction. If the indices are
760
+ # outside the allocated pointer the value is undefined.
761
+ #
762
+ # @param [Value] ptr Pointer to an aggregate value
763
+ # @param [Array<Value>] indices Ruby array of Value representing
764
+ # indices into the aggregate.
765
+ # @param [String] name The name of the result in LLVM IR.
766
+ #
767
+ # @return [InBoundsGEPInst] The resulting pointer.
768
+ def get_element_ptr_in_bounds(ptr, indices, name = '')
769
+ check_array_type(indices, Value, 'indices')
770
+
771
+ indices_ptr = FFI::MemoryPointer.new(:pointer, indices.length)
772
+ indices_ptr.write_array_of_pointer(indices)
773
+
774
+ InBoundsGEPInst.new(Bindings.build_in_bounds_gep(@ptr, ptr, indices_ptr, indices.length, name))
775
+ end
776
+ alias :inbounds_gep :get_element_ptr_in_bounds
777
+
778
+ # Builds a struct getelementptr instruction.
779
+ #
780
+ # @param [Value] ptr Pointer to a structure.
781
+ # @param [Value] index Unsigned integer representing the index of a structure member.
782
+ # @param [String] name Name of the result in LLVM IR.
783
+ #
784
+ # @return [StructGEPInst] The resulting pointer.
785
+ def struct_get_element_ptr(ptr, index, name = '')
786
+ StructGEPInst.new(Bindings.build_struct_gep(@ptr, ptr, index, name))
787
+ end
788
+ alias :struct_getp :struct_get_element_ptr
789
+
790
+ # Creates a global string initialized to a given value.
791
+ #
792
+ # @param [String] string String used by the initialize.
793
+ # @param [String] name Name of the result in LLVM IR.
794
+ #
795
+ # @return [GlobalStringInst] Reference to the global string.
796
+ def global_string(string, name = '')
797
+ GlobalStringInst.new(Bindings.build_global_string(@ptr, string, name))
798
+ end
799
+
800
+ # Creates a pointer to a global string initialized to a given value.
801
+ #
802
+ # @param [String] string String used by the initializer
803
+ # @param [String] name Name of the result in LLVM IR
804
+ #
805
+ # @return [GlobalStringPtrInst] Reference to the global string pointer.
806
+ def gloabl_string_pointer(string, name = '')
807
+ GlobalStringPtrInst(Bindings.build_global_string_ptr(@ptr, string, name))
808
+ end
809
+
810
+ ###############################
811
+ # Type and Value Manipulation #
812
+ ###############################
813
+
814
+ # Cast a value to the given type without changing any bits.
815
+ #
816
+ # @param [Value] val Value to cast.
817
+ # @param [Type] type Target type.
818
+ # @param [String] name Name of the result in LLVM IR.
819
+ #
820
+ # @return [BitCastInst] A value of the target type.
821
+ def bitcast(val, type, name = '')
822
+ BitCastInst.new(Bindings.build_bit_cast(@ptr, val, check_cg_type(type), name))
823
+ end
824
+
825
+ # @param [Value] val Value to cast.
826
+ # @param [Type] type Target type.
827
+ # @param [String] name Name of the result in LLVM IR.
828
+ #
829
+ # @return [FPCastInst] A value of the target type.
830
+ def floating_point_cast(val, type, name = '')
831
+ FPCastInst.new(Bindings.build_fp_cast(@ptr, val, check_cg_type(type), name))
832
+ end
833
+ alias :fp_cast :floating_point_cast
834
+
835
+ # Extend a floating point value.
836
+ #
837
+ # @param [Value] val Floating point or vector of floating point.
838
+ # @param [Type] type Floating point or vector of floating point type of greater size than val's type.
839
+ # @param [String] name Name of the result in LLVM IR.
840
+ #
841
+ # @return [FPExtendInst] The extended value.
842
+ def floating_point_extend(val, type, name = '')
843
+ FPExtendInst.new(Bindings.build_fp_ext(@ptr, val, check_cg_type(type), name))
844
+ end
845
+ alias :fp_ext :floating_point_extend
846
+ alias :fp_extend :floating_point_extend
847
+
848
+ # Convert a floating point to a signed integer.
849
+ #
850
+ # @param [Value] val Floating point or vector of floating points to convert.
851
+ # @param [Type] type Integer or vector of integer target type.
852
+ # @param [String] name Name of the result in LLVM IR.
853
+ #
854
+ # @return [FPToSIInst] The converted value.
855
+ def floating_point_to_signed_int(val, type, name = '')
856
+ FPToSIInst.new(Bindings.build_fp_to_si(@ptr, val, check_cg_type(type), name))
857
+ end
858
+ alias :fp2si :floating_point_to_signed_int
859
+
860
+ # Convert a floating point to an unsigned integer.
861
+ #
862
+ # @param [Value] val Floating point or vector of floating points to convert.
863
+ # @param [Type] type Integer or vector of integer target type.
864
+ # @param [String] name Name of the result in LLVM IR.
865
+ #
866
+ # @return [FPToSIInst] The converted value.
867
+ def floating_point_to_unsigned_int(val, type, name = '')
868
+ FPToUIInst.new(Bindings.build_fp_to_ui(@ptr, val, check_cg_type(type), name))
869
+ end
870
+ alias :fp2ui :floating_point_to_unsigned_int
871
+
872
+ # Truncate a floating point value.
873
+ #
874
+ # @param [Value] val Floating point or vector of floating point.
875
+ # @param [Type] type Floating point or vector of floating point type of lesser size than val's type.
876
+ # @param [String] name Name of the result in LLVM IR.
877
+ #
878
+ # @return [LLVM::Instruction] The truncated value
879
+ def floating_point_truncate(val, type, name = '')
880
+ FPTruncInst.new(Bindings.build_fp_trunc(@ptr, val, check_cg_type(type), name))
881
+ end
882
+ alias :fp_trunc :floating_point_truncate
883
+ alias :fp_truncate :floating_point_truncate
884
+
885
+ # Cast an int to a pointer.
886
+ #
887
+ # @param [Value] val An integer value.
888
+ # @param [Type] type A pointer type.
889
+ # @param [String] name Name of the result in LLVM IR.
890
+ #
891
+ # @return [IntToPtrInst] A pointer of the given type and the address held in val.
892
+ def int_to_ptr(val, type, name = '')
893
+ IntToPtrInst.new(Bindings.build_int_to_ptr(@ptr, val, check_cg_type(type), name))
894
+ end
895
+ alias :int2ptr :int_to_ptr
896
+
897
+ # @param [Value] val An integer value.
898
+ # @param [Type] type Integer or vector of integer target type.
899
+ # @param [String] name Name of the result in LLVM IR.
900
+ #
901
+ # @return [IntCastInst]
902
+ def integer_cast(val, type, name = '')
903
+ IntCastInst.new(Bindings.build_int_cast(@ptr, val, check_cg_type(type), name))
904
+ end
905
+ alias :int_cast :integer_cast
906
+
907
+ # @param [Value] val A pointer value.
908
+ # @param [Type] type A pointer target type.
909
+ # @param [String] name Name of the result in LLVM IR.
910
+ #
911
+ # @return [PtrCastInst]
912
+ def ptr_cast(val, type, name = '')
913
+ PtrCastInst.new(Bindings.build_pointer_cast(@ptr, val, check_cg_type(type), name))
914
+ end
915
+
916
+ # Cast a pointer to an int. Useful for pointer arithmetic.
917
+ #
918
+ # @param [Value] val A pointer value.
919
+ # @param [Type] type An integer type.
920
+ # @param [String] name Name of the result in LLVM IR.
921
+ #
922
+ # @return [PtrToIntInst] An integer of the given type representing the pointer's address.
923
+ def ptr_to_int(val, type, name = '')
924
+ PtrToIntInst.new(Bindings.build_ptr_to_int(@ptr, val, check_cg_type(type), name))
925
+ end
926
+ alias :ptr2int :ptr_to_int
927
+
928
+ # Sign extension by copying the sign bit (highest order bit) of the
929
+ # value until it reaches the bit size of the given type.
930
+ #
931
+ # @param [Value] val Integer or vector of integers to be extended.
932
+ # @param [Type] type Integer or vector of integer type of greater size than the size of val.
933
+ # @param [String] name Name of the result in LLVM IR.
934
+ #
935
+ # @return [SignExtendInst] The extended value.
936
+ def sign_extend(val, type, name = '')
937
+ SignExtendInst.new(Bindings.build_s_ext(@ptr, val, check_cg_type(type), name))
938
+ end
939
+ alias :sext :sign_extend
940
+
941
+ # Sign extension or bitcast.
942
+ #
943
+ # @param [Value] val Integer or vector of integers to be extended.
944
+ # @param [Type] type Integer or vector of integer type of greater size than the size of val.
945
+ # @param [String] name Name of the result in LLVM IR.
946
+ #
947
+ # @return [SignExtendOrBitcastInst] The extended or cast value.
948
+ def sign_extend_or_bitcast(val, type, name = '')
949
+ SignExtendOrBitCastInst.new(Bindings.build_s_ext_or_bit_cast(@ptr, val, check_cg_type(type), name))
950
+ end
951
+ alias :sext_or_bitcast :sign_extend_or_bitcast
952
+
953
+ # Convert a signed integer to a floating point.
954
+ #
955
+ # @param [Value] val Signed integer or vector of signed integer to convert.
956
+ # @param [Type] type Floating point or vector of floating point target type.
957
+ # @param [String] name Name of the result in LLVM IR.
958
+ #
959
+ # @return [SIToFPInst] The converted value.
960
+ def signed_int_to_floating_point(val, type, name = '')
961
+ SIToFPInst.new(Bindings.build_si_to_fp(@ptr, val, check_cg_type(type), name))
962
+ end
963
+ alias :si2fp :signed_int_to_floating_point
964
+
965
+ # Truncates its operand to the given type. The size of the value type
966
+ # must be greater than the size of the target type.
967
+ #
968
+ # @param [Value] val Integer or vector of integers to be truncated.
969
+ # @param [Type] type Integer or vector of integers of equal size to val.
970
+ # @param [String] name Name of the result in LLVM IR.
971
+ #
972
+ # @return [TruncateInst] The truncated value.
973
+ def truncate(val, type, name = '')
974
+ TruncateInst.new(Bindings.build_trunc(@ptr, val, check_cg_type(type), name))
975
+ end
976
+ alias :trunc :truncate
977
+
978
+ # Truncates or bitcast.
979
+ #
980
+ # @param [Value] val Integer or vector of integers to be truncated.
981
+ # @param [Type] type Integer or vector of integers of equal size to val.
982
+ # @param [String] name Name of the result in LLVM IR.
983
+ #
984
+ # @return [TruncateInst] The truncated or cast value.
985
+ def truncate_or_bitcast(val, type, name = '')
986
+ TruncateOrBitCastInst.new(Bindings.build_trunc_or_bit_cast(@ptr, val, check_cg_type(type), name))
987
+ end
988
+
989
+ # Convert an unsigned integer to a floating point.
990
+ #
991
+ # @param [Value] val Signed integer or vector of signed integer to convert.
992
+ # @param [Type] type Floating point or vector of floating point target type.
993
+ # @param [String] name Name of the result in LLVM IR.
994
+ #
995
+ # @return [SIToFPInst] The converted value.
996
+ def unsigned_int_to_floating_point(val, type, name = '')
997
+ UIToFPInst.new(Bindings.build_ui_to_fp(@ptr, val, check_cg_type(type), name))
998
+ end
999
+ alias :ui2fp :unsigned_int_to_floating_point
1000
+
1001
+ # Zero extends its operand to the given type. The size of the value
1002
+ # type must be greater than the size of the target type.
1003
+ #
1004
+ # @param [Value] val Integer or vector of integers to be extended.
1005
+ # @param [Type] type Integer or vector of integer type of greater size than val.
1006
+ # @param [String] name Name of the result in LLVM IR.
1007
+ #
1008
+ # @return [ZeroExtendInst] The extended value.
1009
+ def zero_extend(val, type, name = '')
1010
+ ZeroExtendInst.new(Bindings.build_z_ext(@ptr, val, check_cg_type(type), name))
1011
+ end
1012
+ alias :zext :zero_extend
1013
+
1014
+ # Zero extend or bitcast.
1015
+ #
1016
+ # @param [Value] val Integer or vector of integers to be extended.
1017
+ # @param [Type] type Integer or vector of integer type of greater size than val.
1018
+ # @param [String] name Name of the result in LLVM IR.
1019
+ #
1020
+ # @return [ZeroExtendInst] The extended or cast value.
1021
+ def zero_extend_or_bitcast(val, type, name = '')
1022
+ ZeroExtendOrBitCastInst.new(Bindings.build_z_ext_or_bit_cast(@ptr, val, check_cg_type(type), name))
1023
+ end
1024
+ alias :zext_or_bitcast :zero_extend_or_bitcast
1025
+
1026
+ ###########################
1027
+ # Comparison Instructions #
1028
+ ###########################
1029
+
1030
+ # Builds an icmp instruction. Compares lhs to rhs using the given
1031
+ # symbol predicate.
1032
+ #
1033
+ # @see Bindings._enum_int_predicate_
1034
+ # @LLVMInst icmp
1035
+ #
1036
+ # @param [Symbol] pred An integer predicate.
1037
+ # @param [Value] lhs Left hand side of the comparison, of integer or pointer type.
1038
+ # @param [Value] rhs Right hand side of the comparison, of the same type as lhs.
1039
+ # @param [String] name Name of the result in LLVM IR.
1040
+ #
1041
+ # @return [IntCmpInst] A Boolean represented as Int1.
1042
+ def int_comparison(pred, lhs, rhs, name = '')
1043
+ IntCmpInst.new(Bindings.build_i_cmp(@ptr, pred, lhs, rhs, name))
1044
+ end
1045
+ alias :icmp :int_comparison
1046
+
1047
+ # Builds an fcmp instruction. Compares lhs to rhs as reals using the
1048
+ # given symbol predicate.
1049
+ #
1050
+ # @see Bindings._enum_real_predicate_
1051
+ # @LLVMInst fcmp
1052
+ #
1053
+ # @param [Symbol] pred A real predicate.
1054
+ # @param [Value] lhs Left hand side of the comparison, of floating point type.
1055
+ # @param [Value] rhs Right hand side of the comparison, of the same type as lhs.
1056
+ # @param [String] name Name of the result in LLVM IR.
1057
+ #
1058
+ # @return [FCmpInst] A Boolean represented as an Int1.
1059
+ def fp_comparison(pred, lhs, rhs, name = '')
1060
+ FCmpInst.new(Bindings.build_f_cmp(@ptr, pred, lhs, rhs, name))
1061
+ end
1062
+ alias :fcmp :fp_comparison
1063
+
1064
+ # Calculate the difference between two pointers.
1065
+ #
1066
+ # @param [Value] lhs A pointer.
1067
+ # @param [Value] rhs A pointer.
1068
+ # @param [String] name Name of the result in LLVM IR.
1069
+ #
1070
+ # @return [PtrDiffInst] The integer difference between the two pointers.
1071
+ def ptr_diff(lhs, rhs, name = '')
1072
+ PtrDiffInst.new(Bindings.build_ptr_diff(lhs, rhs, name))
1073
+ end
1074
+
1075
+ # Check if a value is not null.
1076
+ #
1077
+ # @param [Value] val Value to check.
1078
+ # @param [String] name Name of the result in LLVM IR.
1079
+ #
1080
+ # @return [LLVM::Instruction] A Boolean represented as an Int1.
1081
+ def is_not_null(val, name = '')
1082
+ IsNotNullInst.new(Builder.build_is_not_null(@ptr, val, name))
1083
+ end
1084
+
1085
+ # Check if a value is null.
1086
+ #
1087
+ # @param [Value] val Value to check.
1088
+ # @param [String] name Name of the result in LLVM IR.
1089
+ #
1090
+ # @return [LLVM::Instruction] A Boolean represented as an Int1.
1091
+ def is_null(val, name = '')
1092
+ IsNullInst.new(Bindings.build_is_null(@ptr, val, name))
1093
+ end
1094
+ end
1095
+ end