ROSE  0.9.10.54
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 #if 0
58  CFG(SgNode* node, bool is_filtered = false)
59  : graph_(NULL), start_(node), entry_(NULL), exit_(NULL), is_filtered_(is_filtered)
60  { buildCFG(); }
61 #else
62  CFG(SgNode* node, bool is_filtered = false);
63 #endif
64 
67  { return graph_; }
68 
69  virtual ~CFG()
70  { clearNodesAndEdges(); }
71 
73 
74  void setStart(SgNode* node) { start_ = node; }
75 
78  { return entry_; }
79 
82  { return exit_; }
83 
84  bool isFilteredCFG() const { return is_filtered_; }
85  void setFiltered(bool flag) { is_filtered_ = flag; }
86 
87 
89  virtual void buildCFG()
90  {
91  if (is_filtered_) buildFilteredCFG();
92  else buildFullCFG();
93  }
94 
96  virtual void buildFullCFG();
97 
99  virtual void buildFilteredCFG();
100 
101 
102 #if 0
103  std::vector<SgDirectedGraphEdge*> getOutEdges(SgNode* node, int index);
104  std::vector<SgDirectedGraphEdge*> getInEdges(SgNode* node, int index);
105 #endif
106 
107  // The following four functions are for getting in/out edges of a given node.
108  std::vector<SgDirectedGraphEdge*> getOutEdges(SgGraphNode* node);
109  std::vector<SgDirectedGraphEdge*> getInEdges(SgGraphNode* node);
110 
111  // Provide the same interface to get the beginning/end graph node for a SgNode
112  SgGraphNode* cfgForBeginning(SgNode* node);
113  SgGraphNode* cfgForEnd(SgNode* node);
114 
116  static int getIndex(SgGraphNode* node);
117 
119  void cfgToDot(SgNode* node, const std::string& file_name);
120 
121 protected:
122  //void buildCFG(CFGNode n);
123  template <class NodeT, class EdgeT>
124  void buildCFG(NodeT n, std::map<NodeT, SgGraphNode*>& all_nodes, std::set<NodeT>& explored);
125 
127  void clearNodesAndEdges();
128 
129 
130 
131  // The following methods are used to build a DOT file.
132  virtual void processNodes(std::ostream & o, SgGraphNode* n, std::set<SgGraphNode*>& explored);
133  virtual void printNodePlusEdges(std::ostream & o, SgGraphNode* node);
134  virtual void printNode(std::ostream & o, SgGraphNode* node);
135  virtual void printEdge(std::ostream & o, SgDirectedGraphEdge* edge, bool isInEdge);
136 };
137 
138 
140 class CFGNodeAttribute : public AstAttribute
141 {
142  int index_;
143  SgIncidenceDirectedGraph* graph_;
144 
145 public:
146  CFGNodeAttribute(int idx = 0, SgIncidenceDirectedGraph* graph = NULL)
147  : index_(idx), graph_(graph) {}
148 
149  int getIndex() const { return index_; }
150 
151  void setIndex(int idx) { index_ = idx; }
152 
153  const SgIncidenceDirectedGraph* getGraph() const { return graph_; }
154  SgIncidenceDirectedGraph* getGraph() { return graph_; }
155 
156  void setGraph(SgIncidenceDirectedGraph* graph)
157  { graph_ = graph; }
158 };
159 
160 template <class EdgeT>
161 class CFGEdgeAttribute : public AstAttribute
162 {
163  EdgeT edge_;
164 public:
165  CFGEdgeAttribute(const EdgeT& e) : edge_(e) {}
166 
167  void setEdge(const EdgeT& e)
168  { edge_ = e; }
169 
170  EdgeT getEdge() const
171  { return edge_; }
172 };
173 
174 // The following are some auxiliary functions, since SgGraphNode cannot provide them.
175 std::vector<SgDirectedGraphEdge*> outEdges(SgGraphNode* node);
176 std::vector<SgDirectedGraphEdge*> inEdges(SgGraphNode* node);
177 
178 } // end of namespace StaticCFG
179 
180 #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:77
SgGraphNode * entry_
The entry node.
Definition: staticCFG.h:37
virtual void buildCFG()
Build CFG according to the 'is_filtered_' flag.
Definition: staticCFG.h:89
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:111
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:140
A node in the control flow graph.
Definition: virtualCFG.h:70
void setStart(SgNode *node)
Set the start node for graph building.
Definition: staticCFG.h:74
This class represents the base class for all IR nodes within Sage III.
Definition: Cxx_Grammar.h:8672
SgGraphNode * getExit() const
Get the exit node of the CFG.
Definition: staticCFG.h:81
SgGraphNode * exit_
The exit node.
Definition: staticCFG.h:40
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:66
SgIncidenceDirectedGraph * graph_
The graph data structure holding the CFG.
Definition: staticCFG.h:28