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 software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHSTRUCTURAL_HPP
00025 #define SHSTRUCTURAL_HPP
00026 
00027 #include <list>
00028 #include <utility>
00029 #include <iosfwd>
00030 #include "ShDllExport.hpp"
00031 #include "ShRefCount.hpp"
00032 #include "ShInfo.hpp"
00033 #include "ShCtrlGraph.hpp"
00034 #include "ShVariable.hpp"
00035 #include "ShStatement.hpp"
00036 
00037 namespace SH {
00038 
00039 class
00040 SH_DLLEXPORT ShStructuralNode : public virtual ShRefCountable /*, public virtual ShInfo */ {
00041 public:
00042   friend class ShStructural;
00043   
00044   enum NodeType {
00045     UNREDUCED,
00046     BLOCK,
00047     SECTION,
00048     IF, 
00049     IFELSE,
00050     SELFLOOP,
00051     WHILELOOP
00052   };
00053 
00054   ShStructuralNode(const ShCtrlGraphNodePtr& node);
00055   ShStructuralNode(NodeType type);
00056 
00057   // Graphviz-format dump of this node and its children
00058   std::ostream& dump(std::ostream& out, int nodes = -1) const;
00059 
00060   // Structural information
00061   NodeType type;
00062   ShStructuralNode* container;
00063   typedef std::list< ShPointer< ShStructuralNode> > StructNodeList;
00064   StructNodeList structnodes; 
00065   bool contains(ShCtrlGraphNodePtr node) const; 
00066   
00067   // Graph structure
00068   ShCtrlGraphNodePtr cfg_node;
00069   typedef std::pair<ShVariable, ShPointer<ShStructuralNode> > SuccessorEdge;
00070   typedef std::list<SuccessorEdge> SuccessorList;
00071   SuccessorList succs;
00072   typedef std::pair<ShVariable, ShStructuralNode*> PredecessorEdge;
00073   typedef std::list<PredecessorEdge> PredecessorList;
00074   PredecessorList preds;
00075 
00076   // Functions to access underlying CFG structure
00077   
00082   ShStatement *secStart, *secEnd; 
00083  
00084  
00089   struct CfgMatch {
00090     ShCtrlGraphNodePtr from, to;
00091     ShCtrlGraphNode::SuccessorList::iterator S; //< set to succ.end() if edge is from->follower 
00092 
00093     CfgMatch();
00094     CfgMatch(ShCtrlGraphNodePtr from); //< for a follower
00095     CfgMatch(ShCtrlGraphNodePtr from, 
00096         ShCtrlGraphNode::SuccessorList::iterator S); //< for a successor 
00097         
00098     bool isFollower(); 
00099   };
00100   typedef std::list<CfgMatch> CfgMatchList; 
00101 
00102   // Retrieves successors from CFG that match the given SuccessorEdge
00103   void getSuccs(CfgMatchList &result, const SuccessorEdge &edge);
00104   void getPreds(CfgMatchList &result, const PredecessorEdge &edge);
00105   
00106   // Retrieves all successors from CFG in this node that leave the given node 
00107   // node = 0 indicates the typical case of node = this;
00108   void getExits(CfgMatchList &result, ShPointer<ShStructuralNode> node = 0);
00109 
00110   // Retrieves all successor edges from CFG in this node that enter the given node 
00111   // node = 0 indicates the typical case of node = this
00112   void getEntries(CfgMatchList &result, ShPointer<ShStructuralNode> node = 0);
00113 
00114 
00115   // Spanning tree
00116   ShStructuralNode* parent;
00117   typedef std::list< ShPointer<ShStructuralNode> > ChildList;
00118   ChildList children;
00119 
00120 };
00121 
00122 typedef ShPointer<ShStructuralNode> ShStructuralNodePtr;
00123 typedef ShPointer<const ShStructuralNode> ShStructuralNodeCPtr;
00124 
00125 class
00126 SH_DLLEXPORT ShStructural {
00127 public:
00128   ShStructural(const ShCtrlGraphPtr& graph);
00129 
00130   // Graphviz-format dump of the structural tree.
00131   std::ostream& dump(std::ostream& out) const;
00132 
00133   const ShStructuralNodePtr& head();
00134   
00135 private:
00136   ShCtrlGraphPtr m_graph;
00137   ShStructuralNodePtr m_head;
00138 
00139   typedef std::list<ShStructuralNode*> PostorderList;
00140   PostorderList m_postorder;
00141 
00142   ShStructuralNodePtr build_tree(const ShCtrlGraphNodePtr& node);
00143   void build_postorder(const ShStructuralNodePtr& node);
00144 };
00145 
00146 }
00147 
00148 #endif

Generated on Wed Jun 15 18:12:41 2005 for Sh by  doxygen 1.4.3-20050530