Class: FastRuby::Method

Inherits:
Object show all
Defined in:
lib/fastruby/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, owner) ⇒ Method

Returns a new instance of Method.



53
54
55
56
57
# File 'lib/fastruby/builder.rb', line 53

def initialize(method_name, owner)
  @method_name = method_name
  @owner = owner
  @observers = Hash.new
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



36
37
38
# File 'lib/fastruby/builder.rb', line 36

def options
  @options
end

Class Method Details

.notify_method_name(mname) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/fastruby/builder.rb', line 44

def self.notify_method_name(mname)
  return unless @observers
  return unless @observers[mname]
  
  @observers[mname].each do |obs|
    obs.call
  end
end

.observe_method_name(mname, &blk) ⇒ Object



38
39
40
41
42
# File 'lib/fastruby/builder.rb', line 38

def self.observe_method_name(mname, &blk)
  @observers ||= Hash.new
  @observers[mname] = @observers[mname] || Array.new
  @observers[mname] << lambda(&blk)
end

Instance Method Details

#build(signature, noreturn = false) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fastruby/builder.rb', line 71

def build(signature, noreturn = false)
  return nil unless tree

  no_cache = false
  mname = FastRuby.make_str_signature(@method_name, signature)

  if @owner.respond_to? :method_hash
    method_hash = @owner.method_hash(@method_name.to_sym) || {}
    if (@owner.has_fastruby_function(method_hash,mname.to_s))
      FastRuby.logger.info "NOT Building #{@owner}::#{@method_name} for signature #{signature.inspect}, it's already done"
      return nil
    end
  end

  rebuild(signature, noreturn)
end

#has_loops?(tree) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fastruby/builder.rb', line 88

def has_loops?(tree)
  if tree.respond_to? :node_type
    nt = tree.node_type
    return false if nt == :defn or nt == :defs
    return true if nt == :for or nt == :iter or nt == :while or nt == :retry
    tree.each do |subtree|
      return true if has_loops? subtree
    end
  end
  false
end

#observe(key, &blk) ⇒ Object



59
60
61
# File 'lib/fastruby/builder.rb', line 59

def observe(key, &blk)
  @observers[key] = blk
end

#rebuild(signature, noreturn = false) ⇒ Object



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

  # create random method name
  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)
  
  #require "pry"; binding.pry
  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.extra_code
          builder.init_extra = context.init_extra
  
            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

#tree_changedObject



63
64
65
66
67
68
69
# File 'lib/fastruby/builder.rb', line 63

def tree_changed
  FastRuby::Method.notify_method_name(@method_name)
  
  @observers.values.each do |observer|
    observer.call(self)
  end
end