Class: Furnace::AVM2::Transform::ASTBuild
- Inherits:
-
Object
- Object
- Furnace::AVM2::Transform::ASTBuild
- Defined in:
- lib/furnace-avm2/transform/ast_build.rb
Overview
I’m not exactly proud of this code, but it works… for now. I really should rework it if I want to expect it to work good.
Constant Summary collapse
- CONDITIONAL_OPERATORS =
[ :if_eq, :if_false, :if_true, :if_ge, :if_gt, :if_le, :if_lt, :if_ne, :if_nge, :if_ngt, :if_nle, :if_nlt, :if_strict_eq, :if_strict_ne, :if_true ]
- CONST_OPERATORS =
[ :integer, :double, :string, :false, :true, :nan, :undefined, :null ]
Instance Method Summary collapse
-
#initialize(options) ⇒ ASTBuild
constructor
A new instance of ASTBuild.
- #transform(code, method) ⇒ Object
Constructor Details
#initialize(options) ⇒ ASTBuild
Returns a new instance of ASTBuild.
12 13 14 15 16 17 |
# File 'lib/furnace-avm2/transform/ast_build.rb', line 12 def initialize() @validate = [:validate] || false @verbose = [:verbose] || false #@verbose = true end |
Instance Method Details
#transform(code, method) ⇒ 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 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 |
# File 'lib/furnace-avm2/transform/ast_build.rb', line 19 def transform(code, method) stack = [] ast = AST::Node.new(:root) dup = nil spurious = 0 in_shortcut = false shortjump = [] ternary = [] consume = lambda do |count| if count == 0 [] elsif count <= stack.size stack.slice!(-count..-1) else raise "cannot consume #{count}: stack underflow with #{stack.size}" end end produce = lambda do |what| stack.push what end emit = lambda do |node| if @verbose puts "emitted:" puts node.to_sexp(1) end ast.children.push node end extend_complex_expr = lambda do |valid_types, expected_depth=nil| expr, current = consume.(2) if @validate if !valid_types.include?(expr.type) raise "invalid complex expr: #{expr.type} not in #{valid_types}" elsif expected_depth && expr.children.count != expected_depth raise "invalid complex expr: depth #{expr.children.count} != #{expected_depth}" end end expr.children << current produce.(expr) end finalize_complex_expr = lambda do |opcode, worklist, valid_types, expected_depth=nil, wrap_to=nil| while worklist.last == opcode.offset extend_complex_expr.(valid_types, expected_depth) if wrap_to node, *prepend = *wrap_to expr, = consume.(1) expr = AST::Node.new(node, [*prepend, expr]) produce.(expr) end worklist.pop end end = lambda do expressions = [] while stack.any? && CONDITIONAL_OPERATORS.include?(stack.last.type) conditional, = consume.(1) jump_node = AST::Node.new(:jump_if, [ true, conditional.[:offset], conditional ]) expressions.unshift jump_node end ast.children.concat expressions 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 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 # nop 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 CONST_OPERATORS.include?(node.type) || (node.type == :get_local && node.children.first == 0) dup_node = node.dup dup_node.[: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 stack.any? && !CONDITIONAL_OPERATORS.include?(stack.last.type) extend_complex_expr.(CONDITIONAL_OPERATORS) ternary.push opcode.target_offset else .() 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.[:offset] = opcode.target_offset node.children = consume.(opcode.consumes) produce.(node) elsif opcode.is_a?(ABC::AS3Kill) # Ignore. SSA will handle that. else if dup spurious += 1 save_node = AST::Node.new(:set_local, [ -spurious, *consume.(1) ]) emit.(save_node) (1 + dup).times do load_node = AST::Node.new(:get_local, [ -spurious ]) produce.(load_node) end dup = false end node = AST::Node.new(opcode.ast_type, [], label: opcode.offset) 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 # All opcodes which produce 2 results--that is, dup and swap-- # are already handled at the top. if opcode.produces == 0 .() emit.(node) elsif opcode.produces == 1 produce.(node) end end end if @validate extracted_opcodes = [] extract = lambda do |tree| extracted_opcodes.push tree.[:label] tree.select { |c| c.is_a? Node }.each { |c| extract.(c) } end end ast.normalize_hierarchy! end |