ACSE 2.0.2
Advanced Compiler System for Education (basic documentation)
Loading...
Searching...
No Matches
Program Intermediate Representation

Definitions and functions for handling the compiler IR. More...

Data Structures

struct  t_symbol
 
struct  t_program
 

Macros

#define REG_INVALID   ((t_regID)(-1))
 Constant used for invalid register identifiers.
 
#define REG_0   ((t_regID)(0))
 Constant identifying a register whose value is always zero.
 

Typedefs

typedef int t_regID
 Type for register identifiers.
 

Enumerations

enum  t_symbolType { TYPE_INT , TYPE_INT_ARRAY }
 

Handling of labels

t_label * createLabel (t_program *program)
 
void assignLabel (t_program *program, t_label *label)
 
void setLabelName (t_program *program, t_label *label, const char *name)
 
char * getLabelName (t_label *label)
 

Generation of instructions

t_regID getNewRegister (t_program *program)
 
t_instruction * genInstruction (t_program *program, int opcode, t_regID rd, t_regID rs1, t_regID rs2, t_label *label, int immediate)
 
void removeInstructionAt (t_program *program, t_listNode *instrLi)
 

Handling of symbols

t_symbolcreateSymbol (t_program *program, char *ID, t_symbolType type, int arraySize)
 
t_symbolgetSymbol (t_program *program, char *ID)
 
bool isArray (t_symbol *symbol)
 

Utility functions

void genEpilog (t_program *program)
 
void programDump (t_program *program, FILE *fout)
 

Detailed Description

Definitions and functions for handling the compiler IR.

During the compilation process, the program is built instruction by instruction by the code in the syntactic-directed translator in parser.y. The contents of the program are stored in an intermediate data structure of type t_program.

In successive compilation steps, the instructions and data declarations in the program intermediate representation are modified to make them conform to the requirements of the target machine, and at the end they are written to the output assembly file (see target_asm_print.h).


Data Structure Documentation

◆ t_symbol

struct t_symbol

A structure that represents the properties of a given symbol in the source code.

Definition at line 84 of file program.h.

Data Fields
int arraySize For arrays only, the size of the array.
char * ID Symbol name (should never be a NULL pointer or an empty string "").
t_label * label

A label that refers to the location of the variable inside the data segment.

t_symbolType type A valid data type.

◆ t_program

struct t_program

Object containing the program's intermediate representation during the compilation process.

Definition at line 98 of file program.h.

Data Fields
unsigned int firstUnusedLblID Next unused label ID.
t_regID firstUnusedReg Next unused register ID.
t_listNode * instructions List of instructions.
t_listNode * labels List of all labels.
t_label * pendingLabel Next pending label to assign.
t_listNode * symbols Symbol table.

Macro Definition Documentation

◆ REG_0

#define REG_0   ((t_regID)(0))

Constant identifying a register whose value is always zero.

Definition at line 33 of file program.h.

◆ REG_INVALID

#define REG_INVALID   ((t_regID)(-1))

Constant used for invalid register identifiers.

Definition at line 31 of file program.h.

Typedef Documentation

◆ t_regID

typedef int t_regID

Type for register identifiers.

Definition at line 28 of file program.h.

Enumeration Type Documentation

◆ t_symbolType

Supported data types.

Enumerator
TYPE_INT 

‘int’ scalar type.

TYPE_INT_ARRAY 

‘int’ array type.

Definition at line 37 of file program.h.

Function Documentation

◆ assignLabel()

void assignLabel ( t_program * program,
t_label * label )

Assign the given label object to the next instruction to be generated.

Parameters
programThe program where the label belongs.
labelThe label to be assigned.

◆ createLabel()

t_label * createLabel ( t_program * program)

Reserve a new label object, unassigned to any instruction.

Parameters
programThe program where the label belongs.
Returns
The new label object.

◆ createSymbol()

t_symbol * createSymbol ( t_program * program,
char * ID,
t_symbolType type,
int arraySize )

Add a symbol to the program.

Parameters
programThe program where to add the symbol.
IDThe identifier (name) of the new symbol.
typeThe data type of the variable associated to the symbol.
arraySizeFor arrays, the size of the array.
Returns
A pointer to the newly created symbol object.

◆ genEpilog()

void genEpilog ( t_program * program)

Generates the final instruction sequence required at the end of a program.

Parameters
programThe program to be modified.

◆ genInstruction()

t_instruction * genInstruction ( t_program * program,
int opcode,
t_regID rd,
t_regID rs1,
t_regID rs2,
t_label * label,
int immediate )

Add a new instruction at the end the current program's list of instructions.

Parameters
programThe program where to add the instruction.
opcodeIdentifier for the operation performed by the instruction.
rdIdentifier of the destination register argument, or REG_INVALID if none.
rs1Identifier of the first source register argument, or REG_INVALID if none.
rs2Identifier of the second source register argument, or REG_INVALID if none.
labelLabel object representing the label argument, or NULL if none.
immediateImmediate argument to the instruction, if needed.
Returns
the instruction object added to the instruction list.
Warning
This is a low-level primitive for generating instructions. This function is not aware of the semantic meaning of each operation code, and performs no parameter checking. As a result using this function directly may result in the generation of invalid instructions. Use the helper functions in codegen.h instead for generating instructions.

◆ getLabelName()

char * getLabelName ( t_label * label)

Obtain the name of a given label.

Parameters
labelThe label.
Returns
A dynamically allocated string. The caller owns the string and is responsible for freeing it.

◆ getNewRegister()

t_regID getNewRegister ( t_program * program)

Obtain a currently unused temporary register identifier.

Parameters
programThe program where the register will be used.
Returns
The identifier of the register.

◆ getSymbol()

t_symbol * getSymbol ( t_program * program,
char * ID )

Lookup a previously added symbol.

Parameters
programThe program where the symbol belongs.
IDThe identifier of the symbol.
Returns
A pointer to the corresponding symbol object, or NULL if the symbol has not been declared yet.

◆ isArray()

bool isArray ( t_symbol * symbol)

Checks if the type of the given symbol is an array type.

Parameters
symbolThe symbol object.
Returns
‘true’ if the type of the symbol is a kind of array, otherwise returns ‘false’.

◆ programDump()

void programDump ( t_program * program,
FILE * fout )

Dumps the current state of a program object to the specified file.

Parameters
programThe program which will be dumped.
foutThe file where to print the dump.

◆ removeInstructionAt()

void removeInstructionAt ( t_program * program,
t_listNode * instrLi )

Remove an instruction from the program, given its node in the instruction list.

Parameters
programThe program where to remove the instruction.
instrLiNode in the instruction list to remove.

◆ setLabelName()

void setLabelName ( t_program * program,
t_label * label,
const char * name )

Sets the name of a label to the specified string.

Parameters
programThe program where the label belongs.
labelThe label whose name to set.
nameThe string which will be used as label name.
Note
If another label with the same name already exists, the name assigned to this label will be modified to remove any ambiguity.