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



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

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

  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)
    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



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

def byte_length
  map(&:byte_length).reduce(0, :+)
end

#disassembleObject

Transformations



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

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



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

def eliminate_dead!
  cfg = build_cfg
  dead_opcodes = []

  worklist = cfg.nodes.dup
  while worklist.any?
    node = worklist.first
    worklist.delete node

    next if node == cfg.entry

    if node.sources.count == 0 ||
          node.sources == [node]
      dead_opcodes.concat node.insns
    end
  end

  dead_opcodes.each do |opcode|
    delete opcode
  end

  recache!

  dead_opcodes.any?
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



68
69
70
# File 'lib/furnace-avm2/abc/primitives/opcode_sequence.rb', line 68

def offset_of(opcode)
  @opcode_cache[opcode]
end

#opcode_at(position) ⇒ Object



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

def opcode_at(position)
  @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