ruby-llvm 3.0.0.beta → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,76 +2,46 @@ require 'llvm'
2
2
  require 'llvm/core'
3
3
  require 'llvm/target'
4
4
  require 'llvm/analysis'
5
+ require 'llvm/execution_engine_ffi'
5
6
 
6
7
  module LLVM
7
8
  # @private
8
9
  module C
9
- # Generic values
10
- attach_function :LLVMCreateGenericValueOfInt, [:pointer, :long_long, :int], :pointer
11
- attach_function :LLVMCreateGenericValueOfPointer, [:pointer], :pointer
12
- attach_function :LLVMCreateGenericValueOfFloat, [:pointer, :double], :pointer
13
-
14
- attach_function :LLVMGenericValueIntWidth, [:pointer], :uint
15
-
16
- attach_function :LLVMGenericValueToInt, [:pointer, :int], :long_long
17
- attach_function :LLVMGenericValueToPointer, [:pointer], :pointer
18
- attach_function :LLVMGenericValueToFloat, [:pointer, :pointer], :double
19
- attach_function :LLVMDisposeGenericValue, [:pointer], :void
20
-
21
- # Execution engines
22
- attach_function :LLVMCreateExecutionEngineForModule, [:pointer, :pointer, :pointer], :int
23
- attach_function :LLVMCreateInterpreterForModule, [:pointer, :pointer, :pointer], :int
24
- attach_function :LLVMCreateJITCompilerForModule, [:pointer, :pointer, :uint, :pointer], :int
25
- attach_function :LLVMDisposeExecutionEngine, [:pointer], :void
26
-
27
- attach_function :LLVMRunStaticConstructors, [:pointer], :void
28
- attach_function :LLVMRunStaticDestructors, [:pointer], :void
29
-
30
- attach_function :LLVMRunFunctionAsMain, [:pointer, :pointer, :uint, :pointer, :pointer], :int
31
- attach_function :LLVMRunFunction, [:pointer, :pointer, :uint, :pointer], :pointer
32
-
33
- attach_function :LLVMFreeMachineCodeForFunction, [:pointer, :pointer], :void
34
- attach_function :LLVMAddModuleProvider, [:pointer, :pointer], :void
35
- attach_function :LLVMRemoveModuleProvider, [:pointer, :pointer, :pointer, :pointer], :int
36
-
37
- attach_function :LLVMFindFunction, [:pointer, :pointer, :pointer, :pointer], :int
38
-
39
- attach_function :LLVMGetExecutionEngineTargetData, [:pointer], :pointer
40
-
41
- attach_function :LLVMAddGlobalMapping, [:pointer, :pointer, :pointer], :void
42
-
43
- attach_function :LLVMGetPointerToGlobal, [:pointer, :pointer], :pointer
44
-
45
- attach_function :LLVMInitializeX86TargetInfo, [], :void
46
-
47
- attach_function :LLVMInitializeX86Target, [], :void
48
-
49
- attach_function :LLVMInitializeX86TargetMC, [], :void
10
+ attach_function :initialize_x86_target_info, :LLVMInitializeX86TargetInfo, [], :void
11
+ attach_function :initialize_x86_target, :LLVMInitializeX86Target, [], :void
12
+ attach_function :initialize_x86_target_mc, :LLVMInitializeX86TargetMC, [], :void
50
13
  end
51
14
 
52
15
  def LLVM.init_x86
53
- LLVM::C.LLVMInitializeX86Target
54
- LLVM::C.LLVMInitializeX86TargetInfo
55
- LLVM::C.LLVMInitializeX86TargetMC
16
+ LLVM::C.initialize_x86_target
17
+ LLVM::C.initialize_x86_target_info
18
+ LLVM::C.initialize_x86_target_mc
56
19
  end
57
20
 
58
21
  class JITCompiler
22
+ # Important: Call #dispose to free backend memory after use. Do not call #dispose on mod any more.
59
23
  def initialize(mod, opt_level = 3)
