CFG.hpp

Go to the documentation of this file.
00001 
00016 #ifndef CFG_H
00017 #define CFG_H
00018 
00019 //--------------------------------------------------------------------
00020 // standard headers
00021 #ifdef NO_STD_CHEADERS
00022 # include <string.h>
00023 #else
00024 # include <cstring>
00025 #endif
00026 
00027 // STL headers
00028 #include <list>
00029 #include <set>
00030 #include <map>
00031 
00032 // OpenAnalysis headers
00033 #include <OpenAnalysis/Utils/OA_ptr.hpp>
00034 #include <OpenAnalysis/Utils/DGraph/DGraphImplement.hpp>
00035 #include <OpenAnalysis/IRInterface/CFGIRInterface.hpp>
00036 #include <OpenAnalysis/CFG/CFGInterface.hpp>
00037 
00038 namespace OA {
00039   namespace CFG {
00040 
00041 
00042 //--------------------------------------------------------------------
00045 //--------------------------------------------------------------------
00046 
00047   class Node;
00048   class Edge;
00049   class CFG;
00050   //------------------------------------------------------------------
00051   class NodeStatementsIterator;
00052   class NodeStatementsRevIterator;
00053   class NodesIterator;
00054   class EdgesIterator;
00055 
00056   //------------------------------------------------------------------
00059   class NodesIterator : 
00060                        public DGraph::NodesIteratorImplement, 
00061                        public virtual NodesIteratorInterface 
00062   {
00063      public:
00064         NodesIterator(OA_ptr<DGraph::NodesIteratorInterface> ni);
00065     
00066         OA_ptr<NodeInterface> currentCFGNode() const;
00067     
00068   };
00069 
00070   //------------------------------------------------------------------
00071   class EdgesIterator : 
00072                        public DGraph::EdgesIteratorImplement, 
00073                        public virtual EdgesIteratorInterface 
00074   {
00075      public:
00076         EdgesIterator(OA_ptr<DGraph::EdgesIteratorInterface> ni)
00077             : DGraph::EdgesIteratorImplement(ni) {}
00078 
00079         OA_ptr<EdgeInterface> currentCFGEdge() const;
00080 
00081   };
00082    
00083   //--------------------------------------------------------------------
00086   //--------------------------------------------------------------------
00087   class Node : public virtual NodeInterface, 
00088                public virtual DGraph::NodeImplement
00089   {
00090   public:
00091       
00092         Node();
00093         Node(StmtHandle n);
00094        ~Node();
00095     
00096        //=========================================================
00097        // Information
00098        //=========================================================
00099     
00101        //from DGraph::NodeImplement
00102    
00103         /*
00104         unsigned int getId() const;
00105 
00106         int num_incoming () const ; 
00107         int num_outgoing () const ;
00108 
00110         bool isAnEntry() const;
00111 
00113         bool isAnExit() const;
00114         */
00115         
00117         unsigned int size() const;
00118 
00120          bool empty() const;
00121 
00122         /* 
00123         //========================================================
00124         // Comparison operators
00125         //========================================================
00126         bool operator== (DGraph::NodeInterface& other);
00127 
00128         bool operator< (DGraph::NodeInterface& other);
00129 
00130 
00131         //========================================================
00132         // Construction
00133         //========================================================
00134         void addIncomingEdge(OA_ptr<DGraph::EdgeInterface> e);
00135         
00136         void addOutgoingEdge(OA_ptr<DGraph::EdgeInterface> e);
00137 
00138         void removeIncomingEdge(OA_ptr<DGraph::EdgeInterface> e);
00139 
00140         void removeOutgoingEdge(OA_ptr<DGraph::EdgeInterface> e);
00141         */
00142 
00144         void add(StmtHandle h);
00145 
00146         void add_front(StmtHandle h);
00147 
00149         StmtHandle erase(StmtHandle h); 
00150 
00151         void split(StmtHandle splitPoint, OA_ptr<Node> newBlock);
00152 
00153         //=======================================================
00154         // Output
00155         //=======================================================
00156 
00157         void dump(std::ostream& os);
00158 
00159         void dump(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00160        
00166        void dumpdot(CFG &currcfg, std::ostream& os, 
00167                  OA_ptr<IRHandlesIRInterface> ir);
00168 
00169        virtual void output(OA::IRHandlesIRInterface& ir);
00170 
00171        //================================================================
00172        // Iterators
00173        //================================================================ 
00174         
00175        friend class CFG;
00176        friend class NodeStatementsIterator;
00177        friend class NodeStatementsRevIterator;
00178 
00179        OA_ptr<NodeStatementsIteratorInterface> 
00180           getNodeStatementsIterator() const;
00181 
00182        OA_ptr<NodeStatementsRevIteratorInterface> 
00183         getNodeStatementsRevIterator()  const;
00184 
00186   
00187        /*
00188        OA_ptr<DGraph::EdgesIteratorInterface> 
00189            getIncomingEdgesIterator() const;
00190 
00191        OA_ptr<DGraph::EdgesIteratorInterface> 
00192            getOutgoingEdgesIterator() const;
00193 
00194        OA_ptr<DGraph::NodesIteratorInterface> 
00195            getSourceNodesIterator() const;
00196 
00197        OA_ptr<DGraph::NodesIteratorInterface> 
00198            getPredNodesIterator() const;
00199 
00200        OA_ptr<DGraph::NodesIteratorInterface> 
00201            getSinkNodesIterator() const;
00202 
00203        OA_ptr<DGraph::NodesIteratorInterface> 
00204            getSuccNodesIterator() const;
00205        */    
00206 
00207        OA_ptr<EdgesIteratorInterface> 
00208            getCFGIncomingEdgesIterator() const;
00209 
00211 
00212        OA_ptr<EdgesIteratorInterface> 
00213            getCFGOutgoingEdgesIterator() const;
00214 
00215        OA_ptr<NodesIteratorInterface> 
00216            getCFGSourceNodesIterator() const;
00217 
00218        OA_ptr<NodesIteratorInterface> 
00219            getCFGPredNodesIterator() const;
00220 
00221        OA_ptr<NodesIteratorInterface> 
00222            getCFGSinkNodesIterator() const;
00223 
00224        OA_ptr<NodesIteratorInterface> 
00225            getCFGSuccNodesIterator() const;
00226 
00227   protected:
00229        std::list<StmtHandle>::iterator getStmtListBegin ();
00230     
00232        std::list<StmtHandle>::iterator getStmtListEnd ();
00233     
00235        std::list<StmtHandle>::reverse_iterator getStmtListRBegin ();
00236 
00238        std::list<StmtHandle>::reverse_iterator getStmtListREnd ();
00239 
00240 
00241   private:
00242     
00243         void Ctor();
00244     
00245         // OA_ptr because want a reference to it in NodeStatementsIterator
00246         OA_ptr<std::list<StmtHandle> > mStmt_list;
00247     
00248         //std::list<StmtHandle> mStmt_list;
00249         ExprHandle mEnd;
00250     
00251         //DGraph::NodeImplement mdgNode;
00252  
00253   };
00254 
00255   //--------------------------------------------------------------------
00258   //--------------------------------------------------------------------
00259   class Edge : public virtual EdgeInterface,
00260                public virtual DGraph::EdgeImplement
00261   {
00262   public:
00263   
00264          Edge(OA_ptr<DGraph::NodeInterface> source,
00265              OA_ptr<DGraph::NodeInterface> sink, EdgeType _type, 
00266              OA::ExprHandle _expr);
00267 
00268          ~Edge();
00269     
00270          friend class CFG;
00271 
00272          //========================================================
00273          // Iterators
00274          //=======================================================
00275 
00276          /*
00278 
00279          OA_ptr<DGraph::NodeInterface> getSource() const;
00280 
00281          OA_ptr<DGraph::NodeInterface> getSink() const;
00282          */
00283 
00285 
00286          OA_ptr<NodeInterface> getCFGSource() const; 
00287 
00288          OA_ptr<NodeInterface> getCFGSink() const; 
00289 
00290          /*
00291          //========================================================
00292          // Comparison operators
00293          //========================================================
00294 
00295          bool operator== (DGraph::EdgeInterface& other);
00296 
00297          bool operator< (DGraph::EdgeInterface& other);
00298          */
00299          
00300          //=============================================================
00301          // Output   
00302          //==============================================================
00303          void dumpdot(std::ostream &os, OA_ptr<IRHandlesIRInterface> ir);
00304 
00305           void dump(std::ostream& os);
00306          //void dump () { dump(std::cout); }
00307 
00308          void output(IRHandlesIRInterface& ir);
00309 
00310          //=================================================================
00311          // Information
00312          //=================================================================
00313 
00315         // virtual unsigned int getId() const;
00316     
00318          EdgeType getType() const;
00319 
00320 
00322          ExprHandle getExpr() const;
00323     
00324 
00326          const char* edgeTypeToString(EdgeType et) const;
00327 
00328 
00329   protected:
00330 
00332          void setType(EdgeType type);
00333 
00335          void setExpr(ExprHandle expr);
00336 
00337     
00338   private:
00339          EdgeType mType;
00340          ExprHandle mExpr;
00341          static const char* sEdgeTypeToString[];
00342          //DGraph::EdgeImplement mdgEdge;
00343   };  
00344 
00345 //-------------------------------------------------------------------
00346 // CFG methods
00347 //-------------------------------------------------------------------
00348 
00349 class CFG : public virtual CFGInterface,
00350             public virtual DGraph::DGraphImplement  
00351 {
00352 public:
00353         CFG();
00354         ~CFG();
00355 
00356         //-------------------------------------
00357         // CFG information access
00358         //-------------------------------------
00360        OA_ptr<NodeInterface> getEntry() const;
00361 
00362        OA_ptr<NodeInterface> getExit() const;
00363 
00364        SymHandle getName() const;
00365 
00366        /*
00367        int getNumNodes();
00368        int getNumEdges();
00369        */
00370   
00371 
00372        //========================================================
00373        // Output
00374        //========================================================
00375        //void output(IRHandlesIRInterface& ir);
00376  
00377        void dump(std::ostream&, OA_ptr<IRHandlesIRInterface> ); 
00378 
00379        void dumpdot(std::ostream& os, OA_ptr<IRHandlesIRInterface> ir);
00380 
00381        //void output() const; 
00382   
00383        //-------------------------------------
00384        // Iterators
00385        // Calling functions from DGraphImplement.hpp
00386        //-------------------------------------
00387        
00388 
00391        /*
00392        OA_ptr<DGraph::NodesIteratorInterface> 
00393            getNodesIterator() const;
00394 
00395        OA_ptr<DGraph::EdgesIteratorInterface> 
00396            getEdgesIterator() const;
00397 
00398        OA_ptr<DGraph::NodesIteratorInterface> 
00399            getEntryNodesIterator() const;
00400 
00401        OA_ptr<DGraph::NodesIteratorInterface> 
00402            getExitNodesIterator() const;
00403   
00404        OA_ptr<DGraph::NodesIteratorInterface> 
00405            getReversePostDFSIterator(DGraph::DGraphEdgeDirection pOrient);
00406 
00407        OA_ptr<DGraph::NodesIteratorInterface> 
00408            getDFSIterator(OA_ptr<DGraph::NodeInterface> n);
00409        */    
00410 
00413        OA_ptr<NodesIteratorInterface> 
00414            getCFGNodesIterator() const;
00415 
00416        // method to get a more specific iterator
00417        OA_ptr<EdgesIteratorInterface>
00418            getCFGEdgesIterator() const;
00419 
00420        OA_ptr<NodesIteratorInterface>
00421            getCFGEntryNodesIterator() const;
00422 
00423        OA_ptr<NodesIteratorInterface>
00424            getCFGExitNodesIterator() const;
00425 
00426        OA_ptr<NodesIteratorInterface>
00427          getCFGReversePostDFSIterator(DGraph::DGraphEdgeDirection pOrient);
00428 
00429        OA_ptr<NodesIteratorInterface> 
00430            getCFGDFSIterator(OA_ptr<NodeInterface> n);
00431 
00432        
00433        /*
00434        //========================================================
00435        // Construction
00436        //========================================================
00437        void addNode(OA_ptr<DGraph::NodeInterface> n);
00438    
00439        void addEdge(OA_ptr<DGraph::EdgeInterface> e);
00440 
00441        void removeEdge(OA_ptr<DGraph::EdgeInterface> e)
00442        {
00443 
00444           mDG.removeEdge(e);
00445        }
00446 
00447       void removeNode(OA_ptr<DGraph::NodeInterface> n)
00448       {
00449 
00450         mDG.removeNode(n);
00451       }
00452       */
00453  
00454 public: 
00455       //-------------------------------------
00456       // CFG building methods
00457       //-------------------------------------
00458       void setEntry(OA_ptr<Node> n);
00459       void setExit(OA_ptr<Node> n);
00460 
00461       OA_ptr<Node> splitBlock(OA_ptr<Node>, StmtHandle);
00462 
00463       OA_ptr<Edge> connect(OA_ptr<NodeInterface> src,
00464                        OA_ptr<NodeInterface> dst,
00465                        EdgeType type);
00466 
00467   
00468       OA_ptr<Edge> connect(OA_ptr<NodeInterface> src,
00469                        OA_ptr<NodeInterface> dst,
00470                        EdgeType type, ExprHandle expr) ;
00471 
00472 
00473       class NodeLabel;
00474       typedef std::list<NodeLabel> NodeLabelList;
00475       void connect(OA_ptr<NodeInterface>, NodeLabelList&);
00476       void connect(NodeLabelList&, OA_ptr<NodeInterface>);
00477      
00478       void disconnect(OA_ptr<EdgeInterface> e); 
00479       void disconnect(OA_ptr<NodeInterface> n);
00480   
00481       //-------------------------------------
00482       // CFG attaching labels to nodes
00483       //-------------------------------------
00484       OA_ptr<Node> node_from_label(OA::StmtLabel);
00485       OA_ptr<Node> getLabelBlock(StmtLabel lab); 
00487       void mapLabelToNode(OA::StmtLabel lab, OA_ptr<Node> n);
00489       bool isLabelMappedToNode(StmtLabel lab);
00490 
00491       // members for CFG
00492 private:
00493       //static const char* sEdgeTypeToString[]; //moved to edge class
00494       SymHandle mName;
00495       OA_ptr<Node> mEntry;
00496       OA_ptr<Node> mExit;
00497       std::map<OA::StmtLabel, OA_ptr<Node> > mlabel_to_node_map;
00498 
00499 //      DGraph::DGraphImplement mDG;
00500 
00501 public: 
00502 
00503       class NodeLabelListIterator;
00504       friend class NodeLabelListIterator;
00505       /*----------------------------------------------------------------*//*-------------------------------------------------------------*/
00509       class NodeLabel {
00510         public:
00511           NodeLabel(OA_ptr<Node> _n, EdgeType _et, ExprHandle _eh = 0) :
00512                     mN(_n), mEh(_eh), mEt(_et) {}
00513           OA_ptr<Node> getNode();
00514           EdgeType getEdgeType();
00515           OA::ExprHandle getExpr();
00516 
00517         private:
00518           OA_ptr<Node> mN;
00519           OA::ExprHandle mEh;
00520           EdgeType mEt;
00521        };
00522 
00523        /*----------------------------------------------------------------*//*-------------------------------------------------------------*/
00527        class NodeLabelListIterator {
00528          public:
00529             NodeLabelListIterator(NodeLabelList& nl)  
00530                : mList(nl), mIter(mList.begin()) {}
00531            ~NodeLabelListIterator() {}
00532            void operator++();
00533            void operator++(int);
00534            bool isValid() const;
00535            NodeLabel current() const;
00536          private:
00537            NodeLabelList& mList;
00538            std::list<NodeLabel>::iterator mIter;
00539         };
00540 };
00541 
00542  //--------------------------------------------------------------------
00545   //--------------------------------------------------------------------
00546   class NodeStatementsIterator : public virtual
00547       NodeStatementsIteratorInterface {
00548   public:
00549     NodeStatementsIterator(const Node& node)
00550         { mList = node.mStmt_list;
00551           mIter = mList->begin();
00552         }
00553     ~NodeStatementsIterator() {}
00554     void operator++();
00555     void operator++(int);
00557     bool isValid() const;
00559     StmtHandle current() const;
00560     void reset();
00561   private:
00562     OA_ptr<std::list<StmtHandle> > mList;
00563     std::list<StmtHandle>::iterator mIter;
00564   };
00565 
00566   //--------------------------------------------------------------------
00569   //--------------------------------------------------------------------
00570   class NodeStatementsRevIterator : public virtual
00571       NodeStatementsRevIteratorInterface {
00572   public:
00573     NodeStatementsRevIterator(const Node& node)
00574         { mList = node.mStmt_list;
00575           mRevIter = mList->rbegin();
00576         }
00577     ~NodeStatementsRevIterator() {}
00578     void operator++();
00579     void operator++(int);
00581     bool isValid() const;
00583     StmtHandle current() const;
00584     void reset();
00585   private:
00586     OA_ptr<std::list<StmtHandle> > mList;
00587     std::list<StmtHandle>::reverse_iterator mRevIter;
00588   };
00589 
00590 
00591   } // end of CFG namespace
00592 } // end of OA namespace
00593 
00594 #endif

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