00001 #ifndef STATIC_CFG_H
00002 #define STATIC_CFG_H
00003
00004 #include <sage3basic.h>
00005 #include "AstAttributeMechanism.h"
00006 #include "virtualCFG.h"
00007 #include <map>
00008 #include <set>
00009 #include <string>
00010
00011
00012 class SgIncidenceDirectedGraph;
00013 class SgGraphNode;
00014 class SgDirectedGraphEdge;
00015
00016
00017 namespace StaticCFG
00018 {
00019
00020 using VirtualCFG::CFGNode;
00021 using VirtualCFG::CFGEdge;
00022
00023
00024 class CFG
00025 {
00026 protected:
00028 SgIncidenceDirectedGraph* graph_;
00029
00031 std::map<CFGNode, SgGraphNode*> all_nodes_;
00032
00034 SgNode* start_;
00035
00037 SgGraphNode* entry_;
00038
00040 SgGraphNode* exit_;
00041
00043 bool is_filtered_;
00044
00045 public:
00046 CFG() : graph_(NULL), start_(NULL), entry_(NULL), exit_(NULL) {}
00047
00049 CFGNode toCFGNode(SgGraphNode* node);
00050
00053 SgGraphNode *toGraphNode(CFGNode &n) { return ((all_nodes_.count(n)==0) ? NULL : all_nodes_[n]);}
00054
00056
00057 CFG(SgNode* node, bool is_filtered = false)
00058 : graph_(NULL), start_(node), entry_(NULL), exit_(NULL), is_filtered_(is_filtered)
00059 { buildCFG(); }
00060
00062 SgIncidenceDirectedGraph* getGraph() const
00063 { return graph_; }
00064
00065 virtual ~CFG()
00066 { clearNodesAndEdges(); }
00067
00069
00070 void setStart(SgNode* node) { start_ = node; }
00071
00073 SgGraphNode* getEntry() const
00074 { return entry_; }
00075
00077 SgGraphNode* getExit() const
00078 { return exit_; }
00079
00080 bool isFilteredCFG() const { return is_filtered_; }
00081 void setFiltered(bool flag) { is_filtered_ = flag; }
00082
00083
00085 virtual void buildCFG()
00086 {
00087 if (is_filtered_) buildFilteredCFG();
00088 else buildFullCFG();
00089 }
00090
00092 virtual void buildFullCFG();
00093
00095 virtual void buildFilteredCFG();
00096
00097
00098 #if 0
00099 std::vector<SgDirectedGraphEdge*> getOutEdges(SgNode* node, int index);
00100 std::vector<SgDirectedGraphEdge*> getInEdges(SgNode* node, int index);
00101 #endif
00102
00103
00104 std::vector<SgDirectedGraphEdge*> getOutEdges(SgGraphNode* node);
00105 std::vector<SgDirectedGraphEdge*> getInEdges(SgGraphNode* node);
00106
00107
00108 SgGraphNode* cfgForBeginning(SgNode* node);
00109 SgGraphNode* cfgForEnd(SgNode* node);
00110
00112 static int getIndex(SgGraphNode* node);
00113
00115 void cfgToDot(SgNode* node, const std::string& file_name);
00116
00117 protected:
00118
00119 template <class NodeT, class EdgeT>
00120 void buildCFG(NodeT n, std::map<NodeT, SgGraphNode*>& all_nodes, std::set<NodeT>& explored);
00121
00123 void clearNodesAndEdges();
00124
00125
00126
00127
00128 virtual void processNodes(std::ostream & o, SgGraphNode* n, std::set<SgGraphNode*>& explored);
00129 virtual void printNodePlusEdges(std::ostream & o, SgGraphNode* node);
00130 virtual void printNode(std::ostream & o, SgGraphNode* node);
00131 virtual void printEdge(std::ostream & o, SgDirectedGraphEdge* edge, bool isInEdge);
00132 };
00133
00134
00136 class CFGNodeAttribute : public AstAttribute
00137 {
00138 int index_;
00139 SgIncidenceDirectedGraph* graph_;
00140
00141 public:
00142 CFGNodeAttribute(int idx = 0, SgIncidenceDirectedGraph* graph = NULL)
00143 : index_(idx), graph_(graph) {}
00144
00145 int getIndex() const { return index_; }
00146
00147 void setIndex(int idx) { index_ = idx; }
00148
00149 const SgIncidenceDirectedGraph* getGraph() const { return graph_; }
00150 SgIncidenceDirectedGraph* getGraph() { return graph_; }
00151
00152 void setGraph(SgIncidenceDirectedGraph* graph)
00153 { graph_ = graph; }
00154 };
00155
00156 template <class EdgeT>
00157 class CFGEdgeAttribute : public AstAttribute
00158 {
00159 EdgeT edge_;
00160 public:
00161 CFGEdgeAttribute(const EdgeT& e) : edge_(e) {}
00162
00163 void setEdge(const EdgeT& e)
00164 { edge_ = e; }
00165
00166 EdgeT getEdge() const
00167 { return edge_; }
00168 };
00169
00170
00171 std::vector<SgDirectedGraphEdge*> outEdges(SgGraphNode* node);
00172 std::vector<SgDirectedGraphEdge*> inEdges(SgGraphNode* node);
00173
00174 }
00175
00176 #endif