Module: YTL

Includes:
YTLJit
Defined in:
lib/ytl.rb,
lib/ytl/accmem.rb,
lib/ytl/importobj.rb

Defined Under Namespace

Classes: Memory

Constant Summary collapse

ISEQ_OPTS =
{  
  :peephole_optimization    => true,
  :inline_const_cache       => false,
  :specialized_instruction  => false,
}
TheMemory =
Memory.new

Class Method Summary collapse

Class Method Details

.import_ruby_object(context) ⇒ Object



4
5
6
7
8
# File 'lib/ytl/importobj.rb', line 4

def self.import_ruby_object(context)
  context.import_object(Object, :Array, Array) 
  context.import_object(Object, :String, String)
  context.import_object(Object, :Math, Math)
end

.main(options) ⇒ Object



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

def self.main(options)
  tr_context = VM::YARVContext.new
  progs = []

  import_ruby_object(tr_context)
  options[:execute_before_compile].each do |fn|
    rf = File.read(fn)
    prog = eval(rf)
    progs.push prog
    is = RubyVM::InstructionSequence.compile(prog, ARGV[0], 
                                           "", 0, ISEQ_OPTS).to_a
    iseq = VMLib::InstSeqTree.new(nil, is)
    tr = VM::YARVTranslatorCRubyObject.new([iseq])
    tr.translate(tr_context)
  end

  prog = File.read(ARGV[0])
  is = RubyVM::InstructionSequence.compile(prog, ARGV[0], 
                                           "", 0, ISEQ_OPTS).to_a
  iseq = VMLib::InstSeqTree.new(nil, is)
  if options[:dump_yarv] then
    pp iseq
  end
  
  tr = VM::YARVTranslatorCRubyObject.new([iseq])
  tnode = tr.translate(tr_context)
  ci_context = VM::CollectInfoContext.new(tnode)
  tnode.collect_info(ci_context)

  if fn = options[:write_node_before_ti] then
    File.open(fn, "w") do |fp|
      fp.print Marshal.dump(tnode)
    end
  end

  dmylit = VM::Node::LiteralNode.new(tnode, nil)
  arg = [dmylit, dmylit, dmylit]
  sig = []
  arg.each do |ele|
    sig.push RubyType::BaseType.from_ruby_class(NilClass)
  end

  ti_context = VM::TypeInferenceContext.new(tnode)
  begin
    tnode.collect_candidate_type(ti_context, arg, sig)
  end until ti_context.convergent
  ti_context = tnode.collect_candidate_type(ti_context, arg, sig)
  
  c_context = VM::CompileContext.new(tnode)
  c_context.current_method_signature.push sig
  c_context.options = options
  c_context = tnode.compile(c_context)
  tnode.make_frame_struct_tab

  if fn = options[:write_node_after_ti] then
    File.open(fn, "w") do |fp|
      fp.print Marshal.dump(tnode)
    end
  end

  if options[:disasm] then
    tnode.code_space_tab.each do |cs|
      cs.fill_disasm_cache
    end
    tnode.code_space.disassemble
  end

  tcs = tnode.code_space
  STDOUT.flush
  tcs.call(tcs.base_address)
end

.parse_opt(argv) ⇒ Object



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

def self.parse_opt(argv)
  ytlopt = {}
  prelude = File.join(File.dirname(__FILE__), "..", "runtime", "prelude.rb")
  ytlopt[:execute_before_compile] = [prelude]
  opt = OptionParser.new
  
  opt.on('--disasm', 'Disasemble generated code') do |f|
    ytlopt[:disasm] = f
  end

  opt.on('--dump-yarv', 'Dump YARV byte code') do |f|
    ytlopt[:dump_yarv] = f
  end

  opt.on('--disp-signature', 'Display signature of method') do |f|
    ytlopt[:disp_signature] = f
  end

  opt.on('--dump-context', 'Dump context(registor/stack) for debug') do |f|
    ytlopt[:dump_context] = f
  end

  opt.on('--write-code =FILE', 'Write generating code') do |f|
    ytlopt[:write_code] = f
  end

  opt.on('--write-node-before-type-inference =FILE', 
         'Write node of before type inference') do |f|
    ytlopt[:write_node_before_ti] = f
  end

  opt.on('--write-node-after-type-inference =FILE', 
         'Write node of after type inference') do |f|
    ytlopt[:write_node_after_ti] = f
  end

  opt.on('-r FILE', '--execute-before-compile =FILE', 
         'Execute ruby program (execute by CRuby)') do |f|
    ytlopt[:execute_before_compile].push f
  end

  opt.parse!(argv)
  ytlopt
end