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

Instruction object type. More...

#include "instruction.h"

Public Member Functions

 Instruction (int opcode)
 Contructor from opcode. More...
 
 Instruction (int opcode, const Operand &op1)
 Constructor from opcode and one Operand. More...
 
 Instruction (int opcode, const Operand &op1, const Operand &op2)
 Constructor from opcode and two Operands. More...
 
 Instruction (int opcode, const Operand &op1, const Operand &op2, const Operand &op3, unsigned num_operands=3)
 Constructor from opcode and up to three operands. More...
 
Instructionduplicate () const
 Return an exact duplicate of this Instruction. More...
 
int get_opcode () const
 Get the opcode value. More...
 
unsigned get_num_operands () const
 Get the number of operands. More...
 
const Operandget_operand (unsigned index) const
 Get the specified Operand. More...
 
void set_operand (unsigned index, const Operand &operand)
 Modify the specified Operand. More...
 
Operand get_last_operand () const
 Return a copy of the last (rightmost) Operand. More...
 
void set_comment (const std::string &comment)
 Set a textual comment for this Instruction. More...
 
bool has_comment () const
 Check whether this Instruction has a comment set. More...
 
const std::string & get_comment () const
 Get the textual comment for this Instruction. More...
 
void set_symbol (Symbol *sym)
 Set a symbol table entry (Symbol). More...
 
Symbolget_symbol () const
 Return the symbol table entry (Symbol). More...
 

Detailed Description

Instruction object type.

This is a traditional "quad"-style instruction representation. Can be used for either high-level or low-level code.

Constructor & Destructor Documentation

◆ Instruction() [1/4]

Instruction::Instruction ( int  opcode)

Contructor from opcode.

The initialized Instruction object will not have any operands.

Parameters
opcodethe high-level or low-level opcode value

◆ Instruction() [2/4]

Instruction::Instruction ( int  opcode,
const Operand op1 
)

Constructor from opcode and one Operand.

Parameters
opcodethe high-level or low-level opcode value
op1the first (and only) Operand

◆ Instruction() [3/4]

Instruction::Instruction ( int  opcode,
const Operand op1,
const Operand op2 
)

Constructor from opcode and two Operands.

Parameters
opcodethe high-level or low-level opcode value
op1the first Operand
op2the second Operand

◆ Instruction() [4/4]

Instruction::Instruction ( int  opcode,
const Operand op1,
const Operand op2,
const Operand op3,
unsigned  num_operands = 3 
)

Constructor from opcode and up to three operands.

Parameters
opcodethe high-level or low-level opcode value
op1the first Operand
op2the second Operand
op3the third Operand
num_operandsthe number of operands (defaults to 3)

Member Function Documentation

◆ duplicate()

Instruction* Instruction::duplicate ( ) const
inline

Return an exact duplicate of this Instruction.

Returns
an exact duplicate of this Instruction

◆ get_comment()

const std::string& Instruction::get_comment ( ) const
inline

Get the textual comment for this Instruction.

Returns
the comment for this instruction (empty sting if there is no comment)

◆ get_last_operand()

Operand Instruction::get_last_operand ( ) const

Return a copy of the last (rightmost) Operand.

Returns
a copy of the last (rightmost) Operand

◆ get_num_operands()

unsigned Instruction::get_num_operands ( ) const

Get the number of operands.

Returns
the number of operands

◆ get_opcode()

int Instruction::get_opcode ( ) const

Get the opcode value.

In general, this will be the ordinal value of either a HighLevelOpcode or LowLevelOpcode enumeration member, depending on whether the object is a high-level or low-level Instuctiion.

Returns
the opcode value

◆ get_operand()

const Operand & Instruction::get_operand ( unsigned  index) const

Get the specified Operand.

Parameters
indexthe index of the Operand to return (0 for first, etc.)
Returns
the specified Operand

◆ get_symbol()

Symbol* Instruction::get_symbol ( ) const
inline

Return the symbol table entry (Symbol).

Returns
a pointer to the symbol table entry (Symbol), or a null pointer if the Instruction doesn't have a symbol table entry set

◆ has_comment()

bool Instruction::has_comment ( ) const
inline

Check whether this Instruction has a comment set.

Returns
true if this Instruction has a comment set, false otherwise

◆ set_comment()

void Instruction::set_comment ( const std::string &  comment)
inline

Set a textual comment for this Instruction.

The comment will appear in the printed representation of the Instruction. This is useful for debugging. For example, when a high-level Instruction is translated to a sequence of low-level Instructions, a string representation of the high-level instruction can be set as a comment for the first low-level Instruction.

Parameters
commentthe comment to set

◆ set_operand()

void Instruction::set_operand ( unsigned  index,
const Operand operand 
)

Modify the specified Operand.

Parameters
indexthe Operand to modify (0 for first, etc.)
operandthe value to copy to the specified Operand of the Instruction

◆ set_symbol()

void Instruction::set_symbol ( Symbol sym)
inline

Set a symbol table entry (Symbol).

This is useful for function calls, to link the function call with information (such as the FunctionType) of the called function.

Parameters
symthe Symbol to set

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