Class: Furnace::AVM2::ABC::OpcodeSequence

Inherits:
Array
  • Object
show all
Defined in:
lib/furnace-avm2/abc/primitives/opcode_sequence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ OpcodeSequence

Returns a new instance of OpcodeSequence.



7
8
9
10
11
12
13
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 7

def initialize(options={})
  @root, @parent = options[:parent].root, options[:parent]
  @pos_cache    = {}
  @opcode_cache = {}

  @raw_code = nil
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 5

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 5

def root
  @root
end

Instance Method Details

#build_cfgObject



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
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 90

def build_cfg
  graph = CFG::Graph.new

  targets = []

  each do |opcode|
    if opcode.is_a? ControlTransferOpcode
      targets << opcode.target
    elsif opcode.is_a? AS3LookupSwitch
      targets << opcode.default_target
      targets += opcode.case_targets
    end
  end

  @parent.exceptions.each do |exc|
    targets << exc.target
  end

  pending_label = nil
  pending_queue = []

  cutoff = lambda do |targets|
    node = CFG::Node.new(graph, pending_label, pending_queue, nil, targets)

    if graph.nodes.empty?
      graph.entry = node
    end

    graph.nodes.add node

    pending_label = nil
    pending_queue = []
  end

  each do |opcode|
    if targets.include? opcode
      cutoff.([ opcode.offset ])
    end

    pending_label = opcode.offset if pending_label.nil?
    pending_queue << opcode

    if opcode.is_a?(ControlTransferOpcode)
      if opcode.conditional
        cutoff.([ opcode.target.offset, opcode.offset + opcode.byte_length ])
      else
        cutoff.([ opcode.target.offset ])
      end
    elsif opcode.is_a?(AS3LookupSwitch)
      cutoff.(opcode.parameters.flatten)
    elsif opcode.is_a?(FunctionReturnOpcode) ||
          opcode.is_a?(AS3Throw)
      cutoff.([])
    end
  end

  cutoff.([])

  if exceptions.any?
    exception_node = CFG::Node.new(graph, :exception, [], nil,
        exceptions.map(&:target_offset))
    graph.nodes.add exception_node
  end

  graph
end

#byte_lengthObject



76
77
78
79
80
81
82
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 76

def byte_length
  if @raw_code
    @raw_code.length
  else
    map(&:byte_length).reduce(0, :+)
  end
end

#disassembleObject

Transformations



86
87
88
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 86

def disassemble
  map(&:disassemble).join("\n")
end

#eachObject



31
32
33
34
35
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 31

def each
  parse if @raw_code

  super
end

#eliminate_dead!Object



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
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 157

def eliminate_dead!
  begin
    cfg = build_cfg
  rescue Exception => e
    # Ignore. This will fail at later stages.
    return false
  end

  worklist = Set[cfg.entry]
  @parent.exceptions.each do |exc|
    worklist.add cfg.find_node(exc.target_offset)
  end

  livelist = Set[]
  while worklist.any?
    node = worklist.first
    worklist.delete node

    livelist.add node

    node.targets.each do |target|
      worklist.add target unless livelist.include? target
    end
  end

  cfg.nodes.each do |node|
    unless livelist.include? node
      node.insns.each do |opcode|
        delete opcode
      end
    end
  end

  recache!

  livelist != cfg.nodes
end

#flush!Object



59
60
61
62
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 59

def flush!
  @pos_cache    = {}
  @opcode_cache = {}
end

#mapObject



37
38
39
40
41
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 37

def map
  parse if @raw_code

  super
end

#offset_of(opcode) ⇒ Object



70
71
72
73
74
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 70

def offset_of(opcode)
  parse if @raw_code

  @opcode_cache[opcode]
end

#opcode_at(position) ⇒ Object



64
65
66
67
68
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 64

def opcode_at(position)
  parse if @raw_code

  @pos_cache[position]
end

#read(io) ⇒ Object



15
16
17
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 15

def read(io)
  @raw_code = io.read(@parent.code_length)
end

#recache!Object

Offsets



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 45

def recache!
  flush!

  pos = 0
  each do |opcode|
    @pos_cache[pos]       = opcode
    @opcode_cache[opcode] = pos

    pos += opcode.byte_length
  end

  lookup!
end

#write(io) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 19

def write(io)
  if @raw_code
    io.write @raw_code
  else
    lookup!

    each do |opcode|
      opcode.write(io)
    end
  end
end