CallGraph.hpp

Go to the documentation of this file.
00001 
00015 //--------------------------------------------------------------------
00016 //--------------------------------------------------------------------
00017 
00018 #ifndef CallGraph_H
00019 #define CallGraph_H
00020 
00021 //--------------------------------------------------------------------
00022 // standard headers
00023 #ifdef NO_STD_CHEADERS
00024 # include <string.h>
00025 #else
00026 # include <cstring>
00027 #endif
00028 
00029 // STL headers
00030 #include <list>
00031 #include <map>
00032 
00033 // OpenAnalysis headers
00034 #include <OpenAnalysis/Utils/OA_ptr.hpp>
00035 #include <OpenAnalysis/Utils/DGraph/DGraphImplement.hpp>
00036 #include <OpenAnalysis/IRInterface/CallGraphIRInterface.hpp>
00037 
00038 #include <OpenAnalysis/Utils/Iterator.hpp>
00039 #include "CallGraphInterface.hpp"
00040 #include <OpenAnalysis/Utils/DGraph/DGraphInterface.hpp>
00041 //--------------------------------------------------------------------
00042 
00043 namespace OA {
00044   namespace CallGraph {
00045 
00046       class Node;
00047       class Edge;
00048       class NodesIterator;
00049       class EdgesIterator;
00050       class CallGraph;
00051       class CallGraphCalleeProcIter;
00052       class NodeCallsIterator;
00053 
00054       //=======================================================================
00059       //=========================================================================
00060 
00061 
00062       //=======================================================================
00064       class CallGraphCalleeProcIter : public virtual ProcHandleIterator,
00065                            public IRHandleSetIterator<ProcHandle>
00066       {
00067         public:
00068             
00069             CallGraphCalleeProcIter(OA_ptr<std::set<ProcHandle> > pSet);
00070     
00071            ~CallGraphCalleeProcIter();
00072 
00073             void operator++();
00074     
00075             bool isValid() const;
00076     
00077             ProcHandle current() const;
00078     
00079             void reset();
00080     
00081        };
00082 
00083   // ============================================================================= 
00087   // =============================================================================
00088 
00089   class NodesIterator : public DGraph::NodesIteratorImplement,
00090                         public virtual NodesIteratorInterface
00091   {
00092   public:
00093 
00094     NodesIterator(OA_ptr<DGraph::NodesIteratorInterface> ni);
00095 
00096     OA_ptr<NodeInterface> currentCallGraphNode() const;
00097 
00098   };
00099 
00100   //------------------------------------------------------------------
00101   class EdgesIterator : public DGraph::EdgesIteratorImplement, 
00102                         public virtual  EdgesIteratorInterface
00103   {
00104   public:
00105 
00106     EdgesIterator(OA_ptr<DGraph::EdgesIteratorInterface> ni);
00107 
00108     OA_ptr<EdgeInterface> currentCallGraphEdge() const;
00109 
00110   };
00111 
00112  
00113   //===========================================================  
00114 
00115   class Node : public virtual NodeInterface, 
00116                public virtual DGraph::NodeImplement
00117   {
00118   public:
00119     Node();
00120     Node(SymHandle s);
00121     ~Node ();
00122     
00124     //virtual unsigned int getId() const;
00125 
00126     bool isDefined() const;
00127     bool isCalled() const;
00128     
00129     ProcHandle getProc() const;
00130     SymHandle getProcSym() const;
00131 
00132     /*
00133     int num_incoming () const ;
00134     int num_outgoing () const ;
00135     */
00136 
00137     /*
00139     bool isAnEntry() const;
00140     bool isAnExit() const;
00141     */
00142 
00143     /*
00144     //========================================================
00145     // Comparison operators
00146     //========================================================
00147     bool operator== (DGraph::NodeInterface& other);
00148 
00149     bool operator< (DGraph::NodeInterface& other);
00150     */
00151 
00152     /*
00153     //========================================================
00154     // Construction
00155     //========================================================
00156     void addIncomingEdge(OA_ptr<DGraph::EdgeInterface> e);
00157 
00158     void addOutgoingEdge(OA_ptr<DGraph::EdgeInterface> e);
00159 
00160     void removeIncomingEdge(OA_ptr<DGraph::EdgeInterface> e);
00161     
00162     void removeOutgoingEdge(OA_ptr<DGraph::EdgeInterface> e);
00163     */
00164 
00165     virtual void output(OA::IRHandlesIRInterface& ir);
00166 
00167     void dump(std::ostream& os) {} // for full override
00168     void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00169     void longdump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00170     
00171     friend class NodeCallsIterator;
00172 
00173     //==================================================== 
00174     // Be Careful
00175     //===================================================
00176 
00177     void add_def(ProcHandle h);
00178 
00179     void add_call(CallHandle h);
00180 
00181   public:
00182     /*
00183     // Other Iterators
00184     OA_ptr<DGraph::EdgesIteratorInterface> 
00185            getIncomingEdgesIterator() const;
00186 
00187     OA_ptr<DGraph::EdgesIteratorInterface> 
00188            getOutgoingEdgesIterator() const;
00189 
00190     OA_ptr<DGraph::NodesIteratorInterface> 
00191            getSourceNodesIterator() const;
00192 
00193     OA_ptr<DGraph::NodesIteratorInterface> 
00194            getSinkNodesIterator() const;
00195            */
00196 
00197     OA_ptr<EdgesIteratorInterface> 
00198            getCallGraphIncomingEdgesIterator() const;
00199 
00200     OA_ptr<EdgesIteratorInterface> 
00201            getCallGraphOutgoingEdgesIterator() const;
00202 
00203     OA_ptr<NodesIteratorInterface> 
00204            getCallGraphSourceNodesIterator() const;
00205 
00206     OA_ptr<NodesIteratorInterface> 
00207            getCallGraphSinkNodesIterator() const;
00208 
00209   private:
00210     
00211 
00212     SymHandle mSym;      // should be identical across def and uses
00213     ProcHandle mProc;             // where this function is defined (if any)
00214     std::list<CallHandle> mCalls; // places this function is called (if any)
00215     //DGraph::NodeImplement mDGNode;
00216   };
00217   
00218   //--------------------------------------------------------
00219   class Edge : public virtual EdgeInterface,
00220                public virtual DGraph::EdgeImplement
00221 {
00222   public:
00223 
00224    Edge(OA_ptr<DGraph::NodeInterface> source,
00225              OA_ptr<DGraph::NodeInterface> sink, EdgeType _type,
00226              CallHandle call);
00227 
00228     ~Edge ();
00229     
00231     //virtual unsigned int getId() const;
00232     
00233     EdgeType getType() const;
00234 
00236     CallHandle getCallHandle() const;
00237    // void setCallHandle(CallHandle call) { mCallsiteExpr = call; }
00238 
00239 
00240     /* 
00241     // =====================================================================
00243     // ===================================================================
00244     
00246     OA_ptr<DGraph::NodeInterface> getSource () const;
00247     
00249     OA_ptr<DGraph::NodeInterface> getSink () const;
00250     */
00251 
00252     // ============================================================= 
00254     // =============================================================
00255 
00256     OA_ptr<NodeInterface> getCallGraphSource() const;
00257 
00258     OA_ptr<NodeInterface> getCallGraphSink() const;
00259 
00260 
00261     /*
00262     //=========================================================
00264     // =======================================================
00265 
00266     bool operator== (DGraph::EdgeInterface& other);
00267 
00268     bool Edge::operator< (DGraph::EdgeInterface& other);
00269     */
00270 
00271     virtual void output(OA::IRHandlesIRInterface& ir);
00272     
00273     void dump (std::ostream& os);
00274 
00275   private:
00276 
00277     EdgeType mType;
00278     CallHandle mCallsiteExpr;
00279     //DGraph::EdgeImplement mDGEdge;
00280   };  
00281 
00282   // =============================================================
00283 
00284   class NodeCallsIterator : public virtual NodeCallsIteratorInterface {
00285   public:
00286     NodeCallsIterator (OA_ptr<NodeInterface> node_) 
00287         { mNode = node_.convert<Node>(); 
00288           mIter = mNode->mCalls.begin(); }
00289     ~NodeCallsIterator () {}
00290     void operator ++ () { if (mIter != mNode->mCalls.end()) { ++mIter; } }
00291     bool isValid() const { return (mIter != mNode->mCalls.end()); }
00292     CallHandle current() const { return *mIter; }
00293   private:
00294     OA_ptr<Node> mNode;
00295     std::list<CallHandle>::iterator mIter;
00296   };
00297 
00298   // ===========================================================
00299   
00300 class CallGraph: public virtual CallGraphInterface,
00301                  public virtual DGraph::DGraphImplement
00302 {
00303   
00304 public:  // CallGraph class
00305 
00306   CallGraph (const SymHandle name);
00307   
00308   CallGraph();
00309   
00310   virtual ~CallGraph ();
00311 
00312   class NodeLabelListIterator;
00313   
00314   friend class NodeLabelListIterator;
00315 
00316   void connect (OA_ptr<NodeInterface> src, OA_ptr<NodeInterface> dst, 
00317                 EdgeType type, CallHandle call);
00318   
00319   void disconnect (OA_ptr<EdgeInterface> e);
00320 
00321   /*
00322   int getNumNodes();
00323    
00324   int getNumEdges();
00325   */
00326         
00327   OA_ptr<Node> findOrAddNode(SymHandle sym);
00328   
00329   void addToCallProcSetMap(CallHandle call, ProcHandle proc);
00330 
00331   typedef std::map<SymHandle, OA_ptr<Node> > SymToNodeMapType;
00332 
00333   void dump (std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00334   
00335   virtual void output(OA::IRHandlesIRInterface& ir);
00336 
00337  const OA::SymHandle subprog_name () { return mName; }
00338 
00339   //-------------------------------------
00340   // Iterators
00341   // Have same code here as in DGraphStandard.hpp because
00342   // these methods are meant to shadow those in DGraphStandard.hpp
00343   // so that we get subclass specific return values
00344   //-------------------------------------
00345 
00346  /*
00347   OA_ptr<DGraph::NodesIteratorInterface> getNodesIterator() const;
00348 
00349   OA_ptr<DGraph::EdgesIteratorInterface> getEdgesIterator() const;
00350 
00351   OA_ptr<DGraph::NodesIteratorInterface> 
00352       getReversePostDFSIterator(DGraph::DGraphEdgeDirection pOrient);
00353 
00354   OA_ptr<DGraph::NodesIteratorInterface>
00355       getDFSIterator(OA_ptr<DGraph::NodeInterface> n);
00356 
00357   OA_ptr<DGraph::NodesIteratorInterface> 
00358       getEntryNodesIterator() const;
00359 
00360   OA_ptr<DGraph::NodesIteratorInterface> 
00361       getExitNodesIterator() const;
00362   */    
00363 
00364   OA_ptr<NodesIteratorInterface> 
00365       getCallGraphNodesIterator() const;
00366 
00367   OA_ptr<EdgesIteratorInterface> 
00368       getCallGraphEdgesIterator() const;
00369 
00370   OA_ptr<NodesIteratorInterface>
00371       getCallGraphReversePostDFSIterator(DGraph::DGraphEdgeDirection pOrient);
00372 
00373   OA_ptr<NodesIteratorInterface>
00374       getCallGraphDFSIterator(OA_ptr<NodeInterface> n);
00375 
00376 
00377   OA_ptr<NodesIteratorInterface> 
00378       getCallGraphEntryNodesIterator() const;
00379 
00380   OA_ptr<NodesIteratorInterface> 
00381       getCallGraphExitNodesIterator() const;
00382 
00383    /*
00384    //========================================================
00385    // Construction
00386    //========================================================
00387    void addNode(OA_ptr<DGraph::NodeInterface> n);
00388 
00389    void addEdge(OA_ptr<DGraph::EdgeInterface> e);
00390 
00391    void removeEdge(OA_ptr<DGraph::EdgeInterface> e);
00392 
00393    void removeNode(OA_ptr<DGraph::NodeInterface> n);
00394    */
00395    
00399   OA_ptr<ProcHandleIterator> getCalleeProcIter(CallHandle call);
00400 
00401 private:
00402   const SymHandle mName;
00403   
00404   SymToNodeMapType mSymToNodeMap;
00405 
00406   std::map<CallHandle, OA_ptr<std::set<ProcHandle> > > mCallToCalleeProcSetMap;
00407 
00408   //DGraph::DGraphImplement mDGraph;
00409 };
00410 //--------------------------------------------------------------------
00411 
00412   } // end of CallGraph namespace
00413 } // end of OA namespace
00414 
00415 #endif

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