ROSE 0.11.145.147
CFGRewrite.h
1#include <featureTests.h>
2#ifdef ROSE_ENABLE_SOURCE_ANALYSIS
3
4#ifndef CFG_REWRITE_H
5#define CFG_REWRITE_H
6
7#include "DataflowCFG.h"
8#include <string>
9#include <iostream>
10#include <sstream>
11#include <list>
12
13namespace VirtualCFG{
14
16//SgStatement* getParentStmt(SgNode* node);*/
17//
18//void insertAfterCFG(DataflowNode& cfgNode);
19//
20//void placeInserts(SgProject *project);
21
22void initCFGRewrite(SgProject* project);
23
24// returns true if the given SgNode is a value expression or a computational expression with no side-effects
25bool isNonMutatingOperator(SgNode* n);
26
27// returns true if the given SgNode's sub-tree has no side-effects
28bool isNonMutatingSubTree(SgNode* n);
29
30// if the given SgNode is an SgStatement, returns that SgStatement. Otherwise, if it is an
31// gExpression or SgInitializedName, wraps it in an SgStatement and returns that.
32//static SgStatement* convertToStatement(SgNode* n);
33
34// Replace the expression from with the expression to in the SgNode parent, which
35// must be from's parent. Function checks to ensure that it is used properly.
36// Note that if parent is an SgInitializedName, to must be SgInitializer in order
37// for the replacement to work properly.
38void replaceExpressionChecked(SgNode* parent, SgExpression* from, SgExpression* to);
39
40// replace the from statement with the to statement in the parent SgNode
41// Note: Only parent = SgBasicBlock
42void replaceStatement(SgNode* parent, SgStatement* from, SgStatement* to);
43
44Sg_File_Info* getFileInfo(SgNode* n);
45
46 std::string getFileInfoString(SgNode* n);
47
48 std::string getFileInfoString(CFGNode n);
49
50// Returns the source of n's only in-edge. Yells if n has multiple in-edges.
51CFGNode theInEdge(CFGNode n);
52
53// Returns the target of n's only out-edge. Yells if n has multiple out-edges.
54CFGNode theOutEdge(CFGNode n);
55
56// FIXME -- these functions need to split subexpressions, since the expression
57// evaluation order in the CFG and the one used by the compiler may be
58// different
59
60// replace expr with (newNode, expr)
61SgExpression* insertBeforeExpression(SgExpression* expr, SgExpression* newNode);
62
63// replace expr with (expr, newNode)
64SgExpression* insertAfterExpression(SgExpression* expr, SgExpression* newNode);
65
66// replace stmt; with {newNode; stmt;}
67void replaceStatementByBlockBefore(SgStatement* stmt, SgStatement* newNode) ;
68
69// replace stmt; with {stmt; newNode;}
70void replaceStatementByBlockAfter(SgStatement* stmt, SgStatement* newNode);
71
72// creates a new variable declaration for a temporary variable
73// newName: the name of the temporary variable. If newName=="", a random name is generated.
74// varType: the type of the temporary variable, if byReference==true, the new variable's type is a reference type of varType
75//
76// sets varname to the new variable's SgName
77// sets initName to the new variable's SgInitializedName
78// sets newType to the new variable's type
79// sets newType to the new variable's declaration
80 void createTmpVarInit(SgType* varType, std::string newName, bool byReference,
81 SgName& varName, SgInitializedName *& initName, SgType*& newType, SgVariableDeclaration*& varDecl);
82
83// creates and returns a statement contains a call to the given function with no arguments
84SgStatement* createFuncCallStmt(SgFunctionDeclaration* funcDecl);
85
86// given a SgInitializedName, returns a SgVariableSymbol for the variable declared in the SgInitializedName
87SgVariableSymbol* varSymFromInitName(SgInitializedName* initName);
88
89// given a SgInitializedName, returns a SgVarRefExp to the variable declared in the SgInitializedName
90SgVarRefExp* varRefFromInitName(SgInitializedName* initName);
91
92// replaces the given SgExpression with a SgAssignOp (lhsVar = orig)
93// returns the SgAssignOp
94SgAssignOp * replaceExprWithAssignOp(SgExpression* orig, SgVarRefExp* lhsVar);
95
96// Creates a declaration of a new temporary variable of type varType and inserts it before anchor
97// if before=true, the temporary variable in inserted before anchor and otherwise, after anchor
98// sets initName to the SgInitializedName of the declaration
99void insertVarDecl(SgStatement *anchor, SgType *varType, bool before, SgInitializedName*& initName);
100
101// inserts the given expression before or after a given SgDeclaration that appears inside a SgForStatement
102void insertAroundForInit(SgVariableDeclaration* n, SgExpression *newNode, bool before);
103
104typedef void (*CFGTransform)(SgNode *target, SgNode* newNode, void* data);
105
107{
108 typedef enum {insBef, insAft, callback} modType;
109 class modRequest{
110 protected:
111 modType type;
112
113 public:
114 modRequest()
115 {}
116 /*modRequest(modType type_arg)
117 {
118 type = type_arg;
119 }*/
120
121 modType getType()
122 { return type; }
123
124 std::string str() { return ""; }
125 };
126
127 class insertRequest: public modRequest{
128 protected:
129 SgExpression *newNode;
130 SgNode* origNode;
131
132 public:
133 insertRequest(modType type_arg, SgNode* origNode, SgExpression *&newNode)
134 {
135 this->type = type_arg;
136 this->newNode = newNode;
137 this->origNode = origNode;
138 }
139
140 SgNode* getTgtNode() { return origNode; }
141
142 std::string str();
143 friend class cfgRWTransaction;
144 };
145
146 class transformRequest: public modRequest{
147 CFGTransform callbackFunc;
148 SgNode* target;
149 SgNode* newNode;
150 void* data;
151
152 public:
153 transformRequest(CFGTransform callbackFunc, SgNode *&target, SgNode* newNode, void* data)//: modRequest(callback)
154 {
155 this->type = callback;
156 this->target = target;
157 this->newNode = newNode;
158 this->callbackFunc = callbackFunc;
159 this->data = data;
160 }
161
162 std::string str();
163 friend class cfgRWTransaction;
164 };
165
166 public:
167 std::list<modRequest*> requests;
168 //list<void*> requests;
169
170
172
173 void beginTransaction();
174
175 void insertBefore(DataflowNode n, SgExpression* newNode);
176 void insertBefore(SgNode* n, SgExpression* newNode);
177
178 void insertAfter(DataflowNode n, SgExpression* newNode);
179 void insertAfter(SgNode* n, SgExpression* newNode);
180
181 void transform(CFGTransform callbackFunc, SgNode* n, SgNode* newNode, void* data);
182
183 // insert an SgNode along the given CFGEdge
184 void insertAlong(DataflowEdge e, SgExpression* newNode);
185
186 void commitTransaction();
187
188 protected:
189 void do_insertBefore(DataflowNode n, SgExpression* newNode);
190 void do_insertBefore(SgNode* n, SgExpression* newNode);
191 void do_insertAfter(DataflowNode n, SgExpression* newNode);
192 void do_insertAfter(SgNode* n, SgExpression* newNode);
193};
194
195/*************************************************************
196 *** CALL-BACK FUNCTIONS FOR cfgRWTransaction::transform() ***
197 *************************************************************/
198
199// places newNode as thh first statement in the given SgScopeStmt
200void prependToScopeStmt(SgNode *target, SgNode *newNode, void* data);
201
202// places newNode as thh last statement in the given SgBasicBlock
203void appendToScopeStmt(SgNode *target, SgNode *newNode, void* data);
204
205}
206#endif
207#endif
This class represents the notion of an expression. Expressions are derived from SgLocatedNodes,...
This class represents the concept of a function declaration statement.
This class represents the notion of a declared variable.
This class represents strings within the IR nodes.
This class represents the base class for all IR nodes within Sage III.
This class represents a source project, with a list of SgFile objects and global information about th...
This class represents the notion of a statement.
This class represents the base class for all types.
This class represents the variable refernece in expressions.
This class represents the concept of a C or C++ variable declaration.
This class represents the concept of a variable name within the compiler (a shared container for the ...
This class represents the location of the code associated with the IR node in the original source cod...
A node in the control flow graph.
Definition virtualCFG.h:70
ROSE_DLL_API void replaceStatement(SgStatement *oldStmt, SgStatement *newStmt, bool movePreprocessinInfo=false)
Replace a statement with another. Move preprocessing information from oldStmt to newStmt if requested...