163#define FORMAT_AUTO -1
165#define FORMAT_OPIMM 1
167#define FORMAT_LOAD_GL 3
168#define FORMAT_STORE 4
169#define FORMAT_STORE_GL 5
170#define FORMAT_BRANCH 6
174#define FORMAT_SYSTEM 10
175#define FORMAT_FUNC 11
177static int opcodeToFormat(
int opcode)
266 static const char *mcRegIds[] = {
"zero",
"ra",
"sp",
"gp",
"tp",
"t0",
"t1",
267 "t2",
"s0",
"s1",
"a0",
"a1",
"a2",
"a3",
"a4",
"a5",
"a6",
"a7",
"s2",
268 "s3",
"s4",
"s5",
"s6",
"s7",
"s8",
"s9",
"s10",
"s11",
"t3",
"t4",
"t5",
272 if (regID < 0 || regID >= 32)
274 return strdup(mcRegIds[regID]);
278 return strdup(
"invalid_reg");
279 buf = calloc(24,
sizeof(
char));
280 snprintf(buf, 24,
"temp%d", regID);
304 res = snprintf(buf, bufsz,
"%s:", labelName);
306 res = snprintf(buf, bufsz,
"%s", labelName);
314 char *buf,
int bufsz,
t_instruction *instr,
bool machineRegIDs)
318 char *address = NULL;
326 address = calloc((
size_t)n + 1,
sizeof(
char));
333 int format = opcodeToFormat(instr->
opcode);
337 fatalError(
"bug: invalid instruction found in the program");
338 res = snprintf(buf, bufsz,
"%-6s %s, %s, %s", opc, rd, rs1, rs2);
342 fatalError(
"bug: invalid instruction found in the program");
343 res = snprintf(buf, bufsz,
"%-6s %s, %s, %d", opc, rd, rs1, imm);
347 fatalError(
"bug: invalid instruction found in the program");
348 res = snprintf(buf, bufsz,
"%-6s %s, %d(%s)", opc, rd, imm, rs1);
352 fatalError(
"bug: invalid instruction found in the program");
353 res = snprintf(buf, bufsz,
"%-6s %s, %s", opc, rd, address);
357 fatalError(
"bug: invalid instruction found in the program");
358 res = snprintf(buf, bufsz,
"%-6s %s, %d(%s)", opc, rs2, imm, rs1);
362 fatalError(
"bug: invalid instruction found in the program");
363 res = snprintf(buf, bufsz,
"%-6s %s, %s, %s", opc, rs1, address, rd);
367 fatalError(
"bug: invalid instruction found in the program");
368 res = snprintf(buf, bufsz,
"%-6s %s, %s, %s", opc, rs1, rs2, address);
372 fatalError(
"bug: invalid instruction found in the program");
373 res = snprintf(buf, bufsz,
"%-6s %s", opc, address);
377 fatalError(
"bug: invalid instruction found in the program");
378 res = snprintf(buf, bufsz,
"%-6s %s, %d", opc, rd, imm);
382 fatalError(
"bug: invalid instruction found in the program");
383 res = snprintf(buf, bufsz,
"%-6s %s, %s", opc, rd, address);
386 res = snprintf(buf, bufsz,
"%s", opc);
391 buf += sprintf(buf,
"%s = ", rd);
392 buf += sprintf(buf,
"%s(", opc);
394 buf += sprintf(buf,
"%s", rs1);
396 buf += sprintf(buf,
", ");
398 buf += sprintf(buf,
"%s", rs2);
399 buf += sprintf(buf,
")");
400 res = (int)(buf - buf0);
425 res = fprintf(fp,
"%-8s.global %s\n",
"", labelName);
441 if (instr->
label != NULL) {
446 if (fprintf(fp,
"%-8s", buf) < 0)
452 res = fprintf(fp,
"%-48s# %s", buf, instr->
comment);
454 res = fprintf(fp,
"%s", buf);
468 if (fprintf(fp,
"%-8s.text\n",
"") < 0)
472 while (curNode != NULL) {
474 if (curInstr == NULL)
475 fatalError(
"bug: NULL instruction found in the program");
479 if (fprintf(fp,
"\n") < 0)
482 curNode = curNode->
next;
493 if (data->
label != NULL) {
498 if (fprintf(fp,
"%-8s", buf) < 0)
503 switch (data->
type) {
511 fatalError(
"bug: invalid data type found in the program");
513 if (fprintf(fp,
".space %d", size) < 0)
526 if (fprintf(fp,
"%-8s.data\n",
"") < 0)
536 if (fprintf(fp,
"\n") < 0)
549 FILE *fp = fopen(fn,
"w");
562 if (fclose(fp) == EOF)
bool printInstruction(t_instruction *instr, FILE *fp, bool machineRegIDs)
bool writeAssembly(t_program *program, const char *fn)
char * registerIDToString(t_regID regID, bool machineRegIDs)
void fatalError(const char *format,...)
void * data
Pointer to the data associated to this node.
t_symbolType type
A valid data type.
t_listNode * instructions
List of instructions.
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.
bool global
True if the label will be defined as 'global'.
t_instrArg * rSrc1
First source argument (or NULL if none).
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.
char * getLabelName(t_label *label)
int t_regID
Type for register identifiers.
@ TYPE_INT_ARRAY
‘int’ array type.
@ TYPE_INT
‘int’ scalar type.
#define TARGET_REG_ZERO_IS_CONST
#define TARGET_PTR_GRANULARITY
Number of bytes for each memory address.
int instructionToString(char *buf, int bufsz, t_instruction *instr, bool machineRegIDs)
const char * opcodeToString(int opcode)
char * registerToString(t_instrArg *reg, bool machineRegIDs)
int labelToString(char *buf, int bufsz, t_label *label, int finalColon)
bool translateDataSegment(t_program *program, FILE *fp)
int printGlobalDeclaration(t_symbol *data, FILE *fp)
bool translateForwardDeclarations(t_program *program, FILE *fp)
bool translateCodeSegment(t_program *program, FILE *fp)
Generation of the output assembly program.
Properties of the target machine.