Class: YTLJit::VM::Node::BaseNode

Inherits:
Object
  • Object
show all
Includes:
AbsArch, Inspect, TypeListWithSignature
Defined in:
lib/ytljit/vm.rb

Constant Summary collapse

ESCAPE_LEVEL =
{
  nil => -1, 
  :not_export => 5, 
  :local_export => 6,
  :global_export => 10
}

Constants included from AbsArch

AbsArch::AL, AbsArch::BL, AbsArch::CL, AbsArch::DL, AbsArch::FUNC_ARG, AbsArch::FUNC_ARG_YTL, AbsArch::FUNC_FLOAT_ARG, AbsArch::FUNC_FLOAT_ARG_YTL, AbsArch::INDIRECT_BPR, AbsArch::INDIRECT_RETR, AbsArch::INDIRECT_SPR, AbsArch::INDIRECT_TMPR, AbsArch::INDIRECT_TMPR2, AbsArch::INDIRECT_TMPR3

Constants included from SSE

SSE::XMM0, SSE::XMM1, SSE::XMM2, SSE::XMM3, SSE::XMM4, SSE::XMM5, SSE::XMM6, SSE::XMM7

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TypeListWithSignature

#add_type, #set_type_list, #type_list, #type_list_initvar

Methods included from Inspect

#inspect_by_graph

Constructor Details

#initialize(parent) ⇒ BaseNode

Returns a new instance of BaseNode.



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
# File 'lib/ytljit/vm.rb', line 154

def initialize(parent)
  cs = CodeSpace.new
  asm = Assembler.new(cs)
  asm.with_retry do
    asm.mov(TMPR, 4)
    asm.ret
  end

  # iv for structure of VM
  @parent = parent
  @code_space = nil
  @id = [1]
  if @parent then
    @id = @parent.id.dup
    @id[-1] = @id[-1] + 1
  else
    @id = [1]
  end

  # iv for type inference
  @type = nil
  @type_list = type_list_initvar
  @element_node_list = []
  @my_element_node = nil
  @type_inference_proc = cs
  @decided_signature = nil
  @is_escape = nil

  @ti_observer = {}
  @ti_observee = []

  @debug_info = nil
end

Instance Attribute Details

#code_spaceObject (readonly)

Returns the value of attribute code_space.



189
190
191
# File 'lib/ytljit/vm.rb', line 189

def code_space
  @code_space
end

#debug_infoObject

Returns the value of attribute debug_info.



199
200
201
# File 'lib/ytljit/vm.rb', line 199

def debug_info
  @debug_info
end

#element_node_listObject

Returns the value of attribute element_node_list.



193
194
195
# File 'lib/ytljit/vm.rb', line 193

def element_node_list
  @element_node_list
end

#idObject (readonly)

Returns the value of attribute id.



190
191
192
# File 'lib/ytljit/vm.rb', line 190

def id
  @id
end

#is_escapeObject

Returns the value of attribute is_escape.



194
195
196
# File 'lib/ytljit/vm.rb', line 194

def is_escape
  @is_escape
end

#parentObject

Returns the value of attribute parent.



188
189
190
# File 'lib/ytljit/vm.rb', line 188

def parent
  @parent
end

#ti_observeeObject (readonly)

Returns the value of attribute ti_observee.



197
198
199
# File 'lib/ytljit/vm.rb', line 197

def ti_observee
  @ti_observee
end

#ti_observerObject (readonly)

Returns the value of attribute ti_observer.



196
197
198
# File 'lib/ytljit/vm.rb', line 196

def ti_observer
  @ti_observer
end

#typeObject

Returns the value of attribute type.



192
193
194
# File 'lib/ytljit/vm.rb', line 192

def type
  @type
end

Instance Method Details

#add_element_node(curslf, encsig, enode, index, context, backp = false) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/ytljit/vm.rb', line 427

