class Crokus::CFGCleaner

Public Instance Methods

clean(cfg) click to toggle source
# File lib/crokus/cfg_cleaner.rb, line 5
def clean cfg
  puts " "*5+"|--[+] cleaning cfg '#{cfg.name}'" unless $options[:mute]
  @cfg=cfg
  @visited=[]
  @new_succs={}
  clean_rec cfg.starter
  update
  rename
  cfg
end

Private Instance Methods

clean_rec(bb) click to toggle source
# File lib/crokus/cfg_cleaner.rb, line 18
def clean_rec bb
  @visited << bb
  @new_succs[bb]=[]
  bb.succs.each_with_index do |succ,idx|
    cand=find_next_rec(succ)
    if cand
      #puts "  |--> #{bb.label} next #{idx} is #{cand.label}"
      @new_succs[bb] << cand
      unless @visited.include? cand
        clean_rec cand
      end
    end
  end
end
find_next_rec(bb) click to toggle source
# File lib/crokus/cfg_cleaner.rb, line 33
def find_next_rec bb
  if bb.size>0
    return bb
  else
    if bb.succs.any?
      @cfg.bbs.delete(bb)
      return find_next_rec(bb.succs.first)
    else
      return bb #ending
    end
  end
end
rename() click to toggle source
# File lib/crokus/cfg_cleaner.rb, line 56
def rename
  @cfg.bbs.each_with_index do |bb,idx|
    bb.id="L#{idx}"
  end
end
update() click to toggle source
# File lib/crokus/cfg_cleaner.rb, line 46
def update
  @cfg.bbs.each{|bb| bb.succs=@new_succs[bb]}
  @cfg.bbs.each{|bb|
    if (ite=bb.stmts.last).is_a? ITE
      ite.trueBranch=bb.succs.first
      ite.falseBranch=bb.succs.last
    end
  }
end