Nearly CC
An educational compiler skeleton
Classes | Public Types | Public Member Functions | List of all members
InstructionSequence Class Reference

InstructionSequence class, which represents a linear sequence of Instruction objects. More...

#include "instruction_seq.h"

Public Types

typedef ISeqIterator< std::vector< Slot >::const_iteratorconst_iterator
 Iterator over instructions in forward order.
 
typedef ISeqIterator< std::vector< Slot >::const_reverse_iteratorconst_reverse_iterator
 Iterator over instructions in reverse order.
 

Public Member Functions

 InstructionSequence ()
 Default constructor.
 
 InstructionSequence (BasicBlockKind kind, int code_order, const std::string &block_label)
 Constructor from BasicBlockKind, code order value, and block label. More...
 
InstructionSequenceduplicate () const
 Return a dynamically-allocated duplicate of this InstructionSequence. More...
 
const_iterator cbegin () const
 Get begin forward iterator over the instructions. More...
 
const_iterator cend () const
 Get end forward iterator over the instructions. More...
 
const_reverse_iterator crbegin () const
 Get begin reverse iterator over the instructions. More...
 
const_reverse_iterator crend () const
 Get end reverse iterator over the instructions. More...
 
template<typename Fn >
void apply_to_all (Fn f)
 
void append (Instruction *ins)
 Append an Instruction. More...
 
unsigned get_length () const
 Get number of instructions. More...
 
Instructionget_instruction (unsigned index) const
 Get Instruction at specified index. More...
 
Instructionget_last_instruction () const
 Get the last Instruction. More...
 
void define_label (const std::string &label)
 Define a label. More...
 
bool has_label (unsigned index) const
 Determine if Instruction at given index has a label. More...
 
std::string get_label_at_index (unsigned index) const
 Get label of the Instruction at the specified index. More...
 
bool has_label (const_iterator iter) const
 Determine if Instruction referred to by specified iterator has a label. More...
 
bool has_label_at_end () const
 Determine if the InstructionSequence has a label at the end This can happen in code generation for control constructs, when a label is generated as a target for the code that comes after the construct. More...
 
const_iterator get_iterator_at_labeled_position (const std::string &label) const
 Return a forward const iterator positioned at the instruction with the specified label, or the end iterator if there is no instruction with the specified label. More...
 
Instructionfind_labeled_instruction (const std::string &label) const
 Find Instruction labeled with specified label. More...
 
unsigned get_index_of_labeled_instruction (const std::string &label) const
 Return the index of instruction labeled with the specified label. More...
 
BasicBlockKind get_kind () const
 Get the BasicBlockKind. More...
 
void set_kind (BasicBlockKind kind)
 Set the BasicBlockKind. More...
 
bool has_block_label () const
 Check whether the first Instruction is labeled. More...
 
std::string get_block_label () const
 Get the label of the first Instruction. More...
 
void set_block_label (const std::string &block_label)
 Set the label on the first Instruction. More...
 
unsigned get_block_id () const
 Get the basic block id. More...
 
void set_block_id (unsigned block_id)
 Set the basic block id. More...
 
int get_code_order () const
 Get the code order value of this block. More...
 
void set_code_order (int code_order)
 Set the code order value of this block. More...
 

Detailed Description

InstructionSequence class, which represents a linear sequence of Instruction objects.

This can be used for both a "flat" IR for a function (with control flow), or a basic block in a ControlFlowGraph (where a branch or function call can only be the last instruction in the block.)

Constructor & Destructor Documentation

◆ InstructionSequence()

InstructionSequence::InstructionSequence ( BasicBlockKind  kind,
int  code_order,
const std::string &  block_label 
)

Constructor from BasicBlockKind, code order value, and block label.

This is useful for creating an InstructionSequence to be used as a basic block in a ControlFlowGraph.

Parameters
kindthe BasicBlockKind
code_orderthe relative order of this basic block
block_labellabel of first Instruction in the basic block

Member Function Documentation

◆ append()

void InstructionSequence::append ( Instruction ins)

Append an Instruction.

The InstructionSequence will assume responsibility for deleting the Instruction object.

Parameters
inspointer to the Instruction to append (and adopt)

◆ apply_to_all()

template<typename Fn >
void InstructionSequence::apply_to_all ( Fn  f)
inline
Template Parameters
Fnthe type of the function to apply
Parameters
fnthe function to apply to each instruction

◆ cbegin()

const_iterator InstructionSequence::cbegin ( ) const
inline

Get begin forward iterator over the instructions.

Returns
begin forward iterator over the instructions

◆ cend()

const_iterator InstructionSequence::cend ( ) const
inline

Get end forward iterator over the instructions.

Returns
end forward iterator over the instructions

◆ crbegin()

const_reverse_iterator InstructionSequence::crbegin ( ) const
inline

Get begin reverse iterator over the instructions.

Returns
begin reverse iterator over the instructions

◆ crend()

const_reverse_iterator InstructionSequence::crend ( ) const
inline

Get end reverse iterator over the instructions.