60
24
  FFI::MemoryPointer.new(FFI.type_size(:pointer)) do |ptr|
61
25
  error = FFI::MemoryPointer.new(FFI.type_size(:pointer))
62
- status = C.LLVMCreateJITCompilerForModule(ptr, mod, opt_level, error)
26
+ status = C.create_jit_compiler_for_module(ptr, mod, opt_level, error)
63
27
  errorp = error.read_pointer
64
28
  message = errorp.read_string unless errorp.null?
65
29
 
66
30
  if status.zero?
67
31
  @ptr = ptr.read_pointer
68
32
  else
69
- C.LLVMDisposeMessage(error)
33
+ C.dispose_message(error)
70
34
  error.autorelease=false
71
35
  raise RuntimeError, "Error creating JIT compiler: #{message}"
72
36
  end
73
37
  end
74
38
  end
39
+
40
+ def dispose
41
+ return if @ptr.nil?
42
+ C.dispose_execution_engine(@ptr)
43
+ @ptr = nil
44
+ end
75
45
 
76
46
  # @private
77
47
  def to_ptr
@@ -80,19 +50,30 @@ module LLVM
80
50
 
81
51
  # Execute the given LLVM::Function with the supplied args (as
82
52
  # GenericValues).
53
+ # Important: Call #dispose on the returned GenericValue to
54
+ # free backend memory after use.
83
55
  def run_function(fun, *args)
84
56
  FFI::MemoryPointer.new(FFI.type_size(:pointer) * args.size) do |args_ptr|
57
+ new_values = []
85
58
  args_ptr.write_array_of_pointer fun.params.zip(args).map { |p, a|
86
- a.kind_of?(GenericValue) ? a : LLVM.make_generic_value(p.type, a)
59
+ if a.kind_of?(GenericValue)
60
+ a
61
+ else
62
+ value = LLVM.make_generic_value(p.type, a)
63
+ new_values << value
64
+ value
65
+ end
87
66
  }
88
- return LLVM::GenericValue.from_ptr(
89
- C.LLVMRunFunction(self, fun, args.size, args_ptr))
67
+ result = LLVM::GenericValue.from_ptr(
68
+ C.run_function(self, fun, args.size, args_ptr))
69
+ new_values.each(&:dispose)
70
+ return result
90
71
  end
91
72
  end
92
73
 
93
74
  # Obtain an FFI::Pointer to a global within the current module.
94
75
  def pointer_to_global(global)
95
- C.LLVMGetPointerToGlobal(self, global)
76
+ C.get_pointer_to_global(self, global)
96
77
  end
97
78
  end
98
79
 
@@ -109,22 +90,28 @@ module LLVM
109
90
  val.instance_variable_set(:@ptr, ptr)
110
91
  val
111
92
  end
93
+
94
+ def dispose
95
+ return if @ptr.nil?
96
+ C.dispose_generic_value(@ptr)
97
+ @ptr = nil
98
+ end
112
99
 
113
100
  # Creates a Generic Value from an integer. Type is the size of integer to
114
101
  # create (ex. Int32, Int8, etc.)
115
102
  def self.from_i(i, options = {})
116
103
  type = options.fetch(:type, LLVM::Int)
117
104
  signed = options.fetch(:signed, true)
118
- from_ptr(C.LLVMCreateGenericValueOfInt(type, i, signed ? 1 : 0))
105
+ from_ptr(C.create_generic_value_of_int(type, i, signed ? 1 : 0))
119
106
  end
120
107
 
121
108
  # Creates a Generic Value from a Float.
122
109
  def self.from_f(f)
123
- from_ptr(C.LLVMCreateGenericValueOfFloat(LLVM::Float, f))
110
+ from_ptr(C.create_generic_value_of_float(LLVM::Float, f))
124
111
  end
125
112
 
126
113
  def self.from_d(val)
127
- from_ptr(C.LLVMCreateGenericValueOfFloat(LLVM::Double, val))
114
+ from_ptr(C.create_generic_value_of_float(LLVM::Double, val))
128
115
  end
129
116
 
130
117
  # Creates a GenericValue from a Ruby boolean.
@@ -134,17 +121,19 @@ module LLVM
134
121
 
135
122
  # Creates a GenericValue from an FFI::Pointer pointing to some arbitrary value.
136
123
  def self.from_value_ptr(ptr)
137
- from_ptr(LLVM::C.LLVMCreateGenericValueOfPointer(ptr))
124
+ from_ptr(LLVM::C.create_generic_value_of_pointer(ptr))
138
125
  end
139
126
 
140
127
  # Converts a GenericValue to a Ruby Integer.
141
128
  def to_i(signed = true)
142
- C.LLVMGenericValueToInt(self, signed ? 1 : 0)
129
+ v = C.generic_value_to_int(self, signed ? 1 : 0)
130
+ v -= 2**64 if signed and v >= 2**63
131
+ v
143
132
  end
144
133
 
145
134
  # Converts a GenericValue to a Ruby Float.
146
135
  def to_f(type = LLVM::Float.type)
147
- C.LLVMGenericValueToFloat(type, self)
136
+ C.generic_value_to_float(type, self)
148
137
  end
149
138
 
150
139
  # Converts a GenericValue to a Ruby boolean.
@@ -153,7 +142,7 @@ module LLVM
153
142
  end
154
143
 
155
144
  def to_value_ptr
156
- C.LLVMGenericValueToPointer(self)
145
+ C.generic_value_to_pointer(self)
157
146
  end
158
147
  end
159
148
 
@@ -0,0 +1,310 @@
1
+ # Generated by ffi_gen. Please do not change this file by hand.
2
+
3
+ require 'ffi'
4
+
5
+ module LLVM::C
6
+ extend FFI::Library
7
+ ffi_lib 'LLVM-3.0'
8
+
9
+ # (Not documented)
10
+ #
11
+ # @method link_in_jit()
12
+ # @return [nil]
13
+ # @scope class
14
+ attach_function :link_in_jit, :LLVMLinkInJIT, [], :void
15
+
16
+ # (Not documented)
17
+ #
18
+ # @method link_in_interpreter()
19
+ # @return [nil]
20
+ # @scope class
21
+ attach_function :link_in_interpreter, :LLVMLinkInInterpreter, [], :void
22
+
23
+ # (Not documented)
24
+ #
25
+ # = Fields:
26
+ #
27
+ class OpaqueGenericValue < FFI::Struct
28
+ end
29
+
30
+ # (Not documented)
31
+ #
32
+ # = Fields:
33
+ #
34
+ class OpaqueExecutionEngine < FFI::Struct
35
+ end
36
+
37
+ # ===-- Operations on generic values --------------------------------------===
38
+ #
39
+ # @method create_generic_value_of_int(ty, n, is_signed)
40
+ # @param [FFI::Pointer(TypeRef)] ty
41
+ # @param [Integer] n
42
+ # @param [Integer] is_signed
43
+ # @return [FFI::Pointer(GenericValueRef)]
44
+ # @scope class
45
+ attach_function :create_generic_value_of_int, :LLVMCreateGenericValueOfInt, [:pointer, :ulong_long, :int], :pointer
46
+
47
+ # (Not documented)
48
+ #
49
+ # @method create_generic_value_of_pointer(p)
50
+ # @param [FFI::Pointer(*Void)] p
51
+ # @return [FFI::Pointer(GenericValueRef)]
52
+ # @scope class
53
+ attach_function :create_generic_value_of_pointer, :LLVMCreateGenericValueOfPointer, [:pointer], :pointer
54
+
55
+ # (Not documented)
56
+ #
57
+ # @method create_generic_value_of_float(ty, n)
58
+ # @param [FFI::Pointer(TypeRef)] ty
59
+ # @param [Float] n
60
+ # @return [FFI::Pointer(GenericValueRef)]
61
+ # @scope class
62
+ attach_function :create_generic_value_of_float, :LLVMCreateGenericValueOfFloat, [:pointer, :double], :pointer
63
+
64
+ # (Not documented)
65
+ #
66
+ # @method generic_value_int_width(gen_val_ref)
67
+ # @param [FFI::Pointer(GenericValueRef)] gen_val_ref
68
+ # @return [Integer]
69
+ # @scope class
70
+ attach_function :generic_value_int_width, :LLVMGenericValueIntWidth, [:pointer], :uint
71
+
72
+ # (Not documented)
73
+ #
74
+ # @method generic_value_to_int(gen_val, is_signed)
75
+ # @param [FFI::Pointer(GenericValueRef)] gen_val
76
+ # @param [Integer] is_signed
77
+ # @return [Integer]
78
+ # @scope class
79
+ attach_function :generic_value_to_int, :LLVMGenericValueToInt, [:pointer, :int], :ulong_long
80
+
81
+ # (Not documented)
82
+ #
83
+ # @method generic_value_to_pointer(gen_val)
84
+ # @param [FFI::Pointer(GenericValueRef)] gen_val
85
+ # @return [FFI::Pointer(*Void)]
86
+ # @scope class
87
+ attach_function :generic_value_to_pointer, :LLVMGenericValueToPointer, [:pointer], :pointer
88
+
89
+ # (Not documented)
90
+ #
91
+ # @method generic_value_to_float(ty_ref, gen_val)
92
+ # @param [FFI::Pointer(TypeRef)] ty_ref
93
+ # @param [FFI::Pointer(GenericValueRef)] gen_val
94
+ # @return [Float]
95
+ # @scope class
96
+ attach_function :generic_value_to_float, :LLVMGenericValueToFloat, [:pointer, :pointer], :double
97
+
98
+ # (Not documented)
99
+ #
100
+ # @method dispose_generic_value(gen_val)
101
+ # @param [FFI::Pointer(GenericValueRef)] gen_val
102
+ # @return [nil]
103
+ # @scope class
104
+ attach_function :dispose_generic_value, :LLVMDisposeGenericValue, [:pointer], :void
105
+
106
+ # ===-- Operations on execution engines -----------------------------------===
107
+ #
108
+ # @method create_execution_engine_for_module(out_ee, m, out_error)
109
+ # @param [FFI::Pointer(*ExecutionEngineRef)] out_ee
110
+ # @param [FFI::Pointer(ModuleRef)] m
111
+ # @param [FFI::Pointer(**Char_S)] out_error
112
+ # @return [Integer]
113
+ # @scope class
114
+ attach_function :create_execution_engine_for_module, :LLVMCreateExecutionEngineForModule, [:pointer, :pointer, :pointer], :int
115
+
116
+ # (Not documented)
117
+ #
118
+ # @method create_interpreter_for_module(out_interp, m, out_error)
119
+ # @param [FFI::Pointer(*ExecutionEngineRef)] out_interp
120
+ # @param [FFI::Pointer(ModuleRef)] m
121
+ # @param [FFI::Pointer(**Char_S)] out_error
122
+ # @return [Integer]
123
+ # @scope class
124
+ attach_function :create_interpreter_for_module, :LLVMCreateInterpreterForModule, [:pointer, :pointer, :pointer], :int
125
+
126
+ # (Not documented)
127
+ #
128
+ # @method create_jit_compiler_for_module(out_jit, m, opt_level, out_error)
129
+ # @param [FFI::Pointer(*ExecutionEngineRef)] out_jit
130
+ # @param [FFI::Pointer(ModuleRef)] m
131
+ # @param [Integer] opt_level
132
+ # @param [FFI::Pointer(**Char_S)] out_error
133
+ # @return [Integer]
134
+ # @scope class
135
+ attach_function :create_jit_compiler_for_module, :LLVMCreateJITCompilerForModule, [:pointer, :pointer, :uint, :pointer], :int
136
+
137
+ # Deprecated: Use LLVMCreateExecutionEngineForModule instead.
138
+ #
139
+ # @method create_execution_engine(out_ee, mp, out_error)
140
+ # @param [FFI::Pointer(*ExecutionEngineRef)] out_ee
141
+ # @param [FFI::Pointer(ModuleProviderRef)] mp
142
+ # @param [FFI::Pointer(**Char_S)] out_error
143
+ # @return [Integer]
144
+ # @scope class
145
+ attach_function :create_execution_engine, :LLVMCreateExecutionEngine, [:pointer, :pointer, :pointer], :int
146
+
147
+ # Deprecated: Use LLVMCreateInterpreterForModule instead.
148
+ #
149
+ # @method create_interpreter(out_interp, mp, out_error)
150
+ # @param [FFI::Pointer(*ExecutionEngineRef)] out_interp
151
+ # @param [FFI::Pointer(ModuleProviderRef)] mp
152
+ # @param [FFI::Pointer(**Char_S)] out_error
153
+ # @return [Integer]
154
+ # @scope class
155
+ attach_function :create_interpreter, :LLVMCreateInterpreter, [:pointer, :pointer, :pointer], :int
156
+
157
+ # Deprecated: Use LLVMCreateJITCompilerForModule instead.
158
+ #
159
+ # @method create_jit_compiler(out_jit, mp, opt_level, out_error)
160
+ # @param [FFI::Pointer(*ExecutionEngineRef)] out_jit
161
+ # @param [FFI::Pointer(ModuleProviderRef)] mp
162
+ # @param [Integer] opt_level
163
+ # @param [FFI::Pointer(**Char_S)] out_error
164
+ # @return [Integer]
165
+ # @scope class
166
+ attach_function :create_jit_compiler, :LLVMCreateJITCompiler, [:pointer, :pointer, :uint, :pointer], :int
167
+
168
+ # (Not documented)
169
+ #
170
+ # @method dispose_execution_engine(ee)
171
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
172
+ # @return [nil]
173
+ # @scope class
174
+ attach_function :dispose_execution_engine, :LLVMDisposeExecutionEngine, [:pointer], :void
175
+
176
+ # (Not documented)
177
+ #
178
+ # @method run_static_constructors(ee)
179
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
180
+ # @return [nil]
181
+ # @scope class
182
+ attach_function :run_static_constructors, :LLVMRunStaticConstructors, [:pointer], :void
183
+
184
+ # (Not documented)
185
+ #
186
+ # @method run_static_destructors(ee)
187
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
188
+ # @return [nil]
189
+ # @scope class
190
+ attach_function :run_static_destructors, :LLVMRunStaticDestructors, [:pointer], :void
191
+
192
+ # (Not documented)
193
+ #
194
+ # @method run_function_as_main(ee, f, arg_c, arg_v, env_p)
195
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
196
+ # @param [FFI::Pointer(ValueRef)] f
197
+ # @param [Integer] arg_c
198
+ # @param [FFI::Pointer(**Char_S)] arg_v
199
+ # @param [FFI::Pointer(**Char_S)] env_p
200
+ # @return [Integer]
201
+ # @scope class
202
+ attach_function :run_function_as_main, :LLVMRunFunctionAsMain, [:pointer, :pointer, :uint, :pointer, :pointer], :int
203
+
204
+ # (Not documented)
205
+ #
206
+ # @method run_function(ee, f, num_args, args)
207
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
208
+ # @param [FFI::Pointer(ValueRef)] f
209
+ # @param [Integer] num_args
210
+ # @param [FFI::Pointer(*GenericValueRef)] args
211
+ # @return [FFI::Pointer(GenericValueRef)]
212
+ # @scope class
213
+ attach_function :run_function, :LLVMRunFunction, [:pointer, :pointer, :uint, :pointer], :pointer
214
+
215
+ # (Not documented)
216
+ #
217
+ # @method free_machine_code_for_function(ee, f)
218
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
219
+ # @param [FFI::Pointer(ValueRef)] f
220
+ # @return [nil]
221
+ # @scope class
222
+ attach_function :free_machine_code_for_function, :LLVMFreeMachineCodeForFunction, [:pointer, :pointer], :void
223
+
224
+ # (Not documented)
225
+ #
226
+ # @method add_module(ee, m)
227
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
228
+ # @param [FFI::Pointer(ModuleRef)] m
229
+ # @return [nil]
230
+ # @scope class
231
+ attach_function :add_module, :LLVMAddModule, [:pointer, :pointer], :void
232
+
233
+ # Deprecated: Use LLVMAddModule instead.
234
+ #
235
+ # @method add_module_provider(ee, mp)
236
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
237
+ # @param [FFI::Pointer(ModuleProviderRef)] mp
238
+ # @return [nil]
239
+ # @scope class
240
+ attach_function :add_module_provider, :LLVMAddModuleProvider, [:pointer, :pointer], :void
241
+
242
+ # (Not documented)
243
+ #
244
+ # @method remove_module(ee, m, out_mod, out_error)
245
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
246
+ # @param [FFI::Pointer(ModuleRef)] m
247
+ # @param [FFI::Pointer(*ModuleRef)] out_mod
248
+ # @param [FFI::Pointer(**Char_S)] out_error
249
+ # @return [Integer]
250
+ # @scope class
251
+ attach_function :remove_module, :LLVMRemoveModule, [:pointer, :pointer, :pointer, :pointer], :int
252
+
253
+ # Deprecated: Use LLVMRemoveModule instead.
254
+ #
255
+ # @method remove_module_provider(ee, mp, out_mod, out_error)
256
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
257
+ # @param [FFI::Pointer(ModuleProviderRef)] mp
258
+ # @param [FFI::Pointer(*ModuleRef)] out_mod
259
+ # @param [FFI::Pointer(**Char_S)] out_error
260
+ # @return [Integer]
261
+ # @scope class
262
+ attach_function :remove_module_provider, :LLVMRemoveModuleProvider, [:pointer, :pointer, :pointer, :pointer], :int
263
+
264
+ # (Not documented)
265
+ #
266
+ # @method find_function(ee, name, out_fn)
267
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
268
+ # @param [String] name
269
+ # @param [FFI::Pointer(*ValueRef)] out_fn
270
+ # @return [Integer]
271
+ # @scope class
272
+ attach_function :find_function, :LLVMFindFunction, [:pointer, :string, :pointer], :int
273
+
274
+ # (Not documented)
275
+ #
276
+ # @method recompile_and_relink_function(ee, fn)
277
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
278
+ # @param [FFI::Pointer(ValueRef)] fn
279
+ # @return [FFI::Pointer(*Void)]
280
+ # @scope class
281
+ attach_function :recompile_and_relink_function, :LLVMRecompileAndRelinkFunction, [:pointer, :pointer], :pointer
282
+
283
+ # (Not documented)
284
+ #
285
+ # @method get_execution_engine_target_data(ee)
286
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
287
+ # @return [FFI::Pointer(TargetDataRef)]
288
+ # @scope class
289
+ attach_function :get_execution_engine_target_data, :LLVMGetExecutionEngineTargetData, [:pointer], :pointer
290
+
291
+ # (Not documented)
292
+ #
293
+ # @method add_global_mapping(ee, global, addr)
294
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
295
+ # @param [FFI::Pointer(ValueRef)] global
296
+ # @param [FFI::Pointer(*Void)] addr
297
+ # @return [nil]
298
+ # @scope class
299
+ attach_function :add_global_mapping, :LLVMAddGlobalMapping, [:pointer, :pointer, :pointer], :void
300
+
301
+ # (Not documented)
302
+ #
303
+ # @method get_pointer_to_global(ee, global)
304
+ # @param [FFI::Pointer(ExecutionEngineRef)] ee
305
+ # @param [FFI::Pointer(ValueRef)] global
306
+ # @return [FFI::Pointer(*Void)]
307
+ # @scope class
308
+ attach_function :get_pointer_to_global, :LLVMGetPointerToGlobal, [:pointer, :pointer], :pointer
309
+
310
+ end