Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 02ec2ad

Browse files
committed
Simplify disassembler API
1 parent 16f1bb8 commit 02ec2ad

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

lib/syntax_tree/yarv/control_flow_graph.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ def initialize(iseq, insns, blocks)
3434

3535
def disasm
3636
fmt = Disassembler.new(iseq)
37-
fmt.output.puts("== cfg: #{iseq.inspect}")
37+
fmt.puts("== cfg: #{iseq.inspect}")
3838

3939
blocks.each do |block|
40-
fmt.output.puts(block.id)
40+
fmt.puts(block.id)
4141
fmt.with_prefix(" ") do |prefix|
4242
unless block.incoming_blocks.empty?
4343
from = block.incoming_blocks.map(&:id)
44-
fmt.output.puts("#{prefix}== from: #{from.join(", ")}")
44+
fmt.puts("#{prefix}== from: #{from.join(", ")}")
4545
end
4646

4747
fmt.format_insns!(block.insns, block.block_start)
4848

4949
to = block.outgoing_blocks.map(&:id)
5050
to << "leaves" if block.insns.last.leaves?
51-
fmt.output.puts("#{prefix}== to: #{to.join(", ")}")
51+
fmt.puts("#{prefix}== to: #{to.join(", ")}")
5252
end
5353
end
5454

lib/syntax_tree/yarv/data_flow_graph.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ module YARV
66
# control-flow-graph. Data flow is discovered locally and then globally. The
77
# graph only considers data flow through the stack - local variables and
88
# objects are considered fully escaped in this analysis.
9+
#
10+
# You can use this class by calling the ::compile method and passing it a
11+
# control flow graph. It will return a data flow graph object.
12+
#
13+
# iseq = RubyVM::InstructionSequence.compile("1 + 2")
14+
# iseq = SyntaxTree::YARV::InstructionSequence.from(iseq.to_a)
15+
# cfg = SyntaxTree::YARV::ControlFlowGraph.compile(iseq)
16+
# dfg = SyntaxTree::YARV::DataFlowGraph.compile(cfg)
17+
#
918
class DataFlowGraph
1019
# This object represents the flow of data between instructions.
1120
class DataFlow
@@ -28,42 +37,42 @@ def initialize(cfg, insn_flows, block_flows)
2837

2938
def disasm
3039
fmt = Disassembler.new(cfg.iseq)
31-
fmt.output.puts("== dfg: #{cfg.iseq.inspect}")
40+
fmt.puts("== dfg: #{cfg.iseq.inspect}")
3241

3342
cfg.blocks.each do |block|
34-
fmt.output.puts(block.id)
43+
fmt.puts(block.id)
3544
fmt.with_prefix(" ") do |prefix|
3645
unless block.incoming_blocks.empty?
3746
from = block.incoming_blocks.map(&:id)
38-
fmt.output.puts("#{prefix}== from: #{from.join(", ")}")
47+
fmt.puts("#{prefix}== from: #{from.join(", ")}")
3948
end
4049

4150
block_flow = block_flows.fetch(block.id)
4251
unless block_flow.in.empty?
43-
fmt.output.puts("#{prefix}== in: #{block_flow.in.join(", ")}")
52+
fmt.puts("#{prefix}== in: #{block_flow.in.join(", ")}")
4453
end
4554

4655
fmt.format_insns!(block.insns, block.block_start) do |_, length|
4756
insn_flow = insn_flows[length]
4857
next if insn_flow.in.empty? && insn_flow.out.empty?
4958

50-
fmt.output.print(" # ")
59+
fmt.print(" # ")
5160
unless insn_flow.in.empty?
52-
fmt.output.print("in: #{insn_flow.in.join(", ")}")
53-
fmt.output.print("; ") unless insn_flow.out.empty?
61+
fmt.print("in: #{insn_flow.in.join(", ")}")
62+
fmt.print("; ") unless insn_flow.out.empty?
5463
end
5564

5665
unless insn_flow.out.empty?
57-
fmt.output.print("out: #{insn_flow.out.join(", ")}")
66+
fmt.print("out: #{insn_flow.out.join(", ")}")
5867
end
5968
end
6069

6170
to = block.outgoing_blocks.map(&:id)
6271
to << "leaves" if block.insns.last.leaves?
63-
fmt.output.puts("#{prefix}== to: #{to.join(", ")}")
72+
fmt.puts("#{prefix}== to: #{to.join(", ")}")
6473

6574
unless block_flow.out.empty?
66-
fmt.output.puts("#{prefix}== out: #{block_flow.out.join(", ")}")
75+
fmt.puts("#{prefix}== out: #{block_flow.out.join(", ")}")
6776
end
6877
end
6978
end

lib/syntax_tree/yarv/disassembler.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ def object(value)
7878
# Entrypoints
7979
########################################################################
8080

81-
def string
82-
output.string
83-
end
84-
8581
def format!
8682
while (@current_iseq = queue.shift)
8783
output << "\n" if output.pos > 0
@@ -133,6 +129,18 @@ def format_insns!(insns, length = 0)
133129
end
134130
end
135131

132+
def print(string)
133+
output.print(string)
134+
end
135+
136+
def puts(string)
137+
output.puts(string)
138+
end
139+
140+
def string
141+
output.string
142+
end
143+
136144
def with_prefix(value)
137145
previous = @current_prefix
138146

0 commit comments

Comments
 (0)