def add_element_node(curslf, encsig, enode, index, context, 
                     backp = false)
  newele = [curslf, encsig, enode, index]

  # search entry whose index( [3]) is nil
  nlentry = nil
  @element_node_list.each do |e| 
    if e[1] == encsig and
        e[0] == curslf and
        e[3] == nil then
      nlentry = e
      break
    end
  end

  # entry nil index of new self to non-empty table 
  if nlentry == nil then
    nlnode = BaseNode.new(nil)
    nlentry = [curslf, encsig, nlnode, nil]
    @element_node_list.push nlentry
  end

  if !@element_node_list.include?(newele) then
    @element_node_list.push newele
    nelesig = nlentry[1]
    nelenode = nlentry[2]
    if nelenode != enode then
      same_type(nelenode, enode, nelesig, encsig, context)
    end
    if index != nil then
      @element_node_list.each do |tmpslf, tmpsig, tmpnode, tmpindex|
        if tmpslf == curslf and
            tmpindex == index and
            tmpnode != enode then
          same_type(tmpnode, enode, tmpsig, encsig, context)
        end
      end
    end
    
    if !backp then
      ti_changed
    end
    #            context.convergent = false
  end
end

#add_element_node_backward(args) ⇒ Object



379
380
381
382
383
384
385
386
387
# File 'lib/ytljit/vm.rb', line 379

def add_element_node_backward(args)
  add_element_node(*args)
  visitnode = {}
  visitnode[self] = true
  @ti_observee.each do |rec|
    rec.add_element_node(*args, true)
#            rec.add_element_node_backward_aux(args, visitnode)
  end
end

#add_element_node_backward_aux(args, visitnode) ⇒ Object



389
390
391
392
393
394
395
396
397
398
399
# File 'lib/ytljit/vm.rb', line 389

def add_element_node_backward_aux(args, visitnode)
  if visitnode[self] then
    return
  end

  add_element_node(*args, true)
  visitnode[self] = true
  @ti_observee.each do |rec|
    rec.add_element_node_backward_aux(args, visitnode)
  end
end

#collect_candidate_type(context) ⇒ Object



473
474
475
476
# File 'lib/ytljit/vm.rb', line 473

def collect_candidate_type(context)
  raise "You must define collect_candidate_type per node"
  context
end

#collect_info(context) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/ytljit/vm.rb', line 201

def collect_info(context)
  if is_a?(HaveChildlenMixin) then
    traverse_childlen {|rec|
      context = rec.collect_info(context)
    }
  end

  context
end

#compile(context) ⇒ Object



628
629
630
631
# File 'lib/ytljit/vm.rb', line 628

def compile(context)
  @code_space = context.code_space
  context
end

#decide_type(sig) ⇒ Object



601
602
603
604
605
606
607
608
609
# File 'lib/ytljit/vm.rb', line 601

def decide_type(sig)
  decide_type_once(sig)

  if is_a?(HaveChildlenMixin) then
    traverse_childlen {|rec|
      rec.decide_type(sig)
    }
  end
end

#decide_type_core(tlist, cursig, local_cache = {}) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/ytljit/vm.rb', line 478

def decide_type_core(tlist, cursig, local_cache = {})
  tlist = tlist.select {|e| e.class != RubyType::DefaultType0 }

  # This is for sitration of same class and differenc element type.
  # Last element must be local type not propageted type
  if tlist.size > 1 and tlist.all? {|e| 
      e.ruby_type == tlist[0].ruby_type and
      e.boxed == tlist[0].boxed
    } then
    return tlist.last
  end

  case tlist.size
  when 0
    RubyType::DefaultType0.new # .to_unbox
    
  when 1
    tlist[0]

  when 2
    if tlist[0].ruby_type == tlist[1].ruby_type then
      tlist[0]
      
    elsif tlist[0].ruby_type == NilClass then
      # nil-able type
      tlist[1].to_box

    elsif tlist[1].ruby_type == NilClass then
      # nil-able type
      tlist[0].to_box

    elsif tlist[0].ruby_type == Float and
        tlist[1].ruby_type == Fixnum then
      tlist[0]

    elsif tlist[0].ruby_type == Fixnum and
        tlist[1].ruby_type == Float then
      tlist[1]

    elsif tlist[0].ruby_type == TrueClass and
        tlist[1].ruby_type == FalseClass then
      tlist[0]

    elsif tlist[0].ruby_type == FalseClass and
        tlist[1].ruby_type == TrueClass then
      tlist[1]

    else 
      RubyType::DefaultType0.new
    end

  when 3
    tmptlist = tlist.dup
    tmptlist.delete_if {|ele| ele.ruby_type == NilClass}
    if tmptlist.size == 2 and 
        tmptlist[0].ruby_type == tmptlist[1].ruby_type then
      tmptlist[0]

    elsif tmptlist[0].ruby_type == tmptlist[1].ruby_type and
          tmptlist[0].ruby_type == tmptlist[2].ruby_type  then
      tmptlist[1]

    else
      RubyType::DefaultType0.new
    end

  else
    RubyType::DefaultType0.new
  end
