@@ -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
0 commit comments