diff --git a/lab3-4/codegen.cc b/lab3-4/codegen.cc index 299c6c7a7a46fb9eafcfa824d1443bb720926870..66697a24ef78324fd3ad0c559a64b0f38073815f 100644 --- a/lab3-4/codegen.cc +++ b/lab3-4/codegen.cc @@ -26,13 +26,13 @@ long QuadsList::labelCounter; VariableInformation *ASTNode::GenerateCodeAndJump(QuadsList& q, - long label) + long label) { VariableInformation *info; info = this->GenerateCode(q); q += new Quad(jump, label, - (SymbolInformation *)NULL, (SymbolInformation *)NULL); + (SymbolInformation *)NULL, (SymbolInformation *)NULL); return info; } @@ -49,17 +49,11 @@ VariableInformation *ASTNode::GenerateCodeAndJump(QuadsList& q, */ VariableInformation *ElseIfList::GenerateCodeAndJump(QuadsList &q, - long lbl) + long lbl) { - long next; - VariableInformation *info; - /* --- Your code here --- */ - /* --- End your code --- */ - return NULL; - } @@ -71,10 +65,9 @@ VariableInformation *ElseIfList::GenerateCodeAndJump(QuadsList &q, */ void ArrayReference::GenerateAssignment(QuadsList& q, - VariableInformation *val) + VariableInformation *val) { /* --- Your code here --- */ - /* --- End your code --- */ } @@ -89,26 +82,26 @@ void Identifier::GenerateAssignment(QuadsList& q, VariableInformation *val) { if (val->type == NULL || id->type == NULL) { - cerr << "Bug: you created an untyped variable.\n"; - abort(); + cerr << "Bug: you created an untyped variable.\n"; + abort(); } if (id->type == kIntegerType) { - q += new Quad(iassign, - dynamic_cast<SymbolInformation*>(val), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(id)); + q += new Quad(iassign, + dynamic_cast<SymbolInformation*>(val), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(id)); } else if (id->type == kRealType) { - q += new Quad(rassign, - dynamic_cast<SymbolInformation*>(val), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(id)); + q += new Quad(rassign, + dynamic_cast<SymbolInformation*>(val), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(id)); } else if (id->type == val->type) { - q += new Quad(aassign, val, val->type->arrayDimensions, id); + q += new Quad(aassign, val, val->type->arrayDimensions, id); } } @@ -131,7 +124,7 @@ VariableInformation *StatementList::GenerateCode(QuadsList &q) { if (precedingStatements != NULL) { - precedingStatements->GenerateCode(q); + precedingStatements->GenerateCode(q); } return statement->GenerateCode(q); } @@ -149,10 +142,7 @@ VariableInformation *StatementList::GenerateCode(QuadsList &q) VariableInformation *IfStatement::GenerateCode(QuadsList& q) { /* --- Your code here ---*/ - - /* --- End your code --- */ - return NULL; } @@ -168,12 +158,8 @@ VariableInformation *IfStatement::GenerateCode(QuadsList& q) VariableInformation *ElseIfList::GenerateCode(QuadsList& q) { - USEQ; - /* --- Your code here --- */ - /* --- End your code --- */ - cerr << "Call to ElseIfList::GenerateCode. You probably didn't want to do this.\n"; // abort(); } @@ -217,7 +203,7 @@ VariableInformation *WhileStatement::GenerateCode(QuadsList &q) VariableInformation *IntegerConstant::GenerateCode(QuadsList& q) { VariableInformation *info = - currentFunction->TemporaryVariable(kIntegerType); + currentFunction->TemporaryVariable(kIntegerType); q += new Quad(iconst, value, NULL, info); return info; @@ -226,7 +212,7 @@ VariableInformation *IntegerConstant::GenerateCode(QuadsList& q) VariableInformation *RealConstant::GenerateCode(QuadsList& q) { VariableInformation *info = - currentFunction->TemporaryVariable(kRealType); + currentFunction->TemporaryVariable(kRealType); q += new Quad(rconst, value, NULL, info); return info; @@ -235,7 +221,7 @@ VariableInformation *RealConstant::GenerateCode(QuadsList& q) VariableInformation *BooleanConstant::GenerateCode(QuadsList& q) { VariableInformation *info = - currentFunction->TemporaryVariable(kIntegerType); + currentFunction->TemporaryVariable(kIntegerType); q += new Quad(iconst, value ? 1L : 0L, NULL, info); return info; @@ -254,9 +240,9 @@ VariableInformation *BooleanConstant::GenerateCode(QuadsList& q) VariableInformation *ArrayReference::GenerateCode(QuadsList& q) { - /* --- Your code here --- */ - - /* --- End your code --- */ +/* --- Your code here --- */ + return NULL; +/* --- End your code --- */ } /* @@ -287,14 +273,14 @@ VariableInformation *ReturnStatement::GenerateCode(QuadsList &q) info = value->GenerateCode(q); if (info->type != currentFunction->GetReturnType()) { - cerr << "Bug: you forgot to typecheck return statements.\n"; - abort(); + cerr << "Bug: you forgot to typecheck return statements.\n"; + abort(); } q += new Quad(creturn, - static_cast<SymbolInformation*>(NULL), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(info)); + static_cast<SymbolInformation*>(NULL), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(info)); return NULL; } @@ -318,34 +304,34 @@ VariableInformation *ExpressionList::GenerateCode(QuadsList& q) } void ExpressionList::GenerateParameterList(QuadsList &q, - VariableInformation *lastParam) + VariableInformation *lastParam) { VariableInformation *info; if (lastParam == NULL || - (lastParam->prev != NULL && precedingExpressions == NULL)) + (lastParam->prev != NULL && precedingExpressions == NULL)) { - cerr << "Bug: type checking of function params isn't good enough.\n"; - abort(); + cerr << "Bug: type checking of function params isn't good enough.\n"; + abort(); } if (precedingExpressions) - precedingExpressions->GenerateParameterList(q, lastParam->prev); + precedingExpressions->GenerateParameterList(q, lastParam->prev); info = expression->GenerateCode(q); if (expression->valueType == lastParam->type) { - q += new Quad(param, - dynamic_cast<SymbolInformation*>(info), - static_cast<SymbolInformation*>(NULL), - static_cast<SymbolInformation*>(NULL)); + q += new Quad(param, + dynamic_cast<SymbolInformation*>(info), + static_cast<SymbolInformation*>(NULL), + static_cast<SymbolInformation*>(NULL)); } else { - cerr << "Bug: type checking of function params isn't good enough.\n"; - abort(); + cerr << "Bug: type checking of function params isn't good enough.\n"; + abort(); } } @@ -389,15 +375,15 @@ VariableInformation *IntegerToReal::GenerateCode(QuadsList& q) if (value->valueType != kIntegerType) { - cerr << "Bug: you're trying to convert a non-integer to a real.\n"; + cerr << "Bug: you're trying to convert a non-integer to a real.\n"; } info = currentFunction->TemporaryVariable(kRealType); valueInfo = value->GenerateCode(q); q += new Quad(itor, - dynamic_cast<SymbolInformation*>(valueInfo), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(info)); + dynamic_cast<SymbolInformation*>(valueInfo), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(info)); return info; } @@ -408,15 +394,15 @@ VariableInformation *TruncateReal::GenerateCode(QuadsList& q) if (value->valueType != kRealType) { - cerr << "Bug: you're trying to truncate a non-real.\n"; + cerr << "Bug: you're trying to truncate a non-real.\n"; } info = currentFunction->TemporaryVariable(kIntegerType); valueInfo = value->GenerateCode(q); q += new Quad(rtrunc, - dynamic_cast<SymbolInformation*>(valueInfo), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(info)); + dynamic_cast<SymbolInformation*>(valueInfo), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(info)); return info; } @@ -453,19 +439,16 @@ VariableInformation *TruncateReal::GenerateCode(QuadsList& q) */ static VariableInformation *BinaryGenerateCode(QuadsList& q, - tQuadType realop, - tQuadType intop, - ASTNode *left, - ASTNode *right, - ASTNode *node, - TypeInformation *type = NULL) + tQuadType realop, + tQuadType intop, + ASTNode *left, + ASTNode *right, + ASTNode *node, + TypeInformation *type = NULL) { - VariableInformation *leftInfo, *rightInfo, *result; - - /* --- Your code here --- */ - - /* --- End your code --- */ - return NULL; //Should not return NULL! + /* --- Your code here --- */ + return NULL; + /* --- End your code --- */ } /* @@ -520,18 +503,18 @@ VariableInformation *UnaryMinus::GenerateCode(QuadsList& q) if (info->type == kIntegerType) { - q += new Quad(iconst, 0, NULL, constInfo); - q += new Quad(isub, constInfo, info, result); + q += new Quad(iconst, 0, NULL, constInfo); + q += new Quad(isub, constInfo, info, result); } else if (info->type == kRealType) { - q += new Quad(rconst, 0.0, NULL, constInfo); - q += new Quad(rsub, constInfo, info, result); + q += new Quad(rconst, 0.0, NULL, constInfo); + q += new Quad(rsub, constInfo, info, result); } else { - cerr << "Bug: unary minus of a non-numeric type.\n"; - abort(); + cerr << "Bug: unary minus of a non-numeric type.\n"; + abort(); } return result; @@ -596,9 +579,9 @@ VariableInformation *NotEqual::GenerateCode(QuadsList& q) r0 = BinaryGenerateCode(q, req, ieq, left, right, this, kIntegerType); auto result = currentFunction->TemporaryVariable(kIntegerType); q += new Quad(inot, - dynamic_cast<SymbolInformation*>(r0), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(result)); + dynamic_cast<SymbolInformation*>(r0), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(result)); return r0; } @@ -633,15 +616,15 @@ VariableInformation *Not::GenerateCode(QuadsList& q) info = right->GenerateCode(q); if (info->type != kIntegerType) { - cerr << "Bug: not operator applied to a non-integer.\n"; - abort(); + cerr << "Bug: not operator applied to a non-integer.\n"; + abort(); } result = currentFunction->TemporaryVariable(kIntegerType); q += new Quad(inot, - dynamic_cast<SymbolInformation*>(info), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(result)); + dynamic_cast<SymbolInformation*>(info), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(result)); return result; } @@ -664,12 +647,12 @@ VariableInformation *FunctionCall::GenerateCode(QuadsList& q) VariableInformation *info; if (arguments) - arguments->GenerateParameterList(q, function->GetLastParam()); + arguments->GenerateParameterList(q, function->GetLastParam()); info = currentFunction->TemporaryVariable(function->GetReturnType()); q += new Quad(call, - dynamic_cast<SymbolInformation*>(function), - static_cast<SymbolInformation*>(NULL), - dynamic_cast<SymbolInformation*>(info)); + dynamic_cast<SymbolInformation*>(function), + static_cast<SymbolInformation*>(NULL), + dynamic_cast<SymbolInformation*>(info)); return info; } @@ -683,13 +666,13 @@ QuadsList& QuadsList::operator+=(Quad *q) { if (head == NULL) { - head = new QuadsListElement(q, NULL); - tail = head; + head = new QuadsListElement(q, NULL); + tail = head; } else { - tail->next = new QuadsListElement(q, NULL); - tail = tail->next; + tail->next = new QuadsListElement(q, NULL); + tail = tail->next; } return *this; @@ -705,8 +688,8 @@ ostream& QuadsList::print(ostream& o) elem = head; while (elem) { - o << elem->data << '\n'; - elem = elem->next; + o << elem->data << '\n'; + elem = elem->next; } o << LongSymbols; @@ -719,248 +702,248 @@ ostream& Quad::print(ostream& o) switch(opcode) { case iconst: - o << setw(8) << "iconst " - << setw(8) << int1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; + o << setw(8) << "iconst " + << setw(8) << int1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; case rconst: - o << setw(8) <<"rconst " - << setw(8) <<real1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; + o << setw(8) <<"rconst " + << setw(8) <<real1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; case iaddr: - o << setw(8) <<"iaddr " - << setw(8) <<sym1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; + o << setw(8) <<"iaddr " + << setw(8) <<sym1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; case itor: - o << setw(8) <<"itor " - << setw(8) <<sym1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; + o << setw(8) <<"itor " + << setw(8) <<sym1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; case rtrunc: - o << setw(8) <<"rtrunc " - << setw(8) <<sym1 - << setw(8) <<"-" - << setw(8) <<sym3; - break; + o << setw(8) <<"rtrunc " + << setw(8) <<sym1 + << setw(8) <<"-" + << setw(8) <<sym3; + break; case iadd: - o << setw(8) << "iadd " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "iadd " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case isub: - o << setw(8) << "isub " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "isub " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case imul: - o << setw(8) << "imul " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "imul " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case idiv: - o << setw(8) << "idiv " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "idiv " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case ipow: - o << setw(8) << "ipow " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "ipow " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case radd: - o << setw(8) << "radd " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "radd " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case rsub: - o << setw(8) << "rsub " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "rsub " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case rmul: - o << setw(8) << "rmul " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "rmul " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case rdiv: - o << setw(8) << "rdiv " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "rdiv " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case rpow: - o << setw(8) << "rpow " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "rpow " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case igt: - o << setw(8) << "igt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "igt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case ilt: - o << setw(8) << "ilt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "ilt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case ieq: - o << setw(8) << "ieq " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "ieq " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case rgt: - o << setw(8) << "rgt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "rgt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case rlt: - o << setw(8) << "rlt " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "rlt " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case req: - o << setw(8) << "req " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "req " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case iand: - o << setw(8) << "iand " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "iand " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case ior: - o << setw(8) << "ior " - << setw(8) << sym1 - << setw(8) << sym2 - << setw(8) << sym3; - break; + o << setw(8) << "ior " + << setw(8) << sym1 + << setw(8) << sym2 + << setw(8) << sym3; + break; case inot: - o << setw(8) << "inot " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "inot " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case jtrue: - o << setw(8) << "jtrue " - << setw(8) << int1 - << setw(8) << sym2 - << setw(8) << "-"; - break; + o << setw(8) << "jtrue " + << setw(8) << int1 + << setw(8) << sym2 + << setw(8) << "-"; + break; case jfalse: - o << setw(8) << "jfalse " - << setw(8) << int1 - << setw(8) << sym2 - << setw(8) << "-"; - break; + o << setw(8) << "jfalse " + << setw(8) << int1 + << setw(8) << sym2 + << setw(8) << "-"; + break; case jump: - o << setw(8) << "jump " - << setw(8) << int1 - << setw(8) << "-" - << setw(8) << "-"; - break; + o << setw(8) << "jump " + << setw(8) << int1 + << setw(8) << "-" + << setw(8) << "-"; + break; case clabel: - o << setw(8) << "clabel " - << setw(8) << int1 - << setw(8) << "-" - << setw(8) << "-"; - break; + o << setw(8) << "clabel " + << setw(8) << int1 + << setw(8) << "-" + << setw(8) << "-"; + break; case istore: - o << setw(8) << "istore " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "istore " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case iload: - o << setw(8) << "iload " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "iload " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case rstore: - o << setw(8) << "rstore " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "rstore " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case rload: - o << setw(8) << "rload " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "rload " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case creturn: - o << setw(8) << "creturn " - << setw(8) << "-" - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "creturn " + << setw(8) << "-" + << setw(8) << "-" + << setw(8) << sym3; + break; case param: - o << setw(8) << "param " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << "-"; - break; + o << setw(8) << "param " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << "-"; + break; case call: - o << setw(8) << "call " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "call " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case iassign: - o << setw(8) << "iassign " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "iassign " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case rassign: - o << setw(8) << "rassign " - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym3; - break; + o << setw(8) << "rassign " + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym3; + break; case aassign: - o << setw(8) << "aassign " - << setw(8) << sym1 - << setw(8) << int1 - << setw(8) << sym3; - break; + o << setw(8) << "aassign " + << setw(8) << sym1 + << setw(8) << int1 + << setw(8) << sym3; + break; case hcf: - o << setw(8) << "hcf " - << setw(8) << "-" - << setw(8) << "-" - << setw(8) << "-"; - break; + o << setw(8) << "hcf " + << setw(8) << "-" + << setw(8) << "-" + << setw(8) << "-"; + break; case nop: - o << setw(8) << "nop " - << setw(8) << "-" - << setw(8) << "-" - << setw(8) << "-"; - break; + o << setw(8) << "nop " + << setw(8) << "-" + << setw(8) << "-" + << setw(8) << "-"; + break; default: - o << "unknown (" << opcode << ")"; - break; + o << "unknown (" << opcode << ")"; + break; } return o; @@ -977,9 +960,9 @@ void QuadsList::print_c(ostream& o) while (elem) { // o << elem->data << '\n'; - elem->data->print_c(o); - o << "\n"; - elem = elem->next; + elem->data->print_c(o); + o << "\n"; + elem = elem->next; } o << CFormat; @@ -993,291 +976,291 @@ void Quad::print_c(ostream& o) switch(opcode) { case iconst: - o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << int1; - break; + o << setw(8) << "integer" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << int1; + break; case rconst: - o << setw(8) <<"real" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << real1; - break; + o << setw(8) <<"real" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << real1; + break; case iaddr: - o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << "(integer)" << sym1; - break; + o << setw(8) << "integer" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << "(integer)" << sym1; + break; case itor: - o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) <<"=" - << setw(8) << "(real)"<< sym1; - break; + o << setw(8) << "real" + << setw(8) << sym3 + << setw(8) <<"=" + << setw(8) << "(real)"<< sym1; + break; case rtrunc: - o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) <<"=" - << setw(8) << "(integer)" <<sym3; - break; + o << setw(8) << "integer" + << setw(8) << sym3 + << setw(8) <<"=" + << setw(8) << "(integer)" <<sym3; + break; case iadd: o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << " = " - << setw(8) << sym1 - << setw(8) << " + " - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << " = " + << setw(8) << sym1 + << setw(8) << " + " + << setw(8) << sym2; + break; case isub: o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << " = " - << setw(8) << sym1 - << setw(8) << " - " - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << " = " + << setw(8) << sym1 + << setw(8) << " - " + << setw(8) << sym2; + break; case imul: - o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << " = " - << setw(8) << sym1 - << setw(8) << " * " - << setw(8) << sym2; - break; + o << setw(8) << "integer" + << setw(8) << sym3 + << setw(8) << " = " + << setw(8) << sym1 + << setw(8) << " * " + << setw(8) << sym2; + break; case idiv: - o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "/" - << setw(8) << sym2; - break; + o << setw(8) << "real" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "/" + << setw(8) << sym2; + break; case ipow: o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "= pow(" - << setw(8) << sym1 - << setw(8) << "," - << setw(8) << sym2 << ")"; - break; + << setw(8) << sym3 + << setw(8) << "= pow(" + << setw(8) << sym1 + << setw(8) << "," + << setw(8) << sym2 << ")"; + break; case radd: o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "+" - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "+" + << setw(8) << sym2; + break; case rsub: o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "-" - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "-" + << setw(8) << sym2; + break; case rmul: o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "*" - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "*" + << setw(8) << sym2; + break; case rdiv: - o << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "/" - << setw(8) << sym2; - break; + o << setw(8) << "real" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "/" + << setw(8) << sym2; + break; case rpow: o << setw(8) << "real " - << setw(8) << sym3 - << setw(8) << "= pow(" - << setw(8) << sym1 - << setw(8) << "," - << setw(8) << sym2 << ")"; - break; + << setw(8) << sym3 + << setw(8) << "= pow(" + << setw(8) << sym1 + << setw(8) << "," + << setw(8) << sym2 << ")"; + break; case igt: - o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << ">" - << setw(8) << sym2; - break; + o << setw(8) << "conditional" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << ">" + << setw(8) << sym2; + break; case ilt: o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "<" - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "<" + << setw(8) << sym2; + break; case ieq: - o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "==" - << setw(8) << sym2; - break; + o << setw(8) << "conditional" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "==" + << setw(8) << sym2; + break; case rgt: o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << ">" - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << ">" + << setw(8) << sym2; + break; case rlt: o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "<" - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "<" + << setw(8) << sym2; + break; case req: o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "==" - << setw(8) << sym2; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "==" + << setw(8) << sym2; break; case iand: - o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "&&" - << setw(8) << sym2; - break; + o << setw(8) << "conditional" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "&&" + << setw(8) << sym2; + break; case ior: o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << "||" - << setw(8) << sym2; - break; + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << "||" + << setw(8) << sym2; + break; case inot: - o << setw(8) << "conditional" - << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << "!" - << setw(8) << sym3; - break; + o << setw(8) << "conditional" + << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << "!" + << setw(8) << sym3; + break; case jtrue: o << setw(8) << "if(" - << setw(8) << sym2 - << setw(8) << ")" - << setw(8) - << setw(8) << "goto clabel" << int1; - break; + << setw(8) << sym2 + << setw(8) << ")" + << setw(8) + << setw(8) << "goto clabel" << int1; + break; case jfalse: o << setw(8) << "if(!" - << setw(8) << sym2 << setw(8) << ")" - << setw(8) - << setw(8) << "goto clabel" << int1; - break; + << setw(8) << sym2 << setw(8) << ")" + << setw(8) + << setw(8) << "goto clabel" << int1; + break; case jump: - o << setw(8) << "goto " - << setw(8) << "clabel" - << int1 - << setw(8) << "/*jmp*/"; - break; + o << setw(8) << "goto " + << setw(8) << "clabel" + << int1 + << setw(8) << "/*jmp*/"; + break; case clabel: - o << setw(8) << "clabel" - << int1 - << ":"; - break; + o << setw(8) << "clabel" + << int1 + << ":"; + break; case istore: - o << setw(8) << "" - << setw(8) << "*(integer*)" << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; - break; + o << setw(8) << "" + << setw(8) << "*(integer*)" << sym3 + << setw(8) << "=" + << setw(8) << sym1; + break; + break; case iload: - o << setw(8) << "integer" - << setw(8) << sym3 - << setw(8) << "= /*iload*/" - << setw(8) << "*(integer*)" << sym1; - break; + o << setw(8) << "integer" + << setw(8) << sym3 + << setw(8) << "= /*iload*/" + << setw(8) << "*(integer*)" << sym1; + break; case rstore: - o << setw(8) << "" - << setw(8) << "*(real*)" << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; + o << setw(8) << "" + << setw(8) << "*(real*)" << sym3 + << setw(8) << "=" + << setw(8) << sym1; + break; case rload: - o - << setw(8) << "real" - << setw(8) << sym3 - << setw(8) << "= /*rload*/" - << setw(8) << "*(real*)" << sym1; - break; + o + << setw(8) << "real" + << setw(8) << sym3 + << setw(8) << "= /*rload*/" + << setw(8) << "*(real*)" << sym1; + break; case creturn: - o << setw(8) << "return " - << setw(8) << sym3 - << setw(8) - << setw(8) << "/*Return statement*/"; - break; + o << setw(8) << "return " + << setw(8) << sym3 + << setw(8) + << setw(8) << "/*Return statement*/"; + break; case param: { if (firstParam) { - ss = ss + sym1->id; - firstParam = false; + ss = ss + sym1->id; + firstParam = false; } else { - ss = ss + "," + sym1->id; + ss = ss + "," + sym1->id; } } break; case call: { o << setw(8) << "integer " << sym3 //Let it be ints for now.. - << setw(8) << "=" - << setw(8) << sym1 - << setw(8) << ss - << setw(8) << ")"; - ss = "("; //Reset callstack - firstParam = true; + << setw(8) << "=" + << setw(8) << sym1 + << setw(8) << ss + << setw(8) << ")"; + ss = "("; //Reset callstack + firstParam = true; } - break; + break; case iassign: - o << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; + o << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1; + break; case rassign: - o << setw(8) << sym3 - << setw(8) << "=" - << setw(8) << sym1; - break; + o << setw(8) << sym3 + << setw(8) << "=" + << setw(8) << sym1; + break; case aassign: { ::string itVar = sym1->id + "_" + ::string(int1); o << setw(8) << "for(int " << itVar << " = 0 ;" - << setw(8) << itVar << "<" << int1 << ";" - << setw(8) << itVar << "++;)" //Increment & close for loop. - /*Assign the array*/ - << setw(8) << sym3 << "[" << itVar << "]" << "=" - << setw(8) << sym1 << "[" << itVar << "];"; + << setw(8) << itVar << "<" << int1 << ";" + << setw(8) << itVar << "++;)" //Increment & close for loop. + /*Assign the array*/ + << setw(8) << sym3 << "[" << itVar << "]" << "=" + << setw(8) << sym1 << "[" << itVar << "];"; } - break; + break; case hcf: o << setw(8) << "exit(-1)"; - break; + break; case nop: o << setw(8) << ";/*No operation*/" - << setw(8) << ""; - break; + << setw(8) << ""; + break; default: - o << "unknown (" << opcode << ")"; - break; + o << "unknown (" << opcode << ")"; + break; } //Add semicolon to all statements o << setw(8) << ";"; @@ -1287,9 +1270,9 @@ void Quad::print_c(ostream& o) ostream& operator<<(ostream& o, QuadsList *q) { if (q != NULL) - return q->print(o); + return q->print(o); else - return o << " QuadsList @ 0x0\n"; + return o << " QuadsList @ 0x0\n"; } ostream& operator<<(ostream& o, QuadsList& q) @@ -1300,9 +1283,9 @@ ostream& operator<<(ostream& o, QuadsList& q) ostream& operator<<(ostream& o, Quad *q) { if (q != NULL) - return q->print(o); + return q->print(o); else - return o << " Quad @ 0x0"; + return o << " Quad @ 0x0"; } ostream& operator<<(ostream& o, Quad& q) diff --git a/lab3-4/main.cc b/lab3-4/main.cc index 6975bc8d8b9bdfc934ee93b153cb0224c4036d89..d859fb226b860319103664fbdf2f32240ba16882 100644 --- a/lab3-4/main.cc +++ b/lab3-4/main.cc @@ -31,7 +31,7 @@ int main(int argc, char **argv) { int option; extern FILE *yyin; - + /* Set up the symbol table. Adds builtin functions used by the language. @@ -55,7 +55,7 @@ int main(int argc, char **argv) kIReadFunction->SetReturnType(kIntegerType); kFReadFunction->SetReturnType(kRealType); kIReadFunction->SetReturnType(kIntegerType); - + currentFunction->AddSymbol(kIntegerType); currentFunction->AddSymbol(kRealType); currentFunction->AddSymbol(kIPrintFunction); @@ -82,13 +82,13 @@ int main(int argc, char **argv) break; case '?': Usage(argv[0]); - break; - case 'c': - std::cout << "/*Using standard-library*/\n"; - std::cout << "#include \"stl.h\"\n"; - SymbolInformation::outputFormat = SymbolInformation::kCFormat; - currentFunction->outputFormat = SymbolInformation::kCFormat; - break; + break; + case 'c': + std::cout << "/*Using standard-library*/\n"; + std::cout << "#include \"stl.h\"\n"; + SymbolInformation::outputFormat = SymbolInformation::kCFormat; + currentFunction->outputFormat = SymbolInformation::kCFormat; + break; } } @@ -102,7 +102,7 @@ int main(int argc, char **argv) } optind += 1; } - + if (optind < argc) Usage(argv[0]); @@ -113,4 +113,3 @@ int main(int argc, char **argv) int retcode = yyparse(); return retcode; } - diff --git a/lab3-4/parser.y b/lab3-4/parser.y index 5d7c442a13ff1ba8bb68f45678c767fe8eb34da5..b65b7b28541b04972985e3270b3d8a314ddd9e83 100644 --- a/lab3-4/parser.y +++ b/lab3-4/parser.y @@ -7,7 +7,7 @@ extern char *yytext; extern int yylineno, errorCount, warningCount; -extern FunctionInformation *currentFunction; +extern FunctionInformation *currentFunction; extern int yylex(void); extern void yyerror(const char *); @@ -45,12 +45,12 @@ extern ostream& warning(void); FunctionCall *call; LeftValue *lvalue; ElseIfList *elseIfList; - + VariableInformation *variable; TypeInformation *type; FunctionInformation *function; - ::string *id; + ::string *id; int integer; double real; void *null; @@ -184,10 +184,10 @@ functions : functions function */ function : - { - cerr << "Function here" << endl; - } - ; + { + cerr << "Function here" << endl; + } + ; /* --- End your code --- */ @@ -249,7 +249,7 @@ type : id else { typeInfo = info->SymbolAsType(); - + if (typeInfo == NULL) { error() << *($1) << " is not a type" << "\n" <<flush; @@ -299,7 +299,7 @@ statements : statements statement if ($2 == NULL) $$ = NULL; else - $$ = new StatementList($1, $2); + $$ = new StatementList($1, $2); } | /* Empty */ { @@ -403,7 +403,7 @@ returnstmt : RETURN expression error() << " attempt to return " << ShortSymbols << expr->valueType << '\n'; error() << " in function declared to return " - << ShortSymbols + << ShortSymbols << currentFunction->GetReturnType() << LongSymbols << '\n'; $$ = NULL; @@ -454,13 +454,13 @@ variable : id << *($1) << "\n" << flush; - + $$ = NULL; } else { varInfo = info->SymbolAsVariable(); - + if (varInfo == NULL) { error() @@ -545,7 +545,7 @@ call : funcname '(' expressions ')' id : ID { - $$ = new ::string(yytext); + $$ = new ::string(yytext); } ; @@ -576,13 +576,13 @@ real : REAL */ expression : - { - cerr << "Expression here" << endl; - } - ; + { + cerr << "Expression here" << endl; + } + ; /* --- End your code --- */ - + expressions : expressionz { @@ -620,10 +620,10 @@ expressionz : expressionz ',' expression */ condition : - { - cerr << "Condition here" << endl; - } - ; + { + cerr << "Condition here" << endl; + } + ; /* --- End your code --- */ @@ -632,7 +632,7 @@ condition : int errorCount = 0; int warningCount = 0; - + /* --- Your code here --- * @@ -642,14 +642,14 @@ int warningCount = 0; /* It is reasonable to believe that you will need a function * that checks that two expressions are of compatible types, * and if possible makes a type conversion. - * For your convenience a skeleton for such a function is + * For your convenience a skeleton for such a function is * provided below. It will be very similar to CheckAssignmentTypes. */ /* * CheckCompatibleTypes checks that the expressions indirectly pointed * to by left and right are compatible. If type conversion is - * necessary, the pointers left and right point to will be modified to + * necessary, the pointers left and right point to will be modified to * point to the node representing type conversion. That's why you have * to pass a pointer to pointer to Expression in these arguments. */ @@ -702,7 +702,7 @@ char CheckAssignmentTypes(LeftValue **left, Expression **right) /* * CheckFunctionParameters is used to check parameters passed to a - * function. func is the function we're passing parameters to, formals + * function. func is the function we're passing parameters to, formals * is a pointer to the last formal parameter we're checking against * and params is a pointer to the ExpressionList we're checking. If * type conversion is necessary, the Expressions pointed to by the @@ -769,7 +769,7 @@ char CheckFunctionParameters(FunctionInformation *func, << params->expression->valueType << '\n' << LongSymbols << flush; - return 0; + return 0; } } } diff --git a/lab3-4/string.hh b/lab3-4/string.hh index 16cef64af7eb68d9de24ca7cf82d4fe54df015c3..f9aadea3af299b5aea918d3cb7d3b5080170fb4d 100644 --- a/lab3-4/string.hh +++ b/lab3-4/string.hh @@ -20,7 +20,7 @@ using std::ostream; // really necessary. If strings were considered read only (no // destructive update), then it would be possible to keep a pool of // strings with reference counts and just pass along references to -// that pool. +// that pool. // class string @@ -36,7 +36,7 @@ private: public: //To kill warnings and make this custom string play nice... - + class error {}; // Exception thrown when out of memory string(); // Default constructor creates empty string @@ -54,7 +54,7 @@ public: string& operator=(const string&); // Assignment operator string& operator=(const char *); // Assignment operator string& operator=(const char); // Assignment operator - + string& operator+=(const string&); // Append operator string& operator+=(const char); // Append operator string& operator+=(const char *); // Append operator diff --git a/lab3-4/symtab.cc b/lab3-4/symtab.cc index dfa6cfc2932e2612ca25f8bc5f01b085df6e943b..0014b6088462488b01d741ddd48c643ee9a53b68 100644 --- a/lab3-4/symtab.cc +++ b/lab3-4/symtab.cc @@ -44,7 +44,7 @@ ostream& SymbolInformation::print(ostream& o) break; case kCFormat: o << id; - break; + break; default: o << "Bad output format\n"; abort(); @@ -80,10 +80,10 @@ ostream& TypeInformation::print(ostream& o) { o << id; } - + o << " [" << size << "]"; break; - + case kShortFormat: if (elementType != NULL) { @@ -103,7 +103,7 @@ ostream& TypeInformation::print(ostream& o) } else { - o << id; + o << id; } break; @@ -200,21 +200,21 @@ ostream& FunctionInformation::print(ostream& o) tmp = tmp->prev; } o << LongSymbols; - o << '\n'; + o << '\n'; } else { o << " Locals: none\n"; } - + o << " Body: " << (void*)body << '\n'; if (body) o << body; - o << '\n'; - + o << '\n'; + o << " Quads: " << (void*)quads << '\n'; if (quads) o << quads; o << '\n'; - + o << symbolTable; break; @@ -240,59 +240,59 @@ ostream& FunctionInformation::print(ostream& o) break; case kCFormat: - /*C-Code preamble*/ - if (returnType == NULL) { - o << "void\n"; - } else { - o << returnType << "\n"; - } - - o << "" << id << "("; - //Parameters - if (lastParam != NULL) + /*C-Code preamble*/ + if (returnType == NULL) { + o << "void\n"; + } else { + o << returnType << "\n"; + } + + o << "" << id << "("; + //Parameters + if (lastParam != NULL) { tmp = lastParam; while (tmp != NULL) { - if (tmp->type->elementType != NULL){ //Array... - o << tmp->type->elementType << "*" << "\t"; - } else { - // o << tmp->type->elementType; - o << tmp->type << "\t"; - } - if (tmp->prev == NULL) { - o << tmp->id; - } else { - o << tmp->id << ","; - } - tmp = tmp->prev; + if (tmp->type->elementType != NULL){ //Array... + o << tmp->type->elementType << "*" << "\t"; + } else { + // o << tmp->type->elementType; + o << tmp->type << "\t"; + } + if (tmp->prev == NULL) { + o << tmp->id; + } else { + o << tmp->id << ","; + } + tmp = tmp->prev; } } - o << ")\n"; - /* Generate C-Code for the body! */ - o << "{\n"; - if (lastLocal) + o << ")\n"; + /* Generate C-Code for the body! */ + o << "{\n"; + if (lastLocal) { - o << "//Locals:\n"; + o << "//Locals:\n"; tmp = lastLocal; while (tmp != NULL) { - if (tmp->type->elementType != NULL){ //Array... - o << tmp->type->elementType; - o << "\t"; - o << tmp; - o << tmp->type << ";\n"; - } - else { - o << tmp->type << "\t" << tmp << ";\n"; - } - tmp = tmp->prev; + if (tmp->type->elementType != NULL){ //Array... + o << tmp->type->elementType; + o << "\t"; + o << tmp; + o << tmp->type << ";\n"; + } + else { + o << tmp->type << "\t" << tmp << ";\n"; + } + tmp = tmp->prev; } } - quads->print_c(o); - o << "}\n"; - break; - + quads->print_c(o); + o << "}\n"; + break; + default: o << "Bad output format.\n"; abort(); @@ -430,14 +430,14 @@ FunctionInformation *FunctionInformation::AddFunction(const ::string& name, if (xinfo != NULL && xinfo->tag == kTypeInformation) { cerr << "Bug: you tried to create a function that's also a type\n"; - abort(); + abort(); } xinfo = symbolTable.LookupSymbol(name); if (xinfo != NULL) { cerr << "Bug: you tried to create a function with a name " - << "that's already in use\n"; + << "that's already in use\n"; } fn->id = name; @@ -458,7 +458,7 @@ VariableInformation *FunctionInformation::TemporaryVariable(TypeInformation *typ return info; } - + char FunctionInformation::OkToAddSymbol(const ::string& name) {