end

#decide_type_once(cursig, local_cache = {}) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# File 'lib/ytljit/vm.rb', line 549

def decide_type_once(cursig, local_cache = {})
  if local_cache[self] then
    return local_cache[self] 
  end

=begin
  if @decided_signature and @decided_signature != sig then
    p cursig
    p @decided_signature
    p debug_info
    p self.class
    p caller[0]
    @decided_signature = cursig
  end
=end

  if  # @decided_signature != cursig or
      @type.equal?(nil) or 
      @type.is_a?(RubyType::DefaultType0) then
    tlist = type_list(cursig).flatten.reverse.uniq
    @decided_signature = cursig
    @type = decide_type_core(tlist, cursig, local_cache)
  end

  if @type.have_element? and 
      (@type.element_type == nil or
       @type.element_type == {}) then
    local_cache[self] = @type
    etype2 = {}
    etype = nil
    @element_node_list.each do |ele|
      node = ele[2]
      sig = ele[1]
      slf = ele[0]

      if @type == slf then
        # node.type = nil
        tt = node.decide_type_once(sig, local_cache)
        etype2[ele[3]] ||= []
        curidx = etype2[ele[3]]
        if tt.ruby_type != Object and !curidx.include?(tt) then
          curidx.push tt
          etype = etype2
        end
      end
    end
    @type.element_type = etype
  end

  @type
end

#gen_type_inference_proc(code) ⇒ Object



625
626
# File 'lib/ytljit/vm.rb', line 625

def gen_type_inference_proc(code)
end

#get_constant_valueObject



633
634
635
# File 'lib/ytljit/vm.rb', line 633

def get_constant_value
  nil
end

#inference_typeObject



620
621
622
623
# File 'lib/ytljit/vm.rb', line 620

def inference_type
  cs = @type_inference_proc
  cs.call(cs.var_base_address)
end

#marge_element_node(dst, src, context) ⇒ Object



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
# File 'lib/ytljit/vm.rb', line 269

def marge_element_node(dst, src, context)
  res = dst
  src.each do |sele|
    exist_same_type = false
#=begin
    res.each do |rele|
      if rele[3] == sele[3] and
          rele[1] == sele[1] and
          rele[2] != sele[2] then
        # Add entry for old element type version of self
        rtype = rele[2].decide_type_once(rele[1])
        if rtype == nil or 
            rtype.ruby_type == NilClass then
          nele = [rele[0], sele[1], sele[2], sele[3]]
          if !res.include?(nele) then
            res.push nele
            exist_same_type = true
          end
        end
      end
    end
#=end
    
    if !exist_same_type and !res.include?(sele) then
      res.push sele
    end
  end
  
  res
end

#marge_type(dst, src) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/ytljit/vm.rb', line 300

def marge_type(dst, src)
  res = dst
  src.each do |sele|
    org = nil
    res.delete_if {|e| 
      if e.ruby_type == sele.ruby_type and
          e.boxed == sele.boxed then
        org = e
      else
        nil
      end
    }

    if org and org.have_element? and org.element_type then
      res.push org
    else
      res.push sele
    end
  end
  
  res
end

#same_type(dst, src, dsig, ssig, context) ⇒ Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/ytljit/vm.rb', line 358

def same_type(dst, src, dsig, ssig, context)
=begin
  print "#{src.class} -> #{dst.class} \n"
  if dst.is_a?(LocalVarNode) then
    print "#{dst.name} \n"
  end
  if dst.is_a?(LiteralNode) then
    print "#{dst.value.inspect} \n"
  end
  if dst.is_a?(SendNode) then
    print "#{dst.func.name} \n"
  end
