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
181
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/furnace-avm2/transform/ast_build.rb', line 116
def transform(code, body)
@stack = []
@ast = AST::Node.new(:root)
dup = nil
spurious = 0
in_shortcut = false
shortjump = []
ternary = []
current_handler = nil
current_finally = nil
finallies = {}
body.exceptions.each_with_index do |exception, index|
first, second = exception, body.exceptions[index + 1]
next unless second
if first.from_offset == second.from_offset &&
second.to_offset > first.to_offset &&
first.target_offset > second.from_offset &&
first.target_offset < second.to_offset &&
first.to.is_a?(ABC::AS3Jump) &&
first.to.target.is_a?(ABC::AS3PushByte)
entry = first.to.target.next.target
epilogue = nil
cursor = entry.next
while cursor
if cursor.is_a?(ABC::AS3LookupSwitch) &&
cursor.body.default_offset == 8 &&
cursor.body.case_count == 0
epilogue = cursor
break
end
cursor = cursor.next
end
raise "cannot find finally epilogue" if epilogue.nil?
finallies[first.to_offset] = {
first_catch: first,
second_catch: second,
skip_intervals: [ first.target_offset...second.target_offset,
(second.target_offset + 4)...entry.offset ],
begin_offset: first.to.offset,
entry_offset: entry.offset,
epilogue_offset: epilogue.offset,
end_offset: epilogue.offset + epilogue.byte_length,
}
end
end
exceptions = {}
body.exceptions.each_with_index do |exception, index|
next if finallies.find { |k,f| f[:first_catch] == exception }
exceptions[exception.target_offset] = exception
end
code.each do |opcode|
if @verbose
puts "================================"
puts "stack: #{@stack.inspect}"
puts "shortjump: #{shortjump.inspect} ternary: #{ternary.inspect}"
puts "opcode: #{opcode.inspect}"
end
finalize_complex_expr(opcode, ternary, CONDITIONAL_OPERATORS, nil, [:ternary_if, false])
finalize_complex_expr(opcode, shortjump, [ :and, :or ], 1)
if finallies.has_key? opcode.offset
current_finally = finallies[opcode.offset]
end
if current_finally
if current_finally[:begin_offset] == opcode.offset
puts "FINALLY: begin" if @verbose
emit(AST::Node.new(:jump, [ current_finally[:end_offset] ], label: opcode.offset))
next
elsif current_finally[:skip_intervals].find { |si| si.include? opcode.offset }
puts "FINALLY: skip" if @verbose
next
elsif current_finally[:epilogue_offset] == opcode.offset
puts "FINALLY: epilogue" if @verbose
emit(AST::Node.new(:nop, [], label: opcode.offset))
@stack.clear
next
end
end
if exceptions.has_key? opcode.offset
exception = exceptions[opcode.offset]
current_handler = exception
if exception.variable
produce(AST::Node.new(:exception_variable, [ exception.variable.to_astlet ]))
else
produce(AST::Node.new(:exception_variable))
end
end
if dup == 1 && (opcode.is_a?(ABC::AS3CoerceB) ||
opcode.is_a?(ABC::AS3IfTrue) || opcode.is_a?(ABC::AS3IfFalse))
in_shortcut = true
dup = false
end
if in_shortcut
if opcode.is_a?(ABC::AS3IfTrue)
type = :or
elsif opcode.is_a?(ABC::AS3IfFalse)
type = :and
elsif opcode.is_a?(ABC::AS3CoerceB)
next
elsif opcode.is_a?(ABC::AS3Pop)
in_shortcut = false
next
elsif opcode.is_a?(ABC::AS3Jump) && opcode.body.jump_offset == 0
next
else
raise "invalid shortcut"
end
node = AST::Node.new(type, [], label: opcode.offset)
node.children = consume(1)
produce(node)
shortjump.push opcode.target_offset
elsif opcode.is_a?(ABC::AS3Swap)
first, second = @stack.pop, @stack.pop
@stack.push first, second
elsif opcode.is_a?(ABC::AS3Dup)
node = @stack.last
if PURE_OPERATORS.include?(node.type) ||
(node.type == :get_local && node.children.first == 0)
dup_node = node.dup
dup_node.metadata[:label] = opcode.offset
produce(dup_node)
else
dup ||= 0
dup += 1
end
elsif opcode.is_a?(ABC::AS3Jump)
if opcode.body.jump_offset == 0
node = AST::Node.new(:nop, [], label: opcode.offset)
emit(node)
elsif opcode.target.is_a? ABC::AS3Throw
node = AST::Node.new(:throw, [ consume(1) ], label: opcode.offset)
emit(node)
elsif @stack.any? && !CONDITIONAL_OPERATORS.include?(@stack.last.type)
extend_complex_expr(CONDITIONAL_OPERATORS)
ternary.push opcode.target_offset
else
expand_conditionals()
node = AST::Node.new(opcode.ast_type, opcode.parameters, label: opcode.offset)
emit(node)
end
elsif opcode.is_a?(ABC::ControlTransferOpcode)
node = AST::Node.new(opcode.ast_type, [], label: opcode.offset)
node.metadata[:offset] = opcode.target_offset
node.children = consume(opcode.consumes)
produce(node)
else
node = AST::Node.new(opcode.ast_type, [], label: opcode.offset)
if dup == 1
top_opcode, = consume(1)
found = true
if PRE_POST_OPERATORS.include?(top_opcode.type)
top_opcode.update(:"pre_#{top_opcode.type}")
elsif PRE_POST_OPERATORS.include?(node.type)
node.update(:"post_#{node.type}")
elsif SHORT_ASSIGN_OPERATORS.include? top_opcode.type
else
found = false
end
if found
produce(AST::Node.new(:unemit))
dup = false
end
produce(top_opcode)
end
if dup
spurious += 1
save_node = AST::Node.new(:set_local, [ -spurious, *consume(1) ])
expand_conditionals()
emit(save_node)
(1 + dup).times do
load_node = AST::Node.new(:get_local, [ -spurious ])
produce(load_node)
end
dup = false
end
parameters = consume(opcode.consumes)
if opcode.consumes_context
context = opcode.context(consume(opcode.consumes_context))
end
node.children.concat context if context
node.children.concat opcode.parameters
node.children.concat parameters
if opcode.produces == 0
if @stack.any? && @stack.last.type == :unemit
consume(1) produce(node)
next
end
expand_conditionals()
emit(node)
elsif opcode.produces == 1
produce(node)
end
end
end
if @stack.any?
raise "nonempty stack on exit"
end
[ @ast.normalize_hierarchy!, body, finallies.values ]
end
|