Skip to content

Commit 92cbfca

Browse files
committed
Provide BasicBlock.each_with_index
1 parent b8dc901 commit 92cbfca

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

lib/syntax_tree/yarv/control_flow_graph.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ def initialize(block_start, insns)
101101
@successors = []
102102
end
103103

104+
# Yield each instruction in this basic block along with its index from
105+
# the original instruction sequence.
106+
def each_with_index(&block)
107+
insns.each.with_index(block_start, &block)
108+
end
109+
104110
# This method is used to verify that the basic block is well formed. It
105111
# checks that the only instruction in this basic block that branches is
106112
# the last instruction.
@@ -171,7 +177,6 @@ def find_basic_block_starts
171177
# block. They are keyed by the index of their first instruction.
172178
def build_basic_blocks
173179
block_starts = find_basic_block_starts
174-
blocks = {}
175180

176181
block_starts.each_with_index.to_h do |block_start, block_index|
177182
block_end = (block_starts[(block_index + 1)..] + [insns.length]).min

lib/syntax_tree/yarv/data_flow_graph.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ def self.compile(cfg)
3838
# blocks.
3939
block_flows = {}
4040
cfg.blocks.each do |block|
41-
block_flows[block.block_start] = DataFlow.new
41+
block_flows[block.id] = DataFlow.new
4242
end
4343

4444
# Now, discover the data flow within each basic block. Using an abstract
4545
# stack, connect from consumers of data to the producers of that data.
4646
cfg.blocks.each do |block|
47-
block_flow = block_flows.fetch(block.block_start)
47+
block_flow = block_flows.fetch(block.id)
4848

4949
stack = []
5050
stack_initial_depth = 0
5151

5252
# Go through each instruction in the block...
53-
block.insns.each.with_index(block.block_start) do |insn, index|
53+
block.each_with_index do |insn, index|
5454
insn_flow = insn_flows[index]
5555

5656
# How many values will be missing from the local stack to run this
@@ -107,9 +107,9 @@ def self.compile(cfg)
107107
stack = [*cfg.blocks]
108108
until stack.empty?
109109
succ = stack.pop
110-
succ_flow = block_flows.fetch(succ.block_start)
110+
succ_flow = block_flows.fetch(succ.id)
111111
succ.predecessors.each do |pred|
112-
pred_flow = block_flows.fetch(pred.block_start)
112+
pred_flow = block_flows.fetch(pred.id)
113113

114114
# Does a predecessor block have fewer outputs than the successor
115115
# has inputs?
@@ -132,20 +132,20 @@ def self.compile(cfg)
132132

133133
# Verify that we constructed the data flow graph correctly. Check that
134134
# the first block has no arguments.
135-
raise unless block_flows.fetch(cfg.blocks.first.block_start).in.empty?
135+
raise unless block_flows.fetch(cfg.blocks.first.id).in.empty?
136136

137137
# Check all control flow edges between blocks pass the right number of
138138
# arguments.
139139
cfg.blocks.each do |pred|
140-
pred_flow = block_flows.fetch(pred.block_start)
140+
pred_flow = block_flows.fetch(pred.id)
141141

142142
if pred.successors.empty?
143143
# With no successors, there should be no output arguments.
144144
raise unless pred_flow.out.empty?
145145
else
146146
# Check with successor...
147147
pred.successors.each do |succ|
148-
succ_flow = block_flows.fetch(succ.block_start)
148+
succ_flow = block_flows.fetch(succ.id)
149149

150150
# The predecessor should have as many output arguments as the
151151
# success has input arguments.
@@ -170,12 +170,12 @@ def disasm
170170
end
171171
output.puts
172172

173-
block_flow = block_flows.fetch(block.block_start)
173+
block_flow = block_flows.fetch(block.id)
174174
unless block_flow.in.empty?
175175
output.puts " # in: #{block_flow.in.join(", ")}"
176176
end
177177

178-
block.insns.each.with_index(block.block_start) do |insn, index|
178+
block.each_with_index do |insn, index|
179179
output.print(" ")
180180
output.print(insn.disasm(fmt))
181181

0 commit comments

Comments
 (0)