Class: SyntaxTree::YARV::DataFlowGraph
- Inherits:
-
Object
- Object
- SyntaxTree::YARV::DataFlowGraph
- Defined in:
- lib/syntax_tree/yarv/data_flow_graph.rb
Overview
Constructs a data-flow-graph of a YARV instruction sequence, via a control-flow-graph. Data flow is discovered locally and then globally. The graph only considers data flow through the stack - local variables and objects are considered fully escaped in this analysis.
You can use this class by calling the ::compile method and passing it a control flow graph. It will return a data flow graph object.
iseq = RubyVM::InstructionSequence.compile("1 + 2")
iseq = SyntaxTree::YARV::InstructionSequence.from(iseq.to_a)
cfg = SyntaxTree::YARV::ControlFlowGraph.compile(iseq)
dfg = SyntaxTree::YARV::DataFlowGraph.compile(cfg)
Defined Under Namespace
Classes: BlockArgument, Compiler, DataFlow, LocalArgument
Instance Attribute Summary collapse
-
#block_flows ⇒ Object
readonly
Returns the value of attribute block_flows.
-
#cfg ⇒ Object
readonly
Returns the value of attribute cfg.
-
#insn_flows ⇒ Object
readonly
Returns the value of attribute insn_flows.
Class Method Summary collapse
Instance Method Summary collapse
- #blocks ⇒ Object
- #disasm ⇒ Object
-
#initialize(cfg, insn_flows, block_flows) ⇒ DataFlowGraph
constructor
A new instance of DataFlowGraph.
- #to_mermaid ⇒ Object
- #to_son ⇒ Object
-
#verify ⇒ Object
Verify that we constructed the data flow graph correctly.
Constructor Details
#initialize(cfg, insn_flows, block_flows) ⇒ DataFlowGraph
Returns a new instance of DataFlowGraph.
68 69 70 71 72 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 68 def initialize(cfg, insn_flows, block_flows) @cfg = cfg @insn_flows = insn_flows @block_flows = block_flows end |
Instance Attribute Details
#block_flows ⇒ Object (readonly)
Returns the value of attribute block_flows.
66 67 68 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 66 def block_flows @block_flows end |
#cfg ⇒ Object (readonly)
Returns the value of attribute cfg.
66 67 68 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 66 def cfg @cfg end |
#insn_flows ⇒ Object (readonly)
Returns the value of attribute insn_flows.
66 67 68 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 66 def insn_flows @insn_flows end |
Class Method Details
Instance Method Details
#blocks ⇒ Object
74 75 76 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 74 def blocks cfg.blocks end |
#disasm ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 78 def disasm fmt = Disassembler.new(cfg.iseq) fmt.puts("== dfg: #{cfg.iseq.inspect}") blocks.each do |block| fmt.puts(block.id) fmt.with_prefix(" ") do |prefix| unless block.incoming_blocks.empty? from = block.incoming_blocks.map(&:id) fmt.puts("#{prefix}== from: #{from.join(", ")}") end block_flow = block_flows.fetch(block.id) unless block_flow.in.empty? fmt.puts("#{prefix}== in: #{block_flow.in.join(", ")}") end fmt.format_insns!(block.insns, block.block_start) do |_, length| insn_flow = insn_flows[length] next if insn_flow.in.empty? && insn_flow.out.empty? fmt.print(" # ") unless insn_flow.in.empty? fmt.print("in: #{insn_flow.in.join(", ")}") fmt.print("; ") unless insn_flow.out.empty? end unless insn_flow.out.empty? fmt.print("out: #{insn_flow.out.join(", ")}") end end to = block.outgoing_blocks.map(&:id) to << "leaves" if block.insns.last.leaves? fmt.puts("#{prefix}== to: #{to.join(", ")}") unless block_flow.out.empty? fmt.puts("#{prefix}== out: #{block_flow.out.join(", ")}") end end end fmt.string end |
#to_mermaid ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 127 def to_mermaid Mermaid.flowchart do |flowchart| disasm = Disassembler::Squished.new blocks.each do |block| block_flow = block_flows.fetch(block.id) graph_name = if block_flow.in.any? "#{block.id} #{block_flows[block.id].in.join(", ")}" else block.id end flowchart.subgraph(graph_name) do previous = nil block.each_with_length do |insn, length| node = flowchart.node( "node_#{length}", "%04d %s" % [length, insn.disasm(disasm)], shape: :rounded ) flowchart.link(previous, node, color: :red) if previous insn_flows[length].in.each do |input| if input.is_a?(LocalArgument) from = flowchart.fetch("node_#{input.length}") flowchart.link(from, node, color: :green) end end previous = node end end end blocks.each do |block| block.outgoing_blocks.each do |outgoing| offset = block.block_start + block.insns.sum(&:length) - block.insns.last.length from = flowchart.fetch("node_#{offset}") to = flowchart.fetch("node_#{outgoing.block_start}") flowchart.link(from, to, color: :red) end end end end |
#to_son ⇒ Object
123 124 125 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 123 def to_son SeaOfNodes.compile(self) end |
#verify ⇒ Object
Verify that we constructed the data flow graph correctly.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/syntax_tree/yarv/data_flow_graph.rb', line 179 def verify # Check that the first block has no arguments. raise unless block_flows.fetch(blocks.first.id).in.empty? # Check all control flow edges between blocks pass the right number of # arguments. blocks.each do |block| block_flow = block_flows.fetch(block.id) if block.outgoing_blocks.empty? # With no outgoing blocks, there should be no output arguments. raise unless block_flow.out.empty? else # Check with outgoing blocks... block.outgoing_blocks.each do |outgoing_block| outgoing_flow = block_flows.fetch(outgoing_block.id) # The block should have as many output arguments as the # outgoing block has input arguments. raise unless block_flow.out.size == outgoing_flow.in.size end end end end |