Class: Furnace::CFG::Graph

Inherits:
Object
  • Object
show all
Includes:
Algorithms
Defined in:
lib/furnace/cfg/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Algorithms

#compute_generic_domination, #dominators, #eliminate_unreachable!, #identify_loops, #merge_redundant!, #postdominators

Constructor Details

#initializeGraph

Returns a new instance of Graph.



8
9
10
11
12
13
# File 'lib/furnace/cfg/graph.rb', line 8

def initialize
  @nodes = Set.new

  @source_map = nil
  @label_map  = {}
end

Instance Attribute Details

#entryObject

Returns the value of attribute entry.



6
7
8
# File 'lib/furnace/cfg/graph.rb', line 6

def entry
  @entry
end

#exitObject

Returns the value of attribute exit.



6
7
8
# File 'lib/furnace/cfg/graph.rb', line 6

def exit
  @exit
end

#nodesObject (readonly)

Returns the value of attribute nodes.



5
6
7
# File 'lib/furnace/cfg/graph.rb', line 5

def nodes
  @nodes
end

Instance Method Details

#find_node(label) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/furnace/cfg/graph.rb', line 15

def find_node(label)
  if node = @label_map[label]
    node
  elsif node = @nodes.find { |n| n.label == label }
    @label_map[label] = node
    node
  else
    raise "Cannot find CFG node #{label}"
  end
end

#flushObject



55
56
57
58
59
60
# File 'lib/furnace/cfg/graph.rb', line 55

def flush
  @source_map = nil
  @label_map.clear

  super if defined?(super)
end

#sources_for(node, find_exceptions = false) ⇒ Object



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
# File 'lib/furnace/cfg/graph.rb', line 26

def sources_for(node, find_exceptions=false)
  unless @source_map
    @source_map = Hash.new { |h, k| h[k] = [] }
    @exception_source_map = Hash.new { |h, k| h[k] = [] }

    @nodes.each do |node|
      node.targets.each do |target|
        @source_map[target] << node
      end

      @exception_source_map[node.exception] << node
    end

    @source_map.each do |node, sources|
      sources.freeze
    end

    @exception_source_map.each do |node, sources|
      sources.freeze
    end
  end

  if find_exceptions
    @exception_source_map[node]
  else
    @source_map[node]
  end
end

#to_graphvizObject



62
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
# File 'lib/furnace/cfg/graph.rb', line 62

def to_graphviz
  Furnace::Graphviz.new do |graph|
    @nodes.each do |node|
      if node.label == nil
        contents = "<exit>"
      else
        contents = "<#{node.label.inspect}>"
      end

      if node..any?
        contents << "\n#{node..inspect}"
      end

      if node.insns.any?
        contents << "\n#{node.insns.map(&:inspect).join("\n")}"
      end

      options = {}
      if @entry == node
        options.merge! color: 'green'
      elsif @exit == node
        options.merge! color: 'red'
      end

      graph.node node.label, contents, options

      node.target_labels.each_with_index do |label, idx|
        graph.edge node.label, label, "#{idx}"
      end

      if node.exception_label
        graph.edge node.label, node.exception_label, "", color: 'orange'
      end
    end
  end
end