ROSE 0.11.145.192
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
13class SgGraphNode;
15
16
17namespace StaticCFG
18{
19
22
23
24class ROSE_DLL_API CFG
25{
26protected:
29
31 std::map<CFGNode, SgGraphNode*> all_nodes_;
32
35
38
41
44
45public:
46 CFG() : graph_(NULL), start_(NULL), entry_(NULL), exit_(NULL) {}
47
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
121protected:
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
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
141{
142 int index_;
144
145public:
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
160template <class EdgeT>
162{
163 EdgeT edge_;
164public:
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.
175std::vector<SgDirectedGraphEdge*> outEdges(SgGraphNode* node);
176std::vector<SgDirectedGraphEdge*> inEdges(SgGraphNode* node);
177
178} // end of namespace StaticCFG
179
180#endif
Base class for all IR node attribute values.
This class represents the base class for all IR nodes within Sage III.
This class stores index of each node as an attribuite of SgGraphNode.
Definition staticCFG.h:141
SgNode * start_
The start node to begin CFG build.
Definition staticCFG.h:34
SgGraphNode * entry_
The entry node.
Definition staticCFG.h:37
SgGraphNode * getExit() const
Get the exit node of the CFG.
Definition staticCFG.h:81
SgGraphNode * getEntry() const
Get the entry node of the CFG.
Definition staticCFG.h:77
SgIncidenceDirectedGraph * getGraph() const
Get the pointer pointing to the graph used by static CFG.
Definition staticCFG.h:66
void clearNodesAndEdges()
Delete all nodes and edges in the graph and release memories.
SgGraphNode * toGraphNode(CFGNode &n)
Turn a CFG node into a GraphNode which is defined in VirtualCFG namespace.
Definition staticCFG.h:53
CFGNode toCFGNode(SgGraphNode *node)
Turn a graph node into a CFGNode which is defined in VirtualCFG namespace.
std::map< CFGNode, SgGraphNode * > all_nodes_
A map from CFGNode in virtualCFG to node from staticCFG.
Definition staticCFG.h:31
void setStart(SgNode *node)
Set the start node for graph building.
Definition staticCFG.h:74
virtual void buildFullCFG()
Build CFG for debugging.
bool is_filtered_
A flag shows whether this CFG is filtered or not.
Definition staticCFG.h:43
SgGraphNode * exit_
The exit node.
Definition staticCFG.h:40
SgIncidenceDirectedGraph * graph_
The graph data structure holding the CFG.
Definition staticCFG.h:28
virtual void buildCFG()
Build CFG according to the 'is_filtered_' flag.
Definition staticCFG.h:89
void cfgToDot(SgNode *node, const std::string &file_name)
Output the graph to a DOT file.
CFG(SgNode *node, bool is_filtered=false)
The constructor building the CFG.
static int getIndex(SgGraphNode *node)
Get the index of a CFG node.
virtual void buildFilteredCFG()
Build filtered CFG which only contains interesting nodes.
A control flow edge connecting two CFG nodes, with an edge condition to indicate edge types.
Definition virtualCFG.h:111
A node in the control flow graph.
Definition virtualCFG.h:70