15 if (r >= 0 && r < program->firstUnusedReg)
17 fatalError(
"bug: invalid register identifier %d", r);
49 return genRFormatInstruction(program,
OPC_ADD, rd, rs1, rs2);
54 return genRFormatInstruction(program,
OPC_SUB, rd, rs1, rs2);
59 return genRFormatInstruction(program,
OPC_AND, rd, rs1, rs2);
64 return genRFormatInstruction(program,
OPC_OR, rd, rs1, rs2);
69 return genRFormatInstruction(program,
OPC_XOR, rd, rs1, rs2);
74 return genRFormatInstruction(program,
OPC_MUL, rd, rs1, rs2);
79 return genRFormatInstruction(program,
OPC_DIV, rd, rs1, rs2);
84 return genRFormatInstruction(program,
OPC_REM, rd, rs1, rs2);
89 return genRFormatInstruction(program,
OPC_SLL, rd, rs1, rs2);
94 return genRFormatInstruction(program,
OPC_SRL, rd, rs1, rs2);
99 return genRFormatInstruction(program,
OPC_SRA, rd, rs1, rs2);
106 return genIFormatInstruction(program,
OPC_ADDI, rd, rs1, immediate);
112 return genIFormatInstruction(program,
OPC_SUBI, rd, rs1, immediate);
118 return genIFormatInstruction(program,
OPC_ANDI, rd, rs1, immediate);
124 return genIFormatInstruction(program,
OPC_MULI, rd, rs1, immediate);
130 return genIFormatInstruction(program,
OPC_ORI, rd, rs1, immediate);
136 return genIFormatInstruction(program,
OPC_XORI, rd, rs1, immediate);
142 return genIFormatInstruction(program,
OPC_DIVI, rd, rs1, immediate);
148 return genIFormatInstruction(program,
OPC_REMI, rd, rs1, immediate);
154 return genIFormatInstruction(program,
OPC_SLLI, rd, rs1, immediate);
160 return genIFormatInstruction(program,
OPC_SRLI, rd, rs1, immediate);
166 return genIFormatInstruction(program,
OPC_SRAI, rd, rs1, immediate);
172 return genRFormatInstruction(program,
OPC_SEQ, rd, rs1, rs2);
177 return genRFormatInstruction(program,
OPC_SNE, rd, rs1, rs2);
182 return genRFormatInstruction(program,
OPC_SLT, rd, rs1, rs2);
187 return genRFormatInstruction(program,
OPC_SLTU, rd, rs1, rs2);
192 return genRFormatInstruction(program,
OPC_SGE, rd, rs1, rs2);
197 return genRFormatInstruction(program,
OPC_SGEU, rd, rs1, rs2);
202 return genRFormatInstruction(program,
OPC_SGT, rd, rs1, rs2);
207 return genRFormatInstruction(program,
OPC_SGTU, rd, rs1, rs2);
212 return genRFormatInstruction(program,
OPC_SLE, rd, rs1, rs2);
217 return genRFormatInstruction(program,
OPC_SLEU, rd, rs1, rs2);
224 return genIFormatInstruction(program,
OPC_SEQI, rd, rs1, immediate);
230 return genIFormatInstruction(program,
OPC_SNEI, rd, rs1, immediate);
236 return genIFormatInstruction(program,
OPC_SLTI, rd, rs1, immediate);
242 return genIFormatInstruction(program,
OPC_SLTIU, rd, rs1, immediate);
248 return genIFormatInstruction(program,
OPC_SGEI, rd, rs1, immediate);
254 return genIFormatInstruction(program,
OPC_SGEIU, rd, rs1, immediate);
260 return genIFormatInstruction(program,
OPC_SGTI, rd, rs1, immediate);
266 return genIFormatInstruction(program,
OPC_SGTIU, rd, rs1, immediate);
272 return genIFormatInstruction(program,
OPC_SLEI, rd, rs1, immediate);
278 return genIFormatInstruction(program,
OPC_SLEIU, rd, rs1, immediate);
292 return genBFormatInstruction(program,
OPC_BEQ, rs1, rs2, label);
298 return genBFormatInstruction(program,
OPC_BNE, rs1, rs2, label);
304 return genBFormatInstruction(program,
OPC_BLT, rs1, rs2, label);
310 return genBFormatInstruction(program,
OPC_BLTU, rs1, rs2, label);
316 return genBFormatInstruction(program,
OPC_BGE, rs1, rs2, label);
322 return genBFormatInstruction(program,
OPC_BGEU, rs1, rs2, label);
328 return genBFormatInstruction(program,
OPC_BGT, rs1, rs2, label);
334 return genBFormatInstruction(program,
OPC_BGTU, rs1, rs2, label);
340 return genBFormatInstruction(program,
OPC_BLE, rs1, rs2, label);
346 return genBFormatInstruction(program,
OPC_BLEU, rs1, rs2, label);
456 genLW(program, rRes, 0, rAddr);
475 genSW(program, reg, 0, rAddr);
482 genLI(program, rVal, val);
507 genLA(program, rAddr, label);
513 if (sizeofElem != 1) {
515 genMULI(program, rOffset, rIdx, sizeofElem);
522 genADD(program, rAddr, rAddr, rOffset);
533 genLW(program, rVal, 0, rAddr);
544 genSW(program, rVal, 0, rAddr);
552 genLI(program, rVal, val);
void validateRegisterId(t_program *program, t_regID r)
t_regID genLoadArrayAddress(t_program *program, t_symbol *array, t_regID rIdx)
Code generation functions.
void emitError(t_fileLocation loc, const char *fmt,...)
void fatalError(const char *format,...)
t_instruction * genADD(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSGTU(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genPrintCharSyscall(t_program *program, t_regID rs1)
t_instruction * genSGEI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genBNE(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genLW(t_program *program, t_regID rd, int immediate, t_regID rs1)
t_instruction * genSRA(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSGE(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genREM(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
void genStoreRegisterToVariable(t_program *program, t_symbol *var, t_regID reg)
t_instruction * genSLTI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_regID genLoadVariable(t_program *program, t_symbol *var)
t_instruction * genXORI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genECALL(t_program *program)
t_instruction * genBGTU(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genSLE(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSWGlobal(t_program *program, t_regID rs1, t_label *label, t_regID r_temp)
t_instruction * genANDI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genSUB(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
void genStoreRegisterToArrayElement(t_program *program, t_symbol *array, t_regID rIdx, t_regID rVal)
void genStoreConstantToArrayElement(t_program *program, t_symbol *array, t_regID rIdx, int val)
t_instruction * genSEQ(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSLTU(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genLI(t_program *program, t_regID rd, int immediate)
t_instruction * genSRLI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_regID genLoadArrayElement(t_program *program, t_symbol *array, t_regID rIdx)
t_instruction * genBLEU(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genOR(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSGEIU(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genMULI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genDIVI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genExit0Syscall(t_program *program)
t_instruction * genSGTI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genSLTIU(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genLWGlobal(t_program *program, t_regID rd, t_label *label)
t_instruction * genSRAI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genSEQI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genADDI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genSLLI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genSLEI(t_program *program, t_regID rd, t_regID rs1, int immediate)
void genStoreConstantToVariable(t_program *program, t_symbol *var, int val)
t_instruction * genSLEU(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genBEQ(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genXOR(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSNEI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genJ(t_program *program, t_label *label)
t_instruction * genAND(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSLT(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSUBI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genNOP(t_program *program)
t_instruction * genBGE(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genSLEIU(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genLA(t_program *program, t_regID rd, t_label *label)
t_instruction * genSRL(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genORI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genREMI(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genPrintIntSyscall(t_program *program, t_regID rs1)
t_instruction * genSGEU(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSGTIU(t_program *program, t_regID rd, t_regID rs1, int immediate)
t_instruction * genSNE(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genBLT(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genSLL(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSGT(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genReadIntSyscall(t_program *program, t_regID rd)
t_instruction * genBLTU(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genBLE(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genBGEU(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genDIV(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genBGT(t_program *program, t_regID rs1, t_regID rs2, t_label *label)
t_instruction * genMUL(t_program *program, t_regID rd, t_regID rs1, t_regID rs2)
t_instruction * genSW(t_program *program, t_regID rs2, int immediate, t_regID rs1)
t_instruction * genEBREAK(t_program *program)
t_fileLocation curFileLoc
char * ID
Symbol name (should never be a NULL pointer or an empty string "").
t_regID getNewRegister(t_program *program)
#define REG_INVALID
Constant used for invalid register identifiers.
bool isArray(t_symbol *symbol)
#define REG_0
Constant identifying a register whose value is always zero.
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.
#define TARGET_PTR_GRANULARITY
Number of bytes for each memory address.
Header file associated to scanner.y.
Properties of the target machine.