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

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

Class Method Summary collapse

Class Method Details

.dump_node(tnode, fn) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ytl.rb', line 84

def self.dump_node(tnode, fn)
  if defined? yield then
    yield
  end
  File.open(fn, "w") do |fp|
    ClassClassWrapper.instance_tab.keys.each do |klass|
      if !klass.is_a?(ClassClassWrapper) then
        fp.print "class #{klass.name}; end\n"
      end
    end
    
    fp.print "Marshal.load(<<'EOS')\n"
    fp.print Marshal.dump(tnode)
    fp.print "\n"
    fp.print "EOS\n"
  end
end

.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



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

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_context.current_file_name = fn
    tr.translate(tr_context)
  end
  
  tnode = nil
  case File.extname(ARGV[0])
  when ".ytl"
    File.open(ARGV[0]) do |fp|
      tnode = eval(fp.read, TOPLEVEL_BINDING)
    end
    tnode.update_after_restore
    if tnode.code_space then
      tnode.code_space.call(tnode.code_space.base_address)
      return
    end
    
  when ".rb"
    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
    iseqs = [prelude_iseq, iseq]
    
    tr = VM::YARVTranslatorCRubyObject.new(iseqs)
    tr_context.current_file_name = ARGV[0]
    tnode = tr.translate(tr_context)
  end
  
  ci_context = VM::CollectInfoContext.new(tnode)
  ci_context.options = options
  tnode.collect_info(ci_context)
  
  if fn = options[:write_node_before_ti] then
    dump_node(tnode, fn)
  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)
  ti_context.options = options
  begin
    tnode.collect_candidate_type(ti_context, arg, sig)
  end until ti_context.convergent
  ti_context = tnode.collect_candidate_type(ti_context, arg, sig)
  
  if fn = options[:write_node_after_ti] then
    dump_node(tnode, fn)
  end
  
  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_code] then
    dump_node(tnode, fn) {
      tnode.code_store_hook
    }
  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
  if !options[:compile_only] then
    tcs.call(tcs.base_address)
  end
end

.parse_opt(argv) ⇒ Object



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

def self.parse_opt(argv)
  ytlopt = {}
  ytlopt[:execute_before_compile] = []
  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('-o FILE', '--write-code =FILE', 
         'Write generating naitive code and node objects') 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.on('-c', '--compile-only', 
         'Stop when compile finished (not execute compiled code)') do |f|
    ytlopt[:compile_only] = f
  end
  
  opt.on('--compile-array-as-unboxed', 
         'Compile Array as unboxed if nesseary(not excape, not use special methods)') do |f|
    ytlopt[:compile_array_as_uboxed] = f
  end
  
  opt.parse!(argv)
  ytlopt
end

.prelude_iseqObject



75
76
77
78
79
80
81
82
# File 'lib/ytl.rb', line 75

def self.prelude_iseq
  prelude = File.join(File.dirname(__FILE__), "..", "runtime", "prelude.rb")
  rf = File.read(prelude)
  prog = eval(rf)
  is = RubyVM::InstructionSequence.compile(prog, ARGV[0], 
                                           "", 0, ISEQ_OPTS).to_a
  VMLib::InstSeqTree.new(nil, is)
end

.reduced_main(prog, options) ⇒ Object



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

def self.reduced_main(prog, options)
  tr_context = VM::YARVContext.new
  
  import_ruby_object(tr_context)
  
  tnode = nil
  is = RubyVM::InstructionSequence.compile(prog, ARGV[0], 
                                           "", 0, ISEQ_OPTS).to_a
  iseq = VMLib::InstSeqTree.new(nil, is)
  iseqs = [prelude_iseq, iseq]
  
  tr = VM::YARVTranslatorCRubyObject.new(iseqs)
  tnode = tr.translate(tr_context)
  
  ci_context = VM::CollectInfoContext.new(tnode)
  ci_context.options = options
  tnode.collect_info(ci_context)
  
  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)
  ti_context.options = options
  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
  
  tcs = tnode.code_space
  tcs.call(tcs.base_address)
end