Advertisement
Guest User

Untitled

a guest
May 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. //munch.cpp
  2. #include "Munch.h"
  3. #include "Instruction.h"
  4. #include "Registers.h"
  5.  
  6. #include <cassert>
  7. #include <string>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. static InstructionList selectedInstructions;
  14.  
  15. /*
  16. * Use this function to translate integer value to string
  17. */
  18. string toString(int value)
  19. {
  20. string retVal = "";
  21. stringstream s;
  22. s << value;
  23. s >> retVal;
  24. return retVal;
  25. }
  26.  
  27. /*
  28. * Add the instruction to the list of selected instructions
  29. */
  30. void emit(Instruction* instr)
  31. {
  32. selectedInstructions.push_back(instr);
  33. }
  34.  
  35.  
  36. InstructionList& getInstructionList()
  37. {
  38. return selectedInstructions;
  39. }
  40.  
  41.  
  42. void munchStm(Statement* statement)
  43. {
  44. switch (statement->getType())
  45. {
  46. case Statement::MOVE_STM:
  47. {
  48. S_Move* moveStm = static_cast<S_Move*>(statement);
  49.  
  50. Expression* destExp = moveStm->getLeftExp();
  51. Expression* srcExp = moveStm->getRightExp();
  52.  
  53. // if the destination expression (left expression) is memory access
  54. // then the statement represents storing to memory
  55. if (destExp->getType() == Expression::MEM_EXP)
  56. {
  57. // Here goes the code for sw instruction
  58. // sw is represented by T_MOVE with T_MEM as destination, and any expression as source
  59. // consider T_MOVE with all other types of destination expression as not supported ( just call assert(0) )
  60. }
  61. else
  62. {
  63. assert(0);
  64. }
  65. }
  66. break;
  67.  
  68.  
  69. case Statement::SEQ_STM:
  70. {
  71. S_Seq* stmSeq = static_cast<S_Seq*>(statement);
  72.  
  73. Statement* left = stmSeq->getLeftStm();
  74. Statement* right = stmSeq->getRightStm();
  75.  
  76. munchStm(left);
  77. munchStm(right);
  78. }
  79. break;
  80.  
  81.  
  82. case Statement::EXP_STM:
  83. {
  84. S_Exp* stmExp = static_cast<S_Exp*>(statement);
  85.  
  86. Expression* expression = stmExp->getExp();
  87.  
  88. munchExp(expression);
  89. }
  90. break;
  91.  
  92.  
  93. default:
  94. assert(0);
  95. }
  96. }
  97.  
  98.  
  99. Reg munchExp(Expression* expression)
  100. {
  101. switch (expression->getType())
  102. {
  103. case Expression::MEM_EXP:
  104. {
  105. //2
  106. // Here goes the code for lw instruction
  107. E_Mem* memExp = static_cast<E_Mem*>(expression);
  108. if (memExp->getExp()->getType() == E_Binop::BINOP_EXP)
  109. {
  110. E_Binop* binopExp = static_cast<E_Binop*>(memExp->getExp());
  111. Expression* binopLeftExp = binopExp->getLeftExp();
  112. Expression* binopRightExp = binopExp->getRightExp();
  113. if (binopRightExp->getType() == Expression::CONST_EXP)
  114. {
  115. E_Const* constExp = static_cast<E_Const*>(binopRightExp);
  116.  
  117. string instructionString = "lw `d " + toString(constExp->getValue()) + "(`s)";
  118.  
  119. Reg sourceReg = munchExp(binopLeftExp);
  120. Reg dstReg = getNewReg();
  121.  
  122. Instruction* instr = new Instruction(instructionString);
  123. instr->getDst().push_back(dstReg);
  124. instr->getSrc().push_back(sourceReg);
  125.  
  126. emit(instr);
  127.  
  128. return dstReg;
  129. }
  130. else if (binopLeftExp->getType() == Expression::CONST_EXP)
  131. {
  132. E_Const* constExp = static_cast<E_Const*>(binopLeftExp);
  133.  
  134. string instructionString = "lw `d " + toString(constExp->getValue()) + "(`s)";
  135.  
  136. Reg sourceReg = munchExp(binopRightExp);
  137. Reg dstReg = getNewReg();
  138.  
  139. Instruction* instr = new Instruction(instructionString);
  140. instr->getDst().push_back(dstReg);
  141. instr->getSrc().push_back(sourceReg);
  142.  
  143. emit(instr);
  144.  
  145. return dstReg;
  146. }
  147. }
  148. else if (memExp->getExp()->getType() == E_Mem::CONST_EXP)
  149. {
  150. E_Const* constExp = static_cast<E_Const*>(memExp->getExp());
  151. string instructionString = "lw `d " + toString(constExp->getValue()) + "(`s)";
  152. Reg sourceReg = 1;
  153. Reg dstReg = getNewReg();
  154. Instruction* instr = new Instruction(instructionString);
  155. instr->getDst().push_back(dstReg);
  156. instr->getSrc().push_back(sourceReg);
  157. emit(instr);
  158. return dstReg;
  159.  
  160. }
  161. else
  162. {
  163. Reg sourceReg = munchExp(memExp->getExp());
  164. Reg dstReg = getNewReg();
  165. string instructionString = "lw `d, 0(`s)";
  166. Instruction* instr = new Instruction(instructionString);
  167. instr->getDst().push_back(dstReg);
  168. instr->getSrc().push_back(sourceReg);
  169. emit(instr);
  170. }
  171. break;
  172. }
  173. case Expression::BINOP_EXP:
  174. {
  175. E_Binop* binopExp = static_cast<E_Binop*>(expression);
  176.  
  177. if (binopExp->getOperType() == E_Binop::PLUS_OP)
  178. {
  179. Expression* binopLeftExp = binopExp->getLeftExp();
  180. Expression* binopRightExp = binopExp->getRightExp();
  181.  
  182. if (binopRightExp->getType() == Expression::CONST_EXP)
  183. {
  184. E_Const* constExp = static_cast<E_Const*>(binopRightExp);
  185.  
  186. string instructionString = "addi `d,`s, " + toString(constExp->getValue());
  187.  
  188. Reg sourceReg = munchExp(binopLeftExp);
  189. Reg dstReg = getNewReg(); //temp
  190.  
  191. Instruction* instr = new Instruction(instructionString);
  192. instr->getDst().push_back(dstReg);
  193. instr->getSrc().push_back(sourceReg);
  194.  
  195. emit(instr);
  196.  
  197. return dstReg;
  198. }
  199. else if (binopLeftExp->getType() == Expression::CONST_EXP)
  200. {
  201. E_Const* constExp = static_cast<E_Const*>(binopLeftExp);
  202.  
  203. string instructionString = "addi `d,`s, " + toString(constExp->getValue());
  204.  
  205. Reg sourceReg = munchExp(binopRightExp);
  206. Reg dstReg = getNewReg();
  207.  
  208. Instruction* instr = new Instruction(instructionString);
  209. instr->getDst().push_back(dstReg);
  210. instr->getSrc().push_back(sourceReg);
  211.  
  212. emit(instr);
  213.  
  214. return dstReg;
  215. }
  216. else
  217. {
  218. E_Const* constExp = static_cast<E_Const*>(binopLeftExp);
  219. E_Const* constExp2 = static_cast<E_Const*>(binopRightExp);
  220.  
  221. string instructionString = "addi `d,`s,`s ";
  222.  
  223. Reg sourceReg = munchExp(binopRightExp);
  224. Reg sourceReg2 = munchExp(binopLeftExp);
  225. Reg dstReg = getNewReg();
  226.  
  227. Instruction* instr = new Instruction(instructionString);
  228. instr->getDst().push_back(dstReg);
  229. instr->getSrc().push_back(sourceReg2);
  230. instr->getSrc().push_back(sourceReg);
  231.  
  232. emit(instr);
  233.  
  234. return dstReg;
  235. }
  236. }
  237. else
  238. {
  239. assert(0);
  240. return -1;
  241. }
  242. }
  243.  
  244. case Expression::REG_EXP:
  245. {
  246. E_Reg* regExp = static_cast<E_Reg*>(expression);
  247. return regExp->getRegId();
  248. }
  249.  
  250. case Expression::CONST_EXP:
  251. {
  252. E_Const* constExp = static_cast<E_Const*>(expression);
  253.  
  254. string instructionString = "li `d, " + toString(constExp->getValue());
  255.  
  256. Reg dstReg = getNewReg();
  257.  
  258. Instruction* instr = new Instruction(instructionString);
  259. instr->getDst().push_back(dstReg);
  260.  
  261. emit(instr);
  262.  
  263. return dstReg;
  264. }
  265.  
  266. case Expression::ESEQ_EXP:
  267. {
  268. E_Eseq* eseqExp = static_cast<E_Eseq*>(expression);
  269.  
  270. Expression* eseqLeft = eseqExp->getLeftExp();
  271. Statement* eseqRight = eseqExp->getRightStm();
  272.  
  273. Reg dstReg = munchExp(eseqLeft);
  274. munchStm(eseqRight);
  275.  
  276. return dstReg;
  277. }
  278.  
  279. default:
  280. assert(0);
  281. return -1;
  282. }
  283.  
  284. //assert(0);
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement