ACSE 2.0.3
Advanced Compiler System for Education
Loading...
Searching...
No Matches
Program Intermediate Representation

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

Data Structures

struct  t_label
struct  t_instrArg
struct  t_instruction
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 }

Construction/destruction of a program

t_programnewProgram (void)
void deleteProgram (t_program *program)

Handling of labels

t_labelcreateLabel (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_instructiongenInstruction (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_label

struct t_label

Object representing a label in the output assembly file.

Note
A label object does not uniquely identify a label, its labelID field does. This is used for aliasing multiple label objects to the same physical label if more than one label is assigned to an instruction.

Definition at line 47 of file program.h.

Data Fields
bool global True if the label will be defined as 'global'.
bool isAlias

True if this label object is an alias to another one with the same labelID.

unsigned int labelID Unique numeric identifier for the label.
char * name

Name of the label. If NULL, the name will be automatically generated in the form L<ID>.

◆ t_instrArg

struct t_instrArg

Object representing a register argument to an instruction.

Definition at line 61 of file program.h.

Data Fields
t_regID ID The register identifier.
t_listNode * mcRegWhitelist

The list of machine registers where this argument may be allocated. NULL if any machine register is allowed.

◆ t_instruction

struct t_instruction

Object representing a symbolic assembly instruction.

Definition at line 70 of file program.h.

Data Fields
t_label * addressParam

Address argument.

char * comment A comment string associated with the instruction, or NULL if none.
int immediate Immediate argument.
t_label * label Label associated with the instruction, or NULL.
int opcode Instruction opcode.
t_instrArg * rDest Destination argument (or NULL if none).
t_instrArg * rSrc1 First source argument (or NULL if none).
t_instrArg * rSrc2 Second source argument (or NULL if none).

◆ 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.

Definition at line 253 of file program.c.

◆ 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.

Definition at line 173 of file program.c.

◆ 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.

Definition at line 406 of file program.c.

◆ deleteProgram()

void deleteProgram ( t_program * program)

Delete a program object.

Parameters
programThe program object to delete.

Definition at line 162 of file program.c.

◆ genEpilog()

void genEpilog ( t_program * program)

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

Parameters
programThe program to be modified.

Definition at line 475 of file program.c.

◆ 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.

Definition at line 338 of file program.c.

◆ 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.

Definition at line 297 of file program.c.

◆ 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.

Definition at line 398 of file program.c.

◆ 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.

Definition at line 461 of file program.c.

◆ 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’.

Definition at line 445 of file program.c.

◆ newProgram()

t_program * newProgram ( void )

Create a new empty program object.

Definition at line 141 of file program.c.

◆ 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.

Definition at line 493 of file program.c.

◆ 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.

Definition at line 361 of file program.c.

◆ 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.

Definition at line 205 of file program.c.