Class: Furnace::AVM2::Transform::NFNormalize

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

Constant Summary collapse

LocalIncDecMatcher =
AST::Matcher.new do
  [ either_multi[
      [:set_slot,  capture(:index), capture(:scope)],
      [:set_local, capture(:index)],
    ],
    either[
      [:convert, any,
        capture(:inner)],
      [:coerce, :any,
        capture(:inner)],
      capture(:inner)
    ]
  ]
end
LocalIncDecInnerMatcher =
AST::Matcher.new do
  [capture(:operator),
    either[
      [:convert, any,
        capture(:getter)],
      capture(:getter),
    ]
  ]
end
LocalIncDecGetterMatcher =
AST::Matcher.new do
  either[
    [:get_slot,  backref(:index), backref(:scope)],
    [:get_local, backref(:index)],
  ]
end
IncDecOperators =
[
  :pre_increment, :post_increment,
  :pre_decrement, :post_decrement
]
ExpandedForInMatcher =
AST::Matcher.new do
  [:if, [:has_next2, skip], skip]
end
ForInMatcher =
AST::Matcher.new do
  [:while,
    [:has_next2, capture(:object_reg), capture(:index_reg)],
    [:begin,
      [ either_multi[
          [ :set_local, capture(:value_reg) ],
          [ :set_slot, capture(:value_reg), [:get_scope_object, 1] ],
        ],
        [ either[:coerce, :convert], capture(:value_type),
          [ capture(:iterator),
            [:get_local, backref(:object_reg)],
            [:get_local, backref(:index_reg)]]]],
      capture_rest(:body)]]
end
ForInIndexMatcher =
AST::Matcher.new do
  [:set_local, backref(:index_reg), [:integer, 0]]
end
ForInObjectMatcher =
AST::Matcher.new do
  [:set_local, backref(:object_reg),
    [:coerce, :any,
      capture(:root)]]
end
SuperfluousContinueMatcher =
AST::Matcher.new do
  [:continue]
end
OptimizedSwitchSeed =
AST::Matcher.new do
  [:ternary,
    [:===, capture(:case_value),
      [:get_local, capture(:local_index)]],
    [:integer, capture(:case_index)],
    capture(:nested)]
end
OptimizedSwitchNested =
AST::Matcher.new do
  either[
    [:ternary,
      [:===, capture(:case_value),
        [:get_local, backref(:local_index)]],
      [:integer, capture(:case_index)],
      capture(:nested)],
    [:integer, capture(:default_index)]
  ]
end
NumericCase =
AST::Matcher.new do
  [:case, [:integer, capture(:index)]]
end

Instance Method Summary collapse

Instance Method Details

#on_begin(node) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
223
224
225
226
# File 'lib/furnace-avm2/transform/nf_normalize.rb', line 182

def on_begin(node)
  # Fold (with)'s
  with_begin = node.children.index do |child|
    child.type == :push_with
  end
  with_end = nil

  if with_begin
    nesting = 0
    node.children[with_begin..-1].each_with_index do |child, index|
      if child.type == :push_with || child.type == :push_scope
        nesting += 1
      elsif child.type == :pop_scope
        nesting -= 1
        if nesting == 0
          with_end = with_begin + index
          break
        end
      end
    end

    if nesting == 0
      with_scope,  = node.children[with_begin].children
      with_content = node.children.slice (with_begin + 1)..(with_end - 1)

      with_node = AST::Node.new(:with, [
        with_scope,
        AST::Node.new(:begin,
          with_content
        )
      ])

      node.children.slice! with_begin..with_end
      node.children.insert with_begin, with_node
    end
  end

  # Remove obviously dead code
  first_ctn = node.children.index do |child|
    [:return_void, :return_value, :break, :continue, :throw].include? child.type
  end
  return unless first_ctn

  node.children.slice! (first_ctn + 1)..-1
end

#on_if(node) ⇒ Object

Loops can get expanded, but conditionals would never contain has-next2.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/furnace-avm2/transform/nf_normalize.rb', line 93

