Class: RubyVM

Inherits:
Object show all
Defined in:
lib/ruby_vm.rb

Class Method Summary collapse

Class Method Details

.call_bytecode(bytecode, farg) ⇒ Object



293
294
295
296
# File 'lib/ruby_vm.rb', line 293

def self.call_bytecode(bytecode, farg)
  f = compile_bytecode(bytecode)
  ExecutionEngine.run_function(f, nil, farg)
end

.compile_bytecode(bytecode) ⇒ Object



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
# File 'lib/ruby_vm.rb', line 302

def self.compile_bytecode(bytecode) 
  f = @module.get_or_insert_function("vm_func#{@func_n}", Type.function(VALUE, [VALUE, VALUE]))
  @func_n += 1

  get_self = f.arguments[0]

  entry_block = f.create_block
  b = entry_block.builder
  Builder.set_globals(b)
  b.push(f.arguments[1])

  blocks = bytecode.map { f.create_block } 
  exit_block = f.create_block
  blocks << exit_block
  b.br(blocks.first)

  op_builder = OpCodeBuilder.new(@module, f, @rb_funcs)

  bytecode.each_with_index do |opcode, i|
    op, arg = opcode

    block = blocks[i] 
    b.set_insert_point(block)

    case op
    when :jump
      b.br(blocks[arg])
    when :branchif
      v = b.pop
      cmp = b.icmp_eq(v, 0.llvm)
      b.cond_br(cmp, blocks[i+1], blocks[arg])
    when :branchunless
      v = b.pop
      cmp = b.icmp_eq(v, 0.llvm)
      b.cond_br(cmp, blocks[arg], blocks[i+1])
    else
      op_builder.__send__(op, b, arg)
    end

    if op != :jump && op != :branchif && op != :branchunless
      b.br(blocks[i+1])
    end
  end

  b = exit_block.builder
  ret_val = b.pop
  b.return(ret_val)

  f
end

.ftype(ret, args) ⇒ Object



289
290
291
# File 'lib/ruby_vm.rb', line 289

def self.ftype(ret, args)
  Type.function(ret, args)
end

.method_send(recv, compiled_method, farg = nil) ⇒ Object



298
299
300
# File 'lib/ruby_vm.rb', line 298

def self.method_send(recv, compiled_method, farg = nil)
  ExecutionEngine.run_function(compiled_method, recv, farg)
end

.startObject



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
# File 'lib/ruby_vm.rb', line 262

def self.start
  @module = LLVM::Module.new('ruby_vm')
  ExecutionEngine.get(@module)

  @rb_ary_new = @module.external_function('rb_ary_new', ftype(VALUE, []))
  @rb_hash_new = @module.external_function('rb_hash_new', ftype(VALUE, []))
  @rb_hash_aset = @module.external_function('rb_hash_aset', ftype(VALUE, [VALUE, VALUE, VALUE]))
  @rb_ary_store = @module.external_function('rb_ary_store', ftype(VALUE, [VALUE, LONG, VALUE]))
  @rb_to_id = @module.external_function('rb_to_id', ftype(VALUE, [VALUE]))
  @rb_ivar_get = @module.external_function('rb_ivar_get', ftype(VALUE, [VALUE, ID]))
  @rb_ivar_set = @module.external_function('rb_ivar_set', ftype(VALUE, [VALUE, ID, VALUE]))
  @rb_funcall2 = @module.external_function('rb_funcall2', ftype(VALUE, [VALUE, ID, INT, P_VALUE]))

  @rb_funcs = {
    :rb_ary_new => @rb_ary_new,
    :rb_hash_new => @rb_hash_new,
    :rb_hash_aset => @rb_hash_aset,
    :rb_ary_store => @rb_ary_store,
    :rb_to_id => @rb_to_id,
    :rb_ivar_get => @rb_ivar_get,
    :rb_ivar_set => @rb_ivar_set,
    :rb_funcall2 => @rb_funcall2
  }

  @func_n = 0
end