Class: Furnace::AVM2::Transform::CFGBuild

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

Instance Method Summary collapse

Instance Method Details

#transform(ast, body, finallies) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/furnace-avm2/transform/cfg_build.rb', line 4

def transform(ast, body, finallies)
  @jumps      = Set.new
  @exceptions = {}

  @cfg = CFG::Graph.new

  body.exceptions.each_with_index do |exc, index|
    if finallies.find { |f| f[:first_catch] == exc }
      # The first catch in finally is a no-op
      next
    end

    unless exc_block = @exceptions[exc.range]
      exc_block = CFG::Node.new(@cfg, "exc_#{index}")

      dispatch_node = AST::Node.new(:exception_dispatch, [], keep: true)
      exc_block.insns << dispatch_node
      exc_block.cti = dispatch_node

      @exceptions[exc.range] = exc_block
    end

    exc_block.target_labels << exc.target_offset

    if finallies.find { |f| f[:second_catch] == exc }
      exc_block.cti.children <<
          AST::Node.new(:finally,
            [ exc.target_offset ])
    else
      exc_block.cti.children <<
          AST::Node.new(:catch,
            [ (exc.exception.to_astlet if exc.exception),
              exc.variable.to_astlet,
              exc.target_offset ])
    end
  end

  @pending_label = nil
  @pending_exc_block = nil
  @pending_exc_range = nil
  @pending_queue = []

  ast.children.each_with_index do |node, index|
    unless @pending_label
      @pending_label = node.[:label]

      exception_block_for(@pending_label) do |block, range|
        @pending_exc_block = block
        @pending_exc_range = range
      end
    end

    @pending_queue << node if ![:nop, :jump, :label].include? node.type

    next_node  = ast.children[index + 1]
    next_label = next_node.[:label] if next_node

    case node.type
    when :return_value, :return_void, :throw
      cutoff(nil, [nil])

    when :jump
      @jumps.add(node.children[0])
      cutoff(nil, [ node.children.delete_at(0) ])

    when :jump_if
      @jumps.add(node.children[1])
      cutoff(node, [ node.children.delete_at(1), next_label ])

    when :lookup_switch
      jumps_to = [ node.children[0] ] + node.children[1]
      @jumps.merge(jumps_to)
      cutoff(node, jumps_to)

    else
      *, next_exception_block = exception_block_for(next_label)

      if @jumps.include?(next_label) || (next_node && next_node.type == :label)
        cutoff(nil, [next_label])
      elsif body.exceptions.find { |ex| ex.target_offset == next_label }
        cutoff(nil, [next_label])
      elsif @pending_exc_block != next_exception_block
        cutoff(nil, [next_label])
      end
    end
  end

  exit_node = CFG::Node.new(@cfg)
  @cfg.nodes.add exit_node
  @cfg.exit = exit_node

  @exceptions.values.each do |exc_node|
    @cfg.nodes.add exc_node
  end

  @cfg.eliminate_unreachable!
  @cfg.merge_redundant!

  @cfg
end