def on_if(node)
  if ExpandedForInMatcher.match node
    condition, body, rest = node.children

    body.children << AST::Node.new(:break)

    loop = AST::Node.new(:while, [ condition, body ])
    on_while(loop, node.parent, node)

    if rest
      node.update(:expand, [ loop ] + rest.children)
    else
      node.update(:expand, [ loop ])
    end
  end
end

#on_nop(node) ⇒ Object



21
22
23
# File 'lib/furnace-avm2/transform/nf_normalize.rb', line 21

def on_nop(node)
  node.update(:remove)
end

#on_set_local(node) ⇒ Object Also known as: on_set_slot



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/furnace-avm2/transform/nf_normalize.rb', line 62

def on_set_local(node)
  captures = {}
  if LocalIncDecMatcher.match(node, captures) &&
        LocalIncDecInnerMatcher.match(captures[:inner], captures) &&
        IncDecOperators.include?(captures[:operator])
    if captures[:getter].is_a?(AST::Node) &&
        LocalIncDecGetterMatcher.match(captures[:getter], captures)
      if captures[:scope]
        node.update(:"#{captures[:operator]}_slot", [ captures[:index], captures[:scope] ])
      else
        node.update(:"#{captures[:operator]}_local", [ captures[:index] ])
      end
    else
      node.update(:add, [
        AST::Node.new(:set_local, [
          captures[:index],
          captures[:getter]
        ]),
        AST::Node.new(:integer, [ 1 ])
      ])
    end
  end
end

#on_switch(node) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/furnace-avm2/transform/nf_normalize.rb', line 251

def on_switch(node)
  condition, body = node.children

  if captures = OptimizedSwitchSeed.match(condition)
    mapping = { captures[:case_index] => captures[:case_value] }
    while captures = OptimizedSwitchNested.match(captures[:nested], captures)
      break if captures[:default_index]
      mapping[captures[:case_index]] = captures[:case_value]
    end

    return if captures.nil?

    case_mapping = {}

    body.children.each do |child|
      if case_captures = NumericCase.match(child)
        case_index = case_captures[:index]
        if captures[:default_index] == case_index
          case_mapping[child] = nil
        elsif mapping.has_key?(case_index)
          case_mapping[child] = mapping[case_index]
        else
          # fallback
          return
        end
      end
    end

    # At this point, we are sure that this switch can be transformed.

    node.children[0] = AST::Node.new(:get_local, [ captures[:local_index] ])

    case_mapping.each do |child, value|
      if value.nil?
        body.children.delete child
      else
        child.children[0] = value
      end
    end
  end
end

#on_while(node, parent = node.parent, enclosure = node) ⇒ Object



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/nf_normalize.rb', line 139

def on_while(node, parent=node.parent, enclosure=node)
  *whatever, code = node.children
  if SuperfluousContinueMatcher.match code.children.last
    code.children.slice! -1
  end

  if captures = ForInMatcher.match(node)
    case captures[:iterator]
    when :next_name
      type = :for_in
    when :next_value
      type = :for_each_in
    else
      return
    end

    index_node = object_node = nil

    loop_index = parent.children.index(enclosure)
    parent.children[0..loop_index].reverse_each do |parent_node|
      if ForInIndexMatcher.match(parent_node, captures)
        index_node  = parent_node
      elsif ForInObjectMatcher.match(parent_node, captures)
        object_node = parent_node
      end

      break if index_node && object_node
    end

    return unless index_node && object_node

    index_node.update(:remove)
    object_node.update(:remove) if type != :for_each_in

    node.update(type, [
      captures[:value_reg],
      captures[:value_type],
      captures[:object_reg],
      AST::Node.new(:begin, captures[:body])
    ])
  end
end

#remove_useless_returnObject



15
16
17
18
19
# File 'lib/furnace-avm2/transform/nf_normalize.rb', line 15

def remove_useless_return
  if @nf.children.last.type == :return_void
    @nf.children.slice! -1
  end
end

#transform(nf) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/furnace-avm2/transform/nf_normalize.rb', line 6

def transform(nf)
  @nf = nf.normalize_hierarchy!

  remove_useless_return
  visit @nf

  @nf
end