ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CFGRewrite.h
Go to the documentation of this file.
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 
20 
21 // returns true if the given SgNode is a value expression or a computational expression with no side-effects
23 
24 // returns true if the given SgNode's sub-tree has no side-effects
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.
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 
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.
49 
50 // Returns the target of n's only out-edge. Yells if n has multiple out-edges.
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)
59 
60 // replace expr with (expr, newNode)
62 
63 // replace stmt; with {newNode; stmt;}
65 
66 // replace stmt; with {stmt; 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
82 
83 // given a SgInitializedName, returns a SgVariableSymbol for the variable declared in the SgInitializedName
85 
86 // given a SgInitializedName, returns a SgVarRefExp to the variable declared in the SgInitializedName
88 
89 // replaces the given SgExpression with a SgAssignOp (lhsVar = orig)
90 // returns the SgAssignOp
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:
109 
110  public:
112  {}
113  /*modRequest(modType type_arg)
114  {
115  type = type_arg;
116  }*/
117 
119  { return type; }
120 
121  std::string str() { return ""; }
122  };
123 
124  class insertRequest: public modRequest{
125  protected:
128 
129  public:
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 
147  void* data;
148 
149  public:
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