@@ -27,56 +27,48 @@ def initialize(cfg, insn_flows, block_flows)
2727 end
2828
2929 def disasm
30- fmt = Disassembler . new
31- output = StringIO . new
32- output . puts "== dfg #{ cfg . iseq . name } "
30+ fmt = Disassembler . new ( cfg . iseq )
31+ fmt . output . puts ( "== dfg: #{ cfg . iseq . inspect } " )
3332
3433 cfg . blocks . each do |block |
35- output . print ( block . id )
36- unless block . incoming_blocks . empty?
37- srcs = block . incoming_blocks . map ( &:id )
38- output . print ( " # from: #{ srcs . join ( ", " ) } " )
39- end
40- output . puts
41-
42- block_flow = block_flows . fetch ( block . id )
43- unless block_flow . in . empty?
44- output . puts " # in: #{ block_flow . in . join ( ", " ) } "
45- end
46-
47- block . each_with_length do |insn , length |
48- output . print ( " " )
49- output . print ( insn . disasm ( fmt ) )
50-
51- insn_flow = insn_flows [ length ]
52- if insn_flow . in . empty? && insn_flow . out . empty?
53- output . puts
54- next
34+ fmt . output . puts ( block . id )
35+ fmt . with_prefix ( " " ) do
36+ unless block . incoming_blocks . empty?
37+ from = block . incoming_blocks . map ( &:id ) . join ( ", " )
38+ fmt . output . puts ( "#{ fmt . current_prefix } == from: #{ from } " )
5539 end
5640
57- output . print ( " # " )
58- unless insn_flow . in . empty?
59- output . print ( "in: #{ insn_flow . in . join ( ", " ) } " )
60- output . print ( "; " ) unless insn_flow . out . empty?
41+ block_flow = block_flows . fetch ( block . id )
42+ unless block_flow . in . empty?
43+ fmt . output . puts ( "#{ fmt . current_prefix } == in: #{ block_flow . in . join ( ", " ) } " )
6144 end
6245
63- unless insn_flow . out . empty?
64- output . print ( "out: #{ insn_flow . out . join ( ", " ) } " )
46+ fmt . format_insns! ( block . insns , block . block_start ) do |insn , length |
47+ insn_flow = insn_flows [ length ]
48+ next if insn_flow . in . empty? && insn_flow . out . empty?
49+
50+ fmt . output . print ( " # " )
51+ unless insn_flow . in . empty?
52+ fmt . output . print ( "in: #{ insn_flow . in . join ( ", " ) } " )
53+ fmt . output . print ( "; " ) unless insn_flow . out . empty?
54+ end
55+
56+ unless insn_flow . out . empty?
57+ fmt . output . print ( "out: #{ insn_flow . out . join ( ", " ) } " )
58+ end
6559 end
6660
67- output . puts
68- end
69-
70- dests = block . outgoing_blocks . map ( &:id )
71- dests << "leaves" if block . insns . last . leaves?
72- output . puts ( " # to: #{ dests . join ( ", " ) } " ) unless dests . empty?
61+ to = block . outgoing_blocks . map ( &:id )
62+ to << "leaves" if block . insns . last . leaves?
63+ fmt . output . puts ( "#{ fmt . current_prefix } == to: #{ to . join ( ", " ) } " )
7364
74- unless block_flow . out . empty?
75- output . puts " # out: #{ block_flow . out . join ( ", " ) } "
65+ unless block_flow . out . empty?
66+ fmt . output . puts ( "#{ fmt . current_prefix } == out: #{ block_flow . out . join ( ", " ) } " )
67+ end
7668 end
7769 end
7870
79- output . string
71+ fmt . string
8072 end
8173
8274 # Verify that we constructed the data flow graph correctly.
0 commit comments