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

ShSection.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 SHSECTION_HPP
00025 #define SHSECTION_HPP
00026 
00027 #include <map>
00028 #include <list>
00029 #include <iosfwd>
00030 #include "ShDllExport.hpp"
00031 #include "ShInfo.hpp"
00032 #include "ShRefCount.hpp"
00033 #include "ShCtrlGraph.hpp"
00034 #include "ShStructural.hpp"
00035 
00041 // @todo range
00042 // This is poorly written...clean up if this ever needs to be merged into main
00043 
00044 namespace SH {
00045 
00046 /* Really just a bastard child of ShStructuralNode.
00047  * Maintains ShSectionNode information only in a much cleaner form
00048  * (Might just want to merge this into ShStructuralNode later...)
00049  */
00050 class ShSectionNode: public ShRefCountable {
00051   public:
00053     ShSectionNode();
00054 
00055     typedef std::set<ShPointer<ShSectionNode> > SectionSet;
00056     typedef SectionSet::iterator iterator;
00057     typedef SectionSet::const_iterator const_iterator;
00058 
00059     SectionSet children; 
00060     ShSectionNode* parent; 
00061 
00062     iterator begin() { return children.begin(); }
00063     iterator end() { return children.end(); }
00064     const_iterator begin() const { return children.begin(); }
00065     const_iterator end() const { return children.end(); }
00066     void addChild(ShPointer<ShSectionNode> child); 
00067 
00068     typedef std::list<ShCtrlGraphNodePtr> CfgNodeList; 
00069     typedef CfgNodeList::iterator cfg_iterator;
00070     typedef CfgNodeList::const_iterator const_cfg_iterator;
00071 
00078     CfgNodeList cfgNodes;  
00079 
00080     cfg_iterator cfgBegin() { return cfgNodes.begin(); }
00081     cfg_iterator cfgEnd() { return cfgNodes.end(); }
00082     const_cfg_iterator cfgBegin() const { return cfgNodes.begin(); }
00083     const_cfg_iterator cfgEnd() const { return cfgNodes.end(); }
00084     void addCfg(ShCtrlGraphNodePtr cfgNode) { cfgNodes.push_back(cfgNode); }
00085 
00086     /* Returns whether this is the root */
00087     bool isRoot() const { return !parent; }
00088 
00090     ShStatement* getStart();
00091     ShStatement* getEnd();
00092 
00094     std::string name();
00095 
00096     /* Some useful queries about nodes in the tree */ 
00097     int depth();
00098 };
00099 typedef ShPointer<ShSectionNode> ShSectionNodePtr;
00100 
00101 class ShSectionTree {
00102   public:
00103     ShSectionNodePtr root;
00104 
00105     ShSectionTree(ShStructural &structural);
00106 
00107     /* DFS through the section tree
00108      * This is just a copy of the Cfg code...should really refactor */
00109     template<class F>
00110     void dfs(F& functor);
00111      
00112     /* Retrieves the section containing cfgNode */
00113     ShSectionNodePtr operator[](ShCtrlGraphNodePtr cfgNode);
00114 
00115     /* Returns whether a section node in this tree contains a particular section
00116      * node. */ 
00117     bool contains(ShSectionNodePtr section, ShCtrlGraphNodePtr cfgNode);
00118 
00119     /* Graphviz dump */
00120     std::ostream& dump(std::ostream& out); 
00121 
00122     /* Graphviz dump with a special functor with two functions:
00123      *    void operator()(std::ostream&, ShCtrlGraphNodePtr) that outputs a HTML-style label 
00124      *    for a cfg node
00125      *
00126      *    void operator()(std::ostream&, ShSectionNodePtr) that outputs any section specific
00127      *    attributes (as part of the stmts in a a section's subgraph)
00128      */
00129     template<class F>
00130     std::ostream& dump(std::ostream& out, F& functor);
00131 
00132   private:
00133     // adds structural subtree rooted at structNode to the section 
00134     static void makeSection(ShStructuralNodePtr structNode, ShSectionNodePtr section);
00135 
00136     typedef std::map<ShCtrlGraphNodePtr, ShSectionNodePtr> CfgSectionMap;
00137     CfgSectionMap cfgSection; 
00138     void gatherCfgSection(ShSectionNodePtr section); 
00139 
00140     /* DFS through the section tree */
00141     template<class F>
00142     static void realDfs(ShSectionNodePtr node, F& functor);
00143 
00144     /* Dump a node and subtree using the given functor (functor described above in dump()) 
00145      *
00146      * @param out Actual output stream to use
00147      * @param cfgout Output stream to use for edges that cross section
00148      *   boundaries (These must be specified after all involved sections are,
00149      *   otherwise DOT does stupid things with the cfg nodes) 
00150      * @param node The root of the section subtree to process
00151      * @param F functor as described above in dump*/
00152     template<class F>
00153     void realDump(std::ostream& out, std::ostream& cfgout, ShSectionNodePtr node, F& functor);
00154 };
00155 
00156 }
00157 
00158 #include "ShSectionImpl.hpp"
00159 
00160 #endif

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