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_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
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
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
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()
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. |
Definition at line 253 of file program.c.
◆ createLabel()
Reserve a new label object, unassigned to any instruction.
- Parameters
-
| program | The program where the label belongs. |
- Returns
- The new label object.
Definition at line 173 of file program.c.
◆ 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.
Definition at line 406 of file program.c.
◆ deleteProgram()
Delete a program object.
- Parameters
-
| program | The program object to delete. |
Definition at line 162 of file program.c.
◆ genEpilog()
Generates the final instruction sequence required at the end of a program.
- Parameters
-
| program | The program to be modified. |
Definition at line 475 of file program.c.
◆ 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.
Definition at line 338 of file program.c.
◆ 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.
Definition at line 297 of file program.c.
◆ getNewRegister()
Obtain a currently unused temporary register identifier.
- Parameters
-
| program | The program where the register will be used. |
- Returns
- The identifier of the register.
Definition at line 398 of file program.c.
◆ 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.
Definition at line 461 of file program.c.
◆ 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’.
Definition at line 445 of file program.c.
◆ newProgram()
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
-
| program | The program which will be dumped. |
| fout | The file where to print the dump. |
Definition at line 493 of file program.c.
◆ 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. |
Definition at line 361 of file program.c.
◆ setLabelName()
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.
Definition at line 205 of file program.c.