evanphx / rubinius

Rubinius, the Ruby VM

This URL has Read+Write access

rubinius / kernel / compiler / compile.rb
100644 351 lines (288 sloc) 9.98 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
module Rubinius
 
  ##
  # This const controls what the lowest version of compiled methods we can
  # allow is. This allows us to cut off compability at some point, or just
  # increment when major changes are made to the compiler.
 
  CompiledMethodVersion = 6
 
end
 
##
# Main entrace point to the Rubinius compiler. Handles loading and compiling
# ruby code.
 
class Compiler
  # TODO: This is temporary until the compiler is refactored
  def self.version_number
    42 # ?
  end
 
  # TODO: This is temporary until the compiler is refactored
  module Utils
 
  @load_rbc_directly = false
 
  def self.compiler
    return ::Compiler
  end
 
  def self.version_number
    return self.compiler ? self.compiler.version_number : 0
  end
 
  def self.compile_file(path, flags=nil)
    compiler.compile_file(path, flags)
  end
 
  def self.compile_string(string, context=nil, filename="(eval)", line=1)
    compiler.compile_string(string, context, filename, line)
  end
 
  def self.execute(string)
    eval(string, TOPLEVEL_BINDING)
  end
 
  # Sets a flag so that the next script to be loaded gets a breakpoint set at
  # the first instruction
  def self.debug_script!
    @debug_script = true
  end
 
  # Called when we encounter a break keyword that we do not support
  # TODO - This leaves a moderately lame stack trace entry
  def self.__unexpected_break__
    raise LocalJumpError, "unexpected break"
  end
 
  def self.load_from_rbc(path, version)
    Ruby.primitive :compiledfile_load
    raise PrimitiveFailure, "CompiledFile.load_from_rbc primitive failed"
 
    # HACK: remove the primitive above when compiled_file.rb
    # unmarshal_data method is fixed and ruby performance is better
    puts "[Loading: #{path}]" if Rubinius::TaskProbe.enabled? :load_runtime
    File.open(path) do |io|
      cf = Rubinius::CompiledFile.load(io)
      # HACK check version!
      return cf.body
    end
  end
 
  # Internally used by #load and #require. Determines whether to
  # load the file directly or by prefixing it with the paths in
  # $LOAD_PATH and then attempts to locate and load the file.
  def self.unified_load(path, rb, rbc, ext, requiring = nil,
                        options = {:recompile => false})
 
    # ./ ../ ~/ /
    if path =~ %r{\A(?:(\.\.?)|(~))?/}
      if $2 # ~
        rb.slice! '~/' if rb
        rbc.slice! '~/' if rbc
        ext.slice! '~/' if ext
        res = Compiler::Utils.single_load "#{ENV['HOME']}/", rb, rbc, ext, requiring, options
 
      else
        res = Compiler::Utils.single_load '', rb, rbc, ext, requiring, options
      end
 
      return res unless res.nil? # false is valid
 
    # Unqualified
    else
      $LOAD_PATH.each do |dir|
        if rbc and File.file?(dir) and dir.suffix?('.rba') and !options[:recompile]
          begin
            _, _, _, _, _, data = Ar.new(dir).extract rbc
          rescue Ar::Error
          end
 
          cm = if data then
                 # HACK check version
                 Rubinius::CompiledFile.load(data).body
               else
                 data = Ar.new(dir).extract rbc
                 # HACK check version
                 Rubinius::CompiledFile.load(data).body if data
               end
 
          if cm
            return false if requiring and $LOADED_FEATURES.include? rb
 
            cm.compile
            cm.hints = { :source => :rba }
            cm.as_script do |script|
              script.path = rb
            end
 
            $LOADED_FEATURES << rb if requiring
 
            # Add script CM to CompiledMethod.scripts
            Rubinius::CompiledMethod.scripts[rb] = cm
 
            return true
          end
          # Fall through
        end
 
        res = Compiler::Utils.single_load "#{dir}/", rb, rbc, ext, requiring, options
        return res unless res.nil? # false is valid
      end
    end
 
    raise LoadError, "no such file to load -- #{path}"
  end
 
  def self.compile_feature(rb, requiring, &block)
    $LOADED_FEATURES << rb if requiring
    begin
      yield
    rescue Exception => e
      $LOADED_FEATURES.delete(rb) if requiring
      raise e
    end
  end
 
  # Internally used by #unified_load. This attempts to load the
  # designated file from a single prefix path.
  def self.single_load(dir, rb, rbc, ext, requiring, options)
    if rb
      return false if requiring and $LOADED_FEATURES.include? rb
 
      rb_path = "#{dir}#{rb}"
      rb_stat = File::Stat.stat rb_path
 
      if rb_stat and rb_stat.file?
        if rbc
          rbc_path = "#{dir}#{rbc}"
          rbc_stat = File::Stat.stat rbc_path
        else
          rbc_path = nil
          rbc_stat = nil
        end
 
        cm = nil
 
        # Try to load rbc directly if requested. In this mode, we require
        # that the .rbc file exist already if the .rb file does. We fail
        # hard if it doesn't.
        if @load_rbc_directly
 
          if rbc_stat and rbc_stat.file?
            compile_feature(rb, requiring) do
              cm = load_from_rbc(rbc_path, version_number)
              raise LoadError, "Invalid .rbc: #{rbc_path}" unless cm
            end
          else
            raise LoadError, "Unable to find '#{rbc_path}' to load directly"
          end
 
        # Prefer compiled whenever possible
        elsif !rbc_stat or
              !rbc_stat.file? or
               rb_stat.mtime > rbc_stat.mtime or
               options[:recompile]
 
          if $DEBUG_LOADING
            if !rbc_path
              STDERR.puts "[Compiling #{rb_path}: .rbc file disable]"
            elsif !File.file?(rbc_path)
              STDERR.puts "[Compiling #{rb_path}: Missing compiled version]"
            else
              STDERR.puts "[Compiling #{rb_path}: Newer source file]"
            end
          end
 
          compile_feature(rb, requiring) do
            cm = Compiler::Utils.compile_file(rb_path)
            raise LoadError, "Unable to compile: #{rb_path}" unless cm
          end
 
          # Store it for the future
          Rubinius::CompiledFile.dump cm, rbc_path if rbc_path
        else
          if $DEBUG_LOADING
            STDERR.puts "[Loading #{rbc_path}]"
          end
 
          compile_feature(rb, requiring) do
            cm = load_from_rbc(rbc_path, version_number)
            # cm is nil if the file is out of date, version wise.
            unless cm
              if $DEBUG_LOADING
                STDERR.puts "[Recompling #{rb_path}, old version]"
              end
 
              compile_feature(rb, requiring) do
                cm = Compiler::Utils.compile_file(rb_path)
                raise LoadError, "Unable to compile: #{rb_path}" unless cm
              end
 
              Rubinius::CompiledFile.dump cm, rbc_path
            end
          end
        end
 
        # Add script CM to CompiledMethod.scripts
        Rubinius::CompiledMethod.scripts[rb] = cm
 
        begin
          cm.compile
          cm.hints = { :source => :rb }
          # Set a breakpoint on the script CompiledMethod if flag is set
          if @debug_script
            Debugger.instance.set_breakpoint cm, 0
            @debug_script = false
          end
          cm.as_script do |script|
            script.path = rb_path
          end
        rescue Exception => e
          $LOADED_FEATURES.delete(rb) if requiring
          raise e
        end
 
        return true
      end
    end
 
    if rbc
      rb = rbc.chomp 'c'
      return false if requiring and $LOADED_FEATURES.include?(rb)
 
      rbc_path = "#{dir}#{rbc}"
 
      if File.file? rbc_path and !options[:recompile]
        compile_feature(rb, requiring) do
          cm = load_from_rbc(rbc_path, version_number)
          raise LoadError, "Invalid .rbc: #{rbc_path}" unless cm
        end
 
        begin
          cm.compile
          cm.hints = { :source => :rbc }
          cm.as_script do |script|
            script.path = rb_path
          end
        rescue Exception => e
          $LOADED_FEATURES.delete(rb) if requiring
          raise e
        end
 
        return true
      end
    end
 
    if ext
      return false if requiring and $LOADED_FEATURES.include? ext
 
      ext_path = "#{dir}#{ext}"
      ext_name = File.basename ext, "#{Rubinius::LIBSUFFIX}"
 
      if File.file? ext_path
        case Rubinius::NativeMethod.load_extension(ext_path, ext_name)
        when true
          $LOADED_FEATURES << ext if requiring
          return true
        when 0 # Absent or invalid
          return nil
        when 1 # Valid library, but no entry point
          raise LoadError, "Invalid extension at '#{ext_path}'. " \
                           "Did you define Init_#{ext_name}?"
        end
      end
    end
 
    nil
  end
 
  def self.load_from_extension(path)
    path = StringValue(path)
    # Remap all library extensions behind the scenes, just like MRI
    path.gsub!(/\.(so|bundle|dll|dylib)$/, "#{Rubinius::LIBSUFFIX}")
    if path.suffix? '.rbc'
      rb, rbc, ext = nil, path, nil
    elsif path.suffix? '.rb'
      rb, rbc, ext = path, "#{path}c", nil
    elsif path.suffix? "#{Rubinius::LIBSUFFIX}"
      rb, rbc, ext = nil, nil, path
    else
      dir, name = File.split(path)
      rb = path
      ext = nil
 
      if name[0] == ?.
        rbc = nil
      else
        rbc = "#{dir}/#{name}.compiled.rbc"
      end
    end
 
    Compiler::Utils.single_load '', rb, rbc, ext, false, {}
  end
 
  def self.split_path(path)
    # Remap all library extensions behind the scenes, just like MRI
    path.gsub!(/\.(so|bundle|dll|dylib)$/, "#{Rubinius::LIBSUFFIX}")
 
    if path.suffix? '.rbc'
      rb, rbc, ext = nil, path, nil
    elsif path.suffix? '.rb'
      rb, rbc, ext = path, "#{path}c", nil
    elsif path.suffix? "#{Rubinius::LIBSUFFIX}"
      rb, rbc, ext = nil, nil, path
    else
      rb = "#{path}.rb"
      ext = "#{path}#{Rubinius::LIBSUFFIX}"
 
      if name[0] == ?.
        rbc = nil
      else
        rbc = "#{path}.rbc"
      end
    end
    return rb,rbc,ext
  end
 
  end # module Utils
end # class Compiler