void runCurrentFile(vector<string> &argvList, bool debug, bool debug_map) { // Build the AST used by ROSE if (debug) std::cout << ">>>> Starting ROSE frontend ... " << endl; SgProject* project = frontend(argvList); if (debug) std::cout << ">>>> generate PDF " << endl; generatePDF ( *project ); if (debug) std::cout << ">>>> start def-use analysis ... " << endl; // Call the Def-Use Analysis DFAnalysis* defuse = new DefUseAnalysis(project); int val = defuse->run(debug); if (debug) std::cout << "Analysis is : " << (val ? "failure" : "success" ) << " " << val <<std::endl; if (val==1) exit(1); if (debug==false) defuse->dfaToDOT(); LivenessAnalysis* liv = new LivenessAnalysis(true,(DefUseAnalysis*)defuse); //example usage // testing std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions; bool abortme=false; NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition); NodeQuerySynthesizedAttributeType::const_iterator i = vars.begin(); for (; i!=vars.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); string funcName = func->get_declaration()->get_qualified_name().str(); if (debug) cerr << " running live analysis for func : " << funcName << endl; FilteredCFGNode <IsDFAFilter> rem_source = liv->run(func,abortme); if (rem_source.getNode()!=NULL) dfaFunctions.push_back(rem_source); if (abortme) break; } std::ofstream f2("var.dot"); dfaToDot(f2, string("var"), dfaFunctions, (DefUseAnalysis*)defuse, liv); f2.close(); if (abortme) { cerr<<"ABORTING ." << endl; exit(1); } delete project; delete defuse; }
void _doLoad(const std::string& name, AST_Name* node) { int id = analysis->getStringIndex(name); Status& status = statuses[id]; status.addUsage(Status::USED); last_uses[id] = node; }
int main( int argc, char * argv[] ) { vector<string> argvList(argv, argv + argc); SgProject* project = frontend(argvList); if (project->get_fileList().size() ==0) return 0; // Prepare the Def-Use Analysis DFAnalysis* defuse = new DefUseAnalysis(project); bool debug = false; defuse->run(debug); if (debug) defuse->dfaToDOT(); // Prepare liveness analysis LivenessAnalysis* liv = new LivenessAnalysis(debug,(DefUseAnalysis*)defuse); ROSE_ASSERT (liv != NULL); // Find all function definitions Rose_STL_Container<SgNode*> nodeList= NodeQuery::querySubTree(project, V_SgFunctionDefinition); std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions; Rose_STL_Container<SgNode*>::const_iterator i = nodeList.begin(); bool abortme=false; for (; i!=nodeList.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); // run liveness analysis FilteredCFGNode <IsDFAFilter> rem_source = liv->run(func,abortme); if (abortme) { cerr<<"Error: Liveness analysis is ABORTING ." << endl; ROSE_ASSERT(false); } if (rem_source.getNode()!=NULL) dfaFunctions.push_back(rem_source); } SgFilePtrList file_list = project->get_fileList(); std::string firstFileName = StringUtility::stripPathFromFileName(file_list[0]->getFileName()); std::string fileName = firstFileName+"_liveness.dot" ; std::ofstream fs(fileName.c_str()); dfaToDot(fs, string("var"), dfaFunctions, (DefUseAnalysis*)defuse, liv); fs.close(); return 0; }
void _doStore(const std::string& name) { int id = analysis->getStringIndex(name); Status& status = statuses[id]; status.addUsage(Status::DEFINED); auto it = last_uses.find(id); if (it != last_uses.end()) { kills.insert(it->second); last_uses.erase(it); } }
bool isKilledAt(AST_Name* node, bool is_live_at_end) { if (kills.count(node)) return true; // If it's not live at the end, then the last use is a kill // even though we weren't able to determine that in a single // pass if (!is_live_at_end) { auto it = last_uses.find(analysis->getStringIndex(node->id)); if (it != last_uses.end() && node == it->second) return true; } return false; }
void _doStore(const std::string& name) { Status& status = statuses[analysis->getStringIndex(name)]; if (status == NONE) status = KILLED; }
void _doLoad(const std::string& name) { Status& status = statuses[analysis->getStringIndex(name)]; if (status == NONE) status = USED; }
void testOneFunction( std::string funcParamName, vector<string> argvList, bool debug, int nrOfNodes, multimap <int, vector<string> > resultsIn, multimap <int, vector<string> > resultsOut) { cout << " \n\n------------------------------------------\nrunning (variable)... " << argvList[1] << endl; // Build the AST used by ROSE SgProject* project = frontend(argvList); // Call the Def-Use Analysis DFAnalysis* defuse = new DefUseAnalysis(project); int val = defuse->run(debug); if (debug) std::cerr << ">Analysis run is : " << (val ? "failure" : "success" ) << " " << val << std::endl; if (val==1) exit(1); if (debug==false) defuse->dfaToDOT(); LivenessAnalysis* liv = new LivenessAnalysis(debug,(DefUseAnalysis*)defuse); std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions; NodeQuerySynthesizedAttributeType vars = NodeQuery::querySubTree(project, V_SgFunctionDefinition); NodeQuerySynthesizedAttributeType::const_iterator i = vars.begin(); bool abortme=false; int hitIn=0; int hitOut=0; for (; i!=vars.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); std::string name = func->class_name(); string funcName = func->get_declaration()->get_qualified_name().str(); if (debug) cerr << " .. running live analysis for func : " << funcName << endl; FilteredCFGNode <IsDFAFilter> rem_source = liv->run(func,abortme); if (abortme) break; if (funcName!=funcParamName) { if (debug) cerr << " .. skipping live analysis check for func : " << funcName << endl; continue; } if (rem_source.getNode()!=NULL) dfaFunctions.push_back(rem_source); NodeQuerySynthesizedAttributeType nodes = NodeQuery::querySubTree(func, V_SgNode); // Edg3 mistakenly adds SgType nodes to the AST; Edg4 adds some also, but fewer. So we just remove them all. They // make no difference in the variable-liveness analysis anyway. nodes.erase(std::remove_if(nodes.begin(), nodes.end(), is_type_node), nodes.end()); SgFunctionDeclaration* decl = isSgFunctionDeclaration(func->get_declaration()); ROSE_ASSERT(decl); Rose_STL_Container<SgInitializedName*> args = decl->get_parameterList()->get_args(); if (debug) cerr <<"Found args : " << args.size() << endl; Rose_STL_Container<SgInitializedName*>::const_iterator it = args.begin(); for (;it!=args.end();++it) { nodes.push_back(*it); } if((int)nodes.size()-1!=nrOfNodes) { cerr << "Error :: Number of nodes = " << nodes.size()-1 << " should be : " << nrOfNodes << endl; exit(1); } else { if (debug) cerr << "Investigating nodes : " << nodes.size() << endl; } NodeQuerySynthesizedAttributeType::const_iterator nodesIt = nodes.begin(); for (; nodesIt!=nodes.end();++nodesIt) { SgNode* node = *nodesIt; ROSE_ASSERT(node); int tableNr = defuse->getIntForSgNode(node); std::vector<SgInitializedName*> in = liv->getIn(node); std::vector<SgInitializedName*> out = liv->getOut(node); std::vector<string> inName; std::vector<string> outName; std::vector<SgInitializedName*>::const_iterator itv = in.begin(); for (;itv!=in.end();++itv) { SgInitializedName* init = *itv; string name = init->get_name(); inName.push_back(name); } itv = out.begin(); for (;itv!=out.end();++itv) { SgInitializedName* init = *itv; string name = init->get_name(); outName.push_back(name); } std::sort(inName.begin(), inName.end()); std::sort(outName.begin(), outName.end()); multimap <int, vector<string> >::const_iterator k =resultsIn.begin(); for (;k!=resultsIn.end();++k) { int resNr = k->first; vector<string> results = k->second; if (debug) cerr << " ... containing nodes : " << results.size() << " node: " << node->class_name() << " tableNr : " << tableNr << " resNr : " << resNr << endl; if (tableNr==resNr) { std::sort(results.begin(), results.end()); if (results==inName) { if (debug) cerr <<"Contents in IN vector is correct! " << endl; } else { if (debug) { cerr << " >>>>>>>>>> Problem with contents for IN ! " << endl; cerr << " >>>>>>>>>> RESULT ... " << endl; } std::vector<string>::const_iterator itv = inName.begin(); for (;itv!=inName.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) { cerr << endl; cerr << " >>>>>>>>>> USER ... " << endl; } itv = results.begin(); for (;itv!=results.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) { cerr << endl; } exit(1); } if (results.size()==in.size()) { hitIn++; } if (debug) cout << " nodeNr: " << tableNr << ". ResultSize IN should be:" << results.size() << " - resultSize is: " << in.size() << " foundMatches == : " << hitIn << "/" << resultsIn.size() << endl; } } k =resultsOut.begin(); for (;k!=resultsOut.end();++k) { int resNr = k->first; vector<string> results = k->second; // cerr << " ... containing nodes : " << results.size() << " tableNr : " << tableNr << // " resNr : " << resNr << endl; if (tableNr==resNr) { std::sort(results.begin(), results.end()); if (results==outName) { if (debug) cerr <<"Contents in OUT vector is correct! " << endl; } else { if (debug) { cerr << " >>>>>>>>>> Problem with contents for OUT ! " << endl; cerr << " >>>>>>>>>> RESULT ... " << endl; } std::vector<string>::const_iterator itv = outName.begin(); for (;itv!=outName.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) { cerr << endl; cerr << " >>>>>>>>>> USER ... " << endl; } itv = results.begin(); for (;itv!=results.end();++itv) { string name = *itv; if (debug) cerr << name << " " ; } if (debug) cerr << endl; exit(1); } if (results.size()==out.size()) { hitOut++; } if (debug) cout << " nodeNr: " << tableNr << ". ResultSize OUT should be:" << results.size() << " - resultSize is: " << out.size() << " foundMatches == : " << hitOut << "/" << resultsOut.size() << endl; } } if (hitIn==0 && hitOut==0) { if (debug) cout << " nodeNr: " << tableNr << " IN: " << hitIn << "/" << resultsIn.size() << " Out:" << hitOut << "/" << resultsOut.size() << endl; } } if (hitIn!=(int)resultsIn.size() || hitOut!=(int)resultsOut.size()) { cout << " Error: No hit! ... DFA values of node " << nrOfNodes << " are not correct! " << endl; exit(1); } } if (debug) cerr << "Writing out to var.dot... " << endl; std::ofstream f2("var.dot"); dfaToDot(f2, string("var"), dfaFunctions, (DefUseAnalysis*)defuse, liv); f2.close(); if (abortme) { cerr<<"ABORTING ." << endl; ROSE_ASSERT(false); // exit(1); } // iterate again and write second var.dot file i = vars.begin(); abortme=false; std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions2; for (; i!=vars.end();++i) { SgFunctionDefinition* func = isSgFunctionDefinition(*i); std::string name = func->class_name(); string funcName = func->get_declaration()->get_qualified_name().str(); if (debug) cerr << " .. running live analysis for func (fixupStatementsINOUT): " << funcName << endl; FilteredCFGNode <IsDFAFilter> rem_source = liv->run(func,abortme); liv->fixupStatementsINOUT(func); if (rem_source.getNode()!=NULL) dfaFunctions2.push_back(rem_source); } if (debug) cerr << "Writing out to varFix.dot... " << endl; std::ofstream f3("varFix.dot"); dfaToDot(f3, string("varFix"), dfaFunctions2, (DefUseAnalysis*)defuse, liv); f3.close(); if (debug) std::cout << "Analysis test is success." << std::endl; }