Definitions and functions for handling the compiler IR.
More...
|
#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.
|
|
|
typedef int | t_regID |
| Type for register identifiers.
|
|
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).
◆ 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
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. |
◆ REG_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.
◆ t_regID
Type for register identifiers.
Definition at line 28 of file program.h.
◆ 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.
◆ assignLabel()
void assignLabel |
( |
t_program * | program, |
|
|
t_label * | label ) |
Assign the given label object to the next instruction to be generated.
- Parameters
-
program | The program where the label belongs. |
label | The label to be assigned. |
◆ createLabel()
Reserve a new label object, unassigned to any instruction.
- Parameters
-
program | The program where the label belongs. |
- Returns
- The new label object.
◆ createSymbol()
Add a symbol to the program.
- Parameters
-
program | The program where to add the symbol. |
ID | The identifier (name) of the new symbol. |
type | The data type of the variable associated to the symbol. |
arraySize | For arrays, the size of the array. |
- Returns
- A pointer to the newly created symbol object.
◆ genEpilog()
Generates the final instruction sequence required at the end of a program.
- Parameters
-
program | The program to be modified. |
◆ genInstruction()
Add a new instruction at the end the current program's list of instructions.
- Parameters
-
program | The program where to add the instruction. |
opcode | Identifier for the operation performed by the instruction. |
rd | Identifier of the destination register argument, or REG_INVALID if none. |
rs1 | Identifier of the first source register argument, or REG_INVALID if none. |
rs2 | Identifier of the second source register argument, or REG_INVALID if none. |
label | Label object representing the label argument, or NULL if none. |
immediate | Immediate 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
-
- Returns
- A dynamically allocated string. The caller owns the string and is responsible for freeing it.
◆ getNewRegister()
Obtain a currently unused temporary register identifier.
- Parameters
-
program | The program where the register will be used. |
- Returns
- The identifier of the register.
◆ getSymbol()
Lookup a previously added symbol.
- Parameters
-
program | The program where the symbol belongs. |
ID | The identifier of the symbol. |
- Returns
- A pointer to the corresponding symbol object, or NULL if the symbol has not been declared yet.
◆ isArray()
Checks if the type of the given symbol is an array type.
- Parameters
-
- 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
-
program | The program which will be dumped. |
fout | The file where to print the dump. |
◆ removeInstructionAt()
Remove an instruction from the program, given its node in the instruction list.
- Parameters
-
program | The program where to remove the instruction. |
instrLi | Node 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
-
program | The program where the label belongs. |
label | The label whose name to set. |
name | The 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.