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

Commit 28c5a4a

Browse files
committed
Various formatting for CFG and DFG
1 parent 7d1cf1c commit 28c5a4a

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

lib/syntax_tree/yarv/control_flow_graph.rb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ def disasm
3838

3939
blocks.each do |block|
4040
fmt.output.puts(block.id)
41-
fmt.with_prefix(" ") do
41+
fmt.with_prefix(" ") do |prefix|
4242
unless block.incoming_blocks.empty?
43-
from = block.incoming_blocks.map(&:id).join(", ")
44-
fmt.output.puts("#{fmt.current_prefix}== from: #{from}")
43+
from = block.incoming_blocks.map(&:id)
44+
fmt.output.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("#{fmt.current_prefix}== to: #{to.join(", ")}")
51+
fmt.output.puts("#{prefix}== to: #{to.join(", ")}")
5252
end
5353
end
5454

@@ -142,14 +142,19 @@ def build_basic_blocks
142142

143143
length = 0
144144
blocks =
145-
iseq.insns.grep(Instruction).slice_after do |insn|
146-
length += insn.length
147-
block_starts.include?(length)
148-
end
145+
iseq
146+
.insns
147+
.grep(Instruction)
148+
.slice_after do |insn|
149+
length += insn.length
150+
block_starts.include?(length)
151+
end
149152

150-
block_starts.zip(blocks).to_h do |block_start, block_insns|
151-
[block_start, BasicBlock.new(block_start, block_insns)]
152-
end
153+
block_starts
154+
.zip(blocks)
155+
.to_h do |block_start, block_insns|
156+
[block_start, BasicBlock.new(block_start, block_insns)]
157+
end
153158
end
154159

155160
# Connect the blocks by letting them know which blocks are incoming and
@@ -162,7 +167,8 @@ def connect_basic_blocks(blocks)
162167
block.outgoing_blocks << blocks.fetch(labels[branch_target])
163168
end
164169

165-
if (insn.branch_targets.empty? && !insn.leaves?) || insn.falls_through?
170+
if (insn.branch_targets.empty? && !insn.leaves?) ||
171+
insn.falls_through?
166172
fall_through_start = block_start + block.insns.sum(&:length)
167173
block.outgoing_blocks << blocks.fetch(fall_through_start)
168174
end

lib/syntax_tree/yarv/data_flow_graph.rb

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,39 @@ def disasm
3232

3333
cfg.blocks.each do |block|
3434
fmt.output.puts(block.id)
35-
fmt.with_prefix(" ") do
35+
fmt.with_prefix(" ") do |prefix|
3636
unless block.incoming_blocks.empty?
37-
from = block.incoming_blocks.map(&:id).join(", ")
38-
fmt.output.puts("#{fmt.current_prefix}== from: #{from}")
37+
from = block.incoming_blocks.map(&:id)
38+
fmt.output.puts("#{prefix}== from: #{from.join(", ")}")
3939
end
4040

4141
block_flow = block_flows.fetch(block.id)
4242
unless block_flow.in.empty?
43-
fmt.output.puts("#{fmt.current_prefix}== in: #{block_flow.in.join(", ")}")
43+
fmt.output.puts("#{prefix}== in: #{block_flow.in.join(", ")}")
4444
end
4545

46-
fmt.format_insns!(block.insns, block.block_start) do |insn, length|
46+
fmt.format_insns!(block.insns, block.block_start) do |_, length|
4747
insn_flow = insn_flows[length]
4848
next if insn_flow.in.empty? && insn_flow.out.empty?
49-
49+
5050
fmt.output.print(" # ")
5151
unless insn_flow.in.empty?
5252
fmt.output.print("in: #{insn_flow.in.join(", ")}")
5353
fmt.output.print("; ") unless insn_flow.out.empty?
5454
end
55-
55+
5656
unless insn_flow.out.empty?
5757
fmt.output.print("out: #{insn_flow.out.join(", ")}")
5858
end
5959
end
6060

6161
to = block.outgoing_blocks.map(&:id)
6262
to << "leaves" if block.insns.last.leaves?
63-
fmt.output.puts("#{fmt.current_prefix}== to: #{to.join(", ")}")
63+
fmt.output.puts("#{prefix}== to: #{to.join(", ")}")
6464

6565
unless block_flow.out.empty?
66-
fmt.output.puts("#{fmt.current_prefix}== out: #{block_flow.out.join(", ")}")
67-
end
66+
fmt.output.puts("#{prefix}== out: #{block_flow.out.join(", ")}")
67+
end
6868
end
6969
end
7070

@@ -104,23 +104,20 @@ def self.compile(cfg)
104104
# This class is responsible for creating a data flow graph from the given
105105
# control flow graph.
106106
class Compiler
107-
attr_reader :cfg, :insn_flows, :block_flows
107+
# This is the control flow graph that is being compiled.
108+
attr_reader :cfg
108109

109-
def initialize(cfg)
110-
@cfg = cfg
110+
# This data structure will hold the data flow between instructions
111+
# within individual basic blocks.
112+
attr_reader :insn_flows
111113

112-
# This data structure will hold the data flow between instructions
113-
# within individual basic blocks.
114-
@insn_flows = {}
115-
cfg.insns.each_key do |length|
116-
@insn_flows[length] = DataFlow.new
117-
end
114+
# This data structure will hold the data flow between basic blocks.
115+
attr_reader :block_flows
118116

119-
# This data structure will hold the data flow between basic blocks.
120-
@block_flows = {}
121-
cfg.blocks.each do |block|
122-
@block_flows[block.id] = DataFlow.new
123-
end
117+
def initialize(cfg)
118+
@cfg = cfg
119+
@insn_flows = cfg.insns.to_h { |length, _| [length, DataFlow.new] }
120+
@block_flows = cfg.blocks.to_h { |block| [block.id, DataFlow.new] }
124121
end
125122

126123
def compile

lib/syntax_tree/yarv/disassembler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def with_prefix(value)
161161

162162
begin
163163
@current_prefix = value
164-
yield
164+
yield value
165165
ensure
166166
@current_prefix = previous
167167
end

lib/syntax_tree/yarv/instruction_sequence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def disasm
277277
end
278278

279279
def inspect
280-
"#<ISeq:#{name}@<compiled>:1 (#{line},#{0})-(#{line},#{0})>"
280+
"#<ISeq:#{name}@<compiled>:1 (#{line},0)-(#{line},0)>"
281281
end
282282

283283
# This method converts our linked list of instructions into a final array

0 commit comments

Comments
 (0)