ruby-llvm 2.9.0 → 2.9.1
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.
- data/README.rdoc +4 -5
- data/lib/llvm.rb +1 -0
- data/lib/llvm/analysis.rb +1 -0
- data/lib/llvm/core.rb +1 -0
- data/lib/llvm/core/builder.rb +161 -19
- data/lib/llvm/core/context.rb +7 -4
- data/lib/llvm/core/module.rb +42 -15
- data/lib/llvm/core/pass_manager.rb +7 -1
- data/lib/llvm/core/type.rb +27 -3
- data/lib/llvm/core/value.rb +130 -16
- data/lib/llvm/execution_engine.rb +24 -7
- data/lib/llvm/target.rb +1 -0
- data/lib/llvm/transforms/ipo.rb +2 -0
- data/lib/llvm/transforms/scalar.rb +22 -0
- metadata +5 -21
data/lib/llvm/core/context.rb
CHANGED
@@ -2,24 +2,27 @@ module LLVM
|
|
2
2
|
class Context
|
3
3
|
private_class_method :new
|
4
4
|
|
5
|
-
|
5
|
+
# @private
|
6
|
+
def initialize(ptr)
|
6
7
|
@ptr = ptr
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
|
+
# @private
|
11
|
+
def to_ptr
|
10
12
|
@ptr
|
11
13
|
end
|
12
14
|
|
13
|
-
# Creates a new Context
|
15
|
+
# Creates a new Context.
|
14
16
|
def self.create
|
15
17
|
new(C.LLVMContextCreate())
|
16
18
|
end
|
17
19
|
|
18
|
-
# Obtains a reference to the global Context
|
20
|
+
# Obtains a reference to the global Context.
|
19
21
|
def self.global
|
20
22
|
new(C.LLVMGetGlobalContext())
|
21
23
|
end
|
22
24
|
|
25
|
+
# Diposes the Context.
|
23
26
|
def dispose
|
24
27
|
C.LLVMContextDispose(@ptr)
|
25
28
|
end
|
data/lib/llvm/core/module.rb
CHANGED
@@ -2,18 +2,22 @@ module LLVM
|
|
2
2
|
class Module
|
3
3
|
private_class_method :new
|
4
4
|
|
5
|
+
# @private
|
5
6
|
def self.from_ptr(ptr)
|
6
7
|
ptr.null? ? nil : new(ptr)
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
|
+
# @private
|
11
|
+
def initialize(ptr)
|
10
12
|
@ptr = ptr
|
11
13
|
end
|
12
14
|
|
13
|
-
|
15
|
+
# @private
|
16
|
+
def to_ptr
|
14
17
|
@ptr
|
15
18
|
end
|
16
19
|
|
20
|
+
# Checks if the module is equal to other.
|
17
21
|
def ==(other)
|
18
22
|
case other
|
19
23
|
when LLVM::Module
|
@@ -23,14 +27,17 @@ module LLVM
|
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
30
|
+
# Checks if the module is equal to other.
|
26
31
|
def eql?(other)
|
27
32
|
other.instance_of?(self.class) && self == other
|
28
33
|
end
|
29
34
|
|
35
|
+
# Creates a module with the given name.
|
30
36
|
def self.create(name)
|
31
37
|
new(C.LLVMModuleCreateWithName(name))
|
32
38
|
end
|
33
39
|
|
40
|
+
# Returns a TypeCollection of all the Types in the module.
|
34
41
|
def types
|
35
42
|
@types ||= TypeCollection.new(self)
|
36
43
|
end
|
@@ -40,26 +47,28 @@ module LLVM
|
|
40
47
|
@module = mod
|
41
48
|
end
|
42
49
|
|
50
|
+
# Adds the given Type to the collection with the given name (symbol or string).
|
43
51
|
def add(name, type)
|
44
52
|
C.LLVMAddTypeName(@module, name.to_s, type)
|
45
53
|
end
|
46
54
|
|
55
|
+
# Returns the Type with the given name (symbol or string).
|
47
56
|
def named(name)
|
48
|
-
Type.from_ptr(C.LLVMGetTypeByName(@module, name))
|
57
|
+
Type.from_ptr(C.LLVMGetTypeByName(@module, name.to_s))
|
49
58
|
end
|
50
59
|
|
60
|
+
# Returns the Type with the a name equal to key (symbol or string).
|
51
61
|
def [](key)
|
52
|
-
|
53
|
-
when String then named(key)
|
54
|
-
when Symbol then named(key.to_s)
|
55
|
-
end
|
62
|
+
named(key)
|
56
63
|
end
|
57
64
|
|
65
|
+
# Adds the given Type to the collection with a name equal to key (symbol or string).
|
58
66
|
def []=(key, type)
|
59
67
|
add(key, type)
|
60
68
|
end
|
61
69
|
end
|
62
70
|
|
71
|
+
# Returns an Enumerable of all the GlobalVariables in the module.
|
63
72
|
def globals
|
64
73
|
@globals ||= GlobalCollection.new(self)
|
65
74
|
end
|
@@ -71,38 +80,45 @@ module LLVM
|
|
71
80
|
@module = mod
|
72
81
|
end
|
73
82
|
|
83
|
+
# Adds a GlobalVariable with the given type and name to the collection (symbol or string).
|
74
84
|
def add(ty, name)
|
75
|
-
GlobalVariable.from_ptr(C.LLVMAddGlobal(@module, LLVM::Type(ty), name))
|
85
|
+
GlobalVariable.from_ptr(C.LLVMAddGlobal(@module, LLVM::Type(ty), name.to_s))
|
76
86
|
end
|
77
87
|
|
88
|
+
# Returns the GlobalVariable with the given name (symbol or string).
|
78
89
|
def named(name)
|
79
|
-
GlobalValue.from_ptr(C.LLVMGetNamedGlobal(@module, name))
|
90
|
+
GlobalValue.from_ptr(C.LLVMGetNamedGlobal(@module, name.to_s))
|
80
91
|
end
|
81
92
|
|
93
|
+
# Returns the first GlobalVariable in the collection.
|
82
94
|
def first
|
83
95
|
GlobalValue.from_ptr(C.LLVMGetFirstGlobal(@module))
|
84
96
|
end
|
85
97
|
|
98
|
+
# Returns the last GlobalVariable in the collection.
|
86
99
|
def last
|
87
100
|
GlobalValue.from_ptr(C.LLVMGetLastGlobal(@module))
|
88
101
|
end
|
89
102
|
|
103
|
+
# Returns the next GlobalVariable in the collection after global.
|
90
104
|
def next(global)
|
91
105
|
GlobalValue.from_ptr(C.LLVMGetNextGlobal(global))
|
92
106
|
end
|
93
107
|
|
108
|
+
# Returns the previous GlobalVariable in the collection before global.
|
94
109
|
def previous(global)
|
95
110
|
GlobalValue.from_ptr(C.LLVMGetPreviousGlobal(global))
|
96
111
|
end
|
97
112
|
|
113
|
+
# Deletes the GlobalVariable from the collection.
|
98
114
|
def delete(global)
|
99
115
|
C.LLVMDeleteGlobal(global)
|
100
116
|
end
|
101
117
|
|
118
|
+
# Returns the GlobalVariable with a name equal to key (symbol or string) or at key (integer).
|
102
119
|
def [](key)
|
103
120
|
case key
|
104
|
-
when String then named(key)
|
105
|
-
when Symbol then named(key.to_s)
|
121
|
+
when String, Symbol then named(key)
|
106
122
|
when Integer then
|
107
123
|
i = 0
|
108
124
|
g = first
|
@@ -114,6 +130,7 @@ module LLVM
|
|
114
130
|
end
|
115
131
|
end
|
116
132
|
|
133
|
+
# Iterates through each GlobalVariable in the collection.
|
117
134
|
def each
|
118
135
|
g = first
|
119
136
|
until g.nil?
|
@@ -123,6 +140,7 @@ module LLVM
|
|
123
140
|
end
|
124
141
|
end
|
125
142
|
|
143
|
+
# Returns a FunctionCollection of all the Functions in the module.
|
126
144
|
def functions
|
127
145
|
@functions ||= FunctionCollection.new(self)
|
128
146
|
end
|
@@ -134,6 +152,7 @@ module LLVM
|
|
134
152
|
@module = mod
|
135
153
|
end
|
136
154
|
|
155
|
+
# Adds a Function with the given name (symbol or string) and args (Types).
|
137
156
|
def add(name, *args)
|
138
157
|
if args.first.kind_of? Type
|
139
158
|
type = args.first
|
@@ -150,34 +169,40 @@ module LLVM
|
|
150
169
|
function
|
151
170
|
end
|
152
171
|
|
172
|
+
# Returns the Function with the given name (symbol or string).
|
153
173
|
def named(name)
|
154
|
-
Function.from_ptr(C.LLVMGetNamedFunction(@module, name))
|
174
|
+
Function.from_ptr(C.LLVMGetNamedFunction(@module, name.to_s))
|
155
175
|
end
|
156
176
|
|
177
|
+
# Returns the first Function in the collection.
|
157
178
|
def first
|
158
179
|
Function.from_ptr(C.LLVMGetFirstFunction(@module))
|
159
180
|
end
|
160
181
|
|
182
|
+
# Returns the last Function in the collection.
|
161
183
|
def last
|
162
184
|
Function.from_ptr(C.LLVMGetLastFunction(@module))
|
163
185
|
end
|
164
186
|
|
187
|
+
# Returns the next Function in the collection after function.
|
165
188
|
def next(function)
|
166
189
|
Function.from_ptr(C.LLVMGetNextFunction(function))
|
167
190
|
end
|
168
191
|
|
192
|
+
# Returns the previous Function in the collection before function.
|
169
193
|
def previous(function)
|
170
194
|
Function.from_ptr(C.LLVMGetPreviousFunction(function))
|
171
195
|
end
|
172
196
|
|
197
|
+
# Deletes the Function from the collection.
|
173
198
|
def delete(function)
|
174
199
|
C.LLVMDeleteFunction(function)
|
175
200
|
end
|
176
201
|
|
202
|
+
# Returns the Function with a name equal to key (symbol or string) or at key (integer).
|
177
203
|
def [](key)
|
178
204
|
case key
|
179
|
-
when String then named(key)
|
180
|
-
when Symbol then named(key.to_s)
|
205
|
+
when String, Symbol then named(key)
|
181
206
|
when Integer
|
182
207
|
i = 0
|
183
208
|
f = first
|
@@ -189,6 +214,7 @@ module LLVM
|
|
189
214
|
end
|
190
215
|
end
|
191
216
|
|
217
|
+
# Iterates through each Function in the collection.
|
192
218
|
def each
|
193
219
|
f = first
|
194
220
|
until f.nil?
|
@@ -198,11 +224,12 @@ module LLVM
|
|
198
224
|
end
|
199
225
|
end
|
200
226
|
|
201
|
-
# Print the module's IR to stdout
|
227
|
+
# Print the module's IR to stdout.
|
202
228
|
def dump
|
203
229
|
C.LLVMDumpModule(self)
|
204
230
|
end
|
205
231
|
|
232
|
+
# Dispose the module.
|
206
233
|
def dispose
|
207
234
|
C.LLVMDisposeModule(@ptr)
|
208
235
|
end
|
@@ -2,6 +2,7 @@ module LLVM
|
|
2
2
|
# The PassManager runs a queue of passes on a module. See
|
3
3
|
# http://llvm.org/docs/Passes.html for the list of available passes.
|
4
4
|
class PassManager
|
5
|
+
# Creates a new pass manager on the given ExecutionEngine.
|
5
6
|
def initialize(execution_engine)
|
6
7
|
ptr = C.LLVMCreatePassManager()
|
7
8
|
C.LLVMAddTargetData(
|
@@ -9,7 +10,8 @@ module LLVM
|
|
9
10
|
@ptr = ptr
|
10
11
|
end
|
11
12
|
|
12
|
-
|
13
|
+
# @private
|
14
|
+
def to_ptr
|
13
15
|
@ptr
|
14
16
|
end
|
15
17
|
|
@@ -24,16 +26,19 @@ module LLVM
|
|
24
26
|
def do_finalization
|
25
27
|
end
|
26
28
|
|
29
|
+
# Runs the passes on the given module.
|
27
30
|
def run(mod)
|
28
31
|
C.LLVMRunPassManager(self, mod)
|
29
32
|
end
|
30
33
|
|
34
|
+
# Disposes the pass manager.
|
31
35
|
def dispose
|
32
36
|
C.LLVMDisposePassManager(self)
|
33
37
|
end
|
34
38
|
end
|
35
39
|
|
36
40
|
class FunctionPassManager < PassManager
|
41
|
+
# Creates a new pass manager on the given ExecutionEngine and Module.
|
37
42
|
def initialize(execution_engine, mod)
|
38
43
|
ptr = C.LLVMCreateFunctionPassManagerForModule(mod)
|
39
44
|
C.LLVMAddTargetData(
|
@@ -49,6 +54,7 @@ module LLVM
|
|
49
54
|
C.LLVMFinalizeFunctionPassManager(self) != 0
|
50
55
|
end
|
51
56
|
|
57
|
+
# Runs the passes on the given function.
|
52
58
|
def run(fn)
|
53
59
|
C.LLVMRunFunctionPassManager(self, fn)
|
54
60
|
end
|
data/lib/llvm/core/type.rb
CHANGED
@@ -2,11 +2,13 @@ module LLVM
|
|
2
2
|
class Type
|
3
3
|
private_class_method :new
|
4
4
|
|
5
|
-
|
5
|
+
# @private
|
6
|
+
def initialize(ptr)
|
6
7
|
@ptr = ptr
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
|
+
# @private
|
11
|
+
def to_ptr
|
10
12
|
@ptr
|
11
13
|
end
|
12
14
|
|
@@ -20,14 +22,17 @@ module LLVM
|
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
25
|
+
# Checks if the type is equal to other.
|
23
26
|
def eql?(other)
|
24
27
|
other.instance_of?(self.class) && self == other
|
25
28
|
end
|
26
29
|
|
30
|
+
# Returns a symbol representation of the types kind (ex. :pointer, :vector, :array.)
|
27
31
|
def kind
|
28
|
-
|
32
|
+
C.LLVMGetTypeKind(self)
|
29
33
|
end
|
30
34
|
|
35
|
+
# Returns the size of the type.
|
31
36
|
def size
|
32
37
|
Int64.from_ptr(C.LLVMSizeOf(self))
|
33
38
|
end
|
@@ -36,6 +41,7 @@ module LLVM
|
|
36
41
|
Int64.from_ptr(C.LLVMAlignOf(self))
|
37
42
|
end
|
38
43
|
|
44
|
+
# Returns the type of this types elements (works only for Pointer, Vector, and Array types.)
|
39
45
|
def element_type
|
40
46
|
case self.kind
|
41
47
|
when :pointer, :vector, :array
|
@@ -43,14 +49,17 @@ module LLVM
|
|
43
49
|
end
|
44
50
|
end
|
45
51
|
|
52
|
+
# Returns a null pointer ConstantExpr of this type.
|
46
53
|
def null_pointer
|
47
54
|
ConstantExpr.from_ptr(C.LLVMConstPointerNull(self))
|
48
55
|
end
|
49
56
|
|
57
|
+
# Returns a null ConstantExpr of this type.
|
50
58
|
def null
|
51
59
|
ConstantExpr.from_ptr(C.LLVMConstNull(self))
|
52
60
|
end
|
53
61
|
|
62
|
+
# Creates a pointer type with this type and the given address space.
|
54
63
|
def pointer(address_space = 0)
|
55
64
|
Type.pointer(self, address_space)
|
56
65
|
end
|
@@ -59,18 +68,23 @@ module LLVM
|
|
59
68
|
ptr.null? ? nil : new(ptr)
|
60
69
|
end
|
61
70
|
|
71
|
+
# Creates an array type of Type with the given size.
|
62
72
|
def self.array(ty, sz = 0)
|
63
73
|
from_ptr(C.LLVMArrayType(LLVM::Type(ty), sz))
|
64
74
|
end
|
65
75
|
|
76
|
+
# Creates the pointer type of Type with the given address space.
|
66
77
|
def self.pointer(ty, address_space = 0)
|
67
78
|
from_ptr(C.LLVMPointerType(LLVM::Type(ty), address_space))
|
68
79
|
end
|
69
80
|
|
81
|
+
# Creates a vector type of Type with the given element count.
|
70
82
|
def self.vector(ty, element_count)
|
71
83
|
from_ptr(C.LLVMVectorType(LLVM::Type(ty), element_count))
|
72
84
|
end
|
73
85
|
|
86
|
+
# Creates a function type. Takes an array of argument Types and the result Type. The only option is <tt>:varargs</tt>,
|
87
|
+
# which when set to true makes the function type take a variable number of args.
|
74
88
|
def self.function(arg_types, result_type, options = {})
|
75
89
|
arg_types.map! { |ty| LLVM::Type(ty) }
|
76
90
|
arg_types_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * arg_types.size)
|
@@ -78,6 +92,7 @@ module LLVM
|
|
78
92
|
from_ptr(C.LLVMFunctionType(LLVM::Type(result_type), arg_types_ptr, arg_types.size, options[:varargs] ? 1 : 0))
|
79
93
|
end
|
80
94
|
|
95
|
+
# Creates a struct type with the given array of element types.
|
81
96
|
def self.struct(elt_types, is_packed)
|
82
97
|
elt_types.map! { |ty| LLVM::Type(ty) }
|
83
98
|
elt_types_ptr = FFI::MemoryPointer.new(FFI.type_size(:pointer) * elt_types.size)
|
@@ -85,10 +100,12 @@ module LLVM
|
|
85
100
|
from_ptr(C.LLVMStructType(elt_types_ptr, elt_types.size, is_packed ? 1 : 0))
|
86
101
|
end
|
87
102
|
|
103
|
+
# Creates a void type.
|
88
104
|
def self.void
|
89
105
|
from_ptr(C.LLVMVoidType)
|
90
106
|
end
|
91
107
|
|
108
|
+
# Creates an opaque type.
|
92
109
|
def self.opaque
|
93
110
|
from_ptr(C.LLVMOpaqueType)
|
94
111
|
end
|
@@ -105,6 +122,7 @@ module LLVM
|
|
105
122
|
end
|
106
123
|
end
|
107
124
|
|
125
|
+
# Creates a Type from the given object.
|
108
126
|
def LLVM.Type(ty)
|
109
127
|
case ty
|
110
128
|
when LLVM::Type then ty
|
@@ -112,26 +130,32 @@ module LLVM
|
|
112
130
|
end
|
113
131
|
end
|
114
132
|
|
133
|
+
# Shortcut to Type.array.
|
115
134
|
def LLVM.Array(ty, sz = 0)
|
116
135
|
LLVM::Type.array(ty, sz)
|
117
136
|
end
|
118
137
|
|
138
|
+
# Shortcut to Type.pointer.
|
119
139
|
def LLVM.Pointer(ty)
|
120
140
|
LLVM::Type.pointer(ty)
|
121
141
|
end
|
122
142
|
|
143
|
+
# Shortcut to Type.vector.
|
123
144
|
def LLVM.Vector(ty, sz)
|
124
145
|
LLVM::Type.vector(ty, sz)
|
125
146
|
end
|
126
147
|
|
148
|
+
# Shortcut to Type.function.
|
127
149
|
def LLVM.Function(argtypes, rettype, options = {})
|
128
150
|
LLVM::Type.function(argtypes, rettype, options)
|
129
151
|
end
|
130
152
|
|
153
|
+
# Shortcut to Type.struct.
|
131
154
|
def LLVM.Struct(*elt_types)
|
132
155
|
LLVM::Type.struct(elt_types, false)
|
133
156
|
end
|
134
157
|
|
158
|
+
# Shortcut to Type.void.
|
135
159
|
def LLVM.Void
|
136
160
|
LLVM::Type.void
|
137
161
|
end
|
data/lib/llvm/core/value.rb
CHANGED
@@ -1,19 +1,23 @@
|
|
1
1
|
module LLVM
|
2
2
|
class Value
|
3
|
+
# @private
|
3
4
|
def self.from_ptr(ptr)
|
4
5
|
new(ptr) unless ptr.null?
|
5
6
|
end
|
6
7
|
|
7
8
|
private_class_method :new
|
8
9
|
|
10
|
+
# @private
|
9
11
|
def initialize(ptr)
|
10
12
|
@ptr = ptr
|
11
13
|
end
|
12
14
|
|
13
|
-
|
15
|
+
# @private
|
16
|
+
def to_ptr
|
14
17
|
@ptr
|
15
18
|
end
|
16
19
|
|
20
|
+
# Checks if the value is equal to other.
|
17
21
|
def ==(other)
|
18
22
|
case other
|
19
23
|
when LLVM::Value
|
@@ -23,10 +27,12 @@ module LLVM
|
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
30
|
+
# Checks if the value is equal to other.
|
26
31
|
def eql?(other)
|
27
32
|
other.instance_of?(self.class) && self == other
|
28
33
|
end
|
29
34
|
|
35
|
+
# Returns the Value type. This is abstract and is overidden by its subclasses.
|
30
36
|
def self.type
|
31
37
|
raise NotImplementedError, "#{self.name}.type() is abstract."
|
32
38
|
end
|
@@ -35,23 +41,28 @@ module LLVM
|
|
35
41
|
type.to_ptr
|
36
42
|
end
|
37
43
|
|
44
|
+
# Returns the value's type.
|
38
45
|
def type
|
39
46
|
Type.from_ptr(C.LLVMTypeOf(self))
|
40
47
|
end
|
41
48
|
|
49
|
+
# Returns the value's name.
|
42
50
|
def name
|
43
51
|
C.LLVMGetValueName(self)
|
44
52
|
end
|
45
53
|
|
54
|
+
# Sets the value's name to str.
|
46
55
|
def name=(str)
|
47
56
|
C.LLVMSetValueName(self, str)
|
48
57
|
str
|
49
58
|
end
|
50
59
|
|
60
|
+
# Print the value's IR to stdout.
|
51
61
|
def dump
|
52
62
|
C.LLVMDumpValue(self)
|
53
63
|
end
|
54
64
|
|
65
|
+
# Returns whether the value is constant.
|
55
66
|
def constant?
|
56
67
|
case C.LLVMIsConstant(self)
|
57
68
|
when 0 then false
|
@@ -59,6 +70,7 @@ module LLVM
|
|
59
70
|
end
|
60
71
|
end
|
61
72
|
|
73
|
+
# Returns whether the value is null.
|
62
74
|
def null?
|
63
75
|
case C.LLVMIsNull(self)
|
64
76
|
when 0 then false
|
@@ -66,6 +78,7 @@ module LLVM
|
|
66
78
|
end
|
67
79
|
end
|
68
80
|
|
81
|
+
# Returns whether the value is undefined.
|
69
82
|
def undefined?
|
70
83
|
case C.LLVMIsUndef(self)
|
71
84
|
when 0 then false
|
@@ -73,6 +86,7 @@ module LLVM
|
|
73
86
|
end
|
74
87
|
end
|
75
88
|
|
89
|
+
# Adds attr to this value's attributes.
|
76
90
|
def add_attribute(attr)
|
77
91
|
C.LLVMAddAttribute(self, attr)
|
78
92
|
end
|
@@ -82,10 +96,12 @@ module LLVM
|
|
82
96
|
end
|
83
97
|
|
84
98
|
class BasicBlock < Value
|
99
|
+
# Creates a basic block for the given function with the given name.
|
85
100
|
def self.create(fun = nil, name = "")
|
86
101
|
self.from_ptr(C.LLVMAppendBasicBlock(fun, name))
|
87
102
|
end
|
88
103
|
|
104
|
+
# Build the basic block with the given builder. Creates a new one if nil. Yields the builder.
|
89
105
|
def build(builder = nil)
|
90
106
|
if builder.nil?
|
91
107
|
builder = Builder.create
|
@@ -99,16 +115,19 @@ module LLVM
|
|
99
115
|
builder.dispose if islocal
|
100
116
|
end
|
101
117
|
|
118
|
+
# Returns the parent of this basic block (a Function).
|
102
119
|
def parent
|
103
120
|
fp = C.LLVMGetBasicBlockParent(self)
|
104
121
|
LLVM::Function.from_ptr(fp) unless fp.null?
|
105
122
|
end
|
106
123
|
|
124
|
+
# Returns the next basic block in the sequence.
|
107
125
|
def next
|
108
126
|
ptr = C.LLVMGetNextBasicBlock(self)
|
109
127
|
BasicBlock.from_ptr(ptr) unless ptr.null?
|
110
128
|
end
|
111
129
|
|
130
|
+
# Returns the previous basic block in the sequence.
|
112
131
|
def previous
|
113
132
|
ptr = C.LLVMGetPreviousBasicBlock(self)
|
114
133
|
BasicBlock.from_ptr(ptr) unless ptr.null?
|
@@ -122,10 +141,12 @@ module LLVM
|
|
122
141
|
instructions.last
|
123
142
|
end
|
124
143
|
|
144
|
+
# Returns an Enumerable of the Instructions in the current block.
|
125
145
|
def instructions
|
126
146
|
@instructions ||= InstructionCollection.new(self)
|
127
147
|
end
|
128
148
|
|
149
|
+
# @private
|
129
150
|
class InstructionCollection
|
130
151
|
include Enumerable
|
131
152
|
|
@@ -133,6 +154,7 @@ module LLVM
|
|
133
154
|
@block = block
|
134
155
|
end
|
135
156
|
|
157
|
+
# Iterates through each Instruction in the collection.
|
136
158
|
def each
|
137
159
|
return to_enum :each unless block_given?
|
138
160
|
inst, last = first, last
|
@@ -146,11 +168,13 @@ module LLVM
|
|
146
168
|
self
|
147
169
|
end
|
148
170
|
|
171
|
+
# Returns the first Instruction in the collection.
|
149
172
|
def first
|
150
173
|
ptr = C.LLVMGetFirstInstruction(@block)
|
151
174
|
LLVM::Instruction.from_ptr(ptr) unless ptr.null?
|
152
175
|
end
|
153
176
|
|
177
|
+
# Returns the last Instruction in the collection.
|
154
178
|
def last
|
155
179
|
ptr = C.LLVMGetLastInstruction(@block)
|
156
180
|
LLVM::Instruction.from_ptr(ptr) unless ptr.null?
|
@@ -159,10 +183,12 @@ module LLVM
|
|
159
183
|
end
|
160
184
|
|
161
185
|
class User < Value
|
186
|
+
# Returns an Enumerable of the operands in this user.
|
162
187
|
def operands
|
163
188
|
@operand_collection ||= OperandCollection.new(self)
|
164
189
|
end
|
165
190
|
|
191
|
+
# @private
|
166
192
|
class OperandCollection
|
167
193
|
include Enumerable
|
168
194
|
|
@@ -170,19 +196,23 @@ module LLVM
|
|
170
196
|
@user = user
|
171
197
|
end
|
172
198
|
|
199
|
+
# Get a reference to an operand by index.
|
173
200
|
def [](i)
|
174
201
|
ptr = C.LLVMGetOperand(@user, i)
|
175
202
|
Value.from_ptr(ptr) unless ptr.null?
|
176
203
|
end
|
177
204
|
|
205
|
+
# Set or replace an operand by index.
|
178
206
|
def []=(i, v)
|
179
207
|
C.LLVMSetOperand(@user, i, v)
|
180
208
|
end
|
181
209
|
|
210
|
+
# Returns the number of operands in the collection.
|
182
211
|
def size
|
183
212
|
C.LLVMGetNumOperands(@user)
|
184
213
|
end
|
185
214
|
|
215
|
+
# Iterates through each operand in the collection.
|
186
216
|
def each
|
187
217
|
return to_enum :each unless block_given?
|
188
218
|
0.upto(size-1) { |i| yield self[i] }
|
@@ -192,22 +222,28 @@ module LLVM
|
|
192
222
|
end
|
193
223
|
|
194
224
|
class Constant < User
|
195
|
-
|
225
|
+
# Creates a null constant of Type.
|
226
|
+
def self.null(type)
|
196
227
|
from_ptr(C.LLVMConstNull(type))
|
197
228
|
end
|
198
229
|
|
199
|
-
|
230
|
+
# Creates a undefined constant of Type.
|
231
|
+
def self.undef(type)
|
200
232
|
from_ptr(C.LLVMGetUndef(type))
|
201
233
|
end
|
202
234
|
|
203
|
-
|
235
|
+
# Creates a null pointer constant of Type.
|
236
|
+
def self.null_ptr(type)
|
204
237
|
from_ptr(C.LLVMConstPointerNull(type))
|
205
238
|
end
|
206
239
|
|
240
|
+
# Bitcast this constant to Type.
|
207
241
|
def bitcast_to(type)
|
208
242
|
ConstantExpr.from_ptr(C.LLVMConstBitCast(self, type))
|
209
243
|
end
|
210
244
|
|
245
|
+
# Returns the element pointer at the given indices of the constant.
|
246
|
+
# For more information on gep go to: http://llvm.org/docs/GetElementPtr.html
|
211
247
|
def gep(*indices)
|
212
248
|
indices = Array(indices)
|
213
249
|
FFI::MemoryPointer.new(FFI.type_size(:pointer) * indices.size) do |indices_ptr|
|
@@ -255,6 +291,7 @@ module LLVM
|
|
255
291
|
from_ptr(C.LLVMConstAllOnes(type))
|
256
292
|
end
|
257
293
|
|
294
|
+
# Creates a ConstantInt from an integer.
|
258
295
|
def self.from_i(n, signed = true)
|
259
296
|
from_ptr(C.LLVMConstInt(type, n, signed ? 1 : 0))
|
260
297
|
end
|
@@ -263,72 +300,100 @@ module LLVM
|
|
263
300
|
from_ptr(C.LLVMConstIntOfString(type, str, radix))
|
264
301
|
end
|
265
302
|
|
303
|
+
# Negation.
|
266
304
|
def -@
|
267
305
|
self.class.from_ptr(C.LLVMConstNeg(self))
|
268
306
|
end
|
269
307
|
|
308
|
+
# Boolean negation.
|
270
309
|
def not
|
271
310
|
self.class.from_ptr(C.LLVMConstNot(self))
|
272
311
|
end
|
273
312
|
|
313
|
+
# Addition.
|
274
314
|
def +(rhs)
|
275
315
|
self.class.from_ptr(C.LLVMConstAdd(self, rhs))
|
276
316
|
end
|
277
317
|
|
318
|
+
# "No signed wrap" addition. See
|
319
|
+
# http://llvm.org/docs/LangRef.html#i_add for discusison.
|
278
320
|
def nsw_add(rhs)
|
279
321
|
self.class.from_ptr(C.LLVMConstNSWAdd(self, rhs))
|
280
322
|
end
|
281
323
|
|
324
|
+
# Multiplication.
|
282
325
|
def *(rhs)
|
283
326
|
self.class.from_ptr(C.LLVMConstMul(self, rhs))
|
284
327
|
end
|
285
328
|
|
329
|
+
# Unsigned division.
|
286
330
|
def udiv(rhs)
|
287
331
|
self.class.from_ptr(C.LLVMConstUDiv(self, rhs))
|
288
332
|
end
|
289
333
|
|
334
|
+
# Signed division.
|
290
335
|
def /(rhs)
|
291
336
|
self.class.from_ptr(C.LLVMConstSDiv(self, rhs))
|
292
337
|
end
|
293
338
|
|
339
|
+
# Unsigned remainder.
|
294
340
|
def urem(rhs)
|
295
341
|
self.class.from_ptr(C.LLVMConstURem(self, rhs))
|
296
342
|
end
|
297
343
|
|
344
|
+
# Signed remainder.
|
298
345
|
def rem(rhs)
|
299
346
|
self.class.from_ptr(C.LLVMConstSRem(self, rhs))
|
300
347
|
end
|
301
348
|
|
302
|
-
|
349
|
+
# Integer AND.
|
350
|
+
def and(rhs)
|
303
351
|
self.class.from_ptr(C.LLVMConstAnd(self, rhs))
|
304
352
|
end
|
305
353
|
|
306
|
-
|
354
|
+
# Integer OR.
|
355
|
+
def or(rhs)
|
307
356
|
self.class.from_ptr(C.LLVMConstOr(self, rhs))
|
308
357
|
end
|
309
358
|
|
310
|
-
|
359
|
+
# Integer XOR.
|
360
|
+
def xor(rhs)
|
311
361
|
self.class.from_ptr(C.LLVMConstXor(self, rhs))
|
312
362
|
end
|
313
363
|
|
364
|
+
# Integer comparison using the predicate specified via the first parameter.
|
365
|
+
# Predicate can be any of:
|
366
|
+
# :eq - equal to
|
367
|
+
# :ne - not equal to
|
368
|
+
# :ugt - unsigned greater than
|
369
|
+
# :uge - unsigned greater than or equal to
|
370
|
+
# :ult - unsigned less than
|
371
|
+
# :ule - unsigned less than or equal to
|
372
|
+
# :sgt - signed greater than
|
373
|
+
# :sge - signed greater than or equal to
|
374
|
+
# :slt - signed less than
|
375
|
+
# :sle - signed less than or equal to
|
314
376
|
def icmp(pred, rhs)
|
315
377
|
self.class.from_ptr(C.LLVMConstICmp(pred, self, rhs))
|
316
378
|
end
|
317
379
|
|
380
|
+
# Shift left.
|
318
381
|
def <<(bits)
|
319
|
-
self.class.from_ptr(C.LLVMConstShl(self,
|
382
|
+
self.class.from_ptr(C.LLVMConstShl(self, bits))
|
320
383
|
end
|
321
384
|
|
385
|
+
# Shift right.
|
322
386
|
def >>(bits)
|
323
|
-
self.class.from_ptr(C.LLVMConstLShr(self,
|
387
|
+
self.class.from_ptr(C.LLVMConstLShr(self, bits))
|
324
388
|
end
|
325
389
|
|
390
|
+
# Arithmatic shift right.
|
326
391
|
def ashr(bits)
|
327
|
-
self.class.from_ptr(C.LLVMConstAShr(self,
|
392
|
+
self.class.from_ptr(C.LLVMConstAShr(self, bits))
|
328
393
|
end
|
329
394
|
end
|
330
395
|
|
331
|
-
def LLVM.const_missing(const)
|
396
|
+
def LLVM.const_missing(const)
|
332
397
|
case const.to_s
|
333
398
|
when /Int(\d+)/
|
334
399
|
width = $1.to_i
|
@@ -349,6 +414,7 @@ module LLVM
|
|
349
414
|
# Native integer type
|
350
415
|
::LLVM::Int = const_get("Int#{NATIVE_INT_SIZE}")
|
351
416
|
|
417
|
+
# Creates a LLVM Int (subclass of ConstantInt) at the NATIVE_INT_SIZE from a integer (val).
|
352
418
|
def LLVM.Int(val)
|
353
419
|
case val
|
354
420
|
when LLVM::ConstantInt then val
|
@@ -357,45 +423,71 @@ module LLVM
|
|
357
423
|
end
|
358
424
|
|
359
425
|
class ConstantReal < Constant
|
426
|
+
# Creates a ConstantReal from a float of Type.
|
360
427
|
def self.from_f(n)
|
361
428
|
from_ptr(C.LLVMConstReal(type, n))
|
362
429
|
end
|
363
430
|
|
364
|
-
def self.parse(str)
|
431
|
+
def self.parse(type, str)
|
365
432
|
from_ptr(C.LLVMConstRealOfString(type, str))
|
366
433
|
end
|
367
434
|
|
435
|
+
# Negation.
|
368
436
|
def -@
|
369
437
|
self.class.from_ptr(C.LLVMConstFNeg(self))
|
370
438
|
end
|
371
439
|
|
440
|
+
# Returns the result of adding this ConstantReal to rhs.
|
372
441
|
def +(rhs)
|
373
442
|
self.class.from_ptr(C.LLVMConstFAdd(self, rhs))
|
374
443
|
end
|
375
444
|
|
445
|
+
# Returns the result of multiplying this ConstantReal by rhs.
|
376
446
|
def *(rhs)
|
377
447
|
self.class.from_ptr(C.LLVMConstFMul(self, rhs))
|
378
448
|
end
|
379
449
|
|
450
|
+
# Returns the result of dividing this ConstantReal by rhs.
|
380
451
|
def /(rhs)
|
381
452
|
self.class.from_ptr(C.LLVMConstFDiv(self, rhs))
|
382
453
|
end
|
383
454
|
|
455
|
+
# Remainder.
|
384
456
|
def rem(rhs)
|
385
457
|
self.class.from_ptr(C.LLVMConstFRem(self, rhs))
|
386
458
|
end
|
387
459
|
|
460
|
+
# Floating point comparison using the predicate specified via the first
|
461
|
+
# parameter. Predicate can be any of:
|
462
|
+
# :ord - ordered
|
463
|
+
# :uno - unordered: isnan(X) | isnan(Y)
|
464
|
+
# :oeq - ordered and equal to
|
465
|
+
# :oeq - unordered and equal to
|
466
|
+
# :one - ordered and not equal to
|
467
|
+
# :one - unordered and not equal to
|
468
|
+
# :ogt - ordered and greater than
|
469
|
+
# :uge - unordered and greater than or equal to
|
470
|
+
# :olt - ordered and less than
|
471
|
+
# :ule - unordered and less than or equal to
|
472
|
+
# :oge - ordered and greater than or equal to
|
473
|
+
# :sge - unordered and greater than or equal to
|
474
|
+
# :ole - ordered and less than or equal to
|
475
|
+
# :sle - unordered and less than or equal to
|
476
|
+
# :true - always true
|
477
|
+
# :false- always false
|
388
478
|
def fcmp(pred, rhs)
|
389
479
|
self.class.from_ptr(C.LLMVConstFCmp(pred, self, rhs))
|
390
480
|
end
|
391
481
|
end
|
392
482
|
|
393
483
|
class Float < ConstantReal
|
484
|
+
# Return a Type representation of the float.
|
394
485
|
def self.type
|
395
486
|
Type.from_ptr(C.LLVMFloatType)
|
396
487
|
end
|
397
488
|
end
|
398
489
|
|
490
|
+
# Create a LLVM::Float from a Ruby Float (val).
|
399
491
|
def LLVM.Float(val)
|
400
492
|
Float.from_f(val)
|
401
493
|
end
|
@@ -485,23 +577,28 @@ module LLVM
|
|
485
577
|
end
|
486
578
|
|
487
579
|
class Function < GlobalValue
|
580
|
+
# Sets the function's calling convention and returns it.
|
488
581
|
def call_conv=(conv)
|
489
582
|
C.LLVMSetFunctionCallConv(self, conv)
|
490
583
|
conv
|
491
584
|
end
|
492
585
|
|
586
|
+
# Adds the given attribute to the function.
|
493
587
|
def add_attribute(attr)
|
494
588
|
C.LLVMAddFunctionAttr(self, attr)
|
495
589
|
end
|
496
590
|
|
591
|
+
# Removes the given attribute from the function.
|
497
592
|
def remove_attribute(attr)
|
498
593
|
C.LLVMRemoveFunctionAttr(self, attr)
|
499
594
|
end
|
500
595
|
|
596
|
+
# Returns an Enumerable of the BasicBlocks in this function.
|
501
597
|
def basic_blocks
|
502
598
|
@basic_block_collection ||= BasicBlockCollection.new(self)
|
503
599
|
end
|
504
600
|
|
601
|
+
# @private
|
505
602
|
class BasicBlockCollection
|
506
603
|
include Enumerable
|
507
604
|
|
@@ -509,10 +606,12 @@ module LLVM
|
|
509
606
|
@fun = fun
|
510
607
|
end
|
511
608
|
|
609
|
+
# Returns the number of BasicBlocks in the collection.
|
512
610
|
def size
|
513
611
|
C.LLVMCountBasicBlocks(@fun)
|
514
612
|
end
|
515
613
|
|
614
|
+
# Iterates through each BasicBlock in the collection.
|
516
615
|
def each
|
517
616
|
return to_enum :each unless block_given?
|
518
617
|
|
@@ -525,44 +624,54 @@ module LLVM
|
|
525
624
|
self
|
526
625
|
end
|
527
626
|
|
627
|
+
# Adds a BasicBlock with the given name to the end of the collection.
|
528
628
|
def append(name = "")
|
529
629
|
BasicBlock.create(@fun, name)
|
530
630
|
end
|
531
631
|
|
632
|
+
# Returns the entry BasicBlock in the collection. This is the block the
|
633
|
+
# function starts on.
|
532
634
|
def entry
|
533
635
|
BasicBlock.from_ptr(C.LLVMGetEntryBasicBlock(@fun))
|
534
636
|
end
|
535
637
|
|
638
|
+
# Returns the first BasicBlock in the collection.
|
536
639
|
def first
|
537
640
|
ptr = C.LLVMGetFirstBasicBlock(@fun)
|
538
641
|
BasicBlock.from_ptr(ptr) unless ptr.null?
|
539
642
|
end
|
540
643
|
|
644
|
+
# Returns the last BasicBlock in the collection.
|
541
645
|
def last
|
542
646
|
ptr = C.LLVMGetLastBasicBlock(@fun)
|
543
647
|
BasicBlock.from_ptr(ptr) unless ptr.null?
|
544
648
|
end
|
545
649
|
end
|
546
650
|
|
651
|
+
# Returns an Enumerable of the parameters in the function.
|
547
652
|
def params
|
548
653
|
@parameter_collection ||= ParameterCollection.new(self)
|
549
654
|
end
|
550
655
|
|
656
|
+
# @private
|
551
657
|
class ParameterCollection
|
552
658
|
def initialize(fun)
|
553
659
|
@fun = fun
|
554
660
|
end
|
555
661
|
|
662
|
+
# Returns a Value representation of the parameter at the given index.
|
556
663
|
def [](i)
|
557
664
|
Value.from_ptr(C.LLVMGetParam(@fun, i))
|
558
665
|
end
|
559
666
|
|
667
|
+
# Returns the number of paramters in the collection.
|
560
668
|
def size
|
561
669
|
C.LLVMCountParams(@fun)
|
562
670
|
end
|
563
671
|
|
564
672
|
include Enumerable
|
565
673
|
|
674
|
+
# Iteraters through each parameter in the collection.
|
566
675
|
def each
|
567
676
|
return to_enum :each unless block_given?
|
568
677
|
0.upto(size-1) { |i| yield self[i] }
|
@@ -596,16 +705,19 @@ module LLVM
|
|
596
705
|
end
|
597
706
|
|
598
707
|
class Instruction < User
|
708
|
+
# Returns the parent of the instruction (a BasicBlock).
|
599
709
|
def parent
|
600
710
|
ptr = C.LLVMGetInstructionParent(self)
|
601
711
|
LLVM::BasicBlock.from_ptr(ptr) unless ptr.null?
|
602
712
|
end
|
603
713
|
|
714
|
+
# Returns the next instruction after this one.
|
604
715
|
def next
|
605
716
|
ptr = C.LLVMGetNextInstruction(self)
|
606
717
|
LLVM::Instruction.from_ptr(ptr) unless ptr.null?
|
607
718
|
end
|
608
719
|
|
720
|
+
# Returns the previous instruction before this one.
|
609
721
|
def previous
|
610
722
|
ptr = C.LLVMGetPreviousInstruction(self)
|
611
723
|
LLVM::Instruction.from_ptr(ptr) unless ptr.null?
|
@@ -613,19 +725,21 @@ module LLVM
|
|
613
725
|
end
|
614
726
|
|
615
727
|
class CallInst < Instruction
|
728
|
+
# Sets the call convention to conv.
|
616
729
|
def call_conv=(conv)
|
617
730
|
C.LLVMSetInstructionCallConv(self, conv)
|
618
731
|
conv
|
619
732
|
end
|
620
733
|
|
734
|
+
# Returns the call insatnce's call convention.
|
621
735
|
def call_conv
|
622
736
|
C.LLVMGetInstructionCallConv(self)
|
623
737
|
end
|
624
738
|
end
|
625
739
|
|
626
740
|
class Phi < Instruction
|
627
|
-
# Add incoming branches to a phi node by passing an alternating list
|
628
|
-
#
|
741
|
+
# Add incoming branches to a phi node by passing an alternating list of
|
742
|
+
# resulting values and BasicBlocks. e.g.
|
629
743
|
# phi.add_incoming(val1, block1, val2, block2, ...)
|
630
744
|
def add_incoming(*incoming)
|
631
745
|
vals, blocks = [], []
|
@@ -651,8 +765,8 @@ module LLVM
|
|
651
765
|
end
|
652
766
|
|
653
767
|
class SwitchInst < Instruction
|
654
|
-
# Adds a case to a switch instruction. First the value to match on,
|
655
|
-
#
|
768
|
+
# Adds a case to a switch instruction. First the value to match on, then
|
769
|
+
# the basic block.
|
656
770
|
def add_case(val, block)
|
657
771
|
C.LLVMAddCase(self, val, block)
|
658
772
|
end
|