Class: FastRuby::Method

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, owner) ⇒ Method

Returns a new instance of Method.



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

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

Instance Attribute Details

#localsObject

Returns the value of attribute locals.



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

def locals
  @locals
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#snippet_hashObject

Returns the value of attribute snippet_hash.



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

def snippet_hash
  @snippet_hash
end

Instance Method Details

#build(signature, noreturn = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/fastruby/builder.rb', line 55

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

#observe(key, &blk) ⇒ Object



45
46
47
# File 'lib/fastruby/builder.rb', line 45

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

#rebuild(signature, noreturn = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
# File 'lib/fastruby/builder.rb', line 72

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

  inliner = FastRuby::Inliner.new

  context = FastRuby::Context.new
  context.options = options

  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

  # create random method name
  context.snippet_hash = snippet_hash
  context.alt_method_name = "_" + @method_name.to_s + "_" + rand(10000000000).to_s

  (1..signature.size-1).each do |i|
    arg = args_tree[i]
    
    if arg.instance_of? Symbol
      
      if arg
        if arg.to_s.match(/\*/)
          context.infer_lvar_map[arg.to_s.gsub("*","").to_sym] = Array
        else
          context.infer_lvar_map[arg.to_sym] = signature[i]
        end
      end
    end
  end

  context.infer_self = signature[0]
  
  inliner.infer_self = context.infer_self
  inliner.infer_lvar_map = context.infer_lvar_map

  inlined_tree = inliner.inline(tree)
  context.locals = locals + inliner.extra_locals
  
  inliner.extra_inferences.each do |local, itype|
    context.infer_lvar_map[local] = itype
  end
  
  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?
    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



49
50
51
52
53
# File 'lib/fastruby/builder.rb', line 49

def tree_changed
  @observers.values.each do |observer|
    observer.call(self)
  end
end