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     PROPINT
00049   };
00050 
00051   ShStructuralNode(const ShCtrlGraphNodePtr& node);
00052   ShStructuralNode(NodeType type);
00053 
00054   // Graphviz-format dump of this node and its children
00055   std::ostream& dump(std::ostream& out, int nodes = -1) const;
00056 
00057   // Structural information
00058   NodeType type;
00059   ShStructuralNode* container;
00060   typedef std::list< ShPointer< ShStructuralNode> > StructNodeList;
00061   StructNodeList structnodes; 
00062   bool contains(ShCtrlGraphNodePtr node) const; 
00063   
00064   // Graph structure
00065   ShCtrlGraphNodePtr cfg_node;
00066   typedef std::pair<ShVariable, ShPointer<ShStructuralNode> > SuccessorEdge;
00067   typedef std::list<SuccessorEdge> SuccessorList;
00068   SuccessorList succs;
00069   typedef std::pair<ShVariable, ShStructuralNode*> PredecessorEdge;
00070   typedef std::list<PredecessorEdge> PredecessorList;
00071   PredecessorList preds;
00072 
00073   // Functions to access underlying CFG structure
00074   
00079   ShStatement *secStart, *secEnd; 
00080  
00081  
00086   struct CfgMatch {
00087     ShCtrlGraphNodePtr from, to;
00088     ShCtrlGraphNode::SuccessorList::iterator S; //< set to succ.end() if edge is from->follower 
00089 
00090     CfgMatch();
00091     CfgMatch(ShCtrlGraphNodePtr from); //< for a follower
00092     CfgMatch(ShCtrlGraphNodePtr from, 
00093         ShCtrlGraphNode::SuccessorList::iterator S); //< for a successor 
00094         
00095     bool isFollower(); 
00096   };
00097   typedef std::list<CfgMatch> CfgMatchList; 
00098 
00099   // Retrieves successors from CFG that match the given SuccessorEdge
00100   void getSuccs(CfgMatchList &result, const SuccessorEdge &edge);
00101   void getPreds(CfgMatchList &result, const PredecessorEdge &edge);
00102   
00103   // Retrieves all successors from CFG in this node that leave the given node 
00104   // node = 0 indicates the typical case of node = this;
00105   void getExits(CfgMatchList &result, ShPointer<ShStructuralNode> node = 0);
00106 
00107   // Retrieves all successor edges from CFG in this node that enter the given node 
00108   // node = 0 indicates the typical case of node = this
00109   void getEntries(CfgMatchList &result, ShPointer<ShStructuralNode> node = 0);
00110 
00111 
00112   // Spanning tree
00113   ShStructuralNode* parent;
00114   typedef std::list< ShPointer<ShStructuralNode> > ChildList;
00115   ChildList children;
00116 
00117 };
00118 
00119 typedef ShPointer<ShStructuralNode> ShStructuralNodePtr;
00120 typedef ShPointer<const ShStructuralNode> ShStructuralNodeCPtr;
00121 
00122 class
00123 SH_DLLEXPORT ShStructural {
00124 public:
00125   ShStructural(const ShCtrlGraphPtr& graph);
00126 
00127   // Graphviz-format dump of the structural tree.
00128   std::ostream& dump(std::ostream& out) const;
00129 
00130   const ShStructuralNodePtr& head();
00131   
00132 private:
00133   ShCtrlGraphPtr m_graph;
00134   ShStructuralNodePtr m_head;
00135 
00136   typedef std::list<ShStructuralNode*> PostorderList;
00137   PostorderList m_postorder;
00138 
00139   ShStructuralNodePtr build_tree(const ShCtrlGraphNodePtr& node);
00140   void build_postorder(const ShStructuralNodePtr& node);
00141 };
00142 
00143 }
00144 
00145 #endif

Generated on Wed Nov 9 15:29:40 2005 for Sh by  doxygen 1.4.5