Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShStructural.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHSTRUCTURAL_HPP
00021 #define SHSTRUCTURAL_HPP
00022 
00023 #include <list>
00024 #include <utility>
00025 #include <iosfwd>
00026 #include "ShDllExport.hpp"
00027 #include "ShRefCount.hpp"
00028 #include "ShInfo.hpp"
00029 #include "ShCtrlGraph.hpp"
00030 #include "ShVariable.hpp"
00031 #include "ShStatement.hpp"
00032 
00033 namespace SH {
00034 
00035 class
00036 SH_DLLEXPORT ShStructuralNode : public virtual ShRefCountable /*, public virtual ShInfo */ {
00037 public:
00038   friend class ShStructural;
00039   
00040   enum NodeType {
00041     UNREDUCED,
00042     BLOCK,
00043     SECTION,
00044     IF,
00045     IFELSE,
00046     SELFLOOP,
00047     WHILELOOP
00048   };
00049 
00050   ShStructuralNode(const ShCtrlGraphNodePtr& node);
00051   ShStructuralNode(NodeType type);
00052 
00053   // Graphviz-format dump of this node and its children
00054   std::ostream& dump(std::ostream& out, int nodes = -1) const;
00055 
00056   // Structural information
00057   NodeType type;
00058   ShStructuralNode* container;
00059   typedef std::list< ShPointer< ShStructuralNode> > StructNodeList;
00060   StructNodeList structnodes; 
00061   bool contains(ShCtrlGraphNodePtr node) const; 
00062   
00063   // Graph structure
00064   ShCtrlGraphNodePtr cfg_node;
00065   typedef std::pair<ShVariable, ShPointer<ShStructuralNode> > SuccessorEdge;
00066   typedef std::list<SuccessorEdge> SuccessorList;
00067   SuccessorList succs;
00068   typedef std::pair<ShVariable, ShStructuralNode*> PredecessorEdge;
00069   typedef std::list<PredecessorEdge> PredecessorList;
00070   PredecessorList preds;
00071 
00072   // Functions to access underlying CFG structure
00073   
00078   ShStatement *secStart, *secEnd; 
00079  
00080  
00085   struct CfgMatch {
00086     ShCtrlGraphNodePtr from, to;
00087     ShCtrlGraphNode::SuccessorList::iterator S; //< set to succ.end() if edge is from->follower 
00088 
00089     CfgMatch();
00090     CfgMatch(ShCtrlGraphNodePtr from); //< for a follower
00091     CfgMatch(ShCtrlGraphNodePtr from, 
00092         ShCtrlGraphNode::SuccessorList::iterator S); //< for a successor 
00093         
00094     bool isFollower(); 
00095   };
00096   typedef std::list<CfgMatch> CfgMatchList; 
00097 
00098   // Retrieves successors from CFG that match the given SuccessorEdge
00099   void getSuccs(CfgMatchList &result, const SuccessorEdge &edge);
00100   void getPreds(CfgMatchList &result, const PredecessorEdge &edge);
00101   
00102   // Retrieves all successors from CFG in this node that leave the given node 
00103   // node = 0 indicates the typical case of node = this;
00104   void getExits(CfgMatchList &result, ShPointer<ShStructuralNode> node = 0);
00105 
00106   // Retrieves all successor edges from CFG in this node that enter the given node 
00107   // node = 0 indicates the typical case of node = this
00108   void getEntries(CfgMatchList &result, ShPointer<ShStructuralNode> node = 0);
00109 
00110 
00111   // Spanning tree
00112   ShStructuralNode* parent;
00113   typedef std::list< ShPointer<ShStructuralNode> > ChildList;
00114   ChildList children;
00115 
00116 };
00117 
00118 typedef ShPointer<ShStructuralNode> ShStructuralNodePtr;
00119 typedef ShPointer<const ShStructuralNode> ShStructuralNodeCPtr;
00120 
00121 class
00122 SH_DLLEXPORT ShStructural {
00123 public:
00124   ShStructural(const ShCtrlGraphPtr& graph);
00125 
00126   // Graphviz-format dump of the structural tree.
00127   std::ostream& dump(std::ostream& out) const;
00128 
00129   const ShStructuralNodePtr& head();
00130   
00131 private:
00132   ShCtrlGraphPtr m_graph;
00133   ShStructuralNodePtr m_head;
00134 
00135   typedef std::list<ShStructuralNode*> PostorderList;
00136   PostorderList m_postorder;
00137 
00138   ShStructuralNodePtr build_tree(const ShCtrlGraphNodePtr& node);
00139   void build_postorder(const ShStructuralNodePtr& node);
00140 };
00141 
00142 }
00143 
00144 #endif

Generated on Thu Jul 28 17:33:05 2005 for Sh by  doxygen 1.4.3-20050530