Ghidra Decompiler Analysis Engine
slghpatexpress.hh
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  */
16 #ifndef __SLGHPATEXPRESS__
17 #define __SLGHPATEXPRESS__
18 
19 #include "slghpattern.hh"
20 
21 class TokenPattern {
22  Pattern *pattern;
23  vector<Token *> toklist;
24  bool leftellipsis;
25  bool rightellipsis;
26  static PatternBlock *buildSingle(int4 startbit,int4 endbit,uintm byteval);
27  static PatternBlock *buildBigBlock(int4 size,int4 bitstart,int4 bitend,intb value);
28  static PatternBlock *buildLittleBlock(int4 size,int4 bitstart,int4 bitend,intb value);
29  int4 resolveTokens(const TokenPattern &tokpat1,const TokenPattern &tokpat2);
30  TokenPattern(Pattern *pat) { pattern = pat; leftellipsis=false; rightellipsis=false; }
31 public:
32  TokenPattern(void); // TRUE pattern unassociated with a token
33  TokenPattern(bool tf); // TRUE or FALSE pattern unassociated with a token
34  TokenPattern(Token *tok); // TRUE pattern associated with token -tok-
35  TokenPattern(Token *tok,intb value,int4 bitstart,int4 bitend);
36  TokenPattern(intb value,int4 startbit,int4 endbit);
37  TokenPattern(const TokenPattern &tokpat);
38  ~TokenPattern(void) { delete pattern; }
39  const TokenPattern &operator=(const TokenPattern &tokpat);
40  void setLeftEllipsis(bool val) { leftellipsis = val; }
41  void setRightEllipsis(bool val) { rightellipsis = val; }
42  bool getLeftEllipsis(void) const { return leftellipsis; }
43  bool getRightEllipsis(void) const { return rightellipsis; }
44  TokenPattern doAnd(const TokenPattern &tokpat) const;
45  TokenPattern doOr(const TokenPattern &tokpat) const;
46  TokenPattern doCat(const TokenPattern &tokpat) const;
47  TokenPattern commonSubPattern(const TokenPattern &tokpat) const;
48  Pattern *getPattern(void) const { return pattern; }
49  int4 getMinimumLength(void) const;
50  bool alwaysTrue(void) const { return pattern->alwaysTrue(); }
51  bool alwaysFalse(void) const { return pattern->alwaysFalse(); }
52  bool alwaysInstructionTrue(void) const { return pattern->alwaysInstructionTrue(); }
53 };
54 
55 class PatternValue;
57  int4 refcount; // Number of objects referencing this
58  // for deletion
59 protected:
60  virtual ~PatternExpression(void) {} // Only delete through release
61 public:
62  PatternExpression(void) { refcount = 0; }
63  virtual intb getValue(ParserWalker &walker) const=0;
64  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const=0;
65  virtual void listValues(vector<const PatternValue *> &list) const=0;
66  virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const=0;
67  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const=0;
68  virtual void saveXml(ostream &s) const=0;
69  virtual void restoreXml(const Element *el,Translate *trans)=0;
70  intb getSubValue(const vector<intb> &replace) {
71  int4 listpos = 0;
72  return getSubValue(replace,listpos); }
73  void layClaim(void) { refcount += 1; }
74  static void release(PatternExpression *p);
75  static PatternExpression *restoreExpression(const Element *el,Translate *trans);
76 };
77 
79 public:
80  virtual TokenPattern genPattern(intb val) const=0;
81  virtual void listValues(vector<const PatternValue *> &list) const { list.push_back(this); }
82  virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const {
83  minlist.push_back(minValue()); maxlist.push_back(maxValue()); }
84  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const { return replace[listpos++]; }
85  virtual intb minValue(void) const=0;
86  virtual intb maxValue(void) const=0;
87 };
88 
89 class TokenField : public PatternValue {
90  Token *tok;
91  bool bigendian;
92  bool signbit;
93  int4 bitstart,bitend; // Bits within the token, 0 bit is LEAST significant
94  int4 bytestart,byteend; // Bytes to read to get value
95  int4 shift; // Amount to shift to align value (bitstart % 8)
96 public:
97  TokenField(void) {} // For use with restoreXml
98  TokenField(Token *tk,bool s,int4 bstart,int4 bend);
99  virtual intb getValue(ParserWalker &walker) const;
100  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(tok); }
101  virtual TokenPattern genPattern(intb val) const;
102  virtual intb minValue(void) const { return 0; }
103  virtual intb maxValue(void) const { intb res=0; res=~res; zero_extend(res,bitend-bitstart); return res; }
104  virtual void saveXml(ostream &s) const;
105  virtual void restoreXml(const Element *el,Translate *trans);
106 };
107 
108 class ContextField : public PatternValue {
109  int4 startbit,endbit;
110  int4 startbyte,endbyte;
111  int4 shift;
112  bool signbit;
113 public:
114  ContextField(void) {} // For use with restoreXml
115  ContextField(bool s,int4 sbit,int4 ebit);
116  int4 getStartBit(void) const { return startbit; }
117  int4 getEndBit(void) const { return endbit; }
118  bool getSignBit(void) const { return signbit; }
119  virtual intb getValue(ParserWalker &walker) const;
120  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
121  virtual TokenPattern genPattern(intb val) const;
122  virtual intb minValue(void) const { return 0; }
123  virtual intb maxValue(void) const { intb res=0; res=~res; zero_extend(res,(endbit-startbit)); return res; }
124  virtual void saveXml(ostream &s) const;
125  virtual void restoreXml(const Element *el,Translate *trans);
126 };
127 
128 class ConstantValue : public PatternValue {
129  intb val;
130 public:
131  ConstantValue(void) {} // For use with restoreXml
132  ConstantValue(intb v) { val = v; }
133  virtual intb getValue(ParserWalker &walker) const { return val; }
134  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
135  virtual TokenPattern genPattern(intb v) const { return TokenPattern(val==v); }
136  virtual intb minValue(void) const { return val; }
137  virtual intb maxValue(void) const { return val; }
138  virtual void saveXml(ostream &s) const;
139  virtual void restoreXml(const Element *el,Translate *trans);
140 };
141 
143 public:
144  StartInstructionValue(void) {}
145  virtual intb getValue(ParserWalker &walker) const {
146  return (intb)AddrSpace::byteToAddress(walker.getAddr().getOffset(),walker.getAddr().getSpace()->getWordSize()); }
147  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
148  virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
149  virtual intb minValue(void) const { return (intb)0; }
150  virtual intb maxValue(void) const { return (intb)0; }
151  virtual void saveXml(ostream &s) const { s << "<start_exp/>"; }
152  virtual void restoreXml(const Element *el,Translate *trans) {}
153 };
154 
156 public:
157  EndInstructionValue(void) {}
158  virtual intb getValue(ParserWalker &walker) const {
159  return (intb)AddrSpace::byteToAddress(walker.getNaddr().getOffset(),walker.getNaddr().getSpace()->getWordSize()); }
160  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
161  virtual TokenPattern genPattern(intb val) const { return TokenPattern(); }
162  virtual intb minValue(void) const { return (intb)0; }
163  virtual intb maxValue(void) const { return (intb)0; }
164  virtual void saveXml(ostream &s) const { s << "<end_exp/>"; }
165  virtual void restoreXml(const Element *el,Translate *trans) {}
166 };
167 
168 class Constructor; // Forward declaration
169 class OperandSymbol;
170 class OperandValue : public PatternValue {
171  int4 index; // This is the defining field of expression
172  Constructor *ct; // cached pointer to constructor
173 public:
174  OperandValue(void) { } // For use with restoreXml
175  OperandValue(int4 ind,Constructor *c) { index = ind; ct = c; }
176  void changeIndex(int4 newind) { index = newind; }
177  bool isConstructorRelative(void) const;
178  const string &getName(void) const;
179  virtual TokenPattern genPattern(intb val) const;
180  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return ops[index]; }
181  virtual intb getValue(ParserWalker &walker) const;
182  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
183  virtual intb minValue(void) const;
184  virtual intb maxValue(void) const;
185  virtual void saveXml(ostream &s) const;
186  virtual void restoreXml(const Element *el,Translate *trans);
187 };
188 
190  PatternExpression *left,*right;
191 protected:
192  virtual ~BinaryExpression(void);
193 public:
194  BinaryExpression(void) { left = (PatternExpression *)0; right = (PatternExpression *)0; } // For use with restoreXml
196  PatternExpression *getLeft(void) const { return left; }
197  PatternExpression *getRight(void) const { return right; }
198  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
199  virtual void listValues(vector<const PatternValue *> &list) const {
200  left->listValues(list); right->listValues(list); }
201  virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const {
202  left->getMinMax(minlist,maxlist); right->getMinMax(minlist,maxlist); }
203  virtual void saveXml(ostream &s) const;
204  virtual void restoreXml(const Element *el,Translate *trans);
205 };
206 
208  PatternExpression *unary;
209 protected:
210  virtual ~UnaryExpression(void);
211 public:
212  UnaryExpression(void) { unary = (PatternExpression *)0; } // For use with restoreXml
214  PatternExpression *getUnary(void) const { return unary; }
215  virtual TokenPattern genMinPattern(const vector<TokenPattern> &ops) const { return TokenPattern(); }
216  virtual void listValues(vector<const PatternValue *> &list) const {
217  unary->listValues(list); }
218  virtual void getMinMax(vector<intb> &minlist,vector<intb> &maxlist) const {
219  unary->getMinMax(minlist,maxlist);
220  }
221  virtual void saveXml(ostream &s) const;
222  virtual void restoreXml(const Element *el,Translate *trans);
223 };
224 
226 public:
227  PlusExpression(void) {} // For use by restoreXml
229  virtual intb getValue(ParserWalker &walker) const;
230  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
231  virtual void saveXml(ostream &s) const;
232 };
233 
235 public:
236  SubExpression(void) {} // For use with restoreXml
238  virtual intb getValue(ParserWalker &walker) const;
239  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
240  virtual void saveXml(ostream &s) const;
241 };
242 
244 public:
245  MultExpression(void) {} // For use with restoreXml
247  virtual intb getValue(ParserWalker &walker) const;
248  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
249  virtual void saveXml(ostream &s) const;
250 };
251 
253 public:
254  LeftShiftExpression(void) {}
256  virtual intb getValue(ParserWalker &walker) const;
257  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
258  virtual void saveXml(ostream &s) const;
259 };
260 
262 public:
263  RightShiftExpression(void) {}
265  virtual intb getValue(ParserWalker &walker) const;
266  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
267  virtual void saveXml(ostream &s) const;
268 };
269 
271 public:
272  AndExpression(void) {}
274  virtual intb getValue(ParserWalker &walker) const;
275  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
276  virtual void saveXml(ostream &s) const;
277 };
278 
280 public:
281  OrExpression(void) {}
283  virtual intb getValue(ParserWalker &walker) const;
284  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
285  virtual void saveXml(ostream &s) const;
286 };
287 
289 public:
290  XorExpression(void) {}
292  virtual intb getValue(ParserWalker &walker) const;
293  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
294  virtual void saveXml(ostream &s) const;
295 };
296 
298 public:
299  DivExpression(void) {}
301  virtual intb getValue(ParserWalker &walker) const;
302  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
303  virtual void saveXml(ostream &s) const;
304 };
305 
307 public:
308  MinusExpression(void) {}
310  virtual intb getValue(ParserWalker &walker) const;
311  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
312  virtual void saveXml(ostream &s) const;
313 };
314 
316 public:
317  NotExpression(void) {}
319  virtual intb getValue(ParserWalker &walker) const;
320  virtual intb getSubValue(const vector<intb> &replace,int4 &listpos) const;
321  virtual void saveXml(ostream &s) const;
322 };
323 
325  vector<OperandSymbol *> &operands;
326  OperandResolve(vector<OperandSymbol *> &ops) : operands(ops) {
327  base=-1; offset=0; cur_rightmost = -1; size = 0; }
328  int4 base; // Current base operand (as we traverse the pattern equation from left to right)
329  int4 offset; // Bytes we have traversed from the LEFT edge of the current base
330  int4 cur_rightmost; // (resulting) rightmost operand in our pattern
331  int4 size; // (resulting) bytes traversed from the LEFT edge of the rightmost
332 };
333 
334 // operandOrder returns a vector of the self-defining OperandSymbols as the appear
335 // in left to right order in the pattern
337  int4 refcount; // Number of objects referencing this
338 protected:
339  mutable TokenPattern resultpattern; // Resulting pattern generated by this equation
340  virtual ~PatternEquation(void) {} // Only delete through release
341 public:
342  PatternEquation(void) { refcount = 0; }
343  const TokenPattern &getTokenPattern(void) const { return resultpattern; }
344  virtual void genPattern(const vector<TokenPattern> &ops) const=0;
345  virtual bool resolveOperandLeft(OperandResolve &state) const=0;
346  virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const {}
347  void layClaim(void) { refcount += 1; }
348  static void release(PatternEquation *pateq);
349 };
350 
351 class OperandEquation : public PatternEquation { // Equation that defines operand
352  int4 index;
353 public:
354  OperandEquation(int4 ind) { index = ind; }
355  virtual void genPattern(const vector<TokenPattern> &ops) const;
356  virtual bool resolveOperandLeft(OperandResolve &state) const;
357  virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
358 };
359 
360 class UnconstrainedEquation : public PatternEquation { // Unconstrained equation, just get tokens
361  PatternExpression *patex;
362 protected:
363  virtual ~UnconstrainedEquation(void);
364 public:
366  virtual void genPattern(const vector<TokenPattern> &ops) const;
367  virtual bool resolveOperandLeft(OperandResolve &state) const;
368 };
369 
371 protected:
372  PatternValue *lhs;
373  PatternExpression *rhs;
374  virtual ~ValExpressEquation(void);
375 public:
377  virtual bool resolveOperandLeft(OperandResolve &state) const;
378 };
379 
381 public:
383  virtual void genPattern(const vector<TokenPattern> &ops) const;
384 };
385 
387 public:
389  virtual void genPattern(const vector<TokenPattern> &ops) const;
390 };
391 
393 public:
395  virtual void genPattern(const vector<TokenPattern> &ops) const;
396 };
397 
399 public:
401  virtual void genPattern(const vector<TokenPattern> &ops) const;
402 };
403 
405 public:
407  virtual void genPattern(const vector<TokenPattern> &ops) const;
408 };
409 
411 public:
413  virtual void genPattern(const vector<TokenPattern> &ops) const;
414 };
415 
416 class EquationAnd : public PatternEquation { // Pattern Equations ANDed together
417  PatternEquation *left;
418  PatternEquation *right;
419 protected:
420  virtual ~EquationAnd(void);
421 public:
423  virtual void genPattern(const vector<TokenPattern> &ops) const;
424  virtual bool resolveOperandLeft(OperandResolve &state) const;
425  virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
426 };
427 
428 class EquationOr : public PatternEquation { // Pattern Equations ORed together
429  PatternEquation *left;
430  PatternEquation *right;
431 protected:
432  virtual ~EquationOr(void);
433 public:
435  virtual void genPattern(const vector<TokenPattern> &ops) const;
436  virtual bool resolveOperandLeft(OperandResolve &state) const;
437  virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
438 };
439 
440 class EquationCat : public PatternEquation { // Pattern Equations concatenated
441  PatternEquation *left;
442  PatternEquation *right;
443 protected:
444  virtual ~EquationCat(void);
445 public:
447  virtual void genPattern(const vector<TokenPattern> &ops) const;
448  virtual bool resolveOperandLeft(OperandResolve &state) const;
449  virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
450 };
451 
452 class EquationLeftEllipsis : public PatternEquation { // Equation preceded by ellipses
453  PatternEquation *eq;
454 protected:
455  virtual ~EquationLeftEllipsis(void) { PatternEquation::release(eq); }
456 public:
457  EquationLeftEllipsis(PatternEquation *e) { (eq=e)->layClaim(); }
458  virtual void genPattern(const vector<TokenPattern> &ops) const;
459  virtual bool resolveOperandLeft(OperandResolve &state) const;
460  virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
461 };
462 
463 class EquationRightEllipsis : public PatternEquation { // Equation preceded by ellipses
464  PatternEquation *eq;
465 protected:
466  virtual ~EquationRightEllipsis(void) { PatternEquation::release(eq); }
467 public:
468  EquationRightEllipsis(PatternEquation *e) { (eq=e)->layClaim(); }
469  virtual void genPattern(const vector<TokenPattern> &ops) const;
470  virtual bool resolveOperandLeft(OperandResolve &state) const;
471  virtual void operandOrder(Constructor *ct,vector<OperandSymbol *> &order) const;
472 };
473 
474 #endif
EquationCat
Definition: slghpatexpress.hh:440
PlusExpression
Definition: slghpatexpress.hh:225
AndExpression
Definition: slghpatexpress.hh:270
PatternExpression
Definition: slghpatexpress.hh:56
AddrSpace::byteToAddress
static uintb byteToAddress(uintb val, uint4 ws)
Scale from byte units to addressable units.
Definition: space.hh:505
Constructor
Definition: slghsymbol.hh:466
XorExpression
Definition: slghpatexpress.hh:288
AddrSpace::getWordSize
uint4 getWordSize(void) const
Get the addressable unit size.
Definition: space.hh:327
DivExpression
Definition: slghpatexpress.hh:297
EndInstructionValue
Definition: slghpatexpress.hh:155
PatternEquation
Definition: slghpatexpress.hh:336
NotEqualEquation
Definition: slghpatexpress.hh:386
NotExpression
Definition: slghpatexpress.hh:315
OrExpression
Definition: slghpatexpress.hh:279
ConstantValue
Definition: slghpatexpress.hh:128
OperandValue
Definition: slghpatexpress.hh:170
RightShiftExpression
Definition: slghpatexpress.hh:261
OperandSymbol
Definition: slghsymbol.hh:313
TokenField
Definition: slghpatexpress.hh:89
Address::getOffset
uintb getOffset(void) const
Get the address offset.
Definition: address.hh:300
StartInstructionValue
Definition: slghpatexpress.hh:142
zero_extend
void zero_extend(intb &val, int4 bit)
Clear all bits above given bit.
Definition: address.cc:639
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
Token
Definition: context.hh:22
BinaryExpression
Definition: slghpatexpress.hh:189
TokenPattern
Definition: slghpatexpress.hh:21
EquationOr
Definition: slghpatexpress.hh:428
PatternBlock
Definition: slghpattern.hh:22
EquationRightEllipsis
Definition: slghpatexpress.hh:463
PatternValue
Definition: slghpatexpress.hh:78
MultExpression
Definition: slghpatexpress.hh:243
OperandEquation
Definition: slghpatexpress.hh:351
ContextField
Definition: slghpatexpress.hh:108
Address::getSpace
AddrSpace * getSpace(void) const
Get the address space.
Definition: address.hh:294
SubExpression
Definition: slghpatexpress.hh:234
EqualEquation
Definition: slghpatexpress.hh:380
Translate
The interface to a translation engine for a processor.
Definition: translate.hh:294
UnconstrainedEquation
Definition: slghpatexpress.hh:360
MinusExpression
Definition: slghpatexpress.hh:306
LessEquation
Definition: slghpatexpress.hh:392
Pattern
Definition: slghpattern.hh:51
OperandResolve
Definition: slghpatexpress.hh:324
LeftShiftExpression
Definition: slghpatexpress.hh:252
LessEqualEquation
Definition: slghpatexpress.hh:398
EquationAnd
Definition: slghpatexpress.hh:416
GreaterEquation
Definition: slghpatexpress.hh:404
EquationLeftEllipsis
Definition: slghpatexpress.hh:452
ParserWalker
Definition: context.hh:124
ValExpressEquation
Definition: slghpatexpress.hh:370
GreaterEqualEquation
Definition: slghpatexpress.hh:410
UnaryExpression
Definition: slghpatexpress.hh:207