Class: Furnace::AVM2::Transform::CFGReduce

Inherits:
Object
  • Object
show all
Defined in:
lib/furnace-avm2/transform/cfg_reduce.rb

Instance Method Summary collapse

Instance Method Details

#check_nonlocal_loop(loop_stack, block) ⇒ Object

Check if the control transfer is nonlocal according to the innermost loop and adjust @loop_nonlocal accordingly for labels to be inserted where appropriate.



227
228
229
230
231
232
233
234
235
# File 'lib/furnace-avm2/transform/cfg_reduce.rb', line 227

def check_nonlocal_loop(loop_stack, block)
  if loop_stack.first != block
    @loop_nonlocal.add block

    yield [loop_label(block)]
  else
    yield []
  end
end

#completely_dominated?(block, dominator) ⇒ Boolean

Block B is completely dominated by another block D if it is dominated by D and no edges ever lead to block B from any other block, including those dominated by D, but excluding any back edges.

Returns:

  • (Boolean)


186
187
188
189
190
191
192
# File 'lib/furnace-avm2/transform/cfg_reduce.rb', line 186

def completely_dominated?(block, dominator)
  if @loops.include?(block)
    (block.sources - @loops[block].to_a) == [dominator]
  else
    block.sources == [dominator]
  end
end

#extended_block(block, stopgap = nil, loop_stack = []) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/furnace-avm2/transform/cfg_reduce.rb', line 19

def extended_block(block, stopgap=nil, loop_stack=[])
  nodes = []

  while block
    if @loops.include?(block) && loop_stack.include?(block)
      # We have just arrived to loop head. Insert `continue'
      # and exit.
      check_nonlocal_loop(loop_stack, block) do |params|
        nodes << AST::Node.new(:continue, params)
      end
      break
    elsif @loop_tails.include?(block) &&
              loop_stack.include?(@loop_tails[block])
      # We have just arrived to loop tail. Insert `break'
      # and exit.
      loop = @loop_tails[block]
      check_nonlocal_loop(loop_stack, loop) do |params|
        nodes << AST::Node.new(:break, params)
      end
      break
    elsif block == stopgap
      # We have just arrived to a merge point of `if'
      # contidional. Exit.
      break
    end

    if @visited.include? block
      raise "failsafe: block #{block.label} already visited"
    elsif block != @cfg.exit
      @visited.add block
    end

    block.insns.each do |insn|
      next if insn == block.cti
      nodes << insn
    end

    if block.cti
      if block.cti.type == :lookup_switch
        # this is a switch

      elsif @loops.include?(block)
        # we're trapped in a strange loop
        reverse = !block.cti.children[0]
        in_root, out_root = block.targets

        # One of the branch targets should reside within
        # the loop.
        if !@loops[block].include?(in_root)
          in_root, out_root = out_root, in_root
          reverse = !reverse
        end

        # Mark the loop tail so we could detect `break' and
        # `continue' statements.
        @loop_tails[out_root] = block

        # If we reversed the roots or it was a (jump-if false),
        # then reverse the condition.
        expr = normalize_cti_expr(block, reverse)

        body = extended_block(in_root, nil, [ block, *loop_stack ])

        # [(label name)]
        # We first parse the body and then add the label before
        # the loop body if anything in the body requires that label
        # to be present.
        if @loop_nonlocal.include?(block)
          nodes << AST::Node.new(:label, [ loop_label(block) ])
        end

        # (while (condition)
        #   (body ...))
        nodes << AST::Node.new(:while, [
          expr,
          body
        ])

        block = out_root
      else
        # this is an `if', `break' or `continue'
        reverse = !block.cti.children[0]
        left_root, right_root = block.targets

        # (if (condition)
        #   (if-true ...)
        #  [(if-false ...)])
        # Note that you cannot reach expression if-true nor
        # expression if-false without evaluating condition.
        # Thus, to go inside the if body, a root has to be
        # completely dominated by this block--that is, does
        # not have edges coming to it even from other blocks
        # dominated by this block.

        # If the left root isn't dominated by block,
        # then it can't be `if' branch.
        if !completely_dominated?(left_root, block)
          left_root, right_root = right_root, left_root
          reverse = !reverse
        end

        # If the left root still isn't dominated by block,
        # then this is not a proper conditional.
        unless completely_dominated?(left_root, block)
          raise "not-well-formed if"
        end

        # If the right root is dominated by this block, which
        # means that we have an `else' part, and if the condition
        # is reversed, turn that back. This serves purely aesthetical
        # purposes and depends on behavior of ASC code generator.
        if completely_dominated?(right_root, block) && reverse
          left_root, right_root = right_root, left_root
          reverse = false
        end

        # If we reversed the roots or it was a (jump-if false),
        # then reverse the condition.
        expr = normalize_cti_expr(block, reverse)

        # Does this conditional have an `else' block?
        if completely_dominated?(right_root, block)
          # Yes. Find a merge point.
          merge = find_merge_point(block, left_root, right_root, loop_stack)

          # If the merge search did not yield a valid node, use
          # stopgap for the current block to avoid runaway code
          # synthesis.
          #
          # The stopgap block is actually an innermost block from
          # a stopgap block set implicitly represented by a set of
          # objects contained in arguments of recursive calls. As
          # the `if's are fully nested when well-formed, we can only
          # check for collision with innermost stopgap block.
          nodes << AST::Node.new(:if, [
            expr,
            extended_block(left_root,  merge || stopgap, loop_stack),
            extended_block(right_root, merge || stopgap, loop_stack)
          ])

          block = merge
        else
          # No. The "right root" is actually post-if code.
          nodes << AST::Node.new(:if, [
            expr,
            extended_block(left_root, right_root, loop_stack)
          ])

          block = right_root
        end
      end
    elsif block.targets.count == 1
      block = block.targets.first
    elsif block == @cfg.exit
      break
    else
      raise "invalid target count (#{block.targets.count})"
    end
  end

  AST::Node.new(:begin, nodes)
end

#find_merge_point(root, left, right, loop_stack) ⇒ Object

A merge point for blocks R (root), L (left) and D (right) is first block found with BFS starting at L,D so that it is dominated by R, but not L or D.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/furnace-avm2/transform/cfg_reduce.rb', line 197

def find_merge_point(root, left, right, loop_stack)
  worklist = Set[left, right]
  visited  = Set[root, left, right]

  while worklist.any?
    node = worklist.first
    worklist.delete node

    visited.add node

    if (@dom[node].include?(root) &&
        !(@dom[node].include?(left) ||
          @dom[node].include?(right))) ||
        loop_stack.include?(node)
      return node
    end

    node.targets.each do |target|
      next if visited.include? target
      worklist.add target
    end
  end

  # The paths have diverged.
  nil
end

#loop_label(block) ⇒ Object



237
238
239
# File 'lib/furnace-avm2/transform/cfg_reduce.rb', line 237

def loop_label(block)
  "label#{block.label}"
end

#normalize_cti_expr(block, negate) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/furnace-avm2/transform/cfg_reduce.rb', line 241

def normalize_cti_expr(block, negate)
  if negate
    AST::Node.new(:!, [ block.cti.children[1] ])
  else
    block.cti.children[1]
  end
end

#transform(cfg) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/furnace-avm2/transform/cfg_reduce.rb', line 4

def transform(cfg)
  @cfg   = cfg

  @dom   = @cfg.dominators
  @loops = @cfg.identify_loops

  @visited       = Set.new
  @loop_tails    = {}
  @loop_nonlocal = Set.new

  ast, = extended_block(@cfg.entry)

  ast
end