16 #ifndef __SLGHSYMBOL__
17 #define __SLGHSYMBOL__
19 #include "semantics.hh"
20 #include "slghpatexpress.hh"
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,
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 {}
50 return (a->getName() < b->getName()); }
53 typedef set<SleighSymbol *,SymbolCompare> SymbolTree;
61 SymbolScope *getParent(
void)
const {
return parent; }
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; }
71 vector<SleighSymbol *> symbollist;
72 vector<SymbolScope *> table;
80 SymbolScope *getCurrentScope(
void) {
return curscope; }
81 SymbolScope *getGlobalScope(
void) {
return table[0]; }
83 void setCurrentScope(
SymbolScope *scope) { curscope = scope; }
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]; }
93 void saveXml(ostream &s)
const;
95 void restoreSymbolHeader(
const Element *el);
103 AddrSpace *getSpace(
void)
const {
return space; }
104 virtual symbol_type getType(
void)
const {
return space_symbol; }
112 Token *getToken(
void)
const {
return tok; }
113 virtual symbol_type getType(
void)
const {
return token_symbol; }
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; }
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;
153 virtual int4 getSize(
void)
const {
return 0; }
154 virtual void print(ostream &s,
ParserWalker &walker)
const=0;
155 virtual void collectLocalValues(vector<uintb> &results)
const {}
179 virtual void saveXml(ostream &s)
const {}
189 virtual void print(ostream &s,
ParserWalker &walker)
const;
190 virtual symbol_type getType(
void)
const {
return epsilon_symbol; }
192 virtual void saveXml(ostream &s)
const;
193 virtual void saveXmlHeader(ostream &s)
const;
204 virtual PatternValue *getPatternValue(
void)
const {
return patval; }
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;
215 vector<intb> valuetable;
217 void checkTableFill(
void);
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;
231 vector<string> nametable;
233 void checkTableFill(
void);
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;
251 void markAsContext(
void) { context_bits =
true; }
252 const VarnodeData &getFixedVarnode(
void)
const {
return fix; }
255 virtual int4 getSize(
void)
const {
return fix.
size; }
256 virtual void print(ostream &s,
ParserWalker &walker)
const {
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;
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; }
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;
297 vector<VarnodeSymbol *> varnode_table;
299 void checkTableFill(
void);
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;
317 enum { code_address=1, offset_irrel=2, variable_len=4, marked=8 };
327 void setVariableLength(
void) { flags |= variable_len; }
328 bool isVariableLength(
void)
const {
return ((flags&variable_len)!=0); }
332 uint4 getRelativeOffset(
void)
const {
return reloffset; }
333 int4 getOffsetBase(
void)
const {
return offsetbase; }
334 int4 getMinimumLength(
void)
const {
return minimumlength; }
336 TripleSymbol *getDefiningSymbol(
void)
const {
return triple; }
337 int4 getIndex(
void)
const {
return hand; }
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); }
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;
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;
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;
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;
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;
427 virtual void validate(
void)
const=0;
428 virtual void saveXml(ostream &s)
const=0;
442 virtual ~
ContextOp(
void) { PatternExpression::release(patexp); }
443 virtual void validate(
void)
const;
444 virtual void saveXml(ostream &s)
const;
458 virtual void validate(
void)
const {}
459 virtual void saveXml(ostream &s)
const;
470 vector<OperandSymbol *> operands;
471 vector<string> printpiece;
472 vector<ContextChange *> context;
474 vector<ConstructTpl *> namedtempl;
477 int4 firstwhitespace;
481 mutable bool inerror;
482 void orderOperands(
void);
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; }
500 void addSyntax(
const string &syn);
505 int4 getNumOperands(
void)
const {
return operands.size(); }
506 OperandSymbol *getOperand(int4 i)
const {
return operands[i]; }
510 int4 getNumSections(
void)
const {
return namedtempl.size(); }
511 void printInfo(ostream &s)
const;
513 void printMnemonic(ostream &s,
ParserWalker &walker)
const;
515 void removeTrailingSpace(
void);
517 vector<ContextChange *>::const_iterator iter;
518 for(iter=context.begin();iter!=context.end();++iter)
519 (*iter)->apply(walker);
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;
531 vector<pair<Constructor *, Constructor *> > identerrors;
532 vector<pair<Constructor *, Constructor *> > conflicterrors;
536 const vector<pair<Constructor *, Constructor *> > &getIdentErrors(
void)
const {
return identerrors; }
537 const vector<pair<Constructor *, Constructor *> > &getConflictErrors(
void)
const {
return conflicterrors; }
541 vector<pair<DisjointPattern *,Constructor *> > list;
542 vector<DecisionNode *> children;
544 bool contextdecision;
545 int4 startbit,bitsize;
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);
560 void saveXml(ostream &s)
const;
566 bool beingbuilt,errors;
567 vector<Constructor *> construct;
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); }
578 TokenPattern *getPattern(
void)
const {
return pattern; }
579 int4 getNumConstructors(
void)
const {
return construct.size(); }
580 Constructor *getConstructor(uintm
id)
const {
return construct[id]; }
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;
598 vector<OperandSymbol *> operands;
601 int4 getIndex(
void)
const {
return index; }
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]; }
608 virtual symbol_type getType(
void)
const {
return macro_symbol; }
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; }