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