ROSE  0.9.9.139
staticCFG.h
1 #ifndef STATIC_CFG_H
2 #define STATIC_CFG_H
3 
4 #include <sage3basic.h>
5 #include "AstAttributeMechanism.h"
6 #include "virtualCFG.h"
7 #include <map>
8 #include <set>
9 #include <string>
10 
11 
13 class SgGraphNode;
15 
16 
17 namespace StaticCFG
18 {
19 
22 
23 
24 class ROSE_DLL_API CFG
25 {
26 protected:
29 
31  std::map<CFGNode, SgGraphNode*> all_nodes_;
32 
35 
38 
41 
44 
45 public:
46  CFG() : graph_(NULL), start_(NULL), entry_(NULL), exit_(NULL) {}
47 
49  CFGNode toCFGNode(SgGraphNode* node);
50 
53  SgGraphNode *toGraphNode(CFGNode &n) { return ((all_nodes_.count(n)==0) ? NULL : all_nodes_[n]);}
54 
56 
57  CFG(SgNode* node, bool is_filtered = false)
58  : graph_(NULL), start_(node), entry_(NULL), exit_(NULL), is_filtered_(is_filtered)
59  { buildCFG(); }
60 
63  { return graph_; }
64 
65  virtual ~CFG()
66  { clearNodesAndEdges(); }
67 
69 
70  void setStart(SgNode* node) { start_ = node; }
71 
74  { return entry_; }
75 
78  { return exit_; }
79 
80  bool isFilteredCFG() const { return is_filtered_; }
81  void setFiltered(bool flag) { is_filtered_ = flag; }
82 
83 
85  virtual void buildCFG()
86  {
87  if (is_filtered_) buildFilteredCFG();
88  else buildFullCFG();
89  }
90 
92  virtual void buildFullCFG();
93 
95  virtual void buildFilteredCFG();
96 
97 
98 #if 0
99  std::vector<SgDirectedGraphEdge*> getOutEdges(SgNode* node, int index);
100  std::vector<SgDirectedGraphEdge*> getInEdges(SgNode* node, int index);
101 #endif
102 
103  // The following four functions are for getting in/out edges of a given node.
104  std::vector<SgDirectedGraphEdge*> getOutEdges(SgGraphNode* node);
105  std::vector<SgDirectedGraphEdge*> getInEdges(SgGraphNode* node);
106 
107  // Provide the same interface to get the beginning/end graph node for a SgNode
108  SgGraphNode* cfgForBeginning(SgNode* node);
109  SgGraphNode* cfgForEnd(SgNode* node);
110 
112  static int getIndex(SgGraphNode* node);
113 
115  void cfgToDot(SgNode* node, const std::string& file_name);
116 
117 protected:
118  //void buildCFG(CFGNode n);
119  template <class NodeT, class EdgeT>
120  void buildCFG(NodeT n, std::map<NodeT, SgGraphNode*>& all_nodes, std::set<NodeT>& explored);
121 
123  void clearNodesAndEdges();
124 
125 
126 
127  // The following methods are used to build a DOT file.
128  virtual void processNodes(std::ostream & o, SgGraphNode* n, std::set<SgGraphNode*>& explored);
129  virtual void printNodePlusEdges(std::ostream & o, SgGraphNode* node);
130  virtual void printNode(std::ostream & o, SgGraphNode* node);
131  virtual void printEdge(std::ostream & o, SgDirectedGraphEdge* edge, bool isInEdge);
132 };
133 
134 
136 class CFGNodeAttribute : public AstAttribute
137 {
138  int index_;
139  SgIncidenceDirectedGraph* graph_;
140 
141 public:
142  CFGNodeAttribute(int idx = 0, SgIncidenceDirectedGraph* graph = NULL)
143  : index_(idx), graph_(graph) {}
144 
145  int getIndex() const { return index_; }
146 
147  void setIndex(int idx) { index_ = idx; }
148 
149  const SgIncidenceDirectedGraph* getGraph() const { return graph_; }
150  SgIncidenceDirectedGraph* getGraph() { return graph_; }
151 
152  void setGraph(SgIncidenceDirectedGraph* graph)
153  { graph_ = graph; }
154 };
155 
156 template <class EdgeT>
157 class CFGEdgeAttribute : public AstAttribute
158 {
159  EdgeT edge_;
160 public:
161  CFGEdgeAttribute(const EdgeT& e) : edge_(e) {}
162 
163  void setEdge(const EdgeT& e)
164  { edge_ = e; }
165 
166  EdgeT getEdge() const
167  { return edge_; }
168 };
169 
170 // The following are some auxiliary functions, since SgGraphNode cannot provide them.
171 std::vector<SgDirectedGraphEdge*> outEdges(SgGraphNode* node);
172 std::vector<SgDirectedGraphEdge*> inEdges(SgGraphNode* node);
173 
174 } // end of namespace StaticCFG
175 
176 #endif
bool is_filtered_
A flag shows whether this CFG is filtered or not.
Definition: staticCFG.h:43
SgGraphNode * getEntry() const
Get the entry node of the CFG.
Definition: staticCFG.h:73
SgGraphNode * entry_
The entry node.
Definition: staticCFG.h:37
virtual void buildCFG()
Build CFG according to the 'is_filtered_' flag.
Definition: staticCFG.h:85
SgGraphNode * toGraphNode(CFGNode &n)
Turn a CFG node into a GraphNode which is defined in VirtualCFG namespace.
Definition: staticCFG.h:53
A control flow edge connecting two CFG nodes, with an edge condition to indicate edge types...
Definition: virtualCFG.h:108
SgNode * start_
The start node to begin CFG build.
Definition: staticCFG.h:34
This class stores index of each node as an attribuite of SgGraphNode.
Definition: staticCFG.h:136
A node in the control flow graph.
Definition: virtualCFG.h:67
void setStart(SgNode *node)
Set the start node for graph building.
Definition: staticCFG.h:70
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8322
SgGraphNode * getExit() const
Get the exit node of the CFG.
Definition: staticCFG.h:77
SgGraphNode * exit_
The exit node.
Definition: staticCFG.h:40
CFG(SgNode *node, bool is_filtered=false)
The constructor building the CFG.
Definition: staticCFG.h:57
std::map< CFGNode, SgGraphNode * > all_nodes_
A map from CFGNode in virtualCFG to node from staticCFG.
Definition: staticCFG.h:31
SgIncidenceDirectedGraph * getGraph() const
Get the pointer pointing to the graph used by static CFG.
Definition: staticCFG.h:62
SgIncidenceDirectedGraph * graph_
The graph data structure holding the CFG.
Definition: staticCFG.h:28