class SyntaxTree::YARV::ControlFlowGraph::Compiler

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

Attributes

insns[R]

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

iseq[R]

This is the instruction sequence that is being compiled.

labels[R]

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.

Public Class Methods

new(iseq) click to toggle source
# 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

Public Instance Methods

compile() click to toggle source

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

# 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