Class: SyntaxTree::YARV::ControlFlowGraph::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/control_flow_graph.rb

Overview

This class is responsible for creating a control flow graph from the given instruction sequence.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iseq) ⇒ Compiler

Returns a new instance of Compiler.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/syntax_tree/yarv/control_flow_graph.rb', line 34

def initialize(iseq)
  @iseq = iseq

  @insns = {}
  @labels = {}

  length = 0
  iseq.insns.each do |insn|
    case insn
    when Instruction
      @insns[length] = insn
      length += insn.length
    when InstructionSequence::Label
      @labels[insn] = length
    end
  end
end

Instance Attribute Details

#insnsObject (readonly)

This is a hash of indices in the YARV instruction sequence that point to their corresponding instruction.



25
26
27
# File 'lib/syntax_tree/yarv/control_flow_graph.rb', line 25

def insns
  @insns
end

#iseqObject (readonly)

This is the instruction sequence that is being compiled.



21
22
23
# File 'lib/syntax_tree/yarv/control_flow_graph.rb', line 21

def iseq
  @iseq
end

#labelsObject (readonly)

This is a hash of labels that point to their corresponding index into the YARV instruction sequence. Note that this is not the same as the index into the list of instructions on the instruction sequence object. Instead, this is the index into the C array, so it includes operands.



32
33
34
# File 'lib/syntax_tree/yarv/control_flow_graph.rb', line 32

def labels
  @labels
end

Instance Method Details

#compileObject

This method is used to compile the instruction sequence into a control flow graph. It returns an instance of ControlFlowGraph.



54
55
56
57
58
59
60
61
# File 'lib/syntax_tree/yarv/control_flow_graph.rb', line 54

def compile
  blocks = build_basic_blocks

  connect_basic_blocks(blocks)
  prune_basic_blocks(blocks)

  ControlFlowGraph.new(iseq, insns, blocks.values).tap(&:verify)
end