Class: SyntaxTree::YARV::ControlFlowGraph::Compiler
- Inherits:
-
Object
- Object
- SyntaxTree::YARV::ControlFlowGraph::Compiler
- 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
-
#insns ⇒ Object
readonly
This is a hash of indices in the YARV instruction sequence that point to their corresponding instruction.
-
#iseq ⇒ Object
readonly
This is the instruction sequence that is being compiled.
-
#labels ⇒ Object
readonly
This is a hash of labels that point to their corresponding index into the YARV instruction sequence.
Instance Method Summary collapse
-
#compile ⇒ Object
This method is used to compile the instruction sequence into a control flow graph.
-
#initialize(iseq) ⇒ Compiler
constructor
A new instance of Compiler.
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
#insns ⇒ Object (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 |
#iseq ⇒ Object (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 |
#labels ⇒ Object (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
#compile ⇒ Object
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 |