Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string>
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <iterator>
  8.  
  9. using namespace std;
  10.  
  11. string terminals [] = {"BOF", "BECOMES", "COMMA", "ELSE", "EOF", "EQ",
  12. "GE", "GT", "ID", "IF", "INT", "LBRACE", "LE", "LPAREN",
  13. "LT", "MINUS", "NE", "NUM", "PCT", "PLUS", "PRINTLN",
  14. "RBRACE", "RETURN", "RPAREN", "SEMI", "SLASH", "STAR", "WAIN",
  15. "WHILE", "AMP", "LBRACK", "RBRACK", "NEW", "DELETE", "NULL"};
  16.  
  17. struct Tree {
  18. string rule;
  19. vector <string> tokens;
  20. vector<Tree*> children;
  21.  
  22. ~Tree() {
  23. for(vector<Tree*>::iterator it=children.begin(); it != children.end(); it++) {
  24. delete (*it);
  25. }
  26. }
  27. };
  28.  
  29. struct symbol{
  30. string type;
  31. int address;
  32. };
  33.  
  34. struct completeSymbol
  35. {
  36. map <string, symbol> functionSymbolT;
  37. vector <string> paramlists;
  38. };
  39.  
  40. bool isTerminal(string value)
  41. {
  42. int terminalsLength = sizeof(terminals)/sizeof(terminals[0]);
  43. for (int i = 0; i < terminalsLength; i++)
  44. {
  45. if (terminals[i] == value)
  46. {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52.  
  53. void tokenizer(string value, vector <string> &t)
  54. {
  55. istringstream iss(value);
  56. copy(istream_iterator<string>(iss),
  57. istream_iterator<string>(),
  58. back_inserter(t));
  59. }
  60.  
  61. Tree* createParseTree(string value)
  62. {
  63. string line;
  64. getline(cin, line);
  65. vector <string> token;
  66. tokenizer(line, token);
  67. Tree* temp = new Tree;
  68. temp->tokens = token;
  69. token.clear();
  70.  
  71. temp->rule = line;
  72. if (!(isTerminal(temp->tokens[0])))
  73. {
  74. //cout << "heloo" << endl;
  75. for (int i = 1; i < temp->tokens.size(); i++)
  76. {
  77. temp->children.push_back(createParseTree(temp->tokens[i]));
  78. }
  79. }
  80. return temp;
  81.  
  82. }
  83.  
  84. void parameters (Tree* t, vector <string> &paramlist)
  85. {
  86. if (t->tokens[0] == "dcl")
  87. {
  88. string typeOfParam;
  89. if (t->children[0]->children.size() == 2)
  90. {
  91. typeOfParam = "int*";
  92. }
  93. else if (t->children[0]->children.size() == 1)
  94. {
  95. typeOfParam = "int";
  96. }
  97. paramlist.push_back(typeOfParam);
  98. }
  99.  
  100. if (t->children.size() != 0)
  101. {
  102. for (int i = 0; i < t->children.size(); i++)
  103. {
  104. parameters(t->children[i], paramlist);
  105. }
  106. }
  107. }
  108.  
  109. void genSymbolTable(Tree* t, map <string, completeSymbol> &s, string &functionName, vector <string> &mainParams, int &counterForMain, int &offset)
  110. {
  111. if (t->rule == "procedure INT ID LPAREN params RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE")
  112. {
  113. functionName = t->children[1]->tokens[1];
  114. completeSymbol temp1;
  115. s.insert(pair <string, completeSymbol> (functionName, temp1));
  116. offset = 0;
  117. }
  118. else if (t->rule == "procedures main")
  119. {
  120. offset = 0;
  121. functionName = "wain";
  122. completeSymbol temp2;
  123. temp2.paramlists = mainParams;
  124. s.insert(pair <string, completeSymbol> (functionName, temp2));
  125. }
  126.  
  127. if (t->tokens[0] == "dcl")
  128. {
  129. symbol temp;
  130. string variableId = t->children[1]->tokens[1];
  131. string typeOfId;
  132.  
  133. if (t->children[0]->children.size() == 2)
  134. {
  135. typeOfId = "int*";
  136. }
  137. else if (t->children[0]->children.size() == 1)
  138. {
  139. typeOfId = "int";
  140. }
  141. temp.type = typeOfId;
  142. temp.address = offset;
  143. s.find(functionName)->second.functionSymbolT.insert(std::pair<string,symbol>(variableId,temp));
  144. offset -=4;
  145. if (counterForMain < 2 && functionName == "wain")
  146. {
  147. mainParams.push_back(typeOfId);
  148. s.find(functionName)->second.paramlists = mainParams;
  149. counterForMain++;
  150. }
  151.  
  152. }
  153. else if (t->rule == "params paramlist")
  154. {
  155. vector <string> tempParam;
  156. parameters(t, tempParam);
  157. s.find(functionName)->second.paramlists = tempParam;
  158. }
  159.  
  160. if (t->children.size() != 0)
  161. {
  162. for (int i = 0; i < t->children.size(); i++)
  163. {
  164. genSymbolTable(t->children[i], s, functionName, mainParams, counterForMain, offset);
  165. }
  166. }
  167. }
  168.  
  169. string push(int r){
  170. stringstream ss;
  171. ss << "sw $" << r << ", -4($30)\n" << "sub $30, $30, $4\n";
  172. return ss.str();
  173. }
  174.  
  175. string pop(int r){
  176. stringstream ss;
  177. ss << "lw $" << r <<", 0($30)\n" << "add $30, $30, $4\n";
  178. return ss.str();
  179. }
  180.  
  181. int labelNum=-1;
  182. string getLabel(){
  183. labelNum++;
  184. stringstream ss;
  185. ss << "label" << labelNum;
  186. return ss.str();
  187. }
  188.  
  189. void prologue(map <string, completeSymbol> &s)
  190. {
  191. cout << "lis $4" << endl;
  192. cout << ".word 4" << endl;
  193. cout << "lis $11" << endl;
  194. cout << ".word 1" << endl;
  195. cout << ".import print" << endl;
  196. cout << ".import init" << endl;
  197. cout << ".import new" << endl;
  198. cout << ".import delete" << endl;
  199. cout << "sw $31, -4($30)" << endl;
  200. cout << "sub $30, $30, $4" << endl;
  201. string param2 = s.find("wain")->second.paramlists[0];
  202. if (param2 == "int")
  203. {
  204. cout << push(2) << "add $2, $0, $0\n" << "lis $5\n" << ".word init\n" << "jalr $5\n" << pop(2);
  205. }
  206. else
  207. {
  208. cout << "lis $5" << endl;
  209. cout << ".word init" << endl;
  210. cout << "jalr $5" << endl;
  211. }
  212. cout << "beq $0, $0, Fwain\n";
  213.  
  214.  
  215.  
  216. }
  217.  
  218. void epilogue()
  219. {
  220. cout << "add $30, $30, $4" << endl;
  221. cout << "lw $31, -4($30)" << endl;
  222. cout << "jr $31" << endl;
  223. }
  224.  
  225. string getType(Tree *t, map <string, completeSymbol> &s, string &functionName){
  226.  
  227. if (t->rule == "expr term")
  228. {
  229. //cout << "expr term check here" << endl;
  230. return getType(t->children[0], s, functionName);
  231. }
  232. else if (t->rule == "expr expr PLUS term")
  233. {
  234. string leftNode = getType(t->children[0], s, functionName);
  235. string rightNode = getType(t->children[2], s, functionName);
  236. if (leftNode == "int" && rightNode == "int")
  237. {
  238. return "int";
  239. }
  240. else if (leftNode == "int*" && rightNode == "int")
  241. {
  242. return "int*";
  243. }
  244. else if (leftNode == "int" && rightNode == "int*")
  245. {
  246. return "int*";
  247. }
  248. else if (leftNode == "int*" && rightNode == "int*")
  249. {
  250. return "ERROR";
  251. }
  252. }
  253. else if (t->rule == "expr expr MINUS term")
  254. {
  255. string leftNode = getType(t->children[0], s, functionName);
  256. string rightNode = getType(t->children[2], s, functionName);
  257. if (leftNode == "int" && rightNode == "int")
  258. {
  259. return "int";
  260. }
  261. else if (leftNode == "int*" && rightNode == "int")
  262. {
  263. return "int*";
  264. }
  265. else if (leftNode == "int*" && rightNode == "int*")
  266. {
  267. return "int";
  268. }
  269. else
  270. {
  271. return "ERROR";
  272. }
  273. }
  274. else if (t->rule == "term factor")
  275. {
  276. //cout << "term factor here" << endl;
  277. return getType(t->children[0], s, functionName);
  278. }
  279. else if (t->rule == "term term STAR factor")
  280. {
  281. string leftNode = getType(t->children[0], s, functionName);
  282. string rightNode = getType(t->children[2], s, functionName);
  283. if (leftNode == "int" && rightNode == "int")
  284. {
  285. return "int";
  286. }
  287. else
  288. {
  289. return "ERROR";
  290. }
  291. }
  292. else if (t->rule == "term term SLASH factor")
  293. {
  294. string leftNode = getType(t->children[0], s, functionName);
  295. string rightNode = getType(t->children[2], s, functionName);
  296. if (leftNode == "int" && rightNode == "int")
  297. {
  298. return "int";
  299. }
  300. else
  301. {
  302. return "ERROR";
  303. }
  304. }
  305. else if (t->rule == "term term PCT factor")
  306. {
  307. string leftNode = getType(t->children[0], s, functionName);
  308. string rightNode = getType(t->children[2], s, functionName);
  309. if (leftNode == "int" && rightNode == "int")
  310. {
  311. return "int";
  312. }
  313. else
  314. {
  315. return "ERROR";
  316. }
  317. }
  318. else if (t->rule == "factor ID")
  319. {
  320. if (s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1]) == s.find(functionName)->second.functionSymbolT.end())
  321. {
  322. //cerr << "adasdas" << endl;
  323. return "ERROR";
  324. }
  325. //cerr << s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type << endl;
  326. return s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type;
  327. }
  328. else if (t->rule == "factor NUM")
  329. {
  330. //cout << "factor NUM here" << endl;
  331. return "int";
  332. }
  333. else if (t->rule == "factor NULL")
  334. {
  335. return "int*";
  336. }
  337. else if (t->rule == "factor LPAREN expr RPAREN")
  338. {
  339. return getType(t->children[1], s, functionName);
  340. }
  341. else if (t->rule == "factor AMP lvalue")
  342. {
  343. string type = getType(t->children[1], s, functionName);
  344. if (type == "int")
  345. {
  346. return "int*";
  347. }
  348. else
  349. {
  350. return "ERROR";
  351. }
  352. }
  353. else if (t->rule == "factor STAR factor")
  354. {
  355. string type = getType(t->children[1],s, functionName);
  356. if (type == "int*")
  357. {
  358. return "int";
  359. }
  360. else
  361. {
  362. return "ERROR";
  363. }
  364. }
  365. else if (t->rule == "factor NEW INT LBRACK expr RBRACK")
  366. {
  367. string type = getType(t->children[3], s, functionName);
  368. if (type == "int" )
  369. {
  370. return "int*";
  371. }
  372. else
  373. {
  374. return "ERROR";
  375. }
  376. }
  377. else if (t->rule == "factor ID LPAREN RPAREN")
  378. {
  379. //cout << "factor ID LPAREN RPAREN" << endl;
  380. string callingFunction = t->children[0]->tokens[1];
  381. int paramsize = s.find(callingFunction)->second.paramlists.size();
  382. //cout << "double check" << endl;
  383. //cout << paramsize << endl;
  384. if (paramsize != 0)
  385. {
  386. return "ERROR";
  387. }
  388. else
  389. {
  390. return "int";
  391. }
  392. }
  393. else if (t->rule == "factor ID LPAREN arglist RPAREN")
  394. {
  395. string functionCall = t->children[0]->tokens[1];
  396. string checkParam = getType(t->children[2], s, functionName);
  397. vector <string> temp;
  398. vector <string> temp2 = s.find(functionCall)->second.paramlists;
  399. tokenizer(checkParam, temp);
  400. if (temp.size() != temp2.size())
  401. {
  402. return "ERROR";
  403. }
  404. else
  405. {
  406. for (int i = 0; i < temp.size(); i++)
  407. {
  408. if (temp[i] != temp2[i])
  409. {
  410. return "ERROR";
  411. }
  412. }
  413. }
  414.  
  415. return "int";
  416. }
  417. else if (t->rule == "arglist expr")
  418. {
  419. return getType(t->children[0], s, functionName);
  420. }
  421. else if (t->rule == "arglist expr COMMA arglist")
  422. {
  423. return getType(t->children[0], s, functionName) + " " + getType(t->children[2], s, functionName);
  424. }
  425. else if (t->rule == "lvalue ID")
  426. {
  427. //cerr << "adasd" << endl;
  428. if (s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1]) == s.find(functionName)->second.functionSymbolT.end())
  429. {
  430. return "ERROR";
  431. }
  432. //cerr << s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type << endl;
  433. return s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type;
  434. }
  435. else if (t->rule == "lvalue STAR factor")
  436. {
  437. string factorType = getType(t->children[1], s, functionName);
  438. if (factorType == "int*")
  439. {
  440. return "int";
  441. }
  442. else
  443. {
  444. return "ERROR";
  445. }
  446. }
  447. else if (t->rule == "lvalue LPAREN lvalue RPAREN")
  448. {
  449. return getType(t->children[1], s, functionName);
  450. }
  451. else if (t->rule == "dcl type ID")
  452. {
  453. if (t->children[0]->children.size() == 1)
  454. {
  455. return "int";
  456. }
  457. else if (t->children[0]->children.size() == 2)
  458. {
  459. return "int*";
  460. }
  461. }
  462. }
  463.  
  464. void modifySymbolTable(map <string, completeSymbol> &s)
  465. {
  466. for (map<string, completeSymbol>::iterator i = s.begin(); i!= s.end(); i++)
  467. {
  468. if (i->first != "wain")
  469. {
  470. int paramSize = i->second.paramlists.size() * 4;
  471. for (map<string,symbol>::iterator i2 = i->second.functionSymbolT.begin(); i2 != i->second.functionSymbolT.end(); i2++)
  472. {
  473. i2->second.address += paramSize;
  474.  
  475. }
  476.  
  477.  
  478. }
  479. }
  480.  
  481. }
  482. string CodeGen(Tree* t, string &functionName, map <string, completeSymbol> &s)
  483. {
  484. if (t->rule == "procedure INT ID LPAREN params RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE")
  485. {
  486. functionName = t->children[1]->tokens[1];
  487. //cout << "F" << functionName << ":\n";
  488. string z = "F" + functionName + ":\n";
  489. string a = CodeGen(t->children[6], functionName, s);
  490. string b = CodeGen(t->children[7], functionName, s);
  491. string c = CodeGen(t->children[9], functionName, s);
  492. string d = "jr $31\n";
  493. return z + a + b + c + d;
  494. }
  495. else if (t->rule == "procedures procedure procedures")
  496. {
  497. string a = CodeGen(t->children[0], functionName, s);
  498. string b = CodeGen(t->children[1], functionName, s);
  499. return a + b;
  500. }
  501. else if (t->rule == "procedures main")
  502. {
  503. functionName = "wain";
  504. //cout << "Fwain:\n";
  505. string a = CodeGen(t->children[0], functionName, s);
  506. return string("Fwain:\n") + string("sub $29, $30, $4\n")+ a + string("add $30, $29, $4\n");
  507. }
  508. else if (t->rule == "main INT WAIN LPAREN dcl COMMA dcl RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE")
  509. {
  510. string a = CodeGen(t->children[8], functionName, s);
  511. string b = CodeGen(t->children[9], functionName, s);
  512. string c = CodeGen(t->children[11], functionName, s);
  513. return push(1) + push(2) + a + b + c;
  514.  
  515. }
  516. else if (t->tokens[0] == "dcls")
  517. {
  518. if (t->rule == "dcls")
  519. {
  520. return "";
  521. }
  522. else if (t->rule == "dcls dcls dcl BECOMES NUM SEMI")
  523. {
  524. string a = CodeGen(t->children[0], functionName, s);
  525. string b = CodeGen(t->children[3],functionName, s);
  526. return a + b + push(3);
  527. }
  528. else if (t->rule == "dcls dcls dcl BECOMES NULL SEMI")
  529. {
  530. string a = CodeGen(t->children[0], functionName, s);
  531. string b = CodeGen(t->children[3], functionName, s);
  532. return a + b + push(3);
  533. }
  534. }
  535. else if (t->tokens[0] == "statements")
  536. {
  537. if (t->rule == "statements")
  538. {
  539. return "";
  540. }
  541. else if (t->rule == "statements statements statement")
  542. {
  543. string a = CodeGen(t->children[0], functionName, s);
  544. string b = CodeGen(t->children[1], functionName, s);
  545. return a + b;
  546. }
  547. }
  548. else if (t->tokens[0] == "statement")
  549. {
  550. if (t->rule == "statement lvalue BECOMES expr SEMI")
  551. {
  552. stringstream ss;
  553. if (t->children[0]->rule == "lvalue STAR factor")
  554. {
  555. string a = CodeGen(t->children[0], functionName, s);
  556. string b = CodeGen(t->children[2], functionName, s);
  557. ss << a << "add $6, $3, $0\n" << b << "sw $3, 0($6)\n";
  558. return ss.str();
  559. }
  560. else
  561. {
  562. //cerr << "statement with lvalue becoming ID" << endl;
  563. string id = CodeGen(t->children[0], functionName, s);
  564. int offset = s.find(functionName)->second.functionSymbolT.find(id)->second.address;
  565. string a = CodeGen(t->children[2], functionName, s);
  566. ss << a << "sw $3, " << offset << "($29)\n";
  567. return ss.str();
  568. }
  569.  
  570. }
  571. else if (t->rule == "statement IF LPAREN test RPAREN LBRACE statements RBRACE ELSE LBRACE statements RBRACE")
  572. {
  573. string label1 = getLabel();
  574. string label2 = getLabel();
  575. stringstream ss;
  576. string a = CodeGen(t->children[2], functionName, s);
  577. string b = CodeGen(t->children[9], functionName, s);
  578. string c = CodeGen(t->children[5], functionName, s);
  579. ss << a << "beq $3, $11, " << label1 << endl
  580. << b << "beq $0, $0, " << label2 << endl
  581. << label1 << ":\n" << c << label2 << ":\n";
  582. return ss.str();
  583. }
  584. else if (t->rule == "statement WHILE LPAREN test RPAREN LBRACE statements RBRACE")
  585. {
  586. string loop = getLabel();
  587. string loopEx = getLabel();
  588. string a = CodeGen(t->children[2], functionName, s);
  589. string b = CodeGen(t->children[5], functionName, s);
  590. stringstream ss;
  591. ss << loop <<":\n" << a << "beq $3, $0, "<< loopEx << endl << b << "beq $0, $0, " << loop << endl << loopEx <<":\n";
  592. return ss.str();
  593. }
  594. else if (t->rule == "statement PRINTLN LPAREN expr RPAREN SEMI")
  595. {
  596. stringstream ss;
  597. string a = CodeGen(t->children[2], functionName, s);
  598. ss << a << "add $1, $3, $0\n" << push(31) << "lis $5\n" << ".word print\n" << "jalr $5\n" << pop(31);
  599. return ss.str();
  600. }
  601. else if (t->rule == "statement DELETE LBRACK RBRACK expr SEMI")
  602. {
  603. stringstream ss;
  604. /*
  605. ss << CodeGen(t->children[3], functionName, s) << "add $1, $3, $0\n" << push(31) << "lis $5\n" << ".word delete\n" << "jalr $5\n" << pop(31);
  606. return ss.str();
  607. */
  608. string a = CodeGen(t->children[3], functionName, s);
  609. ss << a << "add $1, $3, $0\n" << "beq $1, $11, 7\n" << push(31) << "lis $5\n" << ".word delete\n" << "jalr $5\n" << pop(31);
  610. return ss.str();
  611. /* code (expr), add $1, $3, $0, beq $1, $11, 7, push($31), lis $5, .word delete, jalr $5, pop($31) */
  612. }
  613. }
  614. else if (t->tokens[0] == "dcl")
  615. {
  616. return CodeGen(t->children[1], functionName, s);
  617. }
  618. else if (t->tokens[0] == "expr")
  619. {
  620. //cout << type1 << endl;
  621. //cout << type2 << endl;
  622. if (t->rule == "expr term")
  623. {
  624. return CodeGen(t->children[0], functionName, s);
  625. }
  626. else
  627. {
  628. string type1 = getType(t->children[0], s, functionName);
  629. string type2 = getType(t->children[2], s, functionName);
  630. if (type1 == "int" && type2 == "int")
  631. {
  632. if(t->tokens[2] == "PLUS")
  633. {
  634. string a = CodeGen(t->children[0], functionName, s);
  635. string b = CodeGen(t->children[2], functionName, s);
  636. return a + push(3) + b + pop(5) +"add $3, $3, $5\n";
  637. }
  638. else if(t->tokens[2] == "MINUS")
  639. {
  640. string a = CodeGen(t->children[0], functionName, s);
  641. string b = CodeGen(t->children[2], functionName, s);
  642. return a + push(3) + b + pop(5) + "sub $3, $5, $3\n";
  643. }
  644. }
  645. else if (type1 == "int*" && type2 == "int")
  646. {
  647. stringstream ss;
  648. if (t->tokens[2] == "PLUS")
  649. {
  650. string a = CodeGen(t->children[0], functionName, s);
  651. string b = CodeGen(t->children[2], functionName, s);
  652. ss << a << push(3) << b << pop(5) << "mult $3, $4\n" << "mflo $3\n" << "add $3, $5, $3\n";
  653. }
  654. else if (t->tokens[2] == "MINUS")
  655. {
  656. string a = CodeGen(t->children[0], functionName, s);
  657. string b = CodeGen(t->children[2], functionName, s);
  658. ss << a << push(3) << b
  659. << pop(5) << "mult $3, $4\n" << "mflo $3\n" << "sub $3, $5, $3\n";
  660. }
  661.  
  662. return ss.str();
  663. }
  664. else if (type1 == "int" && type2 == "int*")
  665. {
  666. stringstream ss;
  667. if (t->tokens[2] == "PLUS")
  668. {
  669. string a = CodeGen(t->children[0], functionName, s);
  670. string b = CodeGen(t->children[2], functionName, s);
  671. ss << a << push(3) << b
  672. << pop(5) << "mult $5, $4\n" << "mflo $5\n" << "add $3, $5, $3\n";
  673. }
  674.  
  675. return ss.str();
  676.  
  677. }
  678. else if (type1 == "int*" && type2 == "int*")
  679. {
  680. stringstream ss;
  681. if (t->tokens[2] == "MINUS")
  682. {
  683. string a = CodeGen(t->children[0], functionName, s);
  684. string b = CodeGen(t->children[2], functionName, s);
  685. ss << a << push(3) << b
  686. << pop(5) << "sub $3, $5, $3\n" << "div $3, $4\n" << "mflo $3\n";
  687. }
  688.  
  689. return ss.str();
  690. }
  691. }
  692.  
  693.  
  694. }
  695. else if (t->tokens[0] == "term")
  696. {
  697. if (t->rule == "term factor")
  698. {
  699. return CodeGen(t->children[0], functionName, s);
  700. }
  701. else if (t->tokens[2] == "STAR")
  702. {
  703. //cerr << "term star factor" << endl;
  704. string a = CodeGen(t->children[0], functionName, s);
  705. string b = CodeGen(t->children[2], functionName, s);
  706. return a + push(3) + b + pop(5) + "mult $3, $5\n" + "mflo $3\n";
  707. }
  708. else if (t->tokens[2] == "SLASH")
  709. {
  710. string a = CodeGen(t->children[0], functionName, s);
  711. string b = CodeGen(t->children[2], functionName, s);
  712. return a + push(3) + b + pop(5) + "div $5, $3\n" + "mflo $3\n";
  713. }
  714. else if (t->tokens[2] == "PCT")
  715. {
  716. string a = CodeGen(t->children[0], functionName, s);
  717. string b = CodeGen(t->children[2], functionName, s);
  718. return a + push(3) + b + pop(5) + "div $5, $3\n" + "mfhi $3\n";
  719. }
  720.  
  721. }
  722. else if (t->tokens[0] == "factor")
  723. {
  724. if (t->rule == "factor ID")
  725. {
  726. //cerr << "factor ID" << endl;
  727. return CodeGen(t->children[0], functionName, s);
  728. }
  729. else if (t->rule == "factor LPAREN expr RPAREN")
  730. {
  731. return CodeGen(t->children[1], functionName, s);
  732. }
  733. else if (t->rule == "factor NUM")
  734. {
  735. return CodeGen(t->children[0], functionName, s);
  736. }
  737. else if (t->rule == "factor STAR factor")
  738. {
  739. //cerr << "factor STAR factor" << endl;
  740. stringstream ss;
  741. string a = CodeGen(t->children[1], functionName, s);
  742. ss << a << "lw $3, 0($3)\n";
  743. return ss.str();
  744. }
  745. else if (t->rule == "factor AMP lvalue")
  746. {
  747. if (t->children[1]->rule == "lvalue ID")
  748. {
  749. string variableName = t->children[1]->children[0]->tokens[1];
  750. int offset = s.find(functionName)->second.functionSymbolT.find(variableName)->second.address;
  751. stringstream ss;
  752. ss << "lis $3\n" << ".word " << offset << "\n" << "add $3, $3, $29\n";
  753. return ss.str();
  754. }
  755. else if (t->children[1]->rule == "lvalue STAR factor")
  756. {
  757. //cerr << "lvalue star factpr" << endl;
  758. return CodeGen(t->children[1], functionName, s);
  759. }
  760. else if (t->children[1]->rule == "lvalue LPAREN lvalue RPAREN")
  761. {
  762. return CodeGen(t->children[1], functionName, s);
  763. }
  764. }
  765. else if (t->rule == "factor NULL")
  766. {
  767. return CodeGen(t->children[0], functionName, s);
  768. }
  769. else if (t->rule == "factor NEW INT LBRACK expr RBRACK")
  770. {
  771. stringstream ss;
  772. /*
  773. ss << CodeGen(t->children[3], functionName, s) << "add $1, $3, $0\n" << push(31) << "lis $5\n" << ".word new\n" << "jalr $5\n" << pop(31);
  774. return ss.str();
  775. */
  776. string a = CodeGen(t->children[3], functionName, s);
  777. ss << a << "add $1, $3, $0\n" << push(31) << "lis $5\n" << ".word new\n" << "jalr $5\n" << pop(31) << "bne $3, $0, 1\n" << "add $3, $11, $0\n";
  778. return ss.str();
  779. /* add bne $3, $0, 1, add $3, $11, $0*/
  780. }
  781. else if (t->rule == "factor ID LPAREN RPAREN")
  782. {
  783. string callingFunction = t->children[0]->tokens[1];
  784. /*string restoreFunction = functionName;
  785. string callingFunction = t->children[0]->tokens[1];
  786. functionName = callingFunction;
  787. stringstream ss;*/
  788. stringstream ss;
  789. ss << push(29) << push(31) << "sub $29, $30, $4\n" << "lis $5\n" << ".word " << "F" << callingFunction << "\n" << "jalr $5\n"
  790. << "add $30, $29, $4\n" << pop(31) << pop(29);
  791. //functionName = restoreFunction;
  792. return ss.str();
  793. }
  794. else if (t->rule == "factor ID LPAREN arglist RPAREN")
  795. {
  796. //string restoreFunction = functionName;
  797. string callingFunction = t->children[0]->tokens[1];
  798. int paramSize = s.find(callingFunction)->second.paramlists.size() * 4;
  799. string a = CodeGen(t->children[2], functionName, s);
  800. //functionName = calllingFunction;
  801. stringstream ss;
  802. ss << push(29) << push(31) << a << "sub $29, $30, $4\n" << "lis $18\n" << ".word " << paramSize << "\n"
  803. << "add $29, $29, $18\n" << "lis $5\n" << ".word " << "F" << callingFunction << "\n" << "jalr $5\n"
  804. << "add $30, $29, $4\n" << pop(31) << pop(29);
  805. //functionName = restoreFunction;
  806. return ss.str();
  807. }
  808. }
  809. else if (t->rule == "arglist expr")
  810. {
  811. string a = CodeGen(t->children[0], functionName, s);
  812. return a + push(3);
  813. }
  814. else if (t->rule == "arglist expr COMMA arglist")
  815. {
  816. string a = CodeGen(t->children[0], functionName, s);
  817. string b = CodeGen(t->children[2], functionName, s);
  818. return a + push(3) + b ;
  819. }
  820. else if (t->tokens[0] == "ID")
  821. {
  822. string variableName = t->tokens[1];
  823. int paramOffSet = s.find(functionName)->second.functionSymbolT.find(variableName)->second.address;
  824. stringstream ss;
  825. ss << "lw $3, " << paramOffSet <<"($29)\n";
  826. return ss.str();
  827.  
  828. }
  829. else if (t->tokens[0] == "NUM")
  830. {
  831. string number = t->tokens[1];
  832. return string("lis $3\n") + string (".word ") + number + "\n";
  833. }
  834. else if (t->tokens[0] == "NULL")
  835. {
  836. return string("lis $3\n") + string(".word 1\n");
  837. }
  838. else if (t->tokens[0] == "lvalue")
  839. {
  840. if (t->rule == "lvalue ID")
  841. {
  842. return t->children[0]->tokens[1];
  843. }
  844. else if (t->rule == "lvalue LPAREN lvalue RPAREN")
  845. {
  846. return CodeGen(t->children[1], functionName, s);
  847. }
  848. else if (t->rule == "lvalue STAR factor")
  849. {
  850. return CodeGen(t->children[1], functionName, s);
  851. }
  852. }
  853. else if (t->tokens[0] == "test")
  854. {
  855. string type1 = getType(t->children[0], s, functionName);
  856. string type2 = getType(t->children[2], s, functionName);
  857. stringstream ss;
  858. string a = CodeGen(t->children[0], functionName, s);
  859. string b = CodeGen(t->children[2], functionName, s);
  860. ss << a << push(3) << b << pop(5);
  861. if (type1 == "int" && type2 == "int")
  862. {
  863. if (t->rule == "test expr LT expr")
  864. {
  865. ss << "slt $3, $5, $3" << endl;
  866. }
  867. else if (t->rule == "test expr EQ expr")
  868. {
  869. ss << "slt $6, $3, $5\n" << "slt $7, $5, $3\n" << "add $3, $6, $7\n" << "sub $3, $11, $3" << endl;
  870. }
  871. else if (t->rule == "test expr NE expr")
  872. {
  873. ss << "slt $6, $3, $5\n" << "slt $7, $5, $3\n" << "add $3, $6, $7" << endl;
  874. }
  875. else if (t->rule == "test expr LE expr")
  876. {
  877. ss << "slt $3, $3, $5\n" << "slt $3, $3, $11" << endl;
  878. }
  879. else if (t->rule == "test expr GE expr")
  880. {
  881. ss << "slt $3, $5, $3\n" << "slt $3, $3, $11" <<endl;
  882. }
  883. else if (t->rule == "test expr GT expr")
  884. {
  885. ss << "slt $3, $3, $5" << endl;
  886. }
  887.  
  888. return ss.str();
  889. }
  890. else if (type1 == "int*" && type2 == "int*")
  891. {
  892. if (t->rule == "test expr LT expr")
  893. {
  894. ss << "sltu $3, $5, $3" << endl;
  895. }
  896. else if (t->rule == "test expr EQ expr")
  897. {
  898. ss << "sltu $6, $3, $5\n" << "sltu $7, $5, $3\n" << "add $3, $6, $7\n" << "sub $3, $11, $3" << endl;
  899. }
  900. else if (t->rule == "test expr NE expr")
  901. {
  902. ss << "sltu $6, $3, $5\n" << "sltu $7, $5, $3\n" << "add $3, $6, $7" << endl;
  903. }
  904. else if (t->rule == "test expr LE expr")
  905. {
  906. ss << "sltu $3, $3, $5\n" << "sltu $3, $3, $11" << endl;
  907. }
  908. else if (t->rule == "test expr GE expr")
  909. {
  910. ss << "sltu $3, $5, $3\n" << "sltu $3, $3, $11" <<endl;
  911. }
  912. else if (t->rule == "test expr GT expr")
  913. {
  914. ss << "sltu $3, $3, $5" << endl;
  915. }
  916.  
  917. return ss.str();
  918. }
  919.  
  920. }
  921.  
  922. }
  923.  
  924.  
  925. void outputParseTree(Tree* &root)
  926. {
  927. cout << root->rule << endl;
  928. for (int i = 0; i < root->children.size(); i++)
  929. {
  930. outputParseTree(root->children[i]);
  931. }
  932. return;
  933. }
  934.  
  935. int main()
  936. {
  937. int offset = 0;
  938. int counterForMainArgs = 0;
  939. vector <string> mainParams;
  940. map <string, completeSymbol> globalST;
  941. string functionname = "";
  942. Tree* parseTree = createParseTree("S");
  943. //outputParseTree(parseTree);
  944. genSymbolTable(parseTree, globalST, functionname, mainParams, counterForMainArgs, offset);
  945. //outputParseTree(parseTree);
  946.  
  947.  
  948. prologue(globalST);
  949. cout << CodeGen(parseTree->children[1], functionname, globalST);
  950. epilogue();
  951. delete parseTree;
  952. return 0;
  953. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement