100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
# File 'lib/fastruby/builder.rb', line 100
def rebuild(signature, noreturn = false)
no_cache = false
mname = FastRuby.make_str_signature(@method_name, signature)
args_tree = if tree[0] == :defn
tree[2]
elsif tree[0] == :defs
tree[3]
else
raise ArgumentError, "unknown type of method definition #{tree[0]}"
end
impl_tree = if tree[0] == :defn
tree[3]
elsif tree[0] == :defs
tree[4]
end
infer_lvar_map = Hash.new
(1..signature.size-1).each do |i|
arg = args_tree[i]
if arg.instance_of? Symbol
if arg
if arg.to_s.match(/\*/)
infer_lvar_map[arg.to_s.gsub("*","").to_sym] = Array
else
infer_lvar_map[arg.to_sym] = signature[i]
end
end
end
end
inferencer = Inferencer.new
inferencer.infer_self = signature[0]
locals_inference = LocalsInference.new
locals_inference.infer_self = signature[0]
locals_inference.infer_lvar_map = infer_lvar_map
inference_updater = InferenceUpdater.new(inferencer)
inliner = FastRuby::Inliner.new(inferencer)
pipeline = Pipeline.new
if options[:validate_lvar_types]
pipeline << LvarType.new(locals_inference)
end
pipeline << locals_inference
if has_loops?(impl_tree)
pipeline << inliner
if options[:validate_lvar_types]
pipeline << LvarType.new(locals_inference)
end
pipeline << inference_updater
pipeline << inliner
if options[:validate_lvar_types]
pipeline << LvarType.new(locals_inference)
end
pipeline << inference_updater
end
inlined_tree = pipeline.call(tree)
inliner.inlined_methods.each do |inlined_method|
inlined_method.observe("#{@owner}##{@method_name}#{mname}") do |imethod|
rebuild(signature, noreturn)
end
end
alt_options = options.dup
alt_options.delete(:self)
code_sha1 = FastRuby.cache.hash_snippet(inlined_tree.inspect, FastRuby::VERSION + signature.map(&:to_s).join('-') + alt_options.inspect)
paths = FastRuby.cache.retrieve(code_sha1)
$last_obj_proc = nil
if paths.empty?
unless Object.respond_to? :inline
require "rubygems"
require "inline"
require "fastruby/inline_extension"
end
unless defined? FastRuby::Context
require "fastruby/translator/translator"
end
context = FastRuby::Context.new(true, inferencer)
context.options = options
context.locals = FastRuby::GetLocalsProcessor.get_locals(inlined_tree)
FastRuby.logger.info "Compiling #{@owner}::#{@method_name} for signature #{signature.inspect}"
c_code = context.to_c_method(inlined_tree,signature)
unless options[:main]
context.define_method_at_init(@method_name, args_tree.size+1, signature)
end
so_name = nil
old_class_self = $class_self
$class_self = @owner
begin
unless $inline_extra_flags
$inline_extra_flags = true
['CFLAGS','CXXFLAGS','OPTFLAGS','cflags','cxxflags','optflags'].each do |name|
RbConfig::CONFIG[name].gsub!(/\-O\d/,"-O1") if RbConfig::CONFIG[name]
end
if RUBY_VERSION =~ /^1\.8/
RbConfig::CONFIG['CFLAGS'] << " -DRUBY_1_8 -Wno-clobbered"
elsif RUBY_VERSION =~ /^1\.9/
RbConfig::CONFIG['CFLAGS'] << " -DRUBY_1_9 -Wno-clobbered"
end
end
@owner.class_eval do
inline :C do |builder|
builder.inc << context.
builder. = context.
def builder.generate_ext
ext = []
@inc.unshift "#include \"ruby.h\""
ext << @inc
ext << nil
ext << @src.join("\n\n")
ext << nil
ext << nil
ext << "#ifdef __cplusplus"
ext << "extern \"C\" {"
ext << "#endif"
ext << " void Init_#{module_name}() {"
ext << @init_extra.join("\n") unless @init_extra.empty?
ext << nil
ext << " }"
ext << "#ifdef __cplusplus"
ext << "}"
ext << "#endif"
ext << nil
ext.join "\n"
end
builder.c c_code
so_name = builder.so_name
end
end
unless no_cache
no_cache = context.no_cache
end
unless no_cache
FastRuby.cache.insert(code_sha1, so_name)
end
ensure
$class_self = old_class_self
end
else
paths.each do |path|
require path
end
end
if $last_obj_proc
FastRuby.cache.register_proc(code_sha1, $last_obj_proc)
end
$class_self = @owner
begin
FastRuby.cache.execute(code_sha1, signature, @owner)
ensure
$class_self = old_class_self
end
observe("#{@owner}##{@method_name}#{mname}") do |imethod|
if tree
rebuild(signature, noreturn)
end
end
end
|