Class: RegularExpression::CFG::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/regular_expression/cfg.rb

Overview

A graph is a set of EBBs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blocks, exit_map) ⇒ Graph

Returns a new instance of Graph.



118
119
120
121
# File 'lib/regular_expression/cfg.rb', line 118

def initialize(blocks, exit_map)
  @blocks = blocks
  @exit_map = exit_map
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



116
117
118
# File 'lib/regular_expression/cfg.rb', line 116

def blocks
  @blocks
end

#exit_mapObject (readonly)

Returns the value of attribute exit_map.



116
117
118
# File 'lib/regular_expression/cfg.rb', line 116

def exit_map
  @exit_map
end

Instance Method Details

#dumpObject



127
128
129
130
131
# File 'lib/regular_expression/cfg.rb', line 127

def dump
  output = StringIO.new
  blocks.each { |block| block.dump(exit_map, io: output) }
  output.string
end

#startObject



123
124
125
# File 'lib/regular_expression/cfg.rb', line 123

def start
  blocks.first
end

#to_dot(graph) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/regular_expression/cfg.rb', line 133

def to_dot(graph)
  nodes = {}

  blocks.each do |block|
    label = []

    label.push("#{block.name}:")
    block.insns.each { |insn| label.push("  #{insn}") }

    nodes[block] = graph.add_node(block.object_id, label: label.join($/), labeljust: "l", shape: "box")
  end

  blocks.each do |block|
    successors = block.exits.map { |exit| nodes[exit_map[exit]] }.uniq
    successors.each do |successor|
      nodes[block].connect(successor)
    end
  end
end