=end

  if dst.is_a?(BaseNode) then
    src.ti_add_observer(dst, dsig, ssig, context)
  end

  ti_update(dst, src, dsig, ssig, context)
end

#search_valid_signatureObject



611
612
613
614
615
616
617
618
# File 'lib/ytljit/vm.rb', line 611

def search_valid_signature
  node = @type_list.search_valid_node
  if node then
    node.key
  else
    nil
  end
end

#set_escape_node(value) ⇒ Object



408
409
410
411
412
# File 'lib/ytljit/vm.rb', line 408

def set_escape_node(value)
  if ESCAPE_LEVEL[@is_escape] < ESCAPE_LEVEL[value] then
    @is_escape = value
  end
end

#set_escape_node_backward(value, visitnode = {}) ⇒ Object



414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/ytljit/vm.rb', line 414

def set_escape_node_backward(value, visitnode = {})
  if visitnode[self] then
    return
  end

  set_escape_node(value)

  visitnode[self] = true
  @ti_observee.each do |rec|
    rec.set_escape_node_backward(value, visitnode)
  end
end

#ti_add_observer(dst, dsig, ssig, context) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ytljit/vm.rb', line 211

def ti_add_observer(dst, dsig, ssig, context)
  if @ti_observer[dst] == nil then
    @ti_observer[dst] = []
    dst.ti_observee.push self
  end
  
  if @ti_observer[dst].all? {|edsig, essig, eprc| 
      (edsig != dsig) or (essig != ssig)
    } then
    prc = lambda { send(:ti_update, dst, self, dsig, ssig, context) }
    @ti_observer[dst].push [dsig, ssig, prc]
  end
end

#ti_changedObject



225
226
227
228
229
230
231
232
# File 'lib/ytljit/vm.rb', line 225

def ti_changed
  @ti_observer.keys.each do |rec|
    lst = @ti_observer[rec]
    lst.each do |dsig, ssig, prc|
      prc.call
    end
  end
end


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/ytljit/vm.rb', line 249

def ti_del_link(visitnode = {})
  if visitnode[self] then
    return
  end

  visitnode[self] = true
  @ti_observer.each do |rec, lst|
    delent = []
    lst.each do |ent|
      delent.push ent
        
      rec.ti_del_link(visitnode)
    end

    delent.each do |ent|
      lst.delete(ent)
    end
  end
end

#ti_reset(visitnode = {}) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ytljit/vm.rb', line 234

def ti_reset(visitnode = {})
  if visitnode[self] then
    return
  end

  visitnode[self] = true
  @ti_observer.each do |rec, lst|
    lst.each do |dsig, ssig, prc|
      rec.type_list(dsig)[1] = []

      rec.ti_reset(visitnode)
    end
  end
end

#ti_update(dst, src, dsig, ssig, context) ⇒ Object



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
352
353
354
355
356
# File 'lib/ytljit/vm.rb', line 323

def ti_update(dst, src, dsig, ssig, context)
  dtlistorg = dst.type_list(dsig)
  dtlist = dtlistorg.flatten
  stlist = src.type_list(ssig).flatten
=begin
  print "UPDATE TYPE\n"
  print "#{src.class} #{ssig.inspect} -> #{dst.class} #{dsig.inspect}\n"
  print dtlist.map(&:ruby_type), "\n"
  print stlist.map(&:ruby_type), "\n"
=end
  orgsize = dtlist.size
#          pp "#{dst.class} #{src.class} #{dtlist} #{stlist}"
  newdt = marge_type(dtlistorg[1], stlist)
  dst.set_type_list(dsig, newdt)
  dtsize = dtlistorg[0].size + newdt.size

  if orgsize != dtsize then
    dst.type = nil
    dst.ti_changed
#            context.convergent = false
  end

  dtlist = dst.element_node_list
  stlist = src.element_node_list

  orgsize = dtlist.size
  dst.element_node_list = marge_element_node(dtlist, stlist, context)
  if orgsize != dtlist.size then
    dst.ti_changed
#            context.convergent = false
  end
  
  dst.set_escape_node(src.is_escape)
end