NotationGenerator.cpp

Go to the documentation of this file.
00001 
00008 #include "NotationGenerator.hpp"
00009 
00010 int gIndentLevel = 0;
00011 std::ostream &indt(std::ostream &os)
00012 {
00013     os << '\n';
00014     os.flush();
00015     for(int i = 0; i < gIndentLevel; i++)
00016         os << "    ";
00017     return os;
00018 }
00019 
00020 std::ostream &pushIndt(std::ostream &os)
00021 {
00022     gIndentLevel++;
00023     return os;
00024 }
00025 
00026 std::ostream &popIndt(std::ostream &os)
00027 {
00028     gIndentLevel--;
00029     return os;
00030 }
00031 
00032 NotationGenerator::NotationGenerator(OA::OA_ptr<OA::Alias::AliasIRInterface>
00033     ir, std::ostream &codeStream)
00034     :
00035     mIR(ir),
00036     mCodeStream(codeStream)
00037 {
00038 }
00039 
00040 void NotationGenerator::generate(OA::ProcHandle proc)
00041 {
00042     outputProcedure(proc);
00043     outputFormals(proc);
00044 
00045     // iterate over all statements in the procedure and output them
00046     OA::OA_ptr<OA::IRStmtIterator> i = mIR->getStmtIterator(proc);
00047     for(; i->isValid(); (*i)++) {
00048         outputStmt(i->current());
00049     }
00050 
00051     outputLocations(proc);
00052 
00053     mCodeStream << popIndt;
00054     mCodeStream << std::endl;
00055 }
00056 
00057 void NotationGenerator::outputProcedure(OA::ProcHandle proc)
00058 {
00059     // example procedure:
00060     //     PROCEDURE = { < ProcHandle("main"), SymHandle("main") > }
00061 
00062     mCodeStream << indt << "PROCEDURE = { < ";
00063     outputHandle(proc);
00064     mCodeStream << ", ";
00065     outputHandle(mIR->getSymHandle(proc));
00066     mCodeStream << " > }";
00067     mCodeStream << pushIndt;
00068 }
00069 
00070 void NotationGenerator::outputFormals(OA::ProcHandle proc)
00071 {
00072     // example formals:
00073     //     FORMALS = { 
00074     //         [
00075     //             < 1, SymHandle("p1") >
00076     //             < 2, SymHandle("p2") >
00077     //             < 3, SymHandle("p3") >
00078     //         ] }
00079 
00080     // a formals clause only needs to be outputted if atleast 1 formal exists.
00081     // to determine whether one exists, try to grab the symhandle for the 
00082     // first formal. The IR will return SymHandle(0) if the formal does not
00083     // exist.
00084     int formalId = 0;
00085     OA::SymHandle sym = mIR->getFormalSym(proc, formalId);
00086     if(sym == OA::SymHandle(0)) { return; }
00087 
00088     mCodeStream << indt << "FORMALS = {" << pushIndt;
00089     mCodeStream << indt << "[" << pushIndt;
00090     while ( sym != OA::SymHandle(0) ) {
00091         mCodeStream << indt << "< " << formalId << ", ";
00092         outputHandle(sym);
00093         mCodeStream << " >";
00094         ++formalId;
00095         sym = mIR->getFormalSym(proc, formalId);
00096     }
00097     mCodeStream << popIndt << indt << "] }" << popIndt;
00098 }
00099 
00100 void NotationGenerator::outputStmt(OA::StmtHandle stmt)
00101 {
00102     // statements can consist of the following clauses:
00103     //     MEMREFEXPRS, PTRASSIGNPAIRS, CALLSITES, PARAMBINDPTRASSIGNPAIRS 
00104 
00105     outputMemRefExprs(stmt);
00106     outputPtrAssignPairs(stmt);
00107     outputCallSites(stmt);
00108     outputParamBindPtrAssignPairs(stmt);
00109 }
00110 
00111 void NotationGenerator::outputMemRefExprs(OA::StmtHandle stmt)
00112 {
00113    // example clause:
00114    //     MEMREFEXPRS = { StmtHandle("cptr1 = &c;") =>
00115    //         [
00116    //             MemRefHandle("cptr1_1") =>
00117    //                 NamedRef( DEF, SymHandle("cptr1") )
00118    //             MemRefHandle("&c_1") =>
00119    //                 NamedRef( USE, SymHandle("c"), T, full )
00120    //         ] }
00121 
00122     mCodeStream << indt << "MEMREFEXPRS = { ";
00123       outputHandle(stmt);
00124       mCodeStream << " =>" << pushIndt;
00125     mCodeStream << indt << "[" << pushIndt;
00126 
00127     OA::OA_ptr<OA::MemRefHandleIterator> i = mIR->getAllMemRefs(stmt);
00128     for(; i->isValid(); (*i)++) {
00129         OA::OA_ptr<OA::MemRefExprIterator> j =
00130             mIR->getMemRefExprIterator(i->current());
00131         for(; j->isValid(); (*j)++) {
00132             mCodeStream << indt;
00133               outputHandle(i->current());
00134               mCodeStream << " => " << pushIndt;
00135             mCodeStream << indt;
00136               outputMemRefExpr(j->current());
00137             mCodeStream << popIndt;
00138         }
00139     }
00140 
00141     mCodeStream << popIndt << indt << "] }" << popIndt;
00142 }
00143 
00144 void NotationGenerator::outputPtrAssignPairs(OA::StmtHandle stmt)
00145 {
00146     // example clause:
00147     //    PTRASSIGNPAIRS = { StmtHandle("cptr1 = &c;") =>
00148     //        [
00149     //            < NamedRef( DEF, SymHandle("cptr1") ),
00150     //              NamedRef( USE, SymHandle("c"), T, full ) >
00151     //        ] }
00152 
00153     // if a statement is not of type PTR_ASSIGN_STMT then it can not
00154     // contain a PTRASSIGNPAIRS clause.
00155    
00156     OA::OA_ptr<OA::Alias::PtrAssignPairStmtIterator> ii;
00157     ii = mIR->getPtrAssignStmtPairIterator(stmt);
00158 
00159     if(ii->isValid()) {
00160        mCodeStream << indt << "PTRASSIGNPAIRS = { ";
00161          outputHandle(stmt);
00162          mCodeStream << " =>" << pushIndt;
00163        mCodeStream << indt << "[" << pushIndt;
00164     }
00165 
00166     OA::OA_ptr<OA::Alias::PtrAssignPairStmtIterator> i;
00167     i = mIR->getPtrAssignStmtPairIterator(stmt);
00168     for(; i->isValid(); (*i)++) {
00169         mCodeStream << indt << "< ";
00170           outputMemRefExpr(i->currentTarget());
00171         mCodeStream << indt << ", ";
00172           outputMemRefExpr(i->currentSource());
00173           mCodeStream << " >";
00174     }
00175 
00176 
00177     OA::OA_ptr<OA::Alias::PtrAssignPairStmtIterator> iii;
00178     iii = mIR->getPtrAssignStmtPairIterator(stmt);
00179     if(iii->isValid()) {
00180        mCodeStream << popIndt << indt << "] }" << popIndt;
00181     } 
00182 }
00183     
00184 void NotationGenerator::outputCallSites(OA::StmtHandle stmt)
00185 {
00186     // example clause:
00187     //    CALLSITES = { StmtHandle("foo( S.lf, B, c);") =>
00188     //        [
00189     //            CallHandle("foo( S.lf, B, c);") =>
00190     //                NamedRef( USE, SymHandle("foo"), F, full )
00191     //        ] }
00192 
00193     OA::OA_ptr<OA::IRCallsiteIterator> i;
00194     i = mIR->getCallsites(stmt);
00195     if(!i->isValid()) { return; }
00196 
00197     mCodeStream << indt << "CALLSITES = { ";
00198       outputHandle(stmt);
00199       mCodeStream << " =>" << pushIndt;
00200     mCodeStream << indt << "[" << pushIndt;
00201 
00202     for(; i->isValid(); (*i)++) {
00203         mCodeStream << indt;
00204           outputHandle(i->current());
00205           mCodeStream << " => " << pushIndt;
00206         mCodeStream << indt;
00207           outputMemRefExpr(mIR->getCallMemRefExpr(i->current()));
00208           mCodeStream << popIndt;
00209     }
00210 
00211     mCodeStream << popIndt << indt << "] }" << popIndt;
00212 }
00213 
00214 void NotationGenerator::outputParamBindPtrAssignPairs(OA::StmtHandle stmt)
00215 {
00216     // iterate over all callsites generating PARAMBINDPTRASSIGNPAIRS
00217     // for them
00218     OA::OA_ptr<OA::IRCallsiteIterator> i;
00219     i = mIR->getCallsites(stmt);
00220     for(; i->isValid(); (*i)++) {
00221         outputParamBindPtrAssignPairs(stmt, (*i).current());
00222     }
00223 }
00224 
00225 void NotationGenerator::outputParamBindPtrAssignPairs(OA::StmtHandle stmt,
00226     OA::CallHandle call)
00227 {
00228     // example clause:
00229     //    PARAMBINDPTRASSIGNPAIRS = { CallHandle("foo( S.lf, B, c);") => 
00230     //        [
00231     //            < 2, NamedRef( USE, SymHandle("B"), T, full ) >
00232     //            < 3, NamedRef( USE, SymHandle("c"), T, full ) >
00233     //        ] }
00234 
00235     OA::OA_ptr<OA::Alias::ParamBindPtrAssignIterator> i;
00236     i = mIR->getParamBindPtrAssignIterator(call);
00237     if(!i->isValid()) { return; }
00238 
00239     mCodeStream << indt << "PARAMBINDPTRASSIGNPAIRS = { ";
00240       outputHandle(call);
00241       mCodeStream << " =>" << pushIndt;
00242     mCodeStream << indt << "[" << pushIndt;
00243 
00244     for(; i->isValid(); (*i)++) {
00245         int formalId = (*i).currentFormalId();
00246         OA::OA_ptr<OA::MemRefExpr> actual = (*i).currentActual();
00247 
00248         mCodeStream << indt << "< "
00249                     << (formalId) << ", ";
00250           outputMemRefExpr(actual);
00251           mCodeStream << " >";
00252     }
00253 
00254     mCodeStream << popIndt << indt << "] }" << popIndt;
00255 }
00256 
00257 void NotationGenerator::outputMemRefExpr(OA::OA_ptr<OA::MemRefExpr> expr)
00258 {
00259     // determine what type of MemRefExpr this is and call the proper
00260     // function to output it
00261     NGOutputVisitor visitor(*this);
00262     expr->acceptVisitor(visitor);
00263 }
00264 
00265 void NotationGenerator::outputMemRefExpr(OA::NamedRef& expr)
00266 {
00267     // example: NamedRef( USE, SymHandle("S"), T, part )
00268 
00269     mCodeStream << "NamedRef( ";
00270     outputMemRefType(expr);
00271     mCodeStream << ", ";
00272     outputHandle(expr.getSymHandle());
00273     mCodeStream << ")";
00274 }
00275 
00276 void NotationGenerator::outputMemRefExpr(OA::UnnamedRef& expr)
00277 {
00278     mCodeStream << "UnnamedRef( ";
00279     outputMemRefType(expr);
00280     mCodeStream << ", ";
00281     outputHandle(expr.getExprHandle());
00282     mCodeStream << ", ";
00283     mCodeStream << expr.isLocal();
00284     if(expr.isLocal() == true) {
00285        mCodeStream << ", ";
00286        outputHandle(expr.getProcHandle());
00287     }
00288     mCodeStream << ")";
00289 }
00290 
00291 void NotationGenerator::outputMemRefExpr(OA::UnknownRef& expr)
00292 {
00293     mCodeStream << "UnknownRef( ";
00294     outputMemRefType(expr);
00295     mCodeStream << ")";
00296 }
00297 
00298 void NotationGenerator::outputMemRefExpr(OA::AddressOf& expr)
00299 {
00300     mCodeStream << "AddressOf( ";
00301     outputMemRefType(expr);
00302     mCodeStream << ", ";
00303     outputMemRefExpr(expr.getMemRefExpr());
00304     mCodeStream << ")";
00305 }
00306 
00307 void NotationGenerator::outputMemRefExpr(OA::Deref& expr)
00308 {
00309     //example: Deref( USE, NamedRef ( USE, SymHandle("cptr2") ) )
00310 
00311     mCodeStream << "Deref( ";
00312     outputMemRefType(expr);
00313     mCodeStream << ", ";
00314     outputMemRefExpr(expr.getMemRefExpr());
00315     mCodeStream << ", ";
00316     mCodeStream << expr.getNumDerefs();
00317     mCodeStream << ")";
00318 }
00319 
00320 void NotationGenerator::outputMemRefExpr(OA::FieldAccess& expr)
00321 {
00322     //example: Deref( USE, NamedRef ( USE, SymHandle("cptr2") ) )
00323 
00324     mCodeStream << "FieldAccess( ";
00325     outputMemRefType(expr);
00326     mCodeStream << ", ";
00327     outputMemRefExpr(expr.getMemRefExpr());
00328     mCodeStream << ", ";
00329     mCodeStream << expr.getFieldName();
00330     mCodeStream << ")";
00331 }
00332 
00333 void NotationGenerator::outputMemRefExpr(OA::IdxExprAccess& expr)
00334 {
00335     mCodeStream << "IdxExprAccess( ";
00336     outputMemRefType(expr);
00337     mCodeStream << ", ";
00338     outputMemRefExpr(expr.getMemRefExpr());
00339     mCodeStream << ", ";
00340     outputHandle(expr.getExpr());
00341     mCodeStream << ")";
00342 }
00343 
00344 void NotationGenerator::outputMemRefExpr(OA::IdxAccess& expr)
00345 {
00346     mCodeStream << "IdxAccess( ";
00347     outputMemRefType(expr);
00348     mCodeStream << ", ";
00349     outputMemRefExpr(expr.getMemRefExpr());
00350     mCodeStream << ", ";
00351     mCodeStream << expr.getIdx() << ")";
00352 }
00353 
00354 
00355 // Added PLM 1/23/07
00356 void NotationGenerator::outputMemRefExpr(OA::SubSetRef& expr)
00357 {
00358     mCodeStream << "SubSetRef( ";
00359     outputMemRefType(expr);
00360     mCodeStream << ", ";
00361     outputMemRefExpr(expr.getMemRefExpr());
00362     mCodeStream << ")";
00363 }
00364 
00365 
00366 void NotationGenerator::outputHandle(OA::StmtHandle stmt)
00367 {
00368     mCodeStream << "StmtHandle(\"";
00369     mCodeStream << mIR->toString(stmt);
00370     mCodeStream << "\")";
00371 }
00372 
00373 
00374 void NotationGenerator::outputHandle(OA::ExprHandle expr)
00375 {
00376     mCodeStream << "ExprHandle(\"";
00377     mCodeStream << mIR->toString(expr);
00378     mCodeStream << "\")";
00379 }
00380 
00381 
00382 void NotationGenerator::outputHandle(OA::MemRefHandle h)
00383 {
00384     mCodeStream << "MemRefHandle(\"";
00385     mCodeStream << mIR->toString(h);
00386     mCodeStream << "\")";
00387 }
00388 
00389 void NotationGenerator::outputHandle(OA::ProcHandle h)
00390 {
00391     mCodeStream << "ProcHandle(\"";
00392     mCodeStream << mIR->toString(h);
00393     mCodeStream << "\")";
00394 }
00395 
00396 void NotationGenerator::outputMemRefType(OA::MemRefExpr &expr)
00397 {
00398     if(expr.isDefUse()) { mCodeStream << "DEFUSE"; }
00399     else if(expr.isUseDef()) { mCodeStream << "USEDEF"; }
00400     else if(expr.isDef())    { mCodeStream << "DEF"; }
00401     else if(expr.isUse())    { mCodeStream << "USE"; }
00402 }
00403 
00404 void NotationGenerator::outputHandle(OA::SymHandle h)
00405 {
00406     mCodeStream << "SymHandle(\"";
00407     mCodeStream << mIR->toString(h);
00408     mCodeStream << "\")";
00409     mSymHandles.insert(h);
00410 }
00411 
00412 void NotationGenerator::outputHandle(OA::CallHandle h)
00413 {
00414     mCodeStream << "CallHandle(\"";
00415     mCodeStream << mIR->toString(h);
00416     mCodeStream << "\")";
00417 }
00418 
00419 void NotationGenerator::outputTF(bool hasAddressTaken)
00420 {
00421     if(hasAddressTaken) { mCodeStream << "T"; }
00422     else { mCodeStream << "F"; }
00423 }
00424 
00425 void NotationGenerator::outputAccuracy(bool hasFullAccuracy)
00426 {
00427     if(hasFullAccuracy) { mCodeStream << "full"; }
00428     else { mCodeStream << "part"; }
00429 }
00430 
00431 void NotationGenerator::outputLocations(OA::ProcHandle proc)
00432 {
00433     // iterate over all SymHandles that were found while the notation
00434     // generator was outputting statements
00435     std::set<OA::SymHandle>::iterator i = mSymHandles.begin();
00436     for(; i != mSymHandles.end(); i++)
00437     {
00438         outputLocation(proc, *i);
00439     }
00440 }
00441 
00442 void NotationGenerator::outputLocation(OA::ProcHandle proc, OA::SymHandle sym)
00443 {
00444     // example clause:
00445     //     LOCATION = { < SymHandle("p1"), local > }
00446 
00447     OA::OA_ptr<OA::Location> loc = mIR->getLocation(proc, sym);
00448     if(loc.ptrEqual(0)) { return; }
00449     bool isLocal = loc->isLocal();
00450 
00451 
00452     mCodeStream << indt << "LOCATION = { < ";
00453       outputHandle(sym);
00454       mCodeStream << ", ";
00455       mCodeStream << (isLocal ? "local" : "not local");
00456       mCodeStream << " > }";
00457 }
00458 

Generated on Sat Oct 31 05:21:25 2009 for OpenAnalysis by  doxygen 1.6.1