Returns
end reverse iterator over the instructions

◆ define_label()

void InstructionSequence::define_label ( const std::string &  label)

Define a label.

The next Instruction appended will be labeled with this label. This is useful for labeling an instruction as a control-flow target.

Parameters
labelthe label to apply to use for the next appended Instruction

◆ duplicate()

InstructionSequence * InstructionSequence::duplicate ( ) const

Return a dynamically-allocated duplicate of this InstructionSequence.

Returns
duplicate of this InstructionSequence

◆ find_labeled_instruction()

Instruction * InstructionSequence::find_labeled_instruction ( const std::string &  label) const

Find Instruction labeled with specified label.

Returns null pointer if no Instruction has the specified label.

Parameters
labela label
Returns
pointer to the Instruction with the label, or a null pointer if there is no Instruction with this label

◆ get_block_id()

unsigned InstructionSequence::get_block_id ( ) const
inline

Get the basic block id.

This is an arbitrary integer whose only significance is that it is different than the id of any other basic block.

Returns
the basic block id

◆ get_block_label()

std::string InstructionSequence::get_block_label ( ) const

Get the label of the first Instruction.

Returns
true if the first Instruction is labeled, false if not

◆ get_code_order()

int InstructionSequence::get_code_order ( ) const
inline

Get the code order value of this block.

This represents the relative position of the basic block's instructions in the original "flat" InstructionSequence.

Returns
the basic block's code order value

◆ get_index_of_labeled_instruction()

unsigned InstructionSequence::get_index_of_labeled_instruction ( const std::string &  label) const

Return the index of instruction labeled with the specified label.

Parameters
labela label
theindex of the Instruction with this label
Exceptions
RuntimeErrorif there is no instruction with the label

◆ get_instruction()

Instruction * InstructionSequence::get_instruction ( unsigned  index) const

Get Instruction at specified index.

Parameters
indexindex of instruction to get (0 for first, etc.)
Returns
pointer to the Instruction at that index

◆ get_iterator_at_labeled_position()

InstructionSequence::const_iterator InstructionSequence::get_iterator_at_labeled_position ( const std::string &  label) const

Return a forward const iterator positioned at the instruction with the specified label, or the end iterator if there is no instruction with the specified label.

Parameters
labela label
Returns
the forward iterator pointing to the Instruction with the label, or the end iterator if there is no instruction with this label

◆ get_kind()

BasicBlockKind InstructionSequence::get_kind ( ) const
inline

Get the BasicBlockKind.

This is useful for checking a basic block to see if it is the entry or exit block.

Returns
the BasicBlockKind

◆ get_label_at_index()

std::string InstructionSequence::get_label_at_index ( unsigned  index) const
inline

Get label of the Instruction at the specified index.

Parameters
indexindex of instruction to get (0 for first, etc.)
Returns
label of the instructino at this index (empty string if there is no label)

◆ get_last_instruction()

Instruction * InstructionSequence::get_last_instruction ( ) const

Get the last Instruction.

Returns
pointer to last Instruction

◆ get_length()

unsigned InstructionSequence::get_length ( ) const

Get number of instructions.

Returns
the number of instrtuctions

◆ has_block_label()

bool InstructionSequence::has_block_label ( ) const

Check whether the first Instruction is labeled.

This is useful for identifying whether a basic block is targeted by a branch instruction.

Returns
true if the first Instruction in the block is labeled, false if not

◆ has_label() [1/2]

bool InstructionSequence::has_label ( const_iterator  iter) const
inline

Determine if Instruction referred to by specified iterator has a label.

Parameters
itera forward iterator
Returns
true if the instruction pointed-to by the iterator has a label

◆ has_label() [2/2]

bool InstructionSequence::has_label ( unsigned  index) const
inline

Determine if Instruction at given index has a label.

Parameters
indexindex of instruction to get (0 for first, etc.)
Returns
true if the instruction at this index has a label, false if not

◆ has_label_at_end()

bool InstructionSequence::has_label_at_end ( ) const

Determine if the InstructionSequence has a label at the end This can happen in code generation for control constructs, when a label is generated as a target for the code that comes after the construct.

Returns
true if there is a label at the end of the InstructionSequence, false if not

◆ set_block_id()

void InstructionSequence::set_block_id ( unsigned  block_id)
inline

Set the basic block id.

Parameters
block_idthe basic block id to set

◆ set_block_label()

void InstructionSequence::set_block_label ( const std::string &  block_label)

Set the label on the first Instruction.

This is used by ControlFlowGraphBuilder when constructing a ControlFlowGraph from a "flat" InstructionSequence.

Parameters
block_labelthe label to set on the first Instruction

◆ set_code_order()

void InstructionSequence::set_code_order ( int  code_order)
inline

Set the code order value of this block.

Parameters
code_orderthe code order value to set

◆ set_kind()

void InstructionSequence::set_kind ( BasicBlockKind  kind)
inline

Set the BasicBlockKind.

Parameters
kindthe BasicBlockKind to set

The documentation for this class was generated from the following files: