Ghidra Decompiler Analysis Engine
slghsymbol.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 __SLGHSYMBOL__
17 #define __SLGHSYMBOL__
18 
19 #include "semantics.hh"
20 #include "slghpatexpress.hh"
21 
22 class SleighBase; // Forward declaration
23 class SleighSymbol {
24  friend class SymbolTable;
25 public:
26  enum symbol_type { space_symbol, token_symbol, userop_symbol, value_symbol, valuemap_symbol,
27  name_symbol, varnode_symbol, varnodelist_symbol, operand_symbol,
28  start_symbol, end_symbol, subtable_symbol, macro_symbol, section_symbol,
29  bitrange_symbol, context_symbol, epsilon_symbol, label_symbol,
30  dummy_symbol };
31 private:
32  string name;
33  uintm id; // Unique id across all symbols
34  uintm scopeid; // Unique id of scope this symbol is in
35 public:
36  SleighSymbol(void) {} // For use with restoreXml
37  SleighSymbol(const string &nm) { name = nm; id = 0; }
38  virtual ~SleighSymbol(void) {}
39  const string &getName(void) const { return name; }
40  uintm getId(void) const { return id; }
41  virtual symbol_type getType(void) const { return dummy_symbol; }
42  virtual void saveXmlHeader(ostream &s) const;
43  void restoreXmlHeader(const Element *el);
44  virtual void saveXml(ostream &s) const {}
45  virtual void restoreXml(const Element *el,SleighBase *trans) {}
46 };
47 
48 struct SymbolCompare {
49  bool operator()(const SleighSymbol *a,const SleighSymbol *b) const {
50  return (a->getName() < b->getName()); }
51 };
52 
53 typedef set<SleighSymbol *,SymbolCompare> SymbolTree;
54 class SymbolScope {
55  friend class SymbolTable;
56  SymbolScope *parent;
57  SymbolTree tree;
58  uintm id;
59 public:
60  SymbolScope(SymbolScope *p,uintm i) { parent = p; id = i; }
61  SymbolScope *getParent(void) const { return parent; }
62  SleighSymbol *addSymbol(SleighSymbol *a);
63  SleighSymbol *findSymbol(const string &nm) const;
64  SymbolTree::const_iterator begin(void) const { return tree.begin(); }
65  SymbolTree::const_iterator end(void) const { return tree.end(); }
66  uintm getId(void) const { return id; }
67  void removeSymbol(SleighSymbol *a) { tree.erase(a); }
68 };
69 
70 class SymbolTable {
71  vector<SleighSymbol *> symbollist;
72  vector<SymbolScope *> table;
73  SymbolScope *curscope;
74  SymbolScope *skipScope(int4 i) const;
75  SleighSymbol *findSymbolInternal(SymbolScope *scope,const string &nm) const;
76  void renumber(void);
77 public:
78  SymbolTable(void) { curscope = (SymbolScope *)0; }
79  ~SymbolTable(void);
80  SymbolScope *getCurrentScope(void) { return curscope; }
81  SymbolScope *getGlobalScope(void) { return table[0]; }
82 
83  void setCurrentScope(SymbolScope *scope) { curscope = scope; }
84  void addScope(void); // Add new scope off of current scope, make it current
85  void popScope(void); // Make parent of current scope current
86  void addGlobalSymbol(SleighSymbol *a);
87  void addSymbol(SleighSymbol *a);
88  SleighSymbol *findSymbol(const string &nm) const { return findSymbolInternal(curscope,nm); }
89  SleighSymbol *findSymbol(const string &nm,int4 skip) const { return findSymbolInternal(skipScope(skip),nm); }
90  SleighSymbol *findGlobalSymbol(const string &nm) const { return findSymbolInternal(table[0],nm); }
91  SleighSymbol *findSymbol(uintm id) const { return symbollist[id]; }
92  void replaceSymbol(SleighSymbol *a,SleighSymbol *b);
93  void saveXml(ostream &s) const;
94  void restoreXml(const Element *el,SleighBase *trans);
95  void restoreSymbolHeader(const Element *el);
96  void purge(void);
97 };
98 
99 class SpaceSymbol : public SleighSymbol {
100  AddrSpace *space;
101 public:
102  SpaceSymbol(AddrSpace *spc) : SleighSymbol(spc->getName()) { space = spc; }
103  AddrSpace *getSpace(void) const { return space; }
104  virtual symbol_type getType(void) const { return space_symbol; }
105 };
106 
107 class TokenSymbol : public SleighSymbol {
108  Token *tok;
109 public:
110  TokenSymbol(Token *t) : SleighSymbol(t->getName()) { tok = t; }
111  ~TokenSymbol(void) { delete tok; }
112  Token *getToken(void) const { return tok; }
113  virtual symbol_type getType(void) const { return token_symbol; }
114 };
115 
116 class SectionSymbol : public SleighSymbol { // Named p-code sections
117  int4 templateid; // Index into the ConstructTpl array
118  int4 define_count; // Number of definitions of this named section
119  int4 ref_count; // Number of references to this named section
120 public:
121  SectionSymbol(const string &nm,int4 id) : SleighSymbol(nm) { templateid=id; define_count=0; ref_count=0; }
122  int4 getTemplateId(void) const { return templateid; }
123  void incrementDefineCount(void) { define_count += 1; }
124  void incrementRefCount(void) { ref_count += 1; }
125  int4 getDefineCount(void) const { return define_count; }
126  int4 getRefCount(void) const { return ref_count; }
127  virtual symbol_type getType(void) const { return section_symbol; }
128  // Not saved or restored
129 };
130 
131 class UserOpSymbol : public SleighSymbol { // A user-defined pcode-op
132  uint4 index;
133 public:
134  UserOpSymbol(void) {} // For use with restoreXml
135  UserOpSymbol(const string &nm) : SleighSymbol(nm) { index = 0; }
136  void setIndex(uint4 ind) { index = ind; }
137  uint4 getIndex(void) const { return index; }
138  virtual symbol_type getType(void) const { return userop_symbol; }
139  virtual void saveXml(ostream &s) const;
140  virtual void saveXmlHeader(ostream &s) const;
141  virtual void restoreXml(const Element *el,SleighBase *trans);
142 };
143 
144 class Constructor; // Forward declaration
145 // This is the central sleigh object
146 class TripleSymbol : public SleighSymbol {
147 public:
148  TripleSymbol(void) {}
149  TripleSymbol(const string &nm) : SleighSymbol(nm) {}
150  virtual Constructor *resolve(ParserWalker &walker) { return (Constructor *)0; }
151  virtual PatternExpression *getPatternExpression(void) const=0;
152  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const=0;
153  virtual int4 getSize(void) const { return 0; } // Size out of context
154  virtual void print(ostream &s,ParserWalker &walker) const=0;
155  virtual void collectLocalValues(vector<uintb> &results) const {}
156 };
157 
158 class FamilySymbol : public TripleSymbol {
159 public:
160  FamilySymbol(void) {}
161  FamilySymbol(const string &nm) : TripleSymbol(nm) {}
162  virtual PatternValue *getPatternValue(void) const=0;
163 };
164 
165 class SpecificSymbol : public TripleSymbol {
166 public:
167  SpecificSymbol(void) {}
168  SpecificSymbol(const string &nm) : TripleSymbol(nm) {}
169  virtual VarnodeTpl *getVarnode(void) const=0;
170 };
171 
172 class PatternlessSymbol : public SpecificSymbol { // Behaves like constant 0 pattern
173  ConstantValue *patexp;
174 public:
175  PatternlessSymbol(void); // For use with restoreXml
176  PatternlessSymbol(const string &nm);
177  virtual ~PatternlessSymbol(void);
178  virtual PatternExpression *getPatternExpression(void) const { return patexp; }
179  virtual void saveXml(ostream &s) const {}
180  virtual void restoreXml(const Element *el,SleighBase *trans) {}
181 };
182 
183 class EpsilonSymbol : public PatternlessSymbol { // Another name for zero pattern/value
184  AddrSpace *const_space;
185 public:
186  EpsilonSymbol(void) {} // For use with restoreXml
187  EpsilonSymbol(const string &nm,AddrSpace *spc) : PatternlessSymbol(nm) { const_space=spc; }
188  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
189  virtual void print(ostream &s,ParserWalker &walker) const;
190  virtual symbol_type getType(void) const { return epsilon_symbol; }
191  virtual VarnodeTpl *getVarnode(void) const;
192  virtual void saveXml(ostream &s) const;
193  virtual void saveXmlHeader(ostream &s) const;
194  virtual void restoreXml(const Element *el,SleighBase *trans);
195 };
196 
197 class ValueSymbol : public FamilySymbol {
198 protected:
199  PatternValue *patval;
200 public:
201  ValueSymbol(void) { patval = (PatternValue *)0; } // For use with restoreXml
202  ValueSymbol(const string &nm,PatternValue *pv);
203  virtual ~ValueSymbol(void);
204  virtual PatternValue *getPatternValue(void) const { return patval; }
205  virtual PatternExpression *getPatternExpression(void) const { return patval; }
206  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
207  virtual void print(ostream &s,ParserWalker &walker) const;
208  virtual symbol_type getType(void) const { return value_symbol; }
209  virtual void saveXml(ostream &s) const;
210  virtual void saveXmlHeader(ostream &s) const;
211  virtual void restoreXml(const Element *el,SleighBase *trans);
212 };
213 
214 class ValueMapSymbol : public ValueSymbol {
215  vector<intb> valuetable;
216  bool tableisfilled;
217  void checkTableFill(void);
218 public:
219  ValueMapSymbol(void) {} // For use with restoreXml
220  ValueMapSymbol(const string &nm,PatternValue *pv,const vector<intb> &vt) : ValueSymbol(nm,pv) { valuetable=vt; checkTableFill(); }
221  virtual Constructor *resolve(ParserWalker &walker);
222  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
223  virtual void print(ostream &s,ParserWalker &walker) const;
224  virtual symbol_type getType(void) const { return valuemap_symbol; }
225  virtual void saveXml(ostream &s) const;
226  virtual void saveXmlHeader(ostream &s) const;
227  virtual void restoreXml(const Element *el,SleighBase *trans);
228 };
229 
230 class NameSymbol : public ValueSymbol {
231  vector<string> nametable;
232  bool tableisfilled;
233  void checkTableFill(void);
234 public:
235  NameSymbol(void) {} // For use with restoreXml
236  NameSymbol(const string &nm,PatternValue *pv,const vector<string> &nt) : ValueSymbol(nm,pv) { nametable=nt; checkTableFill(); }
237  virtual Constructor *resolve(ParserWalker &walker);
238  virtual void print(ostream &s,ParserWalker &walker) const;
239  virtual symbol_type getType(void) const { return name_symbol; }
240  virtual void saveXml(ostream &s) const;
241  virtual void saveXmlHeader(ostream &s) const;
242  virtual void restoreXml(const Element *el,SleighBase *trans);
243 };
244 
245 class VarnodeSymbol : public PatternlessSymbol { // A global varnode
246  VarnodeData fix;
247  bool context_bits;
248 public:
249  VarnodeSymbol(void) {} // For use with restoreXml
250  VarnodeSymbol(const string &nm,AddrSpace *base,uintb offset,int4 size);
251  void markAsContext(void) { context_bits = true; }
252  const VarnodeData &getFixedVarnode(void) const { return fix; }
253  virtual VarnodeTpl *getVarnode(void) const;
254  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
255  virtual int4 getSize(void) const { return fix.size; }
256  virtual void print(ostream &s,ParserWalker &walker) const {
257  s << getName(); }
258  virtual void collectLocalValues(vector<uintb> &results) const;
259  virtual symbol_type getType(void) const { return varnode_symbol; }
260  virtual void saveXml(ostream &s) const;
261  virtual void saveXmlHeader(ostream &s) const;
262  virtual void restoreXml(const Element *el,SleighBase *trans);
263 };
264 
265 class BitrangeSymbol : public SleighSymbol { // A smaller bitrange within a varnode
266  VarnodeSymbol *varsym; // Varnode containing the bitrange
267  uint4 bitoffset; // least significant bit of range
268  uint4 numbits; // number of bits in the range
269 public:
270  BitrangeSymbol(void) {} // For use with restoreXml
271  BitrangeSymbol(const string &nm,VarnodeSymbol *sym,uint4 bitoff,uint4 num)
272  : SleighSymbol(nm) { varsym=sym; bitoffset=bitoff; numbits=num; }
273  VarnodeSymbol *getParentSymbol(void) const { return varsym; }
274  uint4 getBitOffset(void) const { return bitoffset; }
275  uint4 numBits(void) const { return numbits; }
276  virtual symbol_type getType(void) const { return bitrange_symbol; }
277 };
278 
279 class ContextSymbol : public ValueSymbol {
280  VarnodeSymbol *vn;
281  uint4 low,high; // into a varnode
282  bool flow;
283 public:
284  ContextSymbol(void) {} // For use with restoreXml
285  ContextSymbol(const string &nm,ContextField *pate,VarnodeSymbol *v,uint4 l,uint4 h,bool flow);
286  VarnodeSymbol *getVarnode(void) const { return vn; }
287  uint4 getLow(void) const { return low; }
288  uint4 getHigh(void) const { return high; }
289  bool getFlow(void) const { return flow; }
290  virtual symbol_type getType(void) const { return context_symbol; }
291  virtual void saveXml(ostream &s) const;
292  virtual void saveXmlHeader(ostream &s) const;
293  virtual void restoreXml(const Element *el,SleighBase *trans);
294 };
295 
297  vector<VarnodeSymbol *> varnode_table;
298  bool tableisfilled;
299  void checkTableFill(void);
300 public:
301  VarnodeListSymbol(void) {} // For use with restoreXml
302  VarnodeListSymbol(const string &nm,PatternValue *pv,const vector<SleighSymbol *> &vt);
303  virtual Constructor *resolve(ParserWalker &walker);
304  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
305  virtual int4 getSize(void) const;
306  virtual void print(ostream &s,ParserWalker &walker) const;
307  virtual symbol_type getType(void) const { return varnodelist_symbol; }
308  virtual void saveXml(ostream &s) const;
309  virtual void saveXmlHeader(ostream &s) const;
310  virtual void restoreXml(const Element *el,SleighBase *trans);
311 };
312 
314  friend class Constructor;
315  friend class OperandEquation;
316 public:
317  enum { code_address=1, offset_irrel=2, variable_len=4, marked=8 };
318 private:
319  uint4 reloffset; // Relative offset
320  int4 offsetbase; // Base operand to which offset is relative (-1=constructor start)
321  int4 minimumlength; // Minimum size of operand (within instruction tokens)
322  int4 hand; // Handle index
323  OperandValue *localexp;
324  TripleSymbol *triple; // Defining symbol
325  PatternExpression *defexp; // OR defining expression
326  uint4 flags;
327  void setVariableLength(void) { flags |= variable_len; }
328  bool isVariableLength(void) const { return ((flags&variable_len)!=0); }
329 public:
330  OperandSymbol(void) {} // For use with restoreXml
331  OperandSymbol(const string &nm,int4 index,Constructor *ct);
332  uint4 getRelativeOffset(void) const { return reloffset; }
333  int4 getOffsetBase(void) const { return offsetbase; }
334  int4 getMinimumLength(void) const { return minimumlength; }
335  PatternExpression *getDefiningExpression(void) const { return defexp; }
336  TripleSymbol *getDefiningSymbol(void) const { return triple; }
337  int4 getIndex(void) const { return hand; }
338  void defineOperand(PatternExpression *pe);
339  void defineOperand(TripleSymbol *tri);
340  void setCodeAddress(void) { flags |= code_address; }
341  bool isCodeAddress(void) const { return ((flags&code_address)!=0); }
342  void setOffsetIrrelevant(void) { flags |= offset_irrel; }
343  bool isOffsetIrrelevant(void) const { return ((flags&offset_irrel)!=0); }
344  void setMark(void) { flags |= marked; }
345  void clearMark(void) { flags &= ~((uint4)marked); }
346  bool isMarked(void) const { return ((flags&marked)!=0); }
347  virtual ~OperandSymbol(void);
348  virtual VarnodeTpl *getVarnode(void) const;
349  virtual PatternExpression *getPatternExpression(void) const { return localexp; }
350  virtual void getFixedHandle(FixedHandle &hnd,ParserWalker &walker) const;
351  virtual int4 getSize(void) const;
352  virtual void print(ostream &s,ParserWalker &walker) const;
353  virtual void collectLocalValues(vector<uintb> &results) const;
354  virtual symbol_type getType(void) const { return operand_symbol; }
355  virtual void saveXml(ostream &s) const;
356  virtual void saveXmlHeader(ostream &s) const;
357  virtual void restoreXml(const Element *el,SleighBase *trans);
358 };
359 
360 class StartSymbol : public SpecificSymbol {
361  AddrSpace *const_space;
362  PatternExpression *patexp;
363 public:
364  StartSymbol(void) { patexp = (PatternExpression *)0; } // For use with restoreXml
365  StartSymbol(const string &nm,AddrSpace *cspc);
366  virtual ~StartSymbol(void);
367  virtual VarnodeTpl *getVarnode(void) const;
368  virtual PatternExpression *getPatternExpression(void) const { return patexp; }
369  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
370  virtual void print(ostream &s,ParserWalker &walker) const;
371  virtual symbol_type getType(void) const { return start_symbol; }
372  virtual void saveXml(ostream &s) const;
373  virtual void saveXmlHeader(ostream &s) const;
374  virtual void restoreXml(const Element *el,SleighBase *trans);
375 };
376 
377 class EndSymbol : public SpecificSymbol {
378  AddrSpace *const_space;
379  PatternExpression *patexp;
380 public:
381  EndSymbol(void) { patexp = (PatternExpression *)0; } // For use with restoreXml
382  EndSymbol(const string &nm,AddrSpace *cspc);
383  virtual ~EndSymbol(void);
384  virtual VarnodeTpl *getVarnode(void) const;
385  virtual PatternExpression *getPatternExpression(void) const { return patexp; }
386  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
387  virtual void print(ostream &s,ParserWalker &walker) const;
388  virtual symbol_type getType(void) const { return end_symbol; }
389  virtual void saveXml(ostream &s) const;
390  virtual void saveXmlHeader(ostream &s) const;
391  virtual void restoreXml(const Element *el,SleighBase *trans);
392 };
393 
395  AddrSpace *const_space;
396 public:
397  FlowDestSymbol(void) {} // For use with restoreXml
398  FlowDestSymbol(const string &nm,AddrSpace *cspc);
399  virtual VarnodeTpl *getVarnode(void) const;
400  virtual PatternExpression *getPatternExpression(void) const { throw SleighError("Cannot use symbol in pattern"); }
401  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
402  virtual void print(ostream &s,ParserWalker &walker) const;
403  virtual symbol_type getType(void) const { return start_symbol; }
404  virtual void saveXml(ostream &s) const;
405  virtual void saveXmlHeader(ostream &s) const;
406  virtual void restoreXml(const Element *el,SleighBase *trans);
407 };
408 
410  AddrSpace *const_space;
411 public:
412  FlowRefSymbol(void) {} // For use with restoreXml
413  FlowRefSymbol(const string &nm,AddrSpace *cspc);
414  virtual VarnodeTpl *getVarnode(void) const;
415  virtual PatternExpression *getPatternExpression(void) const { throw SleighError("Cannot use symbol in pattern"); }
416  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const;
417  virtual void print(ostream &s,ParserWalker &walker) const;
418  virtual symbol_type getType(void) const { return start_symbol; }
419  virtual void saveXml(ostream &s) const;
420  virtual void saveXmlHeader(ostream &s) const;
421  virtual void restoreXml(const Element *el,SleighBase *trans);
422 };
423 
424 class ContextChange { // Change to context command
425 public:
426  virtual ~ContextChange(void) {}
427  virtual void validate(void) const=0;
428  virtual void saveXml(ostream &s) const=0;
429  virtual void restoreXml(const Element *el,SleighBase *trans)=0;
430  virtual void apply(ParserWalkerChange &walker) const=0;
431  virtual ContextChange *clone(void) const=0;
432 };
433 
434 class ContextOp : public ContextChange {
435  PatternExpression *patexp; // Expression determining value
436  int4 num; // index of word containing context variable to set
437  uintm mask; // Mask off size of variable
438  int4 shift; // Number of bits to shift value into place
439 public:
440  ContextOp(int4 startbit,int4 endbit,PatternExpression *pe);
441  ContextOp(void) {} // For use with restoreXml
442  virtual ~ContextOp(void) { PatternExpression::release(patexp); }
443  virtual void validate(void) const;
444  virtual void saveXml(ostream &s) const;
445  virtual void restoreXml(const Element *el,SleighBase *trans);
446  virtual void apply(ParserWalkerChange &walker) const;
447  virtual ContextChange *clone(void) const;
448 };
449 
450 class ContextCommit : public ContextChange {
451  TripleSymbol *sym;
452  int4 num; // Index of word containing context commit
453  uintm mask; // mask of bits in word being committed
454  bool flow; // Whether the context "flows" from the point of change
455 public:
456  ContextCommit(void) {} // For use with restoreXml
457  ContextCommit(TripleSymbol *s,int4 sbit,int4 ebit,bool fl);
458  virtual void validate(void) const {}
459  virtual void saveXml(ostream &s) const;
460  virtual void restoreXml(const Element *el,SleighBase *trans);
461  virtual void apply(ParserWalkerChange &walker) const;
462  virtual ContextChange *clone(void) const;
463 };
464 
465 class SubtableSymbol;
466 class Constructor { // This is NOT a symbol
467  TokenPattern *pattern;
468  SubtableSymbol *parent;
469  PatternEquation *pateq;
470  vector<OperandSymbol *> operands;
471  vector<string> printpiece;
472  vector<ContextChange *> context; // Context commands
473  ConstructTpl *templ; // The main p-code section
474  vector<ConstructTpl *> namedtempl; // Other named p-code sections
475  int4 minimumlength; // Minimum length taken up by this constructor in bytes
476  uintm id; // Unique id of constructor within subtable
477  int4 firstwhitespace; // Index of first whitespace piece in -printpiece-
478  int4 flowthruindex; // if >=0 then print only a single operand no markup
479  int4 lineno;
480  int4 src_index; //source file index
481  mutable bool inerror; // An error is associated with this Constructor
482  void orderOperands(void);
483 public:
484  Constructor(void); // For use with restoreXml
486  ~Constructor(void);
487  TokenPattern *buildPattern(ostream &s);
488  TokenPattern *getPattern(void) const { return pattern; }
489  void setMinimumLength(int4 l) { minimumlength = l; }
490  int4 getMinimumLength(void) const { return minimumlength; }
491  void setId(uintm i) { id = i; }
492  uintm getId(void) const { return id; }
493  void setLineno(int4 ln) { lineno = ln; }
494  int4 getLineno(void) const { return lineno; }
495  void setSrcIndex(int4 index) {src_index = index;}
496  int4 getSrcIndex(void) {return src_index;}
497  void addContext(const vector<ContextChange *> &vec) { context = vec; }
498  void addOperand(OperandSymbol *sym);
499  void addInvisibleOperand(OperandSymbol *sym);
500  void addSyntax(const string &syn);
501  void addEquation(PatternEquation *pe);
502  void setMainSection(ConstructTpl *tpl) { templ = tpl; }
503  void setNamedSection(ConstructTpl *tpl,int4 id);
504  SubtableSymbol *getParent(void) const { return parent; }
505  int4 getNumOperands(void) const { return operands.size(); }
506  OperandSymbol *getOperand(int4 i) const { return operands[i]; }
507  PatternEquation *getPatternEquation(void) const { return pateq; }
508  ConstructTpl *getTempl(void) const { return templ; }
509  ConstructTpl *getNamedTempl(int4 secnum) const;
510  int4 getNumSections(void) const { return namedtempl.size(); }
511  void printInfo(ostream &s) const;
512  void print(ostream &s,ParserWalker &pos) const;
513  void printMnemonic(ostream &s,ParserWalker &walker) const;
514  void printBody(ostream &s,ParserWalker &walker) const;
515  void removeTrailingSpace(void);
516  void applyContext(ParserWalkerChange &walker) const {
517  vector<ContextChange *>::const_iterator iter;
518  for(iter=context.begin();iter!=context.end();++iter)
519  (*iter)->apply(walker);
520  }
521  void markSubtableOperands(vector<int4> &check) const;
522  void collectLocalExports(vector<uintb> &results) const;
523  void setError(bool val) const { inerror = val; }
524  bool isError(void) const { return inerror; }
525  bool isRecursive(void) const;
526  void saveXml(ostream &s) const;
527  void restoreXml(const Element *el,SleighBase *trans);
528 };
529 
531  vector<pair<Constructor *, Constructor *> > identerrors;
532  vector<pair<Constructor *, Constructor *> > conflicterrors;
533 public:
534  void identicalPattern(Constructor *a,Constructor *b);
535  void conflictingPattern(Constructor *a,Constructor *b);
536  const vector<pair<Constructor *, Constructor *> > &getIdentErrors(void) const { return identerrors; }
537  const vector<pair<Constructor *, Constructor *> > &getConflictErrors(void) const { return conflicterrors; }
538 };
539 
541  vector<pair<DisjointPattern *,Constructor *> > list;
542  vector<DecisionNode *> children;
543  int4 num; // Total number of patterns we distinguish
544  bool contextdecision; // True if this is decision based on context
545  int4 startbit,bitsize; // Bits in the stream on which to base the decision
546  DecisionNode *parent;
547  void chooseOptimalField(void);
548  double getScore(int4 low,int4 size,bool context);
549  int4 getNumFixed(int4 low,int4 size,bool context);
550  int4 getMaximumLength(bool context);
551  void consistentValues(vector<uint4> &bins,DisjointPattern *pat);
552 public:
553  DecisionNode(void) {} // For use with restoreXml
555  ~DecisionNode(void);
556  Constructor *resolve(ParserWalker &walker) const;
557  void addConstructorPair(const DisjointPattern *pat,Constructor *ct);
558  void split(DecisionProperties &props);
559  void orderPatterns(DecisionProperties &props);
560  void saveXml(ostream &s) const;
561  void restoreXml(const Element *el,DecisionNode *par,SubtableSymbol *sub);
562 };
563 
564 class SubtableSymbol : public TripleSymbol {
565  TokenPattern *pattern;
566  bool beingbuilt,errors;
567  vector<Constructor *> construct; // All the Constructors in this table
568  DecisionNode *decisiontree;
569 public:
570  SubtableSymbol(void) { pattern = (TokenPattern *)0; decisiontree = (DecisionNode *)0; } // For use with restoreXml
571  SubtableSymbol(const string &nm);
572  virtual ~SubtableSymbol(void);
573  bool isBeingBuilt(void) const { return beingbuilt; }
574  bool isError(void) const { return errors; }
575  void addConstructor(Constructor *ct) { ct->setId(construct.size()); construct.push_back(ct); }
576  void buildDecisionTree(DecisionProperties &props);
577  TokenPattern *buildPattern(ostream &s);
578  TokenPattern *getPattern(void) const { return pattern; }
579  int4 getNumConstructors(void) const { return construct.size(); }
580  Constructor *getConstructor(uintm id) const { return construct[id]; }
581  virtual Constructor *resolve(ParserWalker &walker) { return decisiontree->resolve(walker); }
582  virtual PatternExpression *getPatternExpression(void) const { throw SleighError("Cannot use subtable in expression"); }
583  virtual void getFixedHandle(FixedHandle &hand,ParserWalker &walker) const {
584  throw SleighError("Cannot use subtable in expression"); }
585  virtual int4 getSize(void) const { return -1; }
586  virtual void print(ostream &s,ParserWalker &walker) const {
587  throw SleighError("Cannot use subtable in expression"); }
588  virtual void collectLocalValues(vector<uintb> &results) const;
589  virtual symbol_type getType(void) const { return subtable_symbol; }
590  virtual void saveXml(ostream &s) const;
591  virtual void saveXmlHeader(ostream &s) const;
592  virtual void restoreXml(const Element *el,SleighBase *trans);
593 };
594 
595 class MacroSymbol : public SleighSymbol { // A user-defined pcode-macro
596  int4 index;
597  ConstructTpl *construct;
598  vector<OperandSymbol *> operands;
599 public:
600  MacroSymbol(const string &nm,int4 i) : SleighSymbol(nm) { index = i; construct = (ConstructTpl *)0; }
601  int4 getIndex(void) const { return index; }
602  void setConstruct(ConstructTpl *ct) { construct = ct; }
603  ConstructTpl *getConstruct(void) const { return construct; }
604  void addOperand(OperandSymbol *sym) { operands.push_back(sym); }
605  int4 getNumOperands(void) const { return operands.size(); }
606  OperandSymbol *getOperand(int4 i) const { return operands[i]; }
607  virtual ~MacroSymbol(void) { if (construct != (ConstructTpl *)0) delete construct; }
608  virtual symbol_type getType(void) const { return macro_symbol; }
609 };
610 
611 class LabelSymbol : public SleighSymbol { // A branch label
612  uint4 index; // Local 1 up index of label
613  bool isplaced; // Has the label been placed (not just referenced)
614  uint4 refcount; // Number of references to this label
615 public:
616  LabelSymbol(const string &nm,uint4 i) : SleighSymbol(nm) { index = i; refcount = 0; isplaced=false; }
617  uint4 getIndex(void) const { return index; }
618  void incrementRefCount(void) { refcount += 1; }
619  uint4 getRefCount(void) const { return refcount; }
620  void setPlaced(void) { isplaced = true; }
621  bool isPlaced(void) const { return isplaced; }
622  virtual symbol_type getType(void) const { return label_symbol; }
623 };
624 
625 #endif
ContextSymbol
Definition: slghsymbol.hh:279
VarnodeListSymbol
Definition: slghsymbol.hh:296
PatternExpression
Definition: slghpatexpress.hh:56
Constructor
Definition: slghsymbol.hh:466
AddrSpace
A region where processor data is stored.
Definition: space.hh:73
ContextCommit
Definition: slghsymbol.hh:450
SleighError
Definition: context.hh:176
PatternEquation
Definition: slghpatexpress.hh:336
TripleSymbol
Definition: slghsymbol.hh:146
VarnodeTpl
Definition: semantics.hh:77
ConstantValue
Definition: slghpatexpress.hh:128
OperandValue
Definition: slghpatexpress.hh:170
VarnodeSymbol
Definition: slghsymbol.hh:245
OperandSymbol
Definition: slghsymbol.hh:313
ValueSymbol
Definition: slghsymbol.hh:197
NameSymbol
Definition: slghsymbol.hh:230
ValueMapSymbol
Definition: slghsymbol.hh:214
UserOpSymbol
Definition: slghsymbol.hh:131
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
Token
Definition: context.hh:22
TokenPattern
Definition: slghpatexpress.hh:21
ContextOp
Definition: slghsymbol.hh:434
FlowRefSymbol
Definition: slghsymbol.hh:409
BitrangeSymbol
Definition: slghsymbol.hh:265
SubtableSymbol
Definition: slghsymbol.hh:564
VarnodeData::size
uint4 size
The number of bytes in the location.
Definition: pcoderaw.hh:36
DecisionProperties
Definition: slghsymbol.hh:530
PatternValue
Definition: slghpatexpress.hh:78
DisjointPattern
Definition: slghpattern.hh:69
ParserWalkerChange
Definition: context.hh:163
OperandEquation
Definition: slghpatexpress.hh:351
SectionSymbol
Definition: slghsymbol.hh:116
SymbolCompare
Definition: slghsymbol.hh:48
AddrSpace::getName
const string & getName(void) const
Get the name.
Definition: space.hh:264
ContextField
Definition: slghpatexpress.hh:108
LabelSymbol
Definition: slghsymbol.hh:611
EpsilonSymbol
Definition: slghsymbol.hh:183
FamilySymbol
Definition: slghsymbol.hh:158
SymbolScope
Definition: slghsymbol.hh:54
SymbolTable
Definition: slghsymbol.hh:70
TokenSymbol
Definition: slghsymbol.hh:107
SpecificSymbol
Definition: slghsymbol.hh:165
ContextChange
Definition: slghsymbol.hh:424
SleighBase
Common core of classes that read or write SLEIGH specification files natively.
Definition: sleighbase.hh:57
MacroSymbol
Definition: slghsymbol.hh:595
FixedHandle
Definition: context.hh:35
StartSymbol
Definition: slghsymbol.hh:360
VarnodeData
Data defining a specific memory location.
Definition: pcoderaw.hh:33
DecisionNode
Definition: slghsymbol.hh:540
FlowDestSymbol
Definition: slghsymbol.hh:394
ConstructTpl
Definition: semantics.hh:161
ParserWalker
Definition: context.hh:124
SleighSymbol
Definition: slghsymbol.hh:23
PatternlessSymbol
Definition: slghsymbol.hh:172
SpaceSymbol
Definition: slghsymbol.hh:99
EndSymbol
Definition: slghsymbol.hh:377