Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <string>
- #include <sstream>
- #include <algorithm>
- #include <iterator>
- using namespace std;
- string terminals [] = {"BOF", "BECOMES", "COMMA", "ELSE", "EOF", "EQ",
- "GE", "GT", "ID", "IF", "INT", "LBRACE", "LE", "LPAREN",
- "LT", "MINUS", "NE", "NUM", "PCT", "PLUS", "PRINTLN",
- "RBRACE", "RETURN", "RPAREN", "SEMI", "SLASH", "STAR", "WAIN",
- "WHILE", "AMP", "LBRACK", "RBRACK", "NEW", "DELETE", "NULL"};
- struct Tree {
- string rule;
- vector <string> tokens;
- vector<Tree*> children;
- ~Tree() {
- for(vector<Tree*>::iterator it=children.begin(); it != children.end(); it++) {
- delete (*it);
- }
- }
- };
- struct symbol{
- string type;
- int address;
- };
- struct completeSymbol
- {
- map <string, symbol> functionSymbolT;
- vector <string> paramlists;
- };
- bool isTerminal(string value)
- {
- int terminalsLength = sizeof(terminals)/sizeof(terminals[0]);
- for (int i = 0; i < terminalsLength; i++)
- {
- if (terminals[i] == value)
- {
- return true;
- }
- }
- return false;
- }
- void tokenizer(string value, vector <string> &t)
- {
- istringstream iss(value);
- copy(istream_iterator<string>(iss),
- istream_iterator<string>(),
- back_inserter(t));
- }
- Tree* createParseTree(string value)
- {
- string line;
- getline(cin, line);
- vector <string> token;
- tokenizer(line, token);
- Tree* temp = new Tree;
- temp->tokens = token;
- token.clear();
- temp->rule = line;
- if (!(isTerminal(temp->tokens[0])))
- {
- //cout << "heloo" << endl;
- for (int i = 1; i < temp->tokens.size(); i++)
- {
- temp->children.push_back(createParseTree(temp->tokens[i]));
- }
- }
- return temp;
- }
- void parameters (Tree* t, vector <string> ¶mlist)
- {
- if (t->tokens[0] == "dcl")
- {
- string typeOfParam;
- if (t->children[0]->children.size() == 2)
- {
- typeOfParam = "int*";
- }
- else if (t->children[0]->children.size() == 1)
- {
- typeOfParam = "int";
- }
- paramlist.push_back(typeOfParam);
- }
- if (t->children.size() != 0)
- {
- for (int i = 0; i < t->children.size(); i++)
- {
- parameters(t->children[i], paramlist);
- }
- }
- }
- void genSymbolTable(Tree* t, map <string, completeSymbol> &s, string &functionName, vector <string> &mainParams, int &counterForMain, int &offset)
- {
- if (t->rule == "procedure INT ID LPAREN params RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE")
- {
- functionName = t->children[1]->tokens[1];
- completeSymbol temp1;
- s.insert(pair <string, completeSymbol> (functionName, temp1));
- offset = 0;
- }
- else if (t->rule == "procedures main")
- {
- offset = 0;
- functionName = "wain";
- completeSymbol temp2;
- temp2.paramlists = mainParams;
- s.insert(pair <string, completeSymbol> (functionName, temp2));
- }
- if (t->tokens[0] == "dcl")
- {
- symbol temp;
- string variableId = t->children[1]->tokens[1];
- string typeOfId;
- if (t->children[0]->children.size() == 2)
- {
- typeOfId = "int*";
- }
- else if (t->children[0]->children.size() == 1)
- {
- typeOfId = "int";
- }
- temp.type = typeOfId;
- temp.address = offset;
- s.find(functionName)->second.functionSymbolT.insert(std::pair<string,symbol>(variableId,temp));
- offset -=4;
- if (counterForMain < 2 && functionName == "wain")
- {
- mainParams.push_back(typeOfId);
- s.find(functionName)->second.paramlists = mainParams;
- counterForMain++;
- }
- }
- else if (t->rule == "params paramlist")
- {
- vector <string> tempParam;
- parameters(t, tempParam);
- s.find(functionName)->second.paramlists = tempParam;
- }
- if (t->children.size() != 0)
- {
- for (int i = 0; i < t->children.size(); i++)
- {
- genSymbolTable(t->children[i], s, functionName, mainParams, counterForMain, offset);
- }
- }
- }
- string push(int r){
- stringstream ss;
- ss << "sw $" << r << ", -4($30)\n" << "sub $30, $30, $4\n";
- return ss.str();
- }
- string pop(int r){
- stringstream ss;
- ss << "lw $" << r <<", 0($30)\n" << "add $30, $30, $4\n";
- return ss.str();
- }
- int labelNum=-1;
- string getLabel(){
- labelNum++;
- stringstream ss;
- ss << "label" << labelNum;
- return ss.str();
- }
- void prologue(map <string, completeSymbol> &s)
- {
- cout << "lis $4" << endl;
- cout << ".word 4" << endl;
- cout << "lis $11" << endl;
- cout << ".word 1" << endl;
- cout << ".import print" << endl;
- cout << ".import init" << endl;
- cout << ".import new" << endl;
- cout << ".import delete" << endl;
- cout << "sw $31, -4($30)" << endl;
- cout << "sub $30, $30, $4" << endl;
- string param2 = s.find("wain")->second.paramlists[0];
- if (param2 == "int")
- {
- cout << push(2) << "add $2, $0, $0\n" << "lis $5\n" << ".word init\n" << "jalr $5\n" << pop(2);
- }
- else
- {
- cout << "lis $5" << endl;
- cout << ".word init" << endl;
- cout << "jalr $5" << endl;
- }
- cout << "beq $0, $0, Fwain\n";
- }
- void epilogue()
- {
- cout << "add $30, $30, $4" << endl;
- cout << "lw $31, -4($30)" << endl;
- cout << "jr $31" << endl;
- }
- string getType(Tree *t, map <string, completeSymbol> &s, string &functionName){
- if (t->rule == "expr term")
- {
- //cout << "expr term check here" << endl;
- return getType(t->children[0], s, functionName);
- }
- else if (t->rule == "expr expr PLUS term")
- {
- string leftNode = getType(t->children[0], s, functionName);
- string rightNode = getType(t->children[2], s, functionName);
- if (leftNode == "int" && rightNode == "int")
- {
- return "int";
- }
- else if (leftNode == "int*" && rightNode == "int")
- {
- return "int*";
- }
- else if (leftNode == "int" && rightNode == "int*")
- {
- return "int*";
- }
- else if (leftNode == "int*" && rightNode == "int*")
- {
- return "ERROR";
- }
- }
- else if (t->rule == "expr expr MINUS term")
- {
- string leftNode = getType(t->children[0], s, functionName);
- string rightNode = getType(t->children[2], s, functionName);
- if (leftNode == "int" && rightNode == "int")
- {
- return "int";
- }
- else if (leftNode == "int*" && rightNode == "int")
- {
- return "int*";
- }
- else if (leftNode == "int*" && rightNode == "int*")
- {
- return "int";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "term factor")
- {
- //cout << "term factor here" << endl;
- return getType(t->children[0], s, functionName);
- }
- else if (t->rule == "term term STAR factor")
- {
- string leftNode = getType(t->children[0], s, functionName);
- string rightNode = getType(t->children[2], s, functionName);
- if (leftNode == "int" && rightNode == "int")
- {
- return "int";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "term term SLASH factor")
- {
- string leftNode = getType(t->children[0], s, functionName);
- string rightNode = getType(t->children[2], s, functionName);
- if (leftNode == "int" && rightNode == "int")
- {
- return "int";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "term term PCT factor")
- {
- string leftNode = getType(t->children[0], s, functionName);
- string rightNode = getType(t->children[2], s, functionName);
- if (leftNode == "int" && rightNode == "int")
- {
- return "int";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "factor ID")
- {
- if (s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1]) == s.find(functionName)->second.functionSymbolT.end())
- {
- //cerr << "adasdas" << endl;
- return "ERROR";
- }
- //cerr << s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type << endl;
- return s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type;
- }
- else if (t->rule == "factor NUM")
- {
- //cout << "factor NUM here" << endl;
- return "int";
- }
- else if (t->rule == "factor NULL")
- {
- return "int*";
- }
- else if (t->rule == "factor LPAREN expr RPAREN")
- {
- return getType(t->children[1], s, functionName);
- }
- else if (t->rule == "factor AMP lvalue")
- {
- string type = getType(t->children[1], s, functionName);
- if (type == "int")
- {
- return "int*";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "factor STAR factor")
- {
- string type = getType(t->children[1],s, functionName);
- if (type == "int*")
- {
- return "int";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "factor NEW INT LBRACK expr RBRACK")
- {
- string type = getType(t->children[3], s, functionName);
- if (type == "int" )
- {
- return "int*";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "factor ID LPAREN RPAREN")
- {
- //cout << "factor ID LPAREN RPAREN" << endl;
- string callingFunction = t->children[0]->tokens[1];
- int paramsize = s.find(callingFunction)->second.paramlists.size();
- //cout << "double check" << endl;
- //cout << paramsize << endl;
- if (paramsize != 0)
- {
- return "ERROR";
- }
- else
- {
- return "int";
- }
- }
- else if (t->rule == "factor ID LPAREN arglist RPAREN")
- {
- string functionCall = t->children[0]->tokens[1];
- string checkParam = getType(t->children[2], s, functionName);
- vector <string> temp;
- vector <string> temp2 = s.find(functionCall)->second.paramlists;
- tokenizer(checkParam, temp);
- if (temp.size() != temp2.size())
- {
- return "ERROR";
- }
- else
- {
- for (int i = 0; i < temp.size(); i++)
- {
- if (temp[i] != temp2[i])
- {
- return "ERROR";
- }
- }
- }
- return "int";
- }
- else if (t->rule == "arglist expr")
- {
- return getType(t->children[0], s, functionName);
- }
- else if (t->rule == "arglist expr COMMA arglist")
- {
- return getType(t->children[0], s, functionName) + " " + getType(t->children[2], s, functionName);
- }
- else if (t->rule == "lvalue ID")
- {
- //cerr << "adasd" << endl;
- if (s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1]) == s.find(functionName)->second.functionSymbolT.end())
- {
- return "ERROR";
- }
- //cerr << s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type << endl;
- return s.find(functionName)->second.functionSymbolT.find(t->children[0]->tokens[1])->second.type;
- }
- else if (t->rule == "lvalue STAR factor")
- {
- string factorType = getType(t->children[1], s, functionName);
- if (factorType == "int*")
- {
- return "int";
- }
- else
- {
- return "ERROR";
- }
- }
- else if (t->rule == "lvalue LPAREN lvalue RPAREN")
- {
- return getType(t->children[1], s, functionName);
- }
- else if (t->rule == "dcl type ID")
- {
- if (t->children[0]->children.size() == 1)
- {
- return "int";
- }
- else if (t->children[0]->children.size() == 2)
- {
- return "int*";
- }
- }
- }
- void modifySymbolTable(map <string, completeSymbol> &s)
- {
- for (map<string, completeSymbol>::iterator i = s.begin(); i!= s.end(); i++)
- {
- if (i->first != "wain")
- {
- int paramSize = i->second.paramlists.size() * 4;
- for (map<string,symbol>::iterator i2 = i->second.functionSymbolT.begin(); i2 != i->second.functionSymbolT.end(); i2++)
- {
- i2->second.address += paramSize;
- }
- }
- }
- }
- string CodeGen(Tree* t, string &functionName, map <string, completeSymbol> &s)
- {
- if (t->rule == "procedure INT ID LPAREN params RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE")
- {
- functionName = t->children[1]->tokens[1];
- //cout << "F" << functionName << ":\n";
- string z = "F" + functionName + ":\n";
- string a = CodeGen(t->children[6], functionName, s);
- string b = CodeGen(t->children[7], functionName, s);
- string c = CodeGen(t->children[9], functionName, s);
- string d = "jr $31\n";
- return z + a + b + c + d;
- }
- else if (t->rule == "procedures procedure procedures")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[1], functionName, s);
- return a + b;
- }
- else if (t->rule == "procedures main")
- {
- functionName = "wain";
- //cout << "Fwain:\n";
- string a = CodeGen(t->children[0], functionName, s);
- return string("Fwain:\n") + string("sub $29, $30, $4\n")+ a + string("add $30, $29, $4\n");
- }
- else if (t->rule == "main INT WAIN LPAREN dcl COMMA dcl RPAREN LBRACE dcls statements RETURN expr SEMI RBRACE")
- {
- string a = CodeGen(t->children[8], functionName, s);
- string b = CodeGen(t->children[9], functionName, s);
- string c = CodeGen(t->children[11], functionName, s);
- return push(1) + push(2) + a + b + c;
- }
- else if (t->tokens[0] == "dcls")
- {
- if (t->rule == "dcls")
- {
- return "";
- }
- else if (t->rule == "dcls dcls dcl BECOMES NUM SEMI")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[3],functionName, s);
- return a + b + push(3);
- }
- else if (t->rule == "dcls dcls dcl BECOMES NULL SEMI")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[3], functionName, s);
- return a + b + push(3);
- }
- }
- else if (t->tokens[0] == "statements")
- {
- if (t->rule == "statements")
- {
- return "";
- }
- else if (t->rule == "statements statements statement")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[1], functionName, s);
- return a + b;
- }
- }
- else if (t->tokens[0] == "statement")
- {
- if (t->rule == "statement lvalue BECOMES expr SEMI")
- {
- stringstream ss;
- if (t->children[0]->rule == "lvalue STAR factor")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- ss << a << "add $6, $3, $0\n" << b << "sw $3, 0($6)\n";
- return ss.str();
- }
- else
- {
- //cerr << "statement with lvalue becoming ID" << endl;
- string id = CodeGen(t->children[0], functionName, s);
- int offset = s.find(functionName)->second.functionSymbolT.find(id)->second.address;
- string a = CodeGen(t->children[2], functionName, s);
- ss << a << "sw $3, " << offset << "($29)\n";
- return ss.str();
- }
- }
- else if (t->rule == "statement IF LPAREN test RPAREN LBRACE statements RBRACE ELSE LBRACE statements RBRACE")
- {
- string label1 = getLabel();
- string label2 = getLabel();
- stringstream ss;
- string a = CodeGen(t->children[2], functionName, s);
- string b = CodeGen(t->children[9], functionName, s);
- string c = CodeGen(t->children[5], functionName, s);
- ss << a << "beq $3, $11, " << label1 << endl
- << b << "beq $0, $0, " << label2 << endl
- << label1 << ":\n" << c << label2 << ":\n";
- return ss.str();
- }
- else if (t->rule == "statement WHILE LPAREN test RPAREN LBRACE statements RBRACE")
- {
- string loop = getLabel();
- string loopEx = getLabel();
- string a = CodeGen(t->children[2], functionName, s);
- string b = CodeGen(t->children[5], functionName, s);
- stringstream ss;
- ss << loop <<":\n" << a << "beq $3, $0, "<< loopEx << endl << b << "beq $0, $0, " << loop << endl << loopEx <<":\n";
- return ss.str();
- }
- else if (t->rule == "statement PRINTLN LPAREN expr RPAREN SEMI")
- {
- stringstream ss;
- string a = CodeGen(t->children[2], functionName, s);
- ss << a << "add $1, $3, $0\n" << push(31) << "lis $5\n" << ".word print\n" << "jalr $5\n" << pop(31);
- return ss.str();
- }
- else if (t->rule == "statement DELETE LBRACK RBRACK expr SEMI")
- {
- stringstream ss;
- /*
- 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);
- return ss.str();
- */
- string a = CodeGen(t->children[3], functionName, s);
- 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);
- return ss.str();
- /* code (expr), add $1, $3, $0, beq $1, $11, 7, push($31), lis $5, .word delete, jalr $5, pop($31) */
- }
- }
- else if (t->tokens[0] == "dcl")
- {
- return CodeGen(t->children[1], functionName, s);
- }
- else if (t->tokens[0] == "expr")
- {
- //cout << type1 << endl;
- //cout << type2 << endl;
- if (t->rule == "expr term")
- {
- return CodeGen(t->children[0], functionName, s);
- }
- else
- {
- string type1 = getType(t->children[0], s, functionName);
- string type2 = getType(t->children[2], s, functionName);
- if (type1 == "int" && type2 == "int")
- {
- if(t->tokens[2] == "PLUS")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- return a + push(3) + b + pop(5) +"add $3, $3, $5\n";
- }
- else if(t->tokens[2] == "MINUS")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- return a + push(3) + b + pop(5) + "sub $3, $5, $3\n";
- }
- }
- else if (type1 == "int*" && type2 == "int")
- {
- stringstream ss;
- if (t->tokens[2] == "PLUS")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- ss << a << push(3) << b << pop(5) << "mult $3, $4\n" << "mflo $3\n" << "add $3, $5, $3\n";
- }
- else if (t->tokens[2] == "MINUS")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- ss << a << push(3) << b
- << pop(5) << "mult $3, $4\n" << "mflo $3\n" << "sub $3, $5, $3\n";
- }
- return ss.str();
- }
- else if (type1 == "int" && type2 == "int*")
- {
- stringstream ss;
- if (t->tokens[2] == "PLUS")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- ss << a << push(3) << b
- << pop(5) << "mult $5, $4\n" << "mflo $5\n" << "add $3, $5, $3\n";
- }
- return ss.str();
- }
- else if (type1 == "int*" && type2 == "int*")
- {
- stringstream ss;
- if (t->tokens[2] == "MINUS")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- ss << a << push(3) << b
- << pop(5) << "sub $3, $5, $3\n" << "div $3, $4\n" << "mflo $3\n";
- }
- return ss.str();
- }
- }
- }
- else if (t->tokens[0] == "term")
- {
- if (t->rule == "term factor")
- {
- return CodeGen(t->children[0], functionName, s);
- }
- else if (t->tokens[2] == "STAR")
- {
- //cerr << "term star factor" << endl;
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- return a + push(3) + b + pop(5) + "mult $3, $5\n" + "mflo $3\n";
- }
- else if (t->tokens[2] == "SLASH")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- return a + push(3) + b + pop(5) + "div $5, $3\n" + "mflo $3\n";
- }
- else if (t->tokens[2] == "PCT")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- return a + push(3) + b + pop(5) + "div $5, $3\n" + "mfhi $3\n";
- }
- }
- else if (t->tokens[0] == "factor")
- {
- if (t->rule == "factor ID")
- {
- //cerr << "factor ID" << endl;
- return CodeGen(t->children[0], functionName, s);
- }
- else if (t->rule == "factor LPAREN expr RPAREN")
- {
- return CodeGen(t->children[1], functionName, s);
- }
- else if (t->rule == "factor NUM")
- {
- return CodeGen(t->children[0], functionName, s);
- }
- else if (t->rule == "factor STAR factor")
- {
- //cerr << "factor STAR factor" << endl;
- stringstream ss;
- string a = CodeGen(t->children[1], functionName, s);
- ss << a << "lw $3, 0($3)\n";
- return ss.str();
- }
- else if (t->rule == "factor AMP lvalue")
- {
- if (t->children[1]->rule == "lvalue ID")
- {
- string variableName = t->children[1]->children[0]->tokens[1];
- int offset = s.find(functionName)->second.functionSymbolT.find(variableName)->second.address;
- stringstream ss;
- ss << "lis $3\n" << ".word " << offset << "\n" << "add $3, $3, $29\n";
- return ss.str();
- }
- else if (t->children[1]->rule == "lvalue STAR factor")
- {
- //cerr << "lvalue star factpr" << endl;
- return CodeGen(t->children[1], functionName, s);
- }
- else if (t->children[1]->rule == "lvalue LPAREN lvalue RPAREN")
- {
- return CodeGen(t->children[1], functionName, s);
- }
- }
- else if (t->rule == "factor NULL")
- {
- return CodeGen(t->children[0], functionName, s);
- }
- else if (t->rule == "factor NEW INT LBRACK expr RBRACK")
- {
- stringstream ss;
- /*
- 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);
- return ss.str();
- */
- string a = CodeGen(t->children[3], functionName, s);
- 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";
- return ss.str();
- /* add bne $3, $0, 1, add $3, $11, $0*/
- }
- else if (t->rule == "factor ID LPAREN RPAREN")
- {
- string callingFunction = t->children[0]->tokens[1];
- /*string restoreFunction = functionName;
- string callingFunction = t->children[0]->tokens[1];
- functionName = callingFunction;
- stringstream ss;*/
- stringstream ss;
- ss << push(29) << push(31) << "sub $29, $30, $4\n" << "lis $5\n" << ".word " << "F" << callingFunction << "\n" << "jalr $5\n"
- << "add $30, $29, $4\n" << pop(31) << pop(29);
- //functionName = restoreFunction;
- return ss.str();
- }
- else if (t->rule == "factor ID LPAREN arglist RPAREN")
- {
- //string restoreFunction = functionName;
- string callingFunction = t->children[0]->tokens[1];
- int paramSize = s.find(callingFunction)->second.paramlists.size() * 4;
- string a = CodeGen(t->children[2], functionName, s);
- //functionName = calllingFunction;
- stringstream ss;
- ss << push(29) << push(31) << a << "sub $29, $30, $4\n" << "lis $18\n" << ".word " << paramSize << "\n"
- << "add $29, $29, $18\n" << "lis $5\n" << ".word " << "F" << callingFunction << "\n" << "jalr $5\n"
- << "add $30, $29, $4\n" << pop(31) << pop(29);
- //functionName = restoreFunction;
- return ss.str();
- }
- }
- else if (t->rule == "arglist expr")
- {
- string a = CodeGen(t->children[0], functionName, s);
- return a + push(3);
- }
- else if (t->rule == "arglist expr COMMA arglist")
- {
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- return a + push(3) + b ;
- }
- else if (t->tokens[0] == "ID")
- {
- string variableName = t->tokens[1];
- int paramOffSet = s.find(functionName)->second.functionSymbolT.find(variableName)->second.address;
- stringstream ss;
- ss << "lw $3, " << paramOffSet <<"($29)\n";
- return ss.str();
- }
- else if (t->tokens[0] == "NUM")
- {
- string number = t->tokens[1];
- return string("lis $3\n") + string (".word ") + number + "\n";
- }
- else if (t->tokens[0] == "NULL")
- {
- return string("lis $3\n") + string(".word 1\n");
- }
- else if (t->tokens[0] == "lvalue")
- {
- if (t->rule == "lvalue ID")
- {
- return t->children[0]->tokens[1];
- }
- else if (t->rule == "lvalue LPAREN lvalue RPAREN")
- {
- return CodeGen(t->children[1], functionName, s);
- }
- else if (t->rule == "lvalue STAR factor")
- {
- return CodeGen(t->children[1], functionName, s);
- }
- }
- else if (t->tokens[0] == "test")
- {
- string type1 = getType(t->children[0], s, functionName);
- string type2 = getType(t->children[2], s, functionName);
- stringstream ss;
- string a = CodeGen(t->children[0], functionName, s);
- string b = CodeGen(t->children[2], functionName, s);
- ss << a << push(3) << b << pop(5);
- if (type1 == "int" && type2 == "int")
- {
- if (t->rule == "test expr LT expr")
- {
- ss << "slt $3, $5, $3" << endl;
- }
- else if (t->rule == "test expr EQ expr")
- {
- ss << "slt $6, $3, $5\n" << "slt $7, $5, $3\n" << "add $3, $6, $7\n" << "sub $3, $11, $3" << endl;
- }
- else if (t->rule == "test expr NE expr")
- {
- ss << "slt $6, $3, $5\n" << "slt $7, $5, $3\n" << "add $3, $6, $7" << endl;
- }
- else if (t->rule == "test expr LE expr")
- {
- ss << "slt $3, $3, $5\n" << "slt $3, $3, $11" << endl;
- }
- else if (t->rule == "test expr GE expr")
- {
- ss << "slt $3, $5, $3\n" << "slt $3, $3, $11" <<endl;
- }
- else if (t->rule == "test expr GT expr")
- {
- ss << "slt $3, $3, $5" << endl;
- }
- return ss.str();
- }
- else if (type1 == "int*" && type2 == "int*")
- {
- if (t->rule == "test expr LT expr")
- {
- ss << "sltu $3, $5, $3" << endl;
- }
- else if (t->rule == "test expr EQ expr")
- {
- ss << "sltu $6, $3, $5\n" << "sltu $7, $5, $3\n" << "add $3, $6, $7\n" << "sub $3, $11, $3" << endl;
- }
- else if (t->rule == "test expr NE expr")
- {
- ss << "sltu $6, $3, $5\n" << "sltu $7, $5, $3\n" << "add $3, $6, $7" << endl;
- }
- else if (t->rule == "test expr LE expr")
- {
- ss << "sltu $3, $3, $5\n" << "sltu $3, $3, $11" << endl;
- }
- else if (t->rule == "test expr GE expr")
- {
- ss << "sltu $3, $5, $3\n" << "sltu $3, $3, $11" <<endl;
- }
- else if (t->rule == "test expr GT expr")
- {
- ss << "sltu $3, $3, $5" << endl;
- }
- return ss.str();
- }
- }
- }
- void outputParseTree(Tree* &root)
- {
- cout << root->rule << endl;
- for (int i = 0; i < root->children.size(); i++)
- {
- outputParseTree(root->children[i]);
- }
- return;
- }
- int main()
- {
- int offset = 0;
- int counterForMainArgs = 0;
- vector <string> mainParams;
- map <string, completeSymbol> globalST;
- string functionname = "";
- Tree* parseTree = createParseTree("S");
- //outputParseTree(parseTree);
- genSymbolTable(parseTree, globalST, functionname, mainParams, counterForMainArgs, offset);
- //outputParseTree(parseTree);
- prologue(globalST);
- cout << CodeGen(parseTree->children[1], functionname, globalST);
- epilogue();
- delete parseTree;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement