15static t_label *newLabel(
unsigned int value)
36 while (curNode != NULL) {
39 curNode = curNode->
next;
75 if (inst->
rDest != NULL) {
79 if (inst->
rSrc1 != NULL) {
83 if (inst->
rSrc2 != NULL) {
93 if (instructions == NULL)
97 while (curNode != NULL) {
100 curNode = curNode->
next;
115 result->
label = NULL;
119static void deleteSymbol(
t_symbol *s)
125static void deleteSymbols(
t_listNode *variables)
127 if (variables == NULL)
131 while (curNode != NULL) {
133 deleteSymbol(curSymbol);
134 curNode = curNode->
next;
166 deleteSymbols(program->
symbols);
190 for (i = program->
labels; i != NULL; i = i->
next) {
198 thisLab->
name = strdup(finalName);
200 thisLab->
name = NULL;
208 char *sanitizedName = calloc(strlen(name) + 1,
sizeof(
char));
211 const char *srcp = name;
212 for (
char *dstp = sanitizedName; *srcp; srcp++) {
213 if (*srcp ==
'_' || isalnum(*srcp))
218 size_t allocatedSpace = strlen(sanitizedName) + 24;
219 char *finalName = calloc(allocatedSpace,
sizeof(
char));
222 snprintf(finalName, allocatedSpace,
"%s", sanitizedName);
228 for (i = program->
labels; i != NULL; i = i->
next) {
237 difference = strcmp(finalName, thisLabName);
240 if (difference == 0) {
242 snprintf(finalName, allocatedSpace,
"%s_%d", sanitizedName, ++serial);
302 buf = strdup(label->
name);
304 buf = calloc(24,
sizeof(
char));
305 snprintf(buf, 24,
"l_%d", label->
labelID);
325 size_t strBufSz = fileNameLen + 10 + 1;
326 instr->
comment = calloc(strBufSz,
sizeof(
char));
371 nextInst = nextPos->
data;
374 if (instrToRemove->
label) {
377 if (!nextInst || (nextInst->
label)) {
383 instrToRemove->
label = NULL;
420 if (existingSym != NULL) {
432 char *lblName = calloc(strlen(ID) + 8,
sizeof(
char));
435 sprintf(lblName,
"l_%s", ID);
454static bool compareVariableWithIDString(
void *a,
void *b)
457 char *str = (
char *)b;
458 return strcmp(var->
ID, str) == 0;
468 if (elementFound != NULL)
495 fprintf(fout,
"# Program dump\n\n");
497 fprintf(fout,
"## Variables\n\n");
501 fprintf(fout,
"\"%s\":\n", var->
ID);
504 fprintf(fout,
" type = int\n");
506 fprintf(fout,
" type = int[%d]\n", var->
arraySize);
508 fprintf(fout,
" type = invalid\n");
511 fprintf(fout,
" label = %s (ID=%d)\n", labelName, var->
label->
labelID);
514 curVarNode = curVarNode->
next;
517 fprintf(fout,
"\n## Instructions\n\n");
519 while (curInstNode) {
522 fprintf(fout,
"(null)");
526 curInstNode = curInstNode->
next;
Code generation functions.
bool printInstruction(t_instruction *instr, FILE *fp, bool machineRegIDs)
char * file
The name of the file.
int row
The zero-based index of a line in the file.
void emitError(t_fileLocation loc, const char *fmt,...)
void fatalError(const char *format,...)
Structure that represents a location in a file.
t_instruction * genExit0Syscall(t_program *program)
t_instruction * genNOP(t_program *program)
void * data
Pointer to the data associated to this node.
t_listNode * listInsertAfter(t_listNode *list, t_listNode *listPos, void *data)
t_listNode * listInsert(t_listNode *list, void *data, int pos)
t_listNode * deleteList(t_listNode *list)
t_listNode * listFindWithCallback(t_listNode *list, void *data, bool(*compareFunc)(void *a, void *b))
t_listNode * listRemoveNode(t_listNode *list, t_listNode *element)
t_listNode * listGetLastNode(t_listNode *list)
t_fileLocation curFileLoc
unsigned int firstUnusedLblID
Next unused label ID.
t_symbolType type
A valid data type.
t_listNode * instructions
List of instructions.
t_listNode * mcRegWhitelist
char * comment
A comment string associated with the instruction, or NULL if none.
int arraySize
For arrays only, the size of the array.
t_listNode * labels
List of all labels.
t_listNode * symbols
Symbol table.
t_label * pendingLabel
Next pending label to assign.
unsigned int labelID
Unique numeric identifier for the label.
char * ID
Symbol name (should never be a NULL pointer or an empty string "").
bool global
True if the label will be defined as 'global'.
t_instrArg * rSrc1
First source argument (or NULL if none).
t_regID firstUnusedReg
Next unused register ID.
t_instrArg * rSrc2
Second source argument (or NULL if none).
int immediate
Immediate argument.
t_regID ID
The register identifier.
t_label * label
Label associated with the instruction, or NULL.
t_instrArg * rDest
Destination argument (or NULL if none).
int opcode
Instruction opcode.
void setLabelName(t_program *program, t_label *label, const char *name)
void assignLabel(t_program *program, t_label *label)
t_regID getNewRegister(t_program *program)
#define REG_INVALID
Constant used for invalid register identifiers.
bool isArray(t_symbol *symbol)
t_symbol * getSymbol(t_program *program, char *ID)
void programDump(t_program *program, FILE *fout)
void genEpilog(t_program *program)
t_label * createLabel(t_program *program)
void removeInstructionAt(t_program *program, t_listNode *instrLi)
t_symbol * createSymbol(t_program *program, char *ID, t_symbolType type, int arraySize)
char * getLabelName(t_label *label)
t_instruction * genInstruction(t_program *program, int opcode, t_regID rd, t_regID rs1, t_regID rs2, t_label *label, int immediate)
int t_regID
Type for register identifiers.
t_program * newProgram(void)
void deleteProgram(t_program *program)
@ TYPE_INT_ARRAY
‘int’ array type.
@ TYPE_INT
‘int’ scalar type.
void deleteInstruction(t_instruction *inst)
t_instrArg * newInstrArg(t_regID ID)
void setRawLabelName(t_program *program, t_label *label, const char *finalName)
void deleteLabel(t_label *lab)
void deleteInstructions(t_listNode *instructions)
t_symbol * newSymbol(char *ID, t_symbolType type, int arraySize)
void deleteLabels(t_listNode *labels)
t_instruction * newInstruction(int opcode)
void addInstruction(t_program *program, t_instruction *instr)
Program object definition and management.
Header file associated to scanner.y.
Generation of the output assembly program.
Properties of the target machine.