Ghidra Decompiler Analysis Engine
slghpattern.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 __SLGHPATTERN__
17 #define __SLGHPATTERN__
18 
19 #include "context.hh"
20 
21 // A mask/value pair viewed as two bitstreams
22 class PatternBlock {
23  int4 offset; // Offset to non-zero byte of mask
24  int4 nonzerosize; // Last byte(+1) containing nonzero mask
25  vector<uintm> maskvec; // Mask
26  vector<uintm> valvec; // Value
27  void normalize(void);
28 public:
29  PatternBlock(int4 off,uintm msk,uintm val);
30  PatternBlock(bool tf);
31  PatternBlock(const PatternBlock *a,const PatternBlock *b);
32  PatternBlock(vector<PatternBlock *> &list);
33  PatternBlock *commonSubPattern(const PatternBlock *b) const;
34  PatternBlock *intersect(const PatternBlock *b) const;
35  bool specializes(const PatternBlock *op2) const;
36  bool identical(const PatternBlock *op2) const;
37  PatternBlock *clone(void) const;
38  void shift(int4 sa) { offset += sa; normalize(); }
39  int4 getLength(void) const { return offset+nonzerosize; }
40  uintm getMask(int4 startbit,int4 size) const;
41  uintm getValue(int4 startbit,int4 size) const;
42  bool alwaysTrue(void) const { return (nonzerosize==0); }
43  bool alwaysFalse(void) const { return (nonzerosize==-1); }
44  bool isInstructionMatch(ParserWalker &walker) const;
45  bool isContextMatch(ParserWalker &walker) const;
46  void saveXml(ostream &s) const;
47  void restoreXml(const Element *el);
48 };
49 
50 class DisjointPattern;
51 class Pattern {
52 public:
53  virtual ~Pattern(void) {}
54  virtual Pattern *simplifyClone(void) const=0;
55  virtual void shiftInstruction(int4 sa)=0;
56  virtual Pattern *doOr(const Pattern *b,int4 sa) const=0;
57  virtual Pattern *doAnd(const Pattern *b,int4 sa) const=0;
58  virtual Pattern *commonSubPattern(const Pattern *b,int4 sa) const=0;
59  virtual bool isMatch(ParserWalker &walker) const=0; // Does this pattern match context
60  virtual int4 numDisjoint(void) const=0;
61  virtual DisjointPattern *getDisjoint(int4 i) const=0;
62  virtual bool alwaysTrue(void) const=0;
63  virtual bool alwaysFalse(void) const=0;
64  virtual bool alwaysInstructionTrue(void) const=0;
65  virtual void saveXml(ostream &s) const=0;
66  virtual void restoreXml(const Element *el)=0;
67 };
68 
69 class DisjointPattern : public Pattern { // A pattern with no ORs in it
70  virtual PatternBlock *getBlock(bool context) const=0;
71 public:
72  virtual int4 numDisjoint(void) const { return 0; }
73  virtual DisjointPattern *getDisjoint(int4 i) const { return (DisjointPattern *)0; }
74  uintm getMask(int4 startbit,int4 size,bool context) const;
75  uintm getValue(int4 startbit,int4 size,bool context) const;
76  int4 getLength(bool context) const;
77  bool specializes(const DisjointPattern *op2) const;
78  bool identical(const DisjointPattern *op2) const;
79  bool resolvesIntersect(const DisjointPattern *op1,const DisjointPattern *op2) const;
80  static DisjointPattern *restoreDisjoint(const Element *el);
81 };
82 
83 class InstructionPattern : public DisjointPattern { // Matches the instruction bitstream
84  PatternBlock *maskvalue;
85  virtual PatternBlock *getBlock(bool context) const { return context ? (PatternBlock *)0 : maskvalue; }
86 public:
87  InstructionPattern(void) { maskvalue = (PatternBlock *)0; } // For use with restoreXml
88  InstructionPattern(PatternBlock *mv) { maskvalue = mv; }
89  InstructionPattern(bool tf) { maskvalue = new PatternBlock(tf); }
90  PatternBlock *getBlock(void) { return maskvalue; }
91  virtual ~InstructionPattern(void) { if (maskvalue != (PatternBlock *)0) delete maskvalue; }
92  virtual Pattern *simplifyClone(void) const { return new InstructionPattern(maskvalue->clone()); }
93  virtual void shiftInstruction(int4 sa) { maskvalue->shift(sa); }
94  virtual Pattern *doOr(const Pattern *b,int4 sa) const;
95  virtual Pattern *doAnd(const Pattern *b,int4 sa) const;
96  virtual Pattern *commonSubPattern(const Pattern *b,int4 sa) const;
97  virtual bool isMatch(ParserWalker &walker) const { return maskvalue->isInstructionMatch(walker); }
98  virtual bool alwaysTrue(void) const { return maskvalue->alwaysTrue(); }
99  virtual bool alwaysFalse(void) const { return maskvalue->alwaysFalse(); }
100  virtual bool alwaysInstructionTrue(void) const { return maskvalue->alwaysTrue(); }
101  virtual void saveXml(ostream &s) const;
102  virtual void restoreXml(const Element *el);
103 };
104 
105 class ContextPattern : public DisjointPattern { // Matches the context bitstream
106  PatternBlock *maskvalue;
107  virtual PatternBlock *getBlock(bool context) const { return context ? maskvalue : (PatternBlock *)0; }
108 public:
109  ContextPattern(void) { maskvalue = (PatternBlock *)0; } // For use with restoreXml
110  ContextPattern(PatternBlock *mv) { maskvalue = mv; }
111  PatternBlock *getBlock(void) { return maskvalue; }
112  virtual ~ContextPattern(void) { if (maskvalue != (PatternBlock *)0) delete maskvalue; }
113  virtual Pattern *simplifyClone(void) const { return new ContextPattern(maskvalue->clone()); }
114  virtual void shiftInstruction(int4 sa) { } // do nothing
115  virtual Pattern *doOr(const Pattern *b,int4 sa) const;
116  virtual Pattern *doAnd(const Pattern *b,int4 sa) const;
117  virtual Pattern *commonSubPattern(const Pattern *b,int4 sa) const;
118  virtual bool isMatch(ParserWalker &walker) const { return maskvalue->isContextMatch(walker); }
119  virtual bool alwaysTrue(void) const { return maskvalue->alwaysTrue(); }
120  virtual bool alwaysFalse(void) const { return maskvalue->alwaysFalse(); }
121  virtual bool alwaysInstructionTrue(void) const { return true; }
122  virtual void saveXml(ostream &s) const;
123  virtual void restoreXml(const Element *el);
124 };
125 
126 // A pattern with a context piece and an instruction piece
128  ContextPattern *context; // Context piece
129  InstructionPattern *instr; // Instruction piece
130  virtual PatternBlock *getBlock(bool cont) const { return cont ? context->getBlock() : instr->getBlock(); }
131 public:
132  CombinePattern(void) { context = (ContextPattern *)0; instr = (InstructionPattern *)0; }
134  context = con; instr = in; }
135  virtual ~CombinePattern(void);
136  virtual Pattern *simplifyClone(void) const;
137  virtual void shiftInstruction(int4 sa) { instr->shiftInstruction(sa); }
138  virtual bool isMatch(ParserWalker &walker) const;
139  virtual bool alwaysTrue(void) const;
140  virtual bool alwaysFalse(void) const;
141  virtual bool alwaysInstructionTrue(void) const { return instr->alwaysInstructionTrue(); }
142  virtual Pattern *doOr(const Pattern *b,int4 sa) const;
143  virtual Pattern *doAnd(const Pattern *b,int4 sa) const;
144  virtual Pattern *commonSubPattern(const Pattern *b,int4 sa) const;
145  virtual void saveXml(ostream &s) const;
146  virtual void restoreXml(const Element *el);
147 };
148 
149 class OrPattern : public Pattern {
150  vector<DisjointPattern *> orlist;
151 public:
152  OrPattern(void) {} // For use with restoreXml
154  OrPattern(const vector<DisjointPattern *> &list);
155  virtual ~OrPattern(void);
156  virtual Pattern *simplifyClone(void) const;
157  virtual void shiftInstruction(int4 sa);
158  virtual bool isMatch(ParserWalker &walker) const;
159  virtual int4 numDisjoint(void) const { return orlist.size(); }
160  virtual DisjointPattern *getDisjoint(int4 i) const { return orlist[i]; }
161  virtual bool alwaysTrue(void) const;
162  virtual bool alwaysFalse(void) const;
163  virtual bool alwaysInstructionTrue(void) const;
164  virtual Pattern *doOr(const Pattern *b,int4 sa) const;
165  virtual Pattern *doAnd(const Pattern *b,int4 sa) const;
166  virtual Pattern *commonSubPattern(const Pattern *b,int4 sa) const;
167  virtual void saveXml(ostream &s) const;
168  virtual void restoreXml(const Element *el);
169 };
170 
171 #endif
OrPattern
Definition: slghpattern.hh:149
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
PatternBlock
Definition: slghpattern.hh:22
DisjointPattern
Definition: slghpattern.hh:69
Pattern
Definition: slghpattern.hh:51
CombinePattern
Definition: slghpattern.hh:127
InstructionPattern
Definition: slghpattern.hh:83
ParserWalker
Definition: context.hh:124
ContextPattern
Definition: slghpattern.hh:105