// Substitute s into all members of the set void LocationSet::substitute(Assign& a) { Exp* lhs = a.getLeft(); if (lhs == NULL) return; Exp* rhs = a.getRight(); if (rhs == NULL) return; // ? Will this ever happen? std::set<Exp*, lessExpStar>::iterator it; // Note: it's important not to change the pointer in the set of pointers to expressions, without removing and // inserting again. Otherwise, the set becomes out of order, and operations such as set comparison fail! // To avoid any funny behaviour when iterating the loop, we use the following two sets LocationSet removeSet; // These will be removed after the loop LocationSet removeAndDelete; // These will be removed then deleted LocationSet insertSet; // These will be inserted after the loop bool change; for (it = lset.begin(); it != lset.end(); it++) { Exp* loc = *it; Exp* replace; if (loc->search(lhs, replace)) { if (rhs->isTerminal()) { // This is no longer a location of interest (e.g. %pc) removeSet.insert(loc); continue; } loc = loc->clone()->searchReplaceAll(lhs, rhs, change); if (change) { loc = loc->simplifyArith(); loc = loc->simplify(); // If the result is no longer a register or memory (e.g. // r[28]-4), then delete this expression and insert any // components it uses (in the example, just r[28]) if (!loc->isRegOf() && !loc->isMemOf()) { // Note: can't delete the expression yet, because the // act of insertion into the remove set requires silent // calls to the compare function removeAndDelete.insert(*it); loc->addUsedLocs(insertSet); continue; } // Else we just want to replace it // Regardless of whether the top level expression pointer has // changed, remove and insert it from the set of pointers removeSet.insert(*it); // Note: remove the unmodified ptr insertSet.insert(loc); } } } makeDiff(removeSet); // Remove the items to be removed makeDiff(removeAndDelete); // These are to be removed as well makeUnion(insertSet); // Insert the items to be added // Now delete the expressions that are no longer needed std::set<Exp*, lessExpStar>::iterator dd; for (dd = removeAndDelete.lset.begin(); dd != removeAndDelete.lset.end(); dd++) delete *dd; // Plug that memory leak }
void BoolAssign::setLeftFromList(const std::list<Statement *> &stmts) { assert(stmts.size() == 1); Assign *first = static_cast<Assign *>(stmts.front()); assert(first->getKind() == StmtType::Assign); m_lhs = first->getLeft(); }
bool StrengthReductionReversalPass::execute(UserProc *proc) { StatementList stmts; proc->getStatements(stmts); for (Statement *s : stmts) { if (!s->isAssign()) { continue; } Assign *as = static_cast<Assign *>(s); // of the form x = x{p} + c if ((as->getRight()->getOper() == opPlus) && as->getRight()->getSubExp1()->isSubscript() && (*as->getLeft() == *as->getRight()->getSubExp1()->getSubExp1()) && as->getRight()->getSubExp2()->isIntConst()) { int c = as->getRight()->access<Const, 2>()->getInt(); auto r = as->getRight()->access<RefExp, 1>(); if (r->getDef() && r->getDef()->isPhi()) { PhiAssign *p = static_cast<PhiAssign *>(r->getDef()); if (p->getNumDefs() == 2) { Statement *first = p->begin()->getDef(); Statement *second = p->rbegin()->getDef(); if (first == as) { // want the increment in second std::swap(first, second); } // first must be of form x := 0 if (first && first->isAssign() && static_cast<Assign *>(first)->getRight()->isIntConst() && static_cast<Assign *>(first)->getRight()->access<Const>()->getInt() == 0) { // ok, fun, now we need to find every reference to p and // replace with x{p} * c StatementList stmts2; proc->getStatements(stmts2); for (Statement *stmt2 : stmts2) { if (stmt2 != as) { stmt2->searchAndReplace( *r, Binary::get(opMult, r->clone(), Const::get(c))); } } // that done we can replace c with 1 in as as->getRight()->access<Const, 2>()->setInt(1); } } } } } return true; }
void DefCollector::updateDefs(std::map<Exp*, std::stack<Statement*>, lessExpStar>& Stacks, UserProc* proc) { std::map<Exp*, std::stack<Statement*>, lessExpStar>::iterator it; for (it = Stacks.begin(); it != Stacks.end(); it++) { if (it->second.size() == 0) continue; // This variable's definition doesn't reach here // Create an assignment of the form loc := loc{def} RefExp* re = new RefExp(it->first->clone(), it->second.top()); Assign* as = new Assign(it->first->clone(), re); as->setProc(proc); // Simplify sometimes needs this insert(as); } initialised = true; }
Statement toMicroSt::operator()(Assign v) const { Expression l=v.left(); Expression r=v.left(); OptExp opt_r=v.right(); unsigned int i=0; toMicroExp tom(mmo_class, i, false,true); l = boost::apply_visitor(tom, l); if (opt_r) opt_r = boost::apply_visitor(tom, opt_r.get()); v.left_ref() = l; v.right_ref() = opt_r; return v; }
/*** Parse an assign statement ***/ Assign* Parser::parseAssign() { if(!hasTokens() || peekToken().getType() != T_IDENTIFIER) throw ParserSyntaxException(getToken(), "Expected variable identifier!"); std::auto_ptr<Variable> target(new Variable(getToken().getLexeme())); if(!hasTokens() || peekToken().getType() != T_ASSIGN) throw ParserSyntaxException(getToken(), "Expected ':='!"); Token tok = getToken(); std::auto_ptr<Object> expr(parseExpression()); Assign* as = new Assign(target.release(), expr.release()); as->setLineNumber(tok.getLineNumber()); as->setColumnNumber(tok.getColumnNumber()); return as; }
int main() { codegen::Context ctx; Immediate<int64_t> *imm_i = new Immediate<int64_t>(42); Immediate<double> *imm_f = new Immediate<double>(42.0); imm_f->codegen(ctx)->dump(); imm_i->codegen(ctx)->dump(); BAO *addfi = new BAO(op::ADD, imm_f, imm_i); addfi->codegen(ctx)->dump(); BooleanTrue().codegen(ctx)->dump(); BooleanFalse().codegen(ctx)->dump(); Nonzero(addfi).codegen(ctx)->dump(); Compare *cmp_t = new Compare(op::LT, imm_i, addfi); Compare *cmp_f = new Compare(op::GT, imm_i, addfi); cmp_t->codegen(ctx)->dump(); cmp_f->codegen(ctx)->dump(); BBO *bbo_t = new BBO(op::XOR, cmp_f, cmp_t); bbo_t->codegen(ctx)->dump(); std::string name = "name"; Assign *assign = new Assign(name, addfi); assign->codegen(ctx)->dump(); return 0; }
void RtlTest::testVisitor() { StmtVisitorStub* visitor = new StmtVisitorStub(); /* rtl */ RTL *rtl = new RTL(); rtl->accept(visitor); CPPUNIT_ASSERT(visitor->a); delete rtl; /* jump stmt */ GotoStatement *jump = new GotoStatement; jump->accept(visitor); CPPUNIT_ASSERT(visitor->b); delete jump; /* branch stmt */ BranchStatement *jcond = new BranchStatement; jcond->accept(visitor); CPPUNIT_ASSERT(visitor->c); delete jcond; /* nway jump stmt */ CaseStatement *nwayjump = new CaseStatement; nwayjump->accept(visitor); CPPUNIT_ASSERT(visitor->d); delete nwayjump; /* call stmt */ CallStatement *call = new CallStatement; call->accept(visitor); CPPUNIT_ASSERT(visitor->e); delete call; /* return stmt */ ReturnStatement *ret = new ReturnStatement; ret->accept(visitor); CPPUNIT_ASSERT(visitor->f); delete ret; /* "bool" assgn */ BoolAssign *scond = new BoolAssign(0); scond->accept(visitor); CPPUNIT_ASSERT(visitor->g); delete scond; /* assignment stmt */ Assign *as = new Assign; as->accept(visitor); CPPUNIT_ASSERT(visitor->h); delete as; /* polymorphic */ Statement* s = new CallStatement; s->accept(visitor); CPPUNIT_ASSERT(visitor->e); delete s; /* cleanup */ delete visitor; }
void Load::Execute() { Output *pOut = pManager->GetOutput(); if (pManager->StatCount>0) { pOut->MsgBox("You Should delete any existing statements before loading a new flowchart !!", "Error", true); return; } ifstream In; In.open("Out.txt"); if (!In.good()) { pOut->MsgBox("File couldn't be Loaded !!", "Error", true); return; } int N1; In >> N1; string ST; for (int i = 0; i <N1; i++) { In >> ST; if (ST=="START") { Start* pT = new Start; pT->Load(In, pOut); pManager->AddStatement(pT,true); } else if (ST=="END") { End* pT = new End; pT->Load(In,pOut); pManager->AddStatement(pT, true); } else if (ST == "ASSIGN") { Assign* pT = new Assign; pT->Load(In, pOut); pManager->AddStatement(pT, true); } else if (ST == "COND") { Cond* pT = new Cond; pT->Load(In, pOut); pManager->AddStatement(pT, true); } else if (ST == "WRITE") { Write* pT = new Write; pT->Load(In, pOut); pManager->AddStatement(pT, true); } else if (ST == "READ") { Read* pT = new Read; pT->Load(In, pOut); pManager->AddStatement(pT, true); } } int N2; In >> N2; for (int i = 0; i < N2; i++) { int s, d,t; In >> s >> d>>t; Statement *S, *D; S = D = NULL; Point p1, p2; for (int j = 0; j < pManager->StatCount; j++) { if (s == pManager->StatList[j]->getID()) { S = pManager->StatList[j]; p1 = pManager->StatList[j]->getOutlet(); } if (d == pManager->StatList[j]->getID()) { D = pManager->StatList[j]; p2 = pManager->StatList[j]->getInlet(); } if (S && D) break; } /*Connector* pT = new Connector(S,D); pT->setStartPoint(p1); pT->setEndPoint(p2); pManager->AddConn(pT); pT->Draw(pManager->pOut);*/ pManager->AddSelected(S); pManager->AddSelected(D); Action* pAct = NULL; pAct = new AddConnector(pManager); pAct->Execute(); } In.close(); pOut->MsgBox("File Loaded successfully !", "Info", false); }