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