My Project
action.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
18 #ifndef __CPUI_ACTION__
19 #define __CPUI_ACTION__
20 
21 #include "block.hh"
22 
30  friend class ActionDatabase;
31  set<string> list;
32 public:
37  bool contains(const string &nm) const { return (list.find(nm)!=list.end()); }
38 };
39 
40 class Rule;
41 
50 class Action {
51 public:
53  enum ruleflags {
54  rule_repeatapply = 4,
55  rule_onceperfunc = 8,
56  rule_oneactperfunc = 16,
57  rule_debug = 32,
58  rule_warnings_on = 64,
59  rule_warnings_given = 128
60  };
62  enum statusflags {
63  status_start=1,
64  status_breakstarthit=2,
65  status_repeat=4,
66  status_mid=8,
67  status_end=16,
68  status_actionbreak=32
69  };
71  enum breakflags {
72  break_start = 1,
73  tmpbreak_start = 2,
74  break_action = 4,
75  tmpbreak_action = 8
76  };
77 protected:
78  int4 lcount;
79  int4 count;
80  uint4 status;
81  uint4 breakpoint;
82  uint4 flags;
83  uint4 count_tests;
84  uint4 count_apply;
85  string name;
86  string basegroup;
87  void issueWarning(Architecture *glb);
88  bool checkStartBreak(void);
89  bool checkActionBreak(void);
90  void turnOnWarnings(void) { flags |= rule_warnings_on; }
91  void turnOffWarnings(void) { flags &= ~rule_warnings_on; }
92 public:
93  Action(uint4 f,const string &nm,const string &g);
94  virtual ~Action(void) {}
95 #ifdef OPACTION_DEBUG
96  virtual bool turnOnDebug(const string &nm);
97  virtual bool turnOffDebug(const string &nm);
98 #endif
99  virtual void printStatistics(ostream &s) const;
100  int4 perform(Funcdata &data);
101  bool setBreakPoint(uint4 tp,const string &specify);
102  bool setWarning(bool val,const string &specify);
103  bool disableRule(const string &specify);
104  bool enableRule(const string &specify);
105  const string &getName(void) const { return name; }
106  const string &getGroup(void) const { return basegroup; }
107  uint4 getStatus(void) const { return status; }
108  uint4 getNumTests(void) { return count_tests; }
109  uint4 getNumApply(void) { return count_apply; }
110  virtual Action *clone(const ActionGroupList &grouplist) const=0;
117  virtual void reset(Funcdata &data);
118  virtual void resetStats(void);
119  virtual int4 apply(Funcdata &data)=0;
128  virtual int4 print(ostream &s,int4 num,int4 depth) const;
129  virtual void printState(ostream &s) const;
130  virtual void saveXml(ostream &s) const {}
131  virtual void restoreXml(const Element *el,Funcdata *fd) {}
132  virtual Action *getSubAction(const string &specify);
133  virtual Rule *getSubRule(const string &specify);
134 };
135 
142 class ActionGroup : public Action {
143 protected:
144  vector<Action *> list;
145  vector<Action *>::iterator state;
146 public:
147  ActionGroup(uint4 f,const string &nm) : Action(f,nm,"") {}
148  virtual ~ActionGroup(void);
149  void addAction(Action *ac);
150  virtual Action *clone(const ActionGroupList &grouplist) const;
151  virtual void reset(Funcdata &data);
152  virtual void resetStats(void);
153  virtual int4 apply(Funcdata &data);
154  virtual int4 print(ostream &s,int4 num,int4 depth) const;
155  virtual void printState(ostream &s) const;
156  virtual Action *getSubAction(const string &specify);
157  virtual Rule *getSubRule(const string &specify);
158 #ifdef OPACTION_DEBUG
159  virtual bool turnOnDebug(const string &nm);
160  virtual bool turnOffDebug(const string &nm);
161 #endif
162  virtual void printStatistics(ostream &s) const;
163 };
164 
172  int4 maxrestarts;
173  int4 curstart;
174 public:
175  ActionRestartGroup(uint4 f,const string &nm,int4 max) :
176  ActionGroup(f,nm) { maxrestarts = max; }
177  virtual Action *clone(const ActionGroupList &grouplist) const;
178  virtual void reset(Funcdata &data);
179  virtual int4 apply(Funcdata &data);
180 };
181 
192 class Rule {
193 public:
195  enum typeflags {
196  type_disable = 1,
197  rule_debug = 2,
198  warnings_on = 4,
199  warnings_given = 8
200  };
201 private:
202  friend struct ActionPool;
203  uint4 flags;
204  uint4 breakpoint;
205  string name;
206  string basegroup;
207  uint4 count_tests;
208  uint4 count_apply;
209  void issueWarning(Architecture *glb);
210 public:
211  Rule(const string &g,uint4 fl,const string &nm);
212  virtual ~Rule(void) {}
213  const string &getName(void) const { return name; }
214  const string &getGroup(void) const { return basegroup; }
215  uint4 getNumTests(void) { return count_tests; }
216  uint4 getNumApply(void) { return count_apply; }
217  void setBreak(uint4 tp) { breakpoint |= tp; }
218  void clearBreak(uint4 tp) { breakpoint &= ~tp; }
219  void turnOnWarnings(void) { flags |= warnings_on; }
220  void turnOffWarnings(void) { flags &= ~warnings_on; }
221  bool isDisabled(void) const { return ((flags & type_disable)!=0); }
222  void setDisable(void) { flags |= type_disable; }
223  void clearDisable(void) { flags &= ~type_disable; }
224  bool checkActionBreak(void);
225  uint4 getBreakPoint(void) const { return breakpoint; }
226 
233  virtual Rule *clone(const ActionGroupList &grouplist) const=0;
234  virtual void getOpList(vector<uint4> &oplist) const;
235 
243  virtual int4 applyOp(PcodeOp *op,Funcdata &data) { return 0; }
244  virtual void reset(Funcdata &data);
245  virtual void resetStats(void);
246  virtual void printStatistics(ostream &s) const;
247 #ifdef OPACTION_DEBUG
248  virtual bool turnOnDebug(const string &nm);
249  virtual bool turnOffDebug(const string &nm);
250 #endif
251 };
252 
259 class ActionPool : public Action {
260  vector<Rule *> allrules;
261  vector<Rule *> perop[CPUI_MAX];
262  PcodeOpTree::const_iterator op_state;
263  int4 rule_index;
264  int4 processOp(PcodeOp *op,Funcdata &data);
265 public:
266  ActionPool(uint4 f,const string &nm) : Action(f,nm,"") {}
267  virtual ~ActionPool(void);
268  void addRule(Rule *rl);
269  virtual Action *clone(const ActionGroupList &grouplist) const;
270  virtual void reset(Funcdata &data);
271  virtual void resetStats(void);
272  virtual int4 apply(Funcdata &data);
273  virtual int4 print(ostream &s,int4 num,int4 depth) const;
274  virtual void printState(ostream &s) const;
275  virtual Rule *getSubRule(const string &specify);
276  virtual void printStatistics(ostream &s) const;
277 #ifdef OPACTION_DEBUG
278  virtual bool turnOnDebug(const string &nm);
279  virtual bool turnOffDebug(const string &nm);
280 #endif
281 };
282 
295  Action *currentact;
296  string currentactname;
297  map<string,ActionGroupList> groupmap;
298  map<string,Action *> actionmap;
299  static const char universalname[];
300  void registerAction(const string &nm,Action *act);
301  Action *getAction(const string &nm) const;
302  Action *deriveAction(const string &baseaction,const string &grp);
303 public:
304  ActionDatabase(void) { currentact = (Action *)0; }
305  ~ActionDatabase(void);
306  void registerUniversal(Action *act);
307  Action *getCurrent(void) const { return currentact; }
308  const string &getCurrentName(void) const { return currentactname; }
309  const ActionGroupList &getGroup(const string &grp) const;
310  Action *setCurrent(const string &actname);
311  Action *toggleAction(const string &grp,const string &basegrp,bool val);
312 
313  void setGroup(const string &grp,const char **argv);
314  void cloneGroup(const string &oldname,const string &newname);
315  bool addToGroup(const string &grp,const string &basegroup);
316  bool removeFromGroup(const string &grp,const string &basegroup);
317 };
318 
319 #endif
vector< Action * >::iterator state
Current action being applied.
Definition: action.hh:145
uint4 status
Current status.
Definition: action.hh:80
const string & getName(void) const
Return the name of this Rule.
Definition: action.hh:213
uint4 getNumApply(void)
Definition: action.hh:109
void turnOnWarnings(void)
Enable warnings for this Rule.
Definition: action.hh:219
virtual ~Action(void)
Destructor.
Definition: action.hh:94
A pool of Rules that apply simultaneously.
Definition: action.hh:259
string name
Name of the action.
Definition: action.hh:85
uint4 getStatus(void) const
Get the current status of this Action.
Definition: action.hh:107
const string & getGroup(void) const
Get the Action&#39;s group.
Definition: action.hh:106
The list of groups defining a root Action.
Definition: action.hh:29
Container for data structures associated with a single function.
Definition: funcdata.hh:45
bool isDisabled(void) const
Return true if this Rule is disabled.
Definition: action.hh:221
int4 lcount
Changes not including last call to apply()
Definition: action.hh:78
ActionPool(uint4 f, const string &nm)
Construct providing properties and name.
Definition: action.hh:266
bool contains(const string &nm) const
Check if this ActionGroupList contains a given group.
Definition: action.hh:37
bool checkActionBreak(void)
Check action breakpoint.
Definition: action.cc:115
typeflags
Properties associated with a Rule.
Definition: action.hh:195
uint4 getNumTests(void)
Get number of attempted applications.
Definition: action.hh:215
vector< Action * > list
List of actions to perform in the group.
Definition: action.hh:144
ruleflags
Boolean behavior properties governing this particular Action.
Definition: action.hh:53
Large scale transformations applied to the varnode/op graph.
Definition: action.hh:50
string basegroup
Base group this action belongs to.
Definition: action.hh:86
void clearBreak(uint4 tp)
Clear a breakpoint on this Rule.
Definition: action.hh:218
ActionRestartGroup(uint4 f, const string &nm, int4 max)
Construct this providing maximum number of restarts.
Definition: action.hh:175
uint4 getBreakPoint(void) const
Return breakpoint toggles.
Definition: action.hh:225
Action * getCurrent(void) const
Get the current root Action.
Definition: action.hh:307
uint4 breakpoint
Breakpoint properties.
Definition: action.hh:81
Lowest level operation of the p-code language.
Definition: op.hh:58
statusflags
Boolean properties describing the status of an action.
Definition: action.hh:62
void turnOffWarnings(void)
Disable warnings for this Rule.
Definition: action.hh:220
uint4 count_apply
Number of times apply() made changes.
Definition: action.hh:84
virtual void restoreXml(const Element *el, Funcdata *fd)
Load specifics of action from XML.
Definition: action.hh:131
ActionGroup(uint4 f, const string &nm)
Construct given properties and a name.
Definition: action.hh:147
void issueWarning(Architecture *glb)
Warn that this Action has applied.
Definition: action.cc:39
Manager for all the major decompiler subsystems.
Definition: architecture.hh:117
Classes related to basic blocks and control-flow structuring.
virtual int4 applyOp(PcodeOp *op, Funcdata &data)
Attempt to apply this Rule.
Definition: action.hh:243
virtual ~Rule(void)
Destructor.
Definition: action.hh:212
A group of actions (generally) applied in sequence.
Definition: action.hh:142
const string & getCurrentName(void) const
Get the name of the current root Action.
Definition: action.hh:308
An XML element. A node in the DOM tree.
Definition: xml.hh:150
Class for performing a single transformation on a PcodeOp or Varnode.
Definition: action.hh:192
void clearDisable(void)
Enable this Rule (within its pool)
Definition: action.hh:223
virtual Action * clone(const ActionGroupList &grouplist) const
Clone the Action.
Definition: action.cc:873
ActionDatabase(void)
Constructor.
Definition: action.hh:304
virtual void saveXml(ostream &s) const
Save specifics of this action to stream.
Definition: action.hh:130
uint4 getNumTests(void)
Get the number of times apply() was invoked.
Definition: action.hh:108
const string & getGroup(void) const
Return the group this Rule belongs to.
Definition: action.hh:214
const string & getName(void) const
Get the Action&#39;s name.
Definition: action.hh:105
virtual void reset(Funcdata &data)
Reset the Action for a new function.
Definition: action.cc:890
virtual void resetStats(void)
Reset all the counts to zero.
Definition: action.cc:900
Database of root Action objects that can be used to transform a function.
Definition: action.hh:294
uint4 flags
Behavior properties.
Definition: action.hh:82
Action which checks if restart (sub)actions have been generated and restarts itself.
Definition: action.hh:171
void turnOffWarnings(void)
Disable warnings for this Action.
Definition: action.hh:91
void setBreak(uint4 tp)
Set a breakpoint on this Rule.
Definition: action.hh:217
Value indicating the end of the op-code values.
Definition: opcodes.hh:127
void turnOnWarnings(void)
Enable warnings for this Action.
Definition: action.hh:90
breakflags
Break points associated with an Action.
Definition: action.hh:71
uint4 getNumApply(void)
Get number of successful applications.
Definition: action.hh:216
uint4 count_tests
Number of times apply() has been called.
Definition: action.hh:83
virtual void printStatistics(ostream &s) const
Dump statistics to stream.
Definition: action.cc:938
int4 count
Number of changes made by this action so far.
Definition: action.hh:79
void setDisable(void)
Disable this Rule (within its pool)
Definition: action.hh:222