Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //munch.cpp
- #include "Munch.h"
- #include "Instruction.h"
- #include "Registers.h"
- #include <cassert>
- #include <string>
- #include <sstream>
- using namespace std;
- static InstructionList selectedInstructions;
- /*
- * Use this function to translate integer value to string
- */
- string toString(int value)
- {
- string retVal = "";
- stringstream s;
- s << value;
- s >> retVal;
- return retVal;
- }
- /*
- * Add the instruction to the list of selected instructions
- */
- void emit(Instruction* instr)
- {
- selectedInstructions.push_back(instr);
- }
- InstructionList& getInstructionList()
- {
- return selectedInstructions;
- }
- void munchStm(Statement* statement)
- {
- switch (statement->getType())
- {
- case Statement::MOVE_STM:
- {
- S_Move* moveStm = static_cast<S_Move*>(statement);
- Expression* destExp = moveStm->getLeftExp();
- Expression* srcExp = moveStm->getRightExp();
- // if the destination expression (left expression) is memory access
- // then the statement represents storing to memory
- if (destExp->getType() == Expression::MEM_EXP)
- {
- // Here goes the code for sw instruction
- // sw is represented by T_MOVE with T_MEM as destination, and any expression as source
- // consider T_MOVE with all other types of destination expression as not supported ( just call assert(0) )
- }
- else
- {
- assert(0);
- }
- }
- break;
- case Statement::SEQ_STM:
- {
- S_Seq* stmSeq = static_cast<S_Seq*>(statement);
- Statement* left = stmSeq->getLeftStm();
- Statement* right = stmSeq->getRightStm();
- munchStm(left);
- munchStm(right);
- }
- break;
- case Statement::EXP_STM:
- {
- S_Exp* stmExp = static_cast<S_Exp*>(statement);
- Expression* expression = stmExp->getExp();
- munchExp(expression);
- }
- break;
- default:
- assert(0);
- }
- }
- Reg munchExp(Expression* expression)
- {
- switch (expression->getType())
- {
- case Expression::MEM_EXP:
- {
- //2
- // Here goes the code for lw instruction
- E_Mem* memExp = static_cast<E_Mem*>(expression);
- if (memExp->getExp()->getType() == E_Binop::BINOP_EXP)
- {
- E_Binop* binopExp = static_cast<E_Binop*>(memExp->getExp());
- Expression* binopLeftExp = binopExp->getLeftExp();
- Expression* binopRightExp = binopExp->getRightExp();
- if (binopRightExp->getType() == Expression::CONST_EXP)
- {
- E_Const* constExp = static_cast<E_Const*>(binopRightExp);
- string instructionString = "lw `d " + toString(constExp->getValue()) + "(`s)";
- Reg sourceReg = munchExp(binopLeftExp);
- Reg dstReg = getNewReg();
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- instr->getSrc().push_back(sourceReg);
- emit(instr);
- return dstReg;
- }
- else if (binopLeftExp->getType() == Expression::CONST_EXP)
- {
- E_Const* constExp = static_cast<E_Const*>(binopLeftExp);
- string instructionString = "lw `d " + toString(constExp->getValue()) + "(`s)";
- Reg sourceReg = munchExp(binopRightExp);
- Reg dstReg = getNewReg();
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- instr->getSrc().push_back(sourceReg);
- emit(instr);
- return dstReg;
- }
- }
- else if (memExp->getExp()->getType() == E_Mem::CONST_EXP)
- {
- E_Const* constExp = static_cast<E_Const*>(memExp->getExp());
- string instructionString = "lw `d " + toString(constExp->getValue()) + "(`s)";
- Reg sourceReg = 1;
- Reg dstReg = getNewReg();
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- instr->getSrc().push_back(sourceReg);
- emit(instr);
- return dstReg;
- }
- else
- {
- Reg sourceReg = munchExp(memExp->getExp());
- Reg dstReg = getNewReg();
- string instructionString = "lw `d, 0(`s)";
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- instr->getSrc().push_back(sourceReg);
- emit(instr);
- }
- break;
- }
- case Expression::BINOP_EXP:
- {
- E_Binop* binopExp = static_cast<E_Binop*>(expression);
- if (binopExp->getOperType() == E_Binop::PLUS_OP)
- {
- Expression* binopLeftExp = binopExp->getLeftExp();
- Expression* binopRightExp = binopExp->getRightExp();
- if (binopRightExp->getType() == Expression::CONST_EXP)
- {
- E_Const* constExp = static_cast<E_Const*>(binopRightExp);
- string instructionString = "addi `d,`s, " + toString(constExp->getValue());
- Reg sourceReg = munchExp(binopLeftExp);
- Reg dstReg = getNewReg(); //temp
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- instr->getSrc().push_back(sourceReg);
- emit(instr);
- return dstReg;
- }
- else if (binopLeftExp->getType() == Expression::CONST_EXP)
- {
- E_Const* constExp = static_cast<E_Const*>(binopLeftExp);
- string instructionString = "addi `d,`s, " + toString(constExp->getValue());
- Reg sourceReg = munchExp(binopRightExp);
- Reg dstReg = getNewReg();
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- instr->getSrc().push_back(sourceReg);
- emit(instr);
- return dstReg;
- }
- else
- {
- E_Const* constExp = static_cast<E_Const*>(binopLeftExp);
- E_Const* constExp2 = static_cast<E_Const*>(binopRightExp);
- string instructionString = "addi `d,`s,`s ";
- Reg sourceReg = munchExp(binopRightExp);
- Reg sourceReg2 = munchExp(binopLeftExp);
- Reg dstReg = getNewReg();
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- instr->getSrc().push_back(sourceReg2);
- instr->getSrc().push_back(sourceReg);
- emit(instr);
- return dstReg;
- }
- }
- else
- {
- assert(0);
- return -1;
- }
- }
- case Expression::REG_EXP:
- {
- E_Reg* regExp = static_cast<E_Reg*>(expression);
- return regExp->getRegId();
- }
- case Expression::CONST_EXP:
- {
- E_Const* constExp = static_cast<E_Const*>(expression);
- string instructionString = "li `d, " + toString(constExp->getValue());
- Reg dstReg = getNewReg();
- Instruction* instr = new Instruction(instructionString);
- instr->getDst().push_back(dstReg);
- emit(instr);
- return dstReg;
- }
- case Expression::ESEQ_EXP:
- {
- E_Eseq* eseqExp = static_cast<E_Eseq*>(expression);
- Expression* eseqLeft = eseqExp->getLeftExp();
- Statement* eseqRight = eseqExp->getRightStm();
- Reg dstReg = munchExp(eseqLeft);
- munchStm(eseqRight);
- return dstReg;
- }
- default:
- assert(0);
- return -1;
- }
- //